Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update build for #542 #549

Merged
merged 5 commits into from
Jan 1, 2025
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
3 changes: 2 additions & 1 deletion build/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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')])
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
14 changes: 8 additions & 6 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
14 changes: 8 additions & 6 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions lib/ours/errors.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 18 additions & 1 deletion lib/ours/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
68 changes: 7 additions & 61 deletions lib/ours/util.js
Original file line number Diff line number Diff line change
@@ -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')
MattiasBuelens marked this conversation as resolved.
Show resolved Hide resolved
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
Expand All @@ -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({}),
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions lib/ours/util/inspect.js
Original file line number Diff line number Diff line change
@@ -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 '{}'
}
}
}
14 changes: 8 additions & 6 deletions lib/stream.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
21 changes: 20 additions & 1 deletion src/primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
Loading
Loading