Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,17 @@ import { ConduitError, RateLimitError, InsufficientBalanceError, StreamErrorCode
const _warnedDeprecations = new Set<string>();

/**
* 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);
Expand Down
20 changes: 20 additions & 0 deletions src/tests/streams-deprecation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});