refactor(server): convert network_failures and suppress_warnings to TypeScript (#33872)

* refactor(server): convert network_failures to TypeScript

Migrate network_failures from CommonJS to a typed TypeScript module
with named exports, and update file_server to import getNetworkFailures
directly instead of using a default export.

Co-authored-by: Cursor <cursoragent@cursor.com>

* refactor(server): convert suppress_warnings to TypeScript

Migrate suppress_warnings to a typed TS module and simplify its unit tests to run in-process instead of spawning child node processes.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(server): fix trivially-passing suppress_warnings test

Replace the first test in suppress_warnings_spec which was trivially
true (spy on process.emitWarning, call it directly, assert calledOnce)
with a meaningful regression test. The new test calls suppress() and
then emits a non-TLS warning, verifying that suppress() only filters
TLS/Buffer warnings and still passes through unrelated ones.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Bill Glesias
2026-05-23 10:51:20 -04:00
committed by GitHub
parent 1f118f1232
commit 4a9f4c86eb
5 changed files with 38 additions and 52 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import send from 'send'
import { httpUtils } from '@packages/network'
import { allowDestroy } from './util/server_destroy'
import { id as randomId } from './util/random'
import networkFailures from './util/network_failures'
import { get as getNetworkFailures } from './util/network_failures'
import type { AddressInfo } from 'net'
const debug = debugModule('cypress:server:file_server')
@@ -53,7 +53,7 @@ const onRequest = function (req: http.IncomingMessage, res: http.ServerResponse,
res.setHeader('content-type', 'text/html')
res.statusCode = err.status
return res.end(networkFailures.get(file, err.status))
return res.end(getNetworkFailures(file, err.status))
}).pipe(res)
}
@@ -1,10 +1,10 @@
const { stripIndents, html } = require('common-tags')
import { stripIndents, html } from 'common-tags'
const convertNewLinesToBr = (text) => {
const convertNewLinesToBr = (text: string): string => {
return text.split('\n').join('<br />')
}
const fileErr = (url, status) => {
export const fileErr = (url: string, status: number): string => {
return stripIndents`
Cypress errored trying to serve this file from your system:
@@ -14,7 +14,7 @@ const fileErr = (url, status) => {
`
}
const wrap = (contents) => {
export const wrap = (contents: string): string => {
return html`
<!DOCTYPE html>
<html>
@@ -25,16 +25,8 @@ const wrap = (contents) => {
`
}
const get = (url, status) => {
export const get = (url: string, status: number): string => {
const contents = fileErr(url, status)
return wrap(contents)
}
module.exports = {
fileErr,
wrap,
get,
}
@@ -1,11 +1,11 @@
const _ = require('lodash')
const Debug = require('debug')
import _ from 'lodash'
import Debug from 'debug'
const debug = Debug('cypress:server:lib:util:suppress_warnings')
let suppressed = false
const suppress = () => {
export const suppress = (): void => {
if (suppressed) {
return
}
@@ -14,7 +14,12 @@ const suppress = () => {
const originalEmitWarning = process.emitWarning
process.emitWarning = (warning, type, code, ...args) => {
process.emitWarning = ((
warning: string | Error,
type?: string,
code?: string,
...args: unknown[]
) => {
/**
* Don't emit the NODE_TLS_REJECT_UNAUTHORIZED warning while
* we work on proper SSL verification.
@@ -40,10 +45,6 @@ const suppress = () => {
return
}
return originalEmitWarning.call(process, warning, type, code, ...args)
}
}
module.exports = {
suppress,
return (originalEmitWarning as (warning: string | Error, type?: string, code?: string, ...args: unknown[]) => void).call(process, warning, type, code, ...args)
}) as typeof process.emitWarning
}
+2 -1
View File
@@ -7,6 +7,7 @@ const { override: overrideTty } = require('./lib/util/tty')
const { GracefulExit } = require('./lib/util/graceful-exit')
const { NetProfiler } = require('./lib/util/net_profiler')
const { debugElapsedTime } = require('./lib/util/performance_benchmark')
const { suppress } = require('./lib/util/suppress_warnings')
const { calculateCypressInternalEnv, configureLongStackTraces } = require('./lib/environment')
@@ -112,6 +113,6 @@ process.noDeprecation = process.env.CYPRESS_INTERNAL_ENV === 'production'
// always show stack traces for Electron deprecation warnings
process.traceDeprecation = true
require('./lib/util/suppress_warnings').suppress()
suppress()
module.exports = require('./lib/cypress').start(process.argv)
@@ -1,37 +1,29 @@
import '../../spec_helper'
import { expect } from 'chai'
import execa from 'execa'
import proxyquire from 'proxyquire'
const ERROR_MESSAGE = 'Setting the NODE_TLS_REJECT_UNAUTHORIZED'
const TLS_CONNECT = `require('tls').connect('5000').on('error', ()=>{});`
const SUPPRESS_WARNING = `require('${__dirname}/../../../lib/util/suppress_warnings').suppress();`
const TLS_WARNING = 'Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to \'0\' makes TLS connections and HTTPS requests insecure by disabling certificate verification.'
describe('lib/util/suppress_warnings', function () {
it('tls.connect emits warning if NODE_TLS_REJECT_UNAUTHORIZED=0 and not suppressed', function () {
return execa(`node -e "${TLS_CONNECT}"`, {
shell: true,
env: {
'NODE_TLS_REJECT_UNAUTHORIZED': '0',
},
})
.then(({ stderr }) => {
expect(stderr).to.contain(ERROR_MESSAGE)
})
it('passes through non-TLS warnings when suppressed', () => {
const emitWarning = sinon.spy(process, 'emitWarning')
const { suppress } = proxyquire('../../../lib/util/suppress_warnings', {})
suppress()
process.emitWarning('some unrelated warning')
expect(emitWarning).to.be.calledOnce
})
it('tls.connect does not emit warning if NODE_TLS_REJECT_UNAUTHORIZED=0 and suppressed', function () {
// test 2 sequential tls.connects
return execa(`node -e "${SUPPRESS_WARNING} ${TLS_CONNECT} ${TLS_CONNECT}"`, {
shell: true,
env: {
'NODE_TLS_REJECT_UNAUTHORIZED': '0',
},
})
.then(({ stderr }) => {
expect(stderr).to.not.contain(ERROR_MESSAGE)
})
it('suppresses NODE_TLS_REJECT_UNAUTHORIZED warnings', () => {
const emitWarning = sinon.spy(process, 'emitWarning')
const { suppress } = proxyquire('../../../lib/util/suppress_warnings', {})
suppress()
process.emitWarning(TLS_WARNING)
process.emitWarning(TLS_WARNING)
expect(emitWarning).not.to.be.called
})
it('does not emit buffer deprecation warnings', () => {