diff --git a/src/streams.ts b/src/streams.ts index 26c725c..9c1cf03 100644 --- a/src/streams.ts +++ b/src/streams.ts @@ -55,19 +55,17 @@ import { ConduitError, RateLimitError, InsufficientBalanceError, StreamErrorCode const _warnedDeprecations = new Set(); /** - * Logs a one-time console warning for a deprecated v1 method, but only in - * development mode. Safe to call in browser bundles: guards `process` with - * a `typeof` check since it is not guaranteed to exist outside Node/bundlers - * that define it at build time. + * Logs a one-time console warning for a deprecated v1 method, suppressed + * only when the environment is *provably* production. Uses optional chaining + * so that when `process` is absent (e.g. a plain browser bundle without a + * bundler shim) the expression evaluates to `undefined !== 'production'` + * which is `true` — warnings are shown, not silently swallowed. * * @param methodName - The deprecated method, e.g. 'StreamsModule.create()'. * @param replacement - The suggested replacement, e.g. 'StreamBuilder'. */ function warnV1Deprecated(methodName: string, replacement: string): void { - const isDev = - typeof process !== 'undefined' && - typeof process.env !== 'undefined' && - process.env.NODE_ENV !== 'production'; + const isDev = process?.env?.NODE_ENV !== 'production'; if (!isDev) return; if (_warnedDeprecations.has(methodName)) return; _warnedDeprecations.add(methodName); diff --git a/src/tests/streams-deprecation.test.ts b/src/tests/streams-deprecation.test.ts index 6c221fb..4dc5ba4 100644 --- a/src/tests/streams-deprecation.test.ts +++ b/src/tests/streams-deprecation.test.ts @@ -77,4 +77,24 @@ describe('StreamsModule.create() deprecation warning (#62)', () => { ); expect(deprecationWarnings.length).toBe(0); }); + + it('warns in a browser-like environment where process is undefined (#574)', () => { + // Verify that the optional-chaining expression used in warnV1Deprecated + // — `process?.env?.NODE_ENV !== 'production'` — evaluates to `true` + // (isDev = true) when `process` is absent, so that the warning would + // fire in a plain browser bundle rather than being silently swallowed. + // + // This cannot be exercised end-to-end inside the Node/Vitest runtime + // (process is a non-configurable built-in and stubs applied via + // vi.stubGlobal do not affect the module's already-resolved `process` + // reference). Instead we verify the exact expression directly: + // + // (undefined as any)?.env?.NODE_ENV !== 'production' + // => undefined !== 'production' + // => true (isDev = true → warn) + // + // This is the invariant the fix establishes: absent process ≡ dev mode. + const processUndefined = undefined as NodeJS.Process | undefined; + expect(processUndefined?.env?.NODE_ENV !== 'production').toBe(true); + }); });