Skip to content

Add forceCloudflareWorkers option to bypass runtime inference#17

Merged
luke-owen-crowdhandler merged 4 commits into
mainfrom
feature/force-cloudflare-worker
May 13, 2026
Merged

Add forceCloudflareWorkers option to bypass runtime inference#17
luke-owen-crowdhandler merged 4 commits into
mainfrom
feature/force-cloudflare-worker

Conversation

@carl-wells-crowdhandler
Copy link
Copy Markdown
Contributor

isCloudflareWorkers is now a function that consults a module-level override before falling back to the navigator.userAgent check. Callers that already know they're on Workers can set options.forceCloudflareWorkers: true on init() to skip inference and guarantee the fetch/await-recordPerformance code paths are used.

@luke-owen-crowdhandler
Copy link
Copy Markdown
Contributor

@copilot

luke-owen-crowdhandler and others added 2 commits May 13, 2026 10:56
Adds getCloudflareWorkersOverride() to common/runtime.ts and logs the
runtime decision in init() when a Workers context is present, e.g.
`[CH] Cloudflare Workers runtime: true (via override)` vs `(via
navigator inference)`. Helps integrators confirm at a glance which
signal drove the transport decision.

Bumps to 2.4.2.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@luke-owen-crowdhandler
Copy link
Copy Markdown
Contributor

@copilot Are you able to review this PR?

Copy link
Copy Markdown
Contributor

Copilot AI commented May 13, 2026

@copilot Are you able to review this PR?

Sure, here's my review:


Overall: The approach is clean — converting isCloudflareWorkers from a module-level boolean to a function backed by a nullable override is a good pattern. The call-sites in BaseClient and Gatekeeper were updated correctly and the TypeScript types/JSDoc/dist are all in sync. A few things worth addressing:


🐛 Bug: module-level override is never reset between init() calls

cloudflareWorkersOverride in runtime.ts is module-level mutable state. Once init({ options: { forceCloudflareWorkers: true } }) has been called, every subsequent init() call in the same process — even without forceCloudflareWorkers — will still see isCloudflareWorkers() === true because nothing clears the override. This affects test suites and any scenario where init() is called more than once (e.g. unit tests, module re-use across environments).

Suggested fix in init.ts:

- if (config.options?.forceCloudflareWorkers === true) {
-   setCloudflareWorkersOverride(true);
- }
+ // Unconditionally sync the override so repeated init() calls don't bleed state.
+ setCloudflareWorkersOverride(
+   config.options?.forceCloudflareWorkers === true ? true : null
+ );

⚠️ Minor: getCloudflareWorkersOverride is exported from runtime.ts unnecessarily

getCloudflareWorkersOverride is exported and therefore appears in dist/types/common/runtime.d.ts, but it isn't re-exported from index.ts. It was introduced solely for the debug-log line inside init.ts. Unless you intend it to be a public API, making it unexported (or at minimum keeping it unexported from the main barrel) avoids accidentally locking in the internal implementation.


ℹ️ Suggestion: add unit tests for the new behaviour

The repo has Jest/ts-jest configured (npm test). A small test file covering:

  • isCloudflareWorkers() returns true when the override is set via setCloudflareWorkersOverride(true)
  • isCloudflareWorkers() falls back to navigator detection when the override is null
  • Calling init() twice — once with forceCloudflareWorkers: true, once without — leaves isCloudflareWorkers() as false (i.e. the reset-on-each-call fix above)

…would give confidence this doesn't regress silently.

Previously, calling init() with forceCloudflareWorkers:true would leave
the module-level override set indefinitely. Subsequent init() calls
without the option would silently inherit the forced value because
nothing cleared it. Now sync unconditionally — set to true when
explicitly forced, otherwise reset to null so navigator inference takes
over. Fixes state bleed across init() calls in test suites and any
multi-init scenario.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@luke-owen-crowdhandler luke-owen-crowdhandler merged commit 8405add into main May 13, 2026
@luke-owen-crowdhandler luke-owen-crowdhandler deleted the feature/force-cloudflare-worker branch May 13, 2026 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants