From 54896c33613fb1f67edb5c6fc4655660e22442ed Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Tue, 31 Dec 2024 12:21:37 +0100 Subject: [PATCH 1/5] Fix "use strict" regex --- build/build.mjs | 2 +- lib/internal/streams/end-of-stream.js | 8 +++++--- lib/internal/streams/readable.js | 14 ++++++++------ lib/internal/streams/writable.js | 14 ++++++++------ lib/stream.js | 14 ++++++++------ 5 files changed, 30 insertions(+), 22 deletions(-) diff --git a/build/build.mjs b/build/build.mjs index edf61421b..67065ad48 100644 --- a/build/build.mjs +++ b/build/build.mjs @@ -15,7 +15,7 @@ import { headers } from './headers.mjs' import { replacements } from './replacements.mjs' const baseMatcher = /^(?:lib|test)/ -const strictMatcher = /^(['"']use strict.+)/ +const strictMatcher = /^(['"]use strict.+)/m function highlightFile(file, color) { return `\x1b[${color}m${file.replace(process.cwd() + '/', '')}\x1b[0m` diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index 29feb936b..94d18321d 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -1,12 +1,14 @@ +// Ported from https://github.com/mafintosh/end-of-stream with +// permission from the author, Mathias Buus (@mafintosh). + +'use strict' + /* replacement start */ const process = require('process/') /* replacement end */ -// Ported from https://github.com/mafintosh/end-of-stream with -// permission from the author, Mathias Buus (@mafintosh). -;('use strict') const { AbortError, codes } = require('../../ours/errors') const { ERR_INVALID_ARG_TYPE, ERR_STREAM_PREMATURE_CLOSE } = codes const { kEmptyObject, once } = require('../../ours/util') diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index c6fcc64ca..90c731605 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -1,8 +1,3 @@ -/* replacement start */ - -const process = require('process/') - -/* replacement end */ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -24,7 +19,14 @@ const process = require('process/') // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -;('use strict') +'use strict' + +/* replacement start */ + +const process = require('process/') + +/* replacement end */ + const { ArrayPrototypeIndexOf, NumberIsInteger, diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 8a2800346..b4ecf0e21 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -1,8 +1,3 @@ -/* replacement start */ - -const process = require('process/') - -/* replacement end */ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -28,7 +23,14 @@ const process = require('process/') // Implement an async ._write(chunk, encoding, cb), and it'll handle all // the drain event emission and buffering. -;('use strict') +'use strict' + +/* replacement start */ + +const process = require('process/') + +/* replacement end */ + const { ArrayPrototypeSlice, Error, diff --git a/lib/stream.js b/lib/stream.js index b92b427be..1e2cab315 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -1,8 +1,3 @@ -/* replacement start */ - -const { Buffer } = require('buffer') - -/* replacement end */ // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a @@ -24,7 +19,14 @@ const { Buffer } = require('buffer') // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -;('use strict') +'use strict' + +/* replacement start */ + +const { Buffer } = require('buffer') + +/* replacement end */ + const { ObjectDefineProperty, ObjectKeys, ReflectApply } = require('./ours/primordials') const { promisify: { custom: customPromisify } From ff0501ee3cfb2419a6f70fed3a5341bbe2d35fdd Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Tue, 31 Dec 2024 12:30:17 +0100 Subject: [PATCH 2/5] Fix missing require for ERR_INVALID_ARG_TYPE --- lib/ours/util.js | 3 +++ src/util.js | 3 +++ 2 files changed, 6 insertions(+) diff --git a/lib/ours/util.js b/lib/ours/util.js index 71e7579b5..b56bff895 100644 --- a/lib/ours/util.js +++ b/lib/ours/util.js @@ -1,6 +1,9 @@ 'use strict' const bufferModule = require('buffer') +const { + codes: { ERR_INVALID_ARG_TYPE } +} = require('./errors') const { kResistStopPropagation, SymbolDispose } = require('./primordials') const AbortSignal = globalThis.AbortSignal || require('abort-controller').AbortSignal const AbortController = globalThis.AbortController || require('abort-controller').AbortController diff --git a/src/util.js b/src/util.js index 08a1641f5..a614fd9f9 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,9 @@ 'use strict' const bufferModule = require('buffer') +const { + codes: { ERR_INVALID_ARG_TYPE } +} = require('./errors') const { kResistStopPropagation, SymbolDispose } = require('./primordials') const AbortSignal = globalThis.AbortSignal || require('abort-controller').AbortSignal const AbortController = globalThis.AbortController || require('abort-controller').AbortController From c820e2b0acb399b4286eaad5b0b9a027c973a88d Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Tue, 31 Dec 2024 14:53:25 +0100 Subject: [PATCH 3/5] Move format and inspect to util/inspect --- build/build.mjs | 1 + lib/ours/errors.js | 3 +- lib/ours/util.js | 47 ++------------------------------ lib/ours/util/inspect.js | 55 +++++++++++++++++++++++++++++++++++++ src/errors.js | 3 +- src/util.js | 50 ++-------------------------------- src/util/inspect.js | 59 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 93 deletions(-) create mode 100644 lib/ours/util/inspect.js create mode 100644 src/util/inspect.js diff --git a/build/build.mjs b/build/build.mjs index 67065ad48..feaff82f8 100644 --- a/build/build.mjs +++ b/build/build.mjs @@ -195,6 +195,7 @@ async function main() { contents.push(['lib/ours/errors.js', await readFile('src/errors.js', 'utf-8')]) contents.push(['lib/ours/primordials.js', await readFile('src/primordials.js', 'utf-8')]) contents.push(['lib/ours/util.js', await readFile('src/util.js', 'utf-8')]) + contents.push(['lib/ours/util/inspect.js', await readFile('src/util/inspect.js', 'utf-8')]) for (const file of await readdir('src/test/ours')) { contents.push([`test/ours/${file}`, await readFile(`src/test/ours/${file}`, 'utf-8')]) diff --git a/lib/ours/errors.js b/lib/ours/errors.js index 13cd59d8a..bc4e13848 100644 --- a/lib/ours/errors.js +++ b/lib/ours/errors.js @@ -1,6 +1,7 @@ 'use strict' -const { format, inspect, AggregateError: CustomAggregateError } = require('./util') +const { format, inspect } = require('./util/inspect') +const { AggregateError: CustomAggregateError } = require('./util') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at diff --git a/lib/ours/util.js b/lib/ours/util.js index b56bff895..9a262f6c2 100644 --- a/lib/ours/util.js +++ b/lib/ours/util.js @@ -1,6 +1,7 @@ 'use strict' const bufferModule = require('buffer') +const { format, inspect } = require('./util/inspect') const { codes: { ERR_INVALID_ARG_TYPE } } = require('./errors') @@ -88,50 +89,8 @@ module.exports = { debuglog() { return function () {} }, - format(format, ...args) { - // Simplified version of https://nodejs.org/api/util.html#utilformatformat-args - return format.replace(/%([sdifj])/g, function (...[_unused, type]) { - const replacement = args.shift() - if (type === 'f') { - return replacement.toFixed(6) - } else if (type === 'j') { - return JSON.stringify(replacement) - } else if (type === 's' && typeof replacement === 'object') { - const ctor = replacement.constructor !== Object ? replacement.constructor.name : '' - return `${ctor} {}`.trim() - } else { - return replacement.toString() - } - }) - }, - inspect(value) { - // Vastly simplified version of https://nodejs.org/api/util.html#utilinspectobject-options - switch (typeof value) { - case 'string': - if (value.includes("'")) { - if (!value.includes('"')) { - return `"${value}"` - } else if (!value.includes('`') && !value.includes('${')) { - return `\`${value}\`` - } - } - return `'${value}'` - case 'number': - if (isNaN(value)) { - return 'NaN' - } else if (Object.is(value, -0)) { - return String(value) - } - return value - case 'bigint': - return `${String(value)}n` - case 'boolean': - case 'undefined': - return String(value) - case 'object': - return '{}' - } - }, + format, + inspect, types: { isAsyncFunction(fn) { return fn instanceof AsyncFunction diff --git a/lib/ours/util/inspect.js b/lib/ours/util/inspect.js new file mode 100644 index 000000000..e00844570 --- /dev/null +++ b/lib/ours/util/inspect.js @@ -0,0 +1,55 @@ +'use strict' + +/* + This file is a reduced and adapted version of the main lib/internal/util/inspect.js file defined at + + https://github.com/nodejs/node/blob/main/lib/internal/util/inspect.js + + Don't try to replace with the original file and keep it up to date with the upstream file. +*/ +module.exports = { + format(format, ...args) { + // Simplified version of https://nodejs.org/api/util.html#utilformatformat-args + return format.replace(/%([sdifj])/g, function (...[_unused, type]) { + const replacement = args.shift() + if (type === 'f') { + return replacement.toFixed(6) + } else if (type === 'j') { + return JSON.stringify(replacement) + } else if (type === 's' && typeof replacement === 'object') { + const ctor = replacement.constructor !== Object ? replacement.constructor.name : '' + return `${ctor} {}`.trim() + } else { + return replacement.toString() + } + }) + }, + inspect(value) { + // Vastly simplified version of https://nodejs.org/api/util.html#utilinspectobject-options + switch (typeof value) { + case 'string': + if (value.includes("'")) { + if (!value.includes('"')) { + return `"${value}"` + } else if (!value.includes('`') && !value.includes('${')) { + return `\`${value}\`` + } + } + return `'${value}'` + case 'number': + if (isNaN(value)) { + return 'NaN' + } else if (Object.is(value, -0)) { + return String(value) + } + return value + case 'bigint': + return `${String(value)}n` + case 'boolean': + case 'undefined': + return String(value) + case 'object': + return '{}' + } + } +} diff --git a/src/errors.js b/src/errors.js index 070989aa5..5c054b577 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,6 +1,7 @@ 'use strict' -const { format, inspect, AggregateError: CustomAggregateError } = require('./util') +const { format, inspect } = require('./util/inspect') +const { AggregateError: CustomAggregateError } = require('./util') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at diff --git a/src/util.js b/src/util.js index a614fd9f9..bae9c1c7e 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,7 @@ 'use strict' const bufferModule = require('buffer') +const { format, inspect } = require('./util/inspect') const { codes: { ERR_INVALID_ARG_TYPE } } = require('./errors') @@ -94,53 +95,8 @@ module.exports = { debuglog() { return function () {} }, - format(format, ...args) { - // Simplified version of https://nodejs.org/api/util.html#utilformatformat-args - return format.replace(/%([sdifj])/g, function (...[_unused, type]) { - const replacement = args.shift() - - if (type === 'f') { - return replacement.toFixed(6) - } else if (type === 'j') { - return JSON.stringify(replacement) - } else if (type === 's' && typeof replacement === 'object') { - const ctor = replacement.constructor !== Object ? replacement.constructor.name : '' - return `${ctor} {}`.trim() - } else { - return replacement.toString() - } - }) - }, - inspect(value) { - // Vastly simplified version of https://nodejs.org/api/util.html#utilinspectobject-options - switch (typeof value) { - case 'string': - if (value.includes("'")) { - if (!value.includes('"')) { - return `"${value}"` - } else if (!value.includes('`') && !value.includes('${')) { - return `\`${value}\`` - } - } - - return `'${value}'` - case 'number': - if (isNaN(value)) { - return 'NaN' - } else if (Object.is(value, -0)) { - return String(value) - } - - return value - case 'bigint': - return `${String(value)}n` - case 'boolean': - case 'undefined': - return String(value) - case 'object': - return '{}' - } - }, + format, + inspect, types: { isAsyncFunction(fn) { return fn instanceof AsyncFunction diff --git a/src/util/inspect.js b/src/util/inspect.js new file mode 100644 index 000000000..b6e0b3947 --- /dev/null +++ b/src/util/inspect.js @@ -0,0 +1,59 @@ +'use strict' + +/* + This file is a reduced and adapted version of the main lib/internal/util/inspect.js file defined at + + https://github.com/nodejs/node/blob/main/lib/internal/util/inspect.js + + Don't try to replace with the original file and keep it up to date with the upstream file. +*/ + +module.exports = { + format(format, ...args) { + // Simplified version of https://nodejs.org/api/util.html#utilformatformat-args + return format.replace(/%([sdifj])/g, function (...[_unused, type]) { + const replacement = args.shift() + + if (type === 'f') { + return replacement.toFixed(6) + } else if (type === 'j') { + return JSON.stringify(replacement) + } else if (type === 's' && typeof replacement === 'object') { + const ctor = replacement.constructor !== Object ? replacement.constructor.name : '' + return `${ctor} {}`.trim() + } else { + return replacement.toString() + } + }) + }, + inspect(value) { + // Vastly simplified version of https://nodejs.org/api/util.html#utilinspectobject-options + switch (typeof value) { + case 'string': + if (value.includes("'")) { + if (!value.includes('"')) { + return `"${value}"` + } else if (!value.includes('`') && !value.includes('${')) { + return `\`${value}\`` + } + } + + return `'${value}'` + case 'number': + if (isNaN(value)) { + return 'NaN' + } else if (Object.is(value, -0)) { + return String(value) + } + + return value + case 'bigint': + return `${String(value)}n` + case 'boolean': + case 'undefined': + return String(value) + case 'object': + return '{}' + } + } +} From f66337fb72cceb88f2cab7ae687949ef575ba76a Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Tue, 31 Dec 2024 14:56:12 +0100 Subject: [PATCH 4/5] Move AggregateError to primordials --- lib/ours/errors.js | 2 +- lib/ours/primordials.js | 17 +++++++++++++++++ lib/ours/util.js | 18 +----------------- src/errors.js | 2 +- src/primordials.js | 19 +++++++++++++++++++ src/util.js | 20 +------------------- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/lib/ours/errors.js b/lib/ours/errors.js index bc4e13848..4dea50fae 100644 --- a/lib/ours/errors.js +++ b/lib/ours/errors.js @@ -1,7 +1,7 @@ 'use strict' const { format, inspect } = require('./util/inspect') -const { AggregateError: CustomAggregateError } = require('./util') +const { AggregateError: CustomAggregateError } = require('./primordials') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at diff --git a/lib/ours/primordials.js b/lib/ours/primordials.js index e985b6d97..1e53258a6 100644 --- a/lib/ours/primordials.js +++ b/lib/ours/primordials.js @@ -7,7 +7,24 @@ Don't try to replace with the original file and keep it up to date with the upstream file. */ + +// This is a simplified version of AggregateError +class AggregateError extends Error { + constructor(errors) { + if (!Array.isArray(errors)) { + throw new TypeError(`Expected input to be an Array, got ${typeof errors}`) + } + let message = '' + for (let i = 0; i < errors.length; i++) { + message += ` ${errors[i].stack}\n` + } + super(message) + this.name = 'AggregateError' + this.errors = errors + } +} module.exports = { + AggregateError, ArrayIsArray(self) { return Array.isArray(self) }, diff --git a/lib/ours/util.js b/lib/ours/util.js index 9a262f6c2..b560361ff 100644 --- a/lib/ours/util.js +++ b/lib/ours/util.js @@ -5,7 +5,7 @@ const { format, inspect } = require('./util/inspect') const { codes: { ERR_INVALID_ARG_TYPE } } = require('./errors') -const { kResistStopPropagation, SymbolDispose } = require('./primordials') +const { kResistStopPropagation, AggregateError, SymbolDispose } = require('./primordials') const AbortSignal = globalThis.AbortSignal || require('abort-controller').AbortSignal const AbortController = globalThis.AbortController || require('abort-controller').AbortController const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor @@ -32,22 +32,6 @@ const validateFunction = (value, name) => { throw new ERR_INVALID_ARG_TYPE(name, 'Function', value) } } - -// This is a simplified version of AggregateError -class AggregateError extends Error { - constructor(errors) { - if (!Array.isArray(errors)) { - throw new TypeError(`Expected input to be an Array, got ${typeof errors}`) - } - let message = '' - for (let i = 0; i < errors.length; i++) { - message += ` ${errors[i].stack}\n` - } - super(message) - this.name = 'AggregateError' - this.errors = errors - } -} module.exports = { AggregateError, kEmptyObject: Object.freeze({}), diff --git a/src/errors.js b/src/errors.js index 5c054b577..aa7f91e9c 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,7 +1,7 @@ 'use strict' const { format, inspect } = require('./util/inspect') -const { AggregateError: CustomAggregateError } = require('./util') +const { AggregateError: CustomAggregateError } = require('./primordials') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at diff --git a/src/primordials.js b/src/primordials.js index cfe7b02e5..3c89659ac 100644 --- a/src/primordials.js +++ b/src/primordials.js @@ -8,7 +8,26 @@ Don't try to replace with the original file and keep it up to date with the upstream file. */ +// This is a simplified version of AggregateError +class AggregateError extends Error { + constructor(errors) { + if (!Array.isArray(errors)) { + throw new TypeError(`Expected input to be an Array, got ${typeof errors}`) + } + + let message = '' + for (let i = 0; i < errors.length; i++) { + message += ` ${errors[i].stack}\n` + } + + super(message) + this.name = 'AggregateError' + this.errors = errors + } +} + module.exports = { + AggregateError, ArrayIsArray(self) { return Array.isArray(self) }, diff --git a/src/util.js b/src/util.js index bae9c1c7e..8fbc8b347 100644 --- a/src/util.js +++ b/src/util.js @@ -5,7 +5,7 @@ const { format, inspect } = require('./util/inspect') const { codes: { ERR_INVALID_ARG_TYPE } } = require('./errors') -const { kResistStopPropagation, SymbolDispose } = require('./primordials') +const { kResistStopPropagation, AggregateError, SymbolDispose } = require('./primordials') const AbortSignal = globalThis.AbortSignal || require('abort-controller').AbortSignal const AbortController = globalThis.AbortController || require('abort-controller').AbortController @@ -34,24 +34,6 @@ const validateFunction = (value, name) => { } } -// This is a simplified version of AggregateError -class AggregateError extends Error { - constructor(errors) { - if (!Array.isArray(errors)) { - throw new TypeError(`Expected input to be an Array, got ${typeof errors}`) - } - - let message = '' - for (let i = 0; i < errors.length; i++) { - message += ` ${errors[i].stack}\n` - } - - super(message) - this.name = 'AggregateError' - this.errors = errors - } -} - module.exports = { AggregateError, kEmptyObject: Object.freeze({}), From 00036f2102b68ba3669f1df1b282b5a9e411624e Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Tue, 31 Dec 2024 15:03:29 +0100 Subject: [PATCH 5/5] Update branch name in comments --- lib/ours/errors.js | 2 +- lib/ours/primordials.js | 2 +- src/errors.js | 2 +- src/primordials.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ours/errors.js b/lib/ours/errors.js index 4dea50fae..979957e3c 100644 --- a/lib/ours/errors.js +++ b/lib/ours/errors.js @@ -6,7 +6,7 @@ const { AggregateError: CustomAggregateError } = require('./primordials') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at - https://github.com/nodejs/node/blob/master/lib/internal/errors.js + https://github.com/nodejs/node/blob/main/lib/internal/errors.js Don't try to replace with the original file and keep it up to date (starting from E(...) definitions) with the upstream file. diff --git a/lib/ours/primordials.js b/lib/ours/primordials.js index 1e53258a6..81856fcfa 100644 --- a/lib/ours/primordials.js +++ b/lib/ours/primordials.js @@ -3,7 +3,7 @@ /* This file is a reduced and adapted version of the main lib/internal/per_context/primordials.js file defined at - https://github.com/nodejs/node/blob/master/lib/internal/per_context/primordials.js + https://github.com/nodejs/node/blob/main/lib/internal/per_context/primordials.js Don't try to replace with the original file and keep it up to date with the upstream file. */ diff --git a/src/errors.js b/src/errors.js index aa7f91e9c..4c391da26 100644 --- a/src/errors.js +++ b/src/errors.js @@ -6,7 +6,7 @@ const { AggregateError: CustomAggregateError } = require('./primordials') /* This file is a reduced and adapted version of the main lib/internal/errors.js file defined at - https://github.com/nodejs/node/blob/master/lib/internal/errors.js + https://github.com/nodejs/node/blob/main/lib/internal/errors.js Don't try to replace with the original file and keep it up to date (starting from E(...) definitions) with the upstream file. diff --git a/src/primordials.js b/src/primordials.js index 3c89659ac..ad25a9178 100644 --- a/src/primordials.js +++ b/src/primordials.js @@ -3,7 +3,7 @@ /* This file is a reduced and adapted version of the main lib/internal/per_context/primordials.js file defined at - https://github.com/nodejs/node/blob/master/lib/internal/per_context/primordials.js + https://github.com/nodejs/node/blob/main/lib/internal/per_context/primordials.js Don't try to replace with the original file and keep it up to date with the upstream file. */