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
7 changes: 2 additions & 5 deletions packages/build/src/log/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promisify } from 'util'
import { setTimeout } from 'timers/promises'

import type { ChildProcess } from '../plugins/spawn.js'

Expand All @@ -14,9 +14,6 @@ export type StandardStreams = {
type LogsListener = (logs: string[], outputFlusher: OutputFlusher | undefined, chunk: Buffer) => void
type LogsListeners = { stderrListener: LogsListener; stdoutListener: LogsListener }

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)

// We try to use `stdio: inherit` because it keeps `stdout/stderr` as `TTY`,
// which solves many problems. However we can only do it in build.command.
// Plugins have several events, so need to be switch on and off instead.
Expand Down Expand Up @@ -67,7 +64,7 @@ export const unpipePluginOutput = async function (
standardStreams: StandardStreams,
) {
// Let `childProcess` `stdout` and `stderr` flush before stopping redirecting
await pSetTimeout(0)
await setTimeout(0)

if (!logsAreBuffered(logs)) {
return unstreamOutput(childProcess, standardStreams)
Expand Down
6 changes: 2 additions & 4 deletions packages/build/src/plugins/load.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { promisify } from 'util'
import { setTimeout } from 'timers/promises'

import { addErrorInfo } from '../error/info.js'
import { addPluginLoadErrorStatus } from '../status/load_error.js'
Expand All @@ -7,8 +7,6 @@ import { measureDuration } from '../time/main.js'
import { callChild } from './ipc.js'
import { captureStandardError } from './system_log.js'

const pSetTimeout = promisify(setTimeout)

// Retrieve all plugins steps
// Can use either a module name or a file path to the plugin.
export const loadPlugins = async function ({
Expand Down Expand Up @@ -112,7 +110,7 @@ const loadPlugin = async function (
} catch (error) {
if (featureFlags.netlify_build_plugin_system_log) {
// Wait for stderr to be flushed.
await pSetTimeout(0)
await setTimeout(0)
}

addErrorInfo(error, {
Expand Down
5 changes: 2 additions & 3 deletions packages/build/src/plugins/spawn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequire } from 'module'
import { platform } from 'os'
import { setTimeout } from 'timers/promises'
import { fileURLToPath, pathToFileURL } from 'url'
import { promisify } from 'util'

import { trace } from '@opentelemetry/api'
import { type ExecaChildProcess, execaNode } from 'execa'
Expand Down Expand Up @@ -30,7 +30,6 @@ import { captureStandardError } from './system_log.js'
export type ChildProcess = ExecaChildProcess<string>

const CHILD_MAIN_FILE = fileURLToPath(new URL('child/main.js', import.meta.url))
const pSetTimeout = promisify(setTimeout)
const require = createRequire(import.meta.url)

// Start child processes used by all plugins
Expand Down Expand Up @@ -155,7 +154,7 @@ const startPlugin = async function ({
} catch (error) {
if (featureFlags.netlify_build_plugin_system_log) {
// Wait for stderr to be flushed.
await pSetTimeout(0)
await setTimeout(0)
}

const spawnInfo = getSpawnInfo()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { env, kill } from 'process'
import { promisify } from 'util'
import { setTimeout } from 'timers/promises'

import { processExists } from 'process-exists'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)

// 100ms
const PROCESS_TIMEOUT = 1e2

Expand All @@ -15,6 +12,6 @@ export const onBuild = async function () {
// Signals are async, so we need to wait for the child process to exit
// The while loop is required due to `await`
while (await processExists(env.TEST_PID)) {
await pSetTimeout(PROCESS_TIMEOUT)
await setTimeout(PROCESS_TIMEOUT)
}
}
7 changes: 2 additions & 5 deletions packages/build/tests/error/fixtures/uncaught/plugin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout as setTimeoutPromise } from 'timers/promises'

export const onPreBuild = async function () {
setTimeout(function callback() {
throw new Error('test')
}, 0)
await pSetTimeout(0)
await setTimeoutPromise(0)
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

export const onPreBuild = async function () {
unhandledPromise()
console.log('onPreBuild')
await pSetTimeout(0)
await setTimeout(0)
}

const unhandledPromise = function () {
Expand Down
7 changes: 2 additions & 5 deletions packages/build/tests/error/fixtures/warning/plugin.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { emitWarning } from 'process'
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

// 1 second
const WARNING_TIMEOUT = 1e3

export const onPreBuild = async function () {
emitWarning('test')
console.log('onPreBuild')
await pSetTimeout(WARNING_TIMEOUT)
await setTimeout(WARNING_TIMEOUT)
}
9 changes: 3 additions & 6 deletions packages/build/tests/plugins/fixtures/interleave/plugin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

// 100ms
const LOG_TIMEOUT = 1e2

export const onPreBuild = async function () {
console.log('one')
await pSetTimeout(LOG_TIMEOUT)
await setTimeout(LOG_TIMEOUT)
console.error('two')
await pSetTimeout(LOG_TIMEOUT)
await setTimeout(LOG_TIMEOUT)
console.log('three')
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { nextTick } from 'process'
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

export const onPreBuild = async function ({
utils: {
Expand All @@ -12,5 +9,5 @@ export const onPreBuild = async function ({
nextTick(() => {
cancelBuild('test')
})
await pSetTimeout(0)
await setTimeout(0)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { nextTick } from 'process'
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

export const onPreBuild = async function ({
utils: {
Expand All @@ -12,5 +9,5 @@ export const onPreBuild = async function ({
nextTick(() => {
failBuild('test')
})
await pSetTimeout(0)
await setTimeout(0)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { nextTick } from 'process'
import { promisify } from 'util'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
const pSetTimeout = promisify(setTimeout)
import { setTimeout } from 'timers/promises'

export const onPreBuild = async function ({
utils: {
Expand All @@ -12,5 +9,5 @@ export const onPreBuild = async function ({
nextTick(() => {
failPlugin('test')
})
await pSetTimeout(0)
await setTimeout(0)
}
4 changes: 0 additions & 4 deletions packages/cache-utils/tests/helpers/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { rm, writeFile } from 'fs/promises'
import { join, basename } from 'path'
import { promisify } from 'util'

import { dir as getTmpDir, tmpName } from 'tmp-promise'
const PREFIX = 'test-cache-utils-'

// TODO: replace with `timers/promises` after dropping Node < 15.0.0
export const pSetTimeout = promisify(setTimeout)

export const createTmpDir = async function (opts = {}) {
const { path } = await getTmpDir({ ...opts, prefix: PREFIX })
return path
Expand Down
9 changes: 5 additions & 4 deletions packages/cache-utils/tests/ttl.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { test, expect } from 'vitest'
import { setTimeout } from 'timers/promises'

import { save, has, restore } from '../src/main.js'

import { pSetTimeout, createTmpDir, createTmpFile, removeFiles } from './helpers/main.js'
import { createTmpDir, createTmpFile, removeFiles } from './helpers/main.js'

// 2 seconds
const TTL_TIMEOUT = 2e3
Expand All @@ -12,7 +13,7 @@ test('Should allow a TTL on cached files', async () => {
const [cacheDir, [srcFile, srcDir]] = await Promise.all([createTmpDir(), createTmpFile()])
try {
expect(await save(srcFile, { cacheDir, ttl: 1 })).toBe(true)
await pSetTimeout(TTL_TIMEOUT)
await setTimeout(TTL_TIMEOUT)
expect(await has(srcFile, { cacheDir })).toBe(false)
expect(await restore(srcFile, { cacheDir })).toBe(false)
} finally {
Expand All @@ -24,7 +25,7 @@ test('Should skip TTL when the value is invalid', async () => {
const [cacheDir, [srcFile, srcDir]] = await Promise.all([createTmpDir(), createTmpFile()])
try {
expect(await save(srcFile, { cacheDir, ttl: '1' })).toBe(true)
await pSetTimeout(TTL_TIMEOUT)
await setTimeout(TTL_TIMEOUT)
expect(await has(srcFile, { cacheDir })).toBe(true)
} finally {
await removeFiles([cacheDir, srcDir])
Expand All @@ -35,7 +36,7 @@ test('Should skip TTL when the value is negative', async () => {
const [cacheDir, [srcFile, srcDir]] = await Promise.all([createTmpDir(), createTmpFile()])
try {
expect(await save(srcFile, { cacheDir, ttl: -1 })).toBe(true)
await pSetTimeout(TTL_TIMEOUT)
await setTimeout(TTL_TIMEOUT)
expect(await has(srcFile, { cacheDir })).toBe(true)
} finally {
await removeFiles([cacheDir, srcDir])
Expand Down
6 changes: 1 addition & 5 deletions packages/headers-parser/src/for_regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ const getPartRegExp = function (part: string): string {

// Non-standalone catch-all wildcard like `/segment/hello*world/test`
if (part.includes('*')) {
// @todo use `part.replaceAll('*', ...)` after dropping support for
// Node <15.0.0
return part.replace(CATCH_ALL_CHAR_REGEXP, '(.*)')
return part.replaceAll('*', '(.*)')
}

return escapeStringRegExp(part)
}

const CATCH_ALL_CHAR_REGEXP = /\*/g
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createRequire } from 'module'
import { version as nodeVersion } from 'process'

import { findUp } from 'find-up'
import { pathExists } from 'path-exists'
// @ts-expect-error doesnt export async
import { async as asyncResolve } from 'resolve'
import semver from 'semver'

// The types do not include the mjs api of resolve
const resolveLib = asyncResolve as typeof import('resolve')
Expand All @@ -31,23 +29,15 @@ const BACKSLASH_REGEXP = /\\/g
export const resolvePackage = async function (moduleName: string, baseDirs: string[]): Promise<string> {
try {
return await resolvePathPreserveSymlinks(`${moduleName}/package.json`, baseDirs)
} catch (error) {
if (semver.lt(nodeVersion, REQUEST_RESOLVE_MIN_VERSION)) {
throw error
}

} catch {
try {
return resolvePathFollowSymlinks(`${moduleName}/package.json`, baseDirs)
} catch (error_) {
return await resolvePackageFallback(moduleName, baseDirs, error_)
} catch (error) {
return await resolvePackageFallback(moduleName, baseDirs, error)
}
}
}

// TODO: remove after dropping support for Node <8.9.0
// `require.resolve()` option `paths` was introduced in Node 8.9.0
const REQUEST_RESOLVE_MIN_VERSION = '8.9.0'

// We need to use `new Promise()` due to a bug with `utils.promisify()` on
// `resolve`:
// https://github.com/browserify/resolve/issues/151#issuecomment-368210310
Expand Down
Loading