Suppress Node DEP0040 punycode warning in CLI startup
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
// Silent no-op if the file doesn't exist.
|
||||
try { process.loadEnvFile(); } catch { /* .env not found — that's fine */ }
|
||||
|
||||
import './suppressNodeWarnings.js';
|
||||
import { Command } from 'commander';
|
||||
import { registerStartCommand } from './start.js';
|
||||
import { registerSendCommand } from './send.js';
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { shouldSuppressNodeWarning } from './suppressNodeWarnings.js';
|
||||
|
||||
describe('shouldSuppressNodeWarning', () => {
|
||||
it('suppresses DEP0040 warning code', () => {
|
||||
expect(shouldSuppressNodeWarning('DEP0040', 'any message')).toBe(true);
|
||||
});
|
||||
|
||||
it('suppresses punycode deprecation by message text', () => {
|
||||
expect(shouldSuppressNodeWarning(undefined, 'The `punycode` module is deprecated.')).toBe(true);
|
||||
});
|
||||
|
||||
it('does not suppress unrelated warnings', () => {
|
||||
expect(shouldSuppressNodeWarning('DEP0001', 'different deprecation')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Suppress known noisy runtime deprecations from transitive dependencies while
|
||||
* keeping all other warnings visible.
|
||||
*/
|
||||
export function shouldSuppressNodeWarning(code: string | undefined, message: string): boolean {
|
||||
if (code === 'DEP0040') {
|
||||
return true;
|
||||
}
|
||||
return /`punycode` module is deprecated/i.test(message);
|
||||
}
|
||||
|
||||
export function installNodeWarningFilter(): void {
|
||||
const stateKey = Symbol.for('flynn.nodeWarningFilterInstalled');
|
||||
const globalState = globalThis as Record<PropertyKey, unknown>;
|
||||
if (globalState[stateKey]) {
|
||||
return;
|
||||
}
|
||||
globalState[stateKey] = true;
|
||||
|
||||
const originalEmitWarning = process.emitWarning.bind(process);
|
||||
|
||||
process.emitWarning = ((warning: unknown, ...args: unknown[]) => {
|
||||
const message = warning instanceof Error
|
||||
? warning.message
|
||||
: typeof warning === 'string'
|
||||
? warning
|
||||
: String(warning);
|
||||
|
||||
let code: string | undefined;
|
||||
if (warning instanceof Error && typeof (warning as { code?: unknown }).code === 'string') {
|
||||
code = (warning as { code?: string }).code;
|
||||
}
|
||||
if (typeof args[0] === 'string') {
|
||||
code = args[0];
|
||||
} else if (args[0] && typeof args[0] === 'object' && typeof (args[0] as { code?: unknown }).code === 'string') {
|
||||
code = (args[0] as { code?: string }).code;
|
||||
}
|
||||
|
||||
if (shouldSuppressNodeWarning(code, message)) {
|
||||
return;
|
||||
}
|
||||
|
||||
(originalEmitWarning as (...params: unknown[]) => void)(warning, ...args);
|
||||
}) as typeof process.emitWarning;
|
||||
}
|
||||
|
||||
installNodeWarningFilter();
|
||||
Reference in New Issue
Block a user