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
21 changes: 14 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ export const createClient = <

// Check for Node.js <= 18 deprecation
function shouldShowDeprecationWarning(): boolean {
if (
typeof window !== 'undefined' ||
typeof process === 'undefined' ||
process.version === undefined ||
process.version === null
) {
// Skip in browser environments
if (typeof window !== 'undefined') {
return false
}

const versionMatch = process.version.match(/^v(\d+)\./)
// Skip if process is not available (e.g., Edge Runtime)
if (typeof process === 'undefined') {
return false
}

// Use dynamic property access to avoid Next.js Edge Runtime static analysis warnings
const processVersion = (process as any)['version']
if (processVersion === undefined || processVersion === null) {
return false
}

const versionMatch = processVersion.match(/^v(\d+)\./)
if (!versionMatch) {
return false
}
Expand Down
143 changes: 143 additions & 0 deletions test/unit/deprecation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* @jest-environment node
*/

// Make this file a module to satisfy TypeScript's isolatedModules
export {}

describe('Node.js deprecation warning', () => {
const originalProcess = global.process
const originalWindow = global.window
const originalConsoleWarn = console.warn

beforeEach(() => {
// Reset modules to re-run the deprecation check
jest.resetModules()
// Mock console.warn
console.warn = jest.fn()
})

afterEach(() => {
// Restore original values
global.process = originalProcess
global.window = originalWindow
console.warn = originalConsoleWarn
jest.resetModules()
})

it('should not show warning in browser environment', () => {
// Simulate browser environment
global.window = {} as any

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

// Note: We can't easily test "process is undefined" because dependencies like ws require it
// The code handles it correctly with typeof process === 'undefined' check
// In real Edge Runtime, the module loading context is different

it('should not show warning when process.version is undefined', () => {
// Process exists but version is undefined
// Only mock the version property to avoid TTYWRAP warnings
Object.defineProperty(global.process, 'version', {
value: undefined,
configurable: true,
})

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

it('should not show warning when process.version is null', () => {
// Process exists but version is null
// Only mock the version property to avoid TTYWRAP warnings
Object.defineProperty(global.process, 'version', {
value: null,
configurable: true,
})

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

it('should show warning for Node.js 18', () => {
Object.defineProperty(global.process, 'version', {
value: 'v18.0.0',
configurable: true,
})
delete (global as any).window

require('../../src/index')

expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining('Node.js 18 and below are deprecated')
)
})

it('should show warning for Node.js 16', () => {
Object.defineProperty(global.process, 'version', {
value: 'v16.14.0',
configurable: true,
})
delete (global as any).window

require('../../src/index')

expect(console.warn).toHaveBeenCalledWith(
expect.stringContaining('Node.js 18 and below are deprecated')
)
})

it('should not show warning for Node.js 20', () => {
Object.defineProperty(global.process, 'version', {
value: 'v20.0.0',
configurable: true,
})
delete (global as any).window

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

it('should not show warning for Node.js 22', () => {
Object.defineProperty(global.process, 'version', {
value: 'v22.0.0',
configurable: true,
})
delete (global as any).window

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

it('should handle invalid version format gracefully', () => {
Object.defineProperty(global.process, 'version', {
value: 'invalid-version',
configurable: true,
})
delete (global as any).window

require('../../src/index')

expect(console.warn).not.toHaveBeenCalled()
})

it('should handle version without v prefix', () => {
Object.defineProperty(global.process, 'version', {
value: '18.0.0',
configurable: true,
})
delete (global as any).window

require('../../src/index')

// Should not match the regex and thus not show warning
expect(console.warn).not.toHaveBeenCalled()
})
})
Loading