diff --git a/build/build.mjs b/build/build.mjs index edf61421b..feaff82f8 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` @@ -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/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/ours/errors.js b/lib/ours/errors.js index 13cd59d8a..979957e3c 100644 --- a/lib/ours/errors.js +++ b/lib/ours/errors.js @@ -1,11 +1,12 @@ 'use strict' -const { format, inspect, AggregateError: CustomAggregateError } = require('./util') +const { format, inspect } = require('./util/inspect') +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 e985b6d97..81856fcfa 100644 --- a/lib/ours/primordials.js +++ b/lib/ours/primordials.js @@ -3,11 +3,28 @@ /* 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. */ + +// 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 71e7579b5..b560361ff 100644 --- a/lib/ours/util.js +++ b/lib/ours/util.js @@ -1,7 +1,11 @@ 'use strict' const bufferModule = require('buffer') -const { kResistStopPropagation, SymbolDispose } = require('./primordials') +const { format, inspect } = require('./util/inspect') +const { + codes: { ERR_INVALID_ARG_TYPE } +} = require('./errors') +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 @@ -28,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({}), @@ -85,50 +73,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/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 } diff --git a/src/errors.js b/src/errors.js index 070989aa5..4c391da26 100644 --- a/src/errors.js +++ b/src/errors.js @@ -1,11 +1,12 @@ 'use strict' -const { format, inspect, AggregateError: CustomAggregateError } = require('./util') +const { format, inspect } = require('./util/inspect') +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 cfe7b02e5..ad25a9178 100644 --- a/src/primordials.js +++ b/src/primordials.js @@ -3,12 +3,31 @@ /* 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. */ +// 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 08a1641f5..8fbc8b347 100644 --- a/src/util.js +++ b/src/util.js @@ -1,7 +1,11 @@ 'use strict' const bufferModule = require('buffer') -const { kResistStopPropagation, SymbolDispose } = require('./primordials') +const { format, inspect } = require('./util/inspect') +const { + codes: { ERR_INVALID_ARG_TYPE } +} = require('./errors') +const { kResistStopPropagation, AggregateError, SymbolDispose } = require('./primordials') const AbortSignal = globalThis.AbortSignal || require('abort-controller').AbortSignal const AbortController = globalThis.AbortController || require('abort-controller').AbortController @@ -30,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({}), @@ -91,53 +77,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 '{}' + } + } +}