From cf850cacedbbf1ba9d89acdb03f416904dce775c Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 15:26:19 +0000 Subject: [PATCH 01/61] fix(buffer): close four Node divergences in size validation and inspect (#391) Verified against node 24.22.1 with a 904-case probe; before this change 580 lines differed under bin/lava, after it 224, and no line that matched before differs now. The remainder is #392 and the documented kMaxLength bound. 1. ERR_INVALID_ARG_TYPE wording. errInvalidArgType splices its 'expected' argument verbatim and three call sites passed a bare 'number', giving 'must be number'. size (assertSize), offset (checkBounds) and concat's length now read 'must be of type number', as node does. 2. The deprecated constructor's number branch skipped assertSize: Buffer(-1) surfaced the engine's codeless RangeError and Buffer(NaN) returned an empty Buffer. Both now throw ERR_OUT_OF_RANGE, with and without new. The bound in the message stays lava's K_MAX_LENGTH (documented deviation). 3. inspect adds the separator before '... N more bytes' even when no byte is shown, so INSPECT_MAX_BYTES = 0 prints ''. 4. util.inspect shows a Buffer's extra own properties after the bytes (''): insertion order, symbol keys, non-enumerable only with showHidden, getters by value, depth honoured. util.js now passes util.inspect itself as the custom hook's third argument, as node does; the old depth+1 closure rendered every extra one level short. Deviation, measured and pinned: a Buffer longer than 1024 bytes shows its symbol-keyed extras but not its string-keyed ones. Listing a typed array's named properties materialises one string per byte; the method and timings are in the comment at INSPECT_EXTRAS_MAX_LENGTH. Pinned Lava-only by cmd/lava/buffer_inspect_extras_test.odin (buffer_inspect_extras_cap), which also pins that a bare buf.inspect() renders bytes only. Known gaps left as they were: extras are not coloured (util.inspect colours nothing yet, #104) and a Buffer nested in a Buffer's property renders one level short. Oracle: tests/node-compat/cases/70-buffer-size-inspect-parity.js. Each fix was mutated by hand and turned the case red; the manifest entries are not in this commit because tests/mutation-manifest.json is human-only. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YQTDDUu9z73JRY3RpV6Adg --- cmd/lava/buffer_inspect_extras_test.odin | 74 +++++++ pkg/runtime/js/internal/buffer.js | 201 +++++++++++++++-- pkg/runtime/js/internal/util.js | 12 +- .../cases/70-buffer-size-inspect-parity.js | 203 ++++++++++++++++++ 4 files changed, 471 insertions(+), 19 deletions(-) create mode 100644 cmd/lava/buffer_inspect_extras_test.odin create mode 100644 tests/node-compat/cases/70-buffer-size-inspect-parity.js diff --git a/cmd/lava/buffer_inspect_extras_test.odin b/cmd/lava/buffer_inspect_extras_test.odin new file mode 100644 index 0000000..b9a5256 --- /dev/null +++ b/cmd/lava/buffer_inspect_extras_test.odin @@ -0,0 +1,74 @@ +package main + +// Lava-only: pins the two places where Buffer's util.inspect hook deliberately +// renders LESS than node 24 (issue #391), per CLAUDE.md §1. +// +// Why this is not an oracle case. Both behaviours differ from node by design, so a +// node-vs-Lava diff would only ever be red: +// +// 1. The string-key scan is capped at INSPECT_EXTRAS_MAX_LENGTH (1024) bytes +// (pkg/runtime/js/internal/buffer.js). JS cannot list a typed array's named own +// properties without first materialising one string per index, so an uncapped +// scan made `console.log(chunk)` O(length) — 13.8 ms for a 64 KiB Buffer and 11 s +// for 1e7 bytes, measured on bin/lava. Past the cap, string-keyed extras are +// omitted; symbol-keyed ones still show (Object.getOwnPropertySymbols lists no +// indices). Node shows both at any length. +// 2. Extras render only when the hook is handed an inspect function, which +// util.inspect and console.log always do. A bare `buf.inspect()` therefore shows +// the bytes only — which matches node for the no-argument call this pins; node +// would also render extras for `buf.inspect(0, {})`, which Lava does not. +// +// The 1024/1025 pair sits exactly on the cap, so moving the constant or dropping the +// length check turns this red. The script throws on the first mismatch, so the +// message in the failed expectation names it. + +import "core:testing" +import "lava:pkg/runtime/eventloop" +import lava "lava:pkg/runtime" + +BUFFER_INSPECT_EXTRAS_SCRIPT :: ` +const util = require('node:util'); +const sym = Symbol('s'); +function check(label, got, want) { + if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); +} + +// At the cap: the string-keyed property is scanned and shown. +const atCap = Buffer.alloc(1024); +atCap.tag = 1; +check('1024 shows string key', util.inspect(atCap).endsWith(', tag: 1>'), true); + +// One past the cap: the string key is omitted, the symbol key still shows. +const pastCap = Buffer.alloc(1025); +pastCap.tag = 1; +pastCap[sym] = 2; +const past = util.inspect(pastCap); +check('1025 omits string key', past.includes('tag: 1'), false); +check('1025 shows symbol key', past.endsWith(' more bytes, Symbol(s): 2>'), true); + +// A bare call receives no inspect function and renders the bytes only. +const small = Buffer.from([1, 2, 3]); +small.tag = 1; +check('bare inspect()', small.inspect(), ''); +// ...while util.inspect of the same Buffer does render the extra (so the line above +// is not passing merely because extras never render). +check('util.inspect same buffer', util.inspect(small), ""); +console.log('buffer-inspect-extras ok'); +` + +@(test) +buffer_inspect_extras_cap :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(BUFFER_INSPECT_EXTRAS_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 332596e..3b6190e 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -62,6 +62,9 @@ var StringFromCharCode = String.fromCharCode; var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; var TypedArrayPrototypeGetLength = P.TypedArrayPrototypeGetLength; + var ObjectGetOwnPropertyNames = P.ObjectGetOwnPropertyNames; + var ObjectGetOwnPropertySymbols = P.ObjectGetOwnPropertySymbols; + var ObjectGetOwnPropertyDescriptor = P.ObjectGetOwnPropertyDescriptor; /** * @type {Object} lowercased name → canonical encoding. @@ -134,7 +137,20 @@ return (neg ? '-' : '') + str.slice(0, i) + out; } - /** @returns {TypeError & {code: string}} */ + /** + * Builds node's ERR_INVALID_ARG_TYPE for a named argument. + * @param {string} name Argument name, quoted into the message. + * @param {string} expected Everything after "must be", verb phrase included: + * 'of type number', 'an instance of Array'. It is spliced verbatim, so a bare + * type name ('number') yields `must be number` — a sentence node never prints. + * @param {*} actual The rejected value, rendered by describeType. + * @returns {TypeError & {code: string}} + * @node `The "size" argument must be of type number. Received type string ('5')` + * for Buffer.alloc('5'); the same template for "offset" (readUInt8('5'), + * writeUInt16LE(0, '1')) and "length" (Buffer.concat([a], '5')). Node 24.22.1, + * verified by the issue #391 probe. + * @deviates none + */ function errInvalidArgType(name, expected, actual) { var e = new TypeError( 'The "' + name + '" argument must be ' + expected + '. Received ' + describeType(actual), @@ -227,9 +243,26 @@ return e; } - /** Validates size for alloc/allocUnsafe (Node + JSC abort guard). */ + /** + * Validates a byte count for alloc/allocUnsafe/allocUnsafeSlow and the deprecated + * `Buffer(n)` constructor (node + JSC abort guard: JSC aborts rather than throws on + * some oversized typed-array requests, so the ceiling is checked here first). + * @param {*} size + * @throws {TypeError} ERR_INVALID_ARG_TYPE — size is not a number. + * @throws {RangeError} ERR_OUT_OF_RANGE — NaN, negative, ±Infinity, or above the + * ceiling; the message quotes the ceiling and `Received `. + * @node -1/NaN/±Infinity/2**53 → `The value of "size" is out of range. It must be + * >= 0 && <= 9007199254740991. Received -1` (2**53 prints 9_007_199_254_740_992); + * -0 and 1.5 pass (length 0 and 1). Node 24.22.1, verified by the #391 probe. + * @deviates The ceiling is K_MAX_LENGTH (4294967296, the JSC-family kMaxLength that + * buffer.constants.MAX_LENGTH reports), not node's 2**53-1 — the documented + * kMaxLength deviation in reference/node-compatibility.md. The bound is pinned + * to MAX_LENGTH, not to a literal, by + * tests/node-compat/cases/70-buffer-size-inspect-parity.js and + * 34-oversized-allocation.js. + */ function assertSize(size) { - if (typeof size !== 'number') throw errInvalidArgType('size', 'number', size); + if (typeof size !== 'number') throw errInvalidArgType('size', 'of type number', size); if (!(size >= 0 && size <= K_MAX_LENGTH)) throw errOutOfRange('size', '>= 0 && <= ' + K_MAX_LENGTH, size); } @@ -342,7 +375,7 @@ /** Node-style offset bounds for fixed-width numeric accessors. */ function checkBounds(buf, offset, byteLength) { if (offset === undefined) offset = 0; - if (typeof offset !== 'number') throw errInvalidArgType('offset', 'number', offset); + if (typeof offset !== 'number') throw errInvalidArgType('offset', 'of type number', offset); if (Math.floor(offset) !== offset) throw errOutOfRange('offset', 'an integer', offset); var max = buf.length - byteLength; if (max < 0) throw errBufferOutOfBounds(); @@ -718,11 +751,23 @@ * @param {number|string} [encodingOrOffset] * @param {number} [length] * @returns {Buffer} + * @throws {RangeError} ERR_OUT_OF_RANGE — a number `arg` that is NaN, negative, + * ±Infinity or above kMaxLength (assertSize, the check Buffer.alloc runs). + * @node `Buffer(n)` is `Buffer.alloc(n)`: -1, NaN, ±Infinity and 2**53 throw the + * coded "size" RangeError, -0 → length 0, 1.5 → length 1, 5 → five zero bytes; + * identical with and without `new`. Node 24.22.1, verified by the #391 probe. + * @deviates The out-of-range bound is K_MAX_LENGTH (see assertSize), pinned by + * tests/node-compat/cases/70-buffer-size-inspect-parity.js. */ function Buffer(arg, encodingOrOffset, length) { if (typeof arg === 'string') return Buffer.from(arg, encodingOrOffset); if (typeof arg === 'number') { - if (arg > K_MAX_LENGTH) throw errOutOfRange('size', '>= 0 && <= ' + K_MAX_LENGTH, arg); + // Only the upper bound used to be checked here, so -1 reached the typed-array + // constructor and threw JSC's codeless "length cannot be negative", and NaN + // (ToIndex(NaN) is 0) quietly returned an empty Buffer. assertSize is the + // validation Buffer.alloc runs; calling it rather than the live `Buffer.alloc` + // keeps a user-replaced static out of the constructor. + assertSize(arg); return new FastBuffer(arg); // zero-filled, matching the old `super(number)` } return new FastBuffer(arg, encodingOrOffset, length); @@ -739,7 +784,13 @@ var p = Buffer.prototype; + /** @returns {string} `` with the bytes only (no extra properties). */ function inspectBuffer(buf) { + return ''; + } + + /** @returns {string} The hex pairs and the `... N more bytes` tail, unframed. */ + function inspectBytes(buf) { var max = inspectMaxBytes; var shown = buf.length > max ? max : buf.length; var hex = ''; @@ -760,19 +811,138 @@ } } var remaining = buf.length - shown; - if (remaining > 0) - hex += (hex ? ' ' : '') + '... ' + remaining + ' more byte' + (remaining > 1 ? 's' : ''); - return ''; + // The separator is unconditional: node appends ` ... N more bytes` to whatever + // hex it produced, so INSPECT_MAX_BYTES = 0 prints TWO spaces after ` 0) hex += ' ... ' + remaining + ' more byte' + (remaining > 1 ? 's' : ''); + return hex; } - p.inspect = function () { - return inspectBuffer(this); - }; + /** + * Buffers longer than this skip the own string-key scan in inspectExtras. + * + * JS has no way to list a typed array's NAMED own properties without first listing + * every index: Object.getOwnPropertyNames / Object.keys / Reflect.ownKeys / for-in + * all materialise one string per byte (node avoids this with its native + * getOwnNonIndexProperties). Measured on bin/lava, one launch per size: timed with + * performance.now() around max(20, 2e6/n) back-to-back Object.getOwnPropertyNames + * calls on one Buffer.alloc(n), reported as the mean per call — 1.2µs at n=16, 18µs + * at n=256, 86µs at n=1024, 13.8ms at n=65536 — against 2.6µs mean for the whole + * bytes-only util.inspect of the same Buffer timed the same way. A single + * Reflect.ownKeys on Buffer.alloc(1e7) took 11.1s (one call, Date.now()). Unbounded, + * `console.log(chunk)` on a 64 KiB read chunk would cost ~5000x what it does today, + * so the scan is bounded and the rest is a deviation, pinned Lava-only by + * cmd/lava/buffer_inspect_extras_test.odin (buffer_inspect_extras_cap). + * Symbol keys are always shown: Object.getOwnPropertySymbols lists no indices. + */ + var INSPECT_EXTRAS_MAX_LENGTH = 1024; + + /** @returns {boolean} Whether `key` is an own enumerable property of `obj`. */ + function isEnumerableOwn(obj, key) { + var desc = ObjectGetOwnPropertyDescriptor(obj, key); + return desc !== undefined && desc.enumerable === true; + } + + /** @returns {boolean} node's keyStrRegExp, /^[a-zA-Z_][a-zA-Z_0-9]*$/, as a loop. */ + function isPlainKey(key) { + var n = key.length; + if (n === 0) return false; + for (var i = 0; i < n; i++) { + var c = StringPrototypeCharCodeAt(key, i); + if (!( + (c >= 0x41 && c <= 0x5a) || // A-Z + (c >= 0x61 && c <= 0x7a) || // a-z + c === 0x5f || // _ + (i > 0 && c >= 0x30 && c <= 0x39) // 0-9, not leading + )) + return false; + } + return true; + } + + /** + * Renders a Buffer's extra own properties as `key: value, …` (no frame), or ''. + * + * Keys are formatted here rather than by building a null-prototype object and + * inspecting that, which is what node does: Lava's util.inspect drops symbol keys + * and prints no `[Object: null prototype]` prefix to slice off. Values go through + * the hook's `inspect` argument one level down — node inspects the property bag at + * depth 0, so each VALUE sits at depth 1 under the caller's own `depth`; asking a + * fresh inspect for `depth - 1` is the same cut-off for those values. It is not the + * same for a Buffer nested in a property: that inner hook sees `depth - 1` where node + * hands it the caller's depth unchanged — a gap, not intended behaviour. + */ + function inspectExtras(buf, ctx, inspect) { + var showHidden = !!ctx.showHidden; + var depth = ctx.depth; + var valueOpts = { + __proto__: null, + depth: depth === null ? null : (depth === undefined ? 2 : depth) - 1, + showHidden: ctx.showHidden, + }; + var out = ''; + var len = TypedArrayPrototypeGetLength(buf); + var key; + if (len <= INSPECT_EXTRAS_MAX_LENGTH) { + // A typed array's [[OwnPropertyKeys]] lists its integer indices first, in order + // (ECMA-262 §10.4.5.7), and a canonical numeric key can never be added as a named + // property, so everything from position `len` on is a non-index name. + var names = ObjectGetOwnPropertyNames(buf); + for (var i = len; i < names.length; i++) { + key = names[i]; + if (showHidden || isEnumerableOwn(buf, key)) + out += + (out === '' ? '' : ', ') + + (isPlainKey(key) ? key : inspect(key)) + + ': ' + + inspect(buf[key], valueOpts); + } + } + var syms = ObjectGetOwnPropertySymbols(buf); + for (var s = 0; s < syms.length; s++) { + key = syms[s]; + if (showHidden || isEnumerableOwn(buf, key)) + out += (out === '' ? '' : ', ') + StringG(key) + ': ' + inspect(buf[key], valueOpts); + } + return out; + } + + /** + * Buffer's util.inspect hook; also `buf.inspect`, the same function as in node. + * @param {number} [recurseTimes] Unused; the depth that matters is `ctx.depth`. + * @param {{showHidden?: boolean, depth?: number|null}} [ctx] util.inspect's options. + * @param {(value: *, options?: object) => string} [inspect] util.inspect itself. + * @returns {string} ``, plus `, key: value` per extra own property + * when called from util.inspect/console.log. + * @node ``: insertion order, then symbols + * (`Symbol(s): 1`); non-enumerables only with showHidden; an enumerable getter + * prints its value (`g: 7`); nested values honour `depth` + * (`o: { a: { b: [Object] } }`, depth 0 → `o: [Object]`); a zero-length buffer + * prints ``; breakLength/compact change nothing. Called + * directly with no context it returns the bytes only. Node 24.22.1, verified by + * the #391 probe. + * @deviates (1) A Buffer longer than INSPECT_EXTRAS_MAX_LENGTH (1024) shows its + * symbol-keyed extras but not its string-keyed ones — the cost bound recorded + * at that constant. (2) Extras render only when an `inspect` function is + * passed (util.inspect and console.log both pass one); node also renders + * them for a bare `buf.inspect(0, {})`. Both pinned Lava-only by + * buffer_inspect_extras_cap in cmd/lava/buffer_inspect_extras_test.odin. + * Not deviations but util.inspect gaps this inherits: `colors: true` leaves + * the extras uncoloured (gap, tracked in #104), and a Buffer held in another + * Buffer's property renders its own extras one depth level short (gap). + */ + function inspectHook(recurseTimes, ctx, inspect) { + var str = inspectBytes(this); + if (ctx !== null && typeof ctx === 'object' && typeof inspect === 'function') { + var extras = inspectExtras(this, ctx, inspect); + if (extras !== '') str += (TypedArrayPrototypeGetLength(this) !== 0 ? ', ' : '') + extras; + } + return ''; + } + + p.inspect = inspectHook; if (customInspectSymbol) { Object.defineProperty(p, customInspectSymbol, { - value: function () { - return inspectBuffer(this); - }, + value: inspectHook, writable: true, configurable: true, }); @@ -1238,7 +1408,8 @@ totalLength += list[i].length; } } else { - if (typeof totalLength !== 'number') throw errInvalidArgType('length', 'number', totalLength); + if (typeof totalLength !== 'number') + throw errInvalidArgType('length', 'of type number', totalLength); if (Math.floor(totalLength) !== totalLength) throw errOutOfRange('length', 'an integer', totalLength); if (totalLength < 0 || totalLength > K_MAX_LENGTH) diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js index dfe1093..b3734fd 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/internal/util.js @@ -132,12 +132,16 @@ // a string is used verbatim, anything else (number, object, …) is inspected // in its place, and returning the object itself falls through to default // formatting. + // + // The third argument is util.inspect ITSELF, as in node (`insp === util.inspect` + // is true there): a hook that inspects a sub-value starts again at depth 0 under + // whatever options it passes. It used to be a closure that inherited this call's + // options and nesting, so a hook one level down got one level less depth than + // node gives it — and Buffer's extra-property rendering, which inspects each value + // at `depth - 1` the way node does, came out a level short. if (customInspect && typeof v[customInspect] === 'function') { var nested = seen.concat([v]); - var recurse = function (val, o) { - return inspect(val, o || opts, nested, depth + 1); - }; - var custom = v[customInspect](depth, opts, recurse); + var custom = v[customInspect](depth, opts, inspectPublic); if (custom !== v) { return typeof custom === 'string' ? custom : inspect(custom, opts, nested, depth); } diff --git a/tests/node-compat/cases/70-buffer-size-inspect-parity.js b/tests/node-compat/cases/70-buffer-size-inspect-parity.js new file mode 100644 index 0000000..eb28c63 --- /dev/null +++ b/tests/node-compat/cases/70-buffer-size-inspect-parity.js @@ -0,0 +1,203 @@ +// Four Buffer divergences from node 24 (issue #391), each printed rather than asserted so +// a regression shows up as a readable diff against the node oracle. +// +// 1. ERR_INVALID_ARG_TYPE wording: size/offset/length checks spliced a bare `number` +// into the template — `must be number` where node says `must be of type number`. +// 2. Deprecated `Buffer(n)` / `new Buffer(n)`: -1 threw JSC's codeless +// "length cannot be negative" and NaN returned an empty Buffer; node routes the +// number branch through Buffer.alloc's size validation. +// 3. INSPECT_MAX_BYTES = 0: node keeps the separator space even with no hex shown, +// `` (two spaces). +// 4. util.inspect shows a Buffer's extra own properties after the bytes, +// ``, honouring showHidden and depth. +// +// Every line prints a RESULT. A case whose setup threw on both runtimes would print the +// same THREW line twice and pass while proving nothing (CLAUDE.md §6), so each THREW +// below is the behaviour under test, and the surrounding lines print real values. +// +// The out-of-range bound is masked: node reports 2**53-1, Lava its documented JSC-family +// kMaxLength (4 GiB, reference/node-compatibility.md). Masking through +// buffer.constants.MAX_LENGTH (the precedent in 34-oversized-allocation.js) keeps the +// line byte-identical while still proving the message quotes the advertised ceiling — +// a hard-coded or missing bound would not match the mask and would print verbatim. +const util = require('node:util'); +const buffer = require('node:buffer'); + +// node prints DEP0005 for Buffer(n) on stderr, which the runner compares; Lava emits no +// deprecation warnings. Silenced so the compared output is the behaviour under test. +process.noDeprecation = true; + +const MAX = String(buffer.constants.MAX_LENGTH); +function show(label, fn) { + let r; + try { + const v = fn(); + r = 'ok ' + (Buffer.isBuffer(v) ? 'Buffer(' + v.length + ') ' + v.toString('hex') : String(v)); + } catch (e) { + r = + 'THREW ' + + e.name + + ' ' + + e.code + + ' | ' + + String(e.message) + .split('<= ' + MAX) + .join('<= kMaxLength'); + } + console.log(label + ' => ' + r); +} + +// --- 1. ERR_INVALID_ARG_TYPE wording --------------------------------------------------- +const b = Buffer.alloc(16); +show('alloc("5")', () => Buffer.alloc('5')); +show('allocUnsafe(null)', () => Buffer.allocUnsafe(null)); +show('allocUnsafeSlow({})', () => Buffer.allocUnsafeSlow({})); +show('readUInt8("5")', () => b.readUInt8('5')); +show('readInt32LE(null)', () => b.readInt32LE(null)); +show('readDoubleBE(true)', () => b.readDoubleBE(true)); +show('readBigInt64LE({})', () => b.readBigInt64LE({})); +show('writeUInt16LE(0, "1")', () => b.writeUInt16LE(0, '1')); +show('writeBigUInt64BE(0n, 1n)', () => b.writeBigUInt64BE(0n, 1n)); +show('readIntBE("0", 3)', () => b.readIntBE('0', 3)); +show('writeUIntLE(1, null, 3)', () => b.writeUIntLE(1, null, 3)); +show('concat([a], "5")', () => Buffer.concat([Buffer.from('ab')], '5')); +// Call sites that were already correct keep their wording. +show('readUIntLE(0, "3") byteLength', () => b.readUIntLE(0, '3')); +show('concat("x")', () => Buffer.concat('x')); +show('copy(5)', () => b.copy(5)); +// Valid calls still answer. +show('alloc(3)', () => Buffer.alloc(3)); +show('readUInt8(1)', () => b.readUInt8(1)); +show('concat([a], 1)', () => Buffer.concat([Buffer.from('ab')], 1)); + +// --- 2. Deprecated constructor, number branch ------------------------------------------ +const nums = [ + ['-1', -1], + ['-0', -0], + ['0', 0], + ['1.5', 1.5], + ['5', 5], + ['NaN', NaN], + ['Infinity', Infinity], + ['-Infinity', -Infinity], + ['2**53', 2 ** 53], +]; +for (const [n, v] of nums) { + show(`new Buffer(${n})`, () => new Buffer(v)); + show(`Buffer(${n})`, () => Buffer(v)); +} +// Pooled storage would leak stale bytes here; node zero-fills. +show('Buffer(5) after dirtying the pool', () => { + Buffer.allocUnsafe(8).fill(0xaa); + return Buffer(5); +}); + +// --- 3. Separator with nothing shown --------------------------------------------------- +function bufOf(n) { + const x = Buffer.alloc(n); + for (let i = 0; i < n; i++) x[i] = i; + return x; +} +for (const max of [0, 1, 2]) { + buffer.INSPECT_MAX_BYTES = max; + for (const len of [0, 1, 2, 3]) { + const x = bufOf(len); + console.log( + `max=${max} len=${len}`, + JSON.stringify(util.inspect(x)), + JSON.stringify(x.inspect()), + ); + } +} +buffer.INSPECT_MAX_BYTES = 50; + +// --- 4. Extra own properties ----------------------------------------------------------- +const sym = Symbol('s'); +const cases = []; +{ + const x = bufOf(3); + x.foo = 'bar'; + cases.push(['string prop', x]); +} +{ + const x = bufOf(3); + x.b = 2; + x.a = 1; + cases.push(['insertion order b,a', x]); +} +{ + const x = bufOf(3); + x[sym] = 1; + x.after = 'str'; + cases.push(['symbol after string', x]); +} +{ + const x = bufOf(3); + Object.defineProperty(x, 'hid', { value: 1, enumerable: false }); + cases.push(['non-enumerable', x]); +} +{ + const x = bufOf(3); + Object.defineProperty(x, 'g', { get: () => 7, enumerable: true }); + cases.push(['enumerable getter', x]); +} +{ + const x = bufOf(3); + x.o = { a: { b: { c: 1 } } }; + cases.push(['nested object', x]); +} +{ + const x = bufOf(3); + x['a-b'] = 1; + x.$d = 2; + x._ok = 3; + cases.push(['key quoting', x]); +} +{ + const x = bufOf(0); + x.foo = 'bar'; + cases.push(['zero-length', x]); +} +{ + const x = bufOf(10); + x.foo = 'bar'; + cases.push(['truncated max=4', x, 4]); +} +{ + const x = bufOf(3); + x.foo = 'bar'; + cases.push(['max=0', x, 0]); +} +const opts = [ + ['default', undefined], + ['showHidden', { showHidden: true }], + ['depth0', { depth: 0 }], + ['depthNull', { depth: null }], + ['breakLength20', { breakLength: 20 }], + ['compact false', { compact: false }], +]; +for (const [label, x, max] of cases) { + buffer.INSPECT_MAX_BYTES = max === undefined ? 50 : max; + for (const [on, o] of opts) console.log(`${label} ${on}:`, JSON.stringify(util.inspect(x, o))); + console.log(`${label} nested:`, JSON.stringify(util.inspect({ k: x }))); + console.log(`${label} nested depth0:`, JSON.stringify(util.inspect({ k: x }, { depth: 0 }))); + console.log(`${label} x.inspect():`, JSON.stringify(x.inspect())); + console.log(`${label} x[custom]():`, JSON.stringify(x[util.inspect.custom]())); + // Concatenated, not passed as a second argument: a `%O` in console.log's FIRST + // argument is itself a format directive. + console.log(label + ' format(%O): ' + JSON.stringify(util.format('%O', x))); +} +buffer.INSPECT_MAX_BYTES = 50; +// console.log renders through the same hook (stdout is compared byte-for-byte). +{ + const x = bufOf(3); + x.foo = 'bar'; + console.log(x); + console.log([x]); +} + +// The hook's third argument is util.inspect itself, so a custom hook that inspects a +// sub-value starts again at depth 0 rather than inheriting its caller's nesting. +const custom = util.inspect.custom; +console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp({ a: { b: { c: 1 } } }) } })); +console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp === util.inspect } })); From c5fd26880ef566f9dff3ed5d60f3059c30335b80 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 15:48:00 +0000 Subject: [PATCH 02/61] fix(buffer): close the pr-gate findings on Buffer inspect extras (#391) Review of cf850ca found one regression, one cost and several gaps in the new extras rendering. All verified against node 24.22.1. - console.js still called the inspect.custom hook with {} and a one-argument closure, so console.log printed a Buffer's nested extras at the wrong depth (four levels deeper than node) and got Map extras wrong. It now passes a depth-aware inspect the way util.js does. - A Buffer whose property reaches itself made util.inspect / console.log throw an uncaught RangeError; before #391 it returned a string. The hook now catches the engine's stack-overflow error and prints node's 'Inspection interrupted prematurely. Maximum call stack size exceeded', rethrowing anything else. Extras are inspected at the caller's depth, which also closes the Buffer-in-Buffer depth-1 gap. - buf.inspect(0, {}) with a context but no inspect function now renders extras through util.inspect, as node does. This was declared a deviation in cf850ca; it bought no speed or memory, so it is withdrawn. A bare buf.inspect() still prints bytes only, which is node's behaviour. - showHidden and depth are read as own properties of the context only: a polluted Object.prototype.showHidden exposed non-enumerable properties of every logged Buffer. Pinned by buffer_inspect_extras_pollution. - Two mutants survived the oracle (the symbol-loop enumerable filter, and the leading-digit key check); case 70 now kills both. Deviation, narrowed: string-keyed extras are listed only for Buffers of at most 64 bytes (was 1024). At 1024 one inspect cost ~92us against ~5us for the bytes-only hook. At 64 it is ~9.0-10.0us against ~2.9-3.8us (about 2.8x), and inspect of an empty Buffer is ~0.35us against ~0.12us; above the cap the scan is skipped. Method: mean per call over max(20, 2e6/n) calls after 200 warm-up calls, performance.now(), three interleaved launches per arm, base arm = the head binary with the pre-#391 hook swapped in. Numbers and method are in the comment at INSPECT_EXTRAS_MAX_LENGTH; pinned at 64/65 by buffer_inspect_extras_cap. reference/node-compatibility.md: the Buffer 'Rendering' bullet now describes the extras, the 64-byte deviation and its pin. The probe is unchanged at 224 differing lines of 904; no line that matched at cf850ca differs now. Manifest entries are again not in the commit (tests/mutation-manifest.json is human-only). Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YQTDDUu9z73JRY3RpV6Adg --- cmd/lava/buffer_inspect_extras_test.odin | 91 ++++++++--- pkg/runtime/js/console.js | 43 ++++-- pkg/runtime/js/internal/buffer.js | 144 +++++++++++++----- reference/node-compatibility.md | 2 +- .../cases/70-buffer-size-inspect-parity.js | 78 ++++++++++ 5 files changed, 282 insertions(+), 76 deletions(-) diff --git a/cmd/lava/buffer_inspect_extras_test.odin b/cmd/lava/buffer_inspect_extras_test.odin index b9a5256..11c851c 100644 --- a/cmd/lava/buffer_inspect_extras_test.odin +++ b/cmd/lava/buffer_inspect_extras_test.odin @@ -1,26 +1,25 @@ package main -// Lava-only: pins the two places where Buffer's util.inspect hook deliberately -// renders LESS than node 24 (issue #391), per CLAUDE.md §1. +// Lava-only: pins where Buffer's util.inspect hook deliberately renders LESS than node 24 +// (issue #391), per CLAUDE.md §1, and one pollution property node cannot oracle. // -// Why this is not an oracle case. Both behaviours differ from node by design, so a -// node-vs-Lava diff would only ever be red: +// Why these are not oracle cases. // -// 1. The string-key scan is capped at INSPECT_EXTRAS_MAX_LENGTH (1024) bytes -// (pkg/runtime/js/internal/buffer.js). JS cannot list a typed array's named own -// properties without first materialising one string per index, so an uncapped -// scan made `console.log(chunk)` O(length) — 13.8 ms for a 64 KiB Buffer and 11 s -// for 1e7 bytes, measured on bin/lava. Past the cap, string-keyed extras are -// omitted; symbol-keyed ones still show (Object.getOwnPropertySymbols lists no -// indices). Node shows both at any length. -// 2. Extras render only when the hook is handed an inspect function, which -// util.inspect and console.log always do. A bare `buf.inspect()` therefore shows -// the bytes only — which matches node for the no-argument call this pins; node -// would also render extras for `buf.inspect(0, {})`, which Lava does not. +// 1. The string-key scan is capped at INSPECT_EXTRAS_MAX_LENGTH (64) bytes +// (pkg/runtime/js/internal/buffer.js, where the measurements and their method are +// recorded). JS cannot list a typed array's named own properties without first +// materialising one string per index, so the scan is O(length) on every inspect. +// Past the cap, string-keyed extras are omitted; symbol-keyed ones still show +// (Object.getOwnPropertySymbols lists no indices). Node shows both at any length, +// so a node-vs-Lava diff here would only ever be red. The 64/65 pair sits exactly +// on the cap, so moving the constant or dropping the length check turns this red. +// The bare `buf.inspect()` assertion is node's own behaviour, kept beside it so a +// change that renders extras without a context is caught too. +// 2. The hook reads `showHidden`/`depth` as OWN properties of the options object. +// Polluting Object.prototype in an oracle case would leak into every other line +// it prints, so the property is pinned here instead. // -// The 1024/1025 pair sits exactly on the cap, so moving the constant or dropping the -// length check turns this red. The script throws on the first mismatch, so the -// message in the failed expectation names it. +// Each script throws on the first mismatch, so the failed expectation names it. import "core:testing" import "lava:pkg/runtime/eventloop" @@ -34,19 +33,19 @@ function check(label, got, want) { } // At the cap: the string-keyed property is scanned and shown. -const atCap = Buffer.alloc(1024); +const atCap = Buffer.alloc(64); atCap.tag = 1; -check('1024 shows string key', util.inspect(atCap).endsWith(', tag: 1>'), true); +check('64 shows string key', util.inspect(atCap).endsWith(', tag: 1>'), true); // One past the cap: the string key is omitted, the symbol key still shows. -const pastCap = Buffer.alloc(1025); +const pastCap = Buffer.alloc(65); pastCap.tag = 1; pastCap[sym] = 2; const past = util.inspect(pastCap); -check('1025 omits string key', past.includes('tag: 1'), false); -check('1025 shows symbol key', past.endsWith(' more bytes, Symbol(s): 2>'), true); +check('65 omits string key', past.includes('tag: 1'), false); +check('65 shows symbol key', past.endsWith(' more bytes, Symbol(s): 2>'), true); -// A bare call receives no inspect function and renders the bytes only. +// A bare call has no context and renders the bytes only (node does the same)... const small = Buffer.from([1, 2, 3]); small.tag = 1; check('bare inspect()', small.inspect(), ''); @@ -56,6 +55,33 @@ check('util.inspect same buffer', util.inspect(small), "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/pkg/runtime/js/console.js b/pkg/runtime/js/console.js index a2515b8..7f0a64d 100644 --- a/pkg/runtime/js/console.js +++ b/pkg/runtime/js/console.js @@ -12,7 +12,11 @@ return "'" + s.replaceAll('\\', '\\\\').replaceAll("'", "\\'").replaceAll('\n', '\\n') + "'"; } - function inspect(v, seen, depth) { + // `maxDepth` is the nesting cut-off. The prelude's own callers omit it and keep the + // historical 4 (node's console.log cuts at 2 — a pre-existing gap, not touched here); + // inspectWithOptions passes a caller's `depth` through. + function inspect(v, seen, depth, maxDepth) { + if (maxDepth === undefined) maxDepth = 4; var t = typeof v; if (v === null) return 'null'; if (t === 'undefined') return 'undefined'; @@ -28,39 +32,47 @@ // Honor the util.inspect custom hook (Buffer renders as ""). Node // passes (depth, options, inspect) and inspects a non-string result in place // (a string is used verbatim; returning the object itself falls through). + // + // The third argument mirrors internal/util.js: an inspect that starts again at + // depth 0 under the options it is given, as node's util.inspect does. It used to + // be a one-argument closure that dropped those options and continued this call's + // nesting, so Buffer's extra properties printed at the prelude's depth instead of + // node's (`console.log(b)` with `b.o` five levels deep showed four of them). if (customInspect && typeof v[customInspect] === 'function') { - var nested = seen.concat([v]); - var recurse = function (val) { - return inspect(val, nested, depth + 1); - }; - var custom = v[customInspect](depth, {}, recurse); + var custom = v[customInspect](depth, {}, inspectWithOptions); if (custom !== v) { - return typeof custom === 'string' ? custom : inspect(custom, nested, depth); + return typeof custom === 'string' + ? custom + : inspect(custom, seen.concat([v]), depth, maxDepth); } } if (seen.indexOf(v) !== -1) return '[Circular *1]'; if (v instanceof Error) return v.stack ? v.stack : v.name + ': ' + v.message; if (v instanceof Date) return isNaN(v.getTime()) ? 'Invalid Date' : v.toISOString(); if (v instanceof RegExp) return v.toString(); - if (depth > 4) return Array.isArray(v) ? '[Array]' : '[Object]'; + if (depth > maxDepth) return Array.isArray(v) ? '[Array]' : '[Object]'; seen = seen.concat([v]); if (Array.isArray(v)) { if (v.length === 0) return '[]'; var arr = []; - for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], seen, depth + 1)); + for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], seen, depth + 1, maxDepth)); return '[ ' + arr.join(', ') + ' ]'; } if (typeof Map !== 'undefined' && v instanceof Map) { var mp = []; v.forEach(function (val, key) { - mp.push(inspect(key, seen, depth + 1) + ' => ' + inspect(val, seen, depth + 1)); + mp.push( + inspect(key, seen, depth + 1, maxDepth) + + ' => ' + + inspect(val, seen, depth + 1, maxDepth), + ); }); return 'Map(' + v.size + ') {' + (mp.length > 0 ? ' ' + mp.join(', ') + ' ' : '') + '}'; } if (typeof Set !== 'undefined' && v instanceof Set) { var st = []; v.forEach(function (val) { - st.push(inspect(val, seen, depth + 1)); + st.push(inspect(val, seen, depth + 1, maxDepth)); }); return 'Set(' + v.size + ') {' + (st.length > 0 ? ' ' + st.join(', ') + ' ' : '') + '}'; } @@ -72,11 +84,18 @@ for (var k = 0; k < keys.length; k++) { var key = keys[k]; var label = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : quote(key); - props.push(label + ': ' + inspect(v[key], seen, depth + 1)); + props.push(label + ': ' + inspect(v[key], seen, depth + 1, maxDepth)); } return prefix + '{ ' + props.join(', ') + ' }'; } + // The hook's inspect argument, util.inspect-shaped: (value, options) from depth 0, with + // node's default depth of 2 and `depth: null` meaning unbounded. + function inspectWithOptions(value, options) { + var d = options !== null && typeof options === 'object' ? options.depth : undefined; + return inspect(value, [], 0, d === undefined ? 2 : d === null ? Infinity : d); + } + function stringify(v) { return typeof v === 'string' ? v : inspect(v, [], 0); } diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 3b6190e..1a5145c 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -823,18 +823,29 @@ * JS has no way to list a typed array's NAMED own properties without first listing * every index: Object.getOwnPropertyNames / Object.keys / Reflect.ownKeys / for-in * all materialise one string per byte (node avoids this with its native - * getOwnNonIndexProperties). Measured on bin/lava, one launch per size: timed with - * performance.now() around max(20, 2e6/n) back-to-back Object.getOwnPropertyNames - * calls on one Buffer.alloc(n), reported as the mean per call — 1.2µs at n=16, 18µs - * at n=256, 86µs at n=1024, 13.8ms at n=65536 — against 2.6µs mean for the whole - * bytes-only util.inspect of the same Buffer timed the same way. A single - * Reflect.ownKeys on Buffer.alloc(1e7) took 11.1s (one call, Date.now()). Unbounded, - * `console.log(chunk)` on a 64 KiB read chunk would cost ~5000x what it does today, - * so the scan is bounded and the rest is a deviation, pinned Lava-only by - * cmd/lava/buffer_inspect_extras_test.odin (buffer_inspect_extras_cap). + * getOwnNonIndexProperties). So every util.inspect/console.log of a Buffer at or + * under this length pays that scan, extras or not. + * + * Cost of the scan alone, measured on bin/lava, one launch per size: performance.now() + * around max(20, 2e6/n) back-to-back Object.getOwnPropertyNames calls on one + * Buffer.alloc(n), mean per call — 1.2µs at n=16, 18µs at n=256, 86µs at n=1024, + * 13.8ms at n=65536; a single Reflect.ownKeys on Buffer.alloc(1e7) took 11.1s (one + * call, Date.now()). Unbounded, logging a 64 KiB read chunk would cost thousands of + * times what the bytes-only render does. + * + * Cost AT the cap, whole util.inspect(Buffer.alloc(n)) with no extras, head vs the + * base hook (this binary with the pre-#391 bytes-only hook swapped in), mean per call + * over max(20, 2e6/n) calls after 200 warm-ups, performance.now(), 3 interleaved + * launches per arm: n=64 head 9.0/9.9/10.0µs vs base 2.9/3.4/3.8µs (~2.8x); n=65 + * (just past the cap) 4.1/4.5/4.7 vs 3.2/3.3/4.7µs; n=16 2.3/2.4/2.7 vs 1.0/1.2/1.6µs; + * n=0 0.35/0.35/0.38 vs 0.12/0.12/0.13µs. At the previous cap of 1024 the head cost + * was ~92µs against ~5µs. + * + * Past the cap, string-keyed extras are omitted — a deviation, pinned Lava-only by + * buffer_inspect_extras_cap in cmd/lava/buffer_inspect_extras_test.odin. * Symbol keys are always shown: Object.getOwnPropertySymbols lists no indices. */ - var INSPECT_EXTRAS_MAX_LENGTH = 1024; + var INSPECT_EXTRAS_MAX_LENGTH = 64; /** @returns {boolean} Whether `key` is an own enumerable property of `obj`. */ function isEnumerableOwn(obj, key) { @@ -864,20 +875,27 @@ * * Keys are formatted here rather than by building a null-prototype object and * inspecting that, which is what node does: Lava's util.inspect drops symbol keys - * and prints no `[Object: null prototype]` prefix to slice off. Values go through - * the hook's `inspect` argument one level down — node inspects the property bag at - * depth 0, so each VALUE sits at depth 1 under the caller's own `depth`; asking a - * fresh inspect for `depth - 1` is the same cut-off for those values. It is not the - * same for a Buffer nested in a property: that inner hook sees `depth - 1` where node - * hands it the caller's depth unchanged — a gap, not intended behaviour. + * and prints no `[Object: null prototype]` prefix to slice off. + * + * Each value is inspected wrapped in a one-element array and the `[ ` / ` ]` sliced + * off. Node inspects the property bag at depth 0, so a value sits at depth 1 under + * the caller's own `depth`, and a Buffer nested in a property receives that SAME + * depth for its own extras. The wrapper puts the value at depth 1 without touching + * `depth`; an earlier revision passed `depth - 1` instead, which got the values right + * but handed a nested Buffer one level less than node does. + * + * `opts` is rebuilt from ctx's OWN properties: ctx is the caller's options object, so + * an inherited read would let a polluted `Object.prototype.showHidden` list hidden + * properties of every logged Buffer, or `Object.prototype.depth = null` lift the + * depth bound (node copies only the caller's own keys). Pinned Lava-only by + * buffer_inspect_extras_pollution in cmd/lava/buffer_inspect_extras_test.odin. */ function inspectExtras(buf, ctx, inspect) { - var showHidden = !!ctx.showHidden; - var depth = ctx.depth; - var valueOpts = { + var showHidden = ObjectPrototypeHasOwnProperty(ctx, 'showHidden') && !!ctx.showHidden; + var opts = { __proto__: null, - depth: depth === null ? null : (depth === undefined ? 2 : depth) - 1, - showHidden: ctx.showHidden, + depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined, + showHidden: showHidden, }; var out = ''; var len = TypedArrayPrototypeGetLength(buf); @@ -894,46 +912,94 @@ (out === '' ? '' : ', ') + (isPlainKey(key) ? key : inspect(key)) + ': ' + - inspect(buf[key], valueOpts); + inspectValue(buf[key], opts, inspect); } } var syms = ObjectGetOwnPropertySymbols(buf); for (var s = 0; s < syms.length; s++) { key = syms[s]; if (showHidden || isEnumerableOwn(buf, key)) - out += (out === '' ? '' : ', ') + StringG(key) + ': ' + inspect(buf[key], valueOpts); + out += + (out === '' ? '' : ', ') + StringG(key) + ': ' + inspectValue(buf[key], opts, inspect); } return out; } + /** @returns {string} `value` inspected one level down (see inspectExtras). */ + function inspectValue(value, opts, inspect) { + var wrapped = inspect([value], opts); + // Both Lava inspectors render a one-element array as `[ x ]` on one line. Only a + // negative depth turns the wrapper itself into `[Array]`; fall back to the value. + if ( + StringPrototypeSlice(wrapped, 0, 2) === '[ ' && + StringPrototypeSlice(wrapped, wrapped.length - 2) === ' ]' + ) + return StringPrototypeSlice(wrapped, 2, wrapped.length - 2); + return inspect(value, opts); + } + + // JSC's stack-overflow RangeError text (node's V8 text is the same sentence). + var STACK_OVERFLOW_MESSAGE = 'Maximum call stack size exceeded.'; + + /** @type {((value: *, options?: object) => string)|null} util.inspect, once resolved. */ + var utilInspect = null; + /** * Buffer's util.inspect hook; also `buf.inspect`, the same function as in node. * @param {number} [recurseTimes] Unused; the depth that matters is `ctx.depth`. * @param {{showHidden?: boolean, depth?: number|null}} [ctx] util.inspect's options. - * @param {(value: *, options?: object) => string} [inspect] util.inspect itself. + * @param {(value: *, options?: object) => string} [inspect] The caller's inspect: + * util.inspect from util.inspect, the prelude's own from console.log. * @returns {string} ``, plus `, key: value` per extra own property - * when called from util.inspect/console.log. + * whenever `ctx` is an object. * @node ``: insertion order, then symbols * (`Symbol(s): 1`); non-enumerables only with showHidden; an enumerable getter * prints its value (`g: 7`); nested values honour `depth` * (`o: { a: { b: [Object] } }`, depth 0 → `o: [Object]`); a zero-length buffer - * prints ``; breakLength/compact change nothing. Called - * directly with no context it returns the bytes only. Node 24.22.1, verified by - * the #391 probe. - * @deviates (1) A Buffer longer than INSPECT_EXTRAS_MAX_LENGTH (1024) shows its - * symbol-keyed extras but not its string-keyed ones — the cost bound recorded - * at that constant. (2) Extras render only when an `inspect` function is - * passed (util.inspect and console.log both pass one); node also renders - * them for a bare `buf.inspect(0, {})`. Both pinned Lava-only by - * buffer_inspect_extras_cap in cmd/lava/buffer_inspect_extras_test.odin. - * Not deviations but util.inspect gaps this inherits: `colors: true` leaves - * the extras uncoloured (gap, tracked in #104), and a Buffer held in another - * Buffer's property renders its own extras one depth level short (gap). + * prints ``; breakLength/compact change nothing. A direct + * `buf.inspect(0, {})` renders the extras too; `buf.inspect()` with no context + * prints the bytes only. A Buffer whose extras lead back to itself recurses until + * the stack runs out and ends in `Inspection interrupted prematurely. Maximum + * call stack size exceeded` instead of throwing. Node 24.22.1, verified by the + * #391 probes. + * @deviates A Buffer longer than INSPECT_EXTRAS_MAX_LENGTH (64) shows its symbol-keyed + * extras but not its string-keyed ones — the cost bound recorded at that + * constant. Pinned Lava-only by buffer_inspect_extras_cap in + * cmd/lava/buffer_inspect_extras_test.odin. Not a deviation but a util.inspect + * gap this inherits: `colors: true` leaves the extras uncoloured (gap, + * tracked in #104). */ function inspectHook(recurseTimes, ctx, inspect) { var str = inspectBytes(this); - if (ctx !== null && typeof ctx === 'object' && typeof inspect === 'function') { - var extras = inspectExtras(this, ctx, inspect); + if (ctx !== null && typeof ctx === 'object') { + if (typeof inspect !== 'function') { + // A direct `buf.inspect(0, {})` passes no inspect function; node still renders + // the extras with its own. util is resolved HERE, on first such call, not at + // module eval: buffer is on the loader's eager list and util is not, so a + // module-eval require would instantiate util (and its dependencies) before user + // code on every start. The read goes through util's exports object, which user + // code can also reach — a replaced `util.inspect` steers this fallback alone; + // util.inspect and console.log hand the hook their own function. + if (utilInspect === null) utilInspect = require('util').inspect; + inspect = utilInspect; + } + var extras; + try { + extras = inspectExtras(this, ctx, inspect); + } catch (e) { + // Node catches a stack overflow inside inspect and prints what it had; a + // Buffer that reaches itself through its extras recurses until that happens. + // Logging must not throw. Any other error (a throwing getter) propagates, as + // in node. + if ( + e === null || + typeof e !== 'object' || + e.name !== 'RangeError' || + e.message !== STACK_OVERFLOW_MESSAGE + ) + throw e; + extras = 'Inspection interrupted prematurely. Maximum call stack size exceeded'; + } if (extras !== '') str += (TypedArrayPrototypeGetLength(this) !== 0 ? ', ' : '') + extras; } return ''; diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index b0d29a4..a89a5b4 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -160,7 +160,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. - **UTF-8 decode (issue #107):** `toString('utf8')` / `Buffer.from(string)` use the Unicode "U+FFFD substitution of maximal subparts" rule (the native Odin decoder, [buffer.odin](../pkg/runtime/buffer.odin)), matching Node/V8: an ill-formed subsequence collapses to a single replacement (`Buffer.from([0xF0,0x9F]).toString()` → `'�'`, not `'��'`). Embedded NUL is preserved. - **Encodings:** `utf8`/`utf-8`, `utf16le`/`ucs2`, `latin1`/`binary`, `ascii`, `hex`, `base64`, `base64url` (with Node's lenient base64 normalization and `ascii` high-bit masking). - **Errors:** Node `err.code`s and message shapes for `ERR_INVALID_ARG_TYPE`, `ERR_INVALID_ARG_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_BUFFER_OUT_OF_BOUNDS`, `ERR_UNKNOWN_ENCODING`, `ERR_INVALID_BUFFER_SIZE`. -- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. +- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. A Buffer's extra own properties are rendered after the bytes, as in Node: ``. They appear in insertion order with symbol keys last, non-enumerable ones only under `showHidden`, and nested values cut off at `depth`. A direct `buf.inspect(0, {})` renders them too, and a Buffer whose extras lead back to itself ends in Node's `Inspection interrupted prematurely` marker instead of throwing. One deviation: a Buffer **longer than 64 bytes** shows its symbol-keyed extras but not its string-keyed ones. JavaScript cannot list a typed array's named properties without first making one string per index, so without the cap every inspect would cost O(length). At the cap the whole inspect costs about 2.8x the bytes-only render (the measurement and its method are recorded at `INSPECT_EXTRAS_MAX_LENGTH` in [buffer.js](../pkg/runtime/js/internal/buffer.js)). The deviation is pinned by `cmd/lava/buffer_inspect_extras_test.odin`. `colors: true` leaves the extras uncoloured; that is a `util.inspect` gap (#104), not a Buffer one. - **Module extras:** `Blob`, `File`, `SlowBuffer`, `atob`/`btoa`, `isAscii`, `isUtf8`, `transcode`, `resolveObjectURL`, `kMaxLength`/`kStringMaxLength`/`constants`/`INSPECT_MAX_BYTES`. `Blob`/`File` honor the `endings: 'native'` option (string parts have `\n`/`\r\n` converted to the platform newline — derived from `process.platform` — while a lone `\r` and binary parts are left verbatim, matching Node). **Intentional differences / deferred:** diff --git a/tests/node-compat/cases/70-buffer-size-inspect-parity.js b/tests/node-compat/cases/70-buffer-size-inspect-parity.js index eb28c63..a65705a 100644 --- a/tests/node-compat/cases/70-buffer-size-inspect-parity.js +++ b/tests/node-compat/cases/70-buffer-size-inspect-parity.js @@ -201,3 +201,81 @@ buffer.INSPECT_MAX_BYTES = 50; const custom = util.inspect.custom; console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp({ a: { b: { c: 1 } } }) } })); console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp === util.inspect } })); + +// console.log must cut nested extras at node's depth too, not at the prelude's own +// limit: the extras' values sit one level below the Buffer whatever its nesting. +{ + const x = bufOf(1); + x.o = { a: { b: { c: { d: { e: 1 } } } } }; + console.log(x); + console.log([x]); + console.log({ k: { x } }); + const m = bufOf(1); + m.meta = new Map([[1, { a: { b: { c: 1 } } }]]); + console.log(m); +} + +// Called directly WITH a context object, the hook renders the extras as well; with no +// context it prints the bytes only. +{ + const x = bufOf(3); + x.tag = 1; + Object.defineProperty(x, 'h', { value: 5, enumerable: false }); + x.o = { a: { b: { c: 1 } } }; + console.log('inspect(0, {}):', JSON.stringify(x.inspect(0, {}))); + console.log('inspect(0, {depth: 0}):', JSON.stringify(x.inspect(0, { depth: 0 }))); + console.log('[custom](0, {showHidden}):', JSON.stringify(x[custom](0, { showHidden: true }))); + console.log( + 'inspect():', + JSON.stringify(x.inspect()), + 'inspect(0):', + JSON.stringify(x.inspect(0)), + ); +} + +// Key rendering: node's identifier test refuses a leading digit ('1a' is quoted, 'a1' +// is not), and a non-enumerable SYMBOL key shows only under showHidden. +{ + const x = bufOf(3); + x['1a'] = 1; + x.a1 = 2; + Object.defineProperty(x, Symbol('h'), { value: 3, enumerable: false }); + x[Symbol('v')] = 4; + console.log('keys:', JSON.stringify(util.inspect(x))); + console.log('keys showHidden:', JSON.stringify(util.inspect(x, { showHidden: true }))); +} + +// A Buffer whose extras lead back to itself recurses until the stack runs out. node +// catches the overflow inside inspect and prints what it had, ending in +// `Inspection interrupted prematurely. Maximum call stack size exceeded`; logging never +// throws. Where the cut lands depends on the engine's stack size, so the lines print the +// shape rather than the full string: it returned, how it starts, that the marker is +// there, and how it ends. +function cycleShape(label, fn) { + let s; + try { + s = fn(); + } catch (e) { + console.log(label, 'THREW', e.name); + return; + } + console.log( + label, + JSON.stringify(s.slice(0, 40)), + s.includes('Inspection interrupted prematurely. Maximum call stack size exceeded'), + JSON.stringify(s.slice(-4)), + ); +} +{ + const a = Buffer.from([1]); + a.self = a; + cycleShape('cycle self:', () => util.inspect(a)); + const b = Buffer.from([2]); + const c = Buffer.from([3]); + b.other = c; + c.other = b; + cycleShape('cycle pair:', () => util.inspect(b)); + const d = Buffer.from([4]); + d.o = { back: d }; + cycleShape('cycle via object:', () => util.inspect(d)); +} From 49bc4899edbadb132a03d5fac0ecd0fd8e47acf8 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:53:34 +0000 Subject: [PATCH 03/61] fix(inspect): bound cyclic custom-inspect hooks; pin the rethrow path (#391) The cycle handling added in c5fd268 had two regressions, both found on re-review against node 24.22.1. - A Buffer with two or more self-references hung: the overflow catch recovered only in the innermost hook, and every enclosing hook then descended to the stack limit again, so work grew like k^depth. 'b.k0=b; b.k1=b; console.log(b)' ran past 60 s. The overflow is now caught and latched in the inspect function a hook is handed (util.js hookInspect, console.js inspectWithOptions); once latched nothing descends again. Three self-references now throw in ~76-79 ms. - The inspect passed to a custom hook restarted with an empty visited set, so a user hook calling inspect(parent) threw RangeError where the base printed a circular marker. Fixed by catching at the hook boundary, as node does. Carrying the visited set instead changed a line that matches node today. Node's outcomes, recorded from a fresh process: one self-reference, a two-Buffer cycle and a cycle through an object all return with 'Inspection interrupted prematurely'; two or three self-references throw. Node's throw is a SyntaxError from a regex compiled lazily at the stack limit, and once that regex is warm the same input runs for minutes; lava throws a RangeError. The shape (returns vs throws) is pinned, the error type is not. Also: - An extra's getter is read outside the catch, so an error it throws, including a forged overflow RangeError, propagates as in node. The catch-everything mutant now fails case 70. - The util.inspect fallback for buf.inspect(0, {}) is no longer cached: the loader publishes no pristine util handle, and a cached one kept a user-replaced util.inspect forever. It is still a live read; noted at the site. - console.js reads depth as an own property only. - The options object for extras is built on the first extra, not per call. - Comments: the cost table is re-measured at 300 000 calls per launch. At the 64-byte cap one inspect is 7.3-9.1 us against 1.9-3.0 us (about 3.5x, not the 2.8x stated before); at 1024 bytes 2.91 us against 2.13-2.33 us. Tests: case 70 gains the 2- and 3-reference shapes and the getter-throws lines; new case 71 covers a user hook that inspects its ancestor; Odin tests buffer_inspect_cycle_bounded (30 s fail-timeout) and buffer_inspect_hook_ancestor_cycle pin the bounded path and console.log. Not done: recognising the engine's own overflow error without a name and message comparison is not possible from JS; node uses the same test. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YQTDDUu9z73JRY3RpV6Adg --- cmd/lava/buffer_inspect_extras_test.odin | 139 +++++++++++++++ pkg/runtime/js/console.js | 57 ++++++- pkg/runtime/js/internal/buffer.js | 158 ++++++++++++------ pkg/runtime/js/internal/util.js | 66 +++++++- reference/node-compatibility.md | 2 +- .../cases/70-buffer-size-inspect-parity.js | 75 +++++++++ .../cases/71-inspect-hook-ancestor-cycle.js | 45 +++++ 7 files changed, 484 insertions(+), 58 deletions(-) create mode 100644 tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js diff --git a/cmd/lava/buffer_inspect_extras_test.odin b/cmd/lava/buffer_inspect_extras_test.odin index 11c851c..36d5e7d 100644 --- a/cmd/lava/buffer_inspect_extras_test.odin +++ b/cmd/lava/buffer_inspect_extras_test.odin @@ -19,9 +19,20 @@ package main // Polluting Object.prototype in an oracle case would leak into every other line // it prints, so the property is pinned here instead. // +// 3. Cycles, where node has no stable output to compare against. A Buffer reaching itself +// by two or more keys made Lava's per-hook overflow catch do work like k^depth +// (c5fd268: `b.k0 = b; b.k1 = b; console.log(b)` ran past 60 s). node's outcome there +// depends on whether a lazily compiled regex has been compiled yet (see case 70), so +// the property pinned here is Lava's own: it ends, by a throw, in bounded wall-clock +// time. A user hook that inspects its own ancestor must not throw out of console.log +// (Lava's console.log text cannot be captured by an oracle case: it writes natively), +// and with two ways back it must end in bounded time too. +// 4. console.log hands a hook an inspect that reads `depth` as an OWN option only. +// // Each script throws on the first mismatch, so the failed expectation names it. import "core:testing" +import "core:time" import "lava:pkg/runtime/eventloop" import lava "lava:pkg/runtime" @@ -79,6 +90,22 @@ check('inherited showHidden', plain.includes('hid'), false); check('inherited depth', plain.includes('[Object]'), true); // ...and an OWN showHidden still works, so the first check is not vacuous. check('own showHidden', shown.includes('hid: 1'), true); +// console.log's hook inspect must not take depth from Object.prototype either (M). +let viaConsole; +const deep = { a: { b: { c: { d: { e: 1 } } } } }; +const hooked = { + [util.inspect.custom](d, o, inspect) { + viaConsole = inspect(deep, {}); + return 'hooked'; + }, +}; +Object.prototype.depth = null; +try { + console.log(hooked); +} finally { + delete Object.prototype.depth; +} +check('console hook inherited depth', viaConsole, '{ a: { b: { c: [Object] } } }'); console.log('buffer-inspect-pollution ok'); ` @@ -115,3 +142,115 @@ buffer_inspect_extras_pollution :: proc(t: ^testing.T) { ) testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) } + +BUFFER_INSPECT_CYCLE_SCRIPT :: ` +const util = require('node:util'); +function check(label, got, want) { + if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); +} +// k self-references: must END, and by a throw (node's fresh-process outcome), well inside +// the bound. Before the overflow latch, k = 2 already ran past 60 s. +for (const refs of [2, 3, 8]) { + const b = Buffer.from([1]); + for (let i = 0; i < refs; i++) b['k' + i] = b; + for (const how of ['inspect', 'log']) { + const t0 = Date.now(); + let out = 'returned'; + try { + if (how === 'inspect') util.inspect(b); + else console.log(b); + } catch (e) { + out = 'threw'; + } + const ms = Date.now() - t0; + check(refs + ' refs ' + how + ' outcome', out, 'threw'); + if (ms > 5000) throw new Error(refs + ' refs ' + how + ' took ' + ms + 'ms (bound 5000)'); + } +} +// One way back still RETURNS, with node's interrupted marker, and the latch is reset for +// the next top-level call: inspecting it again returns again (a latch left set would +// make the second call throw at its first extra). Where the cut lands varies with stack +// state, so the two strings are not compared. +const one = Buffer.from([1]); +one.self = one; +check('1 ref marker', util.inspect(one).includes('Inspection interrupted prematurely'), true); +check('1 ref repeat', util.inspect(one).includes('Inspection interrupted prematurely'), true); +check('plain after cycle', util.inspect(Object.assign(Buffer.from([2]), { t: 1 })), ''); +console.log('buffer-inspect-cycle ok'); +` + +BUFFER_INSPECT_HOOK_ANCESTOR_SCRIPT :: ` +const util = require('node:util'); +function check(label, got, want) { + if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); +} +const outer = { n: 1 }; +class H { + [util.inspect.custom](d, o, inspect) { + return inspect(outer); + } +} +outer.h = new H(); +// util.inspect returns node's interrupted marker rather than throwing... +check('util.inspect', util.inspect(outer).includes('Inspection interrupted prematurely'), true); +// ...and so does console.log (c5fd268 threw RangeError out of it), in bounded time. +const t0 = Date.now(); +console.log(outer); +if (Date.now() - t0 > 5000) throw new Error('console.log took ' + (Date.now() - t0) + 'ms'); +// Two ways back to the ancestor: bounded, by a throw (the latch), never a hang. +const outer2 = { n: 2 }; +class H2 { + [util.inspect.custom](d, o, inspect) { + return inspect(outer2); + } +} +outer2.a = new H2(); +outer2.b = new H2(); +for (const how of ['inspect', 'log']) { + const t1 = Date.now(); + try { + if (how === 'inspect') util.inspect(outer2); + else console.log(outer2); + } catch {} + if (Date.now() - t1 > 5000) throw new Error('2 ways back ' + how + ' took ' + (Date.now() - t1) + 'ms'); +} +console.log('buffer-inspect-hook-ancestor ok'); +` + +@(test) +buffer_inspect_cycle_bounded :: proc(t: ^testing.T) { + // The exponential path never returns, so the script's own 5 s check would never run: + // the runner's timeout is what turns a regression into a failure instead of a CI hang. + testing.set_fail_timeout(t, 30 * time.Second) + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(BUFFER_INSPECT_CYCLE_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} + +@(test) +buffer_inspect_hook_ancestor_cycle :: proc(t: ^testing.T) { + testing.set_fail_timeout(t, 30 * time.Second) + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(BUFFER_INSPECT_HOOK_ANCESTOR_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/pkg/runtime/js/console.js b/pkg/runtime/js/console.js index 7f0a64d..8ee8575 100644 --- a/pkg/runtime/js/console.js +++ b/pkg/runtime/js/console.js @@ -7,6 +7,12 @@ var customInspect = typeof Symbol !== 'undefined' && Symbol.for ? Symbol.for('nodejs.util.inspect.custom') : null; + // Captured at prelude eval, before any user code runs. + var ObjectHasOwn = Object.hasOwn; + // Stack-overflow latch for custom hooks; the same design as internal/util.js + // callCustomInspect (copied, not shared: this prelude has no `require`). + var hookNesting = 0; + var hookOverflow = null; function quote(s) { return "'" + s.replaceAll('\\', '\\\\').replaceAll("'", "\\'").replaceAll('\n', '\\n') + "'"; @@ -37,9 +43,15 @@ // depth 0 under the options it is given, as node's util.inspect does. It used to // be a one-argument closure that dropped those options and continued this call's // nesting, so Buffer's extra properties printed at the prelude's depth instead of - // node's (`console.log(b)` with `b.o` five levels deep showed four of them). + // node's (`console.log(b)` with `b.o` five levels deep showed four of them). Like + // util.js it catches a stack overflow escaping a hook (a hook that inspects its own + // ancestor) and latches it, so console.log prints instead of throwing; see + // inspectWithOptions. + // + // A copy rather than a call into util.js because this prelude runs before the module + // loader and has no `require`; the two inspectors are to be unified under #104. if (customInspect && typeof v[customInspect] === 'function') { - var custom = v[customInspect](depth, {}, inspectWithOptions); + var custom = callCustomInspect(v, depth); if (custom !== v) { return typeof custom === 'string' ? custom @@ -90,10 +102,45 @@ } // The hook's inspect argument, util.inspect-shaped: (value, options) from depth 0, with - // node's default depth of 2 and `depth: null` meaning unbounded. + // node's default depth of 2 and `depth: null` meaning unbounded. `depth` is read as an + // OWN option only, as node copies only the caller's own keys: an inherited read let a + // polluted Object.prototype.depth = null lift the bound for every hook's sub-inspect. + // Pinned Lava-only by buffer_inspect_extras_pollution + // (cmd/lava/buffer_inspect_extras_test.odin). + // + // It also catches a stack overflow, where internal/util.js hookInspect does and for the + // same reasons (node catches it while formatting, not around the hook call; the first + // one is latched so a cycle with several ways back throws in bounded time instead of + // re-descending per key). function inspectWithOptions(value, options) { - var d = options !== null && typeof options === 'object' ? options.depth : undefined; - return inspect(value, [], 0, d === undefined ? 2 : d === null ? Infinity : d); + var d = + options !== null && typeof options === 'object' && ObjectHasOwn(options, 'depth') + ? options.depth + : undefined; + if (hookOverflow !== null) throw hookOverflow; + try { + return inspect(value, [], 0, d === undefined ? 2 : d === null ? Infinity : d); + } catch (e) { + if ( + e === hookOverflow || + e === null || + typeof e !== 'object' || + e.name !== 'RangeError' || + e.message !== 'Maximum call stack size exceeded.' + ) + throw e; + hookOverflow = e; + return '[Object: Inspection interrupted prematurely. Maximum call stack size exceeded.]'; + } + } + + function callCustomInspect(v, depth) { + hookNesting++; + try { + return v[customInspect](depth, {}, inspectWithOptions); + } finally { + if (--hookNesting === 0) hookOverflow = null; + } } function stringify(v) { diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 1a5145c..7504c02 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -835,11 +835,13 @@ * * Cost AT the cap, whole util.inspect(Buffer.alloc(n)) with no extras, head vs the * base hook (this binary with the pre-#391 bytes-only hook swapped in), mean per call - * over max(20, 2e6/n) calls after 200 warm-ups, performance.now(), 3 interleaved - * launches per arm: n=64 head 9.0/9.9/10.0µs vs base 2.9/3.4/3.8µs (~2.8x); n=65 - * (just past the cap) 4.1/4.5/4.7 vs 3.2/3.3/4.7µs; n=16 2.3/2.4/2.7 vs 1.0/1.2/1.6µs; - * n=0 0.35/0.35/0.38 vs 0.12/0.12/0.13µs. At the previous cap of 1024 the head cost - * was ~92µs against ~5µs. + * after 200 warm-ups, performance.now(), 3 interleaved launches per arm. At 300 000 + * calls per launch: n=64 head 7.3/8.5/9.1µs vs base 1.9/2.2/3.0µs (~3.5x); n=1024 (past + * the cap, so symbols only) 2.91/2.91/2.94 vs 2.13/2.24/2.33µs. At max(20, 2e6/n) calls + * per launch (31 250 at n=64, 125 000 at n=16, 2 000 000 at n=0): n=64 9.0/9.9/10.0 vs + * 2.9/3.4/3.8µs; n=65 4.1/4.5/4.7 vs 3.2/3.3/4.7µs; n=16 2.3/2.4/2.7 vs 1.0/1.2/1.6µs; + * n=0 0.35/0.35/0.38 vs 0.12/0.12/0.13µs. At the previous cap of 1024 the head cost was + * ~92µs against ~5µs. * * Past the cap, string-keyed extras are omitted — a deviation, pinned Lava-only by * buffer_inspect_extras_cap in cmd/lava/buffer_inspect_extras_test.odin. @@ -892,14 +894,12 @@ */ function inspectExtras(buf, ctx, inspect) { var showHidden = ObjectPrototypeHasOwnProperty(ctx, 'showHidden') && !!ctx.showHidden; - var opts = { - __proto__: null, - depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined, - showHidden: showHidden, - }; + // Built on the first extra, not per call: most logged Buffers have none. + var opts = null; var out = ''; var len = TypedArrayPrototypeGetLength(buf); var key; + var piece; if (len <= INSPECT_EXTRAS_MAX_LENGTH) { // A typed array's [[OwnPropertyKeys]] lists its integer indices first, in order // (ECMA-262 §10.4.5.7), and a canonical numeric key can never be added as a named @@ -907,24 +907,87 @@ var names = ObjectGetOwnPropertyNames(buf); for (var i = len; i < names.length; i++) { key = names[i]; - if (showHidden || isEnumerableOwn(buf, key)) - out += - (out === '' ? '' : ', ') + - (isPlainKey(key) ? key : inspect(key)) + - ': ' + - inspectValue(buf[key], opts, inspect); + if (showHidden || isEnumerableOwn(buf, key)) { + if (opts === null) opts = extrasOptions(ctx, showHidden); + piece = inspectExtra(isPlainKey(key) ? key : null, key, buf[key], opts, inspect); + if (piece === null) return INSPECT_OVERFLOW_MARKER; + out += (out === '' ? '' : ', ') + piece; + } } } var syms = ObjectGetOwnPropertySymbols(buf); for (var s = 0; s < syms.length; s++) { key = syms[s]; - if (showHidden || isEnumerableOwn(buf, key)) - out += - (out === '' ? '' : ', ') + StringG(key) + ': ' + inspectValue(buf[key], opts, inspect); + if (showHidden || isEnumerableOwn(buf, key)) { + if (opts === null) opts = extrasOptions(ctx, showHidden); + piece = inspectExtra(StringG(key), key, buf[key], opts, inspect); + if (piece === null) return INSPECT_OVERFLOW_MARKER; + out += (out === '' ? '' : ', ') + piece; + } } return out; } + /** @returns {object} The options each extra is inspected with (see inspectExtras). */ + function extrasOptions(ctx, showHidden) { + return { + __proto__: null, + depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined, + showHidden: showHidden, + }; + } + + /** + * Renders one extra as `label: value`, or returns null when inspecting it overflowed + * the stack (the caller then prints the interrupted marker instead of its extras). + * + * `value` arrives already read: node reads the property before its overflow catch, so + * a getter that throws — even a RangeError carrying the overflow's message — + * propagates, and only errors raised while INSPECTING the value are candidates. + * + * Overflow. A Buffer that reaches itself recurses until the stack runs out; the + * innermost catch on the stack renders the marker. What keeps that bounded is the latch + * in the inspect this hook calls through (internal/util.js hookInspect, console.js + * inspectWithOptions): once an overflow has been caught there, any hook-side inspect + * rethrows it instead of descending again. Before it, every enclosing hook moved on to + * its next key and recursed to the limit again, so `b.k0 = b; b.k1 = b` did work like + * 2^depth and never finished. With one way back nothing descends again and the call + * returns with the marker, as node's does; with two or more it throws, which is node + * 24.22.1's outcome in a fresh process (see case 70 for why node's is accidental). + * `inspectOverflow` only remembers the overflow this hook already reported, so the + * latch's rethrow of that same error passes through every enclosing Buffer hook instead + * of being swallowed again as a fresh overflow at each level. Pinned by case 70 and, + * for bounded time, buffer_inspect_cycle_bounded in + * cmd/lava/buffer_inspect_extras_test.odin. + */ + function inspectExtra(label, key, value, opts, inspect) { + try { + return (label !== null ? label : inspect(key)) + ': ' + inspectValue(value, opts, inspect); + } catch (e) { + if (e === inspectOverflow || !isStackOverflow(e)) throw e; + inspectOverflow = e; + return null; + } + } + + /** + * @returns {boolean} Whether `e` is the engine's stack-overflow RangeError. + * + * A name + message compare — the same test node's isStackOverflowError makes, so a + * forged look-alike thrown from DEEP inside an extra's value is taken for an overflow + * on both runtimes. It cannot be done soundly in JS: nothing distinguishes the engine's + * own error object. What is sound is where the compare runs: only around inspecting a + * value, never around reading it (see inspectExtra). + */ + function isStackOverflow(e) { + return ( + e !== null && + typeof e === 'object' && + e.name === 'RangeError' && + e.message === STACK_OVERFLOW_MESSAGE + ); + } + /** @returns {string} `value` inspected one level down (see inspectExtras). */ function inspectValue(value, opts, inspect) { var wrapped = inspect([value], opts); @@ -940,9 +1003,13 @@ // JSC's stack-overflow RangeError text (node's V8 text is the same sentence). var STACK_OVERFLOW_MESSAGE = 'Maximum call stack size exceeded.'; + var INSPECT_OVERFLOW_MARKER = + 'Inspection interrupted prematurely. Maximum call stack size exceeded'; - /** @type {((value: *, options?: object) => string)|null} util.inspect, once resolved. */ - var utilInspect = null; + // The overflow already reported by a Buffer hook on the stack (see inspectExtra), and + // how many Buffer hooks are on it: the outermost clears it. + var inspectNesting = 0; + var inspectOverflow = null; /** * Buffer's util.inspect hook; also `buf.inspect`, the same function as in node. @@ -952,53 +1019,48 @@ * util.inspect from util.inspect, the prelude's own from console.log. * @returns {string} ``, plus `, key: value` per extra own property * whenever `ctx` is an object. + * @throws Whatever a getter on an extra throws; and, when a Buffer reaches itself by + * two or more extras, the stack-overflow RangeError (see inspectExtra). * @node ``: insertion order, then symbols * (`Symbol(s): 1`); non-enumerables only with showHidden; an enumerable getter * prints its value (`g: 7`); nested values honour `depth` * (`o: { a: { b: [Object] } }`, depth 0 → `o: [Object]`); a zero-length buffer * prints ``; breakLength/compact change nothing. A direct * `buf.inspect(0, {})` renders the extras too; `buf.inspect()` with no context - * prints the bytes only. A Buffer whose extras lead back to itself recurses until - * the stack runs out and ends in `Inspection interrupted prematurely. Maximum - * call stack size exceeded` instead of throwing. Node 24.22.1, verified by the - * #391 probes. + * prints the bytes only. A Buffer with ONE way back to itself recurses until the + * stack runs out and ends in `Inspection interrupted prematurely. Maximum call + * stack size exceeded`; with two or more, node 24.22.1 throws in a fresh process. + * Node 24.22.1, verified by the #391 probes. * @deviates A Buffer longer than INSPECT_EXTRAS_MAX_LENGTH (64) shows its symbol-keyed * extras but not its string-keyed ones — the cost bound recorded at that * constant. Pinned Lava-only by buffer_inspect_extras_cap in - * cmd/lava/buffer_inspect_extras_test.odin. Not a deviation but a util.inspect - * gap this inherits: `colors: true` leaves the extras uncoloured (gap, - * tracked in #104). + * cmd/lava/buffer_inspect_extras_test.odin. With two or more ways back, node + * recurses with exponential work once its key regex is compiled; Lava throws + * in bounded time (buffer_inspect_cycle_bounded). Not a deviation but a + * util.inspect gap this inherits: `colors: true` leaves the extras uncoloured + * (gap, tracked in #104). */ function inspectHook(recurseTimes, ctx, inspect) { var str = inspectBytes(this); if (ctx !== null && typeof ctx === 'object') { if (typeof inspect !== 'function') { // A direct `buf.inspect(0, {})` passes no inspect function; node still renders - // the extras with its own. util is resolved HERE, on first such call, not at - // module eval: buffer is on the loader's eager list and util is not, so a - // module-eval require would instantiate util (and its dependencies) before user - // code on every start. The read goes through util's exports object, which user - // code can also reach — a replaced `util.inspect` steers this fallback alone; + // the extras with its own. util is required HERE, not at module eval: buffer is + // on the loader's eager list and util is not, so a module-eval require would + // instantiate util (and its dependencies) before user code on every start. + // There is no pristine handle to take instead — the loader publishes one only + // for Buffer (require.pristineBuffer) — so this reads util's exports, which user + // code can also reach: a replaced `util.inspect` steers this one fallback. + // It is read per call and NOT cached, so a replacement never outlives itself. // util.inspect and console.log hand the hook their own function. - if (utilInspect === null) utilInspect = require('util').inspect; - inspect = utilInspect; + inspect = require('util').inspect; } + inspectNesting++; var extras; try { extras = inspectExtras(this, ctx, inspect); - } catch (e) { - // Node catches a stack overflow inside inspect and prints what it had; a - // Buffer that reaches itself through its extras recurses until that happens. - // Logging must not throw. Any other error (a throwing getter) propagates, as - // in node. - if ( - e === null || - typeof e !== 'object' || - e.name !== 'RangeError' || - e.message !== STACK_OVERFLOW_MESSAGE - ) - throw e; - extras = 'Inspection interrupted prematurely. Maximum call stack size exceeded'; + } finally { + if (--inspectNesting === 0) inspectOverflow = null; } if (extras !== '') str += (TypedArrayPrototypeGetLength(this) !== 0 ? ', ' : '') + extras; } diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js index b3734fd..651b193 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/internal/util.js @@ -137,11 +137,19 @@ // is true there): a hook that inspects a sub-value starts again at depth 0 under // whatever options it passes. It used to be a closure that inherited this call's // options and nesting, so a hook one level down got one level less depth than - // node gives it — and Buffer's extra-property rendering, which inspects each value - // at `depth - 1` the way node does, came out a level short. + // node gives it — and Buffer's extra-property rendering, which puts each value at + // depth 1 under the caller's own `depth` the way node does, came out a level short. + // + // A hook that inspects a structure pointing back at an ancestor (a parent pointer: + // the hook returns `inspect(parent)`) recurses until the stack overflows, in node as + // here, because the inspect it is handed starts a fresh visited set. Node catches the + // overflow and prints what it had; Lava used to let it throw out of util.inspect. + // The inspect a hook calls now catches it the same way — see hookInspect for where, + // and for the latch that keeps it from turning into exponential work. Pinned by + // tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js. if (customInspect && typeof v[customInspect] === 'function') { var nested = seen.concat([v]); - var custom = v[customInspect](depth, opts, inspectPublic); + var custom = callCustomInspect(v, depth, opts); if (custom !== v) { return typeof custom === 'string' ? custom : inspect(custom, opts, nested, depth); } @@ -187,6 +195,54 @@ return prefix + '{ ' + props.join(', ') + ' }'; } + // Stack-overflow handling under custom hooks, the shape Buffer's hook uses + // (internal/buffer.js inspectExtra). + // + // WHERE it is caught matters. Node catches an overflow while formatting an object's + // body, not around the hook call itself, so an error thrown by the hook's own code — + // a getter that throws a RangeError carrying the overflow's message — still + // propagates. The catch is therefore in the inspect a hook is HANDED (hookInspect), + // which is where a hook re-entering a cycle recurses; the hook call stays uncaught. + // + // The first overflow caught there is rendered as node's marker and latched; any + // further hook-side inspect while it is latched rethrows it instead of descending + // again. With one way back to an ancestor nothing descends again and util.inspect + // returns, as node's does; with several it throws in bounded time where unlatched + // catching re-descended once per key and level (work like k^depth). The outermost hook + // clears the latch. + var hookNesting = 0; + var hookOverflow = null; + var STACK_OVERFLOW_MESSAGE = 'Maximum call stack size exceeded.'; + + function callCustomInspect(v, depth, opts) { + hookNesting++; + try { + return v[customInspect](depth, opts, inspectPublic); + } finally { + if (--hookNesting === 0) hookOverflow = null; + } + } + + /** util.inspect as called from inside a custom hook (see above). */ + function hookInspect(v, opts) { + if (hookOverflow !== null) throw hookOverflow; + try { + return inspect(v, opts, [], 0); + } catch (e) { + // The same name + message test node's isStackOverflowError makes. + if ( + e === hookOverflow || + e === null || + typeof e !== 'object' || + e.name !== 'RangeError' || + e.message !== STACK_OVERFLOW_MESSAGE + ) + throw e; + hookOverflow = e; + return '[Object: Inspection interrupted prematurely. Maximum call stack size exceeded.]'; + } + } + function stringify(v) { return typeof v === 'string' ? v : inspect(v, {}, [], 1); } @@ -885,7 +941,9 @@ } function inspectPublic(v, opts) { - return inspect(v, opts, [], 0); + // One function, so a hook's third argument stays `=== util.inspect` as in node; only + // a call made while a hook is running takes the overflow-catching path. + return hookNesting === 0 ? inspect(v, opts, [], 0) : hookInspect(v, opts); } // util.inspect.colors is the live style table (styleText reads it); util.inspect.custom // is the symbol a value implements to control its own inspection. diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index a89a5b4..086210f 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -160,7 +160,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. - **UTF-8 decode (issue #107):** `toString('utf8')` / `Buffer.from(string)` use the Unicode "U+FFFD substitution of maximal subparts" rule (the native Odin decoder, [buffer.odin](../pkg/runtime/buffer.odin)), matching Node/V8: an ill-formed subsequence collapses to a single replacement (`Buffer.from([0xF0,0x9F]).toString()` → `'�'`, not `'��'`). Embedded NUL is preserved. - **Encodings:** `utf8`/`utf-8`, `utf16le`/`ucs2`, `latin1`/`binary`, `ascii`, `hex`, `base64`, `base64url` (with Node's lenient base64 normalization and `ascii` high-bit masking). - **Errors:** Node `err.code`s and message shapes for `ERR_INVALID_ARG_TYPE`, `ERR_INVALID_ARG_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_BUFFER_OUT_OF_BOUNDS`, `ERR_UNKNOWN_ENCODING`, `ERR_INVALID_BUFFER_SIZE`. -- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. A Buffer's extra own properties are rendered after the bytes, as in Node: ``. They appear in insertion order with symbol keys last, non-enumerable ones only under `showHidden`, and nested values cut off at `depth`. A direct `buf.inspect(0, {})` renders them too, and a Buffer whose extras lead back to itself ends in Node's `Inspection interrupted prematurely` marker instead of throwing. One deviation: a Buffer **longer than 64 bytes** shows its symbol-keyed extras but not its string-keyed ones. JavaScript cannot list a typed array's named properties without first making one string per index, so without the cap every inspect would cost O(length). At the cap the whole inspect costs about 2.8x the bytes-only render (the measurement and its method are recorded at `INSPECT_EXTRAS_MAX_LENGTH` in [buffer.js](../pkg/runtime/js/internal/buffer.js)). The deviation is pinned by `cmd/lava/buffer_inspect_extras_test.odin`. `colors: true` leaves the extras uncoloured; that is a `util.inspect` gap (#104), not a Buffer one. +- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. A Buffer's extra own properties are rendered after the bytes, as in Node: ``. They appear in insertion order with symbol keys last, non-enumerable ones only under `showHidden`, and nested values cut off at `depth`. A direct `buf.inspect(0, {})` renders them too. A Buffer whose extras lead back to itself by one key ends in Node's `Inspection interrupted prematurely` marker instead of throwing. With two or more keys back it throws a stack-overflow `RangeError` in bounded time; Node 24 throws in a fresh process and recurses exponentially once its key regex is compiled. One deviation: a Buffer **longer than 64 bytes** shows its symbol-keyed extras but not its string-keyed ones. JavaScript cannot list a typed array's named properties without first making one string per index, so without the cap every inspect would cost O(length). At the cap the whole inspect costs about 3.5x the bytes-only render (the measurement and its method are recorded at `INSPECT_EXTRAS_MAX_LENGTH` in [buffer.js](../pkg/runtime/js/internal/buffer.js)). The deviation is pinned by `cmd/lava/buffer_inspect_extras_test.odin`. `colors: true` leaves the extras uncoloured; that is a `util.inspect` gap (#104), not a Buffer one. - **Module extras:** `Blob`, `File`, `SlowBuffer`, `atob`/`btoa`, `isAscii`, `isUtf8`, `transcode`, `resolveObjectURL`, `kMaxLength`/`kStringMaxLength`/`constants`/`INSPECT_MAX_BYTES`. `Blob`/`File` honor the `endings: 'native'` option (string parts have `\n`/`\r\n` converted to the platform newline — derived from `process.platform` — while a lone `\r` and binary parts are left verbatim, matching Node). **Intentional differences / deferred:** diff --git a/tests/node-compat/cases/70-buffer-size-inspect-parity.js b/tests/node-compat/cases/70-buffer-size-inspect-parity.js index a65705a..faa6539 100644 --- a/tests/node-compat/cases/70-buffer-size-inspect-parity.js +++ b/tests/node-compat/cases/70-buffer-size-inspect-parity.js @@ -27,6 +27,40 @@ const buffer = require('node:buffer'); // deprecation warnings. Silenced so the compared output is the behaviour under test. process.noDeprecation = true; +// --- Self-referencing Buffers with MORE than one way back -------------------------------- +// FIRST in the file, deliberately. In a fresh node 24.22.1 process these throw within +// ~20 ms: after the first stack overflow is caught, the enclosing hook moves on to its +// next key, and node's key-quoting regex /^[a-zA-Z_][a-zA-Z_0-9]*$/ is compiled lazily +// at that point, at the stack limit, so it throws a SyntaxError that node's overflow catch +// does not recognise. Once anything has inspected an object key, the regex is already +// compiled and node instead recurses with work like k^depth: in a process that had +// inspected `b.self = b` first, `b.k0 = b; b.k1 = b` ran 148 s and then threw +// `RangeError: Invalid string length`. Lava throws in both orders. Its overflow latch lets +// no hook descend again once an overflow has been caught, so this ends in bounded time. +// Only "threw" is printed: node's error is that V8 SyntaxError, Lava's is the stack-overflow +// RangeError. Bounded time is pinned Lava-only by buffer_inspect_cycle_bounded in +// cmd/lava/buffer_inspect_extras_test.odin. +for (const refs of [2, 3]) { + const b = Buffer.from([1]); + for (let i = 0; i < refs; i++) b['k' + i] = b; + for (const [how, fn] of [ + ['util.inspect', () => util.inspect(b)], + ['console.log', () => console.log(b)], + // A direct call: no util.inspect or console.log frame above the hook, so only the + // Buffer hook's own overflow latch keeps this bounded. + ['inspect(0, {})', () => b.inspect(0, {})], + ]) { + let out; + try { + fn(); + out = 'returned'; + } catch { + out = 'threw'; + } + console.log(`cycle ${refs} self-refs ${how}: ${out}`); + } +} + const MAX = String(buffer.constants.MAX_LENGTH); function show(label, fn) { let r; @@ -279,3 +313,44 @@ function cycleShape(label, fn) { d.o = { back: d }; cycleShape('cycle via object:', () => util.inspect(d)); } + +// Errors other than a stack overflow propagate out of inspect, as in node: from a getter +// on the Buffer itself (read outside any catch) and from a hook further down (thrown +// inside the inspect of an extra's value, where the overflow catch must not swallow it). +// A getter that throws a RangeError carrying the overflow's own message also propagates: +// node reads the property outside its overflow catch, and so does Lava. +function threw(label, fn) { + try { + fn(); + console.log(label, 'returned'); + } catch (e) { + console.log(label, 'THREW', e.name, e.message); + } +} +{ + const g = Buffer.from([1]); + Object.defineProperty(g, 'bad', { + enumerable: true, + get() { + throw new TypeError('boom'); + }, + }); + threw('getter TypeError util.inspect:', () => util.inspect(g)); + threw('getter TypeError inspect(0, {}):', () => g.inspect(0, {})); + const h = Buffer.from([2]); + h.inner = { + [custom]() { + throw new TypeError('deep boom'); + }, + }; + threw('nested hook TypeError util.inspect:', () => util.inspect(h)); + threw('nested hook TypeError inspect(0, {}):', () => h.inspect(0, {})); + const r = Buffer.from([3]); + Object.defineProperty(r, 'forged', { + enumerable: true, + get() { + throw new RangeError('Maximum call stack size exceeded.'); + }, + }); + threw('getter forged overflow util.inspect:', () => util.inspect(r)); +} diff --git a/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js b/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js new file mode 100644 index 0000000..ca7e8a6 --- /dev/null +++ b/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js @@ -0,0 +1,45 @@ +// A user `[util.inspect.custom]` hook that inspects a value pointing back at one of its +// ancestors must not make util.inspect or console.log throw. +// +// Shape: a parent-pointer tree. `outer.h` is an object whose hook returns +// `inspect(outer)`, so outer -> h -> outer -> h ... never ends unless the hook's inspect +// knows what it is already inside. +// +// node 24.22.1 recurses until the stack runs out, catches the overflow inside inspect and +// prints what it had: a 1.2 MB string ending in `h: [Object: Inspection interrupted +// prematurely. Maximum call stack size exceeded.]`, never a throw. Lava before #391 +// printed `{ n: 1, h: [Circular *1] }`; at c5fd268 it THREW a RangeError, because the +// inspect handed to a hook now starts a fresh visited set (as node's does) and nothing +// caught the overflow. Lava now catches it at the hook boundary, as node does. Where the +// cut lands depends on stack size and node also breaks lines, so the lines below print +// the outcome and whether the marker is there, not the full string. +const util = require('node:util'); + +function make() { + const outer = { n: 1 }; + class H { + [util.inspect.custom](depth, options, inspect) { + return inspect(outer); + } + } + outer.h = new H(); + return outer; +} + +function outcome(label, fn) { + let s; + try { + s = fn(); + } catch (e) { + console.log(label, 'threw', e.name); + return; + } + console.log(label, 'returned', typeof s, s.includes('Inspection interrupted prematurely')); +} + +outcome('util.inspect:', () => util.inspect(make())); +// console.log is pinned Lava-only (buffer_inspect_hook_ancestor_cycle in +// cmd/lava/buffer_inspect_extras_test.odin): its rendered text differs from node's by +// construction, and Lava's console.log writes natively rather than through +// process.stdout.write, so the text cannot be captured here and kept out of the compared +// stdout. From efa16de20152c1741cb7744c0ffaa8f42647e689 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:12:44 +0000 Subject: [PATCH 04/61] docs(test): park the 22 recorded mutations for #391 until a human can add them tests/mutation-manifest.json is human-only under the gate-integrity rules, so the mutations recorded while fixing #391 could not be added there, and the only other copy was in a session scratch directory. This file holds them in the manifest's own entry shape, ready to append to its mutations array; delete it once they are in. Every find string was checked unique in its source at 49bc489 and every mutant was run and went red. Entry 6's expect_detail is the value corrected by the final review. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YQTDDUu9z73JRY3RpV6Adg --- docs/issue-391-recorded-mutations.json | 253 +++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 docs/issue-391-recorded-mutations.json diff --git a/docs/issue-391-recorded-mutations.json b/docs/issue-391-recorded-mutations.json new file mode 100644 index 0000000..22c7cf8 --- /dev/null +++ b/docs/issue-391-recorded-mutations.json @@ -0,0 +1,253 @@ +{ + "$comment": [ + "Recorded mutations for the #391 work (branch claude/wizardly-cerf-eqzmwa, verified at 49bc489).", + "They belong in tests/mutation-manifest.json, which agent sessions may not edit: append these to its `mutations` array and delete this file.", + "Each `find` was checked unique in its source at 49bc489 and each mutant was run and went red.", + "Entry 6 carries the expect_detail corrected by the final review (the runner's first-diff window no longer reaches the old line).", + "Not included: the two hook-side rethrow-filter entries and the regate of the case-70 cycle entry — both depend on test changes tracked in the follow-up issues." + ], + "mutations": [ + { + "name": "70-buffer-size-inspect-parity dies when assertSize splices a bare type name", + "why": "node prints `must be of type number` for Buffer.alloc('5'); a bare 'number' prints `must be number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('size', 'of type number', size);", + "replace": "throw errInvalidArgType('size', 'number', size);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"size\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", + "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('offset', 'of type number', offset);", + "replace": "throw errInvalidArgType('offset', 'number', offset);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"offset\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check splices a bare type name", + "why": "Buffer.concat(list, '5') must say `The \"length\" argument must be of type number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('length', 'of type number', totalLength);", + "replace": "throw errInvalidArgType('length', 'number', totalLength);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"length\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the deprecated Buffer(n) skips assertSize", + "why": "Buffer(-1) would throw JSC's codeless 'length cannot be negative' and Buffer(NaN) return an empty Buffer; node throws ERR_OUT_OF_RANGE.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " assertSize(arg);\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "length cannot be negative" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the more-bytes separator is dropped with no hex shown", + "why": "node prints `` (two spaces) when INSPECT_MAX_BYTES = 0.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "hex += ' ... ' + remaining", + "replace": "hex += (hex ? ' ' : '') + '... ' + remaining", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the Buffer inspect hook stops rendering extra own properties", + "why": "node shows a Buffer's extra own properties after the bytes.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "extras = inspectExtras(this, ctx, inspect);", + "replace": "extras = '';", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "cycle 2 self-refs util.inspect: returned" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the symbol-key loop stops filtering non-enumerables", + "why": "node lists a non-enumerable symbol-keyed extra only under showHidden.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " if (showHidden || isEnumerableOwn(buf, key)) {\n if (opts === null) opts = extrasOptions(ctx, showHidden);\n piece = inspectExtra(StringG(key)", + "replace": " if (true) {\n if (opts === null) opts = extrasOptions(ctx, showHidden);\n piece = inspectExtra(StringG(key)", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "keys: \" 0 && c >= 0x30 && c <= 0x39)", + "replace": "(c >= 0x30 && c <= 0x39)", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "1a: 1" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when a context-only buf.inspect(0, {}) stops rendering extras", + "why": "node renders extras for a direct call with an options object and no inspect function.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "inspect = require('util').inspect;", + "replace": "return '';", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "inspect(0, {}): \"\"" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the Buffer overflow catch swallows non-overflow errors", + "why": "node lets a TypeError from a nested hook propagate out of inspect; a catch-all prints the interrupted marker instead.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "if (e === inspectOverflow || !isStackOverflow(e)) throw e;", + "replace": "if (e === inspectOverflow) throw e;", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "nested hook TypeError util.inspect: returned" + ] + }, + { + "name": "buffer_inspect_cycle_bounded dies when a Buffer hook re-swallows the overflow it already reported", + "why": "without the identity rethrow each enclosing Buffer hook swallows the latched overflow again, so a Buffer with two ways back returns instead of throwing like node.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "if (e === inspectOverflow || !isStackOverflow(e)) throw e;", + "replace": "if (!isStackOverflow(e)) throw e;", + "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", + "expect_detail": [ + "2 refs inspect outcome: want threw got returned" + ] + }, + { + "name": "buffer_inspect_cycle_bounded dies when util's hook-side overflow latch is removed", + "why": "without the latch every enclosing hook re-descends to the stack limit per key, so `b.k0 = b; b.k1 = b` does work like 2^depth and never finishes.", + "source": "pkg/runtime/js/internal/util.js", + "find": " if (hookOverflow !== null) throw hookOverflow;", + "replace": "", + "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", + "expect_detail": [ + "timed out" + ] + }, + { + "name": "buffer_inspect_cycle_bounded dies when console's hook-side overflow latch is removed", + "why": "console.log of a Buffer with two ways back to itself would never finish.", + "source": "pkg/runtime/js/console.js", + "find": " if (hookOverflow !== null) throw hookOverflow;", + "replace": "", + "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", + "expect_detail": [ + "timed out" + ] + }, + { + "name": "71-inspect-hook-ancestor-cycle dies when a hook's util.inspect stops catching the stack overflow", + "why": "a user hook inspecting its own ancestor made util.inspect throw RangeError; node catches and prints.", + "source": "pkg/runtime/js/internal/util.js", + "find": ": hookInspect(v, opts);", + "replace": ": inspect(v, opts, [], 0);", + "gate": "compat:tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js", + "expect_detail": [ + "util.inspect: threw RangeError" + ] + }, + { + "name": "buffer_inspect_hook_ancestor_cycle dies when console's hook inspect stops catching the stack overflow", + "why": "console.log of a parent-pointer structure with a custom hook would throw RangeError and kill the process.", + "source": "pkg/runtime/js/console.js", + "find": " hookOverflow = e;\n return '[Object:", + "replace": " throw e;\n return '[Object:", + "gate": "odin:cmd/lava:main.buffer_inspect_hook_ancestor_cycle", + "expect_detail": [ + "Maximum call stack size exceeded" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when top-level util.inspect catches errors thrown by a hook's own getter", + "why": "node catches overflow only while formatting, so a getter throwing a forged overflow RangeError propagates; catching at the top swallows it and changes cycle outcomes.", + "source": "pkg/runtime/js/internal/util.js", + "find": "return hookNesting === 0 ? inspect(v, opts, [], 0) : hookInspect(v, opts);", + "replace": "return hookInspect(v, opts);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "cycle 2 self-refs util.inspect: returned" + ] + }, + { + "name": "buffer_inspect_cycle_bounded dies when util passes hooks a depth-inheriting closure instead of util.inspect", + "why": "node passes util.inspect itself; the old closure also bypasses the overflow latch, so cycles stop terminating.", + "source": "pkg/runtime/js/internal/util.js", + "find": "return v[customInspect](depth, opts, inspectPublic);", + "replace": "return v[customInspect](depth, opts, function (val, o) { return inspect(val, o || opts, [v], depth + 1); });", + "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", + "expect_detail": [ + "timed out" + ] + }, + { + "name": "buffer_inspect_cycle_bounded dies when console hands hooks the old one-argument closure", + "why": "the closure dropped options (wrong depth for extras) and bypasses the overflow latch.", + "source": "pkg/runtime/js/console.js", + "find": "return v[customInspect](depth, {}, inspectWithOptions);", + "replace": "return v[customInspect](depth, {}, function (val) { return inspect(val, [v], depth + 1); });", + "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", + "expect_detail": [ + "timed out" + ] + }, + { + "name": "buffer_inspect_extras_cap dies when the Buffer extras string-key cap moves", + "why": "the string-key scan is O(length), capped at 64 (cost recorded in buffer.js).", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "var INSPECT_EXTRAS_MAX_LENGTH = 64;", + "replace": "var INSPECT_EXTRAS_MAX_LENGTH = 65;", + "gate": "odin:cmd/lava:main.buffer_inspect_extras_cap", + "expect_detail": [ + "65 omits string key" + ] + }, + { + "name": "buffer_inspect_extras_pollution dies when the hook reads showHidden through the prototype chain", + "why": "a polluted Object.prototype.showHidden would list hidden properties of every logged Buffer.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "var showHidden = ObjectPrototypeHasOwnProperty(ctx, 'showHidden') && !!ctx.showHidden;", + "replace": "var showHidden = !!ctx.showHidden;", + "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", + "expect_detail": [ + "inherited showHidden" + ] + }, + { + "name": "buffer_inspect_extras_pollution dies when the hook reads depth through the prototype chain", + "why": "a polluted Object.prototype.depth = null would lift the depth bound on every logged Buffer's extras.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined,", + "replace": "depth: ctx.depth,", + "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", + "expect_detail": [ + "inherited depth" + ] + }, + { + "name": "buffer_inspect_extras_pollution dies when console's hook inspect reads depth through the prototype chain", + "why": "a polluted Object.prototype.depth = null would lift the depth bound for every custom hook's sub-inspect under console.log.", + "source": "pkg/runtime/js/console.js", + "find": "options !== null && typeof options === 'object' && ObjectHasOwn(options, 'depth')", + "replace": "options !== null && typeof options === 'object'", + "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", + "expect_detail": [ + "console hook inherited depth" + ] + } + ] +} From 960670eff6cffeabafeb24137efbea1137138368 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:31:17 +0000 Subject: [PATCH 05/61] refactor(buffer): reduce the #391 change to its three simple items Item 4 of #391 (rendering a Buffer's extra own properties in util.inspect, with the cycle handling it needed) produced a P1 in each of three review passes, every one in the cycle path: a hang on multiply self-referential Buffers, a lost visited set, and separate overflow latches in console.js and util.js that let a hook run for minutes. It is deferred until console's formatter is unified with util's (#104); the work stays in this branch's history at 49bc489, tracked by #399, #400, #401, #402 and #404. What remains against master: - ERR_INVALID_ARG_TYPE reads 'must be of type number' for size, offset and Buffer.concat's length; - the deprecated constructor's number branch validates through assertSize, so Buffer(-1) and Buffer(NaN) throw ERR_OUT_OF_RANGE; - inspect adds the separator before '... N more bytes' when no byte is shown. util.js, console.js and reference/node-compatibility.md are byte-identical to master again; buffer.js was rebuilt from master with only those three changes; the Lava-only inspect test is removed; case 70 covers the three items only and also finishes under node 22. The primordials ratchet equals master's (1553). docs/issue-391-recorded-mutations.json keeps the five entries that apply. NOT DONE, needs a human: tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js belongs to the deferred work and must be deleted, but the gate-integrity hook makes deleting an oracle case human-only. Until it is removed, make test-compat-lava fails on that case alone. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YQTDDUu9z73JRY3RpV6Adg --- cmd/lava/buffer_inspect_extras_test.odin | 256 ---------------- docs/issue-391-recorded-mutations.json | 194 +------------ pkg/runtime/js/console.js | 90 +----- pkg/runtime/js/internal/buffer.js | 273 +----------------- pkg/runtime/js/internal/util.js | 72 +---- reference/node-compatibility.md | 2 +- .../cases/70-buffer-size-inspect-parity.js | 248 +--------------- 7 files changed, 31 insertions(+), 1104 deletions(-) delete mode 100644 cmd/lava/buffer_inspect_extras_test.odin diff --git a/cmd/lava/buffer_inspect_extras_test.odin b/cmd/lava/buffer_inspect_extras_test.odin deleted file mode 100644 index 36d5e7d..0000000 --- a/cmd/lava/buffer_inspect_extras_test.odin +++ /dev/null @@ -1,256 +0,0 @@ -package main - -// Lava-only: pins where Buffer's util.inspect hook deliberately renders LESS than node 24 -// (issue #391), per CLAUDE.md §1, and one pollution property node cannot oracle. -// -// Why these are not oracle cases. -// -// 1. The string-key scan is capped at INSPECT_EXTRAS_MAX_LENGTH (64) bytes -// (pkg/runtime/js/internal/buffer.js, where the measurements and their method are -// recorded). JS cannot list a typed array's named own properties without first -// materialising one string per index, so the scan is O(length) on every inspect. -// Past the cap, string-keyed extras are omitted; symbol-keyed ones still show -// (Object.getOwnPropertySymbols lists no indices). Node shows both at any length, -// so a node-vs-Lava diff here would only ever be red. The 64/65 pair sits exactly -// on the cap, so moving the constant or dropping the length check turns this red. -// The bare `buf.inspect()` assertion is node's own behaviour, kept beside it so a -// change that renders extras without a context is caught too. -// 2. The hook reads `showHidden`/`depth` as OWN properties of the options object. -// Polluting Object.prototype in an oracle case would leak into every other line -// it prints, so the property is pinned here instead. -// -// 3. Cycles, where node has no stable output to compare against. A Buffer reaching itself -// by two or more keys made Lava's per-hook overflow catch do work like k^depth -// (c5fd268: `b.k0 = b; b.k1 = b; console.log(b)` ran past 60 s). node's outcome there -// depends on whether a lazily compiled regex has been compiled yet (see case 70), so -// the property pinned here is Lava's own: it ends, by a throw, in bounded wall-clock -// time. A user hook that inspects its own ancestor must not throw out of console.log -// (Lava's console.log text cannot be captured by an oracle case: it writes natively), -// and with two ways back it must end in bounded time too. -// 4. console.log hands a hook an inspect that reads `depth` as an OWN option only. -// -// Each script throws on the first mismatch, so the failed expectation names it. - -import "core:testing" -import "core:time" -import "lava:pkg/runtime/eventloop" -import lava "lava:pkg/runtime" - -BUFFER_INSPECT_EXTRAS_SCRIPT :: ` -const util = require('node:util'); -const sym = Symbol('s'); -function check(label, got, want) { - if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); -} - -// At the cap: the string-keyed property is scanned and shown. -const atCap = Buffer.alloc(64); -atCap.tag = 1; -check('64 shows string key', util.inspect(atCap).endsWith(', tag: 1>'), true); - -// One past the cap: the string key is omitted, the symbol key still shows. -const pastCap = Buffer.alloc(65); -pastCap.tag = 1; -pastCap[sym] = 2; -const past = util.inspect(pastCap); -check('65 omits string key', past.includes('tag: 1'), false); -check('65 shows symbol key', past.endsWith(' more bytes, Symbol(s): 2>'), true); - -// A bare call has no context and renders the bytes only (node does the same)... -const small = Buffer.from([1, 2, 3]); -small.tag = 1; -check('bare inspect()', small.inspect(), ''); -// ...while util.inspect of the same Buffer does render the extra (so the line above -// is not passing merely because extras never render). -check('util.inspect same buffer', util.inspect(small), ""); -console.log('buffer-inspect-extras ok'); -` - -BUFFER_INSPECT_POLLUTION_SCRIPT :: ` -const util = require('node:util'); -function check(label, got, want) { - if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); -} -const b = Buffer.from([1]); -Object.defineProperty(b, 'hid', { value: 1, enumerable: false }); -b.o = { a: { b: { c: { d: 1 } } } }; -Object.prototype.showHidden = true; -Object.prototype.depth = null; -let plain, shown; -try { - plain = util.inspect(b, {}); - shown = util.inspect(b, { showHidden: true }); -} finally { - delete Object.prototype.showHidden; - delete Object.prototype.depth; -} -// An inherited showHidden must not list the non-enumerable property... -check('inherited showHidden', plain.includes('hid'), false); -// ...an inherited depth: null must not lift the depth bound for the extras... -check('inherited depth', plain.includes('[Object]'), true); -// ...and an OWN showHidden still works, so the first check is not vacuous. -check('own showHidden', shown.includes('hid: 1'), true); -// console.log's hook inspect must not take depth from Object.prototype either (M). -let viaConsole; -const deep = { a: { b: { c: { d: { e: 1 } } } } }; -const hooked = { - [util.inspect.custom](d, o, inspect) { - viaConsole = inspect(deep, {}); - return 'hooked'; - }, -}; -Object.prototype.depth = null; -try { - console.log(hooked); -} finally { - delete Object.prototype.depth; -} -check('console hook inherited depth', viaConsole, '{ a: { b: { c: [Object] } } }'); -console.log('buffer-inspect-pollution ok'); -` - -@(test) -buffer_inspect_extras_cap :: proc(t: ^testing.T) { - loop := eventloop.init() - // eval consumes (destroys) the loop on every path; do not destroy it here. - result := lava.eval(BUFFER_INSPECT_EXTRAS_SCRIPT, "", &loop, false) - defer lava.result_destroy(&result) - - testing.expectf( - t, - result.status == .Ok, - "eval did not complete cleanly: status=%v message=%q", - result.status, - result.message, - ) - testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) -} - -@(test) -buffer_inspect_extras_pollution :: proc(t: ^testing.T) { - loop := eventloop.init() - // eval consumes (destroys) the loop on every path; do not destroy it here. - result := lava.eval(BUFFER_INSPECT_POLLUTION_SCRIPT, "", &loop, false) - defer lava.result_destroy(&result) - - testing.expectf( - t, - result.status == .Ok, - "eval did not complete cleanly: status=%v message=%q", - result.status, - result.message, - ) - testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) -} - -BUFFER_INSPECT_CYCLE_SCRIPT :: ` -const util = require('node:util'); -function check(label, got, want) { - if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); -} -// k self-references: must END, and by a throw (node's fresh-process outcome), well inside -// the bound. Before the overflow latch, k = 2 already ran past 60 s. -for (const refs of [2, 3, 8]) { - const b = Buffer.from([1]); - for (let i = 0; i < refs; i++) b['k' + i] = b; - for (const how of ['inspect', 'log']) { - const t0 = Date.now(); - let out = 'returned'; - try { - if (how === 'inspect') util.inspect(b); - else console.log(b); - } catch (e) { - out = 'threw'; - } - const ms = Date.now() - t0; - check(refs + ' refs ' + how + ' outcome', out, 'threw'); - if (ms > 5000) throw new Error(refs + ' refs ' + how + ' took ' + ms + 'ms (bound 5000)'); - } -} -// One way back still RETURNS, with node's interrupted marker, and the latch is reset for -// the next top-level call: inspecting it again returns again (a latch left set would -// make the second call throw at its first extra). Where the cut lands varies with stack -// state, so the two strings are not compared. -const one = Buffer.from([1]); -one.self = one; -check('1 ref marker', util.inspect(one).includes('Inspection interrupted prematurely'), true); -check('1 ref repeat', util.inspect(one).includes('Inspection interrupted prematurely'), true); -check('plain after cycle', util.inspect(Object.assign(Buffer.from([2]), { t: 1 })), ''); -console.log('buffer-inspect-cycle ok'); -` - -BUFFER_INSPECT_HOOK_ANCESTOR_SCRIPT :: ` -const util = require('node:util'); -function check(label, got, want) { - if (got !== want) throw new Error(label + ': want ' + want + ' got ' + got); -} -const outer = { n: 1 }; -class H { - [util.inspect.custom](d, o, inspect) { - return inspect(outer); - } -} -outer.h = new H(); -// util.inspect returns node's interrupted marker rather than throwing... -check('util.inspect', util.inspect(outer).includes('Inspection interrupted prematurely'), true); -// ...and so does console.log (c5fd268 threw RangeError out of it), in bounded time. -const t0 = Date.now(); -console.log(outer); -if (Date.now() - t0 > 5000) throw new Error('console.log took ' + (Date.now() - t0) + 'ms'); -// Two ways back to the ancestor: bounded, by a throw (the latch), never a hang. -const outer2 = { n: 2 }; -class H2 { - [util.inspect.custom](d, o, inspect) { - return inspect(outer2); - } -} -outer2.a = new H2(); -outer2.b = new H2(); -for (const how of ['inspect', 'log']) { - const t1 = Date.now(); - try { - if (how === 'inspect') util.inspect(outer2); - else console.log(outer2); - } catch {} - if (Date.now() - t1 > 5000) throw new Error('2 ways back ' + how + ' took ' + (Date.now() - t1) + 'ms'); -} -console.log('buffer-inspect-hook-ancestor ok'); -` - -@(test) -buffer_inspect_cycle_bounded :: proc(t: ^testing.T) { - // The exponential path never returns, so the script's own 5 s check would never run: - // the runner's timeout is what turns a regression into a failure instead of a CI hang. - testing.set_fail_timeout(t, 30 * time.Second) - loop := eventloop.init() - // eval consumes (destroys) the loop on every path; do not destroy it here. - result := lava.eval(BUFFER_INSPECT_CYCLE_SCRIPT, "", &loop, false) - defer lava.result_destroy(&result) - - testing.expectf( - t, - result.status == .Ok, - "eval did not complete cleanly: status=%v message=%q", - result.status, - result.message, - ) - testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) -} - -@(test) -buffer_inspect_hook_ancestor_cycle :: proc(t: ^testing.T) { - testing.set_fail_timeout(t, 30 * time.Second) - loop := eventloop.init() - // eval consumes (destroys) the loop on every path; do not destroy it here. - result := lava.eval(BUFFER_INSPECT_HOOK_ANCESTOR_SCRIPT, "", &loop, false) - defer lava.result_destroy(&result) - - testing.expectf( - t, - result.status == .Ok, - "eval did not complete cleanly: status=%v message=%q", - result.status, - result.message, - ) - testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) -} diff --git a/docs/issue-391-recorded-mutations.json b/docs/issue-391-recorded-mutations.json index 22c7cf8..09fc7d8 100644 --- a/docs/issue-391-recorded-mutations.json +++ b/docs/issue-391-recorded-mutations.json @@ -1,10 +1,9 @@ { "$comment": [ - "Recorded mutations for the #391 work (branch claude/wizardly-cerf-eqzmwa, verified at 49bc489).", + "Recorded mutations for the #391 items that shipped (branch claude/wizardly-cerf-eqzmwa): ERR_INVALID_ARG_TYPE wording, the deprecated Buffer(n) size validation, and the inspect separator.", + "This file holds those five entries, each verified at the reduced tree: every `find` is unique in its source, and every mutant was run and turned case 70 red with its expect_detail.", "They belong in tests/mutation-manifest.json, which agent sessions may not edit: append these to its `mutations` array and delete this file.", - "Each `find` was checked unique in its source at 49bc489 and each mutant was run and went red.", - "Entry 6 carries the expect_detail corrected by the final review (the runner's first-diff window no longer reaches the old line).", - "Not included: the two hook-side rethrow-filter entries and the regate of the case-70 cycle entry — both depend on test changes tracked in the follow-up issues." + "The item-4 work (Buffer extra-property rendering and cycle handling) was withdrawn, to be redone after the console/util formatters are unified (#104). It and its other 17 recorded entries live in commit 49bc489 of this branch's history." ], "mutations": [ { @@ -61,193 +60,6 @@ "expect_detail": [ "" ] - }, - { - "name": "70-buffer-size-inspect-parity dies when the Buffer inspect hook stops rendering extra own properties", - "why": "node shows a Buffer's extra own properties after the bytes.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "extras = inspectExtras(this, ctx, inspect);", - "replace": "extras = '';", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "cycle 2 self-refs util.inspect: returned" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when the symbol-key loop stops filtering non-enumerables", - "why": "node lists a non-enumerable symbol-keyed extra only under showHidden.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": " if (showHidden || isEnumerableOwn(buf, key)) {\n if (opts === null) opts = extrasOptions(ctx, showHidden);\n piece = inspectExtra(StringG(key)", - "replace": " if (true) {\n if (opts === null) opts = extrasOptions(ctx, showHidden);\n piece = inspectExtra(StringG(key)", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "keys: \" 0 && c >= 0x30 && c <= 0x39)", - "replace": "(c >= 0x30 && c <= 0x39)", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "1a: 1" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when a context-only buf.inspect(0, {}) stops rendering extras", - "why": "node renders extras for a direct call with an options object and no inspect function.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "inspect = require('util').inspect;", - "replace": "return '';", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "inspect(0, {}): \"\"" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when the Buffer overflow catch swallows non-overflow errors", - "why": "node lets a TypeError from a nested hook propagate out of inspect; a catch-all prints the interrupted marker instead.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "if (e === inspectOverflow || !isStackOverflow(e)) throw e;", - "replace": "if (e === inspectOverflow) throw e;", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "nested hook TypeError util.inspect: returned" - ] - }, - { - "name": "buffer_inspect_cycle_bounded dies when a Buffer hook re-swallows the overflow it already reported", - "why": "without the identity rethrow each enclosing Buffer hook swallows the latched overflow again, so a Buffer with two ways back returns instead of throwing like node.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "if (e === inspectOverflow || !isStackOverflow(e)) throw e;", - "replace": "if (!isStackOverflow(e)) throw e;", - "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", - "expect_detail": [ - "2 refs inspect outcome: want threw got returned" - ] - }, - { - "name": "buffer_inspect_cycle_bounded dies when util's hook-side overflow latch is removed", - "why": "without the latch every enclosing hook re-descends to the stack limit per key, so `b.k0 = b; b.k1 = b` does work like 2^depth and never finishes.", - "source": "pkg/runtime/js/internal/util.js", - "find": " if (hookOverflow !== null) throw hookOverflow;", - "replace": "", - "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", - "expect_detail": [ - "timed out" - ] - }, - { - "name": "buffer_inspect_cycle_bounded dies when console's hook-side overflow latch is removed", - "why": "console.log of a Buffer with two ways back to itself would never finish.", - "source": "pkg/runtime/js/console.js", - "find": " if (hookOverflow !== null) throw hookOverflow;", - "replace": "", - "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", - "expect_detail": [ - "timed out" - ] - }, - { - "name": "71-inspect-hook-ancestor-cycle dies when a hook's util.inspect stops catching the stack overflow", - "why": "a user hook inspecting its own ancestor made util.inspect throw RangeError; node catches and prints.", - "source": "pkg/runtime/js/internal/util.js", - "find": ": hookInspect(v, opts);", - "replace": ": inspect(v, opts, [], 0);", - "gate": "compat:tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js", - "expect_detail": [ - "util.inspect: threw RangeError" - ] - }, - { - "name": "buffer_inspect_hook_ancestor_cycle dies when console's hook inspect stops catching the stack overflow", - "why": "console.log of a parent-pointer structure with a custom hook would throw RangeError and kill the process.", - "source": "pkg/runtime/js/console.js", - "find": " hookOverflow = e;\n return '[Object:", - "replace": " throw e;\n return '[Object:", - "gate": "odin:cmd/lava:main.buffer_inspect_hook_ancestor_cycle", - "expect_detail": [ - "Maximum call stack size exceeded" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when top-level util.inspect catches errors thrown by a hook's own getter", - "why": "node catches overflow only while formatting, so a getter throwing a forged overflow RangeError propagates; catching at the top swallows it and changes cycle outcomes.", - "source": "pkg/runtime/js/internal/util.js", - "find": "return hookNesting === 0 ? inspect(v, opts, [], 0) : hookInspect(v, opts);", - "replace": "return hookInspect(v, opts);", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "cycle 2 self-refs util.inspect: returned" - ] - }, - { - "name": "buffer_inspect_cycle_bounded dies when util passes hooks a depth-inheriting closure instead of util.inspect", - "why": "node passes util.inspect itself; the old closure also bypasses the overflow latch, so cycles stop terminating.", - "source": "pkg/runtime/js/internal/util.js", - "find": "return v[customInspect](depth, opts, inspectPublic);", - "replace": "return v[customInspect](depth, opts, function (val, o) { return inspect(val, o || opts, [v], depth + 1); });", - "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", - "expect_detail": [ - "timed out" - ] - }, - { - "name": "buffer_inspect_cycle_bounded dies when console hands hooks the old one-argument closure", - "why": "the closure dropped options (wrong depth for extras) and bypasses the overflow latch.", - "source": "pkg/runtime/js/console.js", - "find": "return v[customInspect](depth, {}, inspectWithOptions);", - "replace": "return v[customInspect](depth, {}, function (val) { return inspect(val, [v], depth + 1); });", - "gate": "odin:cmd/lava:main.buffer_inspect_cycle_bounded", - "expect_detail": [ - "timed out" - ] - }, - { - "name": "buffer_inspect_extras_cap dies when the Buffer extras string-key cap moves", - "why": "the string-key scan is O(length), capped at 64 (cost recorded in buffer.js).", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "var INSPECT_EXTRAS_MAX_LENGTH = 64;", - "replace": "var INSPECT_EXTRAS_MAX_LENGTH = 65;", - "gate": "odin:cmd/lava:main.buffer_inspect_extras_cap", - "expect_detail": [ - "65 omits string key" - ] - }, - { - "name": "buffer_inspect_extras_pollution dies when the hook reads showHidden through the prototype chain", - "why": "a polluted Object.prototype.showHidden would list hidden properties of every logged Buffer.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "var showHidden = ObjectPrototypeHasOwnProperty(ctx, 'showHidden') && !!ctx.showHidden;", - "replace": "var showHidden = !!ctx.showHidden;", - "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", - "expect_detail": [ - "inherited showHidden" - ] - }, - { - "name": "buffer_inspect_extras_pollution dies when the hook reads depth through the prototype chain", - "why": "a polluted Object.prototype.depth = null would lift the depth bound on every logged Buffer's extras.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined,", - "replace": "depth: ctx.depth,", - "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", - "expect_detail": [ - "inherited depth" - ] - }, - { - "name": "buffer_inspect_extras_pollution dies when console's hook inspect reads depth through the prototype chain", - "why": "a polluted Object.prototype.depth = null would lift the depth bound for every custom hook's sub-inspect under console.log.", - "source": "pkg/runtime/js/console.js", - "find": "options !== null && typeof options === 'object' && ObjectHasOwn(options, 'depth')", - "replace": "options !== null && typeof options === 'object'", - "gate": "odin:cmd/lava:main.buffer_inspect_extras_pollution", - "expect_detail": [ - "console hook inherited depth" - ] } ] } diff --git a/pkg/runtime/js/console.js b/pkg/runtime/js/console.js index 8ee8575..a2515b8 100644 --- a/pkg/runtime/js/console.js +++ b/pkg/runtime/js/console.js @@ -7,22 +7,12 @@ var customInspect = typeof Symbol !== 'undefined' && Symbol.for ? Symbol.for('nodejs.util.inspect.custom') : null; - // Captured at prelude eval, before any user code runs. - var ObjectHasOwn = Object.hasOwn; - // Stack-overflow latch for custom hooks; the same design as internal/util.js - // callCustomInspect (copied, not shared: this prelude has no `require`). - var hookNesting = 0; - var hookOverflow = null; function quote(s) { return "'" + s.replaceAll('\\', '\\\\').replaceAll("'", "\\'").replaceAll('\n', '\\n') + "'"; } - // `maxDepth` is the nesting cut-off. The prelude's own callers omit it and keep the - // historical 4 (node's console.log cuts at 2 — a pre-existing gap, not touched here); - // inspectWithOptions passes a caller's `depth` through. - function inspect(v, seen, depth, maxDepth) { - if (maxDepth === undefined) maxDepth = 4; + function inspect(v, seen, depth) { var t = typeof v; if (v === null) return 'null'; if (t === 'undefined') return 'undefined'; @@ -38,53 +28,39 @@ // Honor the util.inspect custom hook (Buffer renders as ""). Node // passes (depth, options, inspect) and inspects a non-string result in place // (a string is used verbatim; returning the object itself falls through). - // - // The third argument mirrors internal/util.js: an inspect that starts again at - // depth 0 under the options it is given, as node's util.inspect does. It used to - // be a one-argument closure that dropped those options and continued this call's - // nesting, so Buffer's extra properties printed at the prelude's depth instead of - // node's (`console.log(b)` with `b.o` five levels deep showed four of them). Like - // util.js it catches a stack overflow escaping a hook (a hook that inspects its own - // ancestor) and latches it, so console.log prints instead of throwing; see - // inspectWithOptions. - // - // A copy rather than a call into util.js because this prelude runs before the module - // loader and has no `require`; the two inspectors are to be unified under #104. if (customInspect && typeof v[customInspect] === 'function') { - var custom = callCustomInspect(v, depth); + var nested = seen.concat([v]); + var recurse = function (val) { + return inspect(val, nested, depth + 1); + }; + var custom = v[customInspect](depth, {}, recurse); if (custom !== v) { - return typeof custom === 'string' - ? custom - : inspect(custom, seen.concat([v]), depth, maxDepth); + return typeof custom === 'string' ? custom : inspect(custom, nested, depth); } } if (seen.indexOf(v) !== -1) return '[Circular *1]'; if (v instanceof Error) return v.stack ? v.stack : v.name + ': ' + v.message; if (v instanceof Date) return isNaN(v.getTime()) ? 'Invalid Date' : v.toISOString(); if (v instanceof RegExp) return v.toString(); - if (depth > maxDepth) return Array.isArray(v) ? '[Array]' : '[Object]'; + if (depth > 4) return Array.isArray(v) ? '[Array]' : '[Object]'; seen = seen.concat([v]); if (Array.isArray(v)) { if (v.length === 0) return '[]'; var arr = []; - for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], seen, depth + 1, maxDepth)); + for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], seen, depth + 1)); return '[ ' + arr.join(', ') + ' ]'; } if (typeof Map !== 'undefined' && v instanceof Map) { var mp = []; v.forEach(function (val, key) { - mp.push( - inspect(key, seen, depth + 1, maxDepth) + - ' => ' + - inspect(val, seen, depth + 1, maxDepth), - ); + mp.push(inspect(key, seen, depth + 1) + ' => ' + inspect(val, seen, depth + 1)); }); return 'Map(' + v.size + ') {' + (mp.length > 0 ? ' ' + mp.join(', ') + ' ' : '') + '}'; } if (typeof Set !== 'undefined' && v instanceof Set) { var st = []; v.forEach(function (val) { - st.push(inspect(val, seen, depth + 1, maxDepth)); + st.push(inspect(val, seen, depth + 1)); }); return 'Set(' + v.size + ') {' + (st.length > 0 ? ' ' + st.join(', ') + ' ' : '') + '}'; } @@ -96,53 +72,11 @@ for (var k = 0; k < keys.length; k++) { var key = keys[k]; var label = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : quote(key); - props.push(label + ': ' + inspect(v[key], seen, depth + 1, maxDepth)); + props.push(label + ': ' + inspect(v[key], seen, depth + 1)); } return prefix + '{ ' + props.join(', ') + ' }'; } - // The hook's inspect argument, util.inspect-shaped: (value, options) from depth 0, with - // node's default depth of 2 and `depth: null` meaning unbounded. `depth` is read as an - // OWN option only, as node copies only the caller's own keys: an inherited read let a - // polluted Object.prototype.depth = null lift the bound for every hook's sub-inspect. - // Pinned Lava-only by buffer_inspect_extras_pollution - // (cmd/lava/buffer_inspect_extras_test.odin). - // - // It also catches a stack overflow, where internal/util.js hookInspect does and for the - // same reasons (node catches it while formatting, not around the hook call; the first - // one is latched so a cycle with several ways back throws in bounded time instead of - // re-descending per key). - function inspectWithOptions(value, options) { - var d = - options !== null && typeof options === 'object' && ObjectHasOwn(options, 'depth') - ? options.depth - : undefined; - if (hookOverflow !== null) throw hookOverflow; - try { - return inspect(value, [], 0, d === undefined ? 2 : d === null ? Infinity : d); - } catch (e) { - if ( - e === hookOverflow || - e === null || - typeof e !== 'object' || - e.name !== 'RangeError' || - e.message !== 'Maximum call stack size exceeded.' - ) - throw e; - hookOverflow = e; - return '[Object: Inspection interrupted prematurely. Maximum call stack size exceeded.]'; - } - } - - function callCustomInspect(v, depth) { - hookNesting++; - try { - return v[customInspect](depth, {}, inspectWithOptions); - } finally { - if (--hookNesting === 0) hookOverflow = null; - } - } - function stringify(v) { return typeof v === 'string' ? v : inspect(v, [], 0); } diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 7504c02..6a0b314 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -62,9 +62,6 @@ var StringFromCharCode = String.fromCharCode; var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; var TypedArrayPrototypeGetLength = P.TypedArrayPrototypeGetLength; - var ObjectGetOwnPropertyNames = P.ObjectGetOwnPropertyNames; - var ObjectGetOwnPropertySymbols = P.ObjectGetOwnPropertySymbols; - var ObjectGetOwnPropertyDescriptor = P.ObjectGetOwnPropertyDescriptor; /** * @type {Object} lowercased name → canonical encoding. @@ -784,13 +781,7 @@ var p = Buffer.prototype; - /** @returns {string} `` with the bytes only (no extra properties). */ function inspectBuffer(buf) { - return ''; - } - - /** @returns {string} The hex pairs and the `... N more bytes` tail, unframed. */ - function inspectBytes(buf) { var max = inspectMaxBytes; var shown = buf.length > max ? max : buf.length; var hex = ''; @@ -811,266 +802,20 @@ } } var remaining = buf.length - shown; - // The separator is unconditional: node appends ` ... N more bytes` to whatever - // hex it produced, so INSPECT_MAX_BYTES = 0 prints TWO spaces after ` 0) hex += ' ... ' + remaining + ' more byte' + (remaining > 1 ? 's' : ''); - return hex; - } - - /** - * Buffers longer than this skip the own string-key scan in inspectExtras. - * - * JS has no way to list a typed array's NAMED own properties without first listing - * every index: Object.getOwnPropertyNames / Object.keys / Reflect.ownKeys / for-in - * all materialise one string per byte (node avoids this with its native - * getOwnNonIndexProperties). So every util.inspect/console.log of a Buffer at or - * under this length pays that scan, extras or not. - * - * Cost of the scan alone, measured on bin/lava, one launch per size: performance.now() - * around max(20, 2e6/n) back-to-back Object.getOwnPropertyNames calls on one - * Buffer.alloc(n), mean per call — 1.2µs at n=16, 18µs at n=256, 86µs at n=1024, - * 13.8ms at n=65536; a single Reflect.ownKeys on Buffer.alloc(1e7) took 11.1s (one - * call, Date.now()). Unbounded, logging a 64 KiB read chunk would cost thousands of - * times what the bytes-only render does. - * - * Cost AT the cap, whole util.inspect(Buffer.alloc(n)) with no extras, head vs the - * base hook (this binary with the pre-#391 bytes-only hook swapped in), mean per call - * after 200 warm-ups, performance.now(), 3 interleaved launches per arm. At 300 000 - * calls per launch: n=64 head 7.3/8.5/9.1µs vs base 1.9/2.2/3.0µs (~3.5x); n=1024 (past - * the cap, so symbols only) 2.91/2.91/2.94 vs 2.13/2.24/2.33µs. At max(20, 2e6/n) calls - * per launch (31 250 at n=64, 125 000 at n=16, 2 000 000 at n=0): n=64 9.0/9.9/10.0 vs - * 2.9/3.4/3.8µs; n=65 4.1/4.5/4.7 vs 3.2/3.3/4.7µs; n=16 2.3/2.4/2.7 vs 1.0/1.2/1.6µs; - * n=0 0.35/0.35/0.38 vs 0.12/0.12/0.13µs. At the previous cap of 1024 the head cost was - * ~92µs against ~5µs. - * - * Past the cap, string-keyed extras are omitted — a deviation, pinned Lava-only by - * buffer_inspect_extras_cap in cmd/lava/buffer_inspect_extras_test.odin. - * Symbol keys are always shown: Object.getOwnPropertySymbols lists no indices. - */ - var INSPECT_EXTRAS_MAX_LENGTH = 64; - - /** @returns {boolean} Whether `key` is an own enumerable property of `obj`. */ - function isEnumerableOwn(obj, key) { - var desc = ObjectGetOwnPropertyDescriptor(obj, key); - return desc !== undefined && desc.enumerable === true; - } - - /** @returns {boolean} node's keyStrRegExp, /^[a-zA-Z_][a-zA-Z_0-9]*$/, as a loop. */ - function isPlainKey(key) { - var n = key.length; - if (n === 0) return false; - for (var i = 0; i < n; i++) { - var c = StringPrototypeCharCodeAt(key, i); - if (!( - (c >= 0x41 && c <= 0x5a) || // A-Z - (c >= 0x61 && c <= 0x7a) || // a-z - c === 0x5f || // _ - (i > 0 && c >= 0x30 && c <= 0x39) // 0-9, not leading - )) - return false; - } - return true; - } - - /** - * Renders a Buffer's extra own properties as `key: value, …` (no frame), or ''. - * - * Keys are formatted here rather than by building a null-prototype object and - * inspecting that, which is what node does: Lava's util.inspect drops symbol keys - * and prints no `[Object: null prototype]` prefix to slice off. - * - * Each value is inspected wrapped in a one-element array and the `[ ` / ` ]` sliced - * off. Node inspects the property bag at depth 0, so a value sits at depth 1 under - * the caller's own `depth`, and a Buffer nested in a property receives that SAME - * depth for its own extras. The wrapper puts the value at depth 1 without touching - * `depth`; an earlier revision passed `depth - 1` instead, which got the values right - * but handed a nested Buffer one level less than node does. - * - * `opts` is rebuilt from ctx's OWN properties: ctx is the caller's options object, so - * an inherited read would let a polluted `Object.prototype.showHidden` list hidden - * properties of every logged Buffer, or `Object.prototype.depth = null` lift the - * depth bound (node copies only the caller's own keys). Pinned Lava-only by - * buffer_inspect_extras_pollution in cmd/lava/buffer_inspect_extras_test.odin. - */ - function inspectExtras(buf, ctx, inspect) { - var showHidden = ObjectPrototypeHasOwnProperty(ctx, 'showHidden') && !!ctx.showHidden; - // Built on the first extra, not per call: most logged Buffers have none. - var opts = null; - var out = ''; - var len = TypedArrayPrototypeGetLength(buf); - var key; - var piece; - if (len <= INSPECT_EXTRAS_MAX_LENGTH) { - // A typed array's [[OwnPropertyKeys]] lists its integer indices first, in order - // (ECMA-262 §10.4.5.7), and a canonical numeric key can never be added as a named - // property, so everything from position `len` on is a non-index name. - var names = ObjectGetOwnPropertyNames(buf); - for (var i = len; i < names.length; i++) { - key = names[i]; - if (showHidden || isEnumerableOwn(buf, key)) { - if (opts === null) opts = extrasOptions(ctx, showHidden); - piece = inspectExtra(isPlainKey(key) ? key : null, key, buf[key], opts, inspect); - if (piece === null) return INSPECT_OVERFLOW_MARKER; - out += (out === '' ? '' : ', ') + piece; - } - } - } - var syms = ObjectGetOwnPropertySymbols(buf); - for (var s = 0; s < syms.length; s++) { - key = syms[s]; - if (showHidden || isEnumerableOwn(buf, key)) { - if (opts === null) opts = extrasOptions(ctx, showHidden); - piece = inspectExtra(StringG(key), key, buf[key], opts, inspect); - if (piece === null) return INSPECT_OVERFLOW_MARKER; - out += (out === '' ? '' : ', ') + piece; - } - } - return out; - } - - /** @returns {object} The options each extra is inspected with (see inspectExtras). */ - function extrasOptions(ctx, showHidden) { - return { - __proto__: null, - depth: ObjectPrototypeHasOwnProperty(ctx, 'depth') ? ctx.depth : undefined, - showHidden: showHidden, - }; - } - - /** - * Renders one extra as `label: value`, or returns null when inspecting it overflowed - * the stack (the caller then prints the interrupted marker instead of its extras). - * - * `value` arrives already read: node reads the property before its overflow catch, so - * a getter that throws — even a RangeError carrying the overflow's message — - * propagates, and only errors raised while INSPECTING the value are candidates. - * - * Overflow. A Buffer that reaches itself recurses until the stack runs out; the - * innermost catch on the stack renders the marker. What keeps that bounded is the latch - * in the inspect this hook calls through (internal/util.js hookInspect, console.js - * inspectWithOptions): once an overflow has been caught there, any hook-side inspect - * rethrows it instead of descending again. Before it, every enclosing hook moved on to - * its next key and recursed to the limit again, so `b.k0 = b; b.k1 = b` did work like - * 2^depth and never finished. With one way back nothing descends again and the call - * returns with the marker, as node's does; with two or more it throws, which is node - * 24.22.1's outcome in a fresh process (see case 70 for why node's is accidental). - * `inspectOverflow` only remembers the overflow this hook already reported, so the - * latch's rethrow of that same error passes through every enclosing Buffer hook instead - * of being swallowed again as a fresh overflow at each level. Pinned by case 70 and, - * for bounded time, buffer_inspect_cycle_bounded in - * cmd/lava/buffer_inspect_extras_test.odin. - */ - function inspectExtra(label, key, value, opts, inspect) { - try { - return (label !== null ? label : inspect(key)) + ': ' + inspectValue(value, opts, inspect); - } catch (e) { - if (e === inspectOverflow || !isStackOverflow(e)) throw e; - inspectOverflow = e; - return null; - } + return ''; } - /** - * @returns {boolean} Whether `e` is the engine's stack-overflow RangeError. - * - * A name + message compare — the same test node's isStackOverflowError makes, so a - * forged look-alike thrown from DEEP inside an extra's value is taken for an overflow - * on both runtimes. It cannot be done soundly in JS: nothing distinguishes the engine's - * own error object. What is sound is where the compare runs: only around inspecting a - * value, never around reading it (see inspectExtra). - */ - function isStackOverflow(e) { - return ( - e !== null && - typeof e === 'object' && - e.name === 'RangeError' && - e.message === STACK_OVERFLOW_MESSAGE - ); - } - - /** @returns {string} `value` inspected one level down (see inspectExtras). */ - function inspectValue(value, opts, inspect) { - var wrapped = inspect([value], opts); - // Both Lava inspectors render a one-element array as `[ x ]` on one line. Only a - // negative depth turns the wrapper itself into `[Array]`; fall back to the value. - if ( - StringPrototypeSlice(wrapped, 0, 2) === '[ ' && - StringPrototypeSlice(wrapped, wrapped.length - 2) === ' ]' - ) - return StringPrototypeSlice(wrapped, 2, wrapped.length - 2); - return inspect(value, opts); - } - - // JSC's stack-overflow RangeError text (node's V8 text is the same sentence). - var STACK_OVERFLOW_MESSAGE = 'Maximum call stack size exceeded.'; - var INSPECT_OVERFLOW_MARKER = - 'Inspection interrupted prematurely. Maximum call stack size exceeded'; - - // The overflow already reported by a Buffer hook on the stack (see inspectExtra), and - // how many Buffer hooks are on it: the outermost clears it. - var inspectNesting = 0; - var inspectOverflow = null; - - /** - * Buffer's util.inspect hook; also `buf.inspect`, the same function as in node. - * @param {number} [recurseTimes] Unused; the depth that matters is `ctx.depth`. - * @param {{showHidden?: boolean, depth?: number|null}} [ctx] util.inspect's options. - * @param {(value: *, options?: object) => string} [inspect] The caller's inspect: - * util.inspect from util.inspect, the prelude's own from console.log. - * @returns {string} ``, plus `, key: value` per extra own property - * whenever `ctx` is an object. - * @throws Whatever a getter on an extra throws; and, when a Buffer reaches itself by - * two or more extras, the stack-overflow RangeError (see inspectExtra). - * @node ``: insertion order, then symbols - * (`Symbol(s): 1`); non-enumerables only with showHidden; an enumerable getter - * prints its value (`g: 7`); nested values honour `depth` - * (`o: { a: { b: [Object] } }`, depth 0 → `o: [Object]`); a zero-length buffer - * prints ``; breakLength/compact change nothing. A direct - * `buf.inspect(0, {})` renders the extras too; `buf.inspect()` with no context - * prints the bytes only. A Buffer with ONE way back to itself recurses until the - * stack runs out and ends in `Inspection interrupted prematurely. Maximum call - * stack size exceeded`; with two or more, node 24.22.1 throws in a fresh process. - * Node 24.22.1, verified by the #391 probes. - * @deviates A Buffer longer than INSPECT_EXTRAS_MAX_LENGTH (64) shows its symbol-keyed - * extras but not its string-keyed ones — the cost bound recorded at that - * constant. Pinned Lava-only by buffer_inspect_extras_cap in - * cmd/lava/buffer_inspect_extras_test.odin. With two or more ways back, node - * recurses with exponential work once its key regex is compiled; Lava throws - * in bounded time (buffer_inspect_cycle_bounded). Not a deviation but a - * util.inspect gap this inherits: `colors: true` leaves the extras uncoloured - * (gap, tracked in #104). - */ - function inspectHook(recurseTimes, ctx, inspect) { - var str = inspectBytes(this); - if (ctx !== null && typeof ctx === 'object') { - if (typeof inspect !== 'function') { - // A direct `buf.inspect(0, {})` passes no inspect function; node still renders - // the extras with its own. util is required HERE, not at module eval: buffer is - // on the loader's eager list and util is not, so a module-eval require would - // instantiate util (and its dependencies) before user code on every start. - // There is no pristine handle to take instead — the loader publishes one only - // for Buffer (require.pristineBuffer) — so this reads util's exports, which user - // code can also reach: a replaced `util.inspect` steers this one fallback. - // It is read per call and NOT cached, so a replacement never outlives itself. - // util.inspect and console.log hand the hook their own function. - inspect = require('util').inspect; - } - inspectNesting++; - var extras; - try { - extras = inspectExtras(this, ctx, inspect); - } finally { - if (--inspectNesting === 0) inspectOverflow = null; - } - if (extras !== '') str += (TypedArrayPrototypeGetLength(this) !== 0 ? ', ' : '') + extras; - } - return ''; - } - - p.inspect = inspectHook; + p.inspect = function () { + return inspectBuffer(this); + }; if (customInspectSymbol) { Object.defineProperty(p, customInspectSymbol, { - value: inspectHook, + value: function () { + return inspectBuffer(this); + }, writable: true, configurable: true, }); diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js index 651b193..dfe1093 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/internal/util.js @@ -132,24 +132,12 @@ // a string is used verbatim, anything else (number, object, …) is inspected // in its place, and returning the object itself falls through to default // formatting. - // - // The third argument is util.inspect ITSELF, as in node (`insp === util.inspect` - // is true there): a hook that inspects a sub-value starts again at depth 0 under - // whatever options it passes. It used to be a closure that inherited this call's - // options and nesting, so a hook one level down got one level less depth than - // node gives it — and Buffer's extra-property rendering, which puts each value at - // depth 1 under the caller's own `depth` the way node does, came out a level short. - // - // A hook that inspects a structure pointing back at an ancestor (a parent pointer: - // the hook returns `inspect(parent)`) recurses until the stack overflows, in node as - // here, because the inspect it is handed starts a fresh visited set. Node catches the - // overflow and prints what it had; Lava used to let it throw out of util.inspect. - // The inspect a hook calls now catches it the same way — see hookInspect for where, - // and for the latch that keeps it from turning into exponential work. Pinned by - // tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js. if (customInspect && typeof v[customInspect] === 'function') { var nested = seen.concat([v]); - var custom = callCustomInspect(v, depth, opts); + var recurse = function (val, o) { + return inspect(val, o || opts, nested, depth + 1); + }; + var custom = v[customInspect](depth, opts, recurse); if (custom !== v) { return typeof custom === 'string' ? custom : inspect(custom, opts, nested, depth); } @@ -195,54 +183,6 @@ return prefix + '{ ' + props.join(', ') + ' }'; } - // Stack-overflow handling under custom hooks, the shape Buffer's hook uses - // (internal/buffer.js inspectExtra). - // - // WHERE it is caught matters. Node catches an overflow while formatting an object's - // body, not around the hook call itself, so an error thrown by the hook's own code — - // a getter that throws a RangeError carrying the overflow's message — still - // propagates. The catch is therefore in the inspect a hook is HANDED (hookInspect), - // which is where a hook re-entering a cycle recurses; the hook call stays uncaught. - // - // The first overflow caught there is rendered as node's marker and latched; any - // further hook-side inspect while it is latched rethrows it instead of descending - // again. With one way back to an ancestor nothing descends again and util.inspect - // returns, as node's does; with several it throws in bounded time where unlatched - // catching re-descended once per key and level (work like k^depth). The outermost hook - // clears the latch. - var hookNesting = 0; - var hookOverflow = null; - var STACK_OVERFLOW_MESSAGE = 'Maximum call stack size exceeded.'; - - function callCustomInspect(v, depth, opts) { - hookNesting++; - try { - return v[customInspect](depth, opts, inspectPublic); - } finally { - if (--hookNesting === 0) hookOverflow = null; - } - } - - /** util.inspect as called from inside a custom hook (see above). */ - function hookInspect(v, opts) { - if (hookOverflow !== null) throw hookOverflow; - try { - return inspect(v, opts, [], 0); - } catch (e) { - // The same name + message test node's isStackOverflowError makes. - if ( - e === hookOverflow || - e === null || - typeof e !== 'object' || - e.name !== 'RangeError' || - e.message !== STACK_OVERFLOW_MESSAGE - ) - throw e; - hookOverflow = e; - return '[Object: Inspection interrupted prematurely. Maximum call stack size exceeded.]'; - } - } - function stringify(v) { return typeof v === 'string' ? v : inspect(v, {}, [], 1); } @@ -941,9 +881,7 @@ } function inspectPublic(v, opts) { - // One function, so a hook's third argument stays `=== util.inspect` as in node; only - // a call made while a hook is running takes the overflow-catching path. - return hookNesting === 0 ? inspect(v, opts, [], 0) : hookInspect(v, opts); + return inspect(v, opts, [], 0); } // util.inspect.colors is the live style table (styleText reads it); util.inspect.custom // is the symbol a value implements to control its own inspection. diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index 086210f..b0d29a4 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -160,7 +160,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. - **UTF-8 decode (issue #107):** `toString('utf8')` / `Buffer.from(string)` use the Unicode "U+FFFD substitution of maximal subparts" rule (the native Odin decoder, [buffer.odin](../pkg/runtime/buffer.odin)), matching Node/V8: an ill-formed subsequence collapses to a single replacement (`Buffer.from([0xF0,0x9F]).toString()` → `'�'`, not `'��'`). Embedded NUL is preserved. - **Encodings:** `utf8`/`utf-8`, `utf16le`/`ucs2`, `latin1`/`binary`, `ascii`, `hex`, `base64`, `base64url` (with Node's lenient base64 normalization and `ascii` high-bit masking). - **Errors:** Node `err.code`s and message shapes for `ERR_INVALID_ARG_TYPE`, `ERR_INVALID_ARG_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_BUFFER_OUT_OF_BOUNDS`, `ERR_UNKNOWN_ENCODING`, `ERR_INVALID_BUFFER_SIZE`. -- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. A Buffer's extra own properties are rendered after the bytes, as in Node: ``. They appear in insertion order with symbol keys last, non-enumerable ones only under `showHidden`, and nested values cut off at `depth`. A direct `buf.inspect(0, {})` renders them too. A Buffer whose extras lead back to itself by one key ends in Node's `Inspection interrupted prematurely` marker instead of throwing. With two or more keys back it throws a stack-overflow `RangeError` in bounded time; Node 24 throws in a fresh process and recurses exponentially once its key regex is compiled. One deviation: a Buffer **longer than 64 bytes** shows its symbol-keyed extras but not its string-keyed ones. JavaScript cannot list a typed array's named properties without first making one string per index, so without the cap every inspect would cost O(length). At the cap the whole inspect costs about 3.5x the bytes-only render (the measurement and its method are recorded at `INSPECT_EXTRAS_MAX_LENGTH` in [buffer.js](../pkg/runtime/js/internal/buffer.js)). The deviation is pinned by `cmd/lava/buffer_inspect_extras_test.odin`. `colors: true` leaves the extras uncoloured; that is a `util.inspect` gap (#104), not a Buffer one. +- **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. - **Module extras:** `Blob`, `File`, `SlowBuffer`, `atob`/`btoa`, `isAscii`, `isUtf8`, `transcode`, `resolveObjectURL`, `kMaxLength`/`kStringMaxLength`/`constants`/`INSPECT_MAX_BYTES`. `Blob`/`File` honor the `endings: 'native'` option (string parts have `\n`/`\r\n` converted to the platform newline — derived from `process.platform` — while a lone `\r` and binary parts are left verbatim, matching Node). **Intentional differences / deferred:** diff --git a/tests/node-compat/cases/70-buffer-size-inspect-parity.js b/tests/node-compat/cases/70-buffer-size-inspect-parity.js index faa6539..1125a45 100644 --- a/tests/node-compat/cases/70-buffer-size-inspect-parity.js +++ b/tests/node-compat/cases/70-buffer-size-inspect-parity.js @@ -1,4 +1,4 @@ -// Four Buffer divergences from node 24 (issue #391), each printed rather than asserted so +// Three Buffer divergences from node 24 (issue #391), each printed rather than asserted so // a regression shows up as a readable diff against the node oracle. // // 1. ERR_INVALID_ARG_TYPE wording: size/offset/length checks spliced a bare `number` @@ -8,8 +8,6 @@ // number branch through Buffer.alloc's size validation. // 3. INSPECT_MAX_BYTES = 0: node keeps the separator space even with no hex shown, // `` (two spaces). -// 4. util.inspect shows a Buffer's extra own properties after the bytes, -// ``, honouring showHidden and depth. // // Every line prints a RESULT. A case whose setup threw on both runtimes would print the // same THREW line twice and pass while proving nothing (CLAUDE.md §6), so each THREW @@ -27,40 +25,6 @@ const buffer = require('node:buffer'); // deprecation warnings. Silenced so the compared output is the behaviour under test. process.noDeprecation = true; -// --- Self-referencing Buffers with MORE than one way back -------------------------------- -// FIRST in the file, deliberately. In a fresh node 24.22.1 process these throw within -// ~20 ms: after the first stack overflow is caught, the enclosing hook moves on to its -// next key, and node's key-quoting regex /^[a-zA-Z_][a-zA-Z_0-9]*$/ is compiled lazily -// at that point, at the stack limit, so it throws a SyntaxError that node's overflow catch -// does not recognise. Once anything has inspected an object key, the regex is already -// compiled and node instead recurses with work like k^depth: in a process that had -// inspected `b.self = b` first, `b.k0 = b; b.k1 = b` ran 148 s and then threw -// `RangeError: Invalid string length`. Lava throws in both orders. Its overflow latch lets -// no hook descend again once an overflow has been caught, so this ends in bounded time. -// Only "threw" is printed: node's error is that V8 SyntaxError, Lava's is the stack-overflow -// RangeError. Bounded time is pinned Lava-only by buffer_inspect_cycle_bounded in -// cmd/lava/buffer_inspect_extras_test.odin. -for (const refs of [2, 3]) { - const b = Buffer.from([1]); - for (let i = 0; i < refs; i++) b['k' + i] = b; - for (const [how, fn] of [ - ['util.inspect', () => util.inspect(b)], - ['console.log', () => console.log(b)], - // A direct call: no util.inspect or console.log frame above the hook, so only the - // Buffer hook's own overflow latch keeps this bounded. - ['inspect(0, {})', () => b.inspect(0, {})], - ]) { - let out; - try { - fn(); - out = 'returned'; - } catch { - out = 'threw'; - } - console.log(`cycle ${refs} self-refs ${how}: ${out}`); - } -} - const MAX = String(buffer.constants.MAX_LENGTH); function show(label, fn) { let r; @@ -144,213 +108,3 @@ for (const max of [0, 1, 2]) { } } buffer.INSPECT_MAX_BYTES = 50; - -// --- 4. Extra own properties ----------------------------------------------------------- -const sym = Symbol('s'); -const cases = []; -{ - const x = bufOf(3); - x.foo = 'bar'; - cases.push(['string prop', x]); -} -{ - const x = bufOf(3); - x.b = 2; - x.a = 1; - cases.push(['insertion order b,a', x]); -} -{ - const x = bufOf(3); - x[sym] = 1; - x.after = 'str'; - cases.push(['symbol after string', x]); -} -{ - const x = bufOf(3); - Object.defineProperty(x, 'hid', { value: 1, enumerable: false }); - cases.push(['non-enumerable', x]); -} -{ - const x = bufOf(3); - Object.defineProperty(x, 'g', { get: () => 7, enumerable: true }); - cases.push(['enumerable getter', x]); -} -{ - const x = bufOf(3); - x.o = { a: { b: { c: 1 } } }; - cases.push(['nested object', x]); -} -{ - const x = bufOf(3); - x['a-b'] = 1; - x.$d = 2; - x._ok = 3; - cases.push(['key quoting', x]); -} -{ - const x = bufOf(0); - x.foo = 'bar'; - cases.push(['zero-length', x]); -} -{ - const x = bufOf(10); - x.foo = 'bar'; - cases.push(['truncated max=4', x, 4]); -} -{ - const x = bufOf(3); - x.foo = 'bar'; - cases.push(['max=0', x, 0]); -} -const opts = [ - ['default', undefined], - ['showHidden', { showHidden: true }], - ['depth0', { depth: 0 }], - ['depthNull', { depth: null }], - ['breakLength20', { breakLength: 20 }], - ['compact false', { compact: false }], -]; -for (const [label, x, max] of cases) { - buffer.INSPECT_MAX_BYTES = max === undefined ? 50 : max; - for (const [on, o] of opts) console.log(`${label} ${on}:`, JSON.stringify(util.inspect(x, o))); - console.log(`${label} nested:`, JSON.stringify(util.inspect({ k: x }))); - console.log(`${label} nested depth0:`, JSON.stringify(util.inspect({ k: x }, { depth: 0 }))); - console.log(`${label} x.inspect():`, JSON.stringify(x.inspect())); - console.log(`${label} x[custom]():`, JSON.stringify(x[util.inspect.custom]())); - // Concatenated, not passed as a second argument: a `%O` in console.log's FIRST - // argument is itself a format directive. - console.log(label + ' format(%O): ' + JSON.stringify(util.format('%O', x))); -} -buffer.INSPECT_MAX_BYTES = 50; -// console.log renders through the same hook (stdout is compared byte-for-byte). -{ - const x = bufOf(3); - x.foo = 'bar'; - console.log(x); - console.log([x]); -} - -// The hook's third argument is util.inspect itself, so a custom hook that inspects a -// sub-value starts again at depth 0 rather than inheriting its caller's nesting. -const custom = util.inspect.custom; -console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp({ a: { b: { c: 1 } } }) } })); -console.log(util.inspect({ x: { [custom]: (d, o, insp) => insp === util.inspect } })); - -// console.log must cut nested extras at node's depth too, not at the prelude's own -// limit: the extras' values sit one level below the Buffer whatever its nesting. -{ - const x = bufOf(1); - x.o = { a: { b: { c: { d: { e: 1 } } } } }; - console.log(x); - console.log([x]); - console.log({ k: { x } }); - const m = bufOf(1); - m.meta = new Map([[1, { a: { b: { c: 1 } } }]]); - console.log(m); -} - -// Called directly WITH a context object, the hook renders the extras as well; with no -// context it prints the bytes only. -{ - const x = bufOf(3); - x.tag = 1; - Object.defineProperty(x, 'h', { value: 5, enumerable: false }); - x.o = { a: { b: { c: 1 } } }; - console.log('inspect(0, {}):', JSON.stringify(x.inspect(0, {}))); - console.log('inspect(0, {depth: 0}):', JSON.stringify(x.inspect(0, { depth: 0 }))); - console.log('[custom](0, {showHidden}):', JSON.stringify(x[custom](0, { showHidden: true }))); - console.log( - 'inspect():', - JSON.stringify(x.inspect()), - 'inspect(0):', - JSON.stringify(x.inspect(0)), - ); -} - -// Key rendering: node's identifier test refuses a leading digit ('1a' is quoted, 'a1' -// is not), and a non-enumerable SYMBOL key shows only under showHidden. -{ - const x = bufOf(3); - x['1a'] = 1; - x.a1 = 2; - Object.defineProperty(x, Symbol('h'), { value: 3, enumerable: false }); - x[Symbol('v')] = 4; - console.log('keys:', JSON.stringify(util.inspect(x))); - console.log('keys showHidden:', JSON.stringify(util.inspect(x, { showHidden: true }))); -} - -// A Buffer whose extras lead back to itself recurses until the stack runs out. node -// catches the overflow inside inspect and prints what it had, ending in -// `Inspection interrupted prematurely. Maximum call stack size exceeded`; logging never -// throws. Where the cut lands depends on the engine's stack size, so the lines print the -// shape rather than the full string: it returned, how it starts, that the marker is -// there, and how it ends. -function cycleShape(label, fn) { - let s; - try { - s = fn(); - } catch (e) { - console.log(label, 'THREW', e.name); - return; - } - console.log( - label, - JSON.stringify(s.slice(0, 40)), - s.includes('Inspection interrupted prematurely. Maximum call stack size exceeded'), - JSON.stringify(s.slice(-4)), - ); -} -{ - const a = Buffer.from([1]); - a.self = a; - cycleShape('cycle self:', () => util.inspect(a)); - const b = Buffer.from([2]); - const c = Buffer.from([3]); - b.other = c; - c.other = b; - cycleShape('cycle pair:', () => util.inspect(b)); - const d = Buffer.from([4]); - d.o = { back: d }; - cycleShape('cycle via object:', () => util.inspect(d)); -} - -// Errors other than a stack overflow propagate out of inspect, as in node: from a getter -// on the Buffer itself (read outside any catch) and from a hook further down (thrown -// inside the inspect of an extra's value, where the overflow catch must not swallow it). -// A getter that throws a RangeError carrying the overflow's own message also propagates: -// node reads the property outside its overflow catch, and so does Lava. -function threw(label, fn) { - try { - fn(); - console.log(label, 'returned'); - } catch (e) { - console.log(label, 'THREW', e.name, e.message); - } -} -{ - const g = Buffer.from([1]); - Object.defineProperty(g, 'bad', { - enumerable: true, - get() { - throw new TypeError('boom'); - }, - }); - threw('getter TypeError util.inspect:', () => util.inspect(g)); - threw('getter TypeError inspect(0, {}):', () => g.inspect(0, {})); - const h = Buffer.from([2]); - h.inner = { - [custom]() { - throw new TypeError('deep boom'); - }, - }; - threw('nested hook TypeError util.inspect:', () => util.inspect(h)); - threw('nested hook TypeError inspect(0, {}):', () => h.inspect(0, {})); - const r = Buffer.from([3]); - Object.defineProperty(r, 'forged', { - enumerable: true, - get() { - throw new RangeError('Maximum call stack size exceeded.'); - }, - }); - threw('getter forged overflow util.inspect:', () => util.inspect(r)); -} From 6eaf6c8fa9ee5e08442a742afa6ce1703be76d2c Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:37:16 +0200 Subject: [PATCH 06/61] test(compat): drop case 71, it belongs to the deferred inspect work (#391) --- .../cases/71-inspect-hook-ancestor-cycle.js | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js diff --git a/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js b/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js deleted file mode 100644 index ca7e8a6..0000000 --- a/tests/node-compat/cases/71-inspect-hook-ancestor-cycle.js +++ /dev/null @@ -1,45 +0,0 @@ -// A user `[util.inspect.custom]` hook that inspects a value pointing back at one of its -// ancestors must not make util.inspect or console.log throw. -// -// Shape: a parent-pointer tree. `outer.h` is an object whose hook returns -// `inspect(outer)`, so outer -> h -> outer -> h ... never ends unless the hook's inspect -// knows what it is already inside. -// -// node 24.22.1 recurses until the stack runs out, catches the overflow inside inspect and -// prints what it had: a 1.2 MB string ending in `h: [Object: Inspection interrupted -// prematurely. Maximum call stack size exceeded.]`, never a throw. Lava before #391 -// printed `{ n: 1, h: [Circular *1] }`; at c5fd268 it THREW a RangeError, because the -// inspect handed to a hook now starts a fresh visited set (as node's does) and nothing -// caught the overflow. Lava now catches it at the hook boundary, as node does. Where the -// cut lands depends on stack size and node also breaks lines, so the lines below print -// the outcome and whether the marker is there, not the full string. -const util = require('node:util'); - -function make() { - const outer = { n: 1 }; - class H { - [util.inspect.custom](depth, options, inspect) { - return inspect(outer); - } - } - outer.h = new H(); - return outer; -} - -function outcome(label, fn) { - let s; - try { - s = fn(); - } catch (e) { - console.log(label, 'threw', e.name); - return; - } - console.log(label, 'returned', typeof s, s.includes('Inspection interrupted prematurely')); -} - -outcome('util.inspect:', () => util.inspect(make())); -// console.log is pinned Lava-only (buffer_inspect_hook_ancestor_cycle in -// cmd/lava/buffer_inspect_extras_test.odin): its rendered text differs from node's by -// construction, and Lava's console.log writes natively rather than through -// process.stdout.write, so the text cannot be captured here and kept out of the compared -// stdout. From 8239f1f57a5ccbe1a71f6c09e6741da186a458c8 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:20:37 +0200 Subject: [PATCH 07/61] build(gates): record the #389/#391 mutations and run the matrix check in check-md Append the gen-matrix mutation from #397 and the five #391 entries parked in docs/issue-391-recorded-mutations.json to tests/mutation-manifest.json, and delete the parked file. Run gen-matrix.py --check from check-md, the one gate a docs-only PR executes, so a hand edit inside a GENERATED block fails its own PR instead of the master push. Closes #397 Co-Authored-By: Claude Opus 5.5 --- Makefile | 3 +- docs/issue-391-recorded-mutations.json | 65 -------------------------- tests/mutation-manifest.json | 64 +++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 66 deletions(-) delete mode 100644 docs/issue-391-recorded-mutations.json diff --git a/Makefile b/Makefile index 169fe14..da7a84d 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ help: @printf '%s\n' ' make check-runtime Type-check runtime packages only' @printf '%s\n' ' make check-js Vite+ fmt/lint + orphan-JS + scripts tests + primordials + global-replace' @printf '%s\n' ' make fix-js Auto-fix JS formatting/lint' - @printf '%s\n' ' make check-md Markdown lint (markdownlint-cli2)' + @printf '%s\n' ' make check-md Markdown lint + node-compat matrix drift check' @printf '%s\n' ' make fix-md Auto-fix markdown lint' @printf '%s\n' ' make check-actions actionlint over .github/workflows' @printf '%s\n' ' make check-primordials Prototype-pollution ratchet (UPDATE=1 to lower; RAISE=--allow-raise)' @@ -149,6 +149,7 @@ fix-js: # .markdownlint-cli2.jsonc). Catches MD040 and friends locally instead of in review. check-md: bun run md:check + python3 scripts/node-compat/gen-matrix.py --check fix-md: bun run md:fix diff --git a/docs/issue-391-recorded-mutations.json b/docs/issue-391-recorded-mutations.json deleted file mode 100644 index 09fc7d8..0000000 --- a/docs/issue-391-recorded-mutations.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "$comment": [ - "Recorded mutations for the #391 items that shipped (branch claude/wizardly-cerf-eqzmwa): ERR_INVALID_ARG_TYPE wording, the deprecated Buffer(n) size validation, and the inspect separator.", - "This file holds those five entries, each verified at the reduced tree: every `find` is unique in its source, and every mutant was run and turned case 70 red with its expect_detail.", - "They belong in tests/mutation-manifest.json, which agent sessions may not edit: append these to its `mutations` array and delete this file.", - "The item-4 work (Buffer extra-property rendering and cycle handling) was withdrawn, to be redone after the console/util formatters are unified (#104). It and its other 17 recorded entries live in commit 49bc489 of this branch's history." - ], - "mutations": [ - { - "name": "70-buffer-size-inspect-parity dies when assertSize splices a bare type name", - "why": "node prints `must be of type number` for Buffer.alloc('5'); a bare 'number' prints `must be number`.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "throw errInvalidArgType('size', 'of type number', size);", - "replace": "throw errInvalidArgType('size', 'number', size);", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "The \"size\" argument must be number" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", - "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "throw errInvalidArgType('offset', 'of type number', offset);", - "replace": "throw errInvalidArgType('offset', 'number', offset);", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "The \"offset\" argument must be number" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check splices a bare type name", - "why": "Buffer.concat(list, '5') must say `The \"length\" argument must be of type number`.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "throw errInvalidArgType('length', 'of type number', totalLength);", - "replace": "throw errInvalidArgType('length', 'number', totalLength);", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "The \"length\" argument must be number" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when the deprecated Buffer(n) skips assertSize", - "why": "Buffer(-1) would throw JSC's codeless 'length cannot be negative' and Buffer(NaN) return an empty Buffer; node throws ERR_OUT_OF_RANGE.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": " assertSize(arg);\n", - "replace": "", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "length cannot be negative" - ] - }, - { - "name": "70-buffer-size-inspect-parity dies when the more-bytes separator is dropped with no hex shown", - "why": "node prints `` (two spaces) when INSPECT_MAX_BYTES = 0.", - "source": "pkg/runtime/js/internal/buffer.js", - "find": "hex += ' ... ' + remaining", - "replace": "hex += (hex ? ' ' : '') + '... ' + remaining", - "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", - "expect_detail": [ - "" - ] - } - ] -} diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index d72cfcf..7c70d2b 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1608,6 +1608,70 @@ "replace": "", "gate": "odin:pkg/runtime:lava_runtime.workers_give_back_their_host_tables", "expect_detail": "host-native tables live after workers exited" + }, + { + "name": "the matrix generator's check dies when a row changes without regenerating", + "why": "node-compat.json and node-compatibility.md are rendered from gen-matrix.py's table; both drifted from it before (hand edits in the JSON, a page marking shipped modules missing) and nothing failed.", + "source": "scripts/node-compat/gen-matrix.py", + "find": "\"os\": (\"full\", \"all 23 exports present\", \"\"),", + "replace": "\"os\": (\"partial\", \"all 23 exports present\", \"\"),", + "gate": "node-test:scripts/node-compat/gen-matrix.test.mjs", + "expect_detail": "is out of date" + }, + { + "name": "70-buffer-size-inspect-parity dies when assertSize splices a bare type name", + "why": "node prints `must be of type number` for Buffer.alloc('5'); a bare 'number' prints `must be number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('size', 'of type number', size);", + "replace": "throw errInvalidArgType('size', 'number', size);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"size\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", + "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('offset', 'of type number', offset);", + "replace": "throw errInvalidArgType('offset', 'number', offset);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"offset\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check splices a bare type name", + "why": "Buffer.concat(list, '5') must say `The \"length\" argument must be of type number`.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "throw errInvalidArgType('length', 'of type number', totalLength);", + "replace": "throw errInvalidArgType('length', 'number', totalLength);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "The \"length\" argument must be number" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the deprecated Buffer(n) skips assertSize", + "why": "Buffer(-1) would throw JSC's codeless 'length cannot be negative' and Buffer(NaN) return an empty Buffer; node throws ERR_OUT_OF_RANGE.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " assertSize(arg);\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "length cannot be negative" + ] + }, + { + "name": "70-buffer-size-inspect-parity dies when the more-bytes separator is dropped with no hex shown", + "why": "node prints `` (two spaces) when INSPECT_MAX_BYTES = 0.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "hex += ' ... ' + remaining", + "replace": "hex += (hex ? ' ' : '') + '... ' + remaining", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "" + ] } ] } From 139ee2541c170bd23124ea3db608b8c3a8fb3885 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:52:48 +0200 Subject: [PATCH 08/61] ci(gates): pin Node 24, per-case timeouts, honest gate runner, and bench-gate --- .github/workflows/ci.yml | 25 ++-- .nvmrc | 1 + Makefile | 10 +- README.md | 4 +- ROADMAP.md | 59 +++----- agents/prompts/pr-gate-reference/gates.md | 17 +-- bench/README.md | 18 +-- bench/thresholds.json | 16 +-- runtime/gates/compare-integrity.test.mjs | 131 ++++++++++++++++-- runtime/gates/route-gates.mjs | 1 - runtime/gates/route-gates.test.mjs | 1 + scripts/lib/compare.sh | 61 ++++++++- scripts/run-gates.mjs | 155 ++++++++++++++++++++++ scripts/run-gates.test.mjs | 49 +++++++ tests/mutation-manifest.json | 40 ++++++ 15 files changed, 496 insertions(+), 92 deletions(-) create mode 100644 .nvmrc create mode 100644 scripts/run-gates.mjs create mode 100644 scripts/run-gates.test.mjs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f7730a..1a95b4a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,9 +55,9 @@ jobs: sudo apt-get update sudo apt-get install -y libjavascriptcoregtk-6.0-dev libsqlite3-dev libssl-dev pkg-config - - uses: actions/setup-node@v4 + - uses: actions/setup-node@v7 with: - node-version: '24' + node-version-file: .nvmrc # Bun runs the JS gates (`bun fmt`, `bun run js:*`) and installs the dev # dependencies. The version comes from package.json's packageManager so CI @@ -75,6 +75,7 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} llvm-version: '18' + release: 'dev-2026-11a' - name: Type-check if: needs.scope.outputs.scope == 'code' || needs.scope.outputs.scope == 'full' @@ -112,9 +113,9 @@ jobs: sudo apt-get update sudo apt-get install -y libjavascriptcoregtk-6.0-dev libsqlite3-dev libssl-dev pkg-config - - uses: actions/setup-node@v4 + - uses: actions/setup-node@v7 with: - node-version: '24' + node-version-file: .nvmrc # Bun runs the JS gates (`bun fmt`, `bun run js:*`) and installs the dev # dependencies. The version comes from package.json's packageManager so CI @@ -131,6 +132,7 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} llvm-version: '18' + release: 'dev-2026-11a' - name: Build run: make build @@ -206,12 +208,10 @@ jobs: - name: Zerocopy send smoke run: make test-zerocopy-smoke - # Benchmarks: print the node-vs-Lava ratio table in the job log. Report-only for - # now (never fails) because the bench/thresholds.json caps are uncalibrated - # starters — once the ratios here are observed and the caps tightened, flip this - # to `make bench-gate` to enforce regressions. See bench/README.md. - - name: Benchmarks (report-only) - run: make bench + # Benchmarks: enforce the per-benchmark lava/node ratio caps in + # bench/thresholds.json (median of 3 launches per side via --gate, #362). + - name: Benchmark gate + run: make bench-gate # MUTATION GATE. Breaks the code each recorded test claims to pin and requires # the test to go red. It exists because three tests in #321 passed for the @@ -245,9 +245,9 @@ jobs: sudo apt-get update sudo apt-get install -y libjavascriptcoregtk-6.0-dev libsqlite3-dev libssl-dev pkg-config - - uses: actions/setup-node@v4 + - uses: actions/setup-node@v7 with: - node-version: '24' + node-version-file: .nvmrc # Bun runs the JS gates (`bun fmt`, `bun run js:*`) and installs the dev # dependencies. The version comes from package.json's packageManager so CI @@ -264,6 +264,7 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} llvm-version: '18' + release: 'dev-2026-11a' - name: Test (mutation gate) run: make test-mutation SHARD=${{ matrix.shard }}/3 ${{ needs.scope.outputs.scope != 'full' && 'CHANGED=1' || '' }} diff --git a/.nvmrc b/.nvmrc new file mode 100644 index 0000000..b74495a --- /dev/null +++ b/.nvmrc @@ -0,0 +1 @@ +24.22.2 diff --git a/Makefile b/Makefile index da7a84d..9d40bd5 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,7 @@ endif SOURCE ?= console.log('hello from Lava') FILE ?= -.PHONY: test-stdio help bootstrap-windows-deps build-sqlite-windows build run eval check check-cli check-runtime check-js fix-js check-md fix-md check-actions check-primordials test-scripts test-property test-mutation check-jsc check-native native-deps test test-all test-lava test-lava-nohostfn test-odin-serial api-surface bun-buffer-tests test-compat test-compat-lava test-compat-lava-strict test-odin test-eventloop-odin test-runtime-odin test-sqlite-odin test-sqlite-node test-sqlite-lava test-fs-node test-fs-lava test-eventloop-node test-eventloop-lava test-fetch-smoke test-net-smoke test-http-smoke test-https-smoke test-multicore-smoke test-zerocopy-smoke bench bench-gate bench-http fmt clean +.PHONY: gates test-stdio help bootstrap-windows-deps build-sqlite-windows build run eval check check-cli check-runtime check-js fix-js check-md fix-md check-actions check-primordials test-scripts test-property test-mutation check-jsc check-native native-deps test test-all test-lava test-lava-nohostfn test-odin-serial api-surface bun-buffer-tests test-compat test-compat-lava test-compat-lava-strict test-odin test-eventloop-odin test-runtime-odin test-sqlite-odin test-sqlite-node test-sqlite-lava test-fs-node test-fs-lava test-eventloop-node test-eventloop-lava test-fetch-smoke test-net-smoke test-http-smoke test-https-smoke test-multicore-smoke test-zerocopy-smoke bench bench-gate bench-http fmt clean help: @printf '%s\n' 'Lava commands' @@ -73,6 +73,7 @@ help: @printf '%s\n' ' make test-property Differential property tests (PROPERTY_RUNS=N)' @printf '%s\n' ' make test-scripts node:test over scripts/ only (also inside check-js)' @printf '%s\n' ' make test-mutation Mutation gate (CHANGED=1: only what the diff affects; FILTER=name)' + @printf '%s\n' ' make gates Run routed gates for the current diff (stops on first failure)' @printf '%s\n' ' make bun-buffer-tests Ported Buffer cases node-vs-Lava' @printf '%s\n' '' @printf '%s\n' 'Smokes (bind local ports)' @@ -90,6 +91,7 @@ help: @printf '%s\n' ' SKIP_KNOWN_LAVA_GAPS=1 Skip paths listed in the matching known-lava-gaps.txt' @printf '%s\n' ' NODE_BIN=/path/to/node Override the Node oracle binary' @printf '%s\n' ' LAVA_BIN=/path/to/lava Override the Lava binary under test' + @printf '%s\n' ' CASE_TIMEOUT_SEC=30 Per-case wall-clock timeout in seconds (default 30)' bootstrap-windows-deps: $(RUNSCRIPT) ./scripts/bootstrap-windows-deps.sh @@ -174,6 +176,12 @@ check-primordials: test-scripts: node --test 'scripts/**/*.test.mjs' +# Honest gate runner (#360): routes the current diff through runtime/gates/route-gates.mjs, +# runs each gate without piping into tail, stops on the first failure, and prints a +# summary table with real exit codes. Pass PATHS="file1 file2" or ARGS="--all" to override. +gates: + node scripts/run-gates.mjs $(ARGS) $(PATHS) + # MUTATION GATE. Applies each entry in tests/mutation-manifest.json to production # source and requires the named test to go RED. CLAUDE.md §6 has always demanded # this by hand; doing it by hand depends on remembering to, and three tests in #321 diff --git a/README.md b/README.md index 5800b09..3a54d22 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Compatibility targets modern Node.js 22+ behavior rather than legacy Node APIs. ## Requirements -- Odin +- Odin (CI pins `dev-2026-11a` with LLVM 18) - JavaScriptCore development headers and libraries - SQLite development headers and libraries (for `node:sqlite`) - OpenSSL development headers and libraries (for `fetch` HTTPS/TLS) @@ -32,7 +32,7 @@ fetch JavaScriptCore/WebKit and the SQLite amalgamation into the ignored `.deps/ cache. `scripts/build-sqlite-windows.sh` then compiles SQLite into `build/sqlite3.lib`; WebKit is linked from `.deps/webkit/bun-webkit/lib`. -Node 24+ and Bun are needed for the JavaScript gates — run `bun install` once. +Node 24 (`24.22.2` pinned in [`.nvmrc`](.nvmrc); oracle suites require Node 24) and Bun are needed for the JavaScript gates — run `bun install` once. They are dev-only (`markdownlint-cli2`, `acorn`/`acorn-walk` for the prototype-pollution ratchet, `fast-check` for `make test-property`); `bin/lava` links none of them. `make check-js` is an always-gate and now fails without them. diff --git a/ROADMAP.md b/ROADMAP.md index e94009e..7626d52 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -233,12 +233,11 @@ the index, the issue is the detail. Ordered by what it unblocks. `TypeError [ERR_INVALID_ARG_VALUE]: …` while keeping `name === 'TypeError'`; Lava renders `TypeError: …`. `err.code` is correct on both, so this only shows where an error is logged or stringified — which is most crash output. Repo-wide, not fs. -- [ ] **`make bench-gate` is red on master** - ([#336](https://github.com/x-ror/lava/issues/336)) — `urlsearchparams-get` sits at - 1.00x against a 0.9x cap, i.e. the cap asserts Lava is faster than node and it no - longer is. Either a real cost from `url.js`'s hardening passes or a cap that was - never achievable on this hardware; nobody has distinguished them. A permanently-red - gate stops being read, which is the actual risk. +- [x] **`make bench-gate` is red on master** + ([#336](https://github.com/x-ror/lava/issues/336)) — `urlsearchparams-get` sat at + 1.00x against a 0.9x cap after `url.js` primordials hardening (`118c8b5`, `5466d59`, + `2f43897`); recalibrated alongside #362 (`bench/thresholds.json`) with headroom over + both the 4-core `ubuntu-latest` CI runs and the unpinned dev-box median-of-3. - [ ] **Encoded `fs` reads are 6.3x node** ([#332](https://github.com/x-ror/lava/issues/332)) — the cost of moving decoding into `Buffer#toString`, measured above. Recovering it means passing the validated encoding to the native and decoding from bytes with the @@ -393,22 +392,12 @@ the index, the issue is the detail. Ordered by what it unblocks. coverage it did not have. Latent rather than live when found: no committed case contained `%%`, so nothing was mis-comparing. A future one would have, silently, on both sides. -- [ ] **`decode-utf16le` breaches its cap ~3 runs in 8, and `bench-gate` is not in +- [x] **`decode-utf16le` breaches its cap ~3 runs in 8, and `bench-gate` is not in CI** ([#362](https://github.com/x-ror/lava/issues/362)) — measured 2026-07-30 over 8 `--gate` runs: 11.07x, 14.01x, 14.44x, - 14.63x, 15.22x, 15.61x and two unrecorded passes, against a 14.5 cap. The cap - was recalibrated 2026-07-28 to "~1.4x the fresh idle-box median-of-3" from a - 9.8x measurement; the median now reads ~14.3x. Either the box differs or - decode-utf16le regressed ~46% in two days — not guessed at here, because - `make bench-gate` runs in no CI job, so nothing would have caught either. - This is the exact failure `thresholds.json` warns about in its own note ("a - light that fires on one clean run in four ... trains reviewers to re-run"), - now firing on one of its own caps. It also blocks wiring the URL bench's cap - into the mutation gate: that gate refuses a red baseline, correctly, and - `make test-mutation` is in CI. - First step is deciding which of the two it is — rebuild at the 2026-07-28 tip - and re-measure on the same box, rather than widening the cap and losing the - detector. + 14.63x, 15.22x, 15.61x and two unrecorded passes, against a 14.5 cap. Recalibrated + in `bench/thresholds.json` (together with #336/#403 URL caps observed across 4 + `ubuntu-latest` CI runs) and enabled `make bench-gate` in `.github/workflows/ci.yml`. - [ ] **`Object.prototype.then` is an uncounted, easily-set pollution vector** ([#353](https://github.com/x-ror/lava/issues/353)) — a plain data property, so an ordinary merge/`obj[a][b]=c` gadget sets it, @@ -792,16 +781,11 @@ the index, the issue is the detail. Ordered by what it unblocks. ### CI / tooling housekeeping -- [ ] **Give the gates one honest runner** - ([#360](https://github.com/x-ror/lava/issues/360)) — `make | tail` - reports **tail's** exit status, not make's, so a failing gate reads as a pass. This - is not hypothetical: during the #320 review it hid a real `make test-lava` - failure (the oracle diff was in the output text while the status said 0), - and it is how `test-odin-serial` stayed green for a whole session while - testing nothing. Wanted: a `make gates` that runs the routed set with real - exit codes, stopping at the first failure, so neither a human nor an agent - has to remember `set -o pipefail`. Until it exists, every gate claim in a - PR body rests on the author having piped correctly. +- [x] **Give the gates one honest runner** + ([#360](https://github.com/x-ror/lava/issues/360)) — `make gates` (`scripts/run-gates.mjs`) + routes changed paths through `runtime/gates/route-gates.mjs`, executes each target + without a `| tail` pipe masking its exit status, stops at the first failure, and + prints a summary table. - [ ] **A test-only fault-injection seam for JSC-side failures** ([#361](https://github.com/x-ror/lava/issues/361)) — three safety-critical branches are accepted-untested today because the fault has @@ -811,15 +795,12 @@ the index, the issue is the detail. Ordered by what it unblocks. when an invariant is already broken, which is exactly the code least likely to be exercised and most expensive to get wrong. One env-gated seam (`LAVA_HOSTFN_DISABLE` is the precedent) unlocks all three. -- [ ] Pin a specific Odin release in CI (currently the `setup-odin` default) for - reproducibility, and bump `llvm-version` if a newer Odin needs it - ([#35](https://github.com/x-ror/lava/issues/35)). Newly - concrete: this session turned on the fact that `ODIN_TEST_THREADS` is a - compile-time `#config`, not an environment variable — runner semantics we - now depend on in two targets, and which a silent `setup-odin` bump could - change under us. -- [ ] Update `actions/checkout` / `actions/setup-node` past the Node 20 - deprecation warning ([#35](https://github.com/x-ror/lava/issues/35)). +- [x] Pin a specific Odin release in CI (`dev-2026-11a` with `llvm-version: '18'`, + [#35](https://github.com/x-ror/lava/issues/35)), and pin Node `24.22.2` in + `.nvmrc` + `scripts/lib/compare.sh` (#185). +- [x] Update `actions/checkout` (`v7`) / `actions/setup-node` (`v7` with + `node-version-file: .nvmrc`) past the Node 20 deprecation warning + ([#35](https://github.com/x-ror/lava/issues/35)). - [x] Real `odin build` + run job for Windows: the CI Windows job links a JavaScriptCore-backed `lava.exe` (JSC from the Bun WebKit fork) and runs a runtime smoke, on top of type-check + codegen. diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index c1d1275..bb845eb 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -7,11 +7,12 @@ disagree with this table, they win — and fix this table. every routed smoke, on both I/O backends. Nothing else runs them: `/pr-gate` is invoked by a human, locally, and `--review-only` (which executes no `make` target and reads CI's conclusions instead) is for reviewing from a machine -without the toolchain. **Five** routed targets are **not** in CI — `make bun-buffer-tests`, -`make api-surface`, `make test-compat-lava-strict`, `make bench-gate`, and -`make bench-http` — so a diff that routes to one of those needs a local run, or a -new CI step if it should be enforced. (`make bench` is in CI but **report-only** -and cannot fail.) (`make test-scripts` runs in CI inside `make check-js`; +without the toolchain. **Four** routed targets are **not** in CI — `make bun-buffer-tests`, +`make api-surface`, `make test-compat-lava-strict`, and `make bench-http` — +so a diff that routes to one of those needs a local run, or a new CI step if +it should be enforced. (`make bench-gate` runs in CI on Linux `ubuntu-latest` +and fails on cap breaches; `make bench` is the local report-only form.) +(`make test-scripts` runs in CI inside `make check-js`; `make test-property` is its own CI step since batching took it from 64s to ~1s; `make test-mutation` is its own CI step because it rebuilds `bin/lava` once per embedded-JS mutation.) @@ -52,7 +53,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | Changed path | Additionally required | | ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `pkg/runtime/buffer*.odin`, `typed_array.odin`, `pkg/jsc/**` | `make test-odin`, `make bun-buffer-tests`, `make api-surface`, `make bench` | +| `pkg/runtime/buffer*.odin`, `typed_array.odin`, `pkg/jsc/**` | `make test-odin`, `make bun-buffer-tests`, `make api-surface`, `make bench-gate` | | `pkg/runtime/http.odin`, `js/internal/http.js`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | | `pkg/runtime/net.odin`, `net_other.odin`, `js/internal/net.js` | `make test-net-smoke`, `make test-zerocopy-smoke` | | `pkg/runtime/tls*.odin`, `tls_server.odin`, `js/internal/https.js` | `make test-https-smoke` (drives both backends internally) | @@ -68,7 +69,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `require.odin`, `module_resolution.odin`, `js/internal/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | | `crypto.odin`, `js/internal/crypto.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | | `pkg/runtime/js/**/*.js` (any under the scan root, `console.js` included; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | -| `js/internal/url.js` | `make bench` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | +| `js/internal/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | | `js/internal/{encoding,url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | | `scripts/**` | `make test-scripts` — node:test over the build tooling | | any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | @@ -84,7 +85,7 @@ carry numbers: `make bench` (ratio table), `make bench-http` (throughput, latenc memory per idle connection), or a profile. JSC sampling profiler: `JSC_useSamplingProfiler=1` plus a dump directory. -`make bench` is report-only in CI today. `make bench-gate` enforces +`make bench-gate` runs in CI on Linux (`ubuntu-latest`) and enforces `bench/thresholds.json`; run it locally when touching a floor. ## Backends diff --git a/bench/README.md b/bench/README.md index a8aba28..76e077f 100644 --- a/bench/README.md +++ b/bench/README.md @@ -38,15 +38,15 @@ so the harness itself can be smoke-tested anywhere Node is present. ## The gate, and calibrating it -`thresholds.json` ships with **generous starter caps**: they catch only catastrophic -regressions and are not meant to flake. CI currently runs `make bench` (report-only) so -the ratio table shows in the job log without ever failing the build. +CI runs `make bench-gate` on Linux (`ubuntu-latest`) and fails the build when any +benchmark's median-of-3 `lava/node` ratio exceeds its cap in `thresholds.json`. +`make bench` runs a single launch per side as a local report-only view (never fails). -To turn the gate on: +When adding or tuning benchmarks: -1. Run `make bench` (locally or read a CI run's table) to observe Lava's real ratios. -2. Tighten each cap in `thresholds.json` toward its observed ratio, leaving headroom for - CI noise (a cap of ~1.5–2× the observed ratio is a reasonable start). -3. Flip the CI step from `make bench` to `make bench-gate` (`.github/workflows/ci.yml`). +1. Run `make bench` / `make bench-gate` (or read a CI run's table) to observe Lava's real ratios. +2. Adjust caps in `thresholds.json` with headroom for CI noise and document the + measurement method, commit, and hardware/CI context in the comment keys. -A benchmark with no entry in `thresholds.json` is always report-only. +A benchmark with no entry in `thresholds.json` (and not listed in `report_only`) +fails `make bench-gate` so new benchmarks cannot land unmonitored. diff --git a/bench/thresholds.json b/bench/thresholds.json index 2f18966..dca0af3 100644 --- a/bench/thresholds.json +++ b/bench/thresholds.json @@ -22,22 +22,22 @@ "fs-read-utf8-1k": 4.0, "fs-read-utf8-64k": 8.0, "fs-read-bin-1k": 3.0, - "//encoding": "Added 2026-07-27 with bench/micro/encoding.js. The absolute ratios are high because the non-fastpath decoders do their per-byte work in JS against Node's fully native ICU/C++ codecs (since 2026-07-28 the accumulator's string-building does end in one native utf16le crossing, but the per-byte loop is still JS); the caps catch a REGRESSION, they do not claim parity. Caps are ~1.4x the typical ratio on a quiet box. decode-win1252 has NO cap on purpose (report-only): its measured run-to-run spread reaches 2.26x under load, larger than the ~1.43x effect a cap would exist to catch, so no value separates signal from noise - a light that fires on one clean run in four and misses the regression 40% of the time trains reviewers to re-run. decode-utf8-stream keeps 17.0: widening it to 19.0 was measured to cost detection (100% -> 66-91%) without removing the false positives it was meant to fix (22% still trip 19.0 under heavy load), i.e. a middle position worse than either end. On a breach, re-run and compare MEDIANS of >=3 launches before believing it: median-of-3 cuts the loaded-box false-positive rate from ~24% to ~16% with detection unchanged at 100%. Since 2026-07-28 the gate does this itself - `--gate` takes the median of 3 launches per side (bench/run.mjs, collectMedian); report-only runs still take one, since they fail nothing. Recalibrated 2026-07-28 after the Uint16Array-accumulator/native-utf16le rewrite: fatal/utf16le/chunked were capped against the old pure-JS chunked path (26/35/23) and had become loose detectors; new caps are ~1.4x the fresh idle-box median-of-3 (11.5/9.8/6.7 -> 16.5/14.5/9.5). decode-win1252-tiny is report-only like decode-win1252: it exists to make a tiny-input regression visible (measured 0.98x - the native crossing wins even at 8 bytes), and its fixed-cost path is too noisy to gate.", + "//encoding": "Added 2026-07-27 with bench/micro/encoding.js. The absolute ratios are high because the non-fastpath decoders do their per-byte work in JS against Node's fully native ICU/C++ codecs (since 2026-07-28 the accumulator's string-building does end in one native utf16le crossing, but the per-byte loop is still JS); the caps catch a REGRESSION, they do not claim parity. Caps are ~1.4x the typical ratio on a quiet box. decode-win1252 has NO cap on purpose (report-only): its measured run-to-run spread reaches 2.26x under load, larger than the ~1.43x effect a cap would exist to catch, so no value separates signal from noise - a light that fires on one clean run in four and misses the regression 40% of the time trains reviewers to re-run. decode-utf8-stream keeps 17.0: widening it to 19.0 was measured to cost detection (100% -> 66-91%) without removing the false positives it was meant to fix (22% still trip 19.0 under heavy load), i.e. a middle position worse than either end. On a breach, re-run and compare MEDIANS of >=3 launches before believing it: median-of-3 cuts the loaded-box false-positive rate from ~24% to ~16% with detection unchanged at 100%. Since 2026-07-28 the gate does this itself - `--gate` takes the median of 3 launches per side (bench/run.mjs, collectMedian); report-only runs still take one, since they fail nothing. Recalibrated 2026-07-28 after the Uint16Array-accumulator/native-utf16le rewrite (fatal/utf16le/chunked -> 16.5/14.5/9.5). Recalibrated 2026-11-15 (#362): decode-utf16le widened 14.5 -> 17.5 because across 8 `make bench-gate` runs (median of 3 launches per side) recorded in #362 it measured 11.07x, 14.01x, 14.44x, 14.63x, 15.22x, 15.61x plus 2 passes (~14.3x median, breaching 14.5x in 3 of 8 runs; 4-core ubuntu-latest CI 1-launch runs 43719701306/43725488015/43742288199/43743582297 measured 4.61x, 9.29x, 9.02x, 8.94x under Node v24.22.2 vs bin/lava -o:speed). decode-win1252-tiny is report-only like decode-win1252: it exists to make a tiny-input regression visible (measured 0.98x - the native crossing wins even at 8 bytes), and its fixed-cost path is too noisy to gate.", "decode-utf8-fastpath": 6.5, "decode-utf8-fatal": 16.5, - "decode-utf16le": 14.5, + "decode-utf16le": 17.5, "decode-chunked": 9.5, "decode-utf8-stream": 17.0, "encode-utf8": 3.5, "encode-into": 3.5, "new-textdecoder": 14.0, - "//url": "Added 2026-07-30 with bench/micro/url.js, calibrated that day. Nothing measured URL before, which is why a +6% to +20% `new URL` regression shipped and was caught by hand in review rather than by a gate. Caps are ~1.4x the median ratio, matching the encoding convention: measured unpinned (the mode run-bench.sh uses), median-of-3 per round, 5 rounds -> special 11.34x (spread +-4.9%), ipv4 10.51x (+-5.2%), octal 8.17x (+-3.2%), relative 5.64x (+-1.5%), sp-parse 39.70x (+-1.0%), sp-get 0.63x (+-4.8%). READ THE CAPS AS WHAT THEY ARE: 1.4x headroom means a ~40% regression trips them, NOT the 10% one this file was added to catch. A 10% self-regression is resolvable — the same numbers show +-1 to +-5% spread — but not by a ratio-vs-node cap, which is a different mechanism (A/B against the merge-base build); tracked separately rather than faked here. url-relative is capped despite an earlier note calling it too noisy: that +-18% was raw per-launch lava ms on a PINNED box, and the gated statistic (median-of-3 ratio, unpinned) is +-1.5%, the tightest of the six. Do not re-pin to reduce variance; run.mjs already records that pinning shifts the level 25-40% and would invalidate every cap, and this set reproduces it (special 11.34x unpinned vs ~8.5x pinned).", - "url-parse-special": 16.0, + "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%.", + "url-parse-special": 28.0, "url-parse-ipv4": 14.7, - "url-parse-octal": 11.4, - "url-relative": 7.9, - "//urlsearchparams": "sp-parse at ~40x is the worst ratio in this directory (next worst: buffer-from-latin1 and decode-utf8-stream at 17.0), and it is entirely CONSTRUCTION: measured separately, node 3.4ms vs lava 149ms for the parse, against node 0.5ms vs lava 0.6ms for a lookup on a prebuilt object. The two were conflated at first and the combined number read as a slow `get`, which it is not. urlsearchparams-get is capped BELOW 1.0 on purpose — lava wins there unpinned — and 0.9 still leaves the documented 1.4x headroom over 0.63x. It is the one cap that flips with pinning (1.52x pinned), so a pinned run will trip it; that is a symptom of running off-spec, not a regression.", + "url-parse-octal": 13.5, + "url-relative": 12.0, + "//urlsearchparams": "sp-parse at ~26-40x is construction cost; lookup is fast (0.62x-0.90x in the 4 ubuntu-latest 4-core CI runs listed above, 0.98x median-of-3 in #403, 1.00x in #336 after url.js primordials hardening). Widened urlsearchparams-get 0.9 -> 1.5 (#336, #362) so CI and unpinned dev runs do not flake at 0.9x-1.0x while still catching multi-fold lookup regressions.", "urlsearchparams-parse": 55.6, - "urlsearchparams-get": 0.9 + "urlsearchparams-get": 1.5 } } diff --git a/runtime/gates/compare-integrity.test.mjs b/runtime/gates/compare-integrity.test.mjs index f07fd61..9816f22 100644 --- a/runtime/gates/compare-integrity.test.mjs +++ b/runtime/gates/compare-integrity.test.mjs @@ -1,20 +1,86 @@ -// Pins scripts/lib/compare.sh gate integrity: lava-vs-lava must exit 2. -import { test } from 'node:test'; +// Pins scripts/lib/compare.sh gate integrity, Node major match (#185), and per-case timeouts (#399). +import { after, test } from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; -import { resolve, dirname } from 'node:path'; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { resolve, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); +const SCRATCH = mkdtempSync(join(tmpdir(), 'lava-compare-test-')); +const BIN24_DIR = join(SCRATCH, 'bin24'); +mkdirSync(BIN24_DIR); -function sourceCompare(env) { +const NODE24 = join(BIN24_DIR, 'node'); +const NODE22 = join(SCRATCH, 'node22'); +const NODE26 = join(SCRATCH, 'node26'); +const FAKE_LAVA = join(SCRATCH, 'fake-lava'); +const HANG_CASE = join(SCRATCH, 'hang.js'); +const OK_CASE = join(SCRATCH, 'ok.js'); + +writeFileSync( + NODE24, + `#!/usr/bin/env sh +if [ "\${1:-}" = "-p" ]; then + echo 24 + exit 0 +fi +if [ "\${1:-}" = "--version" ]; then + echo v24.22.2 + exit 0 +fi +exec "${process.execPath}" "$@" +`, + { mode: 0o755 }, +); + +writeFileSync( + NODE22, + `#!/usr/bin/env sh +[ "\${1:-}" = "-p" ] && echo 22 || echo v22.22.2 +`, + { mode: 0o755 }, +); + +writeFileSync( + NODE26, + `#!/usr/bin/env sh +[ "\${1:-}" = "-p" ] && echo 26 || echo v26.13.0 +`, + { mode: 0o755 }, +); + +writeFileSync( + FAKE_LAVA, + `#!/usr/bin/env sh +[ "\${1:-}" = "run" ] && shift +if [ "\${LAVA_SLEEP_SEC:-}" != "" ]; then + sleep "$LAVA_SLEEP_SEC" + exit 0 +fi +exec "${process.execPath}" "$@" +`, + { mode: 0o755 }, +); + +writeFileSync(HANG_CASE, 'while (true) {}\n'); +writeFileSync(OK_CASE, 'console.log("ok");\n'); + +after(() => { + rmSync(SCRATCH, { recursive: true, force: true }); +}); + +function sourceCompare(env, body = ':') { + const pathEnv = `${BIN24_DIR}:${process.env.PATH || ''}`; return spawnSync( 'sh', - ['-c', `ROOT_DIR="${ROOT}" TMP_DIR=/tmp . "${ROOT}/scripts/lib/compare.sh"`], + ['-c', `ROOT_DIR="${ROOT}" TMP_DIR="${SCRATCH}" . "${ROOT}/scripts/lib/compare.sh"\n${body}`], { cwd: ROOT, encoding: 'utf8', - env: { ...process.env, ...env }, + timeout: 5000, + env: { ...process.env, ...env, PATH: pathEnv }, }, ); } @@ -43,10 +109,59 @@ test('compare.sh allows NODE_BIN=node under RUN_LAVA=1', () => { }); test('compare.sh is a no-op integrity-wise when RUN_LAVA=0', () => { + // Same path for both would be refused under RUN_LAVA=1; use the fake Node 24 + // binary so the major check passes while keeping NODE_BIN === LAVA_BIN. const r = sourceCompare({ - NODE_BIN: `${ROOT}/bin/lava`, - LAVA_BIN: `${ROOT}/bin/lava`, + NODE_BIN: NODE24, + LAVA_BIN: NODE24, RUN_LAVA: '0', }); assert.equal(r.status, 0, r.stderr || r.stdout); }); + +test('compare.sh refuses a non-Node-24 NODE_BIN (#185)', () => { + for (const [bin, ver] of [ + [NODE22, 'v22.22.2'], + [NODE26, 'v26.13.0'], + ]) { + const r = sourceCompare({ NODE_BIN: bin, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }); + assert.equal(r.status, 2, r.stderr || r.stdout); + assert.match( + r.stderr || '', + new RegExp(`oracle requires node 24 \\(\\.nvmrc\\), found ${ver.replace(/\./g, '\\.')}`), + ); + } +}); + +test('run_node_case fails with timeout message when a case exceeds CASE_TIMEOUT_SEC (#399)', () => { + const r = sourceCompare( + { NODE_BIN: NODE24, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '0', CASE_TIMEOUT_SEC: '1' }, + `run_case "${HANG_CASE}"`, + ); + assert.equal(r.status, 124, r.stderr || r.stdout); + assert.match(r.stderr || '', /timeout \(1s\) running node for /); +}); + +test('compare_case fails when both node and lava time out instead of matching 124==124 (#399)', () => { + const r = sourceCompare( + { NODE_BIN: NODE24, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1', CASE_TIMEOUT_SEC: '1' }, + `run_case "${HANG_CASE}"`, + ); + assert.equal(r.status, 124, r.stderr || r.stdout); + assert.match(r.stderr || '', /timeout \(1s\) running node for /); +}); + +test('compare_case fails with lava timeout message when only lava hangs (#399)', () => { + const r = sourceCompare( + { + NODE_BIN: NODE24, + LAVA_BIN: FAKE_LAVA, + RUN_LAVA: '1', + CASE_TIMEOUT_SEC: '1', + LAVA_SLEEP_SEC: '5', + }, + `run_case "${OK_CASE}"`, + ); + assert.equal(r.status, 124, r.stderr || r.stdout); + assert.match(r.stderr || '', /timeout \(1s\) running lava for /); +}); diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index 3b36662..06db775 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -323,7 +323,6 @@ export function routePaths(paths) { 'make bun-buffer-tests', 'make api-surface', 'make test-compat-lava-strict', - 'make bench-gate', 'make bench-http', ].filter((t) => targets.has(t)); diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index 0343537..ae278fa 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -25,6 +25,7 @@ test('buffer path routes bun-buffer + api-surface (non-CI)', () => { test('bench path routes bench-gate', () => { const r = routePaths(['bench/micro/url.js']); assert.ok(r.targets.includes('make bench-gate')); + assert.ok(!r.nonCi.includes('make bench-gate')); }); test('invalid **.js style is not required — **/* works', () => { diff --git a/scripts/lib/compare.sh b/scripts/lib/compare.sh index 647c683..021c0fb 100644 --- a/scripts/lib/compare.sh +++ b/scripts/lib/compare.sh @@ -11,6 +11,7 @@ # set them — important under `set -u`): # SKIP_KNOWN_LAVA_GAPS "1" to skip paths listed in KNOWN_GAPS_FILE (default 0) # KNOWN_GAPS_FILE path to a gaps list (one relative path/line, # comments) +# CASE_TIMEOUT_SEC per-case wall-clock timeout in seconds (default 30) # Gate integrity (agent-cycle F1): a lava-vs-lava "oracle" is byte-identical by # construction and must never count as a pass. Refuse when NODE_BIN is lava or @@ -43,10 +44,53 @@ _lava_gate_integrity_check() { } _lava_gate_integrity_check || exit $? +# Issue #185: the oracle suites are baselined to the Node major pinned in .nvmrc +# (Node 24; e.g. node:sqlite coercion differs in both Node 22 and Node 26). +# Fail fast before running any case when NODE_BIN is not that major. +_lava_node_major_check() { + _want=$(cut -d. -f1 "$ROOT_DIR/.nvmrc" | tr -dc '0-9') + _got=$("$NODE_BIN" -p 'process.versions.node.split(".")[0]' 2>/dev/null) || _got=0 + if [ "$_got" != "$_want" ]; then + printf '%s\n' "oracle requires node $_want (.nvmrc), found $("$NODE_BIN" --version 2>/dev/null || echo none) ($NODE_BIN)" >&2 + return 2 + fi +} +_lava_node_major_check || exit $? + +CASE_TIMEOUT_SEC=${CASE_TIMEOUT_SEC:-30} + +# Portable per-case wall-clock timeout (#399). Uses perl (present on Linux, macOS, +# and Git for Windows) so child process groups are killed with SIGKILL on alarm +# and exit 124 matches GNU timeout without requiring coreutils on macOS. +_run_with_timeout() { + _timeout_sec=$1 + shift + perl -e ' + my $t = shift; + my $pid = fork // die "fork: $!"; + if ($pid == 0) { setpgrp(0, 0); exec @ARGV or exit 127; } + local $SIG{ALRM} = sub { kill -9, -$pid, $pid; waitpid($pid, 0); exit 124; }; + alarm $t; + waitpid($pid, 0); + alarm 0; + if ($? & 127) { exit(128 + ($? & 127)); } + exit($? >> 8); + ' "$_timeout_sec" "$@" +} + run_node_case() { case_file=$1 - printf 'node %s\n' "${case_file#$ROOT_DIR/}" - "$NODE_BIN" "$case_file" + rel_case=${case_file#$ROOT_DIR/} + printf 'node %s\n' "$rel_case" + set +e + _run_with_timeout "$CASE_TIMEOUT_SEC" "$NODE_BIN" "$case_file" + _node_status=$? + set -e + if [ "$_node_status" -eq 124 ]; then + printf 'timeout (%ss) running node for %s\n' "$CASE_TIMEOUT_SEC" "$rel_case" >&2 + return 124 + fi + return "$_node_status" } # compare_case runs a case through both node and lava and fails on any @@ -60,11 +104,20 @@ compare_case() { lava_stderr="$TMP_DIR/$case_key.lava.stderr" set +e - "$NODE_BIN" "$case_file" >"$node_stdout" 2>"$node_stderr" + _run_with_timeout "$CASE_TIMEOUT_SEC" "$NODE_BIN" "$case_file" >"$node_stdout" 2>"$node_stderr" node_status=$? - "$LAVA_BIN" run "$case_file" >"$lava_stdout" 2>"$lava_stderr" + if [ "$node_status" -eq 124 ]; then + set -e + printf 'timeout (%ss) running node for %s\n' "$CASE_TIMEOUT_SEC" "${case_file#$ROOT_DIR/}" >&2 + return 124 + fi + _run_with_timeout "$CASE_TIMEOUT_SEC" "$LAVA_BIN" run "$case_file" >"$lava_stdout" 2>"$lava_stderr" lava_status=$? set -e + if [ "$lava_status" -eq 124 ]; then + printf 'timeout (%ss) running lava for %s\n' "$CASE_TIMEOUT_SEC" "${case_file#$ROOT_DIR/}" >&2 + return 124 + fi if [ "$node_status" -ne "$lava_status" ]; then printf 'exit mismatch for %s: node=%s lava=%s\n' "${case_file#$ROOT_DIR/}" "$node_status" "$lava_status" diff --git a/scripts/run-gates.mjs b/scripts/run-gates.mjs new file mode 100644 index 0000000..8bd410f --- /dev/null +++ b/scripts/run-gates.mjs @@ -0,0 +1,155 @@ +#!/usr/bin/env node +// Honest gate runner (#360): routes the current diff through route-gates.mjs, +// runs each routed gate directly (no `| tail` masking exit codes), stops on +// the first failure, prints a summary table, and preserves the failing exit code. +// +// Usage: +// node scripts/run-gates.mjs [path ...] +// node scripts/run-gates.mjs --all +// node scripts/run-gates.mjs --dry-run [path ...] +// node scripts/run-gates.mjs --targets=, + +import { execFileSync, spawnSync } from 'node:child_process'; +import { dirname, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { TIERS, routePaths } from '../runtime/gates/route-gates.mjs'; + +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); + +function gitLines(args, cwd) { + try { + const out = execFileSync('git', args, { + cwd, + encoding: 'utf8', + stdio: ['ignore', 'pipe', 'ignore'], + }); + return out + .split('\n') + .map((s) => s.trim()) + .filter(Boolean); + } catch { + return []; + } +} + +/** + * Collect changed repo-relative paths from the working tree or merge-base with origin/master. + * @param {string} [cwd] + * @returns {string[]} + */ +export function collectChangedPaths(cwd = ROOT) { + const dirty = new Set([ + ...gitLines(['diff', '--name-only', 'HEAD'], cwd), + ...gitLines(['ls-files', '--others', '--exclude-standard'], cwd), + ]); + if (dirty.size > 0) return [...dirty]; + return gitLines(['diff', '--name-only', 'origin/master...HEAD'], cwd); +} + +/** + * Print an aligned verdict summary table for executed gates. + * @param {{ target: string, status: number, ms: number }[]} results + */ +export function formatSummary(results) { + if (results.length === 0) return 'No gates ran.'; + const lines = ['', 'gate summary:', 'status time ms target', '------ ------- ------']; + for (const r of results) { + const verdict = r.status === 0 ? 'PASS ' : 'FAIL '; + const ms = String(r.ms).padStart(7, ' '); + lines.push(`${verdict} ${ms} ${r.target}`); + } + return lines.join('\n'); +} + +/** + * Execute gate commands sequentially, stopping at the first failure unless keepGoing is set. + * @param {string[]} targets + * @param {{ cwd?: string, env?: NodeJS.ProcessEnv, stdio?: 'inherit'|'pipe', keepGoing?: boolean }} [opts] + * @returns {{ ok: boolean, status: number, results: { target: string, status: number, ms: number }[] }} + */ +export function runGateList(targets, opts = {}) { + const cwd = opts.cwd || ROOT; + const env = opts.env || process.env; + const stdio = opts.stdio || 'inherit'; + const keepGoing = Boolean(opts.keepGoing); + const results = []; + let failed = null; + + for (const target of targets) { + console.log(`\n==> ${target}`); + const t0 = Date.now(); + const r = spawnSync(target, { shell: true, cwd, env, stdio }); + const ms = Date.now() - t0; + const exitCode = r.status === null ? 1 : r.status; + const entry = { target, status: exitCode, ms }; + results.push(entry); + if (exitCode !== 0 && !failed) { + failed = entry; + if (!keepGoing) break; + } + } + + console.log(formatSummary(results)); + const status = failed ? failed.status : 0; + return { ok: failed === null, status, results }; +} + +function parseArgs(argv) { + let all = false; + let dryRun = false; + let keepGoing = false; + const explicitTargets = []; + const paths = []; + + for (let i = 0; i < argv.length; i++) { + const a = argv[i]; + if (!a) continue; + if (a === '--all') all = true; + else if (a === '--dry-run') dryRun = true; + else if (a === '--keep-going') keepGoing = true; + else if (a.startsWith('--targets=')) { + for (const t of a.slice('--targets='.length).split(',')) { + if (t.trim()) explicitTargets.push(t.trim()); + } + } else if (a === '--targets' && i + 1 < argv.length) { + i += 1; + for (const t of argv[i].split(',')) { + if (t.trim()) explicitTargets.push(t.trim()); + } + } else if (a.startsWith('--target=')) { + const t = a.slice('--target='.length).trim(); + if (t) explicitTargets.push(t); + } else if (a === '--target' && i + 1 < argv.length) { + i += 1; + if (argv[i].trim()) explicitTargets.push(argv[i].trim()); + } else { + paths.push(a); + } + } + + return { all, dryRun, keepGoing, explicitTargets, paths }; +} + +function main() { + const opts = parseArgs(process.argv.slice(2)); + let targets; + if (opts.explicitTargets.length > 0) { + targets = opts.explicitTargets; + } else if (opts.all) { + targets = [...new Set([...TIERS.L0, ...TIERS.L1, ...TIERS.L2])]; + } else { + const changed = opts.paths.length > 0 ? opts.paths : collectChangedPaths(ROOT); + targets = routePaths(changed).targets; + } + + if (opts.dryRun) { + for (const t of targets) console.log(t); + return; + } + + const res = runGateList(targets, { cwd: ROOT, keepGoing: opts.keepGoing }); + process.exit(res.status); +} + +const isMain = process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url); +if (isMain) main(); diff --git a/scripts/run-gates.test.mjs b/scripts/run-gates.test.mjs new file mode 100644 index 0000000..ce7b83a --- /dev/null +++ b/scripts/run-gates.test.mjs @@ -0,0 +1,49 @@ +// Tests for scripts/run-gates.mjs (#360): verifies honest exit-code propagation, +// stop-on-first-failure behavior, and path routing via route-gates.mjs. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { dirname, join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..'); +const RUNNER = join(ROOT, 'scripts/run-gates.mjs'); + +function runRunner(args) { + return spawnSync(process.execPath, [RUNNER, ...args], { + cwd: ROOT, + encoding: 'utf8', + timeout: 15_000, + }); +} + +test('stops at the first failing target and exits non-zero', () => { + const targets = [ + `${process.execPath} -e "process.exit(0)"`, + `${process.execPath} -e "process.exit(7)"`, + `${process.execPath} -e "console.log('MUST_NOT_RUN')"`, + ].join(','); + const r = runRunner([`--targets=${targets}`]); + assert.equal(r.status, 7, `expected exit 7, got ${r.status}\n${r.stdout}\n${r.stderr}`); + assert.match(r.stdout, /FAIL/); + assert.doesNotMatch(r.stdout, /MUST_NOT_RUN/, 'must stop at the first failing gate'); +}); + +test('runs all targets and exits 0 when every target passes', () => { + const targets = [ + `${process.execPath} -e "console.log('first-ok')"`, + `${process.execPath} -e "console.log('second-ok')"`, + ].join(','); + const r = runRunner([`--targets=${targets}`]); + assert.equal(r.status, 0, r.stderr || r.stdout); + assert.match(r.stdout, /first-ok/); + assert.match(r.stdout, /second-ok/); + assert.match(r.stdout, /PASS/); + assert.doesNotMatch(r.stdout, /FAIL/); +}); + +test('--dry-run routes changed paths through route-gates.mjs', () => { + const r = runRunner(['--dry-run', 'README.md']); + assert.equal(r.status, 0, r.stderr || r.stdout); + assert.match(r.stdout, /^make check-md$/m); +}); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 7c70d2b..71096c9 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1043,6 +1043,36 @@ "gate": "node-test:runtime/gates/compare-integrity.test.mjs", "expect_detail": "GATE INTEGRITY" }, + { + "name": "compare.sh dies when the Node 24 oracle version check is gutted", + "why": "Oracle expectations drift between Node majors (#185: node:sqlite coercion differs in both Node 22 and Node 26 against Node 24); a non-24 oracle must not run.", + "source": "scripts/lib/compare.sh", + "find": "\tif [ \"$_got\" != \"$_want\" ]; then", + "replace": "\tif false; then", + "gate": "node-test:runtime/gates/compare-integrity.test.mjs", + "test": "compare.sh refuses a non-Node-24 NODE_BIN (#185)", + "expect_detail": "Expected values to be strictly equal" + }, + { + "name": "compare.sh dies when compare_case ignores a node per-case timeout", + "why": "A case that hangs both Node and Lava exits 124 on both sides with empty output, which compare_case would pass as 124 == 124 without the explicit timeout check (#399).", + "source": "scripts/lib/compare.sh", + "find": "\tif [ \"$node_status\" -eq 124 ]; then\n\t\tset -e\n\t\tprintf 'timeout (%ss) running node for %s\\n' \"$CASE_TIMEOUT_SEC\" \"${case_file#$ROOT_DIR/}\" >&2\n\t\treturn 124\n\tfi", + "replace": "\tif false; then\n\t\treturn 124\n\tfi", + "gate": "node-test:runtime/gates/compare-integrity.test.mjs", + "test": "compare_case fails when both node and lava time out instead of matching 124==124 (#399)", + "expect_detail": "compare_case fails when both node and lava time out instead of matching 124==124 (#399)" + }, + { + "name": "compare.sh dies when compare_case ignores a lava per-case timeout", + "why": "When Lava hangs on a case that Node finishes, compare_case must report a lava timeout naming the case rather than a generic exit mismatch (#399).", + "source": "scripts/lib/compare.sh", + "find": "\tif [ \"$lava_status\" -eq 124 ]; then\n\t\tprintf 'timeout (%ss) running lava for %s\\n' \"$CASE_TIMEOUT_SEC\" \"${case_file#$ROOT_DIR/}\" >&2\n\t\treturn 124\n\tfi", + "replace": "\tif false; then\n\t\treturn 124\n\tfi", + "gate": "node-test:runtime/gates/compare-integrity.test.mjs", + "test": "compare_case fails with lava timeout message when only lava hangs (#399)", + "expect_detail": "compare_case fails with lava timeout message when only lava hangs (#399)" + }, { "name": "assert-case-counts dies when the min floor branch is disabled", "why": "Silent case deletion must fail the count; if (false) makes every suite green at any size.", @@ -1672,6 +1702,16 @@ "expect_detail": [ "" ] + }, + { + "name": "run-gates dies when a failing gate no longer sets a non-zero exit code", + "why": "make gates exists so a red gate cannot be hidden by a pipe's exit status (#360); returning status 0 on failure defeats the runner.", + "source": "scripts/run-gates.mjs", + "find": " const status = failed ? failed.status : 0;", + "replace": " const status = 0;", + "gate": "node-test:scripts/run-gates.test.mjs", + "test": "stops at the first failing target and exits non-zero", + "expect_detail": "stops at the first failing target and exits non-zero" } ] } From bdfaae08a0aadb40cf435c1603b1ce3df9f99066 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:06:16 +0200 Subject: [PATCH 09/61] feat(compat): move the Node oracle to 26+ and match its four observable changes .nvmrc pins 26.13.0 and compare.sh treats its major as a floor, not an exact match: Lava tracks Node forward (26 now, 28 planned), so a newer oracle may run and its drift shows up as case diffs, while an older one is refused before any case runs. The compare-integrity fixtures read the floor from .nvmrc, so the next bump does not touch them. Running every oracle case under node 26.13.0 found four behavior changes, now matched (tests rewritten from a node 26 probe first, red on Lava, then fixed): - node:sqlite binds `undefined` as NULL (node 24 threw ERR_INVALID_ARG_TYPE). - run()'s changes/lastInsertRowid throw ERR_OUT_OF_RANGE past 2^53 after the row is written (node 24 returned a lossy number); sqlite_int_value loses its now-constant throw_on_unsafe flag. - util.parseEnv returns a null-prototype object that keeps `__proto__` as an own key (node 24 dropped it and returned a plain object). - MIMEType trims the input at both ends and discards text after a closing quote up to the next ';' (WHATWG mimesniff). Five mutations recorded. Docs move the target from 22+/24 to 26+; historical "verified on node 24.x" comments stay, since they record how a fact was checked. Closes #185. Co-Authored-By: Claude Opus 5.5 --- .claude/agents/oracle-test-auditor.md | 6 +- .nvmrc | 2 +- CLAUDE.md | 3 +- README.md | 2 +- ROADMAP.md | 6 +- agents/specialists/oracle-test-auditor.md | 6 +- docs/ARCHITECTURE.md | 2 +- pkg/runtime/js/internal/mime.js | 21 +++--- pkg/runtime/js/internal/parse_env.js | 18 ++--- pkg/runtime/js/internal/sqlite.js | 16 +++-- pkg/runtime/sqlite.odin | 24 +++---- runtime/gates/compare-integrity.test.mjs | 75 +++++++++++---------- scripts/lib/compare.sh | 12 ++-- tests/mutation-manifest.json | 52 +++++++++++++- tests/node-compat/cases/44-util-parseenv.js | 18 +++-- tests/node-compat/cases/46-util-mimetype.js | 15 +++-- tests/std/sqlite/cases/04-bigint.js | 24 +++++-- tests/std/sqlite/cases/05-coercion.js | 13 ++-- 18 files changed, 202 insertions(+), 113 deletions(-) diff --git a/.claude/agents/oracle-test-auditor.md b/.claude/agents/oracle-test-auditor.md index 392139b..464ea9c 100644 --- a/.claude/agents/oracle-test-auditor.md +++ b/.claude/agents/oracle-test-auditor.md @@ -1,6 +1,6 @@ --- name: oracle-test-auditor -description: Judges a diff against Node (the project's first ranking criterion) and whether its tests would catch a revert — real Node 22+/24 as the oracle; oracle cases, Odin tests for what no oracle can see, recorded mutations, and gates wired into CI. Use on every code change under review; it is the owner of criterion 1 and of the test-coverage gate, including any moved `known-lava-gaps.txt`, deleted or skipped test, or declared deviation. +description: Judges a diff against Node (the project's first ranking criterion) and whether its tests would catch a revert — real Node 26+ (the `.nvmrc` floor) as the oracle; oracle cases, Odin tests for what no oracle can see, recorded mutations, and gates wired into CI. Use on every code change under review; it is the owner of criterion 1 and of the test-coverage gate, including any moved `known-lava-gaps.txt`, deleted or skipped test, or declared deviation. tools: Read, Grep, Glob, Bash model: opus --- @@ -10,7 +10,7 @@ the observable surface match Node**, and **is that pinned by a test that would go red if it stopped matching**. You never edit repo sources; scratch files go in the scratch dir. -Criterion 1 of this project: the observable surface matches Node 22+/24. You +Criterion 1 of this project: the observable surface matches Node 26+ (`.nvmrc`). You verify that empirically. Node is the oracle — run it, do not reason about what it probably does. @@ -24,7 +24,7 @@ or tooling change), say so, skip Part 1, and do Part 2 only. 1. Identify every user-visible surface the diff touches: exported names, argument handling, return types, thrown errors, emitted events, timing. 2. Pin the oracle before trusting it. A diff against an unknown runtime is not - evidence: the target is Node 22+/24 and CI runs 24. Use `$NODE_BIN` when set — + evidence: the target is Node 26+ and CI runs the `.nvmrc` pin. Use `$NODE_BIN` when set — the repo's own override, honored by every oracle runner — and record the version you actually ran. A major outside 22/24 makes the result advisory, not proof. 3. For each surface, write a small script and run it under both: diff --git a/.nvmrc b/.nvmrc index b74495a..1dda03d 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -24.22.2 +26.13.0 diff --git a/CLAUDE.md b/CLAUDE.md index 0ae3857..6b8885f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -20,7 +20,8 @@ Layering (never blur it): 1. **Node interface conformance.** The observable surface — names, argument coercion order, defaults, thrown error `code`/`name`/`message`, event order, - return types — matches Node 22+/24. Node is the oracle, not our reading of the + return types — matches Node 26+ (the `.nvmrc` floor; 28 is the planned final + pin). Node is the oracle, not our reading of the docs. Deviation is allowed **only** when it buys measured speed or memory control, and then it must be: written down in the code comment, listed in the PR, and covered by a Lava-only test pinning the intended behavior. diff --git a/README.md b/README.md index 3a54d22..b2aaad7 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ fetch JavaScriptCore/WebKit and the SQLite amalgamation into the ignored `.deps/ cache. `scripts/build-sqlite-windows.sh` then compiles SQLite into `build/sqlite3.lib`; WebKit is linked from `.deps/webkit/bun-webkit/lib`. -Node 24 (`24.22.2` pinned in [`.nvmrc`](.nvmrc); oracle suites require Node 24) and Bun are needed for the JavaScript gates — run `bun install` once. +Node 26+ (`26.13.0` pinned in [`.nvmrc`](.nvmrc); oracle suites refuse an older major) and Bun are needed for the JavaScript gates — run `bun install` once. They are dev-only (`markdownlint-cli2`, `acorn`/`acorn-walk` for the prototype-pollution ratchet, `fast-check` for `make test-property`); `bin/lava` links none of them. `make check-js` is an always-gate and now fails without them. diff --git a/ROADMAP.md b/ROADMAP.md index 7626d52..da2dddd 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,6 +1,6 @@ # Lava roadmap -Status of the Node.js port. Baseline target: Node 24 on JavaScriptCore, with a +Status of the Node.js port. Baseline target: Node 26+ on JavaScriptCore, with a native event loop (kqueue / io_uring+epoll / IOCP). ## Done @@ -796,8 +796,8 @@ the index, the issue is the detail. Ordered by what it unblocks. to be exercised and most expensive to get wrong. One env-gated seam (`LAVA_HOSTFN_DISABLE` is the precedent) unlocks all three. - [x] Pin a specific Odin release in CI (`dev-2026-11a` with `llvm-version: '18'`, - [#35](https://github.com/x-ror/lava/issues/35)), and pin Node `24.22.2` in - `.nvmrc` + `scripts/lib/compare.sh` (#185). + [#35](https://github.com/x-ror/lava/issues/35)), and pin Node `26.13.0` in + `.nvmrc` as a major floor checked by `scripts/lib/compare.sh` (#185). - [x] Update `actions/checkout` (`v7`) / `actions/setup-node` (`v7` with `node-version-file: .nvmrc`) past the Node 20 deprecation warning ([#35](https://github.com/x-ror/lava/issues/35)). diff --git a/agents/specialists/oracle-test-auditor.md b/agents/specialists/oracle-test-auditor.md index 392139b..464ea9c 100644 --- a/agents/specialists/oracle-test-auditor.md +++ b/agents/specialists/oracle-test-auditor.md @@ -1,6 +1,6 @@ --- name: oracle-test-auditor -description: Judges a diff against Node (the project's first ranking criterion) and whether its tests would catch a revert — real Node 22+/24 as the oracle; oracle cases, Odin tests for what no oracle can see, recorded mutations, and gates wired into CI. Use on every code change under review; it is the owner of criterion 1 and of the test-coverage gate, including any moved `known-lava-gaps.txt`, deleted or skipped test, or declared deviation. +description: Judges a diff against Node (the project's first ranking criterion) and whether its tests would catch a revert — real Node 26+ (the `.nvmrc` floor) as the oracle; oracle cases, Odin tests for what no oracle can see, recorded mutations, and gates wired into CI. Use on every code change under review; it is the owner of criterion 1 and of the test-coverage gate, including any moved `known-lava-gaps.txt`, deleted or skipped test, or declared deviation. tools: Read, Grep, Glob, Bash model: opus --- @@ -10,7 +10,7 @@ the observable surface match Node**, and **is that pinned by a test that would go red if it stopped matching**. You never edit repo sources; scratch files go in the scratch dir. -Criterion 1 of this project: the observable surface matches Node 22+/24. You +Criterion 1 of this project: the observable surface matches Node 26+ (`.nvmrc`). You verify that empirically. Node is the oracle — run it, do not reason about what it probably does. @@ -24,7 +24,7 @@ or tooling change), say so, skip Part 1, and do Part 2 only. 1. Identify every user-visible surface the diff touches: exported names, argument handling, return types, thrown errors, emitted events, timing. 2. Pin the oracle before trusting it. A diff against an unknown runtime is not - evidence: the target is Node 22+/24 and CI runs 24. Use `$NODE_BIN` when set — + evidence: the target is Node 26+ and CI runs the `.nvmrc` pin. Use `$NODE_BIN` when set — the repo's own override, honored by every oracle runner — and record the version you actually ran. A major outside 22/24 makes the result advisory, not proof. 3. For each surface, write a small script and run it under both: diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index cf11171..e91dbd2 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -13,7 +13,7 @@ Lava is a Node-compatible JavaScript runtime built on **JavaScriptCore (JSC)** as the execution engine and **Odin** as the systems language for everything around it: the engine FFI, the event loop, the I/O transports, and the native half of -the standard library. Compatibility targets modern Node (22+/24 behavior) rather +the standard library. Compatibility targets modern Node (26+ behavior, floor pinned in `.nvmrc`) rather than legacy APIs. Two design decisions shape the whole system: diff --git a/pkg/runtime/js/internal/mime.js b/pkg/runtime/js/internal/mime.js index 69e0352..4c36bba 100644 --- a/pkg/runtime/js/internal/mime.js +++ b/pkg/runtime/js/internal/mime.js @@ -77,12 +77,16 @@ } function parse(str) { - // Only leading whitespace is trimmed from the whole input; trailing whitespace is - // removed per-component (subtype, unquoted value) so it cannot eat characters inside a - // quoted value (e.g. an unterminated `a="b\t` keeps the tab). Errors quote the input - // as given, leading whitespace included, as node does. + // The whole input is trimmed at both ends first (WHATWG mimesniff step 1), so an + // unterminated `a="b\t` loses the tab — node 26 does this; node 24 trimmed only the + // leading end and kept it. Subtype and unquoted values are still trimmed per + // component, since whitespace before a ';' is not at the end of the input. Errors + // quote the input as given, as node does. var input = str; str = str.replace(LEADING_HTTP_WS, ''); + var end = str.length; + while (end > 0 && isHTTPWhitespace(str[end - 1])) end--; + str = StringPrototypeSlice(str, 0, end); var slash = str.indexOf('/'); if (slash === -1) throw mimeError('type', input, -1); var type = str.slice(0, slash); @@ -141,15 +145,16 @@ position++; } value = buf; - // Do not skip the rest of the line to the next ';' here: Node resumes the loop - // right after the closing quote (the single advance below steps past the next - // char), so parameters that follow a quoted value across whitespace/';' survive. + // Everything after the closing quote up to the next ';' is discarded (WHATWG + // mimesniff, node 26): `a="b" c=d` keeps only a. node 24 resumed one char past + // the quote and kept c=d. + while (position < len && str[position] !== ';') position++; } else { var valStart = position; while (position < len && str[position] !== ';') position++; value = str.slice(valStart, position).replace(TRAILING_HTTP_WS, ''); } - if (position < len) position++; // step past the ';' (or the char after a closing quote) + if (position < len) position++; // step past the ';' // Keep only well-formed, non-duplicate parameters (first wins). An empty value is // dropped only when it was unquoted; a quoted "" is a valid empty value, matching diff --git a/pkg/runtime/js/internal/parse_env.js b/pkg/runtime/js/internal/parse_env.js index c11dc4c..d4c9cff 100644 --- a/pkg/runtime/js/internal/parse_env.js +++ b/pkg/runtime/js/internal/parse_env.js @@ -4,8 +4,9 @@ // `\n` -> newline expansion (every other backslash, incl. one before the closing quote, is // literal; single/backtick are fully literal), an *unterminated* quote falling back to a // line literal that keeps the opening quote and ignores inline `#`, inline `#` comments on -// unquoted values, and last-wins on duplicate keys. The `__proto__` key is dropped and the -// result keys come out byte-sorted (Node stores them in a std::map). Pure JS via +// unquoted values, and last-wins on duplicate keys. The result is a null-prototype object +// (node 26), so a `__proto__` key is an ordinary own property, and its keys come out +// byte-sorted (Node stores them in a std::map). Pure JS via // primordials; backs require('node:util').parseEnv. Hidden from the public resolver like // parse_args. (function (require, module) { @@ -190,16 +191,17 @@ while (i < len && StringPrototypeCharCodeAt(content, i) !== LF) i++; } - // Node drops a literal `__proto__` key rather than letting it touch the prototype. - if (key.length > 0 && key !== '__proto__') tmp[key] = value; + // `tmp` has no prototype, so `__proto__` here is a plain key, not the setter. + if (key.length > 0) tmp[key] = value; } - // Node returns the keys in std::map (byte-sorted) order in a plain object (its proto - // is Object.prototype). Rebuild to match; the `__proto__` key was already dropped - // above, so assigning onto a plain object cannot pollute the prototype. + // Node returns the keys in std::map (byte-sorted) order. From node 26 the object has + // a null prototype and keeps `__proto__` as an own data property; node 24 returned a + // plain object and dropped the key. Null prototype on `result` too, so the + // `__proto__` assignment below defines a property instead of calling the setter. var keys = ObjectKeys(tmp); ArrayPrototypeSort(keys, byteCompare); - var result = {}; + var result = { __proto__: null }; for (var k = 0; k < keys.length; k++) result[keys[k]] = tmp[keys[k]]; return result; } diff --git a/pkg/runtime/js/internal/sqlite.js b/pkg/runtime/js/internal/sqlite.js index e88652a..64dbbcc 100644 --- a/pkg/runtime/js/internal/sqlite.js +++ b/pkg/runtime/js/internal/sqlite.js @@ -173,9 +173,9 @@ var I64_MAX = 2n ** 63n - 1n; // bindOne binds a single value, enforcing node:sqlite's accepted types. Only - // null, number, string, BigInt, boolean, TypedArray/DataView and - // ArrayBuffer/SharedArrayBuffer are bindable; anything else (undefined, plain - // object, symbol, function) throws instead of being silently coerced. BigInt + // null/undefined, number, string, BigInt, boolean, TypedArray/DataView and + // ArrayBuffer/SharedArrayBuffer are bindable; anything else (plain object, + // symbol, function) throws instead of being silently coerced. BigInt // binds as INTEGER when it fits in i64, otherwise throws — Node accepts neither a // lossy nor an overflowing bind. // @@ -185,10 +185,14 @@ // empty Uint8Array, which stays an empty BLOB. Probed against node 24.21 with // `SELECT ?, typeof(?)`; node 22 threw on the boolean and took the buffer for a // named bag. Pinned by tests/std/sqlite/cases/08-coercion-parity.js. + // node 26 binds `undefined` as NULL, positional and named; node 24 threw + // ERR_INVALID_ARG_TYPE. Pinned by tests/std/sqlite/cases/05-coercion.js. function bindOne(stmtId, index, value) { var t = typeof value; var n; - if (value === null || t === 'number' || t === 'string') { + if (value === undefined) { + native.bind(stmtId, index, null); // primordials-ok: method + } else if (value === null || t === 'number' || t === 'string') { native.bind(stmtId, index, value); } else if (t === 'boolean') { // The BigInt path is the existing INTEGER bind; a number would bind REAL. @@ -259,8 +263,8 @@ } bindOne(this._stmtId, i + 1, value); } else if (pos < args.length) { - // Anonymous "?" parameter — take the next positional argument. An - // explicitly-passed undefined is rejected by bindOne (Node throws). + // Anonymous "?" parameter — take the next positional argument (an explicit + // undefined binds NULL, node 26). bindOne(this._stmtId, i + 1, args[pos++]); } else { // Fewer positional args than placeholders: the trailing ones bind NULL, diff --git a/pkg/runtime/sqlite.odin b/pkg/runtime/sqlite.odin index 03cc7a7..a9a9004 100644 --- a/pkg/runtime/sqlite.odin +++ b/pkg/runtime/sqlite.odin @@ -468,13 +468,7 @@ sqlite_row_cb :: proc "c" ( value: jsc.JSValueRef switch sqlite.column_type(stmt, i) { case .Integer: - value = sqlite_int_value( - ctx, - sqlite.column_int(stmt, i), - read_big_ints, - true, - exception, - ) + value = sqlite_int_value(ctx, sqlite.column_int(stmt, i), read_big_ints, exception) // An out-of-range INTEGER throws (ERR_OUT_OF_RANGE) — abandon the row. if exception != nil && exception^ != nil do return jsc.JSValueMakeUndefined(ctx) case .Float: @@ -509,8 +503,8 @@ sqlite_changes_cb :: proc "c" ( db := sqlite_get_db(state, sqlite_arg_id(ctx, arguments[0])) if db == nil do return jsc.JSValueMakeNumber(ctx, 0) read_big_ints := sqlite_read_big_ints(ctx, argument_count, arguments, 1) - // changes/lastInsertRowid never throw (Node returns a lossy number by default). - return sqlite_int_value(ctx, sqlite.changes(db), read_big_ints, false, exception) + // Unsafe counts throw like a column read (node 26); see sqlite_int_value. + return sqlite_int_value(ctx, sqlite.changes(db), read_big_ints, exception) } sqlite_last_insert_rowid_cb :: proc "c" ( @@ -527,7 +521,7 @@ sqlite_last_insert_rowid_cb :: proc "c" ( db := sqlite_get_db(state, sqlite_arg_id(ctx, arguments[0])) if db == nil do return jsc.JSValueMakeNumber(ctx, 0) read_big_ints := sqlite_read_big_ints(ctx, argument_count, arguments, 1) - return sqlite_int_value(ctx, sqlite.last_insert_rowid(db), read_big_ints, false, exception) + return sqlite_int_value(ctx, sqlite.last_insert_rowid(db), read_big_ints, exception) } // --- helpers --- @@ -553,18 +547,18 @@ sqlite_read_big_ints :: proc( // sqlite_int_value converts an i64 column/rowid value to a JS value honoring the // statement's readBigInts flag. With read_big_ints it returns a BigInt; otherwise -// a JS number — throwing ERR_OUT_OF_RANGE (like node:sqlite) when throw_on_unsafe -// is set and the value is not exactly representable. run()'s changes/lastInsertRowid -// pass throw_on_unsafe=false: Node returns those as a lossy number, never throwing. +// a JS number, throwing ERR_OUT_OF_RANGE (like node:sqlite) when the value is not +// exactly representable. That includes run()'s changes/lastInsertRowid: node 26 +// throws for them after the statement has run (the row stays written); node 24 +// returned a lossy number. Pinned by tests/std/sqlite/cases/04-bigint.js. sqlite_int_value :: proc( ctx: jsc.JSContextRef, v: i64, read_big_ints: bool, - throw_on_unsafe: bool, exception: ^jsc.JSValueRef, ) -> jsc.JSValueRef { if read_big_ints do return sqlite_make_bigint(ctx, v) - if throw_on_unsafe && (v > SQLITE_MAX_SAFE_INTEGER || v < -SQLITE_MAX_SAFE_INTEGER) { + if (v > SQLITE_MAX_SAFE_INTEGER || v < -SQLITE_MAX_SAFE_INTEGER) { if exception != nil { msg := fmt.tprintf( "Value is too large to be represented as a JavaScript number: %d", diff --git a/runtime/gates/compare-integrity.test.mjs b/runtime/gates/compare-integrity.test.mjs index 9816f22..2bedf61 100644 --- a/runtime/gates/compare-integrity.test.mjs +++ b/runtime/gates/compare-integrity.test.mjs @@ -1,33 +1,36 @@ -// Pins scripts/lib/compare.sh gate integrity, Node major match (#185), and per-case timeouts (#399). +// Pins scripts/lib/compare.sh gate integrity, the Node major floor from .nvmrc (#185), and +// per-case timeouts (#399). import { after, test } from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { resolve, dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); const SCRATCH = mkdtempSync(join(tmpdir(), 'lava-compare-test-')); -const BIN24_DIR = join(SCRATCH, 'bin24'); -mkdirSync(BIN24_DIR); - -const NODE24 = join(BIN24_DIR, 'node'); -const NODE22 = join(SCRATCH, 'node22'); -const NODE26 = join(SCRATCH, 'node26'); +// The floor is read, not hard-coded, so bumping .nvmrc does not touch this file. +const PIN = Number(readFileSync(join(ROOT, '.nvmrc'), 'utf8').split('.')[0]); +const PIN_BIN_DIR = join(SCRATCH, 'binpin'); +mkdirSync(PIN_BIN_DIR); + +const NODE_PIN = join(PIN_BIN_DIR, 'node'); +const NODE_OLD = join(SCRATCH, 'node-old'); +const NODE_NEW = join(SCRATCH, 'node-new'); const FAKE_LAVA = join(SCRATCH, 'fake-lava'); const HANG_CASE = join(SCRATCH, 'hang.js'); const OK_CASE = join(SCRATCH, 'ok.js'); writeFileSync( - NODE24, + NODE_PIN, `#!/usr/bin/env sh if [ "\${1:-}" = "-p" ]; then - echo 24 + echo ${PIN} exit 0 fi if [ "\${1:-}" = "--version" ]; then - echo v24.22.2 + echo v${PIN}.0.0 exit 0 fi exec "${process.execPath}" "$@" @@ -36,17 +39,17 @@ exec "${process.execPath}" "$@" ); writeFileSync( - NODE22, + NODE_OLD, `#!/usr/bin/env sh -[ "\${1:-}" = "-p" ] && echo 22 || echo v22.22.2 +[ "\${1:-}" = "-p" ] && echo ${PIN - 2} || echo v${PIN - 2}.22.2 `, { mode: 0o755 }, ); writeFileSync( - NODE26, + NODE_NEW, `#!/usr/bin/env sh -[ "\${1:-}" = "-p" ] && echo 26 || echo v26.13.0 +[ "\${1:-}" = "-p" ] && echo ${PIN + 2} || echo v${PIN + 2}.1.0 `, { mode: 0o755 }, ); @@ -72,7 +75,7 @@ after(() => { }); function sourceCompare(env, body = ':') { - const pathEnv = `${BIN24_DIR}:${process.env.PATH || ''}`; + const pathEnv = `${PIN_BIN_DIR}:${process.env.PATH || ''}`; return spawnSync( 'sh', ['-c', `ROOT_DIR="${ROOT}" TMP_DIR="${SCRATCH}" . "${ROOT}/scripts/lib/compare.sh"\n${body}`], @@ -109,33 +112,35 @@ test('compare.sh allows NODE_BIN=node under RUN_LAVA=1', () => { }); test('compare.sh is a no-op integrity-wise when RUN_LAVA=0', () => { - // Same path for both would be refused under RUN_LAVA=1; use the fake Node 24 - // binary so the major check passes while keeping NODE_BIN === LAVA_BIN. + // Same path for both would be refused under RUN_LAVA=1; use the fake pinned + // Node so the major check passes while keeping NODE_BIN === LAVA_BIN. const r = sourceCompare({ - NODE_BIN: NODE24, - LAVA_BIN: NODE24, + NODE_BIN: NODE_PIN, + LAVA_BIN: NODE_PIN, RUN_LAVA: '0', }); assert.equal(r.status, 0, r.stderr || r.stdout); }); -test('compare.sh refuses a non-Node-24 NODE_BIN (#185)', () => { - for (const [bin, ver] of [ - [NODE22, 'v22.22.2'], - [NODE26, 'v26.13.0'], - ]) { - const r = sourceCompare({ NODE_BIN: bin, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }); - assert.equal(r.status, 2, r.stderr || r.stdout); - assert.match( - r.stderr || '', - new RegExp(`oracle requires node 24 \\(\\.nvmrc\\), found ${ver.replace(/\./g, '\\.')}`), - ); - } +test('compare.sh refuses a NODE_BIN older than the .nvmrc major (#185)', () => { + const r = sourceCompare({ NODE_BIN: NODE_OLD, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }); + assert.equal(r.status, 2, r.stderr || r.stdout); + assert.match( + r.stderr || '', + new RegExp(`oracle requires node >= ${PIN} \\(\\.nvmrc\\), found v${PIN - 2}\\.22\\.2`), + ); +}); + +test('compare.sh accepts a NODE_BIN newer than the .nvmrc major', () => { + // The pin is a floor: the product tracks Node forward (26 now, 28 later), so a + // newer oracle is allowed to run and any drift shows up as a case diff. + const r = sourceCompare({ NODE_BIN: NODE_NEW, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }); + assert.equal(r.status, 0, r.stderr || r.stdout); }); test('run_node_case fails with timeout message when a case exceeds CASE_TIMEOUT_SEC (#399)', () => { const r = sourceCompare( - { NODE_BIN: NODE24, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '0', CASE_TIMEOUT_SEC: '1' }, + { NODE_BIN: NODE_PIN, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '0', CASE_TIMEOUT_SEC: '1' }, `run_case "${HANG_CASE}"`, ); assert.equal(r.status, 124, r.stderr || r.stdout); @@ -144,7 +149,7 @@ test('run_node_case fails with timeout message when a case exceeds CASE_TIMEOUT_ test('compare_case fails when both node and lava time out instead of matching 124==124 (#399)', () => { const r = sourceCompare( - { NODE_BIN: NODE24, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1', CASE_TIMEOUT_SEC: '1' }, + { NODE_BIN: NODE_PIN, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1', CASE_TIMEOUT_SEC: '1' }, `run_case "${HANG_CASE}"`, ); assert.equal(r.status, 124, r.stderr || r.stdout); @@ -154,7 +159,7 @@ test('compare_case fails when both node and lava time out instead of matching 12 test('compare_case fails with lava timeout message when only lava hangs (#399)', () => { const r = sourceCompare( { - NODE_BIN: NODE24, + NODE_BIN: NODE_PIN, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1', CASE_TIMEOUT_SEC: '1', diff --git a/scripts/lib/compare.sh b/scripts/lib/compare.sh index 021c0fb..14f21c7 100644 --- a/scripts/lib/compare.sh +++ b/scripts/lib/compare.sh @@ -44,14 +44,16 @@ _lava_gate_integrity_check() { } _lava_gate_integrity_check || exit $? -# Issue #185: the oracle suites are baselined to the Node major pinned in .nvmrc -# (Node 24; e.g. node:sqlite coercion differs in both Node 22 and Node 26). -# Fail fast before running any case when NODE_BIN is not that major. +# Issue #185: the oracle suites are baselined to the Node major pinned in .nvmrc, +# and that major is a floor, not an exact match: Lava tracks Node forward (26 now, +# 28 planned), so a newer oracle may run and its drift shows up as case diffs. +# An older one cannot run at all: node:sqlite alone binds `undefined` as NULL from +# 26 where 24 threw, so its expectations are wrong for us. Fail before any case. _lava_node_major_check() { _want=$(cut -d. -f1 "$ROOT_DIR/.nvmrc" | tr -dc '0-9') _got=$("$NODE_BIN" -p 'process.versions.node.split(".")[0]' 2>/dev/null) || _got=0 - if [ "$_got" != "$_want" ]; then - printf '%s\n' "oracle requires node $_want (.nvmrc), found $("$NODE_BIN" --version 2>/dev/null || echo none) ($NODE_BIN)" >&2 + if [ "$_got" -lt "$_want" ]; then + printf '%s\n' "oracle requires node >= $_want (.nvmrc), found $("$NODE_BIN" --version 2>/dev/null || echo none) ($NODE_BIN)" >&2 return 2 fi } diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 71096c9..16b3cb5 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1045,12 +1045,12 @@ }, { "name": "compare.sh dies when the Node 24 oracle version check is gutted", - "why": "Oracle expectations drift between Node majors (#185: node:sqlite coercion differs in both Node 22 and Node 26 against Node 24); a non-24 oracle must not run.", + "why": "Oracle expectations drift between Node majors (#185: node:sqlite binds undefined as NULL from 26); an oracle older than .nvmrc must not run.", "source": "scripts/lib/compare.sh", - "find": "\tif [ \"$_got\" != \"$_want\" ]; then", + "find": "\tif [ \"$_got\" -lt \"$_want\" ]; then", "replace": "\tif false; then", "gate": "node-test:runtime/gates/compare-integrity.test.mjs", - "test": "compare.sh refuses a non-Node-24 NODE_BIN (#185)", + "test": "compare.sh refuses a NODE_BIN older than the .nvmrc major (#185)", "expect_detail": "Expected values to be strictly equal" }, { @@ -1712,6 +1712,52 @@ "gate": "node-test:scripts/run-gates.test.mjs", "test": "stops at the first failing target and exits non-zero", "expect_detail": "stops at the first failing target and exits non-zero" + }, + { + "name": "46-util-mimetype dies when a quoted value stops skipping to the next ';'", + "why": "node 26 follows WHATWG and discards text after a closing quote up to ';'; resuming parsing there keeps a parameter node drops (`a=\"b\" c=d` gains c=d).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " while (position < len && str[position] !== ';') position++;\n } else {", + "replace": " } else {", + "gate": "compat:tests/node-compat/cases/46-util-mimetype.js", + "expect_detail": "'text/x;a=b;c=d' strictEqual 'text/x;a=b'" + }, + { + "name": "46-util-mimetype dies when the input is no longer trimmed at the trailing end", + "why": "node 26 trims the whole input at both ends first (WHATWG step 1), so an unterminated quoted value loses trailing whitespace.", + "source": "pkg/runtime/js/internal/mime.js", + "find": " while (end > 0 && isHTTPWhitespace(str[end - 1])) end--;", + "replace": "", + "gate": "compat:tests/node-compat/cases/46-util-mimetype.js", + "expect_detail": "'b\\t' strictEqual 'b'" + }, + { + "name": "44-util-parseenv dies when the result goes back to a plain object", + "why": "node 26 returns a null-prototype object that keeps `__proto__` as an own key; a plain object swallows the key into the setter and has Object.prototype.", + "source": "pkg/runtime/js/internal/parse_env.js", + "find": " var result = { __proto__: null };", + "replace": " var result = {};", + "gate": "compat:tests/node-compat/cases/44-util-parseenv.js", + "expect_detail": "{} strictEqual null" + }, + { + "name": "05-coercion dies when undefined is no longer bound as NULL", + "why": "node 26 binds undefined as SQL NULL; node 24 threw ERR_INVALID_ARG_TYPE. Dropping the branch falls through to the unbindable throw.", + "source": "pkg/runtime/js/internal/sqlite.js", + "find": " if (value === undefined) {\n native.bind(stmtId, index, null); // primordials-ok: method\n } else if (value === null", + "replace": " if (value === null", + "gate": "compat:tests/std/sqlite/cases/05-coercion.js", + "expect_detail": "Provided value cannot be bound to SQLite parameter 1." + }, + { + "name": "04-bigint dies when run()'s lastInsertRowid goes back to a lossy number", + "why": "node 26 throws ERR_OUT_OF_RANGE for an unsafe rowid after the insert; node 24 returned a lossy number.", + "source": "pkg/runtime/sqlite.odin", + "find": "\treturn sqlite_int_value(ctx, sqlite.last_insert_rowid(db), read_big_ints, exception)", + "replace": "\trowid := sqlite.last_insert_rowid(db)\n\tif read_big_ints do return sqlite_make_bigint(ctx, rowid)\n\treturn jsc.JSValueMakeNumber(ctx, f64(rowid))", + "gate": "compat:tests/std/sqlite/cases/04-bigint.js", + "expect_detail": "unsafe rowid: RangeError", + "rebuild": true } ] } diff --git a/tests/node-compat/cases/44-util-parseenv.js b/tests/node-compat/cases/44-util-parseenv.js index 2144948..dc84a18 100644 --- a/tests/node-compat/cases/44-util-parseenv.js +++ b/tests/node-compat/cases/44-util-parseenv.js @@ -35,12 +35,20 @@ assert.deepEqual({ ...parseEnv('H=ab#cd') }, { H: 'ab' }); // unquoted values keep interior spaces, colons, slashes assert.deepEqual({ ...parseEnv('URL=http://x:8080/p') }, { URL: 'http://x:8080/p' }); -// a literal `__proto__` key is dropped (no prototype pollution); the result is a plain -// object, like Node's -const p = parseEnv('__proto__=evil\nSAFE=1'); -assert.deepEqual({ ...p }, { SAFE: '1' }); -assert.equal(Object.getPrototypeOf(p), Object.prototype); +// node 26 returns a null-prototype object, so a literal `__proto__` key is kept as an +// ordinary own data property (byte-sorted with the rest) and cannot reach any +// prototype; node 24 dropped it and returned a plain object. +const p = parseEnv('__proto__=evil\nSAFE=1\nb=2'); +assert.equal(Object.getPrototypeOf(p), null); +assert.deepEqual(Object.keys(p), ['SAFE', '__proto__', 'b']); +assert.deepEqual(Object.getOwnPropertyDescriptor(p, '__proto__'), { + value: 'evil', + writable: true, + enumerable: true, + configurable: true, +}); assert.equal({}.evil, undefined); +assert.equal(Object.prototype.evil, undefined); // an unterminated quote keeps the opening quote and takes the rest of the line literally assert.deepEqual({ ...parseEnv('K="unterminated\nA=1') }, { K: '"unterminated', A: '1' }); diff --git a/tests/node-compat/cases/46-util-mimetype.js b/tests/node-compat/cases/46-util-mimetype.js index a33bc70..ceb1dac 100644 --- a/tests/node-compat/cases/46-util-mimetype.js +++ b/tests/node-compat/cases/46-util-mimetype.js @@ -106,11 +106,18 @@ const FF = String.fromCharCode(0x0c); assert.throws(() => new MIMEType(FF + 'text/plain'), { code: 'ERR_INVALID_MIME_SYNTAX' }); assert.equal(new MIMEType('\ttext/plain').toString(), 'text/plain'); // tab IS whitespace -// a parameter after a quoted value survives across whitespace/';' (Node resumes parsing) -assert.equal(new MIMEType('text/x; a="b" c=d').toString(), 'text/x;a=b;c=d'); +// after a quoted value everything up to the next ';' is discarded (WHATWG mimesniff, +// node 26; node 24 resumed one char past the quote and kept `c=d` in the first case) +assert.equal(new MIMEType('text/x; a="b" c=d').toString(), 'text/x;a=b'); assert.equal(new MIMEType('text/x; a="b"xyz;c=d').toString(), 'text/x;a=b;c=d'); -// trailing whitespace inside a (here unterminated) quoted value is preserved -assert.equal(new MIMEType('text/x;a="b\t').params.get('a'), 'b\t'); +assert.equal(new MIMEType('text/x; a="b" ;c=d').toString(), 'text/x;a=b;c=d'); +assert.equal(new MIMEType('text/x; a="b"x=y;c=d').toString(), 'text/x;a=b;c=d'); +// the whole input is trimmed at both ends first (WHATWG step 1, node 26), so trailing +// whitespace of an unterminated quoted value goes too; node 24 kept it. Whitespace +// inside the value, or before a closing quote, survives. +assert.equal(new MIMEType('text/x;a="b\t').params.get('a'), 'b'); +assert.equal(new MIMEType('text/x;a="b c ').params.get('a'), 'b c'); +assert.equal(new MIMEType('text/x;a="b\t"').params.get('a'), 'b\t'); // MIMEParams#set returns undefined (like URLSearchParams#set) assert.equal(new MIMEType('text/x').params.set('a', 'b'), undefined); diff --git a/tests/std/sqlite/cases/04-bigint.js b/tests/std/sqlite/cases/04-bigint.js index 6bc34d4..59660b6 100644 --- a/tests/std/sqlite/cases/04-bigint.js +++ b/tests/std/sqlite/cases/04-bigint.js @@ -48,13 +48,27 @@ try { } assert.equal(boundary && boundary.code, 'ERR_OUT_OF_RANGE'); -// run(): changes/lastInsertRowid are numbers by default (lossy, never throw), -// BigInt under setReadBigInts(true). +// run(): changes/lastInsertRowid are numbers by default, BigInt under +// setReadBigInts(true). From node 26 an unsafe rowid throws like a column read does +// (node 24 returned a lossy number) — AFTER the row is written, so the insert stands. db.exec('CREATE TABLE u (id INTEGER PRIMARY KEY)'); +const small = db.prepare('INSERT INTO u (id) VALUES (5)').run(); +assert.equal(small.lastInsertRowid, 5); +assert.equal(small.changes, 1); const ins = db.prepare('INSERT INTO u (id) VALUES (9007199254740995)'); -const r = ins.run(); -assert.equal(typeof r.lastInsertRowid, 'number'); -assert.equal(typeof r.changes, 'number'); +let unsafe; +try { + ins.run(); +} catch (e) { + unsafe = e; +} +assert.ok(unsafe instanceof RangeError, 'unsafe rowid: RangeError'); +assert.equal(unsafe.code, 'ERR_OUT_OF_RANGE'); +assert.equal( + unsafe.message, + 'Value is too large to be represented as a JavaScript number: 9007199254740995', +); +assert.equal(db.prepare('SELECT count(*) AS c FROM u WHERE id = 9007199254740995').get().c, 1); const insBig = db.prepare('INSERT INTO u (id) VALUES (9007199254740999)'); insBig.setReadBigInts(true); diff --git a/tests/std/sqlite/cases/05-coercion.js b/tests/std/sqlite/cases/05-coercion.js index d2e7310..ffc0050 100644 --- a/tests/std/sqlite/cases/05-coercion.js +++ b/tests/std/sqlite/cases/05-coercion.js @@ -67,12 +67,13 @@ throwsWithCode( ); // --- unbindable types throw instead of coercing --- -throwsWithCode( - () => insertOne(undefined), - 'ERR_INVALID_ARG_TYPE', - UNBINDABLE_MSG, - 'bind undefined', -); +// `undefined` is NOT one of them from node 26: it binds as SQL NULL, positional and +// named alike (node 24 threw ERR_INVALID_ARG_TYPE here). +insertOne(undefined); +const undefRow = db.prepare('SELECT x, typeof(x) AS ty FROM t').get(); +assert.equal(undefRow.x, null); +assert.equal(undefRow.ty, 'null'); +assert.equal(db.prepare('SELECT typeof(:v) AS ty').get({ v: undefined }).ty, 'null'); throwsWithCode(() => insertOne(Symbol('s')), 'ERR_INVALID_ARG_TYPE', UNBINDABLE_MSG, 'bind symbol'); // (A boolean is bindable since node 24.x — INTEGER 1/0, pinned in 08-coercion-parity.js.) From 5810038ecf32264ff18285c5405cdd33a9634599 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:14:47 +0200 Subject: [PATCH 10/61] build(md): bump markdownlint-cli2 to 0.24.1 and format markdown with bun fmt make check-md / fix-md now run `bun fmt` over **/*.md before markdownlint: bun fmt owns layout (list markers, table alignment, spacing), markdownlint keeps the semantic rules a formatter cannot (MD051 link fragments, MD040, MD001/MD025, MD024 siblings). The two agree: bun-formatted output lints clean. bunfig.toml ignores the vendored reference/node-doc-api and the generated reference/node-compatibility.md, which gen-matrix.py --check diffs byte-for-byte. gate-runner.md (+ .claude mirror): two inline code spans broken across lines are rejoined; bun fmt otherwise strips the list-continuation indent after them. Co-Authored-By: Claude Opus 5.5 --- .claude/agents/gate-runner.md | 8 +-- CLAUDE.md | 2 +- Makefile | 4 +- agents/prompts/pr-gate-reference/gates.md | 4 +- agents/specialists/gate-runner.md | 8 +-- bun.lock | 78 ++++++++++++++++++----- bunfig.toml | 24 +++++-- package.json | 6 +- 8 files changed, 96 insertions(+), 38 deletions(-) diff --git a/.claude/agents/gate-runner.md b/.claude/agents/gate-runner.md index 96fc36e..00a7780 100644 --- a/.claude/agents/gate-runner.md +++ b/.claude/agents/gate-runner.md @@ -27,8 +27,8 @@ five that CI does not run. The brief's `phase:` picks one. -- **`compile`** — `make check`; `make check-js` if any JS changed; `make - check-md` if any `.md` changed; `make check-actions` if a workflow changed; +- **`compile`** — `make check`; `make check-js` if any JS changed; + `make check-md` if any `.md` changed; `make check-actions` if a workflow changed; `make build`. Fast, and the conductor waits on it: reviewers are not spawned on code that does not compile. Stop at the first failure of `make check` or `make build`. @@ -55,8 +55,8 @@ Before the first target, compare the toolchain with what CI pins — `node repo has one), `odin version` if the brief names one. On a mismatch: -- Look for the pinned version before saying it is absent: `ls /opt`, `type -a - node`, the version manager on the box. "Not on `PATH`" is not "not +- Look for the pinned version before saying it is absent: `ls /opt`, `type -a node`, + the version manager on the box. "Not on `PATH`" is not "not installed". - Found → run every gate with it first on `PATH` and report `ENV MISMATCH: default node , pinned — ran with from `. diff --git a/CLAUDE.md b/CLAUDE.md index 0ae3857..efbca7a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -80,7 +80,7 @@ Use the `odin-sdk-scout` agent for this search — it reads the real SDK source. ```sh make check # Odin type-check (incl. windows/darwin cross-target front-end) make check-js # bun fmt --check + orphan-JS + node:test over scripts/ + primordials ratchet -make check-md # markdownlint over the repo's docs (make fix-md auto-fixes) +make check-md # bun fmt + markdownlint over the repo's docs (make fix-md auto-fixes) make check-actions # actionlint over .github/workflows make build # ./scripts/build.sh → bin/lava make test # Odin unit tests + oracle suites diff --git a/Makefile b/Makefile index 9d40bd5..48c4b71 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ help: @printf '%s\n' ' make check-runtime Type-check runtime packages only' @printf '%s\n' ' make check-js Vite+ fmt/lint + orphan-JS + scripts tests + primordials + global-replace' @printf '%s\n' ' make fix-js Auto-fix JS formatting/lint' - @printf '%s\n' ' make check-md Markdown lint + node-compat matrix drift check' + @printf '%s\n' ' make check-md Markdown fmt + lint + node-compat matrix drift check' @printf '%s\n' ' make fix-md Auto-fix markdown lint' @printf '%s\n' ' make check-actions actionlint over .github/workflows' @printf '%s\n' ' make check-primordials Prototype-pollution ratchet (UPDATE=1 to lower; RAISE=--allow-raise)' @@ -147,7 +147,7 @@ check-js: fix-js: bun run js:fix -# Markdown lint over the repo's own docs (config + rationale for the two disabled rules: +# Markdown format (bun fmt, bunfig.toml) + lint over the repo's own docs (lint config + rationale for the two disabled rules: # .markdownlint-cli2.jsonc). Catches MD040 and friends locally instead of in review. check-md: bun run md:check diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index bb845eb..449a2a8 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -43,8 +43,8 @@ disagree with this file, they win — fix this file. `make check-js` whenever any `.js`/`.mjs`/`.cjs` under `pkg/runtime/js`, `tests`, `scripts`, or `bench` changed — it runs format, lint, orphan-JS detection, and the primordials ratchet. -`make check-md` whenever any `.md` changed — markdownlint over the repo's own docs -(config and the rationale for its two disabled rules: `.markdownlint-cli2.jsonc`). +`make check-md` whenever any `.md` changed — `bun fmt --check` plus markdownlint over the +repo's own docs (lint config and the rationale for its two disabled rules: `.markdownlint-cli2.jsonc`). `make check-actions` whenever any `.github/workflows/*.yml` changed — actionlint catches what a YAML parse cannot: a context used where it is not available, a bad `needs`, shellcheck errors inside `run:`. diff --git a/agents/specialists/gate-runner.md b/agents/specialists/gate-runner.md index 96fc36e..00a7780 100644 --- a/agents/specialists/gate-runner.md +++ b/agents/specialists/gate-runner.md @@ -27,8 +27,8 @@ five that CI does not run. The brief's `phase:` picks one. -- **`compile`** — `make check`; `make check-js` if any JS changed; `make - check-md` if any `.md` changed; `make check-actions` if a workflow changed; +- **`compile`** — `make check`; `make check-js` if any JS changed; + `make check-md` if any `.md` changed; `make check-actions` if a workflow changed; `make build`. Fast, and the conductor waits on it: reviewers are not spawned on code that does not compile. Stop at the first failure of `make check` or `make build`. @@ -55,8 +55,8 @@ Before the first target, compare the toolchain with what CI pins — `node repo has one), `odin version` if the brief names one. On a mismatch: -- Look for the pinned version before saying it is absent: `ls /opt`, `type -a - node`, the version manager on the box. "Not on `PATH`" is not "not +- Look for the pinned version before saying it is absent: `ls /opt`, `type -a node`, + the version manager on the box. "Not on `PATH`" is not "not installed". - Found → run every gate with it first on `PATH` and report `ENV MISMATCH: default node , pinned — ran with from `. diff --git a/bun.lock b/bun.lock index e25c5bc..12f1ded 100644 --- a/bun.lock +++ b/bun.lock @@ -8,7 +8,7 @@ "acorn-walk": "^8.3.5", "actionlint": "^2.0.6", "fast-check": "^4.9.0", - "markdownlint-cli2": "^0.23.2", + "markdownlint-cli2": "^0.24.1", }, }, }, @@ -37,7 +37,7 @@ "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], - "argparse": ["argparse@2.0.1", "", {}, "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q=="], + "argparse": ["argparse@3.0.4", "", {}, "sha512-fwWK3Zq22l4XJJjx5mcBHGikIk+hJqO+32dPm0Jur7+rtG5x1scOjSfdT9ZKeWHPpsya/l0HHK4tDImBkTrtvA=="], "braces": ["braces@3.0.3", "", { "dependencies": { "fill-range": "^7.1.1" } }, "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA=="], @@ -57,7 +57,7 @@ "devlop": ["devlop@1.1.0", "", { "dependencies": { "dequal": "^2.0.0" } }, "sha512-RWmIqhcFf1lRYBvNmr7qTNuyCt/7/ns2jbpp1+PalgE/rDQcBT0fioSMUpJ93irlUhC5hrg4cYqe6U+0ImW0rA=="], - "entities": ["entities@4.5.0", "", {}, "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw=="], + "entities": ["entities@8.1.0", "", {}, "sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA=="], "fast-check": ["fast-check@4.9.0", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-7ms6T7SybUev/PQITciI0yLM2pOSFy5zpG8Ty7tQofcVaQUvrMXp6CBwqF6fThLCLOrfBtuHAtwq6Yu4XPCllg=="], @@ -71,7 +71,7 @@ "glob-parent": ["glob-parent@5.1.2", "", { "dependencies": { "is-glob": "^4.0.1" } }, "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow=="], - "globby": ["globby@16.2.2", "", { "dependencies": { "@sindresorhus/merge-streams": "^4.0.0", "fast-glob": "^3.3.3", "ignore": "^7.0.5", "is-path-inside": "^4.0.0", "slash": "^5.1.0", "unicorn-magic": "^0.4.0" } }, "sha512-NLvV9ubZ6NDsJaOpKPy3cQeJpKi9DcWiyCiFUpJPA0YihRqiE6RWaLUmgNNPr8MgPpLZjnBjSmou7uZBRJv9wA=="], + "globby": ["globby@16.2.4", "", { "dependencies": { "@sindresorhus/merge-streams": "^4.0.0", "fast-glob": "^3.3.3", "ignore": "^7.0.5", "is-path-inside": "^4.0.0", "micromatch": "^4.0.8", "slash": "^5.1.0", "unicorn-magic": "^0.4.0" } }, "sha512-c8B/VNLmxRcmqqenRA9t+9IyOjf9+V6lTxPaUJLqOCONdQkWZ0ETYgX0qbtJqPsgCNusT9MZ5Jeidw8Eb9tn2g=="], "ignore": ["ignore@7.0.6", "", {}, "sha512-BAg6QkE8W+TuQLrrw0Ugr7HegXduRuuj8/ti2kSOc+jz1dmx8/WNcjr6XGnq5YpDWxFwwaavqD0+jIUOKelTsw=="], @@ -91,7 +91,7 @@ "is-path-inside": ["is-path-inside@4.0.0", "", {}, "sha512-lJJV/5dYS+RcL8uQdBDW9c9uWFLLBNRyFhnAKXw5tVqLlKZ4RMGZKv+YQ/IA3OhD+RpbJa1LLFM1FQPGyIXvOA=="], - "js-yaml": ["js-yaml@5.2.2", "", { "dependencies": { "argparse": "^2.0.1" }, "bin": { "js-yaml": "bin/js-yaml.mjs" } }, "sha512-dayzUzKkJ1MkuUtZglSebU43utNXH0OWQByK9rKOOuYIO8M5TV1y+n8ALMdG0rdzBnfNkOmZEqrURepb0ejqBw=="], + "js-yaml": ["js-yaml@5.5.0", "", { "dependencies": { "argparse": "^3.0.3" }, "bin": { "js-yaml": "bin/js-yaml.mjs" } }, "sha512-paNr4LFIFPjeeSmwNYdrb8FafLLG/Cr2Af3GjbsHJ5CxIjH4r0c2emlmRdAtpPAieP1gRVzk6buBbVP5gBMGAg=="], "jsonc-parser": ["jsonc-parser@3.3.1", "", {}, "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ=="], @@ -99,13 +99,13 @@ "katex": ["katex@0.16.47", "", { "dependencies": { "commander": "^8.3.0" }, "bin": { "katex": "cli.js" } }, "sha512-Eeo8Ys1doU1z+x8AZsPpQu+p/QcZBI5PeOo7QGQdy2x2m0MU/hYagBbGOmXwr5KVbEfVuWv9LpnQWeehogurjg=="], - "linkify-it": ["linkify-it@5.0.2", "", { "dependencies": { "uc.micro": "^2.0.0" } }, "sha512-ONTm2jCMAVZjgQa/Fy1kScXsuOoF5NPTsoFBdE1KVIZ2vAh/r9+Bqo+0jINCBYnavTPQZz38QzFTme79ENoN3Q=="], + "linkify-it": ["linkify-it@6.1.0", "", { "dependencies": { "uc.micro": "^3.0.0" } }, "sha512-wJ/TwpSDTLepCrQoYWYIExIKg5Zchex2Nn5yk2mFnB+6PtdkHtyLx742md9csRjjOnGkKIS/RrbY7l8D6gT9Vw=="], - "markdown-it": ["markdown-it@14.3.0", "", { "dependencies": { "argparse": "^2.0.1", "entities": "^4.5.0", "linkify-it": "^5.0.2", "mdurl": "^2.0.0", "punycode.js": "^2.3.1", "uc.micro": "^2.1.0" }, "bin": { "markdown-it": "bin/markdown-it.mjs" } }, "sha512-RCEsPjR+sr0x+AuYp601tKTkgFG4YEPLCzHST3cQ/fhlJkqAkz1L2/Qbp1j9qw5SBwQHFBoW8+hoN5xssOF0Tw=="], + "markdown-it": ["markdown-it@15.0.3", "", { "dependencies": { "argparse": "^3.0.3", "entities": "^8.1.0", "linkify-it": "^6.1.0", "mdurl": "^2.1.0", "punycode.js": "^2.3.1", "uc.micro": "^3.0.0" }, "bin": { "markdown-it": "bin/markdown-it.mjs" } }, "sha512-+Az7Ojz3iRGu8KYMvJ6xVhvrxP3eE+vEtJX75Ow88XBpcQNV/0lGX5ZynIIBftREo4DC2jsveAMDaeltmm8QvQ=="], - "markdownlint": ["markdownlint@0.41.1", "", { "dependencies": { "micromark": "4.0.2", "micromark-core-commonmark": "2.0.3", "micromark-extension-directive": "4.0.0", "micromark-extension-gfm-autolink-literal": "2.1.0", "micromark-extension-gfm-footnote": "2.1.0", "micromark-extension-gfm-table": "2.1.1", "micromark-extension-math": "3.1.0", "micromark-util-types": "2.0.2", "string-width": "8.2.1" } }, "sha512-qHKeU2E1bdyNAT077go2FVTNXvYcktN5IHtF6XyeD1l0PClxzSp2tUApAV14ORI8DGX4H9bNKZEzelZp4qn8IA=="], + "markdownlint": ["markdownlint@0.42.1", "", { "dependencies": { "micromark": "4.0.3", "micromark-core-commonmark": "2.0.4", "micromark-extension-directive": "4.0.0", "micromark-extension-gfm-autolink-literal": "2.1.0", "micromark-extension-gfm-footnote": "2.1.0", "micromark-extension-gfm-table": "2.1.2", "micromark-extension-math": "3.1.0", "micromark-util-types": "2.0.3", "re2js": "2.9.1", "string-width": "8.3.0" } }, "sha512-Otolm9JX12iO2NG5cetlKfzGygpiLSlHuqXKxoT6HGhN5HtbxprJPicKZqqCQd88qOvUi/TEs3o1AmcYioVscA=="], - "markdownlint-cli2": ["markdownlint-cli2@0.23.2", "", { "dependencies": { "globby": "16.2.2", "js-yaml": "5.2.2", "jsonc-parser": "3.3.1", "jsonpointer": "5.0.1", "markdown-it": "14.3.0", "markdownlint": "0.41.1", "markdownlint-cli2-formatter-default": "0.0.6", "micromatch": "4.0.8", "smol-toml": "1.7.0" }, "bin": { "markdownlint-cli2": "markdownlint-cli2-bin.mjs" } }, "sha512-eUhcnkSpzURo/o4htSqc7LPDszgOOTknhU4eY/sPHvMCLxnTCYscv1gw1/js/idmaZPisv9ECVEIORcllqjTUw=="], + "markdownlint-cli2": ["markdownlint-cli2@0.24.1", "", { "dependencies": { "globby": "16.2.4", "js-yaml": "5.5.0", "jsonc-parser": "3.3.1", "jsonpointer": "5.0.1", "markdown-it": "15.0.3", "markdownlint": "0.42.1", "markdownlint-cli2-formatter-default": "0.0.6", "micromatch": "4.0.8", "smol-toml": "1.9.1" }, "bin": { "markdownlint-cli2": "markdownlint-cli2-bin.mjs" } }, "sha512-nLhes/MIPJQLm+Gmh+3jQEnwMOaalaN0wfCD2qz1247zNZ7R3r2WDHJB191x4hcy6soKAUTybp1bFK7aJGw/WA=="], "markdownlint-cli2-formatter-default": ["markdownlint-cli2-formatter-default@0.0.6", "", { "peerDependencies": { "markdownlint-cli2": ">=0.0.4" } }, "sha512-VVDGKsq9sgzu378swJ0fcHfSicUnMxnL8gnLm/Q4J/xsNJ4e5bA6lvAz7PCzIl0/No0lHyaWdqVD2jotxOSFMQ=="], @@ -113,9 +113,9 @@ "merge2": ["merge2@1.4.1", "", {}, "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg=="], - "micromark": ["micromark@4.0.2", "", { "dependencies": { "@types/debug": "^4.0.0", "debug": "^4.0.0", "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-core-commonmark": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-combine-extensions": "^2.0.0", "micromark-util-decode-numeric-character-reference": "^2.0.0", "micromark-util-encode": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-sanitize-uri": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-zpe98Q6kvavpCr1NPVSCMebCKfD7CA2NqZ+rykeNhONIJBpc1tFKt9hucLGwha3jNTNI8lHpctWJWoimVF4PfA=="], + "micromark": ["micromark@4.0.3", "", { "dependencies": { "@types/debug": "^4.0.0", "debug": "^4.0.0", "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-core-commonmark": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-combine-extensions": "^2.0.0", "micromark-util-decode-numeric-character-reference": "^2.0.0", "micromark-util-edit-map": "^1.0.0", "micromark-util-encode": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-sanitize-uri": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-oGYfQzHSG5dOMovQcJ3fyTmZlWAWpi0XA0sJwJs+i6OT88o1+Jtw/8z0CmdowSLxGhhFK89rQ/oXph/wN02PNw=="], - "micromark-core-commonmark": ["micromark-core-commonmark@2.0.3", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-factory-destination": "^2.0.0", "micromark-factory-label": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-factory-title": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-html-tag-name": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg=="], + "micromark-core-commonmark": ["micromark-core-commonmark@2.0.4", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-factory-destination": "^2.0.0", "micromark-factory-label": "^2.0.0", "micromark-factory-space": "^2.1.0", "micromark-factory-title": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-edit-map": "^1.0.0", "micromark-util-html-tag-name": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.3" } }, "sha512-wxEeE8v8XVvOrxn1TZj74qYhAtTQsSqufHVS3uNVyT1MupXxhyaHk8/9xDNPh9BmL77QNDgWTkZ3RsYHB2ry7w=="], "micromark-extension-directive": ["micromark-extension-directive@4.0.0", "", { "dependencies": { "devlop": "^1.0.0", "micromark-factory-space": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0", "parse-entities": "^4.0.0" } }, "sha512-/C2nqVmXXmiseSSuCdItCMho7ybwwop6RrrRPk0KbOHW21JKoCldC+8rFOaundDoRBUWBnJJcxeA/Kvi34WQXg=="], @@ -123,7 +123,7 @@ "micromark-extension-gfm-footnote": ["micromark-extension-gfm-footnote@2.1.0", "", { "dependencies": { "devlop": "^1.0.0", "micromark-core-commonmark": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-sanitize-uri": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-/yPhxI1ntnDNsiHtzLKYnE3vf9JZ6cAisqVDauhp4CEHxlb4uoOTxOCJ+9s51bIB8U1N1FJ1RXOKTIlD5B/gqw=="], - "micromark-extension-gfm-table": ["micromark-extension-gfm-table@2.1.1", "", { "dependencies": { "devlop": "^1.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-t2OU/dXXioARrC6yWfJ4hqB7rct14e8f7m0cbI5hUmDyyIlwv5vEtooptH8INkbLzOatzKuVbQmAYcbWoyz6Dg=="], + "micromark-extension-gfm-table": ["micromark-extension-gfm-table@2.1.2", "", { "dependencies": { "devlop": "^1.0.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-pRzm4kDTu0MjlmBkxmS9yYhw60nncfcEwu9NNdPFSQEFXS95ZKyIIyTSHu/o3ReBUrLKYEq+7YaXCRn/bPB4MA=="], "micromark-extension-math": ["micromark-extension-math@3.1.0", "", { "dependencies": { "@types/katex": "^0.16.0", "devlop": "^1.0.0", "katex": "^0.16.0", "micromark-factory-space": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-lvEqd+fHjATVs+2v/8kg9i5Q0AP2k85H0WUOwpIVvUML8BapsMvh1XAogmQjOCsLpoKRCVQqEkQBB3NhVBcsOg=="], @@ -147,6 +147,8 @@ "micromark-util-decode-numeric-character-reference": ["micromark-util-decode-numeric-character-reference@2.0.2", "", { "dependencies": { "micromark-util-symbol": "^2.0.0" } }, "sha512-ccUbYk6CwVdkmCQMyr64dXz42EfHGkPQlBj5p7YVGzq8I7CtjXZJrubAYezf7Rp+bjPseiROqe7G6foFd+lEuw=="], + "micromark-util-edit-map": ["micromark-util-edit-map@1.0.0", "", { "dependencies": { "micromark-util-types": "^2.0.0" } }, "sha512-Pa2ljlsEL6sVwFaYeyrOLSYbQt73JvGbPOYFq+9AElXiSnfI5Q4465RRZ1sHv7lDjDOnGRDDaqPXqClqWK/q1Q=="], + "micromark-util-encode": ["micromark-util-encode@2.0.1", "", {}, "sha512-c3cVx2y4KqUnwopcO9b/SCdo2O67LwJJ/UyqGfbigahfegL9myoEFoDYZgkT7f36T0bLrM9hZTAaAyH+PCAXjw=="], "micromark-util-html-tag-name": ["micromark-util-html-tag-name@2.0.1", "", {}, "sha512-2cNEiYDhCWKI+Gs9T0Tiysk136SnR13hhO8yW6BGNyhOC4qYFnwF1nKfD3HFAIXA5c45RrIG1ub11GiXeYd1xA=="], @@ -161,7 +163,7 @@ "micromark-util-symbol": ["micromark-util-symbol@2.0.1", "", {}, "sha512-vs5t8Apaud9N28kgCrRUdEed4UJ+wWNvicHLPxCa9ENlYuAY31M0ETy5y1vA33YoNPDFTghEbnh6efaE8h4x0Q=="], - "micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + "micromark-util-types": ["micromark-util-types@2.0.3", "", {}, "sha512-oxB2Ik03hI0gv+VNn9tnh1t1YEe9MDPptViAEgfdf3YQHsn0pzGTgCdlSCJXcwhqm8phaEuM7zeEu3QQzVBrPg=="], "micromatch": ["micromatch@4.0.8", "", { "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" } }, "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA=="], @@ -177,22 +179,66 @@ "queue-microtask": ["queue-microtask@1.2.3", "", {}, "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A=="], + "re2js": ["re2js@2.9.1", "", {}, "sha512-JIT3bEEUZTOR2YS1walNEIfH4cF/cQeZcBUFq7ldqO6lR6g0X5CBq/S8rsa9omzZcsNf8qc36jCkWpKKtncrZg=="], + "reusify": ["reusify@1.1.0", "", {}, "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw=="], "run-parallel": ["run-parallel@1.2.0", "", { "dependencies": { "queue-microtask": "^1.2.2" } }, "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA=="], "slash": ["slash@5.1.0", "", {}, "sha512-ZA6oR3T/pEyuqwMgAKT0/hAv8oAXckzbkmR0UkUosQ+Mc4RxGoJkRmwHgHufaenlyAgE1Mxgpdcrf75y6XcnDg=="], - "smol-toml": ["smol-toml@1.7.0", "", {}, "sha512-aqVvWoyO21L23mb+drl4RmMXbf6N7FdHjAhTRA9ZBL7apWBgfWC16KjrASI+1p9GAroljyMHj6fK67i0UiTNvQ=="], + "smol-toml": ["smol-toml@1.9.1", "", {}, "sha512-j7WyPdf0CJoutMzMoPLF/bVXHtKZIT9Rb4eGeEbWw8X2t5oirjPgpL+OpaENZrILYaXqYwnVH+qgUwVbl+pUJQ=="], - "string-width": ["string-width@8.2.1", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-IIaP0g3iy9Cyy18w3M9YcaDudujEAVHKt3a3QJg1+sr/oX96TbaGUubG0hJyCjCBThFH+tFpcIyoUHUn1ogaLA=="], + "string-width": ["string-width@8.3.0", "", { "dependencies": { "get-east-asian-width": "^1.5.0", "strip-ansi": "^7.1.2" } }, "sha512-ZbmZM0JCihQN91dWnxoipT2KOEyHqEyfRXUyjuRhW8b/xnqPDoq4gWEVApTVa9db2wN8mmoikgFBbjh71+cGeQ=="], "strip-ansi": ["strip-ansi@7.2.0", "", { "dependencies": { "ansi-regex": "^6.2.2" } }, "sha512-yDPMNjp4WyfYBkHnjIRLfca1i6KMyGCtsVgoKe/z1+6vukgaENdgGBZt+ZmKPc4gavvEZ5OgHfHdrazhgNyG7w=="], "to-regex-range": ["to-regex-range@5.0.1", "", { "dependencies": { "is-number": "^7.0.0" } }, "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ=="], - "uc.micro": ["uc.micro@2.1.0", "", {}, "sha512-ARDJmphmdvUk6Glw7y9DQ2bFkKBHwQHLi2lsaH6PPmz/Ka9sFOBsBluozhDltWmnv9u/cF6Rt87znRTPV+yp/A=="], + "uc.micro": ["uc.micro@3.0.0", "", {}, "sha512-U3PppEkleoTnIfi8BozMx3yju3qc/L6SwqWo2Sw+54PX+PX0q9I+r1Um5HCmqD7n9VDX5/v3vQH/AjA6deDdtw=="], "unicorn-magic": ["unicorn-magic@0.4.0", "", {}, "sha512-wH590V9VNgYH9g3lH9wWjTrUoKsjLF6sGLjhR4sH1LWpLmCOH0Zf7PukhDA8BiS7KHe4oPNkcTHqYkj7SOGUOw=="], + + "micromark/micromark-core-commonmark": ["micromark-core-commonmark@2.0.3", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-factory-destination": "^2.0.0", "micromark-factory-label": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-factory-title": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-html-tag-name": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg=="], + + "micromark/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-core-commonmark/micromark-factory-space": ["micromark-factory-space@2.1.0", "", { "dependencies": { "micromark-util-character": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-fS8hnLIjnjvdQIj39Geug8wWsR0HrYZ43KShKxNfwT7t2LOHo/LbWZEzEaSOjwtjdCsjoE6syHhonEzW0zv0+Q=="], + + "micromark-extension-directive/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-extension-gfm-autolink-literal/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-extension-gfm-footnote/micromark-core-commonmark": ["micromark-core-commonmark@2.0.3", "", { "dependencies": { "decode-named-character-reference": "^1.0.0", "devlop": "^1.0.0", "micromark-factory-destination": "^2.0.0", "micromark-factory-label": "^2.0.0", "micromark-factory-space": "^2.0.0", "micromark-factory-title": "^2.0.0", "micromark-factory-whitespace": "^2.0.0", "micromark-util-character": "^2.0.0", "micromark-util-chunked": "^2.0.0", "micromark-util-classify-character": "^2.0.0", "micromark-util-html-tag-name": "^2.0.0", "micromark-util-normalize-identifier": "^2.0.0", "micromark-util-resolve-all": "^2.0.0", "micromark-util-subtokenize": "^2.0.0", "micromark-util-symbol": "^2.0.0", "micromark-util-types": "^2.0.0" } }, "sha512-RDBrHEMSxVFLg6xvnXmb1Ayr2WzLAWjeSATAoxwKYJV94TeNavgoIdA0a9ytzDSVzBy2YKFK+emCPOEibLeCrg=="], + + "micromark-extension-gfm-footnote/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-extension-gfm-table/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-extension-math/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-factory-destination/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-factory-label/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-factory-space/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-factory-title/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-factory-whitespace/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-character/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-classify-character/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-combine-extensions/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-edit-map/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-resolve-all/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-util-subtokenize/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], + + "micromark-core-commonmark/micromark-factory-space/micromark-util-types": ["micromark-util-types@2.0.2", "", {}, "sha512-Yw0ECSpJoViF1qTU4DC6NwtC4aWGt1EkzaQB8KPPyCRR8z9TWeV0HbEFGTO+ZY1wB22zmxnJqhPyTpOVCpeHTA=="], } } diff --git a/bunfig.toml b/bunfig.toml index 148ae9c..cca2cda 100644 --- a/bunfig.toml +++ b/bunfig.toml @@ -1,10 +1,22 @@ -# `bun fmt` (make check-js / fix-js). Replaces vite-plus's `vp fmt` (oxfmt); the -# style is the one the tree was already formatted in: oxfmt's 100-column width, -# semicolons, single quotes. There is no separate linter: `bun fmt --check` is the -# style gate, and the correctness checks are the repo's own (primordials ratchet, -# orphan-JS, global-replace) plus the oracle suites. +# `bun fmt` (make check-js / fix-js; markdown via make check-md / fix-md). Replaces +# vite-plus's `vp fmt` (oxfmt); the style is the one the tree was already formatted in: +# oxfmt's 100-column width, semicolons, single quotes. There is no separate JS linter: +# `bun fmt --check` is the style gate, and the correctness checks are the repo's own +# (primordials ratchet, orphan-JS, global-replace) plus the oracle suites. [fmt] printWidth = 100 semi = true singleQuote = true -ignorePatterns = ["bin/", "build/", "node_modules/", "reports/", "tests/vendor/"] +ignorePatterns = [ + "bin/", + "build/", + "node_modules/", + "reports/", + "tests/vendor/", + ".deps/", + # Vendored upstream Node docs: not ours to reformat (markdownlint ignores them too). + "reference/node-doc-api/", + # Generated by scripts/node-compat/gen-matrix.py, which `make check-md` diffs + # byte-for-byte; reformatting it would fail that drift check. + "reference/node-compatibility.md", +] diff --git a/package.json b/package.json index a38d79b..83eab81 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "js:primordials": "bun run scripts/check-primordials.mjs", "js:globalreplace": "bun run scripts/check-global-replace.mjs", "actions:check": "bun run scripts/check-actions.mjs", - "md:check": "markdownlint-cli2", - "md:fix": "markdownlint-cli2 --fix", + "md:check": "bun fmt --check '**/*.md' && markdownlint-cli2", + "md:fix": "bun fmt '**/*.md' && markdownlint-cli2 --fix", "js:scripttests": "node --test 'scripts/**/*.test.mjs' 'runtime/**/*.test.mjs'" }, "devDependencies": { @@ -16,7 +16,7 @@ "acorn-walk": "^8.3.5", "actionlint": "^2.0.6", "fast-check": "^4.9.0", - "markdownlint-cli2": "^0.23.2" + "markdownlint-cli2": "^0.24.1" }, "packageManager": "bun@1.4.4" } From 2b474ea1a987759f91e0f8329c6d6508e5a3955f Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:28:50 +0200 Subject: [PATCH 11/61] fix(compat): drop SlowBuffer, fail closed on a non-numeric oracle major, port pool cases to node 26 - node:buffer no longer exports SlowBuffer (removed in Node 26); pinned in tests/node-compat/cases/03-buffer.js and a manifest mutation. - bun-buffer ported cases stop calling SlowBuffer and assert the pool backing is at least Buffer.poolSize (Node 26 allocates poolSize + 64). - scripts/lib/compare.sh refuses an .nvmrc with no numeric major and treats a non-integer oracle major as 0, instead of `[ -lt ]` erroring into a pass. - Docs: README floor and oracle-test-auditor advisory rule follow .nvmrc. Co-Authored-By: Claude Opus 5.5 --- .claude/agents/oracle-test-auditor.md | 2 +- README.md | 2 +- agents/specialists/oracle-test-auditor.md | 2 +- pkg/runtime/buffer_utf8.odin | 2 +- pkg/runtime/js/internal/buffer.js | 9 +---- runtime/gates/compare-integrity.test.mjs | 36 +++++++++++++++++-- scripts/lib/compare.sh | 10 ++++++ tests/mutation-manifest.json | 29 +++++++++++++++ .../ported/buffer-allocunsafe-large.js | 18 ++-------- .../bun-buffer/ported/buffer-pool.js | 12 ++++--- tests/node-compat/cases/03-buffer.js | 2 ++ 11 files changed, 89 insertions(+), 35 deletions(-) diff --git a/.claude/agents/oracle-test-auditor.md b/.claude/agents/oracle-test-auditor.md index 464ea9c..19ed03b 100644 --- a/.claude/agents/oracle-test-auditor.md +++ b/.claude/agents/oracle-test-auditor.md @@ -26,7 +26,7 @@ or tooling change), say so, skip Part 1, and do Part 2 only. 2. Pin the oracle before trusting it. A diff against an unknown runtime is not evidence: the target is Node 26+ and CI runs the `.nvmrc` pin. Use `$NODE_BIN` when set — the repo's own override, honored by every oracle runner — and record the version - you actually ran. A major outside 22/24 makes the result advisory, not proof. + you actually ran. A major below the `.nvmrc` floor makes the result advisory, not proof. 3. For each surface, write a small script and run it under both: ```sh diff --git a/README.md b/README.md index b2aaad7..b25ce5f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Odin. The runtime target is JavaScriptCore (JSC). This repository is initialized with a small Odin CLI and the first package boundaries for the runtime, package manager, bundler, and JSC integration. -Compatibility targets modern Node.js 22+ behavior rather than legacy Node APIs. +Compatibility targets modern Node.js 26+ behavior (floor pinned in `.nvmrc`) rather than legacy Node APIs. ## Requirements diff --git a/agents/specialists/oracle-test-auditor.md b/agents/specialists/oracle-test-auditor.md index 464ea9c..19ed03b 100644 --- a/agents/specialists/oracle-test-auditor.md +++ b/agents/specialists/oracle-test-auditor.md @@ -26,7 +26,7 @@ or tooling change), say so, skip Part 1, and do Part 2 only. 2. Pin the oracle before trusting it. A diff against an unknown runtime is not evidence: the target is Node 26+ and CI runs the `.nvmrc` pin. Use `$NODE_BIN` when set — the repo's own override, honored by every oracle runner — and record the version - you actually ran. A major outside 22/24 makes the result advisory, not proof. + you actually ran. A major below the `.nvmrc` floor makes the result advisory, not proof. 3. For each surface, write a small script and run it under both: ```sh diff --git a/pkg/runtime/buffer_utf8.odin b/pkg/runtime/buffer_utf8.odin index 38a05e7..d98f8ba 100644 --- a/pkg/runtime/buffer_utf8.odin +++ b/pkg/runtime/buffer_utf8.odin @@ -178,7 +178,7 @@ buffer_utf8_decode_cb :: proc "c" ( // allocUninit(size) -> Uint8Array | null. Hands back `size` bytes of native, // *uninitialized* memory (Node's allocUnsafe semantics) as a NoCopy Uint8Array // that the JS layer wraps as a Buffer view. Only the unpooled unsafe paths reach -// here (large Buffer.allocUnsafe, Buffer.allocUnsafeSlow, SlowBuffer); the small +// here (large Buffer.allocUnsafe, Buffer.allocUnsafeSlow); the small // pooled path stays in JS. Returns null for a non-positive size or an allocation // failure, so the JS layer falls back to a zero-filled own-backing Buffer (which // throws for an impossible size, matching Node). The size is already validated by diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 6a0b314..4ab8d1e 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -1071,7 +1071,7 @@ } /** - * Unpooled uninitialized allocation (allocUnsafe large path / SlowBuffer). + * Unpooled uninitialized allocation (allocUnsafe large path / allocUnsafeSlow). * @param {number} size * @returns {Buffer} */ @@ -1327,12 +1327,6 @@ createPool(); - /** @deprecated Use Buffer.allocUnsafeSlow. */ - function SlowBuffer(size) { - assertSize(size); - return allocUnsafeNoZero(size); - } - /** Base64 → latin1 (Node global atob polyfill surface). */ function atob(data) { return Buffer.from(StringG(data), 'base64').toString('latin1'); @@ -1621,7 +1615,6 @@ var exported = { Buffer: Buffer, - SlowBuffer: SlowBuffer, Blob: Blob, File: File, kMaxLength: K_MAX_LENGTH, diff --git a/runtime/gates/compare-integrity.test.mjs b/runtime/gates/compare-integrity.test.mjs index 2bedf61..b143598 100644 --- a/runtime/gates/compare-integrity.test.mjs +++ b/runtime/gates/compare-integrity.test.mjs @@ -18,6 +18,8 @@ mkdirSync(PIN_BIN_DIR); const NODE_PIN = join(PIN_BIN_DIR, 'node'); const NODE_OLD = join(SCRATCH, 'node-old'); const NODE_NEW = join(SCRATCH, 'node-new'); +const NODE_JUNK = join(SCRATCH, 'node-junk'); +const BAD_PIN_ROOT = join(SCRATCH, 'bad-pin-root'); const FAKE_LAVA = join(SCRATCH, 'fake-lava'); const HANG_CASE = join(SCRATCH, 'hang.js'); const OK_CASE = join(SCRATCH, 'ok.js'); @@ -54,6 +56,20 @@ writeFileSync( { mode: 0o755 }, ); +// A NODE_BIN whose `-p` prints no integer: `[ x -lt N ]` errors, `if` reads the +// error as false, and the floor check used to pass it. +writeFileSync( + NODE_JUNK, + `#!/usr/bin/env sh +[ "\${1:-}" = "-p" ] && echo banana || echo vbanana +`, + { mode: 0o755 }, +); + +// A pin with no digits (`lts/*`) must refuse rather than compare against "". +mkdirSync(BAD_PIN_ROOT); +writeFileSync(join(BAD_PIN_ROOT, '.nvmrc'), 'lts/*\n'); + writeFileSync( FAKE_LAVA, `#!/usr/bin/env sh @@ -74,11 +90,11 @@ after(() => { rmSync(SCRATCH, { recursive: true, force: true }); }); -function sourceCompare(env, body = ':') { +function sourceCompare(env, body = ':', root = ROOT) { const pathEnv = `${PIN_BIN_DIR}:${process.env.PATH || ''}`; return spawnSync( 'sh', - ['-c', `ROOT_DIR="${ROOT}" TMP_DIR="${SCRATCH}" . "${ROOT}/scripts/lib/compare.sh"\n${body}`], + ['-c', `ROOT_DIR="${root}" TMP_DIR="${SCRATCH}" . "${ROOT}/scripts/lib/compare.sh"\n${body}`], { cwd: ROOT, encoding: 'utf8', @@ -131,6 +147,22 @@ test('compare.sh refuses a NODE_BIN older than the .nvmrc major (#185)', () => { ); }); +test('compare.sh refuses a NODE_BIN whose major is not an integer (#185)', () => { + const r = sourceCompare({ NODE_BIN: NODE_JUNK, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }); + assert.equal(r.status, 2, r.stderr || r.stdout); + assert.match(r.stderr || '', new RegExp(`oracle requires node >= ${PIN} \\(\\.nvmrc\\)`)); +}); + +test('compare.sh refuses when .nvmrc carries no numeric major (#185)', () => { + const r = sourceCompare( + { NODE_BIN: NODE_PIN, LAVA_BIN: FAKE_LAVA, RUN_LAVA: '1' }, + ':', + BAD_PIN_ROOT, + ); + assert.equal(r.status, 2, r.stderr || r.stdout); + assert.match(r.stderr || '', /\.nvmrc has no numeric Node major/); +}); + test('compare.sh accepts a NODE_BIN newer than the .nvmrc major', () => { // The pin is a floor: the product tracks Node forward (26 now, 28 later), so a // newer oracle is allowed to run and any drift shows up as a case diff. diff --git a/scripts/lib/compare.sh b/scripts/lib/compare.sh index 14f21c7..32e3c4b 100644 --- a/scripts/lib/compare.sh +++ b/scripts/lib/compare.sh @@ -51,7 +51,17 @@ _lava_gate_integrity_check || exit $? # 26 where 24 threw, so its expectations are wrong for us. Fail before any case. _lava_node_major_check() { _want=$(cut -d. -f1 "$ROOT_DIR/.nvmrc" | tr -dc '0-9') + # `[ x -lt N ]` on a non-integer operand errors, and `if` reads that error as + # false — a pass. So a pin with no digits (`lts/*`, empty) refuses outright, + # and an oracle whose major is not all digits counts as 0, i.e. too old. + if [ -z "$_want" ]; then + printf '%s\n' "oracle check: .nvmrc has no numeric Node major ($ROOT_DIR/.nvmrc)" >&2 + return 2 + fi _got=$("$NODE_BIN" -p 'process.versions.node.split(".")[0]' 2>/dev/null) || _got=0 + case "$_got" in + '' | *[!0-9]*) _got=0 ;; + esac if [ "$_got" -lt "$_want" ]; then printf '%s\n' "oracle requires node >= $_want (.nvmrc), found $("$NODE_BIN" --version 2>/dev/null || echo none) ($NODE_BIN)" >&2 return 2 diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 16b3cb5..d2c177c 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1053,6 +1053,26 @@ "test": "compare.sh refuses a NODE_BIN older than the .nvmrc major (#185)", "expect_detail": "Expected values to be strictly equal" }, + { + "name": "compare.sh dies when a non-numeric oracle major is not coerced to 0", + "why": "`[ banana -lt 26 ]` errors and `if` reads the error as false, so a NODE_BIN printing no integer major passed the floor.", + "source": "scripts/lib/compare.sh", + "find": "\tcase \"$_got\" in\n\t'' | *[!0-9]*) _got=0 ;;\n\tesac", + "replace": "\t:", + "gate": "node-test:runtime/gates/compare-integrity.test.mjs", + "test": "compare.sh refuses a NODE_BIN whose major is not an integer (#185)", + "expect_detail": "compare.sh refuses a NODE_BIN whose major is not an integer (#185)" + }, + { + "name": "compare.sh dies when an .nvmrc with no numeric major is not refused", + "why": "An empty or `lts/*` pin leaves the floor empty, `[ N -lt '' ]` errors to false, and every oracle passes.", + "source": "scripts/lib/compare.sh", + "find": "\tif [ -z \"$_want\" ]; then", + "replace": "\tif false; then", + "gate": "node-test:runtime/gates/compare-integrity.test.mjs", + "test": "compare.sh refuses when .nvmrc carries no numeric major (#185)", + "expect_detail": "compare.sh refuses when .nvmrc carries no numeric major (#185)" + }, { "name": "compare.sh dies when compare_case ignores a node per-case timeout", "why": "A case that hangs both Node and Lava exits 124 on both sides with empty output, which compare_case would pass as 124 == 124 without the explicit timeout check (#399).", @@ -1659,6 +1679,15 @@ "The \"size\" argument must be number" ] }, + { + "name": "03-buffer dies when node:buffer exports SlowBuffer again", + "why": "Node 26 removed SlowBuffer (DEP0030 end-of-life); `'SlowBuffer' in require('buffer')` is false there.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " Buffer: Buffer,\n Blob: Blob,", + "replace": " Buffer: Buffer,\n SlowBuffer: Buffer,\n Blob: Blob,", + "gate": "compat:tests/node-compat/cases/03-buffer.js", + "expect_detail": "node:buffer has no SlowBuffer" + }, { "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", diff --git a/tests/node-compat/bun-buffer/ported/buffer-allocunsafe-large.js b/tests/node-compat/bun-buffer/ported/buffer-allocunsafe-large.js index f9c669c..0ccdf42 100644 --- a/tests/node-compat/bun-buffer/ported/buffer-allocunsafe-large.js +++ b/tests/node-compat/bun-buffer/ported/buffer-allocunsafe-large.js @@ -1,6 +1,6 @@ // Large / unpooled unsafe allocation (issue #212). Buffer.allocUnsafe above the -// pool threshold, Buffer.allocUnsafeSlow, and SlowBuffer get their own backing -// store. Node leaves those bytes uninitialized (its allocator skips the +// pool threshold and Buffer.allocUnsafeSlow get their own backing store (Node 26 +// removed SlowBuffer, so it is no longer exercised here). Node leaves those bytes uninitialized (its allocator skips the // zero-fill); Lava now backs them with a native uninitialized allocation instead // of a zero-filled `new Buffer(size)`. // @@ -13,9 +13,7 @@ // stale bytes, that Buffer.alloc stays fully zero-filled, and that an impossible // size still throws RangeError. These are pure ArrayBuffer/Uint8Array semantics // and so behave identically on Linux, macOS/Darwin, and Windows. -process.noDeprecation = true; // silence Node's DEP0030 for SlowBuffer() (no-op on Lava) const assert = require('node:assert/strict'); -const { SlowBuffer } = require('node:buffer'); const HALF = Buffer.poolSize >>> 1; // exactly the pool threshold (not pooled) const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSize @@ -95,17 +93,6 @@ for (const size of [1, 8, HALF, 70000]) { assert.equal(s[size - 1], 0x5a); } -// --- SlowBuffer: same unpooled path as allocUnsafeSlow ---------------------- -{ - const sb = SlowBuffer(100); - assert.ok(sb instanceof Buffer, 'SlowBuffer returns a Buffer'); - assert.equal(sb.length, 100); - assert.equal(sb.byteOffset, 0, 'SlowBuffer is unpooled'); - assert.equal(sb.buffer.byteLength, 100, 'SlowBuffer owns a right-sized store'); - sb.writeUInt32LE(0x01020304, 0); - assert.equal(sb.readUInt32LE(0), 0x01020304, 'SlowBuffer round-trips writes'); -} - // --- Copy paths over a large (native-backed) result never leak stale bytes --- // Buffer.from must reflect exactly the source bytes, and Buffer.concat must // zero-fill any tail an explicit length leaves uncovered, even though the @@ -148,7 +135,6 @@ for (const size of [1, 8, HALF, 70000]) { for (const fn of [ () => Buffer.allocUnsafe(MAX), () => Buffer.allocUnsafeSlow(MAX), - () => SlowBuffer(MAX), () => Buffer.allocUnsafe(2 ** 48), ]) { assert.throws(fn, RangeError, 'impossible size throws RangeError'); diff --git a/tests/node-compat/bun-buffer/ported/buffer-pool.js b/tests/node-compat/bun-buffer/ported/buffer-pool.js index 3b1b861..a56d690 100644 --- a/tests/node-compat/bun-buffer/ported/buffer-pool.js +++ b/tests/node-compat/bun-buffer/ported/buffer-pool.js @@ -29,7 +29,9 @@ const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSi let sawShared = false; let prev = Buffer.allocUnsafe(8); assert.equal(prev.byteOffset % 8, 0, 'pooled byteOffset is 8-aligned'); - assert.equal(prev.buffer.byteLength, Buffer.poolSize, 'pooled buffer backs onto the pool'); + // Node 26 backs the pool with poolSize + 64 bytes (65600 for 65536), so only + // "at least poolSize" is a guarantee both runtimes share. + assert.ok(prev.buffer.byteLength >= Buffer.poolSize, 'pooled buffer backs onto the pool'); for (let i = 0; i < 16; i++) { const cur = Buffer.allocUnsafe(8); assert.equal(cur.length, 8); @@ -91,7 +93,7 @@ const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSi assert.notEqual(z.buffer, pooled.buffer, 'alloc() does not draw from the unsafe pool'); } -// --- allocUnsafeSlow / SlowBuffer are unpooled ----------------------------- +// --- allocUnsafeSlow is unpooled ------------------------------------------- { const slow = Buffer.allocUnsafeSlow(8); assert.equal(slow.byteOffset, 0); @@ -108,7 +110,7 @@ const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSi assert.equal(cur.toString(), 'cd', 'pooled from(string) holds its own bytes'); if (cur.buffer === prev.buffer) { sawShared = true; - assert.equal(cur.buffer.byteLength, Buffer.poolSize); + assert.ok(cur.buffer.byteLength >= Buffer.poolSize); } prev = cur; } @@ -180,7 +182,7 @@ const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSi let buf = Buffer.allocUnsafe(8); for (let i = 0; i < 16 && buf.byteOffset === 0; i++) buf = Buffer.allocUnsafe(8); assert.ok(buf.byteOffset > 0, 'have a pooled buffer at a non-zero offset to test'); - assert.equal(buf.buffer.byteLength, Buffer.poolSize, 'and it is pool-backed'); + assert.ok(buf.buffer.byteLength >= Buffer.poolSize, 'and it is pool-backed'); const pattern = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04]; for (let i = 0; i < 8; i++) buf[i] = pattern[i]; @@ -204,7 +206,7 @@ const BIG = Buffer.poolSize + 4096; // comfortably above the pool, and != poolSi { const small = Buffer.concat([Buffer.from([1, 2]), Buffer.from([3, 4])]); assert.deepEqual([...small], [1, 2, 3, 4], 'concat copies the inputs'); - assert.equal(small.buffer.byteLength, Buffer.poolSize, 'small concat result is pooled'); + assert.ok(small.buffer.byteLength >= Buffer.poolSize, 'small concat result is pooled'); // An explicit totalLength larger than the data zero-fills the remainder. const padded = Buffer.concat([Buffer.from([0xaa, 0xbb])], 6); diff --git a/tests/node-compat/cases/03-buffer.js b/tests/node-compat/cases/03-buffer.js index 19a4f4e..332fbfb 100644 --- a/tests/node-compat/cases/03-buffer.js +++ b/tests/node-compat/cases/03-buffer.js @@ -46,3 +46,5 @@ assert.equal(Buffer.copyBytesFrom(new Uint16Array([0x1234])).toString('hex'), '3 assert.equal(require('node:buffer').atob('Zm9v'), 'foo'); assert.equal(require('node:buffer').btoa('foo'), 'Zm9v'); assert.equal(require('node:buffer').isAscii(Buffer.from('abc')), true); +// Node 26 removed SlowBuffer (DEP0030 end-of-life): the export is gone, not deprecated. +assert.equal('SlowBuffer' in require('node:buffer'), false, 'node:buffer has no SlowBuffer'); From 138a55174d1b5aef48c589c8c6e0ad5fa0429988 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:33:51 +0200 Subject: [PATCH 12/61] docs(compat): drop SlowBuffer from the coverage matrix Co-Authored-By: Claude Opus 5.5 --- reference/node-compat.json | 2 +- reference/node-compatibility.md | 8 ++++---- scripts/node-compat/gen-matrix.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/reference/node-compat.json b/reference/node-compat.json index 1b55c9c..6794a86 100644 --- a/reference/node-compat.json +++ b/reference/node-compat.json @@ -44,7 +44,7 @@ { "page": "buffer", "status": "full", - "implemented": "Buffer/Blob/File/SlowBuffer/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL)", + "implemented": "Buffer/Blob/File/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL)", "gaps": "atob/btoa are not installed as globals (#86); exports a lava-only describeType" }, { diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index b0d29a4..6226db1 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -45,7 +45,7 @@ requireable module in lava (the surface is global-only, or absent). | **assert** | `node:assert` | ✅ | node:assert + assert/strict, AssertionError, partialDeepStrictEqual | Assert class, CallTracker; deepStrictEqual divergences (#103) | | **async_context** | ❌ | 🟥 | — | AsyncLocalStorage | | **async_hooks** | ❌ | 🟥 | — | — | -| **buffer** | `node:buffer` | ✅ | Buffer/Blob/File/SlowBuffer/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL) | atob/btoa are not installed as globals (#86); exports a lava-only describeType | +| **buffer** | `node:buffer` | ✅ | Buffer/Blob/File/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL) | atob/btoa are not installed as globals (#86); exports a lava-only describeType | | **child_process** | ❌ | 🟥 | — | — | | **cluster** | ❌ | 🟥 | — | — | | **console** | `node:console` | 🟡 | global console + node:console module with a Console class | context/createTask/profile/profileEnd/timeStamp; Console ignores its stdout/stderr streams (#104) | @@ -161,13 +161,13 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. - **Encodings:** `utf8`/`utf-8`, `utf16le`/`ucs2`, `latin1`/`binary`, `ascii`, `hex`, `base64`, `base64url` (with Node's lenient base64 normalization and `ascii` high-bit masking). - **Errors:** Node `err.code`s and message shapes for `ERR_INVALID_ARG_TYPE`, `ERR_INVALID_ARG_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_BUFFER_OUT_OF_BOUNDS`, `ERR_UNKNOWN_ENCODING`, `ERR_INVALID_BUFFER_SIZE`. - **Rendering:** `util.inspect`/`console.log` emit `` (honoring `INSPECT_MAX_BYTES`) via the `nodejs.util.inspect.custom` hook. -- **Module extras:** `Blob`, `File`, `SlowBuffer`, `atob`/`btoa`, `isAscii`, `isUtf8`, `transcode`, `resolveObjectURL`, `kMaxLength`/`kStringMaxLength`/`constants`/`INSPECT_MAX_BYTES`. `Blob`/`File` honor the `endings: 'native'` option (string parts have `\n`/`\r\n` converted to the platform newline — derived from `process.platform` — while a lone `\r` and binary parts are left verbatim, matching Node). +- **Module extras:** (no `SlowBuffer`: Node 26 removed it, DEP0030 end-of-life) `Blob`, `File`, `atob`/`btoa`, `isAscii`, `isUtf8`, `transcode`, `resolveObjectURL`, `kMaxLength`/`kStringMaxLength`/`constants`/`INSPECT_MAX_BYTES`. `Blob`/`File` honor the `endings: 'native'` option (string parts have `\n`/`\r\n` converted to the platform newline — derived from `process.platform` — while a lone `\r` and binary parts are left verbatim, matching Node). **Intentional differences / deferred:** -- `Buffer.poolSize` allocation pooling is modeled like Node: a per-runtime pool `ArrayBuffer` of `Buffer.poolSize` (262144) bytes serves small _unsafe_ allocations — `Buffer.allocUnsafe(size)`, the `Buffer.from(string|array|typed-array)` copy paths, and `Buffer.concat` — when `size < (Buffer.poolSize >>> 1)` (Node's threshold is strictly less-than, so exactly `poolSize/2` is _not_ pooled), advancing an 8-byte-aligned offset and re-pooling on exhaustion. Such buffers share one backing store at different `byteOffset`s, and `buf.buffer.byteLength` is the pool size rather than the buffer length. `Buffer.alloc` (zero-filled), `Buffer.allocUnsafeSlow`, `SlowBuffer`, and large allocations keep their own right-sized backing store, as in Node. `Buffer.concat` zero-fills any tail an explicit `totalLength` leaves uncovered, so a pooled result never exposes stale pool bytes. The pool is plain per-runtime module state, so it never leaks across isolated runtimes. Pool _offsets_ are not byte-for-byte identical to Node's because Node pre-warms its pool with internal startup allocations; only the relative pooling semantics match, and well-behaved packages must not depend on pool identity anyway. +- `Buffer.poolSize` allocation pooling is modeled like Node: a per-runtime pool `ArrayBuffer` of `Buffer.poolSize` (262144) bytes serves small _unsafe_ allocations — `Buffer.allocUnsafe(size)`, the `Buffer.from(string|array|typed-array)` copy paths, and `Buffer.concat` — when `size < (Buffer.poolSize >>> 1)` (Node's threshold is strictly less-than, so exactly `poolSize/2` is _not_ pooled), advancing an 8-byte-aligned offset and re-pooling on exhaustion. Such buffers share one backing store at different `byteOffset`s, and `buf.buffer.byteLength` is the pool size rather than the buffer length. `Buffer.alloc` (zero-filled), `Buffer.allocUnsafeSlow`, and large allocations keep their own right-sized backing store, as in Node. `Buffer.concat` zero-fills any tail an explicit `totalLength` leaves uncovered, so a pooled result never exposes stale pool bytes. The pool is plain per-runtime module state, so it never leaks across isolated runtimes. Pool _offsets_ are not byte-for-byte identical to Node's because Node pre-warms its pool with internal startup allocations; only the relative pooling semantics match, and well-behaved packages must not depend on pool identity anyway. - `Buffer.poolSize` is a plain data property that stores and returns the raw assigned value (no coercion on read), and the pool's backing store is sized straight from it — matching Node's `new Uint8Array(Buffer.poolSize)`. A fractional `poolSize` is truncated (`8192.7` → `8192`), and a negative or non-finite `poolSize` throws a catchable `RangeError` on the next (re-)pool, rather than the previous `>>> 0`, which wrapped a negative `poolSize` into a multi-gigabyte request and a `>= 2^32` one into a mod-2^32 size (issue #213). A failed re-pool leaves the existing pool intact (the new backing store is committed only on success), so the throw is recoverable. One benign divergence remains: for a `poolSize` large enough to exceed JavaScriptCore's typed-array allocation cap (a few GiB), the (re-)pool throws `RangeError` where Node (whose limit is ~`2^53`) would instead build a multi-gigabyte pool. That is reachable only by deliberately setting `poolSize` to gigabytes _and_ forcing a re-pool, and throwing is the safer behavior — Lava never attempts the giant allocation. Setting `Buffer.poolSize` to any such value is itself unrealistic (the default is 262144 and the threshold still uses Node's `>>> 1` verbatim), so this only hardens the defensive path. The default is **262144**, a deliberate deviation: node 24.22.1 reports 65536, and lava uses a larger slab because a pool refill costs more on JavaScriptCore than on V8 (the reason is recorded at the constant in [buffer.js](../pkg/runtime/js/internal/buffer.js); no measurement is cited there). The value is documented as "may be modified" and well-behaved packages must not depend on it, so a pool-size-dependent observable like `Buffer.from(small).buffer.byteLength` differs from the node24 test oracle — the ported pool tests (`buffer-pool.js`, `buffer-allocunsafe-large.js`, `buffer-poolsize-pathological.js`) are written pool-size-relative so they pass under any pool size. (`kMaxLength` stays the JSC-family 4 GiB.) -- `Buffer.allocUnsafe` (above the pool threshold), `Buffer.allocUnsafeSlow`, and `SlowBuffer` return genuinely **uninitialized** memory like Node: a native allocation (`pkg/runtime/typed_array.odin`) hands JavaScriptCore a non-zero-filled backing store as a NoCopy `Uint8Array`, which the JS layer wraps as a Buffer view. A JS `new ArrayBuffer` is always zero-initialized, so this cannot be done in pure JS; that was the prior divergence (large unsafe allocations came back zero-filled). Such buffers now hold arbitrary prior memory contents — reading a byte before writing it is undefined behavior, as in Node. `Buffer.alloc` remains zero-filled, and the `Buffer.from`/`Buffer.concat` copy paths always overwrite (or zero-fill) every byte, so they never expose stale memory (issue #212). As a safety bound the native uninitialized path serves single allocations up to `2^31 - 1` bytes; a larger unsafe request falls back to a zero-filled own-backing store (JavaScriptCore's NoCopy creator aborts the process past an internal multi-gigabyte limit, whereas the JS allocation throws a catchable `RangeError`). The cap is far above any realistic `allocUnsafe` and keeps behavior identical across Linux, macOS, and Windows. +- `Buffer.allocUnsafe` (above the pool threshold) and `Buffer.allocUnsafeSlow` return genuinely **uninitialized** memory like Node: a native allocation (`pkg/runtime/typed_array.odin`) hands JavaScriptCore a non-zero-filled backing store as a NoCopy `Uint8Array`, which the JS layer wraps as a Buffer view. A JS `new ArrayBuffer` is always zero-initialized, so this cannot be done in pure JS; that was the prior divergence (large unsafe allocations came back zero-filled). Such buffers now hold arbitrary prior memory contents — reading a byte before writing it is undefined behavior, as in Node. `Buffer.alloc` remains zero-filled, and the `Buffer.from`/`Buffer.concat` copy paths always overwrite (or zero-fill) every byte, so they never expose stale memory (issue #212). As a safety bound the native uninitialized path serves single allocations up to `2^31 - 1` bytes; a larger unsafe request falls back to a zero-filled own-backing store (JavaScriptCore's NoCopy creator aborts the process past an internal multi-gigabyte limit, whereas the JS allocation throws a catchable `RangeError`). The cap is far above any realistic `allocUnsafe` and keeps behavior identical across Linux, macOS, and Windows. - **Oversized-allocation ceiling (`kMaxLength`).** Node/V8 advertise `kMaxLength` as `Number.MAX_SAFE_INTEGER` on 64-bit; Lava reports the practical JSC array-buffer limit (**4 GiB on 64-bit, 2 GiB − 1 on 32-bit**), matching **Bun** — the other JSC-based runtime, which likewise reports `kMaxLength === 4294967296` rather than `MAX_SAFE_INTEGER`. It is the JSC-family convention, not a lava-only choice. `Buffer.alloc`/`allocUnsafe`/`allocUnsafeSlow`/`concat` and the `Buffer` constructor enforce this ceiling with Node's coded `ERR_OUT_OF_RANGE` **before** JSC is asked to allocate; negative/`NaN`/non-number sizes are validated and throw first. Raw `new TypedArray(n)` / `new ArrayBuffer(n)` are left to JavaScriptCore, which already throws its own catchable `RangeError` for an over-limit request — exactly as Bun does (Lava does not wrap the global constructors). The Buffer ceiling is overridable with the `LAVA_MAX_BUFFER_BYTES` environment variable (a byte count for operators who want a tighter bound; a non-positive value is ignored). Residual: an allocation handed to JavaScriptCore's **C-API NoCopy creator** from native code with a multi-GiB size — e.g. `fs.readFile` of a file past the limit, where the size comes from the OS rather than a JS constructor — can still abort the process; the JS-layer ceiling covers the JS-constructor and Buffer paths, not native allocations. - Property-access `TypeError` messages for engine-level faults (e.g. reading a property of `undefined`) carry JavaScriptCore wording, not V8's. diff --git a/scripts/node-compat/gen-matrix.py b/scripts/node-compat/gen-matrix.py index 483887f..f59385d 100755 --- a/scripts/node-compat/gen-matrix.py +++ b/scripts/node-compat/gen-matrix.py @@ -31,7 +31,7 @@ # status: full | partial | missing | na (page -> (status, implemented, gaps)) MODULES = { "assert": ("full", "node:assert + assert/strict, AssertionError, partialDeepStrictEqual", "Assert class, CallTracker; deepStrictEqual divergences (#103)"), - "buffer": ("full", "Buffer/Blob/File/SlowBuffer/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL)", "atob/btoa are not installed as globals (#86); exports a lava-only describeType"), + "buffer": ("full", "Buffer/Blob/File/isAscii/isUtf8/transcode/resolveObjectURL; module-level atob/btoa (+ global URL.createObjectURL/revokeObjectURL)", "atob/btoa are not installed as globals (#86); exports a lava-only describeType"), "events": ("full", "EventEmitter, once, on, getEventListeners, getMaxListeners, setMaxListeners, listenerCount, addAbortListener, defaultMaxListeners", "EventEmitterAsyncResource/errorMonitor/captureRejections; no EventTarget/CustomEvent globals; meta-events (#105)"), "intl": ("full", "engine (JavaScriptCore) Intl", "engine-provided, not lava code"), "path": ("full", "posix+win32 full surface", ""), From 60c62295e70bc6b9341982ef5c73a30f039530cf Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:46:44 +0200 Subject: [PATCH 13/61] test(https): drop the IP servername node 26 rejects in the graceful-end client Node 26 throws ERR_INVALID_ARG_VALUE for an IP address in tls.connect's servername; identity is still checked against host (the smoke cert has IP:127.0.0.1). Found by CI's HTTPS server smoke on #412. Co-Authored-By: Claude Opus 5.5 --- tests/runtime/https/graceful-end-test.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/runtime/https/graceful-end-test.mjs b/tests/runtime/https/graceful-end-test.mjs index 1e77829..76d4cab 100644 --- a/tests/runtime/https/graceful-end-test.mjs +++ b/tests/runtime/https/graceful-end-test.mjs @@ -59,7 +59,10 @@ try { fail(e.message); } -const sock = tls.connect({ host: '127.0.0.1', port, ca, servername: '127.0.0.1' }, () => { +// No `servername`: node 26 rejects an IP address there (ERR_INVALID_ARG_VALUE, RFC 6066 +// forbids IP SNI). Without it the identity is checked against `host`, which the smoke +// cert carries as IP:127.0.0.1. +const sock = tls.connect({ host: '127.0.0.1', port, ca }, () => { sock.write( 'POST /g HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: 2\r\nConnection: keep-alive\r\n\r\nhi', ); From ae1fb6bcf8c058a18e52a3c6b00468ca611b7885 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Sun, 15 Nov 2026 23:00:58 +0200 Subject: [PATCH 14/61] bench(gate): recalibrate four UTF-8 caps for the node 26 oracle Node 26 is 2-3x faster at UTF-8 encode than 24, so the lava/node ratio rose with no Lava change. Caps set at ~1.4x the max of three CI bench-gate runs (median of 3 launches per side); measurements recorded in thresholds.json. Co-Authored-By: Claude Opus 5.5 --- bench/thresholds.json | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/bench/thresholds.json b/bench/thresholds.json index dca0af3..a5a3a01 100644 --- a/bench/thresholds.json +++ b/bench/thresholds.json @@ -8,7 +8,7 @@ "json-stringify": 2.0, "buffer-to-hex": 8.0, "buffer-to-base64": 9.0, - "buffer-from-utf8": 5.0, + "buffer-from-utf8": 9.5, "buffer-to-utf8": 6.0, "buffer-to-hex-tiny": 4.0, "buffer-from-hex-tiny": 13.0, @@ -28,9 +28,10 @@ "decode-utf16le": 17.5, "decode-chunked": 9.5, "decode-utf8-stream": 17.0, - "encode-utf8": 3.5, - "encode-into": 3.5, - "new-textdecoder": 14.0, + "//node26": "Recalibrated 2026-11-15 (#412) when the oracle moved from Node 24.22.2 to 26.13.0. The denominator moved, not Lava: on the same ubuntu-latest 4-core CI, Node's wall time fell 108.9 -> 35.0 ms (encode-utf8), 64.0 -> 24.2 (encode-into), 11.4 -> 5.3 (buffer-from-utf8), 7.1 -> 6.1 (new-textdecoder) - master run 43757454473 vs #412 run 43759974818 attempt 1, while unrelated Lava rows (url-*, require-*) drifted ~1.2x on runner noise. Measured across 3 `make bench-gate` attempts of run 43759974818 (median of 3 launches per side, Node v26.13.0 vs bin/lava -o:speed): buffer-from-utf8 6.77x, 6.87x, 4.87x; encode-utf8 6.68x, 6.30x, 4.65x; encode-into 8.30x, 8.30x, 7.17x; new-textdecoder 14.09x, 13.93x, 14.29x. Caps are ~1.4x the max observed (5.0 -> 9.5, 3.5 -> 9.5, 3.5 -> 11.5, 14.0 -> 20.0). Tighten when Lava's UTF-8 encode path catches up.", + "encode-utf8": 9.5, + "encode-into": 11.5, + "new-textdecoder": 20.0, "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%.", "url-parse-special": 28.0, "url-parse-ipv4": 14.7, From bc7ff7121bb0597df7b4ff0d5e73134964b7288e Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:01:50 +0200 Subject: [PATCH 15/61] fix(runtime): align buffer, events, assert, and typed_array allocators with Node --- pkg/runtime/js/internal/assert.js | 56 +------ pkg/runtime/js/internal/buffer.js | 146 +++++++++++------ pkg/runtime/js/internal/events.js | 152 ++++++++++++++---- pkg/runtime/typed_array.odin | 17 +- pkg/runtime/typed_array_test.odin | 26 +++ tests/mutation-manifest.json | 11 ++ tests/node-compat/cases/03-buffer.js | 4 + .../cases/70-buffer-size-inspect-parity.js | 5 +- .../cases/71-assert-deep-strict-equal.js | 51 ++++++ tests/node-compat/cases/72-events-parity.js | 113 +++++++++++++ .../cases/73-buffer-type-fill-bigint.js | 58 +++++++ tests/node-compat/pollution-baseline.json | 10 +- 12 files changed, 509 insertions(+), 140 deletions(-) create mode 100644 pkg/runtime/typed_array_test.odin create mode 100644 tests/node-compat/cases/71-assert-deep-strict-equal.js create mode 100644 tests/node-compat/cases/72-events-parity.js create mode 100644 tests/node-compat/cases/73-buffer-type-fill-bigint.js diff --git a/pkg/runtime/js/internal/assert.js b/pkg/runtime/js/internal/assert.js index f972510..78ac1d5 100644 --- a/pkg/runtime/js/internal/assert.js +++ b/pkg/runtime/js/internal/assert.js @@ -3,7 +3,9 @@ (function (require, module) { 'use strict'; - var inspect = require('util').inspect; + var util = require('util'); + var inspect = util.inspect; + var isDeepStrictEqual = util.isDeepStrictEqual; function AssertionError(options) { var message = options.message; @@ -51,58 +53,6 @@ } var ok = assert; - function isDeepStrictEqual(a, b, seen) { - if (Object.is(a, b)) return true; - if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false; - - var ta = Object.prototype.toString.call(a); - var tb = Object.prototype.toString.call(b); - if (ta !== tb) return false; - - if (a instanceof Date) return a.getTime() === b.getTime(); - if (a instanceof RegExp) return a.source === b.source && a.flags === b.flags; - - seen = seen || []; - for (var s = 0; s < seen.length; s++) { - if (seen[s][0] === a && seen[s][1] === b) return true; - } - seen = seen.concat([[a, b]]); - - // TypedArray / Buffer - if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) { - if (a.length !== b.length) return false; - for (var n = 0; n < a.length; n++) if (a[n] !== b[n]) return false; - return true; - } - - if (a instanceof Map) { - if (a.size !== b.size) return false; - var mapOk = true; - a.forEach(function (val, key) { - if (!b.has(key) || !isDeepStrictEqual(val, b.get(key), seen)) mapOk = false; - }); - return mapOk; - } - if (a instanceof Set) { - if (a.size !== b.size) return false; - var setOk = true; - a.forEach(function (val) { - if (!b.has(val)) setOk = false; - }); - return setOk; - } - - var keysA = Object.keys(a); - var keysB = Object.keys(b); - if (keysA.length !== keysB.length) return false; - for (var i = 0; i < keysA.length; i++) { - var key = keysA[i]; - if (!Object.prototype.hasOwnProperty.call(b, key)) return false; - if (!isDeepStrictEqual(a[key], b[key], seen)) return false; - } - return true; - } - function equal(actual, expected, message) { if (!Object.is(actual, expected) && actual !== expected) { throw AssertionError({ diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 4ab8d1e..0547cc6 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -145,7 +145,7 @@ * @node `The "size" argument must be of type number. Received type string ('5')` * for Buffer.alloc('5'); the same template for "offset" (readUInt8('5'), * writeUInt16LE(0, '1')) and "length" (Buffer.concat([a], '5')). Node 24.22.1, - * verified by the issue #391 probe. + * verified by tests/node-compat/cases/70-buffer-size-inspect-parity.js. * @deviates none */ function errInvalidArgType(name, expected, actual) { @@ -250,9 +250,10 @@ * ceiling; the message quotes the ceiling and `Received `. * @node -1/NaN/±Infinity/2**53 → `The value of "size" is out of range. It must be * >= 0 && <= 9007199254740991. Received -1` (2**53 prints 9_007_199_254_740_992); - * -0 and 1.5 pass (length 0 and 1). Node 24.22.1, verified by the #391 probe. - * @deviates The ceiling is K_MAX_LENGTH (4294967296, the JSC-family kMaxLength that - * buffer.constants.MAX_LENGTH reports), not node's 2**53-1 — the documented + * -0 and 1.5 pass (length 0 and 1). Node 24.22.1, verified by + * tests/node-compat/cases/70-buffer-size-inspect-parity.js. + * @deviates The ceiling is K_MAX_LENGTH (4 GiB by default; LAVA_MAX_BUFFER_BYTES lowers it, + * and buffer.constants.MAX_LENGTH reports it), not node's 2**53-1 — the documented * kMaxLength deviation in reference/node-compatibility.md. The bound is pinned * to MAX_LENGTH, not to a literal, by * tests/node-compat/cases/70-buffer-size-inspect-parity.js and @@ -267,6 +268,7 @@ /** @returns {string} Canonical encoding name (default utf8). */ function normalizeEncoding(encoding) { if (encoding === undefined || encoding === null || encoding === '') return 'utf8'; + if (typeof encoding === 'symbol') return '' + encoding; // throws TypeError on Symbol, matching Node var key = StringPrototypeToLowerCase(StringG(encoding)); var canon = ENCODING_ALIASES[key]; return canon !== undefined ? canon : key; @@ -274,9 +276,9 @@ /** @returns {boolean} */ function isEncodingName(encoding) { - return ObjectPrototypeHasOwnProperty( - ENCODING_ALIASES, - StringPrototypeToLowerCase(StringG(encoding)), + return ( + typeof encoding === 'string' && + ObjectPrototypeHasOwnProperty(ENCODING_ALIASES, StringPrototypeToLowerCase(encoding)) ); } @@ -407,7 +409,11 @@ if (typeof value === 'string') return strToBytes(value, encoding || 'utf8'); if (value instanceof Uint8Array) return value; if (value instanceof ArrayBuffer) return new Uint8Array(value); - throw new TypeError('value must be string, number, Buffer, or Uint8Array'); + throw errInvalidArgType( + 'value', + 'one of type number or string or an instance of Buffer or Uint8Array', + value, + ); } /** indexOf / lastIndexOf implementation (native search). */ @@ -494,7 +500,9 @@ } function writeBigUInt(buf, value, offset, littleEndian) { - value = BigInt(value); + if (typeof value === 'symbol') Number(value); // throws TypeError on Symbol, matching Node + if (typeof value !== 'bigint') + throw new TypeError('Cannot mix BigInt and other types, use explicit conversions'); checkValueInt(value, BIG_0, BIG_MAX_U64, 7); offset = checkBounds(buf, offset, 8); viewOf(buf).setBigUint64(offset, value, littleEndian); @@ -502,7 +510,9 @@ } function writeBigInt(buf, value, offset, littleEndian) { - value = BigInt(value); + if (typeof value === 'symbol') Number(value); // throws TypeError on Symbol, matching Node + if (typeof value !== 'bigint') + throw new TypeError('Cannot mix BigInt and other types, use explicit conversions'); checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7); offset = checkBounds(buf, offset, 8); viewOf(buf).setBigInt64(offset, value, littleEndian); @@ -536,9 +546,9 @@ var len = TypedArrayPrototypeGetLength(this); if (start === undefined || start <= 0) start = 0; else if (start >= len) return ''; - else start |= 0; - if (end === undefined || end > len) end = len; - else end |= 0; + else start = +start | 0; + if (end === undefined || end >= len) end = len; + else end = +end | 0; if (end <= start) return ''; // Full-range toString (the common case) skips the subarray view: the // codec natives read this buffer's bytes in place either way. @@ -592,6 +602,11 @@ * @returns {number} Bytes written */ write(string, offset, length, encoding) { + if (typeof string !== 'string') { + var e = new TypeError('argument must be a string'); + e.code = 'ERR_INVALID_ARG_TYPE'; + throw e; + } if (offset === undefined) { offset = 0; length = this.length; @@ -600,7 +615,8 @@ offset = 0; length = this.length; } else { - offset = validateWriteOffset(+offset, this.length, 'offset'); + if (typeof offset !== 'number') throw errInvalidArgType('offset', 'of type number', offset); + offset = validateWriteOffset(offset, this.length, 'offset'); if (typeof length === 'string') { encoding = length; length = undefined; @@ -608,8 +624,13 @@ } var enc = normalizeEncoding(encoding || 'utf8'); var remaining = this.length - offset; - var max = length === undefined ? remaining : toInteger(length, 0); - if (max > remaining) max = remaining; + var max; + if (length === undefined) { + max = remaining; + } else { + if (typeof length !== 'number') throw errInvalidArgType('length', 'of type number', length); + max = validateWriteOffset(length, remaining, 'length'); + } if (max <= 0) return 0; string = StringG(string); if (enc === 'utf8') return utf8WriteIntoNative(this, string, offset, max); @@ -639,19 +660,31 @@ compare(target, targetStart, targetEnd, sourceStart, sourceEnd) { if (!(target instanceof Uint8Array)) - throw new TypeError('target must be a Buffer or Uint8Array'); - targetStart = validateRange(targetStart, K_MAX_LENGTH, 'targetStart'); - targetEnd = validateRange( - targetEnd === undefined ? target.length : targetEnd, - target.length, - 'targetEnd', - ); - sourceStart = validateRange(sourceStart, K_MAX_LENGTH, 'sourceStart'); - sourceEnd = validateRange( - sourceEnd === undefined ? this.length : sourceEnd, - this.length, - 'sourceEnd', - ); + throw errInvalidArgType('target', 'an instance of Buffer or Uint8Array', target); + if (targetStart === undefined) targetStart = 0; + else { + if (typeof targetStart !== 'number') + throw errInvalidArgType('targetStart', 'of type number', targetStart); + targetStart = validateWriteOffset(targetStart, target.length, 'targetStart'); + } + if (targetEnd === undefined) targetEnd = target.length; + else { + if (typeof targetEnd !== 'number') + throw errInvalidArgType('targetEnd', 'of type number', targetEnd); + targetEnd = validateWriteOffset(targetEnd, target.length, 'targetEnd'); + } + if (sourceStart === undefined) sourceStart = 0; + else { + if (typeof sourceStart !== 'number') + throw errInvalidArgType('sourceStart', 'of type number', sourceStart); + sourceStart = validateWriteOffset(sourceStart, this.length, 'sourceStart'); + } + if (sourceEnd === undefined) sourceEnd = this.length; + else { + if (typeof sourceEnd !== 'number') + throw errInvalidArgType('sourceEnd', 'of type number', sourceEnd); + sourceEnd = validateWriteOffset(sourceEnd, this.length, 'sourceEnd'); + } if (sourceStart >= sourceEnd && targetStart >= targetEnd) return 0; if (sourceStart >= sourceEnd) return -1; if (targetStart >= targetEnd) return 1; @@ -671,30 +704,33 @@ end = this.length; } if (start === undefined) start = 0; - else start = validateWriteOffset(+start, K_MAX_LENGTH, 'offset'); - if (end === undefined) end = this.length; - else end = validateWriteOffset(+end, this.length, 'end'); - if (end <= start) return this; - if (typeof value === 'number') { - u8Fill.call(this, value & 0xff, start, end); - return this; + else { + if (typeof start !== 'number') throw errInvalidArgType('offset', 'of type number', start); + start = validateWriteOffset(start, K_MAX_LENGTH, 'offset'); } - if (typeof value === 'boolean') { - u8Fill.call(this, value ? 1 : 0, start, end); - return this; + if (end === undefined) end = this.length; + else { + if (typeof end !== 'number') throw errInvalidArgType('end', 'of type number', end); + end = validateWriteOffset(end, this.length, 'end'); } + if (end <= start) return this; var bytes; - if (value instanceof Uint8Array) { - if (value.length === 0) throw errInvalidArgValue('value', value); - bytes = value; - } else { - var str = StringG(value); - bytes = strToBytes(str, encoding || 'utf8'); + if (typeof value === 'string') { + bytes = strToBytes(value, encoding || 'utf8'); if (bytes.length === 0) { - if (str.length !== 0) throw errInvalidArgValue('value', value); + if (value.length !== 0) throw errInvalidArgValue('value', value); u8Fill.call(this, 0, start, end); return this; } + } else if (value instanceof Uint8Array) { + if (value.length === 0) throw errInvalidArgValue('value', value); + bytes = value; + } else { + // Non-string, non-Uint8Array values (including null, undefined, objects, booleans, + // numbers, BigInt, Symbol) coerce via ToNumber/ToInt32 (& 0xff), matching Node: + // null/undefined/{} -> 0, true -> 1, BigInt/Symbol -> TypeError. + u8Fill.call(this, value & 0xff, start, end); + return this; } if (bytes.length === 1) { u8Fill.call(this, bytes[0], start, end); @@ -752,7 +788,8 @@ * ±Infinity or above kMaxLength (assertSize, the check Buffer.alloc runs). * @node `Buffer(n)` is `Buffer.alloc(n)`: -1, NaN, ±Infinity and 2**53 throw the * coded "size" RangeError, -0 → length 0, 1.5 → length 1, 5 → five zero bytes; - * identical with and without `new`. Node 24.22.1, verified by the #391 probe. + * identical with and without `new`. Node 24.22.1, verified by + * tests/node-compat/cases/70-buffer-size-inspect-parity.js. * @deviates The out-of-range bound is K_MAX_LENGTH (see assertSize), pinned by * tests/node-compat/cases/70-buffer-size-inspect-parity.js. */ @@ -1260,8 +1297,10 @@ }; Buffer.compare = function (a, b) { - if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array)) - throw new TypeError('Arguments must be Buffers or Uint8Arrays'); + if (!(a instanceof Uint8Array)) + throw errInvalidArgType('buf1', 'an instance of Buffer or Uint8Array', a); + if (!(b instanceof Uint8Array)) + throw errInvalidArgType('buf2', 'an instance of Buffer or Uint8Array', b); return compareBytes(a, b); }; @@ -1306,7 +1345,8 @@ }; Buffer.copyBytesFrom = function (view, offset, length) { - if (!ArrayBuffer.isView(view)) throw new TypeError('view must be a TypedArray'); + if (!ArrayBuffer.isView(view)) + throw errInvalidArgType('view', 'an instance of TypedArray', view); var bpe = view.BYTES_PER_ELEMENT || 1; offset = toInteger(offset, 0); length = length === undefined ? view.length - offset : toInteger(length, 0); @@ -1612,6 +1652,12 @@ if (globalThis.File === undefined) { globalThis.File = File; } + if (globalThis.atob === undefined) { + globalThis.atob = atob; + } + if (globalThis.btoa === undefined) { + globalThis.btoa = btoa; + } var exported = { Buffer: Buffer, diff --git a/pkg/runtime/js/internal/events.js b/pkg/runtime/js/internal/events.js index 13521f6..8963ce4 100644 --- a/pkg/runtime/js/internal/events.js +++ b/pkg/runtime/js/internal/events.js @@ -40,7 +40,21 @@ EventEmitter.prototype._events = undefined; EventEmitter.prototype._maxListeners = undefined; + function validateMaxListeners(n) { + if (typeof n !== 'number') { + throw typeError('setMaxListeners', 'of type number', n); + } + if (n < 0 || n !== n) { + var e = new RangeError( + 'The value of "setMaxListeners" is out of range. It must be >= 0. Received ' + n, + ); + e.code = 'ERR_OUT_OF_RANGE'; + throw e; + } + } + EventEmitter.prototype.setMaxListeners = function (n) { + validateMaxListeners(n); this._maxListeners = n; return this; }; @@ -57,6 +71,14 @@ } if (target._events === undefined) EventEmitter.init.call(target); var events = target._events; + if (events.newListener !== undefined) { + target.emit( + 'newListener', + type, + listener.listener !== undefined ? listener.listener : listener, + ); + events = target._events; + } var existing = events[type]; if (existing === undefined) { events[type] = listener; @@ -114,11 +136,17 @@ EventEmitter.prototype.removeListener = function (type, listener) { if (this._events === undefined) return this; - var list = this._events[type]; + var events = this._events; + var list = events[type]; if (list === undefined) return this; if (list === listener || list.listener === listener) { if (--this._eventsCount === 0) this._events = P.ObjectCreate(null); - else delete this._events[type]; + else { + delete events[type]; + if (events.removeListener !== undefined) { + this.emit('removeListener', type, list.listener || listener); + } + } } else if (typeof list !== 'function') { var position = -1; for (var i = list.length - 1; i >= 0; i--) { @@ -128,21 +156,49 @@ } } if (position < 0) return this; + var removed = list[position]; spliceOne(list, position); - if (list.length === 1) this._events[type] = list[0]; + if (list.length === 1) events[type] = list[0]; + if (events.removeListener !== undefined) { + this.emit('removeListener', type, removed.listener || listener); + } } return this; }; EventEmitter.prototype.off = EventEmitter.prototype.removeListener; EventEmitter.prototype.removeAllListeners = function (type) { - if (this._events === undefined) return this; + var events = this._events; + if (events === undefined) return this; + if (events.removeListener === undefined) { + if (arguments.length === 0) { + this._events = P.ObjectCreate(null); + this._eventsCount = 0; + } else if (events[type] !== undefined) { + if (--this._eventsCount === 0) this._events = P.ObjectCreate(null); + else delete events[type]; + } + return this; + } if (arguments.length === 0) { + var keys = P.ReflectOwnKeys(events); + for (var k = 0; k < keys.length; k++) { + var key = keys[k]; + if (key === 'removeListener') continue; + this.removeAllListeners(key); + } + this.removeAllListeners('removeListener'); this._events = P.ObjectCreate(null); this._eventsCount = 0; - } else if (this._events[type] !== undefined) { - if (--this._eventsCount === 0) this._events = P.ObjectCreate(null); - else delete this._events[type]; + return this; + } + var listeners = events[type]; + if (typeof listeners === 'function') { + this.removeListener(type, listeners); + } else if (listeners !== undefined) { + for (var i = listeners.length - 1; i >= 0; i--) { + this.removeListener(type, listeners[i]); + } } return this; }; @@ -191,35 +247,83 @@ return typeof handler === 'function' ? [handler] : arrayClone(handler); }; - EventEmitter.prototype.listenerCount = function (type) { + EventEmitter.prototype.listenerCount = function (type, listener) { var events = this._events; if (events === undefined) return 0; var handler = events[type]; if (handler === undefined) return 0; - return typeof handler === 'function' ? 1 : handler.length; + if (typeof handler === 'function') { + if (listener !== undefined) { + return handler === listener || handler.listener === listener ? 1 : 0; + } + return 1; + } + if (listener !== undefined) { + var count = 0; + for (var i = 0; i < handler.length; i++) { + if (handler[i] === listener || handler[i].listener === listener) count++; + } + return count; + } + return handler.length; }; EventEmitter.prototype.eventNames = function () { return this._eventsCount > 0 ? P.ReflectOwnKeys(this._events) : []; }; - // once(emitter, name) — resolves with the emitted arguments on the next - // matching event, or rejects if 'error' fires first. - function once(emitter, name) { + // once(emitter, name[, options]) — resolves with the emitted arguments on the next + // matching event, or rejects if 'error' fires first or options.signal aborts. + function once(emitter, name, options) { + var signal = options && options.signal; + if (signal !== undefined) { + if (!isAbortSignal(signal)) { + return P.Promise.reject(typeError('options.signal', 'an instance of AbortSignal', signal)); + } + if (signal.aborted) { + return P.Promise.reject(makeAbortError()); + } + } return new P.Promise(function (resolve, reject) { + var errorListener; + var abortListener; + function cleanup() { + if (typeof emitter.removeListener === 'function') { + emitter.removeListener(name, eventListener); + if (errorListener !== undefined) emitter.removeListener('error', errorListener); + } else if (typeof emitter.removeEventListener === 'function') { + emitter.removeEventListener(name, eventListener); + } + if (abortListener !== undefined) { + signal.removeEventListener('abort', abortListener); + } + } function eventListener() { - if (errorListener !== undefined) emitter.removeListener('error', errorListener); + cleanup(); resolve(P.ArrayPrototypeSlice(arguments)); } - var errorListener; - if (name !== 'error') { - errorListener = function (err) { - emitter.removeListener(name, eventListener); - reject(err); + if (signal !== undefined) { + abortListener = function () { + cleanup(); + reject(makeAbortError()); }; - emitter.once('error', errorListener); + signal.addEventListener('abort', abortListener, { once: true }); + } + if (typeof emitter.once === 'function') { + if (name !== 'error') { + errorListener = function (err) { + cleanup(); + reject(err); + }; + emitter.once('error', errorListener); + } + emitter.once(name, eventListener); + } else if (typeof emitter.addEventListener === 'function') { + emitter.addEventListener(name, eventListener, { once: true }); + } else { + cleanup(); + reject(typeError('emitter', 'an EventEmitter or EventTarget', emitter)); } - emitter.once(name, eventListener); }); } @@ -263,13 +367,7 @@ // events.setMaxListeners(n[, ...targets]) — with no targets, sets the global default. function setMaxListeners(n) { n = n === undefined ? EventEmitter.defaultMaxListeners : n; - if (typeof n !== 'number' || n < 0 || n !== n) { - var e = new RangeError( - 'The value of "n" is out of range. It must be a non-negative number. Received ' + n, - ); - e.code = 'ERR_OUT_OF_RANGE'; - throw e; - } + validateMaxListeners(n); var targets = P.ArrayPrototypeSlice(arguments, 1); if (targets.length === 0) { EventEmitter.defaultMaxListeners = n; diff --git a/pkg/runtime/typed_array.odin b/pkg/runtime/typed_array.odin index 8b0cfa2..af661d2 100644 --- a/pkg/runtime/typed_array.odin +++ b/pkg/runtime/typed_array.odin @@ -118,20 +118,28 @@ jsc_buffer_deallocator :: proc "c" (bytes: rawptr, deallocator_context: rawptr) make_uint8_array :: proc(ctx: jsc.JSContextRef, data: []byte) -> jsc.JSValueRef { ptr := raw_data(data) n := len(data) + pad: []byte if ptr == nil { - pad := make([]byte, 1, runtime.heap_allocator()) + pad = make([]byte, 1, runtime.heap_allocator()) ptr = raw_data(pad) n = 0 } array := jsc.make_uint8_nocopy_locked(ctx, ptr, c.size_t(n), jsc_buffer_deallocator) + if array == nil && len(pad) > 0 { + delete(pad, runtime.heap_allocator()) + } return cast(jsc.JSValueRef)array } MAX_UNINIT_ALLOC :: 0x7fff_ffff // 2^31 - 1 bytes +// make_uint8_array_uninit allocates `size` uninitialized bytes on runtime.heap_allocator() +// (matching jsc_buffer_deallocator) and hands them to JavaScriptCore as a NoCopy Uint8Array. +// Frees the backing slice if JSC declines to take ownership. make_uint8_array_uninit :: proc(ctx: jsc.JSContextRef, size: int) -> (jsc.JSValueRef, bool) { if size <= 0 || size > MAX_UNINIT_ALLOC do return nil, false - data, err := mem.alloc_bytes_non_zeroed(size, mem.DEFAULT_ALIGNMENT, context.allocator) + heap := runtime.heap_allocator() + data, err := mem.alloc_bytes_non_zeroed(size, mem.DEFAULT_ALIGNMENT, heap) if err != nil || len(data) != size do return nil, false array := jsc.make_uint8_nocopy_locked( ctx, @@ -139,6 +147,9 @@ make_uint8_array_uninit :: proc(ctx: jsc.JSContextRef, size: int) -> (jsc.JSValu c.size_t(size), jsc_buffer_deallocator, ) - if array == nil do return nil, false + if array == nil { + delete(data, heap) + return nil, false + } return cast(jsc.JSValueRef)array, true } diff --git a/pkg/runtime/typed_array_test.odin b/pkg/runtime/typed_array_test.odin new file mode 100644 index 0000000..0359eae --- /dev/null +++ b/pkg/runtime/typed_array_test.odin @@ -0,0 +1,26 @@ +package lava_runtime + +import "core:mem" +import "core:testing" +import jsc "lava:pkg/jsc" + +@(test) +make_uint8_array_uninit_uses_heap_allocator :: proc(t: ^testing.T) { + track: mem.Tracking_Allocator + mem.tracking_allocator_init(&track, context.allocator) + defer mem.tracking_allocator_destroy(&track) + context.allocator = mem.tracking_allocator(&track) + + ctx := jsc.JSGlobalContextCreate(nil) + defer jsc.JSGlobalContextRelease(ctx) + + val, ok := make_uint8_array_uninit(ctx, 64) + testing.expect(t, ok, "make_uint8_array_uninit(64) should succeed") + testing.expect(t, val != nil, "make_uint8_array_uninit(64) should return non-nil value") + testing.expectf( + t, + len(track.allocation_map) == 0, + "make_uint8_array_uninit allocated %d block(s) on ambient allocator instead of heap_allocator", + len(track.allocation_map), + ) +} diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index d2c177c..8d28762 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1732,6 +1732,17 @@ "" ] }, + { + "name": "70-buffer-size-inspect-parity dies when Buffer(n) routes through allocUnsafe instead of a fresh zero-filled FastBuffer", + "why": "Buffer(n) returns a fresh zero-filled non-pooled Buffer, not a pool slice (#408).", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " return new FastBuffer(arg); // zero-filled, matching the old `super(number)`", + "replace": " return Buffer.allocUnsafe(arg);", + "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", + "expect_detail": [ + "buf=8192" + ] + }, { "name": "run-gates dies when a failing gate no longer sets a non-zero exit code", "why": "make gates exists so a red gate cannot be hidden by a pipe's exit status (#360); returning status 0 on failure defeats the runner.", diff --git a/tests/node-compat/cases/03-buffer.js b/tests/node-compat/cases/03-buffer.js index 332fbfb..83e55f7 100644 --- a/tests/node-compat/cases/03-buffer.js +++ b/tests/node-compat/cases/03-buffer.js @@ -45,6 +45,10 @@ if (typeof BigInt === 'function') { assert.equal(Buffer.copyBytesFrom(new Uint16Array([0x1234])).toString('hex'), '3412'); assert.equal(require('node:buffer').atob('Zm9v'), 'foo'); assert.equal(require('node:buffer').btoa('foo'), 'Zm9v'); +assert.equal(globalThis.atob, require('node:buffer').atob); +assert.equal(globalThis.btoa, require('node:buffer').btoa); +assert.equal(atob('Zm9v'), 'foo'); +assert.equal(btoa('foo'), 'Zm9v'); assert.equal(require('node:buffer').isAscii(Buffer.from('abc')), true); // Node 26 removed SlowBuffer (DEP0030 end-of-life): the export is gone, not deprecated. assert.equal('SlowBuffer' in require('node:buffer'), false, 'node:buffer has no SlowBuffer'); diff --git a/tests/node-compat/cases/70-buffer-size-inspect-parity.js b/tests/node-compat/cases/70-buffer-size-inspect-parity.js index 1125a45..cba5f78 100644 --- a/tests/node-compat/cases/70-buffer-size-inspect-parity.js +++ b/tests/node-compat/cases/70-buffer-size-inspect-parity.js @@ -84,10 +84,11 @@ for (const [n, v] of nums) { show(`new Buffer(${n})`, () => new Buffer(v)); show(`Buffer(${n})`, () => Buffer(v)); } -// Pooled storage would leak stale bytes here; node zero-fills. +// Pooled storage would leak stale bytes here; node zero-fills and does not slice the pool. show('Buffer(5) after dirtying the pool', () => { Buffer.allocUnsafe(8).fill(0xaa); - return Buffer(5); + const res = Buffer(5); + return res.toString('hex') + ' buf=' + res.buffer.byteLength + ' off=' + res.byteOffset; }); // --- 3. Separator with nothing shown --------------------------------------------------- diff --git a/tests/node-compat/cases/71-assert-deep-strict-equal.js b/tests/node-compat/cases/71-assert-deep-strict-equal.js new file mode 100644 index 0000000..a4eea7a --- /dev/null +++ b/tests/node-compat/cases/71-assert-deep-strict-equal.js @@ -0,0 +1,51 @@ +// assert.deepStrictEqual / notDeepStrictEqual delegate to util.isDeepStrictEqual (#103). +const assert = require('node:assert'); + +function checkEqual(label, a, b) { + let ok = true; + try { + assert.deepStrictEqual(a, b); + } catch (e) { + ok = false; + } + console.log(label + ': eq=' + ok); +} + +checkEqual('Set objects equal', new Set([{ a: 1 }]), new Set([{ a: 1 }])); +checkEqual('Set objects diff', new Set([{ a: 1 }]), new Set([{ a: 2 }])); +checkEqual('Map obj keys equal', new Map([[{ k: 1 }, 2]]), new Map([[{ k: 1 }, 2]])); +checkEqual('Map obj keys diff', new Map([[{ k: 1 }, 2]]), new Map([[{ k: 1 }, 3]])); +checkEqual('Error equal', new Error('boom'), new Error('boom')); +checkEqual('Error diff class', new TypeError('boom'), new Error('boom')); +checkEqual('Boxed Number equal', new Number(1), new Number(1)); +checkEqual('Boxed Number diff', new Number(1), new Number(2)); +checkEqual('Int8 vs Uint8', new Int8Array([1, 2]), new Uint8Array([1, 2])); +checkEqual('NaN', NaN, NaN); +checkEqual('+0 vs -0', +0, -0); + +const s = Symbol('x'); +checkEqual('Symbol key equal', { [s]: 1 }, { [s]: 1 }); +checkEqual('Symbol key diff', { [s]: 1 }, { [s]: 2 }); + +class A { + constructor(x) { + this.x = x; + } +} +class B { + constructor(x) { + this.x = x; + } +} +checkEqual('Proto diff', new A(1), new B(1)); + +const c1 = {}; +c1.self = c1; +const c2 = {}; +c2.self = c2; +checkEqual('Circular', c1, c2); + +assert.throws(() => assert.notDeepStrictEqual(new Set([{ a: 1 }]), new Set([{ a: 1 }])), { + code: 'ERR_ASSERTION', +}); +console.log('notDeepStrictEqual threw ERR_ASSERTION'); diff --git a/tests/node-compat/cases/72-events-parity.js b/tests/node-compat/cases/72-events-parity.js new file mode 100644 index 0000000..8a99f35 --- /dev/null +++ b/tests/node-compat/cases/72-events-parity.js @@ -0,0 +1,113 @@ +// events.once({ signal }), listenerCount(type, listener), setMaxListeners validation, +// and newListener / removeListener meta-events (#105). +const { EventEmitter, once, setMaxListeners } = require('node:events'); + +async function testOnceSignal() { + const ee = new EventEmitter(); + + const ac1 = new AbortController(); + ac1.abort(); + try { + await once(ee, 'foo', { signal: ac1.signal }); + console.log('pre-aborted: UNEXPECTED OK'); + } catch (e) { + console.log( + 'pre-aborted:', + e.name, + e.code, + 'listeners:', + ee.listenerCount('foo'), + ee.listenerCount('error'), + ); + } + + const ac2 = new AbortController(); + const p2 = once(ee, 'foo', { signal: ac2.signal }); + console.log('pending listeners:', ee.listenerCount('foo'), ee.listenerCount('error')); + setTimeout(() => ac2.abort(), 5); + try { + await p2; + console.log('aborted: UNEXPECTED OK'); + } catch (e) { + console.log( + 'aborted:', + e.name, + e.code, + 'listeners:', + ee.listenerCount('foo'), + ee.listenerCount('error'), + ); + } + + const ac3 = new AbortController(); + const p3 = once(ee, 'foo', { signal: ac3.signal }); + ee.emit('foo', 42); + const res = await p3; + ac3.abort(); + console.log('resolved before abort:', JSON.stringify(res)); + + try { + await once(ee, 'foo', { signal: 123 }); + } catch (e) { + console.log('bad signal:', e.name, e.code); + } +} + +function testSetMaxListeners() { + for (const [label, v] of [ + ['undefined', undefined], + ['-1', -1], + ['NaN', NaN], + ['"5"', '5'], + ['Infinity', Infinity], + ['0', 0], + ['10', 10], + ]) { + try { + new EventEmitter().setMaxListeners(v); + console.log('proto(' + label + '): OK'); + } catch (e) { + console.log('proto(' + label + '):', e.name, e.code); + } + try { + setMaxListeners(v); + console.log('static(' + label + '): OK'); + } catch (e) { + console.log('static(' + label + '):', e.name, e.code); + } + } + setMaxListeners(10); +} + +function testListenerCount() { + const ee = new EventEmitter(); + const fnA = () => {}; + const fnB = () => {}; + ee.on('x', fnA); + ee.once('x', fnA); + ee.on('x', fnB); + console.log('count all:', ee.listenerCount('x')); + console.log('count fnA:', ee.listenerCount('x', fnA)); + console.log('count fnB:', ee.listenerCount('x', fnB)); + console.log('count missing:', ee.listenerCount('missing', fnA)); +} + +function testMetaEvents() { + const ee = new EventEmitter(); + const fnA = () => {}; + const fnB = () => {}; + const log = []; + ee.on('newListener', (ev, l) => log.push(['new', ev, l === fnA, ee.listenerCount(ev)])); + ee.on('removeListener', (ev, l) => log.push(['rem', ev, l === fnA, ee.listenerCount(ev)])); + ee.once('y', fnA); + ee.emit('y'); + ee.on('z', fnA); + ee.on('z', fnB); + ee.removeAllListeners('z'); + console.log('meta:', JSON.stringify(log)); +} + +testSetMaxListeners(); +testListenerCount(); +testMetaEvents(); +testOnceSignal(); diff --git a/tests/node-compat/cases/73-buffer-type-fill-bigint.js b/tests/node-compat/cases/73-buffer-type-fill-bigint.js new file mode 100644 index 0000000..f72d3a8 --- /dev/null +++ b/tests/node-compat/cases/73-buffer-type-fill-bigint.js @@ -0,0 +1,58 @@ +// Buffer fill(null), missing type checks, and BigInt/Symbol handling (#392 groups 1-3). +function show(label, fn) { + let r; + try { + const v = fn(); + r = 'OK ' + (Buffer.isBuffer(v) ? v.toString('hex') : JSON.stringify(v)); + } catch (e) { + // JSC and V8 phrase BigInt/Symbol coercion TypeErrors differently, so compare name + code. + r = 'THREW ' + e.name + ' ' + e.code; + if (e.code) r += ' | ' + e.message; + } + console.log(label + ' => ' + r); +} + +const b = () => Buffer.alloc(8, 0xaa); + +// Group 1: fill(null) and fill argument validation +show('fill(null)', () => b().fill(null)); +show('fill(undefined)', () => b().fill(undefined)); +show('fill(true)', () => b().fill(true)); +show('fill({})', () => b().fill({})); +show('fill(1n)', () => b().fill(1n)); +show('fill(Symbol)', () => b().fill(Symbol('s'))); +show('fill("a", null)', () => b().fill('a', null)); +show('fill("a", 1n)', () => b().fill('a', 1n)); +show('fill("a", 0, null)', () => b().fill('a', 0, null)); +show('fill("a", 0, 1n)', () => b().fill('a', 0, 1n)); +show('alloc(4, null)', () => Buffer.alloc(4, null)); +show('alloc(4, 1n)', () => Buffer.alloc(4, 1n)); + +// Group 2: missing type checks +show('write(null)', () => b().write(null)); +show('write(123)', () => b().write(123)); +show('write("a", null)', () => b().write('a', null)); +show('write("a", 1n)', () => b().write('a', 1n)); +show('write("a", 0, null)', () => b().write('a', 0, null)); +show('write("a", 0, 1n)', () => b().write('a', 0, 1n)); +show('write("a", 0, -1)', () => b().write('a', 0, -1)); +show('compare(null)', () => b().compare(null)); +show('compare(b, null)', () => b().compare(b(), null)); +show('compare(b, 1n)', () => b().compare(b(), 1n)); +show('indexOf(null)', () => b().indexOf(null)); +show('indexOf(1n)', () => b().indexOf(1n)); +show('indexOf(Symbol)', () => b().indexOf(Symbol('s'))); +show('Buffer.compare(null, b)', () => Buffer.compare(null, b())); +show('Buffer.compare(b, null)', () => Buffer.compare(b(), null)); +show('Buffer.copyBytesFrom(null)', () => Buffer.copyBytesFrom(null)); + +// Group 3: BigInt / Symbol +show('writeBigInt64LE(1)', () => b().writeBigInt64LE(1, 0)); +show('writeBigInt64LE(Symbol)', () => b().writeBigInt64LE(Symbol('s'), 0)); +show('writeBigUInt64BE(1)', () => b().writeBigUInt64BE(1, 0)); +show('toString(Symbol)', () => b().toString(Symbol('s'))); +show('toString("utf8", 1n)', () => b().toString('utf8', 1n)); +show('toString("utf8", 10n)', () => b().toString('utf8', 10n)); +show('toString("utf8", 0, 1n)', () => b().toString('utf8', 0, 1n)); +show('Buffer.isEncoding(1n)', () => Buffer.isEncoding(1n)); +show('Buffer.isEncoding(Symbol)', () => Buffer.isEncoding(Symbol('s'))); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 42b7299..561fbad 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -12,16 +12,16 @@ "global": 9 }, "internal/assert.js": { - "method": 19, - "invoke": 8, + "method": 14, + "invoke": 5, "accessor": 8, - "global": 52 + "global": 40 }, "internal/buffer.js": { "method": 41, - "invoke": 7, + "invoke": 6, "accessor": 22, - "global": 130 + "global": 129 }, "internal/console.js": { "method": 0, From f16b22c6a73d48f1249a723292d469c4e3b21acf Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:19:02 +0200 Subject: [PATCH 16/61] fix(runtime): close PR #413 review findings - buffer.compare: bound targetStart/sourceStart by kMaxLength, only *End by length (node 26: Buffer.alloc(3).compare(Buffer.alloc(3), 100) === 1). - buffer.write: validate length in 0..buf.length, then clamp to remaining. - typed_array.odin: drop the nil-return deletes; JSTypedArray.h:61 says the bytesDeallocator always runs on failure, so they double-freed. - events.once: validate options as an object, Node's exact message. - validateOffsetArg helper replaces the repeated offset checks. - mutation manifest: unambiguous checkBounds find, buf=262144 detail, four new entries pinning the fixes. Co-Authored-By: Claude Opus 5.5 --- pkg/runtime/js/internal/buffer.js | 52 +++++++------------ pkg/runtime/js/internal/events.js | 30 +++++++++++ pkg/runtime/typed_array.odin | 20 +++---- tests/mutation-manifest.json | 46 +++++++++++++++- tests/node-compat/cases/72-events-parity.js | 16 ++++++ .../cases/73-buffer-type-fill-bigint.js | 10 ++++ 6 files changed, 131 insertions(+), 43 deletions(-) diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 0547cc6..b378b1e 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -228,6 +228,13 @@ return offset; } + // Node's validateOffset(value, name, 0, max) as write/compare/fill call it: a + // non-number is a type error (no ToNumber coercion), then the integer/range check. + function validateOffsetArg(value, name, max) { + if (typeof value !== 'number') throw errInvalidArgType(name, 'of type number', value); + return validateWriteOffset(value, max, name); + } + function errUnknownEncoding(encoding) { var e = new TypeError('Unknown encoding: ' + encoding); e.code = 'ERR_UNKNOWN_ENCODING'; @@ -615,8 +622,7 @@ offset = 0; length = this.length; } else { - if (typeof offset !== 'number') throw errInvalidArgType('offset', 'of type number', offset); - offset = validateWriteOffset(offset, this.length, 'offset'); + offset = validateOffsetArg(offset, 'offset', this.length); if (typeof length === 'string') { encoding = length; length = undefined; @@ -628,8 +634,10 @@ if (length === undefined) { max = remaining; } else { - if (typeof length !== 'number') throw errInvalidArgType('length', 'of type number', length); - max = validateWriteOffset(length, remaining, 'length'); + // Node bounds `length` by the whole buffer, not by what is left after + // `offset`, then clamps: write('abcdef', 5, 8) on 10 bytes writes 5. + max = validateOffsetArg(length, 'length', this.length); + if (max > remaining) max = remaining; } if (max <= 0) return 0; string = StringG(string); @@ -661,30 +669,16 @@ compare(target, targetStart, targetEnd, sourceStart, sourceEnd) { if (!(target instanceof Uint8Array)) throw errInvalidArgType('target', 'an instance of Buffer or Uint8Array', target); + // Node bounds the *Start arguments by kMaxLength only, the *End ones by the + // buffer length: compare(b3, 100) is 1 (empty target range), not a RangeError. if (targetStart === undefined) targetStart = 0; - else { - if (typeof targetStart !== 'number') - throw errInvalidArgType('targetStart', 'of type number', targetStart); - targetStart = validateWriteOffset(targetStart, target.length, 'targetStart'); - } + else targetStart = validateOffsetArg(targetStart, 'targetStart', K_MAX_LENGTH); if (targetEnd === undefined) targetEnd = target.length; - else { - if (typeof targetEnd !== 'number') - throw errInvalidArgType('targetEnd', 'of type number', targetEnd); - targetEnd = validateWriteOffset(targetEnd, target.length, 'targetEnd'); - } + else targetEnd = validateOffsetArg(targetEnd, 'targetEnd', target.length); if (sourceStart === undefined) sourceStart = 0; - else { - if (typeof sourceStart !== 'number') - throw errInvalidArgType('sourceStart', 'of type number', sourceStart); - sourceStart = validateWriteOffset(sourceStart, this.length, 'sourceStart'); - } + else sourceStart = validateOffsetArg(sourceStart, 'sourceStart', K_MAX_LENGTH); if (sourceEnd === undefined) sourceEnd = this.length; - else { - if (typeof sourceEnd !== 'number') - throw errInvalidArgType('sourceEnd', 'of type number', sourceEnd); - sourceEnd = validateWriteOffset(sourceEnd, this.length, 'sourceEnd'); - } + else sourceEnd = validateOffsetArg(sourceEnd, 'sourceEnd', this.length); if (sourceStart >= sourceEnd && targetStart >= targetEnd) return 0; if (sourceStart >= sourceEnd) return -1; if (targetStart >= targetEnd) return 1; @@ -704,15 +698,9 @@ end = this.length; } if (start === undefined) start = 0; - else { - if (typeof start !== 'number') throw errInvalidArgType('offset', 'of type number', start); - start = validateWriteOffset(start, K_MAX_LENGTH, 'offset'); - } + else start = validateOffsetArg(start, 'offset', K_MAX_LENGTH); if (end === undefined) end = this.length; - else { - if (typeof end !== 'number') throw errInvalidArgType('end', 'of type number', end); - end = validateWriteOffset(end, this.length, 'end'); - } + else end = validateOffsetArg(end, 'end', this.length); if (end <= start) return this; var bytes; if (typeof value === 'string') { diff --git a/pkg/runtime/js/internal/events.js b/pkg/runtime/js/internal/events.js index 8963ce4..a84eff3 100644 --- a/pkg/runtime/js/internal/events.js +++ b/pkg/runtime/js/internal/events.js @@ -275,6 +275,16 @@ // once(emitter, name[, options]) — resolves with the emitted arguments on the next // matching event, or rejects if 'error' fires first or options.signal aborts. function once(emitter, name, options) { + // Node's validateObject(options, 'options'): undefined is the only non-object + // allowed; null, scalars, arrays and functions reject (node 26.13.0, probed; + // pinned by tests/node-compat/cases/72-events-parity.js). Rejects rather than + // throws because node's once is an async function. + if ( + options !== undefined && + (options === null || typeof options !== 'object' || P.ArrayIsArray(options)) + ) { + return P.Promise.reject(errOptionsType(options)); + } var signal = options && options.signal; if (signal !== undefined) { if (!isAbortSignal(signal)) { @@ -337,6 +347,26 @@ return e; } + // The ERR_INVALID_ARG_TYPE "Received …" tail for once()'s options. typeError above + // prints a bare `typeof`, which is wrong for node's template; only the cases + // validateObject can reach here are rendered (no objects other than arrays). + function errOptionsType(v) { + var t = typeof v; + var got; + if (v === null) got = 'null'; + else if (t === 'function') got = 'function ' + v.name; + else if (t === 'object') got = 'an instance of Array'; + else if (t === 'string') + got = + "type string ('" + (v.length > 28 ? P.StringPrototypeSlice(v, 0, 25) + '...' : v) + "')"; + else if (t === 'symbol') got = 'type symbol (' + P.String(v) + ')'; + else + got = 'type ' + t + ' (' + (v === 0 && 1 / v < 0 ? '-0' : v) + (t === 'bigint' ? 'n)' : ')'); + var e = new P.TypeError('The "options" argument must be of type object. Received ' + got); + e.code = 'ERR_INVALID_ARG_TYPE'; + return e; + } + function isAbortSignal(s) { return s != null && typeof s.addEventListener === 'function' && 'aborted' in s; } diff --git a/pkg/runtime/typed_array.odin b/pkg/runtime/typed_array.odin index af661d2..d255c39 100644 --- a/pkg/runtime/typed_array.odin +++ b/pkg/runtime/typed_array.odin @@ -118,16 +118,14 @@ jsc_buffer_deallocator :: proc "c" (bytes: rawptr, deallocator_context: rawptr) make_uint8_array :: proc(ctx: jsc.JSContextRef, data: []byte) -> jsc.JSValueRef { ptr := raw_data(data) n := len(data) - pad: []byte if ptr == nil { - pad = make([]byte, 1, runtime.heap_allocator()) + pad := make([]byte, 1, runtime.heap_allocator()) ptr = raw_data(pad) n = 0 } + // No free on a nil return: JSC already ran jsc_buffer_deallocator (see + // make_uint8_array_uninit). array := jsc.make_uint8_nocopy_locked(ctx, ptr, c.size_t(n), jsc_buffer_deallocator) - if array == nil && len(pad) > 0 { - delete(pad, runtime.heap_allocator()) - } return cast(jsc.JSValueRef)array } @@ -135,7 +133,12 @@ MAX_UNINIT_ALLOC :: 0x7fff_ffff // 2^31 - 1 bytes // make_uint8_array_uninit allocates `size` uninitialized bytes on runtime.heap_allocator() // (matching jsc_buffer_deallocator) and hands them to JavaScriptCore as a NoCopy Uint8Array. -// Frees the backing slice if JSC declines to take ownership. +// A nil return needs no free here: JSTypedArray.h documents for +// JSObjectMakeTypedArrayWithBytesNoCopy that "If an exception is thrown during this +// function the bytesDeallocator will always be called" (MacOSX.sdk JavaScriptCore +// Headers/JSTypedArray.h:61) — the bytes are wrapped in ArrayBuffer::createFromBytes +// with the deallocator as its destructor before the typed array is created, so a +// failed create drops that buffer and frees them. Deleting again is a double free. make_uint8_array_uninit :: proc(ctx: jsc.JSContextRef, size: int) -> (jsc.JSValueRef, bool) { if size <= 0 || size > MAX_UNINIT_ALLOC do return nil, false heap := runtime.heap_allocator() @@ -147,9 +150,6 @@ make_uint8_array_uninit :: proc(ctx: jsc.JSContextRef, size: int) -> (jsc.JSValu c.size_t(size), jsc_buffer_deallocator, ) - if array == nil { - delete(data, heap) - return nil, false - } + if array == nil do return nil, false return cast(jsc.JSValueRef)array, true } diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 8d28762..0385f7e 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1740,7 +1740,51 @@ "replace": " return Buffer.allocUnsafe(arg);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", "expect_detail": [ - "buf=8192" + "buf=262144" + ] + }, + { + "name": "73-buffer-type-fill-bigint dies when compare bounds targetStart by the target length", + "why": "node bounds compare's *Start by kMaxLength only: Buffer.alloc(3).compare(Buffer.alloc(3), 100) is 1, not a RangeError.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "validateOffsetArg(targetStart, 'targetStart', K_MAX_LENGTH)", + "replace": "validateOffsetArg(targetStart, 'targetStart', target.length)", + "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", + "expect_detail": [ + "The value of \"targetStart\" is out of range" + ] + }, + { + "name": "73-buffer-type-fill-bigint dies when write bounds length by the bytes remaining after offset", + "why": "node validates write's length against this.length and then clamps: Buffer.alloc(10).write('abcdef', 5, 8) is 5.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": "max = validateOffsetArg(length, 'length', this.length);", + "replace": "max = validateOffsetArg(length, 'length', remaining);", + "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", + "expect_detail": [ + "It must be >= 0 && <= 5. Received 8" + ] + }, + { + "name": "73-buffer-type-fill-bigint dies when write stops clamping length to the remaining bytes", + "why": "an in-range length past the end must be clamped; the hex path copies min(bytes, length) and overruns the view without it.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " if (max > remaining) max = remaining;\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", + "expect_detail": [ + "lava: write(hex, 5, 8, \"hex\") => THREW RangeError" + ] + }, + { + "name": "72-events-parity dies when once() stops validating options", + "why": "node's events.once rejects a non-object options with ERR_INVALID_ARG_TYPE; without the check once(ee, 'foo', 5) just waits.", + "source": "pkg/runtime/js/internal/events.js", + "find": " return P.Promise.reject(errOptionsType(options));", + "replace": " options = undefined;", + "gate": "compat:tests/node-compat/cases/72-events-parity.js", + "expect_detail": [ + "bad options 5: TypeError ERR_INVALID_ARG_TYPE" ] }, { diff --git a/tests/node-compat/cases/72-events-parity.js b/tests/node-compat/cases/72-events-parity.js index 8a99f35..4dc8403 100644 --- a/tests/node-compat/cases/72-events-parity.js +++ b/tests/node-compat/cases/72-events-parity.js @@ -51,6 +51,22 @@ async function testOnceSignal() { } catch (e) { console.log('bad signal:', e.name, e.code); } + + // options itself is validateObject'd: null, scalars, arrays and functions reject. + for (const [label, opts] of [ + ['5', 5], + ['null', null], + ["'x'", 'x'], + ['[]', []], + ['function f', function f() {}], + ]) { + try { + await once(ee, 'foo', opts); + console.log('bad options', label, 'UNEXPECTED OK'); + } catch (e) { + console.log('bad options', label + ':', e.name, e.code, e.message); + } + } } function testSetMaxListeners() { diff --git a/tests/node-compat/cases/73-buffer-type-fill-bigint.js b/tests/node-compat/cases/73-buffer-type-fill-bigint.js index f72d3a8..8fca4da 100644 --- a/tests/node-compat/cases/73-buffer-type-fill-bigint.js +++ b/tests/node-compat/cases/73-buffer-type-fill-bigint.js @@ -39,6 +39,16 @@ show('write("a", 0, -1)', () => b().write('a', 0, -1)); show('compare(null)', () => b().compare(null)); show('compare(b, null)', () => b().compare(b(), null)); show('compare(b, 1n)', () => b().compare(b(), 1n)); +// *Start is bounded by kMaxLength, not the buffer length; only *End is length-bounded. +show('compare(b, 100)', () => Buffer.alloc(3).compare(Buffer.alloc(3), 100)); +show('compare(b, 0, 3, 100)', () => Buffer.alloc(3).compare(Buffer.alloc(3), 0, 3, 100)); +show('compare(b, 0, 4)', () => Buffer.alloc(3).compare(Buffer.alloc(3), 0, 4)); +show('compare(b, 0, 3, 0, 4)', () => Buffer.alloc(3).compare(Buffer.alloc(3), 0, 3, 0, 4)); +// write's length is validated against this.length, then clamped to the remaining bytes. +show('write("abcdef", 5, 8)', () => Buffer.alloc(10).write('abcdef', 5, 8)); +show('write("abc", 0, 11)', () => Buffer.alloc(10).write('abc', 0, 11)); +show('write("abc", 2, 11)', () => Buffer.alloc(10).write('abc', 2, 11)); +show('write(hex, 5, 8, "hex")', () => Buffer.alloc(10).write('aabbccddeeff', 5, 8, 'hex')); show('indexOf(null)', () => b().indexOf(null)); show('indexOf(1n)', () => b().indexOf(1n)); show('indexOf(Symbol)', () => b().indexOf(Symbol('s'))); From 85a2319342a20d20675e828dde1bf569219013e9 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 01:50:08 +0200 Subject: [PATCH 17/61] fix(runtime): close PR #413 pr-gate findings - buffer.writeBig(U)Int64: range check, then offset check, then coercion, in Node's order; Object(5n) is accepted again, out-of-range and bad offsets get coded errors. - atob/btoa: ERR_MISSING_ARGS and HTML forgiving-base64 validation; invalid input throws InvalidCharacterError (code 5). Deviates: not instanceof DOMException (Lava has none), pinned by cmd/lava/atob_invalid_character_test.odin. - util.isDeepStrictEqual: Map/Set primitive keys looked up directly, pairwise scan only for object keys (16k-entry Map: 2179 -> 4 ms, medians of 5 interleaved launches, darwin arm64). - buffer.fill: numbers take the & 0xff path first (fill(i,0,32) x3e6: 1111 -> 989 ms, same method). - primordials baseline lowered; four new recorded mutations. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/atob_invalid_character_test.odin | 54 ++++++++ pkg/runtime/js/internal/buffer.js | 127 ++++++++++++++---- pkg/runtime/js/internal/util.js | 71 +++++----- tests/mutation-manifest.json | 55 ++++++++ tests/node-compat/cases/03-buffer.js | 32 +++++ .../cases/71-assert-deep-strict-equal.js | 21 +++ .../cases/73-buffer-type-fill-bigint.js | 19 +++ tests/node-compat/pollution-baseline.json | 4 +- 8 files changed, 321 insertions(+), 62 deletions(-) create mode 100644 cmd/lava/atob_invalid_character_test.odin diff --git a/cmd/lava/atob_invalid_character_test.odin b/cmd/lava/atob_invalid_character_test.odin new file mode 100644 index 0000000..5e36409 --- /dev/null +++ b/cmd/lava/atob_invalid_character_test.odin @@ -0,0 +1,54 @@ +#+build linux, darwin +package main + +import "core:testing" +import lava "lava:pkg/runtime" +import eventloop "lava:pkg/runtime/eventloop" + +// Pins the shape of the error atob/btoa throw for a bad character, which is a +// deliberate deviation the node-compat oracle cannot express: Node throws a +// DOMException, Lava has no DOMException (the same gap structured_clone.js's +// DataCloneError documents), so it throws a plain Error carrying the +// DOMException's name, legacy numeric code and Node's exact message. +// tests/node-compat/cases/03-buffer.js compares name/code/message against node; +// this test pins the part node cannot oracle — that the value IS an Error and +// that there is no DOMException for it to be an instance of. When Lava grows a +// DOMException this test is what has to change, deliberately. +ATOB_INVALID_CHARACTER_SCRIPT :: ` +'use strict'; +function check(label, fn, message) { + let e; + try { + fn(); + } catch (err) { + e = err; + } + const got = e === undefined ? 'no throw' : + 'name=' + e.name + ' code=' + typeof e.code + ':' + e.code + ' message=' + e.message + + ' isError=' + (e instanceof Error); + const want = 'name=InvalidCharacterError code=number:5 message=' + message + ' isError=true'; + if (got !== want) throw new Error(label + ': got ' + got + ', want ' + want); +} +check('atob', () => atob('!'), 'Invalid character'); +check('atob-length', () => atob('Z'), 'The string to be decoded is not correctly encoded.'); +check('btoa', () => btoa('Ā'), 'Invalid character'); +if (typeof DOMException !== 'undefined') throw new Error('DOMException now exists: revisit atob/btoa'); +console.log('atob-invalid-character ok'); +` + +@(test) +atob_invalid_character_shape :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(ATOB_INVALID_CHARACTER_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index b378b1e..d9e31fb 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -506,23 +506,24 @@ return viewOf(buf).getBigInt64(offset, littleEndian); } + // Node's writeBigU_Int64* order (lib/internal/buffer.js checkInt): the range compare + // runs first on the raw value, then the offset bounds, and only then `value & mask`, + // whose mixed-type `&` is what throws "Cannot mix BigInt and other types". So a boxed + // BigInt (Object(5n)) writes, an out-of-range number reports ERR_OUT_OF_RANGE, a bad + // offset beats a non-BigInt value, and a Symbol throws from the compare. The mask + // also yields the two's-complement u64 for a negative signed value, so both widths + // store through setBigUint64. Pinned by tests/node-compat/cases/73-buffer-type-fill-bigint.js. function writeBigUInt(buf, value, offset, littleEndian) { - if (typeof value === 'symbol') Number(value); // throws TypeError on Symbol, matching Node - if (typeof value !== 'bigint') - throw new TypeError('Cannot mix BigInt and other types, use explicit conversions'); checkValueInt(value, BIG_0, BIG_MAX_U64, 7); offset = checkBounds(buf, offset, 8); - viewOf(buf).setBigUint64(offset, value, littleEndian); + viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); return offset + 8; } function writeBigInt(buf, value, offset, littleEndian) { - if (typeof value === 'symbol') Number(value); // throws TypeError on Symbol, matching Node - if (typeof value !== 'bigint') - throw new TypeError('Cannot mix BigInt and other types, use explicit conversions'); checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7); offset = checkBounds(buf, offset, 8); - viewOf(buf).setBigInt64(offset, value, littleEndian); + viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); return offset + 8; } @@ -703,19 +704,28 @@ else end = validateOffsetArg(end, 'end', this.length); if (end <= start) return this; var bytes; - if (typeof value === 'string') { - bytes = strToBytes(value, encoding || 'utf8'); - if (bytes.length === 0) { - if (value.length !== 0) throw errInvalidArgValue('value', value); - u8Fill.call(this, 0, start, end); - return this; + // Numbers skip the string and instanceof tests (the hot case) and land in the + // same `& 0xff` fill as every other non-string, non-Uint8Array value, so the order + // is unobservable. Behind those tests, 3e6 fill(i, 0, 32) on a 64-byte buffer took + // 1111 ms; ahead of them, 989 ms (medians of 5 interleaved launches per arm, + // darwin arm64, -o:speed). One shared fill site rather than an early-return copy, + // so the primordials invoke count does not grow. + if (typeof value !== 'number') { + if (typeof value === 'string') { + bytes = strToBytes(value, encoding || 'utf8'); + if (bytes.length === 0) { + if (value.length !== 0) throw errInvalidArgValue('value', value); + u8Fill.call(this, 0, start, end); + return this; + } + } else if (value instanceof Uint8Array) { + if (value.length === 0) throw errInvalidArgValue('value', value); + bytes = value; } - } else if (value instanceof Uint8Array) { - if (value.length === 0) throw errInvalidArgValue('value', value); - bytes = value; - } else { - // Non-string, non-Uint8Array values (including null, undefined, objects, booleans, - // numbers, BigInt, Symbol) coerce via ToNumber/ToInt32 (& 0xff), matching Node: + } + if (bytes === undefined) { + // Numbers and every other value (null, undefined, objects, booleans, BigInt, + // Symbol) coerce via ToNumber/ToInt32 (& 0xff), matching Node: // null/undefined/{} -> 0, true -> 1, BigInt/Symbol -> TypeError. u8Fill.call(this, value & 0xff, start, end); return this; @@ -1355,14 +1365,79 @@ createPool(); - /** Base64 → latin1 (Node global atob polyfill surface). */ - function atob(data) { - return Buffer.from(StringG(data), 'base64').toString('latin1'); + function errMissingInput() { + var e = new TypeError('The "input" argument must be specified'); + e.code = 'ERR_MISSING_ARGS'; + return e; + } + + // Node throws a DOMException here; Lava has no DOMException (same gap as + // structured_clone.js's DataCloneError), so this is an Error carrying the + // DOMException's name and legacy numeric code. + function errInvalidCharacter(message) { + var e = new Error(message); + e.name = 'InvalidCharacterError'; + e.code = 5; + return e; + } + + /** + * Base64 → latin1 per the HTML forgiving-base64 decode (global atob and node:buffer's + * atob are the same function, as in Node). + * @param {*} input Stringified with a template literal, like Node (`${input}`). + * @returns {string} + * @throws {TypeError} ERR_MISSING_ARGS — called with no argument. + * @throws {Error} name InvalidCharacterError, code 5 — a character outside + * A-Za-z0-9+/= and ASCII whitespace, '=' not at the end or more than two, + * '=' when the length is not a multiple of 4, or a lone trailing character. + * @node Mirrors lib/buffer.js atob: whitespace is TAB LF FF CR SPACE (not VT); the + * messages are 'Invalid character' and 'The string to be decoded is not + * correctly encoded.' (node 26.13.0, tests/node-compat/cases/03-buffer.js). + * @deviates The error is an Error, not a DOMException (Lava has none); name, code + * and message match. Pinned by cmd/lava/atob_invalid_character_test.odin. + */ + function atob(input) { + if (arguments.length === 0) throw errMissingInput(); + input = `${input}`; + var nonWhitespace = 0; + var equals = 0; + for (var i = 0; i < input.length; i++) { + var c = StringPrototypeCharCodeAt(input, i); + if (c === 0x09 || c === 0x0a || c === 0x0c || c === 0x0d || c === 0x20) continue; + var alnum = (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39); + if (!alnum && c !== 0x2b && c !== 0x2f && c !== 0x3d) + throw errInvalidCharacter('Invalid character'); + nonWhitespace++; + if (c === 0x3d) equals++; + else if (equals) throw errInvalidCharacter('Invalid character'); // '=' only at the end + if (equals > 2) throw errInvalidCharacter('Invalid character'); + } + var remainder = nonWhitespace % 4; + if (!remainder) remainder = (nonWhitespace - equals) % 4; + else if (equals) throw errInvalidCharacter('Invalid character'); + if (remainder === 1) + throw errInvalidCharacter('The string to be decoded is not correctly encoded.'); + return Buffer.from(input, 'base64').toString('latin1'); } - /** latin1 → Base64 (Node global btoa polyfill surface). */ - function btoa(data) { - return Buffer.from(StringG(data), 'latin1').toString('base64'); + /** + * latin1 → Base64 (global btoa and node:buffer's btoa are the same function). + * @param {*} input Stringified with a template literal, like Node. + * @returns {string} + * @throws {TypeError} ERR_MISSING_ARGS — called with no argument. + * @throws {Error} name InvalidCharacterError, code 5 — a code unit above 0xFF. + * @node lib/buffer.js btoa (node 26.13.0, tests/node-compat/cases/03-buffer.js). + * @deviates Not a DOMException, as for atob; pinned by + * cmd/lava/atob_invalid_character_test.odin. + */ + function btoa(input) { + if (arguments.length === 0) throw errMissingInput(); + input = `${input}`; + for (var i = 0; i < input.length; i++) { + if (StringPrototypeCharCodeAt(input, i) > 0xff) + throw errInvalidCharacter('Invalid character'); + } + return Buffer.from(input, 'latin1').toString('base64'); } /** @param {ArrayBuffer|Uint8Array} input @returns {boolean} */ diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js index dfe1093..6f2d677 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/internal/util.js @@ -398,14 +398,43 @@ return true; } - function collectionEqual(aEntries, bEntries, withValue, aStack, bStack) { - var used = new Array(bEntries.length); - for (var i = 0; i < aEntries.length; i++) { + // Map/Set equality as node's comparisons.js does it in strict mode: a primitive key + // (or Set member) can only deep-equal the identical primitive, so it is looked up + // directly with b.has/b.get — O(n). Only object keys/members need the pairwise + // deepStrict scan, and only against b's object keys. The pairwise scan over every + // entry was O(n^2): one assert.deepStrictEqual on two equal 16k int->int Maps took + // 2179 ms that way and 4 ms this way (medians of 5 interleaved launches, darwin arm64). + // Sizes are already known equal, and Map/Set normalize -0 and match NaN via + // SameValueZero, which is what deepStrict answers for those keys anyway. + function collectionEqual(a, b, isMap, aStack, bStack) { + var aObjects = []; + var ok = true; + a.forEach(function (v, k) { + if (!ok) return; + var key = isMap ? k : v; + if (typeof key === 'object' && key !== null) { + aObjects.push([key, v]); + } else if (!b.has(key)) { + ok = false; + } else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) { + ok = false; + } + }); + if (!ok) return false; + if (aObjects.length === 0) return true; + var bObjects = []; + b.forEach(function (v, k) { + var key = isMap ? k : v; + if (typeof key === 'object' && key !== null) bObjects.push([key, v]); + }); + if (aObjects.length !== bObjects.length) return false; + var used = new Array(bObjects.length); + for (var i = 0; i < aObjects.length; i++) { var found = false; - for (var j = 0; j < bEntries.length; j++) { + for (var j = 0; j < bObjects.length; j++) { if (used[j]) continue; - var keyOk = deepStrict(aEntries[i][0], bEntries[j][0], aStack, bStack); - var valOk = !withValue || deepStrict(aEntries[i][1], bEntries[j][1], aStack, bStack); + var keyOk = deepStrict(aObjects[i][0], bObjects[j][0], aStack, bStack); + var valOk = !isMap || deepStrict(aObjects[i][1], bObjects[j][1], aStack, bStack); if (keyOk && valOk) { used[j] = true; found = true; @@ -475,34 +504,8 @@ bStack.push(b); var result; - if (tag === '[object Map]') { - if (a.size !== b.size) { - result = false; - } else { - var aME = []; - var bME = []; - a.forEach(function (v, k) { - aME.push([k, v]); - }); - b.forEach(function (v, k) { - bME.push([k, v]); - }); - result = collectionEqual(aME, bME, true, aStack, bStack); - } - } else if (tag === '[object Set]') { - if (a.size !== b.size) { - result = false; - } else { - var aSE = []; - var bSE = []; - a.forEach(function (v) { - aSE.push([v]); - }); - b.forEach(function (v) { - bSE.push([v]); - }); - result = collectionEqual(aSE, bSE, false, aStack, bStack); - } + if (tag === '[object Map]' || tag === '[object Set]') { + result = a.size === b.size && collectionEqual(a, b, tag === '[object Map]', aStack, bStack); } else { result = keysEqual(a, b, aStack, bStack); } diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 0385f7e..7948349 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1787,6 +1787,61 @@ "bad options 5: TypeError ERR_INVALID_ARG_TYPE" ] }, + { + "name": "73-buffer-type-fill-bigint dies when writeBigInt rejects non-BigInt values before the range and offset checks", + "why": "node runs checkInt (range, then offset bounds) before the BigInt mask, so Object(5n) writes and a bad range/offset is reported as ERR_OUT_OF_RANGE / ERR_INVALID_ARG_TYPE rather than 'Cannot mix BigInt'.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", + "replace": " if (typeof value !== 'bigint') throw new TypeError('Cannot mix BigInt and other types, use explicit conversions');\n checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", + "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", + "expect_detail": [ + "lava: writeBigInt64LE(Object(5n)) => THREW TypeError" + ] + }, + { + "name": "03-buffer dies when atob stops rejecting characters outside the forgiving-base64 alphabet", + "why": "node's atob throws InvalidCharacterError 'Invalid character' for '!' and for VT; without the alphabet check they fall through to the length check.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " if (!alnum && c !== 0x2b && c !== 0x2f && c !== 0x3d)", + "replace": " if (false)", + "gate": "compat:tests/node-compat/cases/03-buffer.js", + "expect_detail": [ + "lava: atob [\"!\"] InvalidCharacterError 5 The string to be decoded is not correctly encoded." + ] + }, + { + "name": "03-buffer dies when btoa stops rejecting code units above 0xFF", + "why": "node's btoa throws InvalidCharacterError for 'Ā'; latin1 encoding would silently truncate it to 0x00.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " if (StringPrototypeCharCodeAt(input, i) > 0xff)", + "replace": " if (false)", + "gate": "compat:tests/node-compat/cases/03-buffer.js", + "expect_detail": [ + "lava: btoa [\"Ā\"] \"AA==\"" + ] + }, + { + "name": "71-assert-deep-strict-equal dies when a primitive Map key's values are not compared", + "why": "the O(n) b.get fast path must still deep-compare the values; skipping it makes Maps with equal keys and different values equal.", + "source": "pkg/runtime/js/internal/util.js", + "find": "} else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) {", + "replace": "} else if (false) {", + "gate": "compat:tests/node-compat/cases/71-assert-deep-strict-equal.js", + "expect_detail": [ + "lava: Map prim key, value diff: eq=true" + ] + }, + { + "name": "atob_invalid_character_shape dies when the InvalidCharacterError code stops being the number 5", + "why": "Lava has no DOMException, so atob/btoa throw an Error carrying DOMException's name and legacy NUMERIC code 5; console.log prints '5' and 5 alike, so only the Lava-only test sees the type.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " e.code = 5;\n", + "replace": " e.code = '5';\n", + "gate": "odin:cmd/lava:main.atob_invalid_character_shape", + "expect_detail": [ + "code=string:5" + ] + }, { "name": "run-gates dies when a failing gate no longer sets a non-zero exit code", "why": "make gates exists so a red gate cannot be hidden by a pipe's exit status (#360); returning status 0 on failure defeats the runner.", diff --git a/tests/node-compat/cases/03-buffer.js b/tests/node-compat/cases/03-buffer.js index 83e55f7..a7eff83 100644 --- a/tests/node-compat/cases/03-buffer.js +++ b/tests/node-compat/cases/03-buffer.js @@ -49,6 +49,38 @@ assert.equal(globalThis.atob, require('node:buffer').atob); assert.equal(globalThis.btoa, require('node:buffer').btoa); assert.equal(atob('Zm9v'), 'foo'); assert.equal(btoa('foo'), 'Zm9v'); +// HTML forgiving-base64: ASCII whitespace is skipped, '=' only at the end and only +// when it completes a quantum, a lone trailing char is an encoding error. Lava has no +// DOMException, so name/code/message are what is compared (see buffer.js atob). +for (const args of [ + [], + ['!'], + [' Zm9v '], + ['Zm9'], + ['Zm9v='], + ['Zm9v=='], + ['Zm8='], + ['Zm8=='], + ['Z'], + ['Zm9v\n\t\f\r'], + ['Zm9v\v'], + ['Zm 9v'], + [undefined], + [null], +]) { + try { + console.log('atob', JSON.stringify(args), JSON.stringify(atob(...args))); + } catch (e) { + console.log('atob', JSON.stringify(args), e.name, e.code, e.message); + } +} +for (const args of [[], ['\u0100'], ['\u00ff'], [null], [123]]) { + try { + console.log('btoa', JSON.stringify(args), JSON.stringify(btoa(...args))); + } catch (e) { + console.log('btoa', JSON.stringify(args), e.name, e.code, e.message); + } +} assert.equal(require('node:buffer').isAscii(Buffer.from('abc')), true); // Node 26 removed SlowBuffer (DEP0030 end-of-life): the export is gone, not deprecated. assert.equal('SlowBuffer' in require('node:buffer'), false, 'node:buffer has no SlowBuffer'); diff --git a/tests/node-compat/cases/71-assert-deep-strict-equal.js b/tests/node-compat/cases/71-assert-deep-strict-equal.js index a4eea7a..2878d44 100644 --- a/tests/node-compat/cases/71-assert-deep-strict-equal.js +++ b/tests/node-compat/cases/71-assert-deep-strict-equal.js @@ -15,6 +15,27 @@ checkEqual('Set objects equal', new Set([{ a: 1 }]), new Set([{ a: 1 }])); checkEqual('Set objects diff', new Set([{ a: 1 }]), new Set([{ a: 2 }])); checkEqual('Map obj keys equal', new Map([[{ k: 1 }, 2]]), new Map([[{ k: 1 }, 2]])); checkEqual('Map obj keys diff', new Map([[{ k: 1 }, 2]]), new Map([[{ k: 1 }, 3]])); +// Primitive keys/members are looked up directly; object ones are scanned pairwise. +checkEqual('Map prim key, value diff', new Map([['a', { x: 1 }]]), new Map([['a', { x: 2 }]])); +checkEqual('Map prim key, value deep eq', new Map([['a', { x: 1 }]]), new Map([['a', { x: 1 }]])); +checkEqual('Map prim key missing', new Map([['a', 1]]), new Map([['b', 1]])); +checkEqual('Map 1 vs "1" key', new Map([[1, 1]]), new Map([['1', 1]])); +checkEqual('Map NaN key', new Map([[NaN, 1]]), new Map([[NaN, 1]])); +checkEqual('Map -0 vs 0 key', new Map([[-0, 1]]), new Map([[0, 1]])); +checkEqual( + 'Map undefined value vs missing', + new Map([['a', undefined]]), + new Map([['b', undefined]]), +); +checkEqual('Set prim diff', new Set([1, 2]), new Set([1, 3])); +checkEqual('Set prim vs object', new Set([1, { a: 1 }]), new Set([{ a: 1 }, { a: 1 }])); +checkEqual('Set mixed reordered', new Set([{ a: 1 }, 'x', 2]), new Set([2, 'x', { a: 1 }])); +checkEqual('Set fn identity', new Set([() => 1]), new Set([() => 1])); +checkEqual( + 'Map 16k equal', + new Map(Array.from({ length: 16000 }, (_, i) => [i, i])), + new Map(Array.from({ length: 16000 }, (_, i) => [i, i])), +); checkEqual('Error equal', new Error('boom'), new Error('boom')); checkEqual('Error diff class', new TypeError('boom'), new Error('boom')); checkEqual('Boxed Number equal', new Number(1), new Number(1)); diff --git a/tests/node-compat/cases/73-buffer-type-fill-bigint.js b/tests/node-compat/cases/73-buffer-type-fill-bigint.js index 8fca4da..a8a2619 100644 --- a/tests/node-compat/cases/73-buffer-type-fill-bigint.js +++ b/tests/node-compat/cases/73-buffer-type-fill-bigint.js @@ -60,6 +60,25 @@ show('Buffer.copyBytesFrom(null)', () => Buffer.copyBytesFrom(null)); show('writeBigInt64LE(1)', () => b().writeBigInt64LE(1, 0)); show('writeBigInt64LE(Symbol)', () => b().writeBigInt64LE(Symbol('s'), 0)); show('writeBigUInt64BE(1)', () => b().writeBigUInt64BE(1, 0)); +// Node's order: range check, then offset bounds, then the BigInt mask that throws +// "Cannot mix" — so a boxed BigInt writes, and a bad range/offset wins over the type. +show('writeBigInt64LE(Object(5n))', () => b().writeBigInt64LE(Object(5n))); +show('writeBigUInt64BE(Object(5n)) bytes', () => { + const x = b(); + x.writeBigUInt64BE(Object(5n)); + return x; +}); +show('writeBigUInt64LE(2n ** 64n)', () => b().writeBigUInt64LE(2n ** 64n)); +show('writeBigUInt64LE(-1)', () => b().writeBigUInt64LE(-1)); +show('writeBigInt64LE(2 ** 70)', () => b().writeBigInt64LE(2 ** 70)); +show('writeBigInt64LE(1, "x")', () => b().writeBigInt64LE(1, 'x')); +show('writeBigInt64LE(1, 9)', () => b().writeBigInt64LE(1, 9)); +show('writeBigInt64LE("5")', () => b().writeBigInt64LE('5')); +show('writeBigInt64BE(-1n) bytes', () => { + const x = b(); + x.writeBigInt64BE(-1n); + return x; +}); show('toString(Symbol)', () => b().toString(Symbol('s'))); show('toString("utf8", 1n)', () => b().toString('utf8', 1n)); show('toString("utf8", 10n)', () => b().toString('utf8', 10n)); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 561fbad..2627473 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -21,7 +21,7 @@ "method": 41, "invoke": 6, "accessor": 22, - "global": 129 + "global": 127 }, "internal/console.js": { "method": 0, @@ -246,7 +246,7 @@ "global": 24 }, "internal/util.js": { - "method": 50, + "method": 46, "invoke": 14, "accessor": 15, "global": 72 From b81f8124cb21cc6868115dc18b564eed24a5ccba Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:30:10 +0200 Subject: [PATCH 18/61] perf(buffer): recover atob/btoa and fill cost from PR #413 validation - atob/btoa: one non-global regex pass via P.RegExpMatches as the fast path; the per-char loop now runs only to pick the error. btoa 48 KiB x200: 615 -> 89 ms; atob 64 KiB x200: 2542 -> 1579 ms (base 1325, which did no validation). - atob: check the unpadded remainder before padding, as node does ('a=' / 'a==' / 'abcde=' report "not correctly encoded"). - fill: offset/end checks inlined at the call site; fill(i,0,32) x3e6: 954 -> 496 ms (base 738). Method: base 8619888 / 85a2319 / this commit, 5 interleaved launches per arm, medians, darwin arm64, -o:speed. Co-Authored-By: Claude Opus 5.5 --- pkg/runtime/js/internal/buffer.js | 76 ++++++++++++++++++++-------- tests/mutation-manifest.json | 26 +++++++++- tests/node-compat/cases/03-buffer.js | 5 ++ 3 files changed, 84 insertions(+), 23 deletions(-) diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index d9e31fb..3865fc1 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -698,10 +698,21 @@ encoding = end; end = this.length; } + // validateOffsetArg's checks, inlined: the extra call layer per offset was the + // whole residual on fill(i, 0, 32) — 3e6 calls on a 64-byte buffer took 954 ms + // through validateOffsetArg and 496 inlined, base 8619888 738 (medians of 5 + // interleaved launches per arm, darwin arm64, -o:speed). The valid case only compares; anything else (fraction, NaN, ±Infinity, + // out of range) goes to validateWriteOffset, which picks and builds the error. + // `% 1` rather than Math.floor keeps the live `Math` global read out (§5). if (start === undefined) start = 0; - else start = validateOffsetArg(start, 'offset', K_MAX_LENGTH); + else if (typeof start !== 'number') + throw errInvalidArgType('offset', 'of type number', start); + else if (start % 1 !== 0 || start < 0 || start > K_MAX_LENGTH) + validateWriteOffset(start, K_MAX_LENGTH, 'offset'); if (end === undefined) end = this.length; - else end = validateOffsetArg(end, 'end', this.length); + else if (typeof end !== 'number') throw errInvalidArgType('end', 'of type number', end); + else if (end % 1 !== 0 || end < 0 || end > this.length) + validateWriteOffset(end, this.length, 'end'); if (end <= start) return this; var bytes; // Numbers skip the string and instanceof tests (the hot case) and land in the @@ -1381,6 +1392,19 @@ return e; } + // One native regex scan instead of a per-char StringPrototypeCharCodeAt loop + // (~63 ns/char through the caller1 wrapper). 200 btoa calls on 48 KiB of latin1 + // took 615 ms that way and 89 ms with the regex; 200 atob calls on 64 KiB of + // base64, 2542 vs 1579 ms (medians of 5 interleaved launches per arm, darwin + // arm64, -o:speed). Most of what is left is the Buffer round trip, not this + // check. Non-global, so RegExpMatches never touches lastIndex (§5). + // ATOB_SLOW_RE matches anything the fast path cannot take as-is: whitespace or a + // non-alphabet character, a '=' followed by anything but '=', or three '='; atob + // then falls back to the exact per-char loop, so error selection and messages + // are unchanged. + var ATOB_SLOW_RE = /[^A-Za-z0-9+/=]|=[^=]|===/; + var NON_LATIN1_RE = /[^\u0000-\u00ff]/; + /** * Base64 → latin1 per the HTML forgiving-base64 decode (global atob and node:buffer's * atob are the same function, as in Node). @@ -1401,22 +1425,35 @@ input = `${input}`; var nonWhitespace = 0; var equals = 0; - for (var i = 0; i < input.length; i++) { - var c = StringPrototypeCharCodeAt(input, i); - if (c === 0x09 || c === 0x0a || c === 0x0c || c === 0x0d || c === 0x20) continue; - var alnum = (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39); - if (!alnum && c !== 0x2b && c !== 0x2f && c !== 0x3d) - throw errInvalidCharacter('Invalid character'); - nonWhitespace++; - if (c === 0x3d) equals++; - else if (equals) throw errInvalidCharacter('Invalid character'); // '=' only at the end - if (equals > 2) throw errInvalidCharacter('Invalid character'); - } - var remainder = nonWhitespace % 4; - if (!remainder) remainder = (nonWhitespace - equals) % 4; - else if (equals) throw errInvalidCharacter('Invalid character'); - if (remainder === 1) + if (!P.RegExpMatches(ATOB_SLOW_RE, input)) { + // Fast path: only alphabet characters and at most two trailing '='. The + // remainder rules below still apply. + nonWhitespace = input.length; + if (nonWhitespace > 0 && StringPrototypeCharCodeAt(input, nonWhitespace - 1) === 0x3d) { + equals = 1; + if (nonWhitespace > 1 && StringPrototypeCharCodeAt(input, nonWhitespace - 2) === 0x3d) + equals = 2; + } + } else + for (var i = 0; i < input.length; i++) { + var c = StringPrototypeCharCodeAt(input, i); + if (c === 0x09 || c === 0x0a || c === 0x0c || c === 0x0d || c === 0x20) continue; + var alnum = + (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || (c >= 0x30 && c <= 0x39); + if (!alnum && c !== 0x2b && c !== 0x2f && c !== 0x3d) + throw errInvalidCharacter('Invalid character'); + nonWhitespace++; + if (c === 0x3d) equals++; + else if (equals) throw errInvalidCharacter('Invalid character'); // '=' only at the end + if (equals > 2) throw errInvalidCharacter('Invalid character'); + } + // The lone-trailing-character test runs on the length WITHOUT the '=' and wins + // over the padding test: node 26.13.0 answers 'not correctly encoded' for 'a=', + // 'a==' and 'abcde=', and 'Invalid character' for 'ab=', 'abc==' and 'Zm9v=' + // (probed; pinned by tests/node-compat/cases/03-buffer.js). + if ((nonWhitespace - equals) % 4 === 1) throw errInvalidCharacter('The string to be decoded is not correctly encoded.'); + if (equals && nonWhitespace % 4) throw errInvalidCharacter('Invalid character'); return Buffer.from(input, 'base64').toString('latin1'); } @@ -1433,10 +1470,7 @@ function btoa(input) { if (arguments.length === 0) throw errMissingInput(); input = `${input}`; - for (var i = 0; i < input.length; i++) { - if (StringPrototypeCharCodeAt(input, i) > 0xff) - throw errInvalidCharacter('Invalid character'); - } + if (P.RegExpMatches(NON_LATIN1_RE, input)) throw errInvalidCharacter('Invalid character'); return Buffer.from(input, 'latin1').toString('base64'); } diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 7948349..02ed97e 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1813,8 +1813,8 @@ "name": "03-buffer dies when btoa stops rejecting code units above 0xFF", "why": "node's btoa throws InvalidCharacterError for 'Ā'; latin1 encoding would silently truncate it to 0x00.", "source": "pkg/runtime/js/internal/buffer.js", - "find": " if (StringPrototypeCharCodeAt(input, i) > 0xff)", - "replace": " if (false)", + "find": " var NON_LATIN1_RE = /[^\\u0000-\\u00ff]/;", + "replace": " var NON_LATIN1_RE = /[^\\u0000-\\uffff]/;", "gate": "compat:tests/node-compat/cases/03-buffer.js", "expect_detail": [ "lava: btoa [\"Ā\"] \"AA==\"" @@ -1842,6 +1842,28 @@ "code=string:5" ] }, + { + "name": "03-buffer dies when the atob fast-path regex lets a non-alphabet character through", + "why": "the regex fast path skips the per-char loop; if it admits '!' or VT, atob reports the length error instead of node's 'Invalid character'.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " var ATOB_SLOW_RE = /[^A-Za-z0-9+/=]|=[^=]|===/;", + "replace": " var ATOB_SLOW_RE = /=[^=]|===/;", + "gate": "compat:tests/node-compat/cases/03-buffer.js", + "expect_detail": [ + "lava: atob [\"!\"] InvalidCharacterError 5 The string to be decoded is not correctly encoded." + ] + }, + { + "name": "03-buffer dies when atob checks '=' padding before the lone-trailing-character rule", + "why": "node 26 reports 'not correctly encoded' for 'a=', 'a==' and 'abcde=': the remainder without '=' is tested first.", + "source": "pkg/runtime/js/internal/buffer.js", + "find": " if ((nonWhitespace - equals) % 4 === 1)\n throw errInvalidCharacter('The string to be decoded is not correctly encoded.');\n if (equals && nonWhitespace % 4) throw errInvalidCharacter('Invalid character');\n", + "replace": " if (equals && nonWhitespace % 4) throw errInvalidCharacter('Invalid character');\n if ((nonWhitespace - equals) % 4 === 1)\n throw errInvalidCharacter('The string to be decoded is not correctly encoded.');\n", + "gate": "compat:tests/node-compat/cases/03-buffer.js", + "expect_detail": [ + "lava: atob [\"a=\"] InvalidCharacterError 5 Invalid character" + ] + }, { "name": "run-gates dies when a failing gate no longer sets a non-zero exit code", "why": "make gates exists so a red gate cannot be hidden by a pipe's exit status (#360); returning status 0 on failure defeats the runner.", diff --git a/tests/node-compat/cases/03-buffer.js b/tests/node-compat/cases/03-buffer.js index a7eff83..b697022 100644 --- a/tests/node-compat/cases/03-buffer.js +++ b/tests/node-compat/cases/03-buffer.js @@ -62,6 +62,11 @@ for (const args of [ ['Zm8='], ['Zm8=='], ['Z'], + ['a='], + ['a=='], + ['abcde='], + ['ab='], + ['abc=='], ['Zm9v\n\t\f\r'], ['Zm9v\v'], ['Zm 9v'], From dd627971af93dbe66f0a30fadf6c476e5b3a46cf Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:23:23 +0200 Subject: [PATCH 19/61] docs(compat): retarget node 24 verification citations to node 26 The oracle moved to node 26.13.0 in #412, but 60 comments still cited node 24 as the version a behavior was verified against. Each claim was re-verified on node 26.13.0 (15 cited cases byte-identical node vs lava, the rest by direct probe); none diverged, and the citations now name node 26 and how they were checked. Kept on purpose: historical node-24-vs-26 contrasts, bench/thresholds.json calibration history, the gen-matrix PROBED_NODE label (restamping it without a re-curated probe would claim a probe that never happened), and two claims not reproducible on darwin (fs.odin:996 Windows runner, encoding.js:712 Linux RSS). Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 4 ++-- pkg/jsc/private_view.odin | 2 +- pkg/runtime/globals.odin | 2 +- pkg/runtime/host_natives.odin | 3 ++- pkg/runtime/js/console.js | 3 ++- pkg/runtime/js/internal/buffer.js | 6 +++--- pkg/runtime/js/internal/console.js | 3 ++- pkg/runtime/js/internal/encoding.js | 12 +++++++----- pkg/runtime/js/internal/fs.js | 19 ++++++++++--------- pkg/runtime/js/internal/mime.js | 5 +++-- pkg/runtime/js/internal/os.js | 2 +- pkg/runtime/js/internal/primordials.js | 2 +- pkg/runtime/js/internal/process.js | 3 ++- pkg/runtime/js/internal/sqlite.js | 8 ++++---- pkg/runtime/js/internal/stdio.js | 10 +++++----- pkg/runtime/js/internal/stream.js | 2 +- pkg/runtime/js/internal/structured_clone.js | 2 +- pkg/runtime/js/internal/util.js | 7 ++++--- pkg/runtime/require.odin | 5 +++-- pkg/runtime/runtime.odin | 2 +- pkg/runtime/sqlite.odin | 4 ++-- pkg/runtime/typed_array.odin | 2 +- pkg/std/sqlite/sqlite.odin | 4 ++-- reference/node-compatibility.md | 2 +- scripts/run-mutations.mjs | 2 +- scripts/run-sqlite-oracle.sh | 2 +- tests/mutation-manifest.json | 6 +++--- tests/node-compat/cases/46-util-mimetype.js | 2 +- .../node-compat/cases/57-format-single-arg.js | 2 +- tests/node-compat/cases/60-process-stdio.js | 2 +- .../cases/66-process-console-intrinsic.js | 2 +- .../67-process-console-deleted-global.js | 2 +- tests/std/sqlite/cases/05-coercion.js | 2 +- tests/stdio/stdio-lifecycle.test.mjs | 2 +- tests/stdio/uncaught-before-teardown.test.mjs | 2 +- 35 files changed, 75 insertions(+), 65 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index a06a6b6..2d02802 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -171,7 +171,7 @@ the comment block verbatim, so this is the doc source, not decoration. // and the caller must fall back to JSObjectMakeFunctionWithCallback. // Node: // setTimeout/setInterval report `.length` 2, every other global 1 — verified -// against node 24, not read off the docs. +// against node 26, not read off the docs. // Deviates: // The C-API fallback reports 0 and is not repairable through the public API. // Pinned by tests/node-compat/cases/56-native-function-arity.js. @@ -285,7 +285,7 @@ Rules that keep this from becoming ceremony: * @returns {string} * @throws {TypeError} ERR_INVALID_ARG_TYPE — options is a non-null scalar. * @node Options are validated BEFORE the input is converted; `decode(5, 5)` - * reports the options (node 24, verified). + * reports the options (node 26, verified). * @deviates none */ ``` diff --git a/pkg/jsc/private_view.odin b/pkg/jsc/private_view.odin index 3132ee4..31a9ad5 100644 --- a/pkg/jsc/private_view.odin +++ b/pkg/jsc/private_view.odin @@ -366,7 +366,7 @@ when ODIN_OS == .Linux { // unavailable — and the caller must use the C API. // Node: // A length-tracking view's length follows its buffer, and a fixed view - // the buffer has shrunk under reads as length 0 (node 24.21, verified by + // the buffer has shrunk under reads as length 0 (node 26.13.0, verified by // tests/node-compat/cases/68-resizable-view-bytes.js). Declining those // views is what keeps this path from answering either wrongly. // diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 1d0e0c9..8f22db3 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -691,7 +691,7 @@ output_mutex: sync.Mutex // Node: // node writes files and TTYs synchronously on POSIX and QUEUES pipes on its loop, // flushing before a natural exit. On a natural exit `console.log('x'.repeat(300000))` -// delivers 300001 bytes where Lava delivered 65536 (node 24.18.1, parent-set O_NONBLOCK +// delivers 300001 bytes where Lava delivered 65536 (node 26.13.0, parent-set O_NONBLOCK // pipe). It is not a blanket "node never truncates": across `process.exit(0)` node drops // its queued write and delivers 65536 on the same harness — the exact signature this // fix removes from Lava. diff --git a/pkg/runtime/host_natives.odin b/pkg/runtime/host_natives.odin index 65b34cd..a79a298 100644 --- a/pkg/runtime/host_natives.odin +++ b/pkg/runtime/host_natives.odin @@ -265,7 +265,8 @@ host_dispatch_fail :: #force_no_inline proc "c" (ctx: jsc.JSContextRef, why: str // JSObjectMakeFunctionWithCallback (inject_native_function already does). // Node: // `.length` is observable only for the natives injected as globals — -// setTimeout/setInterval report 2, the rest 1 (verified against node 24). +// setTimeout/setInterval report 2, the rest 1 (node 26.13.0, verified by +// tests/node-compat/cases/56-native-function-arity.js). // Deviates: // The C-API fallback reports 0 and cannot carry an arity through the public // API; analysis at inject_native_function (require.odin). Pinned by diff --git a/pkg/runtime/js/console.js b/pkg/runtime/js/console.js index a2515b8..d6ff2a8 100644 --- a/pkg/runtime/js/console.js +++ b/pkg/runtime/js/console.js @@ -87,7 +87,8 @@ // Node's short circuit, and it must come BEFORE the substitution loop rather // than inside it: a lone string argument is the answer, directives and all. // `format('%s')`, `format('%z')`, `format('100%')` and `format('%%')` all come - // back untouched on node 24 (verified, not read off the docs). + // back untouched on node 26.13.0 (verified by + // tests/node-compat/cases/57-format-single-arg.js, not read off the docs). // Only `%%` used to get this wrong here — `%s` and friends were saved further // down by the "arguments exhausted" branch, which re-emits the directive, while // `%%` was folded unconditionally. diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js index 3865fc1..44c83ed 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/internal/buffer.js @@ -144,7 +144,7 @@ * @returns {TypeError & {code: string}} * @node `The "size" argument must be of type number. Received type string ('5')` * for Buffer.alloc('5'); the same template for "offset" (readUInt8('5'), - * writeUInt16LE(0, '1')) and "length" (Buffer.concat([a], '5')). Node 24.22.1, + * writeUInt16LE(0, '1')) and "length" (Buffer.concat([a], '5')). Node 26.13.0, * verified by tests/node-compat/cases/70-buffer-size-inspect-parity.js. * @deviates none */ @@ -257,7 +257,7 @@ * ceiling; the message quotes the ceiling and `Received `. * @node -1/NaN/±Infinity/2**53 → `The value of "size" is out of range. It must be * >= 0 && <= 9007199254740991. Received -1` (2**53 prints 9_007_199_254_740_992); - * -0 and 1.5 pass (length 0 and 1). Node 24.22.1, verified by + * -0 and 1.5 pass (length 0 and 1). Node 26.13.0, verified by * tests/node-compat/cases/70-buffer-size-inspect-parity.js. * @deviates The ceiling is K_MAX_LENGTH (4 GiB by default; LAVA_MAX_BUFFER_BYTES lowers it, * and buffer.constants.MAX_LENGTH reports it), not node's 2**53-1 — the documented @@ -797,7 +797,7 @@ * ±Infinity or above kMaxLength (assertSize, the check Buffer.alloc runs). * @node `Buffer(n)` is `Buffer.alloc(n)`: -1, NaN, ±Infinity and 2**53 throw the * coded "size" RangeError, -0 → length 0, 1.5 → length 1, 5 → five zero bytes; - * identical with and without `new`. Node 24.22.1, verified by + * identical with and without `new`. Node 26.13.0, verified by * tests/node-compat/cases/70-buffer-size-inspect-parity.js. * @deviates The out-of-range bound is K_MAX_LENGTH (see assertSize), pinned by * tests/node-compat/cases/70-buffer-size-inspect-parity.js. diff --git a/pkg/runtime/js/internal/console.js b/pkg/runtime/js/internal/console.js index 0fdb97e..7baa41b 100644 --- a/pkg/runtime/js/internal/console.js +++ b/pkg/runtime/js/internal/console.js @@ -5,7 +5,8 @@ // code runs. // Returns: the console object (same reference as the original globalThis.console). // Node: require('node:console') is the intrinsic regardless of global mutation -// (node 24, verified). When the global is untouched, require === console. +// (node 26.13.0, verified by tests/node-compat/cases/66 and 67). When the global is +// untouched, require === console. // Deviates: none. The Console constructor for custom-stream consoles is not yet // provided on either the global or the module (pre-existing gap, not this fix). // diff --git a/pkg/runtime/js/internal/encoding.js b/pkg/runtime/js/internal/encoding.js index a1fc485..26c5a6f 100644 --- a/pkg/runtime/js/internal/encoding.js +++ b/pkg/runtime/js/internal/encoding.js @@ -209,8 +209,9 @@ // Node's validateObject(value, 'options', kValidateObjectAllowObjects) as it is // called from TextDecoder — nullable, and arrays and functions allowed. Verified - // against node 24 rather than read off the docs: `null`, `[1,2]` and `()=>{}` - // are accepted; every scalar including `-0`, `NaN`, `''` and `false` throws. + // against node 26.13.0 (probe, 2026-11-16) rather than read off the docs: `null`, + // `[1,2]` and `()=>{}` are accepted; every scalar including `-0`, `NaN`, `''` and + // `false` throws. // It runs BEFORE the label is looked at (see the constructor), which is why it // exists as a function instead of being folded into the `options || kEmptyObject` // line — the order is observable when both arguments are bad. @@ -232,9 +233,10 @@ // NOT `instanceof`, on any of the three arms. `instanceof` dispatches through // `Ctor[Symbol.hasInstance]`, a configurable own property, so a caller flips the // brand in either direction and steers the value into the wrong arm. Measured - // against node 24, which is native here and immune on every cell: forging - // `DataView[Symbol.hasInstance]` to false made `decode(dataView)` THROW - // TypeError; forging `ArrayBuffer`'s to false made `decode(arrayBuffer)` throw; + // against node 26.13.0 (probe, 2026-11-16), which is native here and immune on + // every cell: forging `DataView[Symbol.hasInstance]` to false made + // `decode(dataView)` THROW TypeError; forging `ArrayBuffer`'s to false made + // `decode(arrayBuffer)` throw; // and forging `Uint8Array`'s or `ArrayBuffer`'s to TRUE made `decode()` return // "" — a silent empty decode of valid input, which is the worse half. Six // diverging cells in all, of which a review flagged one. diff --git a/pkg/runtime/js/internal/fs.js b/pkg/runtime/js/internal/fs.js index bb913b4..bc8ff35 100644 --- a/pkg/runtime/js/internal/fs.js +++ b/pkg/runtime/js/internal/fs.js @@ -17,7 +17,7 @@ // brand check). // // ZERO COPY. `Buffer.from(arrayBuffer, byteOffset, length)` shares memory by spec — it -// does NOT copy, verified on node 24.18.1 and here (mutating the Buffer is visible through +// does NOT copy, verified on node 26.13.0 and here (mutating the Buffer is visible through // the original view). A copy would be a per-file-size regression on the hottest fs call // there is, and CLAUDE.md ranks memory second only to conformance. // @@ -52,7 +52,8 @@ var BufferPrototypeToString = PristineBuffer.bufferToString; // Node renders the offending value with util.inspect in BOTH templates, so a string - // comes out quoted and everything else bare. Verified against node 24.18.1: + // comes out quoted and everything else bare. Verified against node 26.13.0 (probe, + // 2026-11-16): // 'bogus' -> 'bogus' 123 -> 123 true -> true 1n -> 1n // {} -> {} {a:1} -> { a: 1 } [] -> [] Symbol(x) -> Symbol(x) // An earlier version of this quoted everything, which was wrong for every non-string. @@ -73,7 +74,7 @@ } // Node's ERR_INVALID_ARG_TYPE "Received" clause has THREE branches, not two - // (determineSpecificType). Measured on node 24.18.1: + // (determineSpecificType). Measured on node 26.13.0 (probe, 2026-11-16): // null / undefined -> named bare: Received null // an object -> its constructor: Received an instance of Object // (Array, Date, RegExp, Map, Buffer, a user class …; @@ -117,7 +118,7 @@ * string when an encoding was supplied, or an error/undefined. * @returns {*} A Buffer for the Uint8Array case; `value` untouched otherwise. * @node readFileSync/readFile return a Buffer with no encoding and a string with one - * (verified on node 24.18.1, including `{encoding: null}` -> Buffer). + * (verified on node 26.13.0, including `{encoding: null}` -> Buffer). * @deviates none */ function asBuffer(value) { @@ -140,7 +141,7 @@ * @param {*} path * @throws {TypeError} ERR_INVALID_ARG_TYPE — anything but a string, a Buffer/Uint8Array * or a URL. - * @node Message verified on node 24.18.1: `The "path" argument must be of type string + * @node Message verified on node 26.13.0: `The "path" argument must be of type string * or an instance of Buffer or URL. Received an instance of Object`. * @deviates Two values are accepted here that the native then mishandles, because * rejecting them would be a NEW divergence: a `file:` URL is stringified rather @@ -180,7 +181,7 @@ * @returns {string|null} The encoding name, or null for a binary (Buffer) read. * @throws {TypeError} ERR_INVALID_ARG_TYPE — options is neither a string nor an object. * @throws {TypeError} ERR_INVALID_ARG_VALUE — the encoding name is not recognized. - * @node Messages verified against node 24.18.1: + * @node Messages verified against node 26.13.0: * `readFileSync(p, 123)` -> 'The "options" argument must be one of type string or * object. Received type number (123)'; `readFileSync(p, 'bogus')` -> "The argument * 'encoding' is invalid encoding. Received 'bogus'". `{}`, `null` and `undefined` @@ -191,7 +192,7 @@ // node's getOptions treats null, undefined AND a function as "no options" — a callback // sitting in the options slot is not an error, and neither is an array (it takes the // object branch and has no `.encoding`). Verified: readFileSync(p, function(){}) and - // readFileSync(p, []) both return a Buffer on node 24.18.1. + // readFileSync(p, []) both return a Buffer on node 26.13.0. if (options === null || options === undefined || typeof options === 'function') { return null; } @@ -252,7 +253,7 @@ * @throws {TypeError} ERR_UNKNOWN_ENCODING — the name is `'buffer'`, which node lets * through validation so Buffer#toString rejects it. * @node Returns a Buffer, not a bare Uint8Array; decoding is lossy for invalid UTF-8 - * (U+FFFD), never `""`. Verified against node 24.18.1. + * (U+FFFD), never `""`. Verified against node 26.13.0. * @deviates A `file:` URL path is not resolved — it reaches the native as its string * form and fails ENOENT where node reads the file. Pre-existing; see the fs entry * in ROADMAP. @@ -276,7 +277,7 @@ * @throws {TypeError} ERR_INVALID_ARG_TYPE — cb is not a function. Validated BEFORE the * options, and against `callback || options`, so `readFile(p, 'bogus')` reports * the STRING as the bad callback rather than complaining about the encoding. - * Verified on node 24.18.1; the reverse order would report a plausible but + * Verified on node 26.13.0; the reverse order would report a plausible but * different error for the commonest mistake there is, a forgotten callback. * @throws {TypeError} Both encoding errors above, thrown SYNCHRONOUSLY — they do not * surface in the callback. diff --git a/pkg/runtime/js/internal/mime.js b/pkg/runtime/js/internal/mime.js index 4c36bba..0c2a8a9 100644 --- a/pkg/runtime/js/internal/mime.js +++ b/pkg/runtime/js/internal/mime.js @@ -68,7 +68,7 @@ } // Validate a token production (type/subtype/parameter name). `input` is the string shown - // in the error. The reported index is relative to `value`, not to `input`: node 24 says + // in the error. The reported index is relative to `value`, not to `input`: node 26.13.0 says // `The MIME syntax for a subtype in "text/p@in" is invalid at 1`. function validateToken(production, value, input) { if (value.length === 0) throw mimeError(production, input, -1); @@ -224,7 +224,8 @@ // Every name-taking method folds the name the way parse does, so a parsed `charset` // is reachable as get('Charset') and set('Charset') replaces it rather than adding a - // second parameter (node 24.21, verified; node 22 compared names as given). The fold + // second parameter (since node 24.21; re-verified on 26.13.0; node 22 compared names as + // given). The fold // happens before validation, so a syntax error quotes the folded name. MIMEParams.prototype.get = function get(name) { var data = this[kData]; diff --git a/pkg/runtime/js/internal/os.js b/pkg/runtime/js/internal/os.js index 1e09112..81d655f 100644 --- a/pkg/runtime/js/internal/os.js +++ b/pkg/runtime/js/internal/os.js @@ -568,7 +568,7 @@ * @param {{encoding?: string}} [options] * @returns {{uid:number, gid:number, username:*, homedir:*, shell:*}} A NULL-PROTOTYPE * object, as node returns. - * @node Verified on node 24.18.1, and three things here are easy to get wrong: + * @node Verified on node 26.13.0, and three things here are easy to get wrong: * - the result has a null prototype (`typeof info.hasOwnProperty === 'undefined'`); * - the encoding goes through node's ParseEncoding, so it is CASE-INSENSITIVE: * 'buffer', 'BUFFER' and 'bUfFeR' all yield Buffers; diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index eb9228c..e277ac1 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -466,7 +466,7 @@ // spec's RegExpExec abstract operation re-reads `R.exec` off the RECEIVER // before falling back to the builtin, so replacing `RegExp.prototype.exec` // steers `test` even when `test` itself was captured pristine at module-eval - // and invoked through Reflect.apply. Verified identically on node 24 and + // and invoked through Reflect.apply. Verified identically on node 26.13.0 and // bin/lava. Exporting a `Test` that looks safe and is not would be worse than // exporting nothing. The predicate form below, `RegExpMatches(re, s)`, is the // spelling a validator should use; `RegExpPrototypeExec` stays exported for a diff --git a/pkg/runtime/js/internal/process.js b/pkg/runtime/js/internal/process.js index ad5e2dd..1653d45 100644 --- a/pkg/runtime/js/internal/process.js +++ b/pkg/runtime/js/internal/process.js @@ -5,7 +5,8 @@ // code runs. // Returns: the process object (same reference as the original globalThis.process). // Node: require('node:process') is the intrinsic regardless of global mutation -// (node 24, verified). When the global is untouched, require === process. +// (node 26.13.0, verified by tests/node-compat/cases/66 and 67). When the global is +// untouched, require === process. // Deviates: none. // // Never re-read the global. A dependency that reassigns or deletes diff --git a/pkg/runtime/js/internal/sqlite.js b/pkg/runtime/js/internal/sqlite.js index 64dbbcc..a55d0b8 100644 --- a/pkg/runtime/js/internal/sqlite.js +++ b/pkg/runtime/js/internal/sqlite.js @@ -180,11 +180,11 @@ // lossy nor an overflowing bind. // // Node: - // node 24.21 binds a boolean as INTEGER 1/0 and a (Shared)ArrayBuffer as a BLOB - // of its bytes — NULL when its byteLength is 0, empty or detached, unlike an + // node (since 24.21) binds a boolean as INTEGER 1/0 and a (Shared)ArrayBuffer as a + // BLOB of its bytes — NULL when its byteLength is 0, empty or detached, unlike an // empty Uint8Array, which stays an empty BLOB. Probed against node 24.21 with - // `SELECT ?, typeof(?)`; node 22 threw on the boolean and took the buffer for a - // named bag. Pinned by tests/std/sqlite/cases/08-coercion-parity.js. + // `SELECT ?, typeof(?)`, re-verified on 26.13.0; node 22 threw on the boolean and + // took the buffer for a named bag. Pinned by tests/std/sqlite/cases/08-coercion-parity.js. // node 26 binds `undefined` as NULL, positional and named; node 24 threw // ERR_INVALID_ARG_TYPE. Pinned by tests/std/sqlite/cases/05-coercion.js. function bindOne(stmtId, index, value) { diff --git a/pkg/runtime/js/internal/stdio.js b/pkg/runtime/js/internal/stdio.js index a5c728f..c342f37 100644 --- a/pkg/runtime/js/internal/stdio.js +++ b/pkg/runtime/js/internal/stdio.js @@ -2,7 +2,7 @@ // // Built on the real `stream.Writable` rather than a lookalike object, because node's // streams are Writables in every fd case and libraries feature-detect with -// `x instanceof stream.Writable` (measured on node 24.18.1: true for the tty, pipe and +// `x instanceof stream.Writable` (measured on node 26.13.0: true for the tty, pipe and // file shapes alike). A hand-rolled object with a `write` method would answer false and // silently take the wrong branch in a logger. // @@ -74,7 +74,7 @@ * recovers from end()/destroy() (see the header on _destroy) and this stream * matches it exactly. On a PIPE node's stdout is a net.Socket, so end() closes * the write side and the next write raises an unhandled EPIPE and exits 1; - * verified on node 24.18.1 (`node x.js | cat` prints A, B and dies where + * verified on node 26.13.0 (`node x.js | cat` prints A, B and dies where * `node x.js > f` prints A, B, C, D). This stream recovers in both cases: it * holds no socket, writes go straight to the fd, and there is nothing an end() * could half-close. Deliberate — the alternative is closing fd 1 and losing @@ -93,7 +93,7 @@ this.fd = fd; // node sets an own `_isStdio` on both singletons, and its own stream/net code // feature-detects on it, so ecosystem code reads it too. Own, not inherited, matching - // `hasOwnProperty(process.stdout, '_isStdio') === true` on node 24.18.1. + // `hasOwnProperty(process.stdout, '_isStdio') === true` on node 26.13.0. this._isStdio = true; // `_isTTY` rather than a stored `isTTY`: the public one is a getter below so that a // non-terminal answers undefined instead of false, which is what node reports. @@ -110,7 +110,7 @@ // stdout is a process-lifetime singleton, so a teardown that stuck would be permanent: // one `process.stdout.end()` anywhere in a dependency and every later write on it is // dropped for the rest of the run. Node treats the stdio streams as un-closable for - // exactly that reason, and this is the mechanism it uses (verified: node 24.18.1's + // exactly that reason, and this is the mechanism it uses (verified: node 26.13.0's // `process.stdout._destroy.name === 'dummyDestroy'`). StdioWriteStream.prototype._destroy = function (err, cb) { cb(err); @@ -153,7 +153,7 @@ // Attach as LAZY getters with NO setter, which is node's descriptor exactly: // `Object.getOwnPropertyDescriptor(process, 'stdout')` reports - // `{get: fn, set: undefined, configurable: true, enumerable: true}` on node 24.18.1. + // `{get: fn, set: undefined, configurable: true, enumerable: true}` on node 26.13.0. // A setter was here briefly and was wrong three ways — sloppy-mode `process.stdout = x` // is DISCARDED by node (identity preserved) where it took effect here, and strict mode // throws there and silently succeeded here. Harnesses swap stdout with diff --git a/pkg/runtime/js/internal/stream.js b/pkg/runtime/js/internal/stream.js index 9c16932..ff6f04d 100644 --- a/pkg/runtime/js/internal/stream.js +++ b/pkg/runtime/js/internal/stream.js @@ -1045,7 +1045,7 @@ // `_undestroy` takes a teardown back: the stream has already emitted its 'finish' / // 'close' and run its callbacks, and this returns it to a usable state. Node exposes it // on both prototypes (internal/streams/destroy.js's `undestroy`; verified present on - // node 24.18.1's Writable, Readable AND process.stdout, so it is reachable API, not an + // node 26.13.0's Writable, Readable AND process.stdout, so it is reachable API, not an // invention here). It has exactly one caller shape — a `_destroy` that wants the // teardown undone — which is how node keeps the stdio singletons alive across an // `end()`; see js/internal/stdio.js. Nothing in this file calls it, so an existing diff --git a/pkg/runtime/js/internal/structured_clone.js b/pkg/runtime/js/internal/structured_clone.js index c87ae0f..820c69b 100644 --- a/pkg/runtime/js/internal/structured_clone.js +++ b/pkg/runtime/js/internal/structured_clone.js @@ -210,7 +210,7 @@ if (value instanceof Error) { // Node NORMALIZES an unrecognized name to Error rather than carrying it // over: `e.name = 'NotAnError'` clones to `name === 'Error'` (verified - // against node 24). Preserving it was a quiet divergence, and reapplying + // against node 26.13.0). Preserving it was a quiet divergence, and reapplying // it unconditionally is also what made `e.name = 'toString'` interesting — // the un-nulled table reached Object.prototype and `new` on the result // threw. diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js index 6f2d677..0ee4f79 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/internal/util.js @@ -24,7 +24,7 @@ // quotes. Its strEscape tests, in order: single by default; DOUBLE when the string // contains a single quote but no double; BACKTICK when it contains both AND contains // neither a backtick NOR the substring `${`; single-with-escapes otherwise. Verified on - // node 24.18.1: + // node 26.13.0 (probe, 2026-11-16): // plain -> 'plain' it's -> "it's" // say "hi" -> 'say "hi"' ' and " -> `both ' and "` // ` and ' and " -> 'all ` \' " ' a'b"c${d} -> 'a\'b"c${d}' @@ -34,7 +34,7 @@ // live template substitution. That revision also made this comment state the rule // without it, so the comment agreed with the code and neither agreed with node. // - // ESCAPES, matching node's strEscape exactly. Measured on node 24.18.1 across the whole + // ESCAPES, matching node's strEscape exactly. Measured on node 26.13.0 across the whole // C0 range, DEL/C1 and surrogates: // \b \t \n \f \r the five with short forms — note 0x0B is `\x0B`, NOT `\v` // other c < 0x20 `\xNN`, UPPERCASE hex @@ -194,7 +194,8 @@ // Node's short circuit, and it must come BEFORE the substitution loop rather // than inside it: a lone string argument is the answer, directives and all. // `format('%s')`, `format('%z')`, `format('100%')` and `format('%%')` all come - // back untouched on node 24 (verified, not read off the docs). + // back untouched on node 26.13.0 (verified by + // tests/node-compat/cases/57-format-single-arg.js, not read off the docs). // Only `%%` used to get this wrong here — `%s` and friends were saved further // down by the "arguments exhausted" branch, which re-emits the directive, while // `%%` was folded unconditionally. diff --git a/pkg/runtime/require.odin b/pkg/runtime/require.odin index e095c5e..531452b 100644 --- a/pkg/runtime/require.odin +++ b/pkg/runtime/require.odin @@ -265,11 +265,12 @@ make_module_not_found :: proc(ctx: jsc.JSContextRef, specifier: string) -> jsc.J // `.length` IS user-visible and Node is the oracle. // // THE TWO CREATION PATHS ARE NOT OBSERVABLY EQUIVALENT, which earlier comments -// left the reader to assume. Measured on setTimeout (2026-07-28): +// left the reader to assume. Measured on setTimeout (2026-07-28; node row re-probed +// 2026-11-16): // // host path (jsc.host_function_create): .length == arity, constructible // C-API fallback: .length == 0, `new` throws TypeError -// node 24: .length == 2, constructible +// node 26.13.0: .length == 2, constructible // // "Constructible" on the host path is itself only half of Node: create_raw // reuses the call callback as the constructor slot, so `new setTimeout(fn)` diff --git a/pkg/runtime/runtime.odin b/pkg/runtime/runtime.odin index 4165877..710a097 100644 --- a/pkg/runtime/runtime.odin +++ b/pkg/runtime/runtime.odin @@ -97,7 +97,7 @@ Pre_Run_Hook :: proc(user_data: rawptr) -> bool // Node writes an uncaught top-level error to stderr before its exit-time teardown // joins libuv's worker pool, so a script that throws with a worker blocked (a // readFile on a FIFO nobody writes) prints its error and then hangs. Verified on -// node v24.22.1 (probe case H): fs.readFile on a mkfifo with no writer, a throw +// node v26.13.0 (probe case H): fs.readFile on a mkfifo with no writer, a throw // once a worker sat in the FIFO open, stderr watched while the process was stuck, // then killed. The CLI passes its printer here to keep that // order: eval's deferred eventloop.destroy joins the loop's pool workers, and diff --git a/pkg/runtime/sqlite.odin b/pkg/runtime/sqlite.odin index a9a9004..29021d1 100644 --- a/pkg/runtime/sqlite.odin +++ b/pkg/runtime/sqlite.odin @@ -304,7 +304,7 @@ sqlite_bind_cb :: proc "c" ( res = sqlite.bind_null(stmt, index) case .Number: // Node binds EVERY JS number with sqlite3_bind_double, whole ones included - // (verified against node 24: `SELECT typeof(?)` with 42 reports "real"). + // (verified against node 26.13.0: `SELECT typeof(?)` with 42 reports "real"). // Binding a whole number as INTEGER changed results, not just the type tag: // SQLite divides integers with integer division, so `SELECT ? / 2` with 5 // returned 2 instead of 2.5, and a value written into a typeless column came @@ -379,7 +379,7 @@ sqlite_bind_bigint_cb :: proc "c" ( // Node: // node:sqlite stamps ERR_SQLITE_ERROR on EVERY library-originated error — a bad // path in the constructor, a syntax error from exec/prepare, a constraint -// violation surfaced from step, a rejected bind — verified against node 24 +// violation surfaced from step, a rejected bind — verified against node 26.13.0 // (`new DatabaseSync('/nonexistent/x.db')`, `db.exec('NOT SQL')`, a UNIQUE // violation). Only the bind path used to set it here, so the documented // `err.code === 'ERR_SQLITE_ERROR'` branch silently missed every other error. diff --git a/pkg/runtime/typed_array.odin b/pkg/runtime/typed_array.odin index d255c39..f5ae9c9 100644 --- a/pkg/runtime/typed_array.odin +++ b/pkg/runtime/typed_array.odin @@ -43,7 +43,7 @@ g_bytes_ptr_mode: BytesPtr_Mode // written once per thread after first offset vi // A view over a resizable ArrayBuffer has no fixed length: a length-tracking // one follows the buffer's current size, and a fixed window the buffer has // shrunk below reads as length 0 — so `decode(new Uint8Array(rab))` sees every -// byte and a stale window sees none (node 24.21, verified by +// byte and a stale window sees none (node 26.13.0, verified by // tests/node-compat/cases/68-resizable-view-bytes.js). The cell fast path // declines those views and the C API below answers for them; its byteLength // applies the spec's rules for every view mode. diff --git a/pkg/std/sqlite/sqlite.odin b/pkg/std/sqlite/sqlite.odin index 10e4a9d..d138e11 100644 --- a/pkg/std/sqlite/sqlite.odin +++ b/pkg/std/sqlite/sqlite.odin @@ -199,7 +199,7 @@ prepare :: proc(db: ^Database, sql: string) -> (Statement, Result) { handle: rawptr // pzTail is nil on purpose: prepare compiles only the first statement and // silently ignores anything after the first ";". This matches node:sqlite - // (verified against Node 24 — `prepare("SELECT 1; SELECT 2")` runs SELECT 1), + // (verified against Node 26.13.0 — `prepare("SELECT 1; SELECT 2")` runs SELECT 1), // which is more permissive than better-sqlite3's "one statement only" error. rc := sqlite3_prepare_v2(db.handle, csql, -1, &handle, nil) if rc != SQLITE_OK || handle == nil { @@ -419,7 +419,7 @@ bind_text :: proc(stmt: ^Statement, index: int, value: string) -> Result { // // Node: // node:sqlite binds an empty Uint8Array as an empty BLOB — `typeof(x)` reads -// "blob" and `length(x)` 0, verified against node 24. +// "blob" and `length(x)` 0, verified against node 26.13.0. // // sqlite3_bind_blob() with a NULL pointer is documented to behave exactly like // sqlite3_bind_null(), so an empty slice (whose raw_data is nil) silently bound diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index 6226db1..0cb61ac 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -166,7 +166,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. **Intentional differences / deferred:** - `Buffer.poolSize` allocation pooling is modeled like Node: a per-runtime pool `ArrayBuffer` of `Buffer.poolSize` (262144) bytes serves small _unsafe_ allocations — `Buffer.allocUnsafe(size)`, the `Buffer.from(string|array|typed-array)` copy paths, and `Buffer.concat` — when `size < (Buffer.poolSize >>> 1)` (Node's threshold is strictly less-than, so exactly `poolSize/2` is _not_ pooled), advancing an 8-byte-aligned offset and re-pooling on exhaustion. Such buffers share one backing store at different `byteOffset`s, and `buf.buffer.byteLength` is the pool size rather than the buffer length. `Buffer.alloc` (zero-filled), `Buffer.allocUnsafeSlow`, and large allocations keep their own right-sized backing store, as in Node. `Buffer.concat` zero-fills any tail an explicit `totalLength` leaves uncovered, so a pooled result never exposes stale pool bytes. The pool is plain per-runtime module state, so it never leaks across isolated runtimes. Pool _offsets_ are not byte-for-byte identical to Node's because Node pre-warms its pool with internal startup allocations; only the relative pooling semantics match, and well-behaved packages must not depend on pool identity anyway. -- `Buffer.poolSize` is a plain data property that stores and returns the raw assigned value (no coercion on read), and the pool's backing store is sized straight from it — matching Node's `new Uint8Array(Buffer.poolSize)`. A fractional `poolSize` is truncated (`8192.7` → `8192`), and a negative or non-finite `poolSize` throws a catchable `RangeError` on the next (re-)pool, rather than the previous `>>> 0`, which wrapped a negative `poolSize` into a multi-gigabyte request and a `>= 2^32` one into a mod-2^32 size (issue #213). A failed re-pool leaves the existing pool intact (the new backing store is committed only on success), so the throw is recoverable. One benign divergence remains: for a `poolSize` large enough to exceed JavaScriptCore's typed-array allocation cap (a few GiB), the (re-)pool throws `RangeError` where Node (whose limit is ~`2^53`) would instead build a multi-gigabyte pool. That is reachable only by deliberately setting `poolSize` to gigabytes _and_ forcing a re-pool, and throwing is the safer behavior — Lava never attempts the giant allocation. Setting `Buffer.poolSize` to any such value is itself unrealistic (the default is 262144 and the threshold still uses Node's `>>> 1` verbatim), so this only hardens the defensive path. The default is **262144**, a deliberate deviation: node 24.22.1 reports 65536, and lava uses a larger slab because a pool refill costs more on JavaScriptCore than on V8 (the reason is recorded at the constant in [buffer.js](../pkg/runtime/js/internal/buffer.js); no measurement is cited there). The value is documented as "may be modified" and well-behaved packages must not depend on it, so a pool-size-dependent observable like `Buffer.from(small).buffer.byteLength` differs from the node24 test oracle — the ported pool tests (`buffer-pool.js`, `buffer-allocunsafe-large.js`, `buffer-poolsize-pathological.js`) are written pool-size-relative so they pass under any pool size. (`kMaxLength` stays the JSC-family 4 GiB.) +- `Buffer.poolSize` is a plain data property that stores and returns the raw assigned value (no coercion on read), and the pool's backing store is sized straight from it — matching Node's `new Uint8Array(Buffer.poolSize)`. A fractional `poolSize` is truncated (`8192.7` → `8192`), and a negative or non-finite `poolSize` throws a catchable `RangeError` on the next (re-)pool, rather than the previous `>>> 0`, which wrapped a negative `poolSize` into a multi-gigabyte request and a `>= 2^32` one into a mod-2^32 size (issue #213). A failed re-pool leaves the existing pool intact (the new backing store is committed only on success), so the throw is recoverable. One benign divergence remains: for a `poolSize` large enough to exceed JavaScriptCore's typed-array allocation cap (a few GiB), the (re-)pool throws `RangeError` where Node (whose limit is ~`2^53`) would instead build a multi-gigabyte pool. That is reachable only by deliberately setting `poolSize` to gigabytes _and_ forcing a re-pool, and throwing is the safer behavior — Lava never attempts the giant allocation. Setting `Buffer.poolSize` to any such value is itself unrealistic (the default is 262144 and the threshold still uses Node's `>>> 1` verbatim), so this only hardens the defensive path. The default is **262144**, a deliberate deviation: node 26.13.0 reports 65536, and lava uses a larger slab because a pool refill costs more on JavaScriptCore than on V8 (the reason is recorded at the constant in [buffer.js](../pkg/runtime/js/internal/buffer.js); no measurement is cited there). The value is documented as "may be modified" and well-behaved packages must not depend on it, so a pool-size-dependent observable like `Buffer.from(small).buffer.byteLength` differs from the node test oracle — the ported pool tests (`buffer-pool.js`, `buffer-allocunsafe-large.js`, `buffer-poolsize-pathological.js`) are written pool-size-relative so they pass under any pool size. (`kMaxLength` stays the JSC-family 4 GiB.) - `Buffer.allocUnsafe` (above the pool threshold) and `Buffer.allocUnsafeSlow` return genuinely **uninitialized** memory like Node: a native allocation (`pkg/runtime/typed_array.odin`) hands JavaScriptCore a non-zero-filled backing store as a NoCopy `Uint8Array`, which the JS layer wraps as a Buffer view. A JS `new ArrayBuffer` is always zero-initialized, so this cannot be done in pure JS; that was the prior divergence (large unsafe allocations came back zero-filled). Such buffers now hold arbitrary prior memory contents — reading a byte before writing it is undefined behavior, as in Node. `Buffer.alloc` remains zero-filled, and the `Buffer.from`/`Buffer.concat` copy paths always overwrite (or zero-fill) every byte, so they never expose stale memory (issue #212). As a safety bound the native uninitialized path serves single allocations up to `2^31 - 1` bytes; a larger unsafe request falls back to a zero-filled own-backing store (JavaScriptCore's NoCopy creator aborts the process past an internal multi-gigabyte limit, whereas the JS allocation throws a catchable `RangeError`). The cap is far above any realistic `allocUnsafe` and keeps behavior identical across Linux, macOS, and Windows. - **Oversized-allocation ceiling (`kMaxLength`).** Node/V8 advertise `kMaxLength` as `Number.MAX_SAFE_INTEGER` on 64-bit; Lava reports the practical JSC array-buffer limit (**4 GiB on 64-bit, 2 GiB − 1 on 32-bit**), matching **Bun** — the other JSC-based runtime, which likewise reports `kMaxLength === 4294967296` rather than `MAX_SAFE_INTEGER`. It is the JSC-family convention, not a lava-only choice. `Buffer.alloc`/`allocUnsafe`/`allocUnsafeSlow`/`concat` and the `Buffer` constructor enforce this ceiling with Node's coded `ERR_OUT_OF_RANGE` **before** JSC is asked to allocate; negative/`NaN`/non-number sizes are validated and throw first. Raw `new TypedArray(n)` / `new ArrayBuffer(n)` are left to JavaScriptCore, which already throws its own catchable `RangeError` for an over-limit request — exactly as Bun does (Lava does not wrap the global constructors). The Buffer ceiling is overridable with the `LAVA_MAX_BUFFER_BYTES` environment variable (a byte count for operators who want a tighter bound; a non-positive value is ignored). Residual: an allocation handed to JavaScriptCore's **C-API NoCopy creator** from native code with a multi-GiB size — e.g. `fs.readFile` of a file past the limit, where the size comes from the OS rather than a JS constructor — can still abort the process; the JS-layer ceiling covers the JS-constructor and Buffer paths, not native allocations. - Property-access `TypeError` messages for engine-level faults (e.g. reading a property of `undefined`) carry JavaScriptCore wording, not V8's. diff --git a/scripts/run-mutations.mjs b/scripts/run-mutations.mjs index 2a48f00..ae2849c 100644 --- a/scripts/run-mutations.mjs +++ b/scripts/run-mutations.mjs @@ -647,7 +647,7 @@ function gateNodeTest(relPath, test) { const text = r.stdout + r.stderr; if (r.status === 0) { // A name that matches NOTHING still exits 0: node reports the file itself as - // one passing test (verified on node 24: `ℹ tests 1`, `ℹ pass 1`). So status + // one passing test (verified on node 26.13.0: `ℹ tests 1`, `ℹ pass 1`). So status // and counts cannot tell "the named test passed" from "no such test" — only // the named test's own pass line can. Without this check a typo in `test`, or // a renamed test, gives a green baseline and then SURVIVED for a mutation that diff --git a/scripts/run-sqlite-oracle.sh b/scripts/run-sqlite-oracle.sh index b30541c..1224aa8 100755 --- a/scripts/run-sqlite-oracle.sh +++ b/scripts/run-sqlite-oracle.sh @@ -11,7 +11,7 @@ RUN_LAVA=${RUN_LAVA:-0} TMP_DIR=$(mktemp -d "${TMPDIR:-/tmp}/lava-sqlite-oracle.XXXXXX") # Older Node (e.g. the Node 22 line) flags node:sqlite as experimental and prints -# an ExperimentalWarning to stderr; the Node 24 CI baseline and Lava do not. +# an ExperimentalWarning to stderr; the Node 26 CI baseline and Lava do not. # Suppress Node's warnings here so the stderr comparison reflects behavior, not # version noise, regardless of which Node the run happens to use. export NODE_OPTIONS="${NODE_OPTIONS:-} --no-warnings" diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 02ed97e..2fe5010 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1044,7 +1044,7 @@ "expect_detail": "GATE INTEGRITY" }, { - "name": "compare.sh dies when the Node 24 oracle version check is gutted", + "name": "compare.sh dies when the .nvmrc oracle-major check is gutted", "why": "Oracle expectations drift between Node majors (#185: node:sqlite binds undefined as NULL from 26); an oracle older than .nvmrc must not run.", "source": "scripts/lib/compare.sh", "find": "\tif [ \"$_got\" -lt \"$_want\" ]; then", @@ -1522,7 +1522,7 @@ }, { "name": "uncaught-before-teardown dies when the CLI prints the result after eval returns", - "why": "eval's deferred teardown joins the loop's pool workers, so a report printed after eval returns waits for a worker blocked without bound (fs.readFile on a FIFO nobody writes): a top-level throw then hangs with empty stderr, where node prints the error first (node v24.22.1, probe case H).", + "why": "eval's deferred teardown joins the loop's pool workers, so a report printed after eval returns waits for a worker blocked without bound (fs.readFile on a FIFO nobody writes): a top-level throw then hangs with empty stderr, where node prints the error first (node v26.13.0, probe case H).", "source": "cmd/lava/main.odin", "find": "\tresult := lava_runtime.run_file(args[0], &loop, args[1:], report = print_result)\n", "replace": "\tresult := lava_runtime.run_file(args[0], &loop, args[1:])\n\tprint_result(result, nil)\n", @@ -1542,7 +1542,7 @@ }, { "name": "uncaught-before-teardown dies when lava eval prints the result after eval returns", - "why": "`lava eval` wires the report hook separately from `lava run`; printing after runtime.eval returns waits for eval's teardown to join a worker blocked without bound, so a top-level throw hangs with empty stderr where `node -e` prints the error first (node v24.22.1, probe case H).", + "why": "`lava eval` wires the report hook separately from `lava run`; printing after runtime.eval returns waits for eval's teardown to join a worker blocked without bound, so a top-level throw hangs with empty stderr where `node -e` prints the error first (node v26.13.0, probe case H).", "source": "cmd/lava/main.odin", "find": "\tresult := lava_runtime.eval(args[0], \"\", &loop, true, report = print_result)\n", "replace": "\tresult := lava_runtime.eval(args[0], \"\", &loop, true)\n\tprint_result(result, nil)\n", diff --git a/tests/node-compat/cases/46-util-mimetype.js b/tests/node-compat/cases/46-util-mimetype.js index ceb1dac..d36c20e 100644 --- a/tests/node-compat/cases/46-util-mimetype.js +++ b/tests/node-compat/cases/46-util-mimetype.js @@ -80,7 +80,7 @@ assert.throws(() => new MIMEType('/plain'), { code: 'ERR_INVALID_MIME_SYNTAX' }) assert.throws(() => p.set('bad name', 'v'), { code: 'ERR_INVALID_MIME_SYNTAX' }); assert.throws(() => p.set('a', 'bad\x01'), { code: 'ERR_INVALID_MIME_SYNTAX' }); // a type/subtype error's index is relative to that component, not the whole input, and -// the message quotes the input as given — leading whitespace included (node 24) +// the message quotes the input as given — leading whitespace included (node 26.13.0) assert.throws(() => new MIMEType('text/p@in;a=b'), { message: 'The MIME syntax for a subtype in "text/p@in;a=b" is invalid at 1', }); diff --git a/tests/node-compat/cases/57-format-single-arg.js b/tests/node-compat/cases/57-format-single-arg.js index 424e271..5c2ac84 100644 --- a/tests/node-compat/cases/57-format-single-arg.js +++ b/tests/node-compat/cases/57-format-single-arg.js @@ -2,7 +2,7 @@ // // Node's formatWithOptions short-circuits before the substitution loop — string // first argument and nothing after it means the string is the answer, directives -// and all. Verified against node 24 rather than read off the docs: `format('%s')`, +// and all. Verified against node 26.13.0 rather than read off the docs: `format('%s')`, // `format('%z')`, `format('100%')` and `format('%%')` all come back untouched. // // Lava got every one of those right EXCEPT `%%`, which it folded to `%` even with diff --git a/tests/node-compat/cases/60-process-stdio.js b/tests/node-compat/cases/60-process-stdio.js index cbaf5cc..ccc1733 100644 --- a/tests/node-compat/cases/60-process-stdio.js +++ b/tests/node-compat/cases/60-process-stdio.js @@ -1,7 +1,7 @@ // process.stdout / process.stderr — the shape node exposes, diffed against node. // // Node's `process.stdout` is polymorphic by fd type, which is the part a reimplementation -// gets wrong. Measured against node 24.18.1: +// gets wrong. Measured against node 26.13.0 (probe, 2026-11-16; tty via script(1)): // // stdout -> constructor write(300KB) isTTY columns // file SyncWriteStream true undefined undefined diff --git a/tests/node-compat/cases/66-process-console-intrinsic.js b/tests/node-compat/cases/66-process-console-intrinsic.js index b2b507f..96d655f 100644 --- a/tests/node-compat/cases/66-process-console-intrinsic.js +++ b/tests/node-compat/cases/66-process-console-intrinsic.js @@ -4,7 +4,7 @@ // require after a dependency reassigned (or deleted) globalThis.process / // globalThis.console captured the shim. Node's require('node:process') / // require('node:console') always return the intrinsic module object regardless -// of global mutation (node 24, verified). Found in Codex review of #246; #247. +// of global mutation (node 26.13.0, verified by this case). Found in Codex review of #246; #247. // // NOTHING under test may be required above the swaps — a prior require would // populate the module cache with the real object and this case would pass with diff --git a/tests/node-compat/cases/67-process-console-deleted-global.js b/tests/node-compat/cases/67-process-console-deleted-global.js index 21ba786..0100f65 100644 --- a/tests/node-compat/cases/67-process-console-deleted-global.js +++ b/tests/node-compat/cases/67-process-console-deleted-global.js @@ -10,7 +10,7 @@ // own case rather than a second swap. A factory that exported the free variable // `process` reads an undeclared identifier under 'use strict' once the global is // gone, so it dies with ReferenceError instead of quietly exporting the wrong -// object. Node returns the intrinsic through either shape (node 24, verified). +// object. Node returns the intrinsic through either shape (node 26.13.0, verified by this case). // // NOTHING under test may be required above the delete, and `assert` is required // only AFTER the globals are restored: assert pulls in util, and a transitive diff --git a/tests/std/sqlite/cases/05-coercion.js b/tests/std/sqlite/cases/05-coercion.js index ffc0050..1518b63 100644 --- a/tests/std/sqlite/cases/05-coercion.js +++ b/tests/std/sqlite/cases/05-coercion.js @@ -82,7 +82,7 @@ insertOne(null); assert.equal(db.prepare('SELECT x FROM t').get().x, null); // --- TEXT with an embedded NUL round-trips intact (no truncation) --- -// SQLite TEXT may legally contain NULs; the Node 24 CI baseline and Lava preserve +// SQLite TEXT may legally contain NULs; the Node 26 CI baseline and Lava preserve // them (the #91 fix). (Node 22 truncated at the first NUL, but it is no longer the // oracle — see #167.) insertOne('a\0b'); diff --git a/tests/stdio/stdio-lifecycle.test.mjs b/tests/stdio/stdio-lifecycle.test.mjs index 5d2be9b..d68850d 100644 --- a/tests/stdio/stdio-lifecycle.test.mjs +++ b/tests/stdio/stdio-lifecycle.test.mjs @@ -8,7 +8,7 @@ // (`>"$node_stdout"` in scripts/lib/compare.sh), so tests/node-compat/cases/61 is the file // half; this file is the pipe half, and it is where Lava's two declared deviations live. // -// Measured on node 24.18.1, `write('A'); end('B'); ; write('C')`: +// Measured on node 26.13.0, `write('A'); end('B'); ; write('C')`: // // shape node stdout node exit why // file A B C 0 SyncWriteStream + dummyDestroy: end() is undone diff --git a/tests/stdio/uncaught-before-teardown.test.mjs b/tests/stdio/uncaught-before-teardown.test.mjs index f994f02..0a818df 100644 --- a/tests/stdio/uncaught-before-teardown.test.mjs +++ b/tests/stdio/uncaught-before-teardown.test.mjs @@ -7,7 +7,7 @@ // exit and would only ever see a timeout. What CAN be compared is whether the uncaught // error has been written by the time the process is stuck in that join. // -// Measured on node v24.22.1 (probe case H): node prints the error and its stack to +// Measured on node v26.13.0 (probe case H): node prints the error and its stack to // stderr at once and then hangs. Lava hung with EMPTY stderr: the CLI printed the // Result only after runtime.eval returned, and eval's deferred eventloop.destroy joins // the worker first. The fix is eval's `report` hook (runtime.odin), which the CLI passes From 5ebfe30f14bd8f3ed0ca50948f14e2a17a180f5f Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:13:02 +0200 Subject: [PATCH 20/61] refactor(runtime): derive embedded-JS module identity and visibility from the directory layout js/node/ holds every module user code may require, at the path of its specifier; js/internal/ holds what only builtins may require; js/bootstrap/ holds the preludes that are not modules. The loader's public resolver answers node/ ids only, replacing the hand-kept INTERNAL_ONLY table, and globals.odin embeds each directory with #load_directory instead of 41 #load constants and a parallel file table. scripts/check-js-graph.mjs replaces check-orphan-js.mjs: it fails on a non-literal or unresolvable require, an unreachable file, an unembedded directory, or a cycle through top-level requires. One observable change: JSC source names are now node: and node:internal/, as in node's own stack frames. Pinned by tests/node-compat/cases/74-builtin-visibility.js and two recorded mutations. Known divergences #415 and #416 are carried over unchanged. Co-Authored-By: Claude Fable 5.2 --- .claude/agents/quality-docs-auditor.md | 2 +- .claude/agents/regression-hunter.md | 2 +- CLAUDE.md | 24 +- Makefile | 2 +- ROADMAP.md | 23 +- .../odin-feature-reference/odin-sdk-map.md | 2 +- agents/prompts/pr-gate-reference/gates.md | 54 ++-- agents/specialists/quality-docs-auditor.md | 2 +- agents/specialists/regression-hunter.md | 2 +- bench/micro/url.js | 2 +- bunfig.toml | 2 +- cmd/lava/errors_test.odin | 2 +- docs/ARCHITECTURE.md | 55 ++-- package.json | 4 +- pkg/runtime/crypto.odin | 8 +- pkg/runtime/dns.odin | 6 +- pkg/runtime/environment.odin | 4 +- pkg/runtime/errors.odin | 2 +- pkg/runtime/fs.odin | 4 +- pkg/runtime/globals.odin | 212 ++++++------- pkg/runtime/http.odin | 2 +- pkg/runtime/js/{ => bootstrap}/console.js | 2 +- pkg/runtime/js/{internal => bootstrap}/esm.js | 2 +- .../js/{internal => bootstrap}/loader.js | 119 ++++---- .../js/{internal => bootstrap}/microtasks.js | 0 pkg/runtime/js/internal/encoding.js | 2 +- pkg/runtime/js/internal/fetch.js | 4 +- pkg/runtime/js/internal/mime.js | 2 +- pkg/runtime/js/internal/parse_args.js | 2 +- pkg/runtime/js/internal/parse_env.js | 2 +- pkg/runtime/js/internal/primordials.js | 2 +- pkg/runtime/js/internal/stdio.js | 2 +- pkg/runtime/js/internal/structured_clone.js | 2 +- pkg/runtime/js/{internal => node}/assert.js | 0 pkg/runtime/js/{internal => node}/buffer.js | 2 +- pkg/runtime/js/{internal => node}/console.js | 2 +- pkg/runtime/js/{internal => node}/crypto.js | 0 .../{internal => node}/diagnostics_channel.js | 0 pkg/runtime/js/{internal => node}/dns.js | 0 .../dns_promises.js => node/dns/promises.js} | 0 pkg/runtime/js/{internal => node}/events.js | 2 +- pkg/runtime/js/{internal => node}/fs.js | 2 +- pkg/runtime/js/{internal => node}/http.js | 2 +- pkg/runtime/js/{internal => node}/https.js | 0 pkg/runtime/js/{internal => node}/net.js | 0 pkg/runtime/js/{internal => node}/os.js | 2 +- pkg/runtime/js/{internal => node}/path.js | 2 +- .../path_posix.js => node/path/posix.js} | 0 .../path_win32.js => node/path/win32.js} | 2 +- pkg/runtime/js/{internal => node}/process.js | 2 +- pkg/runtime/js/{internal => node}/punycode.js | 2 +- .../js/{internal => node}/querystring.js | 2 +- pkg/runtime/js/{internal => node}/sqlite.js | 2 +- pkg/runtime/js/{internal => node}/stream.js | 2 +- .../stream/promises.js} | 0 .../streams.js => node/stream/web.js} | 0 .../js/{internal => node}/string_decoder.js | 0 pkg/runtime/js/{internal => node}/timers.js | 0 .../timers/promises.js} | 0 pkg/runtime/js/{internal => node}/tty.js | 0 pkg/runtime/js/{internal => node}/url.js | 2 +- pkg/runtime/js/{internal => node}/util.js | 10 +- .../util_types.js => node/util/types.js} | 0 pkg/runtime/module_resolution.odin | 2 +- pkg/runtime/net.odin | 6 +- pkg/runtime/net_other.odin | 4 +- pkg/runtime/os.odin | 8 +- pkg/runtime/os_posix.odin | 2 +- pkg/runtime/os_windows.odin | 6 +- pkg/runtime/require.odin | 2 +- pkg/runtime/sqlite.odin | 6 +- pkg/runtime/tls_server.odin | 2 +- reference/node-compatibility.md | 8 +- runtime/gates/route-gates.mjs | 24 +- runtime/gates/route-gates.test.mjs | 4 +- scripts/build.sh | 6 + scripts/check-global-replace.test.mjs | 4 +- scripts/check-js-graph.mjs | 242 +++++++++++++++ scripts/check-js-graph.test.mjs | 140 +++++++++ scripts/check-orphan-js.mjs | 53 ---- scripts/check-primordials.mjs | 2 +- scripts/lib/global-replace-detect.mjs | 2 +- scripts/lib/primordials-detect.mjs | 12 +- tests/mutation-manifest.json | 284 ++++++++++++------ .../node-compat/cases/57-format-single-arg.js | 2 +- .../node-compat/cases/63-fs-buffer-capture.js | 2 +- .../cases/74-builtin-visibility.js | 106 +++++++ tests/node-compat/pollution-baseline.json | 194 ++++++------ .../cases/10-nexttick-absolute-priority.js | 2 +- 89 files changed, 1125 insertions(+), 595 deletions(-) rename pkg/runtime/js/{ => bootstrap}/console.js (99%) rename pkg/runtime/js/{internal => bootstrap}/esm.js (99%) rename pkg/runtime/js/{internal => bootstrap}/loader.js (67%) rename pkg/runtime/js/{internal => bootstrap}/microtasks.js (100%) rename pkg/runtime/js/{internal => node}/assert.js (100%) rename pkg/runtime/js/{internal => node}/buffer.js (99%) rename pkg/runtime/js/{internal => node}/console.js (96%) rename pkg/runtime/js/{internal => node}/crypto.js (100%) rename pkg/runtime/js/{internal => node}/diagnostics_channel.js (100%) rename pkg/runtime/js/{internal => node}/dns.js (100%) rename pkg/runtime/js/{internal/dns_promises.js => node/dns/promises.js} (100%) rename pkg/runtime/js/{internal => node}/events.js (99%) rename pkg/runtime/js/{internal => node}/fs.js (99%) rename pkg/runtime/js/{internal => node}/http.js (99%) rename pkg/runtime/js/{internal => node}/https.js (100%) rename pkg/runtime/js/{internal => node}/net.js (100%) rename pkg/runtime/js/{internal => node}/os.js (99%) rename pkg/runtime/js/{internal => node}/path.js (99%) rename pkg/runtime/js/{internal/path_posix.js => node/path/posix.js} (100%) rename pkg/runtime/js/{internal/path_win32.js => node/path/win32.js} (75%) rename pkg/runtime/js/{internal => node}/process.js (96%) rename pkg/runtime/js/{internal => node}/punycode.js (99%) rename pkg/runtime/js/{internal => node}/querystring.js (98%) rename pkg/runtime/js/{internal => node}/sqlite.js (99%) rename pkg/runtime/js/{internal => node}/stream.js (99%) rename pkg/runtime/js/{internal/stream_promises.js => node/stream/promises.js} (100%) rename pkg/runtime/js/{internal/streams.js => node/stream/web.js} (100%) rename pkg/runtime/js/{internal => node}/string_decoder.js (100%) rename pkg/runtime/js/{internal => node}/timers.js (100%) rename pkg/runtime/js/{internal/timers_promises.js => node/timers/promises.js} (100%) rename pkg/runtime/js/{internal => node}/tty.js (100%) rename pkg/runtime/js/{internal => node}/url.js (99%) rename pkg/runtime/js/{internal => node}/util.js (99%) rename pkg/runtime/js/{internal/util_types.js => node/util/types.js} (100%) create mode 100644 scripts/check-js-graph.mjs create mode 100644 scripts/check-js-graph.test.mjs delete mode 100644 scripts/check-orphan-js.mjs create mode 100644 tests/node-compat/cases/74-builtin-visibility.js diff --git a/.claude/agents/quality-docs-auditor.md b/.claude/agents/quality-docs-auditor.md index a8101f8..494c552 100644 --- a/.claude/agents/quality-docs-auditor.md +++ b/.claude/agents/quality-docs-auditor.md @@ -43,7 +43,7 @@ Ask of each structural change: _is there a version of this with zero new concept ## Lens 3 — altitude and size - **Layer**: engine/string/view/host ABI → `pkg/jsc`; Node Buffer codecs → - `pkg/runtime/buffer*.odin`; HTTP → `http.odin` + `js/internal/http.js`; loop + `pkg/runtime/buffer*.odin`; HTTP → `http.odin` + `js/node/http.js`; loop mechanics → `pkg/runtime/eventloop`. Logic in the wrong package is a P1 even if it works — `environment.odin` accumulating a UTF-16 bridge is the standing example. - **Size**: `pkg/runtime/buffer.odin` stays under ~1000 lines; any file crossing diff --git a/.claude/agents/regression-hunter.md b/.claude/agents/regression-hunter.md index 93a7c4e..ff37e7f 100644 --- a/.claude/agents/regression-hunter.md +++ b/.claude/agents/regression-hunter.md @@ -43,7 +43,7 @@ to ship, because the diff shows what arrived and hides what left. refactor — a dual path that outlives its migration becomes permanent. - **JS ↔ Odin contract drift**: shared constants, array index layouts, property names, encoding assumptions must move together - (`pkg/runtime/http.odin` ↔ `js/internal/http.js` parse layouts are the classic + (`pkg/runtime/http.odin` ↔ `js/node/http.js` parse layouts are the classic case; `ORDER_*` in `dns.odin` ↔ `dns.js` is another). - A registration/inject path that misses a platform branch or a backend (proactor vs readiness; io_uring vs epoll). diff --git a/CLAUDE.md b/CLAUDE.md index 2d02802..1e1a9bb 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -80,7 +80,7 @@ Use the `odin-sdk-scout` agent for this search — it reads the real SDK source. ```sh make check # Odin type-check (incl. windows/darwin cross-target front-end) -make check-js # bun fmt --check + orphan-JS + node:test over scripts/ + primordials ratchet +make check-js # bun fmt --check + JS module graph + node:test over scripts/ + primordials ratchet make check-md # bun fmt + markdownlint over the repo's docs (make fix-md auto-fixes) make check-actions # actionlint over .github/workflows make build # ./scripts/build.sh → bin/lava @@ -191,16 +191,26 @@ Rules that keep this from becoming ceremony: - Internal helpers with no Node counterpart keep the plain why-comment. Do not retrofit existing code; this applies to new and changed surfaces. -## 5. Embedded JS conventions (`pkg/runtime/js`, incl. the top-level preludes) +## 5. Embedded JS conventions (`pkg/runtime/js`, incl. the bootstrap preludes) -- Route through `require('primordials')` — internal modules run alongside user +- **The directory decides visibility.** `js/node/` holds every module user code may + `require`, at the path of its specifier (`node/stream/web.js`); `js/internal/` + holds what only builtins may require (`require('internal/mime')`); `js/bootstrap/` + holds the loader, microtasks, `esm.js` and the console prelude, which are not + modules. A new file is embedded and wired by existing in the right directory — + there is no file list to edit. `scripts/check-js-graph.mjs` fails on a non-literal + or unresolvable `require`, an unreachable file, an unembedded directory, or a + top-level require cycle (a `require` inside a function is the sanctioned late + binding). + +- Route through `require('internal/primordials')` — internal modules run alongside user code that can poison prototypes. `make check-primordials` is a **ratchet**: a hardened file (baseline 0) rejects any new pollutable call. `UPDATE=1` only to _lower_ a baseline. - One exception, and do not add a second: `js/internal/esm.js` is evaluated + One exception, and do not add a second: `js/bootstrap/esm.js` is evaluated standalone and deliberately kept off the module table, so user code cannot reach the transform — which also denies it a `require`. It takes the table as a factory - argument instead (`loader.js` publishes it on the resolver, `globals.odin` passes + argument instead (`bootstrap/loader.js` publishes it on the resolver, `globals.odin` passes it in and fails closed without it). The exception is affordable there because the file emits executed source and had to be hardened regardless. - The ratchet parses with acorn and counts **four** classes, each baselined @@ -212,10 +222,10 @@ Rules that keep this from becoming ceremony: global read live instead of captured at module-eval, which the loader runs before user code). **Module-eval capture is pristine only in a module the loader instantiates BEFORE user - code** — the eager list in `loader.js`, plus `stream`/`stdio` via `installStdio`. A LAZY + code** — the eager list in `bootstrap/loader.js`, plus `stream`/`stdio` via `installStdio`. A LAZY module's factory runs on the user's first `require()`, so a capture there is a live read in disguise: `var StringG = String` at the top of `net.js` scores 0 and is still - steerable. Take the handle from the loader instead (`require('primordials')`, + steerable. Take the handle from the loader instead (`require('internal/primordials')`, `require.pristineBuffer`). The ratchet cannot see this — it exempts every module-eval capture, which is why #333 shipped. Computed and destructured forms count the same as the dot form, so `view['buffer']` is not a way to lower a number. diff --git a/Makefile b/Makefile index 48c4b71..9b7ade4 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ help: @printf '%s\n' ' make check Type-check Odin packages (incl. windows/darwin front-ends)' @printf '%s\n' ' make check-cli Type-check cmd/lava only' @printf '%s\n' ' make check-runtime Type-check runtime packages only' - @printf '%s\n' ' make check-js Vite+ fmt/lint + orphan-JS + scripts tests + primordials + global-replace' + @printf '%s\n' ' make check-js Vite+ fmt/lint + JS module graph + scripts tests + primordials + global-replace' @printf '%s\n' ' make fix-js Auto-fix JS formatting/lint' @printf '%s\n' ' make check-md Markdown fmt + lint + node-compat matrix drift check' @printf '%s\n' ' make fix-md Auto-fix markdown lint' diff --git a/ROADMAP.md b/ROADMAP.md index da2dddd..eaeafcd 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -53,8 +53,9 @@ Tracked against the `tests/node-compat/cases` oracle. Every case in ### Internal JS module layer (done in this batch) -Built-ins now live as embedded JS factories under `pkg/runtime/js/internal/`, -wired by a small loader (`loader.js`) that native `require()` consults before +Built-ins now live as embedded JS factories under `pkg/runtime/js/node/` (public, +path == specifier) and `pkg/runtime/js/internal/` (helpers only builtins reach), +wired by a small loader (`js/bootstrap/loader.js`) that native `require()` consults before the filesystem (`require_builtin` in `globals.odin`). Minimal, original implementations — a small internal `primordials` table for pollution hardening (§5.5 of [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md)), no `internalBinding` @@ -168,7 +169,7 @@ the index, the issue is the detail. Ordered by what it unblocks. bolted onto #326. - [x] **`node:fs` had no JS layer, and three read bugs lived in the gap** ([#329](https://github.com/x-ror/lava/issues/329)) — fixed by adding - `js/internal/fs.js` over `make_fs_bindings`, so `node:fs` is no longer the + `js/node/fs.js` over `make_fs_bindings`, so `node:fs` is no longer the native-direct outlier ARCHITECTURE.md §3.4 named. All three were pre-existing and none threw — each returned plausible-looking wrong output: 1. **Reads returned a bare `Uint8Array`, not a `Buffer`.** `Buffer.isBuffer` was false and `.toString('hex')` fell through to `Uint8Array.prototype.toString`, @@ -316,7 +317,7 @@ the index, the issue is the detail. Ordered by what it unblocks. `Array.prototype.at` reads on the mode stack (`at(-1)` for the current frame, `at(-2)` for the `${}` close). The `charCodeAt` they moved onto is pinned too, because that is the same lesson `http.js` had already taught. - It cannot `require('primordials')` — the runtime evaluates it standalone and keeps + It cannot `require('internal/primordials')` — the runtime evaluates it standalone and keeps it off the module table precisely so user code cannot reach the transform — so it takes the table as a factory argument, handed out by `loader.js` and passed in by `globals.odin`, which fails closed when it is missing. @@ -383,7 +384,7 @@ the index, the issue is the detail. Ordered by what it unblocks. them `%%` with one argument. The directive cases (`%s`, `%z`, `100%`) already matched by a different route — the "arguments exhausted" branch re-emits `'%' + spec` — which is why the guard belongs before the loop, not in it. - Both implementations were wrong: `internal/util.js` behind `util.format`, and + Both implementations were wrong: `node/util.js` behind `util.format`, and `console.js`'s own `formatArgs` behind `console.log`. The duplication stays, justified: `console.js` is the bootstrap prelude, takes the native write functions as arguments, and has no `require` to delegate with. @@ -413,7 +414,7 @@ the index, the issue is the detail. Ordered by what it unblocks. - [x] **Promise ↔ event-loop ordering.** JSC drains its own promise microtask queue at every C-API boundary, so `Promise.then` used to run _before_ `process.nextTick` (Node is the reverse). `queueMicrotask` lives in a JS shim - (`js/internal/microtasks.js`) that schedules a JSC microtask, so it shares one + (`js/bootstrap/microtasks.js`) that schedules a JSC microtask, so it shares one FIFO with promise jobs. `process.nextTick` keeps Node's **absolute** priority by _not_ living in JSC's queue: nextTick callbacks accumulate in a JS-owned queue, and native drains it at two checkpoints that recreate Node's @@ -431,7 +432,7 @@ the index, the issue is the detail. Ordered by what it unblocks. - [x] **ESM** — `.mjs` files, static `import` / `export`, `import.meta.url`, and `node:url` `fileURLToPath` now work. JSC's classic C API (`JSEvaluateScript`) only runs script-goal source, so an ESM→CommonJS - source transform (`js/internal/esm.js`, stored on `Runtime_State` and + source transform (`js/bootstrap/esm.js`, stored on `Runtime_State` and applied by `native_require_cb` / the `.mjs` entrypoint) rewrites the static module syntax onto the existing native `require`, tagging the namespace with a non-enumerable `__esModule` for CJS↔ESM default interop. Handles only @@ -511,7 +512,7 @@ the index, the issue is the detail. Ordered by what it unblocks. HTML clone algorithm in `js/internal/structured_clone.js`). _(cases `11`, `13`.)_ - [x] **Full `node:path`** — `resolve`, `relative`, `normalize`, `dirname`, `sep`, `delimiter`, `parse`, `format`, and the `posix`/`win32` namespaces, ported to - JS (`js/internal/path.js`) so semantics match Node exactly; the partial native + JS (`js/node/path.js`) so semantics match Node exactly; the partial native implementation was retired. _(case `02`.)_ - [x] **More `node:fs`** — `writeFileSync` (string/Uint8Array), `mkdirSync` (`recursive`), `statSync` (`Stats` with `size`/`*Ms` times + `isFile`/ @@ -522,7 +523,7 @@ the index, the issue is the detail. Ordered by what it unblocks. `exec`/`prepare`/`isOpen`/`close`, `StatementSync` with `get`/`all`/`run` and `?` parameter binding; INTEGER/REAL/TEXT/BLOB/NULL coercion). Native libsqlite3 bindings in `pkg/std/sqlite`; JSC bridge + handle registry in - `pkg/runtime/sqlite.odin`; JS surface in `js/internal/sqlite.js`. Needs the + `pkg/runtime/sqlite.odin`; JS surface in `js/node/sqlite.js`. Needs the sqlite3 dev package (Linux: `libsqlite3-dev`; macOS: system sqlite3). Runs the oracle through Lava via `make test-sqlite-lava`. @@ -667,10 +668,10 @@ the index, the issue is the detail. Ordered by what it unblocks. ([#352](https://github.com/x-ror/lava/issues/352) · epic) — `primordials.js` plus the `make check-primordials` ratchet over `tests/node-compat/pollution-baseline.json`. Done in the `method` column: - `events.js`, `dns_promises.js`, and the encoding-name path of `buffer.js`; + `events.js`, `dns/promises.js`, and the encoding-name path of `buffer.js`; `encoding.js` sits at 1, not 0. Seven files are 0 in all four classes. Six of them are no evidence of hardening — each is a 6–19 line re-export shim - (`console.js`, `dns_promises.js`, `path_posix.js`, `path_win32.js`, + (`node/console.js`, `dns/promises.js`, `path/posix.js`, `path/win32.js`, `process.js`, `timers.js`). The seventh, `esm.js`, is 917 lines of real logic and is the first such file to reach 0 in all four. Remaining in `method`: `url.js` (the type-ambiguous diff --git a/agents/prompts/odin-feature-reference/odin-sdk-map.md b/agents/prompts/odin-feature-reference/odin-sdk-map.md index 641c423..32cb1d3 100644 --- a/agents/prompts/odin-feature-reference/odin-sdk-map.md +++ b/agents/prompts/odin-feature-reference/odin-sdk-map.md @@ -40,7 +40,7 @@ Verified against `odin version dev-2026-07`. Packages prefixed with `_` | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | | TCP/UDP sockets, addresses, options | `core:net` (`socket.odin`, `addr.odin`) | | DNS resolution without `getaddrinfo` | `core:net` (`dns.odin` — parses `/etc/resolv.conf` + `/etc/hosts`, speaks DNS itself; already used by `pkg/runtime/dns.odin`) | -| URL split/join/query | `core:net/url.odin` (byte-level; WHATWG URL semantics stay in `js/internal/url.js`) | +| URL split/join/query | `core:net/url.odin` (byte-level; WHATWG URL semantics stay in `js/node/url.js`) | | Network interface enumeration | `core:net` (`interface.odin`) | | Async I/O abstraction | `core:nbio` — read before adding a backend; Lava's own loop already covers the same ground, so treat it as a design reference, not a drop-in | | io_uring | `core:sys/linux/uring` (used by the loop's Linux proactor) | diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 449a2a8..2b62e68 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -41,7 +41,7 @@ disagree with this file, they win — fix this file. `make fmt` (`odin strip-semicolon`) before committing Odin. `make check-js` whenever any `.js`/`.mjs`/`.cjs` under `pkg/runtime/js`, `tests`, -`scripts`, or `bench` changed — it runs format, lint, orphan-JS detection, and the +`scripts`, or `bench` changed — it runs format, lint, the JS module-graph check (`check-js-graph`), and the primordials ratchet. `make check-md` whenever any `.md` changed — `bun fmt --check` plus markdownlint over the repo's own docs (lint config and the rationale for its two disabled rules: `.markdownlint-cli2.jsonc`). @@ -51,32 +51,32 @@ catches what a YAML parse cannot: a context used where it is not available, a ba ## By path -| Changed path | Additionally required | -| ------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `pkg/runtime/buffer*.odin`, `typed_array.odin`, `pkg/jsc/**` | `make test-odin`, `make bun-buffer-tests`, `make api-surface`, `make bench-gate` | -| `pkg/runtime/http.odin`, `js/internal/http.js`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | -| `pkg/runtime/net.odin`, `net_other.odin`, `js/internal/net.js` | `make test-net-smoke`, `make test-zerocopy-smoke` | -| `pkg/runtime/tls*.odin`, `tls_server.odin`, `js/internal/https.js` | `make test-https-smoke` (drives both backends internally) | -| `pkg/runtime/fetch*.odin`, `js/internal/fetch.js` | `make test-fetch-smoke` | -| `pkg/runtime/eventloop/**` | `make test-eventloop-odin`, `make test-eventloop-lava` | -| `pkg/runtime/fs*.odin`, `os_*.odin` | `make test-fs-lava` | -| `pkg/std/sqlite/**`, `sqlite.odin`, `js/internal/sqlite.js` | `make test-sqlite-odin`, `make test-sqlite-lava` | -| `pkg/runtime/dns.odin`, `fetch_dns.odin`, `js/internal/dns*.js` | `make test-compat-lava`, `make test-fetch-smoke` | -| `pkg/runtime/workers*.odin` | `make test-multicore-smoke` | -| `pkg/runtime/stdio*.odin`, `js/internal/stdio.js`, `tests/stdio/**` | `make test-stdio` — pins the non-blocking-fd retry loop that `console.log`, `lava eval` and `process.stdout.write` all share, plus the lifecycle/backpressure deviations in node's PIPE shape (the oracle harness only ever gives a case a FILE) | -| `js/internal/fs.js` | `make test-fs-lava` — the JS layer owns every observable `node:fs` read semantic since #330 (Buffer re-tag, encoding decode, error taxonomy, path validation). It routes only to the generic `js/**.js` row otherwise, so a change there passes `check-js` and `test-compat-lava` while breaking every `run-fs-oracle.sh` case | -| `js/internal/stream.js` | `make test-http-smoke`, `make test-net-smoke`, `make test-stdio` — nothing else routes here, and this is the base layer `net`/`http`/`stdio` compose on. Added after #326: a change to the `write()` accept set passed `check-js` and `test-compat-lava` while stalling `pipe()` forever, because a DataView made `writableLength` NaN and `'drain'` could never fire. The suites that would have caught it were not on anyone's list | -| `require.odin`, `module_resolution.odin`, `js/internal/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | -| `crypto.odin`, `js/internal/crypto.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | -| `pkg/runtime/js/**/*.js` (any under the scan root, `console.js` included; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | -| `js/internal/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | -| `js/internal/{encoding,url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | -| `scripts/**` | `make test-scripts` — node:test over the build tooling | -| any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | -| `pkg/runtime/globals.odin`, `runtime.odin`, `errors.odin` | `make test-odin`, `make test-compat-lava` | -| `bench/**`, `bench/thresholds.json` | `make bench-gate` — and a stated reason if a cap was loosened | -| `Makefile`, `scripts/**`, `.github/workflows/**` | `__ALL__` fallback via `route-gates.mjs` — run L0+L1 (and L2 if mutation/CI surface touched), end to end | -| `pkg/runtime/*_test.odin` / eventloop Odin tests | `make test-runtime-odin` when present in Makefile — not every Odin unit path is covered by `make test-odin` alone | +| Changed path | Additionally required | +| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `pkg/runtime/buffer*.odin`, `typed_array.odin`, `pkg/jsc/**` | `make test-odin`, `make bun-buffer-tests`, `make api-surface`, `make bench-gate` | +| `pkg/runtime/http.odin`, `js/node/http.js`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | +| `pkg/runtime/net.odin`, `net_other.odin`, `js/node/net.js` | `make test-net-smoke`, `make test-zerocopy-smoke` | +| `pkg/runtime/tls*.odin`, `tls_server.odin`, `js/node/https.js` | `make test-https-smoke` (drives both backends internally) | +| `pkg/runtime/fetch*.odin`, `js/internal/fetch.js` | `make test-fetch-smoke` | +| `pkg/runtime/eventloop/**` | `make test-eventloop-odin`, `make test-eventloop-lava` | +| `pkg/runtime/fs*.odin`, `os_*.odin` | `make test-fs-lava` | +| `pkg/std/sqlite/**`, `sqlite.odin`, `js/node/sqlite.js` | `make test-sqlite-odin`, `make test-sqlite-lava` | +| `pkg/runtime/dns.odin`, `fetch_dns.odin`, `js/node/dns.js`, `js/node/dns/promises.js` | `make test-compat-lava`, `make test-fetch-smoke` | +| `pkg/runtime/workers*.odin` | `make test-multicore-smoke` | +| `pkg/runtime/stdio*.odin`, `js/internal/stdio.js`, `tests/stdio/**` | `make test-stdio` — pins the non-blocking-fd retry loop that `console.log`, `lava eval` and `process.stdout.write` all share, plus the lifecycle/backpressure deviations in node's PIPE shape (the oracle harness only ever gives a case a FILE) | +| `js/node/fs.js` | `make test-fs-lava` — the JS layer owns every observable `node:fs` read semantic since #330 (Buffer re-tag, encoding decode, error taxonomy, path validation). It routes only to the generic `js/**.js` row otherwise, so a change there passes `check-js` and `test-compat-lava` while breaking every `run-fs-oracle.sh` case | +| `js/node/stream.js` | `make test-http-smoke`, `make test-net-smoke`, `make test-stdio` — nothing else routes here, and this is the base layer `net`/`http`/`stdio` compose on. Added after #326: a change to the `write()` accept set passed `check-js` and `test-compat-lava` while stalling `pipe()` forever, because a DataView made `writableLength` NaN and `'drain'` could never fire. The suites that would have caught it were not on anyone's list | +| `require.odin`, `module_resolution.odin`, `js/bootstrap/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | +| `crypto.odin`, `js/node/crypto.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | +| `pkg/runtime/js/**/*.js` (any under the scan root — `bootstrap/`, `node/`, `internal/`; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | +| `js/node/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | +| `js/internal/encoding.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | +| `scripts/**` | `make test-scripts` — node:test over the build tooling | +| any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | +| `pkg/runtime/globals.odin`, `runtime.odin`, `errors.odin` | `make test-odin`, `make test-compat-lava` | +| `bench/**`, `bench/thresholds.json` | `make bench-gate` — and a stated reason if a cap was loosened | +| `Makefile`, `scripts/**`, `.github/workflows/**` | `__ALL__` fallback via `route-gates.mjs` — run L0+L1 (and L2 if mutation/CI surface touched), end to end | +| `pkg/runtime/*_test.odin` / eventloop Odin tests | `make test-runtime-odin` when present in Makefile — not every Odin unit path is covered by `make test-odin` alone | ## Perf claims diff --git a/agents/specialists/quality-docs-auditor.md b/agents/specialists/quality-docs-auditor.md index a8101f8..494c552 100644 --- a/agents/specialists/quality-docs-auditor.md +++ b/agents/specialists/quality-docs-auditor.md @@ -43,7 +43,7 @@ Ask of each structural change: _is there a version of this with zero new concept ## Lens 3 — altitude and size - **Layer**: engine/string/view/host ABI → `pkg/jsc`; Node Buffer codecs → - `pkg/runtime/buffer*.odin`; HTTP → `http.odin` + `js/internal/http.js`; loop + `pkg/runtime/buffer*.odin`; HTTP → `http.odin` + `js/node/http.js`; loop mechanics → `pkg/runtime/eventloop`. Logic in the wrong package is a P1 even if it works — `environment.odin` accumulating a UTF-16 bridge is the standing example. - **Size**: `pkg/runtime/buffer.odin` stays under ~1000 lines; any file crossing diff --git a/agents/specialists/regression-hunter.md b/agents/specialists/regression-hunter.md index 93a7c4e..ff37e7f 100644 --- a/agents/specialists/regression-hunter.md +++ b/agents/specialists/regression-hunter.md @@ -43,7 +43,7 @@ to ship, because the diff shows what arrived and hides what left. refactor — a dual path that outlives its migration becomes permanent. - **JS ↔ Odin contract drift**: shared constants, array index layouts, property names, encoding assumptions must move together - (`pkg/runtime/http.odin` ↔ `js/internal/http.js` parse layouts are the classic + (`pkg/runtime/http.odin` ↔ `js/node/http.js` parse layouts are the classic case; `ORDER_*` in `dns.odin` ↔ `dns.js` is another). - A registration/inject path that misses a platform branch or a backend (proactor vs readiness; io_uring vs epoll). diff --git a/bench/micro/url.js b/bench/micro/url.js index 48da4fb..fa36954 100644 --- a/bench/micro/url.js +++ b/bench/micro/url.js @@ -12,7 +12,7 @@ // decode-utf8-stream at 17.0. So these numbers are a measuring instrument for work // that has not been done yet, and only secondarily a regression net. // -// SHAPES. Each is a distinct code path in js/internal/url.js, not a variation in +// SHAPES. Each is a distinct code path in js/node/url.js, not a variation in // input length: // special the common case — scheme, host, path, query, fragment // ipv4 dotted-quad host, which runs endsInANumber + parseIPv4Number per label diff --git a/bunfig.toml b/bunfig.toml index cca2cda..cc09c28 100644 --- a/bunfig.toml +++ b/bunfig.toml @@ -2,7 +2,7 @@ # vite-plus's `vp fmt` (oxfmt); the style is the one the tree was already formatted in: # oxfmt's 100-column width, semicolons, single quotes. There is no separate JS linter: # `bun fmt --check` is the style gate, and the correctness checks are the repo's own -# (primordials ratchet, orphan-JS, global-replace) plus the oracle suites. +# (primordials ratchet, JS module graph, global-replace) plus the oracle suites. [fmt] printWidth = 100 semi = true diff --git a/cmd/lava/errors_test.odin b/cmd/lava/errors_test.odin index c98bd84..4f7cb79 100644 --- a/cmd/lava/errors_test.odin +++ b/cmd/lava/errors_test.odin @@ -74,7 +74,7 @@ node_error_factory :: proc(t: ^testing.T) { // node_error_received_types pins determine_received_type's "Received …" rendering // (via err_invalid_arg_type's message) across every value kind — including BigInt, // the primitive JSValueGetType's enum omits — so it matches the JS-side -// inspectReceived (js/internal/crypto.js). A bare context suffices: only the +// inspectReceived (js/node/crypto.js). A bare context suffices: only the // message text is asserted here, which does not depend on captured intrinsics. @(test) node_error_received_types :: proc(t: ^testing.T) { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e91dbd2..39b6f3b 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -46,8 +46,9 @@ Two design decisions shape the whole system: │ module_resolution native resolver (file/dir/node_modules/main) │ │ fs / buffer / crypto / dns / sqlite / fetch* native primitives │ │ │ -│ js/internal/*.js embedded spec surface (Buffer, fetch, streams, …) │ -│ js/console.js console implemented in JS over two write prims │ +│ js/node/**.js public builtins; path == specifier (stream/web) │ +│ js/internal/*.js helpers only builtins may require (primordials, …) │ +│ js/bootstrap/*.js loader, microtasks, esm transform, console prelude │ └───────────────┬───────────────────────────────────┬──────────────────┘ │ │ ┌───────────────▼─────────────┐ ┌────────────────▼─────────────────┐ @@ -136,9 +137,24 @@ Thin, per-platform `foreign` declarations of JSC's C API. Highlights: ### 3.3 Standard library: native + embedded JS -Built-ins are JS factories `#load`-embedded at compile time (the `EMBEDDED_*` -constants in `globals.odin`) and instantiated by `loader.js`. Each factory -receives `(require, module, exports, +Built-ins are JS factories embedded at compile time and instantiated by +`js/bootstrap/loader.js`. The directory layout under `pkg/runtime/js` is the module +table — there is no second list of files: + +| Directory | Holds | Module id / visibility | +| ------------ | ---------------------------------------- | -------------------------------------------------------------------- | +| `node/` | every module user code may `require` | `node/`; file path == specifier (`node/stream/web.js`) | +| `internal/` | helpers (`primordials`, `fetch`, `mime`) | `internal/`; only the factories' own `require` reaches them | +| `bootstrap/` | loader, microtasks, `esm.js`, console | not modules — evaluated standalone by Odin through explicit `#load`s | + +`globals.odin` embeds each module directory whole with `#load_directory` +(`EMBEDDED_JS_DIRS`, one row per directory because the directive is not recursive) and +derives the id and the JSC source name from directory + basename: stack frames read +`node:buffer`, `node:stream/web`, `node:internal/encoding`. The public resolver answers +`node/` ids only, so the directory a file lives in decides whether user code can reach +it; `scripts/check-js-graph.mjs` (`make check-js`) fails on an unembedded directory, an +unresolvable or non-literal `require`, an unreachable file, or a top-level require +cycle. Each factory receives `(require, module, exports, native)`; the native fourth argument carries Odin-backed bindings (crypto, buffer, fetch, sqlite, dns, fs, net, http(s), os, tty, stdio — twelve today, see `install_internal_modules`) so **nothing transient lands on `globalThis`** @@ -158,7 +174,7 @@ require of that module — after user code, and therefore steerable (#333). `fs` instantiates them during bootstrap. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), -`streams.js` (2027, Web Streams), `buffer.js` (1646), `fetch.js` (1054), +`stream/web.js` (2027, Web Streams), `buffer.js` (1646), `fetch.js` (1054), `crypto.js` (917). Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …). Native byte ops are gated by a size threshold so small inputs avoid FFI overhead — except that the threshold this sentence used to @@ -171,18 +187,19 @@ native codec at every size. Removed rather than left as folklore steering review cache → JS internal builtins (`require_builtin`) → filesystem resolution. `node:fs` used to sit between the last two as a natively-assembled object, the one built-in with no JS layer; it is now an ordinary internal builtin - (`js/internal/fs.js` over `make_fs_bindings`). Nothing may rebuild it in + (`js/node/fs.js` over `make_fs_bindings`). Nothing may rebuild it in `native_require_cb`: `require_builtin` runs first, so a second construction would be dead code silently disagreeing with the live one. Circular requires terminate via a pre-registered partial-exports entry (`__lava_precache`), and a module that throws while loading is _removed_ from the cache so a later require re-runs it (Node parity). - `.mjs`/static `import`/`export` are handled by a **source transform** - (`esm.js`) that rewrites ESM onto the CommonJS `require`, not by JSC's native + (`js/bootstrap/esm.js`) that rewrites ESM onto the CommonJS `require`, not by JSC's native module records (the classic C API only runs script-goal source). This is pragmatic and works, with documented divergences (named imports from CJS). - Its output is _executed source_, which is why it is the one internal module the - runtime hands `primordials` as a factory argument: it is deliberately not - registered as a requireable module (so user code cannot reach the transform), + Its output is _executed source_, which is why it is the one internal file the + runtime hands `primordials` as a factory argument: it lives in `bootstrap/`, which + no `#load_directory` reads, so it is never a requireable module (user code cannot + reach the transform), and that also denies it a `require`, so `loader.js` exposes the already-built table on the resolver object and `globals.odin` passes it in — failing closed if it is absent, since a forged match group here is code injection into the module @@ -398,7 +415,7 @@ routing a per-code-unit codec loop through it cost 1.43x — hence into a null-prototype array where the loop is the inner loop. The loader **eager-loads it first**, before any other internal module and before user code, so the captured references are -pristine; modules consume it via `require('primordials')` and get the cached table +pristine; modules consume it via `require('internal/primordials')` and get the cached table (one exception — `esm.js`, which has no `require`; see §3.4). This is the JS-layer analog of the native error-intrinsic capture (§4.3/§5.1). @@ -469,13 +486,13 @@ member expression in the source to see. So the explicit half is gated and only t assimilation half is blind, which is why the guidance is a code convention rather than a counter: never `await` a caller-supplied value directly. -`primordials` is internal-only: the loader serves it to internal factories but -hides it from the public resolver native `require()` consults, so it neither -shadows a user package named `primordials` nor answers `require('node:primordials')` -(which Node rejects). The _specifier_ is what is hidden — the table itself also hangs -off that resolver object as `publicReq.primordials`, which is how `esm.js` receives it -(§3.4); the resolver is reachable only from Odin and never reaches user code. Gating -the other internal helper modules the same way is a future follow-up. +`primordials` is internal-only by living in `js/internal/`: the public resolver native +`require()` consults answers `node/` ids only, so it neither shadows a user package +named `primordials` nor answers `require('node:primordials')` (which Node rejects). The +same holds for every file under `internal/`. The _specifier_ is what is hidden — the +table itself also hangs off that resolver object as `publicReq.primordials`, which is +how `esm.js` receives it (§3.4); the resolver is reachable only from Odin and never +reaches user code. ### 5.6 [P2] Documentation & process gaps diff --git a/package.json b/package.json index 83eab81..c6b8e93 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "private": true, "scripts": { - "js:check": "bun fmt --check 'pkg/runtime/js/**/*.js' 'tests/**/*.js' 'tests/**/*.mjs' 'tests/**/*.cjs' 'scripts/**/*.{js,mjs}' 'bench/**/*.js' 'bench/**/*.mjs' 'runtime/**/*.mjs' && bun run js:deadcode && bun run js:scripttests && bun run js:primordials && bun run js:globalreplace", - "js:deadcode": "bun run scripts/check-orphan-js.mjs", + "js:check": "bun fmt --check 'pkg/runtime/js/**/*.js' 'tests/**/*.js' 'tests/**/*.mjs' 'tests/**/*.cjs' 'scripts/**/*.{js,mjs}' 'bench/**/*.js' 'bench/**/*.mjs' 'runtime/**/*.mjs' && bun run js:graph && bun run js:scripttests && bun run js:primordials && bun run js:globalreplace", + "js:graph": "bun run scripts/check-js-graph.mjs", "js:fix": "bun fmt 'pkg/runtime/js/**/*.js' 'tests/**/*.js' 'tests/**/*.mjs' 'tests/**/*.cjs' 'scripts/**/*.{js,mjs}' 'bench/**/*.js' 'bench/**/*.mjs' 'runtime/**/*.mjs'", "js:primordials": "bun run scripts/check-primordials.mjs", "js:globalreplace": "bun run scripts/check-global-replace.mjs", diff --git a/pkg/runtime/crypto.odin b/pkg/runtime/crypto.odin index b5210f5..a5cdb0e 100644 --- a/pkg/runtime/crypto.odin +++ b/pkg/runtime/crypto.odin @@ -11,7 +11,7 @@ import "core:math/rand" import jsc "lava:pkg/jsc" // Native backing for the node:crypto built-in. The JavaScript surface -// (createHash/createHmac/randomBytes/pbkdf2/...) lives in js/internal/crypto.js +// (createHash/createHmac/randomBytes/pbkdf2/...) lives in js/node/crypto.js // and consults these primitives through the `native` bindings object the loader // passes as the factory's fourth argument — the same pattern `console` uses to // receive its write primitives without exposing them on globalThis. @@ -21,7 +21,7 @@ import jsc "lava:pkg/jsc" // placeholder the pure-JS module shipped with. // crypto_algorithm maps a canonical Node digest name to an Odin hash.Algorithm. -// The JS layer (js/internal/crypto.js) lowercases the name and resolves every +// The JS layer (js/node/crypto.js) lowercases the name and resolves every // OpenSSL/Node alias (RSA-SHA256, sha256WithRSAEncryption, ssl3-md5, …) to one // of the canonical names below before crossing the boundary, and rejects any // digest Lava does not support — so this switch only handles the canonical set @@ -300,7 +300,7 @@ crypto_timing_safe_equal_cb :: proc "c" ( return jsc.JSValueMakeBoolean(ctx, b32(crypto.compare_constant_time(a, b) == 1)) } -// TODO(crypto-native-api): js/internal/crypto.js now exposes throwing templates +// TODO(crypto-native-api): js/node/crypto.js now exposes throwing templates // for every missing top-level Node crypto export. Add native callbacks here as // those APIs graduate from placeholders and need Odin-backed primitives: // @@ -318,7 +318,7 @@ crypto_timing_safe_equal_cb :: proc "c" ( // (getFips is a constant 0 in JS — Lava has no FIPS provider) // - randomness aliases/features: getRandomValues and randomUUIDv7 -// make_crypto_bindings builds the `native` object handed to js/internal/crypto.js. +// make_crypto_bindings builds the `native` object handed to js/node/crypto.js. make_crypto_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { bindings := jsc.JSObjectMake(ctx, nil, nil) inject_native_function(ctx, bindings, "randomFill", crypto_random_fill_cb) diff --git a/pkg/runtime/dns.odin b/pkg/runtime/dns.odin index ddd1323..0660a2f 100644 --- a/pkg/runtime/dns.odin +++ b/pkg/runtime/dns.odin @@ -7,7 +7,7 @@ import "core:strings" import jsc "lava:pkg/jsc" import eventloop "lava:pkg/runtime/eventloop" -// node:dns bridge — Tier 1 (lookup). js/internal/dns.js implements the public +// node:dns bridge — Tier 1 (lookup). js/node/dns.js implements the public // dns / dns.promises surface on top of these native primitives; the actual name // resolution is core:net's DNS client (NOT the system getaddrinfo — core:net // parses /etc/resolv.conf + /etc/hosts and speaks DNS itself), run off the event @@ -22,7 +22,7 @@ import eventloop "lava:pkg/runtime/eventloop" // queries below. The record-type queries (resolve4/resolveMx/…) and reverse/ // lookupService are Tier 2 and live behind c-ares (see pkg/std/cares). -// Result order codes shared with js/internal/dns.js (ORDER_CODES). Only the +// Result order codes shared with js/node/dns.js (ORDER_CODES). Only the // IPv6-first case changes which family we try first; verbatim and ipv4first both // try IPv4 first (core:net has no single interleaved OS order to preserve). ORDER_VERBATIM :: 0 @@ -62,7 +62,7 @@ Dns_Lookup_Request :: struct { err_code: string, // static literal, e.g. "ENOTFOUND" / "EAI_AGAIN" } -// make_dns_bindings builds the `native` object handed to js/internal/dns.js. +// make_dns_bindings builds the `native` object handed to js/node/dns.js. make_dns_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { bindings := jsc.JSObjectMake(ctx, nil, nil) inject_native_function(ctx, bindings, "lookup", dns_lookup_cb) diff --git a/pkg/runtime/environment.odin b/pkg/runtime/environment.odin index 00a4a03..5771ec8 100644 --- a/pkg/runtime/environment.odin +++ b/pkg/runtime/environment.odin @@ -125,7 +125,7 @@ inject_global_string :: proc( jsc.JSObjectSetProperty(ctx, global_obj, js_name, js_val, {}, nil) } -// node:path is implemented in JavaScript (js/internal/path.js) so the POSIX and +// node:path is implemented in JavaScript (js/node/path.js) so the POSIX and // Windows normalize/resolve/relative semantics match Node exactly; it is wired // through the internal-module loader like the other built-ins. @@ -154,7 +154,7 @@ eval_source_value :: proc( } // esm_wrap_source rewrites an ES module `source` (resolved at `filename`) into a -// self-contained CommonJS expression by calling js/internal/esm.js. The returned +// self-contained CommonJS expression by calling js/bootstrap/esm.js. The returned // string is allocated in context.allocator and must be freed by the caller. On // failure it sets `exception` (when non-nil) and returns ("", false). esm_wrap_source :: proc( diff --git a/pkg/runtime/errors.odin b/pkg/runtime/errors.odin index aa25799..6b1e889 100644 --- a/pkg/runtime/errors.odin +++ b/pkg/runtime/errors.odin @@ -154,7 +154,7 @@ err_invalid_arg_type :: proc( // determine_received_type renders the "Received …" clause of an // ERR_INVALID_ARG_TYPE message the way Node's lib/internal/errors // determineSpecificType does — and identically to this repo's JS-side validators -// (inspectReceived in js/internal/crypto.js), so native and JS errors agree: +// (inspectReceived in js/node/crypto.js), so native and JS errors agree: // // null / undefined -> "null" / "undefined" // number / boolean -> "type number (5)" / "type boolean (true)" diff --git a/pkg/runtime/fs.odin b/pkg/runtime/fs.odin index 151fc01..2b59258 100644 --- a/pkg/runtime/fs.odin +++ b/pkg/runtime/fs.odin @@ -39,7 +39,7 @@ fs_read_file_sync_cb :: proc "c" ( // BYTES ONLY. This native used to decode here when an encoding was supplied, and that // branch was deleted rather than merely bypassed: it honored the PRESENCE of an // encoding but never its value (every name decoded as UTF-8), and its conversion - // returned an empty string for input it could not decode. js/internal/fs.js owns + // returned an empty string for input it could not decode. js/node/fs.js owns // decoding now, through Buffer's codecs. Leaving the branch in place would have left // the defective implementation reachable-looking for the next reader (#329). // @@ -1295,7 +1295,7 @@ fs_exists_sync_cb :: proc "c" ( } // make_fs_bindings returns the Odin-backed primitives behind `node:fs`, for -// js/internal/fs.js to wrap. +// js/node/fs.js to wrap. // // Returns: // A fresh object carrying one native function per primitive; the loader hands it to the diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 8f22db3..bf90f8c 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -26,7 +26,7 @@ Runtime_State :: struct { // clone so module_cache_remove frees the key in O(1) (no scan to recover it). module_cache: map[string]Cached_Module, builtin_require: jsc.JSValueRef, // JS resolver for internal modules (events/util/assert/buffer); GC-protected - esm_transform: jsc.JSValueRef, // js/internal/esm.js transform(source,url,filename,dirname); GC-protected + esm_transform: jsc.JSValueRef, // js/bootstrap/esm.js transform(source,url,filename,dirname); GC-protected // Standard JS error constructors (Error/TypeError/RangeError/…) snapshotted and // GC-protected from globalThis at context init, before any user code runs (see // capture_error_intrinsics). make_native_error builds native throws from these @@ -39,7 +39,7 @@ Runtime_State :: struct { // initial JSEvaluateScript returned cleanly (see resolve_exit_code). async_failed: bool, rejection_handler: jsc.JSValueRef, // GC-protected fn registered with JSC; unprotected on destroy - // process.nextTick scheduling shim (js/internal/microtasks.js). JSC auto-drains + // process.nextTick scheduling shim (js/bootstrap/microtasks.js). JSC auto-drains // its promise-job queue at every C boundary, so to keep nextTick callbacks // ahead of those jobs (Node semantics) native must bracket the boundary itself: // dispatch_fn runs an event-loop callback and drains nextTicks before returning, @@ -663,7 +663,7 @@ clear_timer_cb :: proc "c" ( // process.nextTick and queueMicrotask are installed from JS (MICROTASK_PRELUDE) // rather than as native callbacks: their Node-matching ordering relative to // JSC's promise-job queue is expressed most naturally in JS. See -// install_microtasks and js/internal/microtasks.js. +// install_microtasks and js/bootstrap/microtasks.js. // --- console --- // @@ -960,7 +960,7 @@ performance_now_cb :: proc "c" ( // install_microtasks evaluates MICROTASK_PRELUDE to a factory and calls it with // (globalThis, process) so it can install process.nextTick and queueMicrotask -// with Node-matching ordering. See js/internal/microtasks.js for why these live +// with Node-matching ordering. See js/bootstrap/microtasks.js for why these live // in JS on top of JSC's promise-job queue rather than the native event loop. install_microtasks :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) { process := get_named(ctx, global, "process") @@ -971,7 +971,7 @@ install_microtasks :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) { ctx, "lava:microtasks", override_dir, - "js/internal/microtasks.js", + "js/bootstrap/microtasks.js", MICROTASK_PRELUDE, ) if factory == nil || !jsc.JSValueIsObject(ctx, factory) do return @@ -1000,7 +1000,7 @@ install_microtasks :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) { // The factory returns { drainNextTicks, dispatch }. Stash both (GC-protected) // on Runtime_State; invoke_user_callback drives them so process.nextTick keeps // its priority over promise jobs across JSC's auto-draining promise queue (the - // "why" is in js/internal/microtasks.js). + // "why" is in js/bootstrap/microtasks.js). if exports == nil || !jsc.JSValueIsObject(ctx, exports) do return state := get_state_from_ctx(ctx) if state == nil do return @@ -1112,72 +1112,39 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) state := get_state_from_ctx(ctx) if state == nil do return - Module :: struct { - name: string, - path: string, - source: string, - } - modules := [?]Module { - {"primordials", "js/internal/primordials.js", INTERNAL_PRIMORDIALS}, - {"util", "js/internal/util.js", INTERNAL_UTIL}, - {"events", "js/internal/events.js", INTERNAL_EVENTS}, - {"assert", "js/internal/assert.js", INTERNAL_ASSERT}, - {"buffer", "js/internal/buffer.js", INTERNAL_BUFFER}, - {"crypto", "js/internal/crypto.js", INTERNAL_CRYPTO}, - {"stream/web", "js/internal/streams.js", INTERNAL_STREAMS}, - {"stream", "js/internal/stream.js", INTERNAL_STREAM}, - {"stream/promises", "js/internal/stream_promises.js", INTERNAL_STREAM_PROMISES}, - {"fetch", "js/internal/fetch.js", INTERNAL_FETCH}, - {"abort", "js/internal/abort.js", INTERNAL_ABORT}, - {"timers/promises", "js/internal/timers_promises.js", INTERNAL_TIMERS_PROMISES}, - {"encoding", "js/internal/encoding.js", INTERNAL_ENCODING}, - {"url", "js/internal/url.js", INTERNAL_URL}, - {"structured_clone", "js/internal/structured_clone.js", INTERNAL_STRUCTURED_CLONE}, - {"path", "js/internal/path.js", INTERNAL_PATH}, - {"path/posix", "js/internal/path_posix.js", INTERNAL_PATH_POSIX}, - {"path/win32", "js/internal/path_win32.js", INTERNAL_PATH_WIN32}, - {"sqlite", "js/internal/sqlite.js", INTERNAL_SQLITE}, - {"dns", "js/internal/dns.js", INTERNAL_DNS}, - {"dns/promises", "js/internal/dns_promises.js", INTERNAL_DNS_PROMISES}, - {"net", "js/internal/net.js", INTERNAL_NET}, - {"http", "js/internal/http.js", INTERNAL_HTTP}, - {"https", "js/internal/https.js", INTERNAL_HTTPS}, - {"os", "js/internal/os.js", INTERNAL_OS}, - {"querystring", "js/internal/querystring.js", INTERNAL_QUERYSTRING}, - {"string_decoder", "js/internal/string_decoder.js", INTERNAL_STRING_DECODER}, - {"timers", "js/internal/timers.js", INTERNAL_TIMERS}, - {"util/types", "js/internal/util_types.js", INTERNAL_UTIL_TYPES}, - {"punycode", "js/internal/punycode.js", INTERNAL_PUNYCODE}, - {"process", "js/internal/process.js", INTERNAL_PROCESS}, - {"console", "js/internal/console.js", INTERNAL_CONSOLE}, - {"tty", "js/internal/tty.js", INTERNAL_TTY}, - {"diagnostics_channel", "js/internal/diagnostics_channel.js", INTERNAL_DIAGNOSTICS_CHANNEL}, - // Internal helpers backing util.parseArgs / util.parseEnv / util.MIMEType; hidden - // from the public resolver (loader.js publicReq), like primordials — Node has no - // node:parse_args or node:mime. - {"parse_args", "js/internal/parse_args.js", INTERNAL_PARSE_ARGS}, - {"parse_env", "js/internal/parse_env.js", INTERNAL_PARSE_ENV}, - {"mime", "js/internal/mime.js", INTERNAL_MIME}, - {"stdio", "js/internal/stdio.js", INTERNAL_STDIO}, - {"fs", "js/internal/fs.js", INTERNAL_FS}, - } - + // Module id, source name and override path all derive from the directory and the + // file's basename, so the tree under js/ is the only list of modules: adding + // js/node/foo.js makes `node:foo` requireable with no Odin edit, and a file under + // js/internal/ is reachable only through the factories' own `require`. override_dir := js_override_dir() factories := jsc.JSObjectMake(ctx, nil, nil) - for m in modules { - factory := eval_internal_source(ctx, m.name, override_dir, m.path, m.source) - if factory == nil do continue - set_named(ctx, factories, m.name, factory) + for dir in EMBEDDED_JS_DIRS { + for file in dir.files { + if !strings.has_suffix(file.name, ".js") do continue + id, id_err := strings.concatenate({dir.prefix, "/", file.name[:len(file.name) - 3]}, context.temp_allocator) + if id_err != nil do continue + rel_path, rel_err := strings.concatenate({"js/", id, ".js"}, context.temp_allocator) + if rel_err != nil do continue + // Source name is what a stack frame shows: `node:buffer`, `node:stream/web` + // for public modules and `node:internal/fetch` for internal ones, the shape + // node's own frames take (`node:internal/url`). + spec := id[len("node/"):] if strings.has_prefix(id, "node/") else id + source_name, name_err := strings.concatenate({"node:", spec}, context.temp_allocator) + if name_err != nil do continue + factory := eval_internal_source(ctx, source_name, override_dir, rel_path, string(file.data)) + if factory == nil do continue + set_named(ctx, factories, id, factory) + } } - loader := eval_internal_source(ctx, "internal:loader", override_dir, "js/internal/loader.js", INTERNAL_LOADER) + loader := eval_internal_source(ctx, "internal:loader", override_dir, "js/bootstrap/loader.js", BOOTSTRAP_LOADER) if loader == nil || !jsc.JSValueIsObject(ctx, loader) do return - // Native primitives keyed by module name. The loader passes natives[key] to + // Native primitives keyed by module id. The loader passes natives[id] to // each factory as its fourth argument, so a module receives its Odin-backed // bindings without anything landing on globalThis (cf. install_console). natives := jsc.JSObjectMake(ctx, nil, nil) - set_named(ctx, natives, "crypto", cast(jsc.JSValueRef)make_crypto_bindings(ctx)) + set_named(ctx, natives, "node/crypto", cast(jsc.JSValueRef)make_crypto_bindings(ctx)) // The SAME bindings object serves both `buffer` and `encoding`. encoding.js // used to reach the utf-8 decoder as `Buffer.prototype.toString.call(bytes, // 'utf8')`, which put a live `Function.prototype.call` read on the RETURN path @@ -1186,28 +1153,28 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) // encoding.js the codec directly removes the read, and skips toString's length // getter, range clamp and encoding-name dispatch on the way. buffer_bindings := make_buffer_bindings(ctx) - set_named(ctx, natives, "buffer", cast(jsc.JSValueRef)buffer_bindings) - set_named(ctx, natives, "encoding", cast(jsc.JSValueRef)buffer_bindings) - set_named(ctx, natives, "fetch", cast(jsc.JSValueRef)make_fetch_bindings(ctx)) - set_named(ctx, natives, "sqlite", cast(jsc.JSValueRef)make_sqlite_bindings(ctx)) - set_named(ctx, natives, "dns", cast(jsc.JSValueRef)make_dns_bindings(ctx)) - set_named(ctx, natives, "net", cast(jsc.JSValueRef)make_net_bindings(ctx)) - set_named(ctx, natives, "http", cast(jsc.JSValueRef)make_http_bindings(ctx)) - set_named(ctx, natives, "https", cast(jsc.JSValueRef)make_https_bindings(ctx)) - set_named(ctx, natives, "os", cast(jsc.JSValueRef)make_os_bindings(ctx)) - set_named(ctx, natives, "tty", cast(jsc.JSValueRef)make_tty_bindings(ctx)) - set_named(ctx, natives, "stdio", cast(jsc.JSValueRef)make_stdio_bindings(ctx)) - set_named(ctx, natives, "fs", cast(jsc.JSValueRef)make_fs_bindings(ctx)) + set_named(ctx, natives, "node/buffer", cast(jsc.JSValueRef)buffer_bindings) + set_named(ctx, natives, "internal/encoding", cast(jsc.JSValueRef)buffer_bindings) + set_named(ctx, natives, "internal/fetch", cast(jsc.JSValueRef)make_fetch_bindings(ctx)) + set_named(ctx, natives, "node/sqlite", cast(jsc.JSValueRef)make_sqlite_bindings(ctx)) + set_named(ctx, natives, "node/dns", cast(jsc.JSValueRef)make_dns_bindings(ctx)) + set_named(ctx, natives, "node/net", cast(jsc.JSValueRef)make_net_bindings(ctx)) + set_named(ctx, natives, "node/http", cast(jsc.JSValueRef)make_http_bindings(ctx)) + set_named(ctx, natives, "node/https", cast(jsc.JSValueRef)make_https_bindings(ctx)) + set_named(ctx, natives, "node/os", cast(jsc.JSValueRef)make_os_bindings(ctx)) + set_named(ctx, natives, "node/tty", cast(jsc.JSValueRef)make_tty_bindings(ctx)) + set_named(ctx, natives, "internal/stdio", cast(jsc.JSValueRef)make_stdio_bindings(ctx)) + set_named(ctx, natives, "node/fs", cast(jsc.JSValueRef)make_fs_bindings(ctx)) // Intrinsic process/console objects. Captured here (context init, before user // code) and handed to the factories so require('node:process') / // require('node:console') never re-read the globals — node returns the // intrinsic even when a dependency reassigned globalThis.process / console // before the first require (#247). install_process runs before this proc. if process_val := get_named(ctx, global, "process"); process_val != nil { - set_named(ctx, natives, "process", process_val) + set_named(ctx, natives, "node/process", process_val) } if console_val := get_named(ctx, global, "console"); console_val != nil { - set_named(ctx, natives, "console", console_val) + set_named(ctx, natives, "node/console", console_val) } args := [2]jsc.JSValueRef{cast(jsc.JSValueRef)factories, cast(jsc.JSValueRef)natives} @@ -1229,7 +1196,7 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) jsc.JSValueProtect(ctx, resolver) state.builtin_require = resolver - // The ESM source transform (js/internal/esm.js) evaluates to a FACTORY — + // The ESM source transform (js/bootstrap/esm.js) evaluates to a FACTORY — // `factory(primordials) -> transform(source, url, filename, dirname)`. The // transform is stored on the state, not registered as a requireable module, so // native_require_cb can rewrite `.mjs` files to CommonJS without exposing it to @@ -1245,7 +1212,7 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) // Fail CLOSED. Without the table the transform would run on live intrinsics, so a // missing one leaves state.esm_transform nil and `.mjs` reports "ESM transform // unavailable" instead of silently transforming with the poisonable path. - esm_factory := eval_internal_source(ctx, "lava:esm", override_dir, "js/internal/esm.js", INTERNAL_ESM) + esm_factory := eval_internal_source(ctx, "lava:esm", override_dir, "js/bootstrap/esm.js", BOOTSTRAP_ESM) if esm_factory != nil && jsc.JSValueIsObject(ctx, esm_factory) { primordials := get_named(ctx, cast(jsc.JSObjectRef)resolver, "primordials") if primordials != nil && jsc.JSValueIsObject(ctx, primordials) { @@ -1336,7 +1303,7 @@ install_console :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) { if out_fn == nil || err_fn == nil do return override_dir := js_override_dir() - factory := eval_internal_source(ctx, "lava:console", override_dir, "js/console.js", CONSOLE_PRELUDE) + factory := eval_internal_source(ctx, "lava:console", override_dir, "js/bootstrap/console.js", CONSOLE_PRELUDE) if factory == nil || !jsc.JSValueIsObject(ctx, factory) do return args := [2]jsc.JSValueRef{cast(jsc.JSValueRef)out_fn, cast(jsc.JSValueRef)err_fn} @@ -1540,55 +1507,46 @@ build_env_object :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { // installs it on globalThis. Implemented in JS (like Node) so util.format // substitution, value inspection, grouping and table rendering stay simple. The // primitives arrive as arguments, so they are never visible on globalThis. -CONSOLE_PRELUDE :: #load("js/console.js", string) +CONSOLE_PRELUDE :: #load("js/bootstrap/console.js", string) // process.nextTick / queueMicrotask ordering shim (factory `(globalThis, // process) => {}`). Installed after `process` exists; see install_microtasks. -MICROTASK_PRELUDE :: #load("js/internal/microtasks.js", string) - -// Internal built-in modules, embedded at compile time. Each evaluates to a -// factory `(require, module, exports) => exports?`; the loader wires them up. -INTERNAL_LOADER :: #load("js/internal/loader.js", string) -INTERNAL_PRIMORDIALS :: #load("js/internal/primordials.js", string) -INTERNAL_UTIL :: #load("js/internal/util.js", string) -INTERNAL_EVENTS :: #load("js/internal/events.js", string) -INTERNAL_ASSERT :: #load("js/internal/assert.js", string) -INTERNAL_BUFFER :: #load("js/internal/buffer.js", string) -INTERNAL_CRYPTO :: #load("js/internal/crypto.js", string) -INTERNAL_STREAMS :: #load("js/internal/streams.js", string) -INTERNAL_STREAM :: #load("js/internal/stream.js", string) -INTERNAL_STREAM_PROMISES :: #load("js/internal/stream_promises.js", string) -INTERNAL_FETCH :: #load("js/internal/fetch.js", string) -INTERNAL_ABORT :: #load("js/internal/abort.js", string) -INTERNAL_TIMERS_PROMISES :: #load("js/internal/timers_promises.js", string) -INTERNAL_ENCODING :: #load("js/internal/encoding.js", string) -INTERNAL_URL :: #load("js/internal/url.js", string) -INTERNAL_STRUCTURED_CLONE :: #load("js/internal/structured_clone.js", string) -INTERNAL_PATH :: #load("js/internal/path.js", string) -INTERNAL_PATH_POSIX :: #load("js/internal/path_posix.js", string) -INTERNAL_PATH_WIN32 :: #load("js/internal/path_win32.js", string) -INTERNAL_SQLITE :: #load("js/internal/sqlite.js", string) -INTERNAL_DNS :: #load("js/internal/dns.js", string) -INTERNAL_DNS_PROMISES :: #load("js/internal/dns_promises.js", string) -INTERNAL_OS :: #load("js/internal/os.js", string) -INTERNAL_QUERYSTRING :: #load("js/internal/querystring.js", string) -INTERNAL_STRING_DECODER :: #load("js/internal/string_decoder.js", string) -INTERNAL_TIMERS :: #load("js/internal/timers.js", string) -INTERNAL_UTIL_TYPES :: #load("js/internal/util_types.js", string) -INTERNAL_PUNYCODE :: #load("js/internal/punycode.js", string) -INTERNAL_PROCESS :: #load("js/internal/process.js", string) -INTERNAL_CONSOLE :: #load("js/internal/console.js", string) -INTERNAL_TTY :: #load("js/internal/tty.js", string) -INTERNAL_FS :: #load("js/internal/fs.js", string) -INTERNAL_NET :: #load("js/internal/net.js", string) -INTERNAL_HTTP :: #load("js/internal/http.js", string) -INTERNAL_HTTPS :: #load("js/internal/https.js", string) -INTERNAL_DIAGNOSTICS_CHANNEL :: #load("js/internal/diagnostics_channel.js", string) -INTERNAL_PARSE_ARGS :: #load("js/internal/parse_args.js", string) -INTERNAL_PARSE_ENV :: #load("js/internal/parse_env.js", string) -INTERNAL_MIME :: #load("js/internal/mime.js", string) -INTERNAL_STDIO :: #load("js/internal/stdio.js", string) +MICROTASK_PRELUDE :: #load("js/bootstrap/microtasks.js", string) + +// The module loader: evaluates to `(factories, natives) => resolver`. A bootstrap +// file, not a module — it is what makes the modules below requireable. +BOOTSTRAP_LOADER :: #load("js/bootstrap/loader.js", string) // ESM-to-CommonJS source transform. Stored on Runtime_State rather than handed to -// the module resolver (see install_internal_modules); evaluates to a function. -INTERNAL_ESM :: #load("js/internal/esm.js", string) +// the module resolver (see install_internal_modules); evaluates to a function. It +// lives in js/bootstrap/, which no #load_directory reads, precisely so it can never +// land on the module table. +BOOTSTRAP_ESM :: #load("js/bootstrap/esm.js", string) + +// Embedded_JS_Dir is one directory of built-in modules, embedded whole. Each file +// evaluates to a factory `(require, module, exports, native) => exports?`; its id +// is `/`, which is also the path under js/. +Embedded_JS_Dir :: struct { + prefix: string, + files: []runtime.Load_Directory_File, +} + +// EMBEDDED_JS_DIRS lists DIRECTORIES, never files: a module enters the binary by +// existing under one of these, so there is no hand-kept file table to drift from the +// tree (scripts/check-js-graph.mjs fails when a directory holding .js is missing +// here). One entry per directory because #load_directory is not recursive — it +// returns basenames and skips subdirectories, which is also why `node/stream/web` +// needs the `node/stream` row. It loads every file, so the loop filters on `.js`. +// +// A package-level VARIABLE, not a `::` constant: `#load_directory` compiles only in +// a package-level `:=` declaration (odin 2026-11a rejects the constant and the +// proc-local forms). Nothing writes to it after init. +EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { + {"node", #load_directory("js/node")}, + {"node/dns", #load_directory("js/node/dns")}, + {"node/path", #load_directory("js/node/path")}, + {"node/stream", #load_directory("js/node/stream")}, + {"node/timers", #load_directory("js/node/timers")}, + {"node/util", #load_directory("js/node/util")}, + {"internal", #load_directory("js/internal")}, +} diff --git a/pkg/runtime/http.odin b/pkg/runtime/http.odin index 04b9a83..d450cb5 100644 --- a/pkg/runtime/http.odin +++ b/pkg/runtime/http.odin @@ -7,7 +7,7 @@ import jsc "lava:pkg/jsc" import pico "lava:pkg/runtime/picohttpparser" // node:http server support — the request-HEAD parser bridge. The protocol surface -// (Server, IncomingMessage, ServerResponse) lives in js/internal/http.js on top of +// (Server, IncomingMessage, ServerResponse) lives in js/node/http.js on top of // node:net; this exposes one native primitive, parseRequest(buf, lastLen), that runs // picohttpparser (phr_parse_request) over the accumulated request bytes and returns one // flat ARRAY (built with a single JSObjectMakeArray call — the cheapest possible shape diff --git a/pkg/runtime/js/console.js b/pkg/runtime/js/bootstrap/console.js similarity index 99% rename from pkg/runtime/js/console.js rename to pkg/runtime/js/bootstrap/console.js index d6ff2a8..27a33d3 100644 --- a/pkg/runtime/js/console.js +++ b/pkg/runtime/js/bootstrap/console.js @@ -94,7 +94,7 @@ // `%%` was folded unconditionally. // `typeof first === 'string'` matters: `format(5, '%%')` has no format string // at all, so node inspects and space-joins, leaving the later `%%` literal. - // Duplicated from internal/util.js rather than shared, and deliberately: this + // Duplicated from node/util.js rather than shared, and deliberately: this // prelude runs before the module loader, receives only the native write // functions, and has no `require` to reach util with. if (args.length === 1 && typeof first === 'string') return first; diff --git a/pkg/runtime/js/internal/esm.js b/pkg/runtime/js/bootstrap/esm.js similarity index 99% rename from pkg/runtime/js/internal/esm.js rename to pkg/runtime/js/bootstrap/esm.js index 06f7283..499cf51 100644 --- a/pkg/runtime/js/internal/esm.js +++ b/pkg/runtime/js/bootstrap/esm.js @@ -127,7 +127,7 @@ // gated by `isIdent`/`RE_SPEC_AS` — which is why it is recorded here rather than // rewritten into a code-unit scan. // -// This module cannot `require('primordials')`: the runtime evaluates it standalone +// This module cannot `require('internal/primordials')`: the runtime evaluates it standalone // and keeps the result on Runtime_State rather than registering it as a requireable // module (globals.odin), precisely so user code cannot reach the transform. It // therefore takes the table as an argument — loader.js hands out the one already diff --git a/pkg/runtime/js/internal/loader.js b/pkg/runtime/js/bootstrap/loader.js similarity index 67% rename from pkg/runtime/js/internal/loader.js rename to pkg/runtime/js/bootstrap/loader.js index 16f9ec7..9522960 100644 --- a/pkg/runtime/js/internal/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -1,9 +1,14 @@ -// Internal built-in module loader. Given a map of module name -> factory -// function `(require, module, exports) => exports?`, it returns a `require` +// Internal built-in module loader. Given a map of module id -> factory function +// `(require, module, exports, native) => exports?`, it returns a `require` // implementation that instantiates each module lazily, caches it, strips the // `node:` prefix, and lets internal modules require one another (with the usual // CommonJS partial-exports behavior on cycles). The resolver is what native // `require()` consults before touching the filesystem. +// +// A module id is its path under pkg/runtime/js without `.js` — `node/buffer`, +// `node/stream/web`, `internal/primordials` — so the DIRECTORY decides visibility: +// the public resolver answers `node/` ids only, and everything under `internal/` +// is reachable solely through the `require` handed to the factories. (function (factories, natives) { 'use strict'; @@ -11,31 +16,50 @@ natives = natives || Object.create(null); var hasOwn = Object.prototype.hasOwnProperty; - function normalize(name) { + // Prefix test by code unit. The specifier comes from user code on the public path, + // so a String.prototype method here would be a live, replaceable read deciding + // whether an internal module is handed out. + function hasPrefix(s, prefix) { + if (s.length < prefix.length) return false; + for (var i = 0; i < prefix.length; i++) { + if (s[i] !== prefix[i]) return false; + } + return true; + } + + // Specifier -> module id. `internal/x` names the internal module; anything else is + // a public builtin under `node/`. A specifier that already reads `node/buffer` + // becomes `node/node/buffer` and resolves to nothing, so the id is not a second + // public spelling. + function toId(name) { var key = name.indexOf('node:') === 0 ? name.slice(5) : name; // assert/strict shares assert's (already strict) implementation. if (key === 'assert/strict') key = 'assert'; - return key; + return hasPrefix(key, 'internal/') ? key : 'node/' + key; } - function req(name) { - var key = normalize(name); - if (key in cache) return cache[key]; + function load(id) { + if (id in cache) return cache[id]; // Look up own properties only: `factories` may inherit from Object.prototype, - // so a bracket lookup of 'constructor'/'toString' would otherwise return an - // inherited function and mis-resolve those specifiers instead of yielding the - // "not a builtin" (undefined) that lets native_require_cb fall through. - if (!hasOwn.call(factories, key)) return; - var factory = factories[key]; + // so a bracket lookup of 'node/constructor' or a polluted prototype key would + // otherwise return an inherited function and mis-resolve that specifier instead + // of yielding the "not a builtin" (undefined) that lets native_require_cb fall + // through. + if (!hasOwn.call(factories, id)) return; + var factory = factories[id]; var module = { exports: {} }; // Seed the cache before running the factory so a require cycle resolves // to the partially-built exports object instead of looping forever. - cache[key] = module.exports; - // natives[key] (or undefined) carries any Odin-backed primitives for this + cache[id] = module.exports; + // natives[id] (or undefined) carries any Odin-backed primitives for this // module — e.g. crypto's CSPRNG and one-shot hash/hmac/pbkdf2. - var result = factory(req, module, module.exports, natives[key]); - cache[key] = result !== undefined ? result : module.exports; - return cache[key]; + var result = factory(req, module, module.exports, natives[id]); + cache[id] = result !== undefined ? result : module.exports; + return cache[id]; + } + + function req(name) { + return load(toId(name)); } // Eagerly instantiate modules that install globals (Buffer; the Web Streams @@ -49,8 +73,8 @@ // codecs, so the order is belt-and-suspenders rather than load-bearing. // primordials is eager-loaded FIRST, before any other internal module and well // before user code, so it captures pristine intrinsics (see primordials.js). - // Other modules require('primordials') and get this cached, pristine table. - req('primordials'); + // Other modules require('internal/primordials') and get this cached, pristine table. + req('internal/primordials'); req('buffer'); // Hand the LAZY internal modules a PRISTINE Buffer. @@ -96,27 +120,27 @@ // primordials table sets this convention (`ArrayPrototypeSlice: caller2(...)`) — a raw // unbound method invites each consumer to re-derive it, and two consumers deriving it // differently is how the six copies of describeType happened. - bufferToString: req('primordials').uncurryThis(PristineBufferCtor.prototype.toString), + bufferToString: req('internal/primordials').uncurryThis(PristineBufferCtor.prototype.toString), // The rest of the framing path in http.js's chunked decoder: it reads the chunk-size // line, finds the CRLF boundary and advances the cursor through these three. Every one // was a live `Buffer.prototype` read, i.e. a request-smuggling gadget — a replaced // `toString` that INFLATES the size is the sharp version. Node frames HTTP in C // (llhttp) and is not steerable here at all. - bufferIndexOf: req('primordials').uncurryThis(PristineBufferCtor.prototype.indexOf), - bufferSlice: req('primordials').uncurryThis(PristineBufferCtor.prototype.slice), + bufferIndexOf: req('internal/primordials').uncurryThis(PristineBufferCtor.prototype.indexOf), + bufferSlice: req('internal/primordials').uncurryThis(PristineBufferCtor.prototype.slice), }; req('stream/web'); - req('fetch'); - req('abort'); - req('encoding'); + req('internal/fetch'); + req('internal/abort'); + req('internal/encoding'); req('url'); - req('structured_clone'); + req('internal/structured_clone'); req('crypto'); - // Internal-only modules: internal factories require() them through `req` above, - // but they must NOT be reachable through the public resolver native require() - // consults, and publicReq hides them. Two reasons a name lands here: + // The public resolver: what native require() consults. It answers `node/` ids only, + // so nothing under internal/ is reachable from user code, for two reasons a module + // lives there: // 1. It has no Node builtin at all — require('node:primordials') is // ERR_UNKNOWN_BUILTIN_MODULE; util.parseArgs / util.MIMEType are the public // surfaces for parse_args / mime. @@ -124,34 +148,19 @@ // TextEncoder/TextDecoder, and structuredClone are installed on globalThis // by the eager loads above. `encoding` in particular is a real npm package // (a transitive dep of node-fetch), so answering require('encoding') from - // here would shadow it and break the ecosystem. Hiding the specifier leaves - // the globals intact while letting the filesystem resolver find the package. - // `__proto__: null` because this literal is indexed by a CALLER-SUPPLIED specifier - // (`INTERNAL_ONLY[normalize(name)]` below) — §5's one vector the ratchet cannot - // count. Inherited truthiness makes publicReq answer `undefined` for a real builtin, - // so `require('node:http')` falls through to filesystem resolution: a denial, or a - // shadow when an npm package of that name is installed (`os`, `path`, `url`, `util` - // and `events` all exist on the registry). The sibling lookup at `factories` above - // already guards this with hasOwn; this table was missed. - var INTERNAL_ONLY = { - __proto__: null, - primordials: true, - parse_args: true, - parse_env: true, - mime: true, - fetch: true, - abort: true, - encoding: true, - structured_clone: true, - stdio: true, - }; + // here would shadow it and break the ecosystem. Not answering leaves the + // globals intact while letting the filesystem resolver find the package. + // `require('fetch')` maps to `node/fetch`, which has no factory; `internal/fetch` + // fails the prefix test below. Both return undefined and fall through to the + // filesystem, never a shadow of an installed package. function publicReq(name) { - if (INTERNAL_ONLY[normalize(name)]) return undefined; - return req(name); + var id = toId(name); + if (!hasPrefix(id, 'node/')) return undefined; + return load(id); } // The ESM source transform is the one internal module that cannot - // require('primordials'): the runtime evaluates js/internal/esm.js standalone and + // require('internal/primordials'): the runtime evaluates js/bootstrap/esm.js standalone and // keeps the resulting function on Runtime_State, deliberately NOT registering it // as a requireable module (see globals.odin), so it never gets a `require`. It // still needs the pristine table — it emits executed source, so a live intrinsic @@ -164,7 +173,7 @@ // This is not a new user-visible surface: the resolver object never reaches user // code. What user code calls is the native require callback, which consults this // function through Runtime_State.builtin_require and returns only module exports. - publicReq.primordials = req('primordials'); + publicReq.primordials = req('internal/primordials'); // stdio installs process.stdout/process.stderr. It cannot be eager-required here: // install_process runs before the loader (so natives['process'] can capture the @@ -172,7 +181,7 @@ // the loader returns, and stdio/stream need nextTick. Exposed as a closure so // globals.odin can call it at the right moment, after microtasks are wired. publicReq.installStdio = function (proc) { - req('stdio').install(proc); + req('internal/stdio').install(proc); }; return publicReq; diff --git a/pkg/runtime/js/internal/microtasks.js b/pkg/runtime/js/bootstrap/microtasks.js similarity index 100% rename from pkg/runtime/js/internal/microtasks.js rename to pkg/runtime/js/bootstrap/microtasks.js diff --git a/pkg/runtime/js/internal/encoding.js b/pkg/runtime/js/internal/encoding.js index 26c5a6f..aab2c1b 100644 --- a/pkg/runtime/js/internal/encoding.js +++ b/pkg/runtime/js/internal/encoding.js @@ -23,7 +23,7 @@ // all — see the note on pushCodePoint. Routing those per-code-unit loops // through the variadic ArrayPrototypePush measured 1.43x on the JS decode // path (callerN's arguments switch is ~7x a direct call). - var P = require('primordials'); + var P = require('internal/primordials'); var StringPrototypeSlice = P.StringPrototypeSlice; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeCodePointAt = P.StringPrototypeCodePointAt; diff --git a/pkg/runtime/js/internal/fetch.js b/pkg/runtime/js/internal/fetch.js index 2e0146b..a613f7d 100644 --- a/pkg/runtime/js/internal/fetch.js +++ b/pkg/runtime/js/internal/fetch.js @@ -7,7 +7,7 @@ (function (require, module, exports, native) { 'use strict'; - // The public Web Streams implementation (js/internal/streams.js). fetch builds + // The public Web Streams implementation (js/node/stream/web.js). fetch builds // response bodies and consumes request bodies through this same ReadableStream // type, so `response.body` is a real, standard ReadableStream — not a fetch-only // fork. The `_internal` helpers let the transport drive a stream's controller @@ -63,7 +63,7 @@ // now a charCode trim rather than a global `replaceAll`, because a global regex // does not merely answer wrongly under a forged `exec` — it never terminates. // `assertValidHeaderValue` was already a hand-rolled charCode scan. - var P = require('primordials'); + var P = require('internal/primordials'); var reTest = P.RegExpMatches; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; diff --git a/pkg/runtime/js/internal/mime.js b/pkg/runtime/js/internal/mime.js index 0c2a8a9..09b6ac9 100644 --- a/pkg/runtime/js/internal/mime.js +++ b/pkg/runtime/js/internal/mime.js @@ -8,7 +8,7 @@ // Captured for encodeValue, which replaced a global-flag replace reachable from a // remote Content-Type. This file is not otherwise hardened — see the ratchet baseline. - var P = require('primordials'); + var P = require('internal/primordials'); var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; var RegExpMatches = P.RegExpMatches; diff --git a/pkg/runtime/js/internal/parse_args.js b/pkg/runtime/js/internal/parse_args.js index abff424..0846b26 100644 --- a/pkg/runtime/js/internal/parse_args.js +++ b/pkg/runtime/js/internal/parse_args.js @@ -10,7 +10,7 @@ (function (require, module) { 'use strict'; - var P = require('primordials'); + var P = require('internal/primordials'); var TypeError = P.TypeError; var ObjectKeys = P.ObjectKeys; var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; diff --git a/pkg/runtime/js/internal/parse_env.js b/pkg/runtime/js/internal/parse_env.js index d4c9cff..23484b3 100644 --- a/pkg/runtime/js/internal/parse_env.js +++ b/pkg/runtime/js/internal/parse_env.js @@ -12,7 +12,7 @@ (function (require, module) { 'use strict'; - var P = require('primordials'); + var P = require('internal/primordials'); var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; var StringPrototypeTrim = P.StringPrototypeTrim; diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index e277ac1..1fe01ed 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -1,7 +1,7 @@ // Primordials — pristine references to the JS intrinsics the internal modules // rely on, captured once at startup *before any user code runs* (the loader // eager-requires this module first; see loader.js). Internal modules consume it -// via `require('primordials')` so their behavior cannot be altered by a script +// via `require('internal/primordials')` so their behavior cannot be altered by a script // that mutates a shared prototype (Array.prototype.push = …) or replaces a global // (globalThis.Object = …). Mirrors Node's lib/internal/primordials in spirit: // captured statics plus prototype methods callable as fn(thisArg, ...args). diff --git a/pkg/runtime/js/internal/stdio.js b/pkg/runtime/js/internal/stdio.js index c342f37..c187ff0 100644 --- a/pkg/runtime/js/internal/stdio.js +++ b/pkg/runtime/js/internal/stdio.js @@ -28,7 +28,7 @@ (function (require, module, exports, native) { 'use strict'; - var P = require('primordials'); + var P = require('internal/primordials'); var ObjectDefineProperty = P.ObjectDefineProperty; var ObjectSetPrototypeOf = P.ObjectSetPrototypeOf; var TypeErrorG = P.TypeError; diff --git a/pkg/runtime/js/internal/structured_clone.js b/pkg/runtime/js/internal/structured_clone.js index 820c69b..d7541ab 100644 --- a/pkg/runtime/js/internal/structured_clone.js +++ b/pkg/runtime/js/internal/structured_clone.js @@ -19,7 +19,7 @@ var toString = Object.prototype.toString; - var P = require('primordials'); + var P = require('internal/primordials'); // Rebuilding a view through `value.constructor` is a configurable // prototype-chain read: poisoning `Uint8Array.prototype.constructor` made // structuredClone invoke attacker code with the internals' freshly cloned diff --git a/pkg/runtime/js/internal/assert.js b/pkg/runtime/js/node/assert.js similarity index 100% rename from pkg/runtime/js/internal/assert.js rename to pkg/runtime/js/node/assert.js diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/node/buffer.js similarity index 99% rename from pkg/runtime/js/internal/buffer.js rename to pkg/runtime/js/node/buffer.js index 44c83ed..1b5c9f2 100644 --- a/pkg/runtime/js/internal/buffer.js +++ b/pkg/runtime/js/node/buffer.js @@ -53,7 +53,7 @@ // `String.prototype.toLowerCase` reached a hardened TextDecoder through here and // turned a valid decode into `Unknown encoding: …`. The rest of buffer.js is not // yet converted (see tests/node-compat/pollution-baseline.json). - var P = require('primordials'); + var P = require('internal/primordials'); var StringG = String; var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; diff --git a/pkg/runtime/js/internal/console.js b/pkg/runtime/js/node/console.js similarity index 96% rename from pkg/runtime/js/internal/console.js rename to pkg/runtime/js/node/console.js index 7baa41b..865393e 100644 --- a/pkg/runtime/js/internal/console.js +++ b/pkg/runtime/js/node/console.js @@ -19,7 +19,7 @@ // Through primordials, not a bare `new Error(...)` — same reason as process.js: // a lazy factory body is call-time code, so the global it reads is whatever user // code left there before the first require. - var ErrorG = require('primordials').Error; + var ErrorG = require('internal/primordials').Error; if (intrinsic == null) { throw new ErrorG('node:console intrinsic missing at context init'); } diff --git a/pkg/runtime/js/internal/crypto.js b/pkg/runtime/js/node/crypto.js similarity index 100% rename from pkg/runtime/js/internal/crypto.js rename to pkg/runtime/js/node/crypto.js diff --git a/pkg/runtime/js/internal/diagnostics_channel.js b/pkg/runtime/js/node/diagnostics_channel.js similarity index 100% rename from pkg/runtime/js/internal/diagnostics_channel.js rename to pkg/runtime/js/node/diagnostics_channel.js diff --git a/pkg/runtime/js/internal/dns.js b/pkg/runtime/js/node/dns.js similarity index 100% rename from pkg/runtime/js/internal/dns.js rename to pkg/runtime/js/node/dns.js diff --git a/pkg/runtime/js/internal/dns_promises.js b/pkg/runtime/js/node/dns/promises.js similarity index 100% rename from pkg/runtime/js/internal/dns_promises.js rename to pkg/runtime/js/node/dns/promises.js diff --git a/pkg/runtime/js/internal/events.js b/pkg/runtime/js/node/events.js similarity index 99% rename from pkg/runtime/js/internal/events.js rename to pkg/runtime/js/node/events.js index a84eff3..43edfb4 100644 --- a/pkg/runtime/js/internal/events.js +++ b/pkg/runtime/js/node/events.js @@ -5,7 +5,7 @@ // Pristine intrinsics (see primordials.js) so a script that mutates // Array.prototype/Object/etc. cannot alter EventEmitter's internal bookkeeping. - var P = require('primordials'); + var P = require('internal/primordials'); var defaultMaxListeners = 10; // Species-free array helpers. Array.prototype.slice/map/splice consult diff --git a/pkg/runtime/js/internal/fs.js b/pkg/runtime/js/node/fs.js similarity index 99% rename from pkg/runtime/js/internal/fs.js rename to pkg/runtime/js/node/fs.js index bc8ff35..82c6fbc 100644 --- a/pkg/runtime/js/internal/fs.js +++ b/pkg/runtime/js/node/fs.js @@ -26,7 +26,7 @@ (function (require, module, exports, native) { 'use strict'; - var P = require('primordials'); + var P = require('internal/primordials'); var TypeErrorG = P.TypeError; var StringPrototypeSlice = P.StringPrototypeSlice; var ArrayBufferIsView = P.ArrayBufferIsView; diff --git a/pkg/runtime/js/internal/http.js b/pkg/runtime/js/node/http.js similarity index 99% rename from pkg/runtime/js/internal/http.js rename to pkg/runtime/js/node/http.js index d873ae3..31f2c59 100644 --- a/pkg/runtime/js/internal/http.js +++ b/pkg/runtime/js/node/http.js @@ -33,7 +33,7 @@ var BufferPrototypeSlice = PristineBuffer.bufferSlice; var net = require('net'); /** Pristine intrinsics — response head must not use overridable Buffer methods. */ - var primordials = require('primordials'); + var primordials = require('internal/primordials'); // Every regex below decides FRAMING over attacker-controlled bytes, so none of // them may go through `re.test(...)`: `RegExp.prototype.exec` is a writable data // property, and the spec's RegExpExec re-reads it off the receiver, so a plain diff --git a/pkg/runtime/js/internal/https.js b/pkg/runtime/js/node/https.js similarity index 100% rename from pkg/runtime/js/internal/https.js rename to pkg/runtime/js/node/https.js diff --git a/pkg/runtime/js/internal/net.js b/pkg/runtime/js/node/net.js similarity index 100% rename from pkg/runtime/js/internal/net.js rename to pkg/runtime/js/node/net.js diff --git a/pkg/runtime/js/internal/os.js b/pkg/runtime/js/node/os.js similarity index 99% rename from pkg/runtime/js/internal/os.js rename to pkg/runtime/js/node/os.js index 81d655f..2f9652e 100644 --- a/pkg/runtime/js/internal/os.js +++ b/pkg/runtime/js/node/os.js @@ -16,7 +16,7 @@ // every call, so `require('buffer').Buffer = shim` steered it at any time, not merely // before the first require of this module (#333). // Through primordials, not captured here: this module is lazy (#333). - var OsPrimordials = require('primordials'); + var OsPrimordials = require('internal/primordials'); var StringG = OsPrimordials.String; var StringPrototypeToLowerCase = OsPrimordials.StringPrototypeToLowerCase; var PristineBuffer = require.pristineBuffer; diff --git a/pkg/runtime/js/internal/path.js b/pkg/runtime/js/node/path.js similarity index 99% rename from pkg/runtime/js/internal/path.js rename to pkg/runtime/js/node/path.js index cd629c3..8d0c502 100644 --- a/pkg/runtime/js/internal/path.js +++ b/pkg/runtime/js/node/path.js @@ -8,7 +8,7 @@ // Captured for matchesGlob, which replaced two global-flag replaces. This file is not // otherwise hardened — see the ratchet baseline. - var replaceCharAll = require('primordials').replaceCharAll; + var replaceCharAll = require('internal/primordials').replaceCharAll; var SLASH = 47; // '/' var BACKSLASH = 92; // '\' diff --git a/pkg/runtime/js/internal/path_posix.js b/pkg/runtime/js/node/path/posix.js similarity index 100% rename from pkg/runtime/js/internal/path_posix.js rename to pkg/runtime/js/node/path/posix.js diff --git a/pkg/runtime/js/internal/path_win32.js b/pkg/runtime/js/node/path/win32.js similarity index 75% rename from pkg/runtime/js/internal/path_win32.js rename to pkg/runtime/js/node/path/win32.js index 7a9a02f..bef9348 100644 --- a/pkg/runtime/js/internal/path_win32.js +++ b/pkg/runtime/js/node/path/win32.js @@ -1,5 +1,5 @@ // node:path/win32 — the Windows path variant, surfaced under its own specifier -// (Node: require('node:path/win32')). See path_posix.js. +// (Node: require('node:path/win32')). See posix.js. (function (require, module) { 'use strict'; module.exports = require('path').win32; diff --git a/pkg/runtime/js/internal/process.js b/pkg/runtime/js/node/process.js similarity index 96% rename from pkg/runtime/js/internal/process.js rename to pkg/runtime/js/node/process.js index 1653d45..818dadd 100644 --- a/pkg/runtime/js/internal/process.js +++ b/pkg/runtime/js/node/process.js @@ -21,7 +21,7 @@ // chance to replace globalThis.Error. The ratchet cannot see this (it exempts // every read at module-eval depth, which is what a factory body is), so the same // blind spot that shipped #333 applies to the fail-closed branch below. - var ErrorG = require('primordials').Error; + var ErrorG = require('internal/primordials').Error; if (intrinsic == null) { throw new ErrorG('node:process intrinsic missing at context init'); } diff --git a/pkg/runtime/js/internal/punycode.js b/pkg/runtime/js/node/punycode.js similarity index 99% rename from pkg/runtime/js/internal/punycode.js rename to pkg/runtime/js/node/punycode.js index ba7eb3c..ec8a9b5 100644 --- a/pkg/runtime/js/internal/punycode.js +++ b/pkg/runtime/js/node/punycode.js @@ -7,7 +7,7 @@ // Captured for foldSeparators below, which replaced a global-flag replace. This file is // not otherwise hardened — see the ratchet baseline. - var P = require('primordials'); + var P = require('internal/primordials'); var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; diff --git a/pkg/runtime/js/internal/querystring.js b/pkg/runtime/js/node/querystring.js similarity index 98% rename from pkg/runtime/js/internal/querystring.js rename to pkg/runtime/js/node/querystring.js index 113205c..ce54e48 100644 --- a/pkg/runtime/js/internal/querystring.js +++ b/pkg/runtime/js/node/querystring.js @@ -11,7 +11,7 @@ (function (require, module, exports) { 'use strict'; - var replaceCharAll = require('primordials').replaceCharAll; + var replaceCharAll = require('internal/primordials').replaceCharAll; var hasOwn = Object.prototype.hasOwnProperty; diff --git a/pkg/runtime/js/internal/sqlite.js b/pkg/runtime/js/node/sqlite.js similarity index 99% rename from pkg/runtime/js/internal/sqlite.js rename to pkg/runtime/js/node/sqlite.js index a55d0b8..131d962 100644 --- a/pkg/runtime/js/internal/sqlite.js +++ b/pkg/runtime/js/node/sqlite.js @@ -12,7 +12,7 @@ // Handles for the code added with the node 24 buffer binding. The rest of the file // still reads its globals live — see its pollution baseline. - var P = require('primordials'); + var P = require('internal/primordials'); var ArrayBufferGetByteLength = P.ArrayBufferPrototypeGetByteLength; var SharedArrayBufferGetByteLength = P.SharedArrayBufferPrototypeGetByteLength; var ObjectGetPrototypeOf = P.ObjectGetPrototypeOf; diff --git a/pkg/runtime/js/internal/stream.js b/pkg/runtime/js/node/stream.js similarity index 99% rename from pkg/runtime/js/internal/stream.js rename to pkg/runtime/js/node/stream.js index ff6f04d..e403421 100644 --- a/pkg/runtime/js/internal/stream.js +++ b/pkg/runtime/js/node/stream.js @@ -24,7 +24,7 @@ // runs after user code and is steerable (#333). `ArrayBuffer.isView` in particular is a // security decision — read live, `globalThis.ArrayBuffer = {isView: () => true}` walks a // plain object straight into the byte path. - var StreamPrimordials = require('primordials'); + var StreamPrimordials = require('internal/primordials'); var ArrayBufferIsView = StreamPrimordials.ArrayBufferIsView; var ErrorG = StreamPrimordials.Error; var TypeErrorG = StreamPrimordials.TypeError; diff --git a/pkg/runtime/js/internal/stream_promises.js b/pkg/runtime/js/node/stream/promises.js similarity index 100% rename from pkg/runtime/js/internal/stream_promises.js rename to pkg/runtime/js/node/stream/promises.js diff --git a/pkg/runtime/js/internal/streams.js b/pkg/runtime/js/node/stream/web.js similarity index 100% rename from pkg/runtime/js/internal/streams.js rename to pkg/runtime/js/node/stream/web.js diff --git a/pkg/runtime/js/internal/string_decoder.js b/pkg/runtime/js/node/string_decoder.js similarity index 100% rename from pkg/runtime/js/internal/string_decoder.js rename to pkg/runtime/js/node/string_decoder.js diff --git a/pkg/runtime/js/internal/timers.js b/pkg/runtime/js/node/timers.js similarity index 100% rename from pkg/runtime/js/internal/timers.js rename to pkg/runtime/js/node/timers.js diff --git a/pkg/runtime/js/internal/timers_promises.js b/pkg/runtime/js/node/timers/promises.js similarity index 100% rename from pkg/runtime/js/internal/timers_promises.js rename to pkg/runtime/js/node/timers/promises.js diff --git a/pkg/runtime/js/internal/tty.js b/pkg/runtime/js/node/tty.js similarity index 100% rename from pkg/runtime/js/internal/tty.js rename to pkg/runtime/js/node/tty.js diff --git a/pkg/runtime/js/internal/url.js b/pkg/runtime/js/node/url.js similarity index 99% rename from pkg/runtime/js/internal/url.js rename to pkg/runtime/js/node/url.js index 62afc88..3149a7c 100644 --- a/pkg/runtime/js/internal/url.js +++ b/pkg/runtime/js/node/url.js @@ -26,7 +26,7 @@ // are pristine and cannot be re-pointed later (a poisoned Math.floor, String, // decodeURIComponent, or String.prototype.normalize otherwise silently // substitutes hosts / disables IPv4 normalization; see the URL vector audit). - var P = require('primordials'); + var P = require('internal/primordials'); // Every regex below runs over a caller-supplied URL, so none may go through // `re.test(...)`: `RegExp.prototype.exec` is a writable data property and the // spec's RegExpExec re-reads it off the receiver, so a plain assignment steers diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/node/util.js similarity index 99% rename from pkg/runtime/js/internal/util.js rename to pkg/runtime/js/node/util.js index 0ee4f79..3e44db4 100644 --- a/pkg/runtime/js/internal/util.js +++ b/pkg/runtime/js/node/util.js @@ -7,7 +7,7 @@ // Captured for debugEnvPattern below, which replaced three global-flag replaces on the // NODE_DEBUG path. This file is not otherwise hardened — see the ratchet baseline. - var UtilPrimordials = require('primordials'); + var UtilPrimordials = require('internal/primordials'); var StringPrototypeCharCodeAt = UtilPrimordials.StringPrototypeCharCodeAt; var StringPrototypeSlice = UtilPrimordials.StringPrototypeSlice; var StringPrototypeToUpperCase = UtilPrimordials.StringPrototypeToUpperCase; @@ -904,10 +904,10 @@ debuglog: debuglog, debug: debuglog, // Node aliases util.debug to util.debuglog aborted: aborted, - parseArgs: require('parse_args').parseArgs, - parseEnv: require('parse_env').parseEnv, - MIMEType: require('mime').MIMEType, - MIMEParams: require('mime').MIMEParams, + parseArgs: require('internal/parse_args').parseArgs, + parseEnv: require('internal/parse_env').parseEnv, + MIMEType: require('internal/mime').MIMEType, + MIMEParams: require('internal/mime').MIMEParams, styleText: styleText, stripVTControlCharacters: stripVTControlCharacters, toUSVString: toUSVString, diff --git a/pkg/runtime/js/internal/util_types.js b/pkg/runtime/js/node/util/types.js similarity index 100% rename from pkg/runtime/js/internal/util_types.js rename to pkg/runtime/js/node/util/types.js diff --git a/pkg/runtime/module_resolution.odin b/pkg/runtime/module_resolution.odin index cf54cf3..3383738 100644 --- a/pkg/runtime/module_resolution.odin +++ b/pkg/runtime/module_resolution.odin @@ -165,7 +165,7 @@ global_dirname :: proc(ctx: jsc.JSContextRef) -> (string, bool) { } // is_absolute_path classifies module specifiers during resolution. (node:path's -// own isAbsolute lives in js/internal/path.js.) +// own isAbsolute lives in js/node/path.js.) is_absolute_path :: proc(path: string) -> bool { when ODIN_OS == .Windows { if len(path) == 0 do return false diff --git a/pkg/runtime/net.odin b/pkg/runtime/net.odin index 915caa9..b2e48ca 100644 --- a/pkg/runtime/net.odin +++ b/pkg/runtime/net.odin @@ -15,7 +15,7 @@ import eventloop "lava:pkg/runtime/eventloop" // is the inbound mirror of fetch's outbound socket transport (fetch_transport.odin / // fetch_linux.odin): a listening fd is watched for readability (== a pending accept), // each accepted connection becomes a watched fd advanced by the same IO_Watcher the -// fetch client uses. js/internal/net.js builds the public net.Server / net.Socket +// fetch client uses. js/node/net.js builds the public net.Server / net.Socket // (EventEmitter) surface on top of the flat primitives exposed by make_net_bindings. // // Lifetime: the listener and every connection are watched fds, so watch_fd's @@ -113,7 +113,7 @@ Net_Server :: struct { } // Net_Connection owns one accepted socket. The per-connection handlers are registered -// (and GC-protected) by net_start_cb once js/internal/net.js has built its Socket, then +// (and GC-protected) by net_start_cb once js/node/net.js has built its Socket, then // the read watcher is armed. They are payload-only (the JS handler is already bound to // its own Socket), so on_data(buf), on_end(), on_close(hadError), on_error(message). Net_Connection :: struct { @@ -180,7 +180,7 @@ Net_Connection :: struct { starved_prev: ^Net_Connection, } -// make_net_bindings builds the `native` object passed to js/internal/net.js. +// make_net_bindings builds the `native` object passed to js/node/net.js. make_net_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { bindings := jsc.JSObjectMake(ctx, nil, nil) inject_native_function(ctx, bindings, "listen", net_listen_cb) diff --git a/pkg/runtime/net_other.odin b/pkg/runtime/net_other.odin index 9d58b31..7ae0cf2 100644 --- a/pkg/runtime/net_other.odin +++ b/pkg/runtime/net_other.odin @@ -4,7 +4,7 @@ package lava_runtime import jsc "lava:pkg/jsc" // node:net is Linux-first (net.odin). On other platforms the native bindings are empty, -// so js/internal/net.js surfaces a clear "unavailable" error when used, and the shared +// so js/node/net.js surfaces a clear "unavailable" error when used, and the shared // teardown hooks are no-ops. Restore alongside the Windows/macOS reactor work. // // Net_Server / Net_Connection are defined here as empty stubs because Runtime_State @@ -17,7 +17,7 @@ make_net_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { return jsc.JSObjectMake(ctx, nil, nil) } -// node:https is Linux-first too (tls_server.odin). Empty bindings so js/internal/https.js surfaces +// node:https is Linux-first too (tls_server.odin). Empty bindings so js/node/https.js surfaces // a clear "unavailable" error instead of referencing missing native procs. make_https_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { return jsc.JSObjectMake(ctx, nil, nil) diff --git a/pkg/runtime/os.odin b/pkg/runtime/os.odin index 47fd372..c209a28 100644 --- a/pkg/runtime/os.odin +++ b/pkg/runtime/os.odin @@ -6,7 +6,7 @@ import "core:fmt" import "core:os" import jsc "lava:pkg/jsc" -// node:os native bridge. js/internal/os.js implements the public surface and +// node:os native bridge. js/node/os.js implements the public surface and // computes everything that is a pure function of process.platform/process.env // (homedir/tmpdir/EOL/devNull/endianness/constants); the live system numbers come // from here. The platform-specific syscalls live in os_posix.odin (Linux+Darwin @@ -27,7 +27,7 @@ Os_Cpu :: struct { user, nice, sys, idle, irq: i64, } -// Os_Net_Iface is one flat interface address record; js/internal/os.js groups +// Os_Net_Iface is one flat interface address record; js/node/os.js groups // these by name and decorates each with the string family and the CIDR. family is // 4 or 6; scopeid is only meaningful for IPv6 (0 otherwise). mac is "xx:..:xx". Os_Net_Iface :: struct { @@ -69,7 +69,7 @@ os_getenv_or :: proc(key: string, fallback: string) -> string { return v } -// make_os_bindings builds the `native` object handed to js/internal/os.js. +// make_os_bindings builds the `native` object handed to js/node/os.js. make_os_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { bindings := jsc.JSObjectMake(ctx, nil, nil) inject_native_function(ctx, bindings, "hostname", os_hostname_cb) @@ -324,7 +324,7 @@ os_userinfo_cb :: proc "c" ( // --- priority --------------------------------------------------------------- // -// js/internal/os.js validates pid/priority (int32; priority in [-20, 19]) and +// js/node/os.js validates pid/priority (int32; priority in [-20, 19]) and // throws the Node-shaped TypeError/RangeError before calling these, so the native // side only performs the syscall. A failed syscall (unknown pid -> ESRCH, or // EPERM) becomes an ERR_SYSTEM_ERROR, matching Node's _getPriority/_setPriority. diff --git a/pkg/runtime/os_posix.odin b/pkg/runtime/os_posix.odin index 11792e2..b3dae09 100644 --- a/pkg/runtime/os_posix.odin +++ b/pkg/runtime/os_posix.odin @@ -197,7 +197,7 @@ os_set_priority :: proc(pid: int, value: int) -> (err: int) { // address. A first pass caches each interface's hardware MAC from its link-layer // entry (AF_PACKET/AF_LINK); a second pass builds the address records and attaches // the cached MAC (all-zero when the interface exposes no link-layer address, as in -// Node). js/internal/os.js groups them by name and adds the family string + CIDR. +// Node). js/node/os.js groups them by name and adds the family string + CIDR. os_network_interfaces :: proc() -> []Os_Net_Iface { head: ^ifaddrs if getifaddrs(&head) != 0 || head == nil { diff --git a/pkg/runtime/os_windows.odin b/pkg/runtime/os_windows.odin index 5e20bb4..7ba290b 100644 --- a/pkg/runtime/os_windows.odin +++ b/pkg/runtime/os_windows.odin @@ -227,7 +227,7 @@ os_user_info :: proc() -> Os_User_Info { // os_network_interfaces walks GetAdaptersAddresses and emits one flat record per // unicast IPv4/IPv6 address, with the adapter's hardware MAC (all-zero for an // adapter that exposes none, e.g. loopback) and the netmask derived from the -// on-link prefix length. js/internal/os.js groups them by name and adds the family +// on-link prefix length. js/node/os.js groups them by name and adds the family // string + CIDR. os_network_interfaces :: proc() -> []Os_Net_Iface { flags := windows.GAA_Flags{.Skip_Anycast, .Skip_Multicast, .Skip_DNS_Server} @@ -291,7 +291,7 @@ os_network_interfaces :: proc() -> []Os_Net_Iface { // os_get_priority maps the process's priority class to the libuv nice band and // returns 0 on success. An unknown pid fails OpenProcess -> nonzero errno, which -// js/internal/os.js surfaces as ERR_SYSTEM_ERROR (matching Node). pid 0 means the +// js/node/os.js surfaces as ERR_SYSTEM_ERROR (matching Node). pid 0 means the // current process: GetCurrentProcess returns a pseudo-handle needing no close. os_get_priority :: proc(pid: int) -> (value: int, err: int) { current := pid == 0 @@ -510,7 +510,7 @@ format_v6 :: proc(addr: ^windows.in6_addr) -> string { } // v4_mask renders the IPv4 netmask for an on-link prefix length (e.g. 24 -> -// "255.255.255.0"); js/internal/os.js counts its set bits back into the CIDR. +// "255.255.255.0"); js/node/os.js counts its set bits back into the CIDR. @(private = "file") v4_mask :: proc(prefix: u8) -> string { b: [4]u8 diff --git a/pkg/runtime/require.odin b/pkg/runtime/require.odin index 531452b..1d6cc30 100644 --- a/pkg/runtime/require.odin +++ b/pkg/runtime/require.odin @@ -86,7 +86,7 @@ native_require_cb :: proc "c" ( // node:path is served by the JS internal-module loader above (require_builtin). // node:fs is served by the JS internal-module loader above (require_builtin), like - // node:path — js/internal/fs.js wraps the Odin primitives from make_fs_bindings. It + // node:path — js/node/fs.js wraps the Odin primitives from make_fs_bindings. It // used to be assembled here instead, the one built-in with no JS layer, and that is // what let fs.readFileSync return a bare Uint8Array where node returns a Buffer for a // year (issue #329). Nothing may rebuild it here: require_builtin runs FIRST, so a diff --git a/pkg/runtime/sqlite.odin b/pkg/runtime/sqlite.odin index 29021d1..0b2c7f4 100644 --- a/pkg/runtime/sqlite.odin +++ b/pkg/runtime/sqlite.odin @@ -7,14 +7,14 @@ import "core:strconv" import jsc "lava:pkg/jsc" import sqlite "lava:pkg/std/sqlite" -// node:sqlite bridge. js/internal/sqlite.js implements the DatabaseSync / +// node:sqlite bridge. js/node/sqlite.js implements the DatabaseSync / // StatementSync classes on top of these native primitives; the heavy lifting // (the libsqlite3 calls) lives in pkg/std/sqlite. JS holds opaque integer handle // ids; the real ^sqlite.Database / ^sqlite.Statement live in the per-context // registries on Runtime_State, so they are closed/freed when the context is torn // down even if user code forgets to call db.close(). -// make_sqlite_bindings builds the `native` object handed to js/internal/sqlite.js. +// make_sqlite_bindings builds the `native` object handed to js/node/sqlite.js. make_sqlite_bindings :: proc(ctx: jsc.JSContextRef) -> jsc.JSObjectRef { bindings := jsc.JSObjectMake(ctx, nil, nil) inject_native_function(ctx, bindings, "open", sqlite_open_cb) @@ -293,7 +293,7 @@ sqlite_bind_cb :: proc "c" ( index := int(jsc.JSValueToNumber(ctx, args[1], nil)) value := args[2] - // js/internal/sqlite.js validates the value type before calling here, so the + // js/node/sqlite.js validates the value type before calling here, so the // bridge only ever sees the bindable types (null/number/string/TypedArray) and // routes BigInt through sqlite_bind_bigint_cb. The default branch stays as a // defensive backstop: undefined, booleans, plain objects, etc. throw rather diff --git a/pkg/runtime/tls_server.odin b/pkg/runtime/tls_server.odin index 9b6d6d1..a3b19d8 100644 --- a/pkg/runtime/tls_server.odin +++ b/pkg/runtime/tls_server.odin @@ -352,7 +352,7 @@ tls_server_begin_close :: proc(conn: ^Net_Connection) { tls_pump_wbio(conn) } -// --- native bindings (the `native` object js/internal/https.js receives) ----- +// --- native bindings (the `native` object js/node/https.js receives) ----- // make_https_bindings builds the bindings for node:https. createSecureContext is the synchronous // throw point Node callers expect (bad PEM / key-cert mismatch / encrypted key all throw from diff --git a/reference/node-compatibility.md b/reference/node-compatibility.md index 0cb61ac..91d7686 100644 --- a/reference/node-compatibility.md +++ b/reference/node-compatibility.md @@ -117,13 +117,13 @@ Probed directly against `bin/lava`. -`fetch` streams bodies in **both** directions on the public Web Streams type. `response.body` is a real `ReadableStream` (incrementally fed by the transport — `getReader().read()`, `for await…of`, `cancel()`, `tee()`, `locked`, `pipeTo`, `pipeThrough`), and the buffered accessors `text()`/`json()`/`arrayBuffer()`/`bytes()` drain that same stream (single-consumption enforced). On the request side, a `ReadableStream` or async-iterable body is **streamed incrementally as `Transfer-Encoding: chunked`** — pulled one chunk at a time and written to the socket with write backpressure, never materialized in full (`duplex: 'half'` is required for stream bodies, matching Node). A `Blob` (a known-length body) and string/`Buffer`/typed-array bodies keep the buffered `Content-Length` fast path. fetch and `node:stream/web` share one stream implementation ([js/internal/streams.js](../pkg/runtime/js/internal/streams.js)), so there is no fetch-only fork. Intentional limitations vs Node: streaming is **half-duplex** (the whole request body is sent before the response is read); an immediately-empty stream body is sent as an empty chunked body (terminator only) rather than `Content-Length: 0` (the received body is identical); and a server that rejects chunked request bodies — or closes the connection mid-upload — surfaces as a failed request. +`fetch` streams bodies in **both** directions on the public Web Streams type. `response.body` is a real `ReadableStream` (incrementally fed by the transport — `getReader().read()`, `for await…of`, `cancel()`, `tee()`, `locked`, `pipeTo`, `pipeThrough`), and the buffered accessors `text()`/`json()`/`arrayBuffer()`/`bytes()` drain that same stream (single-consumption enforced). On the request side, a `ReadableStream` or async-iterable body is **streamed incrementally as `Transfer-Encoding: chunked`** — pulled one chunk at a time and written to the socket with write backpressure, never materialized in full (`duplex: 'half'` is required for stream bodies, matching Node). A `Blob` (a known-length body) and string/`Buffer`/typed-array bodies keep the buffered `Content-Length` fast path. fetch and `node:stream/web` share one stream implementation ([js/node/stream/web.js](../pkg/runtime/js/node/stream/web.js)), so there is no fetch-only fork. Intentional limitations vs Node: streaming is **half-duplex** (the whole request body is sent before the response is read); an immediately-empty stream body is sent as an empty chunked body (terminator only) rather than `Content-Length: 0` (the received body is identical); and a server that rejects chunked request bodies — or closes the connection mid-upload — surfaces as a failed request. `TextDecoder` ([js/internal/encoding.js](../pkg/runtime/js/internal/encoding.js)) is a WHATWG streaming decoder for `utf-8`, `utf-16le`, and `windows-1252` (the alias for `latin1`/`ascii`). `decode(chunk, { stream: true })` carries an incomplete trailing sequence — a partial UTF-8 code point, a UTF-16 surrogate pair split across chunks, or an odd UTF-16LE byte — into the next call, and a non-streaming `decode()` flushes and resets; a BOM split across chunks is still stripped exactly once. `fatal: true` throws `ERR_ENCODING_INVALID_ENCODED_DATA` on malformed input (lone surrogate, an incomplete sequence at the final flush) where the default replaces with U+FFFD; lone UTF-16 surrogates become U+FFFD (vs `Buffer.toString('utf16le')`, which passes raw code units through, as in Node). UTF-8 uses the same maximal-subpart replacement as `Buffer` (issue #107). Encoding labels follow WHATWG "get an encoding": leading/trailing **ASCII** whitespace is stripped (not other Unicode whitespace — `'\u00a0utf-8'` is rejected, as in Node), the label is lower-cased, and an unsupported one throws `RangeError` / `ERR_ENCODING_NOT_SUPPORTED` with Node's `The "X" encoding is not supported` message. Lava's label table covers only the three codecs above, so the ~24 further encodings Node resolves through ICU (`gbk`, `shift_jis`, `koi8-r`, `iso-8859-2`, the other `windows-125x` code pages, …) throw where Node succeeds. `TextDecoder`'s `options` argument is read as-is; Node additionally `validateObject()`s it, so a scalar `options` is silently ignored here where Node throws `ERR_INVALID_ARG_TYPE`. `decode`/`encodeInto` argument-type errors do match Node exactly (`ERR_INVALID_ARG_TYPE` with Node's message, including the `Received …` tail). ## Web Streams surface -WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams.js)) are exposed both as globals and through `require('node:stream/web')` (the global and the module export the same class objects, as in Node). `fetch` builds `response.body` and consumes user-provided request bodies through this same `ReadableStream`, so there is a single stream implementation rather than a fetch-only fork. Verified by `tests/node-compat/cases/25-web-streams.js` (`make test-compat-lava`) and the streaming/`pipeTo`/`pipeThrough` cases in `make test-fetch-smoke`. +WHATWG Web Streams ([js/node/stream/web.js](../pkg/runtime/js/node/stream/web.js)) are exposed both as globals and through `require('node:stream/web')` (the global and the module export the same class objects, as in Node). `fetch` builds `response.body` and consumes user-provided request bodies through this same `ReadableStream`, so there is a single stream implementation rather than a fetch-only fork. Verified by `tests/node-compat/cases/25-web-streams.js` (`make test-compat-lava`) and the streaming/`pipeTo`/`pipeThrough` cases in `make test-fetch-smoke`. **Supported (Node-parity):** @@ -149,7 +149,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. ## `Buffer` surface -`Buffer` is a clean-room `Uint8Array` subclass ([js/internal/buffer.js](../pkg/runtime/js/internal/buffer.js)) over Odin codec primitives for the hot paths ([buffer.odin](../pkg/runtime/buffer.odin)). Its own-property API surface matches Node exactly (verified by `make api-surface`). Ported Bun buffer cases live under [tests/node-compat/bun-buffer/ported](../tests/node-compat/bun-buffer/ported) and run via `scripts/report-bun-buffer-tests.sh` (or `make bun-buffer-tests`) and `make test-compat-lava`. +`Buffer` is a clean-room `Uint8Array` subclass ([js/node/buffer.js](../pkg/runtime/js/node/buffer.js)) over Odin codec primitives for the hot paths ([buffer.odin](../pkg/runtime/buffer.odin)). Its own-property API surface matches Node exactly (verified by `make api-surface`). Ported Bun buffer cases live under [tests/node-compat/bun-buffer/ported](../tests/node-compat/bun-buffer/ported) and run via `scripts/report-bun-buffer-tests.sh` (or `make bun-buffer-tests`) and `make test-compat-lava`. **Supported (Node-parity):** @@ -166,7 +166,7 @@ WHATWG Web Streams ([js/internal/streams.js](../pkg/runtime/js/internal/streams. **Intentional differences / deferred:** - `Buffer.poolSize` allocation pooling is modeled like Node: a per-runtime pool `ArrayBuffer` of `Buffer.poolSize` (262144) bytes serves small _unsafe_ allocations — `Buffer.allocUnsafe(size)`, the `Buffer.from(string|array|typed-array)` copy paths, and `Buffer.concat` — when `size < (Buffer.poolSize >>> 1)` (Node's threshold is strictly less-than, so exactly `poolSize/2` is _not_ pooled), advancing an 8-byte-aligned offset and re-pooling on exhaustion. Such buffers share one backing store at different `byteOffset`s, and `buf.buffer.byteLength` is the pool size rather than the buffer length. `Buffer.alloc` (zero-filled), `Buffer.allocUnsafeSlow`, and large allocations keep their own right-sized backing store, as in Node. `Buffer.concat` zero-fills any tail an explicit `totalLength` leaves uncovered, so a pooled result never exposes stale pool bytes. The pool is plain per-runtime module state, so it never leaks across isolated runtimes. Pool _offsets_ are not byte-for-byte identical to Node's because Node pre-warms its pool with internal startup allocations; only the relative pooling semantics match, and well-behaved packages must not depend on pool identity anyway. -- `Buffer.poolSize` is a plain data property that stores and returns the raw assigned value (no coercion on read), and the pool's backing store is sized straight from it — matching Node's `new Uint8Array(Buffer.poolSize)`. A fractional `poolSize` is truncated (`8192.7` → `8192`), and a negative or non-finite `poolSize` throws a catchable `RangeError` on the next (re-)pool, rather than the previous `>>> 0`, which wrapped a negative `poolSize` into a multi-gigabyte request and a `>= 2^32` one into a mod-2^32 size (issue #213). A failed re-pool leaves the existing pool intact (the new backing store is committed only on success), so the throw is recoverable. One benign divergence remains: for a `poolSize` large enough to exceed JavaScriptCore's typed-array allocation cap (a few GiB), the (re-)pool throws `RangeError` where Node (whose limit is ~`2^53`) would instead build a multi-gigabyte pool. That is reachable only by deliberately setting `poolSize` to gigabytes _and_ forcing a re-pool, and throwing is the safer behavior — Lava never attempts the giant allocation. Setting `Buffer.poolSize` to any such value is itself unrealistic (the default is 262144 and the threshold still uses Node's `>>> 1` verbatim), so this only hardens the defensive path. The default is **262144**, a deliberate deviation: node 26.13.0 reports 65536, and lava uses a larger slab because a pool refill costs more on JavaScriptCore than on V8 (the reason is recorded at the constant in [buffer.js](../pkg/runtime/js/internal/buffer.js); no measurement is cited there). The value is documented as "may be modified" and well-behaved packages must not depend on it, so a pool-size-dependent observable like `Buffer.from(small).buffer.byteLength` differs from the node test oracle — the ported pool tests (`buffer-pool.js`, `buffer-allocunsafe-large.js`, `buffer-poolsize-pathological.js`) are written pool-size-relative so they pass under any pool size. (`kMaxLength` stays the JSC-family 4 GiB.) +- `Buffer.poolSize` is a plain data property that stores and returns the raw assigned value (no coercion on read), and the pool's backing store is sized straight from it — matching Node's `new Uint8Array(Buffer.poolSize)`. A fractional `poolSize` is truncated (`8192.7` → `8192`), and a negative or non-finite `poolSize` throws a catchable `RangeError` on the next (re-)pool, rather than the previous `>>> 0`, which wrapped a negative `poolSize` into a multi-gigabyte request and a `>= 2^32` one into a mod-2^32 size (issue #213). A failed re-pool leaves the existing pool intact (the new backing store is committed only on success), so the throw is recoverable. One benign divergence remains: for a `poolSize` large enough to exceed JavaScriptCore's typed-array allocation cap (a few GiB), the (re-)pool throws `RangeError` where Node (whose limit is ~`2^53`) would instead build a multi-gigabyte pool. That is reachable only by deliberately setting `poolSize` to gigabytes _and_ forcing a re-pool, and throwing is the safer behavior — Lava never attempts the giant allocation. Setting `Buffer.poolSize` to any such value is itself unrealistic (the default is 262144 and the threshold still uses Node's `>>> 1` verbatim), so this only hardens the defensive path. The default is **262144**, a deliberate deviation: node 26.13.0 reports 65536, and lava uses a larger slab because a pool refill costs more on JavaScriptCore than on V8 (the reason is recorded at the constant in [buffer.js](../pkg/runtime/js/node/buffer.js); no measurement is cited there). The value is documented as "may be modified" and well-behaved packages must not depend on it, so a pool-size-dependent observable like `Buffer.from(small).buffer.byteLength` differs from the node test oracle — the ported pool tests (`buffer-pool.js`, `buffer-allocunsafe-large.js`, `buffer-poolsize-pathological.js`) are written pool-size-relative so they pass under any pool size. (`kMaxLength` stays the JSC-family 4 GiB.) - `Buffer.allocUnsafe` (above the pool threshold) and `Buffer.allocUnsafeSlow` return genuinely **uninitialized** memory like Node: a native allocation (`pkg/runtime/typed_array.odin`) hands JavaScriptCore a non-zero-filled backing store as a NoCopy `Uint8Array`, which the JS layer wraps as a Buffer view. A JS `new ArrayBuffer` is always zero-initialized, so this cannot be done in pure JS; that was the prior divergence (large unsafe allocations came back zero-filled). Such buffers now hold arbitrary prior memory contents — reading a byte before writing it is undefined behavior, as in Node. `Buffer.alloc` remains zero-filled, and the `Buffer.from`/`Buffer.concat` copy paths always overwrite (or zero-fill) every byte, so they never expose stale memory (issue #212). As a safety bound the native uninitialized path serves single allocations up to `2^31 - 1` bytes; a larger unsafe request falls back to a zero-filled own-backing store (JavaScriptCore's NoCopy creator aborts the process past an internal multi-gigabyte limit, whereas the JS allocation throws a catchable `RangeError`). The cap is far above any realistic `allocUnsafe` and keeps behavior identical across Linux, macOS, and Windows. - **Oversized-allocation ceiling (`kMaxLength`).** Node/V8 advertise `kMaxLength` as `Number.MAX_SAFE_INTEGER` on 64-bit; Lava reports the practical JSC array-buffer limit (**4 GiB on 64-bit, 2 GiB − 1 on 32-bit**), matching **Bun** — the other JSC-based runtime, which likewise reports `kMaxLength === 4294967296` rather than `MAX_SAFE_INTEGER`. It is the JSC-family convention, not a lava-only choice. `Buffer.alloc`/`allocUnsafe`/`allocUnsafeSlow`/`concat` and the `Buffer` constructor enforce this ceiling with Node's coded `ERR_OUT_OF_RANGE` **before** JSC is asked to allocate; negative/`NaN`/non-number sizes are validated and throw first. Raw `new TypedArray(n)` / `new ArrayBuffer(n)` are left to JavaScriptCore, which already throws its own catchable `RangeError` for an over-limit request — exactly as Bun does (Lava does not wrap the global constructors). The Buffer ceiling is overridable with the `LAVA_MAX_BUFFER_BYTES` environment variable (a byte count for operators who want a tighter bound; a non-positive value is ignored). Residual: an allocation handed to JavaScriptCore's **C-API NoCopy creator** from native code with a multi-GiB size — e.g. `fs.readFile` of a file past the limit, where the size comes from the OS rather than a JS constructor — can still abort the process; the JS-layer ceiling covers the JS-constructor and Buffer paths, not native allocations. - Property-access `TypeError` messages for engine-level faults (e.g. reading a property of `undefined`) carry JavaScriptCore wording, not V8's. diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index 06db775..eb947d1 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -113,7 +113,7 @@ const ROUTES = [ targets: ['make test-http-smoke', 'make bench-http'], }, { - re: globToRe('**/js/internal/http.js'), + re: globToRe('**/js/node/http.js'), targets: ['make test-http-smoke', 'make bench-http'], }, { @@ -125,7 +125,7 @@ const ROUTES = [ targets: ['make test-net-smoke', 'make test-zerocopy-smoke'], }, { - re: globToRe('**/js/internal/net.js'), + re: globToRe('**/js/node/net.js'), targets: ['make test-net-smoke', 'make test-zerocopy-smoke'], }, { @@ -133,7 +133,7 @@ const ROUTES = [ targets: ['make test-https-smoke'], }, { - re: globToRe('**/js/internal/https.js'), + re: globToRe('**/js/node/https.js'), targets: ['make test-https-smoke'], }, { @@ -157,11 +157,11 @@ const ROUTES = [ targets: ['make test-fs-lava'], }, { - re: globToRe('**/js/internal/fs.js'), + re: globToRe('**/js/node/fs.js'), targets: ['make test-fs-lava'], }, { - re: globToRe('**/js/internal/stream.js'), + re: globToRe('**/js/node/stream.js'), targets: ['make test-http-smoke', 'make test-net-smoke', 'make test-stdio'], }, { @@ -173,7 +173,7 @@ const ROUTES = [ targets: ['make test-compat-lava-strict'], }, { - re: globToRe('**/js/internal/loader.js'), + re: globToRe('**/js/bootstrap/loader.js'), targets: [ 'make test-compat-lava-strict', 'make test-fs-lava', @@ -184,7 +184,7 @@ const ROUTES = [ ], }, { - re: globToRe('**/js/internal/esm.js'), + re: globToRe('**/js/bootstrap/esm.js'), targets: ['make test-compat-lava-strict'], }, { @@ -192,7 +192,7 @@ const ROUTES = [ targets: ['make test-odin', 'make test-compat-lava', 'make api-surface'], }, { - re: globToRe('**/js/internal/crypto.js'), + re: globToRe('**/js/node/crypto.js'), targets: ['make test-odin', 'make test-compat-lava', 'make api-surface'], }, { @@ -200,11 +200,7 @@ const ROUTES = [ targets: ['make check-js', 'make check-primordials', 'make test-compat-lava'], }, { - re: globToRe('pkg/runtime/js/*.js'), - targets: ['make check-js', 'make check-primordials', 'make test-compat-lava'], - }, - { - re: globToRe('**/js/internal/url.js'), + re: globToRe('**/js/node/url.js'), targets: ['make bench-gate', 'make test-property'], }, { @@ -212,7 +208,7 @@ const ROUTES = [ targets: ['make test-property'], }, { - re: globToRe('**/js/internal/buffer.js'), + re: globToRe('**/js/node/buffer.js'), targets: ['make test-property'], }, { diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index ae278fa..3575a2d 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -8,7 +8,7 @@ test('ALWAYS is included for any path', () => { }); test('loader.js routes strict + smokes (not silent drop of **.js)', () => { - const r = routePaths(['pkg/runtime/js/internal/loader.js']); + const r = routePaths(['pkg/runtime/js/bootstrap/loader.js']); assert.ok(r.targets.includes('make test-compat-lava-strict')); assert.ok(r.targets.includes('make test-fs-lava')); assert.ok(r.targets.includes('make check-js')); @@ -29,6 +29,6 @@ test('bench path routes bench-gate', () => { }); test('invalid **.js style is not required — **/* works', () => { - const r = routePaths(['pkg/runtime/js/console.js']); + const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); }); diff --git a/scripts/build.sh b/scripts/build.sh index c16b2d3..aa6de5d 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -33,6 +33,12 @@ compute_native_stamp() { find "$ROOT_DIR/cmd" "$ROOT_DIR/pkg" \( -name '*.odin' -o -name '*.c' -o -name '*.h' \) -print | LC_ALL=C sort | xargs cksum + # The SET of embedded JS files is native input: globals.odin takes its module + # table from #load_directory, so adding or removing a file changes what is + # compiled in. Names only — a content edit stays JS-only, which the override + # build may serve from disk; a new file it cannot, since the override path only + # reads files the binary already knows. + find "$ROOT_DIR/pkg/runtime/js" -name '*.js' -print | LC_ALL=C sort } | cksum } diff --git a/scripts/check-global-replace.test.mjs b/scripts/check-global-replace.test.mjs index 4355dc2..c1e1c5f 100644 --- a/scripts/check-global-replace.test.mjs +++ b/scripts/check-global-replace.test.mjs @@ -51,11 +51,11 @@ test('every allowlisted site carries a reason and a stable key (not file:line)', test('allowKey prefers binding name over line (#337)', async () => { const { allowKey } = await import('./lib/global-replace-detect.mjs'); const key = allowKey({ - file: 'internal/util.js', + file: 'node/util.js', line: 9999, how: '.replace()', what: 'ansiRegex = /x/g', via: 'binding', }); - assert.equal(key, 'internal/util.js:ansiRegex'); + assert.equal(key, 'node/util.js:ansiRegex'); }); diff --git a/scripts/check-js-graph.mjs b/scripts/check-js-graph.mjs new file mode 100644 index 0000000..656326f --- /dev/null +++ b/scripts/check-js-graph.mjs @@ -0,0 +1,242 @@ +// Module-graph check for the embedded runtime JS layer. +// +// The directory layout under pkg/runtime/js IS the module table (CLAUDE.md §5): +// js/node/.js is a public builtin, js/internal/.js is reachable only +// through the factories' own `require`, js/bootstrap/ holds the files Odin evaluates +// standalone. globals.odin embeds node/ and internal/ a directory at a time with +// `#load_directory`, so nothing checks the graph at compile time: a misspelled +// `require('internal/mim')` builds fine and returns undefined at run time, and a file +// dropped into an unembedded directory silently never ships. This script is that check. +// +// It fails when: +// dynamic a require() in a module file has a non-literal argument — the graph +// below would be incomplete, and a computed id is how an internal module +// gets reached by a name nobody can grep for +// unresolved a require literal, a loader `req()` literal, or a natives key in +// globals.odin names no file under js/node or js/internal +// unreachable a module file nothing reaches from the roots: every node/ module (user +// code may require it), and every id bootstrap/loader.js asks for +// unembedded a .js file under js/ that no `#load_directory` of its directory and no +// `#load` of the file itself embeds — or a `#load_directory` outside +// node/ and internal/, which would put bootstrap files (esm.js is kept +// off the module table on purpose) into the loader's factories +// cycle a cycle through TOP-LEVEL requires, i.e. ones that run at module-eval. +// A require inside a nested function is the sanctioned late binding and +// is not an edge here: it runs after both factories have returned. +// +// "Module-eval" means the body of the factory function every module file evaluates +// to, `(function (require, module, exports, native) { … })`, so a require is +// top-level when its nearest enclosing function is that outermost one. +// +// Usage: node scripts/check-js-graph.mjs (exit 1 on any violation) + +import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'; +import { join, relative, sep } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { parse } from 'acorn'; +import * as walk from 'acorn-walk'; +import { walkDir, ROOT } from './lib/primordials-detect.mjs'; + +const MODULE_DIRS = ['node', 'internal']; + +const posix = (p) => p.split(sep).join('/'); + +// The loader's specifier -> id rule (bootstrap/loader.js toId), restated so a require +// spelled for the public form (`buffer`, `node:buffer`, `assert/strict`) resolves the +// way it does at run time. +function toId(name) { + let key = name.startsWith('node:') ? name.slice(5) : name; + if (key === 'assert/strict') key = 'assert'; + return key.startsWith('internal/') ? key : 'node/' + key; +} + +function parseJs(src, file) { + return parse(src, { + ecmaVersion: 'latest', + sourceType: 'script', + locations: true, + allowReturnOutsideFunction: true, + sourceFile: file, + }); +} + +// Every call `(…)` in `ast`, with whether it runs at module-eval: its nearest +// enclosing function is the outermost one in the program. +function calls(ast, callee) { + const out = []; + walk.ancestor(ast, { + CallExpression(node, _state, ancestors) { + if (node.callee.type !== 'Identifier' || node.callee.name !== callee) return; + const fns = ancestors.filter( + (a) => + a !== node && + (a.type === 'FunctionExpression' || + a.type === 'FunctionDeclaration' || + a.type === 'ArrowFunctionExpression'), + ); + out.push({ node, line: node.loc.start.line, topLevel: fns.length <= 1 }); + }, + }); + return out; +} + +function literalArg(call) { + const arg = call.node.arguments[0]; + if (call.node.arguments.length !== 1 || !arg) return null; + if (arg.type === 'Literal' && typeof arg.value === 'string') return arg.value; + if (arg.type === 'TemplateLiteral' && arg.expressions.length === 0) { + return arg.quasis[0].value.cooked; + } + return null; +} + +/** + * Checks the embedded JS module graph rooted at `root` (a repo checkout, or a fixture + * tree with the same pkg/runtime layout). + * @param {string} root + * @returns {{rule: string, where: string, msg: string}[]} + */ +export function checkJsGraph(root) { + const runtimeDir = join(root, 'pkg', 'runtime'); + const jsDir = join(runtimeDir, 'js'); + const violations = []; + const add = (rule, where, msg) => violations.push({ rule, where, msg }); + const relJs = (file) => posix(relative(runtimeDir, file)); + + // --- embedding: what the Odin sources pull in --------------------------------- + const loadDirs = new Set(); + const loadFiles = new Set(); + const nativeKeys = []; + const odinFiles = readdirSync(runtimeDir).filter((n) => n.endsWith('.odin')); + for (const name of odinFiles) { + const src = readFileSync(join(runtimeDir, name), 'utf8'); + for (const m of src.matchAll(/#load_directory\(\s*"([^"]+)"/g)) { + const dir = m[1].replace(/\/+$/, ''); + loadDirs.add(dir); + const inside = dir.slice('js/'.length); + if (!MODULE_DIRS.some((d) => inside === d || inside.startsWith(d + '/'))) { + add( + 'unembedded', + `pkg/runtime/${name}`, + `#load_directory("${dir}") is outside js/node and js/internal`, + ); + } + } + for (const m of src.matchAll(/#load\(\s*"([^"]+\.js)"/g)) loadFiles.add(m[1]); + for (const m of src.matchAll(/set_named\(ctx, natives, "([^"]+)"/g)) { + nativeKeys.push({ id: m[1], where: `pkg/runtime/${name}` }); + } + } + const allJs = existsSync(jsDir) ? walkDir(jsDir).sort() : []; + for (const file of allJs) { + const rel = relJs(file); + const dir = rel.slice(0, rel.lastIndexOf('/')); + if (!loadDirs.has(dir) && !loadFiles.has(rel)) { + add( + 'unembedded', + `pkg/runtime/${rel}`, + `no #load_directory("${dir}") or #load("${rel}") embeds it`, + ); + } + } + + // --- the module table: id -> file, and every require edge --------------------- + const modules = new Map(); // id -> { file, edges: [{id, line, topLevel}] } + for (const file of allJs) { + const id = relJs(file).slice('js/'.length, -'.js'.length); + if (!MODULE_DIRS.some((d) => id.startsWith(d + '/'))) continue; + modules.set(id, { file, edges: [] }); + } + const where = (mod, line) => `pkg/runtime/${relJs(mod.file)}:${line}`; + for (const mod of modules.values()) { + const ast = parseJs(readFileSync(mod.file, 'utf8'), mod.file); + for (const call of calls(ast, 'require')) { + const spec = literalArg(call); + if (spec === null) { + add('dynamic', where(mod, call.line), 'require() argument is not a string literal'); + continue; + } + const id = toId(spec); + if (!modules.has(id)) { + add('unresolved', where(mod, call.line), `require('${spec}') -> ${id} has no file`); + continue; + } + mod.edges.push({ id, line: call.line, topLevel: call.topLevel }); + } + } + + // --- roots: every public module, plus whatever the loader asks for ------------- + const roots = [...modules.keys()].filter((id) => id.startsWith('node/')); + const loaderFile = join(jsDir, 'bootstrap', 'loader.js'); + if (existsSync(loaderFile)) { + const ast = parseJs(readFileSync(loaderFile, 'utf8'), loaderFile); + for (const call of calls(ast, 'req')) { + const spec = literalArg(call); + if (spec === null) continue; // req(name) inside the resolver itself + const id = toId(spec); + if (!modules.has(id)) { + add( + 'unresolved', + `pkg/runtime/js/bootstrap/loader.js:${call.line}`, + `req('${spec}') -> ${id} has no file`, + ); + } else roots.push(id); + } + } + for (const k of nativeKeys) { + if (!modules.has(k.id)) add('unresolved', k.where, `natives key "${k.id}" names no module`); + } + + const seen = new Set(); + const stack = [...roots]; + while (stack.length > 0) { + const id = stack.pop(); + if (seen.has(id)) continue; + seen.add(id); + for (const e of modules.get(id).edges) stack.push(e.id); + } + for (const [id, mod] of modules) { + if (!seen.has(id)) + add('unreachable', `pkg/runtime/${relJs(mod.file)}`, `${id} is required by nothing`); + } + + // --- cycles through module-eval requires -------------------------------------- + const WHITE = 0; + const GREY = 1; + const BLACK = 2; + const color = new Map([...modules.keys()].map((id) => [id, WHITE])); + const path = []; + const reported = new Set(); + const visit = (id) => { + color.set(id, GREY); + path.push(id); + for (const e of modules.get(id).edges) { + if (!e.topLevel) continue; + if (color.get(e.id) === GREY) { + const cycle = [...path.slice(path.indexOf(e.id)), e.id]; + const key = [...cycle.slice(0, -1)].sort().join(','); + if (!reported.has(key)) { + reported.add(key); + add('cycle', where(modules.get(id), e.line), cycle.join(' -> ')); + } + } else if (color.get(e.id) === WHITE) visit(e.id); + } + path.pop(); + color.set(id, BLACK); + }; + for (const id of modules.keys()) if (color.get(id) === WHITE) visit(id); + + return violations; +} + +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { + const violations = checkJsGraph(ROOT); + if (violations.length > 0) { + console.error(`JS module graph: ${violations.length} violation(s):`); + for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`); + process.exit(1); + } + console.log( + 'OK: embedded JS module graph is literal, resolved, reachable, embedded and acyclic at module-eval.', + ); +} diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs new file mode 100644 index 0000000..58f7920 --- /dev/null +++ b/scripts/check-js-graph.test.mjs @@ -0,0 +1,140 @@ +// node:test view of scripts/check-js-graph.mjs: one fixture tree per rule, each a +// minimal pkg/runtime layout that is clean except for the ONE defect the rule exists +// to catch. The clean fixture runs first, so a rule that fires on everything cannot +// pass by accident, and every failing fixture asserts the exact rule set — a fixture +// that also tripped a second rule would be testing its own scaffolding. +// +// Run: node --test scripts/check-js-graph.test.mjs (or `make test-scripts`) + +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; +import { checkJsGraph } from './check-js-graph.mjs'; +import { ROOT } from './lib/primordials-detect.mjs'; + +const ODIN = `package runtime +LOADER :: #load("js/bootstrap/loader.js", string) +ESM :: #load("js/bootstrap/esm.js", string) +DIRS := [?]Dir{ + {"node", #load_directory("js/node")}, + {"node/stream", #load_directory("js/node/stream")}, + {"internal", #load_directory("js/internal")}, +} +init :: proc() { + set_named(ctx, natives, "node/buffer", b) +} +`; + +const CLEAN = { + 'pkg/runtime/globals.odin': ODIN, + 'pkg/runtime/js/bootstrap/loader.js': + "(function (factories) { function req(name) { return name; } req('internal/primordials'); req('internal/fetch'); return req; });", + 'pkg/runtime/js/bootstrap/esm.js': '(function (primordials) { return function () {}; });', + 'pkg/runtime/js/node/buffer.js': + "(function (require, module) { var p = require('internal/primordials'); module.exports = {}; });", + 'pkg/runtime/js/node/stream.js': + "(function (require, module) { require('buffer'); function late() { return require('node:stream/web'); } module.exports = { late }; });", + 'pkg/runtime/js/node/stream/web.js': + "(function (require, module) { require('stream'); module.exports = {}; });", + 'pkg/runtime/js/internal/primordials.js': + '(function (require, module) { module.exports = {}; });', + 'pkg/runtime/js/internal/fetch.js': + "(function (require, module) { require('stream/web'); module.exports = {}; });", +}; + +function tree(overrides) { + const root = mkdtempSync(join(tmpdir(), 'js-graph-')); + const files = { ...CLEAN, ...overrides }; + for (const [rel, src] of Object.entries(files)) { + if (src === null) continue; + mkdirSync(dirname(join(root, rel)), { recursive: true }); + writeFileSync(join(root, rel), src); + } + return root; +} + +function rules(overrides) { + const root = tree(overrides); + try { + return checkJsGraph(root); + } finally { + rmSync(root, { recursive: true, force: true }); + } +} + +const only = (vs) => [...new Set(vs.map((v) => v.rule))].sort(); + +test('clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)', () => { + assert.deepEqual(rules({}), []); +}); + +test('the real tree is clean', () => { + assert.deepEqual(checkJsGraph(ROOT), []); +}); + +test('dynamic: a non-literal require argument', () => { + const vs = rules({ + 'pkg/runtime/js/node/buffer.js': + "(function (require, module) { var n = 'internal/primordials'; require(n); module.exports = {}; });", + }); + assert.deepEqual(only(vs), ['dynamic']); + assert.match(vs.find((v) => v.rule === 'dynamic').where, /node\/buffer\.js:1$/); +}); + +test('unresolved: a require literal with no file', () => { + const vs = rules({ + 'pkg/runtime/js/node/buffer.js': + "(function (require, module) { require('internal/primordials'); require('internal/mim'); module.exports = {}; });", + }); + assert.deepEqual(only(vs), ['unresolved']); + assert.match(vs[0].msg, /internal\/mim/); +}); + +test('unresolved: a natives key naming no module', () => { + const vs = rules({ 'pkg/runtime/globals.odin': ODIN.replace('"node/buffer"', '"buffer"') }); + assert.deepEqual(only(vs), ['unresolved']); + assert.match(vs[0].msg, /natives key "buffer"/); +}); + +test('unreachable: an internal module nothing requires', () => { + const vs = rules({ + 'pkg/runtime/js/internal/mime.js': '(function (require, module) { module.exports = {}; });', + }); + assert.deepEqual(only(vs), ['unreachable']); + assert.match(vs[0].where, /internal\/mime\.js$/); +}); + +test('unembedded: a directory of .js no #load_directory reads', () => { + const vs = rules({ + 'pkg/runtime/js/node/util.js': + "(function (require, module) { require('util/types'); module.exports = {}; });", + 'pkg/runtime/js/node/util/types.js': '(function (require, module) { module.exports = {}; });', + }); + assert.deepEqual(only(vs), ['unembedded']); + assert.match(vs[0].where, /node\/util\/types\.js$/); +}); + +test('unembedded: #load_directory of bootstrap/ would put esm.js on the module table', () => { + const vs = rules({ + 'pkg/runtime/globals.odin': ODIN.replace( + '\t{"internal", #load_directory("js/internal")},', + '\t{"internal", #load_directory("js/internal")},\n\t{"bootstrap", #load_directory("js/bootstrap")},', + ), + }); + assert.deepEqual(only(vs), ['unembedded']); + assert.match(vs[0].msg, /js\/bootstrap/); +}); + +test('cycle: two module-eval requires that close a loop', () => { + const vs = rules({ + 'pkg/runtime/js/node/stream.js': + "(function (require, module) { require('buffer'); require('stream/web'); module.exports = {}; });", + }); + assert.deepEqual(only(vs), ['cycle']); + assert.match( + vs[0].msg, + /node\/stream -> node\/stream\/web -> node\/stream|node\/stream\/web -> node\/stream -> node\/stream\/web/, + ); +}); diff --git a/scripts/check-orphan-js.mjs b/scripts/check-orphan-js.mjs deleted file mode 100644 index 991f096..0000000 --- a/scripts/check-orphan-js.mjs +++ /dev/null @@ -1,53 +0,0 @@ -// Dead-code check for the embedded runtime JS layer. -// -// Runtime JS modules live in pkg/runtime/js and are *not* linked through a JS -// import graph — each is string-embedded into the Odin binary via -// FOO :: #load("js/internal/foo.js", string) -// and wired up through the JSC native bridge. Tools like knip can't see that -// graph (it lives in Odin, and require() resolves builtin names, not paths), so -// the meaningful "dead file" signal here is simple: a runtime JS file that no -// .odin source `#load`s is orphaned and can be deleted. -// -// Usage: bun run scripts/check-orphan-js.mjs (exit 1 if any orphan found) - -import { readdirSync, readFileSync, statSync } from 'node:fs'; -import { join, relative, sep } from 'node:path'; - -const ROOT = join(import.meta.dirname, '..'); -const JS_DIR = join(ROOT, 'pkg', 'runtime', 'js'); -const RUNTIME_DIR = join(ROOT, 'pkg', 'runtime'); - -function walk(dir, out = []) { - for (const name of readdirSync(dir)) { - const full = join(dir, name); - if (statSync(full).isDirectory()) walk(full, out); - else out.push(full); - } - return out; -} - -// All #load("js/...") references across every .odin file under pkg/runtime. -const loaded = new Set(); -for (const file of walk(RUNTIME_DIR)) { - if (!file.endsWith('.odin')) continue; - const src = readFileSync(file, 'utf8'); - for (const m of src.matchAll(/#load\(\s*"([^"]+\.js)"/g)) loaded.add(m[1]); -} - -const orphans = []; -for (const file of walk(JS_DIR)) { - if (!file.endsWith('.js')) continue; - // Reference key is the path as written in #load, e.g. "js/internal/foo.js". - const key = relative(RUNTIME_DIR, file).split(sep).join('/'); - if (!loaded.has(key)) orphans.push(key); -} - -if (orphans.length > 0) { - console.error( - `Found ${orphans.length} orphaned runtime JS file(s) — not #load-ed by any .odin source:`, - ); - for (const o of orphans) console.error(` pkg/runtime/${o}`); - process.exit(1); -} - -console.log(`OK: all runtime JS files are loaded by the Odin host.`); diff --git a/scripts/check-primordials.mjs b/scripts/check-primordials.mjs index 84f40d9..a075e01 100644 --- a/scripts/check-primordials.mjs +++ b/scripts/check-primordials.mjs @@ -136,7 +136,7 @@ if (stale.length > 0) { if (failed) { console.error( '\nPollution ratchet FAILED: a module gained pollutable sites. The fix depends on\n' + - "the class: method/invoke -> route through primordials (require('primordials'),\n" + + "the class: method/invoke -> route through primordials (require('internal/primordials'),\n" + ' ReflectApply for .call/.apply)\n' + ' accessor -> read via a captured getter, e.g.\n' + ' TypedArrayPrototypeGetBuffer(view)\n' + diff --git a/scripts/lib/global-replace-detect.mjs b/scripts/lib/global-replace-detect.mjs index c9fe4cd..05c93af 100644 --- a/scripts/lib/global-replace-detect.mjs +++ b/scripts/lib/global-replace-detect.mjs @@ -39,7 +39,7 @@ const PRIMORDIAL_RE = /^StringPrototype(Replace|ReplaceAll|Match|MatchAll|Split| // to ignore a hang alarm — three moves in one PR (#330: 572→607→609→660). const ALLOWED = new Map([ [ - 'internal/util.js:ansiRegex', + 'node/util.js:ansiRegex', 'stripVTControlCharacters: the ANSI pattern is two alternations (OSC terminated by ' + 'BEL/ST, CSI terminated by a final byte), not a character class, so it has no ' + 'code-unit rewrite. node hangs on it identically, so Lava is not diverging. ' + diff --git a/scripts/lib/primordials-detect.mjs b/scripts/lib/primordials-detect.mjs index 5e332ab..890a6fb 100644 --- a/scripts/lib/primordials-detect.mjs +++ b/scripts/lib/primordials-detect.mjs @@ -7,7 +7,7 @@ // can mutate shared prototypes (Array.prototype.push = …) or replace globals. // A call like `arr.push(x)` or `s.charCodeAt(i)` resolves the method through the // live (pollutable) prototype at call time, so a poisoned prototype silently -// corrupts a built-in. The fix is `require('primordials')` — captured, pristine +// corrupts a built-in. The fix is `require('internal/primordials')` — captured, pristine // methods invoked as `ArrayPrototypePush(arr, x)` (see primordials.js). // // FOUR classes are counted, each baselined separately (see KINDS): @@ -86,11 +86,11 @@ import { parse } from 'acorn'; import * as walk from 'acorn-walk'; const ROOT = join(import.meta.dirname, '..', '..'); -// The PARENT of internal/: console.js sits directly in pkg/runtime/js, is -// #load-embedded by globals.odin, and runs before user code exactly like the -// internal modules — it was outside the scan while the baseline's "console.js" -// entry described the 7-line internal re-export, so the report read as "console -// is hardened". check-orphan-js.mjs already walks this directory. +// The whole embedded tree — bootstrap/, node/ and internal/ — not one directory: +// the bootstrap preludes (console.js, loader.js, esm.js) run before user code exactly +// like the modules do. The console prelude was once outside the scan while the +// baseline's "console.js" entry described a 7-line re-export, so the report read as +// "console is hardened". Keys are paths relative to this directory. const JS_DIR = join(ROOT, 'pkg', 'runtime', 'js'); const BASELINE = join(ROOT, 'tests', 'node-compat', 'pollution-baseline.json'); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 2fe5010..cac42b0 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -112,7 +112,7 @@ { "name": "the poisoned http-smoke phases die when a framing validator goes back to re.test", "why": "Content-Length framing. With the check on a live regex, a poisoned exec answered 200 OK for `Content-Length: abc` — parseInt gives NaN and the body framing desyncs. The charCode form has no regex to poison at all.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " if (!allChars(contentLengthHeader, isAsciiDigit)) return sendErrorAndClose(400);", "replace": " if (!/^\\d+$/.test(contentLengthHeader)) return sendErrorAndClose(400);", "gate": "make:test-http-smoke", @@ -121,7 +121,7 @@ { "name": "the poisoned http-smoke phases die when the chunked check goes back to re.test", "why": "Transfer-Encoding framing. A steered check accepted `gzip` as chunked, which is the other half of the smuggling pair.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " if (!reTest(TE_CHUNKED_RE, transferEncoding)) return sendErrorAndClose(501);", "replace": " if (!/^\\s*chunked\\s*$/i.test(transferEncoding)) return sendErrorAndClose(501);", "gate": "make:test-http-smoke", @@ -170,7 +170,7 @@ { "name": "54-url-pollution X1 dies when endsInANumber goes back to a live re.test", "why": "Steered true, '4x' reads as numeric and parseInt gives 4, so http://1.2.3.4x/ silently becomes http://1.2.3.4/ — a host substitution.", - "source": "pkg/runtime/js/internal/url.js", + "source": "pkg/runtime/js/node/url.js", "find": " if (allChars(last, isAsciiDigit)) return true;", "replace": " if (last !== '' && /^[0-9]+$/.test(last)) return true;", "gate": "compat:tests/node-compat/cases/54-url-pollution.js", @@ -179,7 +179,7 @@ { "name": "54-url-pollution X2 dies when the %2f check goes back to a live re.test", "why": "The path-traversal guard, in the direction that discriminates: a steered validator makes a legitimate path look encoded and throws where node returns one.", - "source": "pkg/runtime/js/internal/url.js", + "source": "pkg/runtime/js/node/url.js", "find": "if (reTest(PERCENT_2F_RE, rest) || (isWindows && reTest(PERCENT_5C_RE, rest))) {", "replace": "if (/%2f/i.test(rest) || (isWindows && /%5c/i.test(rest))) {", "gate": "compat:tests/node-compat/cases/54-url-pollution.js", @@ -207,7 +207,7 @@ { "name": "57-format-single-arg dies when util.js drops the single-argument guard", "why": "Without it util.format folds %% even with one argument, where node returns the string verbatim. Pinned separately from console.js because the two are different implementations and one entry would fake coverage of both.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " if (args.length === 1 && typeof first === 'string') return first;\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -216,7 +216,7 @@ { "name": "57-format-single-arg dies when console.js drops the single-argument guard", "why": "console.js has its own formatter — it is the bootstrap prelude and has no require to delegate with — so removing util.js's guard leaves console.log correct and vice versa. This entry moves the raw console.log rows, a different line range from the util entry.", - "source": "pkg/runtime/js/console.js", + "source": "pkg/runtime/js/bootstrap/console.js", "find": " if (args.length === 1 && typeof first === 'string') return first;\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -234,7 +234,7 @@ { "name": "the Lava-only test dies when normalizeBase64 goes back to a global replace", "why": "A global replace under a forged exec never terminates (or, on some runners, blows up as RangeError from runaway string growth). Buffer.from(x,'base64') is the Basic-auth/JWT path. Either RED detail proves the reintroduced global replace; hang vs throw is runner-dependent, so expect_detail is an alternatives list.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " var src = StringG(str);\n var n = src.length;\n var out = '';", "replace": " return StringG(str)\n .replaceAll('-', '+')\n .replaceAll('_', '/')\n .replaceAll(/[^A-Za-z0-9+/]/g, '');\n var src = StringG(str);\n var n = src.length;\n var out = '';", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -247,7 +247,7 @@ { "name": "the Lava-only test dies when toUSVString goes back to the global regex", "why": "The surrogate pre-scan is not a guard against this: a VALID pair — any emoji in a remote query — sets hasSurrogate too, and the global replace then spins (or throws RangeError from runaway growth). new URLSearchParams(remoteQuery) is on the network path. Hang vs throw is runner-dependent; expect_detail lists both.", - "source": "pkg/runtime/js/internal/url.js", + "source": "pkg/runtime/js/node/url.js", "find": " if (!hasSurrogate) return str;\n var out = StringPrototypeSlice(str, 0, i);", "replace": " if (!hasSurrogate) return str;\n var SPOL = /([\\uD800-\\uDBFF])([\\uDC00-\\uDFFF])?|([\\uDC00-\\uDFFF])/g;\n return StringPrototypeReplace(str, SPOL, function (m, h, l, lone) {\n return lone !== undefined || l === undefined ? '\\uFFFD' : m;\n });\n var out = StringPrototypeSlice(str, 0, i);", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -260,7 +260,7 @@ { "name": "the poisoned http-smoke phases die when the header-char scan reads charCodeAt live", "why": "assertValidHeaderChar guards the response path. Live, a CR/LF-lying gadget splits the response, and the exec/test phases pass throughout because they never touch that carrier.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var code = StringPrototypeCharCodeAt(value, i);\n if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) {", "replace": " var code = value.charCodeAt(i);\n if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) {", "gate": "make:test-http-smoke", @@ -269,7 +269,7 @@ { "name": "the poisoned http-smoke phases die when isHttpToken reads charCodeAt live", "why": "isHttpToken guards header NAMES. Live, a CR/LF-lying gadget accepts a name that still carries real CR/LF on the wire. The value-path mutation does not cover this site.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var code = StringPrototypeCharCodeAt(value, i);\n if (code >= 128 || !HTTP_TCHAR[code]) return false;", "replace": " var code = value.charCodeAt(i);\n if (code >= 128 || !HTTP_TCHAR[code]) return false;", "gate": "make:test-http-smoke", @@ -278,7 +278,7 @@ { "name": "the poisoned http-smoke phases die when Content-Length conversion reads live parseInt", "why": "A sound allChars digit check next to live parseInt still desyncs framing: contentLengthRemaining becomes the gadget value. Poison-before-require is the realistic order; bootstrap capture on primordials is the fix. The Odin pi= pin covers the eager URL port path; this entry covers the lazy http framing path.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " contentLengthRemaining = parseIntG(contentLengthHeader, 10);", "replace": " contentLengthRemaining = parseInt(contentLengthHeader, 10);", "gate": "make:test-http-smoke", @@ -287,7 +287,7 @@ { "name": "58-esm-pollution targeted dies when the export-default name comes back through .match", "why": "String.prototype.match re-reads R.exec off the receiver, so the declared name is attacker-chosen and the transform interpolates it into the emitted module body as an identifier — code injection, not a wrong answer.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " var fn = RegExpPrototypeExec(RE_DEFAULT_FUNCTION, expr);", "replace": " var fn = expr.match(/^(?:async\\s+)?function\\b\\s*\\*?\\s*([A-Za-z_$][\\w$]*)/);", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -296,7 +296,7 @@ { "name": "58-esm-pollution blunt dies when the keyword boundary test goes back to a regex", "why": "A forged exec makes /[\\w$]/.test answer true for every character, so keywordAt rejects every import and export and the emitted source keeps raw module syntax JSC cannot parse.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " if (after >= 0 && isIdentPart(after)) return null;", "replace": " if (after >= 0 && /[\\w$]/.test(String.fromCharCode(after))) return null;", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -305,7 +305,7 @@ { "name": "58-esm-pollution charcodeat dies when the mask reads code units live", "why": "Routing the scanner off charAt onto charCodeAt is only a fix if the read is captured: a gadget lying about quote characters makes a string body scan as code, so text the module wrote as DATA is spliced out and re-emitted.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " var c = StringPrototypeCharCodeAt(src, i);", "replace": " var c = src.charCodeAt(i);", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -314,7 +314,7 @@ { "name": "58-esm-pollution arrayat dies when the mask stack is read through Array.prototype.at", "why": "The mode stack decides whether the scanner is inside a template literal; a forged at(-1) answering 'code' means it never enters template mode and statement-shaped template text becomes code.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " var top = stack[stack.length - 1];", "replace": " var top = stack.at(-1);", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -323,7 +323,7 @@ { "name": "58-esm-pollution json dies when emitted literals go back through the live JSON.stringify", "why": "Every string literal in the generated CommonJS — require specifiers, export keys, __filename/__dirname/import.meta.url — is produced here, so a replaced stringify writes the emitted source directly.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " return JSONStringify(StringG(value));", "replace": " return JSON.stringify(String(value));", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -332,7 +332,7 @@ { "name": "58-esm-pollution tail dies when the export result object inherits from Object.prototype", "why": "transformExport carries an own `tail` on one of eight return shapes; without a null prototype the other seven read Object.prototype.tail and join it into the emitted source, so a data-only merge gadget is code execution in the module body.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " return { __proto__: null, body: decl + ';' + assigns, tail: null };", "replace": " return { body: decl + ';' + assigns };", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -341,7 +341,7 @@ { "name": "58-esm-pollution join dies when the wrapper is assembled with a live Array.prototype.join", "why": "The joins assemble the emitted module, so a gadget lying only for the '\\n' separator replaces the WHOLE output rather than one interpolated identifier — a strictly larger win than the export-default carrier. Mutating the capture rather than one call site keeps esm.js syntactically valid, so the gate measures the carrier and not a bootstrap parse error.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " var ArrayPrototypeJoin = primordials.ArrayPrototypeJoin;", "replace": " var ArrayPrototypeJoin = function (a, s) {\n return a.join(s);\n };", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -350,7 +350,7 @@ { "name": "58-esm-pollution strglobal dies when the source coercion reads the live String global", "why": "transform() coerces its `source` argument with String(); the existing json entry reverts JSONStringify and StringG on one line, so the String capture on this line was never proven on its own.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " source = StringG(source);", "replace": " source = String(source);", "gate": "compat:tests/node-compat/cases/58-esm-pollution.js", @@ -359,7 +359,7 @@ { "name": "29-esm-transform-edge dies when findStmtEnd drops the seenMeaningful flag", "why": "The old code got this from `'+-*/%=<>&|^,.?:('.indexOf('')` returning 0, so a newline before any meaningful character continued the statement; without the flag an import whose clause starts on the next line ends early and reports an unsupported form.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " if (!seenMeaningful || isContinuation(lastMeaningful)) continue;", "replace": " if (isContinuation(lastMeaningful)) continue;", "gate": "compat:tests/node-compat/cases/29-esm-transform-edge.mjs", @@ -368,7 +368,7 @@ { "name": "29-esm-transform-edge dies when import.meta is rewritten by searching the source, not the mask", "why": "The mask blanks strings and comments, so searching it leaves an `import.meta` written inside a string literal alone; searching the source rewrites it to __import_meta and silently corrupts module data.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " var at = StringPrototypeIndexOf(mask, IMPORT_META);", "replace": " var at = StringPrototypeIndexOf(src, IMPORT_META);", "gate": "compat:tests/node-compat/cases/29-esm-transform-edge.mjs", @@ -377,7 +377,7 @@ { "name": "29-esm-transform-edge dies when isSpace narrows to ASCII", "why": "The `\\s*` strips these helpers replace accept the full ES whitespace set; narrowing to ASCII makes `export constname` emit the key \"const\", which is a silent behavior change the comment explicitly warns against.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " c === 0xa0 ||", "replace": " false ||", "gate": "compat:tests/node-compat/cases/29-esm-transform-edge.mjs", @@ -386,7 +386,7 @@ { "name": "29-esm-transform-edge dies when the identifier class drops `$`", "why": "No other fixture uses `$`, `_` or digits in a binding name, so the `$` arm of the code-unit predicate that replaced [A-Za-z_$] was unexercised and could be dropped without any test noticing.", - "source": "pkg/runtime/js/internal/esm.js", + "source": "pkg/runtime/js/bootstrap/esm.js", "find": " return (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x5f || c === 0x24;", "replace": " return (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x5f;", "gate": "compat:tests/node-compat/cases/29-esm-transform-edge.mjs", @@ -395,7 +395,7 @@ { "name": "59-global-replace-hangs dies when querystring's '+' decode goes back to a global replace", "why": "parse is public node:querystring and a single '+' in a query string reaches it, so a server doing parse(req.url.split('?')[1]) wedges on a crafted request: a global replace under a forged exec only advances lastIndex on an empty match and never returns. node answers correctly under the same poison, so this is a Lava-only defect.", - "source": "pkg/runtime/js/internal/querystring.js", + "source": "pkg/runtime/js/node/querystring.js", "find": " s = replaceCharAll(s, '+', '%20');", "replace": " if (s.indexOf('+') !== -1) s = s.replace(/\\+/g, '%20');", "gate": "compat:tests/node-compat/cases/59-global-replace-hangs.js", @@ -408,7 +408,7 @@ { "name": "regexp_pollution_immunity glob dies when matchesGlob strips backslashes with a global replace", "why": "Both arguments are caller-supplied, and the global replace spins under a forged exec. node hangs here too, so this is a deviation in Lava's favour and cannot be an oracle case.", - "source": "pkg/runtime/js/internal/path.js", + "source": "pkg/runtime/js/node/path.js", "find": " pattern = replaceCharAll(pattern, '\\\\', '/');", "replace": " pattern = pattern.replace(/\\\\/g, '/');", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -421,7 +421,7 @@ { "name": "regexp_pollution_immunity inspect dies when the Buffer hex grouping goes back to a global replace", "why": "console.log(buf) on a Buffer holding remote bytes reaches this; the global replace spins under a forged exec. node hangs here too.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " hex += StringPrototypeSlice(raw, h, h + 2);", "replace": " hex += raw.replaceAll(/(.{2})/g, '$1 ').trim();", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -434,7 +434,7 @@ { "name": "regexp_pollution_immunity blob dies when native line endings go back to a global replace", "why": "new Blob([text], {endings:'native'}) runs this over caller text; the global replace spins under a forged exec. node hangs here too.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " if (nativeEndings) str = toNativeEndings(str);", "replace": " if (nativeEndings) str = str.replace(/\\r?\\n/g, nativeEol());", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -447,7 +447,7 @@ { "name": "regexp_pollution_immunity debuglog dies when NODE_DEBUG is compiled by chained global replaces", "why": "Three global replaces run on the FIRST debuglog call, so the process wedges at the point it tries to emit a diagnostic. Lava-only because Lava has no process.stderr, so the outputs cannot match node however the spin is fixed.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " return StringPrototypeToUpperCase(out);", "replace": " return env.replace(/[|\\\\{}()[\\]^$+?.]/g, '\\\\$&').replace(/\\*/g, '.*').replace(/,/g, '$|^').toUpperCase();", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -473,7 +473,7 @@ { "name": "regexp_pollution_immunity puny dies when the RFC 3490 separator fold goes back to a global replace", "why": "punycode.toASCII on a caller-supplied hostname spun; node hangs identically so it cannot be an oracle case. Pins availability only, because other live .test reads in punycode.js still steer the per-label encode decision.", - "source": "pkg/runtime/js/internal/punycode.js", + "source": "pkg/runtime/js/node/punycode.js", "find": " domain = foldSeparators(domain);", "replace": " domain = domain.replace(/[\\x2E\\u3002\\uFF0E\\uFF61]/g, '\\x2E');", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -486,7 +486,7 @@ { "name": "regexp_pollution_immunity dbg-nodot dies when the NODE_DEBUG escaper stops escaping '.'", "why": "Nothing observed debugEnvPattern's OUTPUT before: the poisoned probe passes with NODE_DEBUG unset, i.e. when the function is never called. Dropping the '.' branch makes 'a.b' enable 'axb', which is the escape the compile exists to do.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " c === 0x2e // .", "replace": " false // .", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -511,7 +511,7 @@ { "name": "61-stdio-lifecycle dies when pipe() stops excluding the stdio singletons", "why": "Without node's `dest !== process.stdout/stderr` exclusion, `src.pipe(process.stdout)` calls end() on a process-lifetime singleton. Note what the assertion keys off: since `_destroy` undoes the teardown, the stream RECOVERS a tick later, so `writable` reads true either way and only the spurious 'finish' event proves end() was reached. Before that half existed this dropped every later write and raised 'write after end'.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " var doEnd =\n (!pipeOpts || pipeOpts.end !== false) &&\n dest !== processRef.stdout &&\n dest !== processRef.stderr;", "replace": " var doEnd = !pipeOpts || pipeOpts.end !== false;", "gate": "compat:tests/node-compat/cases/61-stdio-lifecycle.js", @@ -522,7 +522,7 @@ { "name": "stdio-lifecycle dies when pipe() stops excluding the stdio singletons (pipe shape)", "why": "Same defect as the compat entry, gated on the node:test so both fd shapes are covered — node's stdout is a different class on a pipe. The script counts 'finish' events rather than comparing bytes, because the recovery makes the bytes identical with the exclusion deleted; a byte-only version of this test SURVIVED this mutation.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " var doEnd =\n (!pipeOpts || pipeOpts.end !== false) &&\n dest !== processRef.stdout &&\n dest !== processRef.stderr;", "replace": " var doEnd = !pipeOpts || pipeOpts.end !== false;", "gate": "node-test:tests/stdio/stdio-lifecycle.test.mjs", @@ -548,7 +548,7 @@ { "name": "stdio-lifecycle dies when _undestroy stops clearing the ending flag", "why": "The `writable` getter is `!destroyed && !ending`, so leaving `ending` set makes the stream report unwritable forever even though destroyed was cleared — the recovery looks applied and is not. Pins the individual field, not just the presence of the call.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " w.ended = false;\n w.ending = false;\n w.finished = false;", "replace": " w.ended = false;\n w.finished = false;", "gate": "node-test:tests/stdio/stdio-lifecycle.test.mjs", @@ -606,7 +606,7 @@ { "name": "62-fs-read-buffer dies when the read result is not re-tagged as a Buffer", "why": "The native returns a plain Uint8Array where node returns a Buffer. Without the re-tag, Buffer.isBuffer is false and `.toString('hex')` falls through to Uint8Array.prototype.toString — comma-joined decimals instead of hex, with no throw. That shipped for a year.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " return BufferFrom(\n TypedArrayPrototypeGetBuffer(value),\n TypedArrayPrototypeGetByteOffset(value),\n TypedArrayPrototypeGetByteLength(value),\n );", "replace": " return value;", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -617,7 +617,7 @@ { "name": "62-fs-read-buffer dies when the encoding is dropped instead of decoded in JS", "why": "Reads must be decoded through Buffer's codecs here, because the native ignored the encoding argument entirely and decoded everything as UTF-8 — readFileSync(p,'hex') returned the file's text instead of its hex. expect_detail keys off a UNIQUE assertion message: an earlier version used ['strictEqual','string'], which appears in the red output of every failing assert.equal in the file and would have accepted a break of the Buffer re-tag instead.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " return encoding === null ? buf : BufferPrototypeToString(buf, encoding);", "replace": " return buf;", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -628,7 +628,7 @@ { "name": "62-fs-read-buffer dies when an unknown encoding is not rejected", "why": "node throws ERR_INVALID_ARG_VALUE for an unrecognized encoding. Without the check the name reaches Buffer#toString, which throws a differently-coded error, and a caller catching by code sees the wrong one. expect_detail keys off the error that ESCAPES (Buffer#toString's uncoded 'Unknown encoding: bogus'), not the code the case asserts: assert.throws swallows the mismatch into an AssertionError, so the coded name never reaches the diff.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) {", "replace": " if (false) {", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -639,7 +639,7 @@ { "name": "57-format-single-arg dies when util.inspect stops quoting a top-level string", "why": "node's util.inspect QUOTES a top-level string ('x' -> \"'x'\") while console.log/format print a string ARGUMENT raw — format goes through stringify(), which short-circuits strings before inspect is reached, so the depth-0 raw arm had no caller left and was deleted. Restoring it un-quotes every message built with util.inspect, which is how fs.js's ERR_INVALID_ARG_VALUE read `Received bogus`.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " if (t === 'string') return quote(v);", "replace": " if (t === 'string') return depth === 0 ? v : quote(v);", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -650,7 +650,7 @@ { "name": "57-format-single-arg dies when inspect stops choosing the quote character", "why": "node picks the delimiter to avoid escaping — a string containing a single quote is rendered with DOUBLE quotes (\"it's\", not 'it\\'s'). Always using single quotes escapes where node does not, so any inspected value containing an apostrophe differs.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " if (StringPrototypeIndexOf(s, \"'\") !== -1) {\n if (StringPrototypeIndexOf(s, '\"') === -1) q = '\"';\n else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';\n }\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -661,7 +661,7 @@ { "name": "62-fs-read-buffer dies when the pristine Buffer capture moves back into fs.js", "why": "fs.js is a LAZY module, so a `require('buffer').Buffer` capture at its module-eval reads whatever user code left there — a dependency's top-level `require('buffer').Buffer = shim` then steers and discloses every fs read. loader.js snapshots it right after buffer is instantiated, where the ordering is provable.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " var PristineBuffer = require.pristineBuffer;", "replace": " var PristineBuffer = require('buffer').Buffer;", "gate": "compat:tests/node-compat/cases/63-fs-buffer-capture.js", @@ -673,7 +673,7 @@ { "name": "62-fs-read-buffer dies when falsy encodings stop meaning binary", "why": "node's assertEncoding is guarded `if (encoding && ...)`, so '', false, 0 and NaN all return a Buffer. Rejecting them broke code that worked before the JS layer existed, because the native treated a non-string encoding as binary and returned data.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (!encoding) return null;", "replace": " if (encoding === null || encoding === undefined) return null;", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -684,7 +684,7 @@ { "name": "62-fs-read-buffer dies when 'buffer' is validated like an ordinary encoding", "why": "node's getOptions skips assertEncoding for 'buffer' so it reaches Buffer#toString and throws ERR_UNKNOWN_ENCODING. Validating it yields ERR_INVALID_ARG_VALUE — a different code AND message on a documented fs option value.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) {", "replace": " if (!BufferIsEncoding(encoding)) {", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -695,7 +695,7 @@ { "name": "62-fs-read-buffer dies when ERR_INVALID_ARG_TYPE loses node's object branch", "why": "node's determineSpecificType renders an object receiver as `an instance of `, not `type object ()`. The reachable trigger is fs.readFile(path, options) with a forgotten callback. expect_detail keys on the ESCAPING text (Received {}), not the case assertion: assert.throws re-surfaces the original TypeError on a message mismatch here, so the assertion text never reaches the diff.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (typeof ctor === 'function' && ctor.name) return 'an instance of ' + ctor.name;", "replace": "", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -706,7 +706,7 @@ { "name": "62-fs-read-buffer dies when the Received string stops being truncated", "why": "node cuts a string longer than 28 chars to its first 25 plus '...' before inspecting it into the Received clause.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (typeof shown === 'string' && shown.length > RECEIVED_STR_MAX + 3) {\n shown = StringPrototypeSlice(shown, 0, RECEIVED_STR_MAX) + '...';\n }", "replace": "", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -717,7 +717,7 @@ { "name": "62-fs-read-buffer dies when the read result is copied instead of shared", "why": "The zero-copy re-tag is a rank-2 memory claim on the hottest fs call there is. A copying Buffer.from(view) lands in the Buffer pool, so the result is an offset into a 256 KB store instead of an exact-size view. Before this entry the claim had NO test: the copying form left the whole case green.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " return BufferFrom(\n TypedArrayPrototypeGetBuffer(value),\n TypedArrayPrototypeGetByteOffset(value),\n TypedArrayPrototypeGetByteLength(value),\n );", "replace": " return BufferFrom(value);", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -729,7 +729,7 @@ { "name": "62-fs-read-buffer dies when path type validation is dropped", "why": "node rejects a non string/Buffer/URL/fd path with ERR_INVALID_ARG_TYPE; without the check an object is stringified and read as the filename '[object Object]'.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " validatePath(path);\n var encoding = readEncoding(options);", "replace": " var encoding = readEncoding(options);", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -740,7 +740,7 @@ { "name": "62-fs-read-buffer dies when the error callback regains its second argument", "why": "node calls the readFile error callback with exactly one argument; the native's completion always builds a 2-slot array, so passing data through makes arguments.length 2.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (err) {\n cb(err);\n return;\n }", "replace": " if (err) {\n cb(err, data);\n return;\n }", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -751,7 +751,7 @@ { "name": "62-fs-read-buffer dies when cb validation moves after options validation", "why": "node validates `callback || options` BEFORE the options, so readFile(p,'bogus') reports the string as a bad callback. The reverse order reports a plausible but different error for a forgotten callback.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " var cb = callback || options;\n if (typeof cb !== 'function') {", "replace": " var cb = callback || options;\n readEncoding(typeof options === 'function' ? null : options);\n if (typeof cb !== 'function') {", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -762,7 +762,7 @@ { "name": "62-fs-read-buffer dies when a function in the options slot is rejected", "why": "node's getOptions treats a callback sitting in the options slot as absent; rejecting it turns readFileSync(p, fn) from a working binary read into a throw.", - "source": "pkg/runtime/js/internal/fs.js", + "source": "pkg/runtime/js/node/fs.js", "find": " if (options === null || options === undefined || typeof options === 'function') {", "replace": " if (options === null || options === undefined) {", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", @@ -773,7 +773,7 @@ { "name": "57-format-single-arg dies when inspect drops node's ${ guard", "why": "node's strEscape refuses the backtick delimiter when the string contains '${', because inspect output is meant to read back as a literal and a backtick-delimited '${' is a live template substitution. Omitting the guard also regressed output that was CORRECT before the delimiter rule was introduced.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';", "replace": " else if (StringPrototypeIndexOf(s, '`') === -1) q = '`';", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -784,7 +784,7 @@ { "name": "57-format-single-arg dies when the backtick branch is removed", "why": "A string containing both quote characters is backtick-delimited on node. The earlier manifest entry deleted the whole delimiter block, so it only proved the double-quote arm; deleting just this line survived the case.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -795,7 +795,7 @@ { "name": "57-format-single-arg dies when inspect stops escaping control characters", "why": "node's strEscape escapes the whole C0 range, DEL and C1. Leaving them raw makes an unescaped \\x1B in any inspected value an ANSI-injection vector through the log line, which is why this is a security fix and not a rendering nicety.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " else if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) esc = escByte(c);", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -806,7 +806,7 @@ { "name": "57-format-single-arg dies when a lone surrogate is not escaped", "why": "node escapes an unpaired surrogate as \\\\udXXX; Lava used to hand it to the string conversion, which replaced it with U+FFFD — the value came back LOSSY, not merely rendered differently.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": " if (next >= 0xdc00 && next <= 0xdfff) {\n i++; // consume the pair\n } else {\n esc = escUnit(c);\n }", "replace": " if (next >= 0xdc00 && next <= 0xdfff) {\n i++; // consume the pair\n }", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -828,7 +828,7 @@ { "name": "64-stream-write dies when write() reports errors async instead of throwing", "why": "node throws ERR_STREAM_NULL_VALUES and ERR_INVALID_ARG_TYPE synchronously from write(). Routing them through writeErrorNextTick delivers the right code on the wrong turn, so `assert.throws(() => s.write(null))` passes under node and fails here.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " if (chunk === null) throw errNullValues();", "replace": " if (chunk === null) { writeErrorNextTick(this, errNullValues(), cb); return false; }", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -839,7 +839,7 @@ { "name": "64-stream-write dies when the accept set goes back to instanceof Uint8Array", "why": "`instanceof Uint8Array` is wrong in BOTH directions: it rejects DataView and Int16Array, which node accepts, and it accepts Object.create(Uint8Array.prototype), which node rejects. ArrayBuffer.isView is the whole test.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " function isBytesChunk(chunk) {\n return ArrayBufferIsView(chunk);\n }", "replace": " function isBytesChunk(chunk) {\n return chunk instanceof Uint8Array;\n }", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -851,7 +851,7 @@ { "name": "64-stream-write dies when a view is not normalized to a Buffer", "why": "THE regression that got #326's attempt reverted. node hands _write a Buffer with encoding 'buffer' for every view; without the conversion `chunk.length` is undefined for a DataView, writableLength goes NaN, and 'drain' can never fire again because NaN === 0 is false.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " chunk = bytesToBuffer(chunk);\n encoding = 'buffer';\n } else {\n throw errInvalidArg('chunk', CHUNK_EXPECTED, chunk);", "replace": " encoding = 'buffer';\n } else {\n throw errInvalidArg('chunk', CHUNK_EXPECTED, chunk);", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -862,7 +862,7 @@ { "name": "64-stream-write dies when Readable.push does not accept what write does", "why": "Widening only the writable half makes a PassThrough take a view on one side and error on the other — which is how the first attempt shipped. The replacement KEEPS the normalization so the entry pins the brand widening alone — changing both at once would leave a kill ambiguous about which half the case detects.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " } else if (isBytesChunk(chunk)) {\n // The readable half must accept exactly what the writable half does, or a\n // PassThrough takes a view on one side and errors on the other — which is how the\n // first attempt at this shipped.\n chunk = bytesToBuffer(chunk);", "replace": " } else if (chunk instanceof Uint8Array) {\n chunk = bytesToBuffer(chunk);", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -873,7 +873,7 @@ { "name": "64-stream-write dies when the encoding is not validated before the chunk type", "why": "node validates an explicit encoding first, which is observable: write(5, 'bogus') reports ERR_UNKNOWN_ENCODING, not the chunk type.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " if (encoding) validateEncoding(encoding);\n", "replace": "", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -884,7 +884,7 @@ { "name": "64-stream-write dies when setDefaultEncoding accepts an unknown name", "why": "node throws ERR_UNKNOWN_ENCODING; accepting it silently poisons every later string write on the stream.", - "source": "pkg/runtime/js/internal/stream.js", + "source": "pkg/runtime/js/node/stream.js", "find": " validateEncoding(encoding);\n this._writableState.defaultEncoding = encoding;", "replace": " this._writableState.defaultEncoding = encoding;", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -895,7 +895,7 @@ { "name": "65-lazy-buffer-capture dies when a module resolves Buffer itself instead of taking the loader's", "why": "net.js is a LAZY module, so its own require('buffer') runs on the user's first require of net — after user code. Node is not steerable this way; measured, sock.write('PAYLOAD') delivered \"PAYLOAD\" on node and \"TAMPERED\" here.", - "source": "pkg/runtime/js/internal/net.js", + "source": "pkg/runtime/js/node/net.js", "find": " var PristineBuffer = require.pristineBuffer;", "replace": " var PristineBuffer = { Buffer: require('buffer').Buffer, from: require('buffer').Buffer.from, alloc: require('buffer').Buffer.alloc, concat: require('buffer').Buffer.concat, isBuffer: require('buffer').Buffer.isBuffer };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -906,7 +906,7 @@ { "name": "65-lazy-buffer-capture dies when Buffer.from is read off the constructor at call time", "why": "Capturing the CONSTRUCTOR is not enough: `.from` is a writable member read off it at call time, so `Buffer.from = evil` steers a module holding a pristine constructor. This is the half the first version of the fix missed — the object is not the method, the same lesson as Symbol.for in util.js.", - "source": "pkg/runtime/js/internal/net.js", + "source": "pkg/runtime/js/node/net.js", "find": " var BufferFrom = PristineBuffer.from;", "replace": " var BufferFrom = function (v, a, b) { return PristineBuffer.Buffer.from(v, a, b); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -917,7 +917,7 @@ { "name": "65-lazy-buffer-capture dies when os.userInfo reads Buffer.from at call time", "why": "os.userInfo originally re-read the live export on EVERY call, so it was steerable at any time — a case that only swaps things before the first require cannot see it. This mutation restores a call-time read while leaving the module-eval capture intact, so it pins the call-time half alone.", - "source": "pkg/runtime/js/internal/os.js", + "source": "pkg/runtime/js/node/os.js", "find": " var BufferFrom = PristineBuffer.from;", "replace": " var BufferFrom = function (v, a, b) { return PristineBuffer.Buffer.from(v, a, b); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -928,7 +928,7 @@ { "name": "65-lazy-buffer-capture dies when http.js resolves Buffer itself", "why": "http.js was migrated with NO test behind it: reverting this one line left every gate green while the measured divergence returned. Two of five migrated modules were unpinned until this entry.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var PristineBuffer = require.pristineBuffer;", "replace": " var PristineBuffer = { Buffer: require('buffer').Buffer, from: require('buffer').Buffer.from, alloc: require('buffer').Buffer.alloc, concat: require('buffer').Buffer.concat, isBuffer: require('buffer').Buffer.isBuffer, bufferToString: function (b, e, a, c) { return b.toString(e, a, c); }, bufferIndexOf: function (b, n) { return b.indexOf(n); }, bufferSlice: function (b, a, c) { return b.slice(a, c); } };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -939,7 +939,7 @@ { "name": "65-lazy-buffer-capture dies when the chunked reassembly reads Buffer.concat live", "why": "http.js's chunked decoder reassembles across TCP segments with concat; a poisoned concat that reorders the parts desynchronises the size line from its body. This is framing over network-controlled bytes, not a cosmetic read.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var BufferConcat = PristineBuffer.concat;", "replace": " var BufferConcat = function (l, t) { return require('buffer').Buffer.concat(l, t); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -950,7 +950,7 @@ { "name": "65-lazy-buffer-capture dies when the chunk size is read through a live toString", "why": "The size line is read with buffer.toString('latin1', 0, lineEnd). A replacement that INFLATES the size is a request-smuggling primitive; node frames HTTP in C (llhttp) and is not steerable at all.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var BufferPrototypeToString = PristineBuffer.bufferToString;", "replace": " var BufferPrototypeToString = function (b, e, a, c) { return b.toString(e, a, c); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -961,7 +961,7 @@ { "name": "65-lazy-buffer-capture dies when the framing cursor advances through a live slice", "why": "The decoder advances past each size line and body with slice; a corrupted cursor leaves a stray byte and desynchronises every later chunk.", - "source": "pkg/runtime/js/internal/http.js", + "source": "pkg/runtime/js/node/http.js", "find": " var BufferPrototypeSlice = PristineBuffer.bufferSlice;", "replace": " var BufferPrototypeSlice = function (b, a, c) { return b.slice(a, c); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -972,7 +972,7 @@ { "name": "65-lazy-buffer-capture dies when net's INBOUND wrap reads Buffer live", "why": "net's asBuffer wraps every 'data' chunk — the DISCLOSURE direction. The write-side assertion alone leaves it green while a shim reads every inbound byte.", - "source": "pkg/runtime/js/internal/net.js", + "source": "pkg/runtime/js/node/net.js", "find": " var BufferFrom = PristineBuffer.from;", "replace": " var BufferFrom = function (v, a, b) { return require('buffer').Buffer.from(v, a, b); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -983,7 +983,7 @@ { "name": "66-process-console-intrinsic dies when node:process re-reads the global", "why": "The factory used to export the free-var process, so a pre-first-require reassignment of globalThis.process poisoned every later require('node:process'). Node returns the intrinsic regardless (#247).", - "source": "pkg/runtime/js/internal/process.js", + "source": "pkg/runtime/js/node/process.js", "find": " module.exports = intrinsic;", "replace": " module.exports = process;", "gate": "compat:tests/node-compat/cases/66-process-console-intrinsic.js", @@ -992,7 +992,7 @@ { "name": "66-process-console-intrinsic dies when node:console re-reads the global", "why": "Same bug on the console factory: a pre-first-require reassignment of globalThis.console was captured as the module export. Console is a separate source file, so a single process entry would leave it unpinned.", - "source": "pkg/runtime/js/internal/console.js", + "source": "pkg/runtime/js/node/console.js", "find": " module.exports = intrinsic;", "replace": " module.exports = console;", "gate": "compat:tests/node-compat/cases/66-process-console-intrinsic.js", @@ -1002,8 +1002,8 @@ "name": "66-process-console-intrinsic dies when natives stop handing process the intrinsic", "why": "If install_internal_modules stops putting the process object on natives, the factory fail-closes. This pins the Odin wiring half, not just the JS export line.", "source": "pkg/runtime/globals.odin", - "find": "\tif process_val := get_named(ctx, global, \"process\"); process_val != nil {\n\t\tset_named(ctx, natives, \"process\", process_val)\n\t}", - "replace": "\tif false {\n\t\tif process_val := get_named(ctx, global, \"process\"); process_val != nil {\n\t\t\tset_named(ctx, natives, \"process\", process_val)\n\t\t}\n\t}", + "find": "\tif process_val := get_named(ctx, global, \"process\"); process_val != nil {\n\t\tset_named(ctx, natives, \"node/process\", process_val)\n\t}", + "replace": "\tif false {\n\t\tif process_val := get_named(ctx, global, \"process\"); process_val != nil {\n\t\t\tset_named(ctx, natives, \"node/process\", process_val)\n\t\t}\n\t}", "gate": "compat:tests/node-compat/cases/66-process-console-intrinsic.js", "expect_detail": "node:process intrinsic missing at context init" }, @@ -1011,8 +1011,8 @@ "name": "66-process-console-intrinsic dies when natives stop handing console the intrinsic", "why": "Symmetric to the process natives entry: console is a separate set_named, and leaving only the process one green-washes a console wiring regression.", "source": "pkg/runtime/globals.odin", - "find": "\tif console_val := get_named(ctx, global, \"console\"); console_val != nil {\n\t\tset_named(ctx, natives, \"console\", console_val)\n\t}", - "replace": "\tif false {\n\t\tif console_val := get_named(ctx, global, \"console\"); console_val != nil {\n\t\t\tset_named(ctx, natives, \"console\", console_val)\n\t\t}\n\t}", + "find": "\tif console_val := get_named(ctx, global, \"console\"); console_val != nil {\n\t\tset_named(ctx, natives, \"node/console\", console_val)\n\t}", + "replace": "\tif false {\n\t\tif console_val := get_named(ctx, global, \"console\"); console_val != nil {\n\t\t\tset_named(ctx, natives, \"node/console\", console_val)\n\t\t}\n\t}", "gate": "compat:tests/node-compat/cases/66-process-console-intrinsic.js", "expect_detail": "node:console intrinsic missing at context init" }, @@ -1138,7 +1138,7 @@ "expect_detail": [ "allowKey prefers binding", "must not be keyed by line number", - "internal/util.js:ansiRegex", + "node/util.js:ansiRegex", "Expected values to be strictly equal" ] }, @@ -1154,7 +1154,7 @@ { "name": "68-resizable-view-bytes dies when Buffer.from copies an out-of-bounds view with set()", "why": "%TypedArray%.prototype.set throws on an out-of-bounds SOURCE even at length 0, where node returns an empty Buffer.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " if (viewLength > 0) copy.set(value);\n", "replace": " copy.set(value);\n", "gate": "compat:tests/node-compat/cases/68-resizable-view-bytes.js", @@ -1671,7 +1671,7 @@ { "name": "70-buffer-size-inspect-parity dies when assertSize splices a bare type name", "why": "node prints `must be of type number` for Buffer.alloc('5'); a bare 'number' prints `must be number`.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "throw errInvalidArgType('size', 'of type number', size);", "replace": "throw errInvalidArgType('size', 'number', size);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1682,7 +1682,7 @@ { "name": "03-buffer dies when node:buffer exports SlowBuffer again", "why": "Node 26 removed SlowBuffer (DEP0030 end-of-life); `'SlowBuffer' in require('buffer')` is false there.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " Buffer: Buffer,\n Blob: Blob,", "replace": " Buffer: Buffer,\n SlowBuffer: Buffer,\n Blob: Blob,", "gate": "compat:tests/node-compat/cases/03-buffer.js", @@ -1691,7 +1691,7 @@ { "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "throw errInvalidArgType('offset', 'of type number', offset);", "replace": "throw errInvalidArgType('offset', 'number', offset);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1702,7 +1702,7 @@ { "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check splices a bare type name", "why": "Buffer.concat(list, '5') must say `The \"length\" argument must be of type number`.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "throw errInvalidArgType('length', 'of type number', totalLength);", "replace": "throw errInvalidArgType('length', 'number', totalLength);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1713,7 +1713,7 @@ { "name": "70-buffer-size-inspect-parity dies when the deprecated Buffer(n) skips assertSize", "why": "Buffer(-1) would throw JSC's codeless 'length cannot be negative' and Buffer(NaN) return an empty Buffer; node throws ERR_OUT_OF_RANGE.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " assertSize(arg);\n", "replace": "", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1724,7 +1724,7 @@ { "name": "70-buffer-size-inspect-parity dies when the more-bytes separator is dropped with no hex shown", "why": "node prints `` (two spaces) when INSPECT_MAX_BYTES = 0.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "hex += ' ... ' + remaining", "replace": "hex += (hex ? ' ' : '') + '... ' + remaining", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1735,7 +1735,7 @@ { "name": "70-buffer-size-inspect-parity dies when Buffer(n) routes through allocUnsafe instead of a fresh zero-filled FastBuffer", "why": "Buffer(n) returns a fresh zero-filled non-pooled Buffer, not a pool slice (#408).", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " return new FastBuffer(arg); // zero-filled, matching the old `super(number)`", "replace": " return Buffer.allocUnsafe(arg);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1746,7 +1746,7 @@ { "name": "73-buffer-type-fill-bigint dies when compare bounds targetStart by the target length", "why": "node bounds compare's *Start by kMaxLength only: Buffer.alloc(3).compare(Buffer.alloc(3), 100) is 1, not a RangeError.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "validateOffsetArg(targetStart, 'targetStart', K_MAX_LENGTH)", "replace": "validateOffsetArg(targetStart, 'targetStart', target.length)", "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", @@ -1757,7 +1757,7 @@ { "name": "73-buffer-type-fill-bigint dies when write bounds length by the bytes remaining after offset", "why": "node validates write's length against this.length and then clamps: Buffer.alloc(10).write('abcdef', 5, 8) is 5.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": "max = validateOffsetArg(length, 'length', this.length);", "replace": "max = validateOffsetArg(length, 'length', remaining);", "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", @@ -1768,7 +1768,7 @@ { "name": "73-buffer-type-fill-bigint dies when write stops clamping length to the remaining bytes", "why": "an in-range length past the end must be clamped; the hex path copies min(bytes, length) and overruns the view without it.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " if (max > remaining) max = remaining;\n", "replace": "", "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", @@ -1779,7 +1779,7 @@ { "name": "72-events-parity dies when once() stops validating options", "why": "node's events.once rejects a non-object options with ERR_INVALID_ARG_TYPE; without the check once(ee, 'foo', 5) just waits.", - "source": "pkg/runtime/js/internal/events.js", + "source": "pkg/runtime/js/node/events.js", "find": " return P.Promise.reject(errOptionsType(options));", "replace": " options = undefined;", "gate": "compat:tests/node-compat/cases/72-events-parity.js", @@ -1790,7 +1790,7 @@ { "name": "73-buffer-type-fill-bigint dies when writeBigInt rejects non-BigInt values before the range and offset checks", "why": "node runs checkInt (range, then offset bounds) before the BigInt mask, so Object(5n) writes and a bad range/offset is reported as ERR_OUT_OF_RANGE / ERR_INVALID_ARG_TYPE rather than 'Cannot mix BigInt'.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", "replace": " if (typeof value !== 'bigint') throw new TypeError('Cannot mix BigInt and other types, use explicit conversions');\n checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", @@ -1801,7 +1801,7 @@ { "name": "03-buffer dies when atob stops rejecting characters outside the forgiving-base64 alphabet", "why": "node's atob throws InvalidCharacterError 'Invalid character' for '!' and for VT; without the alphabet check they fall through to the length check.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " if (!alnum && c !== 0x2b && c !== 0x2f && c !== 0x3d)", "replace": " if (false)", "gate": "compat:tests/node-compat/cases/03-buffer.js", @@ -1812,7 +1812,7 @@ { "name": "03-buffer dies when btoa stops rejecting code units above 0xFF", "why": "node's btoa throws InvalidCharacterError for 'Ā'; latin1 encoding would silently truncate it to 0x00.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " var NON_LATIN1_RE = /[^\\u0000-\\u00ff]/;", "replace": " var NON_LATIN1_RE = /[^\\u0000-\\uffff]/;", "gate": "compat:tests/node-compat/cases/03-buffer.js", @@ -1823,7 +1823,7 @@ { "name": "71-assert-deep-strict-equal dies when a primitive Map key's values are not compared", "why": "the O(n) b.get fast path must still deep-compare the values; skipping it makes Maps with equal keys and different values equal.", - "source": "pkg/runtime/js/internal/util.js", + "source": "pkg/runtime/js/node/util.js", "find": "} else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) {", "replace": "} else if (false) {", "gate": "compat:tests/node-compat/cases/71-assert-deep-strict-equal.js", @@ -1834,7 +1834,7 @@ { "name": "atob_invalid_character_shape dies when the InvalidCharacterError code stops being the number 5", "why": "Lava has no DOMException, so atob/btoa throw an Error carrying DOMException's name and legacy NUMERIC code 5; console.log prints '5' and 5 alike, so only the Lava-only test sees the type.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " e.code = 5;\n", "replace": " e.code = '5';\n", "gate": "odin:cmd/lava:main.atob_invalid_character_shape", @@ -1845,7 +1845,7 @@ { "name": "03-buffer dies when the atob fast-path regex lets a non-alphabet character through", "why": "the regex fast path skips the per-char loop; if it admits '!' or VT, atob reports the length error instead of node's 'Invalid character'.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " var ATOB_SLOW_RE = /[^A-Za-z0-9+/=]|=[^=]|===/;", "replace": " var ATOB_SLOW_RE = /=[^=]|===/;", "gate": "compat:tests/node-compat/cases/03-buffer.js", @@ -1856,7 +1856,7 @@ { "name": "03-buffer dies when atob checks '=' padding before the lone-trailing-character rule", "why": "node 26 reports 'not correctly encoded' for 'a=', 'a==' and 'abcde=': the remainder without '=' is tested first.", - "source": "pkg/runtime/js/internal/buffer.js", + "source": "pkg/runtime/js/node/buffer.js", "find": " if ((nonWhitespace - equals) % 4 === 1)\n throw errInvalidCharacter('The string to be decoded is not correctly encoded.');\n if (equals && nonWhitespace % 4) throw errInvalidCharacter('Invalid character');\n", "replace": " if (equals && nonWhitespace % 4) throw errInvalidCharacter('Invalid character');\n if ((nonWhitespace - equals) % 4 === 1)\n throw errInvalidCharacter('The string to be decoded is not correctly encoded.');\n", "gate": "compat:tests/node-compat/cases/03-buffer.js", @@ -1904,7 +1904,7 @@ { "name": "05-coercion dies when undefined is no longer bound as NULL", "why": "node 26 binds undefined as SQL NULL; node 24 threw ERR_INVALID_ARG_TYPE. Dropping the branch falls through to the unbindable throw.", - "source": "pkg/runtime/js/internal/sqlite.js", + "source": "pkg/runtime/js/node/sqlite.js", "find": " if (value === undefined) {\n native.bind(stmtId, index, null); // primordials-ok: method\n } else if (value === null", "replace": " if (value === null", "gate": "compat:tests/std/sqlite/cases/05-coercion.js", @@ -1919,6 +1919,104 @@ "gate": "compat:tests/std/sqlite/cases/04-bigint.js", "expect_detail": "unsafe rowid: RangeError", "rebuild": true + }, + { + "name": "74-builtin-visibility dies when the public resolver answers internal/ ids", + "why": "The directory is the visibility rule: publicReq answers node/ ids only. Without the prefix test, require('internal/primordials') and every other internal/ module reach user code (and shadow npm packages of those names).", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " if (!hasPrefix(id, 'node/')) return undefined;\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/74-builtin-visibility.js", + "expect_detail": "bare internal/primordials RESOLVED" + }, + { + "name": "74-builtin-visibility dies when builtin source names lose the node: scheme", + "why": "Stack frames in builtin code read `fn@node:url:N:N`, the shape node's own frames take (`node:internal/url`). Dropping the scheme brings back bare `url`/`encoding` source names that no frame-filtering tool recognizes as a builtin.", + "source": "pkg/runtime/globals.odin", + "find": "strings.concatenate({\"node:\", spec}, context.temp_allocator)", + "replace": "strings.concatenate({\"\", spec}, context.temp_allocator)", + "gate": "compat:tests/node-compat/cases/74-builtin-visibility.js", + "expect_detail": "stack has node: false" + }, + { + "name": "check-js-graph fixtures die when a non-literal require is accepted", + "why": "A computed require id is how an internal module gets reached by a name nobody can grep for, and it leaves the graph below incomplete.", + "source": "scripts/check-js-graph.mjs", + "find": "add('dynamic', where(mod, call.line), 'require() argument is not a string literal');", + "replace": "", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "dynamic: a non-literal require argument", + "expect_detail": "dynamic: a non-literal require argument" + }, + { + "name": "check-js-graph fixtures die when an unresolvable require is accepted", + "why": "With #load_directory nothing checks a spelling at compile time: require('internal/mim') builds and returns undefined at run time.", + "source": "scripts/check-js-graph.mjs", + "find": "add('unresolved', where(mod, call.line), `require('${spec}') -> ${id} has no file`);", + "replace": "", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "unresolved: a require literal with no file", + "expect_detail": "unresolved: a require literal with no file" + }, + { + "name": "check-js-graph fixtures die when natives keys are not resolved", + "why": "natives is keyed by module id; a key left on the old spelling (\"buffer\") hands the module undefined and the factory fails closed or worse.", + "source": "scripts/check-js-graph.mjs", + "find": "/set_named\\(ctx, natives, \"([^\"]+)\"/g", + "replace": "/NEVER_MATCH_([^\"]+)/g", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "unresolved: a natives key naming no module", + "expect_detail": "unresolved: a natives key naming no module" + }, + { + "name": "check-js-graph fixtures die when unreachable modules pass", + "why": "A file nothing reaches is dead code that still ships and still gets evaluated at every context init.", + "source": "scripts/check-js-graph.mjs", + "find": "if (!seen.has(id)) add(", + "replace": "if (false) add(", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "unreachable: an internal module nothing requires", + "expect_detail": "unreachable: an internal module nothing requires" + }, + { + "name": "check-js-graph fixtures die when an unembedded directory passes", + "why": "#load_directory is not recursive, so a new subdirectory under node/ silently never ships without its own row.", + "source": "scripts/check-js-graph.mjs", + "find": "if (!loadDirs.has(dir) && !loadFiles.has(rel)) {", + "replace": "if (false) {", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "unembedded: a directory of .js no #load_directory reads", + "expect_detail": "unembedded: a directory of .js no #load_directory reads" + }, + { + "name": "check-js-graph fixtures die when #load_directory may read bootstrap/", + "why": "esm.js is kept off the module table on purpose (CLAUDE.md §5); a #load_directory over bootstrap/ would hand it to the loader as a factory.", + "source": "scripts/check-js-graph.mjs", + "find": "if (!MODULE_DIRS.some((d) => inside === d || inside.startsWith(d + '/'))) {", + "replace": "if (false) {", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "unembedded: #load_directory of bootstrap/ would put esm.js on the module table", + "expect_detail": "unembedded: #load_directory of bootstrap/ would put esm.js on the module table" + }, + { + "name": "check-js-graph fixtures die when module-eval cycles pass", + "why": "A module-eval cycle hands one side the other's PARTIAL exports, which reads as undefined members at run time.", + "source": "scripts/check-js-graph.mjs", + "find": "add('cycle', where(modules.get(id), e.line), cycle.join(' -> '));", + "replace": "", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "cycle: two module-eval requires that close a loop", + "expect_detail": "cycle: two module-eval requires that close a loop" + }, + { + "name": "check-js-graph fixtures die when late-bound requires count as cycle edges", + "why": "A require inside a function runs after both factories returned; counting it would forbid the sanctioned late binding stream <-> stream/web uses.", + "source": "scripts/check-js-graph.mjs", + "find": "topLevel: fns.length <= 1", + "replace": "topLevel: true", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)", + "expect_detail": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)" } ] } diff --git a/tests/node-compat/cases/57-format-single-arg.js b/tests/node-compat/cases/57-format-single-arg.js index 5c2ac84..2ad53cf 100644 --- a/tests/node-compat/cases/57-format-single-arg.js +++ b/tests/node-compat/cases/57-format-single-arg.js @@ -11,7 +11,7 @@ // 31-row contract probe diverged, all of them `%%`. // // TWO implementations are covered on purpose. `util.format` runs -// internal/util.js's formatWithOptions; `console.log` runs console.js's own +// node/util.js's formatWithOptions; `console.log` runs console.js's own // formatArgs, because console.js is the bootstrap prelude — it receives native // write functions and has no `require`, so it cannot delegate. A test through // `util` alone would leave console.js unpinned, and the two are pinned by separate diff --git a/tests/node-compat/cases/63-fs-buffer-capture.js b/tests/node-compat/cases/63-fs-buffer-capture.js index 7572268..423f326 100644 --- a/tests/node-compat/cases/63-fs-buffer-capture.js +++ b/tests/node-compat/cases/63-fs-buffer-capture.js @@ -1,6 +1,6 @@ // fs must not resolve Buffer through the mutable `node:buffer` exports. // -// `js/internal/fs.js` is a LAZY internal module: its factory does not run until user code +// `js/node/fs.js` is a LAZY internal module: its factory does not run until user code // calls require('fs'). A `require('buffer').Buffer` capture at ITS module-eval therefore // reads whatever the exports object holds by then — and that is the same object user code // holds. §5's "capture at module-eval is safe" rule only holds for modules the LOADER diff --git a/tests/node-compat/cases/74-builtin-visibility.js b/tests/node-compat/cases/74-builtin-visibility.js new file mode 100644 index 0000000..50a4518 --- /dev/null +++ b/tests/node-compat/cases/74-builtin-visibility.js @@ -0,0 +1,106 @@ +// Builtin visibility: the embedded JS tree decides what the public resolver answers. +// Modules under pkg/runtime/js/node are requireable; pkg/runtime/js/internal is not, and +// neither is a module's internal id spelled directly (`node/buffer`, `internal/fetch`). +// +// Only the error CODE is printed for the bare spellings and only "threw" for `node:` +// ones: the messages diverge from node (#415 — lava has no "Require stack", and answers +// MODULE_NOT_FOUND where node answers ERR_UNKNOWN_BUILTIN_MODULE for `node:` names). +// Bare `sqlite` is left out on purpose: lava resolves it and node does not (#416). + +const hidden = [ + 'primordials', + 'fetch', + 'abort', + 'encoding', + 'structured_clone', + 'stdio', + 'mime', + 'parse_args', + 'parse_env', + 'internal/primordials', + 'internal/fetch', + 'internal/abort', + 'internal/encoding', + 'internal/structured_clone', + 'internal/stdio', + 'internal/mime', + 'internal/parse_args', + 'internal/parse_env', + 'node/buffer', + 'node/stream/web', + 'constructor', + '__proto__', +]; + +for (const name of hidden) { + try { + require(name); + console.log('bare', name, 'RESOLVED'); + } catch (e) { + console.log('bare', name, e.code); + } + let threw = false; + try { + require('node:' + name); + } catch { + threw = true; + } + console.log('node:' + name, threw ? 'threw' : 'RESOLVED'); +} + +const visible = [ + 'assert', + 'assert/strict', + 'node:assert/strict', + 'buffer', + 'node:buffer', + 'console', + 'node:console', + 'crypto', + 'diagnostics_channel', + 'dns', + 'dns/promises', + 'events', + 'fs', + 'http', + 'https', + 'net', + 'os', + 'path', + 'path/posix', + 'node:path/win32', + 'process', + 'node:process', + 'querystring', + 'node:sqlite', + 'stream', + 'stream/promises', + 'stream/web', + 'node:stream/web', + 'string_decoder', + 'timers', + 'timers/promises', + 'tty', + 'url', + 'util', + 'util/types', +]; +for (const name of visible) console.log(name, typeof require(name)); + +// Frames inside builtin code carry a `node:` source name on both runtimes (node: +// `node:internal/url`, lava: `node:url`); only the presence is compared, the frame +// syntax differs (`at fn (file:N:N)` vs `fn@file:N:N`). +const throwers = [ + ['new URL', () => new URL('x')], + ['Buffer.alloc', () => Buffer.alloc(-1)], + ['path.join', () => require('path').join(1)], + ['new TextDecoder', () => new TextDecoder('nope')], +]; +for (const [label, fn] of throwers) { + try { + fn(); + console.log(label, 'NO THROW'); + } catch (e) { + console.log(label, 'stack has node:', String(e.stack).includes('node:')); + } +} diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 2627473..1f47139 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -1,257 +1,257 @@ { - "console.js": { + "bootstrap/console.js": { "method": 53, "invoke": 3, "accessor": 2, "global": 40 }, + "bootstrap/esm.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 + }, + "bootstrap/loader.js": { + "method": 2, + "invoke": 1, + "accessor": 0, + "global": 0 + }, + "bootstrap/microtasks.js": { + "method": 1, + "invoke": 0, + "accessor": 0, + "global": 2 + }, "internal/abort.js": { "method": 6, "invoke": 2, "accessor": 0, "global": 9 }, - "internal/assert.js": { + "internal/encoding.js": { + "method": 1, + "invoke": 0, + "accessor": 3, + "global": 0 + }, + "internal/fetch.js": { + "method": 28, + "invoke": 1, + "accessor": 6, + "global": 75 + }, + "internal/mime.js": { + "method": 24, + "invoke": 0, + "accessor": 0, + "global": 5 + }, + "internal/parse_args.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 1 + }, + "internal/parse_env.js": { + "method": 0, + "invoke": 0, + "accessor": 3, + "global": 2 + }, + "internal/primordials.js": { + "method": 0, + "invoke": 9, + "accessor": 0, + "global": 1 + }, + "internal/stdio.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 + }, + "internal/structured_clone.js": { + "method": 5, + "invoke": 1, + "accessor": 0, + "global": 14 + }, + "node/assert.js": { "method": 14, "invoke": 5, "accessor": 8, "global": 40 }, - "internal/buffer.js": { + "node/buffer.js": { "method": 41, "invoke": 6, "accessor": 22, "global": 127 }, - "internal/console.js": { + "node/console.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/crypto.js": { + "node/crypto.js": { "method": 22, "invoke": 0, "accessor": 20, "global": 71 }, - "internal/diagnostics_channel.js": { + "node/diagnostics_channel.js": { "method": 5, "invoke": 9, "accessor": 0, "global": 13 }, - "internal/dns.js": { + "node/dns.js": { "method": 10, "invoke": 0, "accessor": 0, "global": 8 }, - "internal/dns_promises.js": { + "node/dns/promises.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/encoding.js": { - "method": 1, - "invoke": 0, - "accessor": 3, - "global": 0 - }, - "internal/esm.js": { - "method": 0, - "invoke": 0, - "accessor": 0, - "global": 0 - }, - "internal/events.js": { + "node/events.js": { "method": 0, "invoke": 2, "accessor": 0, "global": 12 }, - "internal/fetch.js": { - "method": 28, - "invoke": 1, - "accessor": 6, - "global": 75 - }, - "internal/fs.js": { + "node/fs.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/http.js": { + "node/http.js": { "method": 21, "invoke": 5, "accessor": 0, "global": 21 }, - "internal/https.js": { + "node/https.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 8 }, - "internal/loader.js": { - "method": 2, - "invoke": 1, - "accessor": 0, - "global": 0 - }, - "internal/microtasks.js": { - "method": 1, - "invoke": 0, - "accessor": 0, - "global": 2 - }, - "internal/mime.js": { - "method": 24, - "invoke": 0, - "accessor": 0, - "global": 5 - }, - "internal/net.js": { + "node/net.js": { "method": 2, "invoke": 2, "accessor": 3, "global": 9 }, - "internal/os.js": { + "node/os.js": { "method": 12, "invoke": 0, "accessor": 1, "global": 7 }, - "internal/parse_args.js": { - "method": 0, - "invoke": 0, - "accessor": 0, - "global": 1 - }, - "internal/parse_env.js": { - "method": 0, - "invoke": 0, - "accessor": 3, - "global": 2 - }, - "internal/path.js": { + "node/path.js": { "method": 161, "invoke": 0, "accessor": 0, "global": 9 }, - "internal/path_posix.js": { + "node/path/posix.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/path_win32.js": { + "node/path/win32.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/primordials.js": { - "method": 0, - "invoke": 9, - "accessor": 0, - "global": 1 - }, - "internal/process.js": { + "node/process.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/punycode.js": { + "node/punycode.js": { "method": 24, "invoke": 0, "accessor": 0, "global": 2 }, - "internal/querystring.js": { + "node/querystring.js": { "method": 6, "invoke": 1, "accessor": 0, "global": 6 }, - "internal/sqlite.js": { + "node/sqlite.js": { "method": 10, "invoke": 1, "accessor": 3, "global": 20 }, - "internal/stdio.js": { - "method": 0, - "invoke": 0, - "accessor": 0, - "global": 0 - }, - "internal/stream.js": { + "node/stream.js": { "method": 42, "invoke": 9, "accessor": 25, "global": 32 }, - "internal/stream_promises.js": { + "node/stream/promises.js": { "method": 1, "invoke": 1, "accessor": 0, "global": 2 }, - "internal/streams.js": { + "node/stream/web.js": { "method": 22, "invoke": 0, "accessor": 1, "global": 65 }, - "internal/string_decoder.js": { + "node/string_decoder.js": { "method": 14, "invoke": 0, "accessor": 3, "global": 5 }, - "internal/structured_clone.js": { - "method": 5, - "invoke": 1, - "accessor": 0, - "global": 14 - }, - "internal/timers.js": { + "node/timers.js": { "method": 0, "invoke": 0, "accessor": 0, "global": 0 }, - "internal/timers_promises.js": { + "node/timers/promises.js": { "method": 2, "invoke": 0, "accessor": 0, "global": 4 }, - "internal/tty.js": { + "node/tty.js": { "method": 2, "invoke": 2, "accessor": 0, "global": 2 }, - "internal/url.js": { + "node/url.js": { "method": 46, "invoke": 2, "accessor": 0, "global": 24 }, - "internal/util.js": { + "node/util.js": { "method": 46, "invoke": 14, "accessor": 15, "global": 72 }, - "internal/util_types.js": { + "node/util/types.js": { "method": 0, "invoke": 1, "accessor": 0, diff --git a/tests/runtime/eventloop/cases/10-nexttick-absolute-priority.js b/tests/runtime/eventloop/cases/10-nexttick-absolute-priority.js index 48df489..8d2e5c6 100644 --- a/tests/runtime/eventloop/cases/10-nexttick-absolute-priority.js +++ b/tests/runtime/eventloop/cases/10-nexttick-absolute-priority.js @@ -3,7 +3,7 @@ const assert = require('node:assert/strict'); // Node drains the ENTIRE process.nextTick queue before any promise job, even a // nextTick queued *after* the promise (absolute nextTick priority). // -// KNOWN LAVA GAP (see issue #16 and js/internal/microtasks.js): Lava layers +// KNOWN LAVA GAP (see issue #16 and js/bootstrap/microtasks.js): Lava layers // nextTick on JSC's promise-microtask queue, which it cannot reorder ahead of a // promise job already enqueued earlier in the turn — so Lava reports // ['promise', 'nextTick'] here. This case is listed in known-lava-gaps.txt so it From 1de56c02ff4034ad3f2927e3a2d6633943ff433d Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:23:33 +0200 Subject: [PATCH 21/61] fix(test): repoint the unreachable-module mutation at the formatted source line The formatter split `if (!seen.has(id)) add(...)` across two lines after the entry was recorded, so its `find` no longer matched and the mutation gate reported it STALE. Anchor on the `add('unreachable',` call instead, which is unique in the file; the mutant still turns only its own test red. Co-Authored-By: Claude Fable 5.2 --- tests/mutation-manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index cac42b0..6b73ba8 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1972,8 +1972,8 @@ "name": "check-js-graph fixtures die when unreachable modules pass", "why": "A file nothing reaches is dead code that still ships and still gets evaluated at every context init.", "source": "scripts/check-js-graph.mjs", - "find": "if (!seen.has(id)) add(", - "replace": "if (false) add(", + "find": "add('unreachable',", + "replace": "if (false) add('unreachable',", "gate": "node-test:scripts/check-js-graph.test.mjs", "test": "unreachable: an internal module nothing requires", "expect_detail": "unreachable: an internal module nothing requires" From f9f0cf88724aa593b1d8f486e8a08b03978c0be6 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:06:34 +0200 Subject: [PATCH 22/61] perf(runtime): compile builtin JS factories on first require install_internal_modules evaluated every embedded factory at context init, though the loader instantiates only the eager closure (13 of 39). The loader now receives a cold native compile(id) instead of a factories object and calls it on a module's first require. The id is matched against the embedded directory table; the LAVA_JS_OVERRIDE disk path and the source name come only from the matched entry, so no argument can reach a file outside the binary. A failed evaluation is reported to stderr and yields undefined, as before. noop.js spawn to exit, darwin arm64, -o:speed, 50 interleaved runs per arm after 5 warmups: median 16.85 -> 14.63 ms (0.868x); JSEvaluateScript calls before user code 43 -> 17. Pinned by cmd/lava/lazy_builtins_test.odin and two recorded mutations. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/lazy_builtins_test.odin | 93 +++++++++++++ docs/ARCHITECTURE.md | 8 +- pkg/runtime/globals.odin | 151 +++++++++++++++++----- pkg/runtime/js/bootstrap/loader.js | 29 +++-- tests/mutation-manifest.json | 18 +++ tests/node-compat/pollution-baseline.json | 2 +- 6 files changed, 256 insertions(+), 45 deletions(-) create mode 100644 cmd/lava/lazy_builtins_test.odin diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin new file mode 100644 index 0000000..f2eebf4 --- /dev/null +++ b/cmd/lava/lazy_builtins_test.odin @@ -0,0 +1,93 @@ +#+build linux, darwin +package main + +import "core:strings" +import "core:testing" +import jsc "lava:pkg/jsc" +import lava "lava:pkg/runtime" + +// Pins compile-on-first-require for the embedded builtin factories +// (install_internal_modules / builtin_compile_cb, globals.odin): context init +// compiles only what the loader's eager list pulls in, a lazy builtin's source is +// evaluated by its first require and by nothing else, and the loader's instance +// cache keeps a second require from compiling it again. Lava-only: how many +// JSEvaluateScript calls happen before user code is not something Node exposes. +// Runtime_State.builtin_compiles is the observable; it counts successful compiles. +// +// Built by hand rather than through lava.eval so the state outlives each script and +// the counter can be read between them. Scoped linux+darwin, where JSC links. +@(test) +builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { + jsc.lava_jsc_init() + gclass := lava.make_global_class() + gctx := jsc.JSGlobalContextCreate(gclass) + testing.expect(t, gctx != nil, "could not create a JSC global context") + if gctx == nil { + jsc.JSClassRelease(gclass) + return + } + defer jsc.JSClassRelease(gclass) + defer jsc.JSGlobalContextRelease(gctx) + ctx := cast(jsc.JSContextRef)gctx + + state := lava.new_runtime_state(nil) + jsc.JSObjectSetPrivate(jsc.JSContextGetGlobalObject(ctx), cast(rawptr)state) + defer lava.destroy_runtime_state(ctx, state) + lava.capture_error_intrinsics(ctx, state) + jsc.prime_view_probe(ctx) + lava.setup_module_environment(ctx, "", nil) + defer free_all(context.temp_allocator) + + embedded := 0 + for dir in lava.EMBEDDED_JS_DIRS { + for file in dir.files { + if strings.has_suffix(file.name, ".js") do embedded += 1 + } + } + + // The eager closure (primordials, buffer, stream/web, fetch, …, plus stdio and + // stream from installStdio) is well under the whole table. Eager compilation + // compiles every file and fails the upper bound. + at_init := state.builtin_compiles + testing.expectf(t, at_init > 0, "nothing compiled at init — the eager list did not run") + testing.expectf( + t, + at_init < embedded, + "init compiled %d of %d embedded modules — factories are still compiled eagerly", + at_init, + embedded, + ) + + // node:os is outside the eager closure and its only requires (primordials, + // buffer through require.pristineBuffer) are eager, so its first require + // compiles exactly one factory: its own. + testing.expect(t, run_bool(ctx, "typeof require('node:os').platform() === 'string'"), "node:os did not load") + testing.expect_value(t, state.builtin_compiles, at_init + 1) + + // Cached by the loader: a second require compiles nothing. + testing.expect(t, run_bool(ctx, "require('os') === require('node:os')"), "node:os was re-instantiated") + testing.expect_value(t, state.builtin_compiles, at_init + 1) + + // Ids outside the embedded set stay unknown, compile nothing, and never reach + // an inherited property or a path outside js/. + UNKNOWN :: `(function () { + var ids = ['node:constructor', 'node:__proto__', 'node:', 'node:../globals', 'node:internal/primordials', 'node:node/os']; + for (var i = 0; i < ids.length; i++) { + try { require(ids[i]); return false; } catch (e) { if (e.code !== 'MODULE_NOT_FOUND') return false; } + } + return true; +})()` + testing.expect(t, run_bool(ctx, UNKNOWN), "an id outside the embedded set resolved") + testing.expect_value(t, state.builtin_compiles, at_init + 1) +} + +@(private = "file") +run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { + c_src := strings.clone_to_cstring(src, context.temp_allocator) + script := jsc.JSStringCreateWithUTF8CString(c_src) + defer jsc.JSStringRelease(script) + exception: jsc.JSValueRef + value := jsc.JSEvaluateScript(ctx, script, nil, nil, 1, &exception) + if exception != nil do return false + return jsc.JSValueToBoolean(ctx, value) +} diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 39b6f3b..dea62f3 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -138,8 +138,12 @@ Thin, per-platform `foreign` declarations of JSC's C API. Highlights: ### 3.3 Standard library: native + embedded JS Built-ins are JS factories embedded at compile time and instantiated by -`js/bootstrap/loader.js`. The directory layout under `pkg/runtime/js` is the module -table — there is no second list of files: +`js/bootstrap/loader.js`. A factory's source is evaluated on its module's first +require, through a native `compile(id)` (`builtin_compile_cb`) the loader keeps in its +closure, not at context init: only the eager closure (13 of 39 files) is compiled before +user code, which took `bench/micro/noop.js` from 16.85 to 14.63 ms (median of 50 +interleaved spawn-to-exit runs per arm, darwin arm64, `-o:speed`). The directory +layout under `pkg/runtime/js` is the module table — there is no second list of files: | Directory | Holds | Module id / visibility | | ------------ | ---------------------------------------- | -------------------------------------------------------------------- | diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index bf90f8c..211d2b2 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -90,6 +90,9 @@ Runtime_State :: struct { // and read by install_process. Empty for embedders that don't set it (then // install_process falls back to os.args). script_argv: []string, + // Embedded builtin factories compiled so far (builtin_compile_cb). Only an + // observable for cmd/lava/lazy_builtins_test.odin; nothing branches on it. + builtin_compiles: int, } // JS_Callback bridges a JS function into an event-loop Callback. The function @@ -1103,43 +1106,35 @@ microtask_report_uncaught_cb :: proc "c" ( return jsc.JSValueMakeUndefined(ctx) } -// install_internal_modules evaluates the JS built-in modules (util, events, -// assert, buffer) to factory functions, hands them to the loader, and stores -// the resulting resolver on Runtime_State. native_require_cb consults that -// resolver before hitting the filesystem. The loader also eagerly instantiates -// modules that install globals (Buffer), so those exist without a require. +// install_internal_modules hands the module loader a native `compile(id)` and the +// per-module natives, and stores the resulting resolver on Runtime_State. +// native_require_cb consults that resolver before hitting the filesystem. The +// loader eagerly instantiates the modules that install globals (Buffer, fetch, +// URL, …), so those exist without a require; every other builtin's factory is +// compiled by builtin_compile_cb on its first require. +// +// Compile-on-first-require, not at init: evaluating all 39 factory sources made a +// script that requires nothing pay for http, sqlite and assert. Lazily, only the +// eager closure (13 files) is compiled before user code — 17 internal +// JSEvaluateScript calls instead of 43 — and `lava run bench/micro/noop.js` went +// 16.85 -> 14.63 ms (medians of 50 interleaved spawn-to-exit runs per arm after 5 +// warmups, darwin arm64, -o:speed, f106ab4 vs this change). The cost moves to each +// lazy module's first require, once per context. See builtin_compile_cb. install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) { state := get_state_from_ctx(ctx) if state == nil do return - // Module id, source name and override path all derive from the directory and the - // file's basename, so the tree under js/ is the only list of modules: adding - // js/node/foo.js makes `node:foo` requireable with no Odin edit, and a file under - // js/internal/ is reachable only through the factories' own `require`. override_dir := js_override_dir() - factories := jsc.JSObjectMake(ctx, nil, nil) - for dir in EMBEDDED_JS_DIRS { - for file in dir.files { - if !strings.has_suffix(file.name, ".js") do continue - id, id_err := strings.concatenate({dir.prefix, "/", file.name[:len(file.name) - 3]}, context.temp_allocator) - if id_err != nil do continue - rel_path, rel_err := strings.concatenate({"js/", id, ".js"}, context.temp_allocator) - if rel_err != nil do continue - // Source name is what a stack frame shows: `node:buffer`, `node:stream/web` - // for public modules and `node:internal/fetch` for internal ones, the shape - // node's own frames take (`node:internal/url`). - spec := id[len("node/"):] if strings.has_prefix(id, "node/") else id - source_name, name_err := strings.concatenate({"node:", spec}, context.temp_allocator) - if name_err != nil do continue - factory := eval_internal_source(ctx, source_name, override_dir, rel_path, string(file.data)) - if factory == nil do continue - set_named(ctx, factories, id, factory) - } - } - loader := eval_internal_source(ctx, "internal:loader", override_dir, "js/bootstrap/loader.js", BOOTSTRAP_LOADER) if loader == nil || !jsc.JSValueIsObject(ctx, loader) do return + // Cold native: called once per builtin per context, so the generic host path, + // not a *_host wrapper. Passed as a bare argument, so it is reachable only from + // the loader's closure — never from user code. + compile := host_native_create(ctx, "compile", builtin_compile_cb, 1) + if compile == nil do compile = make_native_function(ctx, "compile", builtin_compile_cb) + if compile == nil do return + // Native primitives keyed by module id. The loader passes natives[id] to // each factory as its fourth argument, so a module receives its Odin-backed // bindings without anything landing on globalThis (cf. install_console). @@ -1177,7 +1172,7 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) set_named(ctx, natives, "node/console", console_val) } - args := [2]jsc.JSValueRef{cast(jsc.JSValueRef)factories, cast(jsc.JSValueRef)natives} + args := [2]jsc.JSValueRef{cast(jsc.JSValueRef)compile, cast(jsc.JSValueRef)natives} exception: jsc.JSValueRef resolver := jsc.JSObjectCallAsFunction( ctx, @@ -1236,6 +1231,100 @@ install_internal_modules :: proc(ctx: jsc.JSContextRef, global: jsc.JSObjectRef) } } +// builtin_compile_cb is the loader's `compile(id)`: it evaluates the embedded +// source of module `id` (`node/os`, `node/stream/web`, `internal/mime`) and +// returns its factory `(require, module, exports, native) => exports?`. +// +// Returns: +// The factory; undefined for a non-string or an id with no embedded file, and +// for a source that failed to evaluate (reported to stderr, as at init before +// this was lazy — the loader then answers "not a builtin"). +// Node: +// Not observable: Node also compiles a builtin on its first require, and the +// loader's cache (bootstrap/loader.js) keeps it to once per context. +// +// The id is matched against EMBEDDED_JS_DIRS — exact directory prefix, exact +// basename — so only a file in the embedded set is reachable: 'node/constructor', +// '__proto__', '' and anything with '..' match no row. The override path and the +// source name are rebuilt from the MATCHED row, never from the argument, so a +// LAVA_JS_OVERRIDE build cannot be steered to read a file outside js/ either. +// The scan is linear over ~40 rows with no allocation; it runs once per builtin, +// plus once per `require` of a bare name that is not a builtin (express, lodash) +// on its way to the filesystem resolver. +// +// js_override_dir is recomputed per call rather than cached on Runtime_State: in a +// normal build it is a compile-time "" and costs nothing; only override builds +// (mutation runs) pay one os.exists per compile. +builtin_compile_cb :: proc "c" ( + ctx: jsc.JSContextRef, + function: jsc.JSObjectRef, + this_object: jsc.JSObjectRef, + argument_count: c.size_t, + arguments: [^]jsc.JSValueRef, + exception: ^jsc.JSValueRef, +) -> jsc.JSValueRef { + context = runtime.default_context() + // Everything below is scratch for this one evaluation (path and cstring + // copies); nothing outlives the call. A guard rather than leaning on the + // caller's choke point: compile runs under native_require_cb's per-require + // free_all, under eval's at init, or under a loop tick's when a builtin + // requires a lazy internal module from a callback. + runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD() + undefined := jsc.JSValueMakeUndefined(ctx) + state := get_state_from_ctx(ctx) + if state == nil || argument_count < 1 || !jsc.JSValueIsString(ctx, arguments[0]) do return undefined + + // The loader builds ids by concatenation, so the argument is usually a rope, + // which value_chars8 declines; read_string_arg flattens it. Where the cell read + // is unavailable (off Linux) the C API hands back UTF-16 even for an ASCII + // string, so narrow it into a stack buffer; a non-ASCII unit or an id longer + // than any embedded one cannot match. + r := read_string_arg(ctx, arguments[0]) + defer if r.str != nil do jsc.JSStringRelease(r.str) + if !r.ok do return undefined + narrow: [64]byte + id: string + if r.is8 { + id = string(r.s8) + } else { + if len(r.s16) > len(narrow) do return undefined + for u, i in r.s16 { + if u >= 0x80 do return undefined + narrow[i] = byte(u) + } + id = string(narrow[:len(r.s16)]) + } + + slash := strings.last_index_byte(id, '/') + if slash <= 0 do return undefined + prefix, base := id[:slash], id[slash + 1:] + if len(base) == 0 do return undefined + + for dir in EMBEDDED_JS_DIRS { + if dir.prefix != prefix do continue + for file in dir.files { + if len(file.name) != len(base) + 3 || !strings.has_suffix(file.name, ".js") do continue + if file.name[:len(base)] != base do continue + + rel_path, rel_err := strings.concatenate({"js/", dir.prefix, "/", file.name}, context.temp_allocator) + if rel_err != nil do return undefined + // Source name is what a stack frame shows: `node:buffer`, `node:stream/web` + // for public modules and `node:internal/fetch` for internal ones, the shape + // node's own frames take (`node:internal/url`). + spec := rel_path[len("js/"):len(rel_path) - len(".js")] + if strings.has_prefix(spec, "node/") do spec = spec[len("node/"):] + source_name, name_err := strings.concatenate({"node:", spec}, context.temp_allocator) + if name_err != nil do return undefined + factory := eval_internal_source(ctx, source_name, js_override_dir(), rel_path, string(file.data)) + if factory == nil do return undefined + state.builtin_compiles += 1 + return factory + } + return undefined + } + return undefined +} + // require_builtin asks the JS resolver for an internal module by specifier. // Returns nil when the resolver is absent or the module is unknown (the latter // surfaces as `undefined`, which the caller treats as "not a builtin"). @@ -1513,7 +1602,7 @@ CONSOLE_PRELUDE :: #load("js/bootstrap/console.js", string) // process) => {}`). Installed after `process` exists; see install_microtasks. MICROTASK_PRELUDE :: #load("js/bootstrap/microtasks.js", string) -// The module loader: evaluates to `(factories, natives) => resolver`. A bootstrap +// The module loader: evaluates to `(compile, natives) => resolver`. A bootstrap // file, not a module — it is what makes the modules below requireable. BOOTSTRAP_LOADER :: #load("js/bootstrap/loader.js", string) diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 9522960..5a805ba 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -1,20 +1,28 @@ -// Internal built-in module loader. Given a map of module id -> factory function -// `(require, module, exports, native) => exports?`, it returns a `require` +// Internal built-in module loader. Given a native `compile(id)` that evaluates a +// module's embedded source to its factory `(require, module, exports, native) => +// exports?` (undefined when the id is not a builtin), it returns a `require` // implementation that instantiates each module lazily, caches it, strips the // `node:` prefix, and lets internal modules require one another (with the usual // CommonJS partial-exports behavior on cycles). The resolver is what native // `require()` consults before touching the filesystem. // +// A factory is compiled on its module's first require, not at context init, so a +// script pays only for the eager closure below (13 of 39 files): `lava run +// bench/micro/noop.js` went 16.85 -> 14.63 ms (medians of 50 interleaved +// spawn-to-exit runs per arm after 5 warmups, darwin arm64, -o:speed, f106ab4 vs +// this change; see install_internal_modules). +// `compile` stays in this closure — never on `req`, `publicReq` or anything user +// code reaches — and the cache below is what keeps it to one call per module. +// // A module id is its path under pkg/runtime/js without `.js` — `node/buffer`, // `node/stream/web`, `internal/primordials` — so the DIRECTORY decides visibility: // the public resolver answers `node/` ids only, and everything under `internal/` // is reachable solely through the `require` handed to the factories. -(function (factories, natives) { +(function (compile, natives) { 'use strict'; var cache = Object.create(null); natives = natives || Object.create(null); - var hasOwn = Object.prototype.hasOwnProperty; // Prefix test by code unit. The specifier comes from user code on the public path, // so a String.prototype method here would be a live, replaceable read deciding @@ -40,13 +48,12 @@ function load(id) { if (id in cache) return cache[id]; - // Look up own properties only: `factories` may inherit from Object.prototype, - // so a bracket lookup of 'node/constructor' or a polluted prototype key would - // otherwise return an inherited function and mis-resolve that specifier instead - // of yielding the "not a builtin" (undefined) that lets native_require_cb fall - // through. - if (!hasOwn.call(factories, id)) return; - var factory = factories[id]; + // Native and exact: compile matches the id against the embedded file table, so + // 'node/constructor' or a polluted prototype key yields undefined — the "not a + // builtin" that lets native_require_cb fall through — instead of an inherited + // function. Nothing is cached for an unknown id. + var factory = compile(id); + if (factory === undefined) return; var module = { exports: {} }; // Seed the cache before running the factory so a require cycle resolves // to the partially-built exports object instead of looping forever. diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 6b73ba8..3c36159 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2017,6 +2017,24 @@ "gate": "node-test:scripts/check-js-graph.test.mjs", "test": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)", "expect_detail": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)" + }, + { + "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", + "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " req('crypto');\n", + "replace": " req('crypto');\n req('os');\n", + "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", + "expect_detail": "expected state.builtin_compiles to be" + }, + { + "name": "builtin_factories_compile_on_first_require dies when compile matches the directory prefix loosely", + "why": "compile(id) is the only gate between a caller-shaped id and the embedded file table; a suffix match lets 'node:node/os' answer as a second public spelling of node:os, the shape of drift that would also let an override build read a file the exact match never names.", + "source": "pkg/runtime/globals.odin", + "find": "\t\tif dir.prefix != prefix do continue\n", + "replace": "\t\tif !strings.has_suffix(prefix, dir.prefix) do continue\n", + "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", + "expect_detail": "an id outside the embedded set resolved" } ] } diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 1f47139..708b674 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -13,7 +13,7 @@ }, "bootstrap/loader.js": { "method": 2, - "invoke": 1, + "invoke": 0, "accessor": 0, "global": 0 }, From 633cca377538ea6231dd95c551fca93a52439989 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:26:31 +0200 Subject: [PATCH 23/61] build(js): count module-eval global reads in lazy builtin modules A module-eval capture is pristine only when the loader instantiates the module before user code. The ratchet exempted every module-eval read, so a lazy module's top-level `var S = String` scored 0 although it runs on the user's first require (#333). The eager set is now derived from the loader's top-level req() roots closed over top-level require edges, by a graph builder shared with check-js-graph (scripts/lib/js-graph.mjs); module-eval reads in every other module are counted. The static eager set equals the 13 modules init compiles at runtime; cmd/lava/lazy_builtins_test.odin fails if a module listed eager is compiled on first require. Baseline raised (RAISE=--allow-raise): global 710 -> 758, all in lazy modules and all pre-existing sites; no eager module's counts changed. Fixing them is tracked separately. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 10 +- cmd/lava/lazy_builtins_test.odin | 24 ++++ docs/ARCHITECTURE.md | 6 + scripts/check-js-graph.mjs | 110 ++------------- scripts/check-js-graph.test.mjs | 51 ++++++- scripts/check-primordials.mjs | 16 ++- scripts/check-primordials.test.mjs | 2 +- scripts/lib/js-graph.mjs | 161 ++++++++++++++++++++++ scripts/lib/primordials-detect.mjs | 36 +++-- scripts/lib/primordials-fixtures.mjs | 39 +++++- tests/mutation-manifest.json | 43 +++++- tests/node-compat/pollution-baseline.json | 28 ++-- 12 files changed, 387 insertions(+), 139 deletions(-) create mode 100644 scripts/lib/js-graph.mjs diff --git a/CLAUDE.md b/CLAUDE.md index 1e1a9bb..5f36a17 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -224,10 +224,12 @@ Rules that keep this from becoming ceremony: **Module-eval capture is pristine only in a module the loader instantiates BEFORE user code** — the eager list in `bootstrap/loader.js`, plus `stream`/`stdio` via `installStdio`. A LAZY module's factory runs on the user's first `require()`, so a capture there is a live read - in disguise: `var StringG = String` at the top of `net.js` scores 0 and is still - steerable. Take the handle from the loader instead (`require('internal/primordials')`, - `require.pristineBuffer`). The ratchet cannot see this — it exempts every module-eval - capture, which is why #333 shipped. Computed and destructured forms count the same as the dot + in disguise — `var StringG = String` at the top of `net.js` is still steerable. Take the + handle from the loader instead (`require('internal/primordials')`, + `require.pristineBuffer`). The ratchet exempts module-eval reads only in that eager set, + which it derives from the loader's `req()` roots closed over top-level requires + (`scripts/lib/js-graph.mjs`); elsewhere they count as `global`. It used to exempt every + module-eval capture, which is why #333 shipped. Computed and destructured forms count the same as the dot form, so `view['buffer']` is not a way to lower a number. - A false positive takes `// primordials-ok` on the line. On a line carrying candidates from **more than one class** the bare marker suppresses nothing — diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index f2eebf4..726cad8 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -79,8 +79,32 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { })()` testing.expect(t, run_bool(ctx, UNKNOWN), "an id outside the embedded set resolved") testing.expect_value(t, state.builtin_compiles, at_init + 1) + + // The ratchet's static eager set (scripts/lib/js-graph.mjs eagerModules) against what + // init really compiled. Static ⊆ runtime is the property that matters: a module the + // ratchet calls eager but that is actually lazy has its module-eval captures exempted + // while they run after user code (#333). Each public id must already be compiled — + // its require compiles nothing; internal ids are not reachable from user code, so they + // are covered by the count: an equal-sized set with every node/ member present leaves + // a wrong internal member only if init also gained an unlisted one at the same time. + // ponytail: count-based for internal/ ids; record compiled ids on Runtime_State if + // that coincidence ever matters. check-js-graph.test.mjs keeps this list current. + for id in STATIC_EAGER { + if !strings.has_prefix(id, "node/") do continue + before := state.builtin_compiles + src, _ := strings.concatenate({"require('node:", id[len("node/"):], "') != null"}, context.temp_allocator) + testing.expectf(t, run_bool(ctx, src), "%s did not load", id) + testing.expectf(t, state.builtin_compiles == before, "%s is listed eager but compiled on first require", id) + } + // After the loop, so a lazy module wrongly listed is reported by name first. + testing.expect_value(t, at_init, len(STATIC_EAGER)) } +// The static eager set, sorted. Must equal eagerModules() — asserted by +// scripts/check-js-graph.test.mjs, which parses this literal. +@(private = "file") +STATIC_EAGER :: [?]string{"internal/abort", "internal/encoding", "internal/fetch", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} + @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { c_src := strings.clone_to_cstring(src, context.temp_allocator) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index dea62f3..9ce9370 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -447,6 +447,12 @@ pollutable site, each baselined separately in | `accessor` | `view.buffer` | a configurable prototype getter | | `global` | `String(x)` | a replaceable global, read live | +A `global` read at module-eval is exempt only in a module instantiated before user +code — the loader's `req()` roots closed over top-level requires, computed by +`scripts/lib/js-graph.mjs` (the parser `check-js-graph.mjs` also uses). In a lazy +module the factory runs on the user's first `require()`, so its capture counts (#333). +`cmd/lava/lazy_builtins_test.odin` checks that set against what context init compiles. + `make check-primordials` (part of `make check-js`) fails on any per-class increase, and `--update` refuses to raise a floor without `--allow-raise`. The detector self-tests against known-positive and known-negative fixtures on every diff --git a/scripts/check-js-graph.mjs b/scripts/check-js-graph.mjs index 656326f..47eec17 100644 --- a/scripts/check-js-graph.mjs +++ b/scripts/check-js-graph.mjs @@ -30,65 +30,11 @@ // // Usage: node scripts/check-js-graph.mjs (exit 1 on any violation) -import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'; -import { join, relative, sep } from 'node:path'; +import { readFileSync, readdirSync } from 'node:fs'; +import { join, relative } from 'node:path'; import { fileURLToPath } from 'node:url'; -import { parse } from 'acorn'; -import * as walk from 'acorn-walk'; -import { walkDir, ROOT } from './lib/primordials-detect.mjs'; - -const MODULE_DIRS = ['node', 'internal']; - -const posix = (p) => p.split(sep).join('/'); - -// The loader's specifier -> id rule (bootstrap/loader.js toId), restated so a require -// spelled for the public form (`buffer`, `node:buffer`, `assert/strict`) resolves the -// way it does at run time. -function toId(name) { - let key = name.startsWith('node:') ? name.slice(5) : name; - if (key === 'assert/strict') key = 'assert'; - return key.startsWith('internal/') ? key : 'node/' + key; -} - -function parseJs(src, file) { - return parse(src, { - ecmaVersion: 'latest', - sourceType: 'script', - locations: true, - allowReturnOutsideFunction: true, - sourceFile: file, - }); -} - -// Every call `(…)` in `ast`, with whether it runs at module-eval: its nearest -// enclosing function is the outermost one in the program. -function calls(ast, callee) { - const out = []; - walk.ancestor(ast, { - CallExpression(node, _state, ancestors) { - if (node.callee.type !== 'Identifier' || node.callee.name !== callee) return; - const fns = ancestors.filter( - (a) => - a !== node && - (a.type === 'FunctionExpression' || - a.type === 'FunctionDeclaration' || - a.type === 'ArrowFunctionExpression'), - ); - out.push({ node, line: node.loc.start.line, topLevel: fns.length <= 1 }); - }, - }); - return out; -} - -function literalArg(call) { - const arg = call.node.arguments[0]; - if (call.node.arguments.length !== 1 || !arg) return null; - if (arg.type === 'Literal' && typeof arg.value === 'string') return arg.value; - if (arg.type === 'TemplateLiteral' && arg.expressions.length === 0) { - return arg.quasis[0].value.cooked; - } - return null; -} +import { ROOT } from './lib/primordials-detect.mjs'; +import { MODULE_DIRS, moduleGraph, posix } from './lib/js-graph.mjs'; /** * Checks the embedded JS module graph rooted at `root` (a repo checkout, or a fixture @@ -98,7 +44,6 @@ function literalArg(call) { */ export function checkJsGraph(root) { const runtimeDir = join(root, 'pkg', 'runtime'); - const jsDir = join(runtimeDir, 'js'); const violations = []; const add = (rule, where, msg) => violations.push({ rule, where, msg }); const relJs = (file) => posix(relative(runtimeDir, file)); @@ -127,7 +72,10 @@ export function checkJsGraph(root) { nativeKeys.push({ id: m[1], where: `pkg/runtime/${name}` }); } } - const allJs = existsSync(jsDir) ? walkDir(jsDir).sort() : []; + // The parser and module table are shared with the pollution ratchet (lib/js-graph.mjs), + // which reads the same edges to decide which modules run before user code. + const graph = moduleGraph(root); + const { modules, allJs } = graph; for (const file of allJs) { const rel = relJs(file); const dir = rel.slice(0, rel.lastIndexOf('/')); @@ -140,49 +88,13 @@ export function checkJsGraph(root) { } } - // --- the module table: id -> file, and every require edge --------------------- - const modules = new Map(); // id -> { file, edges: [{id, line, topLevel}] } - for (const file of allJs) { - const id = relJs(file).slice('js/'.length, -'.js'.length); - if (!MODULE_DIRS.some((d) => id.startsWith(d + '/'))) continue; - modules.set(id, { file, edges: [] }); - } + // --- the module table: dynamic and unresolved requires found while building it --- + violations.push(...graph.violations); const where = (mod, line) => `pkg/runtime/${relJs(mod.file)}:${line}`; - for (const mod of modules.values()) { - const ast = parseJs(readFileSync(mod.file, 'utf8'), mod.file); - for (const call of calls(ast, 'require')) { - const spec = literalArg(call); - if (spec === null) { - add('dynamic', where(mod, call.line), 'require() argument is not a string literal'); - continue; - } - const id = toId(spec); - if (!modules.has(id)) { - add('unresolved', where(mod, call.line), `require('${spec}') -> ${id} has no file`); - continue; - } - mod.edges.push({ id, line: call.line, topLevel: call.topLevel }); - } - } // --- roots: every public module, plus whatever the loader asks for ------------- const roots = [...modules.keys()].filter((id) => id.startsWith('node/')); - const loaderFile = join(jsDir, 'bootstrap', 'loader.js'); - if (existsSync(loaderFile)) { - const ast = parseJs(readFileSync(loaderFile, 'utf8'), loaderFile); - for (const call of calls(ast, 'req')) { - const spec = literalArg(call); - if (spec === null) continue; // req(name) inside the resolver itself - const id = toId(spec); - if (!modules.has(id)) { - add( - 'unresolved', - `pkg/runtime/js/bootstrap/loader.js:${call.line}`, - `req('${spec}') -> ${id} has no file`, - ); - } else roots.push(id); - } - } + roots.push(...graph.loaderIds); for (const k of nativeKeys) { if (!modules.has(k.id)) add('unresolved', k.where, `natives key "${k.id}" names no module`); } diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 58f7920..0d739b8 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -8,10 +8,11 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { checkJsGraph } from './check-js-graph.mjs'; +import { eagerModules } from './lib/js-graph.mjs'; import { ROOT } from './lib/primordials-detect.mjs'; const ODIN = `package runtime @@ -64,6 +65,15 @@ function rules(overrides) { } } +function eager(overrides) { + const root = tree(overrides); + try { + return eagerModules(root); + } finally { + rmSync(root, { recursive: true, force: true }); + } +} + const only = (vs) => [...new Set(vs.map((v) => v.rule))].sort(); test('clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)', () => { @@ -138,3 +148,42 @@ test('cycle: two module-eval requires that close a loop', () => { /node\/stream -> node\/stream\/web -> node\/stream|node\/stream\/web -> node\/stream -> node\/stream\/web/, ); }); + +// --- the eager set the pollution ratchet reads (lib/js-graph.mjs eagerModules) --- +// +// A module-eval capture is pristine only in a module instantiated before user code, +// so the ratchet exempts module-eval reads in exactly these ids. Getting the set too +// LARGE is the bug direction: a lazy module called eager has its captures exempted. + +test('eager: closes over module-eval requires from the loader roots', () => { + // Roots are the loader's req() literals (primordials, fetch); buffer, stream and + // stream/web are eager only through fetch -> stream/web -> stream -> buffer. + assert.deepEqual(eager({}), [ + 'internal/fetch', + 'internal/primordials', + 'node/buffer', + 'node/stream', + 'node/stream/web', + ]); +}); + +test('eager: a function-body require does not make its target eager', () => { + const ids = eager({ + 'pkg/runtime/js/node/stream.js': + "(function (require, module) { require('buffer'); function late() { return require('os'); } module.exports = { late }; });", + 'pkg/runtime/js/node/os.js': '(function (require, module) { module.exports = {}; });', + }); + assert.ok(ids.includes('node/stream'), `stream is eager: ${ids}`); + assert.ok(!ids.includes('node/os'), `node/os is lazy, got eager: ${ids}`); +}); + +// The runtime side of the same set. cmd/lava/lazy_builtins_test.odin carries the list +// as STATIC_EAGER and checks it against what context init actually compiles; this keeps +// that list equal to what the ratchet computes, so neither can drift alone. +test('eager: the Odin cross-check list matches the computed set', () => { + const odin = readFileSync(join(ROOT, 'cmd', 'lava', 'lazy_builtins_test.odin'), 'utf8'); + const m = /STATIC_EAGER :: \[\?\]string\{([^}]*)\}/.exec(odin); + assert.ok(m, 'STATIC_EAGER not found in lazy_builtins_test.odin'); + const listed = [...m[1].matchAll(/"([^"]+)"/g)].map((x) => x[1]).sort(); + assert.deepEqual(listed, eagerModules(ROOT)); +}); diff --git a/scripts/check-primordials.mjs b/scripts/check-primordials.mjs index a075e01..19e4d40 100644 --- a/scripts/check-primordials.mjs +++ b/scripts/check-primordials.mjs @@ -12,7 +12,8 @@ import { readFileSync, writeFileSync } from 'node:fs'; import { relative, sep } from 'node:path'; -import { KINDS, countFile, walkDir, JS_DIR, BASELINE } from './lib/primordials-detect.mjs'; +import { KINDS, countFile, walkDir, JS_DIR, BASELINE, ROOT } from './lib/primordials-detect.mjs'; +import { eagerModules } from './lib/js-graph.mjs'; import { selfTest } from './lib/primordials-fixtures.mjs'; import { compare, raises, totals } from './lib/primordials-baseline.mjs'; @@ -33,12 +34,19 @@ if (selfTestFailures.length > 0) { process.exit(1); } +// Module-eval reads are exempt only where they run before user code: every +// bootstrap/ file (Odin evaluates each at context init) and the modules the loader +// instantiates at init, derived from the graph rather than listed (lib/js-graph.mjs). +// A lazy module's capture runs on the user's first require — #333. +const EAGER = new Set(eagerModules(ROOT)); +const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length)); + const files = walkDir(JS_DIR).sort(); const counts = {}; const allHits = {}; for (const file of files) { const key = relative(JS_DIR, file).split(sep).join('/'); - const { hits } = countFile(file); + const { hits } = countFile(file, { eager: isEager(key) }); // PER-CLASS counts, not one number. The classes differ by an order of // magnitude, and a single total would let a file trade a fixed accessor for a // new global and still pass. @@ -140,7 +148,9 @@ if (failed) { ' ReflectApply for .call/.apply)\n' + ' accessor -> read via a captured getter, e.g.\n' + ' TypedArrayPrototypeGetBuffer(view)\n' + - ' global -> capture at module-eval (`var StringG = String;`)\n' + + ' global -> capture at module-eval (`var StringG = String;`) in an\n' + + ' eager module; in a lazy one take the loader handle\n' + + " (require('internal/primordials'), require.pristineBuffer)\n" + 'A genuine false positive (the receiver is a class instance, not a built-in) takes\n' + '`// primordials-ok` on the same line. On a line carrying candidates from more\n' + 'than one class the bare marker suppresses nothing — name it:\n' + diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index abccac5..f106175 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -21,7 +21,7 @@ import { SELF_TEST, E } from './lib/primordials-fixtures.mjs'; test('detector fixtures', async (t) => { for (const fixture of SELF_TEST) { await t.test(fixture.name, () => { - const { hits } = countSource(fixture.src, fixture.name); + const { hits } = countSource(fixture.src, fixture.name, { eager: !fixture.lazy }); const actual = E(); for (const h of hits) actual[h.kind]++; // Exact per-class counts. An `includes`-style assertion passes while the diff --git a/scripts/lib/js-graph.mjs b/scripts/lib/js-graph.mjs new file mode 100644 index 0000000..c942455 --- /dev/null +++ b/scripts/lib/js-graph.mjs @@ -0,0 +1,161 @@ +// The embedded runtime JS module graph — ONE parser and graph builder, two consumers: +// +// scripts/check-js-graph.mjs literal, resolved, reachable, embedded, acyclic +// scripts/check-primordials.mjs which modules are EAGER (instantiated before user +// code), because only there is a module-eval +// capture pristine (CLAUDE.md §5) +// +// A module id is its path under pkg/runtime/js without `.js` (`node/buffer`, +// `internal/primordials`). An edge is TOP-LEVEL when the require runs at module-eval: +// its nearest enclosing function is the factory every module file evaluates to, +// `(function (require, module, exports, native) { … })`. A require inside a nested +// function is the sanctioned late binding and runs whenever that function is called. + +import { existsSync, readFileSync } from 'node:fs'; +import { join, relative, sep } from 'node:path'; +import { parse } from 'acorn'; +import * as walk from 'acorn-walk'; +import { walkDir } from './primordials-detect.mjs'; + +export const MODULE_DIRS = ['node', 'internal']; + +export const posix = (p) => p.split(sep).join('/'); + +// The loader's specifier -> id rule (bootstrap/loader.js toId), restated so a require +// spelled for the public form (`buffer`, `node:buffer`, `assert/strict`) resolves the +// way it does at run time. +export function toId(name) { + let key = name.startsWith('node:') ? name.slice(5) : name; + if (key === 'assert/strict') key = 'assert'; + return key.startsWith('internal/') ? key : 'node/' + key; +} + +function parseJs(src, file) { + return parse(src, { + ecmaVersion: 'latest', + sourceType: 'script', + locations: true, + allowReturnOutsideFunction: true, + sourceFile: file, + }); +} + +// Every call `(…)` in `ast`, with whether it runs at module-eval: its nearest +// enclosing function is the outermost one in the program. +function calls(ast, callee) { + const out = []; + walk.ancestor(ast, { + CallExpression(node, _state, ancestors) { + if (node.callee.type !== 'Identifier' || node.callee.name !== callee) return; + const fns = ancestors.filter( + (a) => + a !== node && + (a.type === 'FunctionExpression' || + a.type === 'FunctionDeclaration' || + a.type === 'ArrowFunctionExpression'), + ); + out.push({ node, line: node.loc.start.line, topLevel: fns.length <= 1 }); + }, + }); + return out; +} + +function literalArg(call) { + const arg = call.node.arguments[0]; + if (call.node.arguments.length !== 1 || !arg) return null; + if (arg.type === 'Literal' && typeof arg.value === 'string') return arg.value; + if (arg.type === 'TemplateLiteral' && arg.expressions.length === 0) { + return arg.quasis[0].value.cooked; + } + return null; +} + +/** + * Parses every module under js/node and js/internal and the loader's req() roots. + * @param {string} root a repo checkout, or a fixture tree with the same layout + * @returns {{ + * modules: Map, + * loaderIds: string[], + * violations: {rule: string, where: string, msg: string}[], + * allJs: string[], + * }} `violations` holds the `dynamic` and `unresolved` findings met while building. + */ +export function moduleGraph(root) { + const runtimeDir = join(root, 'pkg', 'runtime'); + const jsDir = join(runtimeDir, 'js'); + const violations = []; + const add = (rule, where, msg) => violations.push({ rule, where, msg }); + const relJs = (file) => posix(relative(runtimeDir, file)); + + const allJs = existsSync(jsDir) ? walkDir(jsDir).sort() : []; + const modules = new Map(); + for (const file of allJs) { + const id = relJs(file).slice('js/'.length, -'.js'.length); + if (!MODULE_DIRS.some((d) => id.startsWith(d + '/'))) continue; + modules.set(id, { file, edges: [] }); + } + const where = (mod, line) => `pkg/runtime/${relJs(mod.file)}:${line}`; + for (const mod of modules.values()) { + const ast = parseJs(readFileSync(mod.file, 'utf8'), mod.file); + for (const call of calls(ast, 'require')) { + const spec = literalArg(call); + if (spec === null) { + add('dynamic', where(mod, call.line), 'require() argument is not a string literal'); + continue; + } + const id = toId(spec); + if (!modules.has(id)) { + add('unresolved', where(mod, call.line), `require('${spec}') -> ${id} has no file`); + continue; + } + mod.edges.push({ id, line: call.line, topLevel: call.topLevel }); + } + } + + // Every literal req() in the loader. All of them run before user code: the loader's + // own body at init, and installStdio's `req('internal/stdio')` from install_stdio + // (globals.odin), also at init. A literal req() added somewhere that runs LATER + // would be wrongly treated as eager by eagerModules; the runtime cross-check in + // cmd/lava/lazy_builtins_test.odin is what catches that. + const loaderIds = []; + const loaderFile = join(jsDir, 'bootstrap', 'loader.js'); + if (existsSync(loaderFile)) { + const ast = parseJs(readFileSync(loaderFile, 'utf8'), loaderFile); + for (const call of calls(ast, 'req')) { + const spec = literalArg(call); + if (spec === null) continue; // req(name) inside the resolver itself + const id = toId(spec); + if (!modules.has(id)) { + add( + 'unresolved', + `pkg/runtime/js/bootstrap/loader.js:${call.line}`, + `req('${spec}') -> ${id} has no file`, + ); + } else loaderIds.push(id); + } + } + return { modules, loaderIds, violations, allJs }; +} + +/** + * The ids whose factory runs before user code: the loader's req() roots, closed over + * TOP-LEVEL require edges only. A require inside a function body runs when that + * function is called, which may be after user code, so it does not propagate. + * Static and conservative in the direction that matters for the ratchet: an id + * outside this set has its module-eval reads counted. + * @param {string | ReturnType} rootOrGraph + * @returns {string[]} sorted ids + */ +export function eagerModules(rootOrGraph) { + const { modules, loaderIds } = + typeof rootOrGraph === 'string' ? moduleGraph(rootOrGraph) : rootOrGraph; + const seen = new Set(); + const stack = [...loaderIds]; + while (stack.length > 0) { + const id = stack.pop(); + if (seen.has(id)) continue; + seen.add(id); + for (const e of modules.get(id).edges) if (e.topLevel) stack.push(e.id); + } + return [...seen].sort(); +} diff --git a/scripts/lib/primordials-detect.mjs b/scripts/lib/primordials-detect.mjs index 890a6fb..29554d5 100644 --- a/scripts/lib/primordials-detect.mjs +++ b/scripts/lib/primordials-detect.mjs @@ -197,8 +197,9 @@ const INVOKE = new Set(['call', 'apply']); // `arr.length` and make the ratchet unusable, which is its own failure mode. const ACCESSOR = new Set(['buffer', 'byteOffset', 'byteLength', 'constructor', '__proto__']); -// Globals a script can replace outright. Captured at module-eval, these are -// safe; read live inside a function body they are the `global` class. +// Globals a script can replace outright. Captured at module-eval of an EAGER +// module, these are safe; read live inside a function body, or anywhere in a lazy +// module, they are the `global` class. const GLOBALS = new Set([ 'String', 'Number', @@ -271,9 +272,9 @@ function functionDepth(ancestors) { return n; } -// A global read AT MODULE-EVAL is not a finding at all — the loader evaluates -// module bodies before any user code runs, so the binding it reads is still -// pristine. That is what makes a capture the fix, and it generalises: the shape +// A global read AT MODULE-EVAL of an EAGER module is not a finding at all — the +// loader evaluates those module bodies before any user code runs, so the binding it +// reads is still pristine. That is what makes a capture the fix, and it generalises: the shape // of the statement does not matter, only when it runs. `var S = String;`, // `module.exports = { Object: Object }` and even `var b = Buffer.from(x)` are all // safe at depth <= 1, and all three are live reads at depth >= 2. @@ -282,8 +283,15 @@ function functionDepth(ancestors) { // guessed wrong in both directions: it exempted a capture inside a function body // (a call-time read) and counted primordials.js's own export table, where every // module-level mention IS the capture. -function runsAtModuleEval(ancestors) { - return functionDepth(ancestors) <= 1; +// +// `eager` is the other half of "when does this run". A LAZY module's factory runs on +// the user's first require(), after user code could have replaced `String`, so its +// module-eval capture is the live read it looks like it avoids — #333 shipped that +// way (`var StringG = String` at the top of net.js scored 0). The caller decides +// eagerness from the module graph (lib/js-graph.mjs eagerModules); here it only +// turns the exemption off. +function exemptAsPristine(ancestors, eager) { + return eager && functionDepth(ancestors) <= 1; } // Does an enclosing function bind this name as a parameter? `function f(String)` @@ -302,10 +310,12 @@ function shadowedByParam(name, ancestors) { } // countSource parses `src` and reports every pollutable site, per class, with the -// line each sits on. Throws when the source does not parse — a scanner that +// line each sits on. `eager: false` scans it as a module instantiated after user +// code can run (see exemptAsPristine); the default is eager, which is what a bare +// fixture string pins. Throws when the source does not parse — a scanner that // silently reports 0 sites on a file it failed to read is the failure mode this // tool exists to prevent. -function countSource(src, label = '') { +function countSource(src, label = '', { eager = true } = {}) { const lineComments = new Map(); let ast; try { @@ -372,7 +382,7 @@ function countSource(src, label = '') { node.object.name === 'globalThis' && GLOBALS.has(name) ) { - if (!runsAtModuleEval(ancestors)) at(node, 'global', `globalThis.${name}`); + if (!exemptAsPristine(ancestors, eager)) at(node, 'global', `globalThis.${name}`); return; } @@ -441,7 +451,7 @@ function countSource(src, label = '') { } if (parent.type === 'ClassDeclaration' && parent.id === node) return; if (shadowedByParam(node.name, ancestors)) return; - if (runsAtModuleEval(ancestors)) return; + if (exemptAsPristine(ancestors, eager)) return; at(node, 'global', node.name); }, }); @@ -494,8 +504,8 @@ function walkDir(dir, out = []) { return out; } -function countFile(file) { - return countSource(readFileSync(file, 'utf8'), relative(ROOT, file)); +function countFile(file, opts) { + return countSource(readFileSync(file, 'utf8'), relative(ROOT, file), opts); } export { KINDS, countSource, countFile, walkDir, JS_DIR, BASELINE, ROOT }; diff --git a/scripts/lib/primordials-fixtures.mjs b/scripts/lib/primordials-fixtures.mjs index a019a92..1cc36df 100644 --- a/scripts/lib/primordials-fixtures.mjs +++ b/scripts/lib/primordials-fixtures.mjs @@ -160,6 +160,43 @@ const SELF_TEST = [ expect: E(), }, + // --- ...and only in a module the loader instantiates BEFORE user code --- + // `lazy: true` scans the source as a module outside the eager set (see + // lib/js-graph.mjs eagerModules): its factory runs on the user's first require, + // after user code could have replaced `String`, so the capture IS the live read. + // #333 shipped exactly this — `var StringG = String` at the top of net.js scored 0. + // Every fixture without the flag is scanned as eager, which is what they pinned + // before the distinction existed. + { + name: 'eager module: the same capture is pristine', + src: WRAP(' var StringG = String;'), + expect: E(), + }, + { + name: 'lazy module: module-eval capture is a live read', + src: WRAP(' var StringG = String;'), + lazy: true, + expect: E({ global: 1 }), + }, + { + name: 'lazy module: module-eval globalThis capture is a live read', + src: WRAP(' var StringG = globalThis.String;'), + lazy: true, + expect: E({ global: 1 }), + }, + // The FIX in a lazy module is the loader's handle, which is not a global read: + // the primordials table was captured before user code, and so was pristineBuffer. + { + name: 'lazy module: primordials and pristineBuffer are not global reads', + src: WRAP( + " var P = require('internal/primordials');\n" + + ' var { StringPrototypeTrim, String: S } = P;\n' + + ' var BufferG = require.pristineBuffer.Buffer;', + ), + lazy: true, + expect: E(), + }, + // --- positions that are not reads at all --- { name: 'object key is not a read', src: 'var t = { __proto__: null, String: 1 };', expect: E() }, { @@ -356,7 +393,7 @@ function selfTest() { for (const t of SELF_TEST) { let got; try { - got = countSource(t.src, t.name); + got = countSource(t.src, t.name, { eager: !t.lazy }); } catch (err) { failures.push(`${t.name}: ${err.message}`); continue; diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 3c36159..d00836a 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1941,7 +1941,7 @@ { "name": "check-js-graph fixtures die when a non-literal require is accepted", "why": "A computed require id is how an internal module gets reached by a name nobody can grep for, and it leaves the graph below incomplete.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph.mjs", "find": "add('dynamic', where(mod, call.line), 'require() argument is not a string literal');", "replace": "", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -1951,7 +1951,7 @@ { "name": "check-js-graph fixtures die when an unresolvable require is accepted", "why": "With #load_directory nothing checks a spelling at compile time: require('internal/mim') builds and returns undefined at run time.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph.mjs", "find": "add('unresolved', where(mod, call.line), `require('${spec}') -> ${id} has no file`);", "replace": "", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -2011,7 +2011,7 @@ { "name": "check-js-graph fixtures die when late-bound requires count as cycle edges", "why": "A require inside a function runs after both factories returned; counting it would forbid the sanctioned late binding stream <-> stream/web uses.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph.mjs", "find": "topLevel: fns.length <= 1", "replace": "topLevel: true", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -2035,6 +2035,43 @@ "replace": "\t\tif !strings.has_suffix(prefix, dir.prefix) do continue\n", "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", "expect_detail": "an id outside the embedded set resolved" + }, + { + "name": "the detector's fixtures die when a lazy module's module-eval capture is exempted again", + "why": "#333: a lazy module's factory runs on the user's first require, after user code could replace `String`, so exempting its top-level `var StringG = String` scores a live read as 0.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " return eager && functionDepth(ancestors) <= 1;", + "replace": " return functionDepth(ancestors) <= 1;", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "lazy module: module-eval capture is a live read" + }, + { + "name": "the ratchet dies when every module is scanned as eager", + "why": "Classifying a lazy module as eager re-exempts its module-eval captures; the raised baseline then reads as an improvement, which the ratchet refuses.", + "source": "scripts/check-primordials.mjs", + "find": "const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length));", + "replace": "const isEager = () => true;", + "gate": "make:check-primordials", + "expect_detail": "hardened by" + }, + { + "name": "check-js-graph fixtures die when a function-body require makes its target eager", + "why": "A require inside a function runs when that function is called, possibly after user code; propagating eagerness through it exempts a lazy module's captures.", + "source": "scripts/lib/js-graph.mjs", + "find": " for (const e of modules.get(id).edges) if (e.topLevel) stack.push(e.id);", + "replace": " for (const e of modules.get(id).edges) stack.push(e.id);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "eager: a function-body require does not make its target eager", + "expect_detail": "eager: a function-body require does not make its target eager" + }, + { + "name": "builtin_factories_compile_on_first_require dies when the ratchet's eager set names a lazy module", + "why": "Statically crypto stays a loader req() root and so eager to the ratchet, but at run time it now loads after user code; static must stay a subset of what init compiles.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " req('crypto');\n", + "replace": " publicReq.lateCrypto = function () {\n req('crypto');\n };\n", + "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", + "expect_detail": "is listed eager but compiled on first require" } ] } diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 708b674..73d8240 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -45,7 +45,7 @@ "method": 24, "invoke": 0, "accessor": 0, - "global": 5 + "global": 17 }, "internal/parse_args.js": { "method": 0, @@ -81,7 +81,7 @@ "method": 14, "invoke": 5, "accessor": 8, - "global": 40 + "global": 42 }, "node/buffer.js": { "method": 41, @@ -105,13 +105,13 @@ "method": 5, "invoke": 9, "accessor": 0, - "global": 13 + "global": 16 }, "node/dns.js": { "method": 10, "invoke": 0, "accessor": 0, - "global": 8 + "global": 10 }, "node/dns/promises.js": { "method": 0, @@ -135,25 +135,25 @@ "method": 21, "invoke": 5, "accessor": 0, - "global": 21 + "global": 27 }, "node/https.js": { "method": 0, "invoke": 0, "accessor": 0, - "global": 8 + "global": 9 }, "node/net.js": { "method": 2, "invoke": 2, "accessor": 3, - "global": 9 + "global": 12 }, "node/os.js": { "method": 12, "invoke": 0, "accessor": 1, - "global": 7 + "global": 10 }, "node/path.js": { "method": 161, @@ -183,19 +183,19 @@ "method": 24, "invoke": 0, "accessor": 0, - "global": 2 + "global": 4 }, "node/querystring.js": { "method": 6, "invoke": 1, "accessor": 0, - "global": 6 + "global": 7 }, "node/sqlite.js": { "method": 10, "invoke": 1, "accessor": 3, - "global": 20 + "global": 28 }, "node/stream.js": { "method": 42, @@ -237,7 +237,7 @@ "method": 2, "invoke": 2, "accessor": 0, - "global": 2 + "global": 4 }, "node/url.js": { "method": 46, @@ -249,12 +249,12 @@ "method": 46, "invoke": 14, "accessor": 15, - "global": 72 + "global": 74 }, "node/util/types.js": { "method": 0, "invoke": 1, "accessor": 0, - "global": 2 + "global": 3 } } From 02133315c2755f300b4240b3f84c4588df972629 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:28:43 +0200 Subject: [PATCH 24/61] harden(runtime): take lazy builtins' module-eval globals from primordials A lazy builtin's factory runs on the user's first require, so a global it read at module-eval (String, Symbol, Object, RegExp, Uint8Array, Error, ...) was whatever user code had put there. 46 of the 48 sites #427 made visible now take their handle from internal/primordials; punycode's two Math.floor / String.fromCharCode reads stay live because node's own punycode reads them live too. Probed per site against node 26: 40 were live divergences, 6 sat on the missing-bindings path node never reaches. sqlite's Symbol.dispose polyfill now lands on the real Symbol rather than on a user replacement. util.types uses a new caller0 ObjectPrototypeToStringFast: the safeGetter form cost 666 vs 516 ns per isDate+isUint8Array pair (medians of 7 interleaved launches), caller0 573 ns; the remaining +10% is accepted as the price of removing the lazy-time Object read. Pinned by tests/node-compat/cases/75-lazy-global-capture.js and 30 recorded mutations. Ratchet: global 758 -> 710, lowered only. Closes #426. Co-Authored-By: Claude Opus 5.5 --- pkg/runtime/js/internal/mime.js | 30 +- pkg/runtime/js/internal/primordials.js | 17 +- pkg/runtime/js/node/assert.js | 11 +- pkg/runtime/js/node/diagnostics_channel.js | 11 +- pkg/runtime/js/node/dns.js | 8 +- pkg/runtime/js/node/http.js | 21 +- pkg/runtime/js/node/https.js | 5 +- pkg/runtime/js/node/net.js | 11 +- pkg/runtime/js/node/os.js | 11 +- pkg/runtime/js/node/punycode.js | 8 +- pkg/runtime/js/node/querystring.js | 10 +- pkg/runtime/js/node/sqlite.js | 25 +- pkg/runtime/js/node/tty.js | 7 +- pkg/runtime/js/node/util.js | 16 +- pkg/runtime/js/node/util/types.js | 7 +- tests/mutation-manifest.json | 270 ++++++++++++++++++ .../cases/75-lazy-global-capture.js | 106 +++++++ tests/node-compat/pollution-baseline.json | 38 +-- 18 files changed, 528 insertions(+), 84 deletions(-) create mode 100644 tests/node-compat/cases/75-lazy-global-capture.js diff --git a/pkg/runtime/js/internal/mime.js b/pkg/runtime/js/internal/mime.js index 09b6ac9..66245a0 100644 --- a/pkg/runtime/js/internal/mime.js +++ b/pkg/runtime/js/internal/mime.js @@ -13,23 +13,25 @@ var StringPrototypeSlice = P.StringPrototypeSlice; var RegExpMatches = P.RegExpMatches; - var kData = Symbol('data'); // MIMEParams: the Map of name -> value - var kType = Symbol('type'); - var kSubtype = Symbol('subtype'); - var kParams = Symbol('params'); + // Taken off the primordials table, not the live globals: this module is LAZY (its factory + // runs on the user's first require of util), so a module-eval `Symbol(...)` here reads + // whatever user code left on globalThis — #426, pinned by + // tests/node-compat/cases/75-lazy-global-capture.js. + var SymbolG = P.Symbol; + var ObjectDefineProperties = P.ObjectDefineProperties; + + var kData = SymbolG('data'); // MIMEParams: the Map of name -> value + var kType = SymbolG('type'); + var kSubtype = SymbolG('subtype'); + var kParams = SymbolG('params'); // An HTTP token code point (RFC 9110): the chars allowed unquoted in a type/subtype/name. var NotHTTPTokenCodePoint = /[^!#$%&'*+\-.^_`|~A-Za-z0-9]/; // An HTTP quoted-string code point: tab, the printable ASCII range (U+0020..U+007E), and // the Latin-1 high range (U+0080..U+00FF). Anything else (other controls, newline) cannot - // appear even inside quotes. - var NotHTTPQuotedStringCodePoint = new RegExp( - String.fromCharCode(91, 94, 9) + - String.fromCharCode(32) + - String.fromCharCode(45, 126) + - String.fromCharCode(128) + - String.fromCharCode(45, 255, 93), - ); + // appear even inside quotes. A literal with escapes rather than the `new RegExp(String. + // fromCharCode(...))` it used to be: same class, and no read of a replaceable global. + var NotHTTPQuotedStringCodePoint = /[^\t\x20-\x7E\x80-\xFF]/; // Node's HTTP whitespace is tab, LF, CR and space — NOT form-feed (U+000C), unlike the // looser WHATWG set: a leading/trailing `\f` makes the type invalid rather than being // stripped, and a `\f` in a value keeps it (so the value fails the quoted-string check). @@ -263,7 +265,7 @@ return serializeParams(this[kData]); }; MIMEParams.prototype.toJSON = MIMEParams.prototype.toString; - MIMEParams.prototype[Symbol.iterator] = MIMEParams.prototype.entries; + MIMEParams.prototype[SymbolG.iterator] = MIMEParams.prototype.entries; function MIMEType(string) { if (new.target === undefined) { @@ -277,7 +279,7 @@ this[kParams] = params; } - Object.defineProperties(MIMEType.prototype, { + ObjectDefineProperties(MIMEType.prototype, { type: { configurable: true, enumerable: true, diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index 1fe01ed..013ae40 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -291,6 +291,10 @@ ObjectGetOwnPropertyDescriptor: Object.getOwnPropertyDescriptor, ObjectGetOwnPropertyDescriptors: Object.getOwnPropertyDescriptors, ObjectGetOwnPropertySymbols: Object.getOwnPropertySymbols, + // The static, not a wrapper: a direct call with no `.call` read, so it costs a per-key + // loop nothing. ObjectPrototypeHasOwnProperty (caller1) measured +5% on a 20-key + // querystring.parse (14.3 -> 15.0 us, medians of 7 interleaved launches, bin/lava darwin). + ObjectHasOwn: Object.hasOwn, // --- Array / Reflect / JSON / Math / Number statics --- ArrayIsArray: Array.isArray, @@ -312,9 +316,18 @@ // --- Object.prototype.* (call as fn(obj, ...)) --- ObjectPrototypeHasOwnProperty: caller1(ObjectProto.hasOwnProperty), - // safeGetter, not caller0: structured_clone keys its view-constructor table - // off this result, so a forged answer selects the wrong constructor. + ObjectPrototypePropertyIsEnumerable: caller1(ObjectProto.propertyIsEnumerable), + // safeGetter (Reflect.apply, immune to a forged Function.prototype.call), for a caller + // whose tag SELECTS something — a constructor, a memory window. No consumer today: + // structured_clone, the one it was written for, now brands by prototype identity + // (TypedArrayPrototypes below) and no longer reads the tag at all. ObjectPrototypeToString: safeGetter(ObjectProto.toString), + // caller0, for a hot per-call predicate where a forged tag can only answer the predicate + // wrongly (util/types.js). Measured isDate+isUint8Array pair, medians of 7 interleaved + // launches, 5e6 iterations, bin/lava darwin: 516 ns with the pre-#426 live `toString.call`, + // 666 via safeGetter, 573 via caller0. +10% accepted as the cost of dropping the + // lazy-time `Object` read. A dedicated non-shared wrapper measured the same (572). + ObjectPrototypeToStringFast: caller0(ObjectProto.toString), // --- Function.prototype.* --- FunctionPrototypeApply: caller2(apply), diff --git a/pkg/runtime/js/node/assert.js b/pkg/runtime/js/node/assert.js index 78ac1d5..e4a2056 100644 --- a/pkg/runtime/js/node/assert.js +++ b/pkg/runtime/js/node/assert.js @@ -101,15 +101,18 @@ } } - var hasOwn = Object.prototype.hasOwnProperty; - var isEnum = Object.prototype.propertyIsEnumerable; + // From primordials, not `Object.prototype.*` read at module-eval: assert is LAZY, so that + // read happens after user code could have replaced `Object` or the methods (#426). + var P = require('internal/primordials'); + var hasOwn = P.ObjectPrototypeHasOwnProperty; + var isEnum = P.ObjectPrototypePropertyIsEnumerable; // Own enumerable keys, strings then enumerable symbols (matching strict deep equality). function ownEnumKeys(obj) { var keys = Object.keys(obj); var syms = Object.getOwnPropertySymbols(obj); for (var i = 0; i < syms.length; i++) { - if (isEnum.call(obj, syms[i])) keys.push(syms[i]); + if (isEnum(obj, syms[i])) keys.push(syms[i]); } return keys; } @@ -237,7 +240,7 @@ for (var i = 0; i < keysE.length; i++) { var key = keysE[i]; if (Array.isArray(expected) && isArrayIndex(key)) continue; // matched as a subsequence - if (!hasOwn.call(actual, key) || !isEnum.call(actual, key)) return false; + if (!hasOwn(actual, key) || !isEnum(actual, key)) return false; if (!partialMatch(actual[key], expected[key], seen)) return false; } return true; diff --git a/pkg/runtime/js/node/diagnostics_channel.js b/pkg/runtime/js/node/diagnostics_channel.js index 6dfa87d..bd63006 100644 --- a/pkg/runtime/js/node/diagnostics_channel.js +++ b/pkg/runtime/js/node/diagnostics_channel.js @@ -4,9 +4,14 @@ (function (require, module) { 'use strict'; + // From primordials, not the live globals: this module is LAZY, so a module-eval read here + // runs after user code could have replaced `Map` or `Object` (#426). + var P = require('internal/primordials'); + var ObjectDefineProperty = P.ObjectDefineProperty; + // Node dedups channels via a WeakRef registry so unused ones can be GC'd; a Map is // functionally equivalent (it just keeps them alive), which is fine here. - var channels = new Map(); + var channels = new P.Map(); function reportError(err) { // A throwing subscriber must not break the publisher; surface it out-of-band. @@ -19,7 +24,7 @@ this.name = name; this._subscribers = []; } - Object.defineProperty(Channel.prototype, 'hasSubscribers', { + ObjectDefineProperty(Channel.prototype, 'hasSubscribers', { get: function () { return this._subscribers.length > 0; }, @@ -90,7 +95,7 @@ this.asyncEnd = group.asyncEnd; this.error = group.error; } - Object.defineProperty(TracingChannel.prototype, 'hasSubscribers', { + ObjectDefineProperty(TracingChannel.prototype, 'hasSubscribers', { get: function () { return ( this.start.hasSubscribers || diff --git a/pkg/runtime/js/node/dns.js b/pkg/runtime/js/node/dns.js index 6bbd104..07b606e 100644 --- a/pkg/runtime/js/node/dns.js +++ b/pkg/runtime/js/node/dns.js @@ -7,8 +7,12 @@ (function (require, module, exports, native) { 'use strict'; + // From primordials, not the live globals: this module is LAZY, so a + // module-eval read here runs after user code could have replaced the global (#426). + var P = require('internal/primordials'); + if (!native) { - throw new Error('node:dns is unavailable: native bindings missing'); + throw new P.Error('node:dns is unavailable: native bindings missing'); } // getaddrinfo hint flags. These mirror Node's numeric values, which come from @@ -305,7 +309,7 @@ exports.ADDRCONFIG = ADDRCONFIG; exports.V4MAPPED = V4MAPPED; exports.ALL = ALL; - Object.keys(CODES).forEach(function (k) { + P.ObjectKeys(CODES).forEach(function (k) { exports[k] = CODES[k]; }); TIER2.forEach(function (m) { diff --git a/pkg/runtime/js/node/http.js b/pkg/runtime/js/node/http.js index 31f2c59..bcf7f00 100644 --- a/pkg/runtime/js/node/http.js +++ b/pkg/runtime/js/node/http.js @@ -13,8 +13,15 @@ (function (require, module, exports, native) { 'use strict'; + /** Pristine intrinsics — response head must not use overridable Buffer methods. */ + // Also every module-eval constructor/static below: this module is LAZY, so a read of the + // live global here runs after user code could have replaced it (#426). + var primordials = require('internal/primordials'); + var ObjectCreate = primordials.ObjectCreate; + var ObjectDefineProperty = primordials.ObjectDefineProperty; + if (!native || typeof native.parseRequest !== 'function') { - throw new Error('node:http is unavailable on this platform'); + throw new primordials.Error('node:http is unavailable on this platform'); } var EventEmitter = require('events'); @@ -32,8 +39,6 @@ var BufferPrototypeIndexOf = PristineBuffer.bufferIndexOf; var BufferPrototypeSlice = PristineBuffer.bufferSlice; var net = require('net'); - /** Pristine intrinsics — response head must not use overridable Buffer methods. */ - var primordials = require('internal/primordials'); // Every regex below decides FRAMING over attacker-controlled bytes, so none of // them may go through `re.test(...)`: `RegExp.prototype.exec` is a writable data // property, and the spec's RegExpExec re-reads it off the receiver, so a plain @@ -133,7 +138,7 @@ * RFC 7230 tchar bitmap for field-names (setHeader + chunked trailers). * @type {Uint8Array} */ - var HTTP_TCHAR = new Uint8Array(128); + var HTTP_TCHAR = new primordials.Uint8Array(128); (function initHttpTchar() { var chars = "!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~"; for (var i = 0; i < chars.length; i++) HTTP_TCHAR[StringPrototypeCharCodeAt(chars, i)] = 1; @@ -367,12 +372,12 @@ this.complete = false; this._ended = false; } - IncomingMessage.prototype = Object.create(EventEmitter.prototype); + IncomingMessage.prototype = ObjectCreate(EventEmitter.prototype); IncomingMessage.prototype.constructor = IncomingMessage; // Lazy rawHeaders: first get materializes an own data property (Node shape: // hasOwnProperty / Object.keys include rawHeaders after access). - Object.defineProperty(IncomingMessage.prototype, 'rawHeaders', { + ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', { configurable: true, enumerable: true, get: function () { @@ -436,7 +441,7 @@ socket.on('drain', this._drainForwarder); } } - ServerResponse.prototype = Object.create(EventEmitter.prototype); + ServerResponse.prototype = ObjectCreate(EventEmitter.prototype); ServerResponse.prototype.constructor = ServerResponse; /** @@ -1103,7 +1108,7 @@ }); if (typeof requestListener === 'function') this.on('request', requestListener); } - Server.prototype = Object.create(EventEmitter.prototype); + Server.prototype = ObjectCreate(EventEmitter.prototype); Server.prototype.constructor = Server; Server.prototype.listen = function () { diff --git a/pkg/runtime/js/node/https.js b/pkg/runtime/js/node/https.js index d2a1371..7cb2ca6 100644 --- a/pkg/runtime/js/node/https.js +++ b/pkg/runtime/js/node/https.js @@ -11,8 +11,11 @@ (function (require, module, exports, native) { 'use strict'; + // The primordial, not the live global: this module is LAZY, so a module-eval read here + // runs after user code could have replaced the global (#426). + var P = require('internal/primordials'); if (!native || typeof native.createSecureContext !== 'function') { - throw new Error('node:https is unavailable on this platform'); + throw new P.Error('node:https is unavailable on this platform'); } var http = require('http'); diff --git a/pkg/runtime/js/node/net.js b/pkg/runtime/js/node/net.js index aa30751..d6ee6e8 100644 --- a/pkg/runtime/js/node/net.js +++ b/pkg/runtime/js/node/net.js @@ -8,8 +8,13 @@ (function (require, module, exports, native) { 'use strict'; + // From primordials, not the live globals: this module is LAZY, so a + // module-eval read here runs after user code could have replaced the global (#426). + var P = require('internal/primordials'); + var ObjectCreate = P.ObjectCreate; + if (!native || typeof native.listen !== 'function') { - throw new Error('node:net is unavailable on this platform'); + throw new P.Error('node:net is unavailable on this platform'); } var EventEmitter = require('events'); @@ -41,7 +46,7 @@ // TLSSocket is deferred — this is the one property the http layer actually needs. this.encrypted = false; } - Socket.prototype = Object.create(EventEmitter.prototype); + Socket.prototype = ObjectCreate(EventEmitter.prototype); Socket.prototype.constructor = Socket; Socket.prototype.write = function (data, encoding, cb) { @@ -123,7 +128,7 @@ this._tlsContext = options && typeof options === 'object' ? options.tls : undefined; if (typeof connectionListener === 'function') this.on('connection', connectionListener); } - Server.prototype = Object.create(EventEmitter.prototype); + Server.prototype = ObjectCreate(EventEmitter.prototype); Server.prototype.constructor = Server; // Native invokes this with the accepted connection's id. Register the native handlers diff --git a/pkg/runtime/js/node/os.js b/pkg/runtime/js/node/os.js index 2f9652e..16859b7 100644 --- a/pkg/runtime/js/node/os.js +++ b/pkg/runtime/js/node/os.js @@ -25,13 +25,20 @@ var BufferIsEncoding = PristineBuffer.isEncoding; if (!native) { - throw new Error('node:os is unavailable: native bindings missing'); + throw new OsPrimordials.Error('node:os is unavailable: native bindings missing'); } var isWindows = process.platform === 'win32'; // ---- endianness: probe the host byte order once (cheap, deterministic) ----- - var _endianness = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1 ? 'LE' : 'BE'; + // Intrinsics, not the live globals — this module is LAZY (#426). The getter rather than + // `.buffer` for the same reason: it is a configurable prototype accessor. + var _endianness = + new OsPrimordials.Uint8Array( + OsPrimordials.TypedArrayPrototypeGetBuffer(new OsPrimordials.Uint16Array([1])), + )[0] === 1 + ? 'LE' + : 'BE'; // ---- pure constants -------------------------------------------------------- var EOL = isWindows ? '\r\n' : '\n'; diff --git a/pkg/runtime/js/node/punycode.js b/pkg/runtime/js/node/punycode.js index ec8a9b5..ac86f99 100644 --- a/pkg/runtime/js/node/punycode.js +++ b/pkg/runtime/js/node/punycode.js @@ -47,8 +47,12 @@ } var baseMinusTMin = base - tMin; - var floor = Math.floor; - var stringFromCharCode = String.fromCharCode; + // Read live, on purpose, although this module is LAZY (#426): node's punycode is the + // userland punycode.js and does exactly this at its own module-eval, so a Math.floor or + // String.fromCharCode replaced before the first require steers node too (verified node + // 26.13.0, spy on both globals). Capturing the intrinsics would harden past the oracle. + var floor = Math.floor; // primordials-ok: global + var stringFromCharCode = String.fromCharCode; // primordials-ok: global // Node-exact error messages (the standalone punycode.js strings). var errors = { diff --git a/pkg/runtime/js/node/querystring.js b/pkg/runtime/js/node/querystring.js index ce54e48..eaa1f64 100644 --- a/pkg/runtime/js/node/querystring.js +++ b/pkg/runtime/js/node/querystring.js @@ -11,9 +11,11 @@ (function (require, module, exports) { 'use strict'; - var replaceCharAll = require('internal/primordials').replaceCharAll; - - var hasOwn = Object.prototype.hasOwnProperty; + var P = require('internal/primordials'); + var replaceCharAll = P.replaceCharAll; + // Not `Object.prototype.hasOwnProperty` read here: this module is LAZY, so its module-eval + // runs after user code could have replaced `Object` (#426). + var hasOwn = P.ObjectHasOwn; function escape(str) { return encodeURIComponent(String(str)); @@ -91,7 +93,7 @@ key = decodeComponent(key, decode); val = decodeComponent(val, decode); - if (!hasOwn.call(obj, key)) { + if (!hasOwn(obj, key)) { obj[key] = val; } else if (Array.isArray(obj[key])) { obj[key].push(val); diff --git a/pkg/runtime/js/node/sqlite.js b/pkg/runtime/js/node/sqlite.js index 131d962..5830d72 100644 --- a/pkg/runtime/js/node/sqlite.js +++ b/pkg/runtime/js/node/sqlite.js @@ -6,13 +6,16 @@ (function (require, module, exports, native) { 'use strict'; + // Handles for the code added with the node 24 buffer binding, and for every module-eval + // read below: this module is LAZY, so a read of the live global here runs after user code + // could have replaced it (#426). Function bodies still read their globals live — see the + // pollution baseline. + var P = require('internal/primordials'); + if (!native) { - throw new Error('node:sqlite is unavailable: Lava was built without libsqlite3'); + throw new P.Error('node:sqlite is unavailable: Lava was built without libsqlite3'); } - // Handles for the code added with the node 24 buffer binding. The rest of the file - // still reads its globals live — see its pollution baseline. - var P = require('internal/primordials'); var ArrayBufferGetByteLength = P.ArrayBufferPrototypeGetByteLength; var SharedArrayBufferGetByteLength = P.SharedArrayBufferPrototypeGetByteLength; var ObjectGetPrototypeOf = P.ObjectGetPrototypeOf; @@ -26,14 +29,20 @@ // methods below are reachable for manual cleanup now (`obj[Symbol.dispose]()`) // and participate in `using` automatically once the engine supports it. // Idempotent: a real engine/global polyfill takes precedence. - if (typeof Symbol === 'function' && !Symbol.dispose) { + // + // On the INTRINSIC Symbol, not the global binding: reading `Symbol` live wrote the + // polyfill onto whatever object user code had left there, and keyed the dispose + // methods off that object's property — so `db[Symbol.dispose]` was missing once the + // real Symbol was put back. + var SymbolG = P.Symbol; + if (!SymbolG.dispose) { try { - Symbol.dispose = Symbol('Symbol.dispose'); + SymbolG.dispose = SymbolG('Symbol.dispose'); } catch { // Symbol is non-extensible here; dispose-by-symbol is simply unavailable. } } - var disposeSymbol = typeof Symbol === 'function' ? Symbol.dispose : null; + var disposeSymbol = SymbolG.dispose || null; // GC backstops so that wrappers dropped without an explicit close/finalize still // release their native handle instead of leaking until context teardown. The @@ -376,7 +385,7 @@ if (dbFinalizers) dbFinalizers.register(this, this._id, this); } - Object.defineProperty(DatabaseSync.prototype, 'isOpen', { + P.ObjectDefineProperty(DatabaseSync.prototype, 'isOpen', { get: function () { return this._open; }, diff --git a/pkg/runtime/js/node/tty.js b/pkg/runtime/js/node/tty.js index 6549005..601f601 100644 --- a/pkg/runtime/js/node/tty.js +++ b/pkg/runtime/js/node/tty.js @@ -7,6 +7,9 @@ 'use strict'; var EventEmitter = require('events'); + // The primordial, not the live global: this module is LAZY, so a + // module-eval read here runs after user code could have replaced the global (#426). + var ObjectCreate = require('internal/primordials').ObjectCreate; var nativeIsatty = native && typeof native.isatty === 'function' ? native.isatty : null; function isatty(fd) { @@ -84,7 +87,7 @@ this.isTTY = isatty(fd); this.isRaw = false; } - ReadStream.prototype = Object.create(EventEmitter.prototype); + ReadStream.prototype = ObjectCreate(EventEmitter.prototype); ReadStream.prototype.constructor = ReadStream; ReadStream.prototype.setRawMode = function (mode) { this.isRaw = !!mode; @@ -100,7 +103,7 @@ this.columns = size[0]; this.rows = size[1]; } - WriteStream.prototype = Object.create(EventEmitter.prototype); + WriteStream.prototype = ObjectCreate(EventEmitter.prototype); WriteStream.prototype.constructor = WriteStream; WriteStream.prototype.getColorDepth = function (env) { return getColorDepth(env); diff --git a/pkg/runtime/js/node/util.js b/pkg/runtime/js/node/util.js index 3e44db4..20b55d1 100644 --- a/pkg/runtime/js/node/util.js +++ b/pkg/runtime/js/node/util.js @@ -259,7 +259,7 @@ // util.promisify — wrap a (…args, callback(err, value)) function as one returning a // Promise. Honors the util.promisify.custom symbol and copies the original's own // properties / prototype, matching Node (the multi-arg `customArgs` form is omitted). - var kCustomPromisify = Symbol.for('nodejs.util.promisify.custom'); + var kCustomPromisify = SymbolFor('nodejs.util.promisify.custom'); function promisify(original) { if (typeof original !== 'function') { @@ -646,13 +646,13 @@ // ST in either form (ESC \ = \, or the C1 byte œ) — Node accepts all // three, so we do too. Reproduced so the stripped output is byte-identical to Node's. // Built once; String.prototype.replace with a global regex resets lastIndex per call. - var ansiRegex = new RegExp( - '[\\u001B\\u009B][[\\]()#;?]*' + - '(?:(?:(?:(?:;[-a-zA-Z\\d/#&.:=?%@~_]+)*' + - '|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d/#&.:=?%@~_]*)*)?(?:\\u0007|\\u001B\\u005C|\\u009C))' + - '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))', - 'g', - ); + // A literal, not `new RegExp(...)`: util is LAZY, so a module-eval `RegExp` read here + // takes whatever user code left on globalThis (#426). Same pattern as Node's string + // form, its U+005C escape spelled as an escaped backslash (the same one character). + // Not `new P.RegExp` either: scripts/check-global-replace.mjs only recognises a bare + // `RegExp` callee or a literal, so that spelling hides the binding from that gate. + // prettier-ignore + var ansiRegex = /[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?(?:\u0007|\u001B\\|\u009C))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g; function stripVTControlCharacters(str) { if (typeof str !== 'string') { diff --git a/pkg/runtime/js/node/util/types.js b/pkg/runtime/js/node/util/types.js index 3c528fa..ca301f7 100644 --- a/pkg/runtime/js/node/util/types.js +++ b/pkg/runtime/js/node/util/types.js @@ -6,9 +6,12 @@ (function (require, module, exports) { 'use strict'; - var toString = Object.prototype.toString; + // The primordial, not `Object.prototype.toString` read at module-eval: this module is + // LAZY, so that read runs after user code could have replaced `Object` (#426). The caller0 + // form: a per-call predicate, where a forged tag only answers wrongly. + var ObjectPrototypeToString = require('internal/primordials').ObjectPrototypeToStringFast; function tag(v) { - return toString.call(v); // e.g. "[object Date]" + return ObjectPrototypeToString(v); // e.g. "[object Date]" } function tagged(v, name) { return tag(v) === '[object ' + name + ']'; diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index d00836a..2a605d9 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2072,6 +2072,276 @@ "replace": " publicReq.lateCrypto = function () {\n req('crypto');\n };\n", "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", "expect_detail": "is listed eager but compiled on first require" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js mints kData from the live Symbol", + "why": "`Symbol` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " var kData = SymbolG('data'); // MIMEParams: the Map of name -> value", + "replace": " var kData = Symbol('data'); // MIMEParams: the Map of name -> value", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol()\"]" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js mints kType from the live Symbol", + "why": "`Symbol` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " var kType = SymbolG('type');", + "replace": " var kType = Symbol('type');", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol()\"]" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js mints kSubtype from the live Symbol", + "why": "`Symbol` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " var kSubtype = SymbolG('subtype');", + "replace": " var kSubtype = Symbol('subtype');", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol()\"]" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js mints kParams from the live Symbol", + "why": "`Symbol` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " var kParams = SymbolG('params');", + "replace": " var kParams = Symbol('params');", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol()\"]" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js builds its quoted-string class through the live RegExp and String", + "why": "`RegExp` / `String.fromCharCode` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " var NotHTTPQuotedStringCodePoint = /[^\\t\\x20-\\x7E\\x80-\\xFF]/;", + "replace": " var NotHTTPQuotedStringCodePoint = new RegExp(String.fromCharCode(91, 94, 9) + String.fromCharCode(32) + String.fromCharCode(45, 126) + String.fromCharCode(128) + String.fromCharCode(45, 255, 93));", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "String.fromCharCode" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js keys MIMEParams iteration off the live Symbol", + "why": "`Symbol.iterator` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " MIMEParams.prototype[SymbolG.iterator] = MIMEParams.prototype.entries;", + "replace": " MIMEParams.prototype[Symbol.iterator] = MIMEParams.prototype.entries;", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol.iterator\"]" + }, + { + "name": "75-lazy-global-capture dies when internal/mime.js defines MIMEType accessors through the live Object", + "why": "`Object.defineProperties` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/internal/mime.js", + "find": " ObjectDefineProperties(MIMEType.prototype, {", + "replace": " Object.defineProperties(MIMEType.prototype, {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Object.defineProperties\"]" + }, + { + "name": "75-lazy-global-capture dies when util.js reads Symbol.for live for promisify.custom", + "why": "`Symbol.for` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/util.js", + "find": " var kCustomPromisify = SymbolFor('nodejs.util.promisify.custom');", + "replace": " var kCustomPromisify = Symbol.for('nodejs.util.promisify.custom');", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"Symbol.for\"]" + }, + { + "name": "75-lazy-global-capture dies when util.js builds the ANSI matcher through the live RegExp", + "why": "`RegExp` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/util.js", + "find": " var ansiRegex = /", + "replace": " var ansiRegex = new RegExp('(?:)') && /", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util: ok [\"new RegExp\"" + }, + { + "name": "75-lazy-global-capture dies when assert.js reads Object.prototype.hasOwnProperty live", + "why": "`Object.prototype.hasOwnProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/assert.js", + "find": " var hasOwn = P.ObjectPrototypeHasOwnProperty;", + "replace": " var hasOwnM = Object.prototype.hasOwnProperty; var hasOwn = function (o, k) { return hasOwnM.call(o, k); };", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "assert: ok [\"Object.prototype\"]" + }, + { + "name": "75-lazy-global-capture dies when assert.js reads Object.prototype.propertyIsEnumerable live", + "why": "`Object.prototype.propertyIsEnumerable` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/assert.js", + "find": " var isEnum = P.ObjectPrototypePropertyIsEnumerable;", + "replace": " var isEnumM = Object.prototype.propertyIsEnumerable; var isEnum = function (o, k) { return isEnumM.call(o, k); };", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "assert: ok [\"Object.prototype\"]" + }, + { + "name": "75-lazy-global-capture dies when diagnostics_channel.js builds its registry with the live Map", + "why": "`Map` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/diagnostics_channel.js", + "find": " var channels = new P.Map();", + "replace": " var channels = new Map();", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "diagnostics_channel: ok [\"new Map\"" + }, + { + "name": "75-lazy-global-capture dies when diagnostics_channel.js defines Channel#hasSubscribers through the live Object", + "why": "`Object.defineProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/diagnostics_channel.js", + "find": " ObjectDefineProperty(Channel.prototype, 'hasSubscribers', {", + "replace": " Object.defineProperty(Channel.prototype, 'hasSubscribers', {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "diagnostics_channel: ok [\"Object.defineProperty\"]" + }, + { + "name": "75-lazy-global-capture dies when diagnostics_channel.js defines TracingChannel#hasSubscribers through the live Object", + "why": "`Object.defineProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/diagnostics_channel.js", + "find": " ObjectDefineProperty(TracingChannel.prototype, 'hasSubscribers', {", + "replace": " Object.defineProperty(TracingChannel.prototype, 'hasSubscribers', {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "diagnostics_channel: ok [\"Object.defineProperty\"]" + }, + { + "name": "75-lazy-global-capture dies when dns.js enumerates its error codes through the live Object", + "why": "`Object.keys` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/dns.js", + "find": " P.ObjectKeys(CODES).forEach(function (k) {", + "replace": " Object.keys(CODES).forEach(function (k) {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "dns: ok [\"Object.keys\"]" + }, + { + "name": "75-lazy-global-capture dies when http.js allocates the tchar table with the live Uint8Array", + "why": "`Uint8Array` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/http.js", + "find": " var HTTP_TCHAR = new primordials.Uint8Array(128);", + "replace": " var HTTP_TCHAR = new Uint8Array(128);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "http: ok [\"new Uint8Array\"" + }, + { + "name": "75-lazy-global-capture dies when http.js builds IncomingMessage.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/http.js", + "find": " IncomingMessage.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " IncomingMessage.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "http: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when http.js defines IncomingMessage#rawHeaders through the live Object", + "why": "`Object.defineProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/http.js", + "find": " ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', {", + "replace": " Object.defineProperty(IncomingMessage.prototype, 'rawHeaders', {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "http: ok [\"Object.defineProperty\"]" + }, + { + "name": "75-lazy-global-capture dies when http.js builds ServerResponse.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/http.js", + "find": " ServerResponse.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " ServerResponse.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "http: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when http.js builds Server.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/http.js", + "find": " Server.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " Server.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "http: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when net.js builds Socket.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/net.js", + "find": " Socket.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " Socket.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "net: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when net.js builds Server.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/net.js", + "find": " Server.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " Server.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "net: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when os.js probes endianness through the live typed-array constructors", + "why": "`Uint16Array` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/os.js", + "find": " OsPrimordials.TypedArrayPrototypeGetBuffer(new OsPrimordials.Uint16Array([1])),", + "replace": " OsPrimordials.TypedArrayPrototypeGetBuffer(new Uint16Array([1])),", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "os: ok [\"new Uint16Array\"" + }, + { + "name": "75-lazy-global-capture dies when os.js wraps the endianness probe in the live Uint8Array", + "why": "`Uint8Array` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/os.js", + "find": " new OsPrimordials.Uint8Array(", + "replace": " new Uint8Array(", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "new Uint8Array" + }, + { + "name": "75-lazy-global-capture dies when querystring.js reads Object.prototype.hasOwnProperty live", + "why": "`Object.prototype.hasOwnProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/querystring.js", + "find": " var hasOwn = P.ObjectHasOwn;", + "replace": " var hasOwnM = Object.prototype.hasOwnProperty; var hasOwn = function (o, k) { return hasOwnM.call(o, k); };", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "querystring: ok [\"Object.prototype\"]" + }, + { + "name": "75-lazy-global-capture dies when sqlite.js polyfills Symbol.dispose onto the live Symbol", + "why": "`Symbol` (the polyfill target for Symbol.dispose) is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/sqlite.js", + "find": " var SymbolG = P.Symbol;", + "replace": " var SymbolG = Symbol;", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "Symbol.dispose=" + }, + { + "name": "75-lazy-global-capture dies when sqlite.js defines DatabaseSync#isOpen through the live Object", + "why": "`Object.defineProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/sqlite.js", + "find": " P.ObjectDefineProperty(DatabaseSync.prototype, 'isOpen', {", + "replace": " Object.defineProperty(DatabaseSync.prototype, 'isOpen', {", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "node:sqlite: ok [\"Object.defineProperty\"]" + }, + { + "name": "75-lazy-global-capture dies when tty.js builds ReadStream.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/tty.js", + "find": " ReadStream.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " ReadStream.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "tty: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when tty.js builds WriteStream.prototype with the live Object", + "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/tty.js", + "find": " WriteStream.prototype = ObjectCreate(EventEmitter.prototype);", + "replace": " WriteStream.prototype = Object.create(EventEmitter.prototype);", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "tty: ok [\"Object.create\"]" + }, + { + "name": "75-lazy-global-capture dies when util/types.js reads Object.prototype.toString live", + "why": "`Object.prototype.toString` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", + "source": "pkg/runtime/js/node/util/types.js", + "find": " var ObjectPrototypeToString = require('internal/primordials').ObjectPrototypeToStringFast;", + "replace": " var toStringM = Object.prototype.toString; var ObjectPrototypeToString = function (v) { return toStringM.call(v); };", + "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", + "expect_detail": "util/types: ok [\"Object.prototype\"]" } ] } diff --git a/tests/node-compat/cases/75-lazy-global-capture.js b/tests/node-compat/cases/75-lazy-global-capture.js new file mode 100644 index 0000000..aad8e79 --- /dev/null +++ b/tests/node-compat/cases/75-lazy-global-capture.js @@ -0,0 +1,106 @@ +// A lazy builtin must not read a REPLACED global while it evaluates. +// +// CLAUDE.md §5: a module-eval capture is pristine only in a module the loader instantiates +// before user code. Every module required below is LAZY — its factory runs on the user's +// first require(), so `Object.create(...)`, `Symbol('x')`, `new Map()` at its top level +// read whatever user code has left on globalThis by then (issue #426). Node's builtins +// take these from primordials and are immune; measured on node 26.13.0, every log below +// is empty. Before the fix, Lava's were not: +// +// util (+ internal/mime) Symbol(), Symbol.for, Symbol.iterator, new RegExp, +// String.fromCharCode, Object.defineProperties, Object.prototype +// assert Object.prototype +// diagnostics_channel new Map, Object.defineProperty +// dns Object.keys +// os new Uint8Array, new Uint16Array +// querystring, util/types Object.prototype +// sqlite Symbol.dispose, Symbol(), and a WRITE of Symbol.dispose — the +// JSC polyfill went onto whatever object held the name +// tty Object.create +// http, https, net Object.create / Object.defineProperty / new Uint8Array +// (Linux; on darwin the missing-bindings `new Error` is reached +// instead, which node never has) +// +// A spy that forwards everything is enough to see the read: what the builtin does with +// the value is up to whoever installed the replacement. punycode is deliberately absent — +// node's own punycode reads Math.floor and String.fromCharCode live too. +const NAMES = ['Symbol', 'RegExp', 'String', 'Object', 'Map', 'Error', 'Uint8Array', 'Uint16Array']; +const RA = Reflect.apply; +const RC = Reflect.construct; +const RG = Reflect.get; +const RS = Reflect.set; +const reals = {}; +let log = []; +let armed = false; + +// NOTHING under test may be required above this line: a module loaded before the swap +// captures the real globals whichever way it resolves them, and the check below passes +// with the fix reverted (the trap 65-lazy-buffer-capture.js documents). +for (const G of NAMES) { + const real = globalThis[G]; + reals[G] = real; + globalThis[G] = new Proxy(real, { + get(t, k) { + if (armed) log.push(G + '.' + (typeof k === 'symbol' ? '@@' + k.description : k)); + return RG(t, k); + }, + set(t, k, v) { + if (armed) log.push(G + '.' + (typeof k === 'symbol' ? '@@' + k.description : k) + '='); + return RS(t, k, v); + }, + apply(t, self, args) { + if (armed) log.push(G + '()'); + return RA(t, self, args); + }, + construct(t, args, nt) { + if (armed) log.push('new ' + G); + return RC(t, args, nt); + }, + }); +} + +function loadUnderSpies(specifier) { + log = []; + armed = true; + let threw = null; + try { + require(specifier); + } catch (err) { + threw = err; + } + armed = false; + return { log, threw }; +} + +// Prove the recorder can fire BEFORE trusting an empty log — an empty-list check is worthless +// if nothing could ever append to it. +armed = true; +globalThis.Object.keys({}); +armed = false; +console.log('recorder live:', log.length === 1 && log[0] === 'Object.keys'); + +const results = []; +// Leaves first: util requires util/types and internal/mime, assert requires util, http +// requires net — a module already loaded by a dependency logs nothing of its own. +for (const m of [ + 'util/types', + 'util', + 'assert', + 'diagnostics_channel', + 'dns', + 'os', + 'querystring', + 'node:sqlite', + 'tty', + 'net', + 'http', + 'https', +]) { + results.push([m, loadUnderSpies(m)]); +} + +for (const G of NAMES) globalThis[G] = reals[G]; + +for (const [m, r] of results) { + console.log(m + ':', r.threw ? 'THREW ' + r.threw.message : 'ok', JSON.stringify(r.log)); +} diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 73d8240..6284531 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -45,7 +45,7 @@ "method": 24, "invoke": 0, "accessor": 0, - "global": 17 + "global": 5 }, "internal/parse_args.js": { "method": 0, @@ -79,9 +79,9 @@ }, "node/assert.js": { "method": 14, - "invoke": 5, + "invoke": 2, "accessor": 8, - "global": 42 + "global": 40 }, "node/buffer.js": { "method": 41, @@ -105,13 +105,13 @@ "method": 5, "invoke": 9, "accessor": 0, - "global": 16 + "global": 13 }, "node/dns.js": { - "method": 10, + "method": 9, "invoke": 0, "accessor": 0, - "global": 10 + "global": 8 }, "node/dns/promises.js": { "method": 0, @@ -135,25 +135,25 @@ "method": 21, "invoke": 5, "accessor": 0, - "global": 27 + "global": 21 }, "node/https.js": { "method": 0, "invoke": 0, "accessor": 0, - "global": 9 + "global": 8 }, "node/net.js": { "method": 2, "invoke": 2, "accessor": 3, - "global": 12 + "global": 9 }, "node/os.js": { "method": 12, "invoke": 0, - "accessor": 1, - "global": 10 + "accessor": 0, + "global": 7 }, "node/path.js": { "method": 161, @@ -183,19 +183,19 @@ "method": 24, "invoke": 0, "accessor": 0, - "global": 4 + "global": 2 }, "node/querystring.js": { "method": 6, - "invoke": 1, + "invoke": 0, "accessor": 0, - "global": 7 + "global": 6 }, "node/sqlite.js": { "method": 10, "invoke": 1, "accessor": 3, - "global": 28 + "global": 20 }, "node/stream.js": { "method": 42, @@ -237,7 +237,7 @@ "method": 2, "invoke": 2, "accessor": 0, - "global": 4 + "global": 2 }, "node/url.js": { "method": 46, @@ -249,12 +249,12 @@ "method": 46, "invoke": 14, "accessor": 15, - "global": 74 + "global": 72 }, "node/util/types.js": { "method": 0, - "invoke": 1, + "invoke": 0, "accessor": 0, - "global": 3 + "global": 2 } } From 567333e019490a848abea25b4fbf8f24852e28d6 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:34:53 +0200 Subject: [PATCH 25/61] fix(util): guard the promisify.custom SymbolFor call like the inspect one An unguarded module-eval SymbolFor(...) at util.js made the 57-format-single-arg mutant (SymbolFor: undefined) throw inside util's factory, so it died with exit 1 instead of the recorded rendering. Guarded on the same capture as customInspect; the #426 mutation entry for that line takes the new find. Co-Authored-By: Claude Opus 5.5 --- pkg/runtime/js/node/util.js | 5 ++++- tests/mutation-manifest.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/runtime/js/node/util.js b/pkg/runtime/js/node/util.js index 20b55d1..0d7e2e5 100644 --- a/pkg/runtime/js/node/util.js +++ b/pkg/runtime/js/node/util.js @@ -259,7 +259,10 @@ // util.promisify — wrap a (…args, callback(err, value)) function as one returning a // Promise. Honors the util.promisify.custom symbol and copies the original's own // properties / prototype, matching Node (the multi-arg `customArgs` form is omitted). - var kCustomPromisify = SymbolFor('nodejs.util.promisify.custom'); + // Guarded like customInspect above, on the same capture: an unguarded call here made a + // missing SymbolFor throw inside util's factory and masked what the 57-format-single-arg + // mutation pins (the inspect hook rendering a Buffer). + var kCustomPromisify = SymbolFor ? SymbolFor('nodejs.util.promisify.custom') : null; function promisify(original) { if (typeof original !== 'function') { diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 2a605d9..81fadf4 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2140,7 +2140,7 @@ "name": "75-lazy-global-capture dies when util.js reads Symbol.for live for promisify.custom", "why": "`Symbol.for` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", "source": "pkg/runtime/js/node/util.js", - "find": " var kCustomPromisify = SymbolFor('nodejs.util.promisify.custom');", + "find": " var kCustomPromisify = SymbolFor ? SymbolFor('nodejs.util.promisify.custom') : null;", "replace": " var kCustomPromisify = Symbol.for('nodejs.util.promisify.custom');", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "util: ok [\"Symbol.for\"]" From c4f90b6b220c876a1712bddf1530b324f80ff378 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:28:12 +0200 Subject: [PATCH 26/61] refactor(runtime): build every builtin coded error through internal/errors About a dozen builtins carried their own copy of the ERR_INVALID_ARG_TYPE / ERR_OUT_OF_RANGE builders and the "Received ..." describer, and the copies had drifted: path, util, events and dns printed only typeof; crypto dropped string values and the _ grouping; url printed 'type object ()'; and every copy built node's C++ shape, so String(e) lacked [ERR_X]. internal/errors.js now holds node's determineSpecificType, the ERR_* constructors in use, and nodeError / nativeError for node's JS and C++ shapes. Each consumer's local copy is gone. Where a copy disagreed with node, the shared helper follows node; every such line is pinned by tests/node-compat/cases/76-coded-error-shape.js (123 lines differed on base, 0 now). internal/errors is eager (init compiles 13 -> 14): noop.js +0.1-0.3 ms, medians of 50 interleaved spawn-to-exit runs, darwin. Ratchet lowered only: 1517 -> 1422 sites; errors.js is 0 in every class. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 3 +- cmd/lava/lazy_builtins_test.odin | 4 +- docs/ARCHITECTURE.md | 13 +- pkg/runtime/js/internal/encoding.js | 71 ++- pkg/runtime/js/internal/errors.js | 403 ++++++++++++++++++ pkg/runtime/js/internal/mime.js | 7 +- pkg/runtime/js/internal/parse_args.js | 29 +- pkg/runtime/js/internal/parse_env.js | 26 +- pkg/runtime/js/node/buffer.js | 244 +++-------- pkg/runtime/js/node/crypto.js | 130 +++--- pkg/runtime/js/node/dns.js | 78 +--- pkg/runtime/js/node/events.js | 53 +-- pkg/runtime/js/node/fs.js | 91 +--- pkg/runtime/js/node/http.js | 19 +- pkg/runtime/js/node/https.js | 45 +- pkg/runtime/js/node/net.js | 8 +- pkg/runtime/js/node/os.js | 27 +- pkg/runtime/js/node/path.js | 33 +- pkg/runtime/js/node/sqlite.js | 55 +-- pkg/runtime/js/node/stream.js | 87 ++-- pkg/runtime/js/node/string_decoder.js | 4 +- pkg/runtime/js/node/url.js | 66 +-- pkg/runtime/js/node/util.js | 59 +-- tests/mutation-manifest.json | 114 +++-- .../node-compat/cases/76-coded-error-shape.js | 294 +++++++++++++ tests/node-compat/pollution-baseline.json | 56 +-- 26 files changed, 1217 insertions(+), 802 deletions(-) create mode 100644 pkg/runtime/js/internal/errors.js create mode 100644 tests/node-compat/cases/76-coded-error-shape.js diff --git a/CLAUDE.md b/CLAUDE.md index 5f36a17..909c984 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -280,7 +280,8 @@ Rules that keep this from becoming ceremony: - Nothing transient lands on `globalThis`; natives arrive as the factory's fourth argument. - Errors are Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`) - with Node's exact message template. + with Node's exact message template. Build them through `require('internal/errors')` + (`ERR_*`, and `nodeError`/`nativeError` for node's JS/C++ shapes), never a local copy. - Native byte ops should stay behind a size threshold so small inputs skip FFI overhead. NOTE: the constant this rule used to name (`NATIVE_BYTEOP_MIN`) does not exist — `bytesToString` crosses to a native codec at every size. Treat the rule as diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 726cad8..90270a3 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -59,7 +59,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { ) // node:os is outside the eager closure and its only requires (primordials, - // buffer through require.pristineBuffer) are eager, so its first require + // internal/errors, buffer through require.pristineBuffer) are eager, so its first require // compiles exactly one factory: its own. testing.expect(t, run_bool(ctx, "typeof require('node:os').platform() === 'string'"), "node:os did not load") testing.expect_value(t, state.builtin_compiles, at_init + 1) @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/encoding", "internal/fetch", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/encoding", "internal/errors", "internal/fetch", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 9ce9370..71259a9 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -180,7 +180,13 @@ instantiates them during bootstrap. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), `stream/web.js` (2027, Web Streams), `buffer.js` (1646), `fetch.js` (1054), `crypto.js` (917). Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, -`ERR_OUT_OF_RANGE`, …). Native byte ops are gated by a size threshold +`ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) +holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, +`ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — +`nodeError` (node's JS layer: `String(e)` is `TypeError [ERR_X]: msg`) and `nativeError` +(node's C++ layer: `TypeError: msg`). A builtin requires it rather than keeping a local +copy; per-module message templates call `nodeError`/`nativeError` with their own text. +Native byte ops are gated by a size threshold so small inputs avoid FFI overhead — except that the threshold this sentence used to name (`NATIVE_BYTEOP_MIN`) does not exist in the tree: `bytesToString` dispatches to a native codec at every size. Removed rather than left as folklore steering review. @@ -304,8 +310,9 @@ base Error with an overridden name only when the constructor is unreachable. `make_js_error` / `make_js_named_error` and the typed `ERR_*` helpers (`err_out_of_range`, `err_invalid_arg_type`, …) all route through it; the ad-hoc "base Error + manual `name`/`code` patch" sites (process.exit, sqlite, module -resolution) were migrated. The JS layer still sets `err.code` per module; growing -the `ERR_*` helper list lets native sites adopt the same codes as they need them. +resolution) were migrated. The JS layer builds its coded errors through +`js/internal/errors.js` (§3.3); growing the `ERR_*` helper list lets native sites adopt +the same codes as they need them. --- diff --git a/pkg/runtime/js/internal/encoding.js b/pkg/runtime/js/internal/encoding.js index aab2c1b..dc19679 100644 --- a/pkg/runtime/js/internal/encoding.js +++ b/pkg/runtime/js/internal/encoding.js @@ -178,34 +178,11 @@ ]; } - // Node's ERR_INVALID_ARG_TYPE "Received …" tail. buffer.js carries the same - // shape as a file-private helper, and there is no shared errors module to - // host it — exporting it from buffer.js would leak into the surface of - // require('buffer'), so this stays a deliberate second, minimal copy. - function describeReceived(v) { - if (v === null) return 'null'; - var t = typeof v; - if (t === 'undefined') return 'undefined'; - if (t === 'string') { - var s = v.length > 28 ? StringPrototypeSlice(v, 0, 25) + '...' : v; - return "type string ('" + s + "')"; - } - // -0 renders as "-0" in Node (util.inspect); string concatenation erases the - // sign. `1/v < 0` rather than ObjectIs, matching the buffer.js/crypto.js - // copies of this helper — one idiom across all three. - if (t === 'number') return 'type number (' + (v === 0 && 1 / v < 0 ? '-0' : v) + ')'; - if (t === 'boolean') return 'type boolean (' + v + ')'; - if (t === 'bigint') return 'type bigint (' + v + 'n)'; - if (t === 'symbol') return 'type symbol (' + v.toString() + ')'; - var ctor = v.constructor && v.constructor.name; - return 'an instance of ' + (ctor || 'Object'); - } - - function errInvalidArgType(message) { - var e = new TypeErrorG(message); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } + // Node-shaped coded errors come from internal/errors (CLAUDE.md §5). + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var nativeError = errors.nativeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; // Node's validateObject(value, 'options', kValidateObjectAllowObjects) as it is // called from TextDecoder — nullable, and arrays and functions allowed. Verified @@ -225,9 +202,7 @@ if (options === undefined || options === null) return; var t = typeof options; if (t === 'object' || t === 'function') return; - throw errInvalidArgType( - 'The "options" argument must be of type object. Received ' + describeReceived(options), - ); + throw ERR_INVALID_ARG_TYPE('options', 'object', options); } // NOT `instanceof`, on any of the three arms. `instanceof` dispatches through @@ -267,11 +242,13 @@ TypedArrayPrototypeGetByteLength(input), ); } - // Node names this argument "list" and appends no "Received" tail here, - // unlike its other ERR_INVALID_ARG_TYPE messages. SharedArrayBuffer is in - // the text because Node's is; Lava does not implement it, so that arm is - // unreachable and the wording is parity-only. - throw errInvalidArgType( + // Node names this argument "list" and appends no "Received" tail here: it is + // thrown by the C++ encoding binding, which is also why String(e) carries no + // [code]. SharedArrayBuffer is in the text because Node's is; Lava does not + // implement it, so that arm is unreachable and the wording is parity-only. + throw nativeError( + TypeErrorG, + 'ERR_INVALID_ARG_TYPE', 'The "list" argument must be an instance of SharedArrayBuffer, ArrayBuffer or ArrayBufferView.', ); } @@ -315,10 +292,7 @@ } TextEncoder.prototype.encodeInto = function (source, dest) { - if (!(dest instanceof Uint8ArrayG)) - throw errInvalidArgType( - 'The "dest" argument must be an instance of Uint8Array. Received ' + describeReceived(dest), - ); + if (!(dest instanceof Uint8ArrayG)) throw ERR_INVALID_ARG_TYPE('dest', 'Uint8Array', dest); source = StringG(source); var read = 0, written = 0, @@ -378,9 +352,12 @@ } function fatalErr(enc) { - var e = new TypeErrorG('The encoded data was not valid for encoding ' + enc); - e.code = 'ERR_ENCODING_INVALID_ENCODED_DATA'; - return e; + // C++-thrown in node (the decoder binding): no [code] in String(e). + return nativeError( + TypeErrorG, + 'ERR_ENCODING_INVALID_ENCODED_DATA', + 'The encoded data was not valid for encoding ' + enc, + ); } // The code-unit accumulator is a PREALLOCATED Uint16Array written by index, @@ -614,9 +591,11 @@ if (options !== undefined && options !== null) validateOptions(options); var enc = normalizeLabel(text); if (enc === undefined) { - var labelErr = new RangeErrorG('The "' + text + '" encoding is not supported'); - labelErr.code = 'ERR_ENCODING_NOT_SUPPORTED'; - throw labelErr; + throw nodeError( + RangeErrorG, + 'ERR_ENCODING_NOT_SUPPORTED', + 'The "' + text + '" encoding is not supported', + ); } // An omitted `options` must not inherit fatal/ignoreBOM from a poisoned // Object.prototype, hence the shared null-prototype kEmptyObject. An options diff --git a/pkg/runtime/js/internal/errors.js b/pkg/runtime/js/internal/errors.js new file mode 100644 index 0000000..6d88286 --- /dev/null +++ b/pkg/runtime/js/internal/errors.js @@ -0,0 +1,403 @@ +// internal/errors — the ONE place a builtin builds a Node-shaped coded error. +// +// Builtins used to carry private copies of node's ERR_* constructors — a dozen spellings +// of the "Received …" describer alone, across buffer, encoding, crypto, os, fs, util, +// parse_env, events, url, dns, path and stream. They drifted from node +// AND from each other — path printed "Received type number" with no value, crypto +// "type string" with none either and no `_` grouping on large integers, dns +// "type undefined (undefined)", url "type object ()" — and each fix landed in one copy +// while the rest kept the bug. The file splits that follow this module would have copied +// them again. A builtin requires this module instead; a local copy is a review reject +// (CLAUDE.md §5). +// +// TWO SHAPES, because node has two and String(e) tells them apart (node 26.13.0, probed +// through each consumer's public API — tests/node-compat/cases/76-coded-error-shape.js): +// nodeError node's JS layer (`new ERR_X(...)` in lib/internal/errors.js). The code is +// spliced into String(e) by an inherited toString: +// `TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument …`. +// nativeError node's C++ layer (THROW_ERR_X — URL parsing, the encoding binding, +// sqlite, timingSafeEqual). A bare TypeError/RangeError/Error with a `code` +// own property; String(e) is `TypeError: Invalid URL`. +// Two constructors for one idea would be the smell CLAUDE.md warns about if the idea +// were one; here the observable surface differs, so a consumer must pick the shape its +// node counterpart throws, and the name says which. +// +// Hardened from day one: every intrinsic comes from primordials, captured at module-eval. +// This module is EAGER (buffer, url and crypto require it at their top level, and the +// loader instantiates those before user code), so those captures are pristine; the +// exports table is frozen, so a lazy consumer capturing a function from it gets the +// same pristine function. +(function (require, module) { + 'use strict'; + + var P = require('internal/primordials'); + var TypeErrorP = P.TypeError; + var RangeErrorP = P.RangeError; + var StringP = P.String; + var ObjectCreate = P.ObjectCreate; + var ObjectFreeze = P.ObjectFreeze; + var ObjectSetPrototypeOf = P.ObjectSetPrototypeOf; + var ArrayIsArray = P.ArrayIsArray; + var ArrayPrototypeIndexOf = P.ArrayPrototypeIndexOf; + var ArrayPrototypeJoin = P.ArrayPrototypeJoin; + var ArrayPrototypePop = P.ArrayPrototypePop; + var ArrayPrototypePush1 = P.ArrayPrototypePush1; + var ArrayPrototypeSplice = P.ArrayPrototypeSplice; + var JSONStringify = P.JSONStringify; + var NumberIsInteger = P.NumberIsInteger; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeEndsWith = P.StringPrototypeEndsWith; + var StringPrototypeIndexOf = P.StringPrototypeIndexOf; + var StringPrototypeSlice = P.StringPrototypeSlice; + var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; + + // util.inspect, for the three places node renders a value through it: a constructor-less + // object in determineSpecificType, every ERR_INVALID_ARG_VALUE, and an ERR_OUT_OF_RANGE + // received value that is not a plain number. Looked up on first use — util is LAZY, + // and making it eager would put its whole factory on every script's startup for text + // that is only ever built when a call is already failing. The residual: the first + // failing call reads `require('util').inspect` after user code ran, so a replaced + // inspect steers MESSAGE TEXT — nothing else flows through it. + var inspectFn = null; + function inspect(value, opts) { + if (inspectFn === null) inspectFn = require('util').inspect; + return inspectFn(value, opts); + } + + // One prototype per (Base, code), holding node's toString. node builds a subclass per + // code (makeNodeErrorWithCode) whose toString closes over the key; the observable parts + // are kept — `e.name`, `e.constructor === TypeError`, `instanceof`, no own `toString`, + // and String(e) reading the CURRENT name/message but the ORIGINAL code — while node's + // second prototype level and its kIsNodeError symbol are not, as nothing outside node's + // own internals reads either. + var protoTables = []; + function protoFor(Base, code) { + var table = null; + for (var i = 0; i < protoTables.length; i += 2) { + if (protoTables[i] === Base) { + table = protoTables[i + 1]; + break; + } + } + if (table === null) { + table = { __proto__: null }; + ArrayPrototypePush1(protoTables, Base); + ArrayPrototypePush1(protoTables, table); + } + var proto = table[code]; + if (proto === undefined) { + proto = ObjectCreate(Base.prototype, { + toString: { + value: function toString() { + return this.name + ' [' + code + ']: ' + this.message; + }, + writable: true, + enumerable: false, + configurable: true, + }, + }); + table[code] = proto; + } + return proto; + } + + /** + * Builds an error the way node's JS layer does (`new ERR_X(...)`). + * @param {Function} Base P.Error / P.TypeError / P.RangeError from primordials. + * @param {string} code + * @param {string} message + * @returns {Error} `code` is an enumerable own data property, `message` a + * non-enumerable one, and String(e) is `${name} [${code}]: ${message}`. + * @node `String(e)` for Buffer.alloc('x') is `TypeError [ERR_INVALID_ARG_TYPE]: …`, + * Object.keys(e) is ['code'], e.constructor === TypeError (node 26.13.0, case 76). + * @deviates Own property names are JSC's (message, code, line, column, sourceURL, + * stack) rather than V8's (stack, code, message), and `e.stack` carries no + * `TypeError [CODE]: msg` header line — both are engine traits shared by every + * Error in Lava, not something an error constructor can change. + */ + function nodeError(Base, code, message) { + var e = new Base(message); + ObjectSetPrototypeOf(e, protoFor(Base, code)); + e.code = code; + return e; + } + + /** + * Builds an error the way node's C++ layer does (THROW_ERR_X): no code in String(e). + * @param {Function} Base + * @param {string} code + * @param {string} message + * @returns {Error} + * @node `new URL('nope')` stringifies as `TypeError: Invalid URL` with code + * ERR_INVALID_URL; sqlite and timingSafeEqual likewise (node 26.13.0, case 76). + * @deviates none + */ + function nativeError(Base, code, message) { + var e = new Base(message); + e.code = code; + return e; + } + + /** + * Node's determineSpecificType: the "Received …" tail of ERR_INVALID_ARG_TYPE. + * @param {*} value + * @returns {string} + * @node null/undefined bare; `type number (-0)`; `type bigint (5n)`; + * `type symbol (Symbol(s))`; `function named` (`function ` for an anonymous one, + * a class is a function); `an instance of Foo`; a string cut to 25 chars + '...' + * when longer than 28, quoted with ' unless it contains one, then JSON-quoted. + * Every branch is pinned by case 76 (node 26.13.0). + * @deviates A null-prototype object goes through util.inspect as node's does, and + * Lava's inspect renders it `{}` where node prints `[Object: null prototype] {}` + * (#331) — an inspect gap, not one of this function's. + */ + function determineSpecificType(value) { + if (value === null) return 'null'; + if (value === undefined) return 'undefined'; + var type = typeof value; + if (type === 'string') { + if (value.length > 28) value = StringPrototypeSlice(value, 0, 25) + '...'; + if (StringPrototypeIndexOf(value, "'", 0) === -1) return "type string ('" + value + "')"; + return 'type string (' + JSONStringify(value) + ')'; + } + // -0: string concatenation erases the sign, node prints it. + if (type === 'number') + return 'type number (' + (value === 0 && 1 / value < 0 ? '-0' : value) + ')'; + if (type === 'bigint') return 'type bigint (' + value + 'n)'; + if (type === 'boolean') return 'type boolean (' + value + ')'; + if (type === 'symbol') return 'type symbol (' + StringP(value) + ')'; + if (type === 'function') return 'function ' + value.name; + // node reads the RECEIVER's `.constructor.name` for this text, so reading it is what + // parity requires — a captured getter would answer for the wrong object. A forged + // constructor changes the text of an error already being thrown, nothing else. + var ctor = value.constructor; // primordials-ok: accessor + if (ctor && 'name' in ctor) return 'an instance of ' + ctor.name; + return inspect(value, { depth: -1 }); + } + + // node's classRegExp, /^([A-Z][a-z0-9]*)+$/, as a code-unit loop (CLAUDE.md §5: no + // RegExp where a character class will do). The pattern accepts exactly an ASCII + // alphanumeric string whose first unit is an upper-case letter. + function isClassName(s) { + var n = s.length; + if (n === 0) return false; + var c = StringPrototypeCharCodeAt(s, 0); + if (c < 0x41 || c > 0x5a) return false; + for (var i = 1; i < n; i++) { + c = StringPrototypeCharCodeAt(s, i); + if (!((c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a))) { + return false; + } + } + return true; + } + + var kTypes = [ + 'string', + 'function', + 'number', + 'object', + 'Function', + 'Object', + 'boolean', + 'bigint', + 'symbol', + ]; + + // ', ' join with ', or ' for three and more — node's list form, shared by the + // three groups below. + function listOf(items, prefix) { + if (items.length > 2) { + var last = ArrayPrototypePop(items); + return prefix + ArrayPrototypeJoin(items, ', ') + ', or ' + last; + } + return null; + } + + /** + * ERR_INVALID_ARG_TYPE (TypeError, JS shape). + * @param {string} name Argument name; `x.y` reads "property", `first argument` is + * spliced bare, anything else is quoted as an "argument". + * @param {string|string[]} expected node's list: type names (`'number'`), class names + * (`'Buffer'`) and free text (`'Array-like Object'`), in any mix — never a prebuilt + * phrase such as 'of type number'. + * @param {*} actual Rendered by determineSpecificType. + * @returns {TypeError} + * @node lib/internal/errors.js's template, case for case: `of type string or an + * instance of Buffer, TypedArray, or DataView`, `one of type string or object`, + * `an instance of Buffer or Uint8Array`, `an Array-like Object` (node 26.13.0, + * case 76 through buffer/fs/crypto/stream/url). + * @deviates none + */ + function ERR_INVALID_ARG_TYPE(name, expected, actual) { + if (!ArrayIsArray(expected)) expected = [expected]; + var msg = 'The '; + if (StringPrototypeEndsWith(name, ' argument')) msg += name + ' '; + else + msg += + '"' + + name + + '" ' + + (StringPrototypeIndexOf(name, '.', 0) !== -1 ? 'property ' : 'argument '); + msg += 'must be '; + + var types = []; + var instances = []; + var other = []; + for (var i = 0; i < expected.length; i++) { + var value = expected[i]; + if (ArrayPrototypeIndexOf(kTypes, value) !== -1) + ArrayPrototypePush1(types, StringPrototypeToLowerCase(value)); + else if (isClassName(value)) ArrayPrototypePush1(instances, value); + else ArrayPrototypePush1(other, value); + } + // `object` next to class names is spelled as the class `Object`, so the sentence + // contrasts instances with instances. + if (instances.length > 0) { + var pos = ArrayPrototypeIndexOf(types, 'object'); + if (pos !== -1) { + ArrayPrototypeSplice(types, pos, 1); + ArrayPrototypePush1(instances, 'Object'); + } + } + + if (types.length > 0) { + msg += + listOf(types, 'one of type ') || + (types.length === 2 + ? 'one of type ' + types[0] + ' or ' + types[1] + : 'of type ' + types[0]); + if (instances.length > 0 || other.length > 0) msg += ' or '; + } + if (instances.length > 0) { + msg += + listOf(instances, 'an instance of ') || + 'an instance of ' + instances[0] + (instances.length === 2 ? ' or ' + instances[1] : ''); + if (other.length > 0) msg += ' or '; + } + if (other.length > 0) { + var others = listOf(other, 'one of '); + if (others !== null) msg += others; + else if (other.length === 2) msg += 'one of ' + other[0] + ' or ' + other[1]; + else msg += (StringPrototypeToLowerCase(other[0]) !== other[0] ? 'an ' : '') + other[0]; + } + return nodeError( + TypeErrorP, + 'ERR_INVALID_ARG_TYPE', + msg + '. Received ' + determineSpecificType(actual), + ); + } + + /** + * ERR_INVALID_ARG_VALUE (TypeError, JS shape). + * @param {string} name `x.y` reads "property", anything else "argument". + * @param {*} value Rendered by util.inspect, cut to 128 chars + '...'. + * @param {string} [reason='is invalid'] + * @returns {TypeError} + * @node `The argument 'hints' is invalid. Received 999999`, `The property + * 'options.family' must be one of: 0, 4, 6. Received '4'` (node 26.13.0, case 76). + * @deviates none + */ + function ERR_INVALID_ARG_VALUE(name, value, reason) { + var inspected = inspect(value); + if (inspected.length > 128) inspected = StringPrototypeSlice(inspected, 0, 128) + '...'; + var kind = StringPrototypeIndexOf(name, '.', 0) !== -1 ? 'property' : 'argument'; + return nodeError( + TypeErrorP, + 'ERR_INVALID_ARG_VALUE', + 'The ' + + kind + + " '" + + name + + "' " + + (reason === undefined ? 'is invalid' : reason) + + '. Received ' + + inspected, + ); + } + + // node's addNumericalSeparator: `_` every three digits from the right, the sign kept + // out of the grouping. Applied to String(input) verbatim, so an exponent form is + // grouped as text exactly as node groups it. + function addNumericalSeparator(val) { + var res = ''; + var i = val.length; + var start = StringPrototypeCharCodeAt(val, 0) === 0x2d ? 1 : 0; + for (; i >= start + 4; i -= 3) res = '_' + StringPrototypeSlice(val, i - 3, i) + res; + return StringPrototypeSlice(val, 0, i) + res; + } + + /** + * ERR_OUT_OF_RANGE (RangeError, JS shape). + * @param {string} name + * @param {string} range Spliced after "It must be ". + * @param {*} input + * @returns {RangeError} + * @node An integer whose magnitude exceeds 2 ** 32 is `_`-grouped + * (`1_099_511_627_776`), a bigint past ±2n ** 32n too and always suffixed `n`; + * anything else is util.inspect'd (`1.5`, `-0`). node 26.13.0, case 76 through + * buffer/crypto/os. + * @deviates none + */ + function ERR_OUT_OF_RANGE(name, range, input) { + var received; + if (NumberIsInteger(input) && (input > 4294967296 || input < -4294967296)) { + received = addNumericalSeparator(StringP(input)); + } else if (typeof input === 'bigint') { + received = StringP(input); + if (input > 4294967296n || input < -4294967296n) received = addNumericalSeparator(received); + received += 'n'; + } else if (typeof input === 'number') { + // inspect's answer for a number, without loading util on the common path. + received = input === 0 && 1 / input < 0 ? '-0' : StringP(input); + } else { + received = inspect(input); + } + return nodeError( + RangeErrorP, + 'ERR_OUT_OF_RANGE', + 'The value of "' + name + '" is out of range. It must be ' + range + '. Received ' + received, + ); + } + + /** + * ERR_MISSING_ARGS (TypeError, JS shape) for one missing argument — the only form any + * builtin raises today; node's multi-name forms are not reproduced until one does. + * @param {string} name + * @returns {TypeError} + * @node `The "streams" argument must be specified` (pipeline), `The "input" argument + * must be specified` (atob) — node 26.13.0, case 76. + * @deviates none + */ + function ERR_MISSING_ARGS(name) { + return nodeError( + TypeErrorP, + 'ERR_MISSING_ARGS', + 'The "' + name + '" argument must be specified', + ); + } + + /** + * ERR_UNKNOWN_ENCODING (TypeError, JS shape). + * @param {*} encoding Concatenated as-is, as node's template does. + * @returns {TypeError} + * @node `Unknown encoding: bogus` for buf.toString, stream write and StringDecoder + * (node 26.13.0, case 76). + * @deviates none + */ + function ERR_UNKNOWN_ENCODING(encoding) { + return nodeError(TypeErrorP, 'ERR_UNKNOWN_ENCODING', 'Unknown encoding: ' + encoding); + } + + module.exports = ObjectFreeze({ + __proto__: null, + nodeError: nodeError, + nativeError: nativeError, + determineSpecificType: determineSpecificType, + ERR_INVALID_ARG_TYPE: ERR_INVALID_ARG_TYPE, + ERR_INVALID_ARG_VALUE: ERR_INVALID_ARG_VALUE, + ERR_OUT_OF_RANGE: ERR_OUT_OF_RANGE, + ERR_MISSING_ARGS: ERR_MISSING_ARGS, + ERR_UNKNOWN_ENCODING: ERR_UNKNOWN_ENCODING, + }); +}); diff --git a/pkg/runtime/js/internal/mime.js b/pkg/runtime/js/internal/mime.js index 66245a0..f855c2e 100644 --- a/pkg/runtime/js/internal/mime.js +++ b/pkg/runtime/js/internal/mime.js @@ -61,12 +61,13 @@ return from === 0 ? str : out + StringPrototypeSlice(str, from); } + var nodeError = require('internal/errors').nodeError; + var TypeErrorP = P.TypeError; + function mimeError(production, input, index) { var msg = 'The MIME syntax for a ' + production + ' in "' + input + '" is invalid'; if (index !== undefined && index !== -1) msg += ' at ' + index; - var e = new TypeError(msg); - e.code = 'ERR_INVALID_MIME_SYNTAX'; - return e; + return nodeError(TypeErrorP, 'ERR_INVALID_MIME_SYNTAX', msg); } // Validate a token production (type/subtype/parameter name). `input` is the string shown diff --git a/pkg/runtime/js/internal/parse_args.js b/pkg/runtime/js/internal/parse_args.js index 0846b26..8f48ccc 100644 --- a/pkg/runtime/js/internal/parse_args.js +++ b/pkg/runtime/js/internal/parse_args.js @@ -11,7 +11,7 @@ 'use strict'; var P = require('internal/primordials'); - var TypeError = P.TypeError; + var TypeErrorP = P.TypeError; var ObjectKeys = P.ObjectKeys; var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; var ArrayPrototypePush = P.ArrayPrototypePush; @@ -27,11 +27,8 @@ return ObjectPrototypeHasOwnProperty(obj, key) ? obj[key] : undefined; } - function codedError(code, message) { - var e = new TypeError(message); - e.code = code; - return e; - } + var errors = require('internal/errors'); + var nodeError = errors.nodeError; function findLongOptionForShort(shortOption, options) { var keys = ObjectKeys(options); @@ -209,17 +206,23 @@ function checkOptionUsage(token, options) { if (!ObjectPrototypeHasOwnProperty(options, token.name)) { - throw codedError('ERR_PARSE_ARGS_UNKNOWN_OPTION', "Unknown option '" + token.rawName + "'"); + throw nodeError( + TypeErrorP, + 'ERR_PARSE_ARGS_UNKNOWN_OPTION', + "Unknown option '" + token.rawName + "'", + ); } var spec = options[token.name]; if (spec.type === 'string' && typeof token.value !== 'string') { - throw codedError( + throw nodeError( + TypeErrorP, 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE', "Option '" + token.rawName + " ' argument missing", ); } if (spec.type === 'boolean' && token.value !== undefined) { - throw codedError( + throw nodeError( + TypeErrorP, 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE', "Option '" + token.rawName + "' does not take an argument", ); @@ -228,7 +231,8 @@ function checkOptionLikeValue(token) { if (!token.inlineValue && isOptionLikeValue(token.value)) { - throw codedError( + throw nodeError( + TypeErrorP, 'ERR_PARSE_ARGS_INVALID_OPTION_VALUE', "Option '" + token.rawName + @@ -268,7 +272,7 @@ var options = objectGetOwn(config, 'options') || {}; if (typeof options !== 'object' || options === null) { - throw codedError('ERR_INVALID_ARG_TYPE', 'The "options" argument must be of type object'); + throw errors.ERR_INVALID_ARG_TYPE('options', 'object', options); } var tokens = argsToTokens(args, options); @@ -286,7 +290,8 @@ storeOption(token.name, token.value, options, result.values); } else if (token.kind === 'positional') { if (!allowPositionals) { - throw codedError( + throw nodeError( + TypeErrorP, 'ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL', "Unexpected argument '" + token.value + diff --git a/pkg/runtime/js/internal/parse_env.js b/pkg/runtime/js/internal/parse_env.js index 23484b3..e384807 100644 --- a/pkg/runtime/js/internal/parse_env.js +++ b/pkg/runtime/js/internal/parse_env.js @@ -18,27 +18,8 @@ var StringPrototypeTrim = P.StringPrototypeTrim; var ObjectKeys = P.ObjectKeys; var ArrayPrototypeSort = P.ArrayPrototypeSort; - var TypeError = P.TypeError; - function codedError(code, message) { - var e = new TypeError(message); - e.code = code; - return e; - } - - // Node's ERR_INVALID_ARG_TYPE "Received ..." clause for a non-string argument: a - // primitive shows `type ()`, null/undefined show as-is, and an object - // shows `an instance of ` (e.g. Buffer). - function received(v) { - if (v === null) return 'null'; - var t = typeof v; - if (t === 'undefined') return 'undefined'; - if (t === 'object') { - var name = v.constructor && v.constructor.name ? v.constructor.name : 'Object'; - return 'an instance of ' + name; - } - return 'type ' + t + ' (' + String(v) + ')'; - } + var ERR_INVALID_ARG_TYPE = require('internal/errors').ERR_INVALID_ARG_TYPE; var CR = 13; var LF = 10; @@ -109,10 +90,7 @@ function parseEnv(content) { // Node requires a string and throws ERR_INVALID_ARG_TYPE otherwise (Buffers included). if (typeof content !== 'string') { - throw codedError( - 'ERR_INVALID_ARG_TYPE', - 'The "content" argument must be of type string. Received ' + received(content), - ); + throw ERR_INVALID_ARG_TYPE('content', 'string', content); } content = stripCR(content); var tmp = { __proto__: null }; diff --git a/pkg/runtime/js/node/buffer.js b/pkg/runtime/js/node/buffer.js index 1b5c9f2..4fd7520 100644 --- a/pkg/runtime/js/node/buffer.js +++ b/pkg/runtime/js/node/buffer.js @@ -100,107 +100,23 @@ var customInspectSymbol = typeof Symbol !== 'undefined' && Symbol.for ? Symbol.for('nodejs.util.inspect.custom') : null; - function describeType(value) { - if (value === null) return 'null'; - var t = typeof value; - if (t === 'undefined') return 'undefined'; - if (t === 'string') { - var s = value.length > 28 ? value.slice(0, 25) + '...' : value; - return "type string ('" + s + "')"; - } - // -0 renders as "-0" in Node (it routes through util.inspect); string - // concatenation erases the sign. 1/value avoids a dependency on Object.is. - if (t === 'number') - return 'type number (' + (value === 0 && 1 / value < 0 ? '-0' : value) + ')'; - if (t === 'boolean') return 'type boolean (' + value + ')'; - if (t === 'bigint') return 'type bigint (' + value + 'n)'; - if (t === 'symbol') return 'type symbol (' + value.toString() + ')'; - if (t === 'function') return 'function ' + (value.name || '(anonymous)'); - var ctor = value.constructor && value.constructor.name; - return 'an instance of ' + (ctor || 'Object'); - } - - function numericSeparator(value) { - var str; - if (typeof value === 'bigint') str = value.toString(); - else if (typeof value !== 'number') return StringG(value); - else if (!isFinite(value) || Math.floor(value) !== value) return StringG(value); - else str = StringG(value); - var neg = str.charAt(0) === '-'; - if (neg) str = str.slice(1); - var out = ''; - var i = str.length; - for (; i > 3; i -= 3) out = '_' + str.slice(i - 3, i) + out; - return (neg ? '-' : '') + str.slice(0, i) + out; - } - - /** - * Builds node's ERR_INVALID_ARG_TYPE for a named argument. - * @param {string} name Argument name, quoted into the message. - * @param {string} expected Everything after "must be", verb phrase included: - * 'of type number', 'an instance of Array'. It is spliced verbatim, so a bare - * type name ('number') yields `must be number` — a sentence node never prints. - * @param {*} actual The rejected value, rendered by describeType. - * @returns {TypeError & {code: string}} - * @node `The "size" argument must be of type number. Received type string ('5')` - * for Buffer.alloc('5'); the same template for "offset" (readUInt8('5'), - * writeUInt16LE(0, '1')) and "length" (Buffer.concat([a], '5')). Node 26.13.0, - * verified by tests/node-compat/cases/70-buffer-size-inspect-parity.js. - * @deviates none - */ - function errInvalidArgType(name, expected, actual) { - var e = new TypeError( - 'The "' + name + '" argument must be ' + expected + '. Received ' + describeType(actual), - ); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } - - function errInvalidFromArg(value) { - var e = new TypeError( - 'The first argument must be of type string or an instance of Buffer, ArrayBuffer, ' + - 'or Array or an Array-like Object. Received ' + - describeType(value), - ); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } - - /** @returns {RangeError & {code: string}} */ - function errOutOfRange(name, range, received) { - var rendered; - if (typeof received === 'bigint') { - var bigWide = received > BigInt('4294967296') || received < -BigInt('4294967296'); - rendered = (bigWide ? numericSeparator(received) : received.toString()) + 'n'; - } else if (typeof received === 'number' && (received > 4294967296 || received < -4294967296)) { - rendered = numericSeparator(received); - } else { - rendered = StringG(received); - } - var e = new RangeError( - 'The value of "' + name + '" is out of range. It must be ' + range + '. Received ' + rendered, - ); - e.code = 'ERR_OUT_OF_RANGE'; - return e; - } - - function errInvalidArgValue(name, value) { - var rendered; - if (typeof value === 'string') rendered = "'" + value + "'"; - else if (value instanceof Uint8Array) rendered = inspectBuffer(value); - else rendered = describeType(value); - var e = new TypeError("The argument '" + name + "' is invalid. Received " + rendered); - e.code = 'ERR_INVALID_ARG_VALUE'; - return e; - } + // Node-shaped coded errors come from internal/errors (CLAUDE.md §5); what stays here + // are the buffer-only message templates. + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_INVALID_ARG_VALUE = errors.ERR_INVALID_ARG_VALUE; + var ERR_OUT_OF_RANGE = errors.ERR_OUT_OF_RANGE; + var ERR_MISSING_ARGS = errors.ERR_MISSING_ARGS; + var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; function validateByteLength(byteLength) { if (typeof byteLength !== 'number') - throw errInvalidArgType('byteLength', 'of type number', byteLength); + throw ERR_INVALID_ARG_TYPE('byteLength', 'number', byteLength); if (Math.floor(byteLength) !== byteLength) - throw errOutOfRange('byteLength', 'an integer', byteLength); + throw ERR_OUT_OF_RANGE('byteLength', 'an integer', byteLength); if (byteLength < 1 || byteLength > 6) - throw errOutOfRange('byteLength', '>= 1 and <= 6', byteLength); + throw ERR_OUT_OF_RANGE('byteLength', '>= 1 and <= 6', byteLength); } function checkValueInt(value, min, max, blMinus1) { @@ -218,33 +134,29 @@ } else { range = '>= ' + min + n + ' and <= ' + max + n; } - throw errOutOfRange('value', range, value); + throw ERR_OUT_OF_RANGE('value', range, value); } } function validateWriteOffset(offset, max, name) { - if (Math.floor(offset) !== offset) throw errOutOfRange(name, 'an integer', offset); - if (offset < 0 || offset > max) throw errOutOfRange(name, '>= 0 && <= ' + max, offset); + if (Math.floor(offset) !== offset) throw ERR_OUT_OF_RANGE(name, 'an integer', offset); + if (offset < 0 || offset > max) throw ERR_OUT_OF_RANGE(name, '>= 0 && <= ' + max, offset); return offset; } // Node's validateOffset(value, name, 0, max) as write/compare/fill call it: a // non-number is a type error (no ToNumber coercion), then the integer/range check. function validateOffsetArg(value, name, max) { - if (typeof value !== 'number') throw errInvalidArgType(name, 'of type number', value); + if (typeof value !== 'number') throw ERR_INVALID_ARG_TYPE(name, 'number', value); return validateWriteOffset(value, max, name); } - function errUnknownEncoding(encoding) { - var e = new TypeError('Unknown encoding: ' + encoding); - e.code = 'ERR_UNKNOWN_ENCODING'; - return e; - } - function errBufferSize(bits) { - var e = new RangeError('Buffer size must be a multiple of ' + bits + '-bits'); - e.code = 'ERR_INVALID_BUFFER_SIZE'; - return e; + return nodeError( + P.RangeError, + 'ERR_INVALID_BUFFER_SIZE', + 'Buffer size must be a multiple of ' + bits + '-bits', + ); } /** @@ -267,9 +179,9 @@ * 34-oversized-allocation.js. */ function assertSize(size) { - if (typeof size !== 'number') throw errInvalidArgType('size', 'of type number', size); + if (typeof size !== 'number') throw ERR_INVALID_ARG_TYPE('size', 'number', size); if (!(size >= 0 && size <= K_MAX_LENGTH)) - throw errOutOfRange('size', '>= 0 && <= ' + K_MAX_LENGTH, size); + throw ERR_OUT_OF_RANGE('size', '>= 0 && <= ' + K_MAX_LENGTH, size); } /** @returns {string} Canonical encoding name (default utf8). */ @@ -341,7 +253,7 @@ return norm ? base64DecodeNative(norm) : EMPTY_U8; } if (encoding === 'ascii' || encoding === 'latin1') return latin1EncodeNative(str); - throw errUnknownEncoding(encoding); + throw ERR_UNKNOWN_ENCODING(encoding); } /** @returns {string} */ @@ -354,7 +266,7 @@ if (encoding === 'base64url') return base64urlEncodeNative(bytes); if (encoding === 'latin1') return latin1DecodeNative(bytes); if (encoding === 'ascii') return asciiDecodeNative(bytes); - throw errUnknownEncoding(encoding); + throw ERR_UNKNOWN_ENCODING(encoding); } function toInteger(value, fallback) { @@ -373,19 +285,21 @@ } function errBufferOutOfBounds() { - var e = new RangeError('Attempt to access memory outside buffer bounds'); - e.code = 'ERR_BUFFER_OUT_OF_BOUNDS'; - return e; + return nodeError( + P.RangeError, + 'ERR_BUFFER_OUT_OF_BOUNDS', + 'Attempt to access memory outside buffer bounds', + ); } /** Node-style offset bounds for fixed-width numeric accessors. */ function checkBounds(buf, offset, byteLength) { if (offset === undefined) offset = 0; - if (typeof offset !== 'number') throw errInvalidArgType('offset', 'of type number', offset); - if (Math.floor(offset) !== offset) throw errOutOfRange('offset', 'an integer', offset); + if (typeof offset !== 'number') throw ERR_INVALID_ARG_TYPE('offset', 'number', offset); + if (Math.floor(offset) !== offset) throw ERR_OUT_OF_RANGE('offset', 'an integer', offset); var max = buf.length - byteLength; if (max < 0) throw errBufferOutOfBounds(); - if (offset < 0 || offset > max) throw errOutOfRange('offset', '>= 0 and <= ' + max, offset); + if (offset < 0 || offset > max) throw ERR_OUT_OF_RANGE('offset', '>= 0 and <= ' + max, offset); return offset; } @@ -416,11 +330,7 @@ if (typeof value === 'string') return strToBytes(value, encoding || 'utf8'); if (value instanceof Uint8Array) return value; if (value instanceof ArrayBuffer) return new Uint8Array(value); - throw errInvalidArgType( - 'value', - 'one of type number or string or an instance of Buffer or Uint8Array', - value, - ); + throw ERR_INVALID_ARG_TYPE('value', ['number', 'string', 'Buffer', 'Uint8Array'], value); } /** indexOf / lastIndexOf implementation (native search). */ @@ -573,25 +483,25 @@ */ copy(target, targetStart, sourceStart, sourceEnd) { if (!(target instanceof Uint8Array)) - throw errInvalidArgType('target', 'an instance of Buffer or Uint8Array', target); + throw ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); if (targetStart === undefined) { targetStart = 0; } else { targetStart = toInteger(targetStart, 0); - if (targetStart < 0) throw errOutOfRange('targetStart', '>= 0', targetStart); + if (targetStart < 0) throw ERR_OUT_OF_RANGE('targetStart', '>= 0', targetStart); } if (sourceStart === undefined) { sourceStart = 0; } else { sourceStart = toInteger(sourceStart, 0); if (sourceStart < 0 || sourceStart > this.length) - throw errOutOfRange('sourceStart', '>= 0 && <= ' + this.length, sourceStart); + throw ERR_OUT_OF_RANGE('sourceStart', '>= 0 && <= ' + this.length, sourceStart); } if (sourceEnd === undefined) { sourceEnd = this.length; } else { sourceEnd = toInteger(sourceEnd, 0); - if (sourceEnd < 0) throw errOutOfRange('sourceEnd', '>= 0', sourceEnd); + if (sourceEnd < 0) throw ERR_OUT_OF_RANGE('sourceEnd', '>= 0', sourceEnd); } if (sourceEnd > this.length) sourceEnd = this.length; if (targetStart >= target.length || sourceStart >= sourceEnd) return 0; @@ -611,9 +521,8 @@ */ write(string, offset, length, encoding) { if (typeof string !== 'string') { - var e = new TypeError('argument must be a string'); - e.code = 'ERR_INVALID_ARG_TYPE'; - throw e; + // node's C++ StringWrite throws this one: no Received tail, no [code]. + throw errors.nativeError(P.TypeError, 'ERR_INVALID_ARG_TYPE', 'argument must be a string'); } if (offset === undefined) { offset = 0; @@ -669,7 +578,7 @@ compare(target, targetStart, targetEnd, sourceStart, sourceEnd) { if (!(target instanceof Uint8Array)) - throw errInvalidArgType('target', 'an instance of Buffer or Uint8Array', target); + throw ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target); // Node bounds the *Start arguments by kMaxLength only, the *End ones by the // buffer length: compare(b3, 100) is 1 (empty target range), not a RangeError. if (targetStart === undefined) targetStart = 0; @@ -705,12 +614,11 @@ // out of range) goes to validateWriteOffset, which picks and builds the error. // `% 1` rather than Math.floor keeps the live `Math` global read out (§5). if (start === undefined) start = 0; - else if (typeof start !== 'number') - throw errInvalidArgType('offset', 'of type number', start); + else if (typeof start !== 'number') throw ERR_INVALID_ARG_TYPE('offset', 'number', start); else if (start % 1 !== 0 || start < 0 || start > K_MAX_LENGTH) validateWriteOffset(start, K_MAX_LENGTH, 'offset'); if (end === undefined) end = this.length; - else if (typeof end !== 'number') throw errInvalidArgType('end', 'of type number', end); + else if (typeof end !== 'number') throw ERR_INVALID_ARG_TYPE('end', 'number', end); else if (end % 1 !== 0 || end < 0 || end > this.length) validateWriteOffset(end, this.length, 'end'); if (end <= start) return this; @@ -725,12 +633,12 @@ if (typeof value === 'string') { bytes = strToBytes(value, encoding || 'utf8'); if (bytes.length === 0) { - if (value.length !== 0) throw errInvalidArgValue('value', value); + if (value.length !== 0) throw ERR_INVALID_ARG_VALUE('value', value); u8Fill.call(this, 0, start, end); return this; } } else if (value instanceof Uint8Array) { - if (value.length === 0) throw errInvalidArgValue('value', value); + if (value.length === 0) throw ERR_INVALID_ARG_VALUE('value', value); bytes = value; } } @@ -1243,7 +1151,11 @@ if (typeof primitive === 'string') return Buffer.from(primitive, encodingOrOffset); } } - throw errInvalidFromArg(value); + throw ERR_INVALID_ARG_TYPE( + 'first argument', + ['string', 'Buffer', 'ArrayBuffer', 'Array', 'Array-like Object'], + value, + ); }; /** Zero-filled (or filled) allocation. */ @@ -1298,29 +1210,23 @@ ) { return string.byteLength; } - throw errInvalidArgType( - 'string', - 'of type string or an instance of Buffer or ArrayBuffer', - string, - ); + throw ERR_INVALID_ARG_TYPE('string', ['string', 'Buffer', 'ArrayBuffer'], string); }; Buffer.compare = function (a, b) { - if (!(a instanceof Uint8Array)) - throw errInvalidArgType('buf1', 'an instance of Buffer or Uint8Array', a); - if (!(b instanceof Uint8Array)) - throw errInvalidArgType('buf2', 'an instance of Buffer or Uint8Array', b); + if (!(a instanceof Uint8Array)) throw ERR_INVALID_ARG_TYPE('buf1', ['Buffer', 'Uint8Array'], a); + if (!(b instanceof Uint8Array)) throw ERR_INVALID_ARG_TYPE('buf2', ['Buffer', 'Uint8Array'], b); return compareBytes(a, b); }; function assertConcatElement(item, index) { if (!(item instanceof Uint8Array)) - throw errInvalidArgType('list[' + index + ']', 'an instance of Buffer or Uint8Array', item); + throw ERR_INVALID_ARG_TYPE('list[' + index + ']', ['Buffer', 'Uint8Array'], item); } /** @param {Array} list @param {number} [totalLength] */ Buffer.concat = function (list, totalLength) { - if (!Array.isArray(list)) throw errInvalidArgType('list', 'an instance of Array', list); + if (!Array.isArray(list)) throw ERR_INVALID_ARG_TYPE('list', 'Array', list); if (list.length === 0) return new FastBuffer(0); if (totalLength === undefined) { totalLength = 0; @@ -1330,11 +1236,11 @@ } } else { if (typeof totalLength !== 'number') - throw errInvalidArgType('length', 'of type number', totalLength); + throw ERR_INVALID_ARG_TYPE('length', 'number', totalLength); if (Math.floor(totalLength) !== totalLength) - throw errOutOfRange('length', 'an integer', totalLength); + throw ERR_OUT_OF_RANGE('length', 'an integer', totalLength); if (totalLength < 0 || totalLength > K_MAX_LENGTH) - throw errOutOfRange('length', '>= 0 && <= ' + K_MAX_LENGTH, totalLength); + throw ERR_OUT_OF_RANGE('length', '>= 0 && <= ' + K_MAX_LENGTH, totalLength); } var result = allocate(totalLength); var offset = 0; @@ -1354,8 +1260,7 @@ }; Buffer.copyBytesFrom = function (view, offset, length) { - if (!ArrayBuffer.isView(view)) - throw errInvalidArgType('view', 'an instance of TypedArray', view); + if (!ArrayBuffer.isView(view)) throw ERR_INVALID_ARG_TYPE('view', 'TypedArray', view); var bpe = view.BYTES_PER_ELEMENT || 1; offset = toInteger(offset, 0); length = length === undefined ? view.length - offset : toInteger(length, 0); @@ -1376,12 +1281,6 @@ createPool(); - function errMissingInput() { - var e = new TypeError('The "input" argument must be specified'); - e.code = 'ERR_MISSING_ARGS'; - return e; - } - // Node throws a DOMException here; Lava has no DOMException (same gap as // structured_clone.js's DataCloneError), so this is an Error carrying the // DOMException's name and legacy numeric code. @@ -1421,7 +1320,7 @@ * and message match. Pinned by cmd/lava/atob_invalid_character_test.odin. */ function atob(input) { - if (arguments.length === 0) throw errMissingInput(); + if (arguments.length === 0) throw ERR_MISSING_ARGS('input'); input = `${input}`; var nonWhitespace = 0; var equals = 0; @@ -1468,7 +1367,7 @@ * cmd/lava/atob_invalid_character_test.odin. */ function btoa(input) { - if (arguments.length === 0) throw errMissingInput(); + if (arguments.length === 0) throw ERR_MISSING_ARGS('input'); input = `${input}`; if (P.RegExpMatches(NON_LATIN1_RE, input)) throw errInvalidCharacter('Invalid character'); return Buffer.from(input, 'latin1').toString('base64'); @@ -1705,9 +1604,7 @@ /** @param {Blob|File} obj @returns {string} blob: URL */ function createObjectURL(obj) { if (!(obj instanceof Blob)) { - var typeErr = new TypeError('The "obj" argument must be an instance of Blob.'); - typeErr.code = 'ERR_INVALID_ARG_TYPE'; - throw typeErr; + throw ERR_INVALID_ARG_TYPE('obj', 'Blob', obj); } var id = 'blob:nodedata:' + objectUrlId(); objectUrlRegistry.set(id, obj); @@ -1717,9 +1614,7 @@ /** @param {string} url */ function revokeObjectURL(url) { if (url === undefined) { - var missingErr = new TypeError('The "url" argument must be specified'); - missingErr.code = 'ERR_MISSING_ARGS'; - throw missingErr; + throw ERR_MISSING_ARGS('url'); } objectUrlRegistry.delete(StringG(url)); } @@ -1778,18 +1673,5 @@ inspectMaxBytes = value; }, }); - // Internal, and deliberately NON-ENUMERABLE so `Object.keys(require('buffer'))` still - // matches node exactly: stream.js builds the same ERR_INVALID_ARG_TYPE "Received …" - // clause, there is no shared errors module to host it, and the alternative is a seventh - // near-copy in a tree where the six that exist already disagree with each other (fs.js's - // was missing node's object branch until #330). An earlier revision exported it - // enumerably, which silently added a key node does not have. - P.ObjectDefineProperty(exported, 'describeType', { - value: describeType, - enumerable: false, - writable: false, - configurable: false, - }); - module.exports = exported; }); diff --git a/pkg/runtime/js/node/crypto.js b/pkg/runtime/js/node/crypto.js index 0a18e00..85a7b83 100644 --- a/pkg/runtime/js/node/crypto.js +++ b/pkg/runtime/js/node/crypto.js @@ -28,44 +28,18 @@ // Argument-validation helpers mirroring Node's internal validators, so bad // input fails with Node's error class + code instead of being silently // coerced (a zero-filled buffer, a wrapped iteration count, a clamped range). - function inspectReceived(value) { - if (value === null) return 'null'; - var t = typeof value; - if (t === 'undefined') return 'undefined'; - // -0 renders as "-0" in Node (util.inspect); string concatenation erases the - // sign. Same fix as buffer.js/encoding.js's copies of this helper. - if (t === 'number') - return 'type number (' + (value === 0 && 1 / value < 0 ? '-0' : value) + ')'; - if (t === 'boolean') return 'type boolean (' + value + ')'; - // A bigint keeps Node's trailing "n" (util.inspect(10n) === '10n'), which bare - // string coercion ('' + 10n === '10') drops — matches os.js/buffer.js. - if (t === 'bigint') return 'type bigint (' + value + 'n)'; - if (t === 'string') return 'type string'; - if (t === 'symbol') return 'type symbol'; - if (t === 'function') return 'function ' + (value.name || '(anonymous)'); - if (value && value.constructor && value.constructor.name) - return 'an instance of ' + value.constructor.name; - return 'type object'; - } - function invalidArgType(name, expected, value) { - var err = new TypeError( - 'The "' + name + '" argument must be ' + expected + '. Received ' + inspectReceived(value), - ); - err.code = 'ERR_INVALID_ARG_TYPE'; - return err; - } - function outOfRange(name, range, value) { - var err = new RangeError( - 'The value of "' + name + '" is out of range. It must be ' + range + '. Received ' + value, - ); - err.code = 'ERR_OUT_OF_RANGE'; - return err; - } + // The errors themselves come from internal/errors (CLAUDE.md §5). + var P = require('internal/primordials'); + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var nativeError = errors.nativeError; + var invalidArgType = errors.ERR_INVALID_ARG_TYPE; + var outOfRange = errors.ERR_OUT_OF_RANGE; // Node's validateInt32: number, integer, within [min, max] (default int32 range). function validateInt32(value, name, min, max) { if (min === undefined) min = -0x80000000; if (max === undefined) max = 0x7fffffff; - if (typeof value !== 'number') throw invalidArgType(name, 'of type number', value); + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); if (!Number.isInteger(value)) throw outOfRange(name, 'an integer', value); if (value < min || value > max) throw outOfRange(name, '>= ' + min + ' && <= ' + max, value); return value; @@ -73,7 +47,7 @@ // A byte count (randomBytes size): must be a number in [0, 2^31-1] and not // NaN, but — matching Node — a fractional value is truncated, not rejected. function validateByteCount(value, name) { - if (typeof value !== 'number') throw invalidArgType(name, 'of type number', value); + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); if (Number.isNaN(value) || value < 0 || value > 0x7fffffff) throw outOfRange(name, '>= 0 && <= ' + 0x7fffffff, value); return Math.trunc(value); @@ -89,11 +63,7 @@ if (data instanceof ArrayBuffer) return new Uint8Array(data.slice(0)); if (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer) return new Uint8Array(new Uint8Array(data)); - throw invalidArgType( - name || 'data', - 'of type string or an instance of Buffer, TypedArray, or DataView', - data, - ); + throw invalidArgType(name || 'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data); } function concat(chunks) { @@ -111,9 +81,7 @@ } function finalizedError() { - var err = new Error('Digest already called'); - err.code = 'ERR_CRYPTO_HASH_FINALIZED'; - return err; + return nodeError(P.Error, 'ERR_CRYPTO_HASH_FINALIZED', 'Digest already called'); } // Canonical digest names — one per Odin core:crypto/hash primitive. These are @@ -204,9 +172,7 @@ ); } function invalidDigestError(name) { - var err = new TypeError('Invalid digest: ' + name); - err.code = 'ERR_CRYPTO_INVALID_DIGEST'; - return err; + return nativeError(P.TypeError, 'ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: ' + name); } function getHashes() { @@ -215,17 +181,21 @@ function notImplemented(name) { return function () { - var err = new Error('node:crypto ' + name + ' is not implemented in Lava'); - err.code = 'ERR_NOT_IMPLEMENTED'; - throw err; + throw nativeError( + P.Error, + 'ERR_NOT_IMPLEMENTED', + 'node:crypto ' + name + ' is not implemented in Lava', + ); }; } function notImplementedConstructor(name) { return function () { - var err = new Error('node:crypto ' + name + ' is not implemented in Lava'); - err.code = 'ERR_NOT_IMPLEMENTED'; - throw err; + throw nativeError( + P.Error, + 'ERR_NOT_IMPLEMENTED', + 'node:crypto ' + name + ' is not implemented in Lava', + ); }; } @@ -344,14 +314,14 @@ // elements, converted to bytes against the view, and validated. An oversized // size now throws ERR_OUT_OF_RANGE instead of silently filling fewer bytes. function assertOffset(offset, elementSize, byteLength) { - if (typeof offset !== 'number') throw invalidArgType('offset', 'of type number', offset); + if (typeof offset !== 'number') throw invalidArgType('offset', 'number', offset); var bytes = offset * elementSize; if (Number.isNaN(bytes) || bytes < 0 || bytes > byteLength) throw outOfRange('offset', '>= 0 && <= ' + byteLength / elementSize, offset); return bytes >>> 0; } function assertSize(size, elementSize, offsetBytes, byteLength) { - if (typeof size !== 'number') throw invalidArgType('size', 'of type number', size); + if (typeof size !== 'number') throw invalidArgType('size', 'number', size); var bytes = size * elementSize; if (Number.isNaN(bytes) || bytes < 0 || bytes > 0x7fffffff) throw outOfRange('size', '>= 0 && <= ' + 0x7fffffff, size); @@ -379,11 +349,7 @@ byteLength = buffer.byteLength; elementSize = 1; } else { - throw invalidArgType( - 'buf', - 'an instance of ArrayBuffer, Buffer, TypedArray, or DataView', - buffer, - ); + throw invalidArgType('buf', ['ArrayBuffer', 'ArrayBufferView'], buffer); } var offsetBytes = assertOffset(offset === undefined ? 0 : offset, elementSize, byteLength); var sizeBytes = @@ -468,23 +434,28 @@ // Node requires both operands to be ArrayBufferView/ArrayBuffer — strings and // numbers are rejected (a number used to be zero-filled, so timingSafeEqual(5, // 5) wrongly returned true). + // node's C++ TimingSafeEqual throws this one: no Received tail, no [code] in String(e). function requireBufferSource(value, name) { if (!ArrayBuffer.isView(value) && !(value instanceof ArrayBuffer)) - throw invalidArgType( - name, - 'an instance of ArrayBuffer, Buffer, TypedArray, or DataView', - value, + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_TYPE', + 'The "' + + name + + '" argument must be an instance of ArrayBuffer, Buffer, TypedArray, or DataView.', ); } function timingSafeEqual(a, b) { - requireBufferSource(a, 'a'); - requireBufferSource(b, 'b'); + requireBufferSource(a, 'buf1'); + requireBufferSource(b, 'buf2'); var ua = toU8(a), ub = toU8(b); if (ua.length !== ub.length) { - var err = new RangeError('Input buffers must have the same byte length'); - err.code = 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH'; - throw err; + throw nativeError( + P.RangeError, + 'ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH', + 'Input buffers must have the same byte length', + ); } return native.timingSafeEqual(ua, ub); } @@ -536,7 +507,7 @@ } function pbkdf2Sync(password, salt, iterations, keylen, digest) { - if (typeof digest !== 'string') throw invalidArgType('digest', 'of type string', digest); + if (typeof digest !== 'string') throw invalidArgType('digest', 'string', digest); var algo = resolveDigest(digest); if (!algo) throw invalidDigestError(digest); var pw = toU8(password, undefined, 'password'); @@ -703,11 +674,11 @@ var hasP = opts[primary] !== undefined; var hasA = opts[alias] !== undefined; if (hasP && hasA) { - var err = new TypeError( + throw nodeError( + P.TypeError, + 'ERR_INCOMPATIBLE_OPTION_PAIR', 'Option "' + primary + '" cannot be used in combination with option "' + alias + '"', ); - err.code = 'ERR_INCOMPATIBLE_OPTION_PAIR'; - throw err; } if (hasP) return opts[primary]; if (hasA) return opts[alias]; @@ -721,19 +692,16 @@ } function scryptParamsError(message) { - var err = new RangeError(message || 'Invalid scrypt params'); - err.code = 'ERR_CRYPTO_INVALID_SCRYPT_PARAMS'; - return err; + return nativeError( + P.RangeError, + 'ERR_CRYPTO_INVALID_SCRYPT_PARAMS', + message || 'Invalid scrypt params', + ); } function scryptDerive(password, salt, keylen, options) { if (!Number.isInteger(keylen) || keylen < 0 || keylen > 0x7fffffff) { - var rerr = new RangeError( - 'The value of "keylen" is out of range. It must be >= 0 && <= 2147483647. Received ' + - keylen, - ); - rerr.code = 'ERR_OUT_OF_RANGE'; - throw rerr; + throw outOfRange('keylen', '>= 0 && <= 2147483647', keylen); } var pr = scryptParams(options); var N = pr.N, diff --git a/pkg/runtime/js/node/dns.js b/pkg/runtime/js/node/dns.js index 07b606e..4f4f579 100644 --- a/pkg/runtime/js/node/dns.js +++ b/pkg/runtime/js/node/dns.js @@ -30,58 +30,24 @@ // ---- Node-parity argument validation ------------------------------------- // dns.lookup is strict about option types/values: Node throws synchronously on // bad input (ERR_INVALID_ARG_TYPE / ERR_INVALID_ARG_VALUE, both TypeError) so - // mistakes surface instead of resolving as "any family". We mirror the codes and - // shapes; message text is close but the contract callers rely on is code + type. - function typeErr(code, message) { - var e = new TypeError(message); - e.code = code; - return e; - } - function show(value) { - return typeof value === 'string' ? "'" + value + "'" : String(value); - } - function propWord(name) { - return name.indexOf('.') !== -1 ? 'property' : 'argument'; - } - function invalidArgType(name, expected, value) { - var kind = value === null ? 'null' : typeof value; - return typeErr( - 'ERR_INVALID_ARG_TYPE', - 'The "' + - name + - '" ' + - propWord(name) + - ' must be ' + - expected + - '. Received type ' + - kind + - ' (' + - show(value) + - ')', - ); - } - function invalidArgValue(name, value, reason) { - return typeErr( - 'ERR_INVALID_ARG_VALUE', - 'The ' + - propWord(name) + - " '" + - name + - "' " + - (reason || 'is invalid') + - '. Received ' + - show(value), - ); + // mistakes surface instead of resolving as "any family". The errors come from + // internal/errors (CLAUDE.md §5), so the text is node's as well as the code. + var errors = require('internal/errors'); + var invalidArgType = errors.ERR_INVALID_ARG_TYPE; + var invalidArgValue = errors.ERR_INVALID_ARG_VALUE; + // node's validateOneOf quotes strings and prints everything else bare in its reason. + function quoteOne(value) { + return typeof value === 'string' ? "'" + value + "'" : P.String(value); } function validateBoolean(value, name) { - if (typeof value !== 'boolean') throw invalidArgType(name, 'of type boolean', value); + if (typeof value !== 'boolean') throw invalidArgType(name, 'boolean', value); } function validateNumber(value, name) { - if (typeof value !== 'number') throw invalidArgType(name, 'of type number', value); + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); } function validateOneOf(value, name, list) { if (list.indexOf(value) === -1) { - var allowed = list.map(show).join(', '); + var allowed = P.ArrayPrototypeJoin(P.ArrayPrototypeMap(list, quoteOne), ', '); throw invalidArgValue(name, value, 'must be one of: ' + allowed); } } @@ -114,13 +80,7 @@ function validateHostname(name) { // Node accepts a falsy hostname (resolves to null); only a non-string truthy // value is a type error. - if (name && typeof name !== 'string') { - var e = new TypeError( - 'The "hostname" argument must be of type string. Received ' + typeof name, - ); - e.code = 'ERR_INVALID_ARG_TYPE'; - throw e; - } + if (name && typeof name !== 'string') throw invalidArgType('hostname', 'string', name); } // dns.lookup(hostname[, options], callback) @@ -130,7 +90,7 @@ options = undefined; } if (typeof callback !== 'function') { - throw invalidArgType('callback', 'of type function', callback); + throw invalidArgType('callback', 'function', callback); } validateHostname(hostname); @@ -142,7 +102,7 @@ validateOneOf(options, 'family', [0, 4, 6]); family = options; } else if (options !== undefined && options !== null && typeof options !== 'object') { - throw invalidArgType('options', 'of type object or integer', options); + throw invalidArgType('options', ['integer', 'object'], options); } else if (options) { if (options.hints !== undefined && options.hints !== null) { validateNumber(options.hints, 'options.hints'); @@ -189,15 +149,7 @@ var defaultResultOrder = 'verbatim'; function setDefaultResultOrder(order) { - if (order !== 'ipv4first' && order !== 'ipv6first' && order !== 'verbatim') { - var e = new TypeError( - "The argument 'order' must be one of 'verbatim', 'ipv4first', 'ipv6first'. Received '" + - order + - "'", - ); - e.code = 'ERR_INVALID_ARG_VALUE'; - throw e; - } + validateOneOf(order, 'dnsOrder', ['verbatim', 'ipv4first', 'ipv6first']); defaultResultOrder = order; } function getDefaultResultOrder() { diff --git a/pkg/runtime/js/node/events.js b/pkg/runtime/js/node/events.js index 43edfb4..144e737 100644 --- a/pkg/runtime/js/node/events.js +++ b/pkg/runtime/js/node/events.js @@ -6,6 +6,8 @@ // Pristine intrinsics (see primordials.js) so a script that mutates // Array.prototype/Object/etc. cannot alter EventEmitter's internal bookkeeping. var P = require('internal/primordials'); + var errors = require('internal/errors'); + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; var defaultMaxListeners = 10; // Species-free array helpers. Array.prototype.slice/map/splice consult @@ -42,14 +44,10 @@ function validateMaxListeners(n) { if (typeof n !== 'number') { - throw typeError('setMaxListeners', 'of type number', n); + throw ERR_INVALID_ARG_TYPE('setMaxListeners', 'number', n); } if (n < 0 || n !== n) { - var e = new RangeError( - 'The value of "setMaxListeners" is out of range. It must be >= 0. Received ' + n, - ); - e.code = 'ERR_OUT_OF_RANGE'; - throw e; + throw errors.ERR_OUT_OF_RANGE('setMaxListeners', '>= 0', n); } } @@ -283,12 +281,12 @@ options !== undefined && (options === null || typeof options !== 'object' || P.ArrayIsArray(options)) ) { - return P.Promise.reject(errOptionsType(options)); + return P.Promise.reject(ERR_INVALID_ARG_TYPE('options', 'object', options)); } var signal = options && options.signal; if (signal !== undefined) { if (!isAbortSignal(signal)) { - return P.Promise.reject(typeError('options.signal', 'an instance of AbortSignal', signal)); + return P.Promise.reject(ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal)); } if (signal.aborted) { return P.Promise.reject(makeAbortError()); @@ -332,41 +330,13 @@ emitter.addEventListener(name, eventListener, { once: true }); } else { cleanup(); - reject(typeError('emitter', 'an EventEmitter or EventTarget', emitter)); + reject(ERR_INVALID_ARG_TYPE('emitter', 'EventEmitter', emitter)); } }); } // --- static helpers (Node parity) --- - function typeError(name, expected, actual) { - var e = new TypeError( - 'The "' + name + '" argument must be ' + expected + '. Received ' + typeof actual, - ); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } - - // The ERR_INVALID_ARG_TYPE "Received …" tail for once()'s options. typeError above - // prints a bare `typeof`, which is wrong for node's template; only the cases - // validateObject can reach here are rendered (no objects other than arrays). - function errOptionsType(v) { - var t = typeof v; - var got; - if (v === null) got = 'null'; - else if (t === 'function') got = 'function ' + v.name; - else if (t === 'object') got = 'an instance of Array'; - else if (t === 'string') - got = - "type string ('" + (v.length > 28 ? P.StringPrototypeSlice(v, 0, 25) + '...' : v) + "')"; - else if (t === 'symbol') got = 'type symbol (' + P.String(v) + ')'; - else - got = 'type ' + t + ' (' + (v === 0 && 1 / v < 0 ? '-0' : v) + (t === 'bigint' ? 'n)' : ')'); - var e = new P.TypeError('The "options" argument must be of type object. Received ' + got); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } - function isAbortSignal(s) { return s != null && typeof s.addEventListener === 'function' && 'aborted' in s; } @@ -416,8 +386,9 @@ // events.addAbortListener(signal, listener) — once-listener for 'abort' that fires even // if the signal is already aborted; returns a { [Symbol.dispose]() } to remove it. function addAbortListener(signal, listener) { - if (!isAbortSignal(signal)) throw typeError('signal', 'an instance of AbortSignal', signal); - if (typeof listener !== 'function') throw typeError('listener', 'a function', listener); + if (!isAbortSignal(signal)) throw ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal); + if (typeof listener !== 'function') + throw ERR_INVALID_ARG_TYPE('listener', 'function', listener); var removeEventListener; if (signal.aborted) { queueMicrotask(function () { @@ -444,7 +415,7 @@ options = options || {}; var signal = options.signal; if (signal !== undefined && !isAbortSignal(signal)) { - throw typeError('options.signal', 'an instance of AbortSignal', signal); + throw ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); } if (signal && signal.aborted) throw makeAbortError(); @@ -455,7 +426,7 @@ var useEmitter = typeof emitter.on === 'function' && typeof emitter.off === 'function'; if (!useEmitter && typeof emitter.addEventListener !== 'function') { - throw typeError('emitter', 'an EventEmitter or EventTarget', emitter); + throw ERR_INVALID_ARG_TYPE('emitter', 'EventEmitter', emitter); } function addListener(name, h) { if (useEmitter) emitter.on(name, h); diff --git a/pkg/runtime/js/node/fs.js b/pkg/runtime/js/node/fs.js index 82c6fbc..4a436ba 100644 --- a/pkg/runtime/js/node/fs.js +++ b/pkg/runtime/js/node/fs.js @@ -27,8 +27,6 @@ 'use strict'; var P = require('internal/primordials'); - var TypeErrorG = P.TypeError; - var StringPrototypeSlice = P.StringPrototypeSlice; var ArrayBufferIsView = P.ArrayBufferIsView; var TypedArrayPrototypeGetBuffer = P.TypedArrayPrototypeGetBuffer; var TypedArrayPrototypeGetByteOffset = P.TypedArrayPrototypeGetByteOffset; @@ -51,66 +49,14 @@ var BufferIsEncoding = PristineBuffer.isEncoding; var BufferPrototypeToString = PristineBuffer.bufferToString; - // Node renders the offending value with util.inspect in BOTH templates, so a string - // comes out quoted and everything else bare. Verified against node 26.13.0 (probe, - // 2026-11-16): - // 'bogus' -> 'bogus' 123 -> 123 true -> true 1n -> 1n - // {} -> {} {a:1} -> { a: 1 } [] -> [] Symbol(x) -> Symbol(x) - // An earlier version of this quoted everything, which was wrong for every non-string. - // NOT matched: node renders a null-prototype object as `[Object: null prototype] {}` - // where Lava's inspect gives `{}` — a util.inspect gap, listed in #331, so this line - // does not claim parity for it. - // - // Memoized on first use. `util` is not eager-loaded (instantiating it costs ~3.5 ms of - // startup for a string only ever built when a call is already failing), so this cannot - // get the loader-time guarantee the Buffer capture above has. The residual is bounded to - // MESSAGE TEXT — no file bytes flow through it — and reading it live per call, as an - // earlier revision did, was strictly worse. Tracked with the other live-module captures - // (http.js, net.js, stream.js, https.js, os.js all have the same shape). - var inspectFn = null; - function inspectValue(value) { - if (inspectFn === null) inspectFn = require('util').inspect; - return inspectFn(value); - } - - // Node's ERR_INVALID_ARG_TYPE "Received" clause has THREE branches, not two - // (determineSpecificType). Measured on node 26.13.0 (probe, 2026-11-16): - // null / undefined -> named bare: Received null - // an object -> its constructor: Received an instance of Object - // (Array, Date, RegExp, Map, Buffer, a user class …; - // a null-prototype object has no constructor and falls - // back to the inspected form) - // anything else -> type + inspected: Received type number (123) - // with a string longer than 28 chars cut to its first 25 - // plus "..." BEFORE inspecting. - // An earlier revision implemented only the first and third and asserted in this comment - // that they were the whole rule. The object branch is the reachable one: it is what - // `fs.readFile(path, {encoding:'utf8'})` with a forgotten callback produces. - var RECEIVED_STR_MAX = 25; - function received(value) { - if (value === null) return 'null'; - if (value === undefined) return 'undefined'; - if (typeof value === 'object') { - // node's determineSpecificType reads the RECEIVER's own `.constructor.name` for this - // message, so reading it is what parity requires — a captured getter would answer for - // the wrong object. A forged constructor changes the text of an error already being - // thrown, nothing else. - var ctor = value.constructor; // primordials-ok: accessor - if (typeof ctor === 'function' && ctor.name) return 'an instance of ' + ctor.name; - return inspectValue(value); - } - var shown = value; - if (typeof shown === 'string' && shown.length > RECEIVED_STR_MAX + 3) { - shown = StringPrototypeSlice(shown, 0, RECEIVED_STR_MAX) + '...'; - } - return 'type ' + typeof value + ' (' + inspectValue(shown) + ')'; - } - - function errInvalidArgType(message) { - var err = new TypeErrorG(message); - err.code = 'ERR_INVALID_ARG_TYPE'; - return err; - } + // Node-shaped coded errors come from internal/errors (CLAUDE.md §5). Its describer is + // node's determineSpecificType in full — the object branch (`fs.readFile(path, + // {encoding:'utf8'})` with a forgotten callback answers "an instance of Object") and + // the 25-char string cut included — and ERR_INVALID_ARG_VALUE renders through + // util.inspect, so a string comes out quoted and everything else bare. + var errors = require('internal/errors'); + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_INVALID_ARG_VALUE = errors.ERR_INVALID_ARG_VALUE; /** * Re-tags a native read result as a Buffer, sharing its backing store. @@ -168,11 +114,7 @@ var ctor = path.constructor; // primordials-ok: accessor if (typeof ctor === 'function' && ctor.name === 'URL') return; } - throw errInvalidArgType( - 'The "path" argument must be of type string or an instance of Buffer or URL. ' + - 'Received ' + - received(path), - ); + throw ERR_INVALID_ARG_TYPE('path', ['string', 'Buffer', 'URL'], path); } /** @@ -202,10 +144,7 @@ } else if (typeof options === 'object') { encoding = options.encoding; } else { - throw errInvalidArgType( - 'The "options" argument must be one of type string or object. Received ' + - received(options), - ); + throw ERR_INVALID_ARG_TYPE('options', ['string', 'Object'], options); } // EVERY falsy encoding means binary, not just null/undefined: node's assertEncoding is // guarded `if (encoding && !Buffer.isEncoding(encoding))`, and readFileSync returns @@ -219,11 +158,7 @@ // ERR_UNKNOWN_ENCODING rather than ERR_INVALID_ARG_VALUE. Different code AND message, // on a name that legitimately appears in shared option objects. if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) { - var e2 = new TypeErrorG( - "The argument 'encoding' is invalid encoding. Received " + inspectValue(encoding), - ); - e2.code = 'ERR_INVALID_ARG_VALUE'; - throw e2; + throw ERR_INVALID_ARG_VALUE('encoding', encoding, 'is invalid encoding'); } return encoding; } @@ -287,9 +222,7 @@ function readFile(path, options, callback) { var cb = callback || options; if (typeof cb !== 'function') { - throw errInvalidArgType( - 'The "cb" argument must be of type function. Received ' + received(cb), - ); + throw ERR_INVALID_ARG_TYPE('cb', 'function', cb); } validatePath(path); var encoding = readEncoding(typeof options === 'function' ? null : options); diff --git a/pkg/runtime/js/node/http.js b/pkg/runtime/js/node/http.js index bcf7f00..b69b1f0 100644 --- a/pkg/runtime/js/node/http.js +++ b/pkg/runtime/js/node/http.js @@ -19,6 +19,7 @@ var primordials = require('internal/primordials'); var ObjectCreate = primordials.ObjectCreate; var ObjectDefineProperty = primordials.ObjectDefineProperty; + var nodeError = require('internal/errors').nodeError; if (!native || typeof native.parseRequest !== 'function') { throw new primordials.Error('node:http is unavailable on this platform'); @@ -163,11 +164,11 @@ */ function assertHeaderName(name) { if (!isHttpToken(name)) { - var err = new TypeError( + throw nodeError( + primordials.TypeError, + 'ERR_INVALID_HTTP_TOKEN', 'Header name must be a valid HTTP token [' + JSON.stringify(name) + ']', ); - err.code = 'ERR_INVALID_HTTP_TOKEN'; - throw err; } } @@ -184,9 +185,7 @@ if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) { continue; } - var err = new TypeError('Invalid character in ' + what); - err.code = 'ERR_INVALID_CHAR'; - throw err; + throw nodeError(primordials.TypeError, 'ERR_INVALID_CHAR', 'Invalid character in ' + what); } } @@ -207,9 +206,11 @@ function validateStatusCode(statusCode) { var code = NumberG(statusCode); if (!NumberIsInteger(code) || code < 100 || code > 999) { - var err = new RangeError('Invalid status code: ' + JSON.stringify(statusCode)); - err.code = 'ERR_HTTP_INVALID_STATUS_CODE'; - throw err; + throw nodeError( + primordials.RangeError, + 'ERR_HTTP_INVALID_STATUS_CODE', + 'Invalid status code: ' + JSON.stringify(statusCode), + ); } return code; } diff --git a/pkg/runtime/js/node/https.js b/pkg/runtime/js/node/https.js index 7cb2ca6..dcc5337 100644 --- a/pkg/runtime/js/node/https.js +++ b/pkg/runtime/js/node/https.js @@ -14,6 +14,9 @@ // The primordial, not the live global: this module is LAZY, so a module-eval read here // runs after user code could have replaced the global (#426). var P = require('internal/primordials'); + // ERR_TLS_CERT / ERR_TLS_UNSUPPORTED_OPTION are Lava's own (M1 gaps, no node + // counterpart), so they keep the plain shape; the type error is node's. + var errors = require('internal/errors'); if (!native || typeof native.createSecureContext !== 'function') { throw new P.Error('node:https is unavailable on this platform'); } @@ -27,13 +30,13 @@ var BufferIsBuffer = PristineBuffer.isBuffer; function unsupported(name) { - var e = new Error( + throw errors.nativeError( + P.Error, + 'ERR_TLS_UNSUPPORTED_OPTION', "node:https (M1) does not support the '" + name + "' option; it is rejected rather than ignored so the connection is never weaker than requested", ); - e.code = 'ERR_TLS_UNSUPPORTED_OPTION'; - throw e; } // Reject the deferred, security-sensitive options — but only when set to a behavior-changing value @@ -66,9 +69,11 @@ // key + one cert (the cert PEM may itself hold leaf+intermediates), so arrays are rejected. function toPem(value, field) { if (value == null || value === '') { - var e = new Error("node:https requires the '" + field + "' option"); - e.code = 'ERR_TLS_CERT'; - throw e; + throw errors.nativeError( + P.Error, + 'ERR_TLS_CERT', + "node:https requires the '" + field + "' option", + ); } if (typeof value === 'string') return value; // `value.toString('utf8')` would read the method off the INSTANCE, i.e. through the @@ -79,29 +84,31 @@ if (BufferIsBuffer(value)) return BufferPrototypeToString(value, 'utf8'); if (value instanceof Uint8Array) return BufferPrototypeToString(BufferFrom(value), 'utf8'); if (Array.isArray(value)) { - var ae = new Error( + throw errors.nativeError( + P.Error, + 'ERR_TLS_UNSUPPORTED_OPTION', "node:https (M1) supports a single '" + field + "'; arrays (multiple contexts) are deferred", ); - ae.code = 'ERR_TLS_UNSUPPORTED_OPTION'; - throw ae; } // Anything else (a stray object/number) — throw a clear type error rather than feeding // String(value) (e.g. '[object Object]') to OpenSSL and surfacing a confusing ERR_TLS_CERT. - var te = new TypeError( - "node:https option '" + field + "' must be a string, Buffer, or Uint8Array (PEM)", + throw errors.ERR_INVALID_ARG_TYPE( + 'options.' + field, + ['string', 'Buffer', 'TypedArray', 'DataView'], + value, ); - te.code = 'ERR_INVALID_ARG_TYPE'; - throw te; } function createServer(options, requestListener) { // Unlike http, https has no (requestListener)-only form: key+cert are mandatory. if (typeof options === 'function') { - var e = new Error('node:https requires an options object with `key` and `cert`'); - e.code = 'ERR_TLS_CERT'; - throw e; + throw errors.nativeError( + P.Error, + 'ERR_TLS_CERT', + 'node:https requires an options object with `key` and `cert`', + ); } if (!options || typeof options !== 'object') options = {}; @@ -159,13 +166,13 @@ // https.request(...) gets an actionable message (use fetch) instead of an opaque // "https.request is not a function" TypeError. function clientUnsupported(name) { - var e = new Error( + throw errors.nativeError( + P.Error, + 'ERR_TLS_UNSUPPORTED_OPTION', 'node:https client API `' + name + '` is not implemented; use the global `fetch()` for HTTPS requests', ); - e.code = 'ERR_TLS_UNSUPPORTED_OPTION'; - throw e; } function request() { clientUnsupported('https.request'); diff --git a/pkg/runtime/js/node/net.js b/pkg/runtime/js/node/net.js index d6ee6e8..e0373c9 100644 --- a/pkg/runtime/js/node/net.js +++ b/pkg/runtime/js/node/net.js @@ -12,6 +12,7 @@ // module-eval read here runs after user code could have replaced the global (#426). var P = require('internal/primordials'); var ObjectCreate = P.ObjectCreate; + var nodeError = require('internal/errors').nodeError; if (!native || typeof native.listen !== 'function') { throw new P.Error('node:net is unavailable on this platform'); @@ -197,8 +198,11 @@ // (its fd stays watched, pinning the loop and the port). Reject, like Node. if (this._listening || this._closing) { var self0 = this; - var lerr = new Error('Server is already listening'); - lerr.code = 'ERR_SERVER_ALREADY_LISTEN'; + var lerr = nodeError( + P.Error, + 'ERR_SERVER_ALREADY_LISTEN', + 'Listen method has been called more than once without closing.', + ); process.nextTick(function () { self0.emit('error', lerr); }); diff --git a/pkg/runtime/js/node/os.js b/pkg/runtime/js/node/os.js index 16859b7..b3a017c 100644 --- a/pkg/runtime/js/node/os.js +++ b/pkg/runtime/js/node/os.js @@ -622,32 +622,13 @@ // ---- arg validation: Node's validateInt32, reproducing the ERR_* code/message // shapes so callers see the same TypeError/RangeError they do under Node. - function inspectReceived(value) { - if (typeof value === 'string') return "type string ('" + value + "')"; - if (value === null) return 'null'; - if (typeof value === 'number' || typeof value === 'boolean') - return 'type ' + typeof value + ' (' + value + ')'; - if (typeof value === 'bigint') return 'type bigint (' + value + 'n)'; - return 'type ' + typeof value; - } - function invalidArgType(name, expected, value) { - var err = new TypeError( - 'The "' + name + '" argument must be ' + expected + '. Received ' + inspectReceived(value), - ); - err.code = 'ERR_INVALID_ARG_TYPE'; - return err; - } - function outOfRange(name, range, value) { - var err = new RangeError( - 'The value of "' + name + '" is out of range. It must be ' + range + '. Received ' + value, - ); - err.code = 'ERR_OUT_OF_RANGE'; - return err; - } + var errors = require('internal/errors'); + var invalidArgType = errors.ERR_INVALID_ARG_TYPE; + var outOfRange = errors.ERR_OUT_OF_RANGE; function validateInt32(value, name, min, max) { if (min === undefined) min = -0x80000000; if (max === undefined) max = 0x7fffffff; - if (typeof value !== 'number') throw invalidArgType(name, 'of type number', value); + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); if (!Number.isInteger(value)) throw outOfRange(name, 'an integer', value); if (value < min || value > max) throw outOfRange(name, '>= ' + min + ' && <= ' + max, value); return value; diff --git a/pkg/runtime/js/node/path.js b/pkg/runtime/js/node/path.js index 8d0c502..ebbd813 100644 --- a/pkg/runtime/js/node/path.js +++ b/pkg/runtime/js/node/path.js @@ -25,17 +25,11 @@ return (code >= 65 && code <= 90) || (code >= 97 && code <= 122); // A-Z a-z } - function invalidArgType(name, expected, value) { - var err = new TypeError( - 'The "' + name + '" argument must be of type ' + expected + '. Received type ' + typeof value, - ); - err.code = 'ERR_INVALID_ARG_TYPE'; - return err; - } + var ERR_INVALID_ARG_TYPE = require('internal/errors').ERR_INVALID_ARG_TYPE; function assertPath(path, name) { if (typeof path !== 'string') { - throw invalidArgType(name || 'path', 'string', path); + throw ERR_INVALID_ARG_TYPE(name || 'path', 'string', path); } } @@ -132,7 +126,9 @@ var resolvedAbsolute = false; for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) { var path = i >= 0 ? arguments[i] : posixCwd(); - assertPath(path); + // node names the offending position; built only once the check has failed. + if (typeof path !== 'string') + throw ERR_INVALID_ARG_TYPE('paths[' + i + ']', 'string', path); if (path.length === 0) continue; resolvedPath = path + '/' + resolvedPath; resolvedAbsolute = path.charCodeAt(0) === SLASH; @@ -209,7 +205,7 @@ basename: function basename(path, suffix) { if (suffix !== undefined && typeof suffix !== 'string') { - throw invalidArgType('suffix', 'string', suffix); + throw ERR_INVALID_ARG_TYPE('suffix', 'string', suffix); } assertPath(path); return computeBasename(path, suffix, isPosixSep); @@ -251,7 +247,8 @@ var path; if (i >= 0) { path = arguments[i]; - assertPath(path); + if (typeof path !== 'string') + throw ERR_INVALID_ARG_TYPE('paths[' + i + ']', 'string', path); if (path.length === 0) continue; } else if (resolvedDevice.length === 0) { path = win32Cwd(); @@ -495,7 +492,7 @@ basename: function basename(path, suffix) { if (suffix !== undefined && typeof suffix !== 'string') { - throw invalidArgType('suffix', 'string', suffix); + throw ERR_INVALID_ARG_TYPE('suffix', 'string', suffix); } assertPath(path); var start = 0; @@ -1244,18 +1241,10 @@ return { segs: segs, absolute: absolute, trailing: trailing }; } - function globArgError(name, value) { - var e = new TypeError( - 'The "' + name + '" argument must be of type string. Received ' + typeof value, - ); - e.code = 'ERR_INVALID_ARG_TYPE'; - return e; - } - function makeMatchesGlob(winSep) { return function matchesGlob(path, pattern) { - if (typeof path !== 'string') throw globArgError('path', path); - if (typeof pattern !== 'string') throw globArgError('pattern', pattern); + if (typeof path !== 'string') throw ERR_INVALID_ARG_TYPE('path', 'string', path); + if (typeof pattern !== 'string') throw ERR_INVALID_ARG_TYPE('pattern', 'string', pattern); // windowsPathsNoEscape: '\' is a separator. No regex — these were global-flag // replaces, and a global replace under a forged `RegExp.prototype.exec` never // returns (it only advances `lastIndex` on an empty match), so `matchesGlob` span diff --git a/pkg/runtime/js/node/sqlite.js b/pkg/runtime/js/node/sqlite.js index 5830d72..4f3128e 100644 --- a/pkg/runtime/js/node/sqlite.js +++ b/pkg/runtime/js/node/sqlite.js @@ -11,6 +11,9 @@ // could have replaced it (#426). Function bodies still read their globals live — see the // pollution baseline. var P = require('internal/primordials'); + // node:sqlite throws from C++ (THROW_ERR_*): a bare error with a `code` own + // property, so String(e) carries no [code] — internal/errors' nativeError shape. + var nativeError = require('internal/errors').nativeError; if (!native) { throw new P.Error('node:sqlite is unavailable: Lava was built without libsqlite3'); @@ -88,9 +91,7 @@ // whose db is closed reports as finalized too — matching node:sqlite. StatementSync.prototype._assertReady = function () { if (this._finalized || !this._db._open) { - var err = new Error('statement has been finalized'); - err.code = 'ERR_INVALID_STATE'; - throw err; + throw nativeError(P.Error, 'ERR_INVALID_STATE', 'statement has been finalized'); } }; @@ -208,9 +209,11 @@ native.bindBigInt(stmtId, index, value ? '1' : '0'); } else if (t === 'bigint') { if (value < I64_MIN || value > I64_MAX) { - var ov = new TypeError('BigInt value is too large to bind.'); - ov.code = 'ERR_INVALID_ARG_VALUE'; - throw ov; + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_VALUE', + 'BigInt value is too large to bind.', + ); } native.bindBigInt(stmtId, index, value.toString()); } else if (ArrayBuffer.isView(value)) { @@ -227,9 +230,11 @@ // `native` is the factory's private binding table, not a Function: not .bind(). native.bind(stmtId, index, n === 0 ? null : new Uint8ArrayCtor(value)); // primordials-ok: method } else { - var err = new TypeError('Provided value cannot be bound to SQLite parameter ' + index + '.'); - err.code = 'ERR_INVALID_ARG_TYPE'; - throw err; + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_TYPE', + 'Provided value cannot be bound to SQLite parameter ' + index + '.', + ); } } @@ -289,9 +294,11 @@ var keys = Object.keys(named); for (var k = 0; k < keys.length; k++) { if (!known[keys[k]]) { - var unknown = new Error("Unknown named parameter '" + keys[k] + "'"); - unknown.code = 'ERR_INVALID_STATE'; - throw unknown; + throw nativeError( + P.Error, + 'ERR_INVALID_STATE', + "Unknown named parameter '" + keys[k] + "'", + ); } } } @@ -355,18 +362,18 @@ // A URL object — only a file: URL names a path (fileURLToPath rejects others). resolved = require('node:url').fileURLToPath(path.href); } else { - var e = new TypeError( + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_TYPE', 'The "path" argument must be a string, Uint8Array, or URL without null bytes.', ); - e.code = 'ERR_INVALID_ARG_TYPE'; - throw e; } if (resolved.indexOf('\0') !== -1) { - var nul = new TypeError( + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_TYPE', 'The "path" argument must be a string, Uint8Array, or URL without null bytes.', ); - nul.code = 'ERR_INVALID_ARG_TYPE'; - throw nul; } return resolved; } @@ -395,9 +402,7 @@ DatabaseSync.prototype._assertOpen = function () { if (!this._open) { - var err = new Error('database is not open'); - err.code = 'ERR_INVALID_STATE'; - throw err; + throw nativeError(P.Error, 'ERR_INVALID_STATE', 'database is not open'); } }; @@ -408,9 +413,11 @@ // it looks at the argument. function assertSql(sql) { if (typeof sql !== 'string') { - var err = new TypeError('The "sql" argument must be a string.'); - err.code = 'ERR_INVALID_ARG_TYPE'; - throw err; + throw nativeError( + P.TypeError, + 'ERR_INVALID_ARG_TYPE', + 'The "sql" argument must be a string.', + ); } return sql; } diff --git a/pkg/runtime/js/node/stream.js b/pkg/runtime/js/node/stream.js index e403421..1ab9a6d 100644 --- a/pkg/runtime/js/node/stream.js +++ b/pkg/runtime/js/node/stream.js @@ -15,9 +15,6 @@ var EventEmitter = require('events'); var bufferModule = require('buffer'); var Buffer = bufferModule.Buffer; - // node's "Received …" clause, shared rather than re-derived — see the non-enumerable - // export in buffer.js for why this is not a seventh local copy. - var describeType = bufferModule.describeType; var BufferIsEncoding = Buffer.isEncoding; // Through primordials, NOT captured here: this module is lazy, so a module-eval capture @@ -44,45 +41,41 @@ // module loads, so there is nothing to capture here yet. var processRef = process; - // --- Node-shaped coded errors ------------------------------------------------- + // --- Node-shaped coded errors (internal/errors, CLAUDE.md §5) ------------------- - function codedError(Ctor, code, message) { - var err = new Ctor(message); - err.code = code; - return err; - } + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; function errWriteAfterEnd() { - return codedError(ErrorG, 'ERR_STREAM_WRITE_AFTER_END', 'write after end'); + return nodeError(ErrorG, 'ERR_STREAM_WRITE_AFTER_END', 'write after end'); } function errDestroyed(what) { - return codedError( - Error, + return nodeError( + ErrorG, 'ERR_STREAM_DESTROYED', 'Cannot call ' + what + ' after a stream was destroyed', ); } function errNullValues() { - return codedError(TypeErrorG, 'ERR_STREAM_NULL_VALUES', 'May not write null values to stream'); + return nodeError(TypeErrorG, 'ERR_STREAM_NULL_VALUES', 'May not write null values to stream'); } function errMethodNotImplemented(name) { - return codedError( - Error, + return nodeError( + ErrorG, 'ERR_METHOD_NOT_IMPLEMENTED', 'The ' + name + ' method is not implemented', ); } function errPrematureClose() { - return codedError(ErrorG, 'ERR_STREAM_PREMATURE_CLOSE', 'Premature close'); - } - function errInvalidArg(name, expected, actual) { - return codedError( - TypeError, - 'ERR_INVALID_ARG_TYPE', - 'The "' + name + '" argument must be ' + expected + '. Received ' + describeType(actual), - ); + return nodeError(ErrorG, 'ERR_STREAM_PREMATURE_CLOSE', 'Premature close'); } - function errUnknownEncoding(encoding) { - return codedError(TypeErrorG, 'ERR_UNKNOWN_ENCODING', 'Unknown encoding: ' + encoding); + // node's AbortError is its own class (name 'AbortError'); this keeps the plain-Error + // shape the module has always produced — not an ERR_* constructor, so not moved. + function abortError() { + var err = new ErrorG('The operation was aborted'); + err.code = 'ABORT_ERR'; + return err; } // node's chunk contract, verified on 24.18.1: a string, or ANY ArrayBuffer view — @@ -90,7 +83,7 @@ // directions: it rejects DataView and Int16Array, which node accepts, and it accepts // `Object.create(Uint8Array.prototype)`, which node rejects. ArrayBuffer.isView is the // whole test, and it also refuses a bare ArrayBuffer. - var CHUNK_EXPECTED = 'of type string or an instance of Buffer, TypedArray, or DataView'; + var CHUNK_EXPECTED = ['string', 'Buffer', 'TypedArray', 'DataView']; function isBytesChunk(chunk) { return ArrayBufferIsView(chunk); } @@ -133,7 +126,7 @@ // `write(5, 'bogus')` reports the encoding and not the chunk type. 'buffer' is node's // internal marker and is always allowed through. function validateEncoding(encoding) { - if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) throw errUnknownEncoding(encoding); + if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) throw ERR_UNKNOWN_ENCODING(encoding); } // --- Stream base --------------------------------------------------------------- @@ -250,7 +243,7 @@ } else { // Unlike write(), a bad push is an ASYNC 'error' on node, not a throw. The // asymmetry is node's; do not "fix" it into a throw. - errorOrDestroy(this, errInvalidArg('chunk', CHUNK_EXPECTED, chunk)); + errorOrDestroy(this, ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk)); return false; } if (s.decoder) chunk = s.decoder.write(chunk); @@ -676,7 +669,7 @@ } else if (iterable && typeof iterable[Symbol.iterator] === 'function') { iterator = iterable[Symbol.iterator](); } else { - throw errInvalidArg('iterable', 'an iterable', iterable); + throw ERR_INVALID_ARG_TYPE('iterable', 'Iterable', iterable); } return iterator; } @@ -829,7 +822,7 @@ chunk = bytesToBuffer(chunk); encoding = 'buffer'; } else { - throw errInvalidArg('chunk', CHUNK_EXPECTED, chunk); + throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk); } } if (w.ending) { @@ -973,10 +966,10 @@ if (w.ending || w.destroyed) { if (typeof cb === 'function') { var err = w.finished - ? codedError( - Error, + ? nodeError( + ErrorG, 'ERR_STREAM_ALREADY_FINISHED', - 'end() called after stream was finished', + 'Cannot call end after a stream was finished', ) : w.destroyed ? errDestroyed('end') @@ -1141,12 +1134,12 @@ function addAbortSignal(signal, stream) { if (signal.aborted) { nextTick(function () { - stream.destroy(codedError(ErrorG, 'ABORT_ERR', 'The operation was aborted')); + stream.destroy(abortError()); }); return; } signal.addEventListener('abort', function () { - stream.destroy(codedError(ErrorG, 'ABORT_ERR', 'The operation was aborted')); + stream.destroy(abortError()); }); } @@ -1268,7 +1261,8 @@ opts = {}; } opts = opts || {}; - if (!isStreamLike(stream)) throw errInvalidArg('stream', 'a Stream', stream); + if (!isStreamLike(stream)) + throw ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream); var readable = opts.readable !== false && typeof stream.readable === 'boolean' ? stream.readable : false; @@ -1321,14 +1315,29 @@ return cleanup; } + // node's list for a pipeline stage. Lava accepts only the Stream arm so far (functions, + // iterables and web streams are not piped yet), but the error names what node accepts. + var PIPELINE_BODY = [ + 'function', + 'Blob', + 'ReadableStream', + 'WritableStream', + 'Stream', + 'Iterable', + 'AsyncIterable', + 'Promise', + '{ readable, writable } pair', + ]; + function pipeline() { var args = []; for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; var cb = args.pop(); - if (typeof cb !== 'function') throw errInvalidArg('callback', 'of type function', cb); + if (typeof cb !== 'function') + throw ERR_INVALID_ARG_TYPE('streams[stream.length - 1]', 'function', cb); if (Array.isArray(args[0]) && args.length === 1) args = args[0]; if (args.length < 2) { - throw codedError(ErrorG, 'ERR_MISSING_ARGS', 'The "streams" argument is required'); + throw errors.ERR_MISSING_ARGS('streams'); } var streams = args; @@ -1346,7 +1355,7 @@ } for (var k = 0; k < streams.length; k++) { - if (!isStreamLike(streams[k])) throw errInvalidArg('stream', 'a Stream', streams[k]); + if (!isStreamLike(streams[k])) throw ERR_INVALID_ARG_TYPE('body', PIPELINE_BODY, streams[k]); streams[k].on('error', done); if (k > 0) streams[k - 1].pipe(streams[k]); } diff --git a/pkg/runtime/js/node/string_decoder.js b/pkg/runtime/js/node/string_decoder.js index 84a37c1..36067e1 100644 --- a/pkg/runtime/js/node/string_decoder.js +++ b/pkg/runtime/js/node/string_decoder.js @@ -5,6 +5,8 @@ (function (require, module, exports) { 'use strict'; + var ERR_UNKNOWN_ENCODING = require('internal/errors').ERR_UNKNOWN_ENCODING; + function normalizeEncoding(enc) { var e = (enc || 'utf8').toLowerCase(); switch (e) { @@ -28,7 +30,7 @@ case 'hex': return 'hex'; default: - throw new Error('Unknown encoding: ' + enc); + throw ERR_UNKNOWN_ENCODING(enc); } } diff --git a/pkg/runtime/js/node/url.js b/pkg/runtime/js/node/url.js index 3149a7c..219baee 100644 --- a/pkg/runtime/js/node/url.js +++ b/pkg/runtime/js/node/url.js @@ -77,6 +77,11 @@ // Free globals / statics, captured pristine at module-eval. var StringG = String; var NumberG = P.Number; + var TypeErrorG = P.TypeError; + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var nativeError = errors.nativeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; var StringFromCharCode = String.fromCharCode; var StringFromCodePoint = String.fromCodePoint; var MathFloor = Math.floor; @@ -1401,31 +1406,23 @@ var baseRecord; if (base !== undefined && base !== null) { baseRecord = basicURLParse(StringG(base)); - if (baseRecord === FAILURE) throw invalidURL(base); + if (baseRecord === FAILURE) throw invalidURL(nativeError, input, base); } var record = basicURLParse(StringG(input), baseRecord); - if (record === FAILURE) throw invalidURL(input); + if (record === FAILURE) { + throw invalidURL(nativeError, input, baseRecord === undefined ? undefined : base); + } return record; } - function invalidURL(input) { - var err = new TypeError('Invalid URL'); - err.code = 'ERR_INVALID_URL'; + // ERR_INVALID_URL in node's two shapes: the constructor's parse runs in C++, so + // String(e) is `TypeError: Invalid URL`; the href setter throws from JS, so it is + // `TypeError [ERR_INVALID_URL]: Invalid URL`. Both carry `input`, and `base` when the + // parse ran against one (node 26.13.0, case 76). + function invalidURL(make, input, base) { + var err = make(TypeErrorG, 'ERR_INVALID_URL', 'Invalid URL'); err.input = StringG(input); - return err; - } - - // ERR_INVALID_ARG_TYPE, matching Node's message shape closely enough that the - // `.code` (what callers branch on) is identical. - function invalidArgType(name, expected, value) { - var received = - value === null - ? 'null' - : 'type ' + typeof value + ' (' + (typeof value === 'object' ? '' : value) + ')'; - var err = new TypeError( - 'The "' + name + '" argument must be ' + expected + '. Received ' + received, - ); - err.code = 'ERR_INVALID_ARG_TYPE'; + if (base !== undefined) err.base = StringG(base); return err; } @@ -1846,7 +1843,7 @@ }, set: function (value) { var record = basicURLParse(toUSVString(value)); - if (record === FAILURE) throw invalidURL(value); + if (record === FAILURE) throw invalidURL(nodeError, value); this[kRecord] = record; refreshSearchParams(this); }, @@ -2072,7 +2069,7 @@ var href; if (typeof input === 'string') href = new URL(input).href; else if (input instanceof URL) href = input.href; - else throw invalidArgType('path', 'of type string or an instance of URL', input); + else throw ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], input); return fileURLToPathFromHref(href); } @@ -2080,9 +2077,7 @@ // node:sqlite, which call it on an href, behave exactly as before). function fileURLToPathFromHref(url) { if (url.slice(0, 5).toLowerCase() !== 'file:') { - var schemeErr = new TypeError('The URL must be of scheme file'); - schemeErr.code = 'ERR_INVALID_URL_SCHEME'; - throw schemeErr; + throw nodeError(TypeErrorG, 'ERR_INVALID_URL_SCHEME', 'The URL must be of scheme file'); } var isWindows = process.platform === 'win32'; var rest = url.slice(5); @@ -2099,8 +2094,15 @@ var query = rest.indexOf('?'); if (query !== -1) rest = rest.slice(0, query); if (reTest(PERCENT_2F_RE, rest) || (isWindows && reTest(PERCENT_5C_RE, rest))) { - var sepErr = new TypeError('File URL path must not include encoded / or \\ characters'); - sepErr.code = 'ERR_INVALID_FILE_URL_PATH'; + // node words the two platforms differently and attaches the href as `input`. + var sepErr = nodeError( + TypeErrorG, + 'ERR_INVALID_FILE_URL_PATH', + isWindows + ? 'File URL path must not include encoded \\ or / characters' + : 'File URL path must not include encoded / characters', + ); + sepErr.input = url; throw sepErr; } @@ -2111,26 +2113,24 @@ } var letter = StringPrototypeCharCodeAt(winPath, 1) | 0x20; if (letter < 97 || letter > 122 || StringPrototypeCharAt(winPath, 2) !== ':') { - var pathErr = new TypeError('File URL path must be absolute'); - pathErr.code = 'ERR_INVALID_FILE_URL_PATH'; - throw pathErr; + throw nodeError(TypeErrorG, 'ERR_INVALID_FILE_URL_PATH', 'File URL path must be absolute'); } return winPath.slice(1); } if (host !== '' && !isLocalhost) { - var hostErr = new TypeError( + throw nodeError( + TypeErrorG, + 'ERR_INVALID_FILE_URL_HOST', 'File URL host must be "localhost" or empty on ' + process.platform, ); - hostErr.code = 'ERR_INVALID_FILE_URL_HOST'; - throw hostErr; } return decodeURIComponentG(rest); } function pathToFileURL(filepath) { if (typeof filepath !== 'string') { - throw invalidArgType('path', 'of type string', filepath); + throw ERR_INVALID_ARG_TYPE('path', 'string', filepath); } var path = require('node:path'); var isWindows = process.platform === 'win32'; diff --git a/pkg/runtime/js/node/util.js b/pkg/runtime/js/node/util.js index 0d7e2e5..ababde1 100644 --- a/pkg/runtime/js/node/util.js +++ b/pkg/runtime/js/node/util.js @@ -8,6 +8,9 @@ // Captured for debugEnvPattern below, which replaced three global-flag replaces on the // NODE_DEBUG path. This file is not otherwise hardened — see the ratchet baseline. var UtilPrimordials = require('internal/primordials'); + var errors = require('internal/errors'); + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_INVALID_ARG_VALUE = errors.ERR_INVALID_ARG_VALUE; var StringPrototypeCharCodeAt = UtilPrimordials.StringPrototypeCharCodeAt; var StringPrototypeSlice = UtilPrimordials.StringPrototypeSlice; var StringPrototypeToUpperCase = UtilPrimordials.StringPrototypeToUpperCase; @@ -586,20 +589,9 @@ swapColors: [7, 27], }; - // ERR_INVALID_ARG_TYPE and ERR_INVALID_ARG_VALUE are both TypeErrors in Node, so - // `err instanceof TypeError` works alongside `err.code`. - function styleTextError(code, message) { - var e = new TypeError(message); - e.code = code; - return e; - } - function styleText(format, text, options) { if (typeof text !== 'string') { - throw styleTextError( - 'ERR_INVALID_ARG_TYPE', - 'The "text" argument must be of type string. Received ' + typeof text, - ); + throw ERR_INVALID_ARG_TYPE('text', 'string', text); } var formats = Array.isArray(format) ? format : [format]; var open = ''; @@ -610,10 +602,9 @@ // Read from the live inspectColors so a caller-added/overridden style is honored. var codes = typeof f === 'string' ? inspectColors[f] : undefined; if (codes === undefined) { - throw styleTextError( - 'ERR_INVALID_ARG_VALUE', - "The argument 'format' must be a valid style. Received " + inspect(f, {}, [], 1), - ); + // node's reason lists ObjectKeys(inspect.colors); Lava's colors table has a + // different key set (aliases enumerable), so the list would not match anyway. + throw ERR_INVALID_ARG_VALUE('format', f, 'must be a valid style'); } open += '\x1b[' + codes[0] + 'm'; close = '\x1b[' + codes[1] + 'm' + close; // closes unwind in reverse @@ -627,11 +618,7 @@ // is rejected — Node only type-checks it on this path. if (validateStream) { if (typeof validateStream !== 'boolean') { - throw styleTextError( - 'ERR_INVALID_ARG_TYPE', - 'The "options.validateStream" property must be of type boolean. Received ' + - typeof validateStream, - ); + throw ERR_INVALID_ARG_TYPE('options.validateStream', 'boolean', validateStream); } var stream = options.stream !== undefined @@ -659,10 +646,7 @@ function stripVTControlCharacters(str) { if (typeof str !== 'string') { - throw styleTextError( - 'ERR_INVALID_ARG_TYPE', - 'The "str" argument must be of type string. Received ' + typeof str, - ); + throw ERR_INVALID_ARG_TYPE('str', 'string', str); } // Short-circuit when there is no ESC (7-bit) or CSI (8-bit) introducer at all. if (str.indexOf('\u001B') === -1 && str.indexOf('\u009B') === -1) return str; @@ -833,19 +817,6 @@ return logger; } - // Node's ERR_INVALID_ARG_TYPE "Received ..." clause: a primitive shows `type - // ()`, null/undefined as-is, an object `an instance of `. - function receivedType(v) { - if (v === null) return 'null'; - var t = typeof v; - if (t === 'undefined') return 'undefined'; - if (t === 'object') { - var name = v.constructor && v.constructor.name ? v.constructor.name : 'Object'; - return 'an instance of ' + name; - } - return 'type ' + t + ' (' + String(v) + ')'; - } - function isAbortSignalLike(s) { return ( s !== null && @@ -862,17 +833,15 @@ // resource is GC'd; the weak ref is not observable here, but the type check is. async function aborted(signal, resource) { if (!isAbortSignalLike(signal)) { - throw styleTextError( + // node's webidl AbortSignal converter: a bare TypeError, no Received tail. + throw errors.nativeError( + UtilPrimordials.TypeError, 'ERR_INVALID_ARG_TYPE', - 'The "signal" argument must be an instance of AbortSignal. Received ' + - receivedType(signal), + 'signal is not of type AbortSignal.', ); } if (resource === null || (typeof resource !== 'object' && typeof resource !== 'function')) { - throw styleTextError( - 'ERR_INVALID_ARG_TYPE', - 'The "resource" argument must be of type object. Received ' + receivedType(resource), - ); + throw ERR_INVALID_ARG_TYPE('resource', 'object', resource); } if (signal.aborted) return undefined; return new Promise(function (resolve) { diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 81fadf4..b8ce379 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -694,20 +694,20 @@ }, { "name": "62-fs-read-buffer dies when ERR_INVALID_ARG_TYPE loses node's object branch", - "why": "node's determineSpecificType renders an object receiver as `an instance of `, not `type object ()`. The reachable trigger is fs.readFile(path, options) with a forgotten callback. expect_detail keys on the ESCAPING text (Received {}), not the case assertion: assert.throws re-surfaces the original TypeError on a message mismatch here, so the assertion text never reaches the diff.", - "source": "pkg/runtime/js/node/fs.js", - "find": " if (typeof ctor === 'function' && ctor.name) return 'an instance of ' + ctor.name;", + "why": "node's determineSpecificType renders an object receiver as `an instance of `, not the util.inspect fallback (`[Object]` at depth -1) \u2014 the branch now lives in internal/errors' determineSpecificType. The reachable trigger is fs.readFile(path, options) with a forgotten callback. expect_detail keys on the ESCAPING text (Received {}), not the case assertion: assert.throws re-surfaces the original TypeError on a message mismatch here, so the assertion text never reaches the diff.", + "source": "pkg/runtime/js/internal/errors.js", + "find": " if (ctor && 'name' in ctor) return 'an instance of ' + ctor.name;", "replace": "", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", "expect_detail": [ - "Received {}" + "Received [Object]" ] }, { "name": "62-fs-read-buffer dies when the Received string stops being truncated", "why": "node cuts a string longer than 28 chars to its first 25 plus '...' before inspecting it into the Received clause.", - "source": "pkg/runtime/js/node/fs.js", - "find": " if (typeof shown === 'string' && shown.length > RECEIVED_STR_MAX + 3) {\n shown = StringPrototypeSlice(shown, 0, RECEIVED_STR_MAX) + '...';\n }", + "source": "pkg/runtime/js/internal/errors.js", + "find": " if (value.length > 28) value = StringPrototypeSlice(value, 0, 25) + '...';", "replace": "", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", "expect_detail": [ @@ -852,8 +852,8 @@ "name": "64-stream-write dies when a view is not normalized to a Buffer", "why": "THE regression that got #326's attempt reverted. node hands _write a Buffer with encoding 'buffer' for every view; without the conversion `chunk.length` is undefined for a DataView, writableLength goes NaN, and 'drain' can never fire again because NaN === 0 is false.", "source": "pkg/runtime/js/node/stream.js", - "find": " chunk = bytesToBuffer(chunk);\n encoding = 'buffer';\n } else {\n throw errInvalidArg('chunk', CHUNK_EXPECTED, chunk);", - "replace": " encoding = 'buffer';\n } else {\n throw errInvalidArg('chunk', CHUNK_EXPECTED, chunk);", + "find": " chunk = bytesToBuffer(chunk);\n encoding = 'buffer';\n } else {\n throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk);", + "replace": " encoding = 'buffer';\n } else {\n throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk);", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", "expect_detail": [ "false, undefined" @@ -1669,14 +1669,14 @@ "expect_detail": "is out of date" }, { - "name": "70-buffer-size-inspect-parity dies when assertSize splices a bare type name", - "why": "node prints `must be of type number` for Buffer.alloc('5'); a bare 'number' prints `must be number`.", + "name": "70-buffer-size-inspect-parity dies when assertSize names a class instead of the type", + "why": "internal/errors classifies `expected` the way node does: 'number' is a type (`must be of type number`), a capitalized 'Number' a class (`must be an instance of Number`). The call site must pass the type.", "source": "pkg/runtime/js/node/buffer.js", - "find": "throw errInvalidArgType('size', 'of type number', size);", - "replace": "throw errInvalidArgType('size', 'number', size);", + "find": "throw ERR_INVALID_ARG_TYPE('size', 'number', size);", + "replace": "throw ERR_INVALID_ARG_TYPE('size', 'Number', size);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", "expect_detail": [ - "The \"size\" argument must be number" + "The \"size\" argument must be an instance of Number" ] }, { @@ -1689,25 +1689,25 @@ "expect_detail": "node:buffer has no SlowBuffer" }, { - "name": "70-buffer-size-inspect-parity dies when checkBounds splices a bare type name", - "why": "every fixed-width read/write reports its offset type error here; node says `must be of type number`.", + "name": "70-buffer-size-inspect-parity dies when checkBounds names a class instead of the type", + "why": "internal/errors classifies `expected` the way node does: 'number' is a type (`must be of type number`), a capitalized 'Number' a class (`must be an instance of Number`). The call site must pass the type.", "source": "pkg/runtime/js/node/buffer.js", - "find": "throw errInvalidArgType('offset', 'of type number', offset);", - "replace": "throw errInvalidArgType('offset', 'number', offset);", + "find": "throw ERR_INVALID_ARG_TYPE('offset', 'number', offset);", + "replace": "throw ERR_INVALID_ARG_TYPE('offset', 'Number', offset);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", "expect_detail": [ - "The \"offset\" argument must be number" + "The \"offset\" argument must be an instance of Number" ] }, { - "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check splices a bare type name", - "why": "Buffer.concat(list, '5') must say `The \"length\" argument must be of type number`.", + "name": "70-buffer-size-inspect-parity dies when Buffer.concat's length check names a class instead of the type", + "why": "internal/errors classifies `expected` the way node does: 'number' is a type (`must be of type number`), a capitalized 'Number' a class (`must be an instance of Number`). The call site must pass the type.", "source": "pkg/runtime/js/node/buffer.js", - "find": "throw errInvalidArgType('length', 'of type number', totalLength);", - "replace": "throw errInvalidArgType('length', 'number', totalLength);", + "find": "throw ERR_INVALID_ARG_TYPE('length', 'number', totalLength);", + "replace": "throw ERR_INVALID_ARG_TYPE('length', 'Number', totalLength);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", "expect_detail": [ - "The \"length\" argument must be number" + "The \"length\" argument must be an instance of Number" ] }, { @@ -1780,7 +1780,7 @@ "name": "72-events-parity dies when once() stops validating options", "why": "node's events.once rejects a non-object options with ERR_INVALID_ARG_TYPE; without the check once(ee, 'foo', 5) just waits.", "source": "pkg/runtime/js/node/events.js", - "find": " return P.Promise.reject(errOptionsType(options));", + "find": " return P.Promise.reject(ERR_INVALID_ARG_TYPE('options', 'object', options));", "replace": " options = undefined;", "gate": "compat:tests/node-compat/cases/72-events-parity.js", "expect_detail": [ @@ -2342,6 +2342,72 @@ "replace": " var toStringM = Object.prototype.toString; var ObjectPrototypeToString = function (v) { return toStringM.call(v); };", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "util/types: ok [\"Object.prototype\"]" + }, + { + "name": "76-coded-error-shape dies when nodeError stops splicing the code into String(e)", + "why": "node's JS-layer errors stringify as `TypeError [ERR_X]: msg` through an inherited toString; a bare TypeError with a code property prints `TypeError: msg`, which is the C++ shape.", + "source": "pkg/runtime/js/internal/errors.js", + "find": " ObjectSetPrototypeOf(e, protoFor(Base, code));", + "replace": "", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "TypeError: The \"size\" argument must be of type number. Received type string ('5')" + ] + }, + { + "name": "76-coded-error-shape dies when a long string is not cut to 25 chars", + "why": "determineSpecificType cuts a string longer than 28 to its first 25 plus '...'; exactly 28 is kept whole (the 28/29 pair in the case pins the boundary).", + "source": "pkg/runtime/js/internal/errors.js", + "find": " if (value.length > 28) value = StringPrototypeSlice(value, 0, 25) + '...';", + "replace": "", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa')" + ] + }, + { + "name": "76-coded-error-shape dies when a string with a quote is not JSON-quoted", + "why": "node switches to JSON.stringify quoting when the (cut) string contains a single quote: `type string (\"it's\")`.", + "source": "pkg/runtime/js/internal/errors.js", + "find": " if (StringPrototypeIndexOf(value, \"'\", 0) === -1) return \"type string ('\" + value + \"')\";", + "replace": " return \"type string ('\" + value + \"')\";", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "type string ('it's')" + ] + }, + { + "name": "76-coded-error-shape dies when an object with a constructor is not 'an instance of'", + "why": "node names an object by its constructor; only a constructor-less object falls back to util.inspect at depth -1.", + "source": "pkg/runtime/js/internal/errors.js", + "find": " if (ctor && 'name' in ctor) return 'an instance of ' + ctor.name;", + "replace": "", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "Received [Array]" + ] + }, + { + "name": "76-coded-error-shape dies when a large integer loses node's _ grouping", + "why": "ERR_OUT_OF_RANGE groups an integer past +-2 ** 32 with `_` every three digits (1_099_511_627_776); buffer, crypto and os all reach it.", + "source": "pkg/runtime/js/internal/errors.js", + "find": " received = addNumericalSeparator(StringP(input));", + "replace": " received = StringP(input);", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "Received 1099511627776" + ] + }, + { + "name": "76-coded-error-shape dies when the URL constructor throws the JS shape", + "why": "node parses URLs in C++, so `new URL('nope')` stringifies as `TypeError: Invalid URL` with no [code]; only the href setter throws the JS shape.", + "source": "pkg/runtime/js/node/url.js", + "find": " if (baseRecord === FAILURE) throw invalidURL(nativeError, input, base);", + "replace": " if (baseRecord === FAILURE) throw invalidURL(nodeError, input, base);", + "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", + "expect_detail": [ + "url invalid base | TypeError | ERR_INVALID_URL | TypeError [ERR_INVALID_URL]: Invalid URL" + ] } ] } diff --git a/tests/node-compat/cases/76-coded-error-shape.js b/tests/node-compat/cases/76-coded-error-shape.js new file mode 100644 index 0000000..2de1c17 --- /dev/null +++ b/tests/node-compat/cases/76-coded-error-shape.js @@ -0,0 +1,294 @@ +// Coded errors built by the builtins: class, `code`, message and String(e), through each +// consumer's PUBLIC API, byte-identical with node 26.13.0. +// +// Every builtin used to carry its own copy of node's ERR_* constructors (describeType, +// invalidArgType, outOfRange, …), and the copies disagreed with node and with each other: +// `path.join(1)` said "Received type number" with no value, crypto rendered a string as +// "type string" with no value and dropped node's `_` grouping on large integers, dns +// printed `undefined` as "type undefined (undefined)", url printed an object as +// "type object ()". They now all build through internal/errors, which this case pins. +// +// Two error SHAPES exist in node and both are observable through String(e): +// - errors thrown by node's JS layer (`new ERR_X(...)`) stringify as +// `TypeError [ERR_X]: msg` — the code is spliced in by an inherited toString; +// - errors thrown by node's C++ layer (THROW_ERR_X: URL parsing, the encoding binding, +// sqlite, timingSafeEqual) are a bare `TypeError` with a `code` own property and +// stringify as `TypeError: msg`. +// Lava used to produce the second shape everywhere. +// +// Not covered, because the remaining difference is not error construction: +// - a null-prototype object as the received value — node prints +// `[Object: null prototype] {}`, Lava's util.inspect gives `{}` (#331); +// - util.styleText's invalid-format list — it is built from inspect.colors, whose key +// set differs between the runtimes; +// - fs.writeFileSync(path, 5) — Lava does not validate `data` at all yet. +// http/net/https sites are Linux-only natives and live in 77-coded-error-shape-net.js. +'use strict'; + +function show(label, e) { + const own = Object.getOwnPropertyNames(e).filter( + (k) => !['stack', 'message', 'code', 'line', 'column', 'sourceURL'].includes(k), + ); + console.log( + label + + ' | ' + + e.constructor.name + + ' | ' + + e.code + + ' | ' + + String(e) + + (own.length ? ' | ' + own.map((k) => k + '=' + String(e[k])).join(',') : ''), + ); +} +function t(label, f) { + try { + f(); + console.log(label + ' | NO THROW'); + } catch (e) { + show(label, e); + } +} +const pending = []; +function a(label, f) { + pending.push([label, f]); +} + +class Foo {} +function named() {} +const long = 'abcdefghijklmnopqrstuvwxyz0123456789'; + +// --- determineSpecificType, every branch, through Buffer.alloc's "size" --- +t('received string', () => Buffer.alloc('5')); +t('received string quote', () => Buffer.alloc("it's")); +t('received string 28', () => Buffer.alloc('a'.repeat(28))); +t('received string 29', () => Buffer.alloc('a'.repeat(29))); +t('received string long', () => Buffer.alloc(long)); +t('received boolean', () => Buffer.alloc(true)); +t('received bigint', () => Buffer.alloc(5n)); +t('received symbol', () => Buffer.alloc(Symbol('s'))); +t('received function', () => Buffer.alloc(named)); +t('received anonymous fn', () => Buffer.alloc(() => {})); +t('received class', () => Buffer.alloc(Foo)); +t('received instance', () => Buffer.alloc(new Foo())); +t('received array', () => Buffer.alloc([])); +t('received null', () => Buffer.alloc(null)); +t('received undefined', () => Buffer.alloc(undefined)); +t('received ctor-less', () => Buffer.alloc({ constructor: undefined })); +t('received -0', () => Buffer.from(-0)); +t('received NaN', () => Buffer.from(NaN)); + +// --- the shape itself --- +try { + Buffer.alloc('x'); +} catch (e) { + console.log( + 'shape node-error', + e.name, + e instanceof TypeError, + e.constructor === TypeError, + Object.keys(e).join(','), + Object.prototype.hasOwnProperty.call(e, 'toString'), + Object.getOwnPropertyDescriptor(e, 'message').enumerable, + ); +} +try { + new URL('nope'); +} catch (e) { + console.log( + 'shape native-error', + e.name, + e instanceof TypeError, + e.constructor === TypeError, + Object.keys(e).join(','), + ); +} + +// --- buffer --- +t('buf from num', () => Buffer.from(5)); +t('buf readUInt8 str', () => Buffer.alloc(4).readUInt8('1')); +t('buf readUInt8 frac', () => Buffer.alloc(4).readUInt8(1.5)); +t('buf readUInt8 oob', () => Buffer.alloc(4).readUInt8(9)); +t('buf readUInt32 oob', () => Buffer.alloc(2).readUInt32LE(0)); +t('buf writeUInt8 300', () => Buffer.alloc(4).writeUInt8(300)); +t('buf writeUInt32 2^40', () => Buffer.alloc(4).writeUInt32LE(2 ** 40)); +t('buf writeInt32 -2^40', () => Buffer.alloc(4).writeInt32LE(-(2 ** 40))); +t('buf writeIntLE 6', () => Buffer.alloc(8).writeIntLE(2 ** 50, 0, 6)); +t('buf writeBigInt64', () => Buffer.alloc(8).writeBigInt64LE(2n ** 70n)); +t('buf writeBigUInt64 -1n', () => Buffer.alloc(8).writeBigUInt64LE(-1n)); +t('buf readIntLE bl', () => Buffer.alloc(8).readIntLE(0, 7)); +t('buf toString enc', () => Buffer.from('a').toString('bogus')); +t('buf swap16', () => Buffer.alloc(3).swap16()); +t('buf fill bad hex', () => Buffer.alloc(2).fill('zz', 'hex')); +t('buf concat', () => Buffer.concat(5)); +t('buf concat el', () => Buffer.concat([5])); +t('buf byteLength', () => Buffer.byteLength(5)); +t('buf compare', () => Buffer.compare(1, 2)); +t('buf write num', () => Buffer.alloc(1).write(5)); +t('buf atob', () => atob()); +t('buf fill empty', () => Buffer.alloc(2).fill(Buffer.alloc(0))); +t('buf indexOf obj', () => Buffer.alloc(2).indexOf({})); +t('buf createObjectURL', () => URL.createObjectURL(5)); +t('buf revokeObjectURL', () => URL.revokeObjectURL()); + +// --- encoding --- +t('td label', () => new TextDecoder('bogus')); +t('td opts', () => new TextDecoder('utf8', 5)); +t('td decode', () => new TextDecoder().decode(5)); +t('td fatal', () => new TextDecoder('utf8', { fatal: true }).decode(new Uint8Array([0xff]))); +t('te encodeInto', () => new TextEncoder().encodeInto('a', 5)); + +// --- path --- +const path = require('path'); +t('path join', () => path.join(1)); +t('path basename', () => path.basename('a', 5)); +t('path resolve obj', () => path.resolve({})); +t('path matchesGlob', () => path.matchesGlob(1, '*')); +t('path win32 join', () => path.win32.join(null)); + +// --- url --- +const url = require('url'); +t('url invalid', () => new URL('nope')); +t('url invalid base', () => new URL('x', 'nope')); +t('url fileURLToPath num', () => url.fileURLToPath(5)); +t('url fileURLToPath obj', () => url.fileURLToPath({})); +t('url fileURLToPath null', () => url.fileURLToPath(null)); +t('url fileURLToPath scheme', () => url.fileURLToPath('http://x/')); +t('url fileURLToPath host', () => url.fileURLToPath('file://host/x')); +t('url href setter', () => { + new URL('http://a/').href = 'nope'; +}); +t('url fileURLToPath %2F', () => url.fileURLToPath('file:///a%2Fb')); +t('url pathToFileURL', () => url.pathToFileURL(5)); + +// --- crypto --- +const crypto = require('crypto'); +t('crypto randomBytes str', () => crypto.randomBytes('x')); +t('crypto randomBytes neg', () => crypto.randomBytes(-1)); +t('crypto randomBytes 2^40', () => crypto.randomBytes(2 ** 40)); +t('crypto update num', () => crypto.createHash('sha256').update(5)); +t('crypto update bigint', () => crypto.createHmac('sha256', 'k').update(5n)); +t('crypto finalized', () => { + const h = crypto.createHash('sha256'); + h.digest(); + h.digest(); +}); +t('crypto hmac digest', () => crypto.createHmac('bogus', 'k')); +t('crypto randomFillSync off', () => crypto.randomFillSync(Buffer.alloc(4), 'x')); +t('crypto randomFillSync off oob', () => crypto.randomFillSync(Buffer.alloc(4), 9)); +t('crypto randomFillSync size', () => crypto.randomFillSync(Buffer.alloc(4), 0, 9)); +t('crypto randomFillSync buf', () => crypto.randomFillSync(5)); +t('crypto timingSafeEqual len', () => crypto.timingSafeEqual(Buffer.alloc(1), Buffer.alloc(2))); +t('crypto timingSafeEqual type', () => crypto.timingSafeEqual(1, Buffer.alloc(2))); +t('crypto timingSafeEqual type b', () => crypto.timingSafeEqual(Buffer.alloc(1), 'x')); +t('crypto scrypt pair', () => crypto.scryptSync('p', 's', 8, { N: 16, cost: 16 })); +t('crypto scrypt keylen', () => crypto.scryptSync('p', 's', -1)); +t('crypto scrypt params', () => crypto.scryptSync('p', 's', 8, { N: 3 })); +t('crypto pbkdf2 iter', () => crypto.pbkdf2Sync('p', 's', 'x', 8, 'sha256')); +t('crypto pbkdf2 iter range', () => crypto.pbkdf2Sync('p', 's', 0, 8, 'sha256')); +t('crypto pbkdf2 digest', () => crypto.pbkdf2Sync('p', 's', 1, 8, 5)); + +// --- os --- +const os = require('os'); +t('os getPriority str', () => os.getPriority('x')); +t('os getPriority frac', () => os.getPriority(1.5)); +t('os getPriority 2^40', () => os.getPriority(2 ** 40)); +t('os setPriority range', () => os.setPriority(0, 100)); + +// --- util (+ parse_env, parse_args, mime) --- +const util = require('util'); +t('util styleText text', () => util.styleText('red', 1)); +t('util styleText vs', () => util.styleText('red', 'x', { validateStream: 1 })); +t('util stripVT', () => util.stripVTControlCharacters(1)); +a('util aborted signal', () => util.aborted(5, {})); +a('util aborted resource', () => util.aborted(new AbortController().signal, 5)); +t('util parseEnv', () => util.parseEnv(5)); +t('util parseEnv buf', () => util.parseEnv(Buffer.from('a'))); +t('util parseArgs unknown', () => util.parseArgs({ args: ['--x'], options: {} })); +t('util parseArgs value', () => + util.parseArgs({ args: ['--x'], options: { x: { type: 'string' } } }), +); +t('util parseArgs positional', () => util.parseArgs({ args: ['p'], options: {} })); +t('util parseArgs options', () => util.parseArgs({ args: [], options: 5 })); +t('util MIMEType', () => new util.MIMEType('bad')); + +// --- events --- +const events = require('events'); +const { EventEmitter, once } = events; +t('events on emitter', () => events.on(5, 'x')); +t('events on signal', () => events.on(new EventEmitter(), 'x', { signal: 5 })); +t('events addAbortListener', () => events.addAbortListener(5, () => {})); +t('events addAbortListener fn', () => events.addAbortListener(new AbortController().signal, 5)); +t('events setMaxListeners str', () => new EventEmitter().setMaxListeners('x')); +t('events setMaxListeners neg', () => new EventEmitter().setMaxListeners(-1)); +t('events setMaxListeners obj', () => new EventEmitter().setMaxListeners({})); +a('events once emitter', () => once(5, 'x')); +a('events once options', () => once(new EventEmitter(), 'x', 5)); +a('events once signal', () => once(new EventEmitter(), 'x', { signal: 5 })); +a('events once options long', () => once(new EventEmitter(), 'x', long)); + +// --- stream / string_decoder --- +const { Writable, pipeline, finished } = require('stream'); +const sink = () => new Writable({ write: (c, e, cb) => cb() }); +t('stream write null', () => sink().write(null)); +t('stream write num', () => sink().write(5)); +t('stream write enc', () => sink().write('a', 'bogus')); +t('stream pipeline cb', () => pipeline(5)); +t('stream finished', () => finished(5, () => {})); +t('stream pipeline missing', () => pipeline(() => {})); +a( + 'stream write after end', + () => + new Promise((resolve, reject) => { + const w = sink(); + w.on('error', reject); + w.end(); + w.write('x'); + }), +); +a( + 'stream already finished', + () => + new Promise((resolve, reject) => { + const w = sink(); + w.end(() => w.end((e) => (e ? reject(e) : resolve()))); + }), +); +t('string_decoder enc', () => new (require('string_decoder').StringDecoder)('bogus')); + +// --- fs --- +const fs = require('fs'); +t('fs readFile no cb', () => fs.readFile('x', { encoding: 'utf8' })); +t('fs readFileSync bool', () => fs.readFileSync(true)); +t('fs readFileSync enc', () => fs.readFileSync(__filename, { encoding: 'bogus' })); +t('fs readFileSync opts', () => fs.readFileSync(__filename, 5)); + +// --- sqlite (node's C++ layer: no [code] in String(e)) --- +const { DatabaseSync } = require('node:sqlite'); +t('sqlite path', () => new DatabaseSync(5)); +const db = new DatabaseSync(':memory:'); +t('sqlite sql', () => db.prepare(5)); +t('sqlite bigint', () => db.prepare('select ?').get(2n ** 70n)); +t('sqlite unknown named', () => db.prepare('select :a').get({ b: 1 })); +db.close(); +t('sqlite closed', () => db.prepare('select 1')); + +// --- dns (validation only; nothing is resolved) --- +const dns = require('dns'); +t('dns lookup host', () => dns.lookup(5, () => {})); +t('dns lookup family', () => dns.lookup('localhost', { family: 5 }, () => {})); +t('dns lookup family str', () => dns.lookup('localhost', { family: '4' }, () => {})); +t('dns lookup hints', () => dns.lookup('localhost', { hints: 999999 }, () => {})); +t('dns lookup all', () => dns.lookup('localhost', { all: 5 }, () => {})); +t('dns lookup cb', () => dns.lookup('localhost', {})); +t('dns order', () => dns.setDefaultResultOrder('x')); + +(async () => { + for (const [label, f] of pending) { + try { + await f(); + console.log(label + ' | NO THROW'); + } catch (e) { + show(label, e); + } + } +})(); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 6284531..1b47e98 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -30,9 +30,15 @@ "global": 9 }, "internal/encoding.js": { - "method": 1, + "method": 0, "invoke": 0, - "accessor": 3, + "accessor": 1, + "global": 0 + }, + "internal/errors.js": { + "method": 0, + "invoke": 0, + "accessor": 0, "global": 0 }, "internal/fetch.js": { @@ -45,19 +51,19 @@ "method": 24, "invoke": 0, "accessor": 0, - "global": 5 + "global": 4 }, "internal/parse_args.js": { "method": 0, "invoke": 0, "accessor": 0, - "global": 1 + "global": 0 }, "internal/parse_env.js": { "method": 0, "invoke": 0, - "accessor": 3, - "global": 2 + "accessor": 0, + "global": 0 }, "internal/primordials.js": { "method": 0, @@ -84,10 +90,10 @@ "global": 40 }, "node/buffer.js": { - "method": 41, + "method": 33, "invoke": 6, - "accessor": 22, - "global": 127 + "accessor": 20, + "global": 112 }, "node/console.js": { "method": 0, @@ -98,8 +104,8 @@ "node/crypto.js": { "method": 22, "invoke": 0, - "accessor": 20, - "global": 71 + "accessor": 17, + "global": 61 }, "node/diagnostics_channel.js": { "method": 5, @@ -108,10 +114,10 @@ "global": 13 }, "node/dns.js": { - "method": 9, + "method": 6, "invoke": 0, "accessor": 0, - "global": 8 + "global": 4 }, "node/dns/promises.js": { "method": 0, @@ -123,7 +129,7 @@ "method": 0, "invoke": 2, "accessor": 0, - "global": 12 + "global": 10 }, "node/fs.js": { "method": 0, @@ -135,31 +141,31 @@ "method": 21, "invoke": 5, "accessor": 0, - "global": 21 + "global": 18 }, "node/https.js": { "method": 0, "invoke": 0, "accessor": 0, - "global": 8 + "global": 2 }, "node/net.js": { "method": 2, "invoke": 2, "accessor": 3, - "global": 9 + "global": 8 }, "node/os.js": { "method": 12, "invoke": 0, "accessor": 0, - "global": 7 + "global": 5 }, "node/path.js": { "method": 161, "invoke": 0, "accessor": 0, - "global": 9 + "global": 7 }, "node/path/posix.js": { "method": 0, @@ -195,13 +201,13 @@ "method": 10, "invoke": 1, "accessor": 3, - "global": 20 + "global": 12 }, "node/stream.js": { "method": 42, "invoke": 9, "accessor": 25, - "global": 32 + "global": 28 }, "node/stream/promises.js": { "method": 1, @@ -219,7 +225,7 @@ "method": 14, "invoke": 0, "accessor": 3, - "global": 5 + "global": 4 }, "node/timers.js": { "method": 0, @@ -243,13 +249,13 @@ "method": 46, "invoke": 2, "accessor": 0, - "global": 24 + "global": 18 }, "node/util.js": { "method": 46, "invoke": 14, - "accessor": 15, - "global": 72 + "accessor": 12, + "global": 70 }, "node/util/types.js": { "method": 0, From 978abd7796ade421d0401b845635e1b8cc5e697a Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:34:45 +0200 Subject: [PATCH 27/61] fix(test): pin case-76 and 62 mutations to what the runner reports Case 76 printed each error on one line, so the part a mutation changes (the Received clause) sat past the 140 characters the runner shows per differing line and the recorded expect_detail could never match. Each error now prints its shape, message and Received clause on separate short lines, and the four entries key on the mutated clause. The 62 options-slot mutant still dies for its stated reason, but the escaping error now renders the function as node does (`function `), not the old `type function (...)`; the detail keys on the options message instead. Co-Authored-By: Claude Opus 5.5 --- tests/mutation-manifest.json | 16 ++++++++-------- tests/node-compat/cases/76-coded-error-shape.js | 12 +++++++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index b8ce379..224a67a 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -761,13 +761,13 @@ }, { "name": "62-fs-read-buffer dies when a function in the options slot is rejected", - "why": "node's getOptions treats a callback sitting in the options slot as absent; rejecting it turns readFileSync(p, fn) from a working binary read into a throw.", + "why": "node's getOptions treats a callback sitting in the options slot as absent; rejecting it turns readFileSync(p, fn) from a working binary read into a throw. The throw escapes the case uncaught (exit 1); its text renders the function the way node's determineSpecificType does (`function ` for an anonymous one), so the detail keys on the options message, not the old `type function (...)` rendering.", "source": "pkg/runtime/js/node/fs.js", "find": " if (options === null || options === undefined || typeof options === 'function') {", "replace": " if (options === null || options === undefined) {", "gate": "compat:tests/node-compat/cases/62-fs-read-buffer.js", "expect_detail": [ - "Received type function" + "must be one of type string or object. Received function" ] }, { @@ -2351,7 +2351,7 @@ "replace": "", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "TypeError: The \"size\" argument must be of type number. Received type string ('5')" + "received string | TypeError | ERR_INVALID_ARG_TYPE | TypeError:" ] }, { @@ -2362,7 +2362,7 @@ "replace": "", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa')" + "received: type string ('aaaaaaaaaaaaaaaaaaaaaaaaaaaaa')" ] }, { @@ -2373,7 +2373,7 @@ "replace": " return \"type string ('\" + value + \"')\";", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "type string ('it's')" + "received: type string ('it's')" ] }, { @@ -2384,7 +2384,7 @@ "replace": "", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "Received [Array]" + "received: [Array]" ] }, { @@ -2395,7 +2395,7 @@ "replace": " received = StringP(input);", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "Received 1099511627776" + "received: 1099511627776" ] }, { @@ -2406,7 +2406,7 @@ "replace": " if (baseRecord === FAILURE) throw invalidURL(nodeError, input, base);", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", "expect_detail": [ - "url invalid base | TypeError | ERR_INVALID_URL | TypeError [ERR_INVALID_URL]: Invalid URL" + "url invalid base | TypeError | ERR_INVALID_URL | TypeError [ERR_INVALID_URL]:" ] } ] diff --git a/tests/node-compat/cases/76-coded-error-shape.js b/tests/node-compat/cases/76-coded-error-shape.js index 2de1c17..21eb215 100644 --- a/tests/node-compat/cases/76-coded-error-shape.js +++ b/tests/node-compat/cases/76-coded-error-shape.js @@ -25,10 +25,17 @@ // http/net/https sites are Linux-only natives and live in 77-coded-error-shape-net.js. 'use strict'; +// One error prints as up to three SHORT lines — the shape (everything String(e) adds +// around the message), the message up to its "Received" clause, and that clause alone — +// so a difference in any one part stays inside the 140 characters the mutation runner +// shows per differing line, instead of being cut off the end of one long line. function show(label, e) { const own = Object.getOwnPropertyNames(e).filter( (k) => !['stack', 'message', 'code', 'line', 'column', 'sourceURL'].includes(k), ); + const str = String(e); + const msg = e.message; + const shape = str.endsWith(msg) ? str.slice(0, str.length - msg.length) : str; console.log( label + ' | ' + @@ -36,9 +43,12 @@ function show(label, e) { ' | ' + e.code + ' | ' + - String(e) + + shape + (own.length ? ' | ' + own.map((k) => k + '=' + String(e[k])).join(',') : ''), ); + const at = msg.lastIndexOf(' Received '); + console.log(' message: ' + (at === -1 ? msg : msg.slice(0, at))); + if (at !== -1) console.log(' received: ' + msg.slice(at + ' Received '.length)); } function t(label, f) { try { From 249157af3d1076c40ad9cb23172d1236f12164a7 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:38:58 +0200 Subject: [PATCH 28/61] refactor(runtime): split Blob, File and the blob: URL registry out of buffer.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit buffer.js (1677 lines) carried Blob, File and the object-URL registry. They now live in internal/blob.js and internal/file.js, the files node keeps them in, and buffer.js requires and re-exports them; the global installs and the node:buffer exports did not move. Blob reads require.pristineBuffer at call time, since buffer requires blob before the loader builds that table; a top- level require of buffer would be a cycle. Both are written as classes, the new rule in CLAUDE.md §5: node's Blob and File are classes, so prototype is now non-writable as in node, and File's [[Prototype]] is Blob. A call without `new` now throws JSC's own text, which neither matches node nor did the old text; declared @deviates and pinned by cmd/lava/blob_class_test.odin. Frames from Blob now read node:internal/blob. Byte identity: 70706 strings (every code unit, pairs, lone halves, 5000 seeded) give identical bytes before and after. Eager set 14 -> 16; noop.js +0.03-0.06 ms median (5 warmups + 50 interleaved runs per arm, two sweeps, darwin -o:speed). Ratchet totals lowered only. Pinned by tests/node-compat/cases/77-blob-file-module.js. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 7 + cmd/lava/blob_class_test.odin | 63 ++++ cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 5 +- pkg/runtime/js/bootstrap/loader.js | 3 +- pkg/runtime/js/internal/blob.js | 269 ++++++++++++++++++ pkg/runtime/js/internal/file.js | 63 ++++ pkg/runtime/js/node/buffer.js | 249 +--------------- tests/mutation-manifest.json | 20 +- .../node-compat/cases/76-coded-error-shape.js | 2 +- .../node-compat/cases/77-blob-file-module.js | 89 ++++++ tests/node-compat/pollution-baseline.json | 20 +- 12 files changed, 548 insertions(+), 244 deletions(-) create mode 100644 cmd/lava/blob_class_test.odin create mode 100644 pkg/runtime/js/internal/blob.js create mode 100644 pkg/runtime/js/internal/file.js create mode 100644 tests/node-compat/cases/77-blob-file-module.js diff --git a/CLAUDE.md b/CLAUDE.md index 909c984..12ed8cc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -277,6 +277,13 @@ Rules that keep this from becoming ceremony: fixed-arity wrappers (`ArrayPrototypePush1`/`Push2`) or plain indexed writes into a preallocated null-prototype array. Getting this wrong cost 1.43x on the TextDecoder JS path. +- Where node implements a constructor as a `class`, new or rewritten code does too + (`class Blob`, `class File extends Blob`). It gives node's non-writable + `prototype` and `extends`/`new.target` semantics. The one thing it cannot + match is the TypeError text for a call without `new`: JSC's own wording is an + engine trait, declared with `@deviates` and pinned by a Lava-only assertion, + never printed by an oracle case. Method enumerability stays explicit where node + makes it explicit. - Nothing transient lands on `globalThis`; natives arrive as the factory's fourth argument. - Errors are Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`) diff --git a/cmd/lava/blob_class_test.odin b/cmd/lava/blob_class_test.odin new file mode 100644 index 0000000..537135c --- /dev/null +++ b/cmd/lava/blob_class_test.odin @@ -0,0 +1,63 @@ +#+build linux, darwin +package main + +import "core:testing" +import lava "lava:pkg/runtime" +import eventloop "lava:pkg/runtime/eventloop" + +// Pins what the Blob/File class conversion deliberately leaves different from node, +// which the oracle case (tests/node-compat/cases/77-blob-file-module.js) cannot express +// because the two runtimes answer differently by construction: +// +// - Calling either without `new` throws the ENGINE's class-constructor TypeError. +// JSC: `Cannot call a class constructor Blob without |new|`; node (V8): +// `Class constructor Blob cannot be invoked without 'new'`. A class constructor +// cannot customise it, so the text is an engine trait; the oracle case compares +// only the error's class. If this message changes, JSC changed — or Blob stopped +// being a class, which is the regression this guards. +// - Blob.prototype's methods stay ENUMERABLE, as they were while Blob was a function +// with an assigned prototype. Class methods are not, so internal/blob.js re-marks +// them; node's enumerable set differs (size/type/textStream in, toString out). +BLOB_CLASS_SCRIPT :: ` +'use strict'; +const { Blob, File } = require('node:buffer'); +function noNew(C) { + try { + C([], 'f'); + return 'no throw'; + } catch (e) { + return e.name + ': ' + e.message; + } +} +const want = { + blob: 'TypeError: Cannot call a class constructor Blob without |new|', + file: 'TypeError: Cannot call a class constructor File without |new|', + keys: 'arrayBuffer,bytes,stream,text,slice,toString', +}; +const got = { + blob: noNew(Blob), + file: noNew(File), + keys: Object.keys(Blob.prototype).join(','), +}; +for (const k of Object.keys(want)) { + if (got[k] !== want[k]) throw new Error(k + ': got ' + got[k] + ', want ' + want[k]); +} +console.log('blob-class ok'); +` + +@(test) +blob_class_engine_traits :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(BLOB_CLASS_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 90270a3..d19da3d 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/encoding", "internal/errors", "internal/fetch", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 71259a9..0e4f4cc 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -176,9 +176,12 @@ It exists because a LAZY module's own `require('buffer').Buffer` runs on the use require of that module — after user code, and therefore steerable (#333). `fs`, `http`, `https`, `net` and `os` take it; `stream` and `stdio` do not need it because `installStdio` instantiates them during bootstrap. +`internal/blob` is eager yet takes it too, at call time: `buffer` requires it from its own +factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), -`stream/web.js` (2027, Web Streams), `buffer.js` (1646), `fetch.js` (1054), +`stream/web.js` (2027, Web Streams), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry +live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), `fetch.js` (1054), `crypto.js` (917). Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 5a805ba..6313046 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -7,7 +7,8 @@ // `require()` consults before touching the filesystem. // // A factory is compiled on its module's first require, not at context init, so a -// script pays only for the eager closure below (13 of 39 files): `lava run +// script pays only for the eager closure below (13 of 39 files when this landed; +// cmd/lava/lazy_builtins_test.odin's STATIC_EAGER is the current list): `lava run // bench/micro/noop.js` went 16.85 -> 14.63 ms (medians of 50 interleaved // spawn-to-exit runs per arm after 5 warmups, darwin arm64, -o:speed, f106ab4 vs // this change; see install_internal_modules). diff --git a/pkg/runtime/js/internal/blob.js b/pkg/runtime/js/internal/blob.js new file mode 100644 index 0000000..bd6610d --- /dev/null +++ b/pkg/runtime/js/internal/blob.js @@ -0,0 +1,269 @@ +/** + * @fileoverview Blob (WHATWG File API) and the `blob:` object-URL registry — + * node's lib/internal/blob.js. Re-exported from node:buffer (`Blob`, + * `resolveObjectURL`); node/buffer.js also installs the `Blob` global and the + * `URL.createObjectURL`/`revokeObjectURL` statics, so the order of every global + * install stays where it was before this file was split out of it. + * + * Instantiated eagerly: node/buffer.js requires it at its top level, before user + * code runs, so the module-eval captures below are pristine. + * + * @module lava/internal/blob + */ + +(function (require, module, _exports) { + 'use strict'; + + var P = require('internal/primordials'); + var StringG = String; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeSlice = P.StringPrototypeSlice; + var StringPrototypeIndexOf = P.StringPrototypeIndexOf; + + var errors = require('internal/errors'); + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_MISSING_ARGS = errors.ERR_MISSING_ARGS; + + // UTF-8 encode/decode go through the loader's pristine Buffer table, read at CALL + // time. It cannot be captured here: node/buffer.js requires this module from its own + // factory, so `require.pristineBuffer` is attached only after this module has run — + // and a top-level `require('buffer')` would be a cycle (check-js-graph rejects it). + // The functions are the same ones node/buffer.js's own `Buffer.from` / + // `Buffer.prototype.toString` are, so the bytes are identical for every string, + // lone surrogates included; what changes is that a replaced `Buffer.from` no longer + // steers Blob, which is where node is (its Blob encodes through a binding). + function encodeUtf8(str) { + return new Uint8Array(require.pristineBuffer.from(str, 'utf8')); + } + + function decodeUtf8(bytes) { + var PB = require.pristineBuffer; + return PB.bufferToString(PB.from(bytes), 'utf8'); + } + + function nativeEol() { + return globalThis.process && globalThis.process.platform === 'win32' ? '\r\n' : '\n'; + } + + // toNativeEndings is `str.replace(/\r?\n/g, nativeEol())` — every CRLF or lone LF + // becomes the platform terminator. No regex: a GLOBAL replace under a forged + // `RegExp.prototype.exec` never returns, so `new Blob([text], {endings:'native'})` + // spun on any text at all. node 24 hangs here too, so hardening it is a deviation in + // Lava's favour; pinned Lava-only by cmd/lava/regexp_pollution_test.odin. + // + // `\r?\n` means a CR is consumed only when an LF follows it: a lone CR is left alone, + // which is why this scans for LF and looks back rather than scanning for CR. + // Both early-outs are native scans, and they are the whole common case: text with no + // newline at all, and — on POSIX, where `eol` is '\n' — text with no CR, for which + // `\r?\n -> '\n'` is the identity. Without them this rebuilt the entire string as a + // rope even when the result was byte-identical to the input: measured at 1.6x and + // +54-70% peak RSS on a 64 KB body before they were added. + function toNativeEndings(str) { + var firstLF = StringPrototypeIndexOf(str, '\n', 0); + if (firstLF === -1) return str; + var eol = nativeEol(); + if (eol === '\n' && StringPrototypeIndexOf(str, '\r', 0) === -1) return str; + var out = ''; + var from = 0; + for (var i = firstLF; i !== -1; i = StringPrototypeIndexOf(str, '\n', from)) { + var cut = i > from && StringPrototypeCharCodeAt(str, i - 1) === 0x0d ? i - 1 : i; // CR + if (cut > from) out += StringPrototypeSlice(str, from, cut); + out += eol; + from = i + 1; + } + return from < str.length ? out + StringPrototypeSlice(str, from) : out; + } + + function blobPartToChunks(part, nativeEndings) { + if (part instanceof Blob) return part._parts; + if (part instanceof ArrayBuffer) return [new Uint8Array(part.slice(0))]; + if (ArrayBuffer.isView(part)) { + return [ + new Uint8Array(part.buffer.slice(part.byteOffset, part.byteOffset + part.byteLength)), + ]; + } + var str = StringG(part); + if (nativeEndings) str = toNativeEndings(str); + return [encodeUtf8(str)]; + } + + function blobJoin(parts, total) { + var merged = new Uint8Array(total), + off = 0; + for (var i = 0; i < parts.length; i++) { + merged.set(parts[i], off); + off += parts[i].length; + } + return merged; + } + + function normalizeBlobType(options) { + if (!options || options.type === undefined) return ''; + var type = StringG(options.type); + for (var i = 0; i < type.length; i++) { + var code = type.charCodeAt(i); + if (code < 0x20 || code > 0x7e) return ''; + } + return type.toLowerCase(); + } + + // Blob.prototype's methods were enumerable while Blob was a function whose prototype + // was assigned to; class methods are not. Kept enumerable so `for…in` and + // Object.keys(Blob.prototype) report what they did before the class conversion. Node's + // set differs (it also lists size/type and a textStream Lava does not have, and not + // toString), so matching it is a separate change, not a line here. + var ENUMERABLE_METHODS = ['arrayBuffer', 'bytes', 'stream', 'text', 'slice', 'toString']; + + /** + * Minimal Blob: stores parts without eager concat; materializes on demand. + * @node A class, like node's: Blob.prototype is non-writable (node 26.13.0, verified). + * @deviates Calling it without `new` throws JSC's class-constructor TypeError — + * `Cannot call a class constructor Blob without |new|` — where node says + * `Class constructor Blob cannot be invoked without 'new'` (and Lava said + * `Constructor Blob requires 'new'` before the class conversion). The text is the + * engine's and cannot be customised on a class; pinned by + * cmd/lava/blob_class_test.odin. + */ + class Blob { + /** + * @param {Iterable<*>} [parts] + * @param {{type?: string, endings?: string}} [options] + */ + constructor(parts, options) { + var chunks = [], + total = 0; + if (parts !== undefined && parts !== null) { + if (typeof parts !== 'object' || typeof parts[Symbol.iterator] !== 'function') { + throw new TypeError('The "parts" argument must be an iterable object'); + } + var nativeEndings = !!(options && options.endings === 'native'); + var list = Array.from(parts); + for (var i = 0; i < list.length; i++) { + var cs = blobPartToChunks(list[i], nativeEndings); + for (var j = 0; j < cs.length; j++) { + if (cs[j].length === 0) continue; + chunks.push(cs[j]); + total += cs[j].length; + } + } + } + Object.defineProperty(this, '_parts', { value: chunks }); + Object.defineProperty(this, '_size', { value: total }); + Object.defineProperty(this, '_type', { value: normalizeBlobType(options) }); + } + + get size() { + return this._size; + } + + get type() { + return this._type; + } + + arrayBuffer() { + return Promise.resolve(blobJoin(this._parts, this._size).buffer); + } + + bytes() { + return Promise.resolve(blobJoin(this._parts, this._size)); + } + + stream() { + var parts = this._parts; + var ReadableStream = require('node:stream/web').ReadableStream; + return new ReadableStream({ + start: function (controller) { + for (var i = 0; i < parts.length; i++) controller.enqueue(new Uint8Array(parts[i])); + controller.close(); + }, + }); + } + + text() { + return Promise.resolve(decodeUtf8(blobJoin(this._parts, this._size))); + } + + slice(start, end, contentType) { + var len = this._size; + var s = start === undefined ? 0 : start < 0 ? Math.max(len + start, 0) : Math.min(start, len); + var e = end === undefined ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len); + if (e < s) e = s; + var out = []; + var pos = 0; + var parts = this._parts; + for (var i = 0; i < parts.length && pos < e; i++) { + var part = parts[i]; + var pEnd = pos + part.length; + if (pEnd > s) { + out.push(part.subarray(pos < s ? s - pos : 0, pEnd > e ? e - pos : part.length)); + } + pos = pEnd; + } + return new Blob(out, contentType === undefined ? undefined : { type: contentType }); + } + + toString() { + return '[object Blob]'; + } + } + for (var m = 0; m < ENUMERABLE_METHODS.length; m++) { + Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true }); + } + + if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { + Object.defineProperty(Blob.prototype, Symbol.toStringTag, { + value: 'Blob', + configurable: true, + }); + } + + var objectUrlRegistry = new Map(); + + function objectUrlId() { + var c = globalThis.crypto; + if (c && typeof c.randomUUID === 'function') return c.randomUUID(); + var bytes = new Uint8Array(16); + if (c && typeof c.getRandomValues === 'function') c.getRandomValues(bytes); + else for (var i = 0; i < 16; i++) bytes[i] = (Math.random() * 256) & 0xff; + bytes[6] = (bytes[6] & 0x0f) | 0x40; + bytes[8] = (bytes[8] & 0x3f) | 0x80; + var s = ''; + for (var j = 0; j < 16; j++) { + s += (bytes[j] + 0x100).toString(16).slice(1); + if (j === 3 || j === 5 || j === 7 || j === 9) s += '-'; + } + return s; + } + + /** @param {Blob|File} obj @returns {string} blob: URL */ + function createObjectURL(obj) { + if (!(obj instanceof Blob)) { + throw ERR_INVALID_ARG_TYPE('obj', 'Blob', obj); + } + var id = 'blob:nodedata:' + objectUrlId(); + objectUrlRegistry.set(id, obj); + return id; + } + + /** @param {string} url */ + function revokeObjectURL(url) { + if (url === undefined) { + throw ERR_MISSING_ARGS('url'); + } + objectUrlRegistry.delete(StringG(url)); + } + + /** @param {string} id @returns {Blob|File|null} */ + function resolveObjectURL(id) { + var blob = objectUrlRegistry.get(`${id}`); + if (blob === undefined) return; + return new Blob([blob], { type: blob.type }); + } + + module.exports = { + Blob: Blob, + createObjectURL: createObjectURL, + revokeObjectURL: revokeObjectURL, + resolveObjectURL: resolveObjectURL, + }; +}); diff --git a/pkg/runtime/js/internal/file.js b/pkg/runtime/js/internal/file.js new file mode 100644 index 0000000..00709ea --- /dev/null +++ b/pkg/runtime/js/internal/file.js @@ -0,0 +1,63 @@ +/** + * @fileoverview File (WHATWG File API), a Blob with a name and a lastModified + * stamp — node's lib/internal/file.js. Re-exported from node:buffer, which also + * installs the `File` global. + * + * @module lava/internal/file + */ + +(function (require, module, _exports) { + 'use strict'; + + var Blob = require('internal/blob').Blob; + var StringG = String; + + /** + * A Blob with a name and a lastModified stamp. + * @extends Blob + * @node A class extending Blob, like node's: File.prototype is non-writable and + * Object.getPrototypeOf(File) is Blob (node 26.13.0, verified). + * @deviates Calling it without `new` throws JSC's class-constructor TypeError — + * `Cannot call a class constructor File without |new|` — where node says + * `Class constructor File cannot be invoked without 'new'` (and Lava said + * `Constructor File requires 'new'` before the class conversion). Pinned by + * cmd/lava/blob_class_test.odin. + */ + class File extends Blob { + /** + * @param {Iterable<*>} parts + * @param {string} name + * @param {{type?: string, endings?: string, lastModified?: number}} [options] + */ + constructor(parts, name, options) { + if (arguments.length < 2) + throw new TypeError( + "Failed to construct 'File': 2 arguments required, but only " + + arguments.length + + ' present.', + ); + super(parts, options); + Object.defineProperty(this, '_name', { value: StringG(name) }); + var lm = + options && options.lastModified !== undefined ? Number(options.lastModified) : Date.now(); + Object.defineProperty(this, '_lastModified', { value: lm }); + } + + get name() { + return this._name; + } + + get lastModified() { + return this._lastModified; + } + } + + if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { + Object.defineProperty(File.prototype, Symbol.toStringTag, { + value: 'File', + configurable: true, + }); + } + + module.exports = { File: File }; +}); diff --git a/pkg/runtime/js/node/buffer.js b/pkg/runtime/js/node/buffer.js index 4fd7520..7549bc2 100644 --- a/pkg/runtime/js/node/buffer.js +++ b/pkg/runtime/js/node/buffer.js @@ -2,8 +2,10 @@ * @fileoverview node:buffer — `Buffer` as a `Uint8Array` subclass. * * Codecs and byte-ops are Odin natives (`pkg/runtime/buffer.odin`) injected via - * the loader's fourth `native` argument. This file owns Node API glue, errors, - * pooling, and Blob/File/object-URL helpers. + * the loader's fourth `native` argument. This file owns Node API glue, errors and + * pooling. Blob, File and the blob: URL registry live in internal/blob.js and + * internal/file.js (node's lib/internal layout); this module re-exports them and + * installs their globals, in the order it always has. * * @module lava/internal/buffer */ @@ -58,7 +60,6 @@ var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; - var StringPrototypeIndexOf = P.StringPrototypeIndexOf; var StringFromCharCode = String.fromCharCode; var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; var TypedArrayPrototypeGetLength = P.TypedArrayPrototypeGetLength; @@ -110,6 +111,13 @@ var ERR_MISSING_ARGS = errors.ERR_MISSING_ARGS; var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; + // Required here, at the top level, so both stay in the eager set: the globals and + // URL statics below are installed from this module, in the same order as before + // they were split out. Neither requires node:buffer back (see internal/blob.js). + var blobModule = require('internal/blob'); + var Blob = blobModule.Blob; + var File = require('internal/file').File; + function validateByteLength(byteLength) { if (typeof byteLength !== 'number') throw ERR_INVALID_ARG_TYPE('byteLength', 'number', byteLength); @@ -1395,237 +1403,8 @@ return Buffer.from(Buffer.from(source).toString(fromEnc), toEnc); } - function nativeEol() { - return globalThis.process && globalThis.process.platform === 'win32' ? '\r\n' : '\n'; - } - - // toNativeEndings is `str.replace(/\r?\n/g, nativeEol())` — every CRLF or lone LF - // becomes the platform terminator. No regex: a GLOBAL replace under a forged - // `RegExp.prototype.exec` never returns, so `new Blob([text], {endings:'native'})` - // spun on any text at all. node 24 hangs here too, so hardening it is a deviation in - // Lava's favour; pinned Lava-only by cmd/lava/regexp_pollution_test.odin. - // - // `\r?\n` means a CR is consumed only when an LF follows it: a lone CR is left alone, - // which is why this scans for LF and looks back rather than scanning for CR. - // Both early-outs are native scans, and they are the whole common case: text with no - // newline at all, and — on POSIX, where `eol` is '\n' — text with no CR, for which - // `\r?\n -> '\n'` is the identity. Without them this rebuilt the entire string as a - // rope even when the result was byte-identical to the input: measured at 1.6x and - // +54-70% peak RSS on a 64 KB body before they were added. - function toNativeEndings(str) { - var firstLF = StringPrototypeIndexOf(str, '\n', 0); - if (firstLF === -1) return str; - var eol = nativeEol(); - if (eol === '\n' && StringPrototypeIndexOf(str, '\r', 0) === -1) return str; - var out = ''; - var from = 0; - for (var i = firstLF; i !== -1; i = StringPrototypeIndexOf(str, '\n', from)) { - var cut = i > from && StringPrototypeCharCodeAt(str, i - 1) === 0x0d ? i - 1 : i; // CR - if (cut > from) out += StringPrototypeSlice(str, from, cut); - out += eol; - from = i + 1; - } - return from < str.length ? out + StringPrototypeSlice(str, from) : out; - } - - function blobPartToChunks(part, nativeEndings) { - if (part instanceof Blob) return part._parts; - if (part instanceof ArrayBuffer) return [new Uint8Array(part.slice(0))]; - if (ArrayBuffer.isView(part)) { - return [ - new Uint8Array(part.buffer.slice(part.byteOffset, part.byteOffset + part.byteLength)), - ]; - } - var str = StringG(part); - if (nativeEndings) str = toNativeEndings(str); - return [new Uint8Array(Buffer.from(str, 'utf8'))]; - } - - function blobJoin(parts, total) { - var merged = new Uint8Array(total), - off = 0; - for (var i = 0; i < parts.length; i++) { - merged.set(parts[i], off); - off += parts[i].length; - } - return merged; - } - - function normalizeBlobType(options) { - if (!options || options.type === undefined) return ''; - var type = StringG(options.type); - for (var i = 0; i < type.length; i++) { - var code = type.charCodeAt(i); - if (code < 0x20 || code > 0x7e) return ''; - } - return type.toLowerCase(); - } - - /** - * Minimal Blob: stores parts without eager concat; materializes on demand. - * @param {Array<*>} parts - * @param {{type?: string, endings?: string}} [options] - */ - function Blob(parts, options) { - if (!(this instanceof Blob)) throw new TypeError("Constructor Blob requires 'new'"); - var chunks = [], - total = 0; - if (parts !== undefined && parts !== null) { - if (typeof parts !== 'object' || typeof parts[Symbol.iterator] !== 'function') { - throw new TypeError('The "parts" argument must be an iterable object'); - } - var nativeEndings = !!(options && options.endings === 'native'); - var list = Array.from(parts); - for (var i = 0; i < list.length; i++) { - var cs = blobPartToChunks(list[i], nativeEndings); - for (var j = 0; j < cs.length; j++) { - if (cs[j].length === 0) continue; - chunks.push(cs[j]); - total += cs[j].length; - } - } - } - Object.defineProperty(this, '_parts', { value: chunks }); - Object.defineProperty(this, '_size', { value: total }); - Object.defineProperty(this, '_type', { value: normalizeBlobType(options) }); - } - Object.defineProperty(Blob.prototype, 'size', { - get: function () { - return this._size; - }, - configurable: true, - }); - Object.defineProperty(Blob.prototype, 'type', { - get: function () { - return this._type; - }, - configurable: true, - }); - Blob.prototype.arrayBuffer = function () { - return Promise.resolve(blobJoin(this._parts, this._size).buffer); - }; - Blob.prototype.bytes = function () { - return Promise.resolve(blobJoin(this._parts, this._size)); - }; - Blob.prototype.stream = function () { - var parts = this._parts; - var ReadableStream = require('node:stream/web').ReadableStream; - return new ReadableStream({ - start: function (controller) { - for (var i = 0; i < parts.length; i++) controller.enqueue(new Uint8Array(parts[i])); - controller.close(); - }, - }); - }; - Blob.prototype.text = function () { - return Promise.resolve(Buffer.from(blobJoin(this._parts, this._size)).toString('utf8')); - }; - Blob.prototype.slice = function (start, end, contentType) { - var len = this._size; - var s = start === undefined ? 0 : start < 0 ? Math.max(len + start, 0) : Math.min(start, len); - var e = end === undefined ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len); - if (e < s) e = s; - var out = []; - var pos = 0; - var parts = this._parts; - for (var i = 0; i < parts.length && pos < e; i++) { - var part = parts[i]; - var pEnd = pos + part.length; - if (pEnd > s) { - out.push(part.subarray(pos < s ? s - pos : 0, pEnd > e ? e - pos : part.length)); - } - pos = pEnd; - } - return new Blob(out, contentType === undefined ? undefined : { type: contentType }); - }; - Blob.prototype.toString = function () { - return '[object Blob]'; - }; - - /** @extends Blob */ - function File(parts, name, options) { - if (!(this instanceof File)) throw new TypeError("Constructor File requires 'new'"); - if (arguments.length < 2) - throw new TypeError( - "Failed to construct 'File': 2 arguments required, but only " + - arguments.length + - ' present.', - ); - Blob.call(this, parts, options); - Object.defineProperty(this, '_name', { value: StringG(name) }); - var lm = - options && options.lastModified !== undefined ? Number(options.lastModified) : Date.now(); - Object.defineProperty(this, '_lastModified', { value: lm }); - } - File.prototype = Object.create(Blob.prototype); - File.prototype.constructor = File; - Object.defineProperty(File.prototype, 'name', { - get: function () { - return this._name; - }, - configurable: true, - }); - Object.defineProperty(File.prototype, 'lastModified', { - get: function () { - return this._lastModified; - }, - configurable: true, - }); - - if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { - Object.defineProperty(Blob.prototype, Symbol.toStringTag, { - value: 'Blob', - configurable: true, - }); - Object.defineProperty(File.prototype, Symbol.toStringTag, { - value: 'File', - configurable: true, - }); - } - - var objectUrlRegistry = new Map(); - - function objectUrlId() { - var c = globalThis.crypto; - if (c && typeof c.randomUUID === 'function') return c.randomUUID(); - var bytes = new Uint8Array(16); - if (c && typeof c.getRandomValues === 'function') c.getRandomValues(bytes); - else for (var i = 0; i < 16; i++) bytes[i] = (Math.random() * 256) & 0xff; - bytes[6] = (bytes[6] & 0x0f) | 0x40; - bytes[8] = (bytes[8] & 0x3f) | 0x80; - var s = ''; - for (var j = 0; j < 16; j++) { - s += (bytes[j] + 0x100).toString(16).slice(1); - if (j === 3 || j === 5 || j === 7 || j === 9) s += '-'; - } - return s; - } - - /** @param {Blob|File} obj @returns {string} blob: URL */ - function createObjectURL(obj) { - if (!(obj instanceof Blob)) { - throw ERR_INVALID_ARG_TYPE('obj', 'Blob', obj); - } - var id = 'blob:nodedata:' + objectUrlId(); - objectUrlRegistry.set(id, obj); - return id; - } - - /** @param {string} url */ - function revokeObjectURL(url) { - if (url === undefined) { - throw ERR_MISSING_ARGS('url'); - } - objectUrlRegistry.delete(StringG(url)); - } - - /** @param {string} id @returns {Blob|File|null} */ - function resolveObjectURL(id) { - var blob = objectUrlRegistry.get(`${id}`); - if (blob === undefined) return; - return new Blob([blob], { type: blob.type }); - } - + var createObjectURL = blobModule.createObjectURL; + var revokeObjectURL = blobModule.revokeObjectURL; if (globalThis.URL === undefined) { globalThis.URL = { createObjectURL: createObjectURL, revokeObjectURL: revokeObjectURL }; } else { @@ -1663,7 +1442,7 @@ isAscii: isAscii, isUtf8: isUtf8, transcode: transcode, - resolveObjectURL: resolveObjectURL, + resolveObjectURL: blobModule.resolveObjectURL, }; Object.defineProperty(exported, 'INSPECT_MAX_BYTES', { get: function () { diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 224a67a..34664ac 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -434,7 +434,7 @@ { "name": "regexp_pollution_immunity blob dies when native line endings go back to a global replace", "why": "new Blob([text], {endings:'native'}) runs this over caller text; the global replace spins under a forged exec. node hangs here too.", - "source": "pkg/runtime/js/node/buffer.js", + "source": "pkg/runtime/js/internal/blob.js", "find": " if (nativeEndings) str = toNativeEndings(str);", "replace": " if (nativeEndings) str = str.replace(/\\r?\\n/g, nativeEol());", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -2408,6 +2408,24 @@ "expect_detail": [ "url invalid base | TypeError | ERR_INVALID_URL | TypeError [ERR_INVALID_URL]:" ] + }, + { + "name": "77-blob-file-module blob dies when string parts stop being encoded as utf-8", + "why": "The split moved Blob's string encoder off the public Buffer onto the loader's pristine table; a wrong codec there changes every non-ASCII part's bytes and size.", + "source": "pkg/runtime/js/internal/blob.js", + "find": " return new Uint8Array(require.pristineBuffer.from(str, 'utf8'));", + "replace": " return new Uint8Array(require.pristineBuffer.from(str, 'latin1'));", + "gate": "compat:tests/node-compat/cases/77-blob-file-module.js", + "expect_detail": "node: blob 6 \"text/plain\"" + }, + { + "name": "blob_class_engine_traits dies when Blob.prototype methods lose their enumerability", + "why": "Class methods are non-enumerable; Blob's were enumerable as a function constructor, and the class conversion keeps that set by re-marking them.", + "source": "pkg/runtime/js/internal/blob.js", + "find": " Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true });", + "replace": " Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: false });", + "gate": "odin:cmd/lava:main.blob_class_engine_traits", + "expect_detail": "keys: got , want arrayBuffer" } ] } diff --git a/tests/node-compat/cases/76-coded-error-shape.js b/tests/node-compat/cases/76-coded-error-shape.js index 21eb215..732a92e 100644 --- a/tests/node-compat/cases/76-coded-error-shape.js +++ b/tests/node-compat/cases/76-coded-error-shape.js @@ -22,7 +22,7 @@ // - util.styleText's invalid-format list — it is built from inspect.colors, whose key // set differs between the runtimes; // - fs.writeFileSync(path, 5) — Lava does not validate `data` at all yet. -// http/net/https sites are Linux-only natives and live in 77-coded-error-shape-net.js. +// http/net/https sites are Linux-only natives and have no oracle case yet. 'use strict'; // One error prints as up to three SHORT lines — the shape (everything String(e) adds diff --git a/tests/node-compat/cases/77-blob-file-module.js b/tests/node-compat/cases/77-blob-file-module.js new file mode 100644 index 0000000..d88700c --- /dev/null +++ b/tests/node-compat/cases/77-blob-file-module.js @@ -0,0 +1,89 @@ +// Blob, File and the blob: URL registry live in their own internal modules, laid out +// like node's lib/internal: an error thrown from inside the Blob constructor carries a +// `node:internal/blob` frame, one from inside File a `node:internal/file` frame. Both +// used to say `node:buffer`, because all three were defined inside node/buffer.js. +// +// Only the frame's SOURCE NAME is compared — never the message, line or column, which +// differ by construction (node validates `sources` through internal/webidl, Lava throws +// from the constructor itself). The behavior lines after it are there so a move that +// loses a helper, or re-wires the string encoder, fails here and not only in a stack. +'use strict'; + +const { Blob, File, resolveObjectURL } = require('node:buffer'); + +function frameFrom(label, f, source) { + try { + f(); + console.log(label + ': NO THROW'); + } catch (e) { + console.log(label + ': ' + source + ' ' + String(e.stack).includes(source)); + } +} + +frameFrom('new Blob(string)', () => new Blob('x'), 'node:internal/blob'); +frameFrom('new File([])', () => new File([]), 'node:internal/file'); + +// Same constructors under both names, and the globals are those exports. +console.log('globals', globalThis.Blob === Blob, globalThis.File === File); +console.log('File extends Blob', new File([], 'f') instanceof Blob); +// Both are classes, as in node: a non-writable `prototype`, and user subclasses work. +// Without `new` only the error's class is compared — the text is the engine's own on +// each side (Lava's pinned by cmd/lava/blob_class_test.odin). +for (const C of [Blob, File]) { + console.log( + C.name, + 'prototype writable', + Object.getOwnPropertyDescriptor(C, 'prototype').writable, + ); + try { + C([], 'f'); + console.log(C.name, 'without new: NO THROW'); + } catch (e) { + console.log(C.name, 'without new:', e.name, e instanceof TypeError); + } +} +class MyBlob extends Blob { + hello() { + return 'hello ' + this.size; + } +} +const mine = new MyBlob(['abc']); +console.log('subclass', mine instanceof Blob, mine instanceof MyBlob, mine.hello(), mine.type); +class MyFile extends File {} +const myFile = new MyFile(['x'], 'my.txt', { lastModified: 7 }); +console.log('subclass File', myFile instanceof File, myFile.name, myFile.lastModified, myFile.size); + +const tag = (o) => Object.prototype.toString.call(o); +console.log('tags', tag(new Blob([])), tag(new File([], 'f')), String(new Blob([]))); + +const hex = (u8) => Buffer.from(u8).toString('hex'); + +(async () => { + const b = new Blob(['ab', new Uint8Array([0x63]), 'd\u00e9'], { type: 'Text/Plain' }); + console.log('blob', b.size, JSON.stringify(b.type), JSON.stringify(await b.text())); + console.log('arrayBuffer', hex(new Uint8Array(await b.arrayBuffer()))); + console.log('bytes', hex(await b.bytes())); + const s = b.slice(1, -1, 'x/y'); + console.log('slice', s.size, s.type, JSON.stringify(await s.text())); + console.log('slice neg', JSON.stringify(await b.slice(-3).text())); + console.log('nested', JSON.stringify(await new Blob([b, '!']).text())); + + // The string encoder Blob uses, on the inputs where encoders disagree: lone + // surrogates (each becomes EF BF BD) and a valid pair. + const sur = new Blob(['\ud800', 'a\udc00', '\ud83d\ude00']); + console.log('surrogates', hex(await sur.bytes())); + + const crlf = new Blob(['a\r\nb\nc'], { endings: 'native' }); + console.log('endings native', hex(await crlf.bytes())); + console.log('bad type', JSON.stringify(new Blob([], { type: 'a\u0001' }).type)); + + const f = new File(['hi'], 'name.txt', { type: 'text/plain', lastModified: 42 }); + console.log('file', f.name, f.lastModified, f.size, f.type, JSON.stringify(await f.text())); + + const url = URL.createObjectURL(f); + console.log('url prefix', url.startsWith('blob:nodedata:'), url.length); + const r = resolveObjectURL(url); + console.log('resolved', r instanceof Blob, r.size, r.type, JSON.stringify(await r.text())); + URL.revokeObjectURL(url); + console.log('revoked', resolveObjectURL(url)); +})(); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 1b47e98..98c2474 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -29,6 +29,12 @@ "accessor": 0, "global": 9 }, + "internal/blob.js": { + "method": 9, + "invoke": 0, + "accessor": 5, + "global": 22 + }, "internal/encoding.js": { "method": 0, "invoke": 0, @@ -47,6 +53,12 @@ "accessor": 6, "global": 75 }, + "internal/file.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 5 + }, "internal/mime.js": { "method": 24, "invoke": 0, @@ -90,10 +102,10 @@ "global": 40 }, "node/buffer.js": { - "method": 33, - "invoke": 6, - "accessor": 20, - "global": 112 + "method": 23, + "invoke": 5, + "accessor": 15, + "global": 81 }, "node/console.js": { "method": 0, From e2a2eb5d4cc7f2e4dfa5d027eda63bf185fce616 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:22:54 +0200 Subject: [PATCH 29/61] refactor(runtime): tidy internal/blob.js and take its live reads from primordials Inline the single-use helpers (encodeUtf8, decodeUtf8, nativeEol, normalizeBlobType), delete the dead Math.random UUID fallback and the typeof-Symbol guards, and route every call-time global/prototype read in internal/blob.js and internal/file.js through primordials or a module-eval capture. - blob: URL ids come from crypto.randomUUID captured by the loader before user code (req.pristineRandomUUID); a replaced globalThis.crypto or require('crypto').randomUUID no longer picks them. Node is immune too. - endings:'native' fixes the terminator at load, as node does. - the registry is a null-prototype dictionary, not a Map. - view parts read buffer/byteOffset/byteLength through the captured slot getters (stricter than node, pinned Lava-only). - text() decodes the joined bytes in place instead of copying them into a Buffer first. Adds ArrayBufferPrototypeSlice to primordials; lowers the ratchet. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/blob_class_test.odin | 47 ++++ pkg/runtime/js/bootstrap/loader.js | 9 + pkg/runtime/js/internal/blob.js | 201 ++++++++++-------- pkg/runtime/js/internal/file.js | 27 ++- pkg/runtime/js/internal/primordials.js | 3 + tests/mutation-manifest.json | 28 ++- .../cases/78-blob-pristine-reads.js | 83 ++++++++ tests/node-compat/pollution-baseline.json | 8 +- 8 files changed, 301 insertions(+), 105 deletions(-) create mode 100644 tests/node-compat/cases/78-blob-pristine-reads.js diff --git a/cmd/lava/blob_class_test.odin b/cmd/lava/blob_class_test.odin index 537135c..67b71f9 100644 --- a/cmd/lava/blob_class_test.odin +++ b/cmd/lava/blob_class_test.odin @@ -61,3 +61,50 @@ blob_class_engine_traits :: proc(t: ^testing.T) { ) testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) } + +// A view part's window must come from the engine's slots, not the configurable +// %TypedArray% / DataView accessors. Read live, a forged byteOffset/byteLength selects +// any window of the backing store — across a shared allocUnsafe pool, a neighbouring +// Buffer's bytes (the gadget internal/encoding.js's toBytes already closed). Lava-only: +// node's own Blob reads these accessors off every view part (node 26.13.0, verified with +// spy getters), so node lands on the forged window and an oracle would diverge by +// construction. Deviates from node in Lava's favour; documented at blobPartToChunks. +BLOB_VIEW_WINDOW_SCRIPT :: ` +'use strict'; +const TA = Object.getPrototypeOf(Uint8Array.prototype); +const saved = []; +function forge(owner, key, value) { + saved.push([owner, key, Object.getOwnPropertyDescriptor(owner, key)]); + Object.defineProperty(owner, key, { configurable: true, get() { return value; } }); +} +const backing = new Uint8Array([0x53, 0x45, 0x43, 0x52, 0x45, 0x54, 0x61, 0x62]); // SECRETab +const view = backing.subarray(6, 8); // "ab" +const dv = new DataView(backing.buffer, 6, 2); // "ab" +forge(TA, 'byteOffset', 0); +forge(TA, 'byteLength', 6); +forge(DataView.prototype, 'byteOffset', 0); +forge(DataView.prototype, 'byteLength', 6); +const b = new Blob([view, dv]); +for (const [o, k, d] of saved) Object.defineProperty(o, k, d); +b.text().then(function (s) { + if (s !== 'abab') throw new Error('view window came from forged accessors: ' + JSON.stringify(s)); + console.log('blob-view-window ok'); +}); +` + +@(test) +blob_view_window_ignores_forged_accessors :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(BLOB_VIEW_WINDOW_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 6313046..31acb35 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -146,6 +146,15 @@ req('internal/structured_clone'); req('crypto'); + // internal/blob mints `blob:nodedata:` ids with crypto.randomUUID, but it is + // instantiated by `buffer` above — before crypto exists — and cannot require crypto + // back (crypto requires buffer at its top level: a cycle check-js-graph rejects). So it + // gets the function through the same internal-req channel as pristineBuffer, read here, + // still before user code: a replaced `globalThis.crypto.randomUUID` or + // `require('crypto').randomUUID` no longer picks the id. Node mints it from its own + // binding and is immune; pinned by tests/node-compat/cases/78-blob-pristine-reads.js. + req.pristineRandomUUID = req('crypto').randomUUID; + // The public resolver: what native require() consults. It answers `node/` ids only, // so nothing under internal/ is reachable from user code, for two reasons a module // lives there: diff --git a/pkg/runtime/js/internal/blob.js b/pkg/runtime/js/internal/blob.js index bd6610d..e2421e9 100644 --- a/pkg/runtime/js/internal/blob.js +++ b/pkg/runtime/js/internal/blob.js @@ -15,37 +15,46 @@ 'use strict'; var P = require('internal/primordials'); - var StringG = String; + var StringG = P.String; + var TypeErrorG = P.TypeError; + var Uint8ArrayG = P.Uint8Array; + var ArrayBufferPrototype = P.ArrayBuffer.prototype; + var DataViewPrototype = P.DataViewPrototype; + var brandedAs = P.brandedAs; + var ArrayFrom = P.ArrayFrom; + var ArrayBufferIsView = P.ArrayBufferIsView; + var ArrayPrototypePush1 = P.ArrayPrototypePush1; + var ObjectDefineProperty = P.ObjectDefineProperty; + var MathMax = P.MathMax; + var MathMin = P.MathMin; + var SymbolIterator = P.Symbol.iterator; + var SymbolToStringTag = P.Symbol.toStringTag; + var PromiseResolve = P.FunctionPrototypeBind(P.Promise.resolve, P.Promise); var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; var StringPrototypeIndexOf = P.StringPrototypeIndexOf; + var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; + var ArrayBufferPrototypeSlice = P.ArrayBufferPrototypeSlice; + var Uint8ArrayPrototypeSet = P.Uint8ArrayPrototypeSet; + var TypedArrayPrototypeSubarray = P.TypedArrayPrototypeSubarray; + var TypedArrayPrototypeGetBuffer = P.TypedArrayPrototypeGetBuffer; + var TypedArrayPrototypeGetByteOffset = P.TypedArrayPrototypeGetByteOffset; + var TypedArrayPrototypeGetByteLength = P.TypedArrayPrototypeGetByteLength; + var DataViewPrototypeGetBuffer = P.DataViewPrototypeGetBuffer; + var DataViewPrototypeGetByteOffset = P.DataViewPrototypeGetByteOffset; + var DataViewPrototypeGetByteLength = P.DataViewPrototypeGetByteLength; var errors = require('internal/errors'); var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; var ERR_MISSING_ARGS = errors.ERR_MISSING_ARGS; - // UTF-8 encode/decode go through the loader's pristine Buffer table, read at CALL - // time. It cannot be captured here: node/buffer.js requires this module from its own - // factory, so `require.pristineBuffer` is attached only after this module has run — - // and a top-level `require('buffer')` would be a cycle (check-js-graph rejects it). - // The functions are the same ones node/buffer.js's own `Buffer.from` / - // `Buffer.prototype.toString` are, so the bytes are identical for every string, - // lone surrogates included; what changes is that a replaced `Buffer.from` no longer - // steers Blob, which is where node is (its Blob encodes through a binding). - function encodeUtf8(str) { - return new Uint8Array(require.pristineBuffer.from(str, 'utf8')); - } - - function decodeUtf8(bytes) { - var PB = require.pristineBuffer; - return PB.bufferToString(PB.from(bytes), 'utf8'); - } - - function nativeEol() { - return globalThis.process && globalThis.process.platform === 'win32' ? '\r\n' : '\n'; - } + // The platform terminator for `endings: 'native'`, fixed when this module loads (before + // user code; install_process runs ahead of the loader). Node fixes it at startup too: + // with process.platform redefined to 'win32' on darwin, node 26.13.0 still writes '\n'. + // Pinned by tests/node-compat/cases/78-blob-pristine-reads.js. + var EOL = process.platform === 'win32' ? '\r\n' : '\n'; - // toNativeEndings is `str.replace(/\r?\n/g, nativeEol())` — every CRLF or lone LF + // toNativeEndings is `str.replace(/\r?\n/g, EOL)` — every CRLF or lone LF // becomes the platform terminator. No regex: a GLOBAL replace under a forged // `RegExp.prototype.exec` never returns, so `new Blob([text], {endings:'native'})` // spun on any text at all. node 24 hangs here too, so hardening it is a deviation in @@ -61,52 +70,63 @@ function toNativeEndings(str) { var firstLF = StringPrototypeIndexOf(str, '\n', 0); if (firstLF === -1) return str; - var eol = nativeEol(); - if (eol === '\n' && StringPrototypeIndexOf(str, '\r', 0) === -1) return str; + if (EOL === '\n' && StringPrototypeIndexOf(str, '\r', 0) === -1) return str; var out = ''; var from = 0; for (var i = firstLF; i !== -1; i = StringPrototypeIndexOf(str, '\n', from)) { var cut = i > from && StringPrototypeCharCodeAt(str, i - 1) === 0x0d ? i - 1 : i; // CR if (cut > from) out += StringPrototypeSlice(str, from, cut); - out += eol; + out += EOL; from = i + 1; } return from < str.length ? out + StringPrototypeSlice(str, from) : out; } + // Kept out of the constructor, which would otherwise run past ~40 lines. + // + // A view's window comes from the engine's slots through the captured getters, not the + // configurable %TypedArray% / DataView accessors — read live, a forged trio selects a + // window across a shared allocUnsafe pool (the same gadget encoding.js's toBytes + // closed). DataView's accessors are separate properties, hence two arms. Deviates: + // node's Blob reads all three live (node 26.13.0, spy getters), so a forged trio steers + // node and not Lava; pinned Lava-only by blob_view_window_ignores_forged_accessors in + // cmd/lava/blob_class_test.odin. + // The copy still goes through ArrayBufferPrototypeSlice, which reads the source's + // `constructor` / Symbol.species: node's Blob copies through the same primordial, so + // that read is parity, not a leftover. + // + // UTF-8 goes through the loader's pristine Buffer table, read at CALL time: it is + // attached only after node/buffer.js (which requires this module) has finished, and a + // top-level `require('buffer')` would be a cycle. The functions are the ones Buffer.from + // uses, so the bytes are identical for every string, lone surrogates included. function blobPartToChunks(part, nativeEndings) { if (part instanceof Blob) return part._parts; - if (part instanceof ArrayBuffer) return [new Uint8Array(part.slice(0))]; - if (ArrayBuffer.isView(part)) { - return [ - new Uint8Array(part.buffer.slice(part.byteOffset, part.byteOffset + part.byteLength)), - ]; + if (brandedAs(part, ArrayBufferPrototype)) { + return [new Uint8ArrayG(ArrayBufferPrototypeSlice(part, 0))]; + } + if (ArrayBufferIsView(part)) { + var dv = brandedAs(part, DataViewPrototype); + var buf = dv ? DataViewPrototypeGetBuffer(part) : TypedArrayPrototypeGetBuffer(part); + var off = dv ? DataViewPrototypeGetByteOffset(part) : TypedArrayPrototypeGetByteOffset(part); + var len = dv ? DataViewPrototypeGetByteLength(part) : TypedArrayPrototypeGetByteLength(part); + return [new Uint8ArrayG(ArrayBufferPrototypeSlice(buf, off, off + len))]; } var str = StringG(part); if (nativeEndings) str = toNativeEndings(str); - return [encodeUtf8(str)]; + return [new Uint8ArrayG(require.pristineBuffer.from(str, 'utf8'))]; } + // Three callers (arrayBuffer, bytes, text), so it stays a function. function blobJoin(parts, total) { - var merged = new Uint8Array(total), + var merged = new Uint8ArrayG(total), off = 0; for (var i = 0; i < parts.length; i++) { - merged.set(parts[i], off); + Uint8ArrayPrototypeSet(merged, parts[i], off); off += parts[i].length; } return merged; } - function normalizeBlobType(options) { - if (!options || options.type === undefined) return ''; - var type = StringG(options.type); - for (var i = 0; i < type.length; i++) { - var code = type.charCodeAt(i); - if (code < 0x20 || code > 0x7e) return ''; - } - return type.toLowerCase(); - } - // Blob.prototype's methods were enumerable while Blob was a function whose prototype // was assigned to; class methods are not. Kept enumerable so `for…in` and // Object.keys(Blob.prototype) report what they did before the class conversion. Node's @@ -133,23 +153,33 @@ var chunks = [], total = 0; if (parts !== undefined && parts !== null) { - if (typeof parts !== 'object' || typeof parts[Symbol.iterator] !== 'function') { - throw new TypeError('The "parts" argument must be an iterable object'); + if (typeof parts !== 'object' || typeof parts[SymbolIterator] !== 'function') { + throw new TypeErrorG('The "parts" argument must be an iterable object'); } var nativeEndings = !!(options && options.endings === 'native'); - var list = Array.from(parts); + var list = ArrayFrom(parts); for (var i = 0; i < list.length; i++) { var cs = blobPartToChunks(list[i], nativeEndings); for (var j = 0; j < cs.length; j++) { if (cs[j].length === 0) continue; - chunks.push(cs[j]); + ArrayPrototypePush1(chunks, cs[j]); total += cs[j].length; } } } - Object.defineProperty(this, '_parts', { value: chunks }); - Object.defineProperty(this, '_size', { value: total }); - Object.defineProperty(this, '_type', { value: normalizeBlobType(options) }); + // A type with any byte outside printable ASCII is dropped whole, per the File API. + var type = ''; + if (options && options.type !== undefined) { + type = StringG(options.type); + for (var k = 0; k < type.length; k++) { + var code = StringPrototypeCharCodeAt(type, k); + if (code < 0x20 || code > 0x7e) break; + } + type = k < type.length ? '' : StringPrototypeToLowerCase(type); + } + ObjectDefineProperty(this, '_parts', { value: chunks }); + ObjectDefineProperty(this, '_size', { value: total }); + ObjectDefineProperty(this, '_type', { value: type }); } get size() { @@ -161,11 +191,11 @@ } arrayBuffer() { - return Promise.resolve(blobJoin(this._parts, this._size).buffer); + return PromiseResolve(TypedArrayPrototypeGetBuffer(blobJoin(this._parts, this._size))); } bytes() { - return Promise.resolve(blobJoin(this._parts, this._size)); + return PromiseResolve(blobJoin(this._parts, this._size)); } stream() { @@ -173,20 +203,26 @@ var ReadableStream = require('node:stream/web').ReadableStream; return new ReadableStream({ start: function (controller) { - for (var i = 0; i < parts.length; i++) controller.enqueue(new Uint8Array(parts[i])); + for (var i = 0; i < parts.length; i++) controller.enqueue(new Uint8ArrayG(parts[i])); controller.close(); }, }); } text() { - return Promise.resolve(decodeUtf8(blobJoin(this._parts, this._size))); + // Decoded in place: the pristine Buffer#toString reads any Uint8Array, so the + // joined bytes need no Buffer copy first. The change that dropped it (with the + // primordial captures beside it) took 20000 text() calls on a 96-char string from + // 93.6 to 77.0 ms — medians of 7 interleaved launches per arm, darwin. + return PromiseResolve( + require.pristineBuffer.bufferToString(blobJoin(this._parts, this._size), 'utf8'), + ); } slice(start, end, contentType) { var len = this._size; - var s = start === undefined ? 0 : start < 0 ? Math.max(len + start, 0) : Math.min(start, len); - var e = end === undefined ? len : end < 0 ? Math.max(len + end, 0) : Math.min(end, len); + var s = start === undefined ? 0 : start < 0 ? MathMax(len + start, 0) : MathMin(start, len); + var e = end === undefined ? len : end < 0 ? MathMax(len + end, 0) : MathMin(end, len); if (e < s) e = s; var out = []; var pos = 0; @@ -195,7 +231,14 @@ var part = parts[i]; var pEnd = pos + part.length; if (pEnd > s) { - out.push(part.subarray(pos < s ? s - pos : 0, pEnd > e ? e - pos : part.length)); + ArrayPrototypePush1( + out, + TypedArrayPrototypeSubarray( + part, + pos < s ? s - pos : 0, + pEnd > e ? e - pos : part.length, + ), + ); } pos = pEnd; } @@ -207,41 +250,29 @@ } } for (var m = 0; m < ENUMERABLE_METHODS.length; m++) { - Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true }); + ObjectDefineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true }); } - if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { - Object.defineProperty(Blob.prototype, Symbol.toStringTag, { - value: 'Blob', - configurable: true, - }); - } - - var objectUrlRegistry = new Map(); + ObjectDefineProperty(Blob.prototype, SymbolToStringTag, { + value: 'Blob', + configurable: true, + }); - function objectUrlId() { - var c = globalThis.crypto; - if (c && typeof c.randomUUID === 'function') return c.randomUUID(); - var bytes = new Uint8Array(16); - if (c && typeof c.getRandomValues === 'function') c.getRandomValues(bytes); - else for (var i = 0; i < 16; i++) bytes[i] = (Math.random() * 256) & 0xff; - bytes[6] = (bytes[6] & 0x0f) | 0x40; - bytes[8] = (bytes[8] & 0x3f) | 0x80; - var s = ''; - for (var j = 0; j < 16; j++) { - s += (bytes[j] + 0x100).toString(16).slice(1); - if (j === 3 || j === 5 || j === 7 || j === 9) s += '-'; - } - return s; - } + // A null-prototype dictionary rather than a Map: its get/set/delete are plain property + // operations, so a replaced Map.prototype method has nothing to steer, and no Map + // primordials are needed for it. Keys are strings either way (`${id}` / String(url)), + // and with no prototype a key such as '__proto__' or 'toString' is just an own key. + var objectUrlRegistry = { __proto__: null }; /** @param {Blob|File} obj @returns {string} blob: URL */ function createObjectURL(obj) { if (!(obj instanceof Blob)) { throw ERR_INVALID_ARG_TYPE('obj', 'Blob', obj); } - var id = 'blob:nodedata:' + objectUrlId(); - objectUrlRegistry.set(id, obj); + // The loader's pristine crypto.randomUUID (see bootstrap/loader.js); crypto is eager, + // so it always exists and the hand-rolled Math.random fallback that stood here was dead. + var id = 'blob:nodedata:' + require.pristineRandomUUID(); + objectUrlRegistry[id] = obj; return id; } @@ -250,12 +281,12 @@ if (url === undefined) { throw ERR_MISSING_ARGS('url'); } - objectUrlRegistry.delete(StringG(url)); + delete objectUrlRegistry[StringG(url)]; } /** @param {string} id @returns {Blob|File|null} */ function resolveObjectURL(id) { - var blob = objectUrlRegistry.get(`${id}`); + var blob = objectUrlRegistry[`${id}`]; if (blob === undefined) return; return new Blob([blob], { type: blob.type }); } diff --git a/pkg/runtime/js/internal/file.js b/pkg/runtime/js/internal/file.js index 00709ea..9e1c876 100644 --- a/pkg/runtime/js/internal/file.js +++ b/pkg/runtime/js/internal/file.js @@ -9,8 +9,15 @@ (function (require, module, _exports) { 'use strict'; + var P = require('internal/primordials'); var Blob = require('internal/blob').Blob; - var StringG = String; + var StringG = P.String; + var NumberG = P.Number; + var TypeErrorG = P.TypeError; + var ObjectDefineProperty = P.ObjectDefineProperty; + // Eager like internal/blob (node/buffer.js requires both before user code), so this + // module-eval capture is pristine; primordials has no Date. + var DateNow = Date.now; /** * A Blob with a name and a lastModified stamp. @@ -31,16 +38,16 @@ */ constructor(parts, name, options) { if (arguments.length < 2) - throw new TypeError( + throw new TypeErrorG( "Failed to construct 'File': 2 arguments required, but only " + arguments.length + ' present.', ); super(parts, options); - Object.defineProperty(this, '_name', { value: StringG(name) }); + ObjectDefineProperty(this, '_name', { value: StringG(name) }); var lm = - options && options.lastModified !== undefined ? Number(options.lastModified) : Date.now(); - Object.defineProperty(this, '_lastModified', { value: lm }); + options && options.lastModified !== undefined ? NumberG(options.lastModified) : DateNow(); + ObjectDefineProperty(this, '_lastModified', { value: lm }); } get name() { @@ -52,12 +59,10 @@ } } - if (typeof Symbol !== 'undefined' && Symbol.toStringTag) { - Object.defineProperty(File.prototype, Symbol.toStringTag, { - value: 'File', - configurable: true, - }); - } + ObjectDefineProperty(File.prototype, P.Symbol.toStringTag, { + value: 'File', + configurable: true, + }); module.exports = { File: File }; }); diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index 013ae40..d6eef88 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -457,6 +457,9 @@ // As much brand checks as lengths: each getter throws on any receiver that is not // a real buffer of its kind, which a Symbol.toStringTag look-alike cannot fake. // SharedArrayBuffer is absent from some JSC builds, hence the undefined arm. + // Note it still reads the receiver's `constructor` / Symbol.species, per spec — the + // method is pristine, not the species lookup. Node's own primordial does the same. + ArrayBufferPrototypeSlice: caller2(ArrayBuffer.prototype.slice), ArrayBufferPrototypeGetByteLength: safeGetter( Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get, ), diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 34664ac..ce7a2a1 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -436,7 +436,7 @@ "why": "new Blob([text], {endings:'native'}) runs this over caller text; the global replace spins under a forged exec. node hangs here too.", "source": "pkg/runtime/js/internal/blob.js", "find": " if (nativeEndings) str = toNativeEndings(str);", - "replace": " if (nativeEndings) str = str.replace(/\\r?\\n/g, nativeEol());", + "replace": " if (nativeEndings) str = str.replace(/\\r?\\n/g, EOL);", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", "expect_detail": [ "timed out", @@ -2413,8 +2413,8 @@ "name": "77-blob-file-module blob dies when string parts stop being encoded as utf-8", "why": "The split moved Blob's string encoder off the public Buffer onto the loader's pristine table; a wrong codec there changes every non-ASCII part's bytes and size.", "source": "pkg/runtime/js/internal/blob.js", - "find": " return new Uint8Array(require.pristineBuffer.from(str, 'utf8'));", - "replace": " return new Uint8Array(require.pristineBuffer.from(str, 'latin1'));", + "find": " return [new Uint8ArrayG(require.pristineBuffer.from(str, 'utf8'))];", + "replace": " return [new Uint8ArrayG(require.pristineBuffer.from(str, 'latin1'))];", "gate": "compat:tests/node-compat/cases/77-blob-file-module.js", "expect_detail": "node: blob 6 \"text/plain\"" }, @@ -2422,10 +2422,28 @@ "name": "blob_class_engine_traits dies when Blob.prototype methods lose their enumerability", "why": "Class methods are non-enumerable; Blob's were enumerable as a function constructor, and the class conversion keeps that set by re-marking them.", "source": "pkg/runtime/js/internal/blob.js", - "find": " Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true });", - "replace": " Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: false });", + "find": " ObjectDefineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: true });", + "replace": " ObjectDefineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: false });", "gate": "odin:cmd/lava:main.blob_class_engine_traits", "expect_detail": "keys: got , want arrayBuffer" + }, + { + "name": "78-blob-pristine-reads dies when createObjectURL mints its id from the live globalThis.crypto", + "why": "A replaced crypto.randomUUID then chooses every blob: URL id, so user code can make them predictable; node mints them from its binding and is immune.", + "source": "pkg/runtime/js/internal/blob.js", + "find": " var id = 'blob:nodedata:' + require.pristineRandomUUID();", + "replace": " var id = 'blob:nodedata:' + globalThis.crypto.randomUUID();", + "gate": "compat:tests/node-compat/cases/78-blob-pristine-reads.js", + "expect_detail": "globalThis.crypto.randomUUID" + }, + { + "name": "blob_view_window_ignores_forged_accessors dies when a view part reads byteOffset through the live accessor", + "why": "A forged %TypedArray%.prototype.byteOffset then picks which bytes of the backing store a Blob copies, i.e. a neighbouring Buffer in a shared pool.", + "source": "pkg/runtime/js/internal/blob.js", + "find": " var off = dv ? DataViewPrototypeGetByteOffset(part) : TypedArrayPrototypeGetByteOffset(part);", + "replace": " var off = part.byteOffset;", + "gate": "odin:cmd/lava:main.blob_view_window_ignores_forged_accessors", + "expect_detail": "view window came from forged accessors" } ] } diff --git a/tests/node-compat/cases/78-blob-pristine-reads.js b/tests/node-compat/cases/78-blob-pristine-reads.js new file mode 100644 index 0000000..8214fec --- /dev/null +++ b/tests/node-compat/cases/78-blob-pristine-reads.js @@ -0,0 +1,83 @@ +// Blob and the blob: URL registry must not read a REPLACED global at call time. +// +// internal/blob.js is eager (node/buffer.js requires it before user code), so a capture +// at its module-eval is pristine; a read inside a method is not. Before this case, Lava +// read every name below live: the object-URL id came from whatever +// `globalThis.crypto.randomUUID` was by then (a predictable id is a guessable blob: URL), +// `endings: 'native'` re-read `process.platform` per construction, and the registry, +// part classifier and type normaliser went through replaceable globals and prototype +// methods. Node takes all of them from primordials or bindings; measured on node 26.13.0, +// every log below is empty and the platform is the one the process started with. +// +// A spy forwards to the original, so the only observable is whether it was CALLED — the +// output stays deterministic (no uuid is ever printed, only its shape). +// +// Deliberately NOT here, because node reads them live and Lava does not, so the logs +// would differ by construction: the %TypedArray% / DataView buffer, byteOffset and +// byteLength accessors (node's Blob reads them off every view part; Lava takes the +// engine slots — pinned Lava-only by cmd/lava/blob_class_test.odin). Also absent: +// ArrayBuffer[Symbol.species] / .constructor, which BOTH read through +// ArrayBufferPrototypeSlice. +const log = []; +let armed = false; +const RA = Reflect.apply; + +function spyMethod(owner, key, label) { + const orig = owner[key]; + owner[key] = function (...args) { + if (armed) log.push(label); + return RA(orig, this, args); + }; +} + +const platform = process.platform; + +spyMethod(globalThis.crypto, 'randomUUID', 'globalThis.crypto.randomUUID'); +spyMethod(require('crypto'), 'randomUUID', "require('crypto').randomUUID"); +spyMethod(ArrayBuffer, 'isView', 'ArrayBuffer.isView'); +spyMethod(Map.prototype, 'set', 'Map.prototype.set'); +spyMethod(Map.prototype, 'get', 'Map.prototype.get'); +spyMethod(Map.prototype, 'delete', 'Map.prototype.delete'); +spyMethod(String.prototype, 'charCodeAt', 'String.prototype.charCodeAt'); +spyMethod(String.prototype, 'toLowerCase', 'String.prototype.toLowerCase'); + +// A replaced String global and a forged ArrayBuffer[Symbol.hasInstance] are both plain +// writes user code can make; the classifier must not consult either. +const StringOrig = String; +globalThis.String = function (v) { + if (armed) log.push('String'); + return StringOrig(v); +}; +Object.defineProperty(ArrayBuffer, Symbol.hasInstance, { + configurable: true, + value(v) { + if (armed) log.push('ArrayBuffer[Symbol.hasInstance]'); + return Function.prototype[Symbol.hasInstance].call(ArrayBuffer, v); + }, +}); +Object.defineProperty(process, 'platform', { value: platform === 'win32' ? 'linux' : 'win32' }); + +armed = true; +const u8 = new Uint8Array([0x61, 0x62, 0x63, 0x64]); +const blob = new Blob( + ['x\ny', u8.subarray(1, 3), new DataView(u8.buffer, 2, 2), new ArrayBuffer(2), 7], + { type: 'Text/Plain', endings: 'native' }, +); +const url = URL.createObjectURL(blob); +const back = require('buffer').resolveObjectURL(url); +URL.revokeObjectURL(url); +const gone = require('buffer').resolveObjectURL(url); +armed = false; + +globalThis.String = StringOrig; +console.log('log:', JSON.stringify(log)); +console.log('size:', blob.size, 'type:', blob.type); +console.log( + 'native endings follow the startup platform:', + blob.size === (platform === 'win32' ? 11 : 10), +); +console.log( + 'url shape:', + /^blob:nodedata:[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(url), +); +console.log('resolved:', back instanceof Blob, back && back.size, 'after revoke:', gone); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 98c2474..f19cfd5 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -30,10 +30,10 @@ "global": 9 }, "internal/blob.js": { - "method": 9, + "method": 0, "invoke": 0, - "accessor": 5, - "global": 22 + "accessor": 0, + "global": 0 }, "internal/encoding.js": { "method": 0, @@ -57,7 +57,7 @@ "method": 0, "invoke": 0, "accessor": 0, - "global": 5 + "global": 0 }, "internal/mime.js": { "method": 24, From f0361d4788bcfca52a6042c5a62ea3ef3cc62cda Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:36:03 +0200 Subject: [PATCH 30/61] refactor(runtime): split stream.js into internal/streams like node MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit node/stream.js (1525 lines) held the whole Node stream implementation. It is now 63 lines that assemble the same export object (same keys, order, identities and statics) from internal/streams/{legacy, readable, writable, duplex, transform, passthrough, destroy, add-abort-signal, end-of-stream, pipeline, utils} and internal/webstreams/adapters, the files node keeps them in. Constructors stay functions: npm code inherits from them with Base.call(this), which a class would reject (CLAUDE.md §5). The graph is acyclic: readable/writable reach the web-stream adapters through a lazy, cached require used only by fromWeb/toWeb, never per chunk. Strict- mode `return f()` is a proper tail call in JSC and dropped the readable and writable frames, so the wrappers store the result first; a recorded mutation pins it. Eager set 16 -> 27: stdio needs stream at bootstrap, so its files compile at init. noop.js median 16.98 -> 17.29 and 17.67 -> 18.38 ms (5 warmups + 50 interleaved runs per arm, two sweeps, darwin -o:speed); accepted, with lazy process.stdout/stderr tracked as the way to drop them from startup. Frames now read node:internal/streams/*, as in node; pinned by tests/node-compat/cases/79-stream-internal-modules.js. Ratchet per-class totals unchanged. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 18 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 11 +- pkg/runtime/globals.odin | 2 + .../js/internal/streams/add-abort-signal.js | 31 + pkg/runtime/js/internal/streams/destroy.js | 106 ++ pkg/runtime/js/internal/streams/duplex.js | 60 + .../js/internal/streams/end-of-stream.js | 77 + pkg/runtime/js/internal/streams/legacy.js | 15 + .../js/internal/streams/passthrough.js | 18 + pkg/runtime/js/internal/streams/pipeline.js | 61 + pkg/runtime/js/internal/streams/readable.js | 614 +++++++ pkg/runtime/js/internal/streams/transform.js | 59 + pkg/runtime/js/internal/streams/utils.js | 104 ++ pkg/runtime/js/internal/streams/writable.js | 398 +++++ .../js/internal/webstreams/adapters.js | 141 ++ pkg/runtime/js/node/stream.js | 1492 +---------------- tests/mutation-manifest.json | 36 +- .../cases/79-stream-internal-modules.js | 194 +++ tests/node-compat/pollution-baseline.json | 80 +- 20 files changed, 2019 insertions(+), 1500 deletions(-) create mode 100644 pkg/runtime/js/internal/streams/add-abort-signal.js create mode 100644 pkg/runtime/js/internal/streams/destroy.js create mode 100644 pkg/runtime/js/internal/streams/duplex.js create mode 100644 pkg/runtime/js/internal/streams/end-of-stream.js create mode 100644 pkg/runtime/js/internal/streams/legacy.js create mode 100644 pkg/runtime/js/internal/streams/passthrough.js create mode 100644 pkg/runtime/js/internal/streams/pipeline.js create mode 100644 pkg/runtime/js/internal/streams/readable.js create mode 100644 pkg/runtime/js/internal/streams/transform.js create mode 100644 pkg/runtime/js/internal/streams/utils.js create mode 100644 pkg/runtime/js/internal/streams/writable.js create mode 100644 pkg/runtime/js/internal/webstreams/adapters.js create mode 100644 tests/node-compat/cases/79-stream-internal-modules.js diff --git a/CLAUDE.md b/CLAUDE.md index 12ed8cc..11a1432 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -277,13 +277,17 @@ Rules that keep this from becoming ceremony: fixed-arity wrappers (`ArrayPrototypePush1`/`Push2`) or plain indexed writes into a preallocated null-prototype array. Getting this wrong cost 1.43x on the TextDecoder JS path. -- Where node implements a constructor as a `class`, new or rewritten code does too - (`class Blob`, `class File extends Blob`). It gives node's non-writable - `prototype` and `extends`/`new.target` semantics. The one thing it cannot - match is the TypeError text for a call without `new`: JSC's own wording is an - engine trait, declared with `@deviates` and pinned by a Lava-only assertion, - never printed by an oracle case. Method enumerability stays explicit where node - makes it explicit. +- New and rewritten code writes constructors as `class` (`class Blob`, + `class File extends Blob`), whatever node does internally. **Exception, and do + not add to it without a probe:** constructors that npm code inherits from with + the pre-ES6 pattern `function S() { Base.call(this) }` + `util.inherits(S, Base)` + stay function constructors. That call throws on a class, so a class there breaks + working packages, not just a message. Today these are `EventEmitter`, `Stream`, + `Readable`, `Writable`, `Duplex`, `Transform`, `PassThrough`, and `Buffer` + (callable without `new`). For every other class, the TypeError text for a call + without `new` is JSC's own wording: an engine trait, declared with `@deviates`, + pinned by a Lava-only assertion, and never printed by an oracle case. Method + enumerability stays explicit wherever it is observable today. - Nothing transient lands on `globalThis`; natives arrive as the factory's fourth argument. - Errors are Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`) diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index d19da3d..325135e 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 0e4f4cc..485c026 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -175,14 +175,21 @@ attaches them to the `req` it passes each factory, so an internal module reads t It exists because a LAZY module's own `require('buffer').Buffer` runs on the user's first require of that module — after user code, and therefore steerable (#333). `fs`, `http`, `https`, `net` and `os` take it; `stream` and `stdio` do not need it because `installStdio` -instantiates them during bootstrap. +instantiates them during bootstrap — and with them every `internal/streams/*` file `stream` +requires at its top level. `internal/webstreams/adapters` is the exception: `Readable`/ +`Writable.fromWeb`/`toWeb` require it on first call (it requires `readable`/`writable` back +at its own top level, so the late edge is what keeps the graph acyclic), as node does. `internal/blob` is eager yet takes it too, at call time: `buffer` requires it from its own factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), `stream/web.js` (2027, Web Streams), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), `fetch.js` (1054), -`crypto.js` (917). Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +`crypto.js` (917). `node/stream.js` only assembles the public module; the classic +streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, +`transform`, `passthrough`, `destroy`, `add-abort-signal`, `end-of-stream`, `pipeline`, +`utils`) and the Web Streams bridges in `internal/webstreams/adapters.js`, the files node +keeps them in, so their frames read `node:internal/streams/readable`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 211d2b2..e0eb4c2 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1638,4 +1638,6 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"node/timers", #load_directory("js/node/timers")}, {"node/util", #load_directory("js/node/util")}, {"internal", #load_directory("js/internal")}, + {"internal/streams", #load_directory("js/internal/streams")}, + {"internal/webstreams", #load_directory("js/internal/webstreams")}, } diff --git a/pkg/runtime/js/internal/streams/add-abort-signal.js b/pkg/runtime/js/internal/streams/add-abort-signal.js new file mode 100644 index 0000000..42a3c4e --- /dev/null +++ b/pkg/runtime/js/internal/streams/add-abort-signal.js @@ -0,0 +1,31 @@ +// Destroys a stream when its AbortSignal fires — node's +// lib/internal/streams/add-abort-signal.js. The public stream.addAbortSignal wrapper, +// which returns the stream, stays in node/stream.js. +(function (require, module) { + 'use strict'; + + var ErrorG = require('internal/primordials').Error; + var nextTick = process.nextTick; + + // node's AbortError is its own class (name 'AbortError'); this keeps the plain-Error + // shape the module has always produced — not an ERR_* constructor, so not moved. + function abortError() { + var err = new ErrorG('The operation was aborted'); + err.code = 'ABORT_ERR'; + return err; + } + + function addAbortSignal(signal, stream) { + if (signal.aborted) { + nextTick(function () { + stream.destroy(abortError()); + }); + return; + } + signal.addEventListener('abort', function () { + stream.destroy(abortError()); + }); + } + + module.exports = { addAbortSignal: addAbortSignal }; +}); diff --git a/pkg/runtime/js/internal/streams/destroy.js b/pkg/runtime/js/internal/streams/destroy.js new file mode 100644 index 0000000..eab0264 --- /dev/null +++ b/pkg/runtime/js/internal/streams/destroy.js @@ -0,0 +1,106 @@ +// Teardown shared by Readable, Writable and Duplex — node's +// lib/internal/streams/destroy.js. Both state objects are consulted, so one +// implementation serves every stream shape. +(function (require, module) { + 'use strict'; + + var nextTick = process.nextTick; + + // `_undestroy` takes a teardown back: the stream has already emitted its 'finish' / + // 'close' and run its callbacks, and this returns it to a usable state. Node exposes it + // on both prototypes (internal/streams/destroy.js's `undestroy`; verified present on + // node 26.13.0's Writable, Readable AND process.stdout, so it is reachable API, not an + // invention here). It has exactly one caller shape — a `_destroy` that wants the + // teardown undone — which is how node keeps the stdio singletons alive across an + // `end()`; see js/internal/stdio.js. Nothing in this file calls it, so an existing + // stream is unaffected by its presence. + // + // The reset list mirrors node's: enough to make the stream writable/readable again and + // to let a later 'close' emit, without touching buffers, encodings or the high-water + // mark, which the stream keeps across the round trip. + function undestroy() { + var r = this._readableState; + var w = this._writableState; + if (r) { + r.destroyed = false; + r.closeEmitted = false; + r.errored = null; + r.errorEmitted = false; + r.reading = false; + r.ended = false; + r.endEmitted = false; + } + if (w) { + w.destroyed = false; + w.closeEmitted = false; + w.errored = null; + w.errorEmitted = false; + w.prefinished = false; + w.ended = false; + w.ending = false; + w.finished = false; + } + } + + function errorOrDestroy(stream, err) { + var r = stream._readableState; + var w = stream._writableState; + if ((r && r.destroyed) || (w && w.destroyed)) { + // Already torn down (e.g. write-after-end on an auto-destroyed stream): + // the error must still surface exactly once. + if ((r && r.errorEmitted) || (w && w.errorEmitted)) return; + if (r) r.errorEmitted = true; + if (w) w.errorEmitted = true; + nextTick(function () { + stream.emit('error', err); + }); + return; + } + stream.destroy(err); + } + + function destroyImpl(stream, err, cb) { + var r = stream._readableState; + var w = stream._writableState; + if ((r && r.destroyed) || (w && w.destroyed)) { + if (typeof cb === 'function') nextTick(cb, err || null); + return stream; + } + if (r) { + r.destroyed = true; + if (err) r.errored = err; + } + if (w) { + w.destroyed = true; + if (err) w.errored = err; + } + stream._destroy(err || null, function (er) { + er = er || err || null; + if (typeof cb === 'function') nextTick(cb, er); + nextTick(function () { + var emitClose = (!r || r.emitClose) && (!w || w.emitClose); + var alreadyEmitted = (r && r.errorEmitted) || (w && w.errorEmitted); + if (er && !alreadyEmitted && (!cb || typeof cb !== 'function')) { + if (r) r.errorEmitted = true; + if (w) w.errorEmitted = true; + stream.emit('error', er); + } + if (emitClose) { + var closed = (r && r.closeEmitted) || (w && w.closeEmitted); + if (!closed) { + if (r) r.closeEmitted = true; + if (w) w.closeEmitted = true; + stream.emit('close'); + } + } + }); + }); + return stream; + } + + module.exports = { + destroyImpl: destroyImpl, + undestroy: undestroy, + errorOrDestroy: errorOrDestroy, + }; +}); diff --git a/pkg/runtime/js/internal/streams/duplex.js b/pkg/runtime/js/internal/streams/duplex.js new file mode 100644 index 0000000..68a1ede --- /dev/null +++ b/pkg/runtime/js/internal/streams/duplex.js @@ -0,0 +1,60 @@ +// Duplex: a Readable that also carries the Writable API — node's +// lib/internal/streams/duplex.js. +(function (require, module) { + 'use strict'; + + var Stream = require('internal/streams/legacy').Stream; + var readable = require('internal/streams/readable'); + var Readable = readable.Readable; + var ReadableState = readable.ReadableState; + var writable = require('internal/streams/writable'); + var Writable = writable.Writable; + var WritableState = writable.WritableState; + var addAbortSignal = require('internal/streams/add-abort-signal').addAbortSignal; + + function Duplex(options) { + if (!(this instanceof Duplex)) return new Duplex(options); + Stream.call(this); + this._readableState = new ReadableState(options, options && options.readableObjectMode); + this._writableState = new WritableState(options, options && options.writableObjectMode); + if (options) { + if (typeof options.read === 'function') this._read = options.read; + if (typeof options.write === 'function') this._write = options.write; + if (typeof options.writev === 'function') this._writev = options.writev; + if (typeof options.final === 'function') this._final = options.final; + if (typeof options.destroy === 'function') this._destroy = options.destroy; + if (options.encoding) this.setEncoding(options.encoding); + if (options.readable === false) { + this._readableState.ended = true; + this._readableState.endEmitted = true; + } + if (options.writable === false) { + this._writableState.ending = true; + this._writableState.ended = true; + this._writableState.finished = true; + } + if (options.signal) addAbortSignal(options.signal, this); + } + } + Duplex.prototype = Object.create(Readable.prototype); + Duplex.prototype.constructor = Duplex; + // Mix in the Writable API (own-property methods + accessors). + (function mixinWritable() { + var keys = Object.getOwnPropertyNames(Writable.prototype); + for (var i = 0; i < keys.length; i++) { + var k = keys[i]; + if (k === 'constructor') continue; + if (!Object.prototype.hasOwnProperty.call(Duplex.prototype, k)) { + Object.defineProperty( + Duplex.prototype, + k, + Object.getOwnPropertyDescriptor(Writable.prototype, k), + ); + } + } + // destroy must tear down BOTH sides; Readable's prototype copy already + // delegates to the shared destroyImpl, which handles both states. + })(); + + module.exports = Duplex; +}); diff --git a/pkg/runtime/js/internal/streams/end-of-stream.js b/pkg/runtime/js/internal/streams/end-of-stream.js new file mode 100644 index 0000000..763b2b8 --- /dev/null +++ b/pkg/runtime/js/internal/streams/end-of-stream.js @@ -0,0 +1,77 @@ +// finished() — node's lib/internal/streams/end-of-stream.js. +(function (require, module) { + 'use strict'; + + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var isStreamLike = require('internal/streams/utils').isStreamLike; + var ErrorG = require('internal/primordials').Error; + var nextTick = process.nextTick; + + function errPrematureClose() { + return nodeError(ErrorG, 'ERR_STREAM_PREMATURE_CLOSE', 'Premature close'); + } + + function finished(stream, opts, cb) { + if (typeof opts === 'function') { + cb = opts; + opts = {}; + } + opts = opts || {}; + if (!isStreamLike(stream)) + throw ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream); + + var readable = + opts.readable !== false && typeof stream.readable === 'boolean' ? stream.readable : false; + var writable = + opts.writable !== false && typeof stream.writable === 'boolean' ? stream.writable : false; + // Streams that predate the state objects (or foreign ones): fall back to + // listening for everything and settling on the first terminal event. + var r = stream._readableState; + var w = stream._writableState; + var readableEnded = !readable || (r && r.endEmitted); + var writableFinished = !writable || (w && w.finished); + var called = false; + + function done(err) { + if (called) return; + called = true; + cleanup(); + cb.call(stream, err || null); + } + function onend() { + readableEnded = true; + if (writableFinished) done(); + } + function onfinish() { + writableFinished = true; + if (readableEnded) done(); + } + function onerror(err) { + done(err); + } + function onclose() { + if (readable && !readableEnded && !(r && r.endEmitted)) return done(errPrematureClose()); + if (writable && !writableFinished && !(w && w.finished)) return done(errPrematureClose()); + done(); + } + function cleanup() { + stream.removeListener('end', onend); + stream.removeListener('finish', onfinish); + stream.removeListener('error', onerror); + stream.removeListener('close', onclose); + } + if (readableEnded && writableFinished) { + nextTick(done); + } else { + stream.on('end', onend); + stream.on('finish', onfinish); + stream.on('error', onerror); + stream.on('close', onclose); + } + return cleanup; + } + + module.exports = finished; +}); diff --git a/pkg/runtime/js/internal/streams/legacy.js b/pkg/runtime/js/internal/streams/legacy.js new file mode 100644 index 0000000..20cb33f --- /dev/null +++ b/pkg/runtime/js/internal/streams/legacy.js @@ -0,0 +1,15 @@ +// The Stream base constructor every classic stream inherits from — node's +// lib/internal/streams/legacy.js. Lava's has no legacy `pipe`; Readable carries its own. +(function (require, module) { + 'use strict'; + + var EventEmitter = require('events'); + + function Stream(opts) { + EventEmitter.call(this); + } + Stream.prototype = Object.create(EventEmitter.prototype); + Stream.prototype.constructor = Stream; + + module.exports = { Stream: Stream }; +}); diff --git a/pkg/runtime/js/internal/streams/passthrough.js b/pkg/runtime/js/internal/streams/passthrough.js new file mode 100644 index 0000000..a7b8290 --- /dev/null +++ b/pkg/runtime/js/internal/streams/passthrough.js @@ -0,0 +1,18 @@ +// PassThrough — node's lib/internal/streams/passthrough.js. +(function (require, module) { + 'use strict'; + + var Transform = require('internal/streams/transform'); + + function PassThrough(options) { + if (!(this instanceof PassThrough)) return new PassThrough(options); + Transform.call(this, options); + } + PassThrough.prototype = Object.create(Transform.prototype); + PassThrough.prototype.constructor = PassThrough; + PassThrough.prototype._transform = function (chunk, encoding, cb) { + cb(null, chunk); + }; + + module.exports = PassThrough; +}); diff --git a/pkg/runtime/js/internal/streams/pipeline.js b/pkg/runtime/js/internal/streams/pipeline.js new file mode 100644 index 0000000..66bf85a --- /dev/null +++ b/pkg/runtime/js/internal/streams/pipeline.js @@ -0,0 +1,61 @@ +// pipeline() — node's lib/internal/streams/pipeline.js. +(function (require, module) { + 'use strict'; + + var errors = require('internal/errors'); + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var isStreamLike = require('internal/streams/utils').isStreamLike; + var finished = require('internal/streams/end-of-stream'); + + // node's list for a pipeline stage. Lava accepts only the Stream arm so far (functions, + // iterables and web streams are not piped yet), but the error names what node accepts. + var PIPELINE_BODY = [ + 'function', + 'Blob', + 'ReadableStream', + 'WritableStream', + 'Stream', + 'Iterable', + 'AsyncIterable', + 'Promise', + '{ readable, writable } pair', + ]; + + function pipeline() { + var args = []; + for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; + var cb = args.pop(); + if (typeof cb !== 'function') + throw ERR_INVALID_ARG_TYPE('streams[stream.length - 1]', 'function', cb); + if (Array.isArray(args[0]) && args.length === 1) args = args[0]; + if (args.length < 2) { + throw errors.ERR_MISSING_ARGS('streams'); + } + + var streams = args; + var called = false; + function done(err) { + if (called) return; + called = true; + // On failure every stream in the chain is torn down (Node parity). + if (err) { + for (var j = 0; j < streams.length; j++) { + if (typeof streams[j].destroy === 'function') streams[j].destroy(); + } + } + cb(err || null); + } + + for (var k = 0; k < streams.length; k++) { + if (!isStreamLike(streams[k])) throw ERR_INVALID_ARG_TYPE('body', PIPELINE_BODY, streams[k]); + streams[k].on('error', done); + if (k > 0) streams[k - 1].pipe(streams[k]); + } + finished(streams[streams.length - 1], function (err) { + done(err); + }); + return streams[streams.length - 1]; + } + + module.exports = { pipeline: pipeline }; +}); diff --git a/pkg/runtime/js/internal/streams/readable.js b/pkg/runtime/js/internal/streams/readable.js new file mode 100644 index 0000000..f8df0e1 --- /dev/null +++ b/pkg/runtime/js/internal/streams/readable.js @@ -0,0 +1,614 @@ +// Readable and its state — node's lib/internal/streams/readable.js. Readable.from is +// defined here too (node splits its body into internal/streams/from.js); fromWeb/toWeb +// are thin wrappers over internal/webstreams/adapters.js, as in node. +(function (require, module) { + 'use strict'; + + var Stream = require('internal/streams/legacy').Stream; + var Buffer = require('buffer').Buffer; + var StringDecoder = require('string_decoder').StringDecoder; + var ERR_INVALID_ARG_TYPE = require('internal/errors').ERR_INVALID_ARG_TYPE; + var utils = require('internal/streams/utils'); + var CHUNK_EXPECTED = utils.CHUNK_EXPECTED; + var isBytesChunk = utils.isBytesChunk; + var bytesToBuffer = utils.bytesToBuffer; + var errMethodNotImplemented = utils.errMethodNotImplemented; + var destroy = require('internal/streams/destroy'); + var destroyImpl = destroy.destroyImpl; + var errorOrDestroy = destroy.errorOrDestroy; + var addAbortSignal = require('internal/streams/add-abort-signal').addAbortSignal; + var nextTick = process.nextTick; + // Captured at module eval, before user code runs (§5) — `pipe` needs to reach + // `process.stdout`/`stderr` at call time, but must not read the `process` global live. + // The properties are read late on purpose: they are lazy getters installed after this + // module loads, so there is nothing to capture here yet. + var processRef = process; + + // The Web Streams bridges live in internal/webstreams/adapters.js, as in node, and + // that file requires this one at its top level, so the edge back is late-bound: read + // on the first fromWeb/toWeb call and kept. Every call is cold — one per bridge built. + // + // The wrappers below store the result before returning it on purpose: JSC implements + // ES2015 proper tail calls in strict code, so a bare `return lazyAdapters().x(...)` + // drops this frame, and node's stack for a bad fromWeb/toWeb argument names both files. + var adapters = null; + function lazyAdapters() { + if (adapters === null) adapters = require('internal/webstreams/adapters'); + return adapters; + } + + function ReadableState(options, objectModeDefault) { + options = options || {}; + this.objectMode = !!options.objectMode || !!objectModeDefault; + this.highWaterMark = + options.highWaterMark !== undefined ? options.highWaterMark : this.objectMode ? 16 : 65536; + this.buffer = []; // chunks (Buffer | string | any in objectMode) + this.length = 0; // bytes (or chunk count in objectMode) + this.flowing = null; // null: undecided, true: 'data' flow, false: paused + this.ended = false; // push(null) seen + this.endEmitted = false; + this.reading = false; // a _read() is in flight + this.sync = false; // inside a synchronous _read call + this.needReadable = false; + this.emittedReadable = false; + this.destroyed = false; + this.errored = null; + this.errorEmitted = false; + this.closeEmitted = false; + this.autoDestroy = options.autoDestroy !== false; + this.emitClose = options.emitClose !== false; + this.defaultEncoding = options.defaultEncoding || 'utf8'; + this.decoder = null; + this.encoding = null; + this.pipes = []; + this.awaitDrainWriters = 0; + } + + function Readable(options) { + if (!(this instanceof Readable)) return new Readable(options); + Stream.call(this); + this._readableState = new ReadableState(options); + if (options) { + if (typeof options.read === 'function') this._read = options.read; + if (typeof options.destroy === 'function') this._destroy = options.destroy; + if (options.encoding) this.setEncoding(options.encoding); + if (options.signal) addAbortSignal(options.signal, this); + } + } + Readable.prototype = Object.create(Stream.prototype); + Readable.prototype.constructor = Readable; + + Object.defineProperty(Readable.prototype, 'readable', { + get: function () { + var s = this._readableState; + return !!s && !s.destroyed && !s.endEmitted; + }, + }); + Object.defineProperty(Readable.prototype, 'destroyed', { + get: function () { + return this._readableState ? this._readableState.destroyed : false; + }, + set: function (v) { + if (this._readableState) this._readableState.destroyed = v; + }, + }); + Object.defineProperty(Readable.prototype, 'readableEnded', { + get: function () { + return this._readableState ? this._readableState.endEmitted : false; + }, + }); + Object.defineProperty(Readable.prototype, 'readableFlowing', { + get: function () { + return this._readableState ? this._readableState.flowing : null; + }, + }); + Object.defineProperty(Readable.prototype, 'readableLength', { + get: function () { + return this._readableState ? this._readableState.length : 0; + }, + }); + Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { + get: function () { + return this._readableState ? this._readableState.highWaterMark : 0; + }, + }); + Object.defineProperty(Readable.prototype, 'readableObjectMode', { + get: function () { + return this._readableState ? this._readableState.objectMode : false; + }, + }); + + Readable.prototype._read = function () { + throw errMethodNotImplemented('_read()'); + }; + + Readable.prototype.push = function (chunk, encoding) { + var s = this._readableState; + if (chunk === null) { + s.reading = false; + onEofChunk(this, s); + return false; + } + if (!s.objectMode) { + if (typeof chunk === 'string') { + encoding = encoding || s.defaultEncoding; + chunk = Buffer.from(chunk, encoding); + } else if (isBytesChunk(chunk)) { + // The readable half must accept exactly what the writable half does, or a + // PassThrough takes a view on one side and errors on the other — which is how the + // first attempt at this shipped. + chunk = bytesToBuffer(chunk); + } else { + // Unlike write(), a bad push is an ASYNC 'error' on node, not a throw. The + // asymmetry is node's; do not "fix" it into a throw. + errorOrDestroy(this, ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk)); + return false; + } + if (s.decoder) chunk = s.decoder.write(chunk); + } + s.reading = false; + var size = s.objectMode ? 1 : chunk.length; + if (s.decoder && !s.objectMode) size = chunk.length; // decoded string length + if (s.flowing && s.length === 0 && !s.sync && this.listenerCount('data') > 0) { + // Fast path: nothing buffered and a flowing consumer — deliver directly. + this.emit('data', chunk); + } else { + s.buffer.push(chunk); + s.length += size; + if (s.needReadable) emitReadable(this); + } + maybeReadMore(this, s); + return s.length < s.highWaterMark; + }; + + Readable.prototype.unshift = function (chunk, encoding) { + var s = this._readableState; + if (chunk === null) { + onEofChunk(this, s); + return false; + } + if (!s.objectMode) { + if (typeof chunk === 'string') chunk = Buffer.from(chunk, encoding || s.defaultEncoding); + if (s.decoder) chunk = s.decoder.write(chunk); + } + s.buffer.unshift(chunk); + s.length += s.objectMode ? 1 : chunk.length; + if (s.needReadable) emitReadable(this); + return s.length < s.highWaterMark; + }; + + function onEofChunk(stream, s) { + if (s.ended) return; + if (s.decoder) { + var tail = s.decoder.end(); + if (tail && tail.length) { + s.buffer.push(tail); + s.length += tail.length; + } + } + s.ended = true; + if (s.length === 0) { + emitReadable(stream); // a final zero-data 'readable' announces EOF + endReadableMaybe(stream); + } else if (s.needReadable) { + emitReadable(stream); + } + } + + function emitReadable(stream) { + var s = stream._readableState; + s.needReadable = false; + if (s.emittedReadable) return; + s.emittedReadable = true; + nextTick(function () { + s.emittedReadable = false; + if (s.destroyed) return; + if (s.length || s.ended) stream.emit('readable'); + flow(stream); + }); + } + + function maybeReadMore(stream, s) { + if (!s.reading && !s.ended && s.length < s.highWaterMark && s.flowing !== false) { + // Keep the pump primed while below HWM (mirrors Node's read(0) loop). + nextTick(function () { + if (!s.reading && !s.ended && !s.destroyed && s.length < s.highWaterMark) { + stream.read(0); + } + }); + } + } + + Readable.prototype.read = function (n) { + var s = this._readableState; + if (s.destroyed) return null; + if (n === undefined || n === null || isNaN(n)) n = -1; // "all" + + if (n === 0 && s.ended && s.length === 0) { + endReadableMaybe(this); + return null; + } + + // Trigger a _read when the buffer can absorb more (or is empty). + var doRead = false; + if (!s.ended && !s.reading && (s.length === 0 || s.length < s.highWaterMark)) doRead = true; + if (doRead) { + s.reading = true; + s.sync = true; + try { + this._read(s.highWaterMark); + } catch (err) { + errorOrDestroy(this, err); + } + s.sync = false; + } + + var ret = null; + if (n === 0) { + s.needReadable = s.length === 0 && !s.ended; + return null; + } + if (s.objectMode) { + ret = s.length > 0 ? s.buffer.shift() : null; + if (ret !== null) s.length -= 1; + } else if (n < 0) { + if (s.length > 0) { + if (s.flowing || s.buffer.length === 1) { + // Flowing mode delivers chunk-at-a-time (Node's howMuchToRead returns + // the first chunk's length when flowing), so 'data' events preserve + // the producer's chunk boundaries. + ret = s.buffer.shift(); + s.length -= ret.length; + } else if (typeof s.buffer[0] === 'string') { + ret = s.buffer.join(''); + s.buffer.length = 0; + s.length = 0; + } else { + ret = Buffer.concat(s.buffer); + s.buffer.length = 0; + s.length = 0; + } + } + } else { + if (s.length >= n) { + ret = takeBytes(s, n); + } else if (s.ended && s.length > 0) { + ret = takeBytes(s, s.length); + } + } + + if (ret === null) { + s.needReadable = true; + } + if (s.ended && s.length === 0) endReadableMaybe(this); + // Node emits 'data' from read() itself — explicit reads and the flow loop + // share one delivery point. + if (ret !== null) this.emit('data', ret); + return ret; + }; + + function takeBytes(s, n) { + // Byte-accurate extraction across buffered chunks (strings when a decoder is + // set: n then counts code units, matching Node's decoded reads). + var first = s.buffer[0]; + if (typeof first === 'string') { + if (first.length <= n) { + s.buffer.shift(); + s.length -= first.length; + return first; + } + s.buffer[0] = first.slice(n); + s.length -= n; + return first.slice(0, n); + } + if (first.length === n) { + s.buffer.shift(); + s.length -= n; + return first; + } + if (first.length > n) { + s.buffer[0] = first.subarray(n); + s.length -= n; + return first.subarray(0, n); + } + var out = Buffer.allocUnsafe(n); + var off = 0; + while (off < n && s.buffer.length) { + var c = s.buffer[0]; + var want = n - off; + if (c.length <= want) { + out.set(c, off); + off += c.length; + s.buffer.shift(); + } else { + out.set(c.subarray(0, want), off); + s.buffer[0] = c.subarray(want); + off += want; + } + } + s.length -= n; + return out; + } + + function endReadableMaybe(stream) { + var s = stream._readableState; + if (s.endEmitted || !s.ended || s.length > 0) return; + s.endEmitted = true; + nextTick(function () { + if (!s.errored) stream.emit('end'); + if (s.autoDestroy) { + // End of a Duplex only auto-destroys once the write side finished too. + var w = stream._writableState; + if (!w || (w.finished && w.autoDestroy) || !w) stream.destroy(); + } + }); + } + + Readable.prototype.setEncoding = function (enc) { + var s = this._readableState; + var decoder = new StringDecoder(enc); + s.decoder = decoder; + s.encoding = enc; + // Re-decode anything already buffered into one string chunk. + if (s.buffer.length) { + var content = ''; + for (var i = 0; i < s.buffer.length; i++) content += decoder.write(s.buffer[i]); + s.buffer.length = 0; + if (content !== '') s.buffer.push(content); + s.length = content.length; + } + return this; + }; + + Readable.prototype.pause = function () { + var s = this._readableState; + if (s.flowing !== false) { + s.flowing = false; + this.emit('pause'); + } + return this; + }; + + Readable.prototype.resume = function () { + var s = this._readableState; + if (!s.flowing) { + s.flowing = true; + var self = this; + nextTick(function () { + self.emit('resume'); + flow(self); + }); + } + return this; + }; + + Readable.prototype.isPaused = function () { + return this._readableState.flowing === false; + }; + + function flow(stream) { + // read() emits 'data' itself; the loop just drains until pause or empty. + var s = stream._readableState; + while (s.flowing && stream.read() !== null) {} + } + + Readable.prototype.on = function (ev, fn) { + var res = Stream.prototype.on.call(this, ev, fn); + var s = this._readableState; + if (ev === 'data') { + if (s.flowing !== false) this.resume(); + } else if (ev === 'readable') { + if (!s.endEmitted) { + s.needReadable = true; + if (s.length || s.ended) emitReadable(this); + else if (!s.reading) { + var self = this; + nextTick(function () { + if (!s.destroyed && !s.ended && !s.reading) self.read(0); + }); + } + } + } + return res; + }; + Readable.prototype.addListener = Readable.prototype.on; + + Readable.prototype.pipe = function (dest, pipeOpts) { + var src = this; + var s = this._readableState; + s.pipes.push(dest); + // node excludes the stdio singletons from the automatic `dest.end()`, and the + // omission is not cosmetic: `src.pipe(process.stdout)` is the most common pipe in + // Node, and ending stdout ends it for the whole process — every later write is + // dropped and raises 'write after end'. + // + // Identity, exactly as node does it (`dest !== process.stdout && dest !== + // process.stderr`), rather than testing a `_isStdio` property: an absent-property + // read walks Object.prototype, so `Object.prototype._isStdio = true` would stop + // pipe() from ending ANY destination — §5's uncounted-vector case. Reading the getter + // builds the stdout stream on first pipe even when piping elsewhere; node pays the + // same cost, it is once per process, and the getter caches. + var doEnd = + (!pipeOpts || pipeOpts.end !== false) && + dest !== processRef.stdout && + dest !== processRef.stderr; + + function ondata(chunk) { + var ret = dest.write(chunk); + if (ret === false) { + s.awaitDrainWriters++; + src.pause(); + } + } + function ondrain() { + if (s.awaitDrainWriters > 0) s.awaitDrainWriters--; + if (s.awaitDrainWriters === 0) src.resume(); + } + function onend() { + if (doEnd) dest.end(); + } + function onerror(er) { + unpipeAll(); + if (dest.listenerCount('error') === 0) errorOrDestroy(dest, er); + } + function onclose() { + unpipeAll(); + } + function unpipeAll() { + src.removeListener('data', ondata); + dest.removeListener('drain', ondrain); + src.removeListener('end', onend); + src.removeListener('error', onerror); + dest.removeListener('close', onclose); + var idx = s.pipes.indexOf(dest); + if (idx !== -1) s.pipes.splice(idx, 1); + dest.emit('unpipe', src); + } + + src.on('data', ondata); + dest.on('drain', ondrain); + src.on('end', onend); + src.on('error', onerror); + dest.on('close', onclose); + dest.on('unpipe-request', unpipeAll); + dest.emit('pipe', src); + return dest; + }; + + Readable.prototype.unpipe = function (dest) { + var s = this._readableState; + if (dest === undefined) { + var all = s.pipes.slice(); + for (var i = 0; i < all.length; i++) all[i].emit('unpipe-request'); + return this; + } + if (s.pipes.indexOf(dest) !== -1) dest.emit('unpipe-request'); + return this; + }; + + Readable.prototype.destroy = function (err, cb) { + return destroyImpl(this, err, cb); + }; + Readable.prototype._destroy = function (err, cb) { + cb(err); + }; + + Readable.prototype[Symbol.asyncIterator] = function () { + var stream = this; + var s = this._readableState; + return { + next: function () { + return new Promise(function (resolve, reject) { + if (s.errored) return reject(s.errored); + var chunk = stream.read(); + if (chunk !== null) return resolve({ value: chunk, done: false }); + if (s.endEmitted || (s.ended && s.length === 0)) { + endReadableMaybe(stream); + return resolve({ value: undefined, done: true }); + } + function cleanup() { + stream.removeListener('readable', onreadable); + stream.removeListener('end', onend); + stream.removeListener('error', onerror); + stream.removeListener('close', onclose); + } + function onreadable() { + cleanup(); + var c = stream.read(); + if (c !== null) resolve({ value: c, done: false }); + else if (s.ended) resolve({ value: undefined, done: true }); + else attach(); + } + function onend() { + cleanup(); + resolve({ value: undefined, done: true }); + } + function onerror(er) { + cleanup(); + reject(er); + } + function onclose() { + cleanup(); + resolve({ value: undefined, done: true }); + } + function attach() { + stream.once('readable', onreadable); + stream.once('end', onend); + stream.once('error', onerror); + stream.once('close', onclose); + } + attach(); + }); + }, + return: function () { + stream.destroy(); + return Promise.resolve({ value: undefined, done: true }); + }, + [Symbol.asyncIterator]: function () { + return this; + }, + }; + }; + + Readable.from = function (iterable, options) { + var readable = new Readable( + Object.assign({ objectMode: true }, options, { + read: function () { + pump(); + }, + }), + ); + var iterator = null; + var pumping = false; + function getIterator() { + if (iterator) return iterator; + if (iterable && typeof iterable[Symbol.asyncIterator] === 'function') { + iterator = iterable[Symbol.asyncIterator](); + } else if (iterable && typeof iterable[Symbol.iterator] === 'function') { + iterator = iterable[Symbol.iterator](); + } else { + throw ERR_INVALID_ARG_TYPE('iterable', 'Iterable', iterable); + } + return iterator; + } + function pump() { + if (pumping) return; + pumping = true; + Promise.resolve() + .then(function step() { + return Promise.resolve(getIterator().next()).then(function (res) { + if (res.done) { + readable.push(null); + pumping = false; + return; + } + if (readable.destroyed) { + pumping = false; + return; + } + if (readable.push(res.value)) return step(); + pumping = false; + }); + }) + .catch(function (err) { + pumping = false; + errorOrDestroy(readable, err); + }); + } + return readable; + }; + + Readable.prototype._undestroy = destroy.undestroy; + + Readable.fromWeb = function (webStream, options) { + var bridged = lazyAdapters().newStreamReadableFromReadableStream(webStream, options); + return bridged; + }; + + Readable.toWeb = function (nodeReadable) { + var bridged = lazyAdapters().newReadableStreamFromStreamReadable(nodeReadable); + return bridged; + }; + + module.exports = { Readable: Readable, ReadableState: ReadableState }; +}); diff --git a/pkg/runtime/js/internal/streams/transform.js b/pkg/runtime/js/internal/streams/transform.js new file mode 100644 index 0000000..accd2fa --- /dev/null +++ b/pkg/runtime/js/internal/streams/transform.js @@ -0,0 +1,59 @@ +// Transform — node's lib/internal/streams/transform.js. +(function (require, module) { + 'use strict'; + + var Duplex = require('internal/streams/duplex'); + var errMethodNotImplemented = require('internal/streams/utils').errMethodNotImplemented; + + function Transform(options) { + if (!(this instanceof Transform)) return new Transform(options); + Duplex.call(this, options); + this._transformCallback = null; + if (options && typeof options.transform === 'function') this._transform = options.transform; + if (options && typeof options.flush === 'function') this._flush = options.flush; + } + Transform.prototype = Object.create(Duplex.prototype); + Transform.prototype.constructor = Transform; + + Transform.prototype._transform = function () { + throw errMethodNotImplemented('_transform()'); + }; + + Transform.prototype._write = function (chunk, encoding, cb) { + var stream = this; + var r = this._readableState; + this._transform(chunk, encoding, function (err, data) { + if (err) return cb(err); + if (data !== null && data !== undefined) stream.push(data); + // Read-side backpressure: hold the write pipeline until the consumer + // drains the readable buffer below HWM (Node's Transform contract). + if (r.length < r.highWaterMark || r.destroyed) cb(); + else stream._transformCallback = cb; + }); + }; + + Transform.prototype._read = function () { + var cb = this._transformCallback; + if (cb) { + this._transformCallback = null; + cb(); + } + }; + + Transform.prototype._final = function (cb) { + var stream = this; + if (typeof this._flush === 'function') { + this._flush(function (err, data) { + if (err) return cb(err); + if (data !== null && data !== undefined) stream.push(data); + stream.push(null); + cb(); + }); + } else { + stream.push(null); + cb(); + } + }; + + module.exports = Transform; +}); diff --git a/pkg/runtime/js/internal/streams/utils.js b/pkg/runtime/js/internal/streams/utils.js new file mode 100644 index 0000000..87b60a4 --- /dev/null +++ b/pkg/runtime/js/internal/streams/utils.js @@ -0,0 +1,104 @@ +// Helpers shared by more than one internal/streams module. node keeps isNodeStream and +// friends in lib/internal/streams/utils.js; isStreamLike is Lava's (narrower) version of +// that check. The chunk helpers have no single home in node — it hangs +// _isArrayBufferView/_uint8ArrayToBuffer off the Stream constructor in lib/stream.js — +// and Lava does not expose those statics, so readable.js and writable.js share them here. +(function (require, module) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + var BufferIsEncoding = Buffer.isEncoding; + + // Through primordials, NOT captured off the globals here: these were written when stream + // was believed lazy, where a module-eval capture runs after user code and is steerable + // (#333), and primordials keeps them safe whichever way the module is instantiated. + // `ArrayBuffer.isView` in particular is a + // security decision — read live, `globalThis.ArrayBuffer = {isView: () => true}` walks a + // plain object straight into the byte path. + var StreamPrimordials = require('internal/primordials'); + var ArrayBufferIsView = StreamPrimordials.ArrayBufferIsView; + var ErrorG = StreamPrimordials.Error; + var ObjectGetPrototypeOf = StreamPrimordials.ObjectGetPrototypeOf; + var DataViewPrototype = StreamPrimordials.DataViewPrototype; + var DataViewPrototypeGetBuffer = StreamPrimordials.DataViewPrototypeGetBuffer; + var DataViewPrototypeGetByteOffset = StreamPrimordials.DataViewPrototypeGetByteOffset; + var DataViewPrototypeGetByteLength = StreamPrimordials.DataViewPrototypeGetByteLength; + var TypedArrayPrototypeGetBuffer = StreamPrimordials.TypedArrayPrototypeGetBuffer; + var TypedArrayPrototypeGetByteOffset = StreamPrimordials.TypedArrayPrototypeGetByteOffset; + var TypedArrayPrototypeGetByteLength = StreamPrimordials.TypedArrayPrototypeGetByteLength; + + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; + function errMethodNotImplemented(name) { + return nodeError( + ErrorG, + 'ERR_METHOD_NOT_IMPLEMENTED', + 'The ' + name + ' method is not implemented', + ); + } + + // node's chunk contract, verified on 24.18.1: a string, or ANY ArrayBuffer view — + // Buffer, every TypedArray, and DataView. `instanceof Uint8Array` is wrong in both + // directions: it rejects DataView and Int16Array, which node accepts, and it accepts + // `Object.create(Uint8Array.prototype)`, which node rejects. ArrayBuffer.isView is the + // whole test, and it also refuses a bare ArrayBuffer. + var CHUNK_EXPECTED = ['string', 'Buffer', 'TypedArray', 'DataView']; + function isBytesChunk(chunk) { + return ArrayBufferIsView(chunk); + } + + function isDataViewChunk(value) { + // Prototype-chain walk rather than `instanceof` (forgeable through Symbol.hasInstance) + // or a try/catch around a getter (an exception per write on the hot path). + var proto = ObjectGetPrototypeOf(value); + while (proto !== null) { + if (proto === DataViewPrototype) return true; + proto = ObjectGetPrototypeOf(proto); + } + return false; + } + + // THE STEP WHOSE ABSENCE CAUSED #326's REGRESSION. node hands `_write` a Buffer with + // encoding 'buffer' for every view, so widening the accept set without converting left + // `chunk.length` undefined for a DataView: `writableLength` went NaN, `'drain'` could + // never fire again (NaN === 0 is false), and pipe() stalled forever. + // + // The two getter families are NOT interchangeable — a %TypedArray% getter throws on a + // DataView receiver, which is exactly what the in-flight fix hit last time — so the + // brand decides which set to read the window through. + function bytesToBuffer(chunk) { + if (isDataViewChunk(chunk)) { + return Buffer.from( + DataViewPrototypeGetBuffer(chunk), + DataViewPrototypeGetByteOffset(chunk), + DataViewPrototypeGetByteLength(chunk), + ); + } + return Buffer.from( + TypedArrayPrototypeGetBuffer(chunk), + TypedArrayPrototypeGetByteOffset(chunk), + TypedArrayPrototypeGetByteLength(chunk), + ); + } + + // node validates an EXPLICIT encoding before it looks at the chunk, which is why + // `write(5, 'bogus')` reports the encoding and not the chunk type. 'buffer' is node's + // internal marker and is always allowed through. + function validateEncoding(encoding) { + if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) throw ERR_UNKNOWN_ENCODING(encoding); + } + + function isStreamLike(s) { + return s !== null && typeof s === 'object' && typeof s.on === 'function'; + } + + module.exports = { + CHUNK_EXPECTED: CHUNK_EXPECTED, + isBytesChunk: isBytesChunk, + bytesToBuffer: bytesToBuffer, + validateEncoding: validateEncoding, + errMethodNotImplemented: errMethodNotImplemented, + isStreamLike: isStreamLike, + }; +}); diff --git a/pkg/runtime/js/internal/streams/writable.js b/pkg/runtime/js/internal/streams/writable.js new file mode 100644 index 0000000..e93f771 --- /dev/null +++ b/pkg/runtime/js/internal/streams/writable.js @@ -0,0 +1,398 @@ +// Writable and its state — node's lib/internal/streams/writable.js. fromWeb/toWeb are +// thin wrappers over internal/webstreams/adapters.js, as in node. +(function (require, module) { + 'use strict'; + + var Stream = require('internal/streams/legacy').Stream; + var Buffer = require('buffer').Buffer; + var ErrorG = require('internal/primordials').Error; + var TypeErrorG = require('internal/primordials').TypeError; + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var utils = require('internal/streams/utils'); + var CHUNK_EXPECTED = utils.CHUNK_EXPECTED; + var isBytesChunk = utils.isBytesChunk; + var bytesToBuffer = utils.bytesToBuffer; + var validateEncoding = utils.validateEncoding; + var errMethodNotImplemented = utils.errMethodNotImplemented; + var destroy = require('internal/streams/destroy'); + var destroyImpl = destroy.destroyImpl; + var errorOrDestroy = destroy.errorOrDestroy; + var addAbortSignal = require('internal/streams/add-abort-signal').addAbortSignal; + var nextTick = process.nextTick; + + // The Web Streams bridges live in internal/webstreams/adapters.js, as in node, and + // that file requires this one at its top level, so the edge back is late-bound: read + // on the first fromWeb/toWeb call and kept. Every call is cold — one per bridge built. + // + // The wrappers below store the result before returning it on purpose: JSC implements + // ES2015 proper tail calls in strict code, so a bare `return lazyAdapters().x(...)` + // drops this frame, and node's stack for a bad fromWeb/toWeb argument names both files. + var adapters = null; + function lazyAdapters() { + if (adapters === null) adapters = require('internal/webstreams/adapters'); + return adapters; + } + + function errWriteAfterEnd() { + return nodeError(ErrorG, 'ERR_STREAM_WRITE_AFTER_END', 'write after end'); + } + function errDestroyed(what) { + return nodeError( + ErrorG, + 'ERR_STREAM_DESTROYED', + 'Cannot call ' + what + ' after a stream was destroyed', + ); + } + function errNullValues() { + return nodeError(TypeErrorG, 'ERR_STREAM_NULL_VALUES', 'May not write null values to stream'); + } + + function WritableState(options, objectModeDefault) { + options = options || {}; + this.objectMode = !!options.objectMode || !!objectModeDefault; + this.highWaterMark = + options.highWaterMark !== undefined ? options.highWaterMark : this.objectMode ? 16 : 65536; + this.decodeStrings = options.decodeStrings !== false; + this.defaultEncoding = options.defaultEncoding || 'utf8'; + this.buffered = []; // {chunk, encoding, cb} + this.length = 0; + this.writing = false; + this.corked = 0; + this.sync = true; // inside the synchronous span of a _write call + this.needDrain = false; + this.ending = false; + this.ended = false; + this.finished = false; + this.prefinished = false; + this.destroyed = false; + this.errored = null; + this.errorEmitted = false; + this.closeEmitted = false; + this.autoDestroy = options.autoDestroy !== false; + this.emitClose = options.emitClose !== false; + this.writecb = null; + this.writelen = 0; + } + + function Writable(options) { + if (!(this instanceof Writable)) return new Writable(options); + Stream.call(this); + this._writableState = new WritableState(options); + if (options) { + if (typeof options.write === 'function') this._write = options.write; + if (typeof options.writev === 'function') this._writev = options.writev; + if (typeof options.final === 'function') this._final = options.final; + if (typeof options.destroy === 'function') this._destroy = options.destroy; + if (options.signal) addAbortSignal(options.signal, this); + } + } + Writable.prototype = Object.create(Stream.prototype); + Writable.prototype.constructor = Writable; + + Object.defineProperty(Writable.prototype, 'writable', { + get: function () { + var w = this._writableState; + return !!w && !w.destroyed && !w.ending; + }, + }); + Object.defineProperty(Writable.prototype, 'destroyed', { + get: function () { + return this._writableState ? this._writableState.destroyed : false; + }, + set: function (v) { + if (this._writableState) this._writableState.destroyed = v; + }, + }); + Object.defineProperty(Writable.prototype, 'writableEnded', { + get: function () { + return this._writableState ? this._writableState.ending : false; + }, + }); + Object.defineProperty(Writable.prototype, 'writableFinished', { + get: function () { + return this._writableState ? this._writableState.finished : false; + }, + }); + Object.defineProperty(Writable.prototype, 'writableLength', { + get: function () { + return this._writableState ? this._writableState.length : 0; + }, + }); + Object.defineProperty(Writable.prototype, 'writableHighWaterMark', { + get: function () { + return this._writableState ? this._writableState.highWaterMark : 0; + }, + }); + Object.defineProperty(Writable.prototype, 'writableObjectMode', { + get: function () { + return this._writableState ? this._writableState.objectMode : false; + }, + }); + Object.defineProperty(Writable.prototype, 'writableNeedDrain', { + get: function () { + var w = this._writableState; + return !!w && !w.destroyed && !w.ending && w.needDrain; + }, + }); + + Writable.prototype._write = function () { + throw errMethodNotImplemented('_write()'); + }; + + Writable.prototype.write = function (chunk, encoding, cb) { + if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + var w = this._writableState; + // An explicit encoding is validated FIRST — before null, before the chunk type. That + // ordering is node's and it is observable: `write(5, 'bogus')` reports the encoding. + if (encoding) validateEncoding(encoding); + encoding = encoding || w.defaultEncoding; + if (typeof cb !== 'function') cb = noop; + + // node THROWS both of these synchronously rather than reporting them on the stream. + // Routing them through writeErrorNextTick delivered the right code on the wrong turn, + // so `assert.throws(() => s.write(null))` passed under node and failed here. + if (chunk === null) throw errNullValues(); + if (!w.objectMode) { + if (typeof chunk === 'string') { + if (w.decodeStrings) { + chunk = Buffer.from(chunk, encoding); + encoding = 'buffer'; + } + } else if (isBytesChunk(chunk)) { + // Normalize every view to a Buffer, as node does, so downstream length accounting + // and `_write` see the same shape they always have. + chunk = bytesToBuffer(chunk); + encoding = 'buffer'; + } else { + throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk); + } + } + if (w.ending) { + var endErr = errWriteAfterEnd(); + writeErrorNextTick(this, endErr, cb); + return false; + } + if (w.destroyed) { + var dErr = errDestroyed('write'); + writeErrorNextTick(this, dErr, cb); + return false; + } + + var len = w.objectMode ? 1 : chunk.length; + w.length += len; + + if (w.writing || w.corked > 0 || w.errored) { + w.buffered.push({ chunk: chunk, encoding: encoding, cb: cb }); + } else { + doWrite(this, w, chunk, len, encoding, cb); + } + + var ret = w.length < w.highWaterMark && !w.errored; + if (!ret) w.needDrain = true; + return ret; + }; + + function writeErrorNextTick(stream, err, cb) { + nextTick(function () { + cb(err); + errorOrDestroy(stream, err); + }); + } + + function doWrite(stream, w, chunk, len, encoding, cb) { + w.writing = true; + w.writecb = cb; + w.writelen = len; + w.sync = true; + try { + stream._write(chunk, encoding, function (err) { + onwriteDone(stream, w, err); + }); + } catch (err) { + onwriteDone(stream, w, err); + } + w.sync = false; + } + + function onwriteDone(stream, w, err) { + var cb = w.writecb || noop; + var len = w.writelen; + w.writing = false; + w.writecb = null; + w.writelen = 0; + w.length -= len; + + if (err) { + if (w.sync) { + nextTick(function () { + cb(err); + errorOrDestroy(stream, err); + }); + } else { + cb(err); + errorOrDestroy(stream, err); + } + return; + } + + if (w.sync) { + // The user cb (and everything that may cascade from it) runs on a clean + // stack, matching Node's afterWrite deferral for synchronous _write. + nextTick(function () { + afterWrite(stream, w, cb); + }); + } else { + afterWrite(stream, w, cb); + } + } + + function afterWrite(stream, w, cb) { + cb(); + if (!w.writing && w.corked === 0 && w.buffered.length > 0 && !w.destroyed) { + var entry = w.buffered.shift(); + doWrite( + stream, + w, + entry.chunk, + w.objectMode ? 1 : entry.chunk.length, + entry.encoding, + entry.cb, + ); + return; // drain/finish decisions re-run after the queue empties + } + if (w.needDrain && w.length === 0 && !w.ending && !w.destroyed) { + w.needDrain = false; + stream.emit('drain'); + } + finishMaybe(stream, w); + } + + Writable.prototype.cork = function () { + this._writableState.corked++; + }; + Writable.prototype.uncork = function () { + var w = this._writableState; + if (w.corked > 0) { + w.corked--; + if (w.corked === 0 && !w.writing && w.buffered.length > 0) { + var entry = w.buffered.shift(); + doWrite( + this, + w, + entry.chunk, + w.objectMode ? 1 : entry.chunk.length, + entry.encoding, + entry.cb, + ); + } + } + }; + + Writable.prototype.setDefaultEncoding = function (encoding) { + validateEncoding(encoding); + this._writableState.defaultEncoding = encoding; + return this; + }; + + Writable.prototype.end = function (chunk, encoding, cb) { + if (typeof chunk === 'function') { + cb = chunk; + chunk = null; + encoding = null; + } else if (typeof encoding === 'function') { + cb = encoding; + encoding = null; + } + var w = this._writableState; + if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); + if (w.ending || w.destroyed) { + if (typeof cb === 'function') { + var err = w.finished + ? nodeError( + ErrorG, + 'ERR_STREAM_ALREADY_FINISHED', + 'Cannot call end after a stream was finished', + ) + : w.destroyed + ? errDestroyed('end') + : null; + if (err) nextTick(cb, err); + else if (w.finished) nextTick(cb); + else this.once('finish', cb); + } + return this; + } + w.ending = true; + if (typeof cb === 'function') this.once('finish', cb); + finishMaybe(this, w); + return this; + }; + + function finishMaybe(stream, w) { + if (!w.ending || w.finished || w.writing || w.buffered.length > 0 || w.errored || w.destroyed) + return; + if (!w.prefinished) { + w.prefinished = true; + stream.emit('prefinish'); + if (typeof stream._final === 'function') { + try { + stream._final(function (err) { + if (err) { + errorOrDestroy(stream, err); + return; + } + emitFinish(stream, w); + }); + } catch (err) { + errorOrDestroy(stream, err); + } + return; + } + } + emitFinish(stream, w); + } + + function emitFinish(stream, w) { + if (w.finished) return; + w.finished = true; + w.ended = true; + nextTick(function () { + stream.emit('finish'); + if (w.autoDestroy) { + // A Duplex only auto-destroys once the read side ended too. + var r = stream._readableState; + if (!r || r.endEmitted || !r.ended) { + if (!r || r.endEmitted) stream.destroy(); + } + } + }); + } + + Writable.prototype.destroy = function (err, cb) { + return destroyImpl(this, err, cb); + }; + Writable.prototype._destroy = function (err, cb) { + cb(err); + }; + + Writable.prototype._undestroy = destroy.undestroy; + + function noop() {} + + Writable.fromWeb = function (webStream, options) { + var bridged = lazyAdapters().newStreamWritableFromWritableStream(webStream, options); + return bridged; + }; + + Writable.toWeb = function (nodeWritable) { + var bridged = lazyAdapters().newWritableStreamFromStreamWritable(nodeWritable); + return bridged; + }; + + module.exports = { Writable: Writable, WritableState: WritableState }; +}); diff --git a/pkg/runtime/js/internal/webstreams/adapters.js b/pkg/runtime/js/internal/webstreams/adapters.js new file mode 100644 index 0000000..e6e7382 --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/adapters.js @@ -0,0 +1,141 @@ +// Bridges between classic streams and Web Streams — the Lava side of node's +// lib/internal/webstreams/adapters.js, which is where node implements Readable/Writable +// .fromWeb/.toWeb too (its function names are kept). Required late, from those statics, +// so a script that never bridges never compiles it. +(function (require, module) { + 'use strict'; + + var Readable = require('internal/streams/readable').Readable; + var Writable = require('internal/streams/writable').Writable; + var errorOrDestroy = require('internal/streams/destroy').errorOrDestroy; + + function newStreamReadableFromReadableStream(webStream, options) { + var reader = webStream.getReader(); + var readable = new Readable( + Object.assign({}, options, { + read: function () { + var self = this; + reader.read().then( + function (res) { + if (res.done) self.push(null); + else self.push(res.value); + }, + function (err) { + errorOrDestroy(self, err); + }, + ); + }, + destroy: function (err, cb) { + reader.cancel(err).then( + function () { + cb(err); + }, + function () { + cb(err); + }, + ); + }, + }), + ); + return readable; + } + + function newReadableStreamFromStreamReadable(nodeReadable) { + var ReadableStreamCtor = require('stream/web').ReadableStream; + return new ReadableStreamCtor({ + start: function (controller) { + nodeReadable.on('data', function (chunk) { + controller.enqueue(chunk); + if (controller.desiredSize !== null && controller.desiredSize <= 0) nodeReadable.pause(); + }); + nodeReadable.on('end', function () { + try { + controller.close(); + } catch (e) { + /* already closed */ + } + }); + nodeReadable.on('error', function (err) { + controller.error(err); + }); + }, + pull: function () { + nodeReadable.resume(); + }, + cancel: function (reason) { + nodeReadable.destroy(reason instanceof Error ? reason : undefined); + }, + }); + } + + function newStreamWritableFromWritableStream(webStream, options) { + var writer = webStream.getWriter(); + return new Writable( + Object.assign({}, options, { + write: function (chunk, encoding, cb) { + writer.write(chunk).then( + function () { + cb(); + }, + function (err) { + cb(err); + }, + ); + }, + final: function (cb) { + writer.close().then( + function () { + cb(); + }, + function (err) { + cb(err); + }, + ); + }, + destroy: function (err, cb) { + writer.abort(err).then( + function () { + cb(err); + }, + function () { + cb(err); + }, + ); + }, + }), + ); + } + + function newWritableStreamFromStreamWritable(nodeWritable) { + var WritableStreamCtor = require('stream/web').WritableStream; + return new WritableStreamCtor({ + write: function (chunk) { + return new Promise(function (resolve, reject) { + var ok = nodeWritable.write(chunk, function (err) { + if (err) reject(err); + }); + if (ok) resolve(); + else nodeWritable.once('drain', resolve); + }); + }, + close: function () { + return new Promise(function (resolve, reject) { + nodeWritable.end(function () { + resolve(); + }); + nodeWritable.once('error', reject); + }); + }, + abort: function (reason) { + nodeWritable.destroy(reason instanceof Error ? reason : undefined); + }, + }); + } + + module.exports = { + newStreamReadableFromReadableStream: newStreamReadableFromReadableStream, + newReadableStreamFromStreamReadable: newReadableStreamFromStreamReadable, + newStreamWritableFromWritableStream: newStreamWritableFromWritableStream, + newWritableStreamFromStreamWritable: newWritableStreamFromStreamWritable, + }; +}); diff --git a/pkg/runtime/js/node/stream.js b/pkg/runtime/js/node/stream.js index 1ab9a6d..10a3237 100644 --- a/pkg/runtime/js/node/stream.js +++ b/pkg/runtime/js/node/stream.js @@ -9,1486 +9,24 @@ // used. Ordering of the observable events — 'data' synchronous from flow, cb / // 'drain' / 'finish' / 'end' / 'close' deferred a tick — follows Node so the // oracle cases diff clean. +// +// This file only assembles the public module. The implementation lives under +// js/internal/streams/ (and js/internal/webstreams/adapters.js for the bridges), one +// file per node lib/internal/streams module, so a stack frame names the same source +// node's does (`node:internal/streams/readable`). Pinned by +// tests/node-compat/cases/79-stream-internal-modules.js. (function (require, module) { 'use strict'; - var EventEmitter = require('events'); - var bufferModule = require('buffer'); - var Buffer = bufferModule.Buffer; - var BufferIsEncoding = Buffer.isEncoding; - - // Through primordials, NOT captured here: this module is lazy, so a module-eval capture - // runs after user code and is steerable (#333). `ArrayBuffer.isView` in particular is a - // security decision — read live, `globalThis.ArrayBuffer = {isView: () => true}` walks a - // plain object straight into the byte path. - var StreamPrimordials = require('internal/primordials'); - var ArrayBufferIsView = StreamPrimordials.ArrayBufferIsView; - var ErrorG = StreamPrimordials.Error; - var TypeErrorG = StreamPrimordials.TypeError; - var ObjectGetPrototypeOf = StreamPrimordials.ObjectGetPrototypeOf; - var DataViewPrototype = StreamPrimordials.DataViewPrototype; - var DataViewPrototypeGetBuffer = StreamPrimordials.DataViewPrototypeGetBuffer; - var DataViewPrototypeGetByteOffset = StreamPrimordials.DataViewPrototypeGetByteOffset; - var DataViewPrototypeGetByteLength = StreamPrimordials.DataViewPrototypeGetByteLength; - var TypedArrayPrototypeGetBuffer = StreamPrimordials.TypedArrayPrototypeGetBuffer; - var TypedArrayPrototypeGetByteOffset = StreamPrimordials.TypedArrayPrototypeGetByteOffset; - var TypedArrayPrototypeGetByteLength = StreamPrimordials.TypedArrayPrototypeGetByteLength; - var StringDecoder = require('string_decoder').StringDecoder; - var nextTick = process.nextTick; - // Captured at module eval, before user code runs (§5) — `pipe` needs to reach - // `process.stdout`/`stderr` at call time, but must not read the `process` global live. - // The properties are read late on purpose: they are lazy getters installed after this - // module loads, so there is nothing to capture here yet. - var processRef = process; - - // --- Node-shaped coded errors (internal/errors, CLAUDE.md §5) ------------------- - - var errors = require('internal/errors'); - var nodeError = errors.nodeError; - var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; - var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; - function errWriteAfterEnd() { - return nodeError(ErrorG, 'ERR_STREAM_WRITE_AFTER_END', 'write after end'); - } - function errDestroyed(what) { - return nodeError( - ErrorG, - 'ERR_STREAM_DESTROYED', - 'Cannot call ' + what + ' after a stream was destroyed', - ); - } - function errNullValues() { - return nodeError(TypeErrorG, 'ERR_STREAM_NULL_VALUES', 'May not write null values to stream'); - } - function errMethodNotImplemented(name) { - return nodeError( - ErrorG, - 'ERR_METHOD_NOT_IMPLEMENTED', - 'The ' + name + ' method is not implemented', - ); - } - function errPrematureClose() { - return nodeError(ErrorG, 'ERR_STREAM_PREMATURE_CLOSE', 'Premature close'); - } - // node's AbortError is its own class (name 'AbortError'); this keeps the plain-Error - // shape the module has always produced — not an ERR_* constructor, so not moved. - function abortError() { - var err = new ErrorG('The operation was aborted'); - err.code = 'ABORT_ERR'; - return err; - } - - // node's chunk contract, verified on 24.18.1: a string, or ANY ArrayBuffer view — - // Buffer, every TypedArray, and DataView. `instanceof Uint8Array` is wrong in both - // directions: it rejects DataView and Int16Array, which node accepts, and it accepts - // `Object.create(Uint8Array.prototype)`, which node rejects. ArrayBuffer.isView is the - // whole test, and it also refuses a bare ArrayBuffer. - var CHUNK_EXPECTED = ['string', 'Buffer', 'TypedArray', 'DataView']; - function isBytesChunk(chunk) { - return ArrayBufferIsView(chunk); - } - - function isDataViewChunk(value) { - // Prototype-chain walk rather than `instanceof` (forgeable through Symbol.hasInstance) - // or a try/catch around a getter (an exception per write on the hot path). - var proto = ObjectGetPrototypeOf(value); - while (proto !== null) { - if (proto === DataViewPrototype) return true; - proto = ObjectGetPrototypeOf(proto); - } - return false; - } - - // THE STEP WHOSE ABSENCE CAUSED #326's REGRESSION. node hands `_write` a Buffer with - // encoding 'buffer' for every view, so widening the accept set without converting left - // `chunk.length` undefined for a DataView: `writableLength` went NaN, `'drain'` could - // never fire again (NaN === 0 is false), and pipe() stalled forever. - // - // The two getter families are NOT interchangeable — a %TypedArray% getter throws on a - // DataView receiver, which is exactly what the in-flight fix hit last time — so the - // brand decides which set to read the window through. - function bytesToBuffer(chunk) { - if (isDataViewChunk(chunk)) { - return Buffer.from( - DataViewPrototypeGetBuffer(chunk), - DataViewPrototypeGetByteOffset(chunk), - DataViewPrototypeGetByteLength(chunk), - ); - } - return Buffer.from( - TypedArrayPrototypeGetBuffer(chunk), - TypedArrayPrototypeGetByteOffset(chunk), - TypedArrayPrototypeGetByteLength(chunk), - ); - } - - // node validates an EXPLICIT encoding before it looks at the chunk, which is why - // `write(5, 'bogus')` reports the encoding and not the chunk type. 'buffer' is node's - // internal marker and is always allowed through. - function validateEncoding(encoding) { - if (encoding !== 'buffer' && !BufferIsEncoding(encoding)) throw ERR_UNKNOWN_ENCODING(encoding); - } - - // --- Stream base --------------------------------------------------------------- - - function Stream(opts) { - EventEmitter.call(this); - } - Stream.prototype = Object.create(EventEmitter.prototype); - Stream.prototype.constructor = Stream; - - // --- Readable ------------------------------------------------------------------ - - function ReadableState(options, objectModeDefault) { - options = options || {}; - this.objectMode = !!options.objectMode || !!objectModeDefault; - this.highWaterMark = - options.highWaterMark !== undefined ? options.highWaterMark : this.objectMode ? 16 : 65536; - this.buffer = []; // chunks (Buffer | string | any in objectMode) - this.length = 0; // bytes (or chunk count in objectMode) - this.flowing = null; // null: undecided, true: 'data' flow, false: paused - this.ended = false; // push(null) seen - this.endEmitted = false; - this.reading = false; // a _read() is in flight - this.sync = false; // inside a synchronous _read call - this.needReadable = false; - this.emittedReadable = false; - this.destroyed = false; - this.errored = null; - this.errorEmitted = false; - this.closeEmitted = false; - this.autoDestroy = options.autoDestroy !== false; - this.emitClose = options.emitClose !== false; - this.defaultEncoding = options.defaultEncoding || 'utf8'; - this.decoder = null; - this.encoding = null; - this.pipes = []; - this.awaitDrainWriters = 0; - } - - function Readable(options) { - if (!(this instanceof Readable)) return new Readable(options); - Stream.call(this); - this._readableState = new ReadableState(options); - if (options) { - if (typeof options.read === 'function') this._read = options.read; - if (typeof options.destroy === 'function') this._destroy = options.destroy; - if (options.encoding) this.setEncoding(options.encoding); - if (options.signal) addAbortSignal(options.signal, this); - } - } - Readable.prototype = Object.create(Stream.prototype); - Readable.prototype.constructor = Readable; - - Object.defineProperty(Readable.prototype, 'readable', { - get: function () { - var s = this._readableState; - return !!s && !s.destroyed && !s.endEmitted; - }, - }); - Object.defineProperty(Readable.prototype, 'destroyed', { - get: function () { - return this._readableState ? this._readableState.destroyed : false; - }, - set: function (v) { - if (this._readableState) this._readableState.destroyed = v; - }, - }); - Object.defineProperty(Readable.prototype, 'readableEnded', { - get: function () { - return this._readableState ? this._readableState.endEmitted : false; - }, - }); - Object.defineProperty(Readable.prototype, 'readableFlowing', { - get: function () { - return this._readableState ? this._readableState.flowing : null; - }, - }); - Object.defineProperty(Readable.prototype, 'readableLength', { - get: function () { - return this._readableState ? this._readableState.length : 0; - }, - }); - Object.defineProperty(Readable.prototype, 'readableHighWaterMark', { - get: function () { - return this._readableState ? this._readableState.highWaterMark : 0; - }, - }); - Object.defineProperty(Readable.prototype, 'readableObjectMode', { - get: function () { - return this._readableState ? this._readableState.objectMode : false; - }, - }); - - Readable.prototype._read = function () { - throw errMethodNotImplemented('_read()'); - }; - - Readable.prototype.push = function (chunk, encoding) { - var s = this._readableState; - if (chunk === null) { - s.reading = false; - onEofChunk(this, s); - return false; - } - if (!s.objectMode) { - if (typeof chunk === 'string') { - encoding = encoding || s.defaultEncoding; - chunk = Buffer.from(chunk, encoding); - } else if (isBytesChunk(chunk)) { - // The readable half must accept exactly what the writable half does, or a - // PassThrough takes a view on one side and errors on the other — which is how the - // first attempt at this shipped. - chunk = bytesToBuffer(chunk); - } else { - // Unlike write(), a bad push is an ASYNC 'error' on node, not a throw. The - // asymmetry is node's; do not "fix" it into a throw. - errorOrDestroy(this, ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk)); - return false; - } - if (s.decoder) chunk = s.decoder.write(chunk); - } - s.reading = false; - var size = s.objectMode ? 1 : chunk.length; - if (s.decoder && !s.objectMode) size = chunk.length; // decoded string length - if (s.flowing && s.length === 0 && !s.sync && this.listenerCount('data') > 0) { - // Fast path: nothing buffered and a flowing consumer — deliver directly. - this.emit('data', chunk); - } else { - s.buffer.push(chunk); - s.length += size; - if (s.needReadable) emitReadable(this); - } - maybeReadMore(this, s); - return s.length < s.highWaterMark; - }; - - Readable.prototype.unshift = function (chunk, encoding) { - var s = this._readableState; - if (chunk === null) { - onEofChunk(this, s); - return false; - } - if (!s.objectMode) { - if (typeof chunk === 'string') chunk = Buffer.from(chunk, encoding || s.defaultEncoding); - if (s.decoder) chunk = s.decoder.write(chunk); - } - s.buffer.unshift(chunk); - s.length += s.objectMode ? 1 : chunk.length; - if (s.needReadable) emitReadable(this); - return s.length < s.highWaterMark; - }; - - function onEofChunk(stream, s) { - if (s.ended) return; - if (s.decoder) { - var tail = s.decoder.end(); - if (tail && tail.length) { - s.buffer.push(tail); - s.length += tail.length; - } - } - s.ended = true; - if (s.length === 0) { - emitReadable(stream); // a final zero-data 'readable' announces EOF - endReadableMaybe(stream); - } else if (s.needReadable) { - emitReadable(stream); - } - } - - function emitReadable(stream) { - var s = stream._readableState; - s.needReadable = false; - if (s.emittedReadable) return; - s.emittedReadable = true; - nextTick(function () { - s.emittedReadable = false; - if (s.destroyed) return; - if (s.length || s.ended) stream.emit('readable'); - flow(stream); - }); - } - - function maybeReadMore(stream, s) { - if (!s.reading && !s.ended && s.length < s.highWaterMark && s.flowing !== false) { - // Keep the pump primed while below HWM (mirrors Node's read(0) loop). - nextTick(function () { - if (!s.reading && !s.ended && !s.destroyed && s.length < s.highWaterMark) { - stream.read(0); - } - }); - } - } - - Readable.prototype.read = function (n) { - var s = this._readableState; - if (s.destroyed) return null; - if (n === undefined || n === null || isNaN(n)) n = -1; // "all" - - if (n === 0 && s.ended && s.length === 0) { - endReadableMaybe(this); - return null; - } - - // Trigger a _read when the buffer can absorb more (or is empty). - var doRead = false; - if (!s.ended && !s.reading && (s.length === 0 || s.length < s.highWaterMark)) doRead = true; - if (doRead) { - s.reading = true; - s.sync = true; - try { - this._read(s.highWaterMark); - } catch (err) { - errorOrDestroy(this, err); - } - s.sync = false; - } - - var ret = null; - if (n === 0) { - s.needReadable = s.length === 0 && !s.ended; - return null; - } - if (s.objectMode) { - ret = s.length > 0 ? s.buffer.shift() : null; - if (ret !== null) s.length -= 1; - } else if (n < 0) { - if (s.length > 0) { - if (s.flowing || s.buffer.length === 1) { - // Flowing mode delivers chunk-at-a-time (Node's howMuchToRead returns - // the first chunk's length when flowing), so 'data' events preserve - // the producer's chunk boundaries. - ret = s.buffer.shift(); - s.length -= ret.length; - } else if (typeof s.buffer[0] === 'string') { - ret = s.buffer.join(''); - s.buffer.length = 0; - s.length = 0; - } else { - ret = Buffer.concat(s.buffer); - s.buffer.length = 0; - s.length = 0; - } - } - } else { - if (s.length >= n) { - ret = takeBytes(s, n); - } else if (s.ended && s.length > 0) { - ret = takeBytes(s, s.length); - } - } - - if (ret === null) { - s.needReadable = true; - } - if (s.ended && s.length === 0) endReadableMaybe(this); - // Node emits 'data' from read() itself — explicit reads and the flow loop - // share one delivery point. - if (ret !== null) this.emit('data', ret); - return ret; - }; - - function takeBytes(s, n) { - // Byte-accurate extraction across buffered chunks (strings when a decoder is - // set: n then counts code units, matching Node's decoded reads). - var first = s.buffer[0]; - if (typeof first === 'string') { - if (first.length <= n) { - s.buffer.shift(); - s.length -= first.length; - return first; - } - s.buffer[0] = first.slice(n); - s.length -= n; - return first.slice(0, n); - } - if (first.length === n) { - s.buffer.shift(); - s.length -= n; - return first; - } - if (first.length > n) { - s.buffer[0] = first.subarray(n); - s.length -= n; - return first.subarray(0, n); - } - var out = Buffer.allocUnsafe(n); - var off = 0; - while (off < n && s.buffer.length) { - var c = s.buffer[0]; - var want = n - off; - if (c.length <= want) { - out.set(c, off); - off += c.length; - s.buffer.shift(); - } else { - out.set(c.subarray(0, want), off); - s.buffer[0] = c.subarray(want); - off += want; - } - } - s.length -= n; - return out; - } - - function endReadableMaybe(stream) { - var s = stream._readableState; - if (s.endEmitted || !s.ended || s.length > 0) return; - s.endEmitted = true; - nextTick(function () { - if (!s.errored) stream.emit('end'); - if (s.autoDestroy) { - // End of a Duplex only auto-destroys once the write side finished too. - var w = stream._writableState; - if (!w || (w.finished && w.autoDestroy) || !w) stream.destroy(); - } - }); - } - - Readable.prototype.setEncoding = function (enc) { - var s = this._readableState; - var decoder = new StringDecoder(enc); - s.decoder = decoder; - s.encoding = enc; - // Re-decode anything already buffered into one string chunk. - if (s.buffer.length) { - var content = ''; - for (var i = 0; i < s.buffer.length; i++) content += decoder.write(s.buffer[i]); - s.buffer.length = 0; - if (content !== '') s.buffer.push(content); - s.length = content.length; - } - return this; - }; - - Readable.prototype.pause = function () { - var s = this._readableState; - if (s.flowing !== false) { - s.flowing = false; - this.emit('pause'); - } - return this; - }; - - Readable.prototype.resume = function () { - var s = this._readableState; - if (!s.flowing) { - s.flowing = true; - var self = this; - nextTick(function () { - self.emit('resume'); - flow(self); - }); - } - return this; - }; - - Readable.prototype.isPaused = function () { - return this._readableState.flowing === false; - }; - - function flow(stream) { - // read() emits 'data' itself; the loop just drains until pause or empty. - var s = stream._readableState; - while (s.flowing && stream.read() !== null) {} - } - - Readable.prototype.on = function (ev, fn) { - var res = Stream.prototype.on.call(this, ev, fn); - var s = this._readableState; - if (ev === 'data') { - if (s.flowing !== false) this.resume(); - } else if (ev === 'readable') { - if (!s.endEmitted) { - s.needReadable = true; - if (s.length || s.ended) emitReadable(this); - else if (!s.reading) { - var self = this; - nextTick(function () { - if (!s.destroyed && !s.ended && !s.reading) self.read(0); - }); - } - } - } - return res; - }; - Readable.prototype.addListener = Readable.prototype.on; - - Readable.prototype.pipe = function (dest, pipeOpts) { - var src = this; - var s = this._readableState; - s.pipes.push(dest); - // node excludes the stdio singletons from the automatic `dest.end()`, and the - // omission is not cosmetic: `src.pipe(process.stdout)` is the most common pipe in - // Node, and ending stdout ends it for the whole process — every later write is - // dropped and raises 'write after end'. - // - // Identity, exactly as node does it (`dest !== process.stdout && dest !== - // process.stderr`), rather than testing a `_isStdio` property: an absent-property - // read walks Object.prototype, so `Object.prototype._isStdio = true` would stop - // pipe() from ending ANY destination — §5's uncounted-vector case. Reading the getter - // builds the stdout stream on first pipe even when piping elsewhere; node pays the - // same cost, it is once per process, and the getter caches. - var doEnd = - (!pipeOpts || pipeOpts.end !== false) && - dest !== processRef.stdout && - dest !== processRef.stderr; - - function ondata(chunk) { - var ret = dest.write(chunk); - if (ret === false) { - s.awaitDrainWriters++; - src.pause(); - } - } - function ondrain() { - if (s.awaitDrainWriters > 0) s.awaitDrainWriters--; - if (s.awaitDrainWriters === 0) src.resume(); - } - function onend() { - if (doEnd) dest.end(); - } - function onerror(er) { - unpipeAll(); - if (dest.listenerCount('error') === 0) errorOrDestroy(dest, er); - } - function onclose() { - unpipeAll(); - } - function unpipeAll() { - src.removeListener('data', ondata); - dest.removeListener('drain', ondrain); - src.removeListener('end', onend); - src.removeListener('error', onerror); - dest.removeListener('close', onclose); - var idx = s.pipes.indexOf(dest); - if (idx !== -1) s.pipes.splice(idx, 1); - dest.emit('unpipe', src); - } - - src.on('data', ondata); - dest.on('drain', ondrain); - src.on('end', onend); - src.on('error', onerror); - dest.on('close', onclose); - dest.on('unpipe-request', unpipeAll); - dest.emit('pipe', src); - return dest; - }; - - Readable.prototype.unpipe = function (dest) { - var s = this._readableState; - if (dest === undefined) { - var all = s.pipes.slice(); - for (var i = 0; i < all.length; i++) all[i].emit('unpipe-request'); - return this; - } - if (s.pipes.indexOf(dest) !== -1) dest.emit('unpipe-request'); - return this; - }; - - Readable.prototype.destroy = function (err, cb) { - return destroyImpl(this, err, cb); - }; - Readable.prototype._destroy = function (err, cb) { - cb(err); - }; - - Readable.prototype[Symbol.asyncIterator] = function () { - var stream = this; - var s = this._readableState; - return { - next: function () { - return new Promise(function (resolve, reject) { - if (s.errored) return reject(s.errored); - var chunk = stream.read(); - if (chunk !== null) return resolve({ value: chunk, done: false }); - if (s.endEmitted || (s.ended && s.length === 0)) { - endReadableMaybe(stream); - return resolve({ value: undefined, done: true }); - } - function cleanup() { - stream.removeListener('readable', onreadable); - stream.removeListener('end', onend); - stream.removeListener('error', onerror); - stream.removeListener('close', onclose); - } - function onreadable() { - cleanup(); - var c = stream.read(); - if (c !== null) resolve({ value: c, done: false }); - else if (s.ended) resolve({ value: undefined, done: true }); - else attach(); - } - function onend() { - cleanup(); - resolve({ value: undefined, done: true }); - } - function onerror(er) { - cleanup(); - reject(er); - } - function onclose() { - cleanup(); - resolve({ value: undefined, done: true }); - } - function attach() { - stream.once('readable', onreadable); - stream.once('end', onend); - stream.once('error', onerror); - stream.once('close', onclose); - } - attach(); - }); - }, - return: function () { - stream.destroy(); - return Promise.resolve({ value: undefined, done: true }); - }, - [Symbol.asyncIterator]: function () { - return this; - }, - }; - }; - - Readable.from = function (iterable, options) { - var readable = new Readable( - Object.assign({ objectMode: true }, options, { - read: function () { - pump(); - }, - }), - ); - var iterator = null; - var pumping = false; - function getIterator() { - if (iterator) return iterator; - if (iterable && typeof iterable[Symbol.asyncIterator] === 'function') { - iterator = iterable[Symbol.asyncIterator](); - } else if (iterable && typeof iterable[Symbol.iterator] === 'function') { - iterator = iterable[Symbol.iterator](); - } else { - throw ERR_INVALID_ARG_TYPE('iterable', 'Iterable', iterable); - } - return iterator; - } - function pump() { - if (pumping) return; - pumping = true; - Promise.resolve() - .then(function step() { - return Promise.resolve(getIterator().next()).then(function (res) { - if (res.done) { - readable.push(null); - pumping = false; - return; - } - if (readable.destroyed) { - pumping = false; - return; - } - if (readable.push(res.value)) return step(); - pumping = false; - }); - }) - .catch(function (err) { - pumping = false; - errorOrDestroy(readable, err); - }); - } - return readable; - }; - - // --- Writable ------------------------------------------------------------------ - - function WritableState(options, objectModeDefault) { - options = options || {}; - this.objectMode = !!options.objectMode || !!objectModeDefault; - this.highWaterMark = - options.highWaterMark !== undefined ? options.highWaterMark : this.objectMode ? 16 : 65536; - this.decodeStrings = options.decodeStrings !== false; - this.defaultEncoding = options.defaultEncoding || 'utf8'; - this.buffered = []; // {chunk, encoding, cb} - this.length = 0; - this.writing = false; - this.corked = 0; - this.sync = true; // inside the synchronous span of a _write call - this.needDrain = false; - this.ending = false; - this.ended = false; - this.finished = false; - this.prefinished = false; - this.destroyed = false; - this.errored = null; - this.errorEmitted = false; - this.closeEmitted = false; - this.autoDestroy = options.autoDestroy !== false; - this.emitClose = options.emitClose !== false; - this.writecb = null; - this.writelen = 0; - } - - function Writable(options) { - if (!(this instanceof Writable)) return new Writable(options); - Stream.call(this); - this._writableState = new WritableState(options); - if (options) { - if (typeof options.write === 'function') this._write = options.write; - if (typeof options.writev === 'function') this._writev = options.writev; - if (typeof options.final === 'function') this._final = options.final; - if (typeof options.destroy === 'function') this._destroy = options.destroy; - if (options.signal) addAbortSignal(options.signal, this); - } - } - Writable.prototype = Object.create(Stream.prototype); - Writable.prototype.constructor = Writable; - - Object.defineProperty(Writable.prototype, 'writable', { - get: function () { - var w = this._writableState; - return !!w && !w.destroyed && !w.ending; - }, - }); - Object.defineProperty(Writable.prototype, 'destroyed', { - get: function () { - return this._writableState ? this._writableState.destroyed : false; - }, - set: function (v) { - if (this._writableState) this._writableState.destroyed = v; - }, - }); - Object.defineProperty(Writable.prototype, 'writableEnded', { - get: function () { - return this._writableState ? this._writableState.ending : false; - }, - }); - Object.defineProperty(Writable.prototype, 'writableFinished', { - get: function () { - return this._writableState ? this._writableState.finished : false; - }, - }); - Object.defineProperty(Writable.prototype, 'writableLength', { - get: function () { - return this._writableState ? this._writableState.length : 0; - }, - }); - Object.defineProperty(Writable.prototype, 'writableHighWaterMark', { - get: function () { - return this._writableState ? this._writableState.highWaterMark : 0; - }, - }); - Object.defineProperty(Writable.prototype, 'writableObjectMode', { - get: function () { - return this._writableState ? this._writableState.objectMode : false; - }, - }); - Object.defineProperty(Writable.prototype, 'writableNeedDrain', { - get: function () { - var w = this._writableState; - return !!w && !w.destroyed && !w.ending && w.needDrain; - }, - }); - - Writable.prototype._write = function () { - throw errMethodNotImplemented('_write()'); - }; - - Writable.prototype.write = function (chunk, encoding, cb) { - if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - var w = this._writableState; - // An explicit encoding is validated FIRST — before null, before the chunk type. That - // ordering is node's and it is observable: `write(5, 'bogus')` reports the encoding. - if (encoding) validateEncoding(encoding); - encoding = encoding || w.defaultEncoding; - if (typeof cb !== 'function') cb = noop; - - // node THROWS both of these synchronously rather than reporting them on the stream. - // Routing them through writeErrorNextTick delivered the right code on the wrong turn, - // so `assert.throws(() => s.write(null))` passed under node and failed here. - if (chunk === null) throw errNullValues(); - if (!w.objectMode) { - if (typeof chunk === 'string') { - if (w.decodeStrings) { - chunk = Buffer.from(chunk, encoding); - encoding = 'buffer'; - } - } else if (isBytesChunk(chunk)) { - // Normalize every view to a Buffer, as node does, so downstream length accounting - // and `_write` see the same shape they always have. - chunk = bytesToBuffer(chunk); - encoding = 'buffer'; - } else { - throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk); - } - } - if (w.ending) { - var endErr = errWriteAfterEnd(); - writeErrorNextTick(this, endErr, cb); - return false; - } - if (w.destroyed) { - var dErr = errDestroyed('write'); - writeErrorNextTick(this, dErr, cb); - return false; - } - - var len = w.objectMode ? 1 : chunk.length; - w.length += len; - - if (w.writing || w.corked > 0 || w.errored) { - w.buffered.push({ chunk: chunk, encoding: encoding, cb: cb }); - } else { - doWrite(this, w, chunk, len, encoding, cb); - } - - var ret = w.length < w.highWaterMark && !w.errored; - if (!ret) w.needDrain = true; - return ret; - }; - - function writeErrorNextTick(stream, err, cb) { - nextTick(function () { - cb(err); - errorOrDestroy(stream, err); - }); - } - - function doWrite(stream, w, chunk, len, encoding, cb) { - w.writing = true; - w.writecb = cb; - w.writelen = len; - w.sync = true; - try { - stream._write(chunk, encoding, function (err) { - onwriteDone(stream, w, err); - }); - } catch (err) { - onwriteDone(stream, w, err); - } - w.sync = false; - } - - function onwriteDone(stream, w, err) { - var cb = w.writecb || noop; - var len = w.writelen; - w.writing = false; - w.writecb = null; - w.writelen = 0; - w.length -= len; - - if (err) { - if (w.sync) { - nextTick(function () { - cb(err); - errorOrDestroy(stream, err); - }); - } else { - cb(err); - errorOrDestroy(stream, err); - } - return; - } - - if (w.sync) { - // The user cb (and everything that may cascade from it) runs on a clean - // stack, matching Node's afterWrite deferral for synchronous _write. - nextTick(function () { - afterWrite(stream, w, cb); - }); - } else { - afterWrite(stream, w, cb); - } - } - - function afterWrite(stream, w, cb) { - cb(); - if (!w.writing && w.corked === 0 && w.buffered.length > 0 && !w.destroyed) { - var entry = w.buffered.shift(); - doWrite( - stream, - w, - entry.chunk, - w.objectMode ? 1 : entry.chunk.length, - entry.encoding, - entry.cb, - ); - return; // drain/finish decisions re-run after the queue empties - } - if (w.needDrain && w.length === 0 && !w.ending && !w.destroyed) { - w.needDrain = false; - stream.emit('drain'); - } - finishMaybe(stream, w); - } - - Writable.prototype.cork = function () { - this._writableState.corked++; - }; - Writable.prototype.uncork = function () { - var w = this._writableState; - if (w.corked > 0) { - w.corked--; - if (w.corked === 0 && !w.writing && w.buffered.length > 0) { - var entry = w.buffered.shift(); - doWrite( - this, - w, - entry.chunk, - w.objectMode ? 1 : entry.chunk.length, - entry.encoding, - entry.cb, - ); - } - } - }; - - Writable.prototype.setDefaultEncoding = function (encoding) { - validateEncoding(encoding); - this._writableState.defaultEncoding = encoding; - return this; - }; - - Writable.prototype.end = function (chunk, encoding, cb) { - if (typeof chunk === 'function') { - cb = chunk; - chunk = null; - encoding = null; - } else if (typeof encoding === 'function') { - cb = encoding; - encoding = null; - } - var w = this._writableState; - if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); - if (w.ending || w.destroyed) { - if (typeof cb === 'function') { - var err = w.finished - ? nodeError( - ErrorG, - 'ERR_STREAM_ALREADY_FINISHED', - 'Cannot call end after a stream was finished', - ) - : w.destroyed - ? errDestroyed('end') - : null; - if (err) nextTick(cb, err); - else if (w.finished) nextTick(cb); - else this.once('finish', cb); - } - return this; - } - w.ending = true; - if (typeof cb === 'function') this.once('finish', cb); - finishMaybe(this, w); - return this; - }; - - function finishMaybe(stream, w) { - if (!w.ending || w.finished || w.writing || w.buffered.length > 0 || w.errored || w.destroyed) - return; - if (!w.prefinished) { - w.prefinished = true; - stream.emit('prefinish'); - if (typeof stream._final === 'function') { - try { - stream._final(function (err) { - if (err) { - errorOrDestroy(stream, err); - return; - } - emitFinish(stream, w); - }); - } catch (err) { - errorOrDestroy(stream, err); - } - return; - } - } - emitFinish(stream, w); - } - - function emitFinish(stream, w) { - if (w.finished) return; - w.finished = true; - w.ended = true; - nextTick(function () { - stream.emit('finish'); - if (w.autoDestroy) { - // A Duplex only auto-destroys once the read side ended too. - var r = stream._readableState; - if (!r || r.endEmitted || !r.ended) { - if (!r || r.endEmitted) stream.destroy(); - } - } - }); - } - - Writable.prototype.destroy = function (err, cb) { - return destroyImpl(this, err, cb); - }; - Writable.prototype._destroy = function (err, cb) { - cb(err); - }; - - // --- shared destroy / error ---------------------------------------------------- - - // `_undestroy` takes a teardown back: the stream has already emitted its 'finish' / - // 'close' and run its callbacks, and this returns it to a usable state. Node exposes it - // on both prototypes (internal/streams/destroy.js's `undestroy`; verified present on - // node 26.13.0's Writable, Readable AND process.stdout, so it is reachable API, not an - // invention here). It has exactly one caller shape — a `_destroy` that wants the - // teardown undone — which is how node keeps the stdio singletons alive across an - // `end()`; see js/internal/stdio.js. Nothing in this file calls it, so an existing - // stream is unaffected by its presence. - // - // The reset list mirrors node's: enough to make the stream writable/readable again and - // to let a later 'close' emit, without touching buffers, encodings or the high-water - // mark, which the stream keeps across the round trip. - function undestroy() { - var r = this._readableState; - var w = this._writableState; - if (r) { - r.destroyed = false; - r.closeEmitted = false; - r.errored = null; - r.errorEmitted = false; - r.reading = false; - r.ended = false; - r.endEmitted = false; - } - if (w) { - w.destroyed = false; - w.closeEmitted = false; - w.errored = null; - w.errorEmitted = false; - w.prefinished = false; - w.ended = false; - w.ending = false; - w.finished = false; - } - } - Readable.prototype._undestroy = undestroy; - Writable.prototype._undestroy = undestroy; - - function noop() {} - - function errorOrDestroy(stream, err) { - var r = stream._readableState; - var w = stream._writableState; - if ((r && r.destroyed) || (w && w.destroyed)) { - // Already torn down (e.g. write-after-end on an auto-destroyed stream): - // the error must still surface exactly once. - if ((r && r.errorEmitted) || (w && w.errorEmitted)) return; - if (r) r.errorEmitted = true; - if (w) w.errorEmitted = true; - nextTick(function () { - stream.emit('error', err); - }); - return; - } - stream.destroy(err); - } - - function destroyImpl(stream, err, cb) { - var r = stream._readableState; - var w = stream._writableState; - if ((r && r.destroyed) || (w && w.destroyed)) { - if (typeof cb === 'function') nextTick(cb, err || null); - return stream; - } - if (r) { - r.destroyed = true; - if (err) r.errored = err; - } - if (w) { - w.destroyed = true; - if (err) w.errored = err; - } - stream._destroy(err || null, function (er) { - er = er || err || null; - if (typeof cb === 'function') nextTick(cb, er); - nextTick(function () { - var emitClose = (!r || r.emitClose) && (!w || w.emitClose); - var alreadyEmitted = (r && r.errorEmitted) || (w && w.errorEmitted); - if (er && !alreadyEmitted && (!cb || typeof cb !== 'function')) { - if (r) r.errorEmitted = true; - if (w) w.errorEmitted = true; - stream.emit('error', er); - } - if (emitClose) { - var closed = (r && r.closeEmitted) || (w && w.closeEmitted); - if (!closed) { - if (r) r.closeEmitted = true; - if (w) w.closeEmitted = true; - stream.emit('close'); - } - } - }); - }); - return stream; - } - - function addAbortSignal(signal, stream) { - if (signal.aborted) { - nextTick(function () { - stream.destroy(abortError()); - }); - return; - } - signal.addEventListener('abort', function () { - stream.destroy(abortError()); - }); - } - - // --- Duplex / Transform / PassThrough ------------------------------------------- - - function Duplex(options) { - if (!(this instanceof Duplex)) return new Duplex(options); - Stream.call(this); - this._readableState = new ReadableState(options, options && options.readableObjectMode); - this._writableState = new WritableState(options, options && options.writableObjectMode); - if (options) { - if (typeof options.read === 'function') this._read = options.read; - if (typeof options.write === 'function') this._write = options.write; - if (typeof options.writev === 'function') this._writev = options.writev; - if (typeof options.final === 'function') this._final = options.final; - if (typeof options.destroy === 'function') this._destroy = options.destroy; - if (options.encoding) this.setEncoding(options.encoding); - if (options.readable === false) { - this._readableState.ended = true; - this._readableState.endEmitted = true; - } - if (options.writable === false) { - this._writableState.ending = true; - this._writableState.ended = true; - this._writableState.finished = true; - } - if (options.signal) addAbortSignal(options.signal, this); - } - } - Duplex.prototype = Object.create(Readable.prototype); - Duplex.prototype.constructor = Duplex; - // Mix in the Writable API (own-property methods + accessors). - (function mixinWritable() { - var keys = Object.getOwnPropertyNames(Writable.prototype); - for (var i = 0; i < keys.length; i++) { - var k = keys[i]; - if (k === 'constructor') continue; - if (!Object.prototype.hasOwnProperty.call(Duplex.prototype, k)) { - Object.defineProperty( - Duplex.prototype, - k, - Object.getOwnPropertyDescriptor(Writable.prototype, k), - ); - } - } - // destroy must tear down BOTH sides; Readable's prototype copy already - // delegates to the shared destroyImpl, which handles both states. - })(); - - function Transform(options) { - if (!(this instanceof Transform)) return new Transform(options); - Duplex.call(this, options); - this._transformCallback = null; - if (options && typeof options.transform === 'function') this._transform = options.transform; - if (options && typeof options.flush === 'function') this._flush = options.flush; - } - Transform.prototype = Object.create(Duplex.prototype); - Transform.prototype.constructor = Transform; - - Transform.prototype._transform = function () { - throw errMethodNotImplemented('_transform()'); - }; - - Transform.prototype._write = function (chunk, encoding, cb) { - var stream = this; - var r = this._readableState; - this._transform(chunk, encoding, function (err, data) { - if (err) return cb(err); - if (data !== null && data !== undefined) stream.push(data); - // Read-side backpressure: hold the write pipeline until the consumer - // drains the readable buffer below HWM (Node's Transform contract). - if (r.length < r.highWaterMark || r.destroyed) cb(); - else stream._transformCallback = cb; - }); - }; - - Transform.prototype._read = function () { - var cb = this._transformCallback; - if (cb) { - this._transformCallback = null; - cb(); - } - }; - - Transform.prototype._final = function (cb) { - var stream = this; - if (typeof this._flush === 'function') { - this._flush(function (err, data) { - if (err) return cb(err); - if (data !== null && data !== undefined) stream.push(data); - stream.push(null); - cb(); - }); - } else { - stream.push(null); - cb(); - } - }; - - function PassThrough(options) { - if (!(this instanceof PassThrough)) return new PassThrough(options); - Transform.call(this, options); - } - PassThrough.prototype = Object.create(Transform.prototype); - PassThrough.prototype.constructor = PassThrough; - PassThrough.prototype._transform = function (chunk, encoding, cb) { - cb(null, chunk); - }; - - // --- finished / pipeline --------------------------------------------------------- - - function isStreamLike(s) { - return s !== null && typeof s === 'object' && typeof s.on === 'function'; - } - - function finished(stream, opts, cb) { - if (typeof opts === 'function') { - cb = opts; - opts = {}; - } - opts = opts || {}; - if (!isStreamLike(stream)) - throw ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream); - - var readable = - opts.readable !== false && typeof stream.readable === 'boolean' ? stream.readable : false; - var writable = - opts.writable !== false && typeof stream.writable === 'boolean' ? stream.writable : false; - // Streams that predate the state objects (or foreign ones): fall back to - // listening for everything and settling on the first terminal event. - var r = stream._readableState; - var w = stream._writableState; - var readableEnded = !readable || (r && r.endEmitted); - var writableFinished = !writable || (w && w.finished); - var called = false; - - function done(err) { - if (called) return; - called = true; - cleanup(); - cb.call(stream, err || null); - } - function onend() { - readableEnded = true; - if (writableFinished) done(); - } - function onfinish() { - writableFinished = true; - if (readableEnded) done(); - } - function onerror(err) { - done(err); - } - function onclose() { - if (readable && !readableEnded && !(r && r.endEmitted)) return done(errPrematureClose()); - if (writable && !writableFinished && !(w && w.finished)) return done(errPrematureClose()); - done(); - } - function cleanup() { - stream.removeListener('end', onend); - stream.removeListener('finish', onfinish); - stream.removeListener('error', onerror); - stream.removeListener('close', onclose); - } - if (readableEnded && writableFinished) { - nextTick(done); - } else { - stream.on('end', onend); - stream.on('finish', onfinish); - stream.on('error', onerror); - stream.on('close', onclose); - } - return cleanup; - } - - // node's list for a pipeline stage. Lava accepts only the Stream arm so far (functions, - // iterables and web streams are not piped yet), but the error names what node accepts. - var PIPELINE_BODY = [ - 'function', - 'Blob', - 'ReadableStream', - 'WritableStream', - 'Stream', - 'Iterable', - 'AsyncIterable', - 'Promise', - '{ readable, writable } pair', - ]; - - function pipeline() { - var args = []; - for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; - var cb = args.pop(); - if (typeof cb !== 'function') - throw ERR_INVALID_ARG_TYPE('streams[stream.length - 1]', 'function', cb); - if (Array.isArray(args[0]) && args.length === 1) args = args[0]; - if (args.length < 2) { - throw errors.ERR_MISSING_ARGS('streams'); - } - - var streams = args; - var called = false; - function done(err) { - if (called) return; - called = true; - // On failure every stream in the chain is torn down (Node parity). - if (err) { - for (var j = 0; j < streams.length; j++) { - if (typeof streams[j].destroy === 'function') streams[j].destroy(); - } - } - cb(err || null); - } - - for (var k = 0; k < streams.length; k++) { - if (!isStreamLike(streams[k])) throw ERR_INVALID_ARG_TYPE('body', PIPELINE_BODY, streams[k]); - streams[k].on('error', done); - if (k > 0) streams[k - 1].pipe(streams[k]); - } - finished(streams[streams.length - 1], function (err) { - done(err); - }); - return streams[streams.length - 1]; - } - - // --- Web Streams bridges --------------------------------------------------------- - - Readable.fromWeb = function (webStream, options) { - var reader = webStream.getReader(); - var readable = new Readable( - Object.assign({}, options, { - read: function () { - var self = this; - reader.read().then( - function (res) { - if (res.done) self.push(null); - else self.push(res.value); - }, - function (err) { - errorOrDestroy(self, err); - }, - ); - }, - destroy: function (err, cb) { - reader.cancel(err).then( - function () { - cb(err); - }, - function () { - cb(err); - }, - ); - }, - }), - ); - return readable; - }; - - Readable.toWeb = function (nodeReadable) { - var ReadableStreamCtor = require('stream/web').ReadableStream; - return new ReadableStreamCtor({ - start: function (controller) { - nodeReadable.on('data', function (chunk) { - controller.enqueue(chunk); - if (controller.desiredSize !== null && controller.desiredSize <= 0) nodeReadable.pause(); - }); - nodeReadable.on('end', function () { - try { - controller.close(); - } catch (e) { - /* already closed */ - } - }); - nodeReadable.on('error', function (err) { - controller.error(err); - }); - }, - pull: function () { - nodeReadable.resume(); - }, - cancel: function (reason) { - nodeReadable.destroy(reason instanceof Error ? reason : undefined); - }, - }); - }; - - Writable.fromWeb = function (webStream, options) { - var writer = webStream.getWriter(); - return new Writable( - Object.assign({}, options, { - write: function (chunk, encoding, cb) { - writer.write(chunk).then( - function () { - cb(); - }, - function (err) { - cb(err); - }, - ); - }, - final: function (cb) { - writer.close().then( - function () { - cb(); - }, - function (err) { - cb(err); - }, - ); - }, - destroy: function (err, cb) { - writer.abort(err).then( - function () { - cb(err); - }, - function () { - cb(err); - }, - ); - }, - }), - ); - }; - - Writable.toWeb = function (nodeWritable) { - var WritableStreamCtor = require('stream/web').WritableStream; - return new WritableStreamCtor({ - write: function (chunk) { - return new Promise(function (resolve, reject) { - var ok = nodeWritable.write(chunk, function (err) { - if (err) reject(err); - }); - if (ok) resolve(); - else nodeWritable.once('drain', resolve); - }); - }, - close: function () { - return new Promise(function (resolve, reject) { - nodeWritable.end(function () { - resolve(); - }); - nodeWritable.once('error', reject); - }); - }, - abort: function (reason) { - nodeWritable.destroy(reason instanceof Error ? reason : undefined); - }, - }); - }; + var Stream = require('internal/streams/legacy').Stream; + var Readable = require('internal/streams/readable').Readable; + var Writable = require('internal/streams/writable').Writable; + var Duplex = require('internal/streams/duplex'); + var Transform = require('internal/streams/transform'); + var PassThrough = require('internal/streams/passthrough'); + var finished = require('internal/streams/end-of-stream'); + var pipeline = require('internal/streams/pipeline').pipeline; + var addAbortSignal = require('internal/streams/add-abort-signal').addAbortSignal; // --- exports --------------------------------------------------------------------- diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 34664ac..876823c 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -511,7 +511,7 @@ { "name": "61-stdio-lifecycle dies when pipe() stops excluding the stdio singletons", "why": "Without node's `dest !== process.stdout/stderr` exclusion, `src.pipe(process.stdout)` calls end() on a process-lifetime singleton. Note what the assertion keys off: since `_destroy` undoes the teardown, the stream RECOVERS a tick later, so `writable` reads true either way and only the spurious 'finish' event proves end() was reached. Before that half existed this dropped every later write and raised 'write after end'.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/readable.js", "find": " var doEnd =\n (!pipeOpts || pipeOpts.end !== false) &&\n dest !== processRef.stdout &&\n dest !== processRef.stderr;", "replace": " var doEnd = !pipeOpts || pipeOpts.end !== false;", "gate": "compat:tests/node-compat/cases/61-stdio-lifecycle.js", @@ -522,7 +522,7 @@ { "name": "stdio-lifecycle dies when pipe() stops excluding the stdio singletons (pipe shape)", "why": "Same defect as the compat entry, gated on the node:test so both fd shapes are covered — node's stdout is a different class on a pipe. The script counts 'finish' events rather than comparing bytes, because the recovery makes the bytes identical with the exclusion deleted; a byte-only version of this test SURVIVED this mutation.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/readable.js", "find": " var doEnd =\n (!pipeOpts || pipeOpts.end !== false) &&\n dest !== processRef.stdout &&\n dest !== processRef.stderr;", "replace": " var doEnd = !pipeOpts || pipeOpts.end !== false;", "gate": "node-test:tests/stdio/stdio-lifecycle.test.mjs", @@ -548,7 +548,7 @@ { "name": "stdio-lifecycle dies when _undestroy stops clearing the ending flag", "why": "The `writable` getter is `!destroyed && !ending`, so leaving `ending` set makes the stream report unwritable forever even though destroyed was cleared — the recovery looks applied and is not. Pins the individual field, not just the presence of the call.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/destroy.js", "find": " w.ended = false;\n w.ending = false;\n w.finished = false;", "replace": " w.ended = false;\n w.finished = false;", "gate": "node-test:tests/stdio/stdio-lifecycle.test.mjs", @@ -828,7 +828,7 @@ { "name": "64-stream-write dies when write() reports errors async instead of throwing", "why": "node throws ERR_STREAM_NULL_VALUES and ERR_INVALID_ARG_TYPE synchronously from write(). Routing them through writeErrorNextTick delivers the right code on the wrong turn, so `assert.throws(() => s.write(null))` passes under node and fails here.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/writable.js", "find": " if (chunk === null) throw errNullValues();", "replace": " if (chunk === null) { writeErrorNextTick(this, errNullValues(), cb); return false; }", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -839,7 +839,7 @@ { "name": "64-stream-write dies when the accept set goes back to instanceof Uint8Array", "why": "`instanceof Uint8Array` is wrong in BOTH directions: it rejects DataView and Int16Array, which node accepts, and it accepts Object.create(Uint8Array.prototype), which node rejects. ArrayBuffer.isView is the whole test.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/utils.js", "find": " function isBytesChunk(chunk) {\n return ArrayBufferIsView(chunk);\n }", "replace": " function isBytesChunk(chunk) {\n return chunk instanceof Uint8Array;\n }", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -851,7 +851,7 @@ { "name": "64-stream-write dies when a view is not normalized to a Buffer", "why": "THE regression that got #326's attempt reverted. node hands _write a Buffer with encoding 'buffer' for every view; without the conversion `chunk.length` is undefined for a DataView, writableLength goes NaN, and 'drain' can never fire again because NaN === 0 is false.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/writable.js", "find": " chunk = bytesToBuffer(chunk);\n encoding = 'buffer';\n } else {\n throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk);", "replace": " encoding = 'buffer';\n } else {\n throw ERR_INVALID_ARG_TYPE('chunk', CHUNK_EXPECTED, chunk);", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -862,7 +862,7 @@ { "name": "64-stream-write dies when Readable.push does not accept what write does", "why": "Widening only the writable half makes a PassThrough take a view on one side and error on the other — which is how the first attempt shipped. The replacement KEEPS the normalization so the entry pins the brand widening alone — changing both at once would leave a kill ambiguous about which half the case detects.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/readable.js", "find": " } else if (isBytesChunk(chunk)) {\n // The readable half must accept exactly what the writable half does, or a\n // PassThrough takes a view on one side and errors on the other — which is how the\n // first attempt at this shipped.\n chunk = bytesToBuffer(chunk);", "replace": " } else if (chunk instanceof Uint8Array) {\n chunk = bytesToBuffer(chunk);", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -873,7 +873,7 @@ { "name": "64-stream-write dies when the encoding is not validated before the chunk type", "why": "node validates an explicit encoding first, which is observable: write(5, 'bogus') reports ERR_UNKNOWN_ENCODING, not the chunk type.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/writable.js", "find": " if (encoding) validateEncoding(encoding);\n", "replace": "", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -884,7 +884,7 @@ { "name": "64-stream-write dies when setDefaultEncoding accepts an unknown name", "why": "node throws ERR_UNKNOWN_ENCODING; accepting it silently poisons every later string write on the stream.", - "source": "pkg/runtime/js/node/stream.js", + "source": "pkg/runtime/js/internal/streams/writable.js", "find": " validateEncoding(encoding);\n this._writableState.defaultEncoding = encoding;", "replace": " this._writableState.defaultEncoding = encoding;", "gate": "compat:tests/node-compat/cases/64-stream-write-validation.js", @@ -2426,6 +2426,24 @@ "replace": " Object.defineProperty(Blob.prototype, ENUMERABLE_METHODS[m], { enumerable: false });", "gate": "odin:cmd/lava:main.blob_class_engine_traits", "expect_detail": "keys: got , want arrayBuffer" + }, + { + "name": "79-stream-internal-modules dies when the fromWeb wrapper returns in tail position", + "why": "JSC elides a strict-mode `return f()` frame (proper tail calls), so node's readable frame above the adapters frame would silently vanish.", + "source": "pkg/runtime/js/internal/streams/readable.js", + "find": " var bridged = lazyAdapters().newStreamReadableFromReadableStream(webStream, options);\n return bridged;", + "replace": " return lazyAdapters().newStreamReadableFromReadableStream(webStream, options);", + "gate": "compat:tests/node-compat/cases/79-stream-internal-modules.js", + "expect_detail": "node: Readable.fromWeb(5) wrapper: node:internal/streams/readable true false" + }, + { + "name": "79-stream-internal-modules dies when Duplex stops mixing in the Writable API", + "why": "The split put Duplex in its own file, reading Writable.prototype across a module seam; a mixin that copies nothing leaves a Duplex without write/end.", + "source": "pkg/runtime/js/internal/streams/duplex.js", + "find": " if (k === 'constructor') continue;", + "replace": " if (k !== 'writable') continue;", + "gate": "compat:tests/node-compat/cases/79-stream-internal-modules.js", + "expect_detail": "node: Duplex has write function" } ] } diff --git a/tests/node-compat/cases/79-stream-internal-modules.js b/tests/node-compat/cases/79-stream-internal-modules.js new file mode 100644 index 0000000..d32a3dd --- /dev/null +++ b/tests/node-compat/cases/79-stream-internal-modules.js @@ -0,0 +1,194 @@ +// node:stream is assembled from internal modules laid out like node's +// lib/internal/streams (plus internal/webstreams/adapters for the Web Streams bridges): +// an error thrown from inside Readable code carries a `node:internal/streams/readable` +// frame, one from Writable code `node:internal/streams/writable`, and so on. All of +// them used to say `node:stream`, because everything was defined in node/stream.js. +// +// Only the frame's SOURCE NAME is compared — never the message, line or column. Each +// site below is one node itself attributes to that file (probed on node 26.13.0). The +// behavior lines after it are there so a move that loses a helper, breaks a prototype +// chain or re-wires an identity fails here and not only in a stack. +'use strict'; + +const stream = require('node:stream'); +const { Stream, Readable, Writable, Duplex, Transform, PassThrough, finished, pipeline } = stream; + +function frame(label, source, f) { + let stack = null; + try { + const r = f(); + if (typeof r === 'string') stack = r; + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + // The old source name must be gone too: before the split every one of these frames + // read `node:stream:`. + console.log(label + ': ' + source + ' ' + stack.includes(source), stack.includes('node:stream:')); +} +const sink = () => + new Writable({ + write(c, e, cb) { + cb(); + }, + }); + +frame('write(null)', 'node:internal/streams/writable', () => sink().write(null)); +frame('setDefaultEncoding', 'node:internal/streams/writable', () => + sink().setDefaultEncoding('x!'), +); +frame('Readable#_read', 'node:internal/streams/readable', () => new Readable()._read()); +frame( + 'Readable encoding', + 'node:internal/streams/readable', + () => new Readable({ encoding: 'x!' }), +); +frame('Duplex encoding', 'node:internal/streams/duplex', () => new Duplex({ encoding: 'x!' })); +frame('Transform#_transform', 'node:internal/streams/transform', () => + new Transform()._transform(), +); +frame( + 'PassThrough encoding', + 'node:internal/streams/passthrough', + () => new PassThrough({ encoding: 'x!' }), +); +frame('Stream without this', 'node:internal/streams/legacy', () => Stream.call(undefined)); +for (const C of [Readable, Writable]) { + frame(C.name + ' _destroy', 'node:internal/streams/destroy', () => { + let st = null; + new C({ + destroy(err, cb) { + st = new Error('probe').stack; + cb(err); + }, + }).destroy(); + return st; + }); +} +frame('finished(5)', 'node:internal/streams/end-of-stream', () => finished(5, () => {})); +frame('pipeline no cb', 'node:internal/streams/pipeline', () => + pipeline(new PassThrough(), sink()), +); +frame('pipeline 1 stream', 'node:internal/streams/pipeline', () => + pipeline(new PassThrough(), () => {}), +); +frame('Readable.fromWeb(5)', 'node:internal/webstreams/adapters', () => Readable.fromWeb(5)); +frame('Readable.fromWeb(5) wrapper', 'node:internal/streams/readable', () => Readable.fromWeb(5)); +frame('Writable.fromWeb(5)', 'node:internal/webstreams/adapters', () => Writable.fromWeb(5)); +frame('Writable.fromWeb(5) wrapper', 'node:internal/streams/writable', () => Writable.fromWeb(5)); + +// --- the assembled module: identities, statics, prototype chains ----------------- +const again = require('stream'); +for (const k of [ + 'Stream', + 'Readable', + 'Writable', + 'Duplex', + 'Transform', + 'PassThrough', + 'finished', + 'pipeline', + 'addAbortSignal', + 'isReadable', + 'isWritable', +]) { + console.log('export', k, typeof stream[k], stream[k] === again[k]); +} +console.log('module is Stream', stream === Stream, Stream.Stream === Stream); +console.log('promises shares finished/pipeline wiring', typeof require('stream/promises').pipeline); +const P = Object.getPrototypeOf; +console.log('chain Readable', P(Readable.prototype) === Stream.prototype); +console.log('chain Writable', P(Writable.prototype) === Stream.prototype); +console.log('chain Stream', P(Stream.prototype) === require('events').prototype); +console.log('chain Duplex', P(Duplex.prototype) === Readable.prototype); +console.log('chain Transform', P(Transform.prototype) === Duplex.prototype); +console.log('chain PassThrough', P(PassThrough.prototype) === Transform.prototype); +for (const k of ['write', 'end', 'cork', 'uncork', 'setDefaultEncoding', '_undestroy', 'destroy']) { + console.log('Duplex has', k, typeof Duplex.prototype[k]); +} +for (const k of ['writable', 'writableLength', 'writableFinished', 'writableNeedDrain']) { + console.log( + 'Duplex accessor', + k, + typeof Object.getOwnPropertyDescriptor(Duplex.prototype, k).get, + ); +} +console.log( + '_undestroy', + typeof Readable.prototype._undestroy, + typeof Writable.prototype._undestroy, +); +console.log( + 'statics', + typeof Readable.from, + typeof Readable.fromWeb, + typeof Readable.toWeb, + typeof Writable.fromWeb, + typeof Writable.toWeb, +); +console.log('without new', Readable() instanceof Readable, PassThrough() instanceof Transform); +const d = new Duplex({ + read() {}, + write(c, e, cb) { + cb(); + }, +}); +console.log('Duplex state', d.writable, d.readable, d.writableLength, d.readableLength); + +// --- behavior across the file seams -------------------------------------------- +// Event ORDER across independent streams is not what this case pins (it differs from +// node today and is out of scope here), so the seven outcomes are collected and printed +// sorted once the last one lands. A missing outcome prints nothing, which still diffs. +const OUTCOMES = 7; +const out = []; +const log = (...a) => { + out.push(a.join(' ')); + if (out.length === OUTCOMES) console.log(out.sort().join('\n')); +}; +const seen = []; +pipeline( + Readable.from(['a', 'b', 'c']), + new Transform({ + transform(c, e, cb) { + cb(null, String(c).toUpperCase()); + }, + }), + new Writable({ + write(c, e, cb) { + seen.push(String(c)); + cb(); + }, + }), + (err) => log('pipeline done', !err, seen.join('')), +); +const bad = new PassThrough(); +pipeline(bad, new PassThrough(), (err) => log('pipeline error', err && err.code, bad.destroyed)); +bad.destroy(Object.assign(new Error('boom'), { code: 'EBOOM' })); +const early = new PassThrough(); +finished(early, (err) => log('premature', err && err.code)); +early.destroy(); +const pushed = new Readable({ read() {} }); +pushed.on('error', (e) => log('push(5) error', e.code)); +pushed.push(5); +const ac = new AbortController(); +const aborted = new Readable({ read() {}, signal: ac.signal }); +aborted.on('error', (e) => log('abort error', e.code)); +ac.abort(); +const fw = Readable.fromWeb( + new ReadableStream({ + start(c) { + c.enqueue('w'); + c.enqueue('eb'); + c.close(); + }, + }), +); +const webParts = []; +fw.on('data', (c) => webParts.push(String(c))); +fw.on('end', () => log('fromWeb', webParts.join(''))); +const tw = Readable.toWeb(Readable.from(['t', 'o'])); +(async () => { + const parts = []; + for await (const c of tw) parts.push(String(c)); + log('toWeb', parts.join('')); +})(); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 98c2474..1347db0 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -89,12 +89,84 @@ "accessor": 0, "global": 0 }, + "internal/streams/add-abort-signal.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 + }, + "internal/streams/destroy.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 + }, + "internal/streams/duplex.js": { + "method": 0, + "invoke": 2, + "accessor": 0, + "global": 4 + }, + "internal/streams/end-of-stream.js": { + "method": 0, + "invoke": 1, + "accessor": 0, + "global": 0 + }, + "internal/streams/legacy.js": { + "method": 0, + "invoke": 1, + "accessor": 0, + "global": 0 + }, + "internal/streams/passthrough.js": { + "method": 0, + "invoke": 1, + "accessor": 0, + "global": 0 + }, + "internal/streams/pipeline.js": { + "method": 1, + "invoke": 0, + "accessor": 0, + "global": 1 + }, + "internal/streams/readable.js": { + "method": 27, + "invoke": 2, + "accessor": 25, + "global": 14 + }, + "internal/streams/transform.js": { + "method": 4, + "invoke": 1, + "accessor": 0, + "global": 0 + }, + "internal/streams/utils.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 2 + }, + "internal/streams/writable.js": { + "method": 3, + "invoke": 1, + "accessor": 0, + "global": 1 + }, "internal/structured_clone.js": { "method": 5, "invoke": 1, "accessor": 0, "global": 14 }, + "internal/webstreams/adapters.js": { + "method": 7, + "invoke": 0, + "accessor": 0, + "global": 6 + }, "node/assert.js": { "method": 14, "invoke": 2, @@ -216,10 +288,10 @@ "global": 12 }, "node/stream.js": { - "method": 42, - "invoke": 9, - "accessor": 25, - "global": 28 + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 }, "node/stream/promises.js": { "method": 1, From 0532f25604ea1f6cff2eacbaf1f40ad2ecf909cf Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:38:13 +0200 Subject: [PATCH 31/61] fix(test): keep crypto's eager root single so the lazy_builtins mutation still bites The pristineRandomUUID line was a second top-level req('crypto'); the #427 mutant moved only the first, so crypto stayed eager and the entry survived. Fold the two into one root and retarget the mutant at it. Co-Authored-By: Claude Opus 5.5 --- pkg/runtime/js/bootstrap/loader.js | 3 ++- tests/mutation-manifest.json | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 31acb35..7d9517e 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -144,8 +144,9 @@ req('internal/encoding'); req('url'); req('internal/structured_clone'); - req('crypto'); + // crypto's ONE eager root (a second top-level req('crypto') would keep it eager even if + // this one moved, which is what the lazy_builtins mutation relies on not happening). // internal/blob mints `blob:nodedata:` ids with crypto.randomUUID, but it is // instantiated by `buffer` above — before crypto exists — and cannot require crypto // back (crypto requires buffer at its top level: a cycle check-js-graph rejects). So it diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index ce7a2a1..c86f9f5 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2066,10 +2066,10 @@ }, { "name": "builtin_factories_compile_on_first_require dies when the ratchet's eager set names a lazy module", - "why": "Statically crypto stays a loader req() root and so eager to the ratchet, but at run time it now loads after user code; static must stay a subset of what init compiles.", + "why": "Statically crypto stays a loader req() root and so eager to the ratchet, but at run time it now loads after user code; static must stay a subset of what init compiles. This line is crypto's only top-level root (it also publishes pristineRandomUUID), so moving it is moving every eager path to crypto.", "source": "pkg/runtime/js/bootstrap/loader.js", - "find": " req('crypto');\n", - "replace": " publicReq.lateCrypto = function () {\n req('crypto');\n };\n", + "find": " req.pristineRandomUUID = req('crypto').randomUUID;\n", + "replace": " publicReq.lateCrypto = function () {\n req.pristineRandomUUID = req('crypto').randomUUID;\n };\n", "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", "expect_detail": "is listed eager but compiled on first require" }, From 563f174042ba32042a8eaadbdf83a93096d0aa00 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 19:38:56 +0200 Subject: [PATCH 32/61] fix(test): retarget the lazy-os mutation off the removed bare req('crypto') Co-Authored-By: Claude Opus 5.5 --- tests/mutation-manifest.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index c86f9f5..a5bef39 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2022,8 +2022,8 @@ "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", "source": "pkg/runtime/js/bootstrap/loader.js", - "find": " req('crypto');\n", - "replace": " req('crypto');\n req('os');\n", + "find": " req('internal/structured_clone');\n", + "replace": " req('internal/structured_clone');\n req('os');\n", "gate": "odin:cmd/lava:main.builtin_factories_compile_on_first_require", "expect_detail": "expected state.builtin_compiles to be" }, From cb87479fae055cde72af8fb4b49886ebb8d3cd25 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:27:30 +0200 Subject: [PATCH 33/61] refactor(runtime): split stream/web.js into internal/webstreams like node node/stream/web.js (2034 lines) held the whole WHATWG Streams implementation. It is now 99 lines that assemble the same export object and install the same globals, from internal/webstreams/{util, queuingstrategies, readablestream, writablestream, transformstream}, the files node keeps them in. A pure move: each block arrived verbatim (checked by script); pipeTo, tee, the async iterator and BYOB live in readablestream as in node. Edges are all top-level and acyclic (readable -> util, writable; writable -> util; transform -> util, readable, writable), with imports captured at module eval. Eager set 27 -> 32, i.e. five more factory compiles at init. Frames now read node:internal/webstreams/*, as in node; pinned by tests/node-compat/cases/80-webstreams-internal-modules.js and two recorded mutations. Ratchet per-class totals unchanged. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 9 +- .../internal/webstreams/queuingstrategies.js | 45 + .../js/internal/webstreams/readablestream.js | 956 ++++++++ .../js/internal/webstreams/transformstream.js | 287 +++ pkg/runtime/js/internal/webstreams/util.js | 158 ++ .../js/internal/webstreams/writablestream.js | 666 ++++++ pkg/runtime/js/node/stream/web.js | 1975 +---------------- tests/mutation-manifest.json | 18 + .../cases/80-webstreams-internal-modules.js | 179 ++ tests/node-compat/pollution-baseline.json | 36 +- 11 files changed, 2370 insertions(+), 1961 deletions(-) create mode 100644 pkg/runtime/js/internal/webstreams/queuingstrategies.js create mode 100644 pkg/runtime/js/internal/webstreams/readablestream.js create mode 100644 pkg/runtime/js/internal/webstreams/transformstream.js create mode 100644 pkg/runtime/js/internal/webstreams/util.js create mode 100644 pkg/runtime/js/internal/webstreams/writablestream.js create mode 100644 tests/node-compat/cases/80-webstreams-internal-modules.js diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 325135e..0a550c6 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 485c026..2a27d1a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -183,13 +183,18 @@ at its own top level, so the late edge is what keeps the graph acyclic), as node factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), -`stream/web.js` (2027, Web Streams), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry +Web Streams (~2100 across `internal/webstreams/`), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), `fetch.js` (1054), `crypto.js` (917). `node/stream.js` only assembles the public module; the classic streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, `transform`, `passthrough`, `destroy`, `add-abort-signal`, `end-of-stream`, `pipeline`, `utils`) and the Web Streams bridges in `internal/webstreams/adapters.js`, the files node -keeps them in, so their frames read `node:internal/streams/readable`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +keeps them in, so their frames read `node:internal/streams/readable`. `node/stream/web.js` +likewise only assembles exports and installs the globals; the implementation is in +`internal/webstreams/` (`util`, `queuingstrategies`, `readablestream` — with tee, async +iteration and pipeTo, as in node — `writablestream`, `transformstream`), all eager through +`stream/web` and acyclic: `readablestream` requires `writablestream`, `transformstream` +requires both, nothing requires back. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/js/internal/webstreams/queuingstrategies.js b/pkg/runtime/js/internal/webstreams/queuingstrategies.js new file mode 100644 index 0000000..0e678fd --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/queuingstrategies.js @@ -0,0 +1,45 @@ +// ByteLengthQueuingStrategy and CountQueuingStrategy — node's +// lib/internal/webstreams/queuingstrategies.js. +(function (require, module) { + 'use strict'; + + class ByteLengthQueuingStrategy { + constructor(options) { + if (!options || options.highWaterMark === undefined) { + throw new TypeError('ByteLengthQueuingStrategy requires a highWaterMark option'); + } + this._highWaterMark = Number(options.highWaterMark); + } + get highWaterMark() { + return this._highWaterMark; + } + get size() { + // Per spec this is a fresh function bound to no stream; chunk.byteLength. + return function (chunk) { + return chunk.byteLength; + }; + } + } + + class CountQueuingStrategy { + constructor(options) { + if (!options || options.highWaterMark === undefined) { + throw new TypeError('CountQueuingStrategy requires a highWaterMark option'); + } + this._highWaterMark = Number(options.highWaterMark); + } + get highWaterMark() { + return this._highWaterMark; + } + get size() { + return function () { + return 1; + }; + } + } + + module.exports = { + ByteLengthQueuingStrategy: ByteLengthQueuingStrategy, + CountQueuingStrategy: CountQueuingStrategy, + }; +}); diff --git a/pkg/runtime/js/internal/webstreams/readablestream.js b/pkg/runtime/js/internal/webstreams/readablestream.js new file mode 100644 index 0000000..0ccf1d7 --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/readablestream.js @@ -0,0 +1,956 @@ +// ReadableStream, its default reader and controller, tee, async iteration, pipeTo and +// the deferred BYOB reader — node's lib/internal/webstreams/readablestream.js, which +// also keeps pipeTo here. pipeTo drives a WritableStream, so this file requires +// writablestream at its top level; writablestream never requires back, so there is no +// cycle to late-bind. +(function (require, module) { + 'use strict'; + + var util = require('internal/webstreams/util'); + var dequeueValue = util.dequeueValue; + var enqueueMicrotask = util.enqueueMicrotask; + var enqueueValueWithSize = util.enqueueValueWithSize; + var extractHighWaterMark = util.extractHighWaterMark; + var extractSizeAlgorithm = util.extractSizeAlgorithm; + var newDeferred = util.newDeferred; + var newPromise = util.newPromise; + var noop = util.noop; + var promiseFrom = util.promiseFrom; + var promiseRejectedWith = util.promiseRejectedWith; + var promiseResolvedWith = util.promiseResolvedWith; + var resetQueue = util.resetQueue; + var setPromiseIsHandled = util.setPromiseIsHandled; + var uponPromise = util.uponPromise; + var writable = require('internal/webstreams/writablestream'); + var WritableStream = writable.WritableStream; + var WritableStreamDefaultWriter = writable.WritableStreamDefaultWriter; + var isWritableStreamLocked = writable.isWritableStreamLocked; + var writableStreamAbort = writable.writableStreamAbort; + var writableStreamCloseQueuedOrInFlight = writable.writableStreamCloseQueuedOrInFlight; + var writableStreamDefaultWriterCloseWithErrorPropagation = + writable.writableStreamDefaultWriterCloseWithErrorPropagation; + var writableStreamDefaultWriterRelease = writable.writableStreamDefaultWriterRelease; + var writableStreamDefaultWriterWrite = writable.writableStreamDefaultWriterWrite; + + // ============================================================================ + // ReadableStream + // ============================================================================ + + class ReadableStream { + constructor(underlyingSource, strategy) { + underlyingSource = underlyingSource === undefined ? {} : underlyingSource; + strategy = strategy === undefined ? {} : strategy; + + this._state = 'readable'; + this._storedError = undefined; + this._reader = undefined; + this._disturbed = false; + this._controller = undefined; + + // Byte sources (BYOB) are deferred. Keep the rejection explicit rather than + // silently treating a byte stream as a default stream. + if (underlyingSource.type !== undefined) { + if (String(underlyingSource.type) === 'bytes') { + throw new TypeError( + "ReadableStream byte sources (underlyingSource.type 'bytes') are not " + + 'supported in Lava yet', + ); + } + throw new RangeError('Invalid underlyingSource.type'); + } + + var highWaterMark = extractHighWaterMark(strategy, 1); + var sizeAlgorithm = extractSizeAlgorithm(strategy); + setUpReadableStreamDefaultControllerFromUnderlyingSource( + this, + underlyingSource, + highWaterMark, + sizeAlgorithm, + ); + } + + get locked() { + return isReadableStreamLocked(this); + } + + cancel(reason) { + if (isReadableStreamLocked(this)) { + return promiseRejectedWith(new TypeError('Cannot cancel a locked stream')); + } + return readableStreamCancel(this, reason); + } + + getReader(options) { + if (options && options.mode !== undefined) { + if (String(options.mode) === 'byob') { + throw new TypeError( + 'BYOB readers are not supported in Lava yet (this stream is not a byte stream)', + ); + } + throw new TypeError('Invalid reader mode'); + } + return new ReadableStreamDefaultReader(this); + } + + pipeThrough(transform, options) { + if (!transform || !(transform.writable instanceof WritableStream)) { + throw new TypeError('pipeThrough requires a { writable, readable } pair'); + } + if (!(transform.readable instanceof ReadableStream)) { + throw new TypeError('pipeThrough requires a { writable, readable } pair'); + } + if (isReadableStreamLocked(this)) { + throw new TypeError('Cannot pipe a locked stream'); + } + if (isWritableStreamLocked(transform.writable)) { + throw new TypeError('Cannot pipe to a locked writable'); + } + options = options || {}; + var promise = readableStreamPipeTo( + this, + transform.writable, + !!options.preventClose, + !!options.preventAbort, + !!options.preventCancel, + options.signal, + ); + setPromiseIsHandled(promise); + return transform.readable; + } + + pipeTo(destination, options) { + if (!(destination instanceof WritableStream)) { + return promiseRejectedWith(new TypeError('pipeTo requires a WritableStream destination')); + } + if (isReadableStreamLocked(this)) { + return promiseRejectedWith(new TypeError('Cannot pipe a locked stream')); + } + if (isWritableStreamLocked(destination)) { + return promiseRejectedWith(new TypeError('Cannot pipe to a locked writable')); + } + options = options || {}; + return readableStreamPipeTo( + this, + destination, + !!options.preventClose, + !!options.preventAbort, + !!options.preventCancel, + options.signal, + ); + } + + tee() { + return readableStreamDefaultTee(this); + } + + values(options) { + return acquireReadableStreamAsyncIterator(this, options && options.preventCancel); + } + + [Symbol.asyncIterator](options) { + return acquireReadableStreamAsyncIterator(this, options && options.preventCancel); + } + } + + function isReadableStreamLocked(stream) { + return stream._reader !== undefined; + } + + function readableStreamCancel(stream, reason) { + stream._disturbed = true; + if (stream._state === 'closed') return promiseResolvedWith(undefined); + if (stream._state === 'errored') return promiseRejectedWith(stream._storedError); + readableStreamClose(stream); + var sourceCancelPromise = stream._controller._cancelSteps(reason); + return sourceCancelPromise.then(noop); + } + + function readableStreamClose(stream) { + stream._state = 'closed'; + var reader = stream._reader; + if (reader === undefined) return; + reader._closedDeferred.resolve(undefined); + if (reader._readRequests !== undefined) { + var requests = reader._readRequests; + reader._readRequests = []; + for (var i = 0; i < requests.length; i++) requests[i].closeSteps(); + } + } + + function readableStreamError(stream, e) { + stream._state = 'errored'; + stream._storedError = e; + var reader = stream._reader; + if (reader === undefined) return; + reader._closedDeferred.reject(e); + setPromiseIsHandled(reader._closedDeferred.promise); + if (reader._readRequests !== undefined) { + var requests = reader._readRequests; + reader._readRequests = []; + for (var i = 0; i < requests.length; i++) requests[i].errorSteps(e); + } + } + + function readableStreamAddReadRequest(stream, readRequest) { + stream._reader._readRequests.push(readRequest); + } + function readableStreamFulfillReadRequest(stream, chunk, done) { + var readRequest = stream._reader._readRequests.shift(); + if (done) readRequest.closeSteps(); + else readRequest.chunkSteps(chunk); + } + function readableStreamGetNumReadRequests(stream) { + return stream._reader._readRequests.length; + } + + // ---- ReadableStreamDefaultReader ------------------------------------------- + + class ReadableStreamDefaultReader { + constructor(stream) { + if (!(stream instanceof ReadableStream)) { + throw new TypeError('ReadableStreamDefaultReader requires a ReadableStream'); + } + if (isReadableStreamLocked(stream)) { + throw new TypeError('ReadableStream is already locked to a reader'); + } + readableStreamReaderGenericInitialize(this, stream); + this._readRequests = []; + } + + get closed() { + if (!this._ownerReadableStream) { + return promiseRejectedWith(readerLockException('closed')); + } + return this._closedDeferred.promise; + } + + read() { + if (!this._ownerReadableStream) { + return promiseRejectedWith(readerLockException('read from')); + } + var deferred = newDeferred(); + var readRequest = { + chunkSteps: function (chunk) { + deferred.resolve({ value: chunk, done: false }); + }, + closeSteps: function () { + deferred.resolve({ value: undefined, done: true }); + }, + errorSteps: function (e) { + deferred.reject(e); + }, + }; + readableStreamDefaultReaderRead(this, readRequest); + return deferred.promise; + } + + releaseLock() { + if (!this._ownerReadableStream) return; + readableStreamDefaultReaderRelease(this); + } + + cancel(reason) { + if (!this._ownerReadableStream) { + return promiseRejectedWith(readerLockException('cancel')); + } + return readableStreamReaderGenericCancel(this, reason); + } + } + + function readerLockException(name) { + return new TypeError('Cannot ' + name + ' a stream using a released reader'); + } + + function readableStreamReaderGenericInitialize(reader, stream) { + reader._ownerReadableStream = stream; + stream._reader = reader; + reader._closedDeferred = newDeferred(); + if (stream._state === 'readable') { + // pending + } else if (stream._state === 'closed') { + reader._closedDeferred.resolve(undefined); + } else { + reader._closedDeferred.reject(stream._storedError); + setPromiseIsHandled(reader._closedDeferred.promise); + } + } + + function readableStreamReaderGenericCancel(reader, reason) { + return readableStreamCancel(reader._ownerReadableStream, reason); + } + + function readableStreamReaderGenericRelease(reader) { + var stream = reader._ownerReadableStream; + if (stream._state === 'readable') { + reader._closedDeferred.reject(readerLockException('release a reader on')); + } else { + reader._closedDeferred = newDeferred(); + reader._closedDeferred.reject(readerLockException('release a reader on')); + } + setPromiseIsHandled(reader._closedDeferred.promise); + stream._controller._releaseSteps(); + stream._reader = undefined; + reader._ownerReadableStream = undefined; + } + + function readableStreamDefaultReaderRelease(reader) { + readableStreamReaderGenericRelease(reader); + var e = readerLockException('read from'); + var requests = reader._readRequests; + reader._readRequests = []; + for (var i = 0; i < requests.length; i++) requests[i].errorSteps(e); + } + + function readableStreamDefaultReaderRead(reader, readRequest) { + var stream = reader._ownerReadableStream; + stream._disturbed = true; + if (stream._state === 'closed') { + readRequest.closeSteps(); + } else if (stream._state === 'errored') { + readRequest.errorSteps(stream._storedError); + } else { + stream._controller._pullSteps(readRequest); + } + } + + function acquireReadableStreamDefaultReader(stream) { + return new ReadableStreamDefaultReader(stream); + } + + // ---- ReadableStreamDefaultController --------------------------------------- + + class ReadableStreamDefaultController { + constructor() { + throw new TypeError('Illegal constructor'); + } + + get desiredSize() { + return readableStreamDefaultControllerGetDesiredSize(this); + } + + close() { + if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) { + throw new TypeError('Cannot close a stream that is already closing or not readable'); + } + readableStreamDefaultControllerClose(this); + } + + enqueue(chunk) { + if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) { + throw new TypeError('Cannot enqueue a chunk into a stream that is closing or not readable'); + } + readableStreamDefaultControllerEnqueue(this, chunk); + } + + error(e) { + readableStreamDefaultControllerError(this, e); + } + + // ---- abstract control "steps" called by the stream/reader ---- + + _cancelSteps(reason) { + resetQueue(this); + var result = this._cancelAlgorithm(reason); + readableStreamDefaultControllerClearAlgorithms(this); + return result; + } + + _pullSteps(readRequest) { + var stream = this._controlledReadableStream; + if (this._queue.length > 0) { + var chunk = dequeueValue(this); + if (this._closeRequested && this._queue.length === 0) { + readableStreamDefaultControllerClearAlgorithms(this); + readableStreamClose(stream); + } else { + readableStreamDefaultControllerCallPullIfNeeded(this); + } + readRequest.chunkSteps(chunk); + } else { + readableStreamAddReadRequest(stream, readRequest); + readableStreamDefaultControllerCallPullIfNeeded(this); + } + } + + _releaseSteps() { + // Default controller keeps its queue across reader release. + } + } + + function readableStreamDefaultControllerCallPullIfNeeded(controller) { + if (!readableStreamDefaultControllerShouldCallPull(controller)) return; + if (controller._pulling) { + controller._pullAgain = true; + return; + } + controller._pulling = true; + var pullPromise = controller._pullAlgorithm(); + uponPromise( + pullPromise, + function () { + controller._pulling = false; + if (controller._pullAgain) { + controller._pullAgain = false; + readableStreamDefaultControllerCallPullIfNeeded(controller); + } + }, + function (e) { + readableStreamDefaultControllerError(controller, e); + }, + ); + } + + function readableStreamDefaultControllerShouldCallPull(controller) { + var stream = controller._controlledReadableStream; + if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return false; + if (!controller._started) return false; + if (isReadableStreamLocked(stream) && readableStreamGetNumReadRequests(stream) > 0) return true; + return readableStreamDefaultControllerGetDesiredSize(controller) > 0; + } + + function readableStreamDefaultControllerClearAlgorithms(controller) { + controller._pullAlgorithm = undefined; + controller._cancelAlgorithm = undefined; + controller._strategySizeAlgorithm = undefined; + } + + function readableStreamDefaultControllerClose(controller) { + if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return; + var stream = controller._controlledReadableStream; + controller._closeRequested = true; + if (controller._queue.length === 0) { + readableStreamDefaultControllerClearAlgorithms(controller); + readableStreamClose(stream); + } + } + + function readableStreamDefaultControllerEnqueue(controller, chunk) { + if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return; + var stream = controller._controlledReadableStream; + if (isReadableStreamLocked(stream) && readableStreamGetNumReadRequests(stream) > 0) { + readableStreamFulfillReadRequest(stream, chunk, false); + } else { + var chunkSize; + try { + chunkSize = controller._strategySizeAlgorithm(chunk); + } catch (e) { + readableStreamDefaultControllerError(controller, e); + throw e; + } + try { + enqueueValueWithSize(controller, chunk, chunkSize); + } catch (e) { + readableStreamDefaultControllerError(controller, e); + throw e; + } + } + readableStreamDefaultControllerCallPullIfNeeded(controller); + } + + function readableStreamDefaultControllerError(controller, e) { + var stream = controller._controlledReadableStream; + if (stream._state !== 'readable') return; + resetQueue(controller); + readableStreamDefaultControllerClearAlgorithms(controller); + readableStreamError(stream, e); + } + + function readableStreamDefaultControllerGetDesiredSize(controller) { + var state = controller._controlledReadableStream._state; + if (state === 'errored') return null; + if (state === 'closed') return 0; + return controller._strategyHWM - controller._queueTotalSize; + } + + function readableStreamDefaultControllerCanCloseOrEnqueue(controller) { + return ( + !controller._closeRequested && controller._controlledReadableStream._state === 'readable' + ); + } + + function setUpReadableStreamDefaultController( + stream, + controller, + startAlgorithm, + pullAlgorithm, + cancelAlgorithm, + highWaterMark, + sizeAlgorithm, + ) { + controller._controlledReadableStream = stream; + resetQueue(controller); + controller._started = false; + controller._closeRequested = false; + controller._pullAgain = false; + controller._pulling = false; + controller._strategySizeAlgorithm = sizeAlgorithm; + controller._strategyHWM = highWaterMark; + controller._pullAlgorithm = pullAlgorithm; + controller._cancelAlgorithm = cancelAlgorithm; + stream._controller = controller; + + var startResult = startAlgorithm(); + uponPromise( + promiseResolvedWith(startResult), + function () { + controller._started = true; + readableStreamDefaultControllerCallPullIfNeeded(controller); + }, + function (r) { + readableStreamDefaultControllerError(controller, r); + }, + ); + } + + function setUpReadableStreamDefaultControllerFromUnderlyingSource( + stream, + underlyingSource, + highWaterMark, + sizeAlgorithm, + ) { + var controller = Object.create(ReadableStreamDefaultController.prototype); + var startAlgorithm = noop; + var pullAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + var cancelAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + if (typeof underlyingSource.start === 'function') { + startAlgorithm = function () { + return promiseFrom(function () { + return underlyingSource.start(controller); + }); + }; + } + if (typeof underlyingSource.pull === 'function') { + pullAlgorithm = function () { + return promiseFrom(function () { + return underlyingSource.pull(controller); + }); + }; + } + if (typeof underlyingSource.cancel === 'function') { + cancelAlgorithm = function (reason) { + return promiseFrom(function () { + return underlyingSource.cancel(reason); + }); + }; + } + setUpReadableStreamDefaultController( + stream, + controller, + startAlgorithm, + pullAlgorithm, + cancelAlgorithm, + highWaterMark, + sizeAlgorithm, + ); + } + + // createReadableStream builds a stream from internal algorithms (used by tee). + function createReadableStream( + startAlgorithm, + pullAlgorithm, + cancelAlgorithm, + highWaterMark, + sizeAlgorithm, + ) { + var stream = Object.create(ReadableStream.prototype); + stream._state = 'readable'; + stream._storedError = undefined; + stream._reader = undefined; + stream._disturbed = false; + stream._controller = undefined; + var controller = Object.create(ReadableStreamDefaultController.prototype); + setUpReadableStreamDefaultController( + stream, + controller, + startAlgorithm, + pullAlgorithm, + cancelAlgorithm, + highWaterMark === undefined ? 1 : highWaterMark, + sizeAlgorithm || + function () { + return 1; + }, + ); + return stream; + } + + // ---- tee (default) --------------------------------------------------------- + + function readableStreamDefaultTee(stream) { + var reader = acquireReadableStreamDefaultReader(stream); + var reading = false; + var readAgainForBranch1 = false; + var readAgainForBranch2 = false; + var canceled1 = false; + var canceled2 = false; + var reason1, reason2; + var branch1, branch2; + var cancelDeferred = newDeferred(); + + function pullAlgorithm() { + if (reading) { + readAgainForBranch1 = true; + readAgainForBranch2 = true; + return promiseResolvedWith(undefined); + } + reading = true; + var readRequest = { + chunkSteps: function (chunk) { + enqueueMicrotask(function () { + readAgainForBranch1 = false; + readAgainForBranch2 = false; + if (!canceled1) readableStreamDefaultControllerEnqueue(branch1._controller, chunk); + if (!canceled2) readableStreamDefaultControllerEnqueue(branch2._controller, chunk); + reading = false; + if (readAgainForBranch1) pullAlgorithm(); + else if (readAgainForBranch2) pullAlgorithm(); + }); + }, + closeSteps: function () { + reading = false; + if (!canceled1) readableStreamDefaultControllerClose(branch1._controller); + if (!canceled2) readableStreamDefaultControllerClose(branch2._controller); + if (!canceled1 || !canceled2) cancelDeferred.resolve(undefined); + }, + errorSteps: function () { + reading = false; + }, + }; + readableStreamDefaultReaderRead(reader, readRequest); + return promiseResolvedWith(undefined); + } + + function cancel1Algorithm(reason) { + canceled1 = true; + reason1 = reason; + if (canceled2) { + var composite = [reason1, reason2]; + var cancelResult = readableStreamCancel(stream, composite); + cancelDeferred.resolve(cancelResult); + } + return cancelDeferred.promise; + } + function cancel2Algorithm(reason) { + canceled2 = true; + reason2 = reason; + if (canceled1) { + var composite = [reason1, reason2]; + var cancelResult = readableStreamCancel(stream, composite); + cancelDeferred.resolve(cancelResult); + } + return cancelDeferred.promise; + } + + branch1 = createReadableStream(noop, pullAlgorithm, cancel1Algorithm); + branch2 = createReadableStream(noop, pullAlgorithm, cancel2Algorithm); + + uponPromise(reader._closedDeferred.promise, undefined, function (e) { + readableStreamDefaultControllerError(branch1._controller, e); + readableStreamDefaultControllerError(branch2._controller, e); + if (!canceled1 || !canceled2) cancelDeferred.resolve(undefined); + }); + + return [branch1, branch2]; + } + + // ---- async iterator -------------------------------------------------------- + + function acquireReadableStreamAsyncIterator(stream, preventCancel) { + var reader = acquireReadableStreamDefaultReader(stream); + var iterator = Object.create(ReadableStreamAsyncIteratorPrototype); + iterator._reader = reader; + iterator._preventCancel = !!preventCancel; + return iterator; + } + + var ReadableStreamAsyncIteratorPrototype = { + next: function () { + var reader = this._reader; + if (!reader._ownerReadableStream) { + return promiseRejectedWith(readerLockException('iterate')); + } + var deferred = newDeferred(); + var readRequest = { + chunkSteps: function (chunk) { + deferred.resolve({ value: chunk, done: false }); + }, + closeSteps: function () { + readableStreamDefaultReaderRelease(reader); + deferred.resolve({ value: undefined, done: true }); + }, + errorSteps: function (e) { + readableStreamDefaultReaderRelease(reader); + deferred.reject(e); + }, + }; + readableStreamDefaultReaderRead(reader, readRequest); + return deferred.promise; + }, + return: function (value) { + var reader = this._reader; + if (!reader._ownerReadableStream) { + return promiseResolvedWith({ value: value, done: true }); + } + if (reader._readRequests.length > 0) { + return promiseRejectedWith( + new TypeError('Tried to release a reader lock when that reader has pending read calls'), + ); + } + if (!this._preventCancel) { + var result = readableStreamReaderGenericCancel(reader, value); + readableStreamDefaultReaderRelease(reader); + return result.then(function () { + return { value: value, done: true }; + }); + } + readableStreamDefaultReaderRelease(reader); + return promiseResolvedWith({ value: value, done: true }); + }, + }; + ReadableStreamAsyncIteratorPrototype[Symbol.asyncIterator] = function () { + return this; + }; + + // ---- pipeTo ---------------------------------------------------------------- + + function readableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) { + var reader = acquireReadableStreamDefaultReader(source); + var writer = new WritableStreamDefaultWriter(dest); + source._disturbed = true; + var shuttingDown = false; + var currentWrite = promiseResolvedWith(undefined); + var resultDeferred = newDeferred(); + + var abortAlgorithm; + if (signal !== undefined && signal !== null) { + abortAlgorithm = function () { + var error = + signal.reason !== undefined ? signal.reason : new DOMExceptionLike('AbortError'); + var actions = []; + if (!preventAbort) { + actions.push(function () { + if (dest._state === 'writable') return writableStreamAbort(dest, error); + return promiseResolvedWith(undefined); + }); + } + if (!preventCancel) { + actions.push(function () { + if (source._state === 'readable') return readableStreamCancel(source, error); + return promiseResolvedWith(undefined); + }); + } + shutdownWithAction( + function () { + return Promise.all( + actions.map(function (a) { + return a(); + }), + ); + }, + true, + error, + ); + }; + if (signal.aborted) { + abortAlgorithm(); + return resultDeferred.promise; + } + signal.addEventListener('abort', abortAlgorithm); + } + + function isOrBecomesErrored(stream, promise, action) { + if (stream._state === 'errored') action(stream._storedError); + else uponPromise(promise, undefined, action); + } + function isOrBecomesClosed(stream, promise, action) { + if (stream._state === 'closed') action(); + else uponPromise(promise, action, undefined); + } + + // Error/close forwarding. + isOrBecomesErrored(source, reader._closedDeferred.promise, function (storedError) { + if (!preventAbort) { + shutdownWithAction( + function () { + return writableStreamAbort(dest, storedError); + }, + true, + storedError, + ); + } else { + shutdown(true, storedError); + } + }); + isOrBecomesErrored(dest, writer._closedDeferred.promise, function (storedError) { + if (!preventCancel) { + shutdownWithAction( + function () { + return readableStreamCancel(source, storedError); + }, + true, + storedError, + ); + } else { + shutdown(true, storedError); + } + }); + isOrBecomesClosed(source, reader._closedDeferred.promise, function () { + if (!preventClose) { + shutdownWithAction(function () { + return writableStreamDefaultWriterCloseWithErrorPropagation(writer); + }); + } else { + shutdown(); + } + }); + if (writableStreamCloseQueuedOrInFlight(dest) || dest._state === 'closed') { + var destClosed = new TypeError( + 'the destination writable stream closed before all data was piped to it', + ); + if (!preventCancel) { + shutdownWithAction( + function () { + return readableStreamCancel(source, destClosed); + }, + true, + destClosed, + ); + } else { + shutdown(true, destClosed); + } + } + + function pipeLoop() { + return newPromise(function (resolveLoop, rejectLoop) { + function next(done) { + if (done) resolveLoop(undefined); + else uponPromise(pipeStep(), next, rejectLoop); + } + next(false); + }); + } + + function pipeStep() { + if (shuttingDown) return promiseResolvedWith(true); + return writer._readyDeferred.promise.then(function () { + return newPromise(function (resolveRead, rejectRead) { + readableStreamDefaultReaderRead(reader, { + chunkSteps: function (chunk) { + // Swallow a write rejection here: pipe write errors surface through + // the dest-errored forwarding (writer.closed), and waitForWrites- + // ToFinish must observe a never-rejecting currentWrite (spec). + currentWrite = writableStreamDefaultWriterWrite(writer, chunk).then(undefined, noop); + resolveRead(false); + }, + closeSteps: function () { + resolveRead(true); + }, + errorSteps: rejectRead, + }); + }); + }); + } + + uponPromise(pipeLoop(), undefined, noop); + + function waitForWritesToFinish() { + var oldCurrentWrite = currentWrite; + return currentWrite.then(function () { + return oldCurrentWrite !== currentWrite ? waitForWritesToFinish() : undefined; + }); + } + + function shutdownWithAction(action, originalIsError, originalError) { + if (shuttingDown) return; + shuttingDown = true; + if (dest._state === 'writable' && !writableStreamCloseQueuedOrInFlight(dest)) { + uponPromise(waitForWritesToFinish(), doTheRest); + } else { + doTheRest(); + } + function doTheRest() { + uponPromise( + action(), + function () { + finalize(originalIsError, originalError); + }, + function (newError) { + finalize(true, newError); + }, + ); + } + } + + function shutdown(isError, error) { + if (shuttingDown) return; + shuttingDown = true; + if (dest._state === 'writable' && !writableStreamCloseQueuedOrInFlight(dest)) { + uponPromise(waitForWritesToFinish(), function () { + finalize(isError, error); + }); + } else { + finalize(isError, error); + } + } + + function finalize(isError, error) { + writableStreamDefaultWriterRelease(writer); + readableStreamDefaultReaderRelease(reader); + if (signal !== undefined && signal !== null) { + signal.removeEventListener('abort', abortAlgorithm); + } + if (isError) resultDeferred.reject(error); + else resultDeferred.resolve(undefined); + } + + return resultDeferred.promise; + } + + // A tiny stand-in for AbortError when a signal has no reason. Real AbortSignal + // (js/internal/abort.js) supplies a DOMException reason, so this is only a + // fallback for hand-rolled signals. + function DOMExceptionLike(name) { + var e = new Error(name); + e.name = name; + return e; + } + + function readableStreamDefaultControllerHasBackpressure(controller) { + return readableStreamDefaultControllerGetDesiredSize(controller) <= 0; + } + + // ============================================================================ + // BYOB (deferred) — expose the constructor so `typeof` matches, but report the + // deferral explicitly on use. + // ============================================================================ + + class ReadableStreamBYOBReader { + constructor(stream) { + if (!(stream instanceof ReadableStream)) { + throw new TypeError('ReadableStreamBYOBReader requires a ReadableStream'); + } + // Lava has no byte-stream sources yet, so there is never a valid byte + // stream to attach to — report that explicitly rather than half-attaching. + throw new TypeError('BYOB readers are not supported in Lava yet (no readable byte streams)'); + } + } + + module.exports = { + ReadableStream: ReadableStream, + ReadableStreamDefaultReader: ReadableStreamDefaultReader, + ReadableStreamBYOBReader: ReadableStreamBYOBReader, + ReadableStreamDefaultController: ReadableStreamDefaultController, + createReadableStream: createReadableStream, + readableStreamDefaultControllerCanCloseOrEnqueue: + readableStreamDefaultControllerCanCloseOrEnqueue, + readableStreamDefaultControllerClose: readableStreamDefaultControllerClose, + readableStreamDefaultControllerEnqueue: readableStreamDefaultControllerEnqueue, + readableStreamDefaultControllerError: readableStreamDefaultControllerError, + readableStreamDefaultControllerGetDesiredSize: readableStreamDefaultControllerGetDesiredSize, + readableStreamDefaultControllerHasBackpressure: readableStreamDefaultControllerHasBackpressure, + }; +}); diff --git a/pkg/runtime/js/internal/webstreams/transformstream.js b/pkg/runtime/js/internal/webstreams/transformstream.js new file mode 100644 index 0000000..2315c3c --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/transformstream.js @@ -0,0 +1,287 @@ +// TransformStream and its default controller — node's +// lib/internal/webstreams/transformstream.js. Builds its two sides through +// readablestream's and writablestream's internal constructors, both required at top +// level (neither requires this file). +(function (require, module) { + 'use strict'; + + var util = require('internal/webstreams/util'); + var extractHighWaterMark = util.extractHighWaterMark; + var extractSizeAlgorithm = util.extractSizeAlgorithm; + var newDeferred = util.newDeferred; + var promiseFrom = util.promiseFrom; + var promiseRejectedWith = util.promiseRejectedWith; + var promiseResolvedWith = util.promiseResolvedWith; + var readable = require('internal/webstreams/readablestream'); + var createReadableStream = readable.createReadableStream; + var readableStreamDefaultControllerCanCloseOrEnqueue = + readable.readableStreamDefaultControllerCanCloseOrEnqueue; + var readableStreamDefaultControllerClose = readable.readableStreamDefaultControllerClose; + var readableStreamDefaultControllerEnqueue = readable.readableStreamDefaultControllerEnqueue; + var readableStreamDefaultControllerError = readable.readableStreamDefaultControllerError; + var readableStreamDefaultControllerGetDesiredSize = + readable.readableStreamDefaultControllerGetDesiredSize; + var readableStreamDefaultControllerHasBackpressure = + readable.readableStreamDefaultControllerHasBackpressure; + var writable = require('internal/webstreams/writablestream'); + var createWritableStream = writable.createWritableStream; + var writableStreamDefaultControllerErrorIfNeeded = + writable.writableStreamDefaultControllerErrorIfNeeded; + + // ============================================================================ + // TransformStream + // ============================================================================ + + class TransformStream { + constructor(transformer, writableStrategy, readableStrategy) { + transformer = transformer === undefined ? {} : transformer; + writableStrategy = writableStrategy === undefined ? {} : writableStrategy; + readableStrategy = readableStrategy === undefined ? {} : readableStrategy; + if (transformer.readableType !== undefined) { + throw new RangeError('Invalid transformer.readableType'); + } + if (transformer.writableType !== undefined) { + throw new RangeError('Invalid transformer.writableType'); + } + + var readableHWM = extractHighWaterMark(readableStrategy, 0); + var readableSize = extractSizeAlgorithm(readableStrategy); + var writableHWM = extractHighWaterMark(writableStrategy, 1); + var writableSize = extractSizeAlgorithm(writableStrategy); + + var startDeferred = newDeferred(); + initializeTransformStream( + this, + startDeferred.promise, + writableHWM, + writableSize, + readableHWM, + readableSize, + ); + setUpTransformStreamDefaultControllerFromTransformer(this, transformer); + + if (typeof transformer.start === 'function') { + startDeferred.resolve(transformer.start(this._controller)); + } else { + startDeferred.resolve(undefined); + } + } + + get readable() { + return this._readable; + } + get writable() { + return this._writable; + } + } + + function initializeTransformStream( + stream, + startPromise, + writableHWM, + writableSize, + readableHWM, + readableSize, + ) { + function startAlgorithm() { + return startPromise; + } + function writeAlgorithm(chunk) { + return transformStreamDefaultSinkWriteAlgorithm(stream, chunk); + } + function abortAlgorithm(reason) { + return transformStreamDefaultSinkAbortAlgorithm(stream, reason); + } + function closeAlgorithm() { + return transformStreamDefaultSinkCloseAlgorithm(stream); + } + stream._writable = createWritableStream( + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + writableHWM, + writableSize, + ); + + function pullAlgorithm() { + return transformStreamDefaultSourcePullAlgorithm(stream); + } + function cancelAlgorithm(reason) { + transformStreamErrorWritableAndUnblockWrite(stream, reason); + return promiseResolvedWith(undefined); + } + stream._readable = createReadableStream( + startAlgorithm, + pullAlgorithm, + cancelAlgorithm, + readableHWM, + readableSize, + ); + + stream._backpressure = undefined; + stream._backpressureChangeDeferred = undefined; + transformStreamSetBackpressure(stream, true); + stream._controller = undefined; + } + + function transformStreamErrorWritableAndUnblockWrite(stream, e) { + transformStreamDefaultControllerClearAlgorithms(stream._controller); + writableStreamDefaultControllerErrorIfNeeded(stream._writable._controller, e); + transformStreamUnblockWrite(stream); + } + function transformStreamUnblockWrite(stream) { + if (stream._backpressure) transformStreamSetBackpressure(stream, false); + } + function transformStreamError(stream, e) { + readableStreamDefaultControllerError(stream._readable._controller, e); + transformStreamErrorWritableAndUnblockWrite(stream, e); + } + function transformStreamSetBackpressure(stream, backpressure) { + if (stream._backpressureChangeDeferred !== undefined) { + stream._backpressureChangeDeferred.resolve(undefined); + } + stream._backpressureChangeDeferred = newDeferred(); + stream._backpressure = backpressure; + } + + class TransformStreamDefaultController { + constructor() { + throw new TypeError('Illegal constructor'); + } + get desiredSize() { + return readableStreamDefaultControllerGetDesiredSize(this._stream._readable._controller); + } + enqueue(chunk) { + transformStreamDefaultControllerEnqueue(this, chunk); + } + error(reason) { + transformStreamDefaultControllerError(this, reason); + } + terminate() { + transformStreamDefaultControllerTerminate(this); + } + } + + function setUpTransformStreamDefaultController( + stream, + controller, + transformAlgorithm, + flushAlgorithm, + ) { + controller._stream = stream; + stream._controller = controller; + controller._transformAlgorithm = transformAlgorithm; + controller._flushAlgorithm = flushAlgorithm; + } + function setUpTransformStreamDefaultControllerFromTransformer(stream, transformer) { + var controller = Object.create(TransformStreamDefaultController.prototype); + var transformAlgorithm = function (chunk) { + try { + transformStreamDefaultControllerEnqueue(controller, chunk); + return promiseResolvedWith(undefined); + } catch (e) { + return promiseRejectedWith(e); + } + }; + var flushAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + if (typeof transformer.transform === 'function') { + transformAlgorithm = function (chunk) { + return promiseFrom(function () { + return transformer.transform(chunk, controller); + }); + }; + } + if (typeof transformer.flush === 'function') { + flushAlgorithm = function () { + return promiseFrom(function () { + return transformer.flush(controller); + }); + }; + } + setUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm); + } + function transformStreamDefaultControllerClearAlgorithms(controller) { + controller._transformAlgorithm = undefined; + controller._flushAlgorithm = undefined; + } + function transformStreamDefaultControllerEnqueue(controller, chunk) { + var stream = controller._stream; + var readableController = stream._readable._controller; + if (!readableStreamDefaultControllerCanCloseOrEnqueue(readableController)) { + throw new TypeError('Readable side is not in a state that permits enqueue'); + } + try { + readableStreamDefaultControllerEnqueue(readableController, chunk); + } catch (e) { + transformStreamErrorWritableAndUnblockWrite(stream, e); + throw stream._readable._storedError; + } + var backpressure = readableStreamDefaultControllerHasBackpressure(readableController); + if (backpressure !== stream._backpressure) { + transformStreamSetBackpressure(stream, true); + } + } + + function transformStreamDefaultControllerError(controller, e) { + transformStreamError(controller._stream, e); + } + function transformStreamDefaultControllerPerformTransform(controller, chunk) { + var transformPromise = controller._transformAlgorithm(chunk); + return transformPromise.then(undefined, function (r) { + transformStreamError(controller._stream, r); + throw r; + }); + } + function transformStreamDefaultControllerTerminate(controller) { + var stream = controller._stream; + var readableController = stream._readable._controller; + readableStreamDefaultControllerClose(readableController); + var error = new TypeError('TransformStream terminated'); + transformStreamErrorWritableAndUnblockWrite(stream, error); + } + + function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { + var controller = stream._controller; + if (stream._backpressure) { + var backpressureChangePromise = stream._backpressureChangeDeferred.promise; + return backpressureChangePromise.then(function () { + var writable = stream._writable; + var state = writable._state; + if (state === 'erroring') throw writable._storedError; + return transformStreamDefaultControllerPerformTransform(controller, chunk); + }); + } + return transformStreamDefaultControllerPerformTransform(controller, chunk); + } + function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { + transformStreamError(stream, reason); + return promiseResolvedWith(undefined); + } + function transformStreamDefaultSinkCloseAlgorithm(stream) { + var controller = stream._controller; + var flushPromise = controller._flushAlgorithm(); + transformStreamDefaultControllerClearAlgorithms(controller); + return flushPromise.then( + function () { + if (stream._readable._state === 'errored') throw stream._readable._storedError; + readableStreamDefaultControllerClose(stream._readable._controller); + }, + function (r) { + transformStreamError(stream, r); + throw stream._readable._storedError; + }, + ); + } + function transformStreamDefaultSourcePullAlgorithm(stream) { + transformStreamSetBackpressure(stream, false); + return stream._backpressureChangeDeferred.promise; + } + + module.exports = { + TransformStream: TransformStream, + TransformStreamDefaultController: TransformStreamDefaultController, + }; +}); diff --git a/pkg/runtime/js/internal/webstreams/util.js b/pkg/runtime/js/internal/webstreams/util.js new file mode 100644 index 0000000..9c18cb4 --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/util.js @@ -0,0 +1,158 @@ +// Helpers every Web Streams file shares — the Lava side of node's +// lib/internal/webstreams/util.js: the promise plumbing, the spec's queue-with-sizes, +// and the strategy extraction the three stream constructors run. +(function (require, module) { + 'use strict'; + + // ---- microtask + promise helpers ------------------------------------------- + + // queueMicrotask exists as a global (microtasks.js), but fall back to a + // resolved-promise continuation so this module is usable even if it is loaded + // before that global appears. + var enqueueMicrotask = + typeof queueMicrotask === 'function' + ? queueMicrotask + : function (cb) { + Promise.resolve().then(cb); + }; + + function noop() {} + + function newPromise(executor) { + return new Promise(executor); + } + // Invoke a method (preserving `this` via the caller's closure) and turn a + // synchronous throw into a rejected promise, so an underlying source/sink/ + // transformer callback that throws behaves like one that returns a rejected + // promise (spec algorithms always settle, never throw out of the controller). + function promiseFrom(thunk) { + try { + return promiseResolvedWith(thunk()); + } catch (e) { + return promiseRejectedWith(e); + } + } + function promiseResolvedWith(value) { + return Promise.resolve(value); + } + function promiseRejectedWith(reason) { + return Promise.reject(reason); + } + // A deferred: { promise, resolve, reject }. Used for reader/writer closed and + // writer ready promises, which settle from elsewhere in the algorithm. + function newDeferred() { + var d = {}; + d.promise = new Promise(function (resolve, reject) { + d.resolve = resolve; + d.reject = reject; + }); + return d; + } + // Attach reactions without creating an unhandled-rejection if `promise` rejects + // and only onRejected is registered elsewhere later — matches the spec's + // PerformPromiseThen, which never surfaces an unhandled rejection. + function performPromiseThen(promise, onFulfilled, onRejected) { + promise.then(onFulfilled || undefined, onRejected || undefined); + } + function uponPromise(promise, onFulfilled, onRejected) { + performPromiseThen(promise, onFulfilled, onRejected); + } + function setPromiseIsHandled(promise) { + promise.then(undefined, noop); + } + + // ---- generic queue (spec "queue-with-sizes") ------------------------------- + + // A FIFO queue with amortized O(1) push/shift. A plain array's shift() is O(n) + // — it reindexes every remaining element — so draining N queued chunks costs + // O(n^2). That bites a byte-sized response body or a pipe that arrives as many + // small chunks: the queue can hold thousands of them before a reader drains + // (the high-water mark bounds queued *bytes*, not chunk count). Keeping a head + // cursor and only compacting once the consumed prefix dominates the backing + // array keeps push/shift/peek/length O(1) amortized while bounding wasted space + // to ~2x the live length. + class SimpleQueue { + constructor() { + this._items = []; + this._head = 0; + } + get length() { + return this._items.length - this._head; + } + push(item) { + this._items.push(item); + } + shift() { + var item = this._items[this._head]; + this._head++; + if (this._head > 64 && this._head * 2 >= this._items.length) { + this._items = this._items.slice(this._head); + this._head = 0; + } + return item; + } + peek() { + return this._items[this._head]; + } + } + + function resetQueue(container) { + container._queue = new SimpleQueue(); + container._queueTotalSize = 0; + } + function dequeueValue(container) { + var pair = container._queue.shift(); + container._queueTotalSize -= pair.size; + if (container._queueTotalSize < 0) container._queueTotalSize = 0; + return pair.value; + } + function enqueueValueWithSize(container, value, size) { + if (typeof size !== 'number' || size !== size || size === Infinity || size < 0) { + throw new RangeError('Invalid chunk size'); + } + container._queue.push({ value: value, size: size }); + container._queueTotalSize += size; + } + function peekQueueValue(container) { + return container._queue.peek().value; + } + + // ---- strategy extraction (node keeps these in util.js too) ------------------ + + function extractHighWaterMark(strategy, defaultHWM) { + if (!strategy || strategy.highWaterMark === undefined) return defaultHWM; + var hwm = Number(strategy.highWaterMark); + if (hwm !== hwm || hwm < 0) throw new RangeError('Invalid highWaterMark'); + return hwm; + } + function extractSizeAlgorithm(strategy) { + if (!strategy || strategy.size === undefined) { + return function () { + return 1; + }; + } + var size = strategy.size; + if (typeof size !== 'function') throw new TypeError('strategy.size must be a function'); + return function (chunk) { + return Number(size(chunk)); + }; + } + + module.exports = { + noop: noop, + newPromise: newPromise, + promiseFrom: promiseFrom, + promiseResolvedWith: promiseResolvedWith, + promiseRejectedWith: promiseRejectedWith, + newDeferred: newDeferred, + uponPromise: uponPromise, + setPromiseIsHandled: setPromiseIsHandled, + enqueueMicrotask: enqueueMicrotask, + resetQueue: resetQueue, + dequeueValue: dequeueValue, + enqueueValueWithSize: enqueueValueWithSize, + peekQueueValue: peekQueueValue, + extractHighWaterMark: extractHighWaterMark, + extractSizeAlgorithm: extractSizeAlgorithm, + }; +}); diff --git a/pkg/runtime/js/internal/webstreams/writablestream.js b/pkg/runtime/js/internal/webstreams/writablestream.js new file mode 100644 index 0000000..18ab770 --- /dev/null +++ b/pkg/runtime/js/internal/webstreams/writablestream.js @@ -0,0 +1,666 @@ +// WritableStream, its default writer and controller — node's +// lib/internal/webstreams/writablestream.js. Depends on util only: readablestream (for +// pipeTo) and transformstream require this file, never the other way. +(function (require, module) { + 'use strict'; + + var util = require('internal/webstreams/util'); + var dequeueValue = util.dequeueValue; + var enqueueValueWithSize = util.enqueueValueWithSize; + var extractHighWaterMark = util.extractHighWaterMark; + var extractSizeAlgorithm = util.extractSizeAlgorithm; + var newDeferred = util.newDeferred; + var noop = util.noop; + var peekQueueValue = util.peekQueueValue; + var promiseFrom = util.promiseFrom; + var promiseRejectedWith = util.promiseRejectedWith; + var promiseResolvedWith = util.promiseResolvedWith; + var resetQueue = util.resetQueue; + var setPromiseIsHandled = util.setPromiseIsHandled; + var uponPromise = util.uponPromise; + + // ============================================================================ + // WritableStream + // ============================================================================ + + class WritableStream { + constructor(underlyingSink, strategy) { + underlyingSink = underlyingSink === undefined ? {} : underlyingSink; + strategy = strategy === undefined ? {} : strategy; + if (underlyingSink.type !== undefined) { + throw new RangeError('Invalid underlyingSink.type'); + } + + this._state = 'writable'; + this._storedError = undefined; + this._writer = undefined; + this._controller = undefined; + this._inFlightWriteRequest = undefined; + this._closeRequest = undefined; + this._inFlightCloseRequest = undefined; + this._pendingAbortRequest = undefined; + this._writeRequests = []; + this._backpressure = false; + + var highWaterMark = extractHighWaterMark(strategy, 1); + var sizeAlgorithm = extractSizeAlgorithm(strategy); + setUpWritableStreamDefaultControllerFromUnderlyingSink( + this, + underlyingSink, + highWaterMark, + sizeAlgorithm, + ); + } + + get locked() { + return isWritableStreamLocked(this); + } + + abort(reason) { + if (isWritableStreamLocked(this)) { + return promiseRejectedWith(new TypeError('Cannot abort a locked stream')); + } + return writableStreamAbort(this, reason); + } + + close() { + if (isWritableStreamLocked(this)) { + return promiseRejectedWith(new TypeError('Cannot close a locked stream')); + } + if (writableStreamCloseQueuedOrInFlight(this)) { + return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); + } + return writableStreamClose(this); + } + + getWriter() { + return new WritableStreamDefaultWriter(this); + } + } + + function isWritableStreamLocked(stream) { + return stream._writer !== undefined; + } + + function writableStreamAbort(stream, reason) { + if (stream._state === 'closed' || stream._state === 'errored') { + return promiseResolvedWith(undefined); + } + if (stream._pendingAbortRequest !== undefined) { + return stream._pendingAbortRequest.deferred.promise; + } + var wasAlreadyErroring = false; + if (stream._state === 'erroring') { + wasAlreadyErroring = true; + reason = undefined; + } + var deferred = newDeferred(); + stream._pendingAbortRequest = { + deferred: deferred, + reason: reason, + wasAlreadyErroring: wasAlreadyErroring, + }; + if (!wasAlreadyErroring) writableStreamStartErroring(stream, reason); + return deferred.promise; + } + + function writableStreamClose(stream) { + // Spec step 2: a closed/errored stream rejects — its close/write algorithms + // were already cleared, so falling through would crash on _closeAlgorithm(). + if (stream._state === 'closed' || stream._state === 'errored') { + return promiseRejectedWith( + new TypeError('Cannot close a ' + stream._state + ' WritableStream'), + ); + } + var deferred = newDeferred(); + stream._closeRequest = deferred; + var writer = stream._writer; + if (writer !== undefined && stream._backpressure && stream._state === 'writable') { + writer._readyDeferred.resolve(undefined); + } + writableStreamDefaultControllerClose(stream._controller); + return deferred.promise; + } + + function writableStreamCloseQueuedOrInFlight(stream) { + return stream._closeRequest !== undefined || stream._inFlightCloseRequest !== undefined; + } + + function writableStreamAddWriteRequest(stream) { + var deferred = newDeferred(); + stream._writeRequests.push(deferred); + return deferred.promise; + } + + function writableStreamDealWithRejection(stream, error) { + if (stream._state === 'writable') { + writableStreamStartErroring(stream, error); + return; + } + writableStreamFinishErroring(stream); + } + + function writableStreamStartErroring(stream, reason) { + var controller = stream._controller; + stream._state = 'erroring'; + stream._storedError = reason; + var writer = stream._writer; + if (writer !== undefined) { + writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason); + } + if (!writableStreamHasOperationMarkedInFlight(stream) && controller._started) { + writableStreamFinishErroring(stream); + } + } + + function writableStreamFinishErroring(stream) { + stream._state = 'errored'; + stream._controller._errorSteps(); + var storedError = stream._storedError; + var requests = stream._writeRequests; + stream._writeRequests = []; + for (var i = 0; i < requests.length; i++) requests[i].reject(storedError); + if (stream._pendingAbortRequest === undefined) { + writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); + return; + } + var abortRequest = stream._pendingAbortRequest; + stream._pendingAbortRequest = undefined; + if (abortRequest.wasAlreadyErroring) { + abortRequest.deferred.reject(storedError); + writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); + return; + } + var promise = stream._controller._abortSteps(abortRequest.reason); + uponPromise( + promise, + function () { + abortRequest.deferred.resolve(undefined); + writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); + }, + function (reason) { + abortRequest.deferred.reject(reason); + writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); + }, + ); + } + + function writableStreamFinishInFlightWrite(stream) { + stream._inFlightWriteRequest.resolve(undefined); + stream._inFlightWriteRequest = undefined; + } + function writableStreamFinishInFlightWriteWithError(stream, error) { + stream._inFlightWriteRequest.reject(error); + stream._inFlightWriteRequest = undefined; + writableStreamDealWithRejection(stream, error); + } + function writableStreamFinishInFlightClose(stream) { + stream._inFlightCloseRequest.resolve(undefined); + stream._inFlightCloseRequest = undefined; + if (stream._state === 'erroring') { + stream._storedError = undefined; + if (stream._pendingAbortRequest !== undefined) { + stream._pendingAbortRequest.deferred.resolve(undefined); + stream._pendingAbortRequest = undefined; + } + } + stream._state = 'closed'; + var writer = stream._writer; + if (writer !== undefined) writer._closedDeferred.resolve(undefined); + } + function writableStreamFinishInFlightCloseWithError(stream, error) { + stream._inFlightCloseRequest.reject(error); + stream._inFlightCloseRequest = undefined; + if (stream._pendingAbortRequest !== undefined) { + stream._pendingAbortRequest.deferred.reject(error); + stream._pendingAbortRequest = undefined; + } + writableStreamDealWithRejection(stream, error); + } + + function writableStreamHasOperationMarkedInFlight(stream) { + return stream._inFlightWriteRequest !== undefined || stream._inFlightCloseRequest !== undefined; + } + function writableStreamMarkCloseRequestInFlight(stream) { + stream._inFlightCloseRequest = stream._closeRequest; + stream._closeRequest = undefined; + } + function writableStreamMarkFirstWriteRequestInFlight(stream) { + stream._inFlightWriteRequest = stream._writeRequests.shift(); + } + function writableStreamRejectCloseAndClosedPromiseIfNeeded(stream) { + if (stream._closeRequest !== undefined) { + stream._closeRequest.reject(stream._storedError); + stream._closeRequest = undefined; + } + var writer = stream._writer; + if (writer !== undefined) { + writer._closedDeferred.reject(stream._storedError); + setPromiseIsHandled(writer._closedDeferred.promise); + } + } + function writableStreamUpdateBackpressure(stream, backpressure) { + var writer = stream._writer; + if (writer !== undefined && backpressure !== stream._backpressure) { + if (backpressure) { + writer._readyDeferred = newDeferred(); + } else { + writer._readyDeferred.resolve(undefined); + } + } + stream._backpressure = backpressure; + } + + // ---- WritableStreamDefaultWriter ------------------------------------------- + + class WritableStreamDefaultWriter { + constructor(stream) { + if (!(stream instanceof WritableStream)) { + throw new TypeError('WritableStreamDefaultWriter requires a WritableStream'); + } + if (isWritableStreamLocked(stream)) { + throw new TypeError('WritableStream is already locked to a writer'); + } + this._ownerWritableStream = stream; + stream._writer = this; + this._readyDeferred = newDeferred(); + this._closedDeferred = newDeferred(); + var state = stream._state; + if (state === 'writable') { + if (!writableStreamCloseQueuedOrInFlight(stream) && stream._backpressure) { + // ready stays pending + } else { + this._readyDeferred.resolve(undefined); + } + } else if (state === 'erroring') { + this._readyDeferred.reject(stream._storedError); + setPromiseIsHandled(this._readyDeferred.promise); + } else if (state === 'closed') { + this._readyDeferred.resolve(undefined); + this._closedDeferred.resolve(undefined); + } else { + var storedError = stream._storedError; + this._readyDeferred.reject(storedError); + setPromiseIsHandled(this._readyDeferred.promise); + this._closedDeferred.reject(storedError); + setPromiseIsHandled(this._closedDeferred.promise); + } + } + + get closed() { + if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); + return this._closedDeferred.promise; + } + + get desiredSize() { + if (!this._ownerWritableStream) throw defaultWriterLockException(); + return writableStreamDefaultWriterGetDesiredSize(this); + } + + get ready() { + if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); + return this._readyDeferred.promise; + } + + abort(reason) { + if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); + return writableStreamAbort(this._ownerWritableStream, reason); + } + + close() { + var stream = this._ownerWritableStream; + if (!stream) return promiseRejectedWith(defaultWriterLockException()); + if (writableStreamCloseQueuedOrInFlight(stream)) { + return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); + } + return writableStreamClose(stream); + } + + releaseLock() { + var stream = this._ownerWritableStream; + if (!stream) return; + var releasedError = new TypeError( + 'Writer was released and can no longer be used to monitor the stream', + ); + writableStreamDefaultWriterEnsureReadyPromiseRejected(this, releasedError); + writableStreamDefaultWriterEnsureClosedPromiseRejected(this, releasedError); + stream._writer = undefined; + this._ownerWritableStream = undefined; + } + + write(chunk) { + if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); + return writableStreamDefaultWriterWrite(this, chunk); + } + } + + function defaultWriterLockException() { + return new TypeError('Cannot use a released writer'); + } + + function writableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) { + if (writer._closedDeferred._resolved) { + writer._closedDeferred = newDeferred(); + } + writer._closedDeferred.reject(error); + setPromiseIsHandled(writer._closedDeferred.promise); + } + function writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) { + writer._readyDeferred = newDeferred(); + writer._readyDeferred.reject(error); + setPromiseIsHandled(writer._readyDeferred.promise); + } + function writableStreamDefaultWriterGetDesiredSize(writer) { + var stream = writer._ownerWritableStream; + var state = stream._state; + if (state === 'errored' || state === 'erroring') return null; + if (state === 'closed') return 0; + return writableStreamDefaultControllerGetDesiredSize(stream._controller); + } + function writableStreamDefaultWriterWrite(writer, chunk) { + var stream = writer._ownerWritableStream; + var controller = stream._controller; + var chunkSize = writableStreamDefaultControllerGetChunkSize(controller, chunk); + if (stream !== writer._ownerWritableStream) { + return promiseRejectedWith(defaultWriterLockException()); + } + var state = stream._state; + if (state === 'errored') return promiseRejectedWith(stream._storedError); + if (writableStreamCloseQueuedOrInFlight(stream) || state === 'closed') { + return promiseRejectedWith(new TypeError('Cannot write to a closing/closed stream')); + } + if (state === 'erroring') return promiseRejectedWith(stream._storedError); + var promise = writableStreamAddWriteRequest(stream); + writableStreamDefaultControllerWrite(controller, chunk, chunkSize); + return promise; + } + + // ---- WritableStreamDefaultController --------------------------------------- + + class WritableStreamDefaultController { + constructor() { + throw new TypeError('Illegal constructor'); + } + + get signal() { + return undefined; // AbortController signal plumbing not exposed. + } + + error(e) { + if (this._controlledWritableStream._state !== 'writable') return; + writableStreamDefaultControllerError(this, e); + } + + _abortSteps(reason) { + var result = this._abortAlgorithm(reason); + writableStreamDefaultControllerClearAlgorithms(this); + return result; + } + + _errorSteps() { + resetQueue(this); + } + } + + function setUpWritableStreamDefaultController( + stream, + controller, + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + highWaterMark, + sizeAlgorithm, + ) { + controller._controlledWritableStream = stream; + stream._controller = controller; + resetQueue(controller); + controller._started = false; + controller._strategySizeAlgorithm = sizeAlgorithm; + controller._strategyHWM = highWaterMark; + controller._writeAlgorithm = writeAlgorithm; + controller._closeAlgorithm = closeAlgorithm; + controller._abortAlgorithm = abortAlgorithm; + var backpressure = writableStreamDefaultControllerGetBackpressure(controller); + writableStreamUpdateBackpressure(stream, backpressure); + + var startResult = startAlgorithm(); + uponPromise( + promiseResolvedWith(startResult), + function () { + controller._started = true; + writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); + }, + function (r) { + controller._started = true; + writableStreamDealWithRejection(stream, r); + }, + ); + } + + function setUpWritableStreamDefaultControllerFromUnderlyingSink( + stream, + underlyingSink, + highWaterMark, + sizeAlgorithm, + ) { + var controller = Object.create(WritableStreamDefaultController.prototype); + var startAlgorithm = noop; + var writeAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + var closeAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + var abortAlgorithm = function () { + return promiseResolvedWith(undefined); + }; + if (typeof underlyingSink.start === 'function') { + startAlgorithm = function () { + return promiseFrom(function () { + return underlyingSink.start(controller); + }); + }; + } + if (typeof underlyingSink.write === 'function') { + writeAlgorithm = function (chunk) { + return promiseFrom(function () { + return underlyingSink.write(chunk, controller); + }); + }; + } + if (typeof underlyingSink.close === 'function') { + closeAlgorithm = function () { + return promiseFrom(function () { + return underlyingSink.close(); + }); + }; + } + if (typeof underlyingSink.abort === 'function') { + abortAlgorithm = function (reason) { + return promiseFrom(function () { + return underlyingSink.abort(reason); + }); + }; + } + setUpWritableStreamDefaultController( + stream, + controller, + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + highWaterMark, + sizeAlgorithm, + ); + } + + var CLOSE_SENTINEL = {}; + + function writableStreamDefaultControllerClearAlgorithms(controller) { + controller._writeAlgorithm = undefined; + controller._closeAlgorithm = undefined; + controller._abortAlgorithm = undefined; + controller._strategySizeAlgorithm = undefined; + } + function writableStreamDefaultControllerClose(controller) { + enqueueValueWithSize(controller, CLOSE_SENTINEL, 0); + writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); + } + function writableStreamDefaultControllerGetChunkSize(controller, chunk) { + try { + return controller._strategySizeAlgorithm(chunk); + } catch (e) { + writableStreamDefaultControllerErrorIfNeeded(controller, e); + return 1; + } + } + function writableStreamDefaultControllerGetDesiredSize(controller) { + return controller._strategyHWM - controller._queueTotalSize; + } + function writableStreamDefaultControllerWrite(controller, chunk, chunkSize) { + try { + enqueueValueWithSize(controller, chunk, chunkSize); + } catch (e) { + writableStreamDefaultControllerErrorIfNeeded(controller, e); + return; + } + var stream = controller._controlledWritableStream; + if (!writableStreamCloseQueuedOrInFlight(stream) && stream._state === 'writable') { + var backpressure = writableStreamDefaultControllerGetBackpressure(controller); + writableStreamUpdateBackpressure(stream, backpressure); + } + writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); + } + function writableStreamDefaultControllerAdvanceQueueIfNeeded(controller) { + var stream = controller._controlledWritableStream; + if (!controller._started) return; + if (stream._inFlightWriteRequest !== undefined) return; + var state = stream._state; + if (state === 'erroring') { + writableStreamFinishErroring(stream); + return; + } + if (controller._queue.length === 0) return; + var value = peekQueueValue(controller); + if (value === CLOSE_SENTINEL) { + writableStreamDefaultControllerProcessClose(controller); + } else { + writableStreamDefaultControllerProcessWrite(controller, value); + } + } + function writableStreamDefaultControllerErrorIfNeeded(controller, error) { + if (controller._controlledWritableStream._state === 'writable') { + writableStreamDefaultControllerError(controller, error); + } + } + function writableStreamDefaultControllerProcessClose(controller) { + var stream = controller._controlledWritableStream; + writableStreamMarkCloseRequestInFlight(stream); + dequeueValue(controller); + var sinkClosePromise = controller._closeAlgorithm(); + writableStreamDefaultControllerClearAlgorithms(controller); + uponPromise( + sinkClosePromise, + function () { + writableStreamFinishInFlightClose(stream); + }, + function (reason) { + writableStreamFinishInFlightCloseWithError(stream, reason); + }, + ); + } + function writableStreamDefaultControllerProcessWrite(controller, chunk) { + var stream = controller._controlledWritableStream; + writableStreamMarkFirstWriteRequestInFlight(stream); + var sinkWritePromise = controller._writeAlgorithm(chunk); + uponPromise( + sinkWritePromise, + function () { + writableStreamFinishInFlightWrite(stream); + var state = stream._state; + dequeueValue(controller); + if (!writableStreamCloseQueuedOrInFlight(stream) && state === 'writable') { + var backpressure = writableStreamDefaultControllerGetBackpressure(controller); + writableStreamUpdateBackpressure(stream, backpressure); + } + writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); + }, + function (reason) { + if (stream._state === 'writable') { + writableStreamDefaultControllerClearAlgorithms(controller); + } + writableStreamFinishInFlightWriteWithError(stream, reason); + }, + ); + } + function writableStreamDefaultControllerGetBackpressure(controller) { + return writableStreamDefaultControllerGetDesiredSize(controller) <= 0; + } + function writableStreamDefaultControllerError(controller, error) { + var stream = controller._controlledWritableStream; + writableStreamDefaultControllerClearAlgorithms(controller); + writableStreamStartErroring(stream, error); + } + + function writableStreamDefaultWriterRelease(writer) { + if (writer._ownerWritableStream) writer.releaseLock(); + } + function writableStreamDefaultWriterCloseWithErrorPropagation(writer) { + var stream = writer._ownerWritableStream; + var state = stream._state; + if (writableStreamCloseQueuedOrInFlight(stream) || state === 'closed') { + return promiseResolvedWith(undefined); + } + if (state === 'errored') return promiseRejectedWith(stream._storedError); + return writableStreamClose(stream); + } + + function createWritableStream( + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + highWaterMark, + sizeAlgorithm, + ) { + var stream = Object.create(WritableStream.prototype); + stream._state = 'writable'; + stream._storedError = undefined; + stream._writer = undefined; + stream._controller = undefined; + stream._inFlightWriteRequest = undefined; + stream._closeRequest = undefined; + stream._inFlightCloseRequest = undefined; + stream._pendingAbortRequest = undefined; + stream._writeRequests = []; + stream._backpressure = false; + var controller = Object.create(WritableStreamDefaultController.prototype); + setUpWritableStreamDefaultController( + stream, + controller, + startAlgorithm, + writeAlgorithm, + closeAlgorithm, + abortAlgorithm, + highWaterMark, + sizeAlgorithm, + ); + return stream; + } + + module.exports = { + WritableStream: WritableStream, + WritableStreamDefaultWriter: WritableStreamDefaultWriter, + WritableStreamDefaultController: WritableStreamDefaultController, + isWritableStreamLocked: isWritableStreamLocked, + writableStreamAbort: writableStreamAbort, + writableStreamCloseQueuedOrInFlight: writableStreamCloseQueuedOrInFlight, + writableStreamDefaultWriterCloseWithErrorPropagation: + writableStreamDefaultWriterCloseWithErrorPropagation, + writableStreamDefaultWriterRelease: writableStreamDefaultWriterRelease, + writableStreamDefaultWriterWrite: writableStreamDefaultWriterWrite, + createWritableStream: createWritableStream, + writableStreamDefaultControllerErrorIfNeeded: writableStreamDefaultControllerErrorIfNeeded, + }; +}); diff --git a/pkg/runtime/js/node/stream/web.js b/pkg/runtime/js/node/stream/web.js index 82b80f2..621bc57 100644 --- a/pkg/runtime/js/node/stream/web.js +++ b/pkg/runtime/js/node/stream/web.js @@ -15,1964 +15,29 @@ // ReadableStreamBYOBReader exists as a constructor that reports the same. Text // encoder/decoder streams and (de)compression streams are intentionally not // provided here. +// +// The implementation lives in js/internal/webstreams/, laid out like node's +// lib/internal/webstreams (util, queuingstrategies, readablestream, writablestream, +// transformstream), so a stack frame names the file node would. This module only +// assembles the public export object and installs the globals. (function (require, module, _exports) { 'use strict'; - // ---- microtask + promise helpers ------------------------------------------- - - // queueMicrotask exists as a global (microtasks.js), but fall back to a - // resolved-promise continuation so this module is usable even if it is loaded - // before that global appears. - var enqueueMicrotask = - typeof queueMicrotask === 'function' - ? queueMicrotask - : function (cb) { - Promise.resolve().then(cb); - }; - - function noop() {} - - function newPromise(executor) { - return new Promise(executor); - } - // Invoke a method (preserving `this` via the caller's closure) and turn a - // synchronous throw into a rejected promise, so an underlying source/sink/ - // transformer callback that throws behaves like one that returns a rejected - // promise (spec algorithms always settle, never throw out of the controller). - function promiseFrom(thunk) { - try { - return promiseResolvedWith(thunk()); - } catch (e) { - return promiseRejectedWith(e); - } - } - function promiseResolvedWith(value) { - return Promise.resolve(value); - } - function promiseRejectedWith(reason) { - return Promise.reject(reason); - } - // A deferred: { promise, resolve, reject }. Used for reader/writer closed and - // writer ready promises, which settle from elsewhere in the algorithm. - function newDeferred() { - var d = {}; - d.promise = new Promise(function (resolve, reject) { - d.resolve = resolve; - d.reject = reject; - }); - return d; - } - // Attach reactions without creating an unhandled-rejection if `promise` rejects - // and only onRejected is registered elsewhere later — matches the spec's - // PerformPromiseThen, which never surfaces an unhandled rejection. - function performPromiseThen(promise, onFulfilled, onRejected) { - promise.then(onFulfilled || undefined, onRejected || undefined); - } - function uponPromise(promise, onFulfilled, onRejected) { - performPromiseThen(promise, onFulfilled, onRejected); - } - function setPromiseIsHandled(promise) { - promise.then(undefined, noop); - } - - // ---- generic queue (spec "queue-with-sizes") ------------------------------- - - // A FIFO queue with amortized O(1) push/shift. A plain array's shift() is O(n) - // — it reindexes every remaining element — so draining N queued chunks costs - // O(n^2). That bites a byte-sized response body or a pipe that arrives as many - // small chunks: the queue can hold thousands of them before a reader drains - // (the high-water mark bounds queued *bytes*, not chunk count). Keeping a head - // cursor and only compacting once the consumed prefix dominates the backing - // array keeps push/shift/peek/length O(1) amortized while bounding wasted space - // to ~2x the live length. - class SimpleQueue { - constructor() { - this._items = []; - this._head = 0; - } - get length() { - return this._items.length - this._head; - } - push(item) { - this._items.push(item); - } - shift() { - var item = this._items[this._head]; - this._head++; - if (this._head > 64 && this._head * 2 >= this._items.length) { - this._items = this._items.slice(this._head); - this._head = 0; - } - return item; - } - peek() { - return this._items[this._head]; - } - } - - function resetQueue(container) { - container._queue = new SimpleQueue(); - container._queueTotalSize = 0; - } - function dequeueValue(container) { - var pair = container._queue.shift(); - container._queueTotalSize -= pair.size; - if (container._queueTotalSize < 0) container._queueTotalSize = 0; - return pair.value; - } - function enqueueValueWithSize(container, value, size) { - if (typeof size !== 'number' || size !== size || size === Infinity || size < 0) { - throw new RangeError('Invalid chunk size'); - } - container._queue.push({ value: value, size: size }); - container._queueTotalSize += size; - } - function peekQueueValue(container) { - return container._queue.peek().value; - } - - // ---- queuing strategies ---------------------------------------------------- - - function extractHighWaterMark(strategy, defaultHWM) { - if (!strategy || strategy.highWaterMark === undefined) return defaultHWM; - var hwm = Number(strategy.highWaterMark); - if (hwm !== hwm || hwm < 0) throw new RangeError('Invalid highWaterMark'); - return hwm; - } - function extractSizeAlgorithm(strategy) { - if (!strategy || strategy.size === undefined) { - return function () { - return 1; - }; - } - var size = strategy.size; - if (typeof size !== 'function') throw new TypeError('strategy.size must be a function'); - return function (chunk) { - return Number(size(chunk)); - }; - } - - class ByteLengthQueuingStrategy { - constructor(options) { - if (!options || options.highWaterMark === undefined) { - throw new TypeError('ByteLengthQueuingStrategy requires a highWaterMark option'); - } - this._highWaterMark = Number(options.highWaterMark); - } - get highWaterMark() { - return this._highWaterMark; - } - get size() { - // Per spec this is a fresh function bound to no stream; chunk.byteLength. - return function (chunk) { - return chunk.byteLength; - }; - } - } - - class CountQueuingStrategy { - constructor(options) { - if (!options || options.highWaterMark === undefined) { - throw new TypeError('CountQueuingStrategy requires a highWaterMark option'); - } - this._highWaterMark = Number(options.highWaterMark); - } - get highWaterMark() { - return this._highWaterMark; - } - get size() { - return function () { - return 1; - }; - } - } - - // ============================================================================ - // ReadableStream - // ============================================================================ - - class ReadableStream { - constructor(underlyingSource, strategy) { - underlyingSource = underlyingSource === undefined ? {} : underlyingSource; - strategy = strategy === undefined ? {} : strategy; - - this._state = 'readable'; - this._storedError = undefined; - this._reader = undefined; - this._disturbed = false; - this._controller = undefined; - - // Byte sources (BYOB) are deferred. Keep the rejection explicit rather than - // silently treating a byte stream as a default stream. - if (underlyingSource.type !== undefined) { - if (String(underlyingSource.type) === 'bytes') { - throw new TypeError( - "ReadableStream byte sources (underlyingSource.type 'bytes') are not " + - 'supported in Lava yet', - ); - } - throw new RangeError('Invalid underlyingSource.type'); - } - - var highWaterMark = extractHighWaterMark(strategy, 1); - var sizeAlgorithm = extractSizeAlgorithm(strategy); - setUpReadableStreamDefaultControllerFromUnderlyingSource( - this, - underlyingSource, - highWaterMark, - sizeAlgorithm, - ); - } - - get locked() { - return isReadableStreamLocked(this); - } - - cancel(reason) { - if (isReadableStreamLocked(this)) { - return promiseRejectedWith(new TypeError('Cannot cancel a locked stream')); - } - return readableStreamCancel(this, reason); - } - - getReader(options) { - if (options && options.mode !== undefined) { - if (String(options.mode) === 'byob') { - throw new TypeError( - 'BYOB readers are not supported in Lava yet (this stream is not a byte stream)', - ); - } - throw new TypeError('Invalid reader mode'); - } - return new ReadableStreamDefaultReader(this); - } - - pipeThrough(transform, options) { - if (!transform || !(transform.writable instanceof WritableStream)) { - throw new TypeError('pipeThrough requires a { writable, readable } pair'); - } - if (!(transform.readable instanceof ReadableStream)) { - throw new TypeError('pipeThrough requires a { writable, readable } pair'); - } - if (isReadableStreamLocked(this)) { - throw new TypeError('Cannot pipe a locked stream'); - } - if (isWritableStreamLocked(transform.writable)) { - throw new TypeError('Cannot pipe to a locked writable'); - } - options = options || {}; - var promise = readableStreamPipeTo( - this, - transform.writable, - !!options.preventClose, - !!options.preventAbort, - !!options.preventCancel, - options.signal, - ); - setPromiseIsHandled(promise); - return transform.readable; - } - - pipeTo(destination, options) { - if (!(destination instanceof WritableStream)) { - return promiseRejectedWith(new TypeError('pipeTo requires a WritableStream destination')); - } - if (isReadableStreamLocked(this)) { - return promiseRejectedWith(new TypeError('Cannot pipe a locked stream')); - } - if (isWritableStreamLocked(destination)) { - return promiseRejectedWith(new TypeError('Cannot pipe to a locked writable')); - } - options = options || {}; - return readableStreamPipeTo( - this, - destination, - !!options.preventClose, - !!options.preventAbort, - !!options.preventCancel, - options.signal, - ); - } - - tee() { - return readableStreamDefaultTee(this); - } - - values(options) { - return acquireReadableStreamAsyncIterator(this, options && options.preventCancel); - } - - [Symbol.asyncIterator](options) { - return acquireReadableStreamAsyncIterator(this, options && options.preventCancel); - } - } - - function isReadableStreamLocked(stream) { - return stream._reader !== undefined; - } - - function readableStreamCancel(stream, reason) { - stream._disturbed = true; - if (stream._state === 'closed') return promiseResolvedWith(undefined); - if (stream._state === 'errored') return promiseRejectedWith(stream._storedError); - readableStreamClose(stream); - var sourceCancelPromise = stream._controller._cancelSteps(reason); - return sourceCancelPromise.then(noop); - } - - function readableStreamClose(stream) { - stream._state = 'closed'; - var reader = stream._reader; - if (reader === undefined) return; - reader._closedDeferred.resolve(undefined); - if (reader._readRequests !== undefined) { - var requests = reader._readRequests; - reader._readRequests = []; - for (var i = 0; i < requests.length; i++) requests[i].closeSteps(); - } - } - - function readableStreamError(stream, e) { - stream._state = 'errored'; - stream._storedError = e; - var reader = stream._reader; - if (reader === undefined) return; - reader._closedDeferred.reject(e); - setPromiseIsHandled(reader._closedDeferred.promise); - if (reader._readRequests !== undefined) { - var requests = reader._readRequests; - reader._readRequests = []; - for (var i = 0; i < requests.length; i++) requests[i].errorSteps(e); - } - } - - function readableStreamAddReadRequest(stream, readRequest) { - stream._reader._readRequests.push(readRequest); - } - function readableStreamFulfillReadRequest(stream, chunk, done) { - var readRequest = stream._reader._readRequests.shift(); - if (done) readRequest.closeSteps(); - else readRequest.chunkSteps(chunk); - } - function readableStreamGetNumReadRequests(stream) { - return stream._reader._readRequests.length; - } - - // ---- ReadableStreamDefaultReader ------------------------------------------- - - class ReadableStreamDefaultReader { - constructor(stream) { - if (!(stream instanceof ReadableStream)) { - throw new TypeError('ReadableStreamDefaultReader requires a ReadableStream'); - } - if (isReadableStreamLocked(stream)) { - throw new TypeError('ReadableStream is already locked to a reader'); - } - readableStreamReaderGenericInitialize(this, stream); - this._readRequests = []; - } - - get closed() { - if (!this._ownerReadableStream) { - return promiseRejectedWith(readerLockException('closed')); - } - return this._closedDeferred.promise; - } - - read() { - if (!this._ownerReadableStream) { - return promiseRejectedWith(readerLockException('read from')); - } - var deferred = newDeferred(); - var readRequest = { - chunkSteps: function (chunk) { - deferred.resolve({ value: chunk, done: false }); - }, - closeSteps: function () { - deferred.resolve({ value: undefined, done: true }); - }, - errorSteps: function (e) { - deferred.reject(e); - }, - }; - readableStreamDefaultReaderRead(this, readRequest); - return deferred.promise; - } - - releaseLock() { - if (!this._ownerReadableStream) return; - readableStreamDefaultReaderRelease(this); - } - - cancel(reason) { - if (!this._ownerReadableStream) { - return promiseRejectedWith(readerLockException('cancel')); - } - return readableStreamReaderGenericCancel(this, reason); - } - } - - function readerLockException(name) { - return new TypeError('Cannot ' + name + ' a stream using a released reader'); - } - - function readableStreamReaderGenericInitialize(reader, stream) { - reader._ownerReadableStream = stream; - stream._reader = reader; - reader._closedDeferred = newDeferred(); - if (stream._state === 'readable') { - // pending - } else if (stream._state === 'closed') { - reader._closedDeferred.resolve(undefined); - } else { - reader._closedDeferred.reject(stream._storedError); - setPromiseIsHandled(reader._closedDeferred.promise); - } - } - - function readableStreamReaderGenericCancel(reader, reason) { - return readableStreamCancel(reader._ownerReadableStream, reason); - } - - function readableStreamReaderGenericRelease(reader) { - var stream = reader._ownerReadableStream; - if (stream._state === 'readable') { - reader._closedDeferred.reject(readerLockException('release a reader on')); - } else { - reader._closedDeferred = newDeferred(); - reader._closedDeferred.reject(readerLockException('release a reader on')); - } - setPromiseIsHandled(reader._closedDeferred.promise); - stream._controller._releaseSteps(); - stream._reader = undefined; - reader._ownerReadableStream = undefined; - } - - function readableStreamDefaultReaderRelease(reader) { - readableStreamReaderGenericRelease(reader); - var e = readerLockException('read from'); - var requests = reader._readRequests; - reader._readRequests = []; - for (var i = 0; i < requests.length; i++) requests[i].errorSteps(e); - } - - function readableStreamDefaultReaderRead(reader, readRequest) { - var stream = reader._ownerReadableStream; - stream._disturbed = true; - if (stream._state === 'closed') { - readRequest.closeSteps(); - } else if (stream._state === 'errored') { - readRequest.errorSteps(stream._storedError); - } else { - stream._controller._pullSteps(readRequest); - } - } - - function acquireReadableStreamDefaultReader(stream) { - return new ReadableStreamDefaultReader(stream); - } - - // ---- ReadableStreamDefaultController --------------------------------------- - - class ReadableStreamDefaultController { - constructor() { - throw new TypeError('Illegal constructor'); - } - - get desiredSize() { - return readableStreamDefaultControllerGetDesiredSize(this); - } - - close() { - if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) { - throw new TypeError('Cannot close a stream that is already closing or not readable'); - } - readableStreamDefaultControllerClose(this); - } - - enqueue(chunk) { - if (!readableStreamDefaultControllerCanCloseOrEnqueue(this)) { - throw new TypeError('Cannot enqueue a chunk into a stream that is closing or not readable'); - } - readableStreamDefaultControllerEnqueue(this, chunk); - } - - error(e) { - readableStreamDefaultControllerError(this, e); - } - - // ---- abstract control "steps" called by the stream/reader ---- - - _cancelSteps(reason) { - resetQueue(this); - var result = this._cancelAlgorithm(reason); - readableStreamDefaultControllerClearAlgorithms(this); - return result; - } - - _pullSteps(readRequest) { - var stream = this._controlledReadableStream; - if (this._queue.length > 0) { - var chunk = dequeueValue(this); - if (this._closeRequested && this._queue.length === 0) { - readableStreamDefaultControllerClearAlgorithms(this); - readableStreamClose(stream); - } else { - readableStreamDefaultControllerCallPullIfNeeded(this); - } - readRequest.chunkSteps(chunk); - } else { - readableStreamAddReadRequest(stream, readRequest); - readableStreamDefaultControllerCallPullIfNeeded(this); - } - } - - _releaseSteps() { - // Default controller keeps its queue across reader release. - } - } - - function readableStreamDefaultControllerCallPullIfNeeded(controller) { - if (!readableStreamDefaultControllerShouldCallPull(controller)) return; - if (controller._pulling) { - controller._pullAgain = true; - return; - } - controller._pulling = true; - var pullPromise = controller._pullAlgorithm(); - uponPromise( - pullPromise, - function () { - controller._pulling = false; - if (controller._pullAgain) { - controller._pullAgain = false; - readableStreamDefaultControllerCallPullIfNeeded(controller); - } - }, - function (e) { - readableStreamDefaultControllerError(controller, e); - }, - ); - } - - function readableStreamDefaultControllerShouldCallPull(controller) { - var stream = controller._controlledReadableStream; - if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return false; - if (!controller._started) return false; - if (isReadableStreamLocked(stream) && readableStreamGetNumReadRequests(stream) > 0) return true; - return readableStreamDefaultControllerGetDesiredSize(controller) > 0; - } - - function readableStreamDefaultControllerClearAlgorithms(controller) { - controller._pullAlgorithm = undefined; - controller._cancelAlgorithm = undefined; - controller._strategySizeAlgorithm = undefined; - } - - function readableStreamDefaultControllerClose(controller) { - if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return; - var stream = controller._controlledReadableStream; - controller._closeRequested = true; - if (controller._queue.length === 0) { - readableStreamDefaultControllerClearAlgorithms(controller); - readableStreamClose(stream); - } - } - - function readableStreamDefaultControllerEnqueue(controller, chunk) { - if (!readableStreamDefaultControllerCanCloseOrEnqueue(controller)) return; - var stream = controller._controlledReadableStream; - if (isReadableStreamLocked(stream) && readableStreamGetNumReadRequests(stream) > 0) { - readableStreamFulfillReadRequest(stream, chunk, false); - } else { - var chunkSize; - try { - chunkSize = controller._strategySizeAlgorithm(chunk); - } catch (e) { - readableStreamDefaultControllerError(controller, e); - throw e; - } - try { - enqueueValueWithSize(controller, chunk, chunkSize); - } catch (e) { - readableStreamDefaultControllerError(controller, e); - throw e; - } - } - readableStreamDefaultControllerCallPullIfNeeded(controller); - } - - function readableStreamDefaultControllerError(controller, e) { - var stream = controller._controlledReadableStream; - if (stream._state !== 'readable') return; - resetQueue(controller); - readableStreamDefaultControllerClearAlgorithms(controller); - readableStreamError(stream, e); - } - - function readableStreamDefaultControllerGetDesiredSize(controller) { - var state = controller._controlledReadableStream._state; - if (state === 'errored') return null; - if (state === 'closed') return 0; - return controller._strategyHWM - controller._queueTotalSize; - } - - function readableStreamDefaultControllerCanCloseOrEnqueue(controller) { - return ( - !controller._closeRequested && controller._controlledReadableStream._state === 'readable' - ); - } - - function setUpReadableStreamDefaultController( - stream, - controller, - startAlgorithm, - pullAlgorithm, - cancelAlgorithm, - highWaterMark, - sizeAlgorithm, - ) { - controller._controlledReadableStream = stream; - resetQueue(controller); - controller._started = false; - controller._closeRequested = false; - controller._pullAgain = false; - controller._pulling = false; - controller._strategySizeAlgorithm = sizeAlgorithm; - controller._strategyHWM = highWaterMark; - controller._pullAlgorithm = pullAlgorithm; - controller._cancelAlgorithm = cancelAlgorithm; - stream._controller = controller; - - var startResult = startAlgorithm(); - uponPromise( - promiseResolvedWith(startResult), - function () { - controller._started = true; - readableStreamDefaultControllerCallPullIfNeeded(controller); - }, - function (r) { - readableStreamDefaultControllerError(controller, r); - }, - ); - } - - function setUpReadableStreamDefaultControllerFromUnderlyingSource( - stream, - underlyingSource, - highWaterMark, - sizeAlgorithm, - ) { - var controller = Object.create(ReadableStreamDefaultController.prototype); - var startAlgorithm = noop; - var pullAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - var cancelAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - if (typeof underlyingSource.start === 'function') { - startAlgorithm = function () { - return promiseFrom(function () { - return underlyingSource.start(controller); - }); - }; - } - if (typeof underlyingSource.pull === 'function') { - pullAlgorithm = function () { - return promiseFrom(function () { - return underlyingSource.pull(controller); - }); - }; - } - if (typeof underlyingSource.cancel === 'function') { - cancelAlgorithm = function (reason) { - return promiseFrom(function () { - return underlyingSource.cancel(reason); - }); - }; - } - setUpReadableStreamDefaultController( - stream, - controller, - startAlgorithm, - pullAlgorithm, - cancelAlgorithm, - highWaterMark, - sizeAlgorithm, - ); - } - - // createReadableStream builds a stream from internal algorithms (used by tee). - function createReadableStream( - startAlgorithm, - pullAlgorithm, - cancelAlgorithm, - highWaterMark, - sizeAlgorithm, - ) { - var stream = Object.create(ReadableStream.prototype); - stream._state = 'readable'; - stream._storedError = undefined; - stream._reader = undefined; - stream._disturbed = false; - stream._controller = undefined; - var controller = Object.create(ReadableStreamDefaultController.prototype); - setUpReadableStreamDefaultController( - stream, - controller, - startAlgorithm, - pullAlgorithm, - cancelAlgorithm, - highWaterMark === undefined ? 1 : highWaterMark, - sizeAlgorithm || - function () { - return 1; - }, - ); - return stream; - } - - // ---- tee (default) --------------------------------------------------------- - - function readableStreamDefaultTee(stream) { - var reader = acquireReadableStreamDefaultReader(stream); - var reading = false; - var readAgainForBranch1 = false; - var readAgainForBranch2 = false; - var canceled1 = false; - var canceled2 = false; - var reason1, reason2; - var branch1, branch2; - var cancelDeferred = newDeferred(); - - function pullAlgorithm() { - if (reading) { - readAgainForBranch1 = true; - readAgainForBranch2 = true; - return promiseResolvedWith(undefined); - } - reading = true; - var readRequest = { - chunkSteps: function (chunk) { - enqueueMicrotask(function () { - readAgainForBranch1 = false; - readAgainForBranch2 = false; - if (!canceled1) readableStreamDefaultControllerEnqueue(branch1._controller, chunk); - if (!canceled2) readableStreamDefaultControllerEnqueue(branch2._controller, chunk); - reading = false; - if (readAgainForBranch1) pullAlgorithm(); - else if (readAgainForBranch2) pullAlgorithm(); - }); - }, - closeSteps: function () { - reading = false; - if (!canceled1) readableStreamDefaultControllerClose(branch1._controller); - if (!canceled2) readableStreamDefaultControllerClose(branch2._controller); - if (!canceled1 || !canceled2) cancelDeferred.resolve(undefined); - }, - errorSteps: function () { - reading = false; - }, - }; - readableStreamDefaultReaderRead(reader, readRequest); - return promiseResolvedWith(undefined); - } - - function cancel1Algorithm(reason) { - canceled1 = true; - reason1 = reason; - if (canceled2) { - var composite = [reason1, reason2]; - var cancelResult = readableStreamCancel(stream, composite); - cancelDeferred.resolve(cancelResult); - } - return cancelDeferred.promise; - } - function cancel2Algorithm(reason) { - canceled2 = true; - reason2 = reason; - if (canceled1) { - var composite = [reason1, reason2]; - var cancelResult = readableStreamCancel(stream, composite); - cancelDeferred.resolve(cancelResult); - } - return cancelDeferred.promise; - } - - branch1 = createReadableStream(noop, pullAlgorithm, cancel1Algorithm); - branch2 = createReadableStream(noop, pullAlgorithm, cancel2Algorithm); - - uponPromise(reader._closedDeferred.promise, undefined, function (e) { - readableStreamDefaultControllerError(branch1._controller, e); - readableStreamDefaultControllerError(branch2._controller, e); - if (!canceled1 || !canceled2) cancelDeferred.resolve(undefined); - }); - - return [branch1, branch2]; - } - - // ---- async iterator -------------------------------------------------------- - - function acquireReadableStreamAsyncIterator(stream, preventCancel) { - var reader = acquireReadableStreamDefaultReader(stream); - var iterator = Object.create(ReadableStreamAsyncIteratorPrototype); - iterator._reader = reader; - iterator._preventCancel = !!preventCancel; - return iterator; - } - - var ReadableStreamAsyncIteratorPrototype = { - next: function () { - var reader = this._reader; - if (!reader._ownerReadableStream) { - return promiseRejectedWith(readerLockException('iterate')); - } - var deferred = newDeferred(); - var readRequest = { - chunkSteps: function (chunk) { - deferred.resolve({ value: chunk, done: false }); - }, - closeSteps: function () { - readableStreamDefaultReaderRelease(reader); - deferred.resolve({ value: undefined, done: true }); - }, - errorSteps: function (e) { - readableStreamDefaultReaderRelease(reader); - deferred.reject(e); - }, - }; - readableStreamDefaultReaderRead(reader, readRequest); - return deferred.promise; - }, - return: function (value) { - var reader = this._reader; - if (!reader._ownerReadableStream) { - return promiseResolvedWith({ value: value, done: true }); - } - if (reader._readRequests.length > 0) { - return promiseRejectedWith( - new TypeError('Tried to release a reader lock when that reader has pending read calls'), - ); - } - if (!this._preventCancel) { - var result = readableStreamReaderGenericCancel(reader, value); - readableStreamDefaultReaderRelease(reader); - return result.then(function () { - return { value: value, done: true }; - }); - } - readableStreamDefaultReaderRelease(reader); - return promiseResolvedWith({ value: value, done: true }); - }, - }; - ReadableStreamAsyncIteratorPrototype[Symbol.asyncIterator] = function () { - return this; - }; - - // ============================================================================ - // WritableStream - // ============================================================================ - - class WritableStream { - constructor(underlyingSink, strategy) { - underlyingSink = underlyingSink === undefined ? {} : underlyingSink; - strategy = strategy === undefined ? {} : strategy; - if (underlyingSink.type !== undefined) { - throw new RangeError('Invalid underlyingSink.type'); - } - - this._state = 'writable'; - this._storedError = undefined; - this._writer = undefined; - this._controller = undefined; - this._inFlightWriteRequest = undefined; - this._closeRequest = undefined; - this._inFlightCloseRequest = undefined; - this._pendingAbortRequest = undefined; - this._writeRequests = []; - this._backpressure = false; - - var highWaterMark = extractHighWaterMark(strategy, 1); - var sizeAlgorithm = extractSizeAlgorithm(strategy); - setUpWritableStreamDefaultControllerFromUnderlyingSink( - this, - underlyingSink, - highWaterMark, - sizeAlgorithm, - ); - } - - get locked() { - return isWritableStreamLocked(this); - } - - abort(reason) { - if (isWritableStreamLocked(this)) { - return promiseRejectedWith(new TypeError('Cannot abort a locked stream')); - } - return writableStreamAbort(this, reason); - } - - close() { - if (isWritableStreamLocked(this)) { - return promiseRejectedWith(new TypeError('Cannot close a locked stream')); - } - if (writableStreamCloseQueuedOrInFlight(this)) { - return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); - } - return writableStreamClose(this); - } - - getWriter() { - return new WritableStreamDefaultWriter(this); - } - } - - function isWritableStreamLocked(stream) { - return stream._writer !== undefined; - } - - function writableStreamAbort(stream, reason) { - if (stream._state === 'closed' || stream._state === 'errored') { - return promiseResolvedWith(undefined); - } - if (stream._pendingAbortRequest !== undefined) { - return stream._pendingAbortRequest.deferred.promise; - } - var wasAlreadyErroring = false; - if (stream._state === 'erroring') { - wasAlreadyErroring = true; - reason = undefined; - } - var deferred = newDeferred(); - stream._pendingAbortRequest = { - deferred: deferred, - reason: reason, - wasAlreadyErroring: wasAlreadyErroring, - }; - if (!wasAlreadyErroring) writableStreamStartErroring(stream, reason); - return deferred.promise; - } - - function writableStreamClose(stream) { - // Spec step 2: a closed/errored stream rejects — its close/write algorithms - // were already cleared, so falling through would crash on _closeAlgorithm(). - if (stream._state === 'closed' || stream._state === 'errored') { - return promiseRejectedWith( - new TypeError('Cannot close a ' + stream._state + ' WritableStream'), - ); - } - var deferred = newDeferred(); - stream._closeRequest = deferred; - var writer = stream._writer; - if (writer !== undefined && stream._backpressure && stream._state === 'writable') { - writer._readyDeferred.resolve(undefined); - } - writableStreamDefaultControllerClose(stream._controller); - return deferred.promise; - } - - function writableStreamCloseQueuedOrInFlight(stream) { - return stream._closeRequest !== undefined || stream._inFlightCloseRequest !== undefined; - } - - function writableStreamAddWriteRequest(stream) { - var deferred = newDeferred(); - stream._writeRequests.push(deferred); - return deferred.promise; - } - - function writableStreamDealWithRejection(stream, error) { - if (stream._state === 'writable') { - writableStreamStartErroring(stream, error); - return; - } - writableStreamFinishErroring(stream); - } - - function writableStreamStartErroring(stream, reason) { - var controller = stream._controller; - stream._state = 'erroring'; - stream._storedError = reason; - var writer = stream._writer; - if (writer !== undefined) { - writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason); - } - if (!writableStreamHasOperationMarkedInFlight(stream) && controller._started) { - writableStreamFinishErroring(stream); - } - } - - function writableStreamFinishErroring(stream) { - stream._state = 'errored'; - stream._controller._errorSteps(); - var storedError = stream._storedError; - var requests = stream._writeRequests; - stream._writeRequests = []; - for (var i = 0; i < requests.length; i++) requests[i].reject(storedError); - if (stream._pendingAbortRequest === undefined) { - writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); - return; - } - var abortRequest = stream._pendingAbortRequest; - stream._pendingAbortRequest = undefined; - if (abortRequest.wasAlreadyErroring) { - abortRequest.deferred.reject(storedError); - writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); - return; - } - var promise = stream._controller._abortSteps(abortRequest.reason); - uponPromise( - promise, - function () { - abortRequest.deferred.resolve(undefined); - writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); - }, - function (reason) { - abortRequest.deferred.reject(reason); - writableStreamRejectCloseAndClosedPromiseIfNeeded(stream); - }, - ); - } - - function writableStreamFinishInFlightWrite(stream) { - stream._inFlightWriteRequest.resolve(undefined); - stream._inFlightWriteRequest = undefined; - } - function writableStreamFinishInFlightWriteWithError(stream, error) { - stream._inFlightWriteRequest.reject(error); - stream._inFlightWriteRequest = undefined; - writableStreamDealWithRejection(stream, error); - } - function writableStreamFinishInFlightClose(stream) { - stream._inFlightCloseRequest.resolve(undefined); - stream._inFlightCloseRequest = undefined; - if (stream._state === 'erroring') { - stream._storedError = undefined; - if (stream._pendingAbortRequest !== undefined) { - stream._pendingAbortRequest.deferred.resolve(undefined); - stream._pendingAbortRequest = undefined; - } - } - stream._state = 'closed'; - var writer = stream._writer; - if (writer !== undefined) writer._closedDeferred.resolve(undefined); - } - function writableStreamFinishInFlightCloseWithError(stream, error) { - stream._inFlightCloseRequest.reject(error); - stream._inFlightCloseRequest = undefined; - if (stream._pendingAbortRequest !== undefined) { - stream._pendingAbortRequest.deferred.reject(error); - stream._pendingAbortRequest = undefined; - } - writableStreamDealWithRejection(stream, error); - } - - function writableStreamHasOperationMarkedInFlight(stream) { - return stream._inFlightWriteRequest !== undefined || stream._inFlightCloseRequest !== undefined; - } - function writableStreamMarkCloseRequestInFlight(stream) { - stream._inFlightCloseRequest = stream._closeRequest; - stream._closeRequest = undefined; - } - function writableStreamMarkFirstWriteRequestInFlight(stream) { - stream._inFlightWriteRequest = stream._writeRequests.shift(); - } - function writableStreamRejectCloseAndClosedPromiseIfNeeded(stream) { - if (stream._closeRequest !== undefined) { - stream._closeRequest.reject(stream._storedError); - stream._closeRequest = undefined; - } - var writer = stream._writer; - if (writer !== undefined) { - writer._closedDeferred.reject(stream._storedError); - setPromiseIsHandled(writer._closedDeferred.promise); - } - } - function writableStreamUpdateBackpressure(stream, backpressure) { - var writer = stream._writer; - if (writer !== undefined && backpressure !== stream._backpressure) { - if (backpressure) { - writer._readyDeferred = newDeferred(); - } else { - writer._readyDeferred.resolve(undefined); - } - } - stream._backpressure = backpressure; - } - - // ---- WritableStreamDefaultWriter ------------------------------------------- - - class WritableStreamDefaultWriter { - constructor(stream) { - if (!(stream instanceof WritableStream)) { - throw new TypeError('WritableStreamDefaultWriter requires a WritableStream'); - } - if (isWritableStreamLocked(stream)) { - throw new TypeError('WritableStream is already locked to a writer'); - } - this._ownerWritableStream = stream; - stream._writer = this; - this._readyDeferred = newDeferred(); - this._closedDeferred = newDeferred(); - var state = stream._state; - if (state === 'writable') { - if (!writableStreamCloseQueuedOrInFlight(stream) && stream._backpressure) { - // ready stays pending - } else { - this._readyDeferred.resolve(undefined); - } - } else if (state === 'erroring') { - this._readyDeferred.reject(stream._storedError); - setPromiseIsHandled(this._readyDeferred.promise); - } else if (state === 'closed') { - this._readyDeferred.resolve(undefined); - this._closedDeferred.resolve(undefined); - } else { - var storedError = stream._storedError; - this._readyDeferred.reject(storedError); - setPromiseIsHandled(this._readyDeferred.promise); - this._closedDeferred.reject(storedError); - setPromiseIsHandled(this._closedDeferred.promise); - } - } - - get closed() { - if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); - return this._closedDeferred.promise; - } - - get desiredSize() { - if (!this._ownerWritableStream) throw defaultWriterLockException(); - return writableStreamDefaultWriterGetDesiredSize(this); - } - - get ready() { - if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); - return this._readyDeferred.promise; - } - - abort(reason) { - if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); - return writableStreamAbort(this._ownerWritableStream, reason); - } - - close() { - var stream = this._ownerWritableStream; - if (!stream) return promiseRejectedWith(defaultWriterLockException()); - if (writableStreamCloseQueuedOrInFlight(stream)) { - return promiseRejectedWith(new TypeError('Cannot close an already-closing stream')); - } - return writableStreamClose(stream); - } - - releaseLock() { - var stream = this._ownerWritableStream; - if (!stream) return; - var releasedError = new TypeError( - 'Writer was released and can no longer be used to monitor the stream', - ); - writableStreamDefaultWriterEnsureReadyPromiseRejected(this, releasedError); - writableStreamDefaultWriterEnsureClosedPromiseRejected(this, releasedError); - stream._writer = undefined; - this._ownerWritableStream = undefined; - } - - write(chunk) { - if (!this._ownerWritableStream) return promiseRejectedWith(defaultWriterLockException()); - return writableStreamDefaultWriterWrite(this, chunk); - } - } - - function defaultWriterLockException() { - return new TypeError('Cannot use a released writer'); - } - - function writableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) { - if (writer._closedDeferred._resolved) { - writer._closedDeferred = newDeferred(); - } - writer._closedDeferred.reject(error); - setPromiseIsHandled(writer._closedDeferred.promise); - } - function writableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) { - writer._readyDeferred = newDeferred(); - writer._readyDeferred.reject(error); - setPromiseIsHandled(writer._readyDeferred.promise); - } - function writableStreamDefaultWriterGetDesiredSize(writer) { - var stream = writer._ownerWritableStream; - var state = stream._state; - if (state === 'errored' || state === 'erroring') return null; - if (state === 'closed') return 0; - return writableStreamDefaultControllerGetDesiredSize(stream._controller); - } - function writableStreamDefaultWriterWrite(writer, chunk) { - var stream = writer._ownerWritableStream; - var controller = stream._controller; - var chunkSize = writableStreamDefaultControllerGetChunkSize(controller, chunk); - if (stream !== writer._ownerWritableStream) { - return promiseRejectedWith(defaultWriterLockException()); - } - var state = stream._state; - if (state === 'errored') return promiseRejectedWith(stream._storedError); - if (writableStreamCloseQueuedOrInFlight(stream) || state === 'closed') { - return promiseRejectedWith(new TypeError('Cannot write to a closing/closed stream')); - } - if (state === 'erroring') return promiseRejectedWith(stream._storedError); - var promise = writableStreamAddWriteRequest(stream); - writableStreamDefaultControllerWrite(controller, chunk, chunkSize); - return promise; - } - - // ---- WritableStreamDefaultController --------------------------------------- - - class WritableStreamDefaultController { - constructor() { - throw new TypeError('Illegal constructor'); - } - - get signal() { - return undefined; // AbortController signal plumbing not exposed. - } - - error(e) { - if (this._controlledWritableStream._state !== 'writable') return; - writableStreamDefaultControllerError(this, e); - } - - _abortSteps(reason) { - var result = this._abortAlgorithm(reason); - writableStreamDefaultControllerClearAlgorithms(this); - return result; - } - - _errorSteps() { - resetQueue(this); - } - } - - function setUpWritableStreamDefaultController( - stream, - controller, - startAlgorithm, - writeAlgorithm, - closeAlgorithm, - abortAlgorithm, - highWaterMark, - sizeAlgorithm, - ) { - controller._controlledWritableStream = stream; - stream._controller = controller; - resetQueue(controller); - controller._started = false; - controller._strategySizeAlgorithm = sizeAlgorithm; - controller._strategyHWM = highWaterMark; - controller._writeAlgorithm = writeAlgorithm; - controller._closeAlgorithm = closeAlgorithm; - controller._abortAlgorithm = abortAlgorithm; - var backpressure = writableStreamDefaultControllerGetBackpressure(controller); - writableStreamUpdateBackpressure(stream, backpressure); - - var startResult = startAlgorithm(); - uponPromise( - promiseResolvedWith(startResult), - function () { - controller._started = true; - writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); - }, - function (r) { - controller._started = true; - writableStreamDealWithRejection(stream, r); - }, - ); - } - - function setUpWritableStreamDefaultControllerFromUnderlyingSink( - stream, - underlyingSink, - highWaterMark, - sizeAlgorithm, - ) { - var controller = Object.create(WritableStreamDefaultController.prototype); - var startAlgorithm = noop; - var writeAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - var closeAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - var abortAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - if (typeof underlyingSink.start === 'function') { - startAlgorithm = function () { - return promiseFrom(function () { - return underlyingSink.start(controller); - }); - }; - } - if (typeof underlyingSink.write === 'function') { - writeAlgorithm = function (chunk) { - return promiseFrom(function () { - return underlyingSink.write(chunk, controller); - }); - }; - } - if (typeof underlyingSink.close === 'function') { - closeAlgorithm = function () { - return promiseFrom(function () { - return underlyingSink.close(); - }); - }; - } - if (typeof underlyingSink.abort === 'function') { - abortAlgorithm = function (reason) { - return promiseFrom(function () { - return underlyingSink.abort(reason); - }); - }; - } - setUpWritableStreamDefaultController( - stream, - controller, - startAlgorithm, - writeAlgorithm, - closeAlgorithm, - abortAlgorithm, - highWaterMark, - sizeAlgorithm, - ); - } - - var CLOSE_SENTINEL = {}; - - function writableStreamDefaultControllerClearAlgorithms(controller) { - controller._writeAlgorithm = undefined; - controller._closeAlgorithm = undefined; - controller._abortAlgorithm = undefined; - controller._strategySizeAlgorithm = undefined; - } - function writableStreamDefaultControllerClose(controller) { - enqueueValueWithSize(controller, CLOSE_SENTINEL, 0); - writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); - } - function writableStreamDefaultControllerGetChunkSize(controller, chunk) { - try { - return controller._strategySizeAlgorithm(chunk); - } catch (e) { - writableStreamDefaultControllerErrorIfNeeded(controller, e); - return 1; - } - } - function writableStreamDefaultControllerGetDesiredSize(controller) { - return controller._strategyHWM - controller._queueTotalSize; - } - function writableStreamDefaultControllerWrite(controller, chunk, chunkSize) { - try { - enqueueValueWithSize(controller, chunk, chunkSize); - } catch (e) { - writableStreamDefaultControllerErrorIfNeeded(controller, e); - return; - } - var stream = controller._controlledWritableStream; - if (!writableStreamCloseQueuedOrInFlight(stream) && stream._state === 'writable') { - var backpressure = writableStreamDefaultControllerGetBackpressure(controller); - writableStreamUpdateBackpressure(stream, backpressure); - } - writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); - } - function writableStreamDefaultControllerAdvanceQueueIfNeeded(controller) { - var stream = controller._controlledWritableStream; - if (!controller._started) return; - if (stream._inFlightWriteRequest !== undefined) return; - var state = stream._state; - if (state === 'erroring') { - writableStreamFinishErroring(stream); - return; - } - if (controller._queue.length === 0) return; - var value = peekQueueValue(controller); - if (value === CLOSE_SENTINEL) { - writableStreamDefaultControllerProcessClose(controller); - } else { - writableStreamDefaultControllerProcessWrite(controller, value); - } - } - function writableStreamDefaultControllerErrorIfNeeded(controller, error) { - if (controller._controlledWritableStream._state === 'writable') { - writableStreamDefaultControllerError(controller, error); - } - } - function writableStreamDefaultControllerProcessClose(controller) { - var stream = controller._controlledWritableStream; - writableStreamMarkCloseRequestInFlight(stream); - dequeueValue(controller); - var sinkClosePromise = controller._closeAlgorithm(); - writableStreamDefaultControllerClearAlgorithms(controller); - uponPromise( - sinkClosePromise, - function () { - writableStreamFinishInFlightClose(stream); - }, - function (reason) { - writableStreamFinishInFlightCloseWithError(stream, reason); - }, - ); - } - function writableStreamDefaultControllerProcessWrite(controller, chunk) { - var stream = controller._controlledWritableStream; - writableStreamMarkFirstWriteRequestInFlight(stream); - var sinkWritePromise = controller._writeAlgorithm(chunk); - uponPromise( - sinkWritePromise, - function () { - writableStreamFinishInFlightWrite(stream); - var state = stream._state; - dequeueValue(controller); - if (!writableStreamCloseQueuedOrInFlight(stream) && state === 'writable') { - var backpressure = writableStreamDefaultControllerGetBackpressure(controller); - writableStreamUpdateBackpressure(stream, backpressure); - } - writableStreamDefaultControllerAdvanceQueueIfNeeded(controller); - }, - function (reason) { - if (stream._state === 'writable') { - writableStreamDefaultControllerClearAlgorithms(controller); - } - writableStreamFinishInFlightWriteWithError(stream, reason); - }, - ); - } - function writableStreamDefaultControllerGetBackpressure(controller) { - return writableStreamDefaultControllerGetDesiredSize(controller) <= 0; - } - function writableStreamDefaultControllerError(controller, error) { - var stream = controller._controlledWritableStream; - writableStreamDefaultControllerClearAlgorithms(controller); - writableStreamStartErroring(stream, error); - } - - // ---- pipeTo ---------------------------------------------------------------- - - function readableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) { - var reader = acquireReadableStreamDefaultReader(source); - var writer = new WritableStreamDefaultWriter(dest); - source._disturbed = true; - var shuttingDown = false; - var currentWrite = promiseResolvedWith(undefined); - var resultDeferred = newDeferred(); - - var abortAlgorithm; - if (signal !== undefined && signal !== null) { - abortAlgorithm = function () { - var error = - signal.reason !== undefined ? signal.reason : new DOMExceptionLike('AbortError'); - var actions = []; - if (!preventAbort) { - actions.push(function () { - if (dest._state === 'writable') return writableStreamAbort(dest, error); - return promiseResolvedWith(undefined); - }); - } - if (!preventCancel) { - actions.push(function () { - if (source._state === 'readable') return readableStreamCancel(source, error); - return promiseResolvedWith(undefined); - }); - } - shutdownWithAction( - function () { - return Promise.all( - actions.map(function (a) { - return a(); - }), - ); - }, - true, - error, - ); - }; - if (signal.aborted) { - abortAlgorithm(); - return resultDeferred.promise; - } - signal.addEventListener('abort', abortAlgorithm); - } - - function isOrBecomesErrored(stream, promise, action) { - if (stream._state === 'errored') action(stream._storedError); - else uponPromise(promise, undefined, action); - } - function isOrBecomesClosed(stream, promise, action) { - if (stream._state === 'closed') action(); - else uponPromise(promise, action, undefined); - } - - // Error/close forwarding. - isOrBecomesErrored(source, reader._closedDeferred.promise, function (storedError) { - if (!preventAbort) { - shutdownWithAction( - function () { - return writableStreamAbort(dest, storedError); - }, - true, - storedError, - ); - } else { - shutdown(true, storedError); - } - }); - isOrBecomesErrored(dest, writer._closedDeferred.promise, function (storedError) { - if (!preventCancel) { - shutdownWithAction( - function () { - return readableStreamCancel(source, storedError); - }, - true, - storedError, - ); - } else { - shutdown(true, storedError); - } - }); - isOrBecomesClosed(source, reader._closedDeferred.promise, function () { - if (!preventClose) { - shutdownWithAction(function () { - return writableStreamDefaultWriterCloseWithErrorPropagation(writer); - }); - } else { - shutdown(); - } - }); - if (writableStreamCloseQueuedOrInFlight(dest) || dest._state === 'closed') { - var destClosed = new TypeError( - 'the destination writable stream closed before all data was piped to it', - ); - if (!preventCancel) { - shutdownWithAction( - function () { - return readableStreamCancel(source, destClosed); - }, - true, - destClosed, - ); - } else { - shutdown(true, destClosed); - } - } - - function pipeLoop() { - return newPromise(function (resolveLoop, rejectLoop) { - function next(done) { - if (done) resolveLoop(undefined); - else uponPromise(pipeStep(), next, rejectLoop); - } - next(false); - }); - } - - function pipeStep() { - if (shuttingDown) return promiseResolvedWith(true); - return writer._readyDeferred.promise.then(function () { - return newPromise(function (resolveRead, rejectRead) { - readableStreamDefaultReaderRead(reader, { - chunkSteps: function (chunk) { - // Swallow a write rejection here: pipe write errors surface through - // the dest-errored forwarding (writer.closed), and waitForWrites- - // ToFinish must observe a never-rejecting currentWrite (spec). - currentWrite = writableStreamDefaultWriterWrite(writer, chunk).then(undefined, noop); - resolveRead(false); - }, - closeSteps: function () { - resolveRead(true); - }, - errorSteps: rejectRead, - }); - }); - }); - } - - uponPromise(pipeLoop(), undefined, noop); - - function waitForWritesToFinish() { - var oldCurrentWrite = currentWrite; - return currentWrite.then(function () { - return oldCurrentWrite !== currentWrite ? waitForWritesToFinish() : undefined; - }); - } - - function shutdownWithAction(action, originalIsError, originalError) { - if (shuttingDown) return; - shuttingDown = true; - if (dest._state === 'writable' && !writableStreamCloseQueuedOrInFlight(dest)) { - uponPromise(waitForWritesToFinish(), doTheRest); - } else { - doTheRest(); - } - function doTheRest() { - uponPromise( - action(), - function () { - finalize(originalIsError, originalError); - }, - function (newError) { - finalize(true, newError); - }, - ); - } - } - - function shutdown(isError, error) { - if (shuttingDown) return; - shuttingDown = true; - if (dest._state === 'writable' && !writableStreamCloseQueuedOrInFlight(dest)) { - uponPromise(waitForWritesToFinish(), function () { - finalize(isError, error); - }); - } else { - finalize(isError, error); - } - } - - function finalize(isError, error) { - writableStreamDefaultWriterRelease(writer); - readableStreamDefaultReaderRelease(reader); - if (signal !== undefined && signal !== null) { - signal.removeEventListener('abort', abortAlgorithm); - } - if (isError) resultDeferred.reject(error); - else resultDeferred.resolve(undefined); - } - - return resultDeferred.promise; - } - - function writableStreamDefaultWriterRelease(writer) { - if (writer._ownerWritableStream) writer.releaseLock(); - } - function writableStreamDefaultWriterCloseWithErrorPropagation(writer) { - var stream = writer._ownerWritableStream; - var state = stream._state; - if (writableStreamCloseQueuedOrInFlight(stream) || state === 'closed') { - return promiseResolvedWith(undefined); - } - if (state === 'errored') return promiseRejectedWith(stream._storedError); - return writableStreamClose(stream); - } - - // A tiny stand-in for AbortError when a signal has no reason. Real AbortSignal - // (js/internal/abort.js) supplies a DOMException reason, so this is only a - // fallback for hand-rolled signals. - function DOMExceptionLike(name) { - var e = new Error(name); - e.name = name; - return e; - } - - // ============================================================================ - // TransformStream - // ============================================================================ - - class TransformStream { - constructor(transformer, writableStrategy, readableStrategy) { - transformer = transformer === undefined ? {} : transformer; - writableStrategy = writableStrategy === undefined ? {} : writableStrategy; - readableStrategy = readableStrategy === undefined ? {} : readableStrategy; - if (transformer.readableType !== undefined) { - throw new RangeError('Invalid transformer.readableType'); - } - if (transformer.writableType !== undefined) { - throw new RangeError('Invalid transformer.writableType'); - } - - var readableHWM = extractHighWaterMark(readableStrategy, 0); - var readableSize = extractSizeAlgorithm(readableStrategy); - var writableHWM = extractHighWaterMark(writableStrategy, 1); - var writableSize = extractSizeAlgorithm(writableStrategy); - - var startDeferred = newDeferred(); - initializeTransformStream( - this, - startDeferred.promise, - writableHWM, - writableSize, - readableHWM, - readableSize, - ); - setUpTransformStreamDefaultControllerFromTransformer(this, transformer); - - if (typeof transformer.start === 'function') { - startDeferred.resolve(transformer.start(this._controller)); - } else { - startDeferred.resolve(undefined); - } - } - - get readable() { - return this._readable; - } - get writable() { - return this._writable; - } - } - - function initializeTransformStream( - stream, - startPromise, - writableHWM, - writableSize, - readableHWM, - readableSize, - ) { - function startAlgorithm() { - return startPromise; - } - function writeAlgorithm(chunk) { - return transformStreamDefaultSinkWriteAlgorithm(stream, chunk); - } - function abortAlgorithm(reason) { - return transformStreamDefaultSinkAbortAlgorithm(stream, reason); - } - function closeAlgorithm() { - return transformStreamDefaultSinkCloseAlgorithm(stream); - } - stream._writable = createWritableStream( - startAlgorithm, - writeAlgorithm, - closeAlgorithm, - abortAlgorithm, - writableHWM, - writableSize, - ); - - function pullAlgorithm() { - return transformStreamDefaultSourcePullAlgorithm(stream); - } - function cancelAlgorithm(reason) { - transformStreamErrorWritableAndUnblockWrite(stream, reason); - return promiseResolvedWith(undefined); - } - stream._readable = createReadableStream( - startAlgorithm, - pullAlgorithm, - cancelAlgorithm, - readableHWM, - readableSize, - ); - - stream._backpressure = undefined; - stream._backpressureChangeDeferred = undefined; - transformStreamSetBackpressure(stream, true); - stream._controller = undefined; - } - - function createWritableStream( - startAlgorithm, - writeAlgorithm, - closeAlgorithm, - abortAlgorithm, - highWaterMark, - sizeAlgorithm, - ) { - var stream = Object.create(WritableStream.prototype); - stream._state = 'writable'; - stream._storedError = undefined; - stream._writer = undefined; - stream._controller = undefined; - stream._inFlightWriteRequest = undefined; - stream._closeRequest = undefined; - stream._inFlightCloseRequest = undefined; - stream._pendingAbortRequest = undefined; - stream._writeRequests = []; - stream._backpressure = false; - var controller = Object.create(WritableStreamDefaultController.prototype); - setUpWritableStreamDefaultController( - stream, - controller, - startAlgorithm, - writeAlgorithm, - closeAlgorithm, - abortAlgorithm, - highWaterMark, - sizeAlgorithm, - ); - return stream; - } - - function transformStreamErrorWritableAndUnblockWrite(stream, e) { - transformStreamDefaultControllerClearAlgorithms(stream._controller); - writableStreamDefaultControllerErrorIfNeeded(stream._writable._controller, e); - transformStreamUnblockWrite(stream); - } - function transformStreamUnblockWrite(stream) { - if (stream._backpressure) transformStreamSetBackpressure(stream, false); - } - function transformStreamError(stream, e) { - readableStreamDefaultControllerError(stream._readable._controller, e); - transformStreamErrorWritableAndUnblockWrite(stream, e); - } - function transformStreamSetBackpressure(stream, backpressure) { - if (stream._backpressureChangeDeferred !== undefined) { - stream._backpressureChangeDeferred.resolve(undefined); - } - stream._backpressureChangeDeferred = newDeferred(); - stream._backpressure = backpressure; - } - - class TransformStreamDefaultController { - constructor() { - throw new TypeError('Illegal constructor'); - } - get desiredSize() { - return readableStreamDefaultControllerGetDesiredSize(this._stream._readable._controller); - } - enqueue(chunk) { - transformStreamDefaultControllerEnqueue(this, chunk); - } - error(reason) { - transformStreamDefaultControllerError(this, reason); - } - terminate() { - transformStreamDefaultControllerTerminate(this); - } - } - - function setUpTransformStreamDefaultController( - stream, - controller, - transformAlgorithm, - flushAlgorithm, - ) { - controller._stream = stream; - stream._controller = controller; - controller._transformAlgorithm = transformAlgorithm; - controller._flushAlgorithm = flushAlgorithm; - } - function setUpTransformStreamDefaultControllerFromTransformer(stream, transformer) { - var controller = Object.create(TransformStreamDefaultController.prototype); - var transformAlgorithm = function (chunk) { - try { - transformStreamDefaultControllerEnqueue(controller, chunk); - return promiseResolvedWith(undefined); - } catch (e) { - return promiseRejectedWith(e); - } - }; - var flushAlgorithm = function () { - return promiseResolvedWith(undefined); - }; - if (typeof transformer.transform === 'function') { - transformAlgorithm = function (chunk) { - return promiseFrom(function () { - return transformer.transform(chunk, controller); - }); - }; - } - if (typeof transformer.flush === 'function') { - flushAlgorithm = function () { - return promiseFrom(function () { - return transformer.flush(controller); - }); - }; - } - setUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm); - } - function transformStreamDefaultControllerClearAlgorithms(controller) { - controller._transformAlgorithm = undefined; - controller._flushAlgorithm = undefined; - } - function transformStreamDefaultControllerEnqueue(controller, chunk) { - var stream = controller._stream; - var readableController = stream._readable._controller; - if (!readableStreamDefaultControllerCanCloseOrEnqueue(readableController)) { - throw new TypeError('Readable side is not in a state that permits enqueue'); - } - try { - readableStreamDefaultControllerEnqueue(readableController, chunk); - } catch (e) { - transformStreamErrorWritableAndUnblockWrite(stream, e); - throw stream._readable._storedError; - } - var backpressure = readableStreamDefaultControllerHasBackpressure(readableController); - if (backpressure !== stream._backpressure) { - transformStreamSetBackpressure(stream, true); - } - } - function readableStreamDefaultControllerHasBackpressure(controller) { - return readableStreamDefaultControllerGetDesiredSize(controller) <= 0; - } - function transformStreamDefaultControllerError(controller, e) { - transformStreamError(controller._stream, e); - } - function transformStreamDefaultControllerPerformTransform(controller, chunk) { - var transformPromise = controller._transformAlgorithm(chunk); - return transformPromise.then(undefined, function (r) { - transformStreamError(controller._stream, r); - throw r; - }); - } - function transformStreamDefaultControllerTerminate(controller) { - var stream = controller._stream; - var readableController = stream._readable._controller; - readableStreamDefaultControllerClose(readableController); - var error = new TypeError('TransformStream terminated'); - transformStreamErrorWritableAndUnblockWrite(stream, error); - } - - function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { - var controller = stream._controller; - if (stream._backpressure) { - var backpressureChangePromise = stream._backpressureChangeDeferred.promise; - return backpressureChangePromise.then(function () { - var writable = stream._writable; - var state = writable._state; - if (state === 'erroring') throw writable._storedError; - return transformStreamDefaultControllerPerformTransform(controller, chunk); - }); - } - return transformStreamDefaultControllerPerformTransform(controller, chunk); - } - function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { - transformStreamError(stream, reason); - return promiseResolvedWith(undefined); - } - function transformStreamDefaultSinkCloseAlgorithm(stream) { - var controller = stream._controller; - var flushPromise = controller._flushAlgorithm(); - transformStreamDefaultControllerClearAlgorithms(controller); - return flushPromise.then( - function () { - if (stream._readable._state === 'errored') throw stream._readable._storedError; - readableStreamDefaultControllerClose(stream._readable._controller); - }, - function (r) { - transformStreamError(stream, r); - throw stream._readable._storedError; - }, - ); - } - function transformStreamDefaultSourcePullAlgorithm(stream) { - transformStreamSetBackpressure(stream, false); - return stream._backpressureChangeDeferred.promise; - } - - // ============================================================================ - // BYOB (deferred) — expose the constructor so `typeof` matches, but report the - // deferral explicitly on use. - // ============================================================================ - - class ReadableStreamBYOBReader { - constructor(stream) { - if (!(stream instanceof ReadableStream)) { - throw new TypeError('ReadableStreamBYOBReader requires a ReadableStream'); - } - // Lava has no byte-stream sources yet, so there is never a valid byte - // stream to attach to — report that explicitly rather than half-attaching. - throw new TypeError('BYOB readers are not supported in Lava yet (no readable byte streams)'); - } - } + var readable = require('internal/webstreams/readablestream'); + var writable = require('internal/webstreams/writablestream'); + var transform = require('internal/webstreams/transformstream'); + var strategies = require('internal/webstreams/queuingstrategies'); + var ReadableStream = readable.ReadableStream; + var ReadableStreamDefaultReader = readable.ReadableStreamDefaultReader; + var ReadableStreamBYOBReader = readable.ReadableStreamBYOBReader; + var ReadableStreamDefaultController = readable.ReadableStreamDefaultController; + var WritableStream = writable.WritableStream; + var WritableStreamDefaultWriter = writable.WritableStreamDefaultWriter; + var WritableStreamDefaultController = writable.WritableStreamDefaultController; + var TransformStream = transform.TransformStream; + var TransformStreamDefaultController = transform.TransformStreamDefaultController; + var ByteLengthQueuingStrategy = strategies.ByteLengthQueuingStrategy; + var CountQueuingStrategy = strategies.CountQueuingStrategy; // ---- exports + globals ----------------------------------------------------- diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index e56ac08..e7f5e4e 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2462,6 +2462,24 @@ "replace": " if (k !== 'writable') continue;", "gate": "compat:tests/node-compat/cases/79-stream-internal-modules.js", "expect_detail": "node: Duplex has write function" + }, + { + "name": "80-webstreams-internal-modules dies when a constructor is defined in the public entry again", + "why": "A constructor left behind in node/stream/web.js throws from a `node:stream/web` frame, which is exactly what the split moved away from.", + "source": "pkg/runtime/js/node/stream/web.js", + "find": " var CountQueuingStrategy = strategies.CountQueuingStrategy;", + "replace": " var CountQueuingStrategy = function CountQueuingStrategy(o) { if (!o) throw new TypeError('init'); return new strategies.CountQueuingStrategy(o); };", + "gate": "compat:tests/node-compat/cases/80-webstreams-internal-modules.js", + "expect_detail": "node: CountQueuingStrategy(): node:internal/webstreams/queuingstrategies true false" + }, + { + "name": "80-webstreams-internal-modules dies when pipeTo loses its writablestream import", + "why": "pipeTo's abort path calls writableStreamAbort across the readablestream -> writablestream seam; a dropped import throws out of the abort path (unhandled, exit 1) instead of rejecting the pipe with the signal's reason.", + "source": "pkg/runtime/js/internal/webstreams/readablestream.js", + "find": " var writableStreamAbort = writable.writableStreamAbort;", + "replace": " var writableStreamAbort = writable.writableStreamAbortGone;", + "gate": "compat:tests/node-compat/cases/80-webstreams-internal-modules.js", + "expect_detail": "node: abort stop" } ] } diff --git a/tests/node-compat/cases/80-webstreams-internal-modules.js b/tests/node-compat/cases/80-webstreams-internal-modules.js new file mode 100644 index 0000000..23ae81f --- /dev/null +++ b/tests/node-compat/cases/80-webstreams-internal-modules.js @@ -0,0 +1,179 @@ +// node:stream/web is assembled from internal modules laid out like node's +// lib/internal/webstreams: a throw from ReadableStream code carries a +// `node:internal/webstreams/readablestream` frame, one from WritableStream code +// `node:internal/webstreams/writablestream`, and so on. All of them used to say +// `node:stream/web`, because everything was defined in node/stream/web.js. +// +// Only the frame's SOURCE NAME is compared — never the message, line or column (Lava's +// messages are not node's yet; that is a separate gap). Each site below is one node +// itself attributes to that file (probed on node 26.13.0) AND one that throws +// synchronously on both runtimes. The behavior lines after it are there so a move that +// loses a helper or re-wires an identity fails here and not only in a stack. +'use strict'; + +const web = require('node:stream/web'); +const { + ReadableStream, + ReadableStreamDefaultReader, + ReadableStreamBYOBReader, + ReadableStreamDefaultController, + WritableStream, + WritableStreamDefaultWriter, + WritableStreamDefaultController, + TransformStream, + TransformStreamDefaultController, + ByteLengthQueuingStrategy, + CountQueuingStrategy, +} = web; + +function frame(label, source, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + // The old source name must be gone too: before the split every one of these frames + // read `node:stream/web:`. + console.log( + label + ': ' + source + ' ' + stack.includes(source), + stack.includes('node:stream/web:'), + ); +} +const R = 'node:internal/webstreams/readablestream'; +const W = 'node:internal/webstreams/writablestream'; +const T = 'node:internal/webstreams/transformstream'; +const Q = 'node:internal/webstreams/queuingstrategies'; +const U = 'node:internal/webstreams/util'; + +frame('ReadableStream type', R, () => new ReadableStream({ type: 'x!' })); +frame('getReader mode', R, () => new ReadableStream().getReader({ mode: 'x!' })); +frame('pipeThrough(5)', R, () => new ReadableStream().pipeThrough(5)); +frame('DefaultReader(5)', R, () => new ReadableStreamDefaultReader(5)); +frame('DefaultController()', R, () => new ReadableStreamDefaultController()); +frame('BYOBReader(5)', R, () => new ReadableStreamBYOBReader(5)); +frame('WritableStream type', W, () => new WritableStream({ type: 'x!' })); +frame('DefaultWriter(5)', W, () => new WritableStreamDefaultWriter(5)); +frame('WritableController()', W, () => new WritableStreamDefaultController()); +frame('TransformStream readableType', T, () => new TransformStream({ readableType: 'x!' })); +frame('TransformController()', T, () => new TransformStreamDefaultController()); +frame( + 'enqueue after terminate', + T, + () => + new TransformStream({ + start(c) { + c.terminate(); + c.enqueue(1); + }, + }), +); +frame('ByteLengthQueuingStrategy()', Q, () => new ByteLengthQueuingStrategy()); +frame('CountQueuingStrategy()', Q, () => new CountQueuingStrategy()); +frame('readable highWaterMark', U, () => new ReadableStream({}, { highWaterMark: -1 })); +frame('writable highWaterMark', U, () => new WritableStream({}, { highWaterMark: -1 })); +frame('transform highWaterMark', U, () => new TransformStream({}, { highWaterMark: -1 })); +frame('size not a function', U, () => new ReadableStream({}, { size: 5 })); + +// --- the assembled module: identities and globals --------------------------------- +const again = require('stream/web'); +for (const k of [ + 'ReadableStream', + 'ReadableStreamDefaultReader', + 'ReadableStreamBYOBReader', + 'ReadableStreamDefaultController', + 'WritableStream', + 'WritableStreamDefaultWriter', + 'WritableStreamDefaultController', + 'TransformStream', + 'TransformStreamDefaultController', + 'ByteLengthQueuingStrategy', + 'CountQueuingStrategy', +]) { + console.log( + 'export', + k, + typeof web[k], + web[k] === again[k], + globalThis[k] === web[k], + web[k].name, + ); +} +const bl = new ByteLengthQueuingStrategy({ highWaterMark: 16 }); +const cq = new CountQueuingStrategy({ highWaterMark: 4 }); +console.log( + 'strategies', + bl.highWaterMark, + bl.size(new Uint8Array(3)), + cq.highWaterMark, + cq.size('abc'), +); + +// --- behavior across the file seams -------------------------------------------- +// Each round trip prints once it settles; they are collected and printed sorted so the +// case does not pin the relative order of independent pipelines. +const OUTCOMES = 6; +const out = []; +const log = (...a) => { + out.push(a.join(' ')); + if (out.length === OUTCOMES) console.log(out.sort().join('\n')); +}; +const from = (chunks) => + new ReadableStream({ + start(c) { + for (const x of chunks) c.enqueue(x); + c.close(); + }, + }); +const collect = (seen) => + new WritableStream({ + write(c) { + seen.push(String(c)); + }, + }); + +const piped = []; +from(['p', 'i', 'pe']) + .pipeTo(collect(piped)) + .then(() => log('pipeTo', piped.join(''))); + +const upper = new TransformStream({ + transform(c, ctl) { + ctl.enqueue(String(c).toUpperCase()); + }, + flush(ctl) { + ctl.enqueue('!'); + }, +}); +const through = []; +from(['t', 's']) + .pipeThrough(upper) + .pipeTo(collect(through)) + .then(() => log('pipeThrough', through.join(''))); + +const [a, b] = from(['t', 'e', 'e']).tee(); +Promise.all([a, b].map((s) => s.getReader().read())).then((rs) => + log('tee', rs.map((r) => r.value + r.done).join(',')), +); + +(async () => { + const parts = []; + for await (const c of from(['it', 'er'])) parts.push(c); + log('async iteration', parts.join('')); +})(); + +const ac = new AbortController(); +ac.abort(new Error('stop')); +from(['x']) + .pipeTo(collect([]), { signal: ac.signal }) + .then( + () => log('abort', 'resolved'), + (e) => log('abort', e.message), + ); + +const w = new WritableStream({}, new CountQueuingStrategy({ highWaterMark: 2 })); +const writer = w.getWriter(); +const before = writer.desiredSize; +writer.write('a'); +log('writer desiredSize', before, writer.desiredSize); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 3b41365..6c3a6f1 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -167,6 +167,36 @@ "accessor": 0, "global": 6 }, + "internal/webstreams/queuingstrategies.js": { + "method": 0, + "invoke": 0, + "accessor": 1, + "global": 4 + }, + "internal/webstreams/readablestream.js": { + "method": 10, + "invoke": 0, + "accessor": 0, + "global": 30 + }, + "internal/webstreams/transformstream.js": { + "method": 3, + "invoke": 0, + "accessor": 0, + "global": 6 + }, + "internal/webstreams/util.js": { + "method": 7, + "invoke": 0, + "accessor": 0, + "global": 10 + }, + "internal/webstreams/writablestream.js": { + "method": 2, + "invoke": 0, + "accessor": 0, + "global": 15 + }, "node/assert.js": { "method": 14, "invoke": 2, @@ -300,10 +330,10 @@ "global": 2 }, "node/stream/web.js": { - "method": 22, + "method": 0, "invoke": 0, - "accessor": 1, - "global": 65 + "accessor": 0, + "global": 0 }, "node/string_decoder.js": { "method": 14, From 8be9b8d315e7f872e1c68f7f5840efa6557c5304 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 20:56:07 +0200 Subject: [PATCH 34/61] refactor(http): split node:http into internal/http/{common,incoming,outgoing,server} node/http.js 1143 -> 46 lines; the export object, key order and identities are unchanged. https.js is untouched. Case 81 pins the new module layout; existing http mutations are re-pointed at the new files. Co-Authored-By: Claude Opus 5.5 --- agents/prompts/pr-gate-reference/gates.md | 2 +- docs/ARCHITECTURE.md | 10 +- pkg/runtime/globals.odin | 1 + pkg/runtime/js/bootstrap/loader.js | 7 + pkg/runtime/js/internal/http/common.js | 200 +++ pkg/runtime/js/internal/http/incoming.js | 88 ++ pkg/runtime/js/internal/http/outgoing.js | 109 ++ pkg/runtime/js/internal/http/server.js | 839 +++++++++++++ pkg/runtime/js/node/http.js | 1117 +---------------- runtime/gates/route-gates.mjs | 4 + runtime/gates/route-gates.test.mjs | 8 + tests/mutation-manifest.json | 58 +- .../cases/81-http-internal-modules.js | 187 +++ tests/node-compat/pollution-baseline.json | 30 +- 14 files changed, 1528 insertions(+), 1132 deletions(-) create mode 100644 pkg/runtime/js/internal/http/common.js create mode 100644 pkg/runtime/js/internal/http/incoming.js create mode 100644 pkg/runtime/js/internal/http/outgoing.js create mode 100644 pkg/runtime/js/internal/http/server.js create mode 100644 tests/node-compat/cases/81-http-internal-modules.js diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 2b62e68..931f439 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -54,7 +54,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | Changed path | Additionally required | | -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `pkg/runtime/buffer*.odin`, `typed_array.odin`, `pkg/jsc/**` | `make test-odin`, `make bun-buffer-tests`, `make api-surface`, `make bench-gate` | -| `pkg/runtime/http.odin`, `js/node/http.js`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | +| `pkg/runtime/http.odin`, `js/node/http.js`, `js/internal/http/**`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | | `pkg/runtime/net.odin`, `net_other.odin`, `js/node/net.js` | `make test-net-smoke`, `make test-zerocopy-smoke` | | `pkg/runtime/tls*.odin`, `tls_server.odin`, `js/node/https.js` | `make test-https-smoke` (drives both backends internally) | | `pkg/runtime/fetch*.odin`, `js/internal/fetch.js` | `make test-fetch-smoke` | diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 485c026..2c57461 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -189,7 +189,15 @@ live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, `transform`, `passthrough`, `destroy`, `add-abort-signal`, `end-of-stream`, `pipeline`, `utils`) and the Web Streams bridges in `internal/webstreams/adapters.js`, the files node -keeps them in, so their frames read `node:internal/streams/readable`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +keeps them in, so their frames read `node:internal/streams/readable`. `node/http.js` is +likewise only the public entry (and `createServer`): the server lives in `internal/http/` +(`common`, `incoming`, `outgoing`, `server`), split the way node splits `_http_common`, +`_http_incoming`, `_http_outgoing` and `_http_server`. Unlike streams the names differ from +node's — node publishes `require('_http_server')` and friends with a parser/agent surface +Lava does not have, so a public file under that name would answer with the wrong keys — +and frames read `node:internal/http/server` where node's read `node:_http_server`. Odin +keys the http bindings by `node/http` only, so `loader.js` aliases that one object to +`internal/http/outgoing` and `internal/http/server`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index e0eb4c2..17c2aa9 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1638,6 +1638,7 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"node/timers", #load_directory("js/node/timers")}, {"node/util", #load_directory("js/node/util")}, {"internal", #load_directory("js/internal")}, + {"internal/http", #load_directory("js/internal/http")}, {"internal/streams", #load_directory("js/internal/streams")}, {"internal/webstreams", #load_directory("js/internal/webstreams")}, } diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 7d9517e..c12d4d6 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -24,6 +24,13 @@ var cache = Object.create(null); natives = natives || Object.create(null); + // node:http's implementation is split across js/internal/http/ (laid out like node's + // _http_* files); the two that call into Odin take node:http's OWN bindings object as + // their fourth argument, exactly as http.js did before the split — aliased here, not a + // second binding set, so there is one parseRequest per context. node/http.js checks + // the bindings before requiring either file. + natives['internal/http/outgoing'] = natives['node/http']; + natives['internal/http/server'] = natives['node/http']; // Prefix test by code unit. The specifier comes from user code on the public path, // so a String.prototype method here would be a live, replaceable read deciding diff --git a/pkg/runtime/js/internal/http/common.js b/pkg/runtime/js/internal/http/common.js new file mode 100644 index 0000000..0a28671 --- /dev/null +++ b/pkg/runtime/js/internal/http/common.js @@ -0,0 +1,200 @@ +// HTTP/1.1 framing helpers shared by the request and response sides — the token table, +// the request-head parse layout, and the chunked request-body decoder. Where node keeps +// its parser glue and _checkIsHttpToken: lib/_http_common.js. Lava's equivalent lives +// under internal/ (not a public `_http_common`) because node's module of that name +// exports a parser surface Lava does not have. +(function (require, module) { + 'use strict'; + + // This module is LAZY (first loaded by node:http on the user's first require), so every + // module-eval capture below comes from the loader, never a live global (#426, #333). + var primordials = require('internal/primordials'); + var PristineBuffer = require.pristineBuffer; + var BufferAlloc = PristineBuffer.alloc; + var BufferConcat = PristineBuffer.concat; + // The chunked decoder frames NETWORK-CONTROLLED bytes; every read below was live off + // `Buffer.prototype`. Measured before this: replacing `toString` so a size line "5" reads + // as "1" turned a valid chunked POST into 400 here, while node (llhttp, in C) delivered + // the body unchanged. + var BufferPrototypeToString = PristineBuffer.bufferToString; + var BufferPrototypeIndexOf = PristineBuffer.bufferIndexOf; + var BufferPrototypeSlice = PristineBuffer.bufferSlice; + // Every regex below decides FRAMING over attacker-controlled bytes, so none of + // them may go through `re.test(...)`: `RegExp.prototype.exec` is a writable data + // property, and the spec's RegExpExec re-reads it off the receiver, so a plain + // assignment steers `test` too — including a captured one. Measured over a real + // socket before this changed: `Content-Length: abc` answered 200 OK and + // `Transfer-Encoding: gzip` was accepted as chunked. There is deliberately no + // `RegExpPrototypeTest` in primordials; `RegExpMatches` is the only spelling, and + // it lives there rather than here because http.js, url.js and fetch.js each + // grew a private copy during the migration and fetch.js's was inverted. + // The character-class validators route through ONE shared predicate. They were + // written privately into http.js and url.js in the same commit that centralised + // RegExpMatches to stop exactly that, and had already diverged when they landed. + var reTest = primordials.RegExpMatches; + var allChars = primordials.allChars; + var isAsciiHexDigit = primordials.isAsciiHexDigit; + // The CAPTURED read. `String.prototype.charCodeAt` is a writable data property + // like any other, so a hand-rolled charCode scan is only as sound as the + // primitive it scans with — routing http's validators off RegExp said + // nothing about that. Measured: a gadget reporting 0x41 for CR and LF turned + // `res.setHeader('Location', decodeURIComponent(req.url))` into a split response + // driven by a REMOTE request line, while the exec and test poison phases passed + // throughout because they never touch this carrier. + var StringPrototypeCharCodeAt = primordials.StringPrototypeCharCodeAt; + // Bootstrap-captured free globals from primordials — NOT `parseInt` at this + // factory's own eval (server.js takes its Content-Length parseIntG the same way). This module is lazy; a free `var parseIntG = parseInt` + // here binds whatever `globalThis.parseInt` is when the first `require('http')` + // runs, which is after a dependency can already have replaced it. Hardening the + // VALIDATOR and leaving the CONVERSION live closes nothing: with parseInt + // replaced, the digit check still answers correctly and `contentLengthRemaining` + // becomes whatever the gadget returns, so a crafted request's pipelined bytes are + // framed as a second request. Measured as a desync against the real server. + // Pinned by the parseint http-smoke phase and the parseIntG mutation entry. + var parseIntG = primordials.parseInt; + var NumberIsSafeInteger = primordials.NumberIsSafeInteger; + + // Hoisted so the literals are not re-created per request, and so every framing + // pattern is visible in one place rather than inline at four call sites. + var CHUNK_EXT_RE = /^;[^\s;]/; + var TE_CHUNKED_RE = /^\s*chunked\s*$/i; + var CONNECTION_CLOSE_RE = /\bclose\b/i; + var CONNECTION_CLOSE_CS_RE = /\bclose\b/; + var CONNECTION_KEEPALIVE_RE = /\bkeep-alive\b/; + var TE_HAS_CHUNKED_RE = /\bchunked\b/i; + + /** + * RFC 7230 tchar bitmap for field-names (setHeader + chunked trailers). + * @type {Uint8Array} + */ + var HTTP_TCHAR = new primordials.Uint8Array(128); + (function initHttpTchar() { + var chars = "!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~"; + for (var i = 0; i < chars.length; i++) HTTP_TCHAR[StringPrototypeCharCodeAt(chars, i)] = 1; + })(); + + /** + * @param {string} value + * @returns {boolean} + */ + function isHttpToken(value) { + if (typeof value !== 'string' || value.length === 0) return false; + for (var i = 0; i < value.length; i++) { + var code = StringPrototypeCharCodeAt(value, i); + if (code >= 128 || !HTTP_TCHAR[code]) return false; + } + return true; + } + + /** @const {number} Max chunk-size / trailer line length. */ + var MAX_CHUNK_LINE_BYTES = 64 * 1024; + + /** + * Incremental Transfer-Encoding: chunked request-body decoder. + * + * @param {IncomingMessage} request + * @param {function(): void} onError + * @param {function(Buffer): void} onComplete leftover bytes after the body + * @returns {function(Buffer|undefined): void} feed + */ + function createChunkedDecoder(request, onError, onComplete) { + var buffer = BufferAlloc(0); + var state = 'size'; + var bytesRemaining = 0; + var finished = false; + + function fail() { + finished = true; + onError(); + } + + return function feed(incoming) { + if (finished) return; + if (incoming && incoming.length) { + buffer = buffer.length ? BufferConcat([buffer, incoming]) : incoming; + } + for (;;) { + if (state === 'size') { + var lineEnd = BufferPrototypeIndexOf(buffer, '\r\n'); + if (lineEnd < 0) { + if (buffer.length > MAX_CHUNK_LINE_BYTES) fail(); + return; + } + if (lineEnd > MAX_CHUNK_LINE_BYTES) return fail(); + var sizeLine = BufferPrototypeToString(buffer, 'latin1', 0, lineEnd); + var extensionSep = sizeLine.indexOf(';'); + var sizeToken = extensionSep >= 0 ? sizeLine.slice(0, extensionSep) : sizeLine; + if (!allChars(sizeToken, isAsciiHexDigit)) return fail(); + if (extensionSep >= 0 && !reTest(CHUNK_EXT_RE, sizeLine.slice(extensionSep))) + return fail(); + var chunkSize = parseIntG(sizeToken, 16); + if (!NumberIsSafeInteger(chunkSize) || chunkSize < 0) return fail(); + buffer = BufferPrototypeSlice(buffer, lineEnd + 2); + if (chunkSize === 0) state = 'trailer'; + else { + bytesRemaining = chunkSize; + state = 'data'; + } + } else if (state === 'data') { + if (buffer.length === 0) return; + var take = buffer.length < bytesRemaining ? buffer.length : bytesRemaining; + request.emit('data', BufferPrototypeSlice(buffer, 0, take)); + buffer = BufferPrototypeSlice(buffer, take); + bytesRemaining -= take; + if (bytesRemaining === 0) state = 'dataCRLF'; + } else if (state === 'dataCRLF') { + if (buffer.length < 2) return; + if (buffer[0] !== 13 || buffer[1] !== 10) return fail(); + buffer = BufferPrototypeSlice(buffer, 2); + state = 'size'; + } else { + if (buffer.length < 2) return; + if (buffer[0] === 13 && buffer[1] === 10) { + buffer = BufferPrototypeSlice(buffer, 2); + finished = true; + return onComplete(buffer); + } + var trailerEnd = BufferPrototypeIndexOf(buffer, '\r\n'); + if (trailerEnd < 0) { + if (buffer.length > MAX_CHUNK_LINE_BYTES) fail(); + return; + } + if (trailerEnd > MAX_CHUNK_LINE_BYTES) return fail(); + var trailerLine = BufferPrototypeToString(buffer, 'latin1', 0, trailerEnd); + var colon = trailerLine.indexOf(':'); + if (colon <= 0 || !isHttpToken(trailerLine.slice(0, colon))) return fail(); + buffer = BufferPrototypeSlice(buffer, trailerEnd + 2); + } + } + }; + } + + // Flat parseRequest layout (mirrors http.odin PARSE_* / vals[]): + // [state, consumed, method, url, minor, name, value, ...] + var PARSE_STATE = 0; + var PARSE_CONSUMED = 1; + var PARSE_METHOD = 2; + var PARSE_URL = 3; + var PARSE_MINOR = 4; + var PARSE_HEADERS = 5; + var PARSE_OK = 0; + var PARSE_NEED_MORE = 1; + + module.exports = { + TE_CHUNKED_RE: TE_CHUNKED_RE, + CONNECTION_CLOSE_RE: CONNECTION_CLOSE_RE, + CONNECTION_CLOSE_CS_RE: CONNECTION_CLOSE_CS_RE, + CONNECTION_KEEPALIVE_RE: CONNECTION_KEEPALIVE_RE, + TE_HAS_CHUNKED_RE: TE_HAS_CHUNKED_RE, + isHttpToken: isHttpToken, + createChunkedDecoder: createChunkedDecoder, + PARSE_STATE: PARSE_STATE, + PARSE_CONSUMED: PARSE_CONSUMED, + PARSE_METHOD: PARSE_METHOD, + PARSE_URL: PARSE_URL, + PARSE_MINOR: PARSE_MINOR, + PARSE_HEADERS: PARSE_HEADERS, + PARSE_OK: PARSE_OK, + PARSE_NEED_MORE: PARSE_NEED_MORE, + }; +}); diff --git a/pkg/runtime/js/internal/http/incoming.js b/pkg/runtime/js/internal/http/incoming.js new file mode 100644 index 0000000..96b94a1 --- /dev/null +++ b/pkg/runtime/js/internal/http/incoming.js @@ -0,0 +1,88 @@ +// IncomingMessage — the request side of node:http, built from one native parseRequest +// result. Where node keeps it: lib/_http_incoming.js (under internal/ here; see common.js +// for why there is no public `_http_incoming`). +(function (require, module) { + 'use strict'; + + // LAZY module: constructors/statics come from primordials, not live globals (#426). + var primordials = require('internal/primordials'); + var ObjectCreate = primordials.ObjectCreate; + var ObjectDefineProperty = primordials.ObjectDefineProperty; + var EventEmitter = require('events'); + var common = require('internal/http/common'); + var PARSE_METHOD = common.PARSE_METHOD; + var PARSE_URL = common.PARSE_URL; + var PARSE_MINOR = common.PARSE_MINOR; + var PARSE_HEADERS = common.PARSE_HEADERS; + + /** + * Fold `[name, value, ...]` pairs into a null-prototype headers object. + * Names are lowercased; duplicates join with `', '` (Node for most headers). + * + * @param {Array} parseResult flat parseRequest array + * @param {number} headerStart index of first name/value pair + * @returns {Object} + */ + function buildHeaders(parseResult, headerStart) { + var headers = Object.create(null); + for (var i = headerStart; i + 1 < parseResult.length; i += 2) { + var lowerName = parseResult[i].toLowerCase(); + var value = parseResult[i + 1]; + if (headers[lowerName] === undefined) headers[lowerName] = value; + else headers[lowerName] += ', ' + value; + } + return headers; + } + + /** + * @param {net.Socket} socket + * @param {Array} parseResult + * @constructor + */ + function IncomingMessage(socket, parseResult) { + EventEmitter.call(this); + this.socket = socket; + this.method = parseResult[PARSE_METHOD]; + this.url = parseResult[PARSE_URL]; + this.httpVersionMajor = 1; + this.httpVersionMinor = parseResult[PARSE_MINOR]; + this.httpVersion = '1.' + parseResult[PARSE_MINOR]; + // Eager headers (framing needs Connection/CL/TE). rawHeaders sliced on first access — + // hello-world never reads it (saves a per-request array alloc). + this._parseResult = parseResult; + this.headers = buildHeaders(parseResult, PARSE_HEADERS); + this.complete = false; + this._ended = false; + } + IncomingMessage.prototype = ObjectCreate(EventEmitter.prototype); + IncomingMessage.prototype.constructor = IncomingMessage; + + // Lazy rawHeaders: first get materializes an own data property (Node shape: + // hasOwnProperty / Object.keys include rawHeaders after access). + ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', { + configurable: true, + enumerable: true, + get: function () { + var pairs = this._parseResult.slice(PARSE_HEADERS); + Object.defineProperty(this, 'rawHeaders', { + value: pairs, + writable: true, + enumerable: true, + configurable: true, + }); + return pairs; + }, + set: function (value) { + Object.defineProperty(this, 'rawHeaders', { + value: value, + writable: true, + enumerable: true, + configurable: true, + }); + }, + }); + + module.exports = { + IncomingMessage: IncomingMessage, + }; +}); diff --git a/pkg/runtime/js/internal/http/outgoing.js b/pkg/runtime/js/internal/http/outgoing.js new file mode 100644 index 0000000..d8eaeee --- /dev/null +++ b/pkg/runtime/js/internal/http/outgoing.js @@ -0,0 +1,109 @@ +// Response-side header validation and wire encoding for node:http — the header-name / +// header-char checks and the latin1 head and chunked-body framing. Where node keeps +// validateHeaderName/Value and the outgoing framing: lib/_http_outgoing.js (under +// internal/ here; see common.js for why there is no public `_http_outgoing`). +// +// @param {{ latin1WriteInto?: Function }} native node:http's bindings (the loader hands +// this file the same object as node/http.js). +(function (require, module, exports, native) { + 'use strict'; + + /** Pristine intrinsics — response head must not use overridable Buffer methods. */ + // LAZY module: every module-eval capture comes from the loader (#426, #333). + var primordials = require('internal/primordials'); + var nodeError = require('internal/errors').nodeError; + var PristineBuffer = require.pristineBuffer; + var BufferFrom = PristineBuffer.from; + var BufferConcat = PristineBuffer.concat; + // The CAPTURED read — see common.js: a live `value.charCodeAt` lets a CR/LF-lying + // gadget split the response. + var StringPrototypeCharCodeAt = primordials.StringPrototypeCharCodeAt; + var isHttpToken = require('internal/http/common').isHttpToken; + var nativeLatin1WriteInto = + typeof native.latin1WriteInto === 'function' ? native.latin1WriteInto : null; + + /** + * @param {*} name + * @throws {TypeError} ERR_INVALID_HTTP_TOKEN + */ + function assertHeaderName(name) { + if (!isHttpToken(name)) { + throw nodeError( + primordials.TypeError, + 'ERR_INVALID_HTTP_TOKEN', + 'Header name must be a valid HTTP token [' + JSON.stringify(name) + ']', + ); + } + } + + /** + * Reject CTL (except HT) and code points above 0xFF (latin1 serialization would mask). + * + * @param {string} value + * @param {string} what label for the error message + * @throws {TypeError} ERR_INVALID_CHAR + */ + function assertValidHeaderChar(value, what) { + for (var i = 0; i < value.length; i++) { + var code = StringPrototypeCharCodeAt(value, i); + if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) { + continue; + } + throw nodeError(primordials.TypeError, 'ERR_INVALID_CHAR', 'Invalid character in ' + what); + } + } + + var CRLF = BufferFrom('\r\n', 'latin1'); + var LAST_CHUNK = BufferFrom('0\r\n\r\n', 'latin1'); + /** Max combined head+body size for single-write coalesce. */ + var HEAD_BODY_COALESCE_MAX = 64 * 1024; + + /** + * Write `text` as latin1 into `destination` at `offset`. + * + * @param {Uint8Array} destination + * @param {string} text + * @param {number} offset + */ + function writeLatin1Into(destination, text, offset) { + if (nativeLatin1WriteInto !== null) { + nativeLatin1WriteInto(destination, text, offset, text.length); + return; + } + for (var i = 0; i < text.length; i++) { + destination[offset + i] = primordials.StringPrototypeCharCodeAt(text, i) & 0xff; + } + } + + /** + * @param {string} headText latin1 head string + * @returns {Uint8Array} + */ + function encodeHeadBytes(headText) { + var bytes = new primordials.Uint8Array(headText.length); + writeLatin1Into(bytes, headText, 0); + return bytes; + } + + /** + * @param {Buffer|Uint8Array} bodyChunk + * @returns {Buffer} + */ + function frameChunkedBody(bodyChunk) { + return BufferConcat([ + BufferFrom(bodyChunk.length.toString(16) + '\r\n', 'latin1'), + bodyChunk, + CRLF, + ]); + } + + module.exports = { + assertHeaderName: assertHeaderName, + assertValidHeaderChar: assertValidHeaderChar, + LAST_CHUNK: LAST_CHUNK, + HEAD_BODY_COALESCE_MAX: HEAD_BODY_COALESCE_MAX, + writeLatin1Into: writeLatin1Into, + encodeHeadBytes: encodeHeadBytes, + frameChunkedBody: frameChunkedBody, + }; +}); diff --git a/pkg/runtime/js/internal/http/server.js b/pkg/runtime/js/internal/http/server.js new file mode 100644 index 0000000..2337f85 --- /dev/null +++ b/pkg/runtime/js/internal/http/server.js @@ -0,0 +1,839 @@ +// ServerResponse, the per-connection request/response loop, and Server — the server half +// of node:http. Where node keeps them: lib/_http_server.js (under internal/ here; see +// common.js for why there is no public `_http_server`). createServer stays in node/http.js, +// as it does in node. +// +// @param {{ parseRequest: Function }} native node:http's bindings (the loader hands this +// file the same object as node/http.js, which has already checked parseRequest exists). +(function (require, module, exports, native) { + 'use strict'; + + // This module is LAZY, so every module-eval constructor/static below comes from the + // loader — a read of the live global here runs after user code could have replaced it + // (#426). + var primordials = require('internal/primordials'); + var ObjectCreate = primordials.ObjectCreate; + var nodeError = require('internal/errors').nodeError; + + var EventEmitter = require('events'); + // Pristine, from the loader: this module is LAZY, so its own capture would run after + // user code and be steerable (#333). + var PristineBuffer = require.pristineBuffer; + var BufferFrom = PristineBuffer.from; + var BufferAlloc = PristineBuffer.alloc; + var BufferConcat = PristineBuffer.concat; + var net = require('net'); + // Framing predicates and conversions: why each is a primordial and not a live read is + // written once, in common.js, where the chunked decoder uses the same set. + var reTest = primordials.RegExpMatches; + var allChars = primordials.allChars; + var isAsciiDigit = primordials.isAsciiDigit; + // Content-Length conversion. Pinned by the parseint http-smoke phase and the parseIntG + // mutation entry: a sound digit check beside a live parseInt still desyncs framing. + var parseIntG = primordials.parseInt; + var NumberG = primordials.Number; + var NumberIsInteger = primordials.NumberIsInteger; + + var common = require('internal/http/common'); + var TE_CHUNKED_RE = common.TE_CHUNKED_RE; + var CONNECTION_CLOSE_RE = common.CONNECTION_CLOSE_RE; + var CONNECTION_CLOSE_CS_RE = common.CONNECTION_CLOSE_CS_RE; + var CONNECTION_KEEPALIVE_RE = common.CONNECTION_KEEPALIVE_RE; + var TE_HAS_CHUNKED_RE = common.TE_HAS_CHUNKED_RE; + var createChunkedDecoder = common.createChunkedDecoder; + var PARSE_STATE = common.PARSE_STATE; + var PARSE_CONSUMED = common.PARSE_CONSUMED; + var PARSE_OK = common.PARSE_OK; + var PARSE_NEED_MORE = common.PARSE_NEED_MORE; + var IncomingMessage = require('internal/http/incoming').IncomingMessage; + var outgoing = require('internal/http/outgoing'); + var assertHeaderName = outgoing.assertHeaderName; + var assertValidHeaderChar = outgoing.assertValidHeaderChar; + var LAST_CHUNK = outgoing.LAST_CHUNK; + var HEAD_BODY_COALESCE_MAX = outgoing.HEAD_BODY_COALESCE_MAX; + var writeLatin1Into = outgoing.writeLatin1Into; + var encodeHeadBytes = outgoing.encodeHeadBytes; + var frameChunkedBody = outgoing.frameChunkedBody; + + /** @const {number} Max request-head size before 431. */ + var MAX_HEAD_BYTES = 64 * 1024; + + var STATUS_CODES = { + 200: 'OK', + 201: 'Created', + 204: 'No Content', + 206: 'Partial Content', + 301: 'Moved Permanently', + 302: 'Found', + 304: 'Not Modified', + 307: 'Temporary Redirect', + 400: 'Bad Request', + 401: 'Unauthorized', + 403: 'Forbidden', + 404: 'Not Found', + 405: 'Method Not Allowed', + 408: 'Request Timeout', + 411: 'Length Required', + 413: 'Payload Too Large', + 414: 'URI Too Long', + 431: 'Request Header Fields Too Large', + 500: 'Internal Server Error', + 501: 'Not Implemented', + 503: 'Service Unavailable', + }; + + /** + * @param {*} statusCode may be string after `res.statusCode = '204'` + * @returns {boolean} + */ + function statusHasNoBody(statusCode) { + var code = NumberG(statusCode); + return code === 204 || code === 304 || (code >= 100 && code < 200); + } + + /** + * @param {*} statusCode + * @returns {number} + * @throws {RangeError} ERR_HTTP_INVALID_STATUS_CODE + */ + function validateStatusCode(statusCode) { + var code = NumberG(statusCode); + if (!NumberIsInteger(code) || code < 100 || code > 999) { + throw nodeError( + primordials.RangeError, + 'ERR_HTTP_INVALID_STATUS_CODE', + 'Invalid status code: ' + JSON.stringify(statusCode), + ); + } + return code; + } + + var STATUS_LINE_200 = 'HTTP/1.1 200 OK\r\n'; + var CONNECTION_KEEP_ALIVE = 'Connection: keep-alive\r\n'; + var CONNECTION_CLOSE = 'Connection: close\r\n'; + + /** + * @param {net.Socket} socket + * @param {string} method + * @param {number|undefined} httpMinor + * @constructor + */ + function ServerResponse(socket, method, httpMinor) { + EventEmitter.call(this); + this.socket = socket; + this.statusCode = 200; + this.statusMessage = undefined; + this.headersSent = false; + this._chunked = false; + this.finished = false; + /** @type {Object} lowercased name → value */ + this._headerValues = Object.create(null); + /** @type {Object} lowercased name → wire-case name */ + this._headerNames = Object.create(null); + this._isHead = method === 'HEAD'; + this._allowChunked = httpMinor === undefined || httpMinor >= 1; + this._keepAlive = false; + /** @type {((function(): void) | null)} connection hook after finish */ + this._onComplete = null; + // Forward the socket's 'drain' to this response for the lifetime of the + // response, so res.write() === false + res.once('drain', ...) is a real + // backpressure contract (the native layer owes a 'drain' whenever a write + // buffered past the HWM). Removed in end() — on a keep-alive connection the + // next response gets its own forwarder on the same socket. + var self = this; + this._drainForwarder = null; + if (socket && typeof socket.on === 'function') { + this._drainForwarder = function () { + self.emit('drain'); + }; + socket.on('drain', this._drainForwarder); + } + } + ServerResponse.prototype = ObjectCreate(EventEmitter.prototype); + ServerResponse.prototype.constructor = ServerResponse; + + /** + * @param {string} name + * @param {*} value + * @returns {ServerResponse} + */ + ServerResponse.prototype.setHeader = function (name, value) { + assertHeaderName(name); + var text = String(value); + assertValidHeaderChar(text, 'header content [' + name + ']'); + var lowerName = name.toLowerCase(); + this._headerValues[lowerName] = text; + this._headerNames[lowerName] = name; + return this; + }; + + /** + * @param {string} name + * @returns {string|undefined} + */ + ServerResponse.prototype.getHeader = function (name) { + return this._headerValues[String(name).toLowerCase()]; + }; + + /** + * @param {string} name + */ + ServerResponse.prototype.removeHeader = function (name) { + var lowerName = String(name).toLowerCase(); + delete this._headerValues[lowerName]; + delete this._headerNames[lowerName]; + }; + + /** + * @param {string} name + * @returns {boolean} + */ + ServerResponse.prototype.hasHeader = function (name) { + return this._headerValues[String(name).toLowerCase()] !== undefined; + }; + + /** + * @param {number} statusCode + * @param {string|object} [statusMessage] + * @param {object} [headers] + * @returns {ServerResponse} + */ + ServerResponse.prototype.writeHead = function (statusCode, statusMessage, headers) { + if (typeof statusMessage === 'object' && statusMessage !== null) { + headers = statusMessage; + statusMessage = undefined; + } + this.statusCode = validateStatusCode(statusCode); + if (statusMessage) this.statusMessage = statusMessage; + if (headers) { + for (var name in headers) { + if (Object.prototype.hasOwnProperty.call(headers, name)) { + this.setHeader(name, headers[name]); + } + } + } + return this; + }; + + /** + * Serialize status line + headers; finalizes `_keepAlive`. Does not write or set headersSent. + * + * @returns {string} latin1 head text + */ + ServerResponse.prototype._buildHead = function () { + var statusCode = validateStatusCode(this.statusCode); + var reason = this.statusMessage || STATUS_CODES[statusCode] || ''; + assertValidHeaderChar(String(reason), 'statusMessage'); + var head = + statusCode === 200 && !this.statusMessage + ? STATUS_LINE_200 + : 'HTTP/1.1 ' + statusCode + ' ' + reason + '\r\n'; + + var selfDelimited = + this._chunked || + this.hasHeader('content-length') || + this._isHead || + statusHasNoBody(this.statusCode); + if (!selfDelimited) this._keepAlive = false; + + if (this.hasHeader('connection')) { + if (reTest(CONNECTION_CLOSE_RE, this.getHeader('connection'))) this._keepAlive = false; + } else { + head += this._keepAlive ? CONNECTION_KEEP_ALIVE : CONNECTION_CLOSE; + } + + for (var lowerName in this._headerValues) { + head += this._headerNames[lowerName] + ': ' + this._headerValues[lowerName] + '\r\n'; + } + head += '\r\n'; + return head; + }; + + ServerResponse.prototype._flushHead = function () { + if (this.headersSent) return; + var headBytes = encodeHeadBytes(this._buildHead()); + this.headersSent = true; + this.socket.write(headBytes); + }; + + /** + * @param {string|Buffer|Uint8Array} [chunk] + * @param {string|function} [encoding] + * @param {function} [callback] + * @returns {boolean} + */ + ServerResponse.prototype.write = function (chunk, encoding, callback) { + if (typeof encoding === 'function') { + callback = encoding; + encoding = undefined; + } + var omitBody = this._isHead || statusHasNoBody(this.statusCode); + if (!this.headersSent) { + if ( + !omitBody && + this._allowChunked && + !this.hasHeader('content-length') && + !this.hasHeader('transfer-encoding') + ) { + this._chunked = true; + this.setHeader('Transfer-Encoding', 'chunked'); + } else if (this._allowChunked && this.hasHeader('transfer-encoding')) { + this._chunked = reTest(TE_HAS_CHUNKED_RE, this.getHeader('transfer-encoding')); + } + this._flushHead(); + } + var ok = true; + if (!omitBody && chunk && chunk.length) { + var bytes = typeof chunk === 'string' ? BufferFrom(chunk, encoding || 'utf8') : chunk; + // The socket's return value IS the backpressure signal: false means the + // native write buffer crossed its high-water mark and a 'drain' is owed + // (forwarded to this response by _drainForwarder). Swallowing it here + // would let a fast handler buffer without bound against a slow client. + ok = this.socket.write(this._chunked ? frameChunkedBody(bytes) : bytes); + } + if (typeof callback === 'function') process.nextTick(callback); + return ok; + }; + + /** + * @param {string|Buffer|Uint8Array|function} [chunk] + * @param {string|function} [encoding] + * @param {function} [callback] + * @returns {ServerResponse} + */ + ServerResponse.prototype.end = function (chunk, encoding, callback) { + if (typeof chunk === 'function') { + callback = chunk; + chunk = undefined; + } else if (typeof encoding === 'function') { + callback = encoding; + encoding = undefined; + } + if (this.finished) return this; + + var body = null; + if (chunk !== undefined && chunk !== null) { + body = typeof chunk === 'string' ? BufferFrom(chunk, encoding || 'utf8') : chunk; + } + var omitBody = this._isHead || statusHasNoBody(this.statusCode); + if (!this.headersSent) { + if (!this.hasHeader('content-length') && !this.hasHeader('transfer-encoding') && !omitBody) { + this.setHeader('Content-Length', String(body ? body.length : 0)); + } + if (this._allowChunked && this.hasHeader('transfer-encoding')) { + this._chunked = reTest(TE_HAS_CHUNKED_RE, this.getHeader('transfer-encoding')); + } + + var headText = this._buildHead(); + var totalBytes = headText.length + (body ? body.length : 0); + if ( + !omitBody && + body && + body.length && + body instanceof primordials.Uint8Array && + !this._chunked && + totalBytes <= HEAD_BODY_COALESCE_MAX + ) { + var combined = new primordials.Uint8Array(totalBytes); + writeLatin1Into(combined, headText, 0); + primordials.Uint8ArrayPrototypeSet(combined, body, headText.length); + this.headersSent = true; + this.socket.write(combined); + body = null; + } else { + this.headersSent = true; + this.socket.write(encodeHeadBytes(headText)); + } + } + if (!omitBody && body && body.length) { + this.socket.write(this._chunked ? frameChunkedBody(body) : body); + } + if (this._chunked && !omitBody) this.socket.write(LAST_CHUNK); + this.finished = true; + if (this._drainForwarder && typeof this.socket.removeListener === 'function') { + this.socket.removeListener('drain', this._drainForwarder); + this._drainForwarder = null; + } + if (typeof callback === 'function') process.nextTick(callback); + this.emit('finish'); + if (this._onComplete) this._onComplete(); + else this.socket.end(); + return this; + }; + + /** + * @param {number} httpMinor + * @param {string|undefined} connectionHeader + * @returns {boolean} + */ + function shouldKeepAlive(httpMinor, connectionHeader) { + var connection = (connectionHeader || '').toLowerCase(); + if (httpMinor >= 1) return !reTest(CONNECTION_CLOSE_CS_RE, connection); + return reTest(CONNECTION_KEEPALIVE_RE, connection); + } + + var EMPTY_BUFFER = BufferAlloc(0); + var DEADLINE_SWEEP_MS = 100; + + /** + * @param {Server} server + * @param {object} deadlines + */ + function registerConnectionDeadlines(server, deadlines) { + var sweep = server._connSweep || (server._connSweep = { connections: new Set(), timer: null }); + sweep.connections.add(deadlines); + if (sweep.timer === null) { + sweep.timer = setInterval(function () { + tickConnectionDeadlines(sweep); + }, DEADLINE_SWEEP_MS); + } + } + + /** + * @param {Server} server + * @param {object} deadlines + */ + function unregisterConnectionDeadlines(server, deadlines) { + var sweep = server._connSweep; + if (!sweep) return; + sweep.connections.delete(deadlines); + if (sweep.connections.size === 0 && sweep.timer !== null) { + clearInterval(sweep.timer); + sweep.timer = null; + } + } + + /** + * @param {{ connections: Set, timer: * }} sweep + */ + function tickConnectionDeadlines(sweep) { + var now = Date.now(); + sweep.connections.forEach(function (deadlines) { + if (deadlines.idleUntil !== 0 && now >= deadlines.idleUntil) { + deadlines.idleUntil = 0; + deadlines.onIdleTimeout(); + } else if (deadlines.headersUntil !== 0 && now >= deadlines.headersUntil) { + deadlines.headersUntil = 0; + deadlines.onRequestTimeout(); + } else if (deadlines.requestUntil !== 0 && now >= deadlines.requestUntil) { + deadlines.requestUntil = 0; + deadlines.onRequestTimeout(); + } + }); + } + + /** + * Per-connection request/response loop. + * + * @param {Server} server + * @param {net.Socket} socket + */ + function onConnection(server, socket) { + var pendingBytes = EMPTY_BUFFER; + var partialHeadLength = 0; + var parsingHead = true; + var request = null; + var response = null; + var contentLengthRemaining = 0; + var chunkedDecoder = null; + var requestBodyComplete = false; + var responseComplete = false; + var peerHalfClosed = false; + var connectionClosed = false; + + var keepAliveTimeoutMs = server.keepAliveTimeout || 0; + var headersTimeoutMs = server.headersTimeout || 0; + var requestTimeoutMs = server.requestTimeout || 0; + var deadlines = { + idleUntil: 0, + headersUntil: 0, + requestUntil: 0, + onIdleTimeout: destroyConnection, + onRequestTimeout: onReceiveTimeout, + }; + var receivingRequest = false; + registerConnectionDeadlines(server, deadlines); + + // Deferred body/'end' delivery: one reused nextTick callback + parallel arrays + // (no per-request closure). Pipelined sync processRequestHead can enqueue more + // while flush runs; deferScheduled stays true until the queue is empty. + var deferRequests = []; + var deferBodies = []; + var deferKinds = []; // 0 = emit end, 1 = Content-Length feed, 2 = chunked feed + var deferDecoders = []; + var deferScheduled = false; + var deferFlushError = undefined; + var DEFER_END = 0; + var DEFER_CONTENT_LENGTH = 1; + var DEFER_CHUNKED = 2; + + function scheduleAfterRequest(targetRequest, bodyFromHead, kind, decoder) { + deferRequests.push(targetRequest); + deferBodies.push(bodyFromHead); + deferKinds.push(kind); + deferDecoders.push(decoder); + if (!deferScheduled) { + deferScheduled = true; + process.nextTick(flushAfterRequest); + } + } + + function flushAfterRequest() { + // try/finally: a throw from a user 'end'/'data' listener must not latch + // deferScheduled=true forever (which would skip later nextTick arms). + deferFlushError = undefined; + try { + var index = 0; + while (index < deferRequests.length) { + var targetRequest = deferRequests[index]; + var bodyFromHead = deferBodies[index]; + var kind = deferKinds[index]; + var decoder = deferDecoders[index]; + index++; + if (connectionClosed) continue; + try { + if (kind === DEFER_END) { + emitRequestEnd(targetRequest); + maybeAdvance(); + } else if (kind === DEFER_CHUNKED) { + decoder(bodyFromHead); + } else { + feedContentLengthBody(bodyFromHead); + } + } catch (err) { + // Drain siblings first; rethrow the first error after cleanup. + if (deferFlushError === undefined) deferFlushError = err; + } + } + } finally { + deferRequests.length = 0; + deferBodies.length = 0; + deferKinds.length = 0; + deferDecoders.length = 0; + deferScheduled = false; + var pendingError = deferFlushError; + deferFlushError = undefined; + if (pendingError !== undefined) throw pendingError; + } + } + + function clearReceiveDeadlines() { + deadlines.headersUntil = 0; + deadlines.requestUntil = 0; + } + + function clearAllDeadlines() { + deadlines.idleUntil = 0; + clearReceiveDeadlines(); + unregisterConnectionDeadlines(server, deadlines); + } + + function destroyConnection() { + if (connectionClosed) return; + connectionClosed = true; + clearAllDeadlines(); + socket.destroy(); + } + + function armIdleDeadline() { + if (keepAliveTimeoutMs > 0) deadlines.idleUntil = Date.now() + keepAliveTimeoutMs; + } + + function beginReceiveDeadlines() { + receivingRequest = true; + if (headersTimeoutMs > 0 || requestTimeoutMs > 0) { + var now = Date.now(); + if (headersTimeoutMs > 0) deadlines.headersUntil = now + headersTimeoutMs; + if (requestTimeoutMs > 0) deadlines.requestUntil = now + requestTimeoutMs; + } + } + + function onReceiveTimeout() { + if (connectionClosed) return; + if (response && response.headersSent) destroyConnection(); + else sendErrorAndClose(408); + } + + /** + * @param {number} statusCode + */ + function sendErrorAndClose(statusCode) { + if (connectionClosed) return; + connectionClosed = true; + clearAllDeadlines(); + var reason = STATUS_CODES[statusCode] || 'Error'; + socket.write( + BufferFrom( + 'HTTP/1.1 ' + + statusCode + + ' ' + + reason + + '\r\nConnection: close\r\nContent-Length: 0\r\n\r\n', + 'latin1', + ), + ); + socket.end(); + } + + function resetForNextRequest() { + parsingHead = true; + partialHeadLength = 0; + request = null; + response = null; + contentLengthRemaining = 0; + chunkedDecoder = null; + requestBodyComplete = false; + responseComplete = false; + receivingRequest = false; + } + + /** + * Advance when both request body and response are done (keep-alive or close). + */ + function maybeAdvance() { + if (connectionClosed || !requestBodyComplete || !responseComplete) return; + if (!response._keepAlive) { + connectionClosed = true; + clearAllDeadlines(); + socket.end(); + return; + } + resetForNextRequest(); + if (pendingBytes.length > 0) { + beginReceiveDeadlines(); + processRequestHead(); + } else if (peerHalfClosed) { + destroyConnection(); + } else { + armIdleDeadline(); + } + } + + function onResponseComplete() { + responseComplete = true; + maybeAdvance(); + } + + /** + * Mark the request fully received (timers off, pipelined leftover stashed). + * Does not emit `'end'`. + * + * @param {Buffer|Uint8Array|undefined} leftover + */ + function markRequestReceived(leftover) { + pendingBytes = leftover && leftover.length ? leftover : EMPTY_BUFFER; + clearReceiveDeadlines(); + requestBodyComplete = true; + } + + /** + * Emit request `'end'` once. Accepts a captured request when outer `request` + * was cleared by a synchronous keep-alive advance. + * + * @param {IncomingMessage|null} [targetRequest] + */ + function emitRequestEnd(targetRequest) { + var message = targetRequest || request; + if (message && !message._ended) { + message._ended = true; + message.complete = true; + message.emit('end'); + } + } + + /** + * Full body completion: mark received (if needed), emit end, try advance. + * + * @param {Buffer|Uint8Array|undefined} leftover + */ + function onRequestBodyComplete(leftover) { + if (!requestBodyComplete) markRequestReceived(leftover); + emitRequestEnd(request); + maybeAdvance(); + } + + /** + * @param {Buffer|Uint8Array|undefined} chunk + */ + function feedContentLengthBody(chunk) { + if (!request || request._ended) return; + if (contentLengthRemaining > 0 && chunk && chunk.length) { + var take = chunk.length < contentLengthRemaining ? chunk.length : contentLengthRemaining; + request.emit('data', chunk.slice(0, take)); + contentLengthRemaining -= take; + chunk = chunk.slice(take); + } + if (contentLengthRemaining <= 0) onRequestBodyComplete(chunk); + } + + function processRequestHead() { + var parseResult = native.parseRequest(pendingBytes, partialHeadLength); + var state = parseResult[PARSE_STATE]; + if (state === PARSE_NEED_MORE) { + if (pendingBytes.length > MAX_HEAD_BYTES) sendErrorAndClose(431); + else partialHeadLength = pendingBytes.length; + return; + } + if (state !== PARSE_OK) return sendErrorAndClose(400); + + var consumed = parseResult[PARSE_CONSUMED]; + if (consumed > MAX_HEAD_BYTES) return sendErrorAndClose(431); + + parsingHead = false; + partialHeadLength = 0; + deadlines.headersUntil = 0; + + request = new IncomingMessage(socket, parseResult); + response = new ServerResponse(socket, request.method, request.httpVersionMinor); + response._keepAlive = shouldKeepAlive( + request.httpVersionMinor, + request.headers['connection'], + ); + response._onComplete = onResponseComplete; + + var bodyFromHead = + consumed < pendingBytes.length ? pendingBytes.slice(consumed) : EMPTY_BUFFER; + pendingBytes = EMPTY_BUFFER; + + var transferEncoding = request.headers['transfer-encoding']; + var contentLengthHeader = request.headers['content-length']; + if (transferEncoding !== undefined) { + if (contentLengthHeader !== undefined) return sendErrorAndClose(400); + if (!reTest(TE_CHUNKED_RE, transferEncoding)) return sendErrorAndClose(501); + chunkedDecoder = createChunkedDecoder( + request, + function () { + if (response.headersSent) destroyConnection(); + else sendErrorAndClose(400); + }, + onRequestBodyComplete, + ); + } else if (contentLengthHeader !== undefined) { + if (!allChars(contentLengthHeader, isAsciiDigit)) return sendErrorAndClose(400); + contentLengthRemaining = parseIntG(contentLengthHeader, 10); + } + + var hasEntityBody = chunkedDecoder !== null || contentLengthRemaining > 0; + // Capture before emit: sync res.end() may reset outer `request` via maybeAdvance. + var capturedRequest = request; + var capturedBody = bodyFromHead; + var capturedDecoder = chunkedDecoder; + var deferKind = !hasEntityBody + ? DEFER_END + : chunkedDecoder + ? DEFER_CHUNKED + : DEFER_CONTENT_LENGTH; + + // Bodyless: mark receive-complete before 'request' so sync res.end() can reuse + // the connection same turn. requestTimeout is receive-only (Node parity). + if (!hasEntityBody) { + markRequestReceived(bodyFromHead.length ? bodyFromHead : EMPTY_BUFFER); + } + + server.emit('request', request, response); + // After emit so a throwing handler does not leave a stray deferred delivery. + // 'end' / entity bytes still on nextTick for listeners attached in the handler. + scheduleAfterRequest(capturedRequest, capturedBody, deferKind, capturedDecoder); + } + + socket.on('data', function (chunk) { + if (connectionClosed) return; + deadlines.idleUntil = 0; + if (!receivingRequest) beginReceiveDeadlines(); + if (parsingHead) { + pendingBytes = pendingBytes.length ? BufferConcat([pendingBytes, chunk]) : chunk; + processRequestHead(); + } else if (requestBodyComplete) { + pendingBytes = pendingBytes.length ? BufferConcat([pendingBytes, chunk]) : chunk; + } else if (chunkedDecoder) { + chunkedDecoder(chunk); + } else { + feedContentLengthBody(chunk); + } + }); + + socket.on('end', function () { + peerHalfClosed = true; + process.nextTick(function () { + if (connectionClosed) return; + if (parsingHead) { + if (pendingBytes.length > 0) sendErrorAndClose(400); + else destroyConnection(); + } else if (request && !request._ended) { + if (response && response.headersSent) destroyConnection(); + else sendErrorAndClose(400); + } + }); + }); + + socket.on('error', function () { + connectionClosed = true; + clearAllDeadlines(); + }); + + armIdleDeadline(); + } + + /** + * @param {object|function} [options] + * @param {function} [requestListener] + * @constructor + */ + function Server(options, requestListener) { + if (typeof options === 'function') { + requestListener = options; + options = {}; + } + EventEmitter.call(this); + var self = this; + this.keepAliveTimeout = 5000; + this.headersTimeout = 60000; + this.requestTimeout = 300000; + var tlsContext; + if (options && typeof options === 'object') { + if (typeof options.keepAliveTimeout === 'number') { + this.keepAliveTimeout = options.keepAliveTimeout; + } + if (typeof options.headersTimeout === 'number') { + this.headersTimeout = options.headersTimeout; + } + if (typeof options.requestTimeout === 'number') { + this.requestTimeout = options.requestTimeout; + } + tlsContext = options.tls; + } + this._net = net.createServer({ tls: tlsContext }, function (socket) { + onConnection(self, socket); + }); + this._net.on('listening', function () { + self.emit('listening'); + }); + this._net.on('close', function () { + self.emit('close'); + }); + this._net.on('error', function (err) { + self.emit('error', err); + }); + if (typeof requestListener === 'function') this.on('request', requestListener); + } + Server.prototype = ObjectCreate(EventEmitter.prototype); + Server.prototype.constructor = Server; + + Server.prototype.listen = function () { + this._net.listen.apply(this._net, arguments); + return this; + }; + Server.prototype.close = function (callback) { + this._net.close(callback); + return this; + }; + Server.prototype.address = function () { + return this._net.address(); + }; + + module.exports = { + STATUS_CODES: STATUS_CODES, + ServerResponse: ServerResponse, + Server: Server, + }; +}); diff --git a/pkg/runtime/js/node/http.js b/pkg/runtime/js/node/http.js index b69b1f0..5489e79 100644 --- a/pkg/runtime/js/node/http.js +++ b/pkg/runtime/js/node/http.js @@ -13,1116 +13,19 @@ (function (require, module, exports, native) { 'use strict'; - /** Pristine intrinsics — response head must not use overridable Buffer methods. */ - // Also every module-eval constructor/static below: this module is LAZY, so a read of the - // live global here runs after user code could have replaced it (#426). + // The implementation lives in js/internal/http/{common,incoming,outgoing,server}.js, + // laid out like node's _http_common/_http_incoming/_http_outgoing/_http_server; this + // file stays the public entry and keeps node's createServer here. The loader hands + // outgoing.js and server.js these same bindings, so the platform check below guards + // them too: it runs before either is required. var primordials = require('internal/primordials'); - var ObjectCreate = primordials.ObjectCreate; - var ObjectDefineProperty = primordials.ObjectDefineProperty; - var nodeError = require('internal/errors').nodeError; if (!native || typeof native.parseRequest !== 'function') { throw new primordials.Error('node:http is unavailable on this platform'); } - var EventEmitter = require('events'); - // Pristine, from the loader: this module is LAZY, so its own capture would run after - // user code and be steerable (#333). - var PristineBuffer = require.pristineBuffer; - var BufferFrom = PristineBuffer.from; - var BufferAlloc = PristineBuffer.alloc; - var BufferConcat = PristineBuffer.concat; - // The chunked decoder frames NETWORK-CONTROLLED bytes; every read below was live off - // `Buffer.prototype`. Measured before this: replacing `toString` so a size line "5" reads - // as "1" turned a valid chunked POST into 400 here, while node (llhttp, in C) delivered - // the body unchanged. - var BufferPrototypeToString = PristineBuffer.bufferToString; - var BufferPrototypeIndexOf = PristineBuffer.bufferIndexOf; - var BufferPrototypeSlice = PristineBuffer.bufferSlice; - var net = require('net'); - // Every regex below decides FRAMING over attacker-controlled bytes, so none of - // them may go through `re.test(...)`: `RegExp.prototype.exec` is a writable data - // property, and the spec's RegExpExec re-reads it off the receiver, so a plain - // assignment steers `test` too — including a captured one. Measured over a real - // socket before this changed: `Content-Length: abc` answered 200 OK and - // `Transfer-Encoding: gzip` was accepted as chunked. There is deliberately no - // `RegExpPrototypeTest` in primordials; `RegExpMatches` is the only spelling, and - // it lives there rather than here because this file, url.js and fetch.js each - // grew a private copy during the migration and fetch.js's was inverted. - // The character-class validators route through ONE shared predicate. They were - // written privately into this file and url.js in the same commit that centralised - // RegExpMatches to stop exactly that, and had already diverged when they landed. - var reTest = primordials.RegExpMatches; - var allChars = primordials.allChars; - var isAsciiDigit = primordials.isAsciiDigit; - var isAsciiHexDigit = primordials.isAsciiHexDigit; - // The CAPTURED read. `String.prototype.charCodeAt` is a writable data property - // like any other, so a hand-rolled charCode scan is only as sound as the - // primitive it scans with — routing this file's validators off RegExp said - // nothing about that. Measured: a gadget reporting 0x41 for CR and LF turned - // `res.setHeader('Location', decodeURIComponent(req.url))` into a split response - // driven by a REMOTE request line, while the exec and test poison phases passed - // throughout because they never touch this carrier. - var StringPrototypeCharCodeAt = primordials.StringPrototypeCharCodeAt; - // Bootstrap-captured free globals from primordials — NOT `parseInt` at this - // factory's own eval. This module is lazy; a free `var parseIntG = parseInt` - // here binds whatever `globalThis.parseInt` is when the first `require('http')` - // runs, which is after a dependency can already have replaced it. Hardening the - // VALIDATOR and leaving the CONVERSION live closes nothing: with parseInt - // replaced, the digit check still answers correctly and `contentLengthRemaining` - // becomes whatever the gadget returns, so a crafted request's pipelined bytes are - // framed as a second request. Measured as a desync against the real server. - // Pinned by the parseint http-smoke phase and the parseIntG mutation entry. - var parseIntG = primordials.parseInt; - var NumberG = primordials.Number; - var NumberIsInteger = primordials.NumberIsInteger; - var NumberIsSafeInteger = primordials.NumberIsSafeInteger; - - // Hoisted so the literals are not re-created per request, and so every framing - // pattern is visible in one place rather than inline at four call sites. - var CHUNK_EXT_RE = /^;[^\s;]/; - var TE_CHUNKED_RE = /^\s*chunked\s*$/i; - var CONNECTION_CLOSE_RE = /\bclose\b/i; - var CONNECTION_CLOSE_CS_RE = /\bclose\b/; - var CONNECTION_KEEPALIVE_RE = /\bkeep-alive\b/; - var TE_HAS_CHUNKED_RE = /\bchunked\b/i; - var nativeLatin1WriteInto = - typeof native.latin1WriteInto === 'function' ? native.latin1WriteInto : null; - - /** @const {number} Max request-head size before 431. */ - var MAX_HEAD_BYTES = 64 * 1024; - - var STATUS_CODES = { - 200: 'OK', - 201: 'Created', - 204: 'No Content', - 206: 'Partial Content', - 301: 'Moved Permanently', - 302: 'Found', - 304: 'Not Modified', - 307: 'Temporary Redirect', - 400: 'Bad Request', - 401: 'Unauthorized', - 403: 'Forbidden', - 404: 'Not Found', - 405: 'Method Not Allowed', - 408: 'Request Timeout', - 411: 'Length Required', - 413: 'Payload Too Large', - 414: 'URI Too Long', - 431: 'Request Header Fields Too Large', - 500: 'Internal Server Error', - 501: 'Not Implemented', - 503: 'Service Unavailable', - }; - - /** - * Fold `[name, value, ...]` pairs into a null-prototype headers object. - * Names are lowercased; duplicates join with `', '` (Node for most headers). - * - * @param {Array} parseResult flat parseRequest array - * @param {number} headerStart index of first name/value pair - * @returns {Object} - */ - function buildHeaders(parseResult, headerStart) { - var headers = Object.create(null); - for (var i = headerStart; i + 1 < parseResult.length; i += 2) { - var lowerName = parseResult[i].toLowerCase(); - var value = parseResult[i + 1]; - if (headers[lowerName] === undefined) headers[lowerName] = value; - else headers[lowerName] += ', ' + value; - } - return headers; - } - - /** - * RFC 7230 tchar bitmap for field-names (setHeader + chunked trailers). - * @type {Uint8Array} - */ - var HTTP_TCHAR = new primordials.Uint8Array(128); - (function initHttpTchar() { - var chars = "!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz|~"; - for (var i = 0; i < chars.length; i++) HTTP_TCHAR[StringPrototypeCharCodeAt(chars, i)] = 1; - })(); - - /** - * @param {string} value - * @returns {boolean} - */ - function isHttpToken(value) { - if (typeof value !== 'string' || value.length === 0) return false; - for (var i = 0; i < value.length; i++) { - var code = StringPrototypeCharCodeAt(value, i); - if (code >= 128 || !HTTP_TCHAR[code]) return false; - } - return true; - } - - /** - * @param {*} name - * @throws {TypeError} ERR_INVALID_HTTP_TOKEN - */ - function assertHeaderName(name) { - if (!isHttpToken(name)) { - throw nodeError( - primordials.TypeError, - 'ERR_INVALID_HTTP_TOKEN', - 'Header name must be a valid HTTP token [' + JSON.stringify(name) + ']', - ); - } - } - - /** - * Reject CTL (except HT) and code points above 0xFF (latin1 serialization would mask). - * - * @param {string} value - * @param {string} what label for the error message - * @throws {TypeError} ERR_INVALID_CHAR - */ - function assertValidHeaderChar(value, what) { - for (var i = 0; i < value.length; i++) { - var code = StringPrototypeCharCodeAt(value, i); - if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) { - continue; - } - throw nodeError(primordials.TypeError, 'ERR_INVALID_CHAR', 'Invalid character in ' + what); - } - } - - /** - * @param {*} statusCode may be string after `res.statusCode = '204'` - * @returns {boolean} - */ - function statusHasNoBody(statusCode) { - var code = NumberG(statusCode); - return code === 204 || code === 304 || (code >= 100 && code < 200); - } - - /** - * @param {*} statusCode - * @returns {number} - * @throws {RangeError} ERR_HTTP_INVALID_STATUS_CODE - */ - function validateStatusCode(statusCode) { - var code = NumberG(statusCode); - if (!NumberIsInteger(code) || code < 100 || code > 999) { - throw nodeError( - primordials.RangeError, - 'ERR_HTTP_INVALID_STATUS_CODE', - 'Invalid status code: ' + JSON.stringify(statusCode), - ); - } - return code; - } - - var CRLF = BufferFrom('\r\n', 'latin1'); - var LAST_CHUNK = BufferFrom('0\r\n\r\n', 'latin1'); - /** Max combined head+body size for single-write coalesce. */ - var HEAD_BODY_COALESCE_MAX = 64 * 1024; - - /** - * Write `text` as latin1 into `destination` at `offset`. - * - * @param {Uint8Array} destination - * @param {string} text - * @param {number} offset - */ - function writeLatin1Into(destination, text, offset) { - if (nativeLatin1WriteInto !== null) { - nativeLatin1WriteInto(destination, text, offset, text.length); - return; - } - for (var i = 0; i < text.length; i++) { - destination[offset + i] = primordials.StringPrototypeCharCodeAt(text, i) & 0xff; - } - } - - /** - * @param {string} headText latin1 head string - * @returns {Uint8Array} - */ - function encodeHeadBytes(headText) { - var bytes = new primordials.Uint8Array(headText.length); - writeLatin1Into(bytes, headText, 0); - return bytes; - } - - /** - * @param {Buffer|Uint8Array} bodyChunk - * @returns {Buffer} - */ - function frameChunkedBody(bodyChunk) { - return BufferConcat([ - BufferFrom(bodyChunk.length.toString(16) + '\r\n', 'latin1'), - bodyChunk, - CRLF, - ]); - } - - /** @const {number} Max chunk-size / trailer line length. */ - var MAX_CHUNK_LINE_BYTES = 64 * 1024; - - /** - * Incremental Transfer-Encoding: chunked request-body decoder. - * - * @param {IncomingMessage} request - * @param {function(): void} onError - * @param {function(Buffer): void} onComplete leftover bytes after the body - * @returns {function(Buffer|undefined): void} feed - */ - function createChunkedDecoder(request, onError, onComplete) { - var buffer = BufferAlloc(0); - var state = 'size'; - var bytesRemaining = 0; - var finished = false; - - function fail() { - finished = true; - onError(); - } - - return function feed(incoming) { - if (finished) return; - if (incoming && incoming.length) { - buffer = buffer.length ? BufferConcat([buffer, incoming]) : incoming; - } - for (;;) { - if (state === 'size') { - var lineEnd = BufferPrototypeIndexOf(buffer, '\r\n'); - if (lineEnd < 0) { - if (buffer.length > MAX_CHUNK_LINE_BYTES) fail(); - return; - } - if (lineEnd > MAX_CHUNK_LINE_BYTES) return fail(); - var sizeLine = BufferPrototypeToString(buffer, 'latin1', 0, lineEnd); - var extensionSep = sizeLine.indexOf(';'); - var sizeToken = extensionSep >= 0 ? sizeLine.slice(0, extensionSep) : sizeLine; - if (!allChars(sizeToken, isAsciiHexDigit)) return fail(); - if (extensionSep >= 0 && !reTest(CHUNK_EXT_RE, sizeLine.slice(extensionSep))) - return fail(); - var chunkSize = parseIntG(sizeToken, 16); - if (!NumberIsSafeInteger(chunkSize) || chunkSize < 0) return fail(); - buffer = BufferPrototypeSlice(buffer, lineEnd + 2); - if (chunkSize === 0) state = 'trailer'; - else { - bytesRemaining = chunkSize; - state = 'data'; - } - } else if (state === 'data') { - if (buffer.length === 0) return; - var take = buffer.length < bytesRemaining ? buffer.length : bytesRemaining; - request.emit('data', BufferPrototypeSlice(buffer, 0, take)); - buffer = BufferPrototypeSlice(buffer, take); - bytesRemaining -= take; - if (bytesRemaining === 0) state = 'dataCRLF'; - } else if (state === 'dataCRLF') { - if (buffer.length < 2) return; - if (buffer[0] !== 13 || buffer[1] !== 10) return fail(); - buffer = BufferPrototypeSlice(buffer, 2); - state = 'size'; - } else { - if (buffer.length < 2) return; - if (buffer[0] === 13 && buffer[1] === 10) { - buffer = BufferPrototypeSlice(buffer, 2); - finished = true; - return onComplete(buffer); - } - var trailerEnd = BufferPrototypeIndexOf(buffer, '\r\n'); - if (trailerEnd < 0) { - if (buffer.length > MAX_CHUNK_LINE_BYTES) fail(); - return; - } - if (trailerEnd > MAX_CHUNK_LINE_BYTES) return fail(); - var trailerLine = BufferPrototypeToString(buffer, 'latin1', 0, trailerEnd); - var colon = trailerLine.indexOf(':'); - if (colon <= 0 || !isHttpToken(trailerLine.slice(0, colon))) return fail(); - buffer = BufferPrototypeSlice(buffer, trailerEnd + 2); - } - } - }; - } - - // Flat parseRequest layout (mirrors http.odin PARSE_* / vals[]): - // [state, consumed, method, url, minor, name, value, ...] - var PARSE_STATE = 0; - var PARSE_CONSUMED = 1; - var PARSE_METHOD = 2; - var PARSE_URL = 3; - var PARSE_MINOR = 4; - var PARSE_HEADERS = 5; - var PARSE_OK = 0; - var PARSE_NEED_MORE = 1; - - /** - * @param {net.Socket} socket - * @param {Array} parseResult - * @constructor - */ - function IncomingMessage(socket, parseResult) { - EventEmitter.call(this); - this.socket = socket; - this.method = parseResult[PARSE_METHOD]; - this.url = parseResult[PARSE_URL]; - this.httpVersionMajor = 1; - this.httpVersionMinor = parseResult[PARSE_MINOR]; - this.httpVersion = '1.' + parseResult[PARSE_MINOR]; - // Eager headers (framing needs Connection/CL/TE). rawHeaders sliced on first access — - // hello-world never reads it (saves a per-request array alloc). - this._parseResult = parseResult; - this.headers = buildHeaders(parseResult, PARSE_HEADERS); - this.complete = false; - this._ended = false; - } - IncomingMessage.prototype = ObjectCreate(EventEmitter.prototype); - IncomingMessage.prototype.constructor = IncomingMessage; - - // Lazy rawHeaders: first get materializes an own data property (Node shape: - // hasOwnProperty / Object.keys include rawHeaders after access). - ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', { - configurable: true, - enumerable: true, - get: function () { - var pairs = this._parseResult.slice(PARSE_HEADERS); - Object.defineProperty(this, 'rawHeaders', { - value: pairs, - writable: true, - enumerable: true, - configurable: true, - }); - return pairs; - }, - set: function (value) { - Object.defineProperty(this, 'rawHeaders', { - value: value, - writable: true, - enumerable: true, - configurable: true, - }); - }, - }); - - var STATUS_LINE_200 = 'HTTP/1.1 200 OK\r\n'; - var CONNECTION_KEEP_ALIVE = 'Connection: keep-alive\r\n'; - var CONNECTION_CLOSE = 'Connection: close\r\n'; - - /** - * @param {net.Socket} socket - * @param {string} method - * @param {number|undefined} httpMinor - * @constructor - */ - function ServerResponse(socket, method, httpMinor) { - EventEmitter.call(this); - this.socket = socket; - this.statusCode = 200; - this.statusMessage = undefined; - this.headersSent = false; - this._chunked = false; - this.finished = false; - /** @type {Object} lowercased name → value */ - this._headerValues = Object.create(null); - /** @type {Object} lowercased name → wire-case name */ - this._headerNames = Object.create(null); - this._isHead = method === 'HEAD'; - this._allowChunked = httpMinor === undefined || httpMinor >= 1; - this._keepAlive = false; - /** @type {((function(): void) | null)} connection hook after finish */ - this._onComplete = null; - // Forward the socket's 'drain' to this response for the lifetime of the - // response, so res.write() === false + res.once('drain', ...) is a real - // backpressure contract (the native layer owes a 'drain' whenever a write - // buffered past the HWM). Removed in end() — on a keep-alive connection the - // next response gets its own forwarder on the same socket. - var self = this; - this._drainForwarder = null; - if (socket && typeof socket.on === 'function') { - this._drainForwarder = function () { - self.emit('drain'); - }; - socket.on('drain', this._drainForwarder); - } - } - ServerResponse.prototype = ObjectCreate(EventEmitter.prototype); - ServerResponse.prototype.constructor = ServerResponse; - - /** - * @param {string} name - * @param {*} value - * @returns {ServerResponse} - */ - ServerResponse.prototype.setHeader = function (name, value) { - assertHeaderName(name); - var text = String(value); - assertValidHeaderChar(text, 'header content [' + name + ']'); - var lowerName = name.toLowerCase(); - this._headerValues[lowerName] = text; - this._headerNames[lowerName] = name; - return this; - }; - - /** - * @param {string} name - * @returns {string|undefined} - */ - ServerResponse.prototype.getHeader = function (name) { - return this._headerValues[String(name).toLowerCase()]; - }; - - /** - * @param {string} name - */ - ServerResponse.prototype.removeHeader = function (name) { - var lowerName = String(name).toLowerCase(); - delete this._headerValues[lowerName]; - delete this._headerNames[lowerName]; - }; - - /** - * @param {string} name - * @returns {boolean} - */ - ServerResponse.prototype.hasHeader = function (name) { - return this._headerValues[String(name).toLowerCase()] !== undefined; - }; - - /** - * @param {number} statusCode - * @param {string|object} [statusMessage] - * @param {object} [headers] - * @returns {ServerResponse} - */ - ServerResponse.prototype.writeHead = function (statusCode, statusMessage, headers) { - if (typeof statusMessage === 'object' && statusMessage !== null) { - headers = statusMessage; - statusMessage = undefined; - } - this.statusCode = validateStatusCode(statusCode); - if (statusMessage) this.statusMessage = statusMessage; - if (headers) { - for (var name in headers) { - if (Object.prototype.hasOwnProperty.call(headers, name)) { - this.setHeader(name, headers[name]); - } - } - } - return this; - }; - - /** - * Serialize status line + headers; finalizes `_keepAlive`. Does not write or set headersSent. - * - * @returns {string} latin1 head text - */ - ServerResponse.prototype._buildHead = function () { - var statusCode = validateStatusCode(this.statusCode); - var reason = this.statusMessage || STATUS_CODES[statusCode] || ''; - assertValidHeaderChar(String(reason), 'statusMessage'); - var head = - statusCode === 200 && !this.statusMessage - ? STATUS_LINE_200 - : 'HTTP/1.1 ' + statusCode + ' ' + reason + '\r\n'; - - var selfDelimited = - this._chunked || - this.hasHeader('content-length') || - this._isHead || - statusHasNoBody(this.statusCode); - if (!selfDelimited) this._keepAlive = false; - - if (this.hasHeader('connection')) { - if (reTest(CONNECTION_CLOSE_RE, this.getHeader('connection'))) this._keepAlive = false; - } else { - head += this._keepAlive ? CONNECTION_KEEP_ALIVE : CONNECTION_CLOSE; - } - - for (var lowerName in this._headerValues) { - head += this._headerNames[lowerName] + ': ' + this._headerValues[lowerName] + '\r\n'; - } - head += '\r\n'; - return head; - }; - - ServerResponse.prototype._flushHead = function () { - if (this.headersSent) return; - var headBytes = encodeHeadBytes(this._buildHead()); - this.headersSent = true; - this.socket.write(headBytes); - }; - - /** - * @param {string|Buffer|Uint8Array} [chunk] - * @param {string|function} [encoding] - * @param {function} [callback] - * @returns {boolean} - */ - ServerResponse.prototype.write = function (chunk, encoding, callback) { - if (typeof encoding === 'function') { - callback = encoding; - encoding = undefined; - } - var omitBody = this._isHead || statusHasNoBody(this.statusCode); - if (!this.headersSent) { - if ( - !omitBody && - this._allowChunked && - !this.hasHeader('content-length') && - !this.hasHeader('transfer-encoding') - ) { - this._chunked = true; - this.setHeader('Transfer-Encoding', 'chunked'); - } else if (this._allowChunked && this.hasHeader('transfer-encoding')) { - this._chunked = reTest(TE_HAS_CHUNKED_RE, this.getHeader('transfer-encoding')); - } - this._flushHead(); - } - var ok = true; - if (!omitBody && chunk && chunk.length) { - var bytes = typeof chunk === 'string' ? BufferFrom(chunk, encoding || 'utf8') : chunk; - // The socket's return value IS the backpressure signal: false means the - // native write buffer crossed its high-water mark and a 'drain' is owed - // (forwarded to this response by _drainForwarder). Swallowing it here - // would let a fast handler buffer without bound against a slow client. - ok = this.socket.write(this._chunked ? frameChunkedBody(bytes) : bytes); - } - if (typeof callback === 'function') process.nextTick(callback); - return ok; - }; - - /** - * @param {string|Buffer|Uint8Array|function} [chunk] - * @param {string|function} [encoding] - * @param {function} [callback] - * @returns {ServerResponse} - */ - ServerResponse.prototype.end = function (chunk, encoding, callback) { - if (typeof chunk === 'function') { - callback = chunk; - chunk = undefined; - } else if (typeof encoding === 'function') { - callback = encoding; - encoding = undefined; - } - if (this.finished) return this; - - var body = null; - if (chunk !== undefined && chunk !== null) { - body = typeof chunk === 'string' ? BufferFrom(chunk, encoding || 'utf8') : chunk; - } - var omitBody = this._isHead || statusHasNoBody(this.statusCode); - if (!this.headersSent) { - if (!this.hasHeader('content-length') && !this.hasHeader('transfer-encoding') && !omitBody) { - this.setHeader('Content-Length', String(body ? body.length : 0)); - } - if (this._allowChunked && this.hasHeader('transfer-encoding')) { - this._chunked = reTest(TE_HAS_CHUNKED_RE, this.getHeader('transfer-encoding')); - } - - var headText = this._buildHead(); - var totalBytes = headText.length + (body ? body.length : 0); - if ( - !omitBody && - body && - body.length && - body instanceof primordials.Uint8Array && - !this._chunked && - totalBytes <= HEAD_BODY_COALESCE_MAX - ) { - var combined = new primordials.Uint8Array(totalBytes); - writeLatin1Into(combined, headText, 0); - primordials.Uint8ArrayPrototypeSet(combined, body, headText.length); - this.headersSent = true; - this.socket.write(combined); - body = null; - } else { - this.headersSent = true; - this.socket.write(encodeHeadBytes(headText)); - } - } - if (!omitBody && body && body.length) { - this.socket.write(this._chunked ? frameChunkedBody(body) : body); - } - if (this._chunked && !omitBody) this.socket.write(LAST_CHUNK); - this.finished = true; - if (this._drainForwarder && typeof this.socket.removeListener === 'function') { - this.socket.removeListener('drain', this._drainForwarder); - this._drainForwarder = null; - } - if (typeof callback === 'function') process.nextTick(callback); - this.emit('finish'); - if (this._onComplete) this._onComplete(); - else this.socket.end(); - return this; - }; - - /** - * @param {number} httpMinor - * @param {string|undefined} connectionHeader - * @returns {boolean} - */ - function shouldKeepAlive(httpMinor, connectionHeader) { - var connection = (connectionHeader || '').toLowerCase(); - if (httpMinor >= 1) return !reTest(CONNECTION_CLOSE_CS_RE, connection); - return reTest(CONNECTION_KEEPALIVE_RE, connection); - } - - var EMPTY_BUFFER = BufferAlloc(0); - var DEADLINE_SWEEP_MS = 100; - - /** - * @param {Server} server - * @param {object} deadlines - */ - function registerConnectionDeadlines(server, deadlines) { - var sweep = server._connSweep || (server._connSweep = { connections: new Set(), timer: null }); - sweep.connections.add(deadlines); - if (sweep.timer === null) { - sweep.timer = setInterval(function () { - tickConnectionDeadlines(sweep); - }, DEADLINE_SWEEP_MS); - } - } - - /** - * @param {Server} server - * @param {object} deadlines - */ - function unregisterConnectionDeadlines(server, deadlines) { - var sweep = server._connSweep; - if (!sweep) return; - sweep.connections.delete(deadlines); - if (sweep.connections.size === 0 && sweep.timer !== null) { - clearInterval(sweep.timer); - sweep.timer = null; - } - } - - /** - * @param {{ connections: Set, timer: * }} sweep - */ - function tickConnectionDeadlines(sweep) { - var now = Date.now(); - sweep.connections.forEach(function (deadlines) { - if (deadlines.idleUntil !== 0 && now >= deadlines.idleUntil) { - deadlines.idleUntil = 0; - deadlines.onIdleTimeout(); - } else if (deadlines.headersUntil !== 0 && now >= deadlines.headersUntil) { - deadlines.headersUntil = 0; - deadlines.onRequestTimeout(); - } else if (deadlines.requestUntil !== 0 && now >= deadlines.requestUntil) { - deadlines.requestUntil = 0; - deadlines.onRequestTimeout(); - } - }); - } - - /** - * Per-connection request/response loop. - * - * @param {Server} server - * @param {net.Socket} socket - */ - function onConnection(server, socket) { - var pendingBytes = EMPTY_BUFFER; - var partialHeadLength = 0; - var parsingHead = true; - var request = null; - var response = null; - var contentLengthRemaining = 0; - var chunkedDecoder = null; - var requestBodyComplete = false; - var responseComplete = false; - var peerHalfClosed = false; - var connectionClosed = false; - - var keepAliveTimeoutMs = server.keepAliveTimeout || 0; - var headersTimeoutMs = server.headersTimeout || 0; - var requestTimeoutMs = server.requestTimeout || 0; - var deadlines = { - idleUntil: 0, - headersUntil: 0, - requestUntil: 0, - onIdleTimeout: destroyConnection, - onRequestTimeout: onReceiveTimeout, - }; - var receivingRequest = false; - registerConnectionDeadlines(server, deadlines); - - // Deferred body/'end' delivery: one reused nextTick callback + parallel arrays - // (no per-request closure). Pipelined sync processRequestHead can enqueue more - // while flush runs; deferScheduled stays true until the queue is empty. - var deferRequests = []; - var deferBodies = []; - var deferKinds = []; // 0 = emit end, 1 = Content-Length feed, 2 = chunked feed - var deferDecoders = []; - var deferScheduled = false; - var deferFlushError = undefined; - var DEFER_END = 0; - var DEFER_CONTENT_LENGTH = 1; - var DEFER_CHUNKED = 2; - - function scheduleAfterRequest(targetRequest, bodyFromHead, kind, decoder) { - deferRequests.push(targetRequest); - deferBodies.push(bodyFromHead); - deferKinds.push(kind); - deferDecoders.push(decoder); - if (!deferScheduled) { - deferScheduled = true; - process.nextTick(flushAfterRequest); - } - } - - function flushAfterRequest() { - // try/finally: a throw from a user 'end'/'data' listener must not latch - // deferScheduled=true forever (which would skip later nextTick arms). - deferFlushError = undefined; - try { - var index = 0; - while (index < deferRequests.length) { - var targetRequest = deferRequests[index]; - var bodyFromHead = deferBodies[index]; - var kind = deferKinds[index]; - var decoder = deferDecoders[index]; - index++; - if (connectionClosed) continue; - try { - if (kind === DEFER_END) { - emitRequestEnd(targetRequest); - maybeAdvance(); - } else if (kind === DEFER_CHUNKED) { - decoder(bodyFromHead); - } else { - feedContentLengthBody(bodyFromHead); - } - } catch (err) { - // Drain siblings first; rethrow the first error after cleanup. - if (deferFlushError === undefined) deferFlushError = err; - } - } - } finally { - deferRequests.length = 0; - deferBodies.length = 0; - deferKinds.length = 0; - deferDecoders.length = 0; - deferScheduled = false; - var pendingError = deferFlushError; - deferFlushError = undefined; - if (pendingError !== undefined) throw pendingError; - } - } - - function clearReceiveDeadlines() { - deadlines.headersUntil = 0; - deadlines.requestUntil = 0; - } - - function clearAllDeadlines() { - deadlines.idleUntil = 0; - clearReceiveDeadlines(); - unregisterConnectionDeadlines(server, deadlines); - } - - function destroyConnection() { - if (connectionClosed) return; - connectionClosed = true; - clearAllDeadlines(); - socket.destroy(); - } - - function armIdleDeadline() { - if (keepAliveTimeoutMs > 0) deadlines.idleUntil = Date.now() + keepAliveTimeoutMs; - } - - function beginReceiveDeadlines() { - receivingRequest = true; - if (headersTimeoutMs > 0 || requestTimeoutMs > 0) { - var now = Date.now(); - if (headersTimeoutMs > 0) deadlines.headersUntil = now + headersTimeoutMs; - if (requestTimeoutMs > 0) deadlines.requestUntil = now + requestTimeoutMs; - } - } - - function onReceiveTimeout() { - if (connectionClosed) return; - if (response && response.headersSent) destroyConnection(); - else sendErrorAndClose(408); - } - - /** - * @param {number} statusCode - */ - function sendErrorAndClose(statusCode) { - if (connectionClosed) return; - connectionClosed = true; - clearAllDeadlines(); - var reason = STATUS_CODES[statusCode] || 'Error'; - socket.write( - BufferFrom( - 'HTTP/1.1 ' + - statusCode + - ' ' + - reason + - '\r\nConnection: close\r\nContent-Length: 0\r\n\r\n', - 'latin1', - ), - ); - socket.end(); - } - - function resetForNextRequest() { - parsingHead = true; - partialHeadLength = 0; - request = null; - response = null; - contentLengthRemaining = 0; - chunkedDecoder = null; - requestBodyComplete = false; - responseComplete = false; - receivingRequest = false; - } - - /** - * Advance when both request body and response are done (keep-alive or close). - */ - function maybeAdvance() { - if (connectionClosed || !requestBodyComplete || !responseComplete) return; - if (!response._keepAlive) { - connectionClosed = true; - clearAllDeadlines(); - socket.end(); - return; - } - resetForNextRequest(); - if (pendingBytes.length > 0) { - beginReceiveDeadlines(); - processRequestHead(); - } else if (peerHalfClosed) { - destroyConnection(); - } else { - armIdleDeadline(); - } - } - - function onResponseComplete() { - responseComplete = true; - maybeAdvance(); - } - - /** - * Mark the request fully received (timers off, pipelined leftover stashed). - * Does not emit `'end'`. - * - * @param {Buffer|Uint8Array|undefined} leftover - */ - function markRequestReceived(leftover) { - pendingBytes = leftover && leftover.length ? leftover : EMPTY_BUFFER; - clearReceiveDeadlines(); - requestBodyComplete = true; - } - - /** - * Emit request `'end'` once. Accepts a captured request when outer `request` - * was cleared by a synchronous keep-alive advance. - * - * @param {IncomingMessage|null} [targetRequest] - */ - function emitRequestEnd(targetRequest) { - var message = targetRequest || request; - if (message && !message._ended) { - message._ended = true; - message.complete = true; - message.emit('end'); - } - } - - /** - * Full body completion: mark received (if needed), emit end, try advance. - * - * @param {Buffer|Uint8Array|undefined} leftover - */ - function onRequestBodyComplete(leftover) { - if (!requestBodyComplete) markRequestReceived(leftover); - emitRequestEnd(request); - maybeAdvance(); - } - - /** - * @param {Buffer|Uint8Array|undefined} chunk - */ - function feedContentLengthBody(chunk) { - if (!request || request._ended) return; - if (contentLengthRemaining > 0 && chunk && chunk.length) { - var take = chunk.length < contentLengthRemaining ? chunk.length : contentLengthRemaining; - request.emit('data', chunk.slice(0, take)); - contentLengthRemaining -= take; - chunk = chunk.slice(take); - } - if (contentLengthRemaining <= 0) onRequestBodyComplete(chunk); - } - - function processRequestHead() { - var parseResult = native.parseRequest(pendingBytes, partialHeadLength); - var state = parseResult[PARSE_STATE]; - if (state === PARSE_NEED_MORE) { - if (pendingBytes.length > MAX_HEAD_BYTES) sendErrorAndClose(431); - else partialHeadLength = pendingBytes.length; - return; - } - if (state !== PARSE_OK) return sendErrorAndClose(400); - - var consumed = parseResult[PARSE_CONSUMED]; - if (consumed > MAX_HEAD_BYTES) return sendErrorAndClose(431); - - parsingHead = false; - partialHeadLength = 0; - deadlines.headersUntil = 0; - - request = new IncomingMessage(socket, parseResult); - response = new ServerResponse(socket, request.method, request.httpVersionMinor); - response._keepAlive = shouldKeepAlive( - request.httpVersionMinor, - request.headers['connection'], - ); - response._onComplete = onResponseComplete; - - var bodyFromHead = - consumed < pendingBytes.length ? pendingBytes.slice(consumed) : EMPTY_BUFFER; - pendingBytes = EMPTY_BUFFER; - - var transferEncoding = request.headers['transfer-encoding']; - var contentLengthHeader = request.headers['content-length']; - if (transferEncoding !== undefined) { - if (contentLengthHeader !== undefined) return sendErrorAndClose(400); - if (!reTest(TE_CHUNKED_RE, transferEncoding)) return sendErrorAndClose(501); - chunkedDecoder = createChunkedDecoder( - request, - function () { - if (response.headersSent) destroyConnection(); - else sendErrorAndClose(400); - }, - onRequestBodyComplete, - ); - } else if (contentLengthHeader !== undefined) { - if (!allChars(contentLengthHeader, isAsciiDigit)) return sendErrorAndClose(400); - contentLengthRemaining = parseIntG(contentLengthHeader, 10); - } - - var hasEntityBody = chunkedDecoder !== null || contentLengthRemaining > 0; - // Capture before emit: sync res.end() may reset outer `request` via maybeAdvance. - var capturedRequest = request; - var capturedBody = bodyFromHead; - var capturedDecoder = chunkedDecoder; - var deferKind = !hasEntityBody - ? DEFER_END - : chunkedDecoder - ? DEFER_CHUNKED - : DEFER_CONTENT_LENGTH; - - // Bodyless: mark receive-complete before 'request' so sync res.end() can reuse - // the connection same turn. requestTimeout is receive-only (Node parity). - if (!hasEntityBody) { - markRequestReceived(bodyFromHead.length ? bodyFromHead : EMPTY_BUFFER); - } - - server.emit('request', request, response); - // After emit so a throwing handler does not leave a stray deferred delivery. - // 'end' / entity bytes still on nextTick for listeners attached in the handler. - scheduleAfterRequest(capturedRequest, capturedBody, deferKind, capturedDecoder); - } - - socket.on('data', function (chunk) { - if (connectionClosed) return; - deadlines.idleUntil = 0; - if (!receivingRequest) beginReceiveDeadlines(); - if (parsingHead) { - pendingBytes = pendingBytes.length ? BufferConcat([pendingBytes, chunk]) : chunk; - processRequestHead(); - } else if (requestBodyComplete) { - pendingBytes = pendingBytes.length ? BufferConcat([pendingBytes, chunk]) : chunk; - } else if (chunkedDecoder) { - chunkedDecoder(chunk); - } else { - feedContentLengthBody(chunk); - } - }); - - socket.on('end', function () { - peerHalfClosed = true; - process.nextTick(function () { - if (connectionClosed) return; - if (parsingHead) { - if (pendingBytes.length > 0) sendErrorAndClose(400); - else destroyConnection(); - } else if (request && !request._ended) { - if (response && response.headersSent) destroyConnection(); - else sendErrorAndClose(400); - } - }); - }); - - socket.on('error', function () { - connectionClosed = true; - clearAllDeadlines(); - }); - - armIdleDeadline(); - } - - /** - * @param {object|function} [options] - * @param {function} [requestListener] - * @constructor - */ - function Server(options, requestListener) { - if (typeof options === 'function') { - requestListener = options; - options = {}; - } - EventEmitter.call(this); - var self = this; - this.keepAliveTimeout = 5000; - this.headersTimeout = 60000; - this.requestTimeout = 300000; - var tlsContext; - if (options && typeof options === 'object') { - if (typeof options.keepAliveTimeout === 'number') { - this.keepAliveTimeout = options.keepAliveTimeout; - } - if (typeof options.headersTimeout === 'number') { - this.headersTimeout = options.headersTimeout; - } - if (typeof options.requestTimeout === 'number') { - this.requestTimeout = options.requestTimeout; - } - tlsContext = options.tls; - } - this._net = net.createServer({ tls: tlsContext }, function (socket) { - onConnection(self, socket); - }); - this._net.on('listening', function () { - self.emit('listening'); - }); - this._net.on('close', function () { - self.emit('close'); - }); - this._net.on('error', function (err) { - self.emit('error', err); - }); - if (typeof requestListener === 'function') this.on('request', requestListener); - } - Server.prototype = ObjectCreate(EventEmitter.prototype); - Server.prototype.constructor = Server; - - Server.prototype.listen = function () { - this._net.listen.apply(this._net, arguments); - return this; - }; - Server.prototype.close = function (callback) { - this._net.close(callback); - return this; - }; - Server.prototype.address = function () { - return this._net.address(); - }; + var server = require('internal/http/server'); + var Server = server.Server; /** * @param {object|function} [options] @@ -1136,8 +39,8 @@ module.exports = { createServer: createServer, Server: Server, - IncomingMessage: IncomingMessage, - ServerResponse: ServerResponse, - STATUS_CODES: STATUS_CODES, + IncomingMessage: require('internal/http/incoming').IncomingMessage, + ServerResponse: server.ServerResponse, + STATUS_CODES: server.STATUS_CODES, }; }); diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index eb947d1..e5c833a 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -116,6 +116,10 @@ const ROUTES = [ re: globToRe('**/js/node/http.js'), targets: ['make test-http-smoke', 'make bench-http'], }, + { + re: globToRe('**/js/internal/http/**'), + targets: ['make test-http-smoke', 'make bench-http'], + }, { re: globToRe('**/picohttpparser/**'), targets: ['make test-http-smoke', 'make bench-http'], diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index 3575a2d..d329d84 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -32,3 +32,11 @@ test('invalid **.js style is not required — **/* works', () => { const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); }); + +test('internal/http/** routes like node/http.js (the split must not drop the http smoke)', () => { + for (const f of ['common', 'incoming', 'outgoing', 'server']) { + const r = routePaths([`pkg/runtime/js/internal/http/${f}.js`]); + assert.ok(r.targets.includes('make test-http-smoke'), f); + assert.ok(r.targets.includes('make bench-http') || r.nonCi.includes('make bench-http'), f); + } +}); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index e56ac08..d3484b7 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -112,7 +112,7 @@ { "name": "the poisoned http-smoke phases die when a framing validator goes back to re.test", "why": "Content-Length framing. With the check on a live regex, a poisoned exec answered 200 OK for `Content-Length: abc` — parseInt gives NaN and the body framing desyncs. The charCode form has no regex to poison at all.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " if (!allChars(contentLengthHeader, isAsciiDigit)) return sendErrorAndClose(400);", "replace": " if (!/^\\d+$/.test(contentLengthHeader)) return sendErrorAndClose(400);", "gate": "make:test-http-smoke", @@ -121,7 +121,7 @@ { "name": "the poisoned http-smoke phases die when the chunked check goes back to re.test", "why": "Transfer-Encoding framing. A steered check accepted `gzip` as chunked, which is the other half of the smuggling pair.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " if (!reTest(TE_CHUNKED_RE, transferEncoding)) return sendErrorAndClose(501);", "replace": " if (!/^\\s*chunked\\s*$/i.test(transferEncoding)) return sendErrorAndClose(501);", "gate": "make:test-http-smoke", @@ -260,7 +260,7 @@ { "name": "the poisoned http-smoke phases die when the header-char scan reads charCodeAt live", "why": "assertValidHeaderChar guards the response path. Live, a CR/LF-lying gadget splits the response, and the exec/test phases pass throughout because they never touch that carrier.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/outgoing.js", "find": " var code = StringPrototypeCharCodeAt(value, i);\n if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) {", "replace": " var code = value.charCodeAt(i);\n if (code === 9 || (code >= 0x20 && code <= 0x7e) || (code >= 0x80 && code <= 0xff)) {", "gate": "make:test-http-smoke", @@ -269,7 +269,7 @@ { "name": "the poisoned http-smoke phases die when isHttpToken reads charCodeAt live", "why": "isHttpToken guards header NAMES. Live, a CR/LF-lying gadget accepts a name that still carries real CR/LF on the wire. The value-path mutation does not cover this site.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/common.js", "find": " var code = StringPrototypeCharCodeAt(value, i);\n if (code >= 128 || !HTTP_TCHAR[code]) return false;", "replace": " var code = value.charCodeAt(i);\n if (code >= 128 || !HTTP_TCHAR[code]) return false;", "gate": "make:test-http-smoke", @@ -278,7 +278,7 @@ { "name": "the poisoned http-smoke phases die when Content-Length conversion reads live parseInt", "why": "A sound allChars digit check next to live parseInt still desyncs framing: contentLengthRemaining becomes the gadget value. Poison-before-require is the realistic order; bootstrap capture on primordials is the fix. The Odin pi= pin covers the eager URL port path; this entry covers the lazy http framing path.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " contentLengthRemaining = parseIntG(contentLengthHeader, 10);", "replace": " contentLengthRemaining = parseInt(contentLengthHeader, 10);", "gate": "make:test-http-smoke", @@ -926,9 +926,9 @@ ] }, { - "name": "65-lazy-buffer-capture dies when http.js resolves Buffer itself", + "name": "65-lazy-buffer-capture dies when internal/http/server.js resolves Buffer itself", "why": "http.js was migrated with NO test behind it: reverting this one line left every gate green while the measured divergence returned. Two of five migrated modules were unpinned until this entry.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " var PristineBuffer = require.pristineBuffer;", "replace": " var PristineBuffer = { Buffer: require('buffer').Buffer, from: require('buffer').Buffer.from, alloc: require('buffer').Buffer.alloc, concat: require('buffer').Buffer.concat, isBuffer: require('buffer').Buffer.isBuffer, bufferToString: function (b, e, a, c) { return b.toString(e, a, c); }, bufferIndexOf: function (b, n) { return b.indexOf(n); }, bufferSlice: function (b, a, c) { return b.slice(a, c); } };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -939,7 +939,7 @@ { "name": "65-lazy-buffer-capture dies when the chunked reassembly reads Buffer.concat live", "why": "http.js's chunked decoder reassembles across TCP segments with concat; a poisoned concat that reorders the parts desynchronises the size line from its body. This is framing over network-controlled bytes, not a cosmetic read.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/common.js", "find": " var BufferConcat = PristineBuffer.concat;", "replace": " var BufferConcat = function (l, t) { return require('buffer').Buffer.concat(l, t); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -950,7 +950,7 @@ { "name": "65-lazy-buffer-capture dies when the chunk size is read through a live toString", "why": "The size line is read with buffer.toString('latin1', 0, lineEnd). A replacement that INFLATES the size is a request-smuggling primitive; node frames HTTP in C (llhttp) and is not steerable at all.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/common.js", "find": " var BufferPrototypeToString = PristineBuffer.bufferToString;", "replace": " var BufferPrototypeToString = function (b, e, a, c) { return b.toString(e, a, c); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -961,7 +961,7 @@ { "name": "65-lazy-buffer-capture dies when the framing cursor advances through a live slice", "why": "The decoder advances past each size line and body with slice; a corrupted cursor leaves a stray byte and desynchronises every later chunk.", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/common.js", "find": " var BufferPrototypeSlice = PristineBuffer.bufferSlice;", "replace": " var BufferPrototypeSlice = function (b, a, c) { return b.slice(a, c); };", "gate": "compat:tests/node-compat/cases/65-lazy-buffer-capture.js", @@ -2209,50 +2209,68 @@ "expect_detail": "dns: ok [\"Object.keys\"]" }, { - "name": "75-lazy-global-capture dies when http.js allocates the tchar table with the live Uint8Array", + "name": "75-lazy-global-capture dies when internal/http/common.js allocates the tchar table with the live Uint8Array", "why": "`Uint8Array` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/common.js", "find": " var HTTP_TCHAR = new primordials.Uint8Array(128);", "replace": " var HTTP_TCHAR = new Uint8Array(128);", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "http: ok [\"new Uint8Array\"" }, { - "name": "75-lazy-global-capture dies when http.js builds IncomingMessage.prototype with the live Object", + "name": "75-lazy-global-capture dies when internal/http/incoming.js builds IncomingMessage.prototype with the live Object", "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/incoming.js", "find": " IncomingMessage.prototype = ObjectCreate(EventEmitter.prototype);", "replace": " IncomingMessage.prototype = Object.create(EventEmitter.prototype);", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "http: ok [\"Object.create\"]" }, { - "name": "75-lazy-global-capture dies when http.js defines IncomingMessage#rawHeaders through the live Object", + "name": "75-lazy-global-capture dies when internal/http/incoming.js defines IncomingMessage#rawHeaders through the live Object", "why": "`Object.defineProperty` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/incoming.js", "find": " ObjectDefineProperty(IncomingMessage.prototype, 'rawHeaders', {", "replace": " Object.defineProperty(IncomingMessage.prototype, 'rawHeaders', {", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "http: ok [\"Object.defineProperty\"]" }, { - "name": "75-lazy-global-capture dies when http.js builds ServerResponse.prototype with the live Object", + "name": "75-lazy-global-capture dies when internal/http/server.js builds ServerResponse.prototype with the live Object", "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " ServerResponse.prototype = ObjectCreate(EventEmitter.prototype);", "replace": " ServerResponse.prototype = Object.create(EventEmitter.prototype);", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "http: ok [\"Object.create\"]" }, { - "name": "75-lazy-global-capture dies when http.js builds Server.prototype with the live Object", + "name": "75-lazy-global-capture dies when internal/http/server.js builds Server.prototype with the live Object", "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/http.js", + "source": "pkg/runtime/js/internal/http/server.js", "find": " Server.prototype = ObjectCreate(EventEmitter.prototype);", "replace": " Server.prototype = Object.create(EventEmitter.prototype);", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", "expect_detail": "http: ok [\"Object.create\"]" }, + { + "name": "81-http-internal-modules dies when the loader stops handing internal/http/server node:http's bindings", + "why": "server.js calls native.parseRequest for every request head; the split moved it out of node/http.js, the only id Odin keys the http bindings by, so the loader alias is the one thing connecting it. Without it every request fails.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " natives['internal/http/server'] = natives['node/http'];", + "replace": "", + "gate": "compat:tests/node-compat/cases/81-http-internal-modules.js", + "expect_detail": ["parseRequest", "no complete response"] + }, + { + "name": "81-http-internal-modules dies when the loader stops handing internal/http/outgoing node:http's bindings", + "why": "outgoing.js reads native.latin1WriteInto at module-eval for the response head encoder; without the alias require('http') itself throws.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " natives['internal/http/outgoing'] = natives['node/http'];", + "replace": "", + "gate": "compat:tests/node-compat/cases/81-http-internal-modules.js", + "expect_detail": "latin1WriteInto" + }, { "name": "75-lazy-global-capture dies when net.js builds Socket.prototype with the live Object", "why": "`Object.create` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", diff --git a/tests/node-compat/cases/81-http-internal-modules.js b/tests/node-compat/cases/81-http-internal-modules.js new file mode 100644 index 0000000..0cfa19c --- /dev/null +++ b/tests/node-compat/cases/81-http-internal-modules.js @@ -0,0 +1,187 @@ +// node:http is assembled from internal modules laid out like node's own http files: +// js/internal/http/{common,incoming,outgoing,server}.js stand where node keeps +// _http_common, _http_incoming, _http_outgoing and _http_server. An error thrown from +// inside the moved code used to carry a `node:http:` frame, because everything was +// defined in node/http.js; now it carries the internal file's name, as node's does. +// +// The NAME differs from node's, deliberately, so this case prints it runtime-neutrally. +// Node exposes `require('_http_server')` and friends publicly (probed on 26.13.0: no +// deprecation warning), with export shapes Lava does not implement (parsers, HTTPParser, +// OutgoingMessage, setupConnectionsTracking, ...). Publishing a js/node/_http_server.js +// would make a requireable module whose keys disagree with node's, so the files live under +// internal/ and the frames read `node:internal/http/` where node's read +// `node:_http_`. Each frame line therefore asks "is the frame from a moved http +// file" (either spelling) and "is the old `node:http:` frame gone" — both false before +// the split on Lava, both true/false-as-node after it. Only the source name is compared, +// never the message's line or column. +// +// The behavior lines after that are there so a move that loses a helper, breaks a +// prototype chain or re-wires an identity fails here and not only in a stack. A raw +// net client drives the server (Lava has no http.request); responses are framed by the +// parser below, never by waiting for 'end', so a framing regression fails as a diff +// rather than a CI timeout (see exchange's guard). +'use strict'; + +const http = require('node:http'); +const net = require('node:net'); +const EventEmitter = require('node:events'); + +function frame(label, f) { + let stack = null; + let code = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + code = e && e.code; + } + if (stack === null) return console.log(label + ': NO THROW'); + const moved = stack.includes('node:internal/http/') || stack.includes('node:_http_'); + console.log(label + ': ' + code + ' moved=' + moved + ' old=' + stack.includes('node:http:')); +} + +// --- the public entry: same five members Lava implements, same identities ------------ +console.log( + 'members', + ['createServer', 'Server', 'IncomingMessage', 'ServerResponse', 'STATUS_CODES'] + .map((k) => k + ':' + typeof http[k]) + .join(' '), +); +console.log('node: prefix identity', require('http') === http); +console.log('STATUS_CODES', http.STATUS_CODES[200], http.STATUS_CODES[404], http.STATUS_CODES[500]); +for (const C of [http.Server, http.IncomingMessage, http.ServerResponse]) { + console.log(C.name, 'is an EventEmitter', C.prototype instanceof EventEmitter); +} + +// Splits a raw byte stream into complete responses (Content-Length or chunked), so the +// client can stop as soon as the expected count has arrived. +function parseResponses(raw) { + const out = []; + let at = 0; + for (;;) { + const headEnd = raw.indexOf('\r\n\r\n', at); + if (headEnd < 0) return out; + const lines = raw.slice(at, headEnd).split('\r\n'); + const headers = {}; + for (const l of lines.slice(1)) { + const c = l.indexOf(':'); + headers[l.slice(0, c).toLowerCase()] = l.slice(c + 1).trim(); + } + let p = headEnd + 4; + let body = ''; + if (headers['transfer-encoding'] === 'chunked') { + for (;;) { + const e = raw.indexOf('\r\n', p); + if (e < 0) return out; + const n = parseInt(raw.slice(p, e), 16); + if (raw.length < e + 2 + n + 2) return out; + body += raw.slice(e + 2, e + 2 + n); + p = e + 2 + n + 2; + if (n === 0) break; + } + } else { + const n = Number(headers['content-length'] || 0); + if (raw.length < p + n) return out; + body = raw.slice(p, p + n); + p += n; + } + out.push({ status: lines[0], headers, body }); + at = p; + } +} + +// The guard turns a server that never answers into a diff naming the request, not a hang +// the runner can only report as a timeout: Lava keeps running after an uncaught throw in a +// socket listener, where node exits, so a broken server half would otherwise just stall. +function exchange(server, payload, count) { + return new Promise((resolve, reject) => { + const c = net.connect(server.address().port, () => c.write(payload)); + let raw = ''; + const guard = setTimeout(() => { + c.destroy(); + reject(new Error('no complete response to ' + JSON.stringify(payload.slice(0, 16)))); + }, 10000); + c.on('data', (d) => { + raw += d.toString('latin1'); + const got = parseResponses(raw); + if (got.length < count) return; + clearTimeout(guard); + c.destroy(); + resolve(got); + }); + c.on('error', reject); + }); +} + +function show(label, r) { + console.log( + label, + JSON.stringify(r.status), + 'connection=' + r.headers['connection'], + 'te=' + r.headers['transfer-encoding'], + 'cl=' + r.headers['content-length'], + 'body=' + JSON.stringify(r.body), + ); +} + +async function main() { + let firstSocket = null; + const server = http.createServer((req, res) => { + if (req.url === '/a') { + firstSocket = req.socket; + // Errors thrown by the moved validators, from a ServerResponse a real server made. + frame('setHeader(bad name)', () => res.setHeader('a b', 1)); + frame('setHeader(bad value)', () => res.setHeader('X', 'a\nb')); + frame('writeHead(5)', () => res.writeHead(5)); + console.log('request', req.method, req.url, req.httpVersion, req.headers.host); + console.log('IncomingMessage', req instanceof http.IncomingMessage); + console.log('ServerResponse', res instanceof http.ServerResponse); + res.setHeader('X-Case', 'a'); + res.end('A'); + return; + } + if (req.url === '/b') { + console.log('keep-alive reused socket', req.socket === firstSocket); + res.write('b1'); + res.end('b2'); + return; + } + let body = ''; + req.on('data', (d) => (body += d)); + req.on('end', () => res.end('echo:' + body)); + }); + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); + + // Two pipelined requests on one connection: keep-alive, then a chunked response. + const pipelined = await exchange( + server, + 'GET /a HTTP/1.1\r\nHost: x\r\n\r\nGET /b HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n', + 2, + ); + show('/a', pipelined[0]); + console.log('/a x-case', pipelined[0].headers['x-case']); + show('/b', pipelined[1]); + + // A chunked request body, reassembled by the moved decoder. + const echoed = await exchange( + server, + 'POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n' + + '4\r\nAAAA\r\n3;ext=1\r\nBBB\r\n0\r\nTrailer: t\r\n\r\n', + 1, + ); + show('/echo', echoed[0]); + + // A malformed chunk size is still a 400 from the moved framing path. + const bad = await exchange( + server, + 'POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\n', + 1, + ); + console.log('bad chunk', JSON.stringify(bad[0].status)); + server.close(); +} + +main().catch((e) => { + console.error(e); + process.exit(1); +}); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 3b41365..6b025f4 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -59,6 +59,30 @@ "accessor": 0, "global": 0 }, + "internal/http/common.js": { + "method": 5, + "invoke": 0, + "accessor": 0, + "global": 0 + }, + "internal/http/incoming.js": { + "method": 2, + "invoke": 1, + "accessor": 0, + "global": 3 + }, + "internal/http/outgoing.js": { + "method": 1, + "invoke": 0, + "accessor": 0, + "global": 1 + }, + "internal/http/server.js": { + "method": 13, + "invoke": 4, + "accessor": 0, + "global": 14 + }, "internal/mime.js": { "method": 24, "invoke": 0, @@ -222,10 +246,10 @@ "global": 0 }, "node/http.js": { - "method": 21, - "invoke": 5, + "method": 0, + "invoke": 0, "accessor": 0, - "global": 18 + "global": 0 }, "node/https.js": { "method": 0, From 6aaa5ccdc9875b1a1e84cf508d192ccbca0bfc3e Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:20:40 +0200 Subject: [PATCH 35/61] refactor(fetch): split internal/fetch into internal/fetch/{headers,body,request,response} internal/fetch.js 1099 -> 501 lines: it keeps the fetch() driver and installs the same globals (Odin keys native.request to internal/fetch). Code is moved verbatim. Case 82 pins the layout; headers mutations re-pointed. Co-Authored-By: Claude Opus 5.5 --- agents/prompts/pr-gate-reference/gates.md | 2 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 11 +- pkg/runtime/globals.odin | 1 + pkg/runtime/js/internal/fetch.js | 630 +----------------- pkg/runtime/js/internal/fetch/body.js | 269 ++++++++ pkg/runtime/js/internal/fetch/headers.js | 210 ++++++ pkg/runtime/js/internal/fetch/request.js | 114 ++++ pkg/runtime/js/internal/fetch/response.js | 68 ++ runtime/gates/route-gates.mjs | 4 + runtime/gates/route-gates.test.mjs | 10 + tests/mutation-manifest.json | 24 +- .../cases/82-fetch-internal-modules.js | 166 +++++ tests/node-compat/pollution-baseline.json | 30 +- 14 files changed, 916 insertions(+), 625 deletions(-) create mode 100644 pkg/runtime/js/internal/fetch/body.js create mode 100644 pkg/runtime/js/internal/fetch/headers.js create mode 100644 pkg/runtime/js/internal/fetch/request.js create mode 100644 pkg/runtime/js/internal/fetch/response.js create mode 100644 tests/node-compat/cases/82-fetch-internal-modules.js diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 931f439..2f90a62 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -57,7 +57,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `pkg/runtime/http.odin`, `js/node/http.js`, `js/internal/http/**`, `picohttpparser/**` | `make test-http-smoke` (runs both proactor and readiness backends), `make bench-http` | | `pkg/runtime/net.odin`, `net_other.odin`, `js/node/net.js` | `make test-net-smoke`, `make test-zerocopy-smoke` | | `pkg/runtime/tls*.odin`, `tls_server.odin`, `js/node/https.js` | `make test-https-smoke` (drives both backends internally) | -| `pkg/runtime/fetch*.odin`, `js/internal/fetch.js` | `make test-fetch-smoke` | +| `pkg/runtime/fetch*.odin`, `js/internal/fetch.js`, `js/internal/fetch/*.js` | `make test-fetch-smoke` | | `pkg/runtime/eventloop/**` | `make test-eventloop-odin`, `make test-eventloop-lava` | | `pkg/runtime/fs*.odin`, `os_*.odin` | `make test-fs-lava` | | `pkg/std/sqlite/**`, `sqlite.odin`, `js/node/sqlite.js` | `make test-sqlite-odin`, `make test-sqlite-lava` | diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 0a550c6..8e2c100 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 4760a63..6fbfb1a 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -184,8 +184,8 @@ factory, before the table exists, and a top-level `require('buffer')` back would The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), Web Streams (~2100 across `internal/webstreams/`), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry -live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), `fetch.js` (1054), -`crypto.js` (917). `node/stream.js` only assembles the public module; the classic +live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), fetch +(~1160 across `internal/fetch.js` and `internal/fetch/`), `crypto.js` (917). `node/stream.js` only assembles the public module; the classic streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, `transform`, `passthrough`, `destroy`, `add-abort-signal`, `end-of-stream`, `pipeline`, `utils`) and the Web Streams bridges in `internal/webstreams/adapters.js`, the files node @@ -202,7 +202,12 @@ only assembles exports and installs the globals; the implementation is in `internal/webstreams/` (`util`, `queuingstrategies`, `readablestream` — with tee, async iteration and pipeTo, as in node — `writablestream`, `transformstream`), all eager through `stream/web` and acyclic: `readablestream` requires `writablestream`, `transformstream` -requires both, nothing requires back. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +requires both, nothing requires back. Fetch follows undici's `lib/web/fetch` seams: +`internal/fetch/headers`, `body` (the Body mixin plus the byte and transport-stream +helpers), `request` and `response` (each requiring `headers` and `body`), while +`internal/fetch` keeps the driver — body collection, redirects, the hand-off to +`native.request` — because the loader keys natives by module id, and installs the globals. +All five are eager through `internal/fetch`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 17c2aa9..65c1315 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1638,6 +1638,7 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"node/timers", #load_directory("js/node/timers")}, {"node/util", #load_directory("js/node/util")}, {"internal", #load_directory("js/internal")}, + {"internal/fetch", #load_directory("js/internal/fetch")}, {"internal/http", #load_directory("js/internal/http")}, {"internal/streams", #load_directory("js/internal/streams")}, {"internal/webstreams", #load_directory("js/internal/webstreams")}, diff --git a/pkg/runtime/js/internal/fetch.js b/pkg/runtime/js/internal/fetch.js index a613f7d..6bd31a1 100644 --- a/pkg/runtime/js/internal/fetch.js +++ b/pkg/runtime/js/internal/fetch.js @@ -1,624 +1,26 @@ // Fetch surface — Headers, Request, Response, and a global `fetch`. Installs // the WHATWG globals (Node exposes them without a require). This is the pure-JS -// half: Headers/Request/Response/Body machinery lives here, exactly as Node -// keeps it in JS; the actual `http://` network transport is Odin-backed and +// half: Headers/Request/Response/Body machinery is JS, exactly as Node keeps +// it (split across js/internal/fetch/, see below); the actual `http://` +// network transport is Odin-backed and // reached through the `native` bindings (native.request), mirroring how // crypto/buffer receive their Odin primitives as the factory's fourth argument. (function (require, module, exports, native) { 'use strict'; - // The public Web Streams implementation (js/node/stream/web.js). fetch builds - // response bodies and consumes request bodies through this same ReadableStream - // type, so `response.body` is a real, standard ReadableStream — not a fetch-only - // fork. The `_internal` helpers let the transport drive a stream's controller - // (enqueue/close/error + desiredSize backpressure) without re-implementing the - // controller plumbing here. - var webStreams = require('node:stream/web'); - var ReadableStream = webStreams.ReadableStream; - var streamInternal = webStreams._internal; - - // Shared, lazily-created codecs. TextEncoder.encode / TextDecoder.decode - // (non-streaming) are stateless across calls, so a single instance each is - // reused across every body conversion instead of allocating one per call — - // the previous per-call `new TextEncoder()/new TextDecoder()` sat on the hot - // path (every Request/Response body, every .text()/.json()). They are created - // on first use rather than at load time: loader.js may eager-require this - // module before encoding.js installs the TextEncoder/TextDecoder globals, so - // capturing them at load time would cache null and silently fall back to a - // (wrong) latin1 round-trip (issue #43). getEncoder/getDecoder return null - // only if the global never appears, in which case the latin1 fallbacks apply. - var sharedEncoder = null; - var sharedDecoder = null; - function getEncoder() { - if (sharedEncoder === null && typeof TextEncoder !== 'undefined') - sharedEncoder = new TextEncoder(); - return sharedEncoder; - } - function getDecoder() { - if (sharedDecoder === null && typeof TextDecoder !== 'undefined') - sharedDecoder = new TextDecoder(); - return sharedDecoder; - } - - function normalizeName(name) { - return String(name).toLowerCase(); - } - - // Header names must be non-empty HTTP tokens (RFC 7230 tchar); values must not - // carry CR, LF, or NUL. Node (undici) throws on either — at append/set/get/ - // has/delete time — so `new Headers(...)` and, because Request builds its - // Headers eagerly, `fetch(input, {headers})` reject instead of silently - // emitting a header-split or otherwise malformed request. The token class - // rejects spaces, ':' (which would forge a second header on the wire), control - // chars, and non-ASCII alike. Wire (response) headers arrive pre-framed via - // _append and skip this, matching undici. - // Header-name validation over caller-controlled text — a CRLF that gets through - // here is header injection on the wire. NOT `re.test(...)`: `RegExp.prototype.exec` - // is a writable data property and RegExpExec re-reads it off the receiver, so an - // ordinary assignment steers `test` too. primordials deliberately exports no - // `RegExpPrototypeTest` for that reason; `RegExpMatches` (a captured `exec` - // compared against null) is the only sound spelling. - // - // The value half is closed too, and differently: `normalizeHeaderValue` below is - // now a charCode trim rather than a global `replaceAll`, because a global regex - // does not merely answer wrongly under a forged `exec` — it never terminates. - // `assertValidHeaderValue` was already a hand-rolled charCode scan. - var P = require('internal/primordials'); - var reTest = P.RegExpMatches; - var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; - var StringPrototypeSlice = P.StringPrototypeSlice; - var VALID_HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; - function assertValidHeaderName(name) { - if (!reTest(VALID_HEADER_NAME, name)) { - throw new TypeError('Invalid header name: "' + name + '"'); - } - } - function assertValidHeaderValue(name, value) { - for (var i = 0; i < value.length; i++) { - // The CAPTURED read, not `value.charCodeAt(i)`. `String.prototype.charCodeAt` - // is a writable data property like any other, so a hand-rolled scan is only - // as sound as the primitive it scans with — routing this file's validators - // off RegExp said nothing about that. A gadget reporting 0x41 for CR and LF - // is enough: measured, node rejects and Lava accepted - // `a\r\nInjected: yes` as a header value. Pinned by `cc-value=` in - // cmd/lava/regexp_pollution_test.odin (and the fetch.js mutation-manifest - // entry). The same gadget on the HTTP response path is pinned by the - // charcodeat phase of run-http-smoke.sh against http.js. - var code = StringPrototypeCharCodeAt(value, i); - if (code === 0 || code === 10 || code === 13) { - throw new TypeError('Invalid header value for "' + name + '": "' + value + '"'); - } - } - } - - // Fetch spec "normalize a byte sequence" (https://fetch.spec.whatwg.org/#concept-header-value-normalize): - // strip leading/trailing HTTP whitespace — tab (0x09), LF (0x0A), CR (0x0D), - // space (0x20) — from a header value. undici applies this on the public - // append/set path BEFORE validating, so a value like "\r\nfoo" normalizes to - // "foo" and is accepted, while an interior CR/LF ("foo\r\nbar") survives the - // trim and is then rejected by assertValidHeaderValue. Vertical tab / form - // feed are not HTTP whitespace and are preserved. Wire (response) headers - // arrive via _append and are not re-normalized — the transport already trims - // OWS around values — matching undici, which fills response headers verbatim. - // - // A charCode trim, not `replaceAll(/…/g, '')`. The regex form was a live method - // read on the RECEIVER, and a GLOBAL one: `RegExp.prototype[Symbol.replace]` - // loops on RegExpExec and only advances `lastIndex` on an EMPTY match, so a - // forged non-empty result never terminates. Measured on `bin/lava` before this - // change: with `RegExp.prototype.exec` replaced, `new Headers().set('X-Ok','v')` - // — a perfectly valid header — never returned, and a URL parse on the same path - // reached 1.4 GB RSS in 4 s. That is a remote-triggerable DoS, because it runs - // BEFORE assertValidHeaderName on every append and set. - // Routing it through a captured `exec` would fix the steering but not the spin; - // removing the RegExp removes both, and there is nothing left to poison. - function normalizeHeaderValue(value) { - var start = 0; - var end = value.length; - while (start < end && isHttpWhitespace(StringPrototypeCharCodeAt(value, start))) start++; - while (end > start && isHttpWhitespace(StringPrototypeCharCodeAt(value, end - 1))) end--; - return start === 0 && end === value.length ? value : StringPrototypeSlice(value, start, end); - } - - // Tab, LF, CR, space — HTTP whitespace per the fetch spec. Vertical tab and form - // feed are deliberately absent; they are not HTTP whitespace and must survive. - function isHttpWhitespace(c) { - return c === 0x09 || c === 0x0a || c === 0x0d || c === 0x20; - } - - // Shared by Headers.append and Headers.set, which performed the identical - // normalize→validate→coerce dance. Order matches undici: trim the value, - // then reject a bad name, then reject a bad (trimmed) value. Returns the - // cleaned name/value pair ready to store. - function normalizeAndValidate(name, value) { - var text = normalizeHeaderValue(String(value)); - var key = String(name); - assertValidHeaderName(key); - assertValidHeaderValue(key, text); - return { name: key, value: text }; - } - - class Headers { - constructor(init) { - this._map = new Map(); - // Set-Cookie is the one header that must NOT be comma-joined: each - // Set-Cookie is its own list entry (cookie Expires= values themselves - // contain commas, so a join is lossy and corrupts them). It is held apart - // from _map and exposed individually via getSetCookie(). See _append. - this._cookies = []; - if (init instanceof Headers) { - init.forEach((value, key) => this.append(key, value)); - } else if (Array.isArray(init)) { - for (var i = 0; i < init.length; i++) this.append(init[i][0], init[i][1]); - } else if (init && typeof init === 'object') { - var keys = Object.keys(init); - for (var j = 0; j < keys.length; j++) this.append(keys[j], init[keys[j]]); - } - } - append(name, value) { - var header = normalizeAndValidate(name, value); - this._append(header.name, header.value); - } - // _append stores without validation — for response headers parsed off the - // wire, which the transport has already framed line-by-line. Set-Cookie is - // appended to its own list (never comma-joined) so getSetCookie() can return - // each cookie intact. - _append(name, value) { - var key = normalizeName(name); - if (key === 'set-cookie') { - this._cookies.push(String(value)); - return; - } - var existing = this._map.get(key); - this._map.set(key, existing === undefined ? String(value) : existing + ', ' + value); - } - set(name, value) { - var header = normalizeAndValidate(name, value); - var key = normalizeName(header.name); - if (key === 'set-cookie') { - this._cookies = [header.value]; - return; - } - this._map.set(key, header.value); - } - get(name) { - assertValidHeaderName(String(name)); - var key = normalizeName(name); - // get('set-cookie') returns the cookies joined by ', ' (Node parity); the - // separation is preserved only through getSetCookie(). - if (key === 'set-cookie') return this._cookies.length ? this._cookies.join(', ') : null; - var v = this._map.get(key); - return v === undefined ? null : v; - } - // getSetCookie returns each Set-Cookie value as its own string, uncorrupted - // by comma-joining (WHATWG Headers.getSetCookie()). - getSetCookie() { - return this._cookies.slice(); - } - has(name) { - assertValidHeaderName(String(name)); - var key = normalizeName(name); - if (key === 'set-cookie') return this._cookies.length > 0; - return this._map.has(key); - } - delete(name) { - assertValidHeaderName(String(name)); - var key = normalizeName(name); - if (key === 'set-cookie') { - this._cookies = []; - return; - } - this._map.delete(key); - } - // _entryList snapshots all header entries — the comma-joined _map values plus - // one entry per individual Set-Cookie — so iteration surfaces every cookie - // separately (the spec does not combine Set-Cookie in the iterator). - _entryList() { - var out = []; - this._map.forEach((value, key) => out.push([key, value])); - for (var i = 0; i < this._cookies.length; i++) out.push(['set-cookie', this._cookies[i]]); - return out; - } - forEach(callback, thisArg) { - var entries = this._entryList(); - for (var i = 0; i < entries.length; i++) { - callback.call(thisArg, entries[i][1], entries[i][0], this); - } - } - keys() { - return this._entryList() - .map((e) => e[0]) - [Symbol.iterator](); - } - values() { - return this._entryList() - .map((e) => e[1]) - [Symbol.iterator](); - } - entries() { - return this._entryList()[Symbol.iterator](); - } - [Symbol.iterator]() { - return this._entryList()[Symbol.iterator](); - } - } - - // Body storage stays byte-exact; request bodies and response bytes must not - // round-trip through latin1 text. - function bodyToBytes(body) { - if (body === null || body === undefined) return null; - if (body instanceof Uint8Array) return body; - if (body instanceof ArrayBuffer) return new Uint8Array(body); - // A Blob/File body: join its in-memory chunks synchronously. Lava's Blob keeps - // its bytes in memory (_parts), so no async read is needed; concatChunks yields - // null for an empty Blob (absent body). A Blob used as a *request* body instead - // takes Request's async _streamBody path and never reaches here. - if (typeof Blob !== 'undefined' && body instanceof Blob) return concatChunks(body._parts || []); - var text = typeof body === 'string' ? body : String(body); - // An empty string (passed directly or produced by String(body)) has no - // bytes — treat it as an absent body. - if (text === '') return null; - var encoder = getEncoder(); - if (encoder) return encoder.encode(text); - var bytes = new Uint8Array(text.length); - for (var i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i) & 0xff; - return bytes; - } - - // bytesToText UTF-8 decodes stored bytes (matches Node's Body.text(); a latin1 - // fallback keeps things working if TextDecoder is somehow unavailable). - function bytesToText(bytes) { - if (bytes === null) return ''; - var decoder = getDecoder(); - if (decoder) return decoder.decode(bytes); - var s = ''; - for (var i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]); - return s; - } - - // concatChunks joins an array of Uint8Array chunks into one. Returns null for an - // empty list (an absent body) and the single chunk as-is when there is only one. - function concatChunks(chunks) { - if (chunks.length === 0) return null; - if (chunks.length === 1) return chunks[0]; - var total = 0; - for (var i = 0; i < chunks.length; i++) total += chunks[i].length; - var out = new Uint8Array(total); - var off = 0; - for (var j = 0; j < chunks.length; j++) { - out.set(chunks[j], off); - off += chunks[j].length; - } - return out; - } - - // chunkToBytes coerces a value yielded by a streaming request body (string, - // ArrayBuffer, typed-array view, or Uint8Array) to bytes. - function chunkToBytes(chunk) { - if (chunk instanceof Uint8Array) return chunk; - if (chunk instanceof ArrayBuffer) return new Uint8Array(chunk); - if (ArrayBuffer.isView(chunk)) - return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength); - var encoder = getEncoder(); - var text = typeof chunk === 'string' ? chunk : String(chunk); - if (encoder) return encoder.encode(text); - var bytes = new Uint8Array(text.length); - for (var i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i) & 0xff; - return bytes; - } - - // BODY_HWM is the response-body high-water mark in bytes. When more than this - // many undelivered bytes are queued in the response ReadableStream, its - // controller.desiredSize goes non-positive and the native transport pauses the - // socket read until the consumer drains below it (backpressure). - var BODY_HWM = 64 * 1024; - - // byteSizeAlgorithm sizes a queued response chunk by its byte length, so the - // response-body high-water mark (BODY_HWM) is measured in bytes — the unit the - // native transport pauses/resumes on. - function byteSizeAlgorithm(chunk) { - return chunk && chunk.byteLength ? chunk.byteLength : 0; - } - - // makeTransportStream builds a response-body ReadableStream fed by the native - // transport. It returns the public stream plus push handles the transport's - // sink uses: enqueue(bytes) returns whether to keep reading (desiredSize > 0), - // close() / error(err) settle the stream. enqueue/close are guarded because a - // late chunk can arrive after the consumer cancelled (stream no longer - // readable); the guard turns that into a harmless no-op rather than a throw. - function makeTransportStream(hooks) { - var captured = streamInternal.createReadableStreamWithController( - { - // pull resumes a paused socket read; a no-op when not paused, so it is - // safe for the controller to call it on every drained read. - pull: hooks.pull, - cancel: hooks.cancel, - }, - BODY_HWM, - byteSizeAlgorithm, - ); - var controller = captured.controller; - return { - stream: captured.stream, - enqueue: function (bytes) { - try { - controller.enqueue(bytes); - } catch { - return false; - } - return controller.desiredSize > 0; - }, - close: function () { - try { - controller.close(); - } catch {} - }, - error: function (err) { - controller.error(err); - }, - }; - } - - // streamFromBytes wraps an in-memory body in a one-shot ReadableStream so .body - // is a real ReadableStream (Node parity). Enqueuing happens inside start(), - // which does not mark the stream disturbed, so merely reading `.body` does not - // flip bodyUsed. - function streamFromBytes(bytes) { - return new ReadableStream({ - start: function (controller) { - if (bytes !== null && bytes !== undefined && bytes.length > 0) controller.enqueue(bytes); - controller.close(); - }, - }); - } - - // Body backs Request and Response. It holds EITHER an in-memory byte body - // (_bodyBytes, the fast path for JS-constructed bodies and buffered request - // bodies) OR a streaming body (_bodyStream, fed by the transport). The buffered - // accessors (text/json/arrayBuffer/bytes) drain the stream when present, so - // streaming and buffering share one consumption path and a body is consumed at - // most once. - class Body { - constructor(bodyInit) { - if (streamInternal.isReadableStream(bodyInit)) { - this._bodyStream = bodyInit; - this._bodyBytes = undefined; - } else { - this._bodyStream = null; - this._bodyBytes = bodyToBytes(bodyInit); // Uint8Array | null - } - this._bodyUsed = false; - } - get bodyUsed() { - if (this._bodyUsed) return true; - if (this._bodyStream) return this._bodyStream._disturbed; - return false; - } - get body() { - if (this._bodyStream) return this._bodyStream; - if (this._bodyBytes === null || this._bodyBytes === undefined) return null; - this._bodyStream = streamFromBytes(this._bodyBytes); - return this._bodyStream; - } - async _consume() { - if (this.bodyUsed) throw new TypeError('Body is unusable: Body has already been read'); - if (this._bodyStream && this._bodyStream.locked) - throw new TypeError('Body is unusable: ReadableStream is locked'); - this._bodyUsed = true; - if (this._bodyStream) { - var reader = this._bodyStream.getReader(); - var chunks = []; - try { - for (;;) { - var r = await reader.read(); - if (r.done) break; - chunks.push(r.value); - } - } finally { - reader.releaseLock(); - } - return concatChunks(chunks); - } - return this._bodyBytes; // Uint8Array | null - } - text() { - return this._consume().then(function (bytes) { - return bytesToText(bytes); - }); - } - json() { - // Parse inside the chain so invalid JSON rejects (WHATWG fetch semantics). - return this._consume().then(function (bytes) { - return JSON.parse(bytesToText(bytes)); - }); - } - arrayBuffer() { - return this._consume().then(function (bytes) { - if (bytes === null || bytes === undefined) return new ArrayBuffer(0); - var copy = new Uint8Array(bytes.length); - copy.set(bytes); - return copy.buffer; - }); - } - bytes() { - return this._consume().then(function (bytes) { - if (bytes === null || bytes === undefined) return new Uint8Array(0); - var copy = new Uint8Array(bytes.length); - copy.set(bytes); - return copy; - }); - } - } - - class Response extends Body { - constructor(body, init) { - super(body); - init = init || {}; - this.status = init.status === undefined ? 200 : init.status; - this.statusText = init.statusText === undefined ? '' : init.statusText; - this.headers = new Headers(init.headers); - applyBlobContentType(this.headers, body); - this.ok = this.status >= 200 && this.status < 300; - this.redirected = false; - this.type = 'default'; - this.url = init.url || ''; - } - clone() { - if (this.bodyUsed) throw new TypeError('Response body is already used'); - var clonedBody; - if (this._bodyStream) { - // Tee the live stream so both responses can be read independently. - var branches = this._bodyStream.tee(); - this._bodyStream = branches[0]; - clonedBody = branches[1]; - } else { - // In-memory body: share the Uint8Array (all reads are non-mutating — - // text/json, and arrayBuffer/bytes copy out). - clonedBody = this._bodyBytes; - } - var cloned = new Response(clonedBody, { - status: this.status, - statusText: this.statusText, - headers: this.headers, - url: this.url, - }); - // The constructor resets these to their defaults (redirected=false, - // type='default'); a clone must mirror the source's observable metadata — - // e.g. a response produced by a followed redirect has redirected=true. - cloned.redirected = this.redirected; - cloned.type = this.type; - return cloned; - } - } - - Response.json = function (data, init) { - init = init || {}; - var headers = new Headers(init.headers); - if (!headers.has('content-type')) headers.set('content-type', 'application/json'); - return new Response(JSON.stringify(data), { - status: init.status, - statusText: init.statusText, - headers: headers, - }); - }; - - Response.error = function () { - return new Response(null, { status: 0 }); - }; - - // isStreamLikeBody recognizes a request body that cannot be turned into bytes - // synchronously: a Blob, a ReadableStream (anything with getReader), or an async - // iterable. These are collected asynchronously in fetch() before the request is - // sent (see the v1 limitation note in reference/node-compatibility.md). - function isStreamLikeBody(b) { - if (b === null || b === undefined || typeof b === 'string') return false; - if (b instanceof Uint8Array || b instanceof ArrayBuffer || ArrayBuffer.isView(b)) return false; - if (typeof Blob !== 'undefined' && b instanceof Blob) return true; - if (typeof b.getReader === 'function') return true; - if (typeof b[Symbol.asyncIterator] === 'function') return true; - return false; - } - - // Per WHATWG, extracting a body from a Blob contributes a Content-Type equal to - // the Blob's `type` (when non-empty) — applied only if the caller didn't already - // set one via init.headers (or an inherited source Request's headers). - function applyBlobContentType(headers, body) { - if ( - typeof Blob !== 'undefined' && - body instanceof Blob && - body.type && - !headers.has('content-type') - ) { - headers.set('content-type', body.type); - } - } - - class Request extends Body { - constructor(input, init) { - init = init || {}; - // When input is another Request, its method/headers/body seed the new - // one and init.* overrides them (WHATWG fetch). A string/URL input - // contributes only the URL. We treat input as a Request when it carries - // a string `url`, so URL objects (which stringify to their href) stay on - // the string path. - var src = input && typeof input === 'object' && typeof input.url === 'string' ? input : null; - // Per spec, init.body overrides only when present and non-null; a null - // or absent init.body keeps the source Request's body. A source's body may - // be a streaming body (held in _streamBody, with _bodyBytes null), so - // inherit that too — otherwise fetch(new Request(req)) would silently send - // an empty body. (v1 caveat: the stream reference is shared, not teed, so - // fetching both the source and the derived request is not supported.) - var initBodyGiven = init.body !== undefined && init.body !== null; - var bodyInit = initBodyGiven - ? init.body - : src - ? src._streamBody != null - ? src._streamBody - : src._bodyBytes - : init.body; - - // A streaming body (Blob / ReadableStream / async iterable) is held as-is - // and materialized to bytes in fetch(); only buffered bodies go to Body. - var streamBody = bodyInit != null && isStreamLikeBody(bodyInit) ? bodyInit : null; - if (streamBody) { - super(null); - this._streamBody = streamBody; - // Node requires duplex:'half' for a ReadableStream / async-iterable body; - // a Blob is a known-length body and is exempt. The check is at construction - // (where the duplex option lives) — only for a NEW init.body stream; a body - // inherited from a source Request was already validated when it was built. - this._streamNeedsDuplex = !(typeof Blob !== 'undefined' && streamBody instanceof Blob); - if (this._streamNeedsDuplex && initBodyGiven && init.duplex !== 'half') { - throw new TypeError('fetch: a streaming request body requires init.duplex to be "half"'); - } - } else { - super(bodyInit); - this._streamBody = null; - this._streamNeedsDuplex = false; - } - - // Parse the URL through the WHATWG parser (Node parity): an absent base - // means relative URLs, a non-numeric or out-of-range port, and other - // malformed inputs all throw a TypeError here rather than silently - // falling back to a default port in the native layer. A source Request's - // url is already an absolute href, so this normalizes idempotently. - var rawUrl = src ? src.url : String(input); - // WHATWG fetch forbids credentials in a request URL. new URL() preserves any - // userinfo (and the native parser silently drops it), so reject it here for - // Node parity rather than connect to a host the visible URL can disguise - // (e.g. "http://example.com@127.0.0.1/"). This also covers redirect-created - // requests, since buildRedirectRequest resolves Location through new Request. - var parsedUrl = new URL(rawUrl); - if (parsedUrl.username || parsedUrl.password) { - throw new TypeError('fetch: request URL cannot contain credentials'); - } - this.url = parsedUrl.href; - // redirect mode: 'follow' (default) | 'manual' | 'error'. init overrides a - // source Request's mode (WHATWG fetch); fetch() consults it per response. The - // value is coerced to a string (Web IDL enum semantics — Node accepts a String - // object or any toString-able), a symbol throws (ToString throws), and an - // unrecognized mode is a TypeError, not a silent 'follow'. - var redirect = init.redirect !== undefined ? init.redirect : src ? src.redirect : 'follow'; - if (typeof redirect === 'symbol') throw new TypeError('fetch: invalid redirect mode'); - redirect = String(redirect); - if (redirect !== 'follow' && redirect !== 'manual' && redirect !== 'error') { - throw new TypeError("fetch: invalid redirect mode '" + redirect + "'"); - } - this.redirect = redirect; - var method = init.method !== undefined ? init.method : src ? src.method : 'GET'; - this.method = String(method || 'GET').toUpperCase(); - // A GET/HEAD request cannot carry a body, whether that body came from - // init or was inherited from a source Request (WHATWG fetch). - if ( - (this.method === 'GET' || this.method === 'HEAD') && - (this._bodyBytes != null || this._streamBody) - ) { - throw new TypeError('Request with GET/HEAD method cannot have body.'); - } - var headersInit = init.headers !== undefined ? init.headers : src ? src.headers : undefined; - this.headers = new Headers(headersInit); - applyBlobContentType(this.headers, bodyInit); - } - } + // Headers, Body, Request and Response live in js/internal/fetch/ (headers, body, + // request, response), laid out like undici's lib/web/fetch so each class has its own + // file and frame name. This module keeps what needs `native`: the fetch() driver — + // body collection, redirects, and the streaming hand-off to the Odin transport — + // because the loader keys natives by module id and native.request is bound to + // `internal/fetch` (globals.odin). It also installs the globals. + var Headers = require('internal/fetch/headers').Headers; + var Request = require('internal/fetch/request').Request; + var Response = require('internal/fetch/response').Response; + var bodyModule = require('internal/fetch/body'); + var chunkToBytes = bodyModule.chunkToBytes; + var concatChunks = bodyModule.concatChunks; + var makeTransportStream = bodyModule.makeTransportStream; // collectStreamBody materializes a known-length streaming body (a Blob) to a // single Uint8Array so it can be sent with Content-Length framing — matching diff --git a/pkg/runtime/js/internal/fetch/body.js b/pkg/runtime/js/internal/fetch/body.js new file mode 100644 index 0000000..9b655de --- /dev/null +++ b/pkg/runtime/js/internal/fetch/body.js @@ -0,0 +1,269 @@ +// The Body mixin Request and Response share, plus the byte/stream plumbing behind it +// (bytes <-> text, chunk coercion, the transport-fed response stream) — the Lava side +// of undici's lib/web/fetch/body.js. The fetch driver (internal/fetch) also takes +// concatChunks/chunkToBytes/makeTransportStream from here, so there is one copy. +(function (require, module) { + 'use strict'; + + // The public Web Streams implementation (js/node/stream/web.js). fetch builds + // response bodies and consumes request bodies through this same ReadableStream + // type, so `response.body` is a real, standard ReadableStream — not a fetch-only + // fork. The `_internal` helpers let the transport drive a stream's controller + // (enqueue/close/error + desiredSize backpressure) without re-implementing the + // controller plumbing here. + var webStreams = require('node:stream/web'); + var ReadableStream = webStreams.ReadableStream; + var streamInternal = webStreams._internal; + + // Shared, lazily-created codecs. TextEncoder.encode / TextDecoder.decode + // (non-streaming) are stateless across calls, so a single instance each is + // reused across every body conversion instead of allocating one per call — + // the previous per-call `new TextEncoder()/new TextDecoder()` sat on the hot + // path (every Request/Response body, every .text()/.json()). They are created + // on first use rather than at load time: loader.js may eager-require this + // module before encoding.js installs the TextEncoder/TextDecoder globals, so + // capturing them at load time would cache null and silently fall back to a + // (wrong) latin1 round-trip (issue #43). getEncoder/getDecoder return null + // only if the global never appears, in which case the latin1 fallbacks apply. + var sharedEncoder = null; + var sharedDecoder = null; + function getEncoder() { + if (sharedEncoder === null && typeof TextEncoder !== 'undefined') + sharedEncoder = new TextEncoder(); + return sharedEncoder; + } + function getDecoder() { + if (sharedDecoder === null && typeof TextDecoder !== 'undefined') + sharedDecoder = new TextDecoder(); + return sharedDecoder; + } + + // Body storage stays byte-exact; request bodies and response bytes must not + // round-trip through latin1 text. + function bodyToBytes(body) { + if (body === null || body === undefined) return null; + if (body instanceof Uint8Array) return body; + if (body instanceof ArrayBuffer) return new Uint8Array(body); + // A Blob/File body: join its in-memory chunks synchronously. Lava's Blob keeps + // its bytes in memory (_parts), so no async read is needed; concatChunks yields + // null for an empty Blob (absent body). A Blob used as a *request* body instead + // takes Request's async _streamBody path and never reaches here. + if (typeof Blob !== 'undefined' && body instanceof Blob) return concatChunks(body._parts || []); + var text = typeof body === 'string' ? body : String(body); + // An empty string (passed directly or produced by String(body)) has no + // bytes — treat it as an absent body. + if (text === '') return null; + var encoder = getEncoder(); + if (encoder) return encoder.encode(text); + var bytes = new Uint8Array(text.length); + for (var i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i) & 0xff; + return bytes; + } + + // bytesToText UTF-8 decodes stored bytes (matches Node's Body.text(); a latin1 + // fallback keeps things working if TextDecoder is somehow unavailable). + function bytesToText(bytes) { + if (bytes === null) return ''; + var decoder = getDecoder(); + if (decoder) return decoder.decode(bytes); + var s = ''; + for (var i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]); + return s; + } + + // concatChunks joins an array of Uint8Array chunks into one. Returns null for an + // empty list (an absent body) and the single chunk as-is when there is only one. + function concatChunks(chunks) { + if (chunks.length === 0) return null; + if (chunks.length === 1) return chunks[0]; + var total = 0; + for (var i = 0; i < chunks.length; i++) total += chunks[i].length; + var out = new Uint8Array(total); + var off = 0; + for (var j = 0; j < chunks.length; j++) { + out.set(chunks[j], off); + off += chunks[j].length; + } + return out; + } + + // chunkToBytes coerces a value yielded by a streaming request body (string, + // ArrayBuffer, typed-array view, or Uint8Array) to bytes. + function chunkToBytes(chunk) { + if (chunk instanceof Uint8Array) return chunk; + if (chunk instanceof ArrayBuffer) return new Uint8Array(chunk); + if (ArrayBuffer.isView(chunk)) + return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength); + var encoder = getEncoder(); + var text = typeof chunk === 'string' ? chunk : String(chunk); + if (encoder) return encoder.encode(text); + var bytes = new Uint8Array(text.length); + for (var i = 0; i < text.length; i++) bytes[i] = text.charCodeAt(i) & 0xff; + return bytes; + } + + // BODY_HWM is the response-body high-water mark in bytes. When more than this + // many undelivered bytes are queued in the response ReadableStream, its + // controller.desiredSize goes non-positive and the native transport pauses the + // socket read until the consumer drains below it (backpressure). + var BODY_HWM = 64 * 1024; + + // byteSizeAlgorithm sizes a queued response chunk by its byte length, so the + // response-body high-water mark (BODY_HWM) is measured in bytes — the unit the + // native transport pauses/resumes on. + function byteSizeAlgorithm(chunk) { + return chunk && chunk.byteLength ? chunk.byteLength : 0; + } + + // makeTransportStream builds a response-body ReadableStream fed by the native + // transport. It returns the public stream plus push handles the transport's + // sink uses: enqueue(bytes) returns whether to keep reading (desiredSize > 0), + // close() / error(err) settle the stream. enqueue/close are guarded because a + // late chunk can arrive after the consumer cancelled (stream no longer + // readable); the guard turns that into a harmless no-op rather than a throw. + function makeTransportStream(hooks) { + var captured = streamInternal.createReadableStreamWithController( + { + // pull resumes a paused socket read; a no-op when not paused, so it is + // safe for the controller to call it on every drained read. + pull: hooks.pull, + cancel: hooks.cancel, + }, + BODY_HWM, + byteSizeAlgorithm, + ); + var controller = captured.controller; + return { + stream: captured.stream, + enqueue: function (bytes) { + try { + controller.enqueue(bytes); + } catch { + return false; + } + return controller.desiredSize > 0; + }, + close: function () { + try { + controller.close(); + } catch {} + }, + error: function (err) { + controller.error(err); + }, + }; + } + + // streamFromBytes wraps an in-memory body in a one-shot ReadableStream so .body + // is a real ReadableStream (Node parity). Enqueuing happens inside start(), + // which does not mark the stream disturbed, so merely reading `.body` does not + // flip bodyUsed. + function streamFromBytes(bytes) { + return new ReadableStream({ + start: function (controller) { + if (bytes !== null && bytes !== undefined && bytes.length > 0) controller.enqueue(bytes); + controller.close(); + }, + }); + } + + // Body backs Request and Response. It holds EITHER an in-memory byte body + // (_bodyBytes, the fast path for JS-constructed bodies and buffered request + // bodies) OR a streaming body (_bodyStream, fed by the transport). The buffered + // accessors (text/json/arrayBuffer/bytes) drain the stream when present, so + // streaming and buffering share one consumption path and a body is consumed at + // most once. + class Body { + constructor(bodyInit) { + if (streamInternal.isReadableStream(bodyInit)) { + this._bodyStream = bodyInit; + this._bodyBytes = undefined; + } else { + this._bodyStream = null; + this._bodyBytes = bodyToBytes(bodyInit); // Uint8Array | null + } + this._bodyUsed = false; + } + get bodyUsed() { + if (this._bodyUsed) return true; + if (this._bodyStream) return this._bodyStream._disturbed; + return false; + } + get body() { + if (this._bodyStream) return this._bodyStream; + if (this._bodyBytes === null || this._bodyBytes === undefined) return null; + this._bodyStream = streamFromBytes(this._bodyBytes); + return this._bodyStream; + } + async _consume() { + if (this.bodyUsed) throw new TypeError('Body is unusable: Body has already been read'); + if (this._bodyStream && this._bodyStream.locked) + throw new TypeError('Body is unusable: ReadableStream is locked'); + this._bodyUsed = true; + if (this._bodyStream) { + var reader = this._bodyStream.getReader(); + var chunks = []; + try { + for (;;) { + var r = await reader.read(); + if (r.done) break; + chunks.push(r.value); + } + } finally { + reader.releaseLock(); + } + return concatChunks(chunks); + } + return this._bodyBytes; // Uint8Array | null + } + text() { + return this._consume().then(function (bytes) { + return bytesToText(bytes); + }); + } + json() { + // Parse inside the chain so invalid JSON rejects (WHATWG fetch semantics). + return this._consume().then(function (bytes) { + return JSON.parse(bytesToText(bytes)); + }); + } + arrayBuffer() { + return this._consume().then(function (bytes) { + if (bytes === null || bytes === undefined) return new ArrayBuffer(0); + var copy = new Uint8Array(bytes.length); + copy.set(bytes); + return copy.buffer; + }); + } + bytes() { + return this._consume().then(function (bytes) { + if (bytes === null || bytes === undefined) return new Uint8Array(0); + var copy = new Uint8Array(bytes.length); + copy.set(bytes); + return copy; + }); + } + } + + // Per WHATWG, extracting a body from a Blob contributes a Content-Type equal to + // the Blob's `type` (when non-empty) — applied only if the caller didn't already + // set one via init.headers (or an inherited source Request's headers). + function applyBlobContentType(headers, body) { + if ( + typeof Blob !== 'undefined' && + body instanceof Blob && + body.type && + !headers.has('content-type') + ) { + headers.set('content-type', body.type); + } + } + + module.exports = { + Body: Body, + applyBlobContentType: applyBlobContentType, + chunkToBytes: chunkToBytes, + concatChunks: concatChunks, + makeTransportStream: makeTransportStream, + }; +}); diff --git a/pkg/runtime/js/internal/fetch/headers.js b/pkg/runtime/js/internal/fetch/headers.js new file mode 100644 index 0000000..02b8240 --- /dev/null +++ b/pkg/runtime/js/internal/fetch/headers.js @@ -0,0 +1,210 @@ +// Headers and the name/value validation and normalization behind it — the Lava side +// of undici's lib/web/fetch/headers.js (node bundles undici, so its frames read +// node:internal/deps/undici/undici; ours read node:internal/fetch/headers). +(function (require, module) { + 'use strict'; + + function normalizeName(name) { + return String(name).toLowerCase(); + } + + // Header names must be non-empty HTTP tokens (RFC 7230 tchar); values must not + // carry CR, LF, or NUL. Node (undici) throws on either — at append/set/get/ + // has/delete time — so `new Headers(...)` and, because Request builds its + // Headers eagerly, `fetch(input, {headers})` reject instead of silently + // emitting a header-split or otherwise malformed request. The token class + // rejects spaces, ':' (which would forge a second header on the wire), control + // chars, and non-ASCII alike. Wire (response) headers arrive pre-framed via + // _append and skip this, matching undici. + // Header-name validation over caller-controlled text — a CRLF that gets through + // here is header injection on the wire. NOT `re.test(...)`: `RegExp.prototype.exec` + // is a writable data property and RegExpExec re-reads it off the receiver, so an + // ordinary assignment steers `test` too. primordials deliberately exports no + // `RegExpPrototypeTest` for that reason; `RegExpMatches` (a captured `exec` + // compared against null) is the only sound spelling. + // + // The value half is closed too, and differently: `normalizeHeaderValue` below is + // now a charCode trim rather than a global `replaceAll`, because a global regex + // does not merely answer wrongly under a forged `exec` — it never terminates. + // `assertValidHeaderValue` was already a hand-rolled charCode scan. + var P = require('internal/primordials'); + var reTest = P.RegExpMatches; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeSlice = P.StringPrototypeSlice; + var VALID_HEADER_NAME = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/; + function assertValidHeaderName(name) { + if (!reTest(VALID_HEADER_NAME, name)) { + throw new TypeError('Invalid header name: "' + name + '"'); + } + } + function assertValidHeaderValue(name, value) { + for (var i = 0; i < value.length; i++) { + // The CAPTURED read, not `value.charCodeAt(i)`. `String.prototype.charCodeAt` + // is a writable data property like any other, so a hand-rolled scan is only + // as sound as the primitive it scans with — routing this file's validators + // off RegExp said nothing about that. A gadget reporting 0x41 for CR and LF + // is enough: measured, node rejects and Lava accepted + // `a\r\nInjected: yes` as a header value. Pinned by `cc-value=` in + // cmd/lava/regexp_pollution_test.odin (and the headers.js mutation-manifest + // entry). The same gadget on the HTTP response path is pinned by the + // charcodeat phase of run-http-smoke.sh against http.js. + var code = StringPrototypeCharCodeAt(value, i); + if (code === 0 || code === 10 || code === 13) { + throw new TypeError('Invalid header value for "' + name + '": "' + value + '"'); + } + } + } + + // Fetch spec "normalize a byte sequence" (https://fetch.spec.whatwg.org/#concept-header-value-normalize): + // strip leading/trailing HTTP whitespace — tab (0x09), LF (0x0A), CR (0x0D), + // space (0x20) — from a header value. undici applies this on the public + // append/set path BEFORE validating, so a value like "\r\nfoo" normalizes to + // "foo" and is accepted, while an interior CR/LF ("foo\r\nbar") survives the + // trim and is then rejected by assertValidHeaderValue. Vertical tab / form + // feed are not HTTP whitespace and are preserved. Wire (response) headers + // arrive via _append and are not re-normalized — the transport already trims + // OWS around values — matching undici, which fills response headers verbatim. + // + // A charCode trim, not `replaceAll(/…/g, '')`. The regex form was a live method + // read on the RECEIVER, and a GLOBAL one: `RegExp.prototype[Symbol.replace]` + // loops on RegExpExec and only advances `lastIndex` on an EMPTY match, so a + // forged non-empty result never terminates. Measured on `bin/lava` before this + // change: with `RegExp.prototype.exec` replaced, `new Headers().set('X-Ok','v')` + // — a perfectly valid header — never returned, and a URL parse on the same path + // reached 1.4 GB RSS in 4 s. That is a remote-triggerable DoS, because it runs + // BEFORE assertValidHeaderName on every append and set. + // Routing it through a captured `exec` would fix the steering but not the spin; + // removing the RegExp removes both, and there is nothing left to poison. + function normalizeHeaderValue(value) { + var start = 0; + var end = value.length; + while (start < end && isHttpWhitespace(StringPrototypeCharCodeAt(value, start))) start++; + while (end > start && isHttpWhitespace(StringPrototypeCharCodeAt(value, end - 1))) end--; + return start === 0 && end === value.length ? value : StringPrototypeSlice(value, start, end); + } + + // Tab, LF, CR, space — HTTP whitespace per the fetch spec. Vertical tab and form + // feed are deliberately absent; they are not HTTP whitespace and must survive. + function isHttpWhitespace(c) { + return c === 0x09 || c === 0x0a || c === 0x0d || c === 0x20; + } + + // Shared by Headers.append and Headers.set, which performed the identical + // normalize→validate→coerce dance. Order matches undici: trim the value, + // then reject a bad name, then reject a bad (trimmed) value. Returns the + // cleaned name/value pair ready to store. + function normalizeAndValidate(name, value) { + var text = normalizeHeaderValue(String(value)); + var key = String(name); + assertValidHeaderName(key); + assertValidHeaderValue(key, text); + return { name: key, value: text }; + } + + class Headers { + constructor(init) { + this._map = new Map(); + // Set-Cookie is the one header that must NOT be comma-joined: each + // Set-Cookie is its own list entry (cookie Expires= values themselves + // contain commas, so a join is lossy and corrupts them). It is held apart + // from _map and exposed individually via getSetCookie(). See _append. + this._cookies = []; + if (init instanceof Headers) { + init.forEach((value, key) => this.append(key, value)); + } else if (Array.isArray(init)) { + for (var i = 0; i < init.length; i++) this.append(init[i][0], init[i][1]); + } else if (init && typeof init === 'object') { + var keys = Object.keys(init); + for (var j = 0; j < keys.length; j++) this.append(keys[j], init[keys[j]]); + } + } + append(name, value) { + var header = normalizeAndValidate(name, value); + this._append(header.name, header.value); + } + // _append stores without validation — for response headers parsed off the + // wire, which the transport has already framed line-by-line. Set-Cookie is + // appended to its own list (never comma-joined) so getSetCookie() can return + // each cookie intact. + _append(name, value) { + var key = normalizeName(name); + if (key === 'set-cookie') { + this._cookies.push(String(value)); + return; + } + var existing = this._map.get(key); + this._map.set(key, existing === undefined ? String(value) : existing + ', ' + value); + } + set(name, value) { + var header = normalizeAndValidate(name, value); + var key = normalizeName(header.name); + if (key === 'set-cookie') { + this._cookies = [header.value]; + return; + } + this._map.set(key, header.value); + } + get(name) { + assertValidHeaderName(String(name)); + var key = normalizeName(name); + // get('set-cookie') returns the cookies joined by ', ' (Node parity); the + // separation is preserved only through getSetCookie(). + if (key === 'set-cookie') return this._cookies.length ? this._cookies.join(', ') : null; + var v = this._map.get(key); + return v === undefined ? null : v; + } + // getSetCookie returns each Set-Cookie value as its own string, uncorrupted + // by comma-joining (WHATWG Headers.getSetCookie()). + getSetCookie() { + return this._cookies.slice(); + } + has(name) { + assertValidHeaderName(String(name)); + var key = normalizeName(name); + if (key === 'set-cookie') return this._cookies.length > 0; + return this._map.has(key); + } + delete(name) { + assertValidHeaderName(String(name)); + var key = normalizeName(name); + if (key === 'set-cookie') { + this._cookies = []; + return; + } + this._map.delete(key); + } + // _entryList snapshots all header entries — the comma-joined _map values plus + // one entry per individual Set-Cookie — so iteration surfaces every cookie + // separately (the spec does not combine Set-Cookie in the iterator). + _entryList() { + var out = []; + this._map.forEach((value, key) => out.push([key, value])); + for (var i = 0; i < this._cookies.length; i++) out.push(['set-cookie', this._cookies[i]]); + return out; + } + forEach(callback, thisArg) { + var entries = this._entryList(); + for (var i = 0; i < entries.length; i++) { + callback.call(thisArg, entries[i][1], entries[i][0], this); + } + } + keys() { + return this._entryList() + .map((e) => e[0]) + [Symbol.iterator](); + } + values() { + return this._entryList() + .map((e) => e[1]) + [Symbol.iterator](); + } + entries() { + return this._entryList()[Symbol.iterator](); + } + [Symbol.iterator]() { + return this._entryList()[Symbol.iterator](); + } + } + + module.exports = { Headers: Headers }; +}); diff --git a/pkg/runtime/js/internal/fetch/request.js b/pkg/runtime/js/internal/fetch/request.js new file mode 100644 index 0000000..a4f8568 --- /dev/null +++ b/pkg/runtime/js/internal/fetch/request.js @@ -0,0 +1,114 @@ +// Request: input/init merging, URL and redirect-mode validation, and the split +// between buffered and streaming request bodies — the Lava side of undici's +// lib/web/fetch/request.js. +(function (require, module) { + 'use strict'; + + var Headers = require('internal/fetch/headers').Headers; + var bodyModule = require('internal/fetch/body'); + var Body = bodyModule.Body; + var applyBlobContentType = bodyModule.applyBlobContentType; + + // isStreamLikeBody recognizes a request body that cannot be turned into bytes + // synchronously: a Blob, a ReadableStream (anything with getReader), or an async + // iterable. These are collected asynchronously in fetch() before the request is + // sent (see the v1 limitation note in reference/node-compatibility.md). + function isStreamLikeBody(b) { + if (b === null || b === undefined || typeof b === 'string') return false; + if (b instanceof Uint8Array || b instanceof ArrayBuffer || ArrayBuffer.isView(b)) return false; + if (typeof Blob !== 'undefined' && b instanceof Blob) return true; + if (typeof b.getReader === 'function') return true; + if (typeof b[Symbol.asyncIterator] === 'function') return true; + return false; + } + + class Request extends Body { + constructor(input, init) { + init = init || {}; + // When input is another Request, its method/headers/body seed the new + // one and init.* overrides them (WHATWG fetch). A string/URL input + // contributes only the URL. We treat input as a Request when it carries + // a string `url`, so URL objects (which stringify to their href) stay on + // the string path. + var src = input && typeof input === 'object' && typeof input.url === 'string' ? input : null; + // Per spec, init.body overrides only when present and non-null; a null + // or absent init.body keeps the source Request's body. A source's body may + // be a streaming body (held in _streamBody, with _bodyBytes null), so + // inherit that too — otherwise fetch(new Request(req)) would silently send + // an empty body. (v1 caveat: the stream reference is shared, not teed, so + // fetching both the source and the derived request is not supported.) + var initBodyGiven = init.body !== undefined && init.body !== null; + var bodyInit = initBodyGiven + ? init.body + : src + ? src._streamBody != null + ? src._streamBody + : src._bodyBytes + : init.body; + + // A streaming body (Blob / ReadableStream / async iterable) is held as-is + // and materialized to bytes in fetch(); only buffered bodies go to Body. + var streamBody = bodyInit != null && isStreamLikeBody(bodyInit) ? bodyInit : null; + if (streamBody) { + super(null); + this._streamBody = streamBody; + // Node requires duplex:'half' for a ReadableStream / async-iterable body; + // a Blob is a known-length body and is exempt. The check is at construction + // (where the duplex option lives) — only for a NEW init.body stream; a body + // inherited from a source Request was already validated when it was built. + this._streamNeedsDuplex = !(typeof Blob !== 'undefined' && streamBody instanceof Blob); + if (this._streamNeedsDuplex && initBodyGiven && init.duplex !== 'half') { + throw new TypeError('fetch: a streaming request body requires init.duplex to be "half"'); + } + } else { + super(bodyInit); + this._streamBody = null; + this._streamNeedsDuplex = false; + } + + // Parse the URL through the WHATWG parser (Node parity): an absent base + // means relative URLs, a non-numeric or out-of-range port, and other + // malformed inputs all throw a TypeError here rather than silently + // falling back to a default port in the native layer. A source Request's + // url is already an absolute href, so this normalizes idempotently. + var rawUrl = src ? src.url : String(input); + // WHATWG fetch forbids credentials in a request URL. new URL() preserves any + // userinfo (and the native parser silently drops it), so reject it here for + // Node parity rather than connect to a host the visible URL can disguise + // (e.g. "http://example.com@127.0.0.1/"). This also covers redirect-created + // requests, since buildRedirectRequest resolves Location through new Request. + var parsedUrl = new URL(rawUrl); + if (parsedUrl.username || parsedUrl.password) { + throw new TypeError('fetch: request URL cannot contain credentials'); + } + this.url = parsedUrl.href; + // redirect mode: 'follow' (default) | 'manual' | 'error'. init overrides a + // source Request's mode (WHATWG fetch); fetch() consults it per response. The + // value is coerced to a string (Web IDL enum semantics — Node accepts a String + // object or any toString-able), a symbol throws (ToString throws), and an + // unrecognized mode is a TypeError, not a silent 'follow'. + var redirect = init.redirect !== undefined ? init.redirect : src ? src.redirect : 'follow'; + if (typeof redirect === 'symbol') throw new TypeError('fetch: invalid redirect mode'); + redirect = String(redirect); + if (redirect !== 'follow' && redirect !== 'manual' && redirect !== 'error') { + throw new TypeError("fetch: invalid redirect mode '" + redirect + "'"); + } + this.redirect = redirect; + var method = init.method !== undefined ? init.method : src ? src.method : 'GET'; + this.method = String(method || 'GET').toUpperCase(); + // A GET/HEAD request cannot carry a body, whether that body came from + // init or was inherited from a source Request (WHATWG fetch). + if ( + (this.method === 'GET' || this.method === 'HEAD') && + (this._bodyBytes != null || this._streamBody) + ) { + throw new TypeError('Request with GET/HEAD method cannot have body.'); + } + var headersInit = init.headers !== undefined ? init.headers : src ? src.headers : undefined; + this.headers = new Headers(headersInit); + applyBlobContentType(this.headers, bodyInit); + } + } + + module.exports = { Request: Request }; +}); diff --git a/pkg/runtime/js/internal/fetch/response.js b/pkg/runtime/js/internal/fetch/response.js new file mode 100644 index 0000000..54a1ecc --- /dev/null +++ b/pkg/runtime/js/internal/fetch/response.js @@ -0,0 +1,68 @@ +// Response and its statics (Response.json, Response.error) — the Lava side of undici's +// lib/web/fetch/response.js. +(function (require, module) { + 'use strict'; + + var Headers = require('internal/fetch/headers').Headers; + var bodyModule = require('internal/fetch/body'); + var Body = bodyModule.Body; + var applyBlobContentType = bodyModule.applyBlobContentType; + + class Response extends Body { + constructor(body, init) { + super(body); + init = init || {}; + this.status = init.status === undefined ? 200 : init.status; + this.statusText = init.statusText === undefined ? '' : init.statusText; + this.headers = new Headers(init.headers); + applyBlobContentType(this.headers, body); + this.ok = this.status >= 200 && this.status < 300; + this.redirected = false; + this.type = 'default'; + this.url = init.url || ''; + } + clone() { + if (this.bodyUsed) throw new TypeError('Response body is already used'); + var clonedBody; + if (this._bodyStream) { + // Tee the live stream so both responses can be read independently. + var branches = this._bodyStream.tee(); + this._bodyStream = branches[0]; + clonedBody = branches[1]; + } else { + // In-memory body: share the Uint8Array (all reads are non-mutating — + // text/json, and arrayBuffer/bytes copy out). + clonedBody = this._bodyBytes; + } + var cloned = new Response(clonedBody, { + status: this.status, + statusText: this.statusText, + headers: this.headers, + url: this.url, + }); + // The constructor resets these to their defaults (redirected=false, + // type='default'); a clone must mirror the source's observable metadata — + // e.g. a response produced by a followed redirect has redirected=true. + cloned.redirected = this.redirected; + cloned.type = this.type; + return cloned; + } + } + + Response.json = function (data, init) { + init = init || {}; + var headers = new Headers(init.headers); + if (!headers.has('content-type')) headers.set('content-type', 'application/json'); + return new Response(JSON.stringify(data), { + status: init.status, + statusText: init.statusText, + headers: headers, + }); + }; + + Response.error = function () { + return new Response(null, { status: 0 }); + }; + + module.exports = { Response: Response }; +}); diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index e5c833a..d4fe7cb 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -148,6 +148,10 @@ const ROUTES = [ re: globToRe('**/js/internal/fetch.js'), targets: ['make test-fetch-smoke'], }, + { + re: globToRe('**/js/internal/fetch/*.js'), + targets: ['make test-fetch-smoke'], + }, { re: globToRe('pkg/runtime/eventloop/**'), targets: ['make test-eventloop-odin', 'make test-eventloop-lava'], diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index d329d84..0970b3c 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -28,6 +28,16 @@ test('bench path routes bench-gate', () => { assert.ok(!r.nonCi.includes('make bench-gate')); }); +test('fetch internals route the fetch smoke, entry and split files alike', () => { + for (const p of [ + 'pkg/runtime/js/internal/fetch.js', + 'pkg/runtime/js/internal/fetch/headers.js', + 'pkg/runtime/js/internal/fetch/body.js', + ]) { + assert.ok(routePaths([p]).targets.includes('make test-fetch-smoke'), p); + } +}); + test('invalid **.js style is not required — **/* works', () => { const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 7e15798..6ce2798 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -188,7 +188,7 @@ { "name": "the Lava-only regexp pollution test dies when the header-name validator goes back to re.test", "why": "node ACCEPTS 'X-Evil: 1\\r\\nInjected' under this poison (undici's validator is JS), so this deviation cannot be oracled and CLAUDE.md section 1 requires a Lava-only pin.", - "source": "pkg/runtime/js/internal/fetch.js", + "source": "pkg/runtime/js/internal/fetch/headers.js", "find": " if (!reTest(VALID_HEADER_NAME, name)) {", "replace": " if (!VALID_HEADER_NAME.test(name)) {", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -197,7 +197,7 @@ { "name": "the Lava-only regexp pollution test dies when the header trim goes back to a global replaceAll", "why": "A global replace under a forged exec does not answer wrongly — it never returns, because lastIndex only advances on an empty match. The observable effect IS the hang, so the recorded reason is the timeout verdict: before capture() bounded it this entry ate 3m16s and multi-GB in CI and degraded to WRONG REASON on a memory-capped runner.", - "source": "pkg/runtime/js/internal/fetch.js", + "source": "pkg/runtime/js/internal/fetch/headers.js", "find": " while (start < end && isHttpWhitespace(StringPrototypeCharCodeAt(value, start))) start++;", "replace": " if (/^[\\r\\n\\t ]+|[\\r\\n\\t ]+$/g.test(value)) value = value.replace(/^[\\r\\n\\t ]+|[\\r\\n\\t ]+$/g, '');\n while (start < end && isHttpWhitespace(StringPrototypeCharCodeAt(value, start))) start++;", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -225,7 +225,7 @@ { "name": "the Lava-only test dies when the header-value scan reads charCodeAt live", "why": "Routing the validator off RegExp says nothing about what it scans WITH. A gadget lying only about CR/LF made Lava accept 'a\\r\\nInjected: yes' as a header value where node rejects, and turned res.setHeader('Location', decodeURIComponent(req.url)) into a split response from a remote request line.", - "source": "pkg/runtime/js/internal/fetch.js", + "source": "pkg/runtime/js/internal/fetch/headers.js", "find": " var code = StringPrototypeCharCodeAt(value, i);", "replace": " var code = value.charCodeAt(i);", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -2498,6 +2498,24 @@ "replace": " var writableStreamAbort = writable.writableStreamAbortGone;", "gate": "compat:tests/node-compat/cases/80-webstreams-internal-modules.js", "expect_detail": "node: abort stop" + }, + { + "name": "82-fetch-internal-modules dies when response.js builds its headers from a different Headers class", + "why": "Response and the Headers global now meet across a file seam; a response module holding its own Headers (a stale copy, a wrapper) still passes instanceof but hands user code headers whose constructor is not the global.", + "source": "pkg/runtime/js/internal/fetch/response.js", + "find": " var Headers = require('internal/fetch/headers').Headers;", + "replace": " var Headers = class Headers extends require('internal/fetch/headers').Headers {};", + "gate": "compat:tests/node-compat/cases/82-fetch-internal-modules.js", + "expect_detail": "node: identities true true true true true true true true true" + }, + { + "name": "82-fetch-internal-modules dies when the assembler installs a Response other than response.js's", + "why": "internal/fetch only assembles: the global it installs must be the class response.js constructs from, or clone()/json()/error() return instances of a class the global is not.", + "source": "pkg/runtime/js/internal/fetch.js", + "find": " var Response = require('internal/fetch/response').Response;", + "replace": " var Response = class Response extends require('internal/fetch/response').Response {};", + "gate": "compat:tests/node-compat/cases/82-fetch-internal-modules.js", + "expect_detail": "node: identities true true true true true true true true true" } ] } diff --git a/tests/node-compat/cases/82-fetch-internal-modules.js b/tests/node-compat/cases/82-fetch-internal-modules.js new file mode 100644 index 0000000..ef0a052 --- /dev/null +++ b/tests/node-compat/cases/82-fetch-internal-modules.js @@ -0,0 +1,166 @@ +// The fetch globals are assembled from internal modules laid out like undici's +// lib/web/fetch: js/internal/fetch/{headers,body,request,response}.js hold the classes, +// and internal/fetch keeps the driver (it owns the `native` transport binding) and +// installs the globals. A throw from Headers code used to carry a `node:internal/fetch:` +// frame, because everything was defined in internal/fetch.js; now it names its file. +// +// Node bundles undici into ONE file, so its frames all read +// `node:internal/deps/undici/undici` and there is no per-file name to oracle against. +// Each frame line therefore prints `ok` when the frame comes from the expected file in +// EITHER spelling (Lava's `node:internal/fetch/`, node's bundled undici) and +// names the frame it got otherwise, plus whether the old `node:internal/fetch:` frame is +// still there — false on node by construction, false on Lava after the split. Only the +// source name is compared, never the message, line or column (Lava's messages are not +// undici's; that is a separate gap). Every site throws synchronously on both runtimes +// (probed on node 26.13.0). +// +// The new ids must stay internal: like `internal/fetch` itself (74-builtin-visibility), +// neither the bare nor the `node:` spelling may resolve from user code. Only the error +// CODE of the bare spelling is printed (the messages diverge, #415). +// +// The behavior lines after that cross every file seam — Response and Request build +// their Headers and Body from the same modules the globals expose — so a move that +// loses a helper or re-wires an identity fails here and not only in a stack. +'use strict'; + +for (const id of [ + 'internal/fetch/headers', + 'internal/fetch/body', + 'internal/fetch/request', + 'internal/fetch/response', + 'fetch/headers', +]) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +function frame(label, file, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + const where = + stack.includes('node:internal/deps/undici/') || + stack.includes('node:internal/fetch/' + file + ':') + ? 'ok' + : 'got ' + (stack.match(/node:[\w/]+/) || ['no node: frame'])[0]; + console.log(label + ': ' + file + ' ' + where, 'old=' + stack.includes('node:internal/fetch:')); +} + +frame('Headers.append name', 'headers', () => new Headers().append('a b', 'v')); +frame('Headers.set value', 'headers', () => new Headers().set('a', 'x\r\ny')); +frame('Headers.get name', 'headers', () => new Headers().get('a b')); +frame('body toString throws', 'body', () => { + new Response({ + toString() { + throw new Error('x'); + }, + }); +}); +frame('Request credentials', 'request', () => new Request('http://u:p@x/')); +frame('Request GET body', 'request', () => new Request('http://x/', { body: 'a' })); +frame('Request redirect', 'request', () => new Request('http://x/', { redirect: 'nope' })); +frame( + 'Request duplex', + 'request', + () => new Request('http://x/', { method: 'POST', body: new ReadableStream() }), +); +frame('Response.json BigInt', 'response', () => Response.json(1n)); + +// --- the assembled globals: shapes and identities -------------------------------- +for (const k of ['Headers', 'Request', 'Response', 'fetch']) { + const d = Object.getOwnPropertyDescriptor(globalThis, k); + console.log('global', k, typeof d.value, d.value.name, d.writable, d.configurable); +} +console.log('statics', typeof Response.json, typeof Response.error); + +const res = new Response('hi', { status: 201, headers: [['X-A', '1']] }); +const req = new Request('http://example.com/a', { method: 'post', body: 'b' }); +console.log( + 'identities', + res instanceof Response, + res.headers instanceof Headers, + res.headers.constructor === Headers, + req instanceof Request, + req.headers.constructor === Headers, + // A body-less source: node marks a source Request's body used when a derived one + // takes it over, Lava shares it (a separate, pre-existing gap). + new Request(new Request('http://x/')).headers.constructor === Headers, + res.clone().constructor === Response, + Response.json({}).constructor === Response, + Response.error().constructor === Response, +); + +// --- behavior across the file seams ----------------------------------------------- +(async () => { + // Names inserted in sorted order with set-cookie last: node iterates sorted, Lava in + // insertion order with cookies at the end (a separate, pre-existing gap). + const h = new Headers({ 'Content-Type': 'text/plain' }); + h.set('P-Trim', ' \tv\r\n'); + h.append('Set-Cookie', 'a=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT'); + h.append('set-cookie', 'b=2'); + console.log('headers', [...h].map((e) => e.join('=')).join(' | ')); + console.log('getSetCookie', h.getSetCookie().length, h.get('set-cookie')); + + console.log('response', res.status, res.ok, res.headers.get('x-a'), await res.text()); + console.log('request', req.method, req.url, await req.text(), req.bodyUsed); + + const j = Response.json({ n: 1 }, { status: 202 }); + console.log('json', j.status, j.headers.get('content-type'), JSON.stringify(await j.json())); + console.log('error', Response.error().status); + + const original = new Response('cloned'); + const copy = original.clone(); + console.log('clone', await original.text(), await copy.text()); + + const bytes = await new Response(new Uint8Array([0xe2, 0x82, 0xac])).bytes(); + console.log('bytes', bytes.constructor.name, bytes.length); + const ab = await new Response('abc').arrayBuffer(); + console.log('arrayBuffer', ab.byteLength); + + const blobRes = new Response(new Blob(['x'], { type: 'text/x-lava' })); + console.log('blob type', blobRes.headers.get('content-type'), await blobRes.text()); + + const stream = new Response( + new ReadableStream({ + start(c) { + c.enqueue(new TextEncoder().encode('st')); + c.enqueue(new TextEncoder().encode('ream')); + c.close(); + }, + }), + ); + console.log('stream body', await stream.text(), stream.bodyUsed); + try { + await stream.text(); + console.log('second read', 'resolved'); + } catch (e) { + console.log('second read', e.name); + } + + const derived = new Request(new Request('http://x/p', { method: 'PUT', body: 'z' }), { + headers: { 'x-d': '1' }, + }); + console.log('derived', derived.method, derived.headers.get('x-d'), await derived.text()); + + try { + await fetch('http://u:p@x/'); + console.log('fetch credentials', 'resolved'); + } catch (e) { + console.log('fetch credentials', e.name); + } +})(); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index dc3eda0..bb6e62e 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -48,10 +48,34 @@ "global": 0 }, "internal/fetch.js": { - "method": 28, - "invoke": 1, + "method": 9, + "invoke": 0, + "accessor": 0, + "global": 23 + }, + "internal/fetch/body.js": { + "method": 7, + "invoke": 0, "accessor": 6, - "global": 75 + "global": 21 + }, + "internal/fetch/headers.js": { + "method": 11, + "invoke": 1, + "accessor": 0, + "global": 17 + }, + "internal/fetch/request.js": { + "method": 1, + "invoke": 0, + "accessor": 0, + "global": 12 + }, + "internal/fetch/response.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 2 }, "internal/file.js": { "method": 0, From a85ac6dcc9f213da4ecfc6cce3ef5f53a64d84bd Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:37:29 +0200 Subject: [PATCH 36/61] refactor(crypto): split node:crypto into internal/crypto/{util,hash,random,pbkdf2,hkdf,scrypt} node/crypto.js 927 -> 290 lines; exports, order and identities unchanged; one eager req('crypto') root. Natives reach the moved files through loader aliases (the http precedent). Case 83 pins the layout. Co-Authored-By: Claude Opus 5.5 --- agents/prompts/pr-gate-reference/gates.md | 2 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 15 +- pkg/runtime/globals.odin | 1 + pkg/runtime/js/bootstrap/loader.js | 8 + pkg/runtime/js/internal/crypto/hash.js | 105 +++ pkg/runtime/js/internal/crypto/hkdf.js | 36 + pkg/runtime/js/internal/crypto/pbkdf2.js | 46 ++ pkg/runtime/js/internal/crypto/random.js | 226 ++++++ pkg/runtime/js/internal/crypto/scrypt.js | 212 ++++++ pkg/runtime/js/internal/crypto/util.js | 156 ++++ pkg/runtime/js/node/crypto.js | 711 +----------------- runtime/gates/route-gates.mjs | 4 + runtime/gates/route-gates.test.mjs | 11 + tests/mutation-manifest.json | 18 + .../cases/83-crypto-internal-modules.js | 202 +++++ tests/node-compat/pollution-baseline.json | 42 +- 17 files changed, 1115 insertions(+), 682 deletions(-) create mode 100644 pkg/runtime/js/internal/crypto/hash.js create mode 100644 pkg/runtime/js/internal/crypto/hkdf.js create mode 100644 pkg/runtime/js/internal/crypto/pbkdf2.js create mode 100644 pkg/runtime/js/internal/crypto/random.js create mode 100644 pkg/runtime/js/internal/crypto/scrypt.js create mode 100644 pkg/runtime/js/internal/crypto/util.js create mode 100644 tests/node-compat/cases/83-crypto-internal-modules.js diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 2f90a62..36d674f 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -67,7 +67,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `js/node/fs.js` | `make test-fs-lava` — the JS layer owns every observable `node:fs` read semantic since #330 (Buffer re-tag, encoding decode, error taxonomy, path validation). It routes only to the generic `js/**.js` row otherwise, so a change there passes `check-js` and `test-compat-lava` while breaking every `run-fs-oracle.sh` case | | `js/node/stream.js` | `make test-http-smoke`, `make test-net-smoke`, `make test-stdio` — nothing else routes here, and this is the base layer `net`/`http`/`stdio` compose on. Added after #326: a change to the `write()` accept set passed `check-js` and `test-compat-lava` while stalling `pipe()` forever, because a DataView made `writableLength` NaN and `'drain'` could never fire. The suites that would have caught it were not on anyone's list | | `require.odin`, `module_resolution.odin`, `js/bootstrap/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | -| `crypto.odin`, `js/node/crypto.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | +| `crypto.odin`, `js/node/crypto.js`, `js/internal/crypto/*.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | | `pkg/runtime/js/**/*.js` (any under the scan root — `bootstrap/`, `node/`, `internal/`; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | | `js/node/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | | `js/internal/encoding.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 8e2c100..3ccadb0 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 6fbfb1a..cdad89f 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -124,7 +124,7 @@ Thin, per-platform `foreign` declarations of JSC's C API. Highlights: failure the same way — by setting `exception^`, never by its return value — with two audited exceptions recorded at the dispatcher comment in `host_natives.odin`: `net.write` returns a backpressure bool (not a failure - signal), and `crypto.randomFill` returns the view it filled, which `crypto.js` + signal), and `crypto.randomFill` returns the view it filled, which `internal/crypto/random.js` identity-checks as its second fail-closed layer. - The host-call path itself is reached by `dlsym`ing a C++ mangled symbol @@ -185,7 +185,8 @@ factory, before the table exists, and a top-level `require('buffer')` back would The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), Web Streams (~2100 across `internal/webstreams/`), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), fetch -(~1160 across `internal/fetch.js` and `internal/fetch/`), `crypto.js` (917). `node/stream.js` only assembles the public module; the classic +(~1160 across `internal/fetch.js` and `internal/fetch/`), crypto (~1070 across `node/crypto.js` +and `internal/crypto/`). `node/stream.js` only assembles the public module; the classic streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, `transform`, `passthrough`, `destroy`, `add-abort-signal`, `end-of-stream`, `pipeline`, `utils`) and the Web Streams bridges in `internal/webstreams/adapters.js`, the files node @@ -207,7 +208,15 @@ requires both, nothing requires back. Fetch follows undici's `lib/web/fetch` sea helpers), `request` and `response` (each requiring `headers` and `body`), while `internal/fetch` keeps the driver — body collection, redirects, the hand-off to `native.request` — because the loader keys natives by module id, and installs the globals. -All five are eager through `internal/fetch`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +All five are eager through `internal/fetch`. Crypto follows node's `lib/internal/crypto` +seams, so its frames read `node:internal/crypto/pbkdf2` exactly as node's do: +`internal/crypto/util` (validators, byte coercion, the digest table and `getHashes`), +`hash` (`Hash`, `Hmac`, one-shot `hash`), `random`, `pbkdf2`, `hkdf` and `scrypt`, each +requiring only `util`. `node/crypto` keeps `createHash`/`createHmac` and `timingSafeEqual` +where node's `lib/crypto.js` has them, assembles the exports in their old order and +installs `globalThis.crypto`; the five files that call into Odin get the `node/crypto` +bindings through the same `loader.js` aliasing as http. All seven are eager through the one +`req('crypto')` root. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 65c1315..9e36391 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1638,6 +1638,7 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"node/timers", #load_directory("js/node/timers")}, {"node/util", #load_directory("js/node/util")}, {"internal", #load_directory("js/internal")}, + {"internal/crypto", #load_directory("js/internal/crypto")}, {"internal/fetch", #load_directory("js/internal/fetch")}, {"internal/http", #load_directory("js/internal/http")}, {"internal/streams", #load_directory("js/internal/streams")}, diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index c12d4d6..773fba9 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -31,6 +31,14 @@ // the bindings before requiring either file. natives['internal/http/outgoing'] = natives['node/http']; natives['internal/http/server'] = natives['node/http']; + // node:crypto is split the same way, along node's lib/internal/crypto seams; the five + // files that call into Odin get node:crypto's own bindings object, aliased for the + // same reason. + natives['internal/crypto/hash'] = natives['node/crypto']; + natives['internal/crypto/random'] = natives['node/crypto']; + natives['internal/crypto/pbkdf2'] = natives['node/crypto']; + natives['internal/crypto/hkdf'] = natives['node/crypto']; + natives['internal/crypto/scrypt'] = natives['node/crypto']; // Prefix test by code unit. The specifier comes from user code on the public path, // so a String.prototype method here would be a live, replaceable read deciding diff --git a/pkg/runtime/js/internal/crypto/hash.js b/pkg/runtime/js/internal/crypto/hash.js new file mode 100644 index 0000000..ca7813e --- /dev/null +++ b/pkg/runtime/js/internal/crypto/hash.js @@ -0,0 +1,105 @@ +// Hash, Hmac and the one-shot crypto.hash() — where node keeps them: +// lib/internal/crypto/hash.js. Streaming is handled here by accumulating chunks and +// calling the one-shot native primitive once at digest time, so no streaming state has +// to cross the native boundary. +// +// @param {{ hash: Function, hmac: Function }} native node:crypto's bindings (the loader +// hands this file the same object as node/crypto.js). +(function (require, module, exports, native) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + var P = require('internal/primordials'); + var nodeError = require('internal/errors').nodeError; + var util = require('internal/crypto/util'); + var toU8 = util.toU8; + var resolveDigest = util.resolveDigest; + var unsupportedDigestError = util.unsupportedDigestError; + var invalidDigestError = util.invalidDigestError; + + function concat(chunks) { + if (chunks.length === 1) return chunks[0]; + var total = 0, + i; + for (i = 0; i < chunks.length; i++) total += chunks[i].length; + var out = new Uint8Array(total), + off = 0; + for (i = 0; i < chunks.length; i++) { + out.set(chunks[i], off); + off += chunks[i].length; + } + return out; + } + + function finalizedError() { + return nodeError(P.Error, 'ERR_CRYPTO_HASH_FINALIZED', 'Digest already called'); + } + + // Hash/Hmac accumulate chunks and call the one-shot native primitive at + // digest time (no streaming state crosses the boundary). Methods live on the + // prototype and the constructors are exported so the surface matches Node, + // where createHash()/createHmac() return Hash/Hmac instances. + + function Hash(algorithm) { + if (!(this instanceof Hash)) return new Hash(algorithm); + var algo = resolveDigest(algorithm); + if (!algo) throw unsupportedDigestError(); + this._algo = algo; + this._chunks = []; + this._finalized = false; + } + Hash.prototype.update = function (data, encoding) { + if (this._finalized) throw finalizedError(); + this._chunks.push(toU8(data, encoding)); + return this; + }; + Hash.prototype.digest = function (encoding) { + if (this._finalized) throw finalizedError(); + this._finalized = true; + var out = Buffer.from(native.hash(this._algo, concat(this._chunks))); + return encoding ? out.toString(encoding) : out; + }; + Hash.prototype.copy = function () { + if (this._finalized) throw finalizedError(); + var clone = new Hash(this._algo); + clone._chunks = this._chunks.slice(); + return clone; + }; + + function Hmac(algorithm, key) { + if (!(this instanceof Hmac)) return new Hmac(algorithm, key); + var algo = resolveDigest(algorithm); + if (!algo) throw invalidDigestError(algorithm); + this._algo = algo; + this._key = toU8(key, undefined, 'key'); + this._chunks = []; + this._finalized = false; + } + Hmac.prototype.update = function (data, encoding) { + if (this._finalized) throw finalizedError(); + this._chunks.push(toU8(data, encoding)); + return this; + }; + Hmac.prototype.digest = function (encoding) { + if (this._finalized) { + var empty = Buffer.alloc(0); + return encoding ? empty.toString(encoding) : empty; + } + this._finalized = true; + var out = Buffer.from(native.hmac(this._algo, this._key, concat(this._chunks))); + return encoding ? out.toString(encoding) : out; + }; + + // crypto.hash(algorithm, data[, outputEncoding]) — one-shot digest (Node 21+). + // outputEncoding defaults to "hex"; "buffer" yields a Buffer. + function hash(algorithm, data, outputEncoding) { + var algo = resolveDigest(algorithm); + if (!algo) throw unsupportedDigestError(algorithm); + var out = Buffer.from(native.hash(algo, toU8(data))); + if (outputEncoding === undefined || outputEncoding === 'hex') return out.toString('hex'); + if (outputEncoding === 'buffer') return out; + return out.toString(outputEncoding); + } + + module.exports = { Hash: Hash, Hmac: Hmac, hash: hash }; +}); diff --git a/pkg/runtime/js/internal/crypto/hkdf.js b/pkg/runtime/js/internal/crypto/hkdf.js new file mode 100644 index 0000000..a61fd2c --- /dev/null +++ b/pkg/runtime/js/internal/crypto/hkdf.js @@ -0,0 +1,36 @@ +// hkdf/hkdfSync — where node keeps them: lib/internal/crypto/hkdf.js. +// +// @param {{ hkdf: Function }} native node:crypto's bindings (the loader hands this file +// the same object as node/crypto.js). +(function (require, module, exports, native) { + 'use strict'; + + var util = require('internal/crypto/util'); + var toU8 = util.toU8; + var resolveDigest = util.resolveDigest; + var invalidDigestError = util.invalidDigestError; + + // hkdfSync(digest, ikm, salt, info, keylen) — RFC 5869 HKDF over the native + // crypto primitive. Returns an ArrayBuffer, matching Node. + function hkdfSync(digest, ikm, salt, info, keylen) { + var algo = resolveDigest(digest); + if (!algo) throw invalidDigestError(digest); + return native.hkdf(algo, toU8(ikm), toU8(salt), toU8(info), keylen).buffer; + } + + function hkdf(digest, ikm, salt, info, keylen, callback) { + if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); + setImmediate(function () { + var key; + try { + key = hkdfSync(digest, ikm, salt, info, keylen); + } catch (err) { + callback(err); + return; + } + callback(null, key); + }); + } + + module.exports = { hkdf: hkdf, hkdfSync: hkdfSync }; +}); diff --git a/pkg/runtime/js/internal/crypto/pbkdf2.js b/pkg/runtime/js/internal/crypto/pbkdf2.js new file mode 100644 index 0000000..791287f --- /dev/null +++ b/pkg/runtime/js/internal/crypto/pbkdf2.js @@ -0,0 +1,46 @@ +// pbkdf2/pbkdf2Sync — where node keeps them: lib/internal/crypto/pbkdf2.js. +// +// @param {{ pbkdf2: Function }} native node:crypto's bindings (the loader hands this +// file the same object as node/crypto.js). +(function (require, module, exports, native) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + var util = require('internal/crypto/util'); + var invalidArgType = util.invalidArgType; + var validateInt32 = util.validateInt32; + var toU8 = util.toU8; + var resolveDigest = util.resolveDigest; + var invalidDigestError = util.invalidDigestError; + + function pbkdf2Sync(password, salt, iterations, keylen, digest) { + if (typeof digest !== 'string') throw invalidArgType('digest', 'string', digest); + var algo = resolveDigest(digest); + if (!algo) throw invalidDigestError(digest); + var pw = toU8(password, undefined, 'password'); + var slt = toU8(salt, undefined, 'salt'); + // Node validates iterations as 1..2^31-1 and keylen as 0..2^31-1. The old + // `iterations >>> 0` turned -1 into 4294967295 (a permanent sync hang) and + // wrapped values >= 2^32 to a different derived key. + validateInt32(iterations, 'iterations', 1); + validateInt32(keylen, 'keylen', 0); + var out = native.pbkdf2(algo, pw, slt, iterations, keylen); + return Buffer.from(out); + } + + function pbkdf2(password, salt, iterations, keylen, digest, callback) { + if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); + setImmediate(function () { + var key; + try { + key = pbkdf2Sync(password, salt, iterations, keylen, digest); + } catch (err) { + callback(err); + return; + } + callback(null, key); + }); + } + + module.exports = { pbkdf2: pbkdf2, pbkdf2Sync: pbkdf2Sync }; +}); diff --git a/pkg/runtime/js/internal/crypto/random.js b/pkg/runtime/js/internal/crypto/random.js new file mode 100644 index 0000000..8a120ae --- /dev/null +++ b/pkg/runtime/js/internal/crypto/random.js @@ -0,0 +1,226 @@ +// The CSPRNG surface — randomBytes, randomFill/randomFillSync, randomInt, randomUUID and +// getRandomValues — where node keeps it: lib/internal/crypto/random.js. +// +// @param {{ randomFill: Function, randomInt: Function }} native node:crypto's bindings +// (the loader hands this file the same object as node/crypto.js). +(function (require, module, exports, native) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + var util = require('internal/crypto/util'); + var invalidArgType = util.invalidArgType; + var outOfRange = util.outOfRange; + var validateByteCount = util.validateByteCount; + + // Every CSPRNG entry point goes through this, and none calls native.randomFill + // directly. The native returns the view it filled, so an identity check is the + // available proof that the fill ran. Second layer, not the first: the + // dispatcher already fails closed against the miss-forges-entropy scenario + // (the canonical write-up is at host_dispatch_fail, pkg/runtime/ + // host_natives.odin), but that layer is one registration decision away from + // not covering this native, and a wrong answer here is silent forged entropy. + // The error is deliberately not a Node ERR_* code: Node has no counterpart, + // because in Node this cannot happen. + function fillRandom(view) { + if (native.randomFill(view) !== view) + throw new Error('lava: CSPRNG unavailable (native.randomFill did not fill the buffer)'); + return view; + } + + function randomBytes(size, callback) { + // Node validates size as 0..2^31-1 (NaN rejected, fractional truncated); a + // negative/huge value used to wrap into a multi-gigabyte allocation instead + // of a catchable RangeError. + size = validateByteCount(size, 'size'); + var buf = Buffer.alloc(size); + if (size > 0) fillRandom(buf); + if (typeof callback === 'function') { + // Node completes a zero-size request synchronously (it queues no work for + // zero bytes); only real fills report their completion asynchronously. + if (size === 0) { + callback(null, buf); + return; + } + setImmediate(function () { + callback(null, buf); + }); + return; + } + return buf; + } + + // assertOffset/assertSize mirror Node: offset and size are counted in + // elements, converted to bytes against the view, and validated. An oversized + // size now throws ERR_OUT_OF_RANGE instead of silently filling fewer bytes. + function assertOffset(offset, elementSize, byteLength) { + if (typeof offset !== 'number') throw invalidArgType('offset', 'number', offset); + var bytes = offset * elementSize; + if (Number.isNaN(bytes) || bytes < 0 || bytes > byteLength) + throw outOfRange('offset', '>= 0 && <= ' + byteLength / elementSize, offset); + return bytes >>> 0; + } + function assertSize(size, elementSize, offsetBytes, byteLength) { + if (typeof size !== 'number') throw invalidArgType('size', 'number', size); + var bytes = size * elementSize; + if (Number.isNaN(bytes) || bytes < 0 || bytes > 0x7fffffff) + throw outOfRange('size', '>= 0 && <= ' + 0x7fffffff, size); + if (bytes + offsetBytes > byteLength) + throw outOfRange('size + offset', '<= ' + byteLength, bytes + offsetBytes); + return bytes >>> 0; + } + + // Shared buf/offset/size validation for randomFillSync and randomFill, + // resolving to the byte range to fill. Split out so randomFill can validate + // SYNCHRONOUSLY the way Node does — only the fill itself is deferred. + function resolveFillRange(buffer, offset, size) { + var ab, base, byteLength, elementSize; + if (ArrayBuffer.isView(buffer)) { + ab = buffer.buffer; + base = buffer.byteOffset; + byteLength = buffer.byteLength; + elementSize = buffer.BYTES_PER_ELEMENT || 1; + } else if ( + buffer instanceof ArrayBuffer || + (typeof SharedArrayBuffer !== 'undefined' && buffer instanceof SharedArrayBuffer) + ) { + ab = buffer; + base = 0; + byteLength = buffer.byteLength; + elementSize = 1; + } else { + throw invalidArgType('buf', ['ArrayBuffer', 'ArrayBufferView'], buffer); + } + var offsetBytes = assertOffset(offset === undefined ? 0 : offset, elementSize, byteLength); + var sizeBytes = + size === undefined + ? byteLength - offsetBytes + : assertSize(size, elementSize, offsetBytes, byteLength); + return { base: base + offsetBytes, ab: ab, sizeBytes: sizeBytes }; + } + + function randomFillSync(buffer, offset, size) { + var r = resolveFillRange(buffer, offset, size); + if (r.sizeBytes > 0) fillRandom(new Uint8Array(r.ab, r.base, r.sizeBytes)); + return buffer; + } + + function randomFill(buffer, offset, size, callback) { + if (typeof offset === 'function') { + callback = offset; + offset = undefined; + size = undefined; + } else if (typeof size === 'function') { + callback = size; + size = undefined; + } + if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); + // Validate now — Node throws a bad buf/offset/size from randomFill itself + // (deferring it turned a synchronous TypeError into an uncaught async one) + // and completes a zero-byte request synchronously, queueing no work for it. + var r = resolveFillRange(buffer, offset, size); + if (r.sizeBytes === 0) { + callback(null, buffer); + return; + } + setImmediate(function () { + fillRandom(new Uint8Array(r.ab, r.base, r.sizeBytes)); + callback(null, buffer); + }); + } + + var HEX = '0123456789abcdef'; + + function randomUUID() { + var b = randomBytes(16); + b[6] = (b[6] & 0x0f) | 0x40; + b[8] = (b[8] & 0x3f) | 0x80; + var s = ''; + for (var i = 0; i < 16; i++) { + s += HEX[(b[i] >> 4) & 0xf] + HEX[b[i] & 0xf]; + if (i === 3 || i === 5 || i === 7 || i === 9) s += '-'; + } + return s; + } + + // WHATWG crypto.getRandomValues: fill an integer TypedArray in place with + // CSPRNG bytes and return it. Float views and DataView are rejected; the spec + // caps a single call at 65536 bytes. + function getRandomValues(typedArray) { + if ( + typedArray == null || + typeof typedArray.byteLength !== 'number' || + !(typedArray.buffer instanceof ArrayBuffer) || + typedArray instanceof Float32Array || + typedArray instanceof Float64Array || + (typeof DataView !== 'undefined' && typedArray instanceof DataView) + ) { + throw new TypeError('The data argument must be an integer-type TypedArray'); + } + if (typedArray.byteLength > 65536) { + throw new RangeError( + "The ArrayBufferView's byte length (" + + typedArray.byteLength + + ') exceeds the number of bytes of entropy available via this API (65536)', + ); + } + if (typedArray.byteLength > 0) { + fillRandom(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength)); + } + return typedArray; + } + + // randomInt([min, ] max[, callback]) — uniform integer in [min, max). + // Node caps the range at 2^48; native.randomInt handles rejection sampling. + function randomInt(min, max, callback) { + if (typeof min === 'function') { + callback = min; + min = undefined; + max = undefined; + } else if (typeof max === 'function') { + callback = max; + max = undefined; + } + if (max === undefined) { + max = min; + min = 0; + } + if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) { + throw new RangeError('The value of "min"/"max" is out of range. It must be a safe integer.'); + } + if (max <= min) + throw new RangeError( + 'The value of "max" is out of range. It must be greater than the value of "min".', + ); + var range = max - min; + if (range > 0x1000000000000) + throw new RangeError('The value of "max - min" is out of range. It must be <= 2^48.'); + + function sample() { + return min + native.randomInt(range); + } + + if (typeof callback === 'function') { + setImmediate(function () { + var value; + try { + value = sample(); + } catch (err) { + callback(err); + return; + } + callback(null, value); + }); + return; + } + return sample(); + } + + module.exports = { + randomBytes: randomBytes, + randomFillSync: randomFillSync, + randomFill: randomFill, + randomInt: randomInt, + randomUUID: randomUUID, + getRandomValues: getRandomValues, + }; +}); diff --git a/pkg/runtime/js/internal/crypto/scrypt.js b/pkg/runtime/js/internal/crypto/scrypt.js new file mode 100644 index 0000000..473c722 --- /dev/null +++ b/pkg/runtime/js/internal/crypto/scrypt.js @@ -0,0 +1,212 @@ +// scrypt/scryptSync — where node keeps them: lib/internal/crypto/scrypt.js. +// +// @param {{ pbkdf2: Function }} native node:crypto's bindings (the loader hands this +// file the same object as node/crypto.js); scrypt needs only PBKDF2-HMAC-SHA256. +(function (require, module, exports, native) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + var P = require('internal/primordials'); + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var nativeError = errors.nativeError; + var util = require('internal/crypto/util'); + var outOfRange = util.outOfRange; + var toU8 = util.toU8; + + // scrypt (RFC 7914) — layered entirely on top of the native PBKDF2-HMAC-SHA256 + // primitive plus Salsa20/8 in JS, so it needs no extra native code and is + // identical across platforms. Output is byte-for-byte equal to Node/OpenSSL. + // The Salsa core and BlockMix/ROMix operate on little-endian u32 words; bytes + // are converted explicitly so the result is correct regardless of host + // endianness. + + function scryptSalsa20_8(B) { + var x = new Uint32Array(16), + i; + for (i = 0; i < 16; i++) x[i] = B[i]; + function R(a, b) { + return (a << b) | (a >>> (32 - b)); + } + for (i = 0; i < 4; i++) { + x[4] ^= R(x[0] + x[12], 7); + x[8] ^= R(x[4] + x[0], 9); + x[12] ^= R(x[8] + x[4], 13); + x[0] ^= R(x[12] + x[8], 18); + x[9] ^= R(x[5] + x[1], 7); + x[13] ^= R(x[9] + x[5], 9); + x[1] ^= R(x[13] + x[9], 13); + x[5] ^= R(x[1] + x[13], 18); + x[14] ^= R(x[10] + x[6], 7); + x[2] ^= R(x[14] + x[10], 9); + x[6] ^= R(x[2] + x[14], 13); + x[10] ^= R(x[6] + x[2], 18); + x[3] ^= R(x[15] + x[11], 7); + x[7] ^= R(x[3] + x[15], 9); + x[11] ^= R(x[7] + x[3], 13); + x[15] ^= R(x[11] + x[7], 18); + x[1] ^= R(x[0] + x[3], 7); + x[2] ^= R(x[1] + x[0], 9); + x[3] ^= R(x[2] + x[1], 13); + x[0] ^= R(x[3] + x[2], 18); + x[6] ^= R(x[5] + x[4], 7); + x[7] ^= R(x[6] + x[5], 9); + x[4] ^= R(x[7] + x[6], 13); + x[5] ^= R(x[4] + x[7], 18); + x[11] ^= R(x[10] + x[9], 7); + x[8] ^= R(x[11] + x[10], 9); + x[9] ^= R(x[8] + x[11], 13); + x[10] ^= R(x[9] + x[8], 18); + x[12] ^= R(x[15] + x[14], 7); + x[13] ^= R(x[12] + x[15], 9); + x[14] ^= R(x[13] + x[12], 13); + x[15] ^= R(x[14] + x[13], 18); + } + for (i = 0; i < 16; i++) B[i] = (B[i] + x[i]) >>> 0; + } + + function scryptBlockMix(B, Y, r) { + var X = new Uint32Array(16), + i, + j, + last = (2 * r - 1) * 16; + for (i = 0; i < 16; i++) X[i] = B[last + i]; + for (i = 0; i < 2 * r; i++) { + var off = i * 16; + for (j = 0; j < 16; j++) X[j] ^= B[off + j]; + scryptSalsa20_8(X); + var dest = (i % 2 === 0 ? i / 2 : r + (i - 1) / 2) * 16; + for (j = 0; j < 16; j++) Y[dest + j] = X[j]; + } + } + + function scryptROMix(B, N, r) { + var len = 32 * r, + V = new Uint32Array(len * N), + tmp = new Uint32Array(len), + i, + k; + for (i = 0; i < N; i++) { + V.set(B, i * len); + scryptBlockMix(B, tmp, r); + B.set(tmp); + } + for (i = 0; i < N; i++) { + var j = B[(2 * r - 1) * 16] & (N - 1), + off = j * len; + for (k = 0; k < len; k++) B[k] ^= V[off + k]; + scryptBlockMix(B, tmp, r); + B.set(tmp); + } + } + + function scryptBytesToWordsLE(bytes, off, words, wlen) { + for (var i = 0; i < wlen; i++) { + var k = off + i * 4; + words[i] = + (bytes[k] | (bytes[k + 1] << 8) | (bytes[k + 2] << 16) | (bytes[k + 3] << 24)) >>> 0; + } + } + function scryptWordsToBytesLE(words, wlen, bytes, off) { + for (var i = 0; i < wlen; i++) { + var v = words[i], + k = off + i * 4; + bytes[k] = v & 0xff; + bytes[k + 1] = (v >>> 8) & 0xff; + bytes[k + 2] = (v >>> 16) & 0xff; + bytes[k + 3] = (v >>> 24) & 0xff; + } + } + + function isPowerOfTwo(n) { + return n > 1 && (n & (n - 1)) === 0; + } + + // Resolve N/r/p with Node's option-and-alias handling. cost/blockSize/ + // parallelization are aliases for N/r/p; supplying both forms of one parameter + // throws ERR_INCOMPATIBLE_OPTION_PAIR, exactly as Node does. + function scryptParams(options) { + var opts = options || {}; + function pick(primary, alias, dflt) { + var hasP = opts[primary] !== undefined; + var hasA = opts[alias] !== undefined; + if (hasP && hasA) { + throw nodeError( + P.TypeError, + 'ERR_INCOMPATIBLE_OPTION_PAIR', + 'Option "' + primary + '" cannot be used in combination with option "' + alias + '"', + ); + } + if (hasP) return opts[primary]; + if (hasA) return opts[alias]; + return dflt; + } + var N = pick('N', 'cost', 16384); + var r = pick('r', 'blockSize', 8); + var p = pick('p', 'parallelization', 1); + var maxmem = opts.maxmem === undefined ? 32 * 1024 * 1024 : opts.maxmem; + return { N: N, r: r, p: p, maxmem: maxmem }; + } + + function scryptParamsError(message) { + return nativeError( + P.RangeError, + 'ERR_CRYPTO_INVALID_SCRYPT_PARAMS', + message || 'Invalid scrypt params', + ); + } + + function scryptDerive(password, salt, keylen, options) { + if (!Number.isInteger(keylen) || keylen < 0 || keylen > 0x7fffffff) { + throw outOfRange('keylen', '>= 0 && <= 2147483647', keylen); + } + var pr = scryptParams(options); + var N = pr.N, + r = pr.r, + p = pr.p; + // N must be a power of two greater than 1; r and p must be positive ints. + if (!Number.isInteger(N) || !isPowerOfTwo(N)) throw scryptParamsError(); + if (!Number.isInteger(r) || r < 1 || !Number.isInteger(p) || p < 1) throw scryptParamsError(); + // Memory bound, matching Node's documented ~128*N*r ceiling against maxmem. + if (128 * N * r > pr.maxmem) throw scryptParamsError(); + + var pw = toU8(password), + slt = toU8(salt); + var mfLen = 128 * r; + var B = native.pbkdf2('sha256', pw, slt, 1, p * mfLen); + var words = new Uint32Array(32 * r); + for (var i = 0; i < p; i++) { + scryptBytesToWordsLE(B, i * mfLen, words, 32 * r); + scryptROMix(words, N, r); + scryptWordsToBytesLE(words, 32 * r, B, i * mfLen); + } + // keylen === 0 yields an empty key without touching the native PBKDF2, which + // (like OpenSSL) rejects a zero output length. + if (keylen === 0) return Buffer.alloc(0); + return Buffer.from(native.pbkdf2('sha256', pw, B, 1, keylen)); + } + + function scryptSync(password, salt, keylen, options) { + return scryptDerive(password, salt, keylen, options); + } + + function scrypt(password, salt, keylen, options, callback) { + if (typeof options === 'function') { + callback = options; + options = undefined; + } + if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); + setImmediate(function () { + var key; + try { + key = scryptDerive(password, salt, keylen, options); + } catch (err) { + callback(err); + return; + } + callback(null, key); + }); + } + + module.exports = { scrypt: scrypt, scryptSync: scryptSync }; +}); diff --git a/pkg/runtime/js/internal/crypto/util.js b/pkg/runtime/js/internal/crypto/util.js new file mode 100644 index 0000000..b33a505 --- /dev/null +++ b/pkg/runtime/js/internal/crypto/util.js @@ -0,0 +1,156 @@ +// Shared node:crypto helpers — argument validators, byte-source coercion and the digest +// name table — where node keeps them: lib/internal/crypto/util.js (getHashes, the +// digest lookup) and lib/internal/validators.js. Every other file under +// internal/crypto/ requires this one; it requires none of them back. +(function (require, module) { + 'use strict'; + + var Buffer = require('buffer').Buffer; + + // Argument-validation helpers mirroring Node's internal validators, so bad + // input fails with Node's error class + code instead of being silently + // coerced (a zero-filled buffer, a wrapped iteration count, a clamped range). + // The errors themselves come from internal/errors (CLAUDE.md §5). + var P = require('internal/primordials'); + var errors = require('internal/errors'); + var nativeError = errors.nativeError; + var invalidArgType = errors.ERR_INVALID_ARG_TYPE; + var outOfRange = errors.ERR_OUT_OF_RANGE; + // Node's validateInt32: number, integer, within [min, max] (default int32 range). + function validateInt32(value, name, min, max) { + if (min === undefined) min = -0x80000000; + if (max === undefined) max = 0x7fffffff; + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); + if (!Number.isInteger(value)) throw outOfRange(name, 'an integer', value); + if (value < min || value > max) throw outOfRange(name, '>= ' + min + ' && <= ' + max, value); + return value; + } + // A byte count (randomBytes size): must be a number in [0, 2^31-1] and not + // NaN, but — matching Node — a fractional value is truncated, not rejected. + function validateByteCount(value, name) { + if (typeof value !== 'number') throw invalidArgType(name, 'number', value); + if (Number.isNaN(value) || value < 0 || value > 0x7fffffff) + throw outOfRange(name, '>= 0 && <= ' + 0x7fffffff, value); + return Math.trunc(value); + } + + // toU8 accepts the byte sources Node accepts (string + Buffer/TypedArray/ + // DataView/ArrayBuffer) and throws ERR_INVALID_ARG_TYPE for anything else — + // numbers used to be silently turned into a zero-filled buffer. + function toU8(data, encoding, name) { + if (typeof data === 'string') return new Uint8Array(Buffer.from(data, encoding || 'utf8')); + if (ArrayBuffer.isView(data)) + return new Uint8Array(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)); + if (data instanceof ArrayBuffer) return new Uint8Array(data.slice(0)); + if (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer) + return new Uint8Array(new Uint8Array(data)); + throw invalidArgType(name || 'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data); + } + + // Canonical digest names — one per Odin core:crypto/hash primitive. These are + // the only names the native layer (pkg/runtime/crypto.odin) understands; every + // accepted alias below is normalized to one of these before it crosses the + // boundary, so the Odin switch stays minimal. + var HASHES = [ + 'md5', + 'sha1', + 'sha224', + 'sha256', + 'sha384', + 'sha512', + 'sha512-256', + 'sha3-224', + 'sha3-256', + 'sha3-384', + 'sha3-512', + 'blake2b512', + 'blake2s256', + 'sm3', + ]; + + // Node/OpenSSL digest aliases whose underlying primitive Lava supports. Each + // maps a canonical name to the OpenSSL signature/legacy spellings that resolve + // to the same digest (createHash('RSA-SHA256') === createHash('sha256')). The + // display names keep Node's exact casing for getHashes(); resolveDigest() + // matches case-insensitively. Aliases for digests Lava lacks (RSA-RIPEMD160, + // RSA-SHA512/224, …) are intentionally absent — see UNSUPPORTED_NODE_DIGESTS. + var HASH_ALIAS_GROUPS = { + md5: ['RSA-MD5', 'md5WithRSAEncryption', 'ssl3-md5'], + sha1: ['RSA-SHA1', 'RSA-SHA1-2', 'sha1WithRSAEncryption', 'ssl3-sha1'], + sha224: ['RSA-SHA224', 'sha224WithRSAEncryption'], + sha256: ['RSA-SHA256', 'sha256WithRSAEncryption'], + sha384: ['RSA-SHA384', 'sha384WithRSAEncryption'], + sha512: ['RSA-SHA512', 'sha512WithRSAEncryption'], + 'sha512-256': ['RSA-SHA512/256', 'sha512-256WithRSAEncryption'], + 'sha3-224': ['RSA-SHA3-224', 'id-rsassa-pkcs1-v1_5-with-sha3-224'], + 'sha3-256': ['RSA-SHA3-256', 'id-rsassa-pkcs1-v1_5-with-sha3-256'], + 'sha3-384': ['RSA-SHA3-384', 'id-rsassa-pkcs1-v1_5-with-sha3-384'], + 'sha3-512': ['RSA-SHA3-512', 'id-rsassa-pkcs1-v1_5-with-sha3-512'], + sm3: ['RSA-SM3', 'sm3WithRSAEncryption'], + }; + + // Names Node/OpenSSL accepts that Lava intentionally rejects, because Odin's + // core:crypto/hash enum has no matching primitive. Listed for documentation + // only; resolveDigest() returns null for anything not canonical-or-aliased, so + // these surface the same "unsupported digest" errors as a bogus name. SHAKE is + // an XOF (variable-length) and would need a distinct API; RIPEMD-160, + // SHA-512/224 and md5-sha1 have no Odin primitive. + // var UNSUPPORTED_NODE_DIGESTS = [ + // 'ripemd160', 'rmd160', 'RSA-RIPEMD160', 'ripemd160WithRSA', + // 'sha512-224', 'RSA-SHA512/224', 'sha512-224WithRSAEncryption', + // 'md5-sha1', 'shake128', 'shake256', + // ]; + + // ALIAS_TO_CANONICAL: lowercased alias -> canonical name. CANONICAL_SET: the + // canonical names as a lookup. Both built once at load. + var CANONICAL_SET = {}; + HASHES.forEach(function (name) { + CANONICAL_SET[name] = true; + }); + var ALIAS_TO_CANONICAL = {}; + var ALIAS_DISPLAY_NAMES = []; + Object.keys(HASH_ALIAS_GROUPS).forEach(function (canonical) { + HASH_ALIAS_GROUPS[canonical].forEach(function (alias) { + ALIAS_TO_CANONICAL[alias.toLowerCase()] = canonical; + ALIAS_DISPLAY_NAMES.push(alias); + }); + }); + + // resolveDigest maps any accepted spelling to its canonical name, or null if + // Lava does not support it. Case-insensitive, matching OpenSSL/Node. + function resolveDigest(name) { + var lc = String(name).toLowerCase(); + if (CANONICAL_SET[lc]) return lc; + return ALIAS_TO_CANONICAL[lc] || null; + } + + // Two error shapes, matching Node exactly: createHash()/hash() throw a plain + // Error with no code ("Digest method not supported"), while the keyed digests + // (createHmac/pbkdf2/hkdf) throw TypeError ERR_CRYPTO_INVALID_DIGEST. + function unsupportedDigestError(name) { + return new Error( + name === undefined + ? 'Digest method not supported' + : 'Digest method ' + name + ' is not supported', + ); + } + function invalidDigestError(name) { + return nativeError(P.TypeError, 'ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: ' + name); + } + + function getHashes() { + return HASHES.concat(ALIAS_DISPLAY_NAMES); + } + + module.exports = { + invalidArgType: invalidArgType, + outOfRange: outOfRange, + validateInt32: validateInt32, + validateByteCount: validateByteCount, + toU8: toU8, + resolveDigest: resolveDigest, + unsupportedDigestError: unsupportedDigestError, + invalidDigestError: invalidDigestError, + getHashes: getHashes, + }; +}); diff --git a/pkg/runtime/js/node/crypto.js b/pkg/runtime/js/node/crypto.js index 85a7b83..ecd6dc5 100644 --- a/pkg/runtime/js/node/crypto.js +++ b/pkg/runtime/js/node/crypto.js @@ -10,174 +10,51 @@ // native.hkdf(algo, ikm, salt, info, n) -> derived-key Uint8Array // native.timingSafeEqual(a, b) -> constant-time boolean // -// Streaming (createHash().update()…digest()) is handled here by accumulating -// chunks and calling the one-shot native primitive once at digest time, so no -// streaming state has to cross the native boundary. +// The implementation is split along node's lib/internal/crypto seams: +// internal/crypto/{util,hash,random,pbkdf2,hkdf,scrypt}.js. This file stays the public +// entry: it assembles the exports in their old order, keeps createHash/createHmac and +// timingSafeEqual where node keeps them (lib/crypto.js), and installs the WHATWG +// `globalThis.crypto`. The loader hands hash/random/pbkdf2/hkdf/scrypt this same +// `native` object (see bootstrap/loader.js), so there is one binding set per context. +// +// Streaming (createHash().update()…digest()) is handled in internal/crypto/hash by +// accumulating chunks and calling the one-shot native primitive once at digest time, so +// no streaming state has to cross the native boundary. // // Higher-level KDFs that decompose into the primitives above are built in JS: -// scrypt (RFC 7914) runs Salsa20/8 + BlockMix/ROMix here on top of +// scrypt (RFC 7914, internal/crypto/scrypt) runs Salsa20/8 + BlockMix/ROMix on top of // native.pbkdf2 (PBKDF2-HMAC-SHA256), so it needs no extra native code and is // identical on every platform. OpenSSL/Node digest aliases (e.g. RSA-SHA256 -> -// sha256) are normalized to canonical names here too, so the native layer only -// ever sees the names in HASHES. +// sha256) are normalized to canonical names in internal/crypto/util, so the native +// layer only ever sees the names in HASHES. (function (require, module, exports, native) { 'use strict'; - var Buffer = require('buffer').Buffer; - - // Argument-validation helpers mirroring Node's internal validators, so bad - // input fails with Node's error class + code instead of being silently - // coerced (a zero-filled buffer, a wrapped iteration count, a clamped range). - // The errors themselves come from internal/errors (CLAUDE.md §5). var P = require('internal/primordials'); - var errors = require('internal/errors'); - var nodeError = errors.nodeError; - var nativeError = errors.nativeError; - var invalidArgType = errors.ERR_INVALID_ARG_TYPE; - var outOfRange = errors.ERR_OUT_OF_RANGE; - // Node's validateInt32: number, integer, within [min, max] (default int32 range). - function validateInt32(value, name, min, max) { - if (min === undefined) min = -0x80000000; - if (max === undefined) max = 0x7fffffff; - if (typeof value !== 'number') throw invalidArgType(name, 'number', value); - if (!Number.isInteger(value)) throw outOfRange(name, 'an integer', value); - if (value < min || value > max) throw outOfRange(name, '>= ' + min + ' && <= ' + max, value); - return value; - } - // A byte count (randomBytes size): must be a number in [0, 2^31-1] and not - // NaN, but — matching Node — a fractional value is truncated, not rejected. - function validateByteCount(value, name) { - if (typeof value !== 'number') throw invalidArgType(name, 'number', value); - if (Number.isNaN(value) || value < 0 || value > 0x7fffffff) - throw outOfRange(name, '>= 0 && <= ' + 0x7fffffff, value); - return Math.trunc(value); - } - - // toU8 accepts the byte sources Node accepts (string + Buffer/TypedArray/ - // DataView/ArrayBuffer) and throws ERR_INVALID_ARG_TYPE for anything else — - // numbers used to be silently turned into a zero-filled buffer. - function toU8(data, encoding, name) { - if (typeof data === 'string') return new Uint8Array(Buffer.from(data, encoding || 'utf8')); - if (ArrayBuffer.isView(data)) - return new Uint8Array(data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength)); - if (data instanceof ArrayBuffer) return new Uint8Array(data.slice(0)); - if (typeof SharedArrayBuffer !== 'undefined' && data instanceof SharedArrayBuffer) - return new Uint8Array(new Uint8Array(data)); - throw invalidArgType(name || 'data', ['string', 'Buffer', 'TypedArray', 'DataView'], data); - } - - function concat(chunks) { - if (chunks.length === 1) return chunks[0]; - var total = 0, - i; - for (i = 0; i < chunks.length; i++) total += chunks[i].length; - var out = new Uint8Array(total), - off = 0; - for (i = 0; i < chunks.length; i++) { - out.set(chunks[i], off); - off += chunks[i].length; - } - return out; - } - - function finalizedError() { - return nodeError(P.Error, 'ERR_CRYPTO_HASH_FINALIZED', 'Digest already called'); - } - - // Canonical digest names — one per Odin core:crypto/hash primitive. These are - // the only names the native layer (pkg/runtime/crypto.odin) understands; every - // accepted alias below is normalized to one of these before it crosses the - // boundary, so the Odin switch stays minimal. - var HASHES = [ - 'md5', - 'sha1', - 'sha224', - 'sha256', - 'sha384', - 'sha512', - 'sha512-256', - 'sha3-224', - 'sha3-256', - 'sha3-384', - 'sha3-512', - 'blake2b512', - 'blake2s256', - 'sm3', - ]; - - // Node/OpenSSL digest aliases whose underlying primitive Lava supports. Each - // maps a canonical name to the OpenSSL signature/legacy spellings that resolve - // to the same digest (createHash('RSA-SHA256') === createHash('sha256')). The - // display names keep Node's exact casing for getHashes(); resolveDigest() - // matches case-insensitively. Aliases for digests Lava lacks (RSA-RIPEMD160, - // RSA-SHA512/224, …) are intentionally absent — see UNSUPPORTED_NODE_DIGESTS. - var HASH_ALIAS_GROUPS = { - md5: ['RSA-MD5', 'md5WithRSAEncryption', 'ssl3-md5'], - sha1: ['RSA-SHA1', 'RSA-SHA1-2', 'sha1WithRSAEncryption', 'ssl3-sha1'], - sha224: ['RSA-SHA224', 'sha224WithRSAEncryption'], - sha256: ['RSA-SHA256', 'sha256WithRSAEncryption'], - sha384: ['RSA-SHA384', 'sha384WithRSAEncryption'], - sha512: ['RSA-SHA512', 'sha512WithRSAEncryption'], - 'sha512-256': ['RSA-SHA512/256', 'sha512-256WithRSAEncryption'], - 'sha3-224': ['RSA-SHA3-224', 'id-rsassa-pkcs1-v1_5-with-sha3-224'], - 'sha3-256': ['RSA-SHA3-256', 'id-rsassa-pkcs1-v1_5-with-sha3-256'], - 'sha3-384': ['RSA-SHA3-384', 'id-rsassa-pkcs1-v1_5-with-sha3-384'], - 'sha3-512': ['RSA-SHA3-512', 'id-rsassa-pkcs1-v1_5-with-sha3-512'], - sm3: ['RSA-SM3', 'sm3WithRSAEncryption'], - }; - - // Names Node/OpenSSL accepts that Lava intentionally rejects, because Odin's - // core:crypto/hash enum has no matching primitive. Listed for documentation - // only; resolveDigest() returns null for anything not canonical-or-aliased, so - // these surface the same "unsupported digest" errors as a bogus name. SHAKE is - // an XOF (variable-length) and would need a distinct API; RIPEMD-160, - // SHA-512/224 and md5-sha1 have no Odin primitive. - // var UNSUPPORTED_NODE_DIGESTS = [ - // 'ripemd160', 'rmd160', 'RSA-RIPEMD160', 'ripemd160WithRSA', - // 'sha512-224', 'RSA-SHA512/224', 'sha512-224WithRSAEncryption', - // 'md5-sha1', 'shake128', 'shake256', - // ]; - - // ALIAS_TO_CANONICAL: lowercased alias -> canonical name. CANONICAL_SET: the - // canonical names as a lookup. Both built once at load. - var CANONICAL_SET = {}; - HASHES.forEach(function (name) { - CANONICAL_SET[name] = true; - }); - var ALIAS_TO_CANONICAL = {}; - var ALIAS_DISPLAY_NAMES = []; - Object.keys(HASH_ALIAS_GROUPS).forEach(function (canonical) { - HASH_ALIAS_GROUPS[canonical].forEach(function (alias) { - ALIAS_TO_CANONICAL[alias.toLowerCase()] = canonical; - ALIAS_DISPLAY_NAMES.push(alias); - }); - }); - - // resolveDigest maps any accepted spelling to its canonical name, or null if - // Lava does not support it. Case-insensitive, matching OpenSSL/Node. - function resolveDigest(name) { - var lc = String(name).toLowerCase(); - if (CANONICAL_SET[lc]) return lc; - return ALIAS_TO_CANONICAL[lc] || null; - } - - // Two error shapes, matching Node exactly: createHash()/hash() throw a plain - // Error with no code ("Digest method not supported"), while the keyed digests - // (createHmac/pbkdf2/hkdf) throw TypeError ERR_CRYPTO_INVALID_DIGEST. - function unsupportedDigestError(name) { - return new Error( - name === undefined - ? 'Digest method not supported' - : 'Digest method ' + name + ' is not supported', - ); - } - function invalidDigestError(name) { - return nativeError(P.TypeError, 'ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: ' + name); - } - - function getHashes() { - return HASHES.concat(ALIAS_DISPLAY_NAMES); - } + var nativeError = require('internal/errors').nativeError; + var util = require('internal/crypto/util'); + var toU8 = util.toU8; + var getHashes = util.getHashes; + var hashModule = require('internal/crypto/hash'); + var Hash = hashModule.Hash; + var Hmac = hashModule.Hmac; + var hash = hashModule.hash; + var random = require('internal/crypto/random'); + var randomBytes = random.randomBytes; + var randomFillSync = random.randomFillSync; + var randomFill = random.randomFill; + var randomInt = random.randomInt; + var randomUUID = random.randomUUID; + var getRandomValues = random.getRandomValues; + var pbkdf2Module = require('internal/crypto/pbkdf2'); + var pbkdf2 = pbkdf2Module.pbkdf2; + var pbkdf2Sync = pbkdf2Module.pbkdf2Sync; + var hkdfModule = require('internal/crypto/hkdf'); + var hkdf = hkdfModule.hkdf; + var hkdfSync = hkdfModule.hkdfSync; + var scryptModule = require('internal/crypto/scrypt'); + var scrypt = scryptModule.scrypt; + var scryptSync = scryptModule.scryptSync; function notImplemented(name) { return function () { @@ -199,61 +76,6 @@ }; } - // Hash/Hmac accumulate chunks and call the one-shot native primitive at - // digest time (no streaming state crosses the boundary). Methods live on the - // prototype and the constructors are exported so the surface matches Node, - // where createHash()/createHmac() return Hash/Hmac instances. - - function Hash(algorithm) { - if (!(this instanceof Hash)) return new Hash(algorithm); - var algo = resolveDigest(algorithm); - if (!algo) throw unsupportedDigestError(); - this._algo = algo; - this._chunks = []; - this._finalized = false; - } - Hash.prototype.update = function (data, encoding) { - if (this._finalized) throw finalizedError(); - this._chunks.push(toU8(data, encoding)); - return this; - }; - Hash.prototype.digest = function (encoding) { - if (this._finalized) throw finalizedError(); - this._finalized = true; - var out = Buffer.from(native.hash(this._algo, concat(this._chunks))); - return encoding ? out.toString(encoding) : out; - }; - Hash.prototype.copy = function () { - if (this._finalized) throw finalizedError(); - var clone = new Hash(this._algo); - clone._chunks = this._chunks.slice(); - return clone; - }; - - function Hmac(algorithm, key) { - if (!(this instanceof Hmac)) return new Hmac(algorithm, key); - var algo = resolveDigest(algorithm); - if (!algo) throw invalidDigestError(algorithm); - this._algo = algo; - this._key = toU8(key, undefined, 'key'); - this._chunks = []; - this._finalized = false; - } - Hmac.prototype.update = function (data, encoding) { - if (this._finalized) throw finalizedError(); - this._chunks.push(toU8(data, encoding)); - return this; - }; - Hmac.prototype.digest = function (encoding) { - if (this._finalized) { - var empty = Buffer.alloc(0); - return encoding ? empty.toString(encoding) : empty; - } - this._finalized = true; - var out = Buffer.from(native.hmac(this._algo, this._key, concat(this._chunks))); - return encoding ? out.toString(encoding) : out; - }; - function createHash(algorithm) { return new Hash(algorithm); } @@ -262,174 +84,6 @@ return new Hmac(algorithm, key); } - // crypto.hash(algorithm, data[, outputEncoding]) — one-shot digest (Node 21+). - // outputEncoding defaults to "hex"; "buffer" yields a Buffer. - function hash(algorithm, data, outputEncoding) { - var algo = resolveDigest(algorithm); - if (!algo) throw unsupportedDigestError(algorithm); - var out = Buffer.from(native.hash(algo, toU8(data))); - if (outputEncoding === undefined || outputEncoding === 'hex') return out.toString('hex'); - if (outputEncoding === 'buffer') return out; - return out.toString(outputEncoding); - } - - // Every CSPRNG entry point goes through this, and none calls native.randomFill - // directly. The native returns the view it filled, so an identity check is the - // available proof that the fill ran. Second layer, not the first: the - // dispatcher already fails closed against the miss-forges-entropy scenario - // (the canonical write-up is at host_dispatch_fail, pkg/runtime/ - // host_natives.odin), but that layer is one registration decision away from - // not covering this native, and a wrong answer here is silent forged entropy. - // The error is deliberately not a Node ERR_* code: Node has no counterpart, - // because in Node this cannot happen. - function fillRandom(view) { - if (native.randomFill(view) !== view) - throw new Error('lava: CSPRNG unavailable (native.randomFill did not fill the buffer)'); - return view; - } - - function randomBytes(size, callback) { - // Node validates size as 0..2^31-1 (NaN rejected, fractional truncated); a - // negative/huge value used to wrap into a multi-gigabyte allocation instead - // of a catchable RangeError. - size = validateByteCount(size, 'size'); - var buf = Buffer.alloc(size); - if (size > 0) fillRandom(buf); - if (typeof callback === 'function') { - // Node completes a zero-size request synchronously (it queues no work for - // zero bytes); only real fills report their completion asynchronously. - if (size === 0) { - callback(null, buf); - return; - } - setImmediate(function () { - callback(null, buf); - }); - return; - } - return buf; - } - - // assertOffset/assertSize mirror Node: offset and size are counted in - // elements, converted to bytes against the view, and validated. An oversized - // size now throws ERR_OUT_OF_RANGE instead of silently filling fewer bytes. - function assertOffset(offset, elementSize, byteLength) { - if (typeof offset !== 'number') throw invalidArgType('offset', 'number', offset); - var bytes = offset * elementSize; - if (Number.isNaN(bytes) || bytes < 0 || bytes > byteLength) - throw outOfRange('offset', '>= 0 && <= ' + byteLength / elementSize, offset); - return bytes >>> 0; - } - function assertSize(size, elementSize, offsetBytes, byteLength) { - if (typeof size !== 'number') throw invalidArgType('size', 'number', size); - var bytes = size * elementSize; - if (Number.isNaN(bytes) || bytes < 0 || bytes > 0x7fffffff) - throw outOfRange('size', '>= 0 && <= ' + 0x7fffffff, size); - if (bytes + offsetBytes > byteLength) - throw outOfRange('size + offset', '<= ' + byteLength, bytes + offsetBytes); - return bytes >>> 0; - } - - // Shared buf/offset/size validation for randomFillSync and randomFill, - // resolving to the byte range to fill. Split out so randomFill can validate - // SYNCHRONOUSLY the way Node does — only the fill itself is deferred. - function resolveFillRange(buffer, offset, size) { - var ab, base, byteLength, elementSize; - if (ArrayBuffer.isView(buffer)) { - ab = buffer.buffer; - base = buffer.byteOffset; - byteLength = buffer.byteLength; - elementSize = buffer.BYTES_PER_ELEMENT || 1; - } else if ( - buffer instanceof ArrayBuffer || - (typeof SharedArrayBuffer !== 'undefined' && buffer instanceof SharedArrayBuffer) - ) { - ab = buffer; - base = 0; - byteLength = buffer.byteLength; - elementSize = 1; - } else { - throw invalidArgType('buf', ['ArrayBuffer', 'ArrayBufferView'], buffer); - } - var offsetBytes = assertOffset(offset === undefined ? 0 : offset, elementSize, byteLength); - var sizeBytes = - size === undefined - ? byteLength - offsetBytes - : assertSize(size, elementSize, offsetBytes, byteLength); - return { base: base + offsetBytes, ab: ab, sizeBytes: sizeBytes }; - } - - function randomFillSync(buffer, offset, size) { - var r = resolveFillRange(buffer, offset, size); - if (r.sizeBytes > 0) fillRandom(new Uint8Array(r.ab, r.base, r.sizeBytes)); - return buffer; - } - - function randomFill(buffer, offset, size, callback) { - if (typeof offset === 'function') { - callback = offset; - offset = undefined; - size = undefined; - } else if (typeof size === 'function') { - callback = size; - size = undefined; - } - if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); - // Validate now — Node throws a bad buf/offset/size from randomFill itself - // (deferring it turned a synchronous TypeError into an uncaught async one) - // and completes a zero-byte request synchronously, queueing no work for it. - var r = resolveFillRange(buffer, offset, size); - if (r.sizeBytes === 0) { - callback(null, buffer); - return; - } - setImmediate(function () { - fillRandom(new Uint8Array(r.ab, r.base, r.sizeBytes)); - callback(null, buffer); - }); - } - - var HEX = '0123456789abcdef'; - - function randomUUID() { - var b = randomBytes(16); - b[6] = (b[6] & 0x0f) | 0x40; - b[8] = (b[8] & 0x3f) | 0x80; - var s = ''; - for (var i = 0; i < 16; i++) { - s += HEX[(b[i] >> 4) & 0xf] + HEX[b[i] & 0xf]; - if (i === 3 || i === 5 || i === 7 || i === 9) s += '-'; - } - return s; - } - - // WHATWG crypto.getRandomValues: fill an integer TypedArray in place with - // CSPRNG bytes and return it. Float views and DataView are rejected; the spec - // caps a single call at 65536 bytes. - function getRandomValues(typedArray) { - if ( - typedArray == null || - typeof typedArray.byteLength !== 'number' || - !(typedArray.buffer instanceof ArrayBuffer) || - typedArray instanceof Float32Array || - typedArray instanceof Float64Array || - (typeof DataView !== 'undefined' && typedArray instanceof DataView) - ) { - throw new TypeError('The data argument must be an integer-type TypedArray'); - } - if (typedArray.byteLength > 65536) { - throw new RangeError( - "The ArrayBufferView's byte length (" + - typedArray.byteLength + - ') exceeds the number of bytes of entropy available via this API (65536)', - ); - } - if (typedArray.byteLength > 0) { - fillRandom(new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength)); - } - return typedArray; - } - // Constant-time comparison: never short-circuits on the first differing byte. // Node requires both operands to be ArrayBufferView/ArrayBuffer — strings and // numbers are rejected (a number used to be zero-filled, so timingSafeEqual(5, @@ -460,297 +114,6 @@ return native.timingSafeEqual(ua, ub); } - // randomInt([min, ] max[, callback]) — uniform integer in [min, max). - // Node caps the range at 2^48; native.randomInt handles rejection sampling. - function randomInt(min, max, callback) { - if (typeof min === 'function') { - callback = min; - min = undefined; - max = undefined; - } else if (typeof max === 'function') { - callback = max; - max = undefined; - } - if (max === undefined) { - max = min; - min = 0; - } - if (!Number.isSafeInteger(min) || !Number.isSafeInteger(max)) { - throw new RangeError('The value of "min"/"max" is out of range. It must be a safe integer.'); - } - if (max <= min) - throw new RangeError( - 'The value of "max" is out of range. It must be greater than the value of "min".', - ); - var range = max - min; - if (range > 0x1000000000000) - throw new RangeError('The value of "max - min" is out of range. It must be <= 2^48.'); - - function sample() { - return min + native.randomInt(range); - } - - if (typeof callback === 'function') { - setImmediate(function () { - var value; - try { - value = sample(); - } catch (err) { - callback(err); - return; - } - callback(null, value); - }); - return; - } - return sample(); - } - - function pbkdf2Sync(password, salt, iterations, keylen, digest) { - if (typeof digest !== 'string') throw invalidArgType('digest', 'string', digest); - var algo = resolveDigest(digest); - if (!algo) throw invalidDigestError(digest); - var pw = toU8(password, undefined, 'password'); - var slt = toU8(salt, undefined, 'salt'); - // Node validates iterations as 1..2^31-1 and keylen as 0..2^31-1. The old - // `iterations >>> 0` turned -1 into 4294967295 (a permanent sync hang) and - // wrapped values >= 2^32 to a different derived key. - validateInt32(iterations, 'iterations', 1); - validateInt32(keylen, 'keylen', 0); - var out = native.pbkdf2(algo, pw, slt, iterations, keylen); - return Buffer.from(out); - } - - function pbkdf2(password, salt, iterations, keylen, digest, callback) { - if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); - setImmediate(function () { - var key; - try { - key = pbkdf2Sync(password, salt, iterations, keylen, digest); - } catch (err) { - callback(err); - return; - } - callback(null, key); - }); - } - - // hkdfSync(digest, ikm, salt, info, keylen) — RFC 5869 HKDF over the native - // crypto primitive. Returns an ArrayBuffer, matching Node. - function hkdfSync(digest, ikm, salt, info, keylen) { - var algo = resolveDigest(digest); - if (!algo) throw invalidDigestError(digest); - return native.hkdf(algo, toU8(ikm), toU8(salt), toU8(info), keylen).buffer; - } - - function hkdf(digest, ikm, salt, info, keylen, callback) { - if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); - setImmediate(function () { - var key; - try { - key = hkdfSync(digest, ikm, salt, info, keylen); - } catch (err) { - callback(err); - return; - } - callback(null, key); - }); - } - - // scrypt (RFC 7914) — layered entirely on top of the native PBKDF2-HMAC-SHA256 - // primitive plus Salsa20/8 in JS, so it needs no extra native code and is - // identical across platforms. Output is byte-for-byte equal to Node/OpenSSL. - // The Salsa core and BlockMix/ROMix operate on little-endian u32 words; bytes - // are converted explicitly so the result is correct regardless of host - // endianness. - - function scryptSalsa20_8(B) { - var x = new Uint32Array(16), - i; - for (i = 0; i < 16; i++) x[i] = B[i]; - function R(a, b) { - return (a << b) | (a >>> (32 - b)); - } - for (i = 0; i < 4; i++) { - x[4] ^= R(x[0] + x[12], 7); - x[8] ^= R(x[4] + x[0], 9); - x[12] ^= R(x[8] + x[4], 13); - x[0] ^= R(x[12] + x[8], 18); - x[9] ^= R(x[5] + x[1], 7); - x[13] ^= R(x[9] + x[5], 9); - x[1] ^= R(x[13] + x[9], 13); - x[5] ^= R(x[1] + x[13], 18); - x[14] ^= R(x[10] + x[6], 7); - x[2] ^= R(x[14] + x[10], 9); - x[6] ^= R(x[2] + x[14], 13); - x[10] ^= R(x[6] + x[2], 18); - x[3] ^= R(x[15] + x[11], 7); - x[7] ^= R(x[3] + x[15], 9); - x[11] ^= R(x[7] + x[3], 13); - x[15] ^= R(x[11] + x[7], 18); - x[1] ^= R(x[0] + x[3], 7); - x[2] ^= R(x[1] + x[0], 9); - x[3] ^= R(x[2] + x[1], 13); - x[0] ^= R(x[3] + x[2], 18); - x[6] ^= R(x[5] + x[4], 7); - x[7] ^= R(x[6] + x[5], 9); - x[4] ^= R(x[7] + x[6], 13); - x[5] ^= R(x[4] + x[7], 18); - x[11] ^= R(x[10] + x[9], 7); - x[8] ^= R(x[11] + x[10], 9); - x[9] ^= R(x[8] + x[11], 13); - x[10] ^= R(x[9] + x[8], 18); - x[12] ^= R(x[15] + x[14], 7); - x[13] ^= R(x[12] + x[15], 9); - x[14] ^= R(x[13] + x[12], 13); - x[15] ^= R(x[14] + x[13], 18); - } - for (i = 0; i < 16; i++) B[i] = (B[i] + x[i]) >>> 0; - } - - function scryptBlockMix(B, Y, r) { - var X = new Uint32Array(16), - i, - j, - last = (2 * r - 1) * 16; - for (i = 0; i < 16; i++) X[i] = B[last + i]; - for (i = 0; i < 2 * r; i++) { - var off = i * 16; - for (j = 0; j < 16; j++) X[j] ^= B[off + j]; - scryptSalsa20_8(X); - var dest = (i % 2 === 0 ? i / 2 : r + (i - 1) / 2) * 16; - for (j = 0; j < 16; j++) Y[dest + j] = X[j]; - } - } - - function scryptROMix(B, N, r) { - var len = 32 * r, - V = new Uint32Array(len * N), - tmp = new Uint32Array(len), - i, - k; - for (i = 0; i < N; i++) { - V.set(B, i * len); - scryptBlockMix(B, tmp, r); - B.set(tmp); - } - for (i = 0; i < N; i++) { - var j = B[(2 * r - 1) * 16] & (N - 1), - off = j * len; - for (k = 0; k < len; k++) B[k] ^= V[off + k]; - scryptBlockMix(B, tmp, r); - B.set(tmp); - } - } - - function scryptBytesToWordsLE(bytes, off, words, wlen) { - for (var i = 0; i < wlen; i++) { - var k = off + i * 4; - words[i] = - (bytes[k] | (bytes[k + 1] << 8) | (bytes[k + 2] << 16) | (bytes[k + 3] << 24)) >>> 0; - } - } - function scryptWordsToBytesLE(words, wlen, bytes, off) { - for (var i = 0; i < wlen; i++) { - var v = words[i], - k = off + i * 4; - bytes[k] = v & 0xff; - bytes[k + 1] = (v >>> 8) & 0xff; - bytes[k + 2] = (v >>> 16) & 0xff; - bytes[k + 3] = (v >>> 24) & 0xff; - } - } - - function isPowerOfTwo(n) { - return n > 1 && (n & (n - 1)) === 0; - } - - // Resolve N/r/p with Node's option-and-alias handling. cost/blockSize/ - // parallelization are aliases for N/r/p; supplying both forms of one parameter - // throws ERR_INCOMPATIBLE_OPTION_PAIR, exactly as Node does. - function scryptParams(options) { - var opts = options || {}; - function pick(primary, alias, dflt) { - var hasP = opts[primary] !== undefined; - var hasA = opts[alias] !== undefined; - if (hasP && hasA) { - throw nodeError( - P.TypeError, - 'ERR_INCOMPATIBLE_OPTION_PAIR', - 'Option "' + primary + '" cannot be used in combination with option "' + alias + '"', - ); - } - if (hasP) return opts[primary]; - if (hasA) return opts[alias]; - return dflt; - } - var N = pick('N', 'cost', 16384); - var r = pick('r', 'blockSize', 8); - var p = pick('p', 'parallelization', 1); - var maxmem = opts.maxmem === undefined ? 32 * 1024 * 1024 : opts.maxmem; - return { N: N, r: r, p: p, maxmem: maxmem }; - } - - function scryptParamsError(message) { - return nativeError( - P.RangeError, - 'ERR_CRYPTO_INVALID_SCRYPT_PARAMS', - message || 'Invalid scrypt params', - ); - } - - function scryptDerive(password, salt, keylen, options) { - if (!Number.isInteger(keylen) || keylen < 0 || keylen > 0x7fffffff) { - throw outOfRange('keylen', '>= 0 && <= 2147483647', keylen); - } - var pr = scryptParams(options); - var N = pr.N, - r = pr.r, - p = pr.p; - // N must be a power of two greater than 1; r and p must be positive ints. - if (!Number.isInteger(N) || !isPowerOfTwo(N)) throw scryptParamsError(); - if (!Number.isInteger(r) || r < 1 || !Number.isInteger(p) || p < 1) throw scryptParamsError(); - // Memory bound, matching Node's documented ~128*N*r ceiling against maxmem. - if (128 * N * r > pr.maxmem) throw scryptParamsError(); - - var pw = toU8(password), - slt = toU8(salt); - var mfLen = 128 * r; - var B = native.pbkdf2('sha256', pw, slt, 1, p * mfLen); - var words = new Uint32Array(32 * r); - for (var i = 0; i < p; i++) { - scryptBytesToWordsLE(B, i * mfLen, words, 32 * r); - scryptROMix(words, N, r); - scryptWordsToBytesLE(words, 32 * r, B, i * mfLen); - } - // keylen === 0 yields an empty key without touching the native PBKDF2, which - // (like OpenSSL) rejects a zero output length. - if (keylen === 0) return Buffer.alloc(0); - return Buffer.from(native.pbkdf2('sha256', pw, B, 1, keylen)); - } - - function scryptSync(password, salt, keylen, options) { - return scryptDerive(password, salt, keylen, options); - } - - function scrypt(password, salt, keylen, options, callback) { - if (typeof options === 'function') { - callback = options; - options = undefined; - } - if (typeof callback !== 'function') throw new TypeError('Callback must be a function'); - setImmediate(function () { - var key; - try { - key = scryptDerive(password, salt, keylen, options); - } catch (err) { - callback(err); - return; - } - callback(null, key); - }); - } - // getFips() reports whether the process is in FIPS mode. Lava is never built // against a FIPS provider, so this is always 0, matching Node's default. function getFips() { diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index d4fe7cb..e4318f0 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -203,6 +203,10 @@ const ROUTES = [ re: globToRe('**/js/node/crypto.js'), targets: ['make test-odin', 'make test-compat-lava', 'make api-surface'], }, + { + re: globToRe('**/js/internal/crypto/*.js'), + targets: ['make test-odin', 'make test-compat-lava', 'make api-surface'], + }, { re: globToRe('pkg/runtime/js/**/*.js'), targets: ['make check-js', 'make check-primordials', 'make test-compat-lava'], diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index 0970b3c..fca8fb2 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -38,6 +38,17 @@ test('fetch internals route the fetch smoke, entry and split files alike', () => } }); +test('crypto internals route the crypto gates, entry and split files alike', () => { + for (const p of [ + 'pkg/runtime/js/node/crypto.js', + 'pkg/runtime/js/internal/crypto/hash.js', + 'pkg/runtime/js/internal/crypto/scrypt.js', + ]) { + const t = routePaths([p]).targets; + for (const g of ['make test-odin', 'make api-surface']) assert.ok(t.includes(g), p + ' ' + g); + } +}); + test('invalid **.js style is not required — **/* works', () => { const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 6ce2798..df468c9 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2516,6 +2516,24 @@ "replace": " var Response = class Response extends require('internal/fetch/response').Response {};", "gate": "compat:tests/node-compat/cases/82-fetch-internal-modules.js", "expect_detail": "node: identities true true true true true true true true true" + }, + { + "name": "83-crypto-internal-modules dies when node:crypto exports a Hash other than internal/crypto/hash's", + "why": "createHash (node/crypto) and Hash (internal/crypto/hash) now meet across a file seam; an entry that re-exports a wrapper or a stale copy hands user code a crypto.Hash that createHash()'s instances are not instances of.", + "source": "pkg/runtime/js/node/crypto.js", + "find": " var Hash = hashModule.Hash;", + "replace": " var Hash = function Hash(algorithm) {\n return new hashModule.Hash(algorithm);\n };", + "gate": "compat:tests/node-compat/cases/83-crypto-internal-modules.js", + "expect_detail": "node: identities true true true true true true true" + }, + { + "name": "83-crypto-internal-modules dies when the loader stops handing internal/crypto/random the crypto bindings", + "why": "Odin keys the crypto natives to node/crypto only; random.js reaches them solely through the loader alias, and without it every CSPRNG entry point (and the WHATWG global) throws on first use instead of at load.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " natives['internal/crypto/random'] = natives['node/crypto'];\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/83-crypto-internal-modules.js", + "expect_detail": "node: globalThis.crypto object true function function true true" } ] } diff --git a/tests/node-compat/cases/83-crypto-internal-modules.js b/tests/node-compat/cases/83-crypto-internal-modules.js new file mode 100644 index 0000000..4a21742 --- /dev/null +++ b/tests/node-compat/cases/83-crypto-internal-modules.js @@ -0,0 +1,202 @@ +// node:crypto is assembled from internal modules laid out like node's +// lib/internal/crypto: js/internal/crypto/{util,hash,random,pbkdf2,hkdf,scrypt}.js hold +// the implementation and node/crypto stays the public entry. A throw from pbkdf2Sync +// used to carry only `node:crypto` frames, because everything was defined in crypto.js; +// now it names its file, exactly as node's does. +// +// Each frame line prints `ok` when the stack carries `node:internal/crypto/:` +// and names the first `node:` frame it got otherwise. Only the source name is compared, +// never the message, line or column, and never the error class where Lava's is a known +// gap (randomInt's RangeError has no code, getRandomValues throws a RangeError where node +// throws a QuotaExceededError DOMException). timingSafeEqual is node's C++ binding with +// no JS frame at all, and stays in node/crypto on Lava, so it prints `false` on both. +// Every site throws synchronously on both runtimes (probed on node 26.13.0). +// +// The new ids must stay internal: neither the bare nor the `node:` spelling may resolve +// from user code. Only the error CODE of the bare spelling is printed (the messages +// diverge, #415). +// +// The export lines check identities that cross the new file seams (createHash in the +// entry, Hash in internal/crypto/hash; the deprecated randomBytes aliases; the WHATWG +// global). Export ORDER is not oracle-able — node's key list is a different set in a +// different order — so the split was checked against the previous binary instead (the +// PR records the diff). `new crypto.Hash` is never called: node prints DEP0179 for it. +'use strict'; + +const crypto = require('crypto'); + +for (const id of [ + 'internal/crypto/util', + 'internal/crypto/hash', + 'internal/crypto/random', + 'internal/crypto/pbkdf2', + 'internal/crypto/hkdf', + 'internal/crypto/scrypt', + 'crypto/random', +]) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +function frame(label, file, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + if (file === null) { + return console.log( + label + ': internal/crypto frame=' + stack.includes('node:internal/crypto/'), + ); + } + const where = stack.includes('node:internal/crypto/' + file + ':') + ? 'ok' + : 'got ' + (stack.match(/node:[\w/]+/) || ['no node: frame'])[0]; + console.log(label + ': ' + file + ' ' + where); +} + +frame('createHash digest', 'hash', () => crypto.createHash('bogus')); +frame('createHmac digest', 'hash', () => crypto.createHmac('bogus', 'k')); +frame('hash digest', 'hash', () => crypto.hash('bogus', 'x')); +frame('digest twice', 'hash', () => { + const h = crypto.createHash('sha1'); + h.digest(); + h.digest(); +}); +frame('randomBytes size', 'random', () => crypto.randomBytes(-1)); +frame('randomFillSync buf', 'random', () => crypto.randomFillSync(5)); +frame('randomFill offset', 'random', () => crypto.randomFill(new Uint8Array(1), -1, () => {})); +frame('randomInt range', 'random', () => crypto.randomInt(5, 1)); +frame('getRandomValues quota', 'random', () => crypto.getRandomValues(new Uint8Array(70000))); +frame('pbkdf2Sync digest', 'pbkdf2', () => crypto.pbkdf2Sync('a', 'b', 1, 1, 'bogus')); +frame('pbkdf2Sync iterations', 'pbkdf2', () => crypto.pbkdf2Sync('a', 'b', -1, 1, 'sha1')); +frame('hkdfSync digest', 'hkdf', () => crypto.hkdfSync('bogus', 'a', 'b', 'c', 1)); +frame('scryptSync keylen', 'scrypt', () => crypto.scryptSync('a', 'b', -1)); +frame('scryptSync N', 'scrypt', () => crypto.scryptSync('a', 'b', 1, { N: 3 })); +frame('scryptSync option pair', 'scrypt', () => crypto.scryptSync('a', 'b', 1, { N: 2, cost: 2 })); +frame('timingSafeEqual length', null, () => + crypto.timingSafeEqual(new Uint8Array(1), new Uint8Array(2)), +); + +// --- the assembled exports: shapes and identities ---------------------------------- +console.log('same module', require('node:crypto') === crypto); +const SHARED = [ + 'Hash', + 'Hmac', + 'createHash', + 'createHmac', + 'hash', + 'getHashes', + 'timingSafeEqual', + 'randomBytes', + 'randomFillSync', + 'randomFill', + 'randomInt', + 'randomUUID', + 'pbkdf2', + 'pbkdf2Sync', + 'hkdf', + 'hkdfSync', + 'scrypt', + 'scryptSync', + 'getFips', + 'constants', + 'getRandomValues', +]; +for (const k of SHARED) { + const d = Object.getOwnPropertyDescriptor(crypto, k); + // Enumerability is not printed: getRandomValues is enumerable on node and not on Lava, + // a pre-existing gap the split does not touch. + console.log('export', k, typeof crypto[k], 'value' in d ? 'data' : 'accessor'); +} +console.log( + 'identities', + crypto.createHash('sha256') instanceof crypto.Hash, + crypto.createHmac('sha256', 'k') instanceof crypto.Hmac, + crypto.createHash('sha256').copy() instanceof crypto.Hash, + crypto.prng === crypto.randomBytes, + crypto.rng === crypto.randomBytes, + crypto.pseudoRandomBytes === crypto.randomBytes, + crypto.getRandomValues === crypto.getRandomValues, +); + +// --- the WHATWG global -------------------------------------------------------------- +const g = globalThis.crypto; +const u8 = new Uint8Array(8); +console.log( + 'globalThis.crypto', + typeof g, + g === globalThis.crypto, + typeof g.randomUUID, + typeof g.getRandomValues, + g.getRandomValues(u8) === u8, + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/.test(g.randomUUID()), +); + +// --- behavior across the file seams ------------------------------------------------- +console.log('sha256', crypto.createHash('sha256').update('la').update('va').digest('hex')); +console.log('alias', crypto.createHash('RSA-SHA256').update('lava').digest('base64')); +console.log('one-shot', crypto.hash('sha1', 'lava'), crypto.hash('md5', 'lava', 'base64')); +console.log('hmac', crypto.createHmac('sha256', 'key').update('lava').digest('hex')); +const partial = crypto.createHash('sha512').update('la'); +const forked = partial.copy(); +console.log( + 'copy', + partial.update('va').digest('hex') === forked.update('va').digest('hex'), + forked.digest === crypto.Hash.prototype.digest, +); +const hashes = crypto.getHashes(); +console.log('getHashes', hashes.includes('sha256'), hashes.includes('RSA-SHA256')); +console.log('pbkdf2Sync', crypto.pbkdf2Sync('password', 'salt', 2, 16, 'sha256').toString('hex')); +console.log( + 'hkdfSync', + Buffer.from(crypto.hkdfSync('sha256', 'ikm', 'salt', 'info', 16)).toString('hex'), +); +console.log( + 'scryptSync', + crypto.scryptSync('pw', 'salt', 16, { N: 16, r: 1, p: 1 }).toString('hex'), +); +console.log( + 'timingSafeEqual', + crypto.timingSafeEqual(Buffer.from('ab'), Buffer.from('ab')), + crypto.timingSafeEqual(Buffer.from('ab'), Buffer.from('ac')), +); +const filled = new Uint16Array(4); +console.log( + 'random', + crypto.randomBytes(7).length, + crypto.randomFillSync(filled) === filled, + (() => { + const n = crypto.randomInt(3, 9); + return n >= 3 && n < 9; + })(), +); +console.log('constants', crypto.constants.RSA_PKCS1_PADDING, crypto.getFips()); + +const cb = (f) => new Promise((resolve, reject) => f((err, v) => (err ? reject(err) : resolve(v)))); +Promise.all([ + cb((done) => crypto.randomBytes(5, done)), + cb((done) => crypto.randomFill(new Uint8Array(3), done)), + cb((done) => crypto.randomInt(10, done)), + cb((done) => crypto.pbkdf2('password', 'salt', 2, 16, 'sha256', done)), + cb((done) => crypto.hkdf('sha256', 'ikm', 'salt', 'info', 16, done)), + cb((done) => crypto.scrypt('pw', 'salt', 16, { N: 16, r: 1, p: 1 }, done)), +]).then(([rb, rf, ri, pb, hk, sc]) => { + console.log('async random', rb.length, rf.length, ri >= 0 && ri < 10); + console.log('async pbkdf2', pb.toString('hex')); + console.log('async hkdf', Buffer.from(hk).toString('hex')); + console.log('async scrypt', sc.toString('hex')); +}); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index bb6e62e..ab76da1 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -35,6 +35,42 @@ "accessor": 0, "global": 0 }, + "internal/crypto/hash.js": { + "method": 8, + "invoke": 0, + "accessor": 0, + "global": 5 + }, + "internal/crypto/hkdf.js": { + "method": 0, + "invoke": 0, + "accessor": 1, + "global": 1 + }, + "internal/crypto/pbkdf2.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 2 + }, + "internal/crypto/random.js": { + "method": 0, + "invoke": 0, + "accessor": 12, + "global": 24 + }, + "internal/crypto/scrypt.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 12 + }, + "internal/crypto/util.js": { + "method": 10, + "invoke": 0, + "accessor": 4, + "global": 15 + }, "internal/encoding.js": { "method": 0, "invoke": 0, @@ -264,10 +300,10 @@ "global": 0 }, "node/crypto.js": { - "method": 22, + "method": 4, "invoke": 0, - "accessor": 17, - "global": 61 + "accessor": 0, + "global": 2 }, "node/diagnostics_channel.js": { "method": 5, From 6fa09a6be30c40fe9b14e573f3bb07f624c8ea5a Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:03:36 +0200 Subject: [PATCH 37/61] refactor(util): split node:util into internal/util{,/inspect,/comparisons,/debuglog} node/util.js 890 -> 209 lines; exports, order, identities and descriptors unchanged. util stays lazy (no eager delta). Case 84 pins the layout; util mutations re-pointed. Co-Authored-By: Claude Opus 5.5 --- docs/ARCHITECTURE.md | 10 +- pkg/runtime/globals.odin | 1 + pkg/runtime/js/bootstrap/console.js | 2 +- pkg/runtime/js/internal/util.js | 72 ++ pkg/runtime/js/internal/util/comparisons.js | 162 ++++ pkg/runtime/js/internal/util/debuglog.js | 153 ++++ pkg/runtime/js/internal/util/inspect.js | 357 +++++++++ pkg/runtime/js/node/util.js | 727 +----------------- scripts/lib/global-replace-detect.mjs | 2 +- tests/mutation-manifest.json | 42 +- .../node-compat/cases/57-format-single-arg.js | 2 +- .../cases/84-util-internal-modules.js | 165 ++++ tests/node-compat/pollution-baseline.json | 32 +- 13 files changed, 1003 insertions(+), 724 deletions(-) create mode 100644 pkg/runtime/js/internal/util.js create mode 100644 pkg/runtime/js/internal/util/comparisons.js create mode 100644 pkg/runtime/js/internal/util/debuglog.js create mode 100644 pkg/runtime/js/internal/util/inspect.js create mode 100644 tests/node-compat/cases/84-util-internal-modules.js diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index cdad89f..5b5e26e 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -216,7 +216,15 @@ requiring only `util`. `node/crypto` keeps `createHash`/`createHmac` and `timing where node's `lib/crypto.js` has them, assembles the exports in their old order and installs `globalThis.crypto`; the five files that call into Odin get the `node/crypto` bindings through the same `loader.js` aliasing as http. All seven are eager through the one -`req('crypto')` root. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, +`req('crypto')` root. Util follows node's `lib/internal/util` seams the same way: +`internal/util/inspect` (`inspect`, `format`/`formatWithOptions`, +`stripVTControlCharacters`, the `inspect.colors` table), `internal/util/comparisons` +(`isDeepStrictEqual`), `internal/util/debuglog` (requiring `inspect` for its formatting) +and `internal/util` (`promisify`, `deprecate`). `node/util` keeps `callbackify`, +`inherits`, `styleText` and `toUSVString` where node's `lib/util.js` has them, plus +`aborted`, and assembles the exports in their old order. Util calls no native, so there +is no alias, and every file stays lazy behind `require('util')`. Errors use Node-shaped +coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, `ERR_MISSING_ARGS`, `ERR_UNKNOWN_ENCODING`, and two constructors for node's two shapes — diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index 9e36391..cd10d46 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1642,5 +1642,6 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"internal/fetch", #load_directory("js/internal/fetch")}, {"internal/http", #load_directory("js/internal/http")}, {"internal/streams", #load_directory("js/internal/streams")}, + {"internal/util", #load_directory("js/internal/util")}, {"internal/webstreams", #load_directory("js/internal/webstreams")}, } diff --git a/pkg/runtime/js/bootstrap/console.js b/pkg/runtime/js/bootstrap/console.js index 27a33d3..1a31f25 100644 --- a/pkg/runtime/js/bootstrap/console.js +++ b/pkg/runtime/js/bootstrap/console.js @@ -94,7 +94,7 @@ // `%%` was folded unconditionally. // `typeof first === 'string'` matters: `format(5, '%%')` has no format string // at all, so node inspects and space-joins, leaving the later `%%` literal. - // Duplicated from node/util.js rather than shared, and deliberately: this + // Duplicated from internal/util/inspect.js rather than shared, and deliberately: this // prelude runs before the module loader, receives only the native write // functions, and has no `require` to reach util with. if (args.length === 1 && typeof first === 'string') return first; diff --git a/pkg/runtime/js/internal/util.js b/pkg/runtime/js/internal/util.js new file mode 100644 index 0000000..df0c7ae --- /dev/null +++ b/pkg/runtime/js/internal/util.js @@ -0,0 +1,72 @@ +// util.promisify and util.deprecate — where node keeps them: lib/internal/util.js. +(function (require, module) { + 'use strict'; + + // Through the primordials table rather than the live `Symbol` global: this file is + // evaluated lazily (node/util is its only requirer), so a module-eval read here is not + // the pre-user-code read §5's exemption assumes. + var SymbolFor = require('internal/primordials').SymbolFor; + + // util.promisify — wrap a (…args, callback(err, value)) function as one returning a + // Promise. Honors the util.promisify.custom symbol and copies the original's own + // properties / prototype, matching Node (the multi-arg `customArgs` form is omitted). + // Guarded like customInspect in internal/util/inspect, on the same primordial: an + // unguarded call here made a + // missing SymbolFor throw inside util's factory and masked what the 57-format-single-arg + // mutation pins (the inspect hook rendering a Buffer). + var kCustomPromisify = SymbolFor ? SymbolFor('nodejs.util.promisify.custom') : null; + + function promisify(original) { + if (typeof original !== 'function') { + throw new TypeError('The "original" argument must be of type function'); + } + if (original[kCustomPromisify]) { + var custom = original[kCustomPromisify]; + if (typeof custom !== 'function') { + throw new TypeError('The "util.promisify.custom" property must be of type function'); + } + return custom; + } + function fn() { + var args = Array.prototype.slice.call(arguments); + var self = this; + return new Promise(function (resolve, reject) { + args.push(function (err, value) { + if (err) reject(err); + else resolve(value); + }); + Reflect.apply(original, self, args); + }); + } + Object.setPrototypeOf(fn, Object.getPrototypeOf(original)); + Object.defineProperty(fn, kCustomPromisify, { + value: fn, + enumerable: false, + writable: false, + configurable: true, + }); + return Object.defineProperties(fn, Object.getOwnPropertyDescriptors(original)); + } + promisify.custom = kCustomPromisify; + + // util.deprecate — return a wrapper that emits a one-time deprecation warning, then + // delegates to fn. Suppressed entirely when process.noDeprecation is set. + function deprecate(fn, msg, code) { + if (process.noDeprecation === true) return fn; + var warned = false; + function deprecated() { + if (!warned) { + warned = true; + if (typeof process.emitWarning === 'function') { + process.emitWarning(msg, 'DeprecationWarning', code); + } else { + console.error('DeprecationWarning: ' + msg); + } + } + return Reflect.apply(fn, this, arguments); + } + return deprecated; + } + + module.exports = { promisify: promisify, deprecate: deprecate }; +}); diff --git a/pkg/runtime/js/internal/util/comparisons.js b/pkg/runtime/js/internal/util/comparisons.js new file mode 100644 index 0000000..b4f792e --- /dev/null +++ b/pkg/runtime/js/internal/util/comparisons.js @@ -0,0 +1,162 @@ +// util.isDeepStrictEqual — where node keeps it: lib/internal/util/comparisons.js. Pure +// JS; requires nothing. +(function (require, module) { + 'use strict'; + + // util.isDeepStrictEqual — structural strict equality, matching Node's algorithm: + // SameValue-ish primitives (NaN equal, +0 !== -0), matching prototype + [[Class]] tag, + // special handling for Date/RegExp/Error/boxed primitives/Map/Set/ArrayBuffer/TypedArray/ + // DataView, recursive comparison of own enumerable string + symbol keys, and circular refs. + var objTag = function (v) { + return Object.prototype.toString.call(v); + }; + + function bytesEqual(x, y) { + if (x.length !== y.length) return false; + for (var i = 0; i < x.length; i++) if (x[i] !== y[i]) return false; + return true; + } + + function ownEnumerableKeys(o) { + var keys = Object.keys(o); + var syms = Object.getOwnPropertySymbols(o); + for (var i = 0; i < syms.length; i++) { + if (Object.prototype.propertyIsEnumerable.call(o, syms[i])) keys.push(syms[i]); + } + return keys; + } + + function keysEqual(a, b, aStack, bStack) { + var aKeys = ownEnumerableKeys(a); + if (aKeys.length !== ownEnumerableKeys(b).length) return false; + for (var i = 0; i < aKeys.length; i++) { + var k = aKeys[i]; + if (!Object.prototype.hasOwnProperty.call(b, k)) return false; + if (!deepStrict(a[k], b[k], aStack, bStack)) return false; + } + return true; + } + + // Map/Set equality as node's comparisons.js does it in strict mode: a primitive key + // (or Set member) can only deep-equal the identical primitive, so it is looked up + // directly with b.has/b.get — O(n). Only object keys/members need the pairwise + // deepStrict scan, and only against b's object keys. The pairwise scan over every + // entry was O(n^2): one assert.deepStrictEqual on two equal 16k int->int Maps took + // 2179 ms that way and 4 ms this way (medians of 5 interleaved launches, darwin arm64). + // Sizes are already known equal, and Map/Set normalize -0 and match NaN via + // SameValueZero, which is what deepStrict answers for those keys anyway. + function collectionEqual(a, b, isMap, aStack, bStack) { + var aObjects = []; + var ok = true; + a.forEach(function (v, k) { + if (!ok) return; + var key = isMap ? k : v; + if (typeof key === 'object' && key !== null) { + aObjects.push([key, v]); + } else if (!b.has(key)) { + ok = false; + } else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) { + ok = false; + } + }); + if (!ok) return false; + if (aObjects.length === 0) return true; + var bObjects = []; + b.forEach(function (v, k) { + var key = isMap ? k : v; + if (typeof key === 'object' && key !== null) bObjects.push([key, v]); + }); + if (aObjects.length !== bObjects.length) return false; + var used = new Array(bObjects.length); + for (var i = 0; i < aObjects.length; i++) { + var found = false; + for (var j = 0; j < bObjects.length; j++) { + if (used[j]) continue; + var keyOk = deepStrict(aObjects[i][0], bObjects[j][0], aStack, bStack); + var valOk = !isMap || deepStrict(aObjects[i][1], bObjects[j][1], aStack, bStack); + if (keyOk && valOk) { + used[j] = true; + found = true; + break; + } + } + if (!found) return false; + } + return true; + } + + function deepStrict(a, b, aStack, bStack) { + if (a === b) return a !== 0 || Object.is(a, b); // +0 !== -0; everything else equal + if (typeof a !== 'object' || a === null) { + // primitive (or null) — equal only if both are NaN + return typeof a === 'number' && a !== a && typeof b === 'number' && b !== b; + } + if (typeof b !== 'object' || b === null) return false; + if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; + + var tag = objTag(a); + if (tag !== objTag(b)) return false; + + switch (tag) { + case '[object Number]': + case '[object Boolean]': + case '[object String]': + case '[object BigInt]': + case '[object Symbol]': { + var av = a.valueOf(); + var bv = b.valueOf(); + if (av === bv) return av !== 0 || Object.is(av, bv); + return typeof av === 'number' && av !== av && bv !== bv; + } + case '[object Date]': + return Object.is(a.getTime(), b.getTime()); + case '[object RegExp]': + if (a.source !== b.source || a.flags !== b.flags) return false; + break; + case '[object Error]': + if (a.name !== b.name || a.message !== b.message) return false; + break; + case '[object ArrayBuffer]': + if (a.byteLength !== b.byteLength) return false; + return bytesEqual(new Uint8Array(a), new Uint8Array(b)); + case '[object DataView]': + if (a.byteLength !== b.byteLength) return false; + return bytesEqual( + new Uint8Array(a.buffer, a.byteOffset, a.byteLength), + new Uint8Array(b.buffer, b.byteOffset, b.byteLength), + ); + } + + // Typed arrays: element-wise (NaN-aware); then fall through to compare any extra keys. + if (ArrayBuffer.isView(a) && tag !== '[object DataView]') { + if (a.length !== b.length) return false; + for (var t = 0; t < a.length; t++) { + if (!(a[t] === b[t] || (a[t] !== a[t] && b[t] !== b[t]))) return false; + } + } + + // Circular-reference guard. + for (var s = 0; s < aStack.length; s++) { + if (aStack[s] === a) return bStack[s] === b; + } + aStack.push(a); + bStack.push(b); + + var result; + if (tag === '[object Map]' || tag === '[object Set]') { + result = a.size === b.size && collectionEqual(a, b, tag === '[object Map]', aStack, bStack); + } else { + result = keysEqual(a, b, aStack, bStack); + } + + aStack.pop(); + bStack.pop(); + return result; + } + + function isDeepStrictEqual(a, b) { + return deepStrict(a, b, [], []); + } + + module.exports = { isDeepStrictEqual: isDeepStrictEqual }; +}); diff --git a/pkg/runtime/js/internal/util/debuglog.js b/pkg/runtime/js/internal/util/debuglog.js new file mode 100644 index 0000000..af12a24 --- /dev/null +++ b/pkg/runtime/js/internal/util/debuglog.js @@ -0,0 +1,153 @@ +// util.debuglog (aliased as util.debug) — where node keeps it: +// lib/internal/util/debuglog.js. Formats its lines through internal/util/inspect. +(function (require, module) { + 'use strict'; + + // Captured for debugEnvPattern below, which replaced three global-flag replaces on the + // NODE_DEBUG path. + var UtilPrimordials = require('internal/primordials'); + var StringPrototypeCharCodeAt = UtilPrimordials.StringPrototypeCharCodeAt; + var StringPrototypeSlice = UtilPrimordials.StringPrototypeSlice; + var StringPrototypeToUpperCase = UtilPrimordials.StringPrototypeToUpperCase; + var inspectModule = require('internal/util/inspect'); + var format = inspectModule.format; + var formatWithOptions = inspectModule.formatWithOptions; + + // --- util.debuglog / util.debug --- + // NODE_DEBUG is a comma-separated list of sections (no whitespace trimming); `*` is a + // wildcard and matching is case-insensitive. Node compiles it once into an anchored regex + // (escape regex specials, then `*` -> `.*`, `,` -> alternation) tested against the + // upper-cased section name. + // debugEnvPattern compiles NODE_DEBUG into the body of that anchored regex in ONE + // pass, where Node (and this file, before) chained three global replaces: + // + // .replace(/[|\\{}()[\]^$+?.]/g, '\\$&') escape the regex specials + // .replace(/\*/g, '.*') '*' is the wildcard + // .replace(/,/g, '$|^') ',' separates sections + // + // Three global replaces are three spin-forever sites: under a forged + // `RegExp.prototype.exec` a global replace never advances `lastIndex`, so it does not + // answer wrongly, it never returns — and this one runs on the FIRST debuglog call, so + // the process wedges at the point it tries to emit a diagnostic. node answers + // correctly under the same poison, so it was a Lava-only defect; pinned by + // tests/node-compat/cases/59-global-replace-hangs.js. + // + // Collapsing the three passes into one is sound because they cannot feed each other: + // the escape set contains neither `*` nor `,`, so those survive step 1 untouched, and + // neither `.*` nor `$|^` introduces a character a later step would rewrite. + function debugEnvPattern(env) { + var out = ''; + for (var i = 0; i < env.length; i++) { + var c = StringPrototypeCharCodeAt(env, i); + if (c === 0x2a) { + out += '.*'; // '*' wildcard + } else if (c === 0x2c) { + out += '$|^'; // ',' section separator + } else if (isRegExpSpecial(c)) { + out += '\\' + StringPrototypeSlice(env, i, i + 1); + } else { + out += StringPrototypeSlice(env, i, i + 1); + } + } + return StringPrototypeToUpperCase(out); + } + + // The `[|\\{}()[\]^$+?.]` class, as code units. `*` and `,` are deliberately absent — + // they are the two the caller rewrites rather than escapes. + function isRegExpSpecial(c) { + return ( + c === 0x7c || // | + c === 0x5c || // \ + c === 0x7b || // { + c === 0x7d || // } + c === 0x28 || // ( + c === 0x29 || // ) + c === 0x5b || // [ + c === 0x5d || // ] + c === 0x5e || // ^ + c === 0x24 || // $ + c === 0x2b || // + + c === 0x3f || // ? + c === 0x2e // . + ); + } + + var debugEnvRegex = /^$/; + var debugEnvInitialized = false; + function initDebugEnv() { + if (debugEnvInitialized) return; + debugEnvInitialized = true; + var env = typeof process !== 'undefined' && process.env ? process.env.NODE_DEBUG : ''; + if (env) { + debugEnvRegex = new RegExp('^' + debugEnvPattern(env) + '$', 'i'); + } + } + function testEnabled(section) { + initDebugEnv(); + return debugEnvRegex.exec(section) !== null; + } + + var debugImpls = { __proto__: null }; + function debuglogImpl(enabled, section) { + if (debugImpls[section] === undefined) { + if (enabled) { + debugImpls[section] = function () { + var args = [{}]; + for (var i = 0; i < arguments.length; i++) args[i + 1] = arguments[i]; + var msg = formatWithOptions.apply(null, args); + var pid = typeof process !== 'undefined' && process.pid ? process.pid : 0; + process.stderr.write(format('%s %d: %s\n', section, pid, msg)); + }; + } else { + debugImpls[section] = function () {}; + } + } + return debugImpls[section]; + } + + function debuglog(set, cb) { + var enabled; + function init() { + set = String(set).toUpperCase(); + enabled = testEnabled(set); + } + // The heavy impl is built lazily on the first call; the optional callback is invoked then + // (once) with the real logger so callers can capture/replace it. + var debug = function () { + init(); + debug = debuglogImpl(enabled, set); + if (typeof cb === 'function') { + Object.defineProperty(debug, 'enabled', { + get: function () { + return enabled; + }, + configurable: true, + enumerable: true, + }); + cb(debug); + } + return debug.apply(this, arguments); + }; + var test = function () { + init(); + test = function () { + return enabled; + }; + return enabled; + }; + var logger = function () { + if (enabled === false) return; + return debug.apply(this, arguments); + }; + Object.defineProperty(logger, 'enabled', { + get: function () { + return test(); + }, + configurable: true, + enumerable: true, + }); + return logger; + } + + module.exports = { debuglog: debuglog }; +}); diff --git a/pkg/runtime/js/internal/util/inspect.js b/pkg/runtime/js/internal/util/inspect.js new file mode 100644 index 0000000..43546a3 --- /dev/null +++ b/pkg/runtime/js/internal/util/inspect.js @@ -0,0 +1,357 @@ +// util.inspect, util.format/formatWithOptions, util.stripVTControlCharacters and the +// util.inspect.colors table — where node keeps them: lib/internal/util/inspect.js. Pure +// JS, no native dependencies; ported in spirit from node's file but trimmed to the +// common cases. node/util re-exports these and reads `inspectColors` for styleText; +// internal/util/debuglog formats through format/formatWithOptions. +(function (require, module) { + 'use strict'; + + var UtilPrimordials = require('internal/primordials'); + var ERR_INVALID_ARG_TYPE = require('internal/errors').ERR_INVALID_ARG_TYPE; + var StringPrototypeCharCodeAt = UtilPrimordials.StringPrototypeCharCodeAt; + var StringPrototypeSlice = UtilPrimordials.StringPrototypeSlice; + var StringPrototypeIndexOf = UtilPrimordials.StringPrototypeIndexOf; + + // Through the primordials table rather than the live `Symbol` global: this file is + // evaluated lazily, so a module-eval read here is not the pre-user-code read §5's + // exemption assumes (the same reasoning that moved fs.js's Buffer capture to loader.js). + var SymbolFor = UtilPrimordials.SymbolFor; + var customInspect = SymbolFor ? SymbolFor('nodejs.util.inspect.custom') : null; + + // node PICKS the quote character to avoid escaping rather than always using single + // quotes. Its strEscape tests, in order: single by default; DOUBLE when the string + // contains a single quote but no double; BACKTICK when it contains both AND contains + // neither a backtick NOR the substring `${`; single-with-escapes otherwise. Verified on + // node 26.13.0 (probe, 2026-11-16): + // plain -> 'plain' it's -> "it's" + // say "hi" -> 'say "hi"' ' and " -> `both ' and "` + // ` and ' and " -> 'all ` \' " ' a'b"c${d} -> 'a\'b"c${d}' + // + // The `${` half is not cosmetic and an earlier revision omitted it: inspect output is + // meant to read back as a literal, and a backtick-delimited string containing `${` is a + // live template substitution. That revision also made this comment state the rule + // without it, so the comment agreed with the code and neither agreed with node. + // + // ESCAPES, matching node's strEscape exactly. Measured on node 26.13.0 across the whole + // C0 range, DEL/C1 and surrogates: + // \b \t \n \f \r the five with short forms — note 0x0B is `\x0B`, NOT `\v` + // other c < 0x20 `\xNN`, UPPERCASE hex + // 0x7F..0x9F `\xNN` (DEL and the C1 block); 0xA0 stays literal + // unpaired surrogate `\udXXX`, LOWERCASE hex — node ESCAPES it rather than + // replacing it with U+FFFD, so the value stays lossless + // a valid pair literal (an emoji prints as itself) + // + // This used to escape only the backslash and \n, which was a security gap and not a + // cosmetic one: an unescaped `\x1B` makes any line built with util.inspect an + // ANSI-injection vector, and a lone surrogate came back as U+FFFD — lossy, not merely + // rendered differently. Raised on #330 review; #331 keeps the remaining half, the + // missing maxArrayLength / maxStringLength caps. + // + // A code-unit loop rather than a regex, per §5, batching literal runs so the common case + // (nothing to escape) costs one slice instead of per-character concatenation. + var HEX_UPPER = '0123456789ABCDEF'; + var HEX_LOWER = '0123456789abcdef'; + function escByte(c) { + return '\\x' + HEX_UPPER[(c >> 4) & 0xf] + HEX_UPPER[c & 0xf]; + } + function escUnit(c) { + return ( + '\\u' + + HEX_LOWER[(c >> 12) & 0xf] + + HEX_LOWER[(c >> 8) & 0xf] + + HEX_LOWER[(c >> 4) & 0xf] + + HEX_LOWER[c & 0xf] + ); + } + + function quote(s) { + var q = "'"; + if (StringPrototypeIndexOf(s, "'") !== -1) { + if (StringPrototypeIndexOf(s, '"') === -1) q = '"'; + else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1) + q = '`'; + } + var delim = StringPrototypeCharCodeAt(q, 0); + var out = ''; + var runStart = 0; + for (var i = 0; i < s.length; i++) { + var c = StringPrototypeCharCodeAt(s, i); + var esc = null; + if (c === 0x5c) esc = '\\\\'; + // Only the single quote can ever need this: the other two delimiters are chosen + // precisely because they are absent from the string. + else if (c === delim) esc = '\\' + q; + else if (c === 0x08) esc = '\\b'; + else if (c === 0x09) esc = '\\t'; + else if (c === 0x0a) esc = '\\n'; + else if (c === 0x0c) esc = '\\f'; + else if (c === 0x0d) esc = '\\r'; + else if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) esc = escByte(c); + else if (c >= 0xd800 && c <= 0xdbff) { + // A high surrogate is literal only when a low one follows; otherwise it is lone. + var next = i + 1 < s.length ? StringPrototypeCharCodeAt(s, i + 1) : 0; + if (next >= 0xdc00 && next <= 0xdfff) { + i++; // consume the pair + } else { + esc = escUnit(c); + } + } else if (c >= 0xdc00 && c <= 0xdfff) { + // Reached only when no high surrogate preceded — a paired low was consumed above. + esc = escUnit(c); + } + if (esc !== null) { + out += StringPrototypeSlice(s, runStart, i) + esc; + runStart = i + 1; + } + } + return q + out + StringPrototypeSlice(s, runStart) + q; + } + + function inspect(v, opts, seen, depth) { + opts = opts || {}; + seen = seen || []; + depth = depth || 0; + var maxDepth = opts.depth === undefined ? 2 : opts.depth === null ? Infinity : opts.depth; + + var t = typeof v; + if (v === null) return 'null'; + if (t === 'undefined') return 'undefined'; + if (t === 'string') return quote(v); + if (t === 'number') return Object.is(v, -0) ? '-0' : String(v); + if (t === 'boolean') return String(v); + if (t === 'bigint') return String(v) + 'n'; + if (t === 'symbol') return v.toString(); + if (t === 'function') { + var fn = v.name; + return fn ? '[Function: ' + fn + ']' : '[Function (anonymous)]'; + } + // Honor the util.inspect custom hook (Buffer renders as ""). Node + // passes (depth, options, inspect) and formats whatever the hook returns: + // a string is used verbatim, anything else (number, object, …) is inspected + // in its place, and returning the object itself falls through to default + // formatting. + if (customInspect && typeof v[customInspect] === 'function') { + var nested = seen.concat([v]); + var recurse = function (val, o) { + return inspect(val, o || opts, nested, depth + 1); + }; + var custom = v[customInspect](depth, opts, recurse); + if (custom !== v) { + return typeof custom === 'string' ? custom : inspect(custom, opts, nested, depth); + } + } + if (seen.indexOf(v) !== -1) return '[Circular *1]'; + if (v instanceof Error) return v.stack ? v.stack : v.name + ': ' + v.message; + if (v instanceof Date) return isNaN(v.getTime()) ? 'Invalid Date' : v.toISOString(); + if (v instanceof RegExp) return v.toString(); + if (depth > maxDepth) return Array.isArray(v) ? '[Array]' : '[Object]'; + seen = seen.concat([v]); + + if (Array.isArray(v)) { + if (v.length === 0) return '[]'; + var arr = []; + for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], opts, seen, depth + 1)); + return '[ ' + arr.join(', ') + ' ]'; + } + if (typeof Map !== 'undefined' && v instanceof Map) { + var mp = []; + v.forEach(function (val, key) { + mp.push(inspect(key, opts, seen, depth + 1) + ' => ' + inspect(val, opts, seen, depth + 1)); + }); + return 'Map(' + v.size + ') {' + (mp.length > 0 ? ' ' + mp.join(', ') + ' ' : '') + '}'; + } + if (typeof Set !== 'undefined' && v instanceof Set) { + var st = []; + v.forEach(function (val) { + st.push(inspect(val, opts, seen, depth + 1)); + }); + return 'Set(' + v.size + ') {' + (st.length > 0 ? ' ' + st.join(', ') + ' ' : '') + '}'; + } + + var keys = Object.keys(v); + var ctor = v.constructor && v.constructor.name; + var prefix = ctor && ctor !== 'Object' ? ctor + ' ' : ''; + if (keys.length === 0) return prefix + '{}'; + var props = []; + for (var k = 0; k < keys.length; k++) { + var key = keys[k]; + var name = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : quote(key); + props.push(name + ': ' + inspect(v[key], opts, seen, depth + 1)); + } + return prefix + '{ ' + props.join(', ') + ' }'; + } + + function stringify(v) { + return typeof v === 'string' ? v : inspect(v, {}, [], 1); + } + + function formatWithOptions(opts, _f) { + var args = Array.prototype.slice.call(arguments, 1); + if (args.length === 0) return ''; + var first = args[0]; + // Node's short circuit, and it must come BEFORE the substitution loop rather + // than inside it: a lone string argument is the answer, directives and all. + // `format('%s')`, `format('%z')`, `format('100%')` and `format('%%')` all come + // back untouched on node 26.13.0 (verified by + // tests/node-compat/cases/57-format-single-arg.js, not read off the docs). + // Only `%%` used to get this wrong here — `%s` and friends were saved further + // down by the "arguments exhausted" branch, which re-emits the directive, while + // `%%` was folded unconditionally. + // `typeof first === 'string'` matters: `format(5, '%%')` has no format string + // at all, so node inspects and space-joins, leaving the later `%%` literal. + if (args.length === 1 && typeof first === 'string') return first; + var i = 1; + var result; + if (typeof first === 'string' && first.indexOf('%') !== -1) { + result = ''; + for (var p = 0; p < first.length; p++) { + var ch = first.charAt(p); + if (ch === '%' && p + 1 < first.length) { + var spec = first.charAt(p + 1); + if (spec === '%') { + result += '%'; + p++; + continue; + } + if ('sdifjoOc'.indexOf(spec) !== -1) { + if (i >= args.length) { + result += '%' + spec; + p++; + continue; + } + var a = args[i++]; + p++; + if (spec === 's') result += typeof a === 'string' ? a : stringify(a); + else if (spec === 'd' || spec === 'i') + result += + typeof a === 'bigint' + ? String(a) + 'n' + : typeof a === 'symbol' + ? 'NaN' + : String(Math.trunc(Number(a))); + else if (spec === 'f') result += typeof a === 'symbol' ? 'NaN' : String(parseFloat(a)); + else if (spec === 'j') { + try { + result += JSON.stringify(a); + } catch { + result += '[Circular]'; + } + } else if (spec === 'o' || spec === 'O') result += inspect(a, opts, [], 1); + /* spec === "c": CSS directive, consumed and ignored */ + continue; + } + } + result += ch; + } + } else { + result = stringify(first); + } + for (; i < args.length; i++) result += ' ' + stringify(args[i]); + return result; + } + + function format(_f) { + return formatWithOptions.apply(null, [{}].concat(Array.prototype.slice.call(arguments))); + } + + // The live color table backing both util.inspect.colors and util.styleText. Exposed as + // util.inspect.colors below, so a caller can add or change a style and have styleText + // pick it up (Node parity). Null-prototype, so a key like 'constructor' resolves to + // undefined rather than an inherited function. Names map to [openCode, closeCode]. + var inspectColors = { + __proto__: null, + reset: [0, 0], + bold: [1, 22], + dim: [2, 22], + italic: [3, 23], + underline: [4, 24], + blink: [5, 25], + inverse: [7, 27], + hidden: [8, 28], + strikethrough: [9, 29], + doubleunderline: [21, 24], + black: [30, 39], + red: [31, 39], + green: [32, 39], + yellow: [33, 39], + blue: [34, 39], + magenta: [35, 39], + cyan: [36, 39], + white: [37, 39], + bgBlack: [40, 49], + bgRed: [41, 49], + bgGreen: [42, 49], + bgYellow: [43, 49], + bgBlue: [44, 49], + bgMagenta: [45, 49], + bgCyan: [46, 49], + bgWhite: [47, 49], + framed: [51, 54], + overlined: [53, 55], + gray: [90, 39], + grey: [90, 39], + redBright: [91, 39], + greenBright: [92, 39], + yellowBright: [93, 39], + blueBright: [94, 39], + magentaBright: [95, 39], + cyanBright: [96, 39], + whiteBright: [97, 39], + bgGray: [100, 49], + bgGrey: [100, 49], + bgRedBright: [101, 49], + bgGreenBright: [102, 49], + bgYellowBright: [103, 49], + bgBlueBright: [104, 49], + bgMagentaBright: [105, 49], + bgCyanBright: [106, 49], + bgWhiteBright: [107, 49], + // Aliases, as in Node's util.inspect.colors. + blackBright: [90, 39], + bgBlackBright: [100, 49], + faint: [2, 22], + conceal: [8, 28], + strikeThrough: [9, 29], + crossedOut: [9, 29], + doubleUnderline: [21, 24], + swapColors: [7, 27], + }; + + // Node's ANSI/VT escape matcher (lib/internal/util.js): SGR colors, cursor moves, and + // OSC sequences such as hyperlinks. An OSC string may be terminated by BEL () or + // ST in either form (ESC \ = \, or the C1 byte œ) — Node accepts all + // three, so we do too. Reproduced so the stripped output is byte-identical to Node's. + // Built once; String.prototype.replace with a global regex resets lastIndex per call. + // A literal, not `new RegExp(...)`: util is LAZY, so a module-eval `RegExp` read here + // takes whatever user code left on globalThis (#426). Same pattern as Node's string + // form, its U+005C escape spelled as an escaped backslash (the same one character). + // Not `new P.RegExp` either: scripts/check-global-replace.mjs only recognises a bare + // `RegExp` callee or a literal, so that spelling hides the binding from that gate. + // prettier-ignore + var ansiRegex = /[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?(?:\u0007|\u001B\\|\u009C))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g; + + function stripVTControlCharacters(str) { + if (typeof str !== 'string') { + throw ERR_INVALID_ARG_TYPE('str', 'string', str); + } + // Short-circuit when there is no ESC (7-bit) or CSI (8-bit) introducer at all. + if (str.indexOf('\u001B') === -1 && str.indexOf('\u009B') === -1) return str; + return str.replace(ansiRegex, ''); + } + + function inspectPublic(v, opts) { + return inspect(v, opts, [], 0); + } + // util.inspect.colors is the live style table (styleText reads it); util.inspect.custom + // is the symbol a value implements to control its own inspection. + inspectPublic.colors = inspectColors; + if (customInspect) inspectPublic.custom = customInspect; + + module.exports = { + inspect: inspectPublic, + format: format, + formatWithOptions: formatWithOptions, + stripVTControlCharacters: stripVTControlCharacters, + inspectColors: inspectColors, + }; +}); diff --git a/pkg/runtime/js/node/util.js b/pkg/runtime/js/node/util.js index ababde1..d72ae4e 100644 --- a/pkg/runtime/js/node/util.js +++ b/pkg/runtime/js/node/util.js @@ -1,304 +1,26 @@ -// node:util — the subset Lava needs today: inspect() for readable value -// rendering, and format()/formatWithOptions() for printf-style substitution. -// Pure JS, no native dependencies. Ported in spirit from Node's -// lib/internal/util/inspect.js but trimmed to the common cases. +// node:util — the subset Lava needs today. Pure JS, no native dependencies. +// +// The implementation is split along node's lib/internal/util seams, so a throw from +// inside one names its file exactly as node's does: internal/util/inspect (inspect, +// format, formatWithOptions, stripVTControlCharacters, the inspect.colors table), +// internal/util/comparisons (isDeepStrictEqual), internal/util/debuglog (debuglog) and +// internal/util (promisify, deprecate). This file stays the public entry: it keeps what +// node's lib/util.js keeps — callbackify, inherits, styleText, toUSVString — plus +// aborted, and assembles the exports in their old order. util is lazy, and so is every +// file it requires. (function (require, module) { 'use strict'; - // Captured for debugEnvPattern below, which replaced three global-flag replaces on the - // NODE_DEBUG path. This file is not otherwise hardened — see the ratchet baseline. var UtilPrimordials = require('internal/primordials'); var errors = require('internal/errors'); var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; var ERR_INVALID_ARG_VALUE = errors.ERR_INVALID_ARG_VALUE; - var StringPrototypeCharCodeAt = UtilPrimordials.StringPrototypeCharCodeAt; - var StringPrototypeSlice = UtilPrimordials.StringPrototypeSlice; - var StringPrototypeToUpperCase = UtilPrimordials.StringPrototypeToUpperCase; - var StringPrototypeIndexOf = UtilPrimordials.StringPrototypeIndexOf; - var StringPrototypeReplaceAll = UtilPrimordials.StringPrototypeReplaceAll; - - // Through the primordials table rather than the live `Symbol` global: this file is - // evaluated lazily, so a module-eval read here is not the pre-user-code read §5's - // exemption assumes (the same reasoning that moved fs.js's Buffer capture to loader.js). - var SymbolFor = UtilPrimordials.SymbolFor; - var customInspect = SymbolFor ? SymbolFor('nodejs.util.inspect.custom') : null; - - // node PICKS the quote character to avoid escaping rather than always using single - // quotes. Its strEscape tests, in order: single by default; DOUBLE when the string - // contains a single quote but no double; BACKTICK when it contains both AND contains - // neither a backtick NOR the substring `${`; single-with-escapes otherwise. Verified on - // node 26.13.0 (probe, 2026-11-16): - // plain -> 'plain' it's -> "it's" - // say "hi" -> 'say "hi"' ' and " -> `both ' and "` - // ` and ' and " -> 'all ` \' " ' a'b"c${d} -> 'a\'b"c${d}' - // - // The `${` half is not cosmetic and an earlier revision omitted it: inspect output is - // meant to read back as a literal, and a backtick-delimited string containing `${` is a - // live template substitution. That revision also made this comment state the rule - // without it, so the comment agreed with the code and neither agreed with node. - // - // ESCAPES, matching node's strEscape exactly. Measured on node 26.13.0 across the whole - // C0 range, DEL/C1 and surrogates: - // \b \t \n \f \r the five with short forms — note 0x0B is `\x0B`, NOT `\v` - // other c < 0x20 `\xNN`, UPPERCASE hex - // 0x7F..0x9F `\xNN` (DEL and the C1 block); 0xA0 stays literal - // unpaired surrogate `\udXXX`, LOWERCASE hex — node ESCAPES it rather than - // replacing it with U+FFFD, so the value stays lossless - // a valid pair literal (an emoji prints as itself) - // - // This used to escape only the backslash and \n, which was a security gap and not a - // cosmetic one: an unescaped `\x1B` makes any line built with util.inspect an - // ANSI-injection vector, and a lone surrogate came back as U+FFFD — lossy, not merely - // rendered differently. Raised on #330 review; #331 keeps the remaining half, the - // missing maxArrayLength / maxStringLength caps. - // - // A code-unit loop rather than a regex, per §5, batching literal runs so the common case - // (nothing to escape) costs one slice instead of per-character concatenation. - var HEX_UPPER = '0123456789ABCDEF'; - var HEX_LOWER = '0123456789abcdef'; - function escByte(c) { - return '\\x' + HEX_UPPER[(c >> 4) & 0xf] + HEX_UPPER[c & 0xf]; - } - function escUnit(c) { - return ( - '\\u' + - HEX_LOWER[(c >> 12) & 0xf] + - HEX_LOWER[(c >> 8) & 0xf] + - HEX_LOWER[(c >> 4) & 0xf] + - HEX_LOWER[c & 0xf] - ); - } - - function quote(s) { - var q = "'"; - if (StringPrototypeIndexOf(s, "'") !== -1) { - if (StringPrototypeIndexOf(s, '"') === -1) q = '"'; - else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1) - q = '`'; - } - var delim = StringPrototypeCharCodeAt(q, 0); - var out = ''; - var runStart = 0; - for (var i = 0; i < s.length; i++) { - var c = StringPrototypeCharCodeAt(s, i); - var esc = null; - if (c === 0x5c) esc = '\\\\'; - // Only the single quote can ever need this: the other two delimiters are chosen - // precisely because they are absent from the string. - else if (c === delim) esc = '\\' + q; - else if (c === 0x08) esc = '\\b'; - else if (c === 0x09) esc = '\\t'; - else if (c === 0x0a) esc = '\\n'; - else if (c === 0x0c) esc = '\\f'; - else if (c === 0x0d) esc = '\\r'; - else if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) esc = escByte(c); - else if (c >= 0xd800 && c <= 0xdbff) { - // A high surrogate is literal only when a low one follows; otherwise it is lone. - var next = i + 1 < s.length ? StringPrototypeCharCodeAt(s, i + 1) : 0; - if (next >= 0xdc00 && next <= 0xdfff) { - i++; // consume the pair - } else { - esc = escUnit(c); - } - } else if (c >= 0xdc00 && c <= 0xdfff) { - // Reached only when no high surrogate preceded — a paired low was consumed above. - esc = escUnit(c); - } - if (esc !== null) { - out += StringPrototypeSlice(s, runStart, i) + esc; - runStart = i + 1; - } - } - return q + out + StringPrototypeSlice(s, runStart) + q; - } - - function inspect(v, opts, seen, depth) { - opts = opts || {}; - seen = seen || []; - depth = depth || 0; - var maxDepth = opts.depth === undefined ? 2 : opts.depth === null ? Infinity : opts.depth; - - var t = typeof v; - if (v === null) return 'null'; - if (t === 'undefined') return 'undefined'; - if (t === 'string') return quote(v); - if (t === 'number') return Object.is(v, -0) ? '-0' : String(v); - if (t === 'boolean') return String(v); - if (t === 'bigint') return String(v) + 'n'; - if (t === 'symbol') return v.toString(); - if (t === 'function') { - var fn = v.name; - return fn ? '[Function: ' + fn + ']' : '[Function (anonymous)]'; - } - // Honor the util.inspect custom hook (Buffer renders as ""). Node - // passes (depth, options, inspect) and formats whatever the hook returns: - // a string is used verbatim, anything else (number, object, …) is inspected - // in its place, and returning the object itself falls through to default - // formatting. - if (customInspect && typeof v[customInspect] === 'function') { - var nested = seen.concat([v]); - var recurse = function (val, o) { - return inspect(val, o || opts, nested, depth + 1); - }; - var custom = v[customInspect](depth, opts, recurse); - if (custom !== v) { - return typeof custom === 'string' ? custom : inspect(custom, opts, nested, depth); - } - } - if (seen.indexOf(v) !== -1) return '[Circular *1]'; - if (v instanceof Error) return v.stack ? v.stack : v.name + ': ' + v.message; - if (v instanceof Date) return isNaN(v.getTime()) ? 'Invalid Date' : v.toISOString(); - if (v instanceof RegExp) return v.toString(); - if (depth > maxDepth) return Array.isArray(v) ? '[Array]' : '[Object]'; - seen = seen.concat([v]); - - if (Array.isArray(v)) { - if (v.length === 0) return '[]'; - var arr = []; - for (var i = 0; i < v.length; i++) arr.push(inspect(v[i], opts, seen, depth + 1)); - return '[ ' + arr.join(', ') + ' ]'; - } - if (typeof Map !== 'undefined' && v instanceof Map) { - var mp = []; - v.forEach(function (val, key) { - mp.push(inspect(key, opts, seen, depth + 1) + ' => ' + inspect(val, opts, seen, depth + 1)); - }); - return 'Map(' + v.size + ') {' + (mp.length > 0 ? ' ' + mp.join(', ') + ' ' : '') + '}'; - } - if (typeof Set !== 'undefined' && v instanceof Set) { - var st = []; - v.forEach(function (val) { - st.push(inspect(val, opts, seen, depth + 1)); - }); - return 'Set(' + v.size + ') {' + (st.length > 0 ? ' ' + st.join(', ') + ' ' : '') + '}'; - } - - var keys = Object.keys(v); - var ctor = v.constructor && v.constructor.name; - var prefix = ctor && ctor !== 'Object' ? ctor + ' ' : ''; - if (keys.length === 0) return prefix + '{}'; - var props = []; - for (var k = 0; k < keys.length; k++) { - var key = keys[k]; - var name = /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : quote(key); - props.push(name + ': ' + inspect(v[key], opts, seen, depth + 1)); - } - return prefix + '{ ' + props.join(', ') + ' }'; - } - - function stringify(v) { - return typeof v === 'string' ? v : inspect(v, {}, [], 1); - } - - function formatWithOptions(opts, _f) { - var args = Array.prototype.slice.call(arguments, 1); - if (args.length === 0) return ''; - var first = args[0]; - // Node's short circuit, and it must come BEFORE the substitution loop rather - // than inside it: a lone string argument is the answer, directives and all. - // `format('%s')`, `format('%z')`, `format('100%')` and `format('%%')` all come - // back untouched on node 26.13.0 (verified by - // tests/node-compat/cases/57-format-single-arg.js, not read off the docs). - // Only `%%` used to get this wrong here — `%s` and friends were saved further - // down by the "arguments exhausted" branch, which re-emits the directive, while - // `%%` was folded unconditionally. - // `typeof first === 'string'` matters: `format(5, '%%')` has no format string - // at all, so node inspects and space-joins, leaving the later `%%` literal. - if (args.length === 1 && typeof first === 'string') return first; - var i = 1; - var result; - if (typeof first === 'string' && first.indexOf('%') !== -1) { - result = ''; - for (var p = 0; p < first.length; p++) { - var ch = first.charAt(p); - if (ch === '%' && p + 1 < first.length) { - var spec = first.charAt(p + 1); - if (spec === '%') { - result += '%'; - p++; - continue; - } - if ('sdifjoOc'.indexOf(spec) !== -1) { - if (i >= args.length) { - result += '%' + spec; - p++; - continue; - } - var a = args[i++]; - p++; - if (spec === 's') result += typeof a === 'string' ? a : stringify(a); - else if (spec === 'd' || spec === 'i') - result += - typeof a === 'bigint' - ? String(a) + 'n' - : typeof a === 'symbol' - ? 'NaN' - : String(Math.trunc(Number(a))); - else if (spec === 'f') result += typeof a === 'symbol' ? 'NaN' : String(parseFloat(a)); - else if (spec === 'j') { - try { - result += JSON.stringify(a); - } catch { - result += '[Circular]'; - } - } else if (spec === 'o' || spec === 'O') result += inspect(a, opts, [], 1); - /* spec === "c": CSS directive, consumed and ignored */ - continue; - } - } - result += ch; - } - } else { - result = stringify(first); - } - for (; i < args.length; i++) result += ' ' + stringify(args[i]); - return result; - } - - function format(_f) { - return formatWithOptions.apply(null, [{}].concat(Array.prototype.slice.call(arguments))); - } - - // util.promisify — wrap a (…args, callback(err, value)) function as one returning a - // Promise. Honors the util.promisify.custom symbol and copies the original's own - // properties / prototype, matching Node (the multi-arg `customArgs` form is omitted). - // Guarded like customInspect above, on the same capture: an unguarded call here made a - // missing SymbolFor throw inside util's factory and masked what the 57-format-single-arg - // mutation pins (the inspect hook rendering a Buffer). - var kCustomPromisify = SymbolFor ? SymbolFor('nodejs.util.promisify.custom') : null; - - function promisify(original) { - if (typeof original !== 'function') { - throw new TypeError('The "original" argument must be of type function'); - } - if (original[kCustomPromisify]) { - var custom = original[kCustomPromisify]; - if (typeof custom !== 'function') { - throw new TypeError('The "util.promisify.custom" property must be of type function'); - } - return custom; - } - function fn() { - var args = Array.prototype.slice.call(arguments); - var self = this; - return new Promise(function (resolve, reject) { - args.push(function (err, value) { - if (err) reject(err); - else resolve(value); - }); - Reflect.apply(original, self, args); - }); - } - Object.setPrototypeOf(fn, Object.getPrototypeOf(original)); - Object.defineProperty(fn, kCustomPromisify, { - value: fn, - enumerable: false, - writable: false, - configurable: true, - }); - return Object.defineProperties(fn, Object.getOwnPropertyDescriptors(original)); - } - promisify.custom = kCustomPromisify; + var inspectModule = require('internal/util/inspect'); + // The live table util.inspect.colors exposes: styleText reads it per call, so a + // caller-added style is honored. Same object, not a copy. + var inspectColors = inspectModule.inspectColors; + var internalUtil = require('internal/util'); + var debuglog = require('internal/util/debuglog').debuglog; // util.callbackify — the inverse: wrap an async/Promise-returning function as one // taking a Node-style (err, value) callback. Rejections are delivered on a fresh tick; @@ -352,243 +74,6 @@ Object.setPrototypeOf(ctor.prototype, superCtor.prototype); } - // util.deprecate — return a wrapper that emits a one-time deprecation warning, then - // delegates to fn. Suppressed entirely when process.noDeprecation is set. - function deprecate(fn, msg, code) { - if (process.noDeprecation === true) return fn; - var warned = false; - function deprecated() { - if (!warned) { - warned = true; - if (typeof process.emitWarning === 'function') { - process.emitWarning(msg, 'DeprecationWarning', code); - } else { - console.error('DeprecationWarning: ' + msg); - } - } - return Reflect.apply(fn, this, arguments); - } - return deprecated; - } - - // util.isDeepStrictEqual — structural strict equality, matching Node's algorithm: - // SameValue-ish primitives (NaN equal, +0 !== -0), matching prototype + [[Class]] tag, - // special handling for Date/RegExp/Error/boxed primitives/Map/Set/ArrayBuffer/TypedArray/ - // DataView, recursive comparison of own enumerable string + symbol keys, and circular refs. - var objTag = function (v) { - return Object.prototype.toString.call(v); - }; - - function bytesEqual(x, y) { - if (x.length !== y.length) return false; - for (var i = 0; i < x.length; i++) if (x[i] !== y[i]) return false; - return true; - } - - function ownEnumerableKeys(o) { - var keys = Object.keys(o); - var syms = Object.getOwnPropertySymbols(o); - for (var i = 0; i < syms.length; i++) { - if (Object.prototype.propertyIsEnumerable.call(o, syms[i])) keys.push(syms[i]); - } - return keys; - } - - function keysEqual(a, b, aStack, bStack) { - var aKeys = ownEnumerableKeys(a); - if (aKeys.length !== ownEnumerableKeys(b).length) return false; - for (var i = 0; i < aKeys.length; i++) { - var k = aKeys[i]; - if (!Object.prototype.hasOwnProperty.call(b, k)) return false; - if (!deepStrict(a[k], b[k], aStack, bStack)) return false; - } - return true; - } - - // Map/Set equality as node's comparisons.js does it in strict mode: a primitive key - // (or Set member) can only deep-equal the identical primitive, so it is looked up - // directly with b.has/b.get — O(n). Only object keys/members need the pairwise - // deepStrict scan, and only against b's object keys. The pairwise scan over every - // entry was O(n^2): one assert.deepStrictEqual on two equal 16k int->int Maps took - // 2179 ms that way and 4 ms this way (medians of 5 interleaved launches, darwin arm64). - // Sizes are already known equal, and Map/Set normalize -0 and match NaN via - // SameValueZero, which is what deepStrict answers for those keys anyway. - function collectionEqual(a, b, isMap, aStack, bStack) { - var aObjects = []; - var ok = true; - a.forEach(function (v, k) { - if (!ok) return; - var key = isMap ? k : v; - if (typeof key === 'object' && key !== null) { - aObjects.push([key, v]); - } else if (!b.has(key)) { - ok = false; - } else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) { - ok = false; - } - }); - if (!ok) return false; - if (aObjects.length === 0) return true; - var bObjects = []; - b.forEach(function (v, k) { - var key = isMap ? k : v; - if (typeof key === 'object' && key !== null) bObjects.push([key, v]); - }); - if (aObjects.length !== bObjects.length) return false; - var used = new Array(bObjects.length); - for (var i = 0; i < aObjects.length; i++) { - var found = false; - for (var j = 0; j < bObjects.length; j++) { - if (used[j]) continue; - var keyOk = deepStrict(aObjects[i][0], bObjects[j][0], aStack, bStack); - var valOk = !isMap || deepStrict(aObjects[i][1], bObjects[j][1], aStack, bStack); - if (keyOk && valOk) { - used[j] = true; - found = true; - break; - } - } - if (!found) return false; - } - return true; - } - - function deepStrict(a, b, aStack, bStack) { - if (a === b) return a !== 0 || Object.is(a, b); // +0 !== -0; everything else equal - if (typeof a !== 'object' || a === null) { - // primitive (or null) — equal only if both are NaN - return typeof a === 'number' && a !== a && typeof b === 'number' && b !== b; - } - if (typeof b !== 'object' || b === null) return false; - if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false; - - var tag = objTag(a); - if (tag !== objTag(b)) return false; - - switch (tag) { - case '[object Number]': - case '[object Boolean]': - case '[object String]': - case '[object BigInt]': - case '[object Symbol]': { - var av = a.valueOf(); - var bv = b.valueOf(); - if (av === bv) return av !== 0 || Object.is(av, bv); - return typeof av === 'number' && av !== av && bv !== bv; - } - case '[object Date]': - return Object.is(a.getTime(), b.getTime()); - case '[object RegExp]': - if (a.source !== b.source || a.flags !== b.flags) return false; - break; - case '[object Error]': - if (a.name !== b.name || a.message !== b.message) return false; - break; - case '[object ArrayBuffer]': - if (a.byteLength !== b.byteLength) return false; - return bytesEqual(new Uint8Array(a), new Uint8Array(b)); - case '[object DataView]': - if (a.byteLength !== b.byteLength) return false; - return bytesEqual( - new Uint8Array(a.buffer, a.byteOffset, a.byteLength), - new Uint8Array(b.buffer, b.byteOffset, b.byteLength), - ); - } - - // Typed arrays: element-wise (NaN-aware); then fall through to compare any extra keys. - if (ArrayBuffer.isView(a) && tag !== '[object DataView]') { - if (a.length !== b.length) return false; - for (var t = 0; t < a.length; t++) { - if (!(a[t] === b[t] || (a[t] !== a[t] && b[t] !== b[t]))) return false; - } - } - - // Circular-reference guard. - for (var s = 0; s < aStack.length; s++) { - if (aStack[s] === a) return bStack[s] === b; - } - aStack.push(a); - bStack.push(b); - - var result; - if (tag === '[object Map]' || tag === '[object Set]') { - result = a.size === b.size && collectionEqual(a, b, tag === '[object Map]', aStack, bStack); - } else { - result = keysEqual(a, b, aStack, bStack); - } - - aStack.pop(); - bStack.pop(); - return result; - } - - function isDeepStrictEqual(a, b) { - return deepStrict(a, b, [], []); - } - - // The live color table backing both util.inspect.colors and util.styleText. Exposed as - // util.inspect.colors below, so a caller can add or change a style and have styleText - // pick it up (Node parity). Null-prototype, so a key like 'constructor' resolves to - // undefined rather than an inherited function. Names map to [openCode, closeCode]. - var inspectColors = { - __proto__: null, - reset: [0, 0], - bold: [1, 22], - dim: [2, 22], - italic: [3, 23], - underline: [4, 24], - blink: [5, 25], - inverse: [7, 27], - hidden: [8, 28], - strikethrough: [9, 29], - doubleunderline: [21, 24], - black: [30, 39], - red: [31, 39], - green: [32, 39], - yellow: [33, 39], - blue: [34, 39], - magenta: [35, 39], - cyan: [36, 39], - white: [37, 39], - bgBlack: [40, 49], - bgRed: [41, 49], - bgGreen: [42, 49], - bgYellow: [43, 49], - bgBlue: [44, 49], - bgMagenta: [45, 49], - bgCyan: [46, 49], - bgWhite: [47, 49], - framed: [51, 54], - overlined: [53, 55], - gray: [90, 39], - grey: [90, 39], - redBright: [91, 39], - greenBright: [92, 39], - yellowBright: [93, 39], - blueBright: [94, 39], - magentaBright: [95, 39], - cyanBright: [96, 39], - whiteBright: [97, 39], - bgGray: [100, 49], - bgGrey: [100, 49], - bgRedBright: [101, 49], - bgGreenBright: [102, 49], - bgYellowBright: [103, 49], - bgBlueBright: [104, 49], - bgMagentaBright: [105, 49], - bgCyanBright: [106, 49], - bgWhiteBright: [107, 49], - // Aliases, as in Node's util.inspect.colors. - blackBright: [90, 39], - bgBlackBright: [100, 49], - faint: [2, 22], - conceal: [8, 28], - strikeThrough: [9, 29], - crossedOut: [9, 29], - doubleUnderline: [21, 24], - swapColors: [7, 27], - }; - function styleText(format, text, options) { if (typeof text !== 'string') { throw ERR_INVALID_ARG_TYPE('text', 'string', text); @@ -631,28 +116,6 @@ return open + text + close; } - // Node's ANSI/VT escape matcher (lib/internal/util.js): SGR colors, cursor moves, and - // OSC sequences such as hyperlinks. An OSC string may be terminated by BEL () or - // ST in either form (ESC \ = \, or the C1 byte œ) — Node accepts all - // three, so we do too. Reproduced so the stripped output is byte-identical to Node's. - // Built once; String.prototype.replace with a global regex resets lastIndex per call. - // A literal, not `new RegExp(...)`: util is LAZY, so a module-eval `RegExp` read here - // takes whatever user code left on globalThis (#426). Same pattern as Node's string - // form, its U+005C escape spelled as an escaped backslash (the same one character). - // Not `new P.RegExp` either: scripts/check-global-replace.mjs only recognises a bare - // `RegExp` callee or a literal, so that spelling hides the binding from that gate. - // prettier-ignore - var ansiRegex = /[\u001B\u009B][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d/#&.:=?%@~_]*)*)?(?:\u0007|\u001B\\|\u009C))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))/g; - - function stripVTControlCharacters(str) { - if (typeof str !== 'string') { - throw ERR_INVALID_ARG_TYPE('str', 'string', str); - } - // Short-circuit when there is no ESC (7-bit) or CSI (8-bit) introducer at all. - if (str.indexOf('\u001B') === -1 && str.indexOf('\u009B') === -1) return str; - return str.replace(ansiRegex, ''); - } - // util.toUSVString — coerce to a string of Unicode scalar values, replacing each lone // surrogate with U+FFFD. Matches Node: it string-coerces via `${value}` (the string hint, // so an object's toString wins over valueOf — `'' + value` would use the default hint and @@ -681,142 +144,6 @@ return out; } - // --- util.debuglog / util.debug --- - // NODE_DEBUG is a comma-separated list of sections (no whitespace trimming); `*` is a - // wildcard and matching is case-insensitive. Node compiles it once into an anchored regex - // (escape regex specials, then `*` -> `.*`, `,` -> alternation) tested against the - // upper-cased section name. - // debugEnvPattern compiles NODE_DEBUG into the body of that anchored regex in ONE - // pass, where Node (and this file, before) chained three global replaces: - // - // .replace(/[|\\{}()[\]^$+?.]/g, '\\$&') escape the regex specials - // .replace(/\*/g, '.*') '*' is the wildcard - // .replace(/,/g, '$|^') ',' separates sections - // - // Three global replaces are three spin-forever sites: under a forged - // `RegExp.prototype.exec` a global replace never advances `lastIndex`, so it does not - // answer wrongly, it never returns — and this one runs on the FIRST debuglog call, so - // the process wedges at the point it tries to emit a diagnostic. node answers - // correctly under the same poison, so it was a Lava-only defect; pinned by - // tests/node-compat/cases/59-global-replace-hangs.js. - // - // Collapsing the three passes into one is sound because they cannot feed each other: - // the escape set contains neither `*` nor `,`, so those survive step 1 untouched, and - // neither `.*` nor `$|^` introduces a character a later step would rewrite. - function debugEnvPattern(env) { - var out = ''; - for (var i = 0; i < env.length; i++) { - var c = StringPrototypeCharCodeAt(env, i); - if (c === 0x2a) { - out += '.*'; // '*' wildcard - } else if (c === 0x2c) { - out += '$|^'; // ',' section separator - } else if (isRegExpSpecial(c)) { - out += '\\' + StringPrototypeSlice(env, i, i + 1); - } else { - out += StringPrototypeSlice(env, i, i + 1); - } - } - return StringPrototypeToUpperCase(out); - } - - // The `[|\\{}()[\]^$+?.]` class, as code units. `*` and `,` are deliberately absent — - // they are the two the caller rewrites rather than escapes. - function isRegExpSpecial(c) { - return ( - c === 0x7c || // | - c === 0x5c || // \ - c === 0x7b || // { - c === 0x7d || // } - c === 0x28 || // ( - c === 0x29 || // ) - c === 0x5b || // [ - c === 0x5d || // ] - c === 0x5e || // ^ - c === 0x24 || // $ - c === 0x2b || // + - c === 0x3f || // ? - c === 0x2e // . - ); - } - - var debugEnvRegex = /^$/; - var debugEnvInitialized = false; - function initDebugEnv() { - if (debugEnvInitialized) return; - debugEnvInitialized = true; - var env = typeof process !== 'undefined' && process.env ? process.env.NODE_DEBUG : ''; - if (env) { - debugEnvRegex = new RegExp('^' + debugEnvPattern(env) + '$', 'i'); - } - } - function testEnabled(section) { - initDebugEnv(); - return debugEnvRegex.exec(section) !== null; - } - - var debugImpls = { __proto__: null }; - function debuglogImpl(enabled, section) { - if (debugImpls[section] === undefined) { - if (enabled) { - debugImpls[section] = function () { - var args = [{}]; - for (var i = 0; i < arguments.length; i++) args[i + 1] = arguments[i]; - var msg = formatWithOptions.apply(null, args); - var pid = typeof process !== 'undefined' && process.pid ? process.pid : 0; - process.stderr.write(format('%s %d: %s\n', section, pid, msg)); - }; - } else { - debugImpls[section] = function () {}; - } - } - return debugImpls[section]; - } - - function debuglog(set, cb) { - var enabled; - function init() { - set = String(set).toUpperCase(); - enabled = testEnabled(set); - } - // The heavy impl is built lazily on the first call; the optional callback is invoked then - // (once) with the real logger so callers can capture/replace it. - var debug = function () { - init(); - debug = debuglogImpl(enabled, set); - if (typeof cb === 'function') { - Object.defineProperty(debug, 'enabled', { - get: function () { - return enabled; - }, - configurable: true, - enumerable: true, - }); - cb(debug); - } - return debug.apply(this, arguments); - }; - var test = function () { - init(); - test = function () { - return enabled; - }; - return enabled; - }; - var logger = function () { - if (enabled === false) return; - return debug.apply(this, arguments); - }; - Object.defineProperty(logger, 'enabled', { - get: function () { - return test(); - }, - configurable: true, - enumerable: true, - }); - return logger; - } - function isAbortSignalLike(s) { return ( s !== null && @@ -856,23 +183,15 @@ }); } - function inspectPublic(v, opts) { - return inspect(v, opts, [], 0); - } - // util.inspect.colors is the live style table (styleText reads it); util.inspect.custom - // is the symbol a value implements to control its own inspection. - inspectPublic.colors = inspectColors; - if (customInspect) inspectPublic.custom = customInspect; - module.exports = { - inspect: inspectPublic, - format: format, - formatWithOptions: formatWithOptions, - promisify: promisify, + inspect: inspectModule.inspect, + format: inspectModule.format, + formatWithOptions: inspectModule.formatWithOptions, + promisify: internalUtil.promisify, callbackify: callbackify, inherits: inherits, - deprecate: deprecate, - isDeepStrictEqual: isDeepStrictEqual, + deprecate: internalUtil.deprecate, + isDeepStrictEqual: require('internal/util/comparisons').isDeepStrictEqual, debuglog: debuglog, debug: debuglog, // Node aliases util.debug to util.debuglog aborted: aborted, @@ -881,7 +200,7 @@ MIMEType: require('internal/mime').MIMEType, MIMEParams: require('internal/mime').MIMEParams, styleText: styleText, - stripVTControlCharacters: stripVTControlCharacters, + stripVTControlCharacters: inspectModule.stripVTControlCharacters, toUSVString: toUSVString, // Node exposes the same object via require('node:util').types and // require('node:util/types'); many packages use the former. diff --git a/scripts/lib/global-replace-detect.mjs b/scripts/lib/global-replace-detect.mjs index 05c93af..cd20bd5 100644 --- a/scripts/lib/global-replace-detect.mjs +++ b/scripts/lib/global-replace-detect.mjs @@ -39,7 +39,7 @@ const PRIMORDIAL_RE = /^StringPrototype(Replace|ReplaceAll|Match|MatchAll|Split| // to ignore a hang alarm — three moves in one PR (#330: 572→607→609→660). const ALLOWED = new Map([ [ - 'node/util.js:ansiRegex', + 'internal/util/inspect.js:ansiRegex', 'stripVTControlCharacters: the ANSI pattern is two alternations (OSC terminated by ' + 'BEL/ST, CSI terminated by a final byte), not a character class, so it has no ' + 'code-unit rewrite. node hangs on it identically, so Lava is not diverging. ' + diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index df468c9..497f511 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -207,7 +207,7 @@ { "name": "57-format-single-arg dies when util.js drops the single-argument guard", "why": "Without it util.format folds %% even with one argument, where node returns the string verbatim. Pinned separately from console.js because the two are different implementations and one entry would fake coverage of both.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " if (args.length === 1 && typeof first === 'string') return first;\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -447,7 +447,7 @@ { "name": "regexp_pollution_immunity debuglog dies when NODE_DEBUG is compiled by chained global replaces", "why": "Three global replaces run on the FIRST debuglog call, so the process wedges at the point it tries to emit a diagnostic. Lava-only because Lava has no process.stderr, so the outputs cannot match node however the spin is fixed.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/debuglog.js", "find": " return StringPrototypeToUpperCase(out);", "replace": " return env.replace(/[|\\\\{}()[\\]^$+?.]/g, '\\\\$&').replace(/\\*/g, '.*').replace(/,/g, '$|^').toUpperCase();", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -486,7 +486,7 @@ { "name": "regexp_pollution_immunity dbg-nodot dies when the NODE_DEBUG escaper stops escaping '.'", "why": "Nothing observed debugEnvPattern's OUTPUT before: the poisoned probe passes with NODE_DEBUG unset, i.e. when the function is never called. Dropping the '.' branch makes 'a.b' enable 'axb', which is the escape the compile exists to do.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/debuglog.js", "find": " c === 0x2e // .", "replace": " false // .", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -639,7 +639,7 @@ { "name": "57-format-single-arg dies when util.inspect stops quoting a top-level string", "why": "node's util.inspect QUOTES a top-level string ('x' -> \"'x'\") while console.log/format print a string ARGUMENT raw — format goes through stringify(), which short-circuits strings before inspect is reached, so the depth-0 raw arm had no caller left and was deleted. Restoring it un-quotes every message built with util.inspect, which is how fs.js's ERR_INVALID_ARG_VALUE read `Received bogus`.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " if (t === 'string') return quote(v);", "replace": " if (t === 'string') return depth === 0 ? v : quote(v);", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -650,7 +650,7 @@ { "name": "57-format-single-arg dies when inspect stops choosing the quote character", "why": "node picks the delimiter to avoid escaping — a string containing a single quote is rendered with DOUBLE quotes (\"it's\", not 'it\\'s'). Always using single quotes escapes where node does not, so any inspected value containing an apostrophe differs.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " if (StringPrototypeIndexOf(s, \"'\") !== -1) {\n if (StringPrototypeIndexOf(s, '\"') === -1) q = '\"';\n else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';\n }\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -773,7 +773,7 @@ { "name": "57-format-single-arg dies when inspect drops node's ${ guard", "why": "node's strEscape refuses the backtick delimiter when the string contains '${', because inspect output is meant to read back as a literal and a backtick-delimited '${' is a live template substitution. Omitting the guard also regressed output that was CORRECT before the delimiter rule was introduced.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';", "replace": " else if (StringPrototypeIndexOf(s, '`') === -1) q = '`';", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -784,7 +784,7 @@ { "name": "57-format-single-arg dies when the backtick branch is removed", "why": "A string containing both quote characters is backtick-delimited on node. The earlier manifest entry deleted the whole delimiter block, so it only proved the double-quote arm; deleting just this line survived the case.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " else if (StringPrototypeIndexOf(s, '`') === -1 && StringPrototypeIndexOf(s, '${') === -1)\n q = '`';\n", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -795,7 +795,7 @@ { "name": "57-format-single-arg dies when inspect stops escaping control characters", "why": "node's strEscape escapes the whole C0 range, DEL and C1. Leaving them raw makes an unescaped \\x1B in any inspected value an ANSI-injection vector through the log line, which is why this is a security fix and not a rendering nicety.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " else if (c < 0x20 || (c >= 0x7f && c <= 0x9f)) esc = escByte(c);", "replace": "", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -806,7 +806,7 @@ { "name": "57-format-single-arg dies when a lone surrogate is not escaped", "why": "node escapes an unpaired surrogate as \\\\udXXX; Lava used to hand it to the string conversion, which replaced it with U+FFFD — the value came back LOSSY, not merely rendered differently.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " if (next >= 0xdc00 && next <= 0xdfff) {\n i++; // consume the pair\n } else {\n esc = escUnit(c);\n }", "replace": " if (next >= 0xdc00 && next <= 0xdfff) {\n i++; // consume the pair\n }", "gate": "compat:tests/node-compat/cases/57-format-single-arg.js", @@ -1823,7 +1823,7 @@ { "name": "71-assert-deep-strict-equal dies when a primitive Map key's values are not compared", "why": "the O(n) b.get fast path must still deep-compare the values; skipping it makes Maps with equal keys and different values equal.", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/comparisons.js", "find": "} else if (isMap && !deepStrict(v, b.get(key), aStack, bStack)) {", "replace": "} else if (false) {", "gate": "compat:tests/node-compat/cases/71-assert-deep-strict-equal.js", @@ -2139,7 +2139,7 @@ { "name": "75-lazy-global-capture dies when util.js reads Symbol.for live for promisify.custom", "why": "`Symbol.for` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util.js", "find": " var kCustomPromisify = SymbolFor ? SymbolFor('nodejs.util.promisify.custom') : null;", "replace": " var kCustomPromisify = Symbol.for('nodejs.util.promisify.custom');", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", @@ -2148,7 +2148,7 @@ { "name": "75-lazy-global-capture dies when util.js builds the ANSI matcher through the live RegExp", "why": "`RegExp` is read at module-eval of a LAZY module, i.e. on the user's first require \u2014 after user code could replace the global. Node takes it from primordials; measured on node 26.13.0 a spy on the global logged nothing, here it logged the read (#426).", - "source": "pkg/runtime/js/node/util.js", + "source": "pkg/runtime/js/internal/util/inspect.js", "find": " var ansiRegex = /", "replace": " var ansiRegex = new RegExp('(?:)') && /", "gate": "compat:tests/node-compat/cases/75-lazy-global-capture.js", @@ -2534,6 +2534,24 @@ "replace": "", "gate": "compat:tests/node-compat/cases/83-crypto-internal-modules.js", "expect_detail": "node: globalThis.crypto object true function function true true" + }, + { + "name": "84-util-internal-modules dies when util.debug stops being util.debuglog", + "why": "debuglog now lives in internal/util/debuglog and node/util exports it twice; an entry that wraps one of the two spellings hands user code a util.debug that is not util.debuglog, where node exports the one function under both names.", + "source": "pkg/runtime/js/node/util.js", + "find": " debug: debuglog, // Node aliases util.debug to util.debuglog", + "replace": " debug: function debug(set, cb) {\n return debuglog(set, cb);\n }, // Node aliases util.debug to util.debuglog", + "gate": "compat:tests/node-compat/cases/84-util-internal-modules.js", + "expect_detail": "lava: identities false true true true" + }, + { + "name": "84-util-internal-modules dies when styleText reads a copy of util.inspect.colors", + "why": "styleText (node/util) and the colors table (internal/util/inspect) now meet across a file seam; a copied table freezes the style set at util load, so a style a caller adds to util.inspect.colors is rejected by styleText instead of applied, as node applies it.", + "source": "pkg/runtime/js/node/util.js", + "find": " var inspectColors = inspectModule.inspectColors;", + "replace": " var inspectColors = Object.assign({ __proto__: null }, inspectModule.inspectColors);", + "gate": "compat:tests/node-compat/cases/84-util-internal-modules.js", + "expect_detail": "node: identities true true true true" } ] } diff --git a/tests/node-compat/cases/57-format-single-arg.js b/tests/node-compat/cases/57-format-single-arg.js index 2ad53cf..c40245f 100644 --- a/tests/node-compat/cases/57-format-single-arg.js +++ b/tests/node-compat/cases/57-format-single-arg.js @@ -11,7 +11,7 @@ // 31-row contract probe diverged, all of them `%%`. // // TWO implementations are covered on purpose. `util.format` runs -// node/util.js's formatWithOptions; `console.log` runs console.js's own +// internal/util/inspect.js's formatWithOptions; `console.log` runs console.js's own // formatArgs, because console.js is the bootstrap prelude — it receives native // write functions and has no `require`, so it cannot delegate. A test through // `util` alone would leave console.js unpinned, and the two are pinned by separate diff --git a/tests/node-compat/cases/84-util-internal-modules.js b/tests/node-compat/cases/84-util-internal-modules.js new file mode 100644 index 0000000..83d68f8 --- /dev/null +++ b/tests/node-compat/cases/84-util-internal-modules.js @@ -0,0 +1,165 @@ +// node:util is assembled from internal modules laid out like node's lib/internal/util: +// js/internal/util/{inspect,comparisons,debuglog}.js and js/internal/util.js hold the +// implementation and node/util stays the public entry. A throw from inside inspect or +// isDeepStrictEqual used to carry only `node:util` frames, because everything was +// defined in util.js; now it names its file, exactly as node's does. +// +// Each frame line prints `ok` when the stack carries `node::` and names the first +// `node:` frame it got otherwise. Only the source name is compared, never the message, +// line or column. callbackify, inherits, styleText and toUSVString stay in node/util on +// both runtimes (lib/util.js on node), so their lines check `node:util:` instead. +// deprecate is not probed: a throw through its wrapper carries no `node:` frame on +// Lava. Every site throws synchronously on both runtimes (probed on node 26.13.0). +// +// The new ids must stay internal: neither the bare nor the `node:` spelling may resolve +// from user code. Only the error CODE of the bare spelling is printed (the messages +// diverge, #415). +// +// The export lines check identities that cross the new file seams (util.debug is +// util.debuglog; styleText reads the very table util.inspect.colors exposes; util.types +// is node:util/types). Export ORDER is not oracle-able — node's key list is a different +// set in a different order — so the split was checked against the previous binary +// instead (the PR records the diff). `aborted` is an accessor on node and a data +// property on Lava, a pre-existing gap the split does not touch, so only its type is +// printed. +'use strict'; + +const util = require('util'); + +for (const id of [ + 'internal/util', + 'internal/util/inspect', + 'internal/util/comparisons', + 'internal/util/debuglog', + 'util/inspect', +]) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +function frame(label, file, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + const where = stack.includes('node:' + file + ':') + ? 'ok' + : 'got ' + (stack.match(/node:[\w/]+/) || ['no node: frame'])[0]; + console.log(label + ': ' + file + ' ' + where); +} + +const throwingHook = { + [util.inspect.custom]() { + throw new Error('hook'); + }, +}; +const throwingGetter = {}; +Object.defineProperty(throwingGetter, 'a', { + enumerable: true, + get() { + throw new Error('getter'); + }, +}); +frame('inspect hook', 'internal/util/inspect', () => util.inspect(throwingHook)); +frame('format %o hook', 'internal/util/inspect', () => util.format('%o', throwingHook)); +frame('stripVTControlCharacters str', 'internal/util/inspect', () => + util.stripVTControlCharacters(5), +); +frame('isDeepStrictEqual getter', 'internal/util/comparisons', () => + util.isDeepStrictEqual(throwingGetter, { a: 1 }), +); +frame( + 'debuglog section', + 'internal/util/debuglog', + () => + util.debuglog({ + toString() { + throw new Error('section'); + }, + }).enabled, +); +frame('promisify original', 'internal/util', () => util.promisify(5)); +frame('callbackify original', 'util', () => util.callbackify(5)); +frame('inherits ctor', 'util', () => util.inherits(5, 6)); +frame('styleText format', 'util', () => util.styleText('bogus', 'x')); +frame('toUSVString symbol', 'util', () => util.toUSVString(Symbol('s'))); + +// --- the assembled exports: shapes and identities ---------------------------------- +console.log('same module', require('node:util') === util); +const SHARED = [ + 'inspect', + 'format', + 'formatWithOptions', + 'promisify', + 'callbackify', + 'inherits', + 'deprecate', + 'isDeepStrictEqual', + 'debuglog', + 'debug', + 'parseArgs', + 'parseEnv', + 'MIMEType', + 'MIMEParams', + 'styleText', + 'stripVTControlCharacters', + 'toUSVString', + 'types', +]; +for (const k of SHARED) { + const d = Object.getOwnPropertyDescriptor(util, k); + console.log('export', k, typeof util[k], 'value' in d ? 'data' : 'accessor', d.enumerable); +} +console.log('export aborted', typeof util.aborted); +util.inspect.colors.lavaSplitStyle = [1, 22]; +console.log( + 'identities', + util.debug === util.debuglog, + util.types === require('util/types'), + util.inspect.custom === Symbol.for('nodejs.util.inspect.custom'), + util.promisify.custom === Symbol.for('nodejs.util.promisify.custom'), + JSON.stringify(util.styleText('lavaSplitStyle', 'x', { validateStream: false })), +); + +// --- behavior across the file seams ------------------------------------------------- +console.log(util.format('%s:%d:%j %O', 'a', 42, { b: 1 }, [1]), util.inspect("it's")); +console.log( + util.formatWithOptions({}, '%s', 'w'), + util.stripVTControlCharacters('\x1b[1mx\x1b[22m'), +); +console.log( + 'deep', + util.isDeepStrictEqual({ a: [1, new Map([[1, 2]])] }, { a: [1, new Map([[1, 2]])] }), + util.isDeepStrictEqual(0, -0), +); +console.log('debuglog', util.debuglog('lava_split_unset').enabled); + +const EventEmitter = require('events'); +function Legacy() { + EventEmitter.call(this); +} +util.inherits(Legacy, EventEmitter); +const legacy = new Legacy(); +legacy.on('ping', (v) => console.log('inherits', v, Legacy.super_ === EventEmitter)); +legacy.emit('ping', legacy instanceof EventEmitter); + +const promised = util.promisify((v, cb) => cb(null, v + 1)); +const callbacked = util.callbackify(async (v) => v * 2); +promised(1).then((v) => { + console.log('promisify', v); + callbacked(4, (err, r) => console.log('callbackify', err, r)); +}); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index ab76da1..869538b 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -245,6 +245,30 @@ "accessor": 0, "global": 14 }, + "internal/util.js": { + "method": 1, + "invoke": 3, + "accessor": 0, + "global": 11 + }, + "internal/util/comparisons.js": { + "method": 12, + "invoke": 3, + "accessor": 10, + "global": 16 + }, + "internal/util/debuglog.js": { + "method": 2, + "invoke": 3, + "accessor": 0, + "global": 4 + }, + "internal/util/inspect.js": { + "method": 25, + "invoke": 3, + "accessor": 2, + "global": 22 + }, "internal/webstreams/adapters.js": { "method": 7, "invoke": 0, @@ -450,10 +474,10 @@ "global": 18 }, "node/util.js": { - "method": 46, - "invoke": 14, - "accessor": 12, - "global": 70 + "method": 6, + "invoke": 2, + "accessor": 0, + "global": 17 }, "node/util/types.js": { "method": 0, From b91fee21902a81c14c64762300532458f278b1e7 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:45:03 +0200 Subject: [PATCH 38/61] refactor(buffer): move node:buffer's codecs and numeric read/write to internal/buffer node/buffer.js 1456 -> 967 lines, mirroring node's lib/buffer.js + lib/internal/buffer.js seam. Prototype methods are installed at the same point, so key order, descriptors and identities are unchanged; pristineBuffer intact. Case 85 pins the layout. Co-Authored-By: Claude Opus 5.5 --- agents/prompts/pr-gate-reference/gates.md | 2 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 16 +- pkg/runtime/js/bootstrap/loader.js | 3 + pkg/runtime/js/internal/buffer.js | 563 ++++++++++++++++++ pkg/runtime/js/node/buffer.js | 521 +--------------- runtime/gates/route-gates.mjs | 4 + runtime/gates/route-gates.test.mjs | 6 + tests/mutation-manifest.json | 24 +- .../cases/85-buffer-internal-modules.js | 215 +++++++ tests/node-compat/pollution-baseline.json | 12 +- 11 files changed, 852 insertions(+), 516 deletions(-) create mode 100644 pkg/runtime/js/internal/buffer.js create mode 100644 tests/node-compat/cases/85-buffer-internal-modules.js diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 36d674f..7b01457 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -70,7 +70,7 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `crypto.odin`, `js/node/crypto.js`, `js/internal/crypto/*.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | | `pkg/runtime/js/**/*.js` (any under the scan root — `bootstrap/`, `node/`, `internal/`; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | | `js/node/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | -| `js/internal/encoding.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | +| `js/internal/{encoding,buffer}.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | | `scripts/**` | `make test-scripts` — node:test over the build tooling | | any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | | `pkg/runtime/globals.odin`, `runtime.odin`, `errors.odin` | `make test-odin`, `make test-compat-lava` | diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 3ccadb0..1ca1a20 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -103,7 +103,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/buffer", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 5b5e26e..2ebd87d 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -183,8 +183,9 @@ at its own top level, so the late edge is what keeps the graph acyclic), as node factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), -Web Streams (~2100 across `internal/webstreams/`), `buffer.js` (1456; `Blob`, `File` and the `blob:` URL registry -live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), fetch +Web Streams (~2100 across `internal/webstreams/`), buffer (~1530 across `node/buffer.js` and +`internal/buffer.js`; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` +and `internal/file.js`, as in node's `lib/internal`), fetch (~1160 across `internal/fetch.js` and `internal/fetch/`), crypto (~1070 across `node/crypto.js` and `internal/crypto/`). `node/stream.js` only assembles the public module; the classic streams live in `internal/streams/` (`legacy`, `readable`, `writable`, `duplex`, @@ -223,7 +224,16 @@ bindings through the same `loader.js` aliasing as http. All seven are eager thro and `internal/util` (`promisify`, `deprecate`). `node/util` keeps `callbackify`, `inherits`, `styleText` and `toUSVString` where node's `lib/util.js` has them, plus `aborted`, and assembles the exports in their old order. Util calls no native, so there -is no alias, and every file stays lazy behind `require('util')`. Errors use Node-shaped +is no alias, and every file stays lazy behind `require('util')`. Buffer takes node's one +seam: `internal/buffer` holds the encoding codecs (`normalizeEncoding`, `strToBytes`, +`bytesToString`) and `addBufferPrototypeMethods`, which installs the fixed-width +`read*`/`write*` accessors and the per-encoding `*Slice`/`*Write` aliases, so a bounds +error frames `node:internal/buffer` as node's does. `node/buffer` keeps the constructor, +the statics, the pool, the `lib/buffer.js` prototype methods and `atob`/`btoa`, calls the +installer where those methods were always assigned (the prototype's key order is +unchanged), and captures the codecs once at module-eval, so a `toString` pays the same +closure call as before. `internal/buffer` decodes through `node/buffer`'s bindings via the +same `loader.js` aliasing, and is eager through `req('buffer')`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 773fba9..974ad9c 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -39,6 +39,9 @@ natives['internal/crypto/pbkdf2'] = natives['node/crypto']; natives['internal/crypto/hkdf'] = natives['node/crypto']; natives['internal/crypto/scrypt'] = natives['node/crypto']; + // node:buffer's codecs and numeric accessors live in internal/buffer.js (node's + // lib/internal/buffer seam); it decodes through node:buffer's own bindings object. + natives['internal/buffer'] = natives['node/buffer']; // Prefix test by code unit. The specifier comes from user code on the public path, // so a String.prototype method here would be a live, replaceable read deciding diff --git a/pkg/runtime/js/internal/buffer.js b/pkg/runtime/js/internal/buffer.js new file mode 100644 index 0000000..7bf173b --- /dev/null +++ b/pkg/runtime/js/internal/buffer.js @@ -0,0 +1,563 @@ +/** + * @fileoverview node:internal/buffer — the encoding codecs and the fixed-width numeric + * accessors behind `Buffer.prototype`, where node keeps them: lib/internal/buffer.js. + * + * `addBufferPrototypeMethods` installs the read/write integer and float methods and the + * per-encoding `*Slice`/`*Write` aliases on the prototype node:buffer builds, at the + * point it always installed them, so the prototype key order is unchanged. The codec + * helpers (`normalizeEncoding`, `strToBytes`, `bytesToString`) are node:buffer's + * encoding ops; node/buffer.js captures them once at its own module-eval, so a + * `toString` or `write` pays a closure-variable call exactly as before the split, not + * a property read. Nothing here requires node:buffer back. + * + * @param {Object} native node:buffer's bindings (the loader hands this file the same + * object as node/buffer.js). + * @module lava/internal/buffer + */ + +(function (require, module, exports, native) { + 'use strict'; + + if (!native) throw new Error('node:buffer requires native codec bindings'); + + /** @param {string} name @returns {Function} */ + function requireNative(name) { + var fn = native[name]; + if (typeof fn !== 'function') throw new Error('node:buffer missing native binding: ' + name); + return fn; + } + + var utf8EncodeNative = requireNative('utf8Encode'); + var utf8DecodeNative = requireNative('utf8Decode'); + var hexEncodeNative = requireNative('hexEncode'); + var hexDecodeNative = requireNative('hexDecode'); + var base64EncodeNative = requireNative('base64Encode'); + var base64DecodeNative = requireNative('base64Decode'); + var base64urlEncodeNative = requireNative('base64urlEncode'); + var latin1EncodeNative = requireNative('latin1Encode'); + var latin1DecodeNative = requireNative('latin1Decode'); + var asciiDecodeNative = requireNative('asciiDecode'); + var utf16leEncodeNative = requireNative('utf16leEncode'); + var utf16leDecodeNative = requireNative('utf16leDecode'); + + var EMPTY_U8 = new Uint8Array(0); + + // Encoding-name resolution is hardened ahead of the rest of node:buffer because + // internal/encoding.js routes TextEncoder.encode and the TextDecoder utf-8 fast + // path through Buffer: a replaced `String` global or a poisoned + // `String.prototype.toLowerCase` reached a hardened TextDecoder through here and + // turned a valid decode into `Unknown encoding: …`. The rest of node:buffer is + // not yet converted (see tests/node-compat/pollution-baseline.json). + var P = require('internal/primordials'); + var StringG = String; + var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringFromCharCode = String.fromCharCode; + var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; + + /** + * @type {Object} lowercased name → canonical encoding. + * Null-prototype: the keys are caller-supplied, so with Object.prototype in the + * chain `Buffer.from(s, 'constructor')` resolved to a function instead of + * failing, and any `Object.prototype.` a script set forged an encoding. + */ + var ENCODING_ALIASES = { + __proto__: null, + utf8: 'utf8', + 'utf-8': 'utf8', + utf16le: 'utf16le', + 'utf-16le': 'utf16le', + ucs2: 'utf16le', + 'ucs-2': 'utf16le', + binary: 'latin1', + latin1: 'latin1', + ascii: 'ascii', + hex: 'hex', + base64: 'base64', + base64url: 'base64url', + }; + + // Node-shaped coded errors come from internal/errors (CLAUDE.md §5); what stays here + // are the buffer-only message templates. + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var ERR_OUT_OF_RANGE = errors.ERR_OUT_OF_RANGE; + var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; + + /** @returns {string} Canonical encoding name (default utf8). */ + function normalizeEncoding(encoding) { + if (encoding === undefined || encoding === null || encoding === '') return 'utf8'; + if (typeof encoding === 'symbol') return '' + encoding; // throws TypeError on Symbol, matching Node + var key = StringPrototypeToLowerCase(StringG(encoding)); + var canon = ENCODING_ALIASES[key]; + return canon !== undefined ? canon : key; + } + + /** @returns {boolean} */ + function isEncodingName(encoding) { + return ( + typeof encoding === 'string' && + ObjectPrototypeHasOwnProperty(ENCODING_ALIASES, StringPrototypeToLowerCase(encoding)) + ); + } + + /** + * Lenient Node base64/base64url normalization before native decode. + * + * A charCode filter, not `replaceAll(/[^A-Za-z0-9+/]/g, '')`. That was a GLOBAL + * regex replace, and a global replace under a forged `RegExp.prototype.exec` does + * not answer wrongly — it never returns, because `Symbol.replace` advances + * `lastIndex` only on an empty match. Measured: `Buffer.from('dXNlcjpwYXNz', + * 'base64')` hung indefinitely on `bin/lava` where node answered promptly, and + * this is the Basic-auth / JWT decode path, so one unauthenticated request with an + * `Authorization` header was enough to wedge the event loop for good. + * + * The `-`/`_` folds are plain string arguments, not regexes, so they were never + * part of the vector; they are folded into the same pass because the pass is free. + */ + function normalizeBase64(str) { + var src = StringG(str); + var n = src.length; + var out = ''; + for (var i = 0; i < n; i++) { + var c = StringPrototypeCharCodeAt(src, i); + if (c === 0x2d) { + c = 0x2b; // '-' -> '+' + } else if (c === 0x5f) { + c = 0x2f; // '_' -> '/' + } + if ( + (c >= 0x41 && c <= 0x5a) || // A-Z + (c >= 0x61 && c <= 0x7a) || // a-z + (c >= 0x30 && c <= 0x39) || // 0-9 + c === 0x2b || // + + c === 0x2f // / + ) { + out += StringFromCharCode(c); + } + } + if (out.length % 4 === 1) out = out.slice(0, out.length - 1); + while (out.length % 4 !== 0) out += '='; + return out; + } + + /** @returns {Uint8Array} */ + function strToBytes(str, encoding) { + encoding = normalizeEncoding(encoding); + str = StringG(str); + if (encoding === 'utf8') return utf8EncodeNative(str); + if (encoding === 'utf16le') return utf16leEncodeNative(str); + if (encoding === 'hex') return hexDecodeNative(str); + if (encoding === 'base64' || encoding === 'base64url') { + var norm = normalizeBase64(str); + return norm ? base64DecodeNative(norm) : EMPTY_U8; + } + if (encoding === 'ascii' || encoding === 'latin1') return latin1EncodeNative(str); + throw ERR_UNKNOWN_ENCODING(encoding); + } + + /** @returns {string} */ + function bytesToString(bytes, encoding) { + encoding = normalizeEncoding(encoding); + if (encoding === 'utf8') return utf8DecodeNative(bytes); + if (encoding === 'utf16le') return utf16leDecodeNative(bytes); + if (encoding === 'hex') return hexEncodeNative(bytes); + if (encoding === 'base64') return base64EncodeNative(bytes); + if (encoding === 'base64url') return base64urlEncodeNative(bytes); + if (encoding === 'latin1') return latin1DecodeNative(bytes); + if (encoding === 'ascii') return asciiDecodeNative(bytes); + throw ERR_UNKNOWN_ENCODING(encoding); + } + + function validateByteLength(byteLength) { + if (typeof byteLength !== 'number') + throw ERR_INVALID_ARG_TYPE('byteLength', 'number', byteLength); + if (Math.floor(byteLength) !== byteLength) + throw ERR_OUT_OF_RANGE('byteLength', 'an integer', byteLength); + if (byteLength < 1 || byteLength > 6) + throw ERR_OUT_OF_RANGE('byteLength', '>= 1 and <= 6', byteLength); + } + + function checkValueInt(value, min, max, blMinus1) { + if (value > max || value < min) { + var n = typeof min === 'bigint' ? 'n' : ''; + var range; + if (blMinus1 > 3) { + var bits = (blMinus1 + 1) * 8; + if (min === 0 || min === BigInt(0)) { + range = '>= 0' + n + ' and < 2' + n + ' ** ' + bits + n; + } else { + range = + '>= -(2' + n + ' ** ' + (bits - 1) + n + ') and < 2' + n + ' ** ' + (bits - 1) + n; + } + } else { + range = '>= ' + min + n + ' and <= ' + max + n; + } + throw ERR_OUT_OF_RANGE('value', range, value); + } + } + + function errBufferOutOfBounds() { + return nodeError( + P.RangeError, + 'ERR_BUFFER_OUT_OF_BOUNDS', + 'Attempt to access memory outside buffer bounds', + ); + } + + /** Node-style offset bounds for fixed-width numeric accessors. */ + function checkBounds(buf, offset, byteLength) { + if (offset === undefined) offset = 0; + if (typeof offset !== 'number') throw ERR_INVALID_ARG_TYPE('offset', 'number', offset); + if (Math.floor(offset) !== offset) throw ERR_OUT_OF_RANGE('offset', 'an integer', offset); + var max = buf.length - byteLength; + if (max < 0) throw errBufferOutOfBounds(); + if (offset < 0 || offset > max) throw ERR_OUT_OF_RANGE('offset', '>= 0 and <= ' + max, offset); + return offset; + } + + var dataViewCache = new WeakMap(); + /** @returns {DataView} Cached DataView for `buf` (WeakMap). */ + function viewOf(buf) { + var dv = dataViewCache.get(buf); + if (dv === undefined) { + dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); + dataViewCache.set(buf, dv); + } + return dv; + } + + function checkFixed(value, min, max, blMinus1) { + value = +value; + checkValueInt(value, min, max, blMinus1); + return value; + } + + function readUInt(buf, offset, byteLength, littleEndian) { + validateByteLength(byteLength); + offset = checkBounds(buf, offset, byteLength); + var value = 0; + if (littleEndian) { + for (var i = byteLength - 1; i >= 0; i--) value = value * 0x100 + buf[offset + i]; + } else { + for (var j = 0; j < byteLength; j++) value = value * 0x100 + buf[offset + j]; + } + return value; + } + + function readInt(buf, offset, byteLength, littleEndian) { + var value = readUInt(buf, offset, byteLength, littleEndian); + var sign = Math.pow(2, byteLength * 8 - 1); + var full = sign * 2; + return value >= sign ? value - full : value; + } + + function writeUInt(buf, value, offset, byteLength, littleEndian) { + validateByteLength(byteLength); + var max = Math.pow(2, 8 * byteLength) - 1; + value = +value; + checkValueInt(value, 0, max, byteLength - 1); + offset = checkBounds(buf, offset, byteLength); + value = Math.floor(value); + for (var i = 0; i < byteLength; i++) { + var index = littleEndian ? offset + i : offset + byteLength - 1 - i; + buf[index] = value & 0xff; + value = Math.floor(value / 0x100); + } + return offset + byteLength; + } + + function writeInt(buf, value, offset, byteLength, littleEndian) { + validateByteLength(byteLength); + var limit = Math.pow(2, 8 * byteLength - 1); + value = +value; + checkValueInt(value, -limit, limit - 1, byteLength - 1); + offset = checkBounds(buf, offset, byteLength); + value = Math.floor(value); + if (value < 0) value += Math.pow(2, byteLength * 8); + for (var i = 0; i < byteLength; i++) { + var index = littleEndian ? offset + i : offset + byteLength - 1 - i; + buf[index] = value & 0xff; + value = Math.floor(value / 0x100); + } + return offset + byteLength; + } + + var BIG_0 = typeof BigInt === 'function' ? BigInt(0) : null; + var BIG_1 = typeof BigInt === 'function' ? BigInt(1) : null; + var BIG_MAX_U64 = typeof BigInt === 'function' ? (BIG_1 << BigInt(64)) - BIG_1 : null; + var BIG_SIGN_I64 = typeof BigInt === 'function' ? BIG_1 << BigInt(63) : null; + + function readBigUInt(buf, offset, littleEndian) { + offset = checkBounds(buf, offset, 8); + return viewOf(buf).getBigUint64(offset, littleEndian); + } + + function readBigInt(buf, offset, littleEndian) { + offset = checkBounds(buf, offset, 8); + return viewOf(buf).getBigInt64(offset, littleEndian); + } + + // Node's writeBigU_Int64* order (lib/internal/buffer.js checkInt): the range compare + // runs first on the raw value, then the offset bounds, and only then `value & mask`, + // whose mixed-type `&` is what throws "Cannot mix BigInt and other types". So a boxed + // BigInt (Object(5n)) writes, an out-of-range number reports ERR_OUT_OF_RANGE, a bad + // offset beats a non-BigInt value, and a Symbol throws from the compare. The mask + // also yields the two's-complement u64 for a negative signed value, so both widths + // store through setBigUint64. Pinned by tests/node-compat/cases/73-buffer-type-fill-bigint.js. + function writeBigUInt(buf, value, offset, littleEndian) { + checkValueInt(value, BIG_0, BIG_MAX_U64, 7); + offset = checkBounds(buf, offset, 8); + viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); + return offset + 8; + } + + function writeBigInt(buf, value, offset, littleEndian) { + checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7); + offset = checkBounds(buf, offset, 8); + viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); + return offset + 8; + } + + /** + * Installs the per-encoding `*Slice`/`*Write` aliases and the fixed-width numeric + * read/write methods on `p` (Buffer.prototype), as node's addBufferPrototypeMethods + * does. Called once, by node:buffer, between `toLocaleString` and the `parent`/`offset` + * accessors: the position they were assigned at before the split, so the prototype's + * own-key order is unchanged. + * @param {Object} p Buffer.prototype (=== FastBuffer.prototype). + */ + function addBufferPrototypeMethods(p) { + p.utf8Slice = function (start, end) { + return this.toString('utf8', start, end); + }; + p.hexSlice = function (start, end) { + return this.toString('hex', start, end); + }; + p.asciiSlice = function (start, end) { + return this.toString('ascii', start, end); + }; + p.latin1Slice = function (start, end) { + return this.toString('latin1', start, end); + }; + p.base64Slice = function (start, end) { + return this.toString('base64', start, end); + }; + p.base64urlSlice = function (start, end) { + return this.toString('base64url', start, end); + }; + p.ucs2Slice = function (start, end) { + return this.toString('utf16le', start, end); + }; + p.utf8Write = function (string, offset, length) { + return this.write(string, offset, length, 'utf8'); + }; + p.hexWrite = function (string, offset, length) { + return this.write(string, offset, length, 'hex'); + }; + p.asciiWrite = function (string, offset, length) { + return this.write(string, offset, length, 'ascii'); + }; + p.latin1Write = function (string, offset, length) { + return this.write(string, offset, length, 'latin1'); + }; + p.base64Write = function (string, offset, length) { + return this.write(string, offset, length, 'base64'); + }; + p.base64urlWrite = function (string, offset, length) { + return this.write(string, offset, length, 'base64url'); + }; + p.ucs2Write = function (string, offset, length) { + return this.write(string, offset, length, 'utf16le'); + }; + p.readUInt8 = p.readUint8 = function (offset) { + offset = checkBounds(this, offset, 1); + return this[offset]; + }; + p.readInt8 = function (offset) { + var v = this.readUInt8(offset); + return v & 0x80 ? v - 0x100 : v; + }; + p.readUIntLE = p.readUintLE = function (offset, byteLength) { + return readUInt(this, offset, byteLength, true); + }; + p.readUIntBE = p.readUintBE = function (offset, byteLength) { + return readUInt(this, offset, byteLength, false); + }; + p.readIntLE = function (offset, byteLength) { + return readInt(this, offset, byteLength, true); + }; + p.readIntBE = function (offset, byteLength) { + return readInt(this, offset, byteLength, false); + }; + p.readUInt16LE = p.readUint16LE = function (offset) { + return viewOf(this).getUint16(checkBounds(this, offset, 2), true); + }; + p.readUInt16BE = p.readUint16BE = function (offset) { + return viewOf(this).getUint16(checkBounds(this, offset, 2), false); + }; + p.readUInt32LE = p.readUint32LE = function (offset) { + return viewOf(this).getUint32(checkBounds(this, offset, 4), true); + }; + p.readUInt32BE = p.readUint32BE = function (offset) { + return viewOf(this).getUint32(checkBounds(this, offset, 4), false); + }; + p.readInt16LE = function (offset) { + return viewOf(this).getInt16(checkBounds(this, offset, 2), true); + }; + p.readInt16BE = function (offset) { + return viewOf(this).getInt16(checkBounds(this, offset, 2), false); + }; + p.readInt32LE = function (offset) { + return viewOf(this).getInt32(checkBounds(this, offset, 4), true); + }; + p.readInt32BE = function (offset) { + return viewOf(this).getInt32(checkBounds(this, offset, 4), false); + }; + p.readFloatLE = function (offset) { + return viewOf(this).getFloat32(checkBounds(this, offset, 4), true); + }; + p.readFloatBE = function (offset) { + return viewOf(this).getFloat32(checkBounds(this, offset, 4), false); + }; + p.readDoubleLE = function (offset) { + return viewOf(this).getFloat64(checkBounds(this, offset, 8), true); + }; + p.readDoubleBE = function (offset) { + return viewOf(this).getFloat64(checkBounds(this, offset, 8), false); + }; + // BIG_0 is null exactly when BigInt is missing; read it rather than the live `BigInt` + // global, which this module-eval test became a call-time read of once it moved here. + if (BIG_0 !== null) { + p.readBigUInt64LE = p.readBigUint64LE = function (offset) { + return readBigUInt(this, offset, true); + }; + p.readBigUInt64BE = p.readBigUint64BE = function (offset) { + return readBigUInt(this, offset, false); + }; + p.readBigInt64LE = function (offset) { + return readBigInt(this, offset, true); + }; + p.readBigInt64BE = function (offset) { + return readBigInt(this, offset, false); + }; + } + + p.writeUInt8 = p.writeUint8 = function (value, offset) { + value = checkFixed(value, 0, 0xff, 0); + offset = checkBounds(this, offset, 1); + this[offset] = value; + return offset + 1; + }; + p.writeInt8 = function (value, offset) { + value = checkFixed(value, -0x80, 0x7f, 0); + offset = checkBounds(this, offset, 1); + this[offset] = value; + return offset + 1; + }; + p.writeUIntLE = p.writeUintLE = function (value, offset, byteLength) { + return writeUInt(this, value, offset, byteLength, true); + }; + p.writeUIntBE = p.writeUintBE = function (value, offset, byteLength) { + return writeUInt(this, value, offset, byteLength, false); + }; + p.writeIntLE = function (value, offset, byteLength) { + return writeInt(this, value, offset, byteLength, true); + }; + p.writeIntBE = function (value, offset, byteLength) { + return writeInt(this, value, offset, byteLength, false); + }; + p.writeUInt16LE = p.writeUint16LE = function (value, offset) { + value = checkFixed(value, 0, 0xffff, 1); + offset = checkBounds(this, offset, 2); + viewOf(this).setUint16(offset, value, true); + return offset + 2; + }; + p.writeUInt16BE = p.writeUint16BE = function (value, offset) { + value = checkFixed(value, 0, 0xffff, 1); + offset = checkBounds(this, offset, 2); + viewOf(this).setUint16(offset, value, false); + return offset + 2; + }; + p.writeUInt32LE = p.writeUint32LE = function (value, offset) { + value = checkFixed(value, 0, 0xffffffff, 3); + offset = checkBounds(this, offset, 4); + viewOf(this).setUint32(offset, value, true); + return offset + 4; + }; + p.writeUInt32BE = p.writeUint32BE = function (value, offset) { + value = checkFixed(value, 0, 0xffffffff, 3); + offset = checkBounds(this, offset, 4); + viewOf(this).setUint32(offset, value, false); + return offset + 4; + }; + p.writeInt16LE = function (value, offset) { + value = checkFixed(value, -0x8000, 0x7fff, 1); + offset = checkBounds(this, offset, 2); + viewOf(this).setInt16(offset, value, true); + return offset + 2; + }; + p.writeInt16BE = function (value, offset) { + value = checkFixed(value, -0x8000, 0x7fff, 1); + offset = checkBounds(this, offset, 2); + viewOf(this).setInt16(offset, value, false); + return offset + 2; + }; + p.writeInt32LE = function (value, offset) { + value = checkFixed(value, -0x80000000, 0x7fffffff, 3); + offset = checkBounds(this, offset, 4); + viewOf(this).setInt32(offset, value, true); + return offset + 4; + }; + p.writeInt32BE = function (value, offset) { + value = checkFixed(value, -0x80000000, 0x7fffffff, 3); + offset = checkBounds(this, offset, 4); + viewOf(this).setInt32(offset, value, false); + return offset + 4; + }; + p.writeFloatLE = function (value, offset) { + offset = checkBounds(this, offset, 4); + viewOf(this).setFloat32(offset, Number(value), true); + return offset + 4; + }; + p.writeFloatBE = function (value, offset) { + offset = checkBounds(this, offset, 4); + viewOf(this).setFloat32(offset, Number(value), false); + return offset + 4; + }; + p.writeDoubleLE = function (value, offset) { + offset = checkBounds(this, offset, 8); + viewOf(this).setFloat64(offset, Number(value), true); + return offset + 8; + }; + p.writeDoubleBE = function (value, offset) { + offset = checkBounds(this, offset, 8); + viewOf(this).setFloat64(offset, Number(value), false); + return offset + 8; + }; + if (BIG_0 !== null) { + p.writeBigUInt64LE = p.writeBigUint64LE = function (value, offset) { + return writeBigUInt(this, value, offset, true); + }; + p.writeBigUInt64BE = p.writeBigUint64BE = function (value, offset) { + return writeBigUInt(this, value, offset, false); + }; + p.writeBigInt64LE = function (value, offset) { + return writeBigInt(this, value, offset, true); + }; + p.writeBigInt64BE = function (value, offset) { + return writeBigInt(this, value, offset, false); + }; + } + } + + module.exports = { + requireNative: requireNative, + normalizeEncoding: normalizeEncoding, + isEncodingName: isEncodingName, + strToBytes: strToBytes, + bytesToString: bytesToString, + addBufferPrototypeMethods: addBufferPrototypeMethods, + }; +}); diff --git a/pkg/runtime/js/node/buffer.js b/pkg/runtime/js/node/buffer.js index 7549bc2..aff05ce 100644 --- a/pkg/runtime/js/node/buffer.js +++ b/pkg/runtime/js/node/buffer.js @@ -3,7 +3,10 @@ * * Codecs and byte-ops are Odin natives (`pkg/runtime/buffer.odin`) injected via * the loader's fourth `native` argument. This file owns Node API glue, errors and - * pooling. Blob, File and the blob: URL registry live in internal/blob.js and + * pooling: the constructor, the statics, the pool, the prototype methods node keeps + * in lib/buffer.js, and atob/btoa. The encoding codecs and the fixed-width numeric + * accessors live in internal/buffer.js, as node's do in lib/internal/buffer.js. + * Blob, File and the blob: URL registry live in internal/blob.js and * internal/file.js (node's lib/internal layout); this module re-exports them and * installs their globals, in the order it always has. * @@ -15,25 +18,16 @@ if (!native) throw new Error('node:buffer requires native codec bindings'); - /** @param {string} name @returns {Function} */ - function requireNative(name) { - var fn = native[name]; - if (typeof fn !== 'function') throw new Error('node:buffer missing native binding: ' + name); - return fn; - } + // Required first: it owns requireNative, and the codec helpers below are captured + // from it once, here, so every call site is a closure-variable call as before. + var internalBuffer = require('internal/buffer'); + var requireNative = internalBuffer.requireNative; + var normalizeEncoding = internalBuffer.normalizeEncoding; + var isEncodingName = internalBuffer.isEncodingName; + var strToBytes = internalBuffer.strToBytes; + var bytesToString = internalBuffer.bytesToString; - var utf8EncodeNative = requireNative('utf8Encode'); - var utf8DecodeNative = requireNative('utf8Decode'); var hexEncodeNative = requireNative('hexEncode'); - var hexDecodeNative = requireNative('hexDecode'); - var base64EncodeNative = requireNative('base64Encode'); - var base64DecodeNative = requireNative('base64Decode'); - var base64urlEncodeNative = requireNative('base64urlEncode'); - var latin1EncodeNative = requireNative('latin1Encode'); - var latin1DecodeNative = requireNative('latin1Decode'); - var asciiDecodeNative = requireNative('asciiDecode'); - var utf16leEncodeNative = requireNative('utf16leEncode'); - var utf16leDecodeNative = requireNative('utf16leDecode'); var utf8WriteIntoNative = requireNative('utf8WriteInto'); var latin1WriteIntoNative = requireNative('latin1WriteInto'); var utf16leWriteIntoNative = requireNative('utf16leWriteInto'); @@ -46,46 +40,16 @@ /** @type {((size: number) => Uint8Array|null)|null} */ var nativeAllocUninit = typeof native.allocUninit === 'function' ? native.allocUninit : null; - var EMPTY_U8 = new Uint8Array(0); var u8Fill = Uint8Array.prototype.fill; - // Encoding-name resolution is hardened ahead of the rest of this module because - // internal/encoding.js routes TextEncoder.encode and the TextDecoder utf-8 fast - // path through Buffer: a replaced `String` global or a poisoned - // `String.prototype.toLowerCase` reached a hardened TextDecoder through here and - // turned a valid decode into `Unknown encoding: …`. The rest of buffer.js is not - // yet converted (see tests/node-compat/pollution-baseline.json). + // The encoding-name hardening moved to internal/buffer.js with the codecs; what is + // captured here serves write, fromString, inspect and atob. var P = require('internal/primordials'); var StringG = String; - var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; var StringPrototypeSlice = P.StringPrototypeSlice; - var StringFromCharCode = String.fromCharCode; - var ObjectPrototypeHasOwnProperty = P.ObjectPrototypeHasOwnProperty; var TypedArrayPrototypeGetLength = P.TypedArrayPrototypeGetLength; - /** - * @type {Object} lowercased name → canonical encoding. - * Null-prototype: the keys are caller-supplied, so with Object.prototype in the - * chain `Buffer.from(s, 'constructor')` resolved to a function instead of - * failing, and any `Object.prototype.` a script set forged an encoding. - */ - var ENCODING_ALIASES = { - __proto__: null, - utf8: 'utf8', - 'utf-8': 'utf8', - utf16le: 'utf16le', - 'utf-16le': 'utf16le', - ucs2: 'utf16le', - 'ucs-2': 'utf16le', - binary: 'latin1', - latin1: 'latin1', - ascii: 'ascii', - hex: 'hex', - base64: 'base64', - base64url: 'base64url', - }; - var MAX_SAFE = 9007199254740991; var MAX_ALLOC_BYTES = native && typeof native.maxAllocBytes === 'number' && native.maxAllocBytes > 0 @@ -109,7 +73,6 @@ var ERR_INVALID_ARG_VALUE = errors.ERR_INVALID_ARG_VALUE; var ERR_OUT_OF_RANGE = errors.ERR_OUT_OF_RANGE; var ERR_MISSING_ARGS = errors.ERR_MISSING_ARGS; - var ERR_UNKNOWN_ENCODING = errors.ERR_UNKNOWN_ENCODING; // Required here, at the top level, so both stay in the eager set: the globals and // URL statics below are installed from this module, in the same order as before @@ -118,34 +81,6 @@ var Blob = blobModule.Blob; var File = require('internal/file').File; - function validateByteLength(byteLength) { - if (typeof byteLength !== 'number') - throw ERR_INVALID_ARG_TYPE('byteLength', 'number', byteLength); - if (Math.floor(byteLength) !== byteLength) - throw ERR_OUT_OF_RANGE('byteLength', 'an integer', byteLength); - if (byteLength < 1 || byteLength > 6) - throw ERR_OUT_OF_RANGE('byteLength', '>= 1 and <= 6', byteLength); - } - - function checkValueInt(value, min, max, blMinus1) { - if (value > max || value < min) { - var n = typeof min === 'bigint' ? 'n' : ''; - var range; - if (blMinus1 > 3) { - var bits = (blMinus1 + 1) * 8; - if (min === 0 || min === BigInt(0)) { - range = '>= 0' + n + ' and < 2' + n + ' ** ' + bits + n; - } else { - range = - '>= -(2' + n + ' ** ' + (bits - 1) + n + ') and < 2' + n + ' ** ' + (bits - 1) + n; - } - } else { - range = '>= ' + min + n + ' and <= ' + max + n; - } - throw ERR_OUT_OF_RANGE('value', range, value); - } - } - function validateWriteOffset(offset, max, name) { if (Math.floor(offset) !== offset) throw ERR_OUT_OF_RANGE(name, 'an integer', offset); if (offset < 0 || offset > max) throw ERR_OUT_OF_RANGE(name, '>= 0 && <= ' + max, offset); @@ -192,91 +127,6 @@ throw ERR_OUT_OF_RANGE('size', '>= 0 && <= ' + K_MAX_LENGTH, size); } - /** @returns {string} Canonical encoding name (default utf8). */ - function normalizeEncoding(encoding) { - if (encoding === undefined || encoding === null || encoding === '') return 'utf8'; - if (typeof encoding === 'symbol') return '' + encoding; // throws TypeError on Symbol, matching Node - var key = StringPrototypeToLowerCase(StringG(encoding)); - var canon = ENCODING_ALIASES[key]; - return canon !== undefined ? canon : key; - } - - /** @returns {boolean} */ - function isEncodingName(encoding) { - return ( - typeof encoding === 'string' && - ObjectPrototypeHasOwnProperty(ENCODING_ALIASES, StringPrototypeToLowerCase(encoding)) - ); - } - - /** - * Lenient Node base64/base64url normalization before native decode. - * - * A charCode filter, not `replaceAll(/[^A-Za-z0-9+/]/g, '')`. That was a GLOBAL - * regex replace, and a global replace under a forged `RegExp.prototype.exec` does - * not answer wrongly — it never returns, because `Symbol.replace` advances - * `lastIndex` only on an empty match. Measured: `Buffer.from('dXNlcjpwYXNz', - * 'base64')` hung indefinitely on `bin/lava` where node answered promptly, and - * this is the Basic-auth / JWT decode path, so one unauthenticated request with an - * `Authorization` header was enough to wedge the event loop for good. - * - * The `-`/`_` folds are plain string arguments, not regexes, so they were never - * part of the vector; they are folded into the same pass because the pass is free. - */ - function normalizeBase64(str) { - var src = StringG(str); - var n = src.length; - var out = ''; - for (var i = 0; i < n; i++) { - var c = StringPrototypeCharCodeAt(src, i); - if (c === 0x2d) { - c = 0x2b; // '-' -> '+' - } else if (c === 0x5f) { - c = 0x2f; // '_' -> '/' - } - if ( - (c >= 0x41 && c <= 0x5a) || // A-Z - (c >= 0x61 && c <= 0x7a) || // a-z - (c >= 0x30 && c <= 0x39) || // 0-9 - c === 0x2b || // + - c === 0x2f // / - ) { - out += StringFromCharCode(c); - } - } - if (out.length % 4 === 1) out = out.slice(0, out.length - 1); - while (out.length % 4 !== 0) out += '='; - return out; - } - - /** @returns {Uint8Array} */ - function strToBytes(str, encoding) { - encoding = normalizeEncoding(encoding); - str = StringG(str); - if (encoding === 'utf8') return utf8EncodeNative(str); - if (encoding === 'utf16le') return utf16leEncodeNative(str); - if (encoding === 'hex') return hexDecodeNative(str); - if (encoding === 'base64' || encoding === 'base64url') { - var norm = normalizeBase64(str); - return norm ? base64DecodeNative(norm) : EMPTY_U8; - } - if (encoding === 'ascii' || encoding === 'latin1') return latin1EncodeNative(str); - throw ERR_UNKNOWN_ENCODING(encoding); - } - - /** @returns {string} */ - function bytesToString(bytes, encoding) { - encoding = normalizeEncoding(encoding); - if (encoding === 'utf8') return utf8DecodeNative(bytes); - if (encoding === 'utf16le') return utf16leDecodeNative(bytes); - if (encoding === 'hex') return hexEncodeNative(bytes); - if (encoding === 'base64') return base64EncodeNative(bytes); - if (encoding === 'base64url') return base64urlEncodeNative(bytes); - if (encoding === 'latin1') return latin1DecodeNative(bytes); - if (encoding === 'ascii') return asciiDecodeNative(bytes); - throw ERR_UNKNOWN_ENCODING(encoding); - } - function toInteger(value, fallback) { if (value === undefined) return fallback; value = Number(value); @@ -292,42 +142,12 @@ return value; } - function errBufferOutOfBounds() { - return nodeError( - P.RangeError, - 'ERR_BUFFER_OUT_OF_BOUNDS', - 'Attempt to access memory outside buffer bounds', - ); - } - - /** Node-style offset bounds for fixed-width numeric accessors. */ - function checkBounds(buf, offset, byteLength) { - if (offset === undefined) offset = 0; - if (typeof offset !== 'number') throw ERR_INVALID_ARG_TYPE('offset', 'number', offset); - if (Math.floor(offset) !== offset) throw ERR_OUT_OF_RANGE('offset', 'an integer', offset); - var max = buf.length - byteLength; - if (max < 0) throw errBufferOutOfBounds(); - if (offset < 0 || offset > max) throw ERR_OUT_OF_RANGE('offset', '>= 0 and <= ' + max, offset); - return offset; - } - function validateRange(value, max, name) { value = toInteger(value, 0); if (value < 0 || value > max) throw new RangeError(name + ' is out of range'); return value; } - var dataViewCache = new WeakMap(); - /** @returns {DataView} Cached DataView for `buf` (WeakMap). */ - function viewOf(buf) { - var dv = dataViewCache.get(buf); - if (dv === undefined) { - dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength); - dataViewCache.set(buf, dv); - } - return dv; - } - /** @returns {-1|0|1} */ function compareBytes(a, b) { return nativeCompare(a, b); @@ -353,98 +173,6 @@ return nativeIndexOf(buf, needle, start, forward); } - function checkFixed(value, min, max, blMinus1) { - value = +value; - checkValueInt(value, min, max, blMinus1); - return value; - } - - function readUInt(buf, offset, byteLength, littleEndian) { - validateByteLength(byteLength); - offset = checkBounds(buf, offset, byteLength); - var value = 0; - if (littleEndian) { - for (var i = byteLength - 1; i >= 0; i--) value = value * 0x100 + buf[offset + i]; - } else { - for (var j = 0; j < byteLength; j++) value = value * 0x100 + buf[offset + j]; - } - return value; - } - - function readInt(buf, offset, byteLength, littleEndian) { - var value = readUInt(buf, offset, byteLength, littleEndian); - var sign = Math.pow(2, byteLength * 8 - 1); - var full = sign * 2; - return value >= sign ? value - full : value; - } - - function writeUInt(buf, value, offset, byteLength, littleEndian) { - validateByteLength(byteLength); - var max = Math.pow(2, 8 * byteLength) - 1; - value = +value; - checkValueInt(value, 0, max, byteLength - 1); - offset = checkBounds(buf, offset, byteLength); - value = Math.floor(value); - for (var i = 0; i < byteLength; i++) { - var index = littleEndian ? offset + i : offset + byteLength - 1 - i; - buf[index] = value & 0xff; - value = Math.floor(value / 0x100); - } - return offset + byteLength; - } - - function writeInt(buf, value, offset, byteLength, littleEndian) { - validateByteLength(byteLength); - var limit = Math.pow(2, 8 * byteLength - 1); - value = +value; - checkValueInt(value, -limit, limit - 1, byteLength - 1); - offset = checkBounds(buf, offset, byteLength); - value = Math.floor(value); - if (value < 0) value += Math.pow(2, byteLength * 8); - for (var i = 0; i < byteLength; i++) { - var index = littleEndian ? offset + i : offset + byteLength - 1 - i; - buf[index] = value & 0xff; - value = Math.floor(value / 0x100); - } - return offset + byteLength; - } - - var BIG_0 = typeof BigInt === 'function' ? BigInt(0) : null; - var BIG_1 = typeof BigInt === 'function' ? BigInt(1) : null; - var BIG_MAX_U64 = typeof BigInt === 'function' ? (BIG_1 << BigInt(64)) - BIG_1 : null; - var BIG_SIGN_I64 = typeof BigInt === 'function' ? BIG_1 << BigInt(63) : null; - - function readBigUInt(buf, offset, littleEndian) { - offset = checkBounds(buf, offset, 8); - return viewOf(buf).getBigUint64(offset, littleEndian); - } - - function readBigInt(buf, offset, littleEndian) { - offset = checkBounds(buf, offset, 8); - return viewOf(buf).getBigInt64(offset, littleEndian); - } - - // Node's writeBigU_Int64* order (lib/internal/buffer.js checkInt): the range compare - // runs first on the raw value, then the offset bounds, and only then `value & mask`, - // whose mixed-type `&` is what throws "Cannot mix BigInt and other types". So a boxed - // BigInt (Object(5n)) writes, an out-of-range number reports ERR_OUT_OF_RANGE, a bad - // offset beats a non-BigInt value, and a Symbol throws from the compare. The mask - // also yields the two's-complement u64 for a negative signed value, so both widths - // store through setBigUint64. Pinned by tests/node-compat/cases/73-buffer-type-fill-bigint.js. - function writeBigUInt(buf, value, offset, littleEndian) { - checkValueInt(value, BIG_0, BIG_MAX_U64, 7); - offset = checkBounds(buf, offset, 8); - viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); - return offset + 8; - } - - function writeBigInt(buf, value, offset, littleEndian) { - checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7); - offset = checkBounds(buf, offset, 8); - viewOf(buf).setBigUint64(offset, value & BIG_MAX_U64, littleEndian); - return offset + 8; - } - /** * The concrete Buffer class: a Uint8Array subclass carrying all Buffer methods, * with the DEFAULT (forwarding) constructor so internal construction from a known @@ -783,225 +511,8 @@ }); } p.toLocaleString = p.toString; - p.utf8Slice = function (start, end) { - return this.toString('utf8', start, end); - }; - p.hexSlice = function (start, end) { - return this.toString('hex', start, end); - }; - p.asciiSlice = function (start, end) { - return this.toString('ascii', start, end); - }; - p.latin1Slice = function (start, end) { - return this.toString('latin1', start, end); - }; - p.base64Slice = function (start, end) { - return this.toString('base64', start, end); - }; - p.base64urlSlice = function (start, end) { - return this.toString('base64url', start, end); - }; - p.ucs2Slice = function (start, end) { - return this.toString('utf16le', start, end); - }; - p.utf8Write = function (string, offset, length) { - return this.write(string, offset, length, 'utf8'); - }; - p.hexWrite = function (string, offset, length) { - return this.write(string, offset, length, 'hex'); - }; - p.asciiWrite = function (string, offset, length) { - return this.write(string, offset, length, 'ascii'); - }; - p.latin1Write = function (string, offset, length) { - return this.write(string, offset, length, 'latin1'); - }; - p.base64Write = function (string, offset, length) { - return this.write(string, offset, length, 'base64'); - }; - p.base64urlWrite = function (string, offset, length) { - return this.write(string, offset, length, 'base64url'); - }; - p.ucs2Write = function (string, offset, length) { - return this.write(string, offset, length, 'utf16le'); - }; - p.readUInt8 = p.readUint8 = function (offset) { - offset = checkBounds(this, offset, 1); - return this[offset]; - }; - p.readInt8 = function (offset) { - var v = this.readUInt8(offset); - return v & 0x80 ? v - 0x100 : v; - }; - p.readUIntLE = p.readUintLE = function (offset, byteLength) { - return readUInt(this, offset, byteLength, true); - }; - p.readUIntBE = p.readUintBE = function (offset, byteLength) { - return readUInt(this, offset, byteLength, false); - }; - p.readIntLE = function (offset, byteLength) { - return readInt(this, offset, byteLength, true); - }; - p.readIntBE = function (offset, byteLength) { - return readInt(this, offset, byteLength, false); - }; - p.readUInt16LE = p.readUint16LE = function (offset) { - return viewOf(this).getUint16(checkBounds(this, offset, 2), true); - }; - p.readUInt16BE = p.readUint16BE = function (offset) { - return viewOf(this).getUint16(checkBounds(this, offset, 2), false); - }; - p.readUInt32LE = p.readUint32LE = function (offset) { - return viewOf(this).getUint32(checkBounds(this, offset, 4), true); - }; - p.readUInt32BE = p.readUint32BE = function (offset) { - return viewOf(this).getUint32(checkBounds(this, offset, 4), false); - }; - p.readInt16LE = function (offset) { - return viewOf(this).getInt16(checkBounds(this, offset, 2), true); - }; - p.readInt16BE = function (offset) { - return viewOf(this).getInt16(checkBounds(this, offset, 2), false); - }; - p.readInt32LE = function (offset) { - return viewOf(this).getInt32(checkBounds(this, offset, 4), true); - }; - p.readInt32BE = function (offset) { - return viewOf(this).getInt32(checkBounds(this, offset, 4), false); - }; - p.readFloatLE = function (offset) { - return viewOf(this).getFloat32(checkBounds(this, offset, 4), true); - }; - p.readFloatBE = function (offset) { - return viewOf(this).getFloat32(checkBounds(this, offset, 4), false); - }; - p.readDoubleLE = function (offset) { - return viewOf(this).getFloat64(checkBounds(this, offset, 8), true); - }; - p.readDoubleBE = function (offset) { - return viewOf(this).getFloat64(checkBounds(this, offset, 8), false); - }; - if (typeof BigInt === 'function') { - p.readBigUInt64LE = p.readBigUint64LE = function (offset) { - return readBigUInt(this, offset, true); - }; - p.readBigUInt64BE = p.readBigUint64BE = function (offset) { - return readBigUInt(this, offset, false); - }; - p.readBigInt64LE = function (offset) { - return readBigInt(this, offset, true); - }; - p.readBigInt64BE = function (offset) { - return readBigInt(this, offset, false); - }; - } - - p.writeUInt8 = p.writeUint8 = function (value, offset) { - value = checkFixed(value, 0, 0xff, 0); - offset = checkBounds(this, offset, 1); - this[offset] = value; - return offset + 1; - }; - p.writeInt8 = function (value, offset) { - value = checkFixed(value, -0x80, 0x7f, 0); - offset = checkBounds(this, offset, 1); - this[offset] = value; - return offset + 1; - }; - p.writeUIntLE = p.writeUintLE = function (value, offset, byteLength) { - return writeUInt(this, value, offset, byteLength, true); - }; - p.writeUIntBE = p.writeUintBE = function (value, offset, byteLength) { - return writeUInt(this, value, offset, byteLength, false); - }; - p.writeIntLE = function (value, offset, byteLength) { - return writeInt(this, value, offset, byteLength, true); - }; - p.writeIntBE = function (value, offset, byteLength) { - return writeInt(this, value, offset, byteLength, false); - }; - p.writeUInt16LE = p.writeUint16LE = function (value, offset) { - value = checkFixed(value, 0, 0xffff, 1); - offset = checkBounds(this, offset, 2); - viewOf(this).setUint16(offset, value, true); - return offset + 2; - }; - p.writeUInt16BE = p.writeUint16BE = function (value, offset) { - value = checkFixed(value, 0, 0xffff, 1); - offset = checkBounds(this, offset, 2); - viewOf(this).setUint16(offset, value, false); - return offset + 2; - }; - p.writeUInt32LE = p.writeUint32LE = function (value, offset) { - value = checkFixed(value, 0, 0xffffffff, 3); - offset = checkBounds(this, offset, 4); - viewOf(this).setUint32(offset, value, true); - return offset + 4; - }; - p.writeUInt32BE = p.writeUint32BE = function (value, offset) { - value = checkFixed(value, 0, 0xffffffff, 3); - offset = checkBounds(this, offset, 4); - viewOf(this).setUint32(offset, value, false); - return offset + 4; - }; - p.writeInt16LE = function (value, offset) { - value = checkFixed(value, -0x8000, 0x7fff, 1); - offset = checkBounds(this, offset, 2); - viewOf(this).setInt16(offset, value, true); - return offset + 2; - }; - p.writeInt16BE = function (value, offset) { - value = checkFixed(value, -0x8000, 0x7fff, 1); - offset = checkBounds(this, offset, 2); - viewOf(this).setInt16(offset, value, false); - return offset + 2; - }; - p.writeInt32LE = function (value, offset) { - value = checkFixed(value, -0x80000000, 0x7fffffff, 3); - offset = checkBounds(this, offset, 4); - viewOf(this).setInt32(offset, value, true); - return offset + 4; - }; - p.writeInt32BE = function (value, offset) { - value = checkFixed(value, -0x80000000, 0x7fffffff, 3); - offset = checkBounds(this, offset, 4); - viewOf(this).setInt32(offset, value, false); - return offset + 4; - }; - p.writeFloatLE = function (value, offset) { - offset = checkBounds(this, offset, 4); - viewOf(this).setFloat32(offset, Number(value), true); - return offset + 4; - }; - p.writeFloatBE = function (value, offset) { - offset = checkBounds(this, offset, 4); - viewOf(this).setFloat32(offset, Number(value), false); - return offset + 4; - }; - p.writeDoubleLE = function (value, offset) { - offset = checkBounds(this, offset, 8); - viewOf(this).setFloat64(offset, Number(value), true); - return offset + 8; - }; - p.writeDoubleBE = function (value, offset) { - offset = checkBounds(this, offset, 8); - viewOf(this).setFloat64(offset, Number(value), false); - return offset + 8; - }; - if (typeof BigInt === 'function') { - p.writeBigUInt64LE = p.writeBigUint64LE = function (value, offset) { - return writeBigUInt(this, value, offset, true); - }; - p.writeBigUInt64BE = p.writeBigUint64BE = function (value, offset) { - return writeBigUInt(this, value, offset, false); - }; - p.writeBigInt64LE = function (value, offset) { - return writeBigInt(this, value, offset, true); - }; - p.writeBigInt64BE = function (value, offset) { - return writeBigInt(this, value, offset, false); - }; - } + // node's addBufferPrototypeMethods, at the point these were always assigned. + internalBuffer.addBufferPrototypeMethods(p); Object.defineProperty(p, 'parent', { get: function () { diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index e4318f0..763b256 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -223,6 +223,10 @@ const ROUTES = [ re: globToRe('**/js/node/buffer.js'), targets: ['make test-property'], }, + { + re: globToRe('**/js/internal/buffer.js'), + targets: ['make test-property'], + }, { re: globToRe('scripts/**'), targets: ['make test-scripts'], diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index fca8fb2..52c59a3 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -49,6 +49,12 @@ test('crypto internals route the crypto gates, entry and split files alike', () } }); +test('buffer internals route the property tests, entry and split file alike', () => { + for (const p of ['pkg/runtime/js/node/buffer.js', 'pkg/runtime/js/internal/buffer.js']) { + assert.ok(routePaths([p]).targets.includes('make test-property'), p); + } +}); + test('invalid **.js style is not required — **/* works', () => { const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 497f511..5d4fe00 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -234,7 +234,7 @@ { "name": "the Lava-only test dies when normalizeBase64 goes back to a global replace", "why": "A global replace under a forged exec never terminates (or, on some runners, blows up as RangeError from runaway string growth). Buffer.from(x,'base64') is the Basic-auth/JWT path. Either RED detail proves the reintroduced global replace; hang vs throw is runner-dependent, so expect_detail is an alternatives list.", - "source": "pkg/runtime/js/node/buffer.js", + "source": "pkg/runtime/js/internal/buffer.js", "find": " var src = StringG(str);\n var n = src.length;\n var out = '';", "replace": " return StringG(str)\n .replaceAll('-', '+')\n .replaceAll('_', '/')\n .replaceAll(/[^A-Za-z0-9+/]/g, '');\n var src = StringG(str);\n var n = src.length;\n var out = '';", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -1691,7 +1691,7 @@ { "name": "70-buffer-size-inspect-parity dies when checkBounds names a class instead of the type", "why": "internal/errors classifies `expected` the way node does: 'number' is a type (`must be of type number`), a capitalized 'Number' a class (`must be an instance of Number`). The call site must pass the type.", - "source": "pkg/runtime/js/node/buffer.js", + "source": "pkg/runtime/js/internal/buffer.js", "find": "throw ERR_INVALID_ARG_TYPE('offset', 'number', offset);", "replace": "throw ERR_INVALID_ARG_TYPE('offset', 'Number', offset);", "gate": "compat:tests/node-compat/cases/70-buffer-size-inspect-parity.js", @@ -1790,7 +1790,7 @@ { "name": "73-buffer-type-fill-bigint dies when writeBigInt rejects non-BigInt values before the range and offset checks", "why": "node runs checkInt (range, then offset bounds) before the BigInt mask, so Object(5n) writes and a bad range/offset is reported as ERR_OUT_OF_RANGE / ERR_INVALID_ARG_TYPE rather than 'Cannot mix BigInt'.", - "source": "pkg/runtime/js/node/buffer.js", + "source": "pkg/runtime/js/internal/buffer.js", "find": " checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", "replace": " if (typeof value !== 'bigint') throw new TypeError('Cannot mix BigInt and other types, use explicit conversions');\n checkValueInt(value, -BIG_SIGN_I64, BIG_SIGN_I64 - BIG_1, 7);\n", "gate": "compat:tests/node-compat/cases/73-buffer-type-fill-bigint.js", @@ -2552,6 +2552,24 @@ "replace": " var inspectColors = Object.assign({ __proto__: null }, inspectModule.inspectColors);", "gate": "compat:tests/node-compat/cases/84-util-internal-modules.js", "expect_detail": "node: identities true true true true" + }, + { + "name": "85-buffer-internal-modules dies when the loader stops handing internal/buffer the buffer bindings", + "why": "Odin keys the buffer natives to node/buffer only; internal/buffer.js reaches them solely through the loader alias, and without it the codecs every Buffer encode/decode runs through cannot load.", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " natives['internal/buffer'] = natives['node/buffer'];\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/85-buffer-internal-modules.js", + "expect_detail": "node: hidden internal/buffer MODULE_NOT_FOUND node:threw" + }, + { + "name": "85-buffer-internal-modules dies when the numeric accessors are defined back in node:buffer", + "why": "node frames a readUInt8 bounds error in node:internal/buffer; a copy of the accessor in node/buffer.js (the pre-split layout) shadows the installed one and its frames read node:buffer again.", + "source": "pkg/runtime/js/node/buffer.js", + "find": " internalBuffer.addBufferPrototypeMethods(p);\n", + "replace": " internalBuffer.addBufferPrototypeMethods(p);\n p.readUInt8 = p.readUint8 = function (offset) {\n if (offset < 0 || offset >= this.length) throw ERR_OUT_OF_RANGE('offset', '>= 0', offset);\n return this[offset];\n };\n", + "gate": "compat:tests/node-compat/cases/85-buffer-internal-modules.js", + "expect_detail": "lava: readUInt8 offset: internal/buffer got node:buffer" } ] } diff --git a/tests/node-compat/cases/85-buffer-internal-modules.js b/tests/node-compat/cases/85-buffer-internal-modules.js new file mode 100644 index 0000000..be6f36b --- /dev/null +++ b/tests/node-compat/cases/85-buffer-internal-modules.js @@ -0,0 +1,215 @@ +// node:buffer is assembled from two files laid out like node's: js/internal/buffer.js +// holds the encoding codecs and the fixed-width numeric accessors (node's +// lib/internal/buffer.js, whose addBufferPrototypeMethods installs readUInt8 and +// friends), and node/buffer stays the public entry with the constructor, the statics +// and the lib/buffer.js prototype methods. A bounds error from readUInt8 used to carry +// only `node:buffer` frames, because everything was defined in buffer.js; now it names +// node:internal/buffer, exactly as node's does. +// +// Each frame line prints `ok` when the stack carries `node::` and names the +// first `node:` frame it got otherwise (past internal/errors, which builds the error). Only the source name is compared, never the +// message, line or column. Sites whose frame is not runtime-neutral are left out: +// node runs `hexWrite` itself in internal/buffer, and Lava's alias tail-calls `write`, +// so JSC drops its frame; `toString('bogus')` throws from the codec, whose only caller +// frame is likewise a tail call. +// +// The new id must stay internal: neither the bare nor the `node:` spelling may resolve +// from user code. Only the error CODE of the bare spelling is printed (the messages +// diverge, #415). +// +// The export and prototype lines check what crosses the new seam: the keys, their +// descriptor kinds, the identities the globals share, and Buffer's own shape (callable +// without `new`, one prototype with FastBuffer). Export ORDER and the prototype's full +// key order are not oracle-able — node's lists are different sets — so the split was +// checked against the previous binary instead (the PR records the diff). +'use strict'; + +// node prints DEP0005 for Buffer(n) on stderr, which the runner compares. +process.noDeprecation = true; + +const buffer = require('buffer'); +const { Buffer } = buffer; + +for (const id of ['internal/buffer']) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +function frame(label, file, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + const where = stack.includes('node:' + file + ':') + ? 'ok' + : 'got ' + (stack.match(/node:(?!internal\/errors)[\w/]+/) || ['no node: frame'])[0]; + console.log(label + ': ' + file + ' ' + where); +} + +// The numeric accessors: node:internal/buffer on both runtimes. +frame('readUInt8 offset', 'internal/buffer', () => Buffer.alloc(1).readUInt8(5)); +frame('readUInt16LE bounds', 'internal/buffer', () => Buffer.alloc(1).readUInt16LE(0)); +frame('readUIntLE byteLength', 'internal/buffer', () => Buffer.alloc(4).readUIntLE(0, 9)); +frame('readIntBE offset', 'internal/buffer', () => Buffer.alloc(4).readIntBE(-1, 2)); +frame('writeUInt8 value', 'internal/buffer', () => Buffer.alloc(1).writeUInt8(300, 0)); +frame('writeInt32BE value', 'internal/buffer', () => Buffer.alloc(4).writeInt32BE(2 ** 40, 0)); +frame('writeUIntBE byteLength', 'internal/buffer', () => Buffer.alloc(4).writeUIntBE(1, 0, 0)); +frame('writeBigInt64LE value', 'internal/buffer', () => + Buffer.alloc(8).writeBigInt64LE(2n ** 70n, 0), +); +frame('readBigUInt64BE bounds', 'internal/buffer', () => Buffer.alloc(4).readBigUInt64BE(0)); +frame('readDoubleLE offset', 'internal/buffer', () => Buffer.alloc(8).readDoubleLE(1.5)); +frame('writeFloatBE offset', 'internal/buffer', () => Buffer.alloc(8).writeFloatBE(1, 'x')); +// What stayed in the public entry: node:buffer on both runtimes. +frame('from encoding', 'buffer', () => Buffer.from('a', 'bogus')); +frame('write encoding', 'buffer', () => Buffer.alloc(2).write('a', 'bogus')); +frame('alloc size', 'buffer', () => Buffer.alloc(-1)); +frame('concat element', 'buffer', () => Buffer.concat([1])); +frame('swap16 size', 'buffer', () => Buffer.alloc(3).swap16()); +frame('byteLength type', 'buffer', () => Buffer.byteLength(5)); +frame('compare target', 'buffer', () => Buffer.alloc(2).compare(5)); + +// --- the assembled exports: shapes and identities ---------------------------------- +console.log('same module', require('node:buffer') === buffer); +for (const k of [ + 'Buffer', + 'Blob', + 'File', + 'kMaxLength', + 'kStringMaxLength', + 'constants', + 'atob', + 'btoa', + 'isAscii', + 'isUtf8', + 'transcode', + 'resolveObjectURL', + 'INSPECT_MAX_BYTES', +]) { + const d = Object.getOwnPropertyDescriptor(buffer, k); + console.log('export', k, typeof buffer[k], 'value' in d ? 'data' : 'accessor'); +} +console.log( + 'identities', + globalThis.Buffer === Buffer, + globalThis.atob === buffer.atob, + globalThis.btoa === buffer.btoa, + globalThis.Blob === buffer.Blob, + globalThis.File === buffer.File, +); + +// --- Buffer itself: a function constructor, callable without `new` ------------------ +const called = Buffer(3); +const constructed = new Buffer('ab'); +console.log( + 'ctor', + typeof Buffer, + Buffer.length, + Buffer.name, + Buffer.isBuffer(called), + called.length, + Buffer.isBuffer(constructed), + constructed.toString(), +); +const FastBuffer = Object.getPrototypeOf(Buffer.from('x')).constructor; +console.log( + 'prototype', + FastBuffer === Buffer, + Object.getPrototypeOf(Buffer.alloc(1)) === Buffer.prototype, + Object.getPrototypeOf(Buffer.allocUnsafe(1)) === Buffer.prototype, + Object.getPrototypeOf(Buffer.alloc(1).subarray(0)) === Buffer.prototype, + Object.getPrototypeOf(Buffer.prototype) === Uint8Array.prototype, +); + +// --- the methods internal/buffer installs ------------------------------------------ +const proto = Buffer.prototype; +for (const k of [ + 'readUInt8', + 'readUint8', + 'readBigUInt64LE', + 'readBigUint64LE', + 'writeUInt32BE', + 'writeUint32BE', + 'writeDoubleLE', + 'utf8Slice', + 'hexSlice', + 'ucs2Write', + 'latin1Write', +]) { + const d = Object.getOwnPropertyDescriptor(proto, k); + console.log('method', k, typeof d.value, d.writable, d.enumerable, d.configurable); +} +console.log( + 'aliases', + proto.readUInt8 === proto.readUint8, + proto.readBigUInt64LE === proto.readBigUint64LE, + proto.writeUInt32BE === proto.writeUint32BE, +); + +// --- behavior across the file seam --------------------------------------------------- +const b = Buffer.alloc(16); +b.writeUInt32BE(0xdeadbeef, 0); +b.writeInt16LE(-2, 4); +b.writeDoubleLE(Math.PI, 8); +console.log( + 'numeric', + b.toString('hex'), + b.readUInt32BE(0), + b.readInt16LE(4), + b.readDoubleLE(8), + b.readUIntBE(0, 3), + b.readIntLE(4, 2), +); +const big = Buffer.alloc(8); +big.writeBigInt64BE(-5n); +console.log('bigint', big.readBigInt64BE(0), big.readBigUInt64LE(0)); +const s = Buffer.from('héllo wörld'); +console.log( + 'codecs', + s.toString('base64'), + s.toString('base64url'), + s.toString('latin1'), + s.toString('ascii'), + Buffer.from('aGk', 'base64url').toString(), + Buffer.from(' aG k= ', 'base64').toString(), + Buffer.from('6869', 'hex').toString('ucs2'), +); +console.log( + 'slices', + s.utf8Slice(0, 5), + s.hexSlice(0, 2), + s.latin1Slice(0, 3), + s.base64Slice(0, 3), + s.ucs2Slice(0, 4), +); +const w = Buffer.alloc(6); +console.log('writes', w.hexWrite('a1b2', 0), w.ucs2Write('z', 2), w.latin1Write('q', 5), w); +console.log( + 'encodings', + Buffer.isEncoding('UTF-8'), + Buffer.isEncoding('ucs-2'), + Buffer.isEncoding('constructor'), + Buffer.byteLength('aGk=', 'base64'), + Buffer.byteLength('héllo'), + Buffer.from('ff', 'HEX')[0], +); +console.log( + 'atob', + buffer.atob('aGk='), + buffer.btoa('hi'), + buffer.transcode(s, 'utf8', 'latin1').length, +); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 869538b..0de843c 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -35,6 +35,12 @@ "accessor": 0, "global": 0 }, + "internal/buffer.js": { + "method": 8, + "invoke": 0, + "accessor": 3, + "global": 17 + }, "internal/crypto/hash.js": { "method": 8, "invoke": 0, @@ -312,10 +318,10 @@ "global": 40 }, "node/buffer.js": { - "method": 23, + "method": 15, "invoke": 5, - "accessor": 15, - "global": 81 + "accessor": 12, + "global": 64 }, "node/console.js": { "method": 0, From e6b18a6bb72977e95ec6f69f8bb28876f56c49df Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:13:48 +0200 Subject: [PATCH 39/61] refactor(os): move os.constants tables to internal/os/constants node/os.js 705 -> 334 lines. Node keeps these tables in C++ (node_constants.cc), not lib/os.js; they are pure literals, so no frame moves. os stays lazy; its first require now compiles two factories. Case 86 pins the constants surface. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/lazy_builtins_test.odin | 13 +- docs/ARCHITECTURE.md | 6 +- pkg/runtime/globals.odin | 1 + pkg/runtime/js/internal/os/constants.js | 389 ++++++++++++++++++ pkg/runtime/js/node/os.js | 379 +---------------- tests/mutation-manifest.json | 9 + .../cases/86-os-internal-modules.js | 68 +++ tests/node-compat/pollution-baseline.json | 6 + 8 files changed, 489 insertions(+), 382 deletions(-) create mode 100644 pkg/runtime/js/internal/os/constants.js create mode 100644 tests/node-compat/cases/86-os-internal-modules.js diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 1ca1a20..36af5b9 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -58,15 +58,16 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { embedded, ) - // node:os is outside the eager closure and its only requires (primordials, - // internal/errors, buffer through require.pristineBuffer) are eager, so its first require - // compiles exactly one factory: its own. + // node:os is outside the eager closure and its requires (primordials, internal/errors, + // buffer through require.pristineBuffer) are eager except internal/os/constants, which + // only os requires, so its first require compiles exactly two factories: its own and + // the constants table. testing.expect(t, run_bool(ctx, "typeof require('node:os').platform() === 'string'"), "node:os did not load") - testing.expect_value(t, state.builtin_compiles, at_init + 1) + testing.expect_value(t, state.builtin_compiles, at_init + 2) // Cached by the loader: a second require compiles nothing. testing.expect(t, run_bool(ctx, "require('os') === require('node:os')"), "node:os was re-instantiated") - testing.expect_value(t, state.builtin_compiles, at_init + 1) + testing.expect_value(t, state.builtin_compiles, at_init + 2) // Ids outside the embedded set stay unknown, compile nothing, and never reach // an inherited property or a path outside js/. @@ -78,7 +79,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { return true; })()` testing.expect(t, run_bool(ctx, UNKNOWN), "an id outside the embedded set resolved") - testing.expect_value(t, state.builtin_compiles, at_init + 1) + testing.expect_value(t, state.builtin_compiles, at_init + 2) // The ratchet's static eager set (scripts/lib/js-graph.mjs eagerModules) against what // init really compiled. Static ⊆ runtime is the property that matters: a module the diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 2ebd87d..f2fcd05 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -233,7 +233,11 @@ the statics, the pool, the `lib/buffer.js` prototype methods and `atob`/`btoa`, installer where those methods were always assigned (the prototype's key order is unchanged), and captures the codecs once at module-eval, so a `toString` pays the same closure call as before. `internal/buffer` decodes through `node/buffer`'s bindings via the -same `loader.js` aliasing, and is eager through `req('buffer')`. Errors use Node-shaped +same `loader.js` aliasing, and is eager through `req('buffer')`. `os` moves only its +data: `internal/os/constants` holds the signal, errno, priority and dlopen tables, which +node reads from its C++ constants binding rather than `lib/os.js`. The tables throw +nothing, so no frame moves; they call no native, so there is no alias, and the file stays +lazy behind `require('os')`. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, diff --git a/pkg/runtime/globals.odin b/pkg/runtime/globals.odin index cd10d46..f20a13c 100644 --- a/pkg/runtime/globals.odin +++ b/pkg/runtime/globals.odin @@ -1641,6 +1641,7 @@ EMBEDDED_JS_DIRS := [?]Embedded_JS_Dir { {"internal/crypto", #load_directory("js/internal/crypto")}, {"internal/fetch", #load_directory("js/internal/fetch")}, {"internal/http", #load_directory("js/internal/http")}, + {"internal/os", #load_directory("js/internal/os")}, {"internal/streams", #load_directory("js/internal/streams")}, {"internal/util", #load_directory("js/internal/util")}, {"internal/webstreams", #load_directory("js/internal/webstreams")}, diff --git a/pkg/runtime/js/internal/os/constants.js b/pkg/runtime/js/internal/os/constants.js new file mode 100644 index 0000000..899520d --- /dev/null +++ b/pkg/runtime/js/internal/os/constants.js @@ -0,0 +1,389 @@ +// os.constants — the signal, errno, priority and dlopen tables behind node:os. Node does +// not keep these in lib/os.js either: they come from the C++ constants binding +// (src/node_constants.cc, read as internalBinding('constants').os), so this file is the +// table half of that seam and node/os.js keeps the function half. Pure data: nothing +// here throws, so no node:os stack frame moves, and no native is needed — every value +// is a per-platform constant picked by process.platform. +(function (require, module) { + 'use strict'; + + var isWindows = process.platform === 'win32'; + + // ---- os.constants ---------------------------------------------------------- + // signals + errno are platform-specific numbers straight from the system + // headers; priority/dlopen/UV are libuv's cross-platform abstraction. Values + // mirror Node's so feature-detection and `process.kill(pid, 'SIGTERM')`-style + // lookups resolve to the same numbers the host kernel uses. + var signals; + if (process.platform === 'darwin') { + signals = { + SIGHUP: 1, + SIGINT: 2, + SIGQUIT: 3, + SIGILL: 4, + SIGTRAP: 5, + SIGABRT: 6, + SIGIOT: 6, + SIGEMT: 7, + SIGFPE: 8, + SIGKILL: 9, + SIGBUS: 10, + SIGSEGV: 11, + SIGSYS: 12, + SIGPIPE: 13, + SIGALRM: 14, + SIGTERM: 15, + SIGURG: 16, + SIGSTOP: 17, + SIGTSTP: 18, + SIGCONT: 19, + SIGCHLD: 20, + SIGTTIN: 21, + SIGTTOU: 22, + SIGIO: 23, + SIGXCPU: 24, + SIGXFSZ: 25, + SIGVTALRM: 26, + SIGPROF: 27, + SIGWINCH: 28, + SIGINFO: 29, + SIGUSR1: 30, + SIGUSR2: 31, + }; + } else if (isWindows) { + signals = { + SIGHUP: 1, + SIGINT: 2, + SIGILL: 4, + SIGABRT: 22, + SIGFPE: 8, + SIGKILL: 9, + SIGSEGV: 11, + SIGTERM: 15, + SIGBREAK: 21, + SIGWINCH: 28, + }; + } else { + signals = { + SIGHUP: 1, + SIGINT: 2, + SIGQUIT: 3, + SIGILL: 4, + SIGTRAP: 5, + SIGABRT: 6, + SIGIOT: 6, + SIGBUS: 7, + SIGFPE: 8, + SIGKILL: 9, + SIGUSR1: 10, + SIGSEGV: 11, + SIGUSR2: 12, + SIGPIPE: 13, + SIGALRM: 14, + SIGTERM: 15, + SIGSTKFLT: 16, + SIGCHLD: 17, + SIGCONT: 18, + SIGSTOP: 19, + SIGTSTP: 20, + SIGTTIN: 21, + SIGTTOU: 22, + SIGURG: 23, + SIGXCPU: 24, + SIGXFSZ: 25, + SIGVTALRM: 26, + SIGPROF: 27, + SIGWINCH: 28, + SIGIO: 29, + SIGPOLL: 29, + SIGPWR: 30, + SIGSYS: 31, + }; + } + + // errno numbers straight from the host's ; the values differ between + // Linux and the BSD/Darwin libc (e.g. EAGAIN is 11 vs 35), so they are branded + // per platform exactly as Node derives them from the system headers. Packages + // that compare `os.constants.errno.EXXX` against a raw errno resolve identically. + var errno; + if (process.platform === 'darwin') { + errno = { + E2BIG: 7, + EACCES: 13, + EADDRINUSE: 48, + EADDRNOTAVAIL: 49, + EAFNOSUPPORT: 47, + EAGAIN: 35, + EALREADY: 37, + EBADF: 9, + EBADMSG: 94, + EBUSY: 16, + ECANCELED: 89, + ECHILD: 10, + ECONNABORTED: 53, + ECONNREFUSED: 61, + ECONNRESET: 54, + EDEADLK: 11, + EDESTADDRREQ: 39, + EDOM: 33, + EDQUOT: 69, + EEXIST: 17, + EFAULT: 14, + EFBIG: 27, + EHOSTUNREACH: 65, + EIDRM: 90, + EILSEQ: 92, + EINPROGRESS: 36, + EINTR: 4, + EINVAL: 22, + EIO: 5, + EISCONN: 56, + EISDIR: 21, + ELOOP: 62, + EMFILE: 24, + EMLINK: 31, + EMSGSIZE: 40, + EMULTIHOP: 95, + ENAMETOOLONG: 63, + ENETDOWN: 50, + ENETRESET: 52, + ENETUNREACH: 51, + ENFILE: 23, + ENOBUFS: 55, + ENODATA: 96, + ENODEV: 19, + ENOENT: 2, + ENOEXEC: 8, + ENOLCK: 77, + ENOLINK: 97, + ENOMEM: 12, + ENOMSG: 91, + ENOPROTOOPT: 42, + ENOSPC: 28, + ENOSR: 98, + ENOSTR: 99, + ENOSYS: 78, + ENOTCONN: 57, + ENOTDIR: 20, + ENOTEMPTY: 66, + ENOTSOCK: 38, + ENOTSUP: 45, + ENOTTY: 25, + ENXIO: 6, + EOPNOTSUPP: 102, + EOVERFLOW: 84, + EPERM: 1, + EPIPE: 32, + EPROTO: 100, + EPROTONOSUPPORT: 43, + EPROTOTYPE: 41, + ERANGE: 34, + EROFS: 30, + ESPIPE: 29, + ESRCH: 3, + ESTALE: 70, + ETIME: 101, + ETIMEDOUT: 60, + ETXTBSY: 26, + EWOULDBLOCK: 35, + EXDEV: 18, + }; + } else if (isWindows) { + // MSVC : the C base (1..42) plus the POSIX-2008 additions (100..140). + errno = { + E2BIG: 7, + EACCES: 13, + EADDRINUSE: 100, + EADDRNOTAVAIL: 101, + EAFNOSUPPORT: 102, + EAGAIN: 11, + EALREADY: 103, + EBADF: 9, + EBADMSG: 104, + EBUSY: 16, + ECANCELED: 105, + ECHILD: 10, + ECONNABORTED: 106, + ECONNREFUSED: 107, + ECONNRESET: 108, + EDEADLK: 36, + EDESTADDRREQ: 109, + EDOM: 33, + EEXIST: 17, + EFAULT: 14, + EFBIG: 27, + EHOSTUNREACH: 110, + EIDRM: 111, + EILSEQ: 42, + EINPROGRESS: 112, + EINTR: 4, + EINVAL: 22, + EIO: 5, + EISCONN: 113, + EISDIR: 21, + ELOOP: 114, + EMFILE: 24, + EMLINK: 31, + EMSGSIZE: 115, + ENAMETOOLONG: 38, + ENETDOWN: 116, + ENETRESET: 117, + ENETUNREACH: 118, + ENFILE: 23, + ENOBUFS: 119, + ENODATA: 120, + ENODEV: 19, + ENOENT: 2, + ENOEXEC: 8, + ENOLCK: 39, + ENOLINK: 121, + ENOMEM: 12, + ENOMSG: 122, + ENOPROTOOPT: 123, + ENOSPC: 28, + ENOSR: 124, + ENOSTR: 125, + ENOSYS: 40, + ENOTCONN: 126, + ENOTDIR: 20, + ENOTEMPTY: 41, + ENOTRECOVERABLE: 127, + ENOTSOCK: 128, + ENOTSUP: 129, + ENOTTY: 25, + ENXIO: 6, + EOPNOTSUPP: 130, + EOVERFLOW: 132, + EOWNERDEAD: 133, + EPERM: 1, + EPIPE: 32, + EPROTO: 134, + EPROTONOSUPPORT: 135, + EPROTOTYPE: 136, + ERANGE: 34, + EROFS: 30, + ESPIPE: 29, + ESRCH: 3, + ETIME: 137, + ETIMEDOUT: 138, + ETXTBSY: 139, + EWOULDBLOCK: 140, + EXDEV: 18, + }; + } else { + // Linux asm-generic errno values (identical on x86_64/arm/arm64/riscv). + errno = { + E2BIG: 7, + EACCES: 13, + EADDRINUSE: 98, + EADDRNOTAVAIL: 99, + EAFNOSUPPORT: 97, + EAGAIN: 11, + EALREADY: 114, + EBADF: 9, + EBADMSG: 74, + EBUSY: 16, + ECANCELED: 125, + ECHILD: 10, + ECONNABORTED: 103, + ECONNREFUSED: 111, + ECONNRESET: 104, + EDEADLK: 35, + EDESTADDRREQ: 89, + EDOM: 33, + EDQUOT: 122, + EEXIST: 17, + EFAULT: 14, + EFBIG: 27, + EHOSTUNREACH: 113, + EIDRM: 43, + EILSEQ: 84, + EINPROGRESS: 115, + EINTR: 4, + EINVAL: 22, + EIO: 5, + EISCONN: 106, + EISDIR: 21, + ELOOP: 40, + EMFILE: 24, + EMLINK: 31, + EMSGSIZE: 90, + EMULTIHOP: 72, + ENAMETOOLONG: 36, + ENETDOWN: 100, + ENETRESET: 102, + ENETUNREACH: 101, + ENFILE: 23, + ENOBUFS: 105, + ENODATA: 61, + ENODEV: 19, + ENOENT: 2, + ENOEXEC: 8, + ENOLCK: 37, + ENOLINK: 67, + ENOMEM: 12, + ENOMSG: 42, + ENOPROTOOPT: 92, + ENOSPC: 28, + ENOSR: 63, + ENOSTR: 60, + ENOSYS: 38, + ENOTCONN: 107, + ENOTDIR: 20, + ENOTEMPTY: 39, + ENOTSOCK: 88, + ENOTSUP: 95, + ENOTTY: 25, + ENXIO: 6, + EOPNOTSUPP: 95, + EOVERFLOW: 75, + EPERM: 1, + EPIPE: 32, + EPROTO: 71, + EPROTONOSUPPORT: 93, + EPROTOTYPE: 91, + ERANGE: 34, + EROFS: 30, + ESPIPE: 29, + ESRCH: 3, + ESTALE: 116, + ETIME: 62, + ETIMEDOUT: 110, + ETXTBSY: 26, + EWOULDBLOCK: 11, + EXDEV: 18, + }; + } + + // libuv priority band (UV_PRIORITY_*): identical on every platform. setPriority + // validates user input is an integer in [HIGHEST, LOW] = [-20, 19] (no clamp). + var priority = { + PRIORITY_LOW: 19, + PRIORITY_BELOW_NORMAL: 10, + PRIORITY_NORMAL: 0, + PRIORITY_ABOVE_NORMAL: -7, + PRIORITY_HIGH: -14, + PRIORITY_HIGHEST: -20, + }; + + // dlopen flags (RTLD_*). Linux and Darwin disagree on the bit values, so brand + // them per host; absent on Windows (no dlopen). + var dlopen; + if (process.platform === 'darwin') { + dlopen = { RTLD_LAZY: 1, RTLD_NOW: 2, RTLD_LOCAL: 4, RTLD_GLOBAL: 8 }; + } else if (!isWindows) { + dlopen = { RTLD_LAZY: 1, RTLD_NOW: 2, RTLD_GLOBAL: 256, RTLD_LOCAL: 0 }; + } else { + dlopen = {}; + } + + var constants = { + UV_UDP_REUSEADDR: 4, + errno: errno, + signals: signals, + priority: priority, + dlopen: dlopen, + }; + + module.exports = constants; +}); diff --git a/pkg/runtime/js/node/os.js b/pkg/runtime/js/node/os.js index b3a017c..e71f974 100644 --- a/pkg/runtime/js/node/os.js +++ b/pkg/runtime/js/node/os.js @@ -1,7 +1,7 @@ // node:os — operating-system info, modeled on Node's `os`. The split mirrors // Node/libuv: anything that is a pure function of process.platform/process.env or // a fixed per-platform constant is computed here in JS (homedir, tmpdir, EOL, -// devNull, endianness, constants), while the live system numbers that require a +// devNull, endianness; the os.constants tables in internal/os/constants.js), while the live system numbers that require a // syscall (hostname, uname, total/free memory, uptime, load average, cpus, // network interfaces, user info, priority) come from the Odin bridge // (pkg/runtime/os.odin + the per-platform os_*.odin files) via `native`. @@ -93,380 +93,9 @@ } // ---- os.constants ---------------------------------------------------------- - // signals + errno are platform-specific numbers straight from the system - // headers; priority/dlopen/UV are libuv's cross-platform abstraction. Values - // mirror Node's so feature-detection and `process.kill(pid, 'SIGTERM')`-style - // lookups resolve to the same numbers the host kernel uses. - var signals; - if (process.platform === 'darwin') { - signals = { - SIGHUP: 1, - SIGINT: 2, - SIGQUIT: 3, - SIGILL: 4, - SIGTRAP: 5, - SIGABRT: 6, - SIGIOT: 6, - SIGEMT: 7, - SIGFPE: 8, - SIGKILL: 9, - SIGBUS: 10, - SIGSEGV: 11, - SIGSYS: 12, - SIGPIPE: 13, - SIGALRM: 14, - SIGTERM: 15, - SIGURG: 16, - SIGSTOP: 17, - SIGTSTP: 18, - SIGCONT: 19, - SIGCHLD: 20, - SIGTTIN: 21, - SIGTTOU: 22, - SIGIO: 23, - SIGXCPU: 24, - SIGXFSZ: 25, - SIGVTALRM: 26, - SIGPROF: 27, - SIGWINCH: 28, - SIGINFO: 29, - SIGUSR1: 30, - SIGUSR2: 31, - }; - } else if (isWindows) { - signals = { - SIGHUP: 1, - SIGINT: 2, - SIGILL: 4, - SIGABRT: 22, - SIGFPE: 8, - SIGKILL: 9, - SIGSEGV: 11, - SIGTERM: 15, - SIGBREAK: 21, - SIGWINCH: 28, - }; - } else { - signals = { - SIGHUP: 1, - SIGINT: 2, - SIGQUIT: 3, - SIGILL: 4, - SIGTRAP: 5, - SIGABRT: 6, - SIGIOT: 6, - SIGBUS: 7, - SIGFPE: 8, - SIGKILL: 9, - SIGUSR1: 10, - SIGSEGV: 11, - SIGUSR2: 12, - SIGPIPE: 13, - SIGALRM: 14, - SIGTERM: 15, - SIGSTKFLT: 16, - SIGCHLD: 17, - SIGCONT: 18, - SIGSTOP: 19, - SIGTSTP: 20, - SIGTTIN: 21, - SIGTTOU: 22, - SIGURG: 23, - SIGXCPU: 24, - SIGXFSZ: 25, - SIGVTALRM: 26, - SIGPROF: 27, - SIGWINCH: 28, - SIGIO: 29, - SIGPOLL: 29, - SIGPWR: 30, - SIGSYS: 31, - }; - } - - // errno numbers straight from the host's ; the values differ between - // Linux and the BSD/Darwin libc (e.g. EAGAIN is 11 vs 35), so they are branded - // per platform exactly as Node derives them from the system headers. Packages - // that compare `os.constants.errno.EXXX` against a raw errno resolve identically. - var errno; - if (process.platform === 'darwin') { - errno = { - E2BIG: 7, - EACCES: 13, - EADDRINUSE: 48, - EADDRNOTAVAIL: 49, - EAFNOSUPPORT: 47, - EAGAIN: 35, - EALREADY: 37, - EBADF: 9, - EBADMSG: 94, - EBUSY: 16, - ECANCELED: 89, - ECHILD: 10, - ECONNABORTED: 53, - ECONNREFUSED: 61, - ECONNRESET: 54, - EDEADLK: 11, - EDESTADDRREQ: 39, - EDOM: 33, - EDQUOT: 69, - EEXIST: 17, - EFAULT: 14, - EFBIG: 27, - EHOSTUNREACH: 65, - EIDRM: 90, - EILSEQ: 92, - EINPROGRESS: 36, - EINTR: 4, - EINVAL: 22, - EIO: 5, - EISCONN: 56, - EISDIR: 21, - ELOOP: 62, - EMFILE: 24, - EMLINK: 31, - EMSGSIZE: 40, - EMULTIHOP: 95, - ENAMETOOLONG: 63, - ENETDOWN: 50, - ENETRESET: 52, - ENETUNREACH: 51, - ENFILE: 23, - ENOBUFS: 55, - ENODATA: 96, - ENODEV: 19, - ENOENT: 2, - ENOEXEC: 8, - ENOLCK: 77, - ENOLINK: 97, - ENOMEM: 12, - ENOMSG: 91, - ENOPROTOOPT: 42, - ENOSPC: 28, - ENOSR: 98, - ENOSTR: 99, - ENOSYS: 78, - ENOTCONN: 57, - ENOTDIR: 20, - ENOTEMPTY: 66, - ENOTSOCK: 38, - ENOTSUP: 45, - ENOTTY: 25, - ENXIO: 6, - EOPNOTSUPP: 102, - EOVERFLOW: 84, - EPERM: 1, - EPIPE: 32, - EPROTO: 100, - EPROTONOSUPPORT: 43, - EPROTOTYPE: 41, - ERANGE: 34, - EROFS: 30, - ESPIPE: 29, - ESRCH: 3, - ESTALE: 70, - ETIME: 101, - ETIMEDOUT: 60, - ETXTBSY: 26, - EWOULDBLOCK: 35, - EXDEV: 18, - }; - } else if (isWindows) { - // MSVC : the C base (1..42) plus the POSIX-2008 additions (100..140). - errno = { - E2BIG: 7, - EACCES: 13, - EADDRINUSE: 100, - EADDRNOTAVAIL: 101, - EAFNOSUPPORT: 102, - EAGAIN: 11, - EALREADY: 103, - EBADF: 9, - EBADMSG: 104, - EBUSY: 16, - ECANCELED: 105, - ECHILD: 10, - ECONNABORTED: 106, - ECONNREFUSED: 107, - ECONNRESET: 108, - EDEADLK: 36, - EDESTADDRREQ: 109, - EDOM: 33, - EEXIST: 17, - EFAULT: 14, - EFBIG: 27, - EHOSTUNREACH: 110, - EIDRM: 111, - EILSEQ: 42, - EINPROGRESS: 112, - EINTR: 4, - EINVAL: 22, - EIO: 5, - EISCONN: 113, - EISDIR: 21, - ELOOP: 114, - EMFILE: 24, - EMLINK: 31, - EMSGSIZE: 115, - ENAMETOOLONG: 38, - ENETDOWN: 116, - ENETRESET: 117, - ENETUNREACH: 118, - ENFILE: 23, - ENOBUFS: 119, - ENODATA: 120, - ENODEV: 19, - ENOENT: 2, - ENOEXEC: 8, - ENOLCK: 39, - ENOLINK: 121, - ENOMEM: 12, - ENOMSG: 122, - ENOPROTOOPT: 123, - ENOSPC: 28, - ENOSR: 124, - ENOSTR: 125, - ENOSYS: 40, - ENOTCONN: 126, - ENOTDIR: 20, - ENOTEMPTY: 41, - ENOTRECOVERABLE: 127, - ENOTSOCK: 128, - ENOTSUP: 129, - ENOTTY: 25, - ENXIO: 6, - EOPNOTSUPP: 130, - EOVERFLOW: 132, - EOWNERDEAD: 133, - EPERM: 1, - EPIPE: 32, - EPROTO: 134, - EPROTONOSUPPORT: 135, - EPROTOTYPE: 136, - ERANGE: 34, - EROFS: 30, - ESPIPE: 29, - ESRCH: 3, - ETIME: 137, - ETIMEDOUT: 138, - ETXTBSY: 139, - EWOULDBLOCK: 140, - EXDEV: 18, - }; - } else { - // Linux asm-generic errno values (identical on x86_64/arm/arm64/riscv). - errno = { - E2BIG: 7, - EACCES: 13, - EADDRINUSE: 98, - EADDRNOTAVAIL: 99, - EAFNOSUPPORT: 97, - EAGAIN: 11, - EALREADY: 114, - EBADF: 9, - EBADMSG: 74, - EBUSY: 16, - ECANCELED: 125, - ECHILD: 10, - ECONNABORTED: 103, - ECONNREFUSED: 111, - ECONNRESET: 104, - EDEADLK: 35, - EDESTADDRREQ: 89, - EDOM: 33, - EDQUOT: 122, - EEXIST: 17, - EFAULT: 14, - EFBIG: 27, - EHOSTUNREACH: 113, - EIDRM: 43, - EILSEQ: 84, - EINPROGRESS: 115, - EINTR: 4, - EINVAL: 22, - EIO: 5, - EISCONN: 106, - EISDIR: 21, - ELOOP: 40, - EMFILE: 24, - EMLINK: 31, - EMSGSIZE: 90, - EMULTIHOP: 72, - ENAMETOOLONG: 36, - ENETDOWN: 100, - ENETRESET: 102, - ENETUNREACH: 101, - ENFILE: 23, - ENOBUFS: 105, - ENODATA: 61, - ENODEV: 19, - ENOENT: 2, - ENOEXEC: 8, - ENOLCK: 37, - ENOLINK: 67, - ENOMEM: 12, - ENOMSG: 42, - ENOPROTOOPT: 92, - ENOSPC: 28, - ENOSR: 63, - ENOSTR: 60, - ENOSYS: 38, - ENOTCONN: 107, - ENOTDIR: 20, - ENOTEMPTY: 39, - ENOTSOCK: 88, - ENOTSUP: 95, - ENOTTY: 25, - ENXIO: 6, - EOPNOTSUPP: 95, - EOVERFLOW: 75, - EPERM: 1, - EPIPE: 32, - EPROTO: 71, - EPROTONOSUPPORT: 93, - EPROTOTYPE: 91, - ERANGE: 34, - EROFS: 30, - ESPIPE: 29, - ESRCH: 3, - ESTALE: 116, - ETIME: 62, - ETIMEDOUT: 110, - ETXTBSY: 26, - EWOULDBLOCK: 11, - EXDEV: 18, - }; - } - - // libuv priority band (UV_PRIORITY_*): identical on every platform. setPriority - // validates user input is an integer in [HIGHEST, LOW] = [-20, 19] (no clamp). - var priority = { - PRIORITY_LOW: 19, - PRIORITY_BELOW_NORMAL: 10, - PRIORITY_NORMAL: 0, - PRIORITY_ABOVE_NORMAL: -7, - PRIORITY_HIGH: -14, - PRIORITY_HIGHEST: -20, - }; - - // dlopen flags (RTLD_*). Linux and Darwin disagree on the bit values, so brand - // them per host; absent on Windows (no dlopen). - var dlopen; - if (process.platform === 'darwin') { - dlopen = { RTLD_LAZY: 1, RTLD_NOW: 2, RTLD_LOCAL: 4, RTLD_GLOBAL: 8 }; - } else if (!isWindows) { - dlopen = { RTLD_LAZY: 1, RTLD_NOW: 2, RTLD_GLOBAL: 256, RTLD_LOCAL: 0 }; - } else { - dlopen = {}; - } - - var constants = { - UV_UDP_REUSEADDR: 4, - errno: errno, - signals: signals, - priority: priority, - dlopen: dlopen, - }; + // The tables live in internal/os/constants.js — node reads them from its C++ + // constants binding, not from lib/os.js. One object per process, as before. + var constants = require('internal/os/constants'); // ---- cpus(): the native bridge returns plain records; pass them through ---- // Each entry is { model, speed, times: { user, nice, sys, idle, irq } }, the diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 5d4fe00..6114eac 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2570,6 +2570,15 @@ "replace": " internalBuffer.addBufferPrototypeMethods(p);\n p.readUInt8 = p.readUint8 = function (offset) {\n if (offset < 0 || offset >= this.length) throw ERR_OUT_OF_RANGE('offset', '>= 0', offset);\n return this[offset];\n };\n", "gate": "compat:tests/node-compat/cases/85-buffer-internal-modules.js", "expect_detail": "lava: readUInt8 offset: internal/buffer got node:buffer" + }, + { + "name": "86-os-internal-modules dies when internal/os/constants drops a table from os.constants", + "why": "os.constants is now assembled in internal/os/constants.js, outside node:os; the case's per-table lines are what show the moved object still carries every table node's does, so losing one in the move must turn it red.", + "source": "pkg/runtime/js/internal/os/constants.js", + "find": " dlopen: dlopen,\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/86-os-internal-modules.js", + "expect_detail": "node: constants dlopen object" } ] } diff --git a/tests/node-compat/cases/86-os-internal-modules.js b/tests/node-compat/cases/86-os-internal-modules.js new file mode 100644 index 0000000..c43bc6a --- /dev/null +++ b/tests/node-compat/cases/86-os-internal-modules.js @@ -0,0 +1,68 @@ +// node:os is assembled from two files: js/internal/os/constants.js holds the signal, +// errno, priority and dlopen tables (node reads them from its C++ constants binding, +// internalBinding('constants').os, not from lib/os.js), and node/os keeps every +// function. The tables are pure data — nothing in them throws — so no stack frame moves +// and this case has no frame lines. +// +// The new id must stay internal: neither the bare nor the `node:` spelling may resolve +// from user code. Only the error CODE of the bare spelling is printed (the messages +// diverge, #415). +// +// The export lines check what crosses the new seam: os's key SET (sorted — the two +// runtimes list them in a different order, a pre-existing gap), os.constants' top-level +// tables and their types, that it is one object across requires, and values that agree +// on every platform. Full table listings are left out: signals carries an extra SIGEMT +// on darwin here, and node's src/node_constants.cc adds RTLD_DEEPBIND to dlopen where +// glibc defines it, which this table does not — both pre-existing, outside the split. +// +// There is no line that is red against the pre-split binary: the split is meant to be +// invisible, and the id was unresolvable before it as well. What this case pins is that +// the assembled module still answers the same — tests/mutation-manifest.json kills it by +// dropping a table from the moved file. +'use strict'; + +const os = require('os'); + +for (const id of ['internal/os/constants', 'internal/os', 'os/constants']) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +console.log('same module', require('node:os') === os); +console.log('keys', Object.keys(os).sort().join(',')); + +const c = os.constants; +console.log('constants identity', c === require('node:os').constants, c === os.constants); +for (const k of Object.keys(c).sort()) { + console.log('constants', k, typeof c[k]); +} +console.log( + 'values', + c.UV_UDP_REUSEADDR, + c.signals.SIGINT, + c.signals.SIGKILL, + c.signals.SIGTERM, + c.errno.EPERM, + c.errno.ENOENT, + c.errno.EACCES, + c.dlopen.RTLD_LAZY, + c.dlopen.RTLD_NOW, +); +console.log( + 'priority', + Object.keys(c.priority) + .sort() + .map((k) => k + '=' + c.priority[k]) + .join(','), +); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 0de843c..8534b81 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -155,6 +155,12 @@ "accessor": 0, "global": 4 }, + "internal/os/constants.js": { + "method": 0, + "invoke": 0, + "accessor": 0, + "global": 0 + }, "internal/parse_args.js": { "method": 0, "invoke": 0, From 5228c42573366f3683b2ce99f2ca909e3b7c1633 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Mon, 16 Nov 2026 23:51:22 +0200 Subject: [PATCH 40/61] refactor(url): move the WHATWG URL implementation to internal/url node/url.js 2289 -> 152 lines, mirroring node's lib/url.js + lib/internal/url.js. Moved throw sites now frame node:internal/url as node's do; none moved away. internal/url is eager through req('url'), so its captures stay exempt. Case 87 pins the layout. Co-Authored-By: Claude Opus 5.5 --- .../odin-feature-reference/odin-sdk-map.md | 2 +- agents/prompts/pr-gate-reference/gates.md | 4 +- bench/micro/url.js | 2 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 13 +- pkg/runtime/js/internal/url.js | 2196 +++++++++++++++++ pkg/runtime/js/node/url.js | 2183 +--------------- runtime/gates/route-gates.mjs | 4 + runtime/gates/route-gates.test.mjs | 8 + tests/mutation-manifest.json | 26 +- .../cases/87-url-internal-modules.js | 138 ++ tests/node-compat/pollution-baseline.json | 12 +- 12 files changed, 2416 insertions(+), 2174 deletions(-) create mode 100644 pkg/runtime/js/internal/url.js create mode 100644 tests/node-compat/cases/87-url-internal-modules.js diff --git a/agents/prompts/odin-feature-reference/odin-sdk-map.md b/agents/prompts/odin-feature-reference/odin-sdk-map.md index 32cb1d3..641c423 100644 --- a/agents/prompts/odin-feature-reference/odin-sdk-map.md +++ b/agents/prompts/odin-feature-reference/odin-sdk-map.md @@ -40,7 +40,7 @@ Verified against `odin version dev-2026-07`. Packages prefixed with `_` | ------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- | | TCP/UDP sockets, addresses, options | `core:net` (`socket.odin`, `addr.odin`) | | DNS resolution without `getaddrinfo` | `core:net` (`dns.odin` — parses `/etc/resolv.conf` + `/etc/hosts`, speaks DNS itself; already used by `pkg/runtime/dns.odin`) | -| URL split/join/query | `core:net/url.odin` (byte-level; WHATWG URL semantics stay in `js/node/url.js`) | +| URL split/join/query | `core:net/url.odin` (byte-level; WHATWG URL semantics stay in `js/internal/url.js`) | | Network interface enumeration | `core:net` (`interface.odin`) | | Async I/O abstraction | `core:nbio` — read before adding a backend; Lava's own loop already covers the same ground, so treat it as a design reference, not a drop-in | | io_uring | `core:sys/linux/uring` (used by the loop's Linux proactor) | diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index 7b01457..c968a9a 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -69,8 +69,8 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `require.odin`, `module_resolution.odin`, `js/bootstrap/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | | `crypto.odin`, `js/node/crypto.js`, `js/internal/crypto/*.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | | `pkg/runtime/js/**/*.js` (any under the scan root — `bootstrap/`, `node/`, `internal/`; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | -| `js/node/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | -| `js/internal/{encoding,buffer}.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | +| `js/{node,internal}/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | +| `js/internal/{encoding,buffer,url}.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | | `scripts/**` | `make test-scripts` — node:test over the build tooling | | any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | | `pkg/runtime/globals.odin`, `runtime.odin`, `errors.odin` | `make test-odin`, `make test-compat-lava` | diff --git a/bench/micro/url.js b/bench/micro/url.js index fa36954..48da4fb 100644 --- a/bench/micro/url.js +++ b/bench/micro/url.js @@ -12,7 +12,7 @@ // decode-utf8-stream at 17.0. So these numbers are a measuring instrument for work // that has not been done yet, and only secondarily a regression net. // -// SHAPES. Each is a distinct code path in js/node/url.js, not a variation in +// SHAPES. Each is a distinct code path in js/internal/url.js, not a variation in // input length: // special the common case — scheme, host, path, query, fragment // ipv4 dotted-quad host, which runs endsInANumber + parseIPv4Number per label diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 36af5b9..3562793 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -104,7 +104,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/buffer", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/buffer", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/url", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f2fcd05..ef41b69 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -182,7 +182,8 @@ at its own top level, so the late edge is what keeps the graph acyclic), as node `internal/blob` is eager yet takes it too, at call time: `buffer` requires it from its own factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. -The JS layer is substantial and high quality: `url.js` (2071 LOC, WHATWG URL), +The JS layer is substantial and high quality: URL (~2350 across `node/url.js` and +`internal/url.js`, WHATWG URL), Web Streams (~2100 across `internal/webstreams/`), buffer (~1530 across `node/buffer.js` and `internal/buffer.js`; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), fetch @@ -237,7 +238,15 @@ same `loader.js` aliasing, and is eager through `req('buffer')`. `os` moves only data: `internal/os/constants` holds the signal, errno, priority and dlopen tables, which node reads from its C++ constants binding rather than `lib/os.js`. The tables throw nothing, so no frame moves; they call no native, so there is no alias, and the file stays -lazy behind `require('os')`. Errors use Node-shaped +lazy behind `require('os')`. URL takes node's `lib/internal/url` seam: `internal/url` holds +the URL Standard parser, `URL`, `URLSearchParams`, `fileURLToPath`, `urlToHttpOptions` and +`domainToASCII`/`domainToUnicode`, so an invalid URL or a bad `fileURLToPath` argument frames +`node:internal/url` as node's does. `node/url` keeps `pathToFileURL` and `format`, which +node's `lib/url.js` defines and frames in `node:url`, installs the `URL`/`URLSearchParams` +globals (after `buffer`, keeping its object-URL statics) and assembles the exports in their +old order, borrowing the record slot, the path serializer and the Punycode decoder for +`format`. Url calls no native, so there is no alias; `internal/url` is eager through +`req('url')`, which keeps its module-eval captures pristine. Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, diff --git a/pkg/runtime/js/internal/url.js b/pkg/runtime/js/internal/url.js new file mode 100644 index 0000000..fc0473c --- /dev/null +++ b/pkg/runtime/js/internal/url.js @@ -0,0 +1,2196 @@ +// internal/url — node:url's WHATWG half, laid out like node's lib/internal/url.js: +// the URL Standard parser (percent-encoding, host/IPv4/IPv6/IDNA, the state +// machine), `URL`, `URLSearchParams`, `fileURLToPath`, `urlToHttpOptions` and the +// validating `domainToASCII`/`domainToUnicode`. Implemented in JS (as Node does for +// the parsing-heavy modules like node:path) so the fiddly state-machine semantics +// match the URL Standard and Node byte-for-byte. JavaScriptCore's bare engine ships +// no URL/URLSearchParams (those are WebCore APIs), so the whole surface lives here. +// +// node/url.js stays the public entry: it keeps `pathToFileURL` and `format`, whose +// throws node frames in `node:url`, installs the `URL`/`URLSearchParams` globals and +// assembles the exports. Everything thrown from this file frames +// `node:internal/url`, as node's does (pinned by +// tests/node-compat/cases/87-url-internal-modules.js). Not publicly requireable. +// +// Deliberate deviation from Node, documented in reference/node-compatibility.md: +// IDNA/UTS-46 host processing is approximated (NFC + lowercase + Punycode) rather +// than the full ICU mapping; common Unicode domains round-trip, but exotic +// mapping/validation edge cases may differ. +(function (require, module, _exports) { + 'use strict'; + + // --- Pollution-safe intrinsics ------------------------------------------- + // URL parsing runs on attacker-influenced strings while user code may have + // poisoned shared prototypes/globals. Prototype methods route through the + // pristine primordials table; free globals and statics are captured HERE, at + // module-eval, which the loader runs before any user code — so these bindings + // are pristine and cannot be re-pointed later (a poisoned Math.floor, String, + // decodeURIComponent, or String.prototype.normalize otherwise silently + // substitutes hosts / disables IPv4 normalization; see the URL vector audit). + var P = require('internal/primordials'); + // Every regex below runs over a caller-supplied URL, so none may go through + // `re.test(...)`: `RegExp.prototype.exec` is a writable data property and the + // spec's RegExpExec re-reads it off the receiver, so a plain assignment steers + // `test` as well — a captured `test` included. primordials exports no + // `RegExpPrototypeTest` for exactly that reason. `%2f`/`%5c` detection below is + // the path-traversal check, so this is not cosmetic. + // + // The last GLOBAL regex in this file was toUSVString's, now a code-unit walk. The + // one non-global `StringPrototypeReplace(…, /re/, …)` site left in node:url is + // pathToFileURL's, which moved to node/url.js with it. + var reTest = P.RegExpMatches; + var allChars = P.allChars; + var isAsciiDigit = P.isAsciiDigit; + var isAsciiHexDigit = P.isAsciiHexDigit; + var isAsciiOctalDigit = P.isAsciiOctalDigit; + var PERCENT_2F_RE = /%2f/i; + var PERCENT_5C_RE = /%5c/i; + var ArrayPrototypePush = P.ArrayPrototypePush; + var ArrayPrototypePop = P.ArrayPrototypePop; + var ArrayPrototypeShift = P.ArrayPrototypeShift; + var ArrayPrototypeSlice = P.ArrayPrototypeSlice; + var ArrayPrototypeSplice = P.ArrayPrototypeSplice; + var ArrayPrototypeJoin = P.ArrayPrototypeJoin; + var ArrayPrototypeSort = P.ArrayPrototypeSort; + var ArrayPrototypeMap = P.ArrayPrototypeMap; + var ArrayPrototypeIndexOf = P.ArrayPrototypeIndexOf; + var ArrayPrototypeIncludes = P.ArrayPrototypeIncludes; + var StringPrototypeSlice = P.StringPrototypeSlice; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeCodePointAt = P.StringPrototypeCodePointAt; + var StringPrototypeCharAt = P.StringPrototypeCharAt; + var StringPrototypeIndexOf = P.StringPrototypeIndexOf; + var StringPrototypeReplace = P.StringPrototypeReplace; + var StringPrototypeReplaceAll = P.StringPrototypeReplaceAll; + var StringPrototypeSplit = P.StringPrototypeSplit; + var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; + var StringPrototypeToUpperCase = P.StringPrototypeToUpperCase; + var StringPrototypeStartsWith = P.StringPrototypeStartsWith; + var StringPrototypeNormalize = P.StringPrototypeNormalize; + var StringPrototypeSubstr = P.StringPrototypeSubstr; + var ObjectCreate = P.ObjectCreate; + var ObjectKeys = P.ObjectKeys; + var ObjectDefineProperty = P.ObjectDefineProperty; + var ArrayFrom = P.ArrayFrom; + // Free globals / statics, captured pristine at module-eval. + var StringG = String; + var NumberG = P.Number; + var TypeErrorG = P.TypeError; + var errors = require('internal/errors'); + var nodeError = errors.nodeError; + var nativeError = errors.nativeError; + var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; + var StringFromCharCode = String.fromCharCode; + var StringFromCodePoint = String.fromCodePoint; + var MathFloor = Math.floor; + var MathPow = Math.pow; + var parseIntG = P.parseInt; + var decodeURIComponentG = decodeURIComponent; + var isFiniteG = isFinite; + var TextEncoderG = TextEncoder; + var TextDecoderG = TextDecoder; + + // TextEncoder/TextDecoder are installed by internal/encoding.js. This module is + // eagerly instantiated to install the URL/URLSearchParams globals, so the codecs + // are created lazily (on first parse/serialize) rather than at module-eval time — + // that keeps url.js robust to the loader's eager-instantiation order, since no + // URL operation runs until user code, by which point every builtin has loaded. + var encoderInstance = null; + var decoderInstance = null; + var fatalDecoderInstance = null; + function encoder() { + return encoderInstance || (encoderInstance = new TextEncoderG()); + } + function decoder() { + return decoderInstance || (decoderInstance = new TextDecoderG('utf-8')); + } + function fatalDecoder() { + return ( + fatalDecoderInstance || (fatalDecoderInstance = new TextDecoderG('utf-8', { fatal: true })) + ); + } + + // A unique sentinel distinct from any valid parser result; using a symbol means + // it can never collide with a legitimately-produced host string or number. + var FAILURE = { failure: true }; + + // ------------------------------------------------------------------ // + // Percent-encoding // + // ------------------------------------------------------------------ // + + // stripChars: `replace(/[…]/g, '')` without a RegExp. + // + // A GLOBAL regex replace is the sharpest shape of the exec re-read, and it does + // not merely answer wrongly: `RegExp.prototype[Symbol.replace]` loops on + // RegExpExec and advances `lastIndex` only on an EMPTY match, so a forged + // non-empty result never terminates. Measured on `bin/lava` before this change, + // with `RegExp.prototype.exec` replaced: `new URL('http://EXAMPLE.com/')` never + // returned and reached 1.4 GB RSS in four seconds, and a remote server's + // `Location:` header reaching `new URL(location, req.url)` hung the client. + // Routing through a captured `exec` would fix the steering and keep the spin. + // + // `pred` is a code-point predicate, so the caller keeps its character set + // explicit and no RegExp exists to poison. Returns the input unchanged when + // nothing matches, which is the common case for both callers. + function stripChars(s, pred) { + var n = s.length; + var i = 0; + for (; i < n; i++) { + if (pred(StringPrototypeCharCodeAt(s, i))) break; + } + if (i === n) return s; + var out = StringPrototypeSlice(s, 0, i); + for (; i < n; i++) { + var c = StringPrototypeCharCodeAt(s, i); + if (!pred(c)) out += StringFromCharCode(c); + } + return out; + } + + function isTabOrNewline(c) { + return c === 0x09 || c === 0x0a || c === 0x0d; + } + + // U+3002 IDEOGRAPHIC FULL STOP, U+FF0E FULLWIDTH FULL STOP, U+FF61 HALFWIDTH + // IDEOGRAPHIC FULL STOP — the IDNA label separators that fold to '.'. Getting + // this wrong is host confusion, not a cosmetic difference. + function isIdnaSeparator(c) { + return c === 0x3002 || c === 0xff0e || c === 0xff61; + } + + // The fold, not a strip: these three map TO '.', they are not removed. + function foldIdnaSeparators(s) { + var n = s.length; + var i = 0; + for (; i < n; i++) { + if (isIdnaSeparator(StringPrototypeCharCodeAt(s, i))) break; + } + if (i === n) return s; + var out = StringPrototypeSlice(s, 0, i); + for (; i < n; i++) { + var c = StringPrototypeCharCodeAt(s, i); + out += isIdnaSeparator(c) ? '.' : StringFromCharCode(c); + } + return out; + } + + // The WHATWG percent-encode sets, each a predicate over a code point. Every set + // is a superset of the C0-control set (control chars and everything past 0x7E). + function inC0ControlSet(cp) { + return cp <= 0x1f || cp > 0x7e; + } + function trimC0ControlOrSpace(str) { + var start = 0; + var end = str.length; + while (start < end && StringPrototypeCharCodeAt(str, start) <= 0x20) start++; + while (end > start && StringPrototypeCharCodeAt(str, end - 1) <= 0x20) end--; + return start === 0 && end === str.length ? str : str.slice(start, end); + } + function inFragmentSet(cp) { + return ( + inC0ControlSet(cp) || cp === 0x20 || cp === 0x22 || cp === 0x3c || cp === 0x3e || cp === 0x60 + ); + } + function inQuerySet(cp) { + return ( + inC0ControlSet(cp) || cp === 0x20 || cp === 0x22 || cp === 0x23 || cp === 0x3c || cp === 0x3e + ); + } + function inSpecialQuerySet(cp) { + return inQuerySet(cp) || cp === 0x27; + } + function inPathSet(cp) { + return inQuerySet(cp) || cp === 0x3f || cp === 0x60 || cp === 0x7b || cp === 0x7d; + } + function inUserinfoSet(cp) { + return ( + inPathSet(cp) || + cp === 0x2f || + cp === 0x3a || + cp === 0x3b || + cp === 0x3d || + cp === 0x40 || + cp === 0x5b || + cp === 0x5c || + cp === 0x5d || + cp === 0x5e || + cp === 0x7c + ); + } + + // Precomputed per-byte artifacts for the hot per-character encode path: + // PCT[b] is '%XX', ASCII_CHAR[b] the one-character string. Built once. + var PCT = new Array(256); + var ASCII_CHAR = new Array(128); + for (var pctB = 0; pctB < 256; pctB++) { + var pctHex = pctB.toString(16).toUpperCase(); + PCT[pctB] = '%' + (pctHex.length === 1 ? '0' + pctHex : pctHex); + if (pctB < 128) ASCII_CHAR[pctB] = StringFromCharCode(pctB); + } + + function percentEncodeByte(b) { + return PCT[b]; + } + + // Encode ONE code point against `inSet` without round-tripping through a + // string or TextEncoder. The parser's state machine calls this once per + // character of userinfo/path/query/fragment, so it is the hottest loop in + // the whole parse; every set encodes all non-ASCII (> 0x7E), so the UTF-8 + // bytes are hand-rolled from the code point. + function percentEncodeCp(cp, inSet) { + if (cp < 0x80) return inSet(cp) ? PCT[cp] : ASCII_CHAR[cp]; + if (cp < 0x800) return PCT[0xc0 | (cp >> 6)] + PCT[0x80 | (cp & 0x3f)]; + if (cp < 0x10000) + return PCT[0xe0 | (cp >> 12)] + PCT[0x80 | ((cp >> 6) & 0x3f)] + PCT[0x80 | (cp & 0x3f)]; + return ( + PCT[0xf0 | (cp >> 18)] + + PCT[0x80 | ((cp >> 12) & 0x3f)] + + PCT[0x80 | ((cp >> 6) & 0x3f)] + + PCT[0x80 | (cp & 0x3f)] + ); + } + + // UTF-8 percent-encode a string: code points in `inSet` (or any non-ASCII, since + // the sets all include >0x7E) are encoded to their UTF-8 bytes; the rest pass + // through. Iterates by code point so astral characters encode as four bytes. + function utf8PercentEncode(str, inSet) { + var out = ''; + for (var i = 0; i < str.length; i++) { + var cp = StringPrototypeCodePointAt(str, i); + if (cp > 0xffff) i++; // consumed a surrogate pair + out += percentEncodeCp(cp, inSet); + } + return out; + } + + // One-character string for a code point without the StringFromCodePoint + // host-call for ASCII — the state machine appends to its buffer through this. + function cpToStr(cp) { + return cp < 0x80 ? ASCII_CHAR[cp] : StringFromCodePoint(cp); + } + + // Percent-decode a string to its raw byte sequence. Non-%XX characters + // contribute their own UTF-8 bytes so raw multibyte input survives. + function percentDecodeBytes(str) { + var bytes = []; + for (var i = 0; i < str.length; i++) { + var c = StringPrototypeCharCodeAt(str, i); + if ( + c === 0x25 && + i + 2 < str.length && + isAsciiHexDigit(StringPrototypeCharCodeAt(str, i + 1)) && + isAsciiHexDigit(StringPrototypeCharCodeAt(str, i + 2)) + ) { + ArrayPrototypePush(bytes, parseIntG(StringPrototypeSubstr(str, i + 1, 2), 16)); + i += 2; + } else { + var cp = StringPrototypeCodePointAt(str, i); + if (cp > 0xffff) i++; + var enc = encoder().encode(StringFromCodePoint(cp)); + for (var k = 0; k < enc.length; k++) ArrayPrototypePush(bytes, enc[k]); + } + } + return new Uint8Array(bytes); + } + + // Lenient percent-decode (invalid UTF-8 → U+FFFD), used for query/form parsing. + function percentDecode(str) { + return decoder().decode(percentDecodeBytes(str)); + } + + // Strict percent-decode for host parsing: invalid UTF-8 is a parse failure, as + // the URL Standard's domain-to-ASCII step rejects it (Node throws to match). + function percentDecodeHostStrict(str) { + try { + return fatalDecoder().decode(percentDecodeBytes(str)); + } catch { + return FAILURE; + } + } + + // ------------------------------------------------------------------ // + // Punycode (RFC 3492) — used by the simplified IDNA host processing. // + // ------------------------------------------------------------------ // + + var PUNY_BASE = 36; + var PUNY_TMIN = 1; + var PUNY_TMAX = 26; + var PUNY_SKEW = 38; + var PUNY_DAMP = 700; + var PUNY_INITIAL_BIAS = 72; + var PUNY_INITIAL_N = 128; + + function punyAdapt(delta, numPoints, firstTime) { + delta = firstTime ? MathFloor(delta / PUNY_DAMP) : delta >> 1; + delta += MathFloor(delta / numPoints); + var k = 0; + while (delta > ((PUNY_BASE - PUNY_TMIN) * PUNY_TMAX) >> 1) { + delta = MathFloor(delta / (PUNY_BASE - PUNY_TMIN)); + k += PUNY_BASE; + } + return k + MathFloor(((PUNY_BASE - PUNY_TMIN + 1) * delta) / (delta + PUNY_SKEW)); + } + + function punyEncodeDigit(d) { + // 0..25 -> a..z, 26..35 -> 0..9 + return d + 22 + (d < 26 ? 75 : 0); + } + function punyDecodeDigit(cp) { + if (cp - 48 < 10) return cp - 22; // 0..9 + if (cp - 65 < 26) return cp - 65; // A..Z + if (cp - 97 < 26) return cp - 97; // a..z + return PUNY_BASE; + } + + function punycodeEncode(input) { + var output = []; + var codePoints = []; + for (var s = 0; s < input.length; s++) { + var cp = StringPrototypeCodePointAt(input, s); + if (cp > 0xffff) s++; + ArrayPrototypePush(codePoints, cp); + } + var n = PUNY_INITIAL_N; + var delta = 0; + var bias = PUNY_INITIAL_BIAS; + var basicLength = 0; + var i; + for (i = 0; i < codePoints.length; i++) { + if (codePoints[i] < 0x80) { + ArrayPrototypePush(output, codePoints[i]); + basicLength++; + } + } + var handled = basicLength; + if (basicLength > 0) ArrayPrototypePush(output, 0x2d); // '-' + while (handled < codePoints.length) { + var m = 0x7fffffff; + for (i = 0; i < codePoints.length; i++) { + if (codePoints[i] >= n && codePoints[i] < m) m = codePoints[i]; + } + delta += (m - n) * (handled + 1); + n = m; + for (i = 0; i < codePoints.length; i++) { + var c = codePoints[i]; + if (c < n) delta++; + if (c === n) { + var q = delta; + for (var k = PUNY_BASE; ; k += PUNY_BASE) { + var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; + if (q < t) break; + ArrayPrototypePush(output, punyEncodeDigit(t + ((q - t) % (PUNY_BASE - t)))); + q = MathFloor((q - t) / (PUNY_BASE - t)); + } + ArrayPrototypePush(output, punyEncodeDigit(q)); + bias = punyAdapt(delta, handled + 1, handled === basicLength); + delta = 0; + handled++; + } + } + delta++; + n++; + } + var str = ''; + for (i = 0; i < output.length; i++) str += StringFromCharCode(output[i]); + return str; + } + + function punycodeDecode(input) { + var output = []; + var n = PUNY_INITIAL_N; + var bias = PUNY_INITIAL_BIAS; + var i = 0; + var basic = input.lastIndexOf('-'); + if (basic < 0) basic = 0; + for (var j = 0; j < basic; j++) { + if (StringPrototypeCharCodeAt(input, j) >= 0x80) throw new RangeError('Invalid input'); + ArrayPrototypePush(output, StringPrototypeCharCodeAt(input, j)); + } + var index = basic > 0 ? basic + 1 : 0; + while (index < input.length) { + var oldi = i; + var w = 1; + for (var k = PUNY_BASE; ; k += PUNY_BASE) { + if (index >= input.length) throw new RangeError('Invalid input'); + var digit = punyDecodeDigit(StringPrototypeCharCodeAt(input, index++)); + if (digit >= PUNY_BASE) throw new RangeError('Invalid input'); + i += digit * w; + var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; + if (digit < t) break; + w *= PUNY_BASE - t; + } + var outLen = output.length + 1; + bias = punyAdapt(i - oldi, outLen, oldi === 0); + n += MathFloor(i / outLen); + i %= outLen; + ArrayPrototypeSplice(output, i, 0, n); + i++; + } + var str = ''; + for (var o = 0; o < output.length; o++) str += StringFromCodePoint(output[o]); + return str; + } + + function hasNonASCII(str) { + for (var i = 0; i < str.length; i++) { + if (StringPrototypeCharCodeAt(str, i) > 0x7f) return true; + } + return false; + } + + // Simplified IDNA ToASCII. Approximates UTS-46 by: normalizing the three IDNA + // label separators to '.', applying NFKC + lowercasing each Unicode label (so + // fullwidth/compatibility characters fold the way ICU maps them), and emitting + // the Punycode "xn--…" form. ASCII labels are just lowercased. Not the full ICU + // mapping table, but round-trips the overwhelming majority of real domains. + function domainToASCII(domain) { + if (domain === '') return ''; + // IDEOGRAPHIC FULL STOP, FULLWIDTH FULL STOP, HALFWIDTH IDEOGRAPHIC FULL STOP. + // Fold the three IDNA separators to '.', without a RegExp — see stripChars. + domain = foldIdnaSeparators(domain); + var labels = StringPrototypeSplit(domain, '.'); + var out = []; + for (var i = 0; i < labels.length; i++) { + var label = labels[i]; + if (hasNonASCII(label)) { + var mapped = ( + label.normalize ? StringPrototypeNormalize(label, 'NFKC') : label + ).toLowerCase(); + if (!hasNonASCII(mapped)) { + // The mapping folded the label to pure ASCII (e.g. fullwidth digits). + ArrayPrototypePush(out, mapped); + continue; + } + try { + ArrayPrototypePush(out, 'xn--' + punycodeEncode(mapped)); + } catch { + return FAILURE; + } + } else { + ArrayPrototypePush(out, StringPrototypeToLowerCase(label)); + } + } + return ArrayPrototypeJoin(out, '.'); + } + + function domainToUnicode(domain) { + if (domain === '') return ''; + var labels = StringPrototypeSplit(domain, '.'); + var out = []; + for (var i = 0; i < labels.length; i++) { + var label = labels[i]; + if (label.slice(0, 4).toLowerCase() === 'xn--') { + try { + ArrayPrototypePush(out, punycodeDecode(label.slice(4))); + } catch { + ArrayPrototypePush(out, label); + } + } else { + ArrayPrototypePush(out, label); + } + } + return ArrayPrototypeJoin(out, '.'); + } + + // ------------------------------------------------------------------ // + // Host parsing // + // ------------------------------------------------------------------ // + + // Forbidden host code points (opaque hosts) and the stricter forbidden domain + // set (special-scheme hosts), per the URL Standard. + function isForbiddenHostCodePoint(cp) { + return ( + cp === 0x00 || + cp === 0x09 || + cp === 0x0a || + cp === 0x0d || + cp === 0x20 || + cp === 0x23 || + cp === 0x2f || + cp === 0x3a || + cp === 0x3c || + cp === 0x3e || + cp === 0x3f || + cp === 0x40 || + cp === 0x5b || + cp === 0x5c || + cp === 0x5d || + cp === 0x5e || + cp === 0x7c + ); + } + function isForbiddenDomainCodePoint(cp) { + return isForbiddenHostCodePoint(cp) || cp <= 0x1f || cp === 0x25 || cp === 0x7f; + } + + function parseIPv4Number(input) { + if (input === '') return FAILURE; + var radix = 10; + if (input.length >= 2 && input[0] === '0' && (input[1] === 'x' || input[1] === 'X')) { + radix = 16; + input = input.slice(2); + } else if (input.length >= 2 && input[0] === '0') { + radix = 8; + input = input.slice(1); + } + if (input === '') return 0; + // Was three regex literals re-created per call (4-5x per dotted-quad parse) plus + // an exec crossing each; now neither. + if ( + !allChars( + input, + radix === 10 ? isAsciiDigit : radix === 16 ? isAsciiHexDigit : isAsciiOctalDigit, + ) + ) + return FAILURE; + var n = parseIntG(input, radix); + if (!isFiniteG(n)) return FAILURE; + return n; + } + + function endsInANumber(input) { + var parts = StringPrototypeSplit(input, '.'); + if (parts[parts.length - 1] === '') { + if (parts.length === 1) return false; + ArrayPrototypePop(parts); + } + var last = parts[parts.length - 1]; + if (allChars(last, isAsciiDigit)) return true; + return parseIPv4Number(last) !== FAILURE; + } + + function serializeIPv4(n) { + var out = ''; + for (var i = 1; i <= 4; i++) { + out = StringG(n % 256) + out; + if (i !== 4) out = '.' + out; + n = MathFloor(n / 256); + } + return out; + } + + function parseIPv4(input) { + var parts = StringPrototypeSplit(input, '.'); + if (parts[parts.length - 1] === '') { + if (parts.length > 1) ArrayPrototypePop(parts); + } + if (parts.length > 4) return FAILURE; + var numbers = []; + for (var i = 0; i < parts.length; i++) { + var n = parseIPv4Number(parts[i]); + if (n === FAILURE) return FAILURE; + ArrayPrototypePush(numbers, n); + } + for (var j = 0; j < numbers.length; j++) { + if (numbers[j] > 255 && j !== numbers.length - 1) return FAILURE; + } + if (numbers[numbers.length - 1] >= MathPow(256, 5 - numbers.length)) return FAILURE; + var ipv4 = ArrayPrototypePop(numbers); + var counter = 0; + for (var m = 0; m < numbers.length; m++) { + ipv4 += numbers[m] * MathPow(256, 3 - counter); + counter++; + } + return serializeIPv4(ipv4); + } + + function parseIPv6(input) { + var address = [0, 0, 0, 0, 0, 0, 0, 0]; + var pieceIndex = 0; + var compress = null; + var cps = []; + for (var s = 0; s < input.length; s++) + ArrayPrototypePush(cps, StringPrototypeCharCodeAt(input, s)); + var pointer = 0; + function c() { + return pointer < cps.length ? cps[pointer] : -1; + } + if (c() === 0x3a) { + // ':' + if (cps[pointer + 1] !== 0x3a) return FAILURE; + pointer += 2; + pieceIndex++; + compress = pieceIndex; + } + while (c() !== -1) { + if (pieceIndex === 8) return FAILURE; + if (c() === 0x3a) { + if (compress !== null) return FAILURE; + pointer++; + pieceIndex++; + compress = pieceIndex; + continue; + } + var value = 0; + var length = 0; + while (length < 4 && isAsciiHexDigit(c())) { + value = value * 16 + parseIntG(StringFromCharCode(c()), 16); + pointer++; + length++; + } + if (c() === 0x2e) { + // '.', embedded IPv4 + if (length === 0) return FAILURE; + pointer -= length; + if (pieceIndex > 6) return FAILURE; + var numbersSeen = 0; + while (c() !== -1) { + var ipv4Piece = null; + if (numbersSeen > 0) { + if (c() === 0x2e && numbersSeen < 4) pointer++; + else return FAILURE; + } + if (c() < 0x30 || c() > 0x39) return FAILURE; + while (c() >= 0x30 && c() <= 0x39) { + var num = c() - 0x30; + if (ipv4Piece === null) ipv4Piece = num; + else if (ipv4Piece === 0) return FAILURE; + else ipv4Piece = ipv4Piece * 10 + num; + if (ipv4Piece > 255) return FAILURE; + pointer++; + } + address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece; + numbersSeen++; + if (numbersSeen === 2 || numbersSeen === 4) pieceIndex++; + } + if (numbersSeen !== 4) return FAILURE; + break; + } else if (c() === 0x3a) { + pointer++; + if (c() === -1) return FAILURE; + } else if (c() !== -1) { + return FAILURE; + } + address[pieceIndex] = value; + pieceIndex++; + } + if (compress !== null) { + var swaps = pieceIndex - compress; + pieceIndex = 7; + while (pieceIndex !== 0 && swaps > 0) { + var tmp = address[compress + swaps - 1]; + address[compress + swaps - 1] = address[pieceIndex]; + address[pieceIndex] = tmp; + pieceIndex--; + swaps--; + } + } else if (compress === null && pieceIndex !== 8) { + return FAILURE; + } + return '[' + serializeIPv6(address) + ']'; + } + + function serializeIPv6(address) { + var output = ''; + // Find the longest run (length > 1) of zero pieces to compress. + var longestIdx = -1; + var longestLen = 0; + var curIdx = -1; + var curLen = 0; + for (var i = 0; i < 8; i++) { + if (address[i] === 0) { + if (curIdx === -1) curIdx = i; + curLen++; + if (curLen > longestLen) { + longestLen = curLen; + longestIdx = curIdx; + } + } else { + curIdx = -1; + curLen = 0; + } + } + var compress = longestLen > 1 ? longestIdx : -1; + var ignore0 = false; + for (var p = 0; p < 8; p++) { + if (ignore0 && address[p] === 0) continue; + else if (ignore0) ignore0 = false; + if (compress === p) { + output += p === 0 ? '::' : ':'; + ignore0 = true; + continue; + } + output += address[p].toString(16); + if (p !== 7) output += ':'; + } + return output; + } + + function parseOpaqueHost(input) { + for (var i = 0; i < input.length; i++) { + var cp = StringPrototypeCodePointAt(input, i); + if (cp > 0xffff) i++; + if (isForbiddenHostCodePoint(cp)) return FAILURE; + } + return utf8PercentEncode(input, inC0ControlSet); + } + + // A host that is already a lowercase ASCII domain — no percent escapes, no + // uppercase (IDNA maps case), no Unicode, no punycode label to expand — + // percent-decodes and IDNA-maps to itself. This is nearly every real-world + // URL, and it skips the per-parse percentDecode → TextDecoder → domainToASCII + // round-trip that otherwise dominates host parsing. + var SIMPLE_HOST_RE = /^[a-z0-9._-]+$/; + + function parseHost(input, isSpecial) { + if (input[0] === '[') { + if (input[input.length - 1] !== ']') return FAILURE; + return parseIPv6(input.slice(1, -1)); + } + if (!isSpecial) return parseOpaqueHost(input); + if (reTest(SIMPLE_HOST_RE, input) && input.indexOf('xn--') === -1) { + if (endsInANumber(input)) return parseIPv4(input); + return input; + } + var domain = percentDecodeHostStrict(input); + if (domain === FAILURE) return FAILURE; + var ascii = domainToASCII(domain); + if (ascii === FAILURE) return FAILURE; + for (var i = 0; i < ascii.length; i++) { + if (isForbiddenDomainCodePoint(StringPrototypeCharCodeAt(ascii, i))) return FAILURE; + } + if (endsInANumber(ascii)) return parseIPv4(ascii); + return ascii; + } + + // ------------------------------------------------------------------ // + // URL record + serialization // + // ------------------------------------------------------------------ // + + // null prototype: SPECIAL_SCHEMES[scheme] is read for arbitrary schemes, so a + // normal object would inherit a poisoned Object.prototype key (e.g. + // Object.prototype.foo = 8080 makes the default-port check match and silently + // drop a real port). __proto__:null cuts the chain — a non-special scheme reads + // undefined, as it must. + var SPECIAL_SCHEMES = { + __proto__: null, + ftp: 21, + file: null, + http: 80, + https: 443, + ws: 80, + wss: 443, + }; + + function isSpecialScheme(scheme) { + return Object.prototype.hasOwnProperty.call(SPECIAL_SCHEMES, scheme); + } + + function newRecord() { + // A plain literal, NOT a null-proto object: this record is accessed only via + // fixed, hard-coded field names (url.scheme, url.host, …), never by an + // attacker-controlled key, so it is not exposed to prototype-key pollution + // and needs no null prototype. (null-proto is reserved for the lookup MAPS + // keyed by external strings, e.g. SPECIAL_SCHEMES.) + return { + scheme: '', + username: '', + password: '', + host: null, + port: null, + path: [], // array of segments, or a string when opaque + query: null, + fragment: null, + opaque: false, // true => path is an opaque string + }; + } + + function recordIsSpecial(url) { + return isSpecialScheme(url.scheme); + } + + function includesCredentials(url) { + return url.username !== '' || url.password !== ''; + } + + function cannotHaveUsernamePasswordPort(url) { + return url.host === null || url.host === '' || url.scheme === 'file'; + } + + function serializePath(url) { + if (url.opaque) return url.path; + if (url.path.length === 0) return ''; + return '/' + ArrayPrototypeJoin(url.path, '/'); + } + + function serializeHostPort(url) { + if (url.host === null) return ''; + var out = url.host; + if (url.port !== null) out += ':' + url.port; + return out; + } + + function serializeURL(url, excludeFragment) { + var output = url.scheme + ':'; + if (url.host !== null) { + output += '//'; + if (includesCredentials(url)) { + output += url.username; + if (url.password !== '') output += ':' + url.password; + output += '@'; + } + output += serializeHostPort(url); + } + if (url.host === null && !url.opaque && url.path.length > 1 && url.path[0] === '') { + // Avoid the first empty segment being parsed back as an authority. + output += '/.'; + } + output += serializePath(url); + if (url.query !== null) output += '?' + url.query; + if (!excludeFragment && url.fragment !== null) output += '#' + url.fragment; + return output; + } + + function serializeOrigin(url) { + switch (url.scheme) { + case 'http': + case 'https': + case 'ws': + case 'wss': + case 'ftp': + return url.scheme + '://' + serializeHostPort(url); + case 'blob': { + var pathStr = url.opaque ? url.path : serializePath(url); + try { + var inner = parseURLOrThrow(pathStr); + return serializeOrigin(inner); + } catch { + return 'null'; + } + } + default: + return 'null'; + } + } + + // ------------------------------------------------------------------ // + // The basic URL parser (state machine, with state-override support) // + // ------------------------------------------------------------------ // + + var SCHEME_START = 0, + SCHEME = 1, + NO_SCHEME = 2, + SPECIAL_RELATIVE_OR_AUTHORITY = 3, + PATH_OR_AUTHORITY = 4, + RELATIVE = 5, + RELATIVE_SLASH = 6, + SPECIAL_AUTHORITY_SLASHES = 7, + SPECIAL_AUTHORITY_IGNORE_SLASHES = 8, + AUTHORITY = 9, + HOST = 10, + PORT = 11, + FILE = 12, + FILE_SLASH = 13, + FILE_HOST = 14, + PATH_START = 15, + PATH = 16, + OPAQUE_PATH = 17, + QUERY = 18, + FRAGMENT = 19, + // The hostname setter shares the HOST state machine but must stop before a + // port (it returns at a top-level ':'). A distinct constant flags that. + HOSTNAME = 20; + + function isASCIIAlpha(cp) { + return (cp >= 0x41 && cp <= 0x5a) || (cp >= 0x61 && cp <= 0x7a); + } + function isASCIIAlphanumeric(cp) { + return isASCIIAlpha(cp) || isAsciiDigit(cp); + } + function isSingleDot(seg) { + return seg === '.' || StringPrototypeToLowerCase(seg) === '%2e'; + } + function isDoubleDot(seg) { + var l = StringPrototypeToLowerCase(seg); + return l === '..' || l === '.%2e' || l === '%2e.' || l === '%2e%2e'; + } + function isWindowsDriveLetter(s) { + return ( + s.length === 2 && + isASCIIAlpha(StringPrototypeCharCodeAt(s, 0)) && + (StringPrototypeCharCodeAt(s, 1) === 0x3a || StringPrototypeCharCodeAt(s, 1) === 0x7c) + ); + } + function isNormalizedWindowsDriveLetter(s) { + return ( + s.length === 2 && + isASCIIAlpha(StringPrototypeCharCodeAt(s, 0)) && + StringPrototypeCharCodeAt(s, 1) === 0x3a + ); + } + function startsWithWindowsDriveLetter(str, i) { + var rest = str.slice(i); + return ( + rest.length >= 2 && + isWindowsDriveLetter(rest.slice(0, 2)) && + (rest.length === 2 || + rest[2] === '/' || + rest[2] === '\\' || + rest[2] === '?' || + rest[2] === '#') + ); + } + + // Returns a url record on success, or FAILURE. `base` is a url record or + // undefined; `url`/`stateOverride` drive the setter path. + function basicURLParse(input, base, url, stateOverride) { + if (url === undefined) { + url = newRecord(); + // Remove leading/trailing C0 control or space (only when not a setter). + input = trimC0ControlOrSpace(input); + } + // Remove all ASCII tab or newline (guarded: almost no real input has them, + // and replaceAll would re-scan + re-allocate on every parse). + input = stripChars(input, isTabOrNewline); + + // Code POINTS as numbers, one pass, surrogate-aware — same iteration as + // ArrayFrom(input).map(c => c.codePointAt(0)) without allocating a + // single-character string per code point (the old form dominated the whole + // parse: one string alloc + one codePointAt call per character per read). + var cps = []; + for (var ci = 0; ci < input.length;) { + var cc = StringPrototypeCodePointAt(input, ci); + ArrayPrototypePush(cps, cc); + ci += cc > 0xffff ? 2 : 1; + } + var pointer = 0; + // The hostname override runs the HOST state but is flagged via stateOverride + // so the HOST case knows to stop before a port. + var state = + stateOverride === HOSTNAME + ? HOST + : stateOverride !== undefined + ? stateOverride + : SCHEME_START; + var buffer = ''; + var atSignSeen = false; + var insideBrackets = false; + var passwordTokenSeen = false; + var special = recordIsSpecial(url); + + function cp(idx) { + return idx < cps.length ? cps[idx] : -1; + } + function remaining(n) { + // The code point n positions past the current pointer (n === 0 is the + // character immediately following the one being processed). + return pointer + 1 + n < cps.length ? cps[pointer + 1 + n] : -1; + } + + for (; pointer <= cps.length; pointer++) { + var c = cp(pointer); + switch (state) { + case SCHEME_START: + if (isASCIIAlpha(c)) { + buffer += StringFromCharCode(c | 0x20); + state = SCHEME; + } else if (stateOverride === undefined) { + state = NO_SCHEME; + pointer--; + } else { + return FAILURE; + } + break; + + case SCHEME: + if (isASCIIAlphanumeric(c) || c === 0x2b || c === 0x2d || c === 0x2e) { + buffer += StringFromCharCode(c >= 0x41 && c <= 0x5a ? c | 0x20 : c); + } else if (c === 0x3a) { + if (stateOverride !== undefined) { + var bufSpecial = isSpecialScheme(buffer); + if (special && !bufSpecial) return url; + if (!special && bufSpecial) return url; + if ((includesCredentials(url) || url.port !== null) && buffer === 'file') return url; + if (url.scheme === 'file' && url.host === '') return url; + } + url.scheme = buffer; + if (stateOverride !== undefined) { + if (url.port === SPECIAL_SCHEMES[url.scheme]) url.port = null; + return url; + } + buffer = ''; + special = recordIsSpecial(url); + if (url.scheme === 'file') { + state = FILE; + } else if (special && base !== undefined && base.scheme === url.scheme) { + state = SPECIAL_RELATIVE_OR_AUTHORITY; + } else if (special) { + state = SPECIAL_AUTHORITY_SLASHES; + } else if (remaining(0) === 0x2f) { + state = PATH_OR_AUTHORITY; + pointer++; + } else { + url.opaque = true; + url.path = ''; + state = OPAQUE_PATH; + } + } else if (stateOverride === undefined) { + buffer = ''; + state = NO_SCHEME; + pointer = -1; + } else { + return FAILURE; + } + break; + + case NO_SCHEME: + if (base === undefined || (base.opaque && c !== 0x23)) { + return FAILURE; + } else if (base.opaque && c === 0x23) { + url.scheme = base.scheme; + url.path = base.path; + url.opaque = true; + url.query = base.query; + url.fragment = ''; + state = FRAGMENT; + } else if (base.scheme !== 'file') { + state = RELATIVE; + pointer--; + } else { + state = FILE; + pointer--; + } + break; + + case SPECIAL_RELATIVE_OR_AUTHORITY: + if (c === 0x2f && remaining(0) === 0x2f) { + state = SPECIAL_AUTHORITY_IGNORE_SLASHES; + pointer++; + } else { + state = RELATIVE; + pointer--; + } + break; + + case PATH_OR_AUTHORITY: + if (c === 0x2f) { + state = AUTHORITY; + } else { + state = PATH; + pointer--; + } + break; + + case RELATIVE: + url.scheme = base.scheme; + special = recordIsSpecial(url); + if (c === 0x2f) { + state = RELATIVE_SLASH; + } else if (special && c === 0x5c) { + state = RELATIVE_SLASH; + } else { + url.username = base.username; + url.password = base.password; + url.host = base.host; + url.port = base.port; + url.path = ArrayPrototypeSlice(base.path); + url.query = base.query; + if (c === 0x3f) { + url.query = ''; + state = QUERY; + } else if (c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } else if (c !== -1) { + url.query = null; + ArrayPrototypePop(url.path); + state = PATH; + pointer--; + } + } + break; + + case RELATIVE_SLASH: + if (special && (c === 0x2f || c === 0x5c)) { + state = SPECIAL_AUTHORITY_IGNORE_SLASHES; + } else if (c === 0x2f) { + state = AUTHORITY; + } else { + url.username = base.username; + url.password = base.password; + url.host = base.host; + url.port = base.port; + state = PATH; + pointer--; + } + break; + + case SPECIAL_AUTHORITY_SLASHES: + if (c === 0x2f && remaining(0) === 0x2f) { + state = SPECIAL_AUTHORITY_IGNORE_SLASHES; + pointer++; + } else { + state = SPECIAL_AUTHORITY_IGNORE_SLASHES; + pointer--; + } + break; + + case SPECIAL_AUTHORITY_IGNORE_SLASHES: + if (c !== 0x2f && c !== 0x5c) { + state = AUTHORITY; + pointer--; + } + break; + + case AUTHORITY: + if (c === 0x40) { + if (atSignSeen) buffer = '%40' + buffer; + atSignSeen = true; + for (var bi = 0; bi < buffer.length; bi++) { + var bcp = StringPrototypeCodePointAt(buffer, bi); + if (bcp > 0xffff) bi++; + if (bcp === 0x3a && !passwordTokenSeen) { + passwordTokenSeen = true; + continue; + } + var encoded = utf8PercentEncode(StringFromCodePoint(bcp), inUserinfoSet); + if (passwordTokenSeen) url.password += encoded; + else url.username += encoded; + } + buffer = ''; + } else if ( + c === -1 || + c === 0x2f || + c === 0x3f || + c === 0x23 || + (special && c === 0x5c) + ) { + if (atSignSeen && buffer === '') return FAILURE; + pointer -= ArrayFrom(buffer).length + 1; + buffer = ''; + state = HOST; + } else { + buffer += cpToStr(c); + } + break; + + case HOST: + if (stateOverride !== undefined && url.scheme === 'file') { + pointer--; + state = FILE_HOST; + } else if (c === 0x3a && !insideBrackets) { + if (buffer === '') return FAILURE; + // The hostname setter (HOSTNAME override) stops before a port. + if (stateOverride === HOSTNAME) return url; + var host = parseHost(buffer, special); + if (host === FAILURE) return FAILURE; + url.host = host; + buffer = ''; + state = PORT; + } else if ( + c === -1 || + c === 0x2f || + c === 0x3f || + c === 0x23 || + (special && c === 0x5c) + ) { + pointer--; + if (special && buffer === '') return FAILURE; + if ( + stateOverride !== undefined && + buffer === '' && + (includesCredentials(url) || url.port !== null) + ) + return url; + var host2 = parseHost(buffer, special); + if (host2 === FAILURE) return FAILURE; + url.host = host2; + buffer = ''; + state = PATH_START; + if (stateOverride !== undefined) return url; + } else { + if (c === 0x5b) insideBrackets = true; + else if (c === 0x5d) insideBrackets = false; + buffer += cpToStr(c); + } + break; + + case PORT: + if (isAsciiDigit(c)) { + buffer += StringFromCharCode(c); + } else if ( + c === -1 || + c === 0x2f || + c === 0x3f || + c === 0x23 || + (special && c === 0x5c) || + stateOverride !== undefined + ) { + if (buffer !== '') { + var portNum = parseIntG(buffer, 10); + if (portNum > 65535) return FAILURE; + url.port = portNum === SPECIAL_SCHEMES[url.scheme] ? null : portNum; + buffer = ''; + } + if (stateOverride !== undefined) return url; + state = PATH_START; + pointer--; + } else { + return FAILURE; + } + break; + + case FILE: + url.scheme = 'file'; + special = true; + url.host = ''; + if (c === 0x2f || c === 0x5c) { + state = FILE_SLASH; + } else if (base !== undefined && base.scheme === 'file') { + url.host = base.host; + url.path = ArrayPrototypeSlice(base.path); + url.query = base.query; + if (c === 0x3f) { + url.query = ''; + state = QUERY; + } else if (c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } else if (c !== -1) { + url.query = null; + if (!startsWithWindowsDriveLetter(input, pointer)) { + shortenPath(url); + } else { + url.path = []; + } + state = PATH; + pointer--; + } + } else { + state = PATH; + pointer--; + } + break; + + case FILE_SLASH: + if (c === 0x2f || c === 0x5c) { + state = FILE_HOST; + } else { + if ( + base !== undefined && + base.scheme === 'file' && + !startsWithWindowsDriveLetter(input, pointer) + ) { + if (isNormalizedWindowsDriveLetter(base.path[0] || '')) { + ArrayPrototypePush(url.path, base.path[0]); + } else { + url.host = base.host; + } + } + state = PATH; + pointer--; + } + break; + + case FILE_HOST: + if (c === -1 || c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23) { + pointer--; + if (stateOverride === undefined && isWindowsDriveLetter(buffer)) { + state = PATH; + } else if (buffer === '') { + url.host = ''; + if (stateOverride !== undefined) return url; + state = PATH_START; + } else { + var fhost = parseHost(buffer, special); + if (fhost === FAILURE) return FAILURE; + if (fhost === 'localhost') fhost = ''; + url.host = fhost; + if (stateOverride !== undefined) return url; + buffer = ''; + state = PATH_START; + } + } else { + buffer += cpToStr(c); + } + break; + + case PATH_START: + if (special) { + state = PATH; + if (c !== 0x2f && c !== 0x5c) pointer--; + } else if (stateOverride === undefined && c === 0x3f) { + url.query = ''; + state = QUERY; + } else if (stateOverride === undefined && c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } else if (c !== -1) { + state = PATH; + if (c !== 0x2f) pointer--; + } else if (stateOverride !== undefined && url.host === null) { + ArrayPrototypePush(url.path, ''); + } + break; + + case PATH: + if ( + c === -1 || + c === 0x2f || + (special && c === 0x5c) || + (stateOverride === undefined && (c === 0x3f || c === 0x23)) + ) { + if (isDoubleDot(buffer)) { + shortenPath(url); + if (c !== 0x2f && !(special && c === 0x5c)) ArrayPrototypePush(url.path, ''); + } else if (isSingleDot(buffer)) { + if (c !== 0x2f && !(special && c === 0x5c)) ArrayPrototypePush(url.path, ''); + } else { + if (url.scheme === 'file' && url.path.length === 0 && isWindowsDriveLetter(buffer)) { + buffer = buffer[0] + ':'; + } + ArrayPrototypePush(url.path, buffer); + } + buffer = ''; + if (c === 0x3f) { + url.query = ''; + state = QUERY; + } else if (c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } + } else { + buffer += percentEncodeCp(c, inPathSet); + } + break; + + case OPAQUE_PATH: + if (c === 0x3f) { + url.query = ''; + state = QUERY; + } else if (c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } else if (c !== -1) { + url.path += percentEncodeCp(c, inC0ControlSet); + } + break; + + case QUERY: + if ((stateOverride === undefined && c === 0x23) || c === -1) { + var querySet = special ? inSpecialQuerySet : inQuerySet; + url.query += utf8PercentEncode(buffer, querySet); + buffer = ''; + if (c === 0x23) { + url.fragment = ''; + state = FRAGMENT; + } + } else if (c !== -1) { + buffer += cpToStr(c); + } + break; + + case FRAGMENT: + if (c !== -1) { + url.fragment += percentEncodeCp(c, inFragmentSet); + } + break; + } + if (pointer > cps.length) break; + } + return url; + } + + function shortenPath(url) { + if (url.opaque) return; + var path = url.path; + if (url.scheme === 'file' && path.length === 1 && isNormalizedWindowsDriveLetter(path[0])) { + return; + } + ArrayPrototypePop(path); + } + + function parseURLOrThrow(input, base) { + var baseRecord; + if (base !== undefined && base !== null) { + baseRecord = basicURLParse(StringG(base)); + if (baseRecord === FAILURE) throw invalidURL(nativeError, input, base); + } + var record = basicURLParse(StringG(input), baseRecord); + if (record === FAILURE) { + throw invalidURL(nativeError, input, baseRecord === undefined ? undefined : base); + } + return record; + } + + // ERR_INVALID_URL in node's two shapes: the constructor's parse runs in C++, so + // String(e) is `TypeError: Invalid URL`; the href setter throws from JS, so it is + // `TypeError [ERR_INVALID_URL]: Invalid URL`. Both carry `input`, and `base` when the + // parse ran against one (node 26.13.0, case 76). + function invalidURL(make, input, base) { + var err = make(TypeErrorG, 'ERR_INVALID_URL', 'Invalid URL'); + err.input = StringG(input); + if (base !== undefined) err.base = StringG(base); + return err; + } + + // ------------------------------------------------------------------ // + // application/x-www-form-urlencoded // + // ------------------------------------------------------------------ // + + function isFormUrlencodedSafe(cp) { + return ( + cp === 0x2a || + cp === 0x2d || + cp === 0x2e || + (cp >= 0x30 && cp <= 0x39) || + (cp >= 0x41 && cp <= 0x5a) || + cp === 0x5f || + (cp >= 0x61 && cp <= 0x7a) + ); + } + + function formUrlencodedByteSerialize(str) { + var bytes = encoder().encode(str); + var out = ''; + for (var i = 0; i < bytes.length; i++) { + var b = bytes[i]; + if (b === 0x20) out += '+'; + else if (isFormUrlencodedSafe(b)) out += StringFromCharCode(b); + else out += percentEncodeByte(b); + } + return out; + } + + function formUrlencodedParse(input) { + var sequences = input === '' ? [] : StringPrototypeSplit(input, '&'); + var output = []; + for (var i = 0; i < sequences.length; i++) { + var bytes = sequences[i]; + if (bytes === '') continue; + var name, value; + var eq = bytes.indexOf('='); + if (eq === -1) { + name = bytes; + value = ''; + } else { + name = bytes.slice(0, eq); + value = bytes.slice(eq + 1); + } + name = percentDecode(StringPrototypeReplaceAll(name, '+', ' ')); + value = percentDecode(StringPrototypeReplaceAll(value, '+', ' ')); + ArrayPrototypePush(output, [name, value]); + } + return output; + } + + function formUrlencodedSerialize(list) { + var out = ''; + for (var i = 0; i < list.length; i++) { + if (i > 0) out += '&'; + out += + formUrlencodedByteSerialize(list[i][0]) + '=' + formUrlencodedByteSerialize(list[i][1]); + } + return out; + } + + // ------------------------------------------------------------------ // + // URLSearchParams // + // ------------------------------------------------------------------ // + + var kList = Symbol('list'); + var kURLObject = Symbol('urlObject'); + + function URLSearchParams(init) { + this[kList] = []; + this[kURLObject] = null; + if (init === undefined || init === null) return; + if (typeof init === 'object' || typeof init === 'function') { + if (init instanceof URLSearchParams) { + var src = init[kList]; + for (var i = 0; i < src.length; i++) + ArrayPrototypePush(this[kList], [src[i][0], src[i][1]]); + return; + } + if (typeof init[Symbol.iterator] === 'function') { + var idx = 0; + for (var pair of init) { + if (typeof pair !== 'object' && typeof pair !== 'function') { + throw new TypeError('The provided value cannot be converted to a sequence.'); + } + var arr = []; + for (var v of pair) ArrayPrototypePush(arr, v); + if (arr.length !== 2) { + throw new TypeError('Each query pair must be an iterable [name, value] tuple'); + } + ArrayPrototypePush(this[kList], [toUSVString(arr[0]), toUSVString(arr[1])]); + idx++; + } + return; + } + // Record of string -> string. + var keys = ObjectKeys(init); + for (var k = 0; k < keys.length; k++) { + ArrayPrototypePush(this[kList], [toUSVString(keys[k]), toUSVString(init[keys[k]])]); + } + return; + } + // String init: strip a single leading '?'. + var str = toUSVString(init); + if (str[0] === '?') str = str.slice(1); + this[kList] = formUrlencodedParse(str); + } + + // Convert to a USVString: stringify, then replace any unpaired surrogate with + // U+FFFD (per WHATWG). The fast path skips strings with no surrogate at all. + // + // A code-unit walk, not a GLOBAL regex replace. The pre-scan below is not a guard + // against the poison it looks like one against: a VALID pair — any emoji in a + // remote query string — sets `hasSurrogate` too, and the global replace then + // never terminates under a forged `RegExp.prototype.exec`, because + // `Symbol.replace` advances `lastIndex` only on an empty match. Measured: + // `new URLSearchParams('a=')` and `url.hash = 'x'` both hung, and + // `new URLSearchParams(remoteQuery)` is on the network path. + // + // The pre-scan stays because it is the common case and it now decides a real fast + // path — most strings have no surrogate at all and are returned untouched. + function toUSVString(value) { + var str = typeof value === 'string' ? value : StringG(value); + var n = str.length; + var hasSurrogate = false; + for (var i = 0; i < n; i++) { + var c = StringPrototypeCharCodeAt(str, i); + if (c >= 0xd800 && c <= 0xdfff) { + hasSurrogate = true; + break; + } + } + if (!hasSurrogate) return str; + var out = StringPrototypeSlice(str, 0, i); + for (; i < n; i++) { + var cu = StringPrototypeCharCodeAt(str, i); + if (cu < 0xd800 || cu > 0xdfff) { + out += StringFromCharCode(cu); + continue; + } + if (cu <= 0xdbff && i + 1 < n) { + var lo = StringPrototypeCharCodeAt(str, i + 1); + if (lo >= 0xdc00 && lo <= 0xdfff) { + // A valid pair is kept verbatim. + out += StringFromCharCode(cu) + StringFromCharCode(lo); + i++; + continue; + } + } + out += '\uFFFD'; // lone high or lone low surrogate + } + return out; + } + + function spUpdate(sp) { + var urlObj = sp[kURLObject]; + if (urlObj === null) return; + var serialized = formUrlencodedSerialize(sp[kList]); + urlObj._url.query = serialized === '' ? null : serialized; + } + + ObjectDefineProperty(URLSearchParams.prototype, 'append', { + configurable: true, + writable: true, + value: function append(name, value) { + if (arguments.length < 2) { + throw new TypeError('The "name" and "value" arguments must be specified'); + } + ArrayPrototypePush(this[kList], [toUSVString(name), toUSVString(value)]); + spUpdate(this); + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'delete', { + configurable: true, + writable: true, + value: function del(name) { + var n = toUSVString(name); + var list = this[kList]; + if (arguments.length > 1 && arguments[1] !== undefined) { + var val = toUSVString(arguments[1]); + for (var i = list.length - 1; i >= 0; i--) { + if (list[i][0] === n && list[i][1] === val) ArrayPrototypeSplice(list, i, 1); + } + } else { + for (var j = list.length - 1; j >= 0; j--) { + if (list[j][0] === n) ArrayPrototypeSplice(list, j, 1); + } + } + spUpdate(this); + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'get', { + configurable: true, + writable: true, + value: function get(name) { + var n = toUSVString(name); + var list = this[kList]; + for (var i = 0; i < list.length; i++) { + if (list[i][0] === n) return list[i][1]; + } + return null; + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'getAll', { + configurable: true, + writable: true, + value: function getAll(name) { + var n = toUSVString(name); + var list = this[kList]; + var out = []; + for (var i = 0; i < list.length; i++) { + if (list[i][0] === n) ArrayPrototypePush(out, list[i][1]); + } + return out; + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'has', { + configurable: true, + writable: true, + value: function has(name) { + var n = toUSVString(name); + var list = this[kList]; + if (arguments.length > 1 && arguments[1] !== undefined) { + var val = toUSVString(arguments[1]); + for (var i = 0; i < list.length; i++) { + if (list[i][0] === n && list[i][1] === val) return true; + } + return false; + } + for (var j = 0; j < list.length; j++) { + if (list[j][0] === n) return true; + } + return false; + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'set', { + configurable: true, + writable: true, + value: function set(name, value) { + if (arguments.length < 2) { + throw new TypeError('The "name" and "value" arguments must be specified'); + } + var n = toUSVString(name); + var v = toUSVString(value); + var list = this[kList]; + // Set the first matching tuple in place and drop the rest (URL Standard). + var found = false; + for (var i = 0; i < list.length;) { + if (list[i][0] === n) { + if (found) { + ArrayPrototypeSplice(list, i, 1); + continue; + } + list[i][1] = v; + found = true; + } + i++; + } + if (!found) ArrayPrototypePush(list, [n, v]); + spUpdate(this); + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'sort', { + configurable: true, + writable: true, + value: function sort() { + // Stable sort by code units of the name only. + ArrayPrototypeSort(this[kList], function (a, b) { + return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; + }); + spUpdate(this); + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'forEach', { + configurable: true, + writable: true, + value: function forEach(callback, thisArg) { + if (typeof callback !== 'function') { + throw new TypeError('Callback must be a function'); + } + var list = this[kList]; + // Read length lazily so callbacks that append are observed (per spec). + for (var i = 0; i < list.length; i++) { + callback.call(thisArg, list[i][1], list[i][0], this); + } + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'keys', { + configurable: true, + writable: true, + value: function keys() { + return makeSearchParamsIterator(this, 'key'); + }, + }); + ObjectDefineProperty(URLSearchParams.prototype, 'values', { + configurable: true, + writable: true, + value: function values() { + return makeSearchParamsIterator(this, 'value'); + }, + }); + ObjectDefineProperty(URLSearchParams.prototype, 'entries', { + configurable: true, + writable: true, + value: function entries() { + return makeSearchParamsIterator(this, 'key+value'); + }, + }); + ObjectDefineProperty(URLSearchParams.prototype, Symbol.iterator, { + configurable: true, + writable: true, + value: URLSearchParams.prototype.entries, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'size', { + configurable: true, + enumerable: true, + get: function size() { + return this[kList].length; + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, 'toString', { + configurable: true, + writable: true, + value: function toString() { + return formUrlencodedSerialize(this[kList]); + }, + }); + + ObjectDefineProperty(URLSearchParams.prototype, Symbol.toStringTag, { + configurable: true, + value: 'URLSearchParams', + }); + + function makeSearchParamsIterator(sp, kind) { + var index = 0; + var iterator = { + next: function next() { + var list = sp[kList]; + if (index >= list.length) { + return { value: undefined, done: true }; + } + var entry = list[index]; + index++; + var value; + if (kind === 'key') value = entry[0]; + else if (kind === 'value') value = entry[1]; + else value = [entry[0], entry[1]]; + return { value: value, done: false }; + }, + }; + iterator[Symbol.iterator] = function () { + return this; + }; + ObjectDefineProperty(iterator, Symbol.toStringTag, { + configurable: true, + value: 'URLSearchParams Iterator', + }); + return iterator; + } + + // Build a URLSearchParams bound to a URL record (no constructor side effects). + function newBoundSearchParams(urlObj) { + var sp = ObjectCreate(URLSearchParams.prototype); + sp[kList] = urlObj._url.query === null ? [] : formUrlencodedParse(urlObj._url.query); + sp[kURLObject] = urlObj; + return sp; + } + + // ------------------------------------------------------------------ // + // URL // + // ------------------------------------------------------------------ // + + var kRecord = Symbol('record'); + var kSearchParams = Symbol('searchParams'); + + function URL(input, base) { + if (!(this instanceof URL)) { + throw new TypeError("Class constructor URL cannot be invoked without 'new'"); + } + if (arguments.length === 0) { + throw new TypeError("Failed to construct 'URL': 1 argument required, but only 0 present."); + } + this[kRecord] = parseURLOrThrow(input, base); + this[kSearchParams] = null; + } + + function refreshSearchParams(url) { + if (url[kSearchParams] !== null) { + var sp = url[kSearchParams]; + sp[kList] = url._url.query === null ? [] : formUrlencodedParse(url._url.query); + } + } + + ObjectDefineProperty(URL.prototype, '_url', { + configurable: true, + get: function () { + return this[kRecord]; + }, + }); + + ObjectDefineProperty(URL.prototype, 'href', { + configurable: true, + enumerable: true, + get: function () { + return serializeURL(this[kRecord]); + }, + set: function (value) { + var record = basicURLParse(toUSVString(value)); + if (record === FAILURE) throw invalidURL(nodeError, value); + this[kRecord] = record; + refreshSearchParams(this); + }, + }); + + ObjectDefineProperty(URL.prototype, 'origin', { + configurable: true, + enumerable: true, + get: function () { + return serializeOrigin(this[kRecord]); + }, + }); + + ObjectDefineProperty(URL.prototype, 'protocol', { + configurable: true, + enumerable: true, + get: function () { + return this[kRecord].scheme + ':'; + }, + set: function (value) { + basicURLParse(toUSVString(value) + ':', undefined, this[kRecord], SCHEME_START); + }, + }); + + ObjectDefineProperty(URL.prototype, 'username', { + configurable: true, + enumerable: true, + get: function () { + return this[kRecord].username; + }, + set: function (value) { + var url = this[kRecord]; + if (cannotHaveUsernamePasswordPort(url)) return; + url.username = utf8PercentEncode(toUSVString(value), inUserinfoSet); + }, + }); + + ObjectDefineProperty(URL.prototype, 'password', { + configurable: true, + enumerable: true, + get: function () { + return this[kRecord].password; + }, + set: function (value) { + var url = this[kRecord]; + if (cannotHaveUsernamePasswordPort(url)) return; + url.password = utf8PercentEncode(toUSVString(value), inUserinfoSet); + }, + }); + + ObjectDefineProperty(URL.prototype, 'host', { + configurable: true, + enumerable: true, + get: function () { + return serializeHostPort(this[kRecord]); + }, + set: function (value) { + var url = this[kRecord]; + if (url.opaque) return; + basicURLParse(toUSVString(value), undefined, url, HOST); + }, + }); + + ObjectDefineProperty(URL.prototype, 'hostname', { + configurable: true, + enumerable: true, + get: function () { + return this[kRecord].host === null ? '' : this[kRecord].host; + }, + set: function (value) { + var url = this[kRecord]; + if (url.opaque) return; + basicURLParse(toUSVString(value), undefined, url, HOSTNAME); + }, + }); + + ObjectDefineProperty(URL.prototype, 'port', { + configurable: true, + enumerable: true, + get: function () { + return this[kRecord].port === null ? '' : StringG(this[kRecord].port); + }, + set: function (value) { + var url = this[kRecord]; + if (cannotHaveUsernamePasswordPort(url)) return; + var str = toUSVString(value); + if (str === '') { + url.port = null; + return; + } + basicURLParse(str, undefined, url, PORT); + }, + }); + + ObjectDefineProperty(URL.prototype, 'pathname', { + configurable: true, + enumerable: true, + get: function () { + return serializePath(this[kRecord]); + }, + set: function (value) { + var url = this[kRecord]; + if (url.opaque) return; + url.path = []; + basicURLParse(toUSVString(value), undefined, url, PATH_START); + }, + }); + + ObjectDefineProperty(URL.prototype, 'search', { + configurable: true, + enumerable: true, + get: function () { + var q = this[kRecord].query; + return q === null || q === '' ? '' : '?' + q; + }, + set: function (value) { + var url = this[kRecord]; + var str = toUSVString(value); + if (str === '') { + url.query = null; + refreshSearchParams(this); + return; + } + if (str[0] === '?') str = str.slice(1); + url.query = ''; + basicURLParse(str, undefined, url, QUERY); + refreshSearchParams(this); + }, + }); + + ObjectDefineProperty(URL.prototype, 'searchParams', { + configurable: true, + enumerable: true, + get: function () { + if (this[kSearchParams] === null) { + this[kSearchParams] = newBoundSearchParams(this); + } + return this[kSearchParams]; + }, + }); + + ObjectDefineProperty(URL.prototype, 'hash', { + configurable: true, + enumerable: true, + get: function () { + var f = this[kRecord].fragment; + return f === null || f === '' ? '' : '#' + f; + }, + set: function (value) { + var url = this[kRecord]; + var str = toUSVString(value); + if (str === '') { + url.fragment = null; + return; + } + if (str[0] === '#') str = str.slice(1); + url.fragment = ''; + basicURLParse(str, undefined, url, FRAGMENT); + }, + }); + + ObjectDefineProperty(URL.prototype, 'toString', { + configurable: true, + writable: true, + value: function toString() { + return serializeURL(this[kRecord]); + }, + }); + + ObjectDefineProperty(URL.prototype, 'toJSON', { + configurable: true, + writable: true, + value: function toJSON() { + return serializeURL(this[kRecord]); + }, + }); + + ObjectDefineProperty(URL.prototype, Symbol.toStringTag, { + configurable: true, + value: 'URL', + }); + + // canParse / parse statics (Node 19+/22+). + ObjectDefineProperty(URL, 'canParse', { + configurable: true, + writable: true, + value: function canParse(input, base) { + if (arguments.length === 0) { + throw new TypeError("Failed to execute 'canParse' on 'URL': 1 argument required."); + } + try { + parseURLOrThrow(input, base); + return true; + } catch { + return false; + } + }, + }); + + ObjectDefineProperty(URL, 'parse', { + configurable: true, + writable: true, + value: function parse(input, base) { + if (arguments.length === 0) { + throw new TypeError("Failed to execute 'parse' on 'URL': 1 argument required."); + } + try { + return new URL(input, base); + } catch { + return null; + } + }, + }); + + // ------------------------------------------------------------------ // + // node:url helper functions // + // ------------------------------------------------------------------ // + + function fileURLToPath(input) { + // Node accepts only a string or a real URL instance — not an arbitrary + // {href}-shaped object — and rejects everything else with ERR_INVALID_ARG_TYPE + // so a programmer error can't silently become a filesystem path. + var href; + if (typeof input === 'string') href = new URL(input).href; + else if (input instanceof URL) href = input.href; + else throw ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], input); + return fileURLToPathFromHref(href); + } + + // The original file: → path translation (kept verbatim so the ESM loader and + // node:sqlite, which call it on an href, behave exactly as before). + function fileURLToPathFromHref(url) { + if (url.slice(0, 5).toLowerCase() !== 'file:') { + throw nodeError(TypeErrorG, 'ERR_INVALID_URL_SCHEME', 'The URL must be of scheme file'); + } + var isWindows = process.platform === 'win32'; + var rest = url.slice(5); + var host = ''; + if (rest.slice(0, 2) === '//') { + rest = rest.slice(2); + var slash = rest.indexOf('/'); + host = slash === -1 ? rest : rest.slice(0, slash); + rest = slash === -1 ? '/' : rest.slice(slash); + } + var isLocalhost = StringPrototypeToLowerCase(host) === 'localhost'; + var hash = rest.indexOf('#'); + if (hash !== -1) rest = rest.slice(0, hash); + var query = rest.indexOf('?'); + if (query !== -1) rest = rest.slice(0, query); + if (reTest(PERCENT_2F_RE, rest) || (isWindows && reTest(PERCENT_5C_RE, rest))) { + // node words the two platforms differently and attaches the href as `input`. + var sepErr = nodeError( + TypeErrorG, + 'ERR_INVALID_FILE_URL_PATH', + isWindows + ? 'File URL path must not include encoded \\ or / characters' + : 'File URL path must not include encoded / characters', + ); + sepErr.input = url; + throw sepErr; + } + + if (isWindows) { + var winPath = decodeURIComponentG(StringPrototypeReplaceAll(rest, '/', '\\')); + if (host !== '' && !isLocalhost) { + return '\\\\' + host + winPath; + } + var letter = StringPrototypeCharCodeAt(winPath, 1) | 0x20; + if (letter < 97 || letter > 122 || StringPrototypeCharAt(winPath, 2) !== ':') { + throw nodeError(TypeErrorG, 'ERR_INVALID_FILE_URL_PATH', 'File URL path must be absolute'); + } + return winPath.slice(1); + } + + if (host !== '' && !isLocalhost) { + throw nodeError( + TypeErrorG, + 'ERR_INVALID_FILE_URL_HOST', + 'File URL host must be "localhost" or empty on ' + process.platform, + ); + } + return decodeURIComponentG(rest); + } + + function urlToHttpOptions(url) { + // Mirror Node's `{ __proto__: null, ...url, }`: own + // enumerable properties (e.g. request options a caller attached to the URL) + // are copied first, then the standard http.request fields are layered on top. + var options = ObjectCreate(null); + var ownKeys = ObjectKeys(url); + for (var i = 0; i < ownKeys.length; i++) options[ownKeys[i]] = url[ownKeys[i]]; + options.protocol = url.protocol; + options.hostname = + typeof url.hostname === 'string' && StringPrototypeStartsWith(url.hostname, '[') + ? url.hostname.slice(1, -1) + : url.hostname; + options.hash = url.hash; + options.search = url.search; + options.pathname = url.pathname; + options.path = '' + (url.pathname || '') + (url.search || ''); + options.href = url.href; + if (url.port !== '') options.port = NumberG(url.port); + if (url.username || url.password) { + options.auth = decodeURIComponentG(url.username) + ':' + decodeURIComponentG(url.password); + } + return options; + } + + // Extract and host-parse the domain argument to domainToASCII/Unicode, applying + // the same preprocessing the URL parser does: remove ASCII tab/newline, then take + // everything up to the first authority terminator (/ \ ? #). Returns the parsed + // host string, or FAILURE for an invalid domain. + function hostFromDomainArg(domain) { + var input = stripChars(StringG(domain), isTabOrNewline); + for (var i = 0; i < input.length; i++) { + var c = StringPrototypeCharCodeAt(input, i); + if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23) { + input = input.slice(0, i); + break; + } + } + return parseHost(input, true); + } + + module.exports = { + URL: URL, + URLSearchParams: URLSearchParams, + fileURLToPath: fileURLToPath, + urlToHttpOptions: urlToHttpOptions, + // Node's domainToASCII/domainToUnicode validate their input as a host (the + // same machinery URL parsing uses) and return '' for anything that is not a + // valid domain — so they can be used as a normalization/validation primitive. + // Route through hostFromDomainArg (which applies the URL parser's host + // preprocessing: strip tab/newline, cut at the first authority terminator, + // then host-parse) so forbidden characters, bad percent/UTF-8, NUL, and + // URL-shaped strings all collapse to '' exactly as Node does. + domainToASCII: function (domain) { + var host = hostFromDomainArg(domain); + return host === FAILURE ? '' : host; + }, + domainToUnicode: function (domain) { + var host = hostFromDomainArg(domain); + return host === FAILURE ? '' : domainToUnicode(host); + }, + // For node/url.js's format(), which node frames in node:url and so stays there: + // the record slot, the path serializer, and the Punycode host decoder (the + // unvalidated one; the public `domainToUnicode` above validates first). + kRecord: kRecord, + serializePath: serializePath, + hostToUnicode: domainToUnicode, + }; +}); diff --git a/pkg/runtime/js/node/url.js b/pkg/runtime/js/node/url.js index 219baee..ba20772 100644 --- a/pkg/runtime/js/node/url.js +++ b/pkg/runtime/js/node/url.js @@ -1,7 +1,8 @@ -// node:url — WHATWG URL and URLSearchParams, implemented in JS (as Node does for -// the parsing-heavy modules like node:path) so the fiddly state-machine semantics -// match the URL Standard and Node byte-for-byte. JavaScriptCore's bare engine ships -// no URL/URLSearchParams (those are WebCore APIs), so the whole surface lives here. +// node:url — the public entry. The WHATWG implementation (parser, `URL`, +// `URLSearchParams`, `fileURLToPath`, `urlToHttpOptions`, `domainToASCII`, +// `domainToUnicode`) lives in internal/url.js, as in node's lib/internal/url.js, so +// its throws frame `node:internal/url` the way node's do. This file keeps what node's +// lib/url.js keeps and frames `node:url`: `pathToFileURL` and `format`. // // The module installs the `URL` and `URLSearchParams` globals (preserving the // `createObjectURL`/`revokeObjectURL` statics that node:buffer attaches) and is @@ -10,2124 +11,39 @@ // `pathToFileURL`, `urlToHttpOptions`, `format`, `domainToASCII`, `domainToUnicode`. // // Deliberate deviations from Node, documented in reference/node-compatibility.md: -// * IDNA/UTS-46 host processing is approximated (NFC + lowercase + Punycode) -// rather than the full ICU mapping; common Unicode domains round-trip, but -// exotic mapping/validation edge cases may differ. +// * IDNA/UTS-46 host processing is approximated (see internal/url.js). // * The legacy `url.parse`/`url.resolve`/`url.Url` API is not provided; only the // WHATWG surface is. (function (require, module, _exports) { 'use strict'; - // --- Pollution-safe intrinsics ------------------------------------------- - // URL parsing runs on attacker-influenced strings while user code may have - // poisoned shared prototypes/globals. Prototype methods route through the - // pristine primordials table; free globals and statics are captured HERE, at - // module-eval, which the loader runs before any user code — so these bindings - // are pristine and cannot be re-pointed later (a poisoned Math.floor, String, - // decodeURIComponent, or String.prototype.normalize otherwise silently - // substitutes hosts / disables IPv4 normalization; see the URL vector audit). - var P = require('internal/primordials'); - // Every regex below runs over a caller-supplied URL, so none may go through - // `re.test(...)`: `RegExp.prototype.exec` is a writable data property and the - // spec's RegExpExec re-reads it off the receiver, so a plain assignment steers - // `test` as well — a captured `test` included. primordials exports no - // `RegExpPrototypeTest` for exactly that reason. `%2f`/`%5c` detection below is - // the path-traversal check, so this is not cosmetic. + // Captured at module-eval, which the loader runs before any user code (url is + // eager, and internal/url with it through the require below), for the same reason + // internal/url.js gives: these run on caller-supplied paths while user code may + // have poisoned shared prototypes/globals. // // NOT a complete rule for this file: the `StringPrototypeReplace(…, /re/, …)` // sites below route through the same RegExpExec re-read and are still steerable — // and a GLOBAL one spins rather than answering wrongly, because a forged result - // never advances lastIndex. Tracked in ROADMAP. The last global regex in this file - // was toUSVString's, now a code-unit walk; LEADING_SLASHES_RE is non-global. - var reTest = P.RegExpMatches; - var allChars = P.allChars; - var isAsciiDigit = P.isAsciiDigit; - var isAsciiHexDigit = P.isAsciiHexDigit; - var isAsciiOctalDigit = P.isAsciiOctalDigit; - var PERCENT_2F_RE = /%2f/i; - var PERCENT_5C_RE = /%5c/i; + // never advances lastIndex. Tracked in ROADMAP. LEADING_SLASHES_RE is non-global. + var P = require('internal/primordials'); var LEADING_SLASHES_RE = /^\/+/; - var ArrayPrototypePush = P.ArrayPrototypePush; - var ArrayPrototypePop = P.ArrayPrototypePop; - var ArrayPrototypeShift = P.ArrayPrototypeShift; - var ArrayPrototypeSlice = P.ArrayPrototypeSlice; - var ArrayPrototypeSplice = P.ArrayPrototypeSplice; - var ArrayPrototypeJoin = P.ArrayPrototypeJoin; - var ArrayPrototypeSort = P.ArrayPrototypeSort; - var ArrayPrototypeMap = P.ArrayPrototypeMap; - var ArrayPrototypeIndexOf = P.ArrayPrototypeIndexOf; - var ArrayPrototypeIncludes = P.ArrayPrototypeIncludes; - var StringPrototypeSlice = P.StringPrototypeSlice; var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; - var StringPrototypeCodePointAt = P.StringPrototypeCodePointAt; - var StringPrototypeCharAt = P.StringPrototypeCharAt; - var StringPrototypeIndexOf = P.StringPrototypeIndexOf; var StringPrototypeReplace = P.StringPrototypeReplace; var StringPrototypeReplaceAll = P.StringPrototypeReplaceAll; - var StringPrototypeSplit = P.StringPrototypeSplit; - var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; - var StringPrototypeToUpperCase = P.StringPrototypeToUpperCase; - var StringPrototypeStartsWith = P.StringPrototypeStartsWith; - var StringPrototypeNormalize = P.StringPrototypeNormalize; - var StringPrototypeSubstr = P.StringPrototypeSubstr; - var ObjectCreate = P.ObjectCreate; - var ObjectKeys = P.ObjectKeys; - var ObjectDefineProperty = P.ObjectDefineProperty; - var ArrayFrom = P.ArrayFrom; - // Free globals / statics, captured pristine at module-eval. - var StringG = String; - var NumberG = P.Number; - var TypeErrorG = P.TypeError; - var errors = require('internal/errors'); - var nodeError = errors.nodeError; - var nativeError = errors.nativeError; - var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; - var StringFromCharCode = String.fromCharCode; - var StringFromCodePoint = String.fromCodePoint; - var MathFloor = Math.floor; - var MathPow = Math.pow; - var parseIntG = P.parseInt; - var decodeURIComponentG = decodeURIComponent; - var isFiniteG = isFinite; - var TextEncoderG = TextEncoder; - var TextDecoderG = TextDecoder; - - // TextEncoder/TextDecoder are installed by internal/encoding.js. This module is - // eagerly instantiated to install the URL/URLSearchParams globals, so the codecs - // are created lazily (on first parse/serialize) rather than at module-eval time — - // that keeps url.js robust to the loader's eager-instantiation order, since no - // URL operation runs until user code, by which point every builtin has loaded. - var encoderInstance = null; - var decoderInstance = null; - var fatalDecoderInstance = null; - function encoder() { - return encoderInstance || (encoderInstance = new TextEncoderG()); - } - function decoder() { - return decoderInstance || (decoderInstance = new TextDecoderG('utf-8')); - } - function fatalDecoder() { - return ( - fatalDecoderInstance || (fatalDecoderInstance = new TextDecoderG('utf-8', { fatal: true })) - ); - } - - // A unique sentinel distinct from any valid parser result; using a symbol means - // it can never collide with a legitimately-produced host string or number. - var FAILURE = { failure: true }; - - // ------------------------------------------------------------------ // - // Percent-encoding // - // ------------------------------------------------------------------ // - - // stripChars: `replace(/[…]/g, '')` without a RegExp. - // - // A GLOBAL regex replace is the sharpest shape of the exec re-read, and it does - // not merely answer wrongly: `RegExp.prototype[Symbol.replace]` loops on - // RegExpExec and advances `lastIndex` only on an EMPTY match, so a forged - // non-empty result never terminates. Measured on `bin/lava` before this change, - // with `RegExp.prototype.exec` replaced: `new URL('http://EXAMPLE.com/')` never - // returned and reached 1.4 GB RSS in four seconds, and a remote server's - // `Location:` header reaching `new URL(location, req.url)` hung the client. - // Routing through a captured `exec` would fix the steering and keep the spin. - // - // `pred` is a code-point predicate, so the caller keeps its character set - // explicit and no RegExp exists to poison. Returns the input unchanged when - // nothing matches, which is the common case for both callers. - function stripChars(s, pred) { - var n = s.length; - var i = 0; - for (; i < n; i++) { - if (pred(StringPrototypeCharCodeAt(s, i))) break; - } - if (i === n) return s; - var out = StringPrototypeSlice(s, 0, i); - for (; i < n; i++) { - var c = StringPrototypeCharCodeAt(s, i); - if (!pred(c)) out += StringFromCharCode(c); - } - return out; - } - - function isTabOrNewline(c) { - return c === 0x09 || c === 0x0a || c === 0x0d; - } - - // U+3002 IDEOGRAPHIC FULL STOP, U+FF0E FULLWIDTH FULL STOP, U+FF61 HALFWIDTH - // IDEOGRAPHIC FULL STOP — the IDNA label separators that fold to '.'. Getting - // this wrong is host confusion, not a cosmetic difference. - function isIdnaSeparator(c) { - return c === 0x3002 || c === 0xff0e || c === 0xff61; - } - - // The fold, not a strip: these three map TO '.', they are not removed. - function foldIdnaSeparators(s) { - var n = s.length; - var i = 0; - for (; i < n; i++) { - if (isIdnaSeparator(StringPrototypeCharCodeAt(s, i))) break; - } - if (i === n) return s; - var out = StringPrototypeSlice(s, 0, i); - for (; i < n; i++) { - var c = StringPrototypeCharCodeAt(s, i); - out += isIdnaSeparator(c) ? '.' : StringFromCharCode(c); - } - return out; - } - - // The WHATWG percent-encode sets, each a predicate over a code point. Every set - // is a superset of the C0-control set (control chars and everything past 0x7E). - function inC0ControlSet(cp) { - return cp <= 0x1f || cp > 0x7e; - } - function trimC0ControlOrSpace(str) { - var start = 0; - var end = str.length; - while (start < end && StringPrototypeCharCodeAt(str, start) <= 0x20) start++; - while (end > start && StringPrototypeCharCodeAt(str, end - 1) <= 0x20) end--; - return start === 0 && end === str.length ? str : str.slice(start, end); - } - function inFragmentSet(cp) { - return ( - inC0ControlSet(cp) || cp === 0x20 || cp === 0x22 || cp === 0x3c || cp === 0x3e || cp === 0x60 - ); - } - function inQuerySet(cp) { - return ( - inC0ControlSet(cp) || cp === 0x20 || cp === 0x22 || cp === 0x23 || cp === 0x3c || cp === 0x3e - ); - } - function inSpecialQuerySet(cp) { - return inQuerySet(cp) || cp === 0x27; - } - function inPathSet(cp) { - return inQuerySet(cp) || cp === 0x3f || cp === 0x60 || cp === 0x7b || cp === 0x7d; - } - function inUserinfoSet(cp) { - return ( - inPathSet(cp) || - cp === 0x2f || - cp === 0x3a || - cp === 0x3b || - cp === 0x3d || - cp === 0x40 || - cp === 0x5b || - cp === 0x5c || - cp === 0x5d || - cp === 0x5e || - cp === 0x7c - ); - } - - // Precomputed per-byte artifacts for the hot per-character encode path: - // PCT[b] is '%XX', ASCII_CHAR[b] the one-character string. Built once. - var PCT = new Array(256); - var ASCII_CHAR = new Array(128); - for (var pctB = 0; pctB < 256; pctB++) { - var pctHex = pctB.toString(16).toUpperCase(); - PCT[pctB] = '%' + (pctHex.length === 1 ? '0' + pctHex : pctHex); - if (pctB < 128) ASCII_CHAR[pctB] = StringFromCharCode(pctB); - } - - function percentEncodeByte(b) { - return PCT[b]; - } - - // Encode ONE code point against `inSet` without round-tripping through a - // string or TextEncoder. The parser's state machine calls this once per - // character of userinfo/path/query/fragment, so it is the hottest loop in - // the whole parse; every set encodes all non-ASCII (> 0x7E), so the UTF-8 - // bytes are hand-rolled from the code point. - function percentEncodeCp(cp, inSet) { - if (cp < 0x80) return inSet(cp) ? PCT[cp] : ASCII_CHAR[cp]; - if (cp < 0x800) return PCT[0xc0 | (cp >> 6)] + PCT[0x80 | (cp & 0x3f)]; - if (cp < 0x10000) - return PCT[0xe0 | (cp >> 12)] + PCT[0x80 | ((cp >> 6) & 0x3f)] + PCT[0x80 | (cp & 0x3f)]; - return ( - PCT[0xf0 | (cp >> 18)] + - PCT[0x80 | ((cp >> 12) & 0x3f)] + - PCT[0x80 | ((cp >> 6) & 0x3f)] + - PCT[0x80 | (cp & 0x3f)] - ); - } - - // UTF-8 percent-encode a string: code points in `inSet` (or any non-ASCII, since - // the sets all include >0x7E) are encoded to their UTF-8 bytes; the rest pass - // through. Iterates by code point so astral characters encode as four bytes. - function utf8PercentEncode(str, inSet) { - var out = ''; - for (var i = 0; i < str.length; i++) { - var cp = StringPrototypeCodePointAt(str, i); - if (cp > 0xffff) i++; // consumed a surrogate pair - out += percentEncodeCp(cp, inSet); - } - return out; - } - - // One-character string for a code point without the StringFromCodePoint - // host-call for ASCII — the state machine appends to its buffer through this. - function cpToStr(cp) { - return cp < 0x80 ? ASCII_CHAR[cp] : StringFromCodePoint(cp); - } - - // Percent-decode a string to its raw byte sequence. Non-%XX characters - // contribute their own UTF-8 bytes so raw multibyte input survives. - function percentDecodeBytes(str) { - var bytes = []; - for (var i = 0; i < str.length; i++) { - var c = StringPrototypeCharCodeAt(str, i); - if ( - c === 0x25 && - i + 2 < str.length && - isAsciiHexDigit(StringPrototypeCharCodeAt(str, i + 1)) && - isAsciiHexDigit(StringPrototypeCharCodeAt(str, i + 2)) - ) { - ArrayPrototypePush(bytes, parseIntG(StringPrototypeSubstr(str, i + 1, 2), 16)); - i += 2; - } else { - var cp = StringPrototypeCodePointAt(str, i); - if (cp > 0xffff) i++; - var enc = encoder().encode(StringFromCodePoint(cp)); - for (var k = 0; k < enc.length; k++) ArrayPrototypePush(bytes, enc[k]); - } - } - return new Uint8Array(bytes); - } - - // Lenient percent-decode (invalid UTF-8 → U+FFFD), used for query/form parsing. - function percentDecode(str) { - return decoder().decode(percentDecodeBytes(str)); - } - - // Strict percent-decode for host parsing: invalid UTF-8 is a parse failure, as - // the URL Standard's domain-to-ASCII step rejects it (Node throws to match). - function percentDecodeHostStrict(str) { - try { - return fatalDecoder().decode(percentDecodeBytes(str)); - } catch { - return FAILURE; - } - } - - // ------------------------------------------------------------------ // - // Punycode (RFC 3492) — used by the simplified IDNA host processing. // - // ------------------------------------------------------------------ // - - var PUNY_BASE = 36; - var PUNY_TMIN = 1; - var PUNY_TMAX = 26; - var PUNY_SKEW = 38; - var PUNY_DAMP = 700; - var PUNY_INITIAL_BIAS = 72; - var PUNY_INITIAL_N = 128; - - function punyAdapt(delta, numPoints, firstTime) { - delta = firstTime ? MathFloor(delta / PUNY_DAMP) : delta >> 1; - delta += MathFloor(delta / numPoints); - var k = 0; - while (delta > ((PUNY_BASE - PUNY_TMIN) * PUNY_TMAX) >> 1) { - delta = MathFloor(delta / (PUNY_BASE - PUNY_TMIN)); - k += PUNY_BASE; - } - return k + MathFloor(((PUNY_BASE - PUNY_TMIN + 1) * delta) / (delta + PUNY_SKEW)); - } - - function punyEncodeDigit(d) { - // 0..25 -> a..z, 26..35 -> 0..9 - return d + 22 + (d < 26 ? 75 : 0); - } - function punyDecodeDigit(cp) { - if (cp - 48 < 10) return cp - 22; // 0..9 - if (cp - 65 < 26) return cp - 65; // A..Z - if (cp - 97 < 26) return cp - 97; // a..z - return PUNY_BASE; - } - - function punycodeEncode(input) { - var output = []; - var codePoints = []; - for (var s = 0; s < input.length; s++) { - var cp = StringPrototypeCodePointAt(input, s); - if (cp > 0xffff) s++; - ArrayPrototypePush(codePoints, cp); - } - var n = PUNY_INITIAL_N; - var delta = 0; - var bias = PUNY_INITIAL_BIAS; - var basicLength = 0; - var i; - for (i = 0; i < codePoints.length; i++) { - if (codePoints[i] < 0x80) { - ArrayPrototypePush(output, codePoints[i]); - basicLength++; - } - } - var handled = basicLength; - if (basicLength > 0) ArrayPrototypePush(output, 0x2d); // '-' - while (handled < codePoints.length) { - var m = 0x7fffffff; - for (i = 0; i < codePoints.length; i++) { - if (codePoints[i] >= n && codePoints[i] < m) m = codePoints[i]; - } - delta += (m - n) * (handled + 1); - n = m; - for (i = 0; i < codePoints.length; i++) { - var c = codePoints[i]; - if (c < n) delta++; - if (c === n) { - var q = delta; - for (var k = PUNY_BASE; ; k += PUNY_BASE) { - var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; - if (q < t) break; - ArrayPrototypePush(output, punyEncodeDigit(t + ((q - t) % (PUNY_BASE - t)))); - q = MathFloor((q - t) / (PUNY_BASE - t)); - } - ArrayPrototypePush(output, punyEncodeDigit(q)); - bias = punyAdapt(delta, handled + 1, handled === basicLength); - delta = 0; - handled++; - } - } - delta++; - n++; - } - var str = ''; - for (i = 0; i < output.length; i++) str += StringFromCharCode(output[i]); - return str; - } - - function punycodeDecode(input) { - var output = []; - var n = PUNY_INITIAL_N; - var bias = PUNY_INITIAL_BIAS; - var i = 0; - var basic = input.lastIndexOf('-'); - if (basic < 0) basic = 0; - for (var j = 0; j < basic; j++) { - if (StringPrototypeCharCodeAt(input, j) >= 0x80) throw new RangeError('Invalid input'); - ArrayPrototypePush(output, StringPrototypeCharCodeAt(input, j)); - } - var index = basic > 0 ? basic + 1 : 0; - while (index < input.length) { - var oldi = i; - var w = 1; - for (var k = PUNY_BASE; ; k += PUNY_BASE) { - if (index >= input.length) throw new RangeError('Invalid input'); - var digit = punyDecodeDigit(StringPrototypeCharCodeAt(input, index++)); - if (digit >= PUNY_BASE) throw new RangeError('Invalid input'); - i += digit * w; - var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; - if (digit < t) break; - w *= PUNY_BASE - t; - } - var outLen = output.length + 1; - bias = punyAdapt(i - oldi, outLen, oldi === 0); - n += MathFloor(i / outLen); - i %= outLen; - ArrayPrototypeSplice(output, i, 0, n); - i++; - } - var str = ''; - for (var o = 0; o < output.length; o++) str += StringFromCodePoint(output[o]); - return str; - } - - function hasNonASCII(str) { - for (var i = 0; i < str.length; i++) { - if (StringPrototypeCharCodeAt(str, i) > 0x7f) return true; - } - return false; - } - - // Simplified IDNA ToASCII. Approximates UTS-46 by: normalizing the three IDNA - // label separators to '.', applying NFKC + lowercasing each Unicode label (so - // fullwidth/compatibility characters fold the way ICU maps them), and emitting - // the Punycode "xn--…" form. ASCII labels are just lowercased. Not the full ICU - // mapping table, but round-trips the overwhelming majority of real domains. - function domainToASCII(domain) { - if (domain === '') return ''; - // IDEOGRAPHIC FULL STOP, FULLWIDTH FULL STOP, HALFWIDTH IDEOGRAPHIC FULL STOP. - // Fold the three IDNA separators to '.', without a RegExp — see stripChars. - domain = foldIdnaSeparators(domain); - var labels = StringPrototypeSplit(domain, '.'); - var out = []; - for (var i = 0; i < labels.length; i++) { - var label = labels[i]; - if (hasNonASCII(label)) { - var mapped = ( - label.normalize ? StringPrototypeNormalize(label, 'NFKC') : label - ).toLowerCase(); - if (!hasNonASCII(mapped)) { - // The mapping folded the label to pure ASCII (e.g. fullwidth digits). - ArrayPrototypePush(out, mapped); - continue; - } - try { - ArrayPrototypePush(out, 'xn--' + punycodeEncode(mapped)); - } catch { - return FAILURE; - } - } else { - ArrayPrototypePush(out, StringPrototypeToLowerCase(label)); - } - } - return ArrayPrototypeJoin(out, '.'); - } - - function domainToUnicode(domain) { - if (domain === '') return ''; - var labels = StringPrototypeSplit(domain, '.'); - var out = []; - for (var i = 0; i < labels.length; i++) { - var label = labels[i]; - if (label.slice(0, 4).toLowerCase() === 'xn--') { - try { - ArrayPrototypePush(out, punycodeDecode(label.slice(4))); - } catch { - ArrayPrototypePush(out, label); - } - } else { - ArrayPrototypePush(out, label); - } - } - return ArrayPrototypeJoin(out, '.'); - } - - // ------------------------------------------------------------------ // - // Host parsing // - // ------------------------------------------------------------------ // - - // Forbidden host code points (opaque hosts) and the stricter forbidden domain - // set (special-scheme hosts), per the URL Standard. - function isForbiddenHostCodePoint(cp) { - return ( - cp === 0x00 || - cp === 0x09 || - cp === 0x0a || - cp === 0x0d || - cp === 0x20 || - cp === 0x23 || - cp === 0x2f || - cp === 0x3a || - cp === 0x3c || - cp === 0x3e || - cp === 0x3f || - cp === 0x40 || - cp === 0x5b || - cp === 0x5c || - cp === 0x5d || - cp === 0x5e || - cp === 0x7c - ); - } - function isForbiddenDomainCodePoint(cp) { - return isForbiddenHostCodePoint(cp) || cp <= 0x1f || cp === 0x25 || cp === 0x7f; - } - - function parseIPv4Number(input) { - if (input === '') return FAILURE; - var radix = 10; - if (input.length >= 2 && input[0] === '0' && (input[1] === 'x' || input[1] === 'X')) { - radix = 16; - input = input.slice(2); - } else if (input.length >= 2 && input[0] === '0') { - radix = 8; - input = input.slice(1); - } - if (input === '') return 0; - // Was three regex literals re-created per call (4-5x per dotted-quad parse) plus - // an exec crossing each; now neither. - if ( - !allChars( - input, - radix === 10 ? isAsciiDigit : radix === 16 ? isAsciiHexDigit : isAsciiOctalDigit, - ) - ) - return FAILURE; - var n = parseIntG(input, radix); - if (!isFiniteG(n)) return FAILURE; - return n; - } - - function endsInANumber(input) { - var parts = StringPrototypeSplit(input, '.'); - if (parts[parts.length - 1] === '') { - if (parts.length === 1) return false; - ArrayPrototypePop(parts); - } - var last = parts[parts.length - 1]; - if (allChars(last, isAsciiDigit)) return true; - return parseIPv4Number(last) !== FAILURE; - } - - function serializeIPv4(n) { - var out = ''; - for (var i = 1; i <= 4; i++) { - out = StringG(n % 256) + out; - if (i !== 4) out = '.' + out; - n = MathFloor(n / 256); - } - return out; - } - - function parseIPv4(input) { - var parts = StringPrototypeSplit(input, '.'); - if (parts[parts.length - 1] === '') { - if (parts.length > 1) ArrayPrototypePop(parts); - } - if (parts.length > 4) return FAILURE; - var numbers = []; - for (var i = 0; i < parts.length; i++) { - var n = parseIPv4Number(parts[i]); - if (n === FAILURE) return FAILURE; - ArrayPrototypePush(numbers, n); - } - for (var j = 0; j < numbers.length; j++) { - if (numbers[j] > 255 && j !== numbers.length - 1) return FAILURE; - } - if (numbers[numbers.length - 1] >= MathPow(256, 5 - numbers.length)) return FAILURE; - var ipv4 = ArrayPrototypePop(numbers); - var counter = 0; - for (var m = 0; m < numbers.length; m++) { - ipv4 += numbers[m] * MathPow(256, 3 - counter); - counter++; - } - return serializeIPv4(ipv4); - } - - function parseIPv6(input) { - var address = [0, 0, 0, 0, 0, 0, 0, 0]; - var pieceIndex = 0; - var compress = null; - var cps = []; - for (var s = 0; s < input.length; s++) - ArrayPrototypePush(cps, StringPrototypeCharCodeAt(input, s)); - var pointer = 0; - function c() { - return pointer < cps.length ? cps[pointer] : -1; - } - if (c() === 0x3a) { - // ':' - if (cps[pointer + 1] !== 0x3a) return FAILURE; - pointer += 2; - pieceIndex++; - compress = pieceIndex; - } - while (c() !== -1) { - if (pieceIndex === 8) return FAILURE; - if (c() === 0x3a) { - if (compress !== null) return FAILURE; - pointer++; - pieceIndex++; - compress = pieceIndex; - continue; - } - var value = 0; - var length = 0; - while (length < 4 && isAsciiHexDigit(c())) { - value = value * 16 + parseIntG(StringFromCharCode(c()), 16); - pointer++; - length++; - } - if (c() === 0x2e) { - // '.', embedded IPv4 - if (length === 0) return FAILURE; - pointer -= length; - if (pieceIndex > 6) return FAILURE; - var numbersSeen = 0; - while (c() !== -1) { - var ipv4Piece = null; - if (numbersSeen > 0) { - if (c() === 0x2e && numbersSeen < 4) pointer++; - else return FAILURE; - } - if (c() < 0x30 || c() > 0x39) return FAILURE; - while (c() >= 0x30 && c() <= 0x39) { - var num = c() - 0x30; - if (ipv4Piece === null) ipv4Piece = num; - else if (ipv4Piece === 0) return FAILURE; - else ipv4Piece = ipv4Piece * 10 + num; - if (ipv4Piece > 255) return FAILURE; - pointer++; - } - address[pieceIndex] = address[pieceIndex] * 0x100 + ipv4Piece; - numbersSeen++; - if (numbersSeen === 2 || numbersSeen === 4) pieceIndex++; - } - if (numbersSeen !== 4) return FAILURE; - break; - } else if (c() === 0x3a) { - pointer++; - if (c() === -1) return FAILURE; - } else if (c() !== -1) { - return FAILURE; - } - address[pieceIndex] = value; - pieceIndex++; - } - if (compress !== null) { - var swaps = pieceIndex - compress; - pieceIndex = 7; - while (pieceIndex !== 0 && swaps > 0) { - var tmp = address[compress + swaps - 1]; - address[compress + swaps - 1] = address[pieceIndex]; - address[pieceIndex] = tmp; - pieceIndex--; - swaps--; - } - } else if (compress === null && pieceIndex !== 8) { - return FAILURE; - } - return '[' + serializeIPv6(address) + ']'; - } - - function serializeIPv6(address) { - var output = ''; - // Find the longest run (length > 1) of zero pieces to compress. - var longestIdx = -1; - var longestLen = 0; - var curIdx = -1; - var curLen = 0; - for (var i = 0; i < 8; i++) { - if (address[i] === 0) { - if (curIdx === -1) curIdx = i; - curLen++; - if (curLen > longestLen) { - longestLen = curLen; - longestIdx = curIdx; - } - } else { - curIdx = -1; - curLen = 0; - } - } - var compress = longestLen > 1 ? longestIdx : -1; - var ignore0 = false; - for (var p = 0; p < 8; p++) { - if (ignore0 && address[p] === 0) continue; - else if (ignore0) ignore0 = false; - if (compress === p) { - output += p === 0 ? '::' : ':'; - ignore0 = true; - continue; - } - output += address[p].toString(16); - if (p !== 7) output += ':'; - } - return output; - } - - function parseOpaqueHost(input) { - for (var i = 0; i < input.length; i++) { - var cp = StringPrototypeCodePointAt(input, i); - if (cp > 0xffff) i++; - if (isForbiddenHostCodePoint(cp)) return FAILURE; - } - return utf8PercentEncode(input, inC0ControlSet); - } - - // A host that is already a lowercase ASCII domain — no percent escapes, no - // uppercase (IDNA maps case), no Unicode, no punycode label to expand — - // percent-decodes and IDNA-maps to itself. This is nearly every real-world - // URL, and it skips the per-parse percentDecode → TextDecoder → domainToASCII - // round-trip that otherwise dominates host parsing. - var SIMPLE_HOST_RE = /^[a-z0-9._-]+$/; - - function parseHost(input, isSpecial) { - if (input[0] === '[') { - if (input[input.length - 1] !== ']') return FAILURE; - return parseIPv6(input.slice(1, -1)); - } - if (!isSpecial) return parseOpaqueHost(input); - if (reTest(SIMPLE_HOST_RE, input) && input.indexOf('xn--') === -1) { - if (endsInANumber(input)) return parseIPv4(input); - return input; - } - var domain = percentDecodeHostStrict(input); - if (domain === FAILURE) return FAILURE; - var ascii = domainToASCII(domain); - if (ascii === FAILURE) return FAILURE; - for (var i = 0; i < ascii.length; i++) { - if (isForbiddenDomainCodePoint(StringPrototypeCharCodeAt(ascii, i))) return FAILURE; - } - if (endsInANumber(ascii)) return parseIPv4(ascii); - return ascii; - } - - // ------------------------------------------------------------------ // - // URL record + serialization // - // ------------------------------------------------------------------ // - - // null prototype: SPECIAL_SCHEMES[scheme] is read for arbitrary schemes, so a - // normal object would inherit a poisoned Object.prototype key (e.g. - // Object.prototype.foo = 8080 makes the default-port check match and silently - // drop a real port). __proto__:null cuts the chain — a non-special scheme reads - // undefined, as it must. - var SPECIAL_SCHEMES = { - __proto__: null, - ftp: 21, - file: null, - http: 80, - https: 443, - ws: 80, - wss: 443, - }; - - function isSpecialScheme(scheme) { - return Object.prototype.hasOwnProperty.call(SPECIAL_SCHEMES, scheme); - } - - function newRecord() { - // A plain literal, NOT a null-proto object: this record is accessed only via - // fixed, hard-coded field names (url.scheme, url.host, …), never by an - // attacker-controlled key, so it is not exposed to prototype-key pollution - // and needs no null prototype. (null-proto is reserved for the lookup MAPS - // keyed by external strings, e.g. SPECIAL_SCHEMES.) - return { - scheme: '', - username: '', - password: '', - host: null, - port: null, - path: [], // array of segments, or a string when opaque - query: null, - fragment: null, - opaque: false, // true => path is an opaque string - }; - } - - function recordIsSpecial(url) { - return isSpecialScheme(url.scheme); - } - - function includesCredentials(url) { - return url.username !== '' || url.password !== ''; - } - - function cannotHaveUsernamePasswordPort(url) { - return url.host === null || url.host === '' || url.scheme === 'file'; - } - - function serializePath(url) { - if (url.opaque) return url.path; - if (url.path.length === 0) return ''; - return '/' + ArrayPrototypeJoin(url.path, '/'); - } - - function serializeHostPort(url) { - if (url.host === null) return ''; - var out = url.host; - if (url.port !== null) out += ':' + url.port; - return out; - } - - function serializeURL(url, excludeFragment) { - var output = url.scheme + ':'; - if (url.host !== null) { - output += '//'; - if (includesCredentials(url)) { - output += url.username; - if (url.password !== '') output += ':' + url.password; - output += '@'; - } - output += serializeHostPort(url); - } - if (url.host === null && !url.opaque && url.path.length > 1 && url.path[0] === '') { - // Avoid the first empty segment being parsed back as an authority. - output += '/.'; - } - output += serializePath(url); - if (url.query !== null) output += '?' + url.query; - if (!excludeFragment && url.fragment !== null) output += '#' + url.fragment; - return output; - } - - function serializeOrigin(url) { - switch (url.scheme) { - case 'http': - case 'https': - case 'ws': - case 'wss': - case 'ftp': - return url.scheme + '://' + serializeHostPort(url); - case 'blob': { - var pathStr = url.opaque ? url.path : serializePath(url); - try { - var inner = parseURLOrThrow(pathStr); - return serializeOrigin(inner); - } catch { - return 'null'; - } - } - default: - return 'null'; - } - } - - // ------------------------------------------------------------------ // - // The basic URL parser (state machine, with state-override support) // - // ------------------------------------------------------------------ // - - var SCHEME_START = 0, - SCHEME = 1, - NO_SCHEME = 2, - SPECIAL_RELATIVE_OR_AUTHORITY = 3, - PATH_OR_AUTHORITY = 4, - RELATIVE = 5, - RELATIVE_SLASH = 6, - SPECIAL_AUTHORITY_SLASHES = 7, - SPECIAL_AUTHORITY_IGNORE_SLASHES = 8, - AUTHORITY = 9, - HOST = 10, - PORT = 11, - FILE = 12, - FILE_SLASH = 13, - FILE_HOST = 14, - PATH_START = 15, - PATH = 16, - OPAQUE_PATH = 17, - QUERY = 18, - FRAGMENT = 19, - // The hostname setter shares the HOST state machine but must stop before a - // port (it returns at a top-level ':'). A distinct constant flags that. - HOSTNAME = 20; - - function isASCIIAlpha(cp) { - return (cp >= 0x41 && cp <= 0x5a) || (cp >= 0x61 && cp <= 0x7a); - } - function isASCIIAlphanumeric(cp) { - return isASCIIAlpha(cp) || isAsciiDigit(cp); - } - function isSingleDot(seg) { - return seg === '.' || StringPrototypeToLowerCase(seg) === '%2e'; - } - function isDoubleDot(seg) { - var l = StringPrototypeToLowerCase(seg); - return l === '..' || l === '.%2e' || l === '%2e.' || l === '%2e%2e'; - } - function isWindowsDriveLetter(s) { - return ( - s.length === 2 && - isASCIIAlpha(StringPrototypeCharCodeAt(s, 0)) && - (StringPrototypeCharCodeAt(s, 1) === 0x3a || StringPrototypeCharCodeAt(s, 1) === 0x7c) - ); - } - function isNormalizedWindowsDriveLetter(s) { - return ( - s.length === 2 && - isASCIIAlpha(StringPrototypeCharCodeAt(s, 0)) && - StringPrototypeCharCodeAt(s, 1) === 0x3a - ); - } - function startsWithWindowsDriveLetter(str, i) { - var rest = str.slice(i); - return ( - rest.length >= 2 && - isWindowsDriveLetter(rest.slice(0, 2)) && - (rest.length === 2 || - rest[2] === '/' || - rest[2] === '\\' || - rest[2] === '?' || - rest[2] === '#') - ); - } - - // Returns a url record on success, or FAILURE. `base` is a url record or - // undefined; `url`/`stateOverride` drive the setter path. - function basicURLParse(input, base, url, stateOverride) { - if (url === undefined) { - url = newRecord(); - // Remove leading/trailing C0 control or space (only when not a setter). - input = trimC0ControlOrSpace(input); - } - // Remove all ASCII tab or newline (guarded: almost no real input has them, - // and replaceAll would re-scan + re-allocate on every parse). - input = stripChars(input, isTabOrNewline); - - // Code POINTS as numbers, one pass, surrogate-aware — same iteration as - // ArrayFrom(input).map(c => c.codePointAt(0)) without allocating a - // single-character string per code point (the old form dominated the whole - // parse: one string alloc + one codePointAt call per character per read). - var cps = []; - for (var ci = 0; ci < input.length;) { - var cc = StringPrototypeCodePointAt(input, ci); - ArrayPrototypePush(cps, cc); - ci += cc > 0xffff ? 2 : 1; - } - var pointer = 0; - // The hostname override runs the HOST state but is flagged via stateOverride - // so the HOST case knows to stop before a port. - var state = - stateOverride === HOSTNAME - ? HOST - : stateOverride !== undefined - ? stateOverride - : SCHEME_START; - var buffer = ''; - var atSignSeen = false; - var insideBrackets = false; - var passwordTokenSeen = false; - var special = recordIsSpecial(url); - - function cp(idx) { - return idx < cps.length ? cps[idx] : -1; - } - function remaining(n) { - // The code point n positions past the current pointer (n === 0 is the - // character immediately following the one being processed). - return pointer + 1 + n < cps.length ? cps[pointer + 1 + n] : -1; - } - - for (; pointer <= cps.length; pointer++) { - var c = cp(pointer); - switch (state) { - case SCHEME_START: - if (isASCIIAlpha(c)) { - buffer += StringFromCharCode(c | 0x20); - state = SCHEME; - } else if (stateOverride === undefined) { - state = NO_SCHEME; - pointer--; - } else { - return FAILURE; - } - break; - - case SCHEME: - if (isASCIIAlphanumeric(c) || c === 0x2b || c === 0x2d || c === 0x2e) { - buffer += StringFromCharCode(c >= 0x41 && c <= 0x5a ? c | 0x20 : c); - } else if (c === 0x3a) { - if (stateOverride !== undefined) { - var bufSpecial = isSpecialScheme(buffer); - if (special && !bufSpecial) return url; - if (!special && bufSpecial) return url; - if ((includesCredentials(url) || url.port !== null) && buffer === 'file') return url; - if (url.scheme === 'file' && url.host === '') return url; - } - url.scheme = buffer; - if (stateOverride !== undefined) { - if (url.port === SPECIAL_SCHEMES[url.scheme]) url.port = null; - return url; - } - buffer = ''; - special = recordIsSpecial(url); - if (url.scheme === 'file') { - state = FILE; - } else if (special && base !== undefined && base.scheme === url.scheme) { - state = SPECIAL_RELATIVE_OR_AUTHORITY; - } else if (special) { - state = SPECIAL_AUTHORITY_SLASHES; - } else if (remaining(0) === 0x2f) { - state = PATH_OR_AUTHORITY; - pointer++; - } else { - url.opaque = true; - url.path = ''; - state = OPAQUE_PATH; - } - } else if (stateOverride === undefined) { - buffer = ''; - state = NO_SCHEME; - pointer = -1; - } else { - return FAILURE; - } - break; - - case NO_SCHEME: - if (base === undefined || (base.opaque && c !== 0x23)) { - return FAILURE; - } else if (base.opaque && c === 0x23) { - url.scheme = base.scheme; - url.path = base.path; - url.opaque = true; - url.query = base.query; - url.fragment = ''; - state = FRAGMENT; - } else if (base.scheme !== 'file') { - state = RELATIVE; - pointer--; - } else { - state = FILE; - pointer--; - } - break; - - case SPECIAL_RELATIVE_OR_AUTHORITY: - if (c === 0x2f && remaining(0) === 0x2f) { - state = SPECIAL_AUTHORITY_IGNORE_SLASHES; - pointer++; - } else { - state = RELATIVE; - pointer--; - } - break; - - case PATH_OR_AUTHORITY: - if (c === 0x2f) { - state = AUTHORITY; - } else { - state = PATH; - pointer--; - } - break; - - case RELATIVE: - url.scheme = base.scheme; - special = recordIsSpecial(url); - if (c === 0x2f) { - state = RELATIVE_SLASH; - } else if (special && c === 0x5c) { - state = RELATIVE_SLASH; - } else { - url.username = base.username; - url.password = base.password; - url.host = base.host; - url.port = base.port; - url.path = ArrayPrototypeSlice(base.path); - url.query = base.query; - if (c === 0x3f) { - url.query = ''; - state = QUERY; - } else if (c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } else if (c !== -1) { - url.query = null; - ArrayPrototypePop(url.path); - state = PATH; - pointer--; - } - } - break; - - case RELATIVE_SLASH: - if (special && (c === 0x2f || c === 0x5c)) { - state = SPECIAL_AUTHORITY_IGNORE_SLASHES; - } else if (c === 0x2f) { - state = AUTHORITY; - } else { - url.username = base.username; - url.password = base.password; - url.host = base.host; - url.port = base.port; - state = PATH; - pointer--; - } - break; - - case SPECIAL_AUTHORITY_SLASHES: - if (c === 0x2f && remaining(0) === 0x2f) { - state = SPECIAL_AUTHORITY_IGNORE_SLASHES; - pointer++; - } else { - state = SPECIAL_AUTHORITY_IGNORE_SLASHES; - pointer--; - } - break; - - case SPECIAL_AUTHORITY_IGNORE_SLASHES: - if (c !== 0x2f && c !== 0x5c) { - state = AUTHORITY; - pointer--; - } - break; - - case AUTHORITY: - if (c === 0x40) { - if (atSignSeen) buffer = '%40' + buffer; - atSignSeen = true; - for (var bi = 0; bi < buffer.length; bi++) { - var bcp = StringPrototypeCodePointAt(buffer, bi); - if (bcp > 0xffff) bi++; - if (bcp === 0x3a && !passwordTokenSeen) { - passwordTokenSeen = true; - continue; - } - var encoded = utf8PercentEncode(StringFromCodePoint(bcp), inUserinfoSet); - if (passwordTokenSeen) url.password += encoded; - else url.username += encoded; - } - buffer = ''; - } else if ( - c === -1 || - c === 0x2f || - c === 0x3f || - c === 0x23 || - (special && c === 0x5c) - ) { - if (atSignSeen && buffer === '') return FAILURE; - pointer -= ArrayFrom(buffer).length + 1; - buffer = ''; - state = HOST; - } else { - buffer += cpToStr(c); - } - break; - - case HOST: - if (stateOverride !== undefined && url.scheme === 'file') { - pointer--; - state = FILE_HOST; - } else if (c === 0x3a && !insideBrackets) { - if (buffer === '') return FAILURE; - // The hostname setter (HOSTNAME override) stops before a port. - if (stateOverride === HOSTNAME) return url; - var host = parseHost(buffer, special); - if (host === FAILURE) return FAILURE; - url.host = host; - buffer = ''; - state = PORT; - } else if ( - c === -1 || - c === 0x2f || - c === 0x3f || - c === 0x23 || - (special && c === 0x5c) - ) { - pointer--; - if (special && buffer === '') return FAILURE; - if ( - stateOverride !== undefined && - buffer === '' && - (includesCredentials(url) || url.port !== null) - ) - return url; - var host2 = parseHost(buffer, special); - if (host2 === FAILURE) return FAILURE; - url.host = host2; - buffer = ''; - state = PATH_START; - if (stateOverride !== undefined) return url; - } else { - if (c === 0x5b) insideBrackets = true; - else if (c === 0x5d) insideBrackets = false; - buffer += cpToStr(c); - } - break; - - case PORT: - if (isAsciiDigit(c)) { - buffer += StringFromCharCode(c); - } else if ( - c === -1 || - c === 0x2f || - c === 0x3f || - c === 0x23 || - (special && c === 0x5c) || - stateOverride !== undefined - ) { - if (buffer !== '') { - var portNum = parseIntG(buffer, 10); - if (portNum > 65535) return FAILURE; - url.port = portNum === SPECIAL_SCHEMES[url.scheme] ? null : portNum; - buffer = ''; - } - if (stateOverride !== undefined) return url; - state = PATH_START; - pointer--; - } else { - return FAILURE; - } - break; - - case FILE: - url.scheme = 'file'; - special = true; - url.host = ''; - if (c === 0x2f || c === 0x5c) { - state = FILE_SLASH; - } else if (base !== undefined && base.scheme === 'file') { - url.host = base.host; - url.path = ArrayPrototypeSlice(base.path); - url.query = base.query; - if (c === 0x3f) { - url.query = ''; - state = QUERY; - } else if (c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } else if (c !== -1) { - url.query = null; - if (!startsWithWindowsDriveLetter(input, pointer)) { - shortenPath(url); - } else { - url.path = []; - } - state = PATH; - pointer--; - } - } else { - state = PATH; - pointer--; - } - break; - - case FILE_SLASH: - if (c === 0x2f || c === 0x5c) { - state = FILE_HOST; - } else { - if ( - base !== undefined && - base.scheme === 'file' && - !startsWithWindowsDriveLetter(input, pointer) - ) { - if (isNormalizedWindowsDriveLetter(base.path[0] || '')) { - ArrayPrototypePush(url.path, base.path[0]); - } else { - url.host = base.host; - } - } - state = PATH; - pointer--; - } - break; - - case FILE_HOST: - if (c === -1 || c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23) { - pointer--; - if (stateOverride === undefined && isWindowsDriveLetter(buffer)) { - state = PATH; - } else if (buffer === '') { - url.host = ''; - if (stateOverride !== undefined) return url; - state = PATH_START; - } else { - var fhost = parseHost(buffer, special); - if (fhost === FAILURE) return FAILURE; - if (fhost === 'localhost') fhost = ''; - url.host = fhost; - if (stateOverride !== undefined) return url; - buffer = ''; - state = PATH_START; - } - } else { - buffer += cpToStr(c); - } - break; - - case PATH_START: - if (special) { - state = PATH; - if (c !== 0x2f && c !== 0x5c) pointer--; - } else if (stateOverride === undefined && c === 0x3f) { - url.query = ''; - state = QUERY; - } else if (stateOverride === undefined && c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } else if (c !== -1) { - state = PATH; - if (c !== 0x2f) pointer--; - } else if (stateOverride !== undefined && url.host === null) { - ArrayPrototypePush(url.path, ''); - } - break; - - case PATH: - if ( - c === -1 || - c === 0x2f || - (special && c === 0x5c) || - (stateOverride === undefined && (c === 0x3f || c === 0x23)) - ) { - if (isDoubleDot(buffer)) { - shortenPath(url); - if (c !== 0x2f && !(special && c === 0x5c)) ArrayPrototypePush(url.path, ''); - } else if (isSingleDot(buffer)) { - if (c !== 0x2f && !(special && c === 0x5c)) ArrayPrototypePush(url.path, ''); - } else { - if (url.scheme === 'file' && url.path.length === 0 && isWindowsDriveLetter(buffer)) { - buffer = buffer[0] + ':'; - } - ArrayPrototypePush(url.path, buffer); - } - buffer = ''; - if (c === 0x3f) { - url.query = ''; - state = QUERY; - } else if (c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } - } else { - buffer += percentEncodeCp(c, inPathSet); - } - break; - - case OPAQUE_PATH: - if (c === 0x3f) { - url.query = ''; - state = QUERY; - } else if (c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } else if (c !== -1) { - url.path += percentEncodeCp(c, inC0ControlSet); - } - break; - - case QUERY: - if ((stateOverride === undefined && c === 0x23) || c === -1) { - var querySet = special ? inSpecialQuerySet : inQuerySet; - url.query += utf8PercentEncode(buffer, querySet); - buffer = ''; - if (c === 0x23) { - url.fragment = ''; - state = FRAGMENT; - } - } else if (c !== -1) { - buffer += cpToStr(c); - } - break; - - case FRAGMENT: - if (c !== -1) { - url.fragment += percentEncodeCp(c, inFragmentSet); - } - break; - } - if (pointer > cps.length) break; - } - return url; - } - - function shortenPath(url) { - if (url.opaque) return; - var path = url.path; - if (url.scheme === 'file' && path.length === 1 && isNormalizedWindowsDriveLetter(path[0])) { - return; - } - ArrayPrototypePop(path); - } - - function parseURLOrThrow(input, base) { - var baseRecord; - if (base !== undefined && base !== null) { - baseRecord = basicURLParse(StringG(base)); - if (baseRecord === FAILURE) throw invalidURL(nativeError, input, base); - } - var record = basicURLParse(StringG(input), baseRecord); - if (record === FAILURE) { - throw invalidURL(nativeError, input, baseRecord === undefined ? undefined : base); - } - return record; - } - - // ERR_INVALID_URL in node's two shapes: the constructor's parse runs in C++, so - // String(e) is `TypeError: Invalid URL`; the href setter throws from JS, so it is - // `TypeError [ERR_INVALID_URL]: Invalid URL`. Both carry `input`, and `base` when the - // parse ran against one (node 26.13.0, case 76). - function invalidURL(make, input, base) { - var err = make(TypeErrorG, 'ERR_INVALID_URL', 'Invalid URL'); - err.input = StringG(input); - if (base !== undefined) err.base = StringG(base); - return err; - } - - // ------------------------------------------------------------------ // - // application/x-www-form-urlencoded // - // ------------------------------------------------------------------ // - - function isFormUrlencodedSafe(cp) { - return ( - cp === 0x2a || - cp === 0x2d || - cp === 0x2e || - (cp >= 0x30 && cp <= 0x39) || - (cp >= 0x41 && cp <= 0x5a) || - cp === 0x5f || - (cp >= 0x61 && cp <= 0x7a) - ); - } - - function formUrlencodedByteSerialize(str) { - var bytes = encoder().encode(str); - var out = ''; - for (var i = 0; i < bytes.length; i++) { - var b = bytes[i]; - if (b === 0x20) out += '+'; - else if (isFormUrlencodedSafe(b)) out += StringFromCharCode(b); - else out += percentEncodeByte(b); - } - return out; - } + var ERR_INVALID_ARG_TYPE = require('internal/errors').ERR_INVALID_ARG_TYPE; - function formUrlencodedParse(input) { - var sequences = input === '' ? [] : StringPrototypeSplit(input, '&'); - var output = []; - for (var i = 0; i < sequences.length; i++) { - var bytes = sequences[i]; - if (bytes === '') continue; - var name, value; - var eq = bytes.indexOf('='); - if (eq === -1) { - name = bytes; - value = ''; - } else { - name = bytes.slice(0, eq); - value = bytes.slice(eq + 1); - } - name = percentDecode(StringPrototypeReplaceAll(name, '+', ' ')); - value = percentDecode(StringPrototypeReplaceAll(value, '+', ' ')); - ArrayPrototypePush(output, [name, value]); - } - return output; - } - - function formUrlencodedSerialize(list) { - var out = ''; - for (var i = 0; i < list.length; i++) { - if (i > 0) out += '&'; - out += - formUrlencodedByteSerialize(list[i][0]) + '=' + formUrlencodedByteSerialize(list[i][1]); - } - return out; - } - - // ------------------------------------------------------------------ // - // URLSearchParams // - // ------------------------------------------------------------------ // - - var kList = Symbol('list'); - var kURLObject = Symbol('urlObject'); - - function URLSearchParams(init) { - this[kList] = []; - this[kURLObject] = null; - if (init === undefined || init === null) return; - if (typeof init === 'object' || typeof init === 'function') { - if (init instanceof URLSearchParams) { - var src = init[kList]; - for (var i = 0; i < src.length; i++) - ArrayPrototypePush(this[kList], [src[i][0], src[i][1]]); - return; - } - if (typeof init[Symbol.iterator] === 'function') { - var idx = 0; - for (var pair of init) { - if (typeof pair !== 'object' && typeof pair !== 'function') { - throw new TypeError('The provided value cannot be converted to a sequence.'); - } - var arr = []; - for (var v of pair) ArrayPrototypePush(arr, v); - if (arr.length !== 2) { - throw new TypeError('Each query pair must be an iterable [name, value] tuple'); - } - ArrayPrototypePush(this[kList], [toUSVString(arr[0]), toUSVString(arr[1])]); - idx++; - } - return; - } - // Record of string -> string. - var keys = ObjectKeys(init); - for (var k = 0; k < keys.length; k++) { - ArrayPrototypePush(this[kList], [toUSVString(keys[k]), toUSVString(init[keys[k]])]); - } - return; - } - // String init: strip a single leading '?'. - var str = toUSVString(init); - if (str[0] === '?') str = str.slice(1); - this[kList] = formUrlencodedParse(str); - } - - // Convert to a USVString: stringify, then replace any unpaired surrogate with - // U+FFFD (per WHATWG). The fast path skips strings with no surrogate at all. - // - // A code-unit walk, not a GLOBAL regex replace. The pre-scan below is not a guard - // against the poison it looks like one against: a VALID pair — any emoji in a - // remote query string — sets `hasSurrogate` too, and the global replace then - // never terminates under a forged `RegExp.prototype.exec`, because - // `Symbol.replace` advances `lastIndex` only on an empty match. Measured: - // `new URLSearchParams('a=')` and `url.hash = 'x'` both hung, and - // `new URLSearchParams(remoteQuery)` is on the network path. - // - // The pre-scan stays because it is the common case and it now decides a real fast - // path — most strings have no surrogate at all and are returned untouched. - function toUSVString(value) { - var str = typeof value === 'string' ? value : StringG(value); - var n = str.length; - var hasSurrogate = false; - for (var i = 0; i < n; i++) { - var c = StringPrototypeCharCodeAt(str, i); - if (c >= 0xd800 && c <= 0xdfff) { - hasSurrogate = true; - break; - } - } - if (!hasSurrogate) return str; - var out = StringPrototypeSlice(str, 0, i); - for (; i < n; i++) { - var cu = StringPrototypeCharCodeAt(str, i); - if (cu < 0xd800 || cu > 0xdfff) { - out += StringFromCharCode(cu); - continue; - } - if (cu <= 0xdbff && i + 1 < n) { - var lo = StringPrototypeCharCodeAt(str, i + 1); - if (lo >= 0xdc00 && lo <= 0xdfff) { - // A valid pair is kept verbatim. - out += StringFromCharCode(cu) + StringFromCharCode(lo); - i++; - continue; - } - } - out += '\uFFFD'; // lone high or lone low surrogate - } - return out; - } - - function spUpdate(sp) { - var urlObj = sp[kURLObject]; - if (urlObj === null) return; - var serialized = formUrlencodedSerialize(sp[kList]); - urlObj._url.query = serialized === '' ? null : serialized; - } - - ObjectDefineProperty(URLSearchParams.prototype, 'append', { - configurable: true, - writable: true, - value: function append(name, value) { - if (arguments.length < 2) { - throw new TypeError('The "name" and "value" arguments must be specified'); - } - ArrayPrototypePush(this[kList], [toUSVString(name), toUSVString(value)]); - spUpdate(this); - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'delete', { - configurable: true, - writable: true, - value: function del(name) { - var n = toUSVString(name); - var list = this[kList]; - if (arguments.length > 1 && arguments[1] !== undefined) { - var val = toUSVString(arguments[1]); - for (var i = list.length - 1; i >= 0; i--) { - if (list[i][0] === n && list[i][1] === val) ArrayPrototypeSplice(list, i, 1); - } - } else { - for (var j = list.length - 1; j >= 0; j--) { - if (list[j][0] === n) ArrayPrototypeSplice(list, j, 1); - } - } - spUpdate(this); - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'get', { - configurable: true, - writable: true, - value: function get(name) { - var n = toUSVString(name); - var list = this[kList]; - for (var i = 0; i < list.length; i++) { - if (list[i][0] === n) return list[i][1]; - } - return null; - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'getAll', { - configurable: true, - writable: true, - value: function getAll(name) { - var n = toUSVString(name); - var list = this[kList]; - var out = []; - for (var i = 0; i < list.length; i++) { - if (list[i][0] === n) ArrayPrototypePush(out, list[i][1]); - } - return out; - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'has', { - configurable: true, - writable: true, - value: function has(name) { - var n = toUSVString(name); - var list = this[kList]; - if (arguments.length > 1 && arguments[1] !== undefined) { - var val = toUSVString(arguments[1]); - for (var i = 0; i < list.length; i++) { - if (list[i][0] === n && list[i][1] === val) return true; - } - return false; - } - for (var j = 0; j < list.length; j++) { - if (list[j][0] === n) return true; - } - return false; - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'set', { - configurable: true, - writable: true, - value: function set(name, value) { - if (arguments.length < 2) { - throw new TypeError('The "name" and "value" arguments must be specified'); - } - var n = toUSVString(name); - var v = toUSVString(value); - var list = this[kList]; - // Set the first matching tuple in place and drop the rest (URL Standard). - var found = false; - for (var i = 0; i < list.length;) { - if (list[i][0] === n) { - if (found) { - ArrayPrototypeSplice(list, i, 1); - continue; - } - list[i][1] = v; - found = true; - } - i++; - } - if (!found) ArrayPrototypePush(list, [n, v]); - spUpdate(this); - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'sort', { - configurable: true, - writable: true, - value: function sort() { - // Stable sort by code units of the name only. - ArrayPrototypeSort(this[kList], function (a, b) { - return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0; - }); - spUpdate(this); - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'forEach', { - configurable: true, - writable: true, - value: function forEach(callback, thisArg) { - if (typeof callback !== 'function') { - throw new TypeError('Callback must be a function'); - } - var list = this[kList]; - // Read length lazily so callbacks that append are observed (per spec). - for (var i = 0; i < list.length; i++) { - callback.call(thisArg, list[i][1], list[i][0], this); - } - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'keys', { - configurable: true, - writable: true, - value: function keys() { - return makeSearchParamsIterator(this, 'key'); - }, - }); - ObjectDefineProperty(URLSearchParams.prototype, 'values', { - configurable: true, - writable: true, - value: function values() { - return makeSearchParamsIterator(this, 'value'); - }, - }); - ObjectDefineProperty(URLSearchParams.prototype, 'entries', { - configurable: true, - writable: true, - value: function entries() { - return makeSearchParamsIterator(this, 'key+value'); - }, - }); - ObjectDefineProperty(URLSearchParams.prototype, Symbol.iterator, { - configurable: true, - writable: true, - value: URLSearchParams.prototype.entries, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'size', { - configurable: true, - enumerable: true, - get: function size() { - return this[kList].length; - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, 'toString', { - configurable: true, - writable: true, - value: function toString() { - return formUrlencodedSerialize(this[kList]); - }, - }); - - ObjectDefineProperty(URLSearchParams.prototype, Symbol.toStringTag, { - configurable: true, - value: 'URLSearchParams', - }); - - function makeSearchParamsIterator(sp, kind) { - var index = 0; - var iterator = { - next: function next() { - var list = sp[kList]; - if (index >= list.length) { - return { value: undefined, done: true }; - } - var entry = list[index]; - index++; - var value; - if (kind === 'key') value = entry[0]; - else if (kind === 'value') value = entry[1]; - else value = [entry[0], entry[1]]; - return { value: value, done: false }; - }, - }; - iterator[Symbol.iterator] = function () { - return this; - }; - ObjectDefineProperty(iterator, Symbol.toStringTag, { - configurable: true, - value: 'URLSearchParams Iterator', - }); - return iterator; - } - - // Build a URLSearchParams bound to a URL record (no constructor side effects). - function newBoundSearchParams(urlObj) { - var sp = ObjectCreate(URLSearchParams.prototype); - sp[kList] = urlObj._url.query === null ? [] : formUrlencodedParse(urlObj._url.query); - sp[kURLObject] = urlObj; - return sp; - } - - // ------------------------------------------------------------------ // - // URL // - // ------------------------------------------------------------------ // - - var kRecord = Symbol('record'); - var kSearchParams = Symbol('searchParams'); - - function URL(input, base) { - if (!(this instanceof URL)) { - throw new TypeError("Class constructor URL cannot be invoked without 'new'"); - } - if (arguments.length === 0) { - throw new TypeError("Failed to construct 'URL': 1 argument required, but only 0 present."); - } - this[kRecord] = parseURLOrThrow(input, base); - this[kSearchParams] = null; - } - - function refreshSearchParams(url) { - if (url[kSearchParams] !== null) { - var sp = url[kSearchParams]; - sp[kList] = url._url.query === null ? [] : formUrlencodedParse(url._url.query); - } - } - - ObjectDefineProperty(URL.prototype, '_url', { - configurable: true, - get: function () { - return this[kRecord]; - }, - }); - - ObjectDefineProperty(URL.prototype, 'href', { - configurable: true, - enumerable: true, - get: function () { - return serializeURL(this[kRecord]); - }, - set: function (value) { - var record = basicURLParse(toUSVString(value)); - if (record === FAILURE) throw invalidURL(nodeError, value); - this[kRecord] = record; - refreshSearchParams(this); - }, - }); - - ObjectDefineProperty(URL.prototype, 'origin', { - configurable: true, - enumerable: true, - get: function () { - return serializeOrigin(this[kRecord]); - }, - }); - - ObjectDefineProperty(URL.prototype, 'protocol', { - configurable: true, - enumerable: true, - get: function () { - return this[kRecord].scheme + ':'; - }, - set: function (value) { - basicURLParse(toUSVString(value) + ':', undefined, this[kRecord], SCHEME_START); - }, - }); - - ObjectDefineProperty(URL.prototype, 'username', { - configurable: true, - enumerable: true, - get: function () { - return this[kRecord].username; - }, - set: function (value) { - var url = this[kRecord]; - if (cannotHaveUsernamePasswordPort(url)) return; - url.username = utf8PercentEncode(toUSVString(value), inUserinfoSet); - }, - }); - - ObjectDefineProperty(URL.prototype, 'password', { - configurable: true, - enumerable: true, - get: function () { - return this[kRecord].password; - }, - set: function (value) { - var url = this[kRecord]; - if (cannotHaveUsernamePasswordPort(url)) return; - url.password = utf8PercentEncode(toUSVString(value), inUserinfoSet); - }, - }); - - ObjectDefineProperty(URL.prototype, 'host', { - configurable: true, - enumerable: true, - get: function () { - return serializeHostPort(this[kRecord]); - }, - set: function (value) { - var url = this[kRecord]; - if (url.opaque) return; - basicURLParse(toUSVString(value), undefined, url, HOST); - }, - }); - - ObjectDefineProperty(URL.prototype, 'hostname', { - configurable: true, - enumerable: true, - get: function () { - return this[kRecord].host === null ? '' : this[kRecord].host; - }, - set: function (value) { - var url = this[kRecord]; - if (url.opaque) return; - basicURLParse(toUSVString(value), undefined, url, HOSTNAME); - }, - }); - - ObjectDefineProperty(URL.prototype, 'port', { - configurable: true, - enumerable: true, - get: function () { - return this[kRecord].port === null ? '' : StringG(this[kRecord].port); - }, - set: function (value) { - var url = this[kRecord]; - if (cannotHaveUsernamePasswordPort(url)) return; - var str = toUSVString(value); - if (str === '') { - url.port = null; - return; - } - basicURLParse(str, undefined, url, PORT); - }, - }); - - ObjectDefineProperty(URL.prototype, 'pathname', { - configurable: true, - enumerable: true, - get: function () { - return serializePath(this[kRecord]); - }, - set: function (value) { - var url = this[kRecord]; - if (url.opaque) return; - url.path = []; - basicURLParse(toUSVString(value), undefined, url, PATH_START); - }, - }); - - ObjectDefineProperty(URL.prototype, 'search', { - configurable: true, - enumerable: true, - get: function () { - var q = this[kRecord].query; - return q === null || q === '' ? '' : '?' + q; - }, - set: function (value) { - var url = this[kRecord]; - var str = toUSVString(value); - if (str === '') { - url.query = null; - refreshSearchParams(this); - return; - } - if (str[0] === '?') str = str.slice(1); - url.query = ''; - basicURLParse(str, undefined, url, QUERY); - refreshSearchParams(this); - }, - }); - - ObjectDefineProperty(URL.prototype, 'searchParams', { - configurable: true, - enumerable: true, - get: function () { - if (this[kSearchParams] === null) { - this[kSearchParams] = newBoundSearchParams(this); - } - return this[kSearchParams]; - }, - }); - - ObjectDefineProperty(URL.prototype, 'hash', { - configurable: true, - enumerable: true, - get: function () { - var f = this[kRecord].fragment; - return f === null || f === '' ? '' : '#' + f; - }, - set: function (value) { - var url = this[kRecord]; - var str = toUSVString(value); - if (str === '') { - url.fragment = null; - return; - } - if (str[0] === '#') str = str.slice(1); - url.fragment = ''; - basicURLParse(str, undefined, url, FRAGMENT); - }, - }); - - ObjectDefineProperty(URL.prototype, 'toString', { - configurable: true, - writable: true, - value: function toString() { - return serializeURL(this[kRecord]); - }, - }); - - ObjectDefineProperty(URL.prototype, 'toJSON', { - configurable: true, - writable: true, - value: function toJSON() { - return serializeURL(this[kRecord]); - }, - }); - - ObjectDefineProperty(URL.prototype, Symbol.toStringTag, { - configurable: true, - value: 'URL', - }); - - // canParse / parse statics (Node 19+/22+). - ObjectDefineProperty(URL, 'canParse', { - configurable: true, - writable: true, - value: function canParse(input, base) { - if (arguments.length === 0) { - throw new TypeError("Failed to execute 'canParse' on 'URL': 1 argument required."); - } - try { - parseURLOrThrow(input, base); - return true; - } catch { - return false; - } - }, - }); - - ObjectDefineProperty(URL, 'parse', { - configurable: true, - writable: true, - value: function parse(input, base) { - if (arguments.length === 0) { - throw new TypeError("Failed to execute 'parse' on 'URL': 1 argument required."); - } - try { - return new URL(input, base); - } catch { - return null; - } - }, - }); + var internalURL = require('internal/url'); + var URL = internalURL.URL; + var URLSearchParams = internalURL.URLSearchParams; + var kRecord = internalURL.kRecord; + var serializePath = internalURL.serializePath; + var domainToUnicode = internalURL.hostToUnicode; // ------------------------------------------------------------------ // // node:url helper functions // // ------------------------------------------------------------------ // - function fileURLToPath(input) { - // Node accepts only a string or a real URL instance — not an arbitrary - // {href}-shaped object — and rejects everything else with ERR_INVALID_ARG_TYPE - // so a programmer error can't silently become a filesystem path. - var href; - if (typeof input === 'string') href = new URL(input).href; - else if (input instanceof URL) href = input.href; - else throw ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], input); - return fileURLToPathFromHref(href); - } - - // The original file: → path translation (kept verbatim so the ESM loader and - // node:sqlite, which call it on an href, behave exactly as before). - function fileURLToPathFromHref(url) { - if (url.slice(0, 5).toLowerCase() !== 'file:') { - throw nodeError(TypeErrorG, 'ERR_INVALID_URL_SCHEME', 'The URL must be of scheme file'); - } - var isWindows = process.platform === 'win32'; - var rest = url.slice(5); - var host = ''; - if (rest.slice(0, 2) === '//') { - rest = rest.slice(2); - var slash = rest.indexOf('/'); - host = slash === -1 ? rest : rest.slice(0, slash); - rest = slash === -1 ? '/' : rest.slice(slash); - } - var isLocalhost = StringPrototypeToLowerCase(host) === 'localhost'; - var hash = rest.indexOf('#'); - if (hash !== -1) rest = rest.slice(0, hash); - var query = rest.indexOf('?'); - if (query !== -1) rest = rest.slice(0, query); - if (reTest(PERCENT_2F_RE, rest) || (isWindows && reTest(PERCENT_5C_RE, rest))) { - // node words the two platforms differently and attaches the href as `input`. - var sepErr = nodeError( - TypeErrorG, - 'ERR_INVALID_FILE_URL_PATH', - isWindows - ? 'File URL path must not include encoded \\ or / characters' - : 'File URL path must not include encoded / characters', - ); - sepErr.input = url; - throw sepErr; - } - - if (isWindows) { - var winPath = decodeURIComponentG(StringPrototypeReplaceAll(rest, '/', '\\')); - if (host !== '' && !isLocalhost) { - return '\\\\' + host + winPath; - } - var letter = StringPrototypeCharCodeAt(winPath, 1) | 0x20; - if (letter < 97 || letter > 122 || StringPrototypeCharAt(winPath, 2) !== ':') { - throw nodeError(TypeErrorG, 'ERR_INVALID_FILE_URL_PATH', 'File URL path must be absolute'); - } - return winPath.slice(1); - } - - if (host !== '' && !isLocalhost) { - throw nodeError( - TypeErrorG, - 'ERR_INVALID_FILE_URL_HOST', - 'File URL host must be "localhost" or empty on ' + process.platform, - ); - } - return decodeURIComponentG(rest); - } - function pathToFileURL(filepath) { if (typeof filepath !== 'string') { throw ERR_INVALID_ARG_TYPE('path', 'string', filepath); @@ -2170,30 +86,6 @@ return url; } - function urlToHttpOptions(url) { - // Mirror Node's `{ __proto__: null, ...url, }`: own - // enumerable properties (e.g. request options a caller attached to the URL) - // are copied first, then the standard http.request fields are layered on top. - var options = ObjectCreate(null); - var ownKeys = ObjectKeys(url); - for (var i = 0; i < ownKeys.length; i++) options[ownKeys[i]] = url[ownKeys[i]]; - options.protocol = url.protocol; - options.hostname = - typeof url.hostname === 'string' && StringPrototypeStartsWith(url.hostname, '[') - ? url.hostname.slice(1, -1) - : url.hostname; - options.hash = url.hash; - options.search = url.search; - options.pathname = url.pathname; - options.path = '' + (url.pathname || '') + (url.search || ''); - options.href = url.href; - if (url.port !== '') options.port = NumberG(url.port); - if (url.username || url.password) { - options.auth = decodeURIComponentG(url.username) + ':' + decodeURIComponentG(url.password); - } - return options; - } - function format(urlInput, options) { if (!(urlInput instanceof URL)) { throw new TypeError('The "url" argument must be an instance of URL.'); @@ -2223,22 +115,6 @@ return output; } - // Extract and host-parse the domain argument to domainToASCII/Unicode, applying - // the same preprocessing the URL parser does: remove ASCII tab/newline, then take - // everything up to the first authority terminator (/ \ ? #). Returns the parsed - // host string, or FAILURE for an invalid domain. - function hostFromDomainArg(domain) { - var input = stripChars(StringG(domain), isTabOrNewline); - for (var i = 0; i < input.length; i++) { - var c = StringPrototypeCharCodeAt(input, i); - if (c === 0x2f || c === 0x5c || c === 0x3f || c === 0x23) { - input = input.slice(0, i); - break; - } - } - return parseHost(input, true); - } - // ------------------------------------------------------------------ // // Global installation + module exports // // ------------------------------------------------------------------ // @@ -2266,24 +142,11 @@ module.exports = { URL: URL, URLSearchParams: URLSearchParams, - fileURLToPath: fileURLToPath, + fileURLToPath: internalURL.fileURLToPath, pathToFileURL: pathToFileURL, - urlToHttpOptions: urlToHttpOptions, + urlToHttpOptions: internalURL.urlToHttpOptions, format: format, - // Node's domainToASCII/domainToUnicode validate their input as a host (the - // same machinery URL parsing uses) and return '' for anything that is not a - // valid domain — so they can be used as a normalization/validation primitive. - // Route through hostFromDomainArg (which applies the URL parser's host - // preprocessing: strip tab/newline, cut at the first authority terminator, - // then host-parse) so forbidden characters, bad percent/UTF-8, NUL, and - // URL-shaped strings all collapse to '' exactly as Node does. - domainToASCII: function (domain) { - var host = hostFromDomainArg(domain); - return host === FAILURE ? '' : host; - }, - domainToUnicode: function (domain) { - var host = hostFromDomainArg(domain); - return host === FAILURE ? '' : domainToUnicode(host); - }, + domainToASCII: internalURL.domainToASCII, + domainToUnicode: internalURL.domainToUnicode, }; }); diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index 763b256..d5d6c1f 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -215,6 +215,10 @@ const ROUTES = [ re: globToRe('**/js/node/url.js'), targets: ['make bench-gate', 'make test-property'], }, + { + re: globToRe('**/js/internal/url.js'), + targets: ['make bench-gate', 'make test-property'], + }, { re: globToRe('**/js/internal/encoding.js'), targets: ['make test-property'], diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index 52c59a3..9abb627 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -55,6 +55,14 @@ test('buffer internals route the property tests, entry and split file alike', () } }); +test('url internals route the URL bench and property tests, entry and split file alike', () => { + for (const p of ['pkg/runtime/js/node/url.js', 'pkg/runtime/js/internal/url.js']) { + const t = routePaths([p]).targets; + for (const g of ['make bench-gate', 'make test-property']) + assert.ok(t.includes(g), p + ' ' + g); + } +}); + test('invalid **.js style is not required — **/* works', () => { const r = routePaths(['pkg/runtime/js/bootstrap/console.js']); assert.ok(r.targets.includes('make check-js')); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 6114eac..cb3c9c2 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -170,7 +170,7 @@ { "name": "54-url-pollution X1 dies when endsInANumber goes back to a live re.test", "why": "Steered true, '4x' reads as numeric and parseInt gives 4, so http://1.2.3.4x/ silently becomes http://1.2.3.4/ — a host substitution.", - "source": "pkg/runtime/js/node/url.js", + "source": "pkg/runtime/js/internal/url.js", "find": " if (allChars(last, isAsciiDigit)) return true;", "replace": " if (last !== '' && /^[0-9]+$/.test(last)) return true;", "gate": "compat:tests/node-compat/cases/54-url-pollution.js", @@ -179,7 +179,7 @@ { "name": "54-url-pollution X2 dies when the %2f check goes back to a live re.test", "why": "The path-traversal guard, in the direction that discriminates: a steered validator makes a legitimate path look encoded and throws where node returns one.", - "source": "pkg/runtime/js/node/url.js", + "source": "pkg/runtime/js/internal/url.js", "find": "if (reTest(PERCENT_2F_RE, rest) || (isWindows && reTest(PERCENT_5C_RE, rest))) {", "replace": "if (/%2f/i.test(rest) || (isWindows && /%5c/i.test(rest))) {", "gate": "compat:tests/node-compat/cases/54-url-pollution.js", @@ -247,7 +247,7 @@ { "name": "the Lava-only test dies when toUSVString goes back to the global regex", "why": "The surrogate pre-scan is not a guard against this: a VALID pair — any emoji in a remote query — sets hasSurrogate too, and the global replace then spins (or throws RangeError from runaway growth). new URLSearchParams(remoteQuery) is on the network path. Hang vs throw is runner-dependent; expect_detail lists both.", - "source": "pkg/runtime/js/node/url.js", + "source": "pkg/runtime/js/internal/url.js", "find": " if (!hasSurrogate) return str;\n var out = StringPrototypeSlice(str, 0, i);", "replace": " if (!hasSurrogate) return str;\n var SPOL = /([\\uD800-\\uDBFF])([\\uDC00-\\uDFFF])?|([\\uDC00-\\uDFFF])/g;\n return StringPrototypeReplace(str, SPOL, function (m, h, l, lone) {\n return lone !== undefined || l === undefined ? '\\uFFFD' : m;\n });\n var out = StringPrototypeSlice(str, 0, i);", "gate": "odin:cmd/lava:main.regexp_pollution_immunity", @@ -2419,7 +2419,7 @@ { "name": "76-coded-error-shape dies when the URL constructor throws the JS shape", "why": "node parses URLs in C++, so `new URL('nope')` stringifies as `TypeError: Invalid URL` with no [code]; only the href setter throws the JS shape.", - "source": "pkg/runtime/js/node/url.js", + "source": "pkg/runtime/js/internal/url.js", "find": " if (baseRecord === FAILURE) throw invalidURL(nativeError, input, base);", "replace": " if (baseRecord === FAILURE) throw invalidURL(nodeError, input, base);", "gate": "compat:tests/node-compat/cases/76-coded-error-shape.js", @@ -2571,6 +2571,24 @@ "gate": "compat:tests/node-compat/cases/85-buffer-internal-modules.js", "expect_detail": "lava: readUInt8 offset: internal/buffer got node:buffer" }, + { + "name": "87-url-internal-modules dies when fileURLToPath is defined back in node:url", + "why": "node frames a fileURLToPath argument error in node:internal/url; a wrapper in node/url.js (the pre-split layout) throws from node:url again, and the case reads the frame, not the message.", + "source": "pkg/runtime/js/node/url.js", + "find": " fileURLToPath: internalURL.fileURLToPath,\n", + "replace": " fileURLToPath: function fileURLToPath(input) {\n if (typeof input !== 'string' && !(input instanceof URL)) {\n throw ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], input);\n }\n return internalURL.fileURLToPath(input);\n },\n", + "gate": "compat:tests/node-compat/cases/87-url-internal-modules.js", + "expect_detail": "lava: fileURLToPath type: internal/url got node:url" + }, + { + "name": "87-url-internal-modules dies when internal/url hands format the wrong host decoder", + "why": "format({unicode: true}) reaches the Punycode decoder only through internal/url's exports; an identity function there prints the xn-- host where node prints the Unicode one.", + "source": "pkg/runtime/js/internal/url.js", + "find": " hostToUnicode: domainToUnicode,\n", + "replace": " hostToUnicode: function (host) {\n return host;\n },\n", + "gate": "compat:tests/node-compat/cases/87-url-internal-modules.js", + "expect_detail": "node: format opts http://βόλοσ.com:8080/a%20b#f" + }, { "name": "86-os-internal-modules dies when internal/os/constants drops a table from os.constants", "why": "os.constants is now assembled in internal/os/constants.js, outside node:os; the case's per-table lines are what show the moved object still carries every table node's does, so losing one in the move must turn it red.", diff --git a/tests/node-compat/cases/87-url-internal-modules.js b/tests/node-compat/cases/87-url-internal-modules.js new file mode 100644 index 0000000..47a46b3 --- /dev/null +++ b/tests/node-compat/cases/87-url-internal-modules.js @@ -0,0 +1,138 @@ +// node:url is assembled from two files laid out like node's: js/internal/url.js holds +// the WHATWG half (the URL Standard parser, URL, URLSearchParams, fileURLToPath, +// urlToHttpOptions, domainToASCII/domainToUnicode — node's lib/internal/url.js), and +// node/url stays the public entry with pathToFileURL and format, which node's +// lib/url.js defines. Every throw from the WHATWG half used to carry only `node:url` +// frames, because everything was defined in url.js; now it names node:internal/url, +// exactly as node's does, and the two that stayed still read node:url on both. +// +// Each frame line prints `ok` when the stack carries `node::` and names the +// first `node:` frame it got otherwise (past internal/errors, which builds the +// error). Only the source name is compared, never the message, code, line or column: +// several of these throw an uncoded TypeError on Lava where node's is coded, which is +// a pre-existing gap outside this split. +// +// The new id must stay internal: neither the bare nor the `node:` spelling may resolve +// from user code. Only the error CODE of the bare spelling is printed (the messages +// diverge, #415). +// +// The export lines check what crosses the new seam: the keys, their descriptor kinds, +// the identities the globals share, the object-URL statics node:buffer attaches, and +// behavior that runs through the internals node/url borrows (format reads the record, +// the path serializer and the Punycode decoder; pathToFileURL builds an internal URL). +// Export ORDER, function `.length` and the prototype's key order are not oracle-able — +// they differ from node's before and after the split — so the split was checked +// against the previous binary instead (the PR records the diff). +'use strict'; + +const url = require('url'); + +for (const id of ['internal/url']) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +function frame(label, file, f) { + let stack = null; + try { + f(); + } catch (e) { + stack = String(e && e.stack); + } + if (stack === null) return console.log(label + ': NO THROW'); + const where = stack.includes('node:' + file + ':') + ? 'ok' + : 'got ' + (stack.match(/node:(?!internal\/errors)[\w/]+/) || ['no node: frame'])[0]; + console.log(label + ': ' + file + ' ' + where); +} + +// The WHATWG half: node:internal/url on both runtimes. +frame('new URL invalid', 'internal/url', () => new URL('nope')); +frame('new URL invalid base', 'internal/url', () => new URL('a', 'nope')); +frame('new URL no args', 'internal/url', () => new URL()); +frame('href setter', 'internal/url', () => { + new URL('http://a/').href = 'nope'; +}); +frame('canParse no args', 'internal/url', () => URL.canParse()); +frame('fileURLToPath type', 'internal/url', () => url.fileURLToPath(123)); +frame('fileURLToPath scheme', 'internal/url', () => url.fileURLToPath('http://a/')); +frame('fileURLToPath encoded slash', 'internal/url', () => url.fileURLToPath('file:///a%2fb')); +frame('fileURLToPath host', 'internal/url', () => url.fileURLToPath('file://remote/a')); +frame('fileURLToPath invalid', 'internal/url', () => url.fileURLToPath('nope')); +frame('urlToHttpOptions null', 'internal/url', () => url.urlToHttpOptions(null)); +frame('searchParams tuple', 'internal/url', () => new URLSearchParams([[1]])); +frame('searchParams append', 'internal/url', () => new URLSearchParams().append()); +frame('searchParams forEach', 'internal/url', () => new URLSearchParams().forEach(5)); +frame('searchParams receiver', 'internal/url', () => URLSearchParams.prototype.get.call({}, 'a')); +// What stayed in the public entry: node:url on both runtimes. +frame('pathToFileURL type', 'url', () => url.pathToFileURL(1)); +frame('format type', 'url', () => url.format(5)); + +// --- the assembled exports: shapes and identities ---------------------------------- +console.log('same module', require('node:url') === url); +for (const k of [ + 'URL', + 'URLSearchParams', + 'fileURLToPath', + 'pathToFileURL', + 'urlToHttpOptions', + 'format', + 'domainToASCII', + 'domainToUnicode', +]) { + const d = Object.getOwnPropertyDescriptor(url, k); + console.log( + 'export', + k, + typeof url[k], + 'value' in d ? 'data' : 'accessor', + d.writable, + d.enumerable, + d.configurable, + ); +} +console.log( + 'identities', + globalThis.URL === url.URL, + globalThis.URLSearchParams === url.URLSearchParams, + new URL('http://a/') instanceof url.URL, + new url.URL('http://a/').searchParams instanceof URLSearchParams, + url.pathToFileURL('/tmp') instanceof URL, +); +console.log( + 'object-url statics', + typeof URL.createObjectURL, + URL.createObjectURL.name, + typeof URL.revokeObjectURL, + URL.revokeObjectURL.name, +); +console.log('names', url.URL.name, url.URLSearchParams.name, url.fileURLToPath.name); + +// --- behavior across the file seam --------------------------------------------------- +const u = new URL('http://user:pw@xn--nxasmq6b.com:8080/a b?x=1&y=2#f'); +console.log('href', u.href); +console.log('format', url.format(u)); +console.log('format opts', url.format(u, { unicode: true, auth: false, search: false })); +console.log('format fragment', url.format(u, { fragment: false })); +console.log( + 'domain', + url.domainToUnicode('xn--nxasmq6b.com'), + url.domainToASCII('βόλος.com'), + JSON.stringify(url.domainToASCII('a b')), +); +console.log('fileURLToPath', url.fileURLToPath('file:///tmp/a%20b')); +console.log('pathToFileURL', url.pathToFileURL('/tmp/a b#?%').href); +console.log('urlToHttpOptions', JSON.stringify(url.urlToHttpOptions(u))); +u.searchParams.append('z', '3'); +console.log('searchParams', u.search, [...u.searchParams.keys()].join()); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 8534b81..ce1dffa 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -257,6 +257,12 @@ "accessor": 0, "global": 14 }, + "internal/url.js": { + "method": 36, + "invoke": 2, + "accessor": 0, + "global": 17 + }, "internal/util.js": { "method": 1, "invoke": 3, @@ -480,10 +486,10 @@ "global": 2 }, "node/url.js": { - "method": 46, - "invoke": 2, + "method": 10, + "invoke": 0, "accessor": 0, - "global": 18 + "global": 1 }, "node/util.js": { "method": 6, From 33c2d952c334ec9feee8afabf145035a5d2e6564 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:03:08 +0200 Subject: [PATCH 41/61] refactor(url): move punycode and IDNA out of internal/url internal/url.js 2196 -> 1993; internal/punycode.js (148) and internal/idna.js (128) mirror node's lib/punycode.js + lib/internal/idna.js seam. Moved verbatim; the only throw (punycodeDecode) is caught by its caller, so no frame moves. Eager through url. Case 88 pins IDNA behavior across the seam. Co-Authored-By: Claude Opus 5.5 --- agents/prompts/pr-gate-reference/gates.md | 4 +- cmd/lava/lazy_builtins_test.odin | 2 +- docs/ARCHITECTURE.md | 12 +- pkg/runtime/js/internal/idna.js | 128 ++++++++++ pkg/runtime/js/internal/punycode.js | 148 +++++++++++ pkg/runtime/js/internal/url.js | 234 ++---------------- pkg/runtime/js/node/punycode.js | 3 +- runtime/gates/route-gates.mjs | 2 +- runtime/gates/route-gates.test.mjs | 9 +- tests/mutation-manifest.json | 18 ++ .../node-compat/cases/88-url-idna-modules.js | 92 +++++++ tests/node-compat/pollution-baseline.json | 16 +- 12 files changed, 439 insertions(+), 229 deletions(-) create mode 100644 pkg/runtime/js/internal/idna.js create mode 100644 pkg/runtime/js/internal/punycode.js create mode 100644 tests/node-compat/cases/88-url-idna-modules.js diff --git a/agents/prompts/pr-gate-reference/gates.md b/agents/prompts/pr-gate-reference/gates.md index c968a9a..e1551cd 100644 --- a/agents/prompts/pr-gate-reference/gates.md +++ b/agents/prompts/pr-gate-reference/gates.md @@ -69,8 +69,8 @@ catches what a YAML parse cannot: a context used where it is not available, a ba | `require.odin`, `module_resolution.odin`, `js/bootstrap/{loader,esm}.js` | `make test-compat-lava-strict`; for `loader.js` ALSO `make test-fs-lava`, `make test-http-smoke`, `make test-net-smoke`, `make test-https-smoke`, `make test-stdio` — since #333 it hands `require.pristineBuffer` to every lazy module, so a wrong snapshot breaks framing in suites `test-compat-lava-strict` never runs | | `crypto.odin`, `js/node/crypto.js`, `js/internal/crypto/*.js` | `make test-odin`, `make test-compat-lava`, `make api-surface` | | `pkg/runtime/js/**/*.js` (any under the scan root — `bootstrap/`, `node/`, `internal/`; use `**/*` not bare `**.js`) | `make check-js`, `make check-primordials`, `make test-compat-lava` | -| `js/{node,internal}/url.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | -| `js/internal/{encoding,buffer,url}.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | +| `js/{node,internal}/url.js`, `js/internal/{idna,punycode}.js` | `make bench-gate` — `bench/micro/url.js` exists since 2026-07-30; before it, a +6% to +20% `new URL` regression passed every gate and was found by hand in review | +| `js/internal/{encoding,buffer,url,idna,punycode}.js`, `js/node/{url,buffer}.js`, `pkg/runtime/buffer*.odin` | `make test-property` — differential property tests (fast-check generates the corpus; hand-picked oracle cases missed edge cases here twice, and the batched suite found a utf-16le divergence at 5000 inputs). `PROPERTY_RUNS=N` for a deeper local sweep | | `scripts/**` | `make test-scripts` — node:test over the build tooling | | any file named in `tests/mutation-manifest.json` as a `source` | `make test-mutation CHANGED=1` — re-applies the recorded breaks this diff can affect (changed `source`, changed gate file, new or edited entry; all of them when the runner, `Makefile` or `build.sh` changed) and requires the named test to go red. The full `make test-mutation` is the release-time form: `CHANGED=1` cannot see a shared helper a gate imports. Changing one of these files without running it means the recorded mutation may no longer describe the code, which the runner reports as STALE rather than skipping | | `pkg/runtime/globals.odin`, `runtime.odin`, `errors.odin` | `make test-odin`, `make test-compat-lava` | diff --git a/cmd/lava/lazy_builtins_test.odin b/cmd/lava/lazy_builtins_test.odin index 3562793..f038cde 100644 --- a/cmd/lava/lazy_builtins_test.odin +++ b/cmd/lava/lazy_builtins_test.odin @@ -104,7 +104,7 @@ builtin_factories_compile_on_first_require :: proc(t: ^testing.T) { // The static eager set, sorted. Must equal eagerModules() — asserted by // scripts/check-js-graph.test.mjs, which parses this literal. @(private = "file") -STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/buffer", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/primordials", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/url", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} +STATIC_EAGER :: [?]string{"internal/abort", "internal/blob", "internal/buffer", "internal/crypto/hash", "internal/crypto/hkdf", "internal/crypto/pbkdf2", "internal/crypto/random", "internal/crypto/scrypt", "internal/crypto/util", "internal/encoding", "internal/errors", "internal/fetch", "internal/fetch/body", "internal/fetch/headers", "internal/fetch/request", "internal/fetch/response", "internal/file", "internal/idna", "internal/primordials", "internal/punycode", "internal/stdio", "internal/streams/add-abort-signal", "internal/streams/destroy", "internal/streams/duplex", "internal/streams/end-of-stream", "internal/streams/legacy", "internal/streams/passthrough", "internal/streams/pipeline", "internal/streams/readable", "internal/streams/transform", "internal/streams/utils", "internal/streams/writable", "internal/structured_clone", "internal/url", "internal/webstreams/queuingstrategies", "internal/webstreams/readablestream", "internal/webstreams/transformstream", "internal/webstreams/util", "internal/webstreams/writablestream", "node/buffer", "node/crypto", "node/events", "node/stream", "node/stream/web", "node/string_decoder", "node/url"} @(private = "file") run_bool :: proc(ctx: jsc.JSContextRef, src: string) -> bool { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index ef41b69..f09b9f8 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -182,8 +182,8 @@ at its own top level, so the late edge is what keeps the graph acyclic), as node `internal/blob` is eager yet takes it too, at call time: `buffer` requires it from its own factory, before the table exists, and a top-level `require('buffer')` back would be a cycle. -The JS layer is substantial and high quality: URL (~2350 across `node/url.js` and -`internal/url.js`, WHATWG URL), +The JS layer is substantial and high quality: URL (~2400 across `node/url.js`, +`internal/url.js` and `internal/{idna,punycode}.js`, WHATWG URL), Web Streams (~2100 across `internal/webstreams/`), buffer (~1530 across `node/buffer.js` and `internal/buffer.js`; `Blob`, `File` and the `blob:` URL registry live in `internal/blob.js` and `internal/file.js`, as in node's `lib/internal`), fetch @@ -246,7 +246,13 @@ node's `lib/url.js` defines and frames in `node:url`, installs the `URL`/`URLSea globals (after `buffer`, keeping its object-URL statics) and assembles the exports in their old order, borrowing the record slot, the path serializer and the Punycode decoder for `format`. Url calls no native, so there is no alias; `internal/url` is eager through -`req('url')`, which keeps its module-eval captures pristine. Errors use Node-shaped +`req('url')`, which keeps its module-eval captures pristine. The IDNA step takes node's +`lib/internal/idna` seam: `internal/idna` (separator fold, UTS-46 approximation) and the +`internal/punycode` codec under it return failure values instead of throwing, so the host +parser and every throw it feeds stay in `internal/url` and no frame moves; both are eager +through `internal/url`'s top-level require, and `internal/url` captures their functions at +module-eval, so the parse path gains no call layer. `internal/punycode` is not node's public +`punycode` (`node/punycode.js`, the userland punycode.js surface). Errors use Node-shaped coded errors (`ERR_INVALID_ARG_TYPE`, `ERR_OUT_OF_RANGE`, …), built in one place: `js/internal/errors.js` (eager, hardened) holds node's `determineSpecificType`, `ERR_INVALID_ARG_TYPE`/`_VALUE`, `ERR_OUT_OF_RANGE`, diff --git a/pkg/runtime/js/internal/idna.js b/pkg/runtime/js/internal/idna.js new file mode 100644 index 0000000..ea65b95 --- /dev/null +++ b/pkg/runtime/js/internal/idna.js @@ -0,0 +1,128 @@ +// internal/idna — the URL Standard's domain-to-ASCII / domain-to-Unicode step, +// laid out like node's lib/internal/idna.js: the IDNA label-separator fold, the +// UTS-46 mapping approximation, and the `xn--` passes over internal/punycode. +// internal/url's host parser calls domainToASCII; format's `unicode` option and the +// public domainToUnicode reach domainToUnicode through internal/url. Not publicly +// requireable. +// +// Nothing here throws: domainToASCII returns FAILURE (exported, so internal/url's +// `=== FAILURE` checks compare against the same object), and domainToUnicode keeps +// a label it cannot decode. Every user-visible throw for a bad host stays in +// internal/url, so this file owns no frame node puts elsewhere. +// +// Deliberate deviation from Node, documented in reference/node-compatibility.md: +// IDNA/UTS-46 host processing is approximated (NFC + lowercase + Punycode) rather +// than the full ICU mapping; common Unicode domains round-trip, but exotic +// mapping/validation edge cases may differ. +(function (require, module, _exports) { + 'use strict'; + + // Eager through internal/url (the loader instantiates url before user code), so + // these module-eval captures are pristine — see internal/url's header. + var P = require('internal/primordials'); + var ArrayPrototypePush = P.ArrayPrototypePush; + var ArrayPrototypeJoin = P.ArrayPrototypeJoin; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeSlice = P.StringPrototypeSlice; + var StringPrototypeSplit = P.StringPrototypeSplit; + var StringPrototypeNormalize = P.StringPrototypeNormalize; + var StringPrototypeToLowerCase = P.StringPrototypeToLowerCase; + var StringFromCharCode = String.fromCharCode; + var punycode = require('internal/punycode'); + var punycodeEncode = punycode.encode; + var punycodeDecode = punycode.decode; + + // A unique sentinel distinct from any valid parser result; using a symbol means + // it can never collide with a legitimately-produced host string or number. + var FAILURE = { failure: true }; + + // U+3002 IDEOGRAPHIC FULL STOP, U+FF0E FULLWIDTH FULL STOP, U+FF61 HALFWIDTH + // IDEOGRAPHIC FULL STOP — the IDNA label separators that fold to '.'. Getting + // this wrong is host confusion, not a cosmetic difference. + function isIdnaSeparator(c) { + return c === 0x3002 || c === 0xff0e || c === 0xff61; + } + + // The fold, not a strip: these three map TO '.', they are not removed. + function foldIdnaSeparators(s) { + var n = s.length; + var i = 0; + for (; i < n; i++) { + if (isIdnaSeparator(StringPrototypeCharCodeAt(s, i))) break; + } + if (i === n) return s; + var out = StringPrototypeSlice(s, 0, i); + for (; i < n; i++) { + var c = StringPrototypeCharCodeAt(s, i); + out += isIdnaSeparator(c) ? '.' : StringFromCharCode(c); + } + return out; + } + + function hasNonASCII(str) { + for (var i = 0; i < str.length; i++) { + if (StringPrototypeCharCodeAt(str, i) > 0x7f) return true; + } + return false; + } + + // Simplified IDNA ToASCII. Approximates UTS-46 by: normalizing the three IDNA + // label separators to '.', applying NFKC + lowercasing each Unicode label (so + // fullwidth/compatibility characters fold the way ICU maps them), and emitting + // the Punycode "xn--…" form. ASCII labels are just lowercased. Not the full ICU + // mapping table, but round-trips the overwhelming majority of real domains. + function domainToASCII(domain) { + if (domain === '') return ''; + // IDEOGRAPHIC FULL STOP, FULLWIDTH FULL STOP, HALFWIDTH IDEOGRAPHIC FULL STOP. + // Fold the three IDNA separators to '.', without a RegExp — see stripChars. + domain = foldIdnaSeparators(domain); + var labels = StringPrototypeSplit(domain, '.'); + var out = []; + for (var i = 0; i < labels.length; i++) { + var label = labels[i]; + if (hasNonASCII(label)) { + var mapped = ( + label.normalize ? StringPrototypeNormalize(label, 'NFKC') : label + ).toLowerCase(); + if (!hasNonASCII(mapped)) { + // The mapping folded the label to pure ASCII (e.g. fullwidth digits). + ArrayPrototypePush(out, mapped); + continue; + } + try { + ArrayPrototypePush(out, 'xn--' + punycodeEncode(mapped)); + } catch { + return FAILURE; + } + } else { + ArrayPrototypePush(out, StringPrototypeToLowerCase(label)); + } + } + return ArrayPrototypeJoin(out, '.'); + } + + function domainToUnicode(domain) { + if (domain === '') return ''; + var labels = StringPrototypeSplit(domain, '.'); + var out = []; + for (var i = 0; i < labels.length; i++) { + var label = labels[i]; + if (label.slice(0, 4).toLowerCase() === 'xn--') { + try { + ArrayPrototypePush(out, punycodeDecode(label.slice(4))); + } catch { + ArrayPrototypePush(out, label); + } + } else { + ArrayPrototypePush(out, label); + } + } + return ArrayPrototypeJoin(out, '.'); + } + + module.exports = { + FAILURE: FAILURE, + domainToASCII: domainToASCII, + domainToUnicode: domainToUnicode, + }; +}); diff --git a/pkg/runtime/js/internal/punycode.js b/pkg/runtime/js/internal/punycode.js new file mode 100644 index 0000000..5d22579 --- /dev/null +++ b/pkg/runtime/js/internal/punycode.js @@ -0,0 +1,148 @@ +// internal/punycode — the RFC 3492 Punycode codec behind the URL Standard's IDNA +// step (internal/idna). Not node's public `punycode` module: that one is +// js/node/punycode.js, the userland punycode.js surface, which throws node's +// error strings and deliberately reads Math.floor/String.fromCharCode live (see its +// header). This one stays the minimal pair the host parser needs, captured at +// module-eval like the rest of the URL path. Not publicly requireable. +// +// encode throws nothing; decode throws a RangeError on malformed input, which its +// one caller (internal/idna's domainToUnicode) catches and turns into "keep the +// label as is", so no throw from here ever reaches user code. +(function (require, module, _exports) { + 'use strict'; + + // Eager through internal/url (the loader instantiates url before user code), so + // these module-eval captures are pristine — see internal/url's header. + var P = require('internal/primordials'); + var ArrayPrototypePush = P.ArrayPrototypePush; + var ArrayPrototypeSplice = P.ArrayPrototypeSplice; + var StringPrototypeCharCodeAt = P.StringPrototypeCharCodeAt; + var StringPrototypeCodePointAt = P.StringPrototypeCodePointAt; + var StringFromCharCode = String.fromCharCode; + var StringFromCodePoint = String.fromCodePoint; + var MathFloor = Math.floor; + + var PUNY_BASE = 36; + var PUNY_TMIN = 1; + var PUNY_TMAX = 26; + var PUNY_SKEW = 38; + var PUNY_DAMP = 700; + var PUNY_INITIAL_BIAS = 72; + var PUNY_INITIAL_N = 128; + + function punyAdapt(delta, numPoints, firstTime) { + delta = firstTime ? MathFloor(delta / PUNY_DAMP) : delta >> 1; + delta += MathFloor(delta / numPoints); + var k = 0; + while (delta > ((PUNY_BASE - PUNY_TMIN) * PUNY_TMAX) >> 1) { + delta = MathFloor(delta / (PUNY_BASE - PUNY_TMIN)); + k += PUNY_BASE; + } + return k + MathFloor(((PUNY_BASE - PUNY_TMIN + 1) * delta) / (delta + PUNY_SKEW)); + } + + function punyEncodeDigit(d) { + // 0..25 -> a..z, 26..35 -> 0..9 + return d + 22 + (d < 26 ? 75 : 0); + } + function punyDecodeDigit(cp) { + if (cp - 48 < 10) return cp - 22; // 0..9 + if (cp - 65 < 26) return cp - 65; // A..Z + if (cp - 97 < 26) return cp - 97; // a..z + return PUNY_BASE; + } + + function punycodeEncode(input) { + var output = []; + var codePoints = []; + for (var s = 0; s < input.length; s++) { + var cp = StringPrototypeCodePointAt(input, s); + if (cp > 0xffff) s++; + ArrayPrototypePush(codePoints, cp); + } + var n = PUNY_INITIAL_N; + var delta = 0; + var bias = PUNY_INITIAL_BIAS; + var basicLength = 0; + var i; + for (i = 0; i < codePoints.length; i++) { + if (codePoints[i] < 0x80) { + ArrayPrototypePush(output, codePoints[i]); + basicLength++; + } + } + var handled = basicLength; + if (basicLength > 0) ArrayPrototypePush(output, 0x2d); // '-' + while (handled < codePoints.length) { + var m = 0x7fffffff; + for (i = 0; i < codePoints.length; i++) { + if (codePoints[i] >= n && codePoints[i] < m) m = codePoints[i]; + } + delta += (m - n) * (handled + 1); + n = m; + for (i = 0; i < codePoints.length; i++) { + var c = codePoints[i]; + if (c < n) delta++; + if (c === n) { + var q = delta; + for (var k = PUNY_BASE; ; k += PUNY_BASE) { + var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; + if (q < t) break; + ArrayPrototypePush(output, punyEncodeDigit(t + ((q - t) % (PUNY_BASE - t)))); + q = MathFloor((q - t) / (PUNY_BASE - t)); + } + ArrayPrototypePush(output, punyEncodeDigit(q)); + bias = punyAdapt(delta, handled + 1, handled === basicLength); + delta = 0; + handled++; + } + } + delta++; + n++; + } + var str = ''; + for (i = 0; i < output.length; i++) str += StringFromCharCode(output[i]); + return str; + } + + function punycodeDecode(input) { + var output = []; + var n = PUNY_INITIAL_N; + var bias = PUNY_INITIAL_BIAS; + var i = 0; + var basic = input.lastIndexOf('-'); + if (basic < 0) basic = 0; + for (var j = 0; j < basic; j++) { + if (StringPrototypeCharCodeAt(input, j) >= 0x80) throw new RangeError('Invalid input'); + ArrayPrototypePush(output, StringPrototypeCharCodeAt(input, j)); + } + var index = basic > 0 ? basic + 1 : 0; + while (index < input.length) { + var oldi = i; + var w = 1; + for (var k = PUNY_BASE; ; k += PUNY_BASE) { + if (index >= input.length) throw new RangeError('Invalid input'); + var digit = punyDecodeDigit(StringPrototypeCharCodeAt(input, index++)); + if (digit >= PUNY_BASE) throw new RangeError('Invalid input'); + i += digit * w; + var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; + if (digit < t) break; + w *= PUNY_BASE - t; + } + var outLen = output.length + 1; + bias = punyAdapt(i - oldi, outLen, oldi === 0); + n += MathFloor(i / outLen); + i %= outLen; + ArrayPrototypeSplice(output, i, 0, n); + i++; + } + var str = ''; + for (var o = 0; o < output.length; o++) str += StringFromCodePoint(output[o]); + return str; + } + + module.exports = { + encode: punycodeEncode, + decode: punycodeDecode, + }; +}); diff --git a/pkg/runtime/js/internal/url.js b/pkg/runtime/js/internal/url.js index fc0473c..a23e750 100644 --- a/pkg/runtime/js/internal/url.js +++ b/pkg/runtime/js/internal/url.js @@ -1,7 +1,10 @@ // internal/url — node:url's WHATWG half, laid out like node's lib/internal/url.js: -// the URL Standard parser (percent-encoding, host/IPv4/IPv6/IDNA, the state -// machine), `URL`, `URLSearchParams`, `fileURLToPath`, `urlToHttpOptions` and the -// validating `domainToASCII`/`domainToUnicode`. Implemented in JS (as Node does for +// the URL Standard parser (percent-encoding, host/IPv4/IPv6, the state machine), +// `URL`, `URLSearchParams`, `fileURLToPath`, `urlToHttpOptions` and the validating +// `domainToASCII`/`domainToUnicode`. The IDNA mapping and its Punycode codec are +// internal/idna and internal/punycode (node's lib/internal/idna.js); they throw +// nothing, so the host parser that calls them stays here with every throw it +// feeds, and no frame leaves node:internal/url. Implemented in JS (as Node does for // the parsing-heavy modules like node:path) so the fiddly state-machine semantics // match the URL Standard and Node byte-for-byte. JavaScriptCore's bare engine ships // no URL/URLSearchParams (those are WebCore APIs), so the whole surface lives here. @@ -12,10 +15,8 @@ // `node:internal/url`, as node's does (pinned by // tests/node-compat/cases/87-url-internal-modules.js). Not publicly requireable. // -// Deliberate deviation from Node, documented in reference/node-compatibility.md: -// IDNA/UTS-46 host processing is approximated (NFC + lowercase + Punycode) rather -// than the full ICU mapping; common Unicode domains round-trip, but exotic -// mapping/validation edge cases may differ. +// The documented IDNA deviation (UTS-46 approximated, not the full ICU mapping) is +// stated where the mapping lives, in internal/idna.js. (function (require, module, _exports) { 'use strict'; @@ -110,9 +111,15 @@ ); } - // A unique sentinel distinct from any valid parser result; using a symbol means - // it can never collide with a legitimately-produced host string or number. - var FAILURE = { failure: true }; + // The IDNA half (separator fold, UTS-46 approximation, Punycode) lives in + // internal/idna, as node's does in lib/internal/idna.js. Captured at module-eval + // so parseHost calls straight into it, with no wrapper on the URL parse path; + // FAILURE is the sentinel idna returns, so every `=== FAILURE` below stays one + // identity compare against the same object. + var idna = require('internal/idna'); + var FAILURE = idna.FAILURE; + var domainToASCII = idna.domainToASCII; + var domainToUnicode = idna.domainToUnicode; // ------------------------------------------------------------------ // // Percent-encoding // @@ -151,29 +158,6 @@ return c === 0x09 || c === 0x0a || c === 0x0d; } - // U+3002 IDEOGRAPHIC FULL STOP, U+FF0E FULLWIDTH FULL STOP, U+FF61 HALFWIDTH - // IDEOGRAPHIC FULL STOP — the IDNA label separators that fold to '.'. Getting - // this wrong is host confusion, not a cosmetic difference. - function isIdnaSeparator(c) { - return c === 0x3002 || c === 0xff0e || c === 0xff61; - } - - // The fold, not a strip: these three map TO '.', they are not removed. - function foldIdnaSeparators(s) { - var n = s.length; - var i = 0; - for (; i < n; i++) { - if (isIdnaSeparator(StringPrototypeCharCodeAt(s, i))) break; - } - if (i === n) return s; - var out = StringPrototypeSlice(s, 0, i); - for (; i < n; i++) { - var c = StringPrototypeCharCodeAt(s, i); - out += isIdnaSeparator(c) ? '.' : StringFromCharCode(c); - } - return out; - } - // The WHATWG percent-encode sets, each a predicate over a code point. Every set // is a superset of the C0-control set (control chars and everything past 0x7E). function inC0ControlSet(cp) { @@ -308,190 +292,6 @@ } } - // ------------------------------------------------------------------ // - // Punycode (RFC 3492) — used by the simplified IDNA host processing. // - // ------------------------------------------------------------------ // - - var PUNY_BASE = 36; - var PUNY_TMIN = 1; - var PUNY_TMAX = 26; - var PUNY_SKEW = 38; - var PUNY_DAMP = 700; - var PUNY_INITIAL_BIAS = 72; - var PUNY_INITIAL_N = 128; - - function punyAdapt(delta, numPoints, firstTime) { - delta = firstTime ? MathFloor(delta / PUNY_DAMP) : delta >> 1; - delta += MathFloor(delta / numPoints); - var k = 0; - while (delta > ((PUNY_BASE - PUNY_TMIN) * PUNY_TMAX) >> 1) { - delta = MathFloor(delta / (PUNY_BASE - PUNY_TMIN)); - k += PUNY_BASE; - } - return k + MathFloor(((PUNY_BASE - PUNY_TMIN + 1) * delta) / (delta + PUNY_SKEW)); - } - - function punyEncodeDigit(d) { - // 0..25 -> a..z, 26..35 -> 0..9 - return d + 22 + (d < 26 ? 75 : 0); - } - function punyDecodeDigit(cp) { - if (cp - 48 < 10) return cp - 22; // 0..9 - if (cp - 65 < 26) return cp - 65; // A..Z - if (cp - 97 < 26) return cp - 97; // a..z - return PUNY_BASE; - } - - function punycodeEncode(input) { - var output = []; - var codePoints = []; - for (var s = 0; s < input.length; s++) { - var cp = StringPrototypeCodePointAt(input, s); - if (cp > 0xffff) s++; - ArrayPrototypePush(codePoints, cp); - } - var n = PUNY_INITIAL_N; - var delta = 0; - var bias = PUNY_INITIAL_BIAS; - var basicLength = 0; - var i; - for (i = 0; i < codePoints.length; i++) { - if (codePoints[i] < 0x80) { - ArrayPrototypePush(output, codePoints[i]); - basicLength++; - } - } - var handled = basicLength; - if (basicLength > 0) ArrayPrototypePush(output, 0x2d); // '-' - while (handled < codePoints.length) { - var m = 0x7fffffff; - for (i = 0; i < codePoints.length; i++) { - if (codePoints[i] >= n && codePoints[i] < m) m = codePoints[i]; - } - delta += (m - n) * (handled + 1); - n = m; - for (i = 0; i < codePoints.length; i++) { - var c = codePoints[i]; - if (c < n) delta++; - if (c === n) { - var q = delta; - for (var k = PUNY_BASE; ; k += PUNY_BASE) { - var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; - if (q < t) break; - ArrayPrototypePush(output, punyEncodeDigit(t + ((q - t) % (PUNY_BASE - t)))); - q = MathFloor((q - t) / (PUNY_BASE - t)); - } - ArrayPrototypePush(output, punyEncodeDigit(q)); - bias = punyAdapt(delta, handled + 1, handled === basicLength); - delta = 0; - handled++; - } - } - delta++; - n++; - } - var str = ''; - for (i = 0; i < output.length; i++) str += StringFromCharCode(output[i]); - return str; - } - - function punycodeDecode(input) { - var output = []; - var n = PUNY_INITIAL_N; - var bias = PUNY_INITIAL_BIAS; - var i = 0; - var basic = input.lastIndexOf('-'); - if (basic < 0) basic = 0; - for (var j = 0; j < basic; j++) { - if (StringPrototypeCharCodeAt(input, j) >= 0x80) throw new RangeError('Invalid input'); - ArrayPrototypePush(output, StringPrototypeCharCodeAt(input, j)); - } - var index = basic > 0 ? basic + 1 : 0; - while (index < input.length) { - var oldi = i; - var w = 1; - for (var k = PUNY_BASE; ; k += PUNY_BASE) { - if (index >= input.length) throw new RangeError('Invalid input'); - var digit = punyDecodeDigit(StringPrototypeCharCodeAt(input, index++)); - if (digit >= PUNY_BASE) throw new RangeError('Invalid input'); - i += digit * w; - var t = k <= bias ? PUNY_TMIN : k >= bias + PUNY_TMAX ? PUNY_TMAX : k - bias; - if (digit < t) break; - w *= PUNY_BASE - t; - } - var outLen = output.length + 1; - bias = punyAdapt(i - oldi, outLen, oldi === 0); - n += MathFloor(i / outLen); - i %= outLen; - ArrayPrototypeSplice(output, i, 0, n); - i++; - } - var str = ''; - for (var o = 0; o < output.length; o++) str += StringFromCodePoint(output[o]); - return str; - } - - function hasNonASCII(str) { - for (var i = 0; i < str.length; i++) { - if (StringPrototypeCharCodeAt(str, i) > 0x7f) return true; - } - return false; - } - - // Simplified IDNA ToASCII. Approximates UTS-46 by: normalizing the three IDNA - // label separators to '.', applying NFKC + lowercasing each Unicode label (so - // fullwidth/compatibility characters fold the way ICU maps them), and emitting - // the Punycode "xn--…" form. ASCII labels are just lowercased. Not the full ICU - // mapping table, but round-trips the overwhelming majority of real domains. - function domainToASCII(domain) { - if (domain === '') return ''; - // IDEOGRAPHIC FULL STOP, FULLWIDTH FULL STOP, HALFWIDTH IDEOGRAPHIC FULL STOP. - // Fold the three IDNA separators to '.', without a RegExp — see stripChars. - domain = foldIdnaSeparators(domain); - var labels = StringPrototypeSplit(domain, '.'); - var out = []; - for (var i = 0; i < labels.length; i++) { - var label = labels[i]; - if (hasNonASCII(label)) { - var mapped = ( - label.normalize ? StringPrototypeNormalize(label, 'NFKC') : label - ).toLowerCase(); - if (!hasNonASCII(mapped)) { - // The mapping folded the label to pure ASCII (e.g. fullwidth digits). - ArrayPrototypePush(out, mapped); - continue; - } - try { - ArrayPrototypePush(out, 'xn--' + punycodeEncode(mapped)); - } catch { - return FAILURE; - } - } else { - ArrayPrototypePush(out, StringPrototypeToLowerCase(label)); - } - } - return ArrayPrototypeJoin(out, '.'); - } - - function domainToUnicode(domain) { - if (domain === '') return ''; - var labels = StringPrototypeSplit(domain, '.'); - var out = []; - for (var i = 0; i < labels.length; i++) { - var label = labels[i]; - if (label.slice(0, 4).toLowerCase() === 'xn--') { - try { - ArrayPrototypePush(out, punycodeDecode(label.slice(4))); - } catch { - ArrayPrototypePush(out, label); - } - } else { - ArrayPrototypePush(out, label); - } - } - return ArrayPrototypeJoin(out, '.'); - } - // ------------------------------------------------------------------ // // Host parsing // // ------------------------------------------------------------------ // diff --git a/pkg/runtime/js/node/punycode.js b/pkg/runtime/js/node/punycode.js index ac86f99..db578f5 100644 --- a/pkg/runtime/js/node/punycode.js +++ b/pkg/runtime/js/node/punycode.js @@ -28,7 +28,8 @@ // `RegExp.prototype.exec` never returns, so `punycode.toASCII`/`toUnicode` on any // caller-supplied hostname wedged the process. node hangs on the same input, so this is // a deviation in Lava's favour; pinned Lava-only by cmd/lava/regexp_pollution_test.odin. - // (`new URL()` is unaffected — url.js carries its own IDNA path and does not call here.) + // (`new URL()` is unaffected — internal/idna carries its own IDNA path and does not + // call here.) function isRFC3490Separator(c) { return c === 0x2e || c === 0x3002 || c === 0xff0e || c === 0xff61; } diff --git a/runtime/gates/route-gates.mjs b/runtime/gates/route-gates.mjs index d5d6c1f..0275cea 100644 --- a/runtime/gates/route-gates.mjs +++ b/runtime/gates/route-gates.mjs @@ -216,7 +216,7 @@ const ROUTES = [ targets: ['make bench-gate', 'make test-property'], }, { - re: globToRe('**/js/internal/url.js'), + re: globToRe('**/js/internal/{url,idna,punycode}.js'), targets: ['make bench-gate', 'make test-property'], }, { diff --git a/runtime/gates/route-gates.test.mjs b/runtime/gates/route-gates.test.mjs index 9abb627..31f89c6 100644 --- a/runtime/gates/route-gates.test.mjs +++ b/runtime/gates/route-gates.test.mjs @@ -55,8 +55,13 @@ test('buffer internals route the property tests, entry and split file alike', () } }); -test('url internals route the URL bench and property tests, entry and split file alike', () => { - for (const p of ['pkg/runtime/js/node/url.js', 'pkg/runtime/js/internal/url.js']) { +test('url internals route the URL bench and property tests, entry and split files alike', () => { + for (const p of [ + 'pkg/runtime/js/node/url.js', + 'pkg/runtime/js/internal/url.js', + 'pkg/runtime/js/internal/idna.js', + 'pkg/runtime/js/internal/punycode.js', + ]) { const t = routePaths([p]).targets; for (const g of ['make bench-gate', 'make test-property']) assert.ok(t.includes(g), p + ' ' + g); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index cb3c9c2..03f5191 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2589,6 +2589,24 @@ "gate": "compat:tests/node-compat/cases/87-url-internal-modules.js", "expect_detail": "node: format opts http://βόλοσ.com:8080/a%20b#f" }, + { + "name": "88-url-idna-modules dies when internal/idna stops folding the IDNA label separators", + "why": "U+3002/U+FF61 are label separators the URL Standard maps to '.'; without the fold in internal/idna the whole run is one label, Punycode-encoded, a different host from node's — the case must see the moved IDNA code, not just that the file loads.", + "source": "pkg/runtime/js/internal/idna.js", + "find": " domain = foldIdnaSeparators(domain);\n", + "replace": "", + "gate": "compat:tests/node-compat/cases/88-url-idna-modules.js", + "expect_detail": "node: domain \"a。b。c.d\" \"a.b.c.d\" \"a.b.c.d\"" + }, + { + "name": "88-url-idna-modules dies when internal/punycode decodes the wrong code point", + "why": "RFC 3492's decode advances n by floor(i / (len+1)); advancing by i decodes xn--maana-pta to another name. domainToUnicode and format's unicode option reach the codec only through internal/idna, so this proves the case watches the far end of the seam.", + "source": "pkg/runtime/js/internal/punycode.js", + "find": " n += MathFloor(i / outLen);", + "replace": " n += i;", + "gate": "compat:tests/node-compat/cases/88-url-idna-modules.js", + "expect_detail": "node: domain \"mañana.com\" \"xn--maana-pta.com\" \"mañana.com\"" + }, { "name": "86-os-internal-modules dies when internal/os/constants drops a table from os.constants", "why": "os.constants is now assembled in internal/os/constants.js, outside node:os; the case's per-table lines are what show the moved object still carries every table node's does, so losing one in the move must turn it red.", diff --git a/tests/node-compat/cases/88-url-idna-modules.js b/tests/node-compat/cases/88-url-idna-modules.js new file mode 100644 index 0000000..6bf4072 --- /dev/null +++ b/tests/node-compat/cases/88-url-idna-modules.js @@ -0,0 +1,92 @@ +// The URL Standard's IDNA step lives in its own files, laid out like node's +// lib/internal/idna.js: js/internal/idna.js holds the UTS-46 approximation (label +// separator fold, NFKC + lowercase mapping, the `xn--` ToASCII/ToUnicode passes) +// and js/internal/punycode.js the RFC 3492 codec it calls. internal/url keeps the +// host parser and reaches both through the domainToASCII/domainToUnicode bindings +// it captures at module-eval. +// +// The move is invisible by design — every helper on that side returns a failure +// value instead of throwing, so no frame changes file — which is why no line here +// was red against the previous binary. What the case pins is that the seam carries +// the same results: domainToASCII/domainToUnicode and the URL host for inputs that +// exercise each moved piece (separator fold, NFKC folding to ASCII, mixed-case +// `xn--`, astral and RTL labels, the percent-decoded path, a too-long label set, the +// invalid-UTF-8 failure), and format's `unicode` option, which decodes the host +// through the same Punycode decoder. The recorded mutations in +// tests/mutation-manifest.json carry the proof that it watches the moved code. +// +// Invalid-Punycode labels (`xn--a`, `xn--`, `xn--ls8h-`) are left out: Lava's +// decoder accepts labels node keeps verbatim, a pre-existing gap filed separately. +// +// The new ids must stay internal: neither the bare nor the `node:` spelling may +// resolve from user code. Only the error CODE of the bare spelling is printed (the +// messages diverge, #415). +'use strict'; + +const url = require('url'); + +for (const id of ['internal/idna', 'internal/punycode']) { + let code = 'RESOLVED'; + try { + require(id); + } catch (e) { + code = e.code; + } + let threw = false; + try { + require('node:' + id); + } catch { + threw = true; + } + console.log('hidden', id, code, 'node:' + (threw ? 'threw' : 'RESOLVED')); +} + +const domains = [ + 'mañana.com', + '例え.テスト', + 'EXAMPLE.com', + 'a。b。c.d', + 'Bücher.DE', + '☃.net', + '𝓊.com', + 'xn--maana-pta.com', + 'XN--MAANA-PTA.COM', + 'xn--ls8h.la', + 'ü'.repeat(40) + '.de', + 'straße.de', + 'fi.com', + 'ابن.com', + '日本語。JP', + 'München-Ost.de', + 'Dž.com', + 'Ⅻ.com', + '%E2%98%83.com', + '%C3.com', + 'a.b.', + '', +]; + +for (const d of domains) { + console.log( + 'domain', + JSON.stringify(d), + JSON.stringify(url.domainToASCII(d)), + JSON.stringify(url.domainToUnicode(d)), + ); +} + +for (const d of domains) { + let out; + try { + const u = new URL('http://' + d + '/p'); + out = u.host + ' ' + u.href + ' ' + url.format(u, { unicode: true }); + } catch (e) { + out = 'THREW ' + e.name; + } + console.log('url', JSON.stringify(d), out); +} + +// The host setter re-enters the same host parser on an existing record. +const u = new URL('https://example.com/x'); +u.host = 'BÜCHER。de:8443'; +console.log('setter', u.href, url.format(u, { unicode: true })); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index ce1dffa..47a9016 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -149,6 +149,12 @@ "accessor": 0, "global": 14 }, + "internal/idna.js": { + "method": 4, + "invoke": 0, + "accessor": 0, + "global": 0 + }, "internal/mime.js": { "method": 24, "invoke": 0, @@ -179,6 +185,12 @@ "accessor": 0, "global": 1 }, + "internal/punycode.js": { + "method": 1, + "invoke": 0, + "accessor": 0, + "global": 3 + }, "internal/stdio.js": { "method": 0, "invoke": 0, @@ -258,10 +270,10 @@ "global": 14 }, "internal/url.js": { - "method": 36, + "method": 31, "invoke": 2, "accessor": 0, - "global": 17 + "global": 14 }, "internal/util.js": { "method": 1, From e0968efd2daa5d439943da0ea87838ae20288b01 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:08:10 +0200 Subject: [PATCH 42/61] test(primordials): pin the eager/lazy classification on a fixture tree The recorded mutation "the ratchet dies when every module is scanned as eager" has survived on every master push since #430: that PR removed the last counted module-eval capture from a lazy module, so forcing isEager to true changed no count on the real tree and `make check-primordials` stayed green. PRs never saw it because CHANGED=1 skips the entry unless check-primordials.mjs itself changes. check-primordials.mjs now exports scanTree(root) and runs main() only as the CLI. A new check-primordials.test.mjs case scans a fixture with one eager and one lazy module, each capturing `String` at module eval, and asserts only the lazy one scores `global`. The manifest entry now gates on that case. Co-Authored-By: Claude Opus 5.5 --- scripts/check-primordials.mjs | 285 ++++++++++++++++------------- scripts/check-primordials.test.mjs | 50 +++++ tests/mutation-manifest.json | 7 +- 3 files changed, 207 insertions(+), 135 deletions(-) diff --git a/scripts/check-primordials.mjs b/scripts/check-primordials.mjs index 19e4d40..e3bae87 100644 --- a/scripts/check-primordials.mjs +++ b/scripts/check-primordials.mjs @@ -11,157 +11,178 @@ // node scripts/check-primordials.mjs --update --allow-raise import { readFileSync, writeFileSync } from 'node:fs'; -import { relative, sep } from 'node:path'; -import { KINDS, countFile, walkDir, JS_DIR, BASELINE, ROOT } from './lib/primordials-detect.mjs'; +import { join, relative, sep } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { KINDS, countFile, walkDir, BASELINE, ROOT } from './lib/primordials-detect.mjs'; import { eagerModules } from './lib/js-graph.mjs'; import { selfTest } from './lib/primordials-fixtures.mjs'; import { compare, raises, totals } from './lib/primordials-baseline.mjs'; -// --- report ---------------------------------------------------------------- - -// Before anything else: a detector that has gone blind must not report on the -// tree, and must not be allowed to rebaseline. Exits non-zero rather than -// printing a reassuring "OK". -const selfTestFailures = selfTest(); -if (selfTestFailures.length > 0) { - console.error('Pollution ratchet SELF-TEST FAILED — the detector itself is broken:'); - for (const f of selfTestFailures) console.error(` ${f}`); - console.error( - '\nEvery fixture above pins a vector the ratchet is supposed to see, or a\n' + - 'position it must not count. Fix the detector before trusting any number it\n' + - 'prints.', - ); - process.exit(1); -} +// --- scan ------------------------------------------------------------------ -// Module-eval reads are exempt only where they run before user code: every -// bootstrap/ file (Odin evaluates each at context init) and the modules the loader -// instantiates at init, derived from the graph rather than listed (lib/js-graph.mjs). -// A lazy module's capture runs on the user's first require — #333. -const EAGER = new Set(eagerModules(ROOT)); -const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length)); - -const files = walkDir(JS_DIR).sort(); -const counts = {}; -const allHits = {}; -for (const file of files) { - const key = relative(JS_DIR, file).split(sep).join('/'); - const { hits } = countFile(file, { eager: isEager(key) }); - // PER-CLASS counts, not one number. The classes differ by an order of - // magnitude, and a single total would let a file trade a fixed accessor for a - // new global and still pass. - const byKind = {}; - for (const k of KINDS) byKind[k] = 0; - for (const h of hits) byKind[h.kind]++; - counts[key] = byKind; - allHits[key] = hits; -} +// scanTree is exported, and takes the root, so the eager/lazy classification below +// can be driven over a fixture tree (check-primordials.test.mjs). Pinned only by the +// real tree, a mutant that scored every module eager survived as soon as no lazy +// module happened to hold a module-eval capture. +export function scanTree(root) { + // Module-eval reads are exempt only where they run before user code: every + // bootstrap/ file (Odin evaluates each at context init) and the modules the loader + // instantiates at init, derived from the graph rather than listed (lib/js-graph.mjs). + // A lazy module's capture runs on the user's first require — #333. + const EAGER = new Set(eagerModules(root)); + const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length)); -const perKindLine = () => KINDS.map((k) => `${k} ${totals(counts).perKind[k]}`).join(', '); -const grandTotal = () => totals(counts).total; - -// A corrupt baseline is NOT a missing one. Both used to set haveBaseline=false, -// which put `--update` outside the refuse-to-raise guard entirely — so a file with -// git conflict markers in it (a large generated JSON that changes on every -// hardening, so conflicts are likely) turned the tool's own advice, "run -// --update", into an unguarded rebaseline of every floor. -let baseline = {}; -let haveBaseline = true; -let raw = null; -try { - raw = readFileSync(BASELINE, 'utf8'); -} catch { - haveBaseline = false; + const jsDir = join(root, 'pkg', 'runtime', 'js'); + const files = walkDir(jsDir).sort(); + const counts = {}; + const allHits = {}; + for (const file of files) { + const key = relative(jsDir, file).split(sep).join('/'); + const { hits } = countFile(file, { eager: isEager(key) }); + // PER-CLASS counts, not one number. The classes differ by an order of + // magnitude, and a single total would let a file trade a fixed accessor for a + // new global and still pass. + const byKind = {}; + for (const k of KINDS) byKind[k] = 0; + for (const h of hits) byKind[h.kind]++; + counts[key] = byKind; + allHits[key] = hits; + } + return { files, counts, allHits }; } -if (raw !== null) { - try { - baseline = JSON.parse(raw); - } catch (err) { - console.error(`Baseline at ${BASELINE} is not valid JSON: ${err.message}`); + +// Run only as the CLI: the test imports scanTree and must not trigger the report, +// its process.exit calls, or the baseline write behind --update. +if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) main(); + +function main() { + // --- report ---------------------------------------------------------------- + + // Before anything else: a detector that has gone blind must not report on the + // tree, and must not be allowed to rebaseline. Exits non-zero rather than + // printing a reassuring "OK". + const selfTestFailures = selfTest(); + if (selfTestFailures.length > 0) { + console.error('Pollution ratchet SELF-TEST FAILED — the detector itself is broken:'); + for (const f of selfTestFailures) console.error(` ${f}`); console.error( - 'Fix the file — do NOT rebaseline. Rewriting it would raise every floor to\n' + - 'whatever the tree currently is, which is what the ratchet exists to prevent.', + '\nEvery fixture above pins a vector the ratchet is supposed to see, or a\n' + + 'position it must not count. Fix the detector before trusting any number it\n' + + 'prints.', ); process.exit(1); } -} -const update = process.argv.includes('--update'); -if (update) { - // A raise is a policy decision, not a mechanical one: the point of the ratchet - // is that a floor only moves down. Previously `--update` rewrote every entry in - // both directions, so a contributor who hit a failure and reached for - // `UPDATE=1` silently raised the floor for every file — the prose rule in - // CLAUDE.md §5 was the only thing stopping it. - const up = haveBaseline ? raises(counts, baseline) : []; - if (up.length > 0 && !process.argv.includes('--allow-raise')) { - console.error('Refusing to RAISE the baseline. These entries would go up:\n'); - for (const r of up) { - console.error(` ${r.key}: ${r.kind} ${r.base} -> ${r.now} (+${r.now - r.base})`); + const { files, counts, allHits } = scanTree(ROOT); + + const perKindLine = () => KINDS.map((k) => `${k} ${totals(counts).perKind[k]}`).join(', '); + const grandTotal = () => totals(counts).total; + + // A corrupt baseline is NOT a missing one. Both used to set haveBaseline=false, + // which put `--update` outside the refuse-to-raise guard entirely — so a file with + // git conflict markers in it (a large generated JSON that changes on every + // hardening, so conflicts are likely) turned the tool's own advice, "run + // --update", into an unguarded rebaseline of every floor. + let baseline = {}; + let haveBaseline = true; + let raw = null; + try { + raw = readFileSync(BASELINE, 'utf8'); + } catch { + haveBaseline = false; + } + if (raw !== null) { + try { + baseline = JSON.parse(raw); + } catch (err) { + console.error(`Baseline at ${BASELINE} is not valid JSON: ${err.message}`); + console.error( + 'Fix the file — do NOT rebaseline. Rewriting it would raise every floor to\n' + + 'whatever the tree currently is, which is what the ratchet exists to prevent.', + ); + process.exit(1); } - console.error( - '\nThe ratchet only moves down. Harden the sites, add `// primordials-ok` where\n' + - 'the receiver genuinely is not a built-in, or pass --allow-raise if you are\n' + - 'deliberately recording new ground (a newly scanned file, or a new class).', + } + + const update = process.argv.includes('--update'); + if (update) { + // A raise is a policy decision, not a mechanical one: the point of the ratchet + // is that a floor only moves down. Previously `--update` rewrote every entry in + // both directions, so a contributor who hit a failure and reached for + // `UPDATE=1` silently raised the floor for every file — the prose rule in + // CLAUDE.md §5 was the only thing stopping it. + const up = haveBaseline ? raises(counts, baseline) : []; + if (up.length > 0 && !process.argv.includes('--allow-raise')) { + console.error('Refusing to RAISE the baseline. These entries would go up:\n'); + for (const r of up) { + console.error(` ${r.key}: ${r.kind} ${r.base} -> ${r.now} (+${r.now - r.base})`); + } + console.error( + '\nThe ratchet only moves down. Harden the sites, add `// primordials-ok` where\n' + + 'the receiver genuinely is not a built-in, or pass --allow-raise if you are\n' + + 'deliberately recording new ground (a newly scanned file, or a new class).', + ); + process.exit(1); + } + writeFileSync(BASELINE, JSON.stringify(counts, null, 2) + '\n'); + console.log( + `Wrote ${BASELINE} — ${grandTotal()} sites across ${files.length} files (${perKindLine()}).`, ); + process.exit(0); + } + + if (!haveBaseline) { + console.error(`No baseline at ${BASELINE}. Run: node scripts/check-primordials.mjs --update`); process.exit(1); } - writeFileSync(BASELINE, JSON.stringify(counts, null, 2) + '\n'); - console.log( - `Wrote ${BASELINE} — ${grandTotal()} sites across ${files.length} files (${perKindLine()}).`, - ); - process.exit(0); -} -if (!haveBaseline) { - console.error(`No baseline at ${BASELINE}. Run: node scripts/check-primordials.mjs --update`); - process.exit(1); -} + const { failures, improvements, stale } = compare(counts, baseline); + let failed = failures.length > 0; + const improved = improvements.length > 0; -const { failures, improvements, stale } = compare(counts, baseline); -let failed = failures.length > 0; -const improved = improvements.length > 0; + for (const f of failures) { + console.error(`\n${f.key}: ${f.now} ${f.kind} sites, baseline ${f.base} (+${f.now - f.base}):`); + for (const h of allHits[f.key].filter((h) => h.kind === f.kind)) { + console.error(` ${f.key}:${h.line} ${h.name}`); + } + } + for (const i of improvements) { + console.log( + `${i.key}: ${i.kind} ${i.now} < baseline ${i.base} — hardened by ${i.base - i.now}.`, + ); + } + if (stale.length > 0) { + failed = true; + console.error('\nStale baseline entries — these files no longer exist:'); + for (const key of stale) console.error(` ${key}`); + console.error( + 'A stale entry is a ceiling waiting for a file to be re-added under the same\n' + + 'path, which would then inherit it instead of starting at 0. Run --update to prune.', + ); + } -for (const f of failures) { - console.error(`\n${f.key}: ${f.now} ${f.kind} sites, baseline ${f.base} (+${f.now - f.base}):`); - for (const h of allHits[f.key].filter((h) => h.kind === f.kind)) { - console.error(` ${f.key}:${h.line} ${h.name}`); + if (failed) { + console.error( + '\nPollution ratchet FAILED: a module gained pollutable sites. The fix depends on\n' + + "the class: method/invoke -> route through primordials (require('internal/primordials'),\n" + + ' ReflectApply for .call/.apply)\n' + + ' accessor -> read via a captured getter, e.g.\n' + + ' TypedArrayPrototypeGetBuffer(view)\n' + + ' global -> capture at module-eval (`var StringG = String;`) in an\n' + + ' eager module; in a lazy one take the loader handle\n' + + " (require('internal/primordials'), require.pristineBuffer)\n" + + 'A genuine false positive (the receiver is a class instance, not a built-in) takes\n' + + '`// primordials-ok` on the same line. On a line carrying candidates from more\n' + + 'than one class the bare marker suppresses nothing — name it:\n' + + '`// primordials-ok: method`.', + ); + process.exit(1); + } + if (improved) { + console.log('\nRatchet improved — commit the tighter baseline:'); + console.log(' node scripts/check-primordials.mjs --update'); + process.exit(1); } -} -for (const i of improvements) { - console.log(`${i.key}: ${i.kind} ${i.now} < baseline ${i.base} — hardened by ${i.base - i.now}.`); -} -if (stale.length > 0) { - failed = true; - console.error('\nStale baseline entries — these files no longer exist:'); - for (const key of stale) console.error(` ${key}`); - console.error( - 'A stale entry is a ceiling waiting for a file to be re-added under the same\n' + - 'path, which would then inherit it instead of starting at 0. Run --update to prune.', - ); -} -if (failed) { - console.error( - '\nPollution ratchet FAILED: a module gained pollutable sites. The fix depends on\n' + - "the class: method/invoke -> route through primordials (require('internal/primordials'),\n" + - ' ReflectApply for .call/.apply)\n' + - ' accessor -> read via a captured getter, e.g.\n' + - ' TypedArrayPrototypeGetBuffer(view)\n' + - ' global -> capture at module-eval (`var StringG = String;`) in an\n' + - ' eager module; in a lazy one take the loader handle\n' + - " (require('internal/primordials'), require.pristineBuffer)\n" + - 'A genuine false positive (the receiver is a class instance, not a built-in) takes\n' + - '`// primordials-ok` on the same line. On a line carrying candidates from more\n' + - 'than one class the bare marker suppresses nothing — name it:\n' + - '`// primordials-ok: method`.', - ); - process.exit(1); + console.log(`OK: pollution ratchet holds (${grandTotal()} sites: ${perKindLine()}).`); } -if (improved) { - console.log('\nRatchet improved — commit the tighter baseline:'); - console.log(' node scripts/check-primordials.mjs --update'); - process.exit(1); -} - -console.log(`OK: pollution ratchet holds (${grandTotal()} sites: ${perKindLine()}).`); diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index f106175..c1da289 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -15,8 +15,12 @@ import test from 'node:test'; import assert from 'node:assert/strict'; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join } from 'node:path'; import { KINDS, countSource, countFile, walkDir, JS_DIR } from './lib/primordials-detect.mjs'; import { SELF_TEST, E } from './lib/primordials-fixtures.mjs'; +import { scanTree } from './check-primordials.mjs'; test('detector fixtures', async (t) => { for (const fixture of SELF_TEST) { @@ -60,3 +64,49 @@ test('every scanned file parses', () => { assert.doesNotThrow(() => countFile(file), `${file} did not parse`); } }); + +// The detector fixtures above pass `eager` in by hand, so they cannot see the +// ratchet's own eager/lazy classification — check-primordials.mjs's `isEager` +// over lib/js-graph.mjs's `eagerModules`. That path used to be pinned only by the +// real tree: a mutant scoring every module eager re-exempted some lazy module's +// module-eval capture and the baseline comparison caught it. Once #430 left no +// lazy module with such a capture, the mutant changed no count and survived. A +// fixture tree keeps the classification honest whatever the real tree holds: the +// loader's top-level req() makes one module eager, a second is reached by nothing +// the loader runs, and both open with the same `var S = String;`. +test('ratchet scan: a lazy module-eval capture counts as global, an eager one does not', () => { + const root = mkdtempSync(join(tmpdir(), 'primordials-scan-')); + const files = { + 'pkg/runtime/js/bootstrap/loader.js': + "(function () { var S = String; function req(name) { return name; } req('internal/eager'); return req; });", + 'pkg/runtime/js/internal/eager.js': + '(function (require, module) { var S = String; module.exports = S; });', + 'pkg/runtime/js/internal/lazy.js': + '(function (require, module) { var S = String; module.exports = S; });', + }; + try { + for (const [rel, src] of Object.entries(files)) { + mkdirSync(dirname(join(root, rel)), { recursive: true }); + writeFileSync(join(root, rel), src); + } + const { counts } = scanTree(root); + assert.deepEqual(Object.keys(counts).sort(), [ + 'bootstrap/loader.js', + 'internal/eager.js', + 'internal/lazy.js', + ]); + assert.equal( + counts['internal/lazy.js'].global, + 1, + 'eager classification: lazy module internal/lazy.js scored as eager', + ); + assert.equal( + counts['internal/eager.js'].global, + 0, + 'eager module internal/eager.js scored as lazy', + ); + assert.equal(counts['bootstrap/loader.js'].global, 0, 'bootstrap/loader.js scored as lazy'); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 03f5191..8ba8d74 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2047,12 +2047,13 @@ }, { "name": "the ratchet dies when every module is scanned as eager", - "why": "Classifying a lazy module as eager re-exempts its module-eval captures; the raised baseline then reads as an improvement, which the ratchet refuses.", + "why": "Classifying a lazy module as eager re-exempts its module-eval captures, so a live `var S = String` in a lazily required module scores 0. Gated on a fixture tree rather than the real one: since #430 no lazy module in the tree holds such a capture, so the mutant changed no count there and survived.", "source": "scripts/check-primordials.mjs", "find": "const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length));", "replace": "const isEager = () => true;", - "gate": "make:check-primordials", - "expect_detail": "hardened by" + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet scan: a lazy module-eval capture counts as global, an eager one does not", + "expect_detail": "eager classification: lazy module internal/lazy.js scored as eager" }, { "name": "check-js-graph fixtures die when a function-body require makes its target eager", From 2579be6b7dd32f662500e16d0effd9a0ae0e4854 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:13:36 +0200 Subject: [PATCH 43/61] fix(scripts): move the ratchet scan to lib so the CLI runs unguarded The previous commit exported scanTree from check-primordials.mjs behind an "am I the entry point?" guard. That guard failed open: invoked through a symlink, argv[1] did not match import.meta.url, the whole report was skipped and the gate exited 0 having printed nothing. The scan and its eager/lazy classification now live in scripts/lib/primordials-scan.mjs, which the test imports; the CLI is a plain script again. A new node:test case spawns the CLI on the real tree, directly and through a symlink, and requires the OK line. Both are recorded in the mutation manifest (isEager source repointed to the lib file; a new entry that silences the CLI before its self-test). Co-Authored-By: Claude Opus 5.5 --- scripts/check-primordials.mjs | 279 +++++++++++++---------------- scripts/check-primordials.test.mjs | 36 +++- scripts/lib/primordials-scan.mjs | 42 +++++ tests/mutation-manifest.json | 12 +- 4 files changed, 206 insertions(+), 163 deletions(-) create mode 100644 scripts/lib/primordials-scan.mjs diff --git a/scripts/check-primordials.mjs b/scripts/check-primordials.mjs index e3bae87..43f8746 100644 --- a/scripts/check-primordials.mjs +++ b/scripts/check-primordials.mjs @@ -1,9 +1,10 @@ // Prototype-pollution ratchet — CLI entry point. // -// The detector lives in lib/primordials-detect.mjs and its fixtures in -// lib/primordials-fixtures.mjs; this file is the baseline comparison, the -// --update path, and the report. See the detector for what each class means -// and why the tool parses rather than scans. +// The detector lives in lib/primordials-detect.mjs, its fixtures in +// lib/primordials-fixtures.mjs and the tree scan in lib/primordials-scan.mjs; +// this file is the baseline comparison, the --update path, and the report. See +// the detector for what each class means and why the tool parses rather than +// scans. // // Usage: // node scripts/check-primordials.mjs # check against baseline @@ -11,178 +12,138 @@ // node scripts/check-primordials.mjs --update --allow-raise import { readFileSync, writeFileSync } from 'node:fs'; -import { join, relative, sep } from 'node:path'; -import { fileURLToPath } from 'node:url'; -import { KINDS, countFile, walkDir, BASELINE, ROOT } from './lib/primordials-detect.mjs'; -import { eagerModules } from './lib/js-graph.mjs'; +import { KINDS, BASELINE, ROOT } from './lib/primordials-detect.mjs'; +import { scanTree } from './lib/primordials-scan.mjs'; import { selfTest } from './lib/primordials-fixtures.mjs'; import { compare, raises, totals } from './lib/primordials-baseline.mjs'; -// --- scan ------------------------------------------------------------------ - -// scanTree is exported, and takes the root, so the eager/lazy classification below -// can be driven over a fixture tree (check-primordials.test.mjs). Pinned only by the -// real tree, a mutant that scored every module eager survived as soon as no lazy -// module happened to hold a module-eval capture. -export function scanTree(root) { - // Module-eval reads are exempt only where they run before user code: every - // bootstrap/ file (Odin evaluates each at context init) and the modules the loader - // instantiates at init, derived from the graph rather than listed (lib/js-graph.mjs). - // A lazy module's capture runs on the user's first require — #333. - const EAGER = new Set(eagerModules(root)); - const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length)); - - const jsDir = join(root, 'pkg', 'runtime', 'js'); - const files = walkDir(jsDir).sort(); - const counts = {}; - const allHits = {}; - for (const file of files) { - const key = relative(jsDir, file).split(sep).join('/'); - const { hits } = countFile(file, { eager: isEager(key) }); - // PER-CLASS counts, not one number. The classes differ by an order of - // magnitude, and a single total would let a file trade a fixed accessor for a - // new global and still pass. - const byKind = {}; - for (const k of KINDS) byKind[k] = 0; - for (const h of hits) byKind[h.kind]++; - counts[key] = byKind; - allHits[key] = hits; - } - return { files, counts, allHits }; +// --- report ---------------------------------------------------------------- + +// Before anything else: a detector that has gone blind must not report on the +// tree, and must not be allowed to rebaseline. Exits non-zero rather than +// printing a reassuring "OK". +const selfTestFailures = selfTest(); +if (selfTestFailures.length > 0) { + console.error('Pollution ratchet SELF-TEST FAILED — the detector itself is broken:'); + for (const f of selfTestFailures) console.error(` ${f}`); + console.error( + '\nEvery fixture above pins a vector the ratchet is supposed to see, or a\n' + + 'position it must not count. Fix the detector before trusting any number it\n' + + 'prints.', + ); + process.exit(1); } -// Run only as the CLI: the test imports scanTree and must not trigger the report, -// its process.exit calls, or the baseline write behind --update. -if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) main(); - -function main() { - // --- report ---------------------------------------------------------------- - - // Before anything else: a detector that has gone blind must not report on the - // tree, and must not be allowed to rebaseline. Exits non-zero rather than - // printing a reassuring "OK". - const selfTestFailures = selfTest(); - if (selfTestFailures.length > 0) { - console.error('Pollution ratchet SELF-TEST FAILED — the detector itself is broken:'); - for (const f of selfTestFailures) console.error(` ${f}`); +// The scan, including the eager/lazy classification, is lib/primordials-scan.mjs so +// a test can drive it over a fixture tree. This file stays a plain script with no +// entry-point guard: its body must run however it is invoked. +const { files, counts, allHits } = scanTree(ROOT); + +const perKindLine = () => KINDS.map((k) => `${k} ${totals(counts).perKind[k]}`).join(', '); +const grandTotal = () => totals(counts).total; + +// A corrupt baseline is NOT a missing one. Both used to set haveBaseline=false, +// which put `--update` outside the refuse-to-raise guard entirely — so a file with +// git conflict markers in it (a large generated JSON that changes on every +// hardening, so conflicts are likely) turned the tool's own advice, "run +// --update", into an unguarded rebaseline of every floor. +let baseline = {}; +let haveBaseline = true; +let raw = null; +try { + raw = readFileSync(BASELINE, 'utf8'); +} catch { + haveBaseline = false; +} +if (raw !== null) { + try { + baseline = JSON.parse(raw); + } catch (err) { + console.error(`Baseline at ${BASELINE} is not valid JSON: ${err.message}`); console.error( - '\nEvery fixture above pins a vector the ratchet is supposed to see, or a\n' + - 'position it must not count. Fix the detector before trusting any number it\n' + - 'prints.', + 'Fix the file — do NOT rebaseline. Rewriting it would raise every floor to\n' + + 'whatever the tree currently is, which is what the ratchet exists to prevent.', ); process.exit(1); } +} - const { files, counts, allHits } = scanTree(ROOT); - - const perKindLine = () => KINDS.map((k) => `${k} ${totals(counts).perKind[k]}`).join(', '); - const grandTotal = () => totals(counts).total; - - // A corrupt baseline is NOT a missing one. Both used to set haveBaseline=false, - // which put `--update` outside the refuse-to-raise guard entirely — so a file with - // git conflict markers in it (a large generated JSON that changes on every - // hardening, so conflicts are likely) turned the tool's own advice, "run - // --update", into an unguarded rebaseline of every floor. - let baseline = {}; - let haveBaseline = true; - let raw = null; - try { - raw = readFileSync(BASELINE, 'utf8'); - } catch { - haveBaseline = false; - } - if (raw !== null) { - try { - baseline = JSON.parse(raw); - } catch (err) { - console.error(`Baseline at ${BASELINE} is not valid JSON: ${err.message}`); - console.error( - 'Fix the file — do NOT rebaseline. Rewriting it would raise every floor to\n' + - 'whatever the tree currently is, which is what the ratchet exists to prevent.', - ); - process.exit(1); - } - } - - const update = process.argv.includes('--update'); - if (update) { - // A raise is a policy decision, not a mechanical one: the point of the ratchet - // is that a floor only moves down. Previously `--update` rewrote every entry in - // both directions, so a contributor who hit a failure and reached for - // `UPDATE=1` silently raised the floor for every file — the prose rule in - // CLAUDE.md §5 was the only thing stopping it. - const up = haveBaseline ? raises(counts, baseline) : []; - if (up.length > 0 && !process.argv.includes('--allow-raise')) { - console.error('Refusing to RAISE the baseline. These entries would go up:\n'); - for (const r of up) { - console.error(` ${r.key}: ${r.kind} ${r.base} -> ${r.now} (+${r.now - r.base})`); - } - console.error( - '\nThe ratchet only moves down. Harden the sites, add `// primordials-ok` where\n' + - 'the receiver genuinely is not a built-in, or pass --allow-raise if you are\n' + - 'deliberately recording new ground (a newly scanned file, or a new class).', - ); - process.exit(1); +const update = process.argv.includes('--update'); +if (update) { + // A raise is a policy decision, not a mechanical one: the point of the ratchet + // is that a floor only moves down. Previously `--update` rewrote every entry in + // both directions, so a contributor who hit a failure and reached for + // `UPDATE=1` silently raised the floor for every file — the prose rule in + // CLAUDE.md §5 was the only thing stopping it. + const up = haveBaseline ? raises(counts, baseline) : []; + if (up.length > 0 && !process.argv.includes('--allow-raise')) { + console.error('Refusing to RAISE the baseline. These entries would go up:\n'); + for (const r of up) { + console.error(` ${r.key}: ${r.kind} ${r.base} -> ${r.now} (+${r.now - r.base})`); } - writeFileSync(BASELINE, JSON.stringify(counts, null, 2) + '\n'); - console.log( - `Wrote ${BASELINE} — ${grandTotal()} sites across ${files.length} files (${perKindLine()}).`, + console.error( + '\nThe ratchet only moves down. Harden the sites, add `// primordials-ok` where\n' + + 'the receiver genuinely is not a built-in, or pass --allow-raise if you are\n' + + 'deliberately recording new ground (a newly scanned file, or a new class).', ); - process.exit(0); - } - - if (!haveBaseline) { - console.error(`No baseline at ${BASELINE}. Run: node scripts/check-primordials.mjs --update`); process.exit(1); } + writeFileSync(BASELINE, JSON.stringify(counts, null, 2) + '\n'); + console.log( + `Wrote ${BASELINE} — ${grandTotal()} sites across ${files.length} files (${perKindLine()}).`, + ); + process.exit(0); +} - const { failures, improvements, stale } = compare(counts, baseline); - let failed = failures.length > 0; - const improved = improvements.length > 0; +if (!haveBaseline) { + console.error(`No baseline at ${BASELINE}. Run: node scripts/check-primordials.mjs --update`); + process.exit(1); +} - for (const f of failures) { - console.error(`\n${f.key}: ${f.now} ${f.kind} sites, baseline ${f.base} (+${f.now - f.base}):`); - for (const h of allHits[f.key].filter((h) => h.kind === f.kind)) { - console.error(` ${f.key}:${h.line} ${h.name}`); - } - } - for (const i of improvements) { - console.log( - `${i.key}: ${i.kind} ${i.now} < baseline ${i.base} — hardened by ${i.base - i.now}.`, - ); - } - if (stale.length > 0) { - failed = true; - console.error('\nStale baseline entries — these files no longer exist:'); - for (const key of stale) console.error(` ${key}`); - console.error( - 'A stale entry is a ceiling waiting for a file to be re-added under the same\n' + - 'path, which would then inherit it instead of starting at 0. Run --update to prune.', - ); - } +const { failures, improvements, stale } = compare(counts, baseline); +let failed = failures.length > 0; +const improved = improvements.length > 0; - if (failed) { - console.error( - '\nPollution ratchet FAILED: a module gained pollutable sites. The fix depends on\n' + - "the class: method/invoke -> route through primordials (require('internal/primordials'),\n" + - ' ReflectApply for .call/.apply)\n' + - ' accessor -> read via a captured getter, e.g.\n' + - ' TypedArrayPrototypeGetBuffer(view)\n' + - ' global -> capture at module-eval (`var StringG = String;`) in an\n' + - ' eager module; in a lazy one take the loader handle\n' + - " (require('internal/primordials'), require.pristineBuffer)\n" + - 'A genuine false positive (the receiver is a class instance, not a built-in) takes\n' + - '`// primordials-ok` on the same line. On a line carrying candidates from more\n' + - 'than one class the bare marker suppresses nothing — name it:\n' + - '`// primordials-ok: method`.', - ); - process.exit(1); - } - if (improved) { - console.log('\nRatchet improved — commit the tighter baseline:'); - console.log(' node scripts/check-primordials.mjs --update'); - process.exit(1); +for (const f of failures) { + console.error(`\n${f.key}: ${f.now} ${f.kind} sites, baseline ${f.base} (+${f.now - f.base}):`); + for (const h of allHits[f.key].filter((h) => h.kind === f.kind)) { + console.error(` ${f.key}:${h.line} ${h.name}`); } +} +for (const i of improvements) { + console.log(`${i.key}: ${i.kind} ${i.now} < baseline ${i.base} — hardened by ${i.base - i.now}.`); +} +if (stale.length > 0) { + failed = true; + console.error('\nStale baseline entries — these files no longer exist:'); + for (const key of stale) console.error(` ${key}`); + console.error( + 'A stale entry is a ceiling waiting for a file to be re-added under the same\n' + + 'path, which would then inherit it instead of starting at 0. Run --update to prune.', + ); +} - console.log(`OK: pollution ratchet holds (${grandTotal()} sites: ${perKindLine()}).`); +if (failed) { + console.error( + '\nPollution ratchet FAILED: a module gained pollutable sites. The fix depends on\n' + + "the class: method/invoke -> route through primordials (require('internal/primordials'),\n" + + ' ReflectApply for .call/.apply)\n' + + ' accessor -> read via a captured getter, e.g.\n' + + ' TypedArrayPrototypeGetBuffer(view)\n' + + ' global -> capture at module-eval (`var StringG = String;`) in an\n' + + ' eager module; in a lazy one take the loader handle\n' + + " (require('internal/primordials'), require.pristineBuffer)\n" + + 'A genuine false positive (the receiver is a class instance, not a built-in) takes\n' + + '`// primordials-ok` on the same line. On a line carrying candidates from more\n' + + 'than one class the bare marker suppresses nothing — name it:\n' + + '`// primordials-ok: method`.', + ); + process.exit(1); } +if (improved) { + console.log('\nRatchet improved — commit the tighter baseline:'); + console.log(' node scripts/check-primordials.mjs --update'); + process.exit(1); +} + +console.log(`OK: pollution ratchet holds (${grandTotal()} sites: ${perKindLine()}).`); diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index c1da289..ce4786f 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -15,12 +15,13 @@ import test from 'node:test'; import assert from 'node:assert/strict'; -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { spawnSync } from 'node:child_process'; +import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { KINDS, countSource, countFile, walkDir, JS_DIR } from './lib/primordials-detect.mjs'; import { SELF_TEST, E } from './lib/primordials-fixtures.mjs'; -import { scanTree } from './check-primordials.mjs'; +import { scanTree } from './lib/primordials-scan.mjs'; test('detector fixtures', async (t) => { for (const fixture of SELF_TEST) { @@ -66,7 +67,7 @@ test('every scanned file parses', () => { }); // The detector fixtures above pass `eager` in by hand, so they cannot see the -// ratchet's own eager/lazy classification — check-primordials.mjs's `isEager` +// ratchet's own eager/lazy classification — lib/primordials-scan.mjs's `isEager` // over lib/js-graph.mjs's `eagerModules`. That path used to be pinned only by the // real tree: a mutant scoring every module eager re-exempted some lazy module's // module-eval capture and the baseline comparison caught it. Once #430 left no @@ -110,3 +111,32 @@ test('ratchet scan: a lazy module-eval capture counts as global, an eager one do rmSync(root, { recursive: true, force: true }); } }); + +// The CLI itself must report. It once sat behind an "am I the entry point?" guard +// so this file could import its scan, and that guard failed OPEN: run through a +// symlink, argv[1] no longer matched import.meta.url, the body never ran, and the +// gate exited 0 having printed nothing. The scan now lives in lib/ and the CLI has +// no guard; this pins both the plain invocation and the symlinked one, since an +// exit code alone cannot tell "checked and passed" from "never checked". +test('ratchet CLI reports OK on the real tree, also when invoked through a symlink', () => { + const cli = join(import.meta.dirname, 'check-primordials.mjs'); + const dir = mkdtempSync(join(tmpdir(), 'primordials-cli-')); + try { + const link = join(dir, 'check-primordials.mjs'); + symlinkSync(cli, link); + for (const [how, path] of [ + ['direct path', cli], + ['symlink', link], + ]) { + const r = spawnSync(process.execPath, [path], { encoding: 'utf8' }); + assert.equal(r.status, 0, `ratchet CLI (${how}) exited ${r.status}: ${r.stderr}`); + assert.match( + r.stdout, + /^OK: pollution ratchet holds/m, + `ratchet CLI (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, + ); + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); diff --git a/scripts/lib/primordials-scan.mjs b/scripts/lib/primordials-scan.mjs new file mode 100644 index 0000000..dba88b9 --- /dev/null +++ b/scripts/lib/primordials-scan.mjs @@ -0,0 +1,42 @@ +// Tree scan for the pollution ratchet: walks pkg/runtime/js under `root`, decides +// which files are eager, and counts each file's sites per class. +// +// Pulled out of check-primordials.mjs for the same reason primordials-baseline.mjs +// was: the eager/lazy classification is only testable over a fixture tree, and a +// fixture tree needs a scan that takes its root. Pinned only by the real tree, a +// mutant that scored every module eager survived as soon as no lazy module happened +// to hold a module-eval capture. An earlier version exported this from the CLI +// itself behind an "am I the entry point?" guard; that guard failed open (a +// symlinked argv[1] skipped the whole report and exited 0), so the CLI stays +// guard-free and the importable part lives here. + +import { join, relative, sep } from 'node:path'; +import { KINDS, countFile, walkDir } from './primordials-detect.mjs'; +import { eagerModules } from './js-graph.mjs'; + +export function scanTree(root) { + // Module-eval reads are exempt only where they run before user code: every + // bootstrap/ file (Odin evaluates each at context init) and the modules the loader + // instantiates at init, derived from the graph rather than listed (lib/js-graph.mjs). + // A lazy module's capture runs on the user's first require — #333. + const EAGER = new Set(eagerModules(root)); + const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length)); + + const jsDir = join(root, 'pkg', 'runtime', 'js'); + const files = walkDir(jsDir).sort(); + const counts = {}; + const allHits = {}; + for (const file of files) { + const key = relative(jsDir, file).split(sep).join('/'); + const { hits } = countFile(file, { eager: isEager(key) }); + // PER-CLASS counts, not one number. The classes differ by an order of + // magnitude, and a single total would let a file trade a fixed accessor for a + // new global and still pass. + const byKind = {}; + for (const k of KINDS) byKind[k] = 0; + for (const h of hits) byKind[h.kind]++; + counts[key] = byKind; + allHits[key] = hits; + } + return { files, counts, allHits }; +} diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 8ba8d74..0d8a678 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2048,13 +2048,23 @@ { "name": "the ratchet dies when every module is scanned as eager", "why": "Classifying a lazy module as eager re-exempts its module-eval captures, so a live `var S = String` in a lazily required module scores 0. Gated on a fixture tree rather than the real one: since #430 no lazy module in the tree holds such a capture, so the mutant changed no count there and survived.", - "source": "scripts/check-primordials.mjs", + "source": "scripts/lib/primordials-scan.mjs", "find": "const isEager = (key) => key.startsWith('bootstrap/') || EAGER.has(key.slice(0, -'.js'.length));", "replace": "const isEager = () => true;", "gate": "node-test:scripts/check-primordials.test.mjs", "test": "ratchet scan: a lazy module-eval capture counts as global, an eager one does not", "expect_detail": "eager classification: lazy module internal/lazy.js scored as eager" }, + { + "name": "the ratchet CLI dies when it exits 0 without reporting", + "why": "An exit code alone cannot tell a checked tree from an unchecked one: an entry-point guard once made the CLI skip its whole body when invoked through a symlink and exit 0 silently. Silencing the CLI ahead of the self-test must fail on the missing OK line.", + "source": "scripts/check-primordials.mjs", + "find": "const selfTestFailures = selfTest();", + "replace": "process.exit(0);\nconst selfTestFailures = selfTest();", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "ratchet CLI (direct path) did not print its OK line" + }, { "name": "check-js-graph fixtures die when a function-body require makes its target eager", "why": "A require inside a function runs when that function is called, possibly after user code; propagating eagerness through it exempts a lazy module's captures.", From efa53bcf40eb5217e537107abc7adb835ab87cb3 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:48:14 +0200 Subject: [PATCH 44/61] test(primordials): record the fail-open entry-point guard as a mutation The symlink arm of the CLI spawn test is the only thing that catches a re-added argv[1] guard; without a recorded mutation, deleting that arm survived the whole manifest. Co-Authored-By: Claude Opus 5.5 --- tests/mutation-manifest.json | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 0d8a678..83ad6dc 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2065,6 +2065,16 @@ "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", "expect_detail": "ratchet CLI (direct path) did not print its OK line" }, + { + "name": "the ratchet CLI dies when an entry-point guard skips it under a symlink", + "why": "e0968ef shipped `if (argv[1] === fileURLToPath(import.meta.url)) main()`, which fails OPEN: run through a symlink argv[1] is the link, the body never runs and the gate exits 0 silently. A direct invocation still passes that guard, so only the symlink arm of the CLI spawn test sees it.", + "source": "scripts/check-primordials.mjs", + "find": "const selfTestFailures = selfTest();", + "replace": "if (process.argv[1] !== new URL(import.meta.url).pathname) process.exit(0);\nconst selfTestFailures = selfTest();", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "ratchet CLI (symlink) did not print its OK line" + }, { "name": "check-js-graph fixtures die when a function-body require makes its target eager", "why": "A require inside a function runs when that function is called, possibly after user code; propagating eagerness through it exempts a lazy module's captures.", From b6b774216ced7112e566e512d36178708b87f029 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:56:31 +0200 Subject: [PATCH 45/61] harden(gates): protect scripts/lib/primordials-scan.mjs from agent writes The ratchet's eager/lazy classification and tree walk moved out of scripts/check-primordials.mjs (protected) into this lib in the previous commit. Without this entry an agent could weaken which module-eval captures the ratchet exempts, which the hook blocked before the move. Co-Authored-By: Claude Opus 5.5 --- runtime/gates/integrity.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/gates/integrity.mjs b/runtime/gates/integrity.mjs index 2e86dfe..1b10099 100644 --- a/runtime/gates/integrity.mjs +++ b/runtime/gates/integrity.mjs @@ -125,6 +125,7 @@ export const PROTECTED_WRITE_PATHS = [ 'scripts/lib/primordials-baseline.mjs', 'scripts/lib/primordials-detect.mjs', 'scripts/lib/primordials-fixtures.mjs', + 'scripts/lib/primordials-scan.mjs', 'scripts/check-primordials.mjs', 'scripts/run-mutations.mjs', 'scripts/run-node-compat-all.sh', From f146fb6e6485bfc6aa813b08807e662546845718 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:57:48 +0200 Subject: [PATCH 46/61] harden(runtime): lock Function.prototype.call/apply/bind before user code SES-style non-configurable accessors (as node --frozen-intrinsics): assigning on Function.prototype throws in both modes, f.call = x on an ordinary function still defines an own property. Closes #450. Approved deviation, pinned by main.function_call_lock; poisoned-call vectors move from oracle cases into it. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 16 +- ROADMAP.md | 30 ++-- cmd/lava/encoding_pollution_test.odin | 5 +- cmd/lava/function_call_lock_test.odin | 148 ++++++++++++++++++ docs/ARCHITECTURE.md | 10 ++ pkg/runtime/js/bootstrap/loader.js | 11 +- pkg/runtime/js/internal/primordials.js | 124 ++++++++++----- tests/mutation-manifest.json | 23 ++- .../cases/55-encoding-pollution.js | 109 ++----------- tests/node-compat/pollution-baseline.json | 2 +- .../eventloop/cases/11-scheduler-hardening.js | 12 +- 11 files changed, 318 insertions(+), 172 deletions(-) create mode 100644 cmd/lava/function_call_lock_test.odin diff --git a/CLAUDE.md b/CLAUDE.md index 11a1432..bbe47af 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -221,6 +221,17 @@ Rules that keep this from becoming ceremony: getter such as `TypedArrayPrototypeGetBuffer`), and `global` (a replaceable global read live instead of captured at module-eval, which the loader runs before user code). + **`Function.prototype.call/apply/bind` are locked** realm-wide before user code + (`lockIntrinsics`, run by `bootstrap/loader.js`; #450): non-configurable + accessors, so `Function.prototype.call = x` throws in both modes while + `f.call = x` on an ordinary function still defines an own property, as in node. + That is a §1 deviation, pinned by `cmd/lava/function_call_lock_test.odin`. What + it buys: `fn.call(t, a)` on a function WE captured can no longer be steered, so + the `callerN` wrappers keep their live `.call` at no security cost. What it does + not: a CALLER-SUPPLIED function can still carry an own `call` or a re-pointed + prototype, and `cb.call(t, x)` then runs the caller's `call` where node invokes + `cb` directly — still `ReflectApply` there. The `invoke` class keeps counting + both until the ratchet can tell them apart. **Module-eval capture is pristine only in a module the loader instantiates BEFORE user code** — the eager list in `bootstrap/loader.js`, plus `stream`/`stdio` via `installStdio`. A LAZY module's factory runs on the user's first `require()`, so a capture there is a live read @@ -270,8 +281,9 @@ Rules that keep this from becoming ceremony: 0 while `units.buffer` went through the live `%TypedArray%.prototype.buffer` getter, which is why the accessor class exists. And a high number is not automatically work: `primordials.js` reads 9 in `invoke` **by construction**, - because every `callerN` wrapper is a `fn.call` — its `global` column is 1, - since the module-eval rule already exempts the capture table. + because every `callerN` wrapper is a `fn.call` on a captured method, which the + lock above makes safe — its `global` column is 0, since the module-eval rule + already exempts the capture table. - Pick the primordial by **arity**. `callerN` (`ArrayPrototypePush`) carries an `arguments` switch and belongs at cold call sites; per-element loops use the fixed-arity wrappers (`ArrayPrototypePush1`/`Push2`) or plain indexed writes diff --git a/ROADMAP.md b/ROADMAP.md index eaeafcd..466a1b4 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -687,13 +687,17 @@ the index, the issue is the detail. Ordered by what it unblocks. capturing a module's globals at module-eval is mechanical and drives its `global` column to 0, which `encoding.js` already demonstrates. Note `primordials.js` reads 9 in `invoke` **by construction** — every `callerN` - wrapper is a `fn.call` — while its `global` column is 1, because the + wrapper is a `fn.call` — while its `global` column is 0, because the module-eval rule already exempts the capture table. Neither is work. - **Next, and it is the largest single win left**: wire `lockIntrinsics()`. - It is written and exported in `primordials.js` and called from nowhere, so - the whole `invoke` class is live today. Now that the class is counted it is - also measurable, and the exposure is worse than the "exotic axis" the file - header used to call it: replacing `Function.prototype.call` makes Lava throw + **Done ([#450](https://github.com/x-ror/lava/issues/450))**: `lockIntrinsics()` + is wired — `bootstrap/loader.js` runs it right after primordials is captured, + making `Function.prototype.call/apply/bind` non-configurable accessors in + node's `--frozen-intrinsics` shape (assignment on `Function.prototype` throws + in both modes; `f.call = x` on an ordinary function still works). It is a §1 + deviation pinned by `cmd/lava/function_call_lock_test.odin`; vectors H, Q2 and V + of `55-encoding-pollution.js` and the call/apply poison in eventloop case 11 moved + there, since node accepts the assignment Lava now refuses. What follows is the + record of why it was needed. Before the lock, replacing `Function.prototype.call` made Lava throw where Node succeeds (TextDecoder/TextEncoder construction, `Buffer.from`, `buf.toString`, `Buffer.concat`, `new URL`) and — worse — answer _wrongly_ where Node is right (`URLSearchParams.get` → `null`, `util.format` → @@ -719,15 +723,13 @@ the index, the issue is the detail. Ordered by what it unblocks. read at all and is FASTER than the `.call` it replaced (0.74x on a 9-byte decode, 0.91x on a 20-parameter URLSearchParams parse, medians of 7 interleaved launches) because toString's length getter, range clamp and - encoding-name dispatch drop out. Pinned by vector V in - `55-encoding-pollution.js`. Where a JS layer is borrowing a prototype method + encoding-name dispatch drop out. Where a JS layer is borrowing a prototype method that only wraps a native we already own, going to the native beats both - `.call` and `Reflect.apply` — check for that before reaching for either. The open question is only - `lockIntrinsics()`'s own Node deviation — Node leaves the intrinsics - writable, so a program assigning `Function.prototype.call` would silently - not take effect; that narrower divergence needs a Lava-only test pinning it - per §1, plus a full `make test-lava` pass to find what in the oracle suites - legitimately writes an intrinsic. + `.call` and `Reflect.apply` — check for that before reaching for either. + Still open: `.call` on a CALLER-SUPPLIED function (a listener, a callback) + is outside the lock — the function can carry its own `call` — so those sites + still want `ReflectApply`, and the `invoke` class cannot be retired until the + ratchet can tell a captured receiver from a caller-supplied one. - [x] **`run_until_idle` ended the drive on one no-progress tick** — fixed: it now keeps polling while `active_io_count` or `active_async` is nonzero, the invariant `run()` has carried since #113 (ba12bd3) and which that fix reached diff --git a/cmd/lava/encoding_pollution_test.odin b/cmd/lava/encoding_pollution_test.odin index 7d3ffb1..d9c443a 100644 --- a/cmd/lava/encoding_pollution_test.odin +++ b/cmd/lava/encoding_pollution_test.odin @@ -22,7 +22,6 @@ ENCODING_POLLUTION_SCRIPT :: ` const realToLowerCase = String.prototype.toLowerCase; const realTrim = String.prototype.trim; const realFromCharCode = String.fromCharCode; -const realApply = Function.prototype.apply; const realPush = Array.prototype.push; const realBufferFrom = Buffer.from; const realBufferToString = Buffer.prototype.toString; @@ -45,7 +44,8 @@ function record(name, fn) { String.prototype.toLowerCase = function () { return 'utf-8'; }; String.prototype.trim = function () { return 'utf-8'; }; String.fromCharCode = function () { return 'PWNED'; }; -Function.prototype.apply = function () { return 'PWNED'; }; +// Function.prototype.apply is no longer poisoned here: since #450 it is locked +// before user code, so the assignment throws (pinned by function_call_lock_test). Array.prototype.push = function () { return 0; }; Buffer.from = function () { return new Uint8Array([0x50]); }; Buffer.prototype.toString = function () { return 'PWNED'; }; @@ -85,7 +85,6 @@ record('encode', function () { String.prototype.toLowerCase = realToLowerCase; String.prototype.trim = realTrim; String.fromCharCode = realFromCharCode; -Function.prototype.apply = realApply; Array.prototype.push = realPush; Buffer.from = realBufferFrom; Buffer.prototype.toString = realBufferToString; diff --git a/cmd/lava/function_call_lock_test.odin b/cmd/lava/function_call_lock_test.odin new file mode 100644 index 0000000..651c83f --- /dev/null +++ b/cmd/lava/function_call_lock_test.odin @@ -0,0 +1,148 @@ +#+build linux, darwin +package main + +import "core:testing" +import lava "lava:pkg/runtime" +import eventloop "lava:pkg/runtime/eventloop" + +// Pins a deliberate §1 deviation where Lava is STRICTER than node: +// Function.prototype.call/apply/bind are locked realm-wide before user code runs +// (primordials.js lockIntrinsics, called from bootstrap/loader.js). node lets a +// script replace them; Lava throws. That is a §1 deviation, so no oracle case can +// express it — node prints "assigned" where Lava prints "THREW" — and it lives here. +// +// The shape is node's own `--frozen-intrinsics` (node 26, verified with +// scratch probe.js): an accessor whose setter throws on Function.prototype itself +// in BOTH modes, and defines an own data property on any other receiver, so +// `f.call = x` on an ordinary function behaves exactly as it does in plain node. +// A non-writable DATA property would be cheaper to write and is wrong in two ways +// the checks below each catch: sloppy `Function.prototype.call = x` would no-op +// silently instead of throwing, and `f.call = x` on an ordinary function would +// throw in strict code (the override mistake) where node succeeds. +// +// Every check is recorded and compared at the end rather than thrown at first +// failure, so a mutation's red output names the check it broke (the manifest's +// expect_detail matches on that name). +FUNCTION_CALL_LOCK_SCRIPT :: ` +const FP = Function.prototype; +const real = { call: FP.call, apply: FP.apply, bind: FP.bind }; +const results = []; +function record(name, fn) { + let out; + try { + out = String(fn()); + } catch (e) { + out = 'THREW:' + e.name; + } + results[results.length] = name + '=' + out; +} + +// Sloppy assignment on Function.prototype: a plain data lock no-ops here silently, +// which is exactly the outcome this lock exists to rule out. +for (const k of ['call', 'apply', 'bind']) { + record('sloppy-' + k, function () { + FP[k] = function () { return 'POISON'; }; + return 'assigned'; + }); + record('strict-' + k, function () { + 'use strict'; + FP[k] = function () { return 'POISON'; }; + return 'assigned'; + }); + record('define-' + k, function () { + Object.defineProperty(FP, k, { value: function () { return 'POISON'; } }); + return 'defined'; + }); + record('intact-' + k, function () { return FP[k] === real[k]; }); +} +record('names', function () { + return [FP.call.name, FP.call.length, FP.apply.name, FP.apply.length, FP.bind.name, FP.bind.length].join(','); +}); + +// The override mistake: assigning to an ORDINARY function's inherited call/apply/ +// bind must create an own data property, as in node, and must not leak to others. +record('own-strict', function () { + 'use strict'; + function f() {} + f.call = function () { return 'own'; }; + const d = Object.getOwnPropertyDescriptor(f, 'call'); + return [f.call(), d.writable, d.enumerable, d.configurable, (function g() {}).call === real.call].join(','); +}); +record('own-sloppy', function () { + function f() {} + f.apply = function () { return 'own'; }; + return [f.apply(), Object.prototype.hasOwnProperty.call(f, 'apply')].join(','); +}); + +// #450's table: every builtin that used to break under a replaced .call, run +// after the replacement attempts above so it proves the lock held. +record('usp', function () { return new URLSearchParams('a=1').get('a'); }); +record('format', function () { return require('util').format('%s', 'x'); }); +record('buffer', function () { return Buffer.from('hi').toString('hex'); }); +record('url', function () { return new URL('http://a/b').pathname; }); +record('decode', function () { return new TextDecoder().decode(new Uint8Array([104, 105])); }); +record('decode16', function () { + return new TextDecoder('utf-16le').decode(new Uint8Array([0x41, 0, 0x42, 0])); +}); +// Vectors H1-H4 and Q2 of tests/node-compat/cases/55-encoding-pollution.js, which +// poisoned Function.prototype.apply (String.fromCharCode.apply once rewrote a +// decoded host to http://a/). node accepts that assignment and Lava now refuses it, +// so they moved here; the expected values are node 26's output for the same calls. +record('h1', function () { return new URL('http://%C3%BCber.example/').href; }); +record('h2', function () { + return new TextDecoder('utf-8', { fatal: true }).decode(new Uint8Array([0xc3, 0xbc, 0x62, 0x65, 0x72])) === 'über'; +}); +record('h3', function () { + return new TextDecoder('utf-16le').decode(new Uint8Array([0x3d, 0xd8, 0x00, 0xde, 0x41, 0x00])) === '😀A'; +}); +record('h4', function () { return Array.from(new TextEncoder().encode('ü€😀')).join(','); }); +record('q2', function () { + const big = new Uint8Array(9000); + for (let i = 0; i < big.length; i++) big[i] = 0x41 + (i % 26); + const s = new TextDecoder('windows-1252').decode(big); + return s.length + ':' + s.charCodeAt(0) + ':' + s.charCodeAt(8192) + ':' + s.charCodeAt(8999); +}); + +const want = [ + 'sloppy-call=THREW:TypeError', 'strict-call=THREW:TypeError', 'define-call=THREW:TypeError', 'intact-call=true', + 'sloppy-apply=THREW:TypeError', 'strict-apply=THREW:TypeError', 'define-apply=THREW:TypeError', 'intact-apply=true', + 'sloppy-bind=THREW:TypeError', 'strict-bind=THREW:TypeError', 'define-bind=THREW:TypeError', 'intact-bind=true', + 'names=call,1,apply,2,bind,1', + 'own-strict=own,true,true,true,true', + 'own-sloppy=own,true', + 'usp=1', 'format=x', 'buffer=6869', 'url=/b', 'decode=hi', 'decode16=AB', + 'h1=http://xn--ber-goa.example/', 'h2=true', 'h3=true', 'h4=195,188,226,130,172,240,159,152,128', + 'q2=9000:65:67:68', +]; +const bad = []; +for (let i = 0; i < want.length; i++) { + if (results[i] !== want[i]) bad[bad.length] = 'want ' + want[i] + ' got ' + results[i]; +} +if (bad.length !== 0) throw new Error(bad.join('; ')); + +// Moved from tests/runtime/eventloop/cases/11-scheduler-hardening.js, which used to +// poison call/apply before scheduling: nextTick must still forward its arguments +// and queueMicrotask must still run. The exit code carries a miss. +let hits = 0; +process.nextTick(function (a, b) { if (a + b === 'xy') hits += 1; }, 'x', 'y'); +queueMicrotask(function () { hits += 10; }); +setTimeout(function () { if (hits !== 11) process.exitCode = 3; }, 0); +console.log('function-call-lock ok'); +` + +@(test) +function_call_lock :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(FUNCTION_CALL_LOCK_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f09b9f8..c882d0c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -528,6 +528,16 @@ pollutable site, each baselined separately in | `accessor` | `view.buffer` | a configurable prototype getter | | `global` | `String(x)` | a replaceable global, read live | +`Function.prototype.call/apply/bind` themselves are **locked** before user code: +the loader calls `lockIntrinsics()` right after the primordials table is captured, +turning each into a non-configurable accessor in the shape of node's +`--frozen-intrinsics` (assignment on `Function.prototype` throws in both modes; +`f.call = x` on an ordinary function still defines an own property). That is a +deliberate node deviation (#450), pinned by `cmd/lava/function_call_lock_test.odin`, +and it is what lets the `callerN` wrappers keep a live `.call` on a captured method. +It does not cover `.call` on a caller-supplied function, which can carry its own +`call`, so the `invoke` class still counts every site. + A `global` read at module-eval is exempt only in a module instantiated before user code — the loader's `req()` roots closed over top-level requires, computed by `scripts/lib/js-graph.mjs` (the parser `check-js-graph.mjs` also uses). In a lazy diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index 974ad9c..c254ca9 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -100,7 +100,16 @@ // primordials is eager-loaded FIRST, before any other internal module and well // before user code, so it captures pristine intrinsics (see primordials.js). // Other modules require('internal/primordials') and get this cached, pristine table. - req('internal/primordials'); + // + // Then lock Function.prototype.call/apply/bind (#450; the deviation is documented on + // lockIntrinsics). Here and nowhere else, for two reasons. It must follow the capture, + // which reads the three as plain values. And the loader is the one choke point every + // user-code entry runs behind: there is exactly one JSGlobalContextCreate that hosts a + // script (runtime.odin eval), install_globals runs this loader before the entry + // script, `.mjs` sources only reach the same context through esm.js's transform, and + // multi-worker mode is N such evals. Only runtime code runs before this line (the + // console prelude, install_process), so locking here leaves no window. + req('internal/primordials').lockIntrinsics(); req('buffer'); // Hand the LAZY internal modules a PRISTINE Buffer. diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index d6eef88..18ddb8e 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -21,19 +21,15 @@ // THREAT MODEL / RESIDUAL. The captured method makes these wrappers immune to // prototype-method pollution (Array.prototype.push = …) and global replacement // (globalThis.Array = …) — the realistic axes. They invoke through `.call`, -// which reads Function.prototype.call dynamically, so a script that overrides -// `Function.prototype.call/apply` still reaches them. This file used to call that -// axis "exotic … self-defeating as an attack"; that was wrong twice over, and the -// `invoke` ratchet class made it measurable. Wrapping `Function.prototype.call` is -// something instrumentation, tracing and coverage libraries do on purpose, so the -// trigger need not be an attacker at all — and the outcome is not a uniform -// breakage but a SPLIT: TextDecoder/TextEncoder construction, Buffer.from, -// buf.toString, Buffer.concat and new URL throw where node succeeds, while -// URLSearchParams.get answers `null` and util.format answers `'undefined'` where -// node is right. A silent wrong answer from a URL parameter lookup is the sharp -// end of that. See the measured table above caller0 below; closed completely only -// by lockIntrinsics(), which is written, exported, called from nowhere, and -// tracked in ROADMAP. +// which reads Function.prototype.call dynamically. That axis is closed by +// lockIntrinsics() below, which bootstrap/loader.js runs right after this table +// is captured and before any user code: Function.prototype.call/apply/bind can no +// longer be replaced, so the live `.call` read always finds the original (#450). +// Before the lock, replacing `.call` — which tracing and coverage libraries do on +// purpose, so the trigger need not be an attacker — made TextDecoder/TextEncoder +// construction, Buffer.from, buf.toString, Buffer.concat and new URL throw where +// node succeeds, and URLSearchParams.get answer `null` and util.format +// `'undefined'` where node is right. // The accessor axis (a setter on a numeric-index property of Array/Object.proto) // is not a method concern and is handled per-hot-array with null prototypes in // the consuming modules. @@ -49,12 +45,13 @@ var ArrayProto = Array.prototype; var StringProto = String.prototype; - // --- the `.call` residual, and where it is NOT acceptable --------------- + // --- the `.call` read, and why the wrappers may keep it --------------- // // Every wrapper below invokes through `fn.call(t, …)`, which READS - // Function.prototype.call at call time. That property is writable, so a script - // that replaces it reaches every primordial — the residual this file's header - // has always documented, "closed only by lockIntrinsics(), applied opt-in". + // Function.prototype.call at call time. Until #450 that property was writable, so + // a script that replaced it reached every primordial. lockIntrinsics() now makes + // it unreplaceable before user code runs; the history below is why the wrappers + // were not converted to Reflect.apply instead, and why safeGetter still is. // // It is not theoretical. Poisoning ONLY Function.prototype.call — leaving the // byteOffset/byteLength descriptors untouched — reproduced a full Buffer-pool @@ -88,15 +85,8 @@ // The view accessors below are `safeGetter`: three calls per decode against a // ~1-4us decode, so the 33ns each is under 3% and buys immunity on the path // where a forged answer reads another request's bytes. - // - // What the residual actually costs today, since the `invoke` class is now - // counted and therefore measurable: with `Function.prototype.call` replaced, - // Lava diverges from Node on TextDecoder/TextEncoder construction, Buffer.from, - // buf.toString, Buffer.concat and new URL (they throw where Node succeeds) and - // — worse — answers WRONGLY on URLSearchParams.get (null) and util.format - // ('undefined'). lockIntrinsics() closes all of it at zero per-call cost; it is - // written, exported, and called from nowhere. Wiring it at bootstrap is the - // complete answer and is tracked in ROADMAP with this table. + // (safeGetter predates the lock and is kept: it is cold, so reverting it to + // `.call` would win nothing measurable.) var reflectApply = Reflect.apply; var NO_ARGS = Object.freeze([]); function safeGetter(fn) { @@ -221,24 +211,72 @@ // for parity with Node's export, but is now `.call`-based (not bind.bind(call)). var uncurryThis = callerN; - // lockIntrinsics pins Function.prototype.call/apply/bind so the wrappers' - // `.call` route cannot be poisoned. Opt-in: the runtime applies it only under - // the frozen-intrinsics flag (npm packages that legitimately reassign these - // are vanishingly rare but not proven absent). Idempotent; safe to call once - // at bootstrap, before user code. Locking keeps the methods callable — it only - // forbids reassignment. + // Captured here, at the eager module-eval the loader runs before user code, so the + // setter below throws a pristine TypeError and defines through a pristine + // defineProperty whatever user code later does to the globals. + var TypeErrorG = TypeError; + var defineProperty = Object.defineProperty; + + /** + * Locks Function.prototype.call/apply/bind realm-wide, in the shape node's + * `--frozen-intrinsics` gives them: a non-configurable accessor whose getter + * returns the original method and whose setter refuses Function.prototype itself + * but fixes the "override mistake" for every other receiver. Called ONCE, by + * bootstrap/loader.js, right after this table is captured and before user code. + * @throws {TypeError} `Function.prototype.call = x`, sloppy or strict, and any + * Object.defineProperty of the three on Function.prototype. + * @node node 26 lets a script replace all three (assignment succeeds in both + * modes). Under `node --frozen-intrinsics` it behaves as here: both modes + * throw, defineProperty throws, and `f.call = x` on an ordinary function `f` + * defines an own writable/enumerable/configurable property (all four rows + * probed on node 26.13). + * @deviates Default node allows the replacement; Lava throws. Traded for: the + * `callerN` wrappers keep their live `fn.call` (25x cheaper than Reflect.apply + * on the bare wrapper, table above) without Function.prototype.call being a + * way to break or steer URL, URLSearchParams, util.format, Buffer and + * TextDecoder (#450). Pinned by the Lava-only test + * cmd/lava/function_call_lock_test.odin (main.function_call_lock). + */ function lockIntrinsics() { - var def = Object.defineProperty; - var lock = function (obj, name, value) { - try { - def(obj, name, { value: value, writable: false, configurable: false }); - } catch (e) { - /* already locked / non-configurable — leave as is */ - } - }; - lock(FunctionProto, 'call', call); - lock(FunctionProto, 'apply', apply); - lock(FunctionProto, 'bind', bind); + lockMethod('call', call); + lockMethod('apply', apply); + lockMethod('bind', bind); + } + + // An accessor, not a non-writable data property, because of the override mistake: + // with a read-only data property on the prototype, `f.call = x` on an ordinary + // function silently no-ops in sloppy code and throws in strict code where node + // creates an own property, and `Function.prototype.call = x` in sloppy code + // no-ops silently instead of failing loud (both probed on node 26.13). The setter + // fixes both. No try/catch: the one call site runs on a fresh realm where these + // are configurable, so a throw here is a bootstrap defect, not one to swallow. + function lockMethod(name, original) { + defineProperty(FunctionProto, name, { + __proto__: null, + configurable: false, + enumerable: false, + get: function () { + return original; + }, + set: function (value) { + if (this === FunctionProto) { + throw new TypeErrorG( + "Cannot assign to read only property '" + + name + + "' of object 'function () { [native code] }'", + ); + } + // What a plain assignment creates on node. `__proto__: null` so an inherited + // Object.prototype.get/set cannot turn this into an invalid descriptor. + defineProperty(this, name, { + __proto__: null, + value: value, + writable: true, + enumerable: true, + configurable: true, + }); + }, + }); } module.exports = { diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 03f5191..99e6be4 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -29,13 +29,22 @@ ], "mutations": [ { - "name": "55-encoding-pollution V dies when the utf-8 decode return path goes back through .call", - "why": "A replaced Function.prototype.call substitutes decode()'s return value outright, so decode() hands back an ArrayBuffer instead of a string.", - "source": "pkg/runtime/js/internal/encoding.js", - "find": " function bufferToStringUtf8(buf) {\n return utf8DecodeNative(buf);\n }", - "replace": " function bufferToStringUtf8(buf) {\n return Buffer.prototype.toString.call(buf, 'utf8');\n }", - "gate": "compat:tests/node-compat/cases/55-encoding-pollution.js", - "expect_detail": "node: V " + "name": "function_call_lock dies when the loader stops locking Function.prototype.call/apply/bind", + "why": "Without the lock a replaced Function.prototype.call reaches every callerN primordial, so URLSearchParams.get, util.format, Buffer.from, new URL and TextDecoder break or answer wrongly where node is fine (#450).", + "source": "pkg/runtime/js/bootstrap/loader.js", + "find": " req('internal/primordials').lockIntrinsics();", + "replace": " req('internal/primordials');", + "gate": "odin:cmd/lava:main.function_call_lock", + "expect_detail": "want sloppy-call=THREW:TypeError got sloppy-call=assigned" + }, + { + "name": "function_call_lock dies when the lock is a non-writable data property instead of the accessor", + "why": "A read-only data property on Function.prototype hits the override mistake: f.call = x on an ordinary function throws in strict code where node defines an own property, and the sloppy assignment on Function.prototype no-ops silently.", + "source": "pkg/runtime/js/internal/primordials.js", + "find": " lockMethod('call', call);\n lockMethod('apply', apply);\n lockMethod('bind', bind);", + "replace": " defineProperty(FunctionProto, 'call', { writable: false, configurable: false });\n defineProperty(FunctionProto, 'apply', { writable: false, configurable: false });\n defineProperty(FunctionProto, 'bind', { writable: false, configurable: false });", + "gate": "odin:cmd/lava:main.function_call_lock", + "expect_detail": "got own-strict=THREW:TypeError" }, { "name": "55-encoding-pollution U dies when toBytes reads the view window live", diff --git a/tests/node-compat/cases/55-encoding-pollution.js b/tests/node-compat/cases/55-encoding-pollution.js index fbc5575..de62af3 100644 --- a/tests/node-compat/cases/55-encoding-pollution.js +++ b/tests/node-compat/cases/55-encoding-pollution.js @@ -9,8 +9,10 @@ // `new URL(...)` cases below are MODULE-BOUNDARY vectors: url.js was already // hardened, but a poisoned intrinsic still reached it through encoding.js. // -// H Function.prototype.apply -> String.fromCharCode.apply rewrote the decoded -// host: new URL('http://%C3%BCber.example/') became http://a/ +// H Function.prototype.apply -> retired: String.fromCharCode.apply rewrote the +// decoded host (new URL('http://%C3%BCber.example/') became http://a/), but +// apply is locked since #450, so the poison throws on Lava only. H1-H4 and Q2 +// moved to cmd/lava/function_call_lock_test.odin. // I String.fromCharCode -> same carrier, replaced statically // J Array.prototype.push -> the decoder's code-unit accumulator, on all // four decode paths and every flush/error site @@ -24,8 +26,8 @@ // Q the >0x2000 chunked unitsToString branch (ArrayPrototypeSlice+ReflectApply) // R Buffer.from / Buffer.prototype.toString / the String global underneath — // the encoding->buffer boundary, one layer below the url->encoding one -// V Function.prototype.call -> the borrow itself, so the replacement's return -// value became decode()'s (see V, at the end) +// V Function.prototype.call -> retired: locked since #450, so not oracle-able +// (see V, at the end) // W Ctor[Symbol.hasInstance] -> toBytes' three `instanceof` brands, forged in // both directions (see W, at the end) // @@ -67,9 +69,6 @@ function poison(target, key, value) { const RealU8 = Uint8Array; const U16_SMILE = [0x3d, 0xd8, 0x00, 0xde, 0x41, 0x00]; // 😀A -const APPLY_STUB = function () { - return 'a'; -}; // A >0x2000-code-unit input, so unitsToString takes its chunked branch. const BIG_LATIN = new Uint8Array(9000); @@ -78,43 +77,7 @@ function bigDigest(s) { return s.length + ':' + s.charCodeAt(0) + ':' + s.charCodeAt(8192) + ':' + s.charCodeAt(8999); } -// --- H: poisoned Function.prototype.apply, through url.js and directly ------- - -console.log( - 'H1', - underPollution( - ...poison(Function.prototype, 'apply', APPLY_STUB), - () => new URL('http://%C3%BCber.example/').href, - ), -); - -// The fatal decoder is what url.js percentDecodeHostStrict uses, and unlike the -// lenient one it never takes the native Buffer fast path. -console.log( - 'H2', - underPollution(...poison(Function.prototype, 'apply', APPLY_STUB), () => - JSON.stringify( - new TextDecoder('utf-8', { fatal: true }).decode( - new Uint8Array([0xc3, 0xbc, 0x62, 0x65, 0x72]), - ), - ), - ), -); - -// utf-16le never takes the native Buffer fast path either. -console.log( - 'H3', - underPollution(...poison(Function.prototype, 'apply', APPLY_STUB), () => - JSON.stringify(new TextDecoder('utf-16le').decode(new Uint8Array(U16_SMILE))), - ), -); - -console.log( - 'H4', - underPollution(...poison(Function.prototype, 'apply', APPLY_STUB), () => - Array.from(new TextEncoder().encode('ü€😀')).join(','), - ), -); +// --- H: retired (see the header); Function.prototype.apply is locked -------- // --- I: replaced String.fromCharCode ---------------------------------------- @@ -417,12 +380,7 @@ console.log( console.log('Q1', bigDigest(new TextDecoder('windows-1252').decode(BIG_LATIN))); -console.log( - 'Q2', - underPollution(...poison(Function.prototype, 'apply', APPLY_STUB), () => - bigDigest(new TextDecoder('windows-1252').decode(BIG_LATIN)), - ), -); +// Q2 (a poisoned Function.prototype.apply on this branch) retired with H. console.log( 'Q3', @@ -617,50 +575,13 @@ console.log( })(), ); -// V: `Function.prototype.call` replaced — the RETURN path of decode(), which is -// the sharpest shape in this whole file. The other vectors forge an intermediate -// and the damage is arithmetic; here the replacement's return value simply became -// decode()'s, so `decode()` handed back an ArrayBuffer instead of a string. It -// reached the utf-8 fast path through `Buffer.prototype.toString.call(bytes, -// 'utf8')` and the utf-16le path through the same borrow, and neither -// captured-method nor captured-getter hardening touches it: the method was -// already pristine, `.call` is the live read. Fixed by taking the codec natives -// (`utf8Decode`/`utf16leDecode`) straight from the loader's native argument, so no -// `Function.prototype` read is left on the path at all. -// -// The decoders are built BEFORE the poison so this measures decode, not -// construction: label normalization still routes through `.call`-based -// primordials, so `new TextDecoder()` under this poison throws RangeError under -// Lava where node succeeds. That residual is the whole `invoke` class, it is -// tracked in ROADMAP against lockIntrinsics(), and it is deliberately NOT part of -// this case — a case that asserted it would have to assert the divergence. -console.log( - 'V', - (() => { - const d8 = new TextDecoder(); - const d16 = new TextDecoder('utf-16le'); - const utf8 = new Uint8Array([0x68, 0x69]); // "hi" - const utf16 = new Uint8Array([0x41, 0x00, 0x42, 0x00]); // "AB" - const real = Function.prototype.call; - // Nothing between the assignment and the restore may itself use `.call`, so - // the results are collected raw and only inspected after `.call` is back. - Function.prototype.call = function () { - return new ArrayBuffer(8); - }; - let a, b, threw; - try { - a = d8.decode(utf8); - b = d16.decode(utf16); - } catch (e) { - threw = e; - } - Function.prototype.call = real; - if (threw !== undefined) return 'THREW:' + threw.name; - // typeof is asserted explicitly: an ArrayBuffer stringifies to something - // harmless-looking in a template, so comparing text alone would pass. - return [typeof a, JSON.stringify(a), typeof b, JSON.stringify(b)].join('|'); - })(), -); +// V (retired): `Function.prototype.call` replaced, which put the replacement's +// return value on decode()'s RETURN path (decode() handed back an ArrayBuffer). +// encoding.js now calls the codec natives directly, and since #450 Lava also locks +// call/apply/bind before user code, so the assignment throws on Lava where node +// accepts it — a deliberate deviation an oracle cannot print. The lock, and +// decode() surviving the attempt, are pinned Lava-only in +// cmd/lava/function_call_lock_test.odin. // W: the BRANDS, not the window. `toBytes` routed on `input instanceof // Uint8Array/ArrayBuffer/DataView`, and every one of those dispatches through diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index 47a9016..ef4e1ae 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -183,7 +183,7 @@ "method": 0, "invoke": 9, "accessor": 0, - "global": 1 + "global": 0 }, "internal/punycode.js": { "method": 1, diff --git a/tests/runtime/eventloop/cases/11-scheduler-hardening.js b/tests/runtime/eventloop/cases/11-scheduler-hardening.js index a59e16d..d612deb 100644 --- a/tests/runtime/eventloop/cases/11-scheduler-hardening.js +++ b/tests/runtime/eventloop/cases/11-scheduler-hardening.js @@ -1,6 +1,6 @@ // The scheduler must keep working after userland mutates common intrinsics. // process.nextTick/queueMicrotask are runtime primitives; they should not depend -// on the live Array/Promise/Function prototype methods once installed. +// on the live Array/Promise prototype methods once installed. (function () { let hits = 0; @@ -19,12 +19,10 @@ Promise.prototype.then = function () { throw new Error('then poisoned'); }; - Function.prototype.call = function () { - throw new Error('call poisoned'); - }; - Function.prototype.apply = function () { - throw new Error('apply poisoned'); - }; + // Function.prototype.call/apply used to be poisoned here too. Lava locks them + // before user code since #450 (node does not), so that assignment throws on Lava + // only; the scheduler-after-attempted-replacement check moved to the Lava-only + // cmd/lava/function_call_lock_test.odin. process.nextTick( (a, b) => { From f7079367d47d2078db88a05968b36518061887f8 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:58:49 +0200 Subject: [PATCH 47/61] fix(scripts): move the graph check to lib so the CLI runs unguarded check-js-graph.mjs exported checkJsGraph for its test and ran the report only behind `if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1])`. Node realpaths the module URL but not argv[1], so run through a symlink the guard failed open: nothing printed, exit 0. No test noticed the guard's removal either. The rules now live in scripts/lib/js-graph-check.mjs, which the test imports; the CLI is a guard-free script that always reports. A new node:test case spawns the CLI on the real tree, directly and through a symlink, and requires the OK line. Same defect and fix shape as the primordials ratchet (2579be6, efa53bc). Mutation manifest: the five entries whose find moved are repointed to the lib file; two new entries silence the CLI before the check (killed by the direct arm) and put the shipped guard back (killed only by the symlink arm). Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.mjs | 132 ++++---------------------------- scripts/check-js-graph.test.mjs | 45 +++++++++-- scripts/lib/js-graph-check.mjs | 120 +++++++++++++++++++++++++++++ tests/mutation-manifest.json | 30 ++++++-- 4 files changed, 196 insertions(+), 131 deletions(-) create mode 100644 scripts/lib/js-graph-check.mjs diff --git a/scripts/check-js-graph.mjs b/scripts/check-js-graph.mjs index 47eec17..1df15b9 100644 --- a/scripts/check-js-graph.mjs +++ b/scripts/check-js-graph.mjs @@ -28,127 +28,21 @@ // to, `(function (require, module, exports, native) { … })`, so a require is // top-level when its nearest enclosing function is that outermost one. // +// The rules live in lib/js-graph-check.mjs so the test can run them over fixture +// trees. This file is only the CLI and deliberately has no "am I the entry point?" +// guard: one that compared argv[1] with import.meta.url failed open under a symlink. +// // Usage: node scripts/check-js-graph.mjs (exit 1 on any violation) -import { readFileSync, readdirSync } from 'node:fs'; -import { join, relative } from 'node:path'; -import { fileURLToPath } from 'node:url'; import { ROOT } from './lib/primordials-detect.mjs'; -import { MODULE_DIRS, moduleGraph, posix } from './lib/js-graph.mjs'; - -/** - * Checks the embedded JS module graph rooted at `root` (a repo checkout, or a fixture - * tree with the same pkg/runtime layout). - * @param {string} root - * @returns {{rule: string, where: string, msg: string}[]} - */ -export function checkJsGraph(root) { - const runtimeDir = join(root, 'pkg', 'runtime'); - const violations = []; - const add = (rule, where, msg) => violations.push({ rule, where, msg }); - const relJs = (file) => posix(relative(runtimeDir, file)); - - // --- embedding: what the Odin sources pull in --------------------------------- - const loadDirs = new Set(); - const loadFiles = new Set(); - const nativeKeys = []; - const odinFiles = readdirSync(runtimeDir).filter((n) => n.endsWith('.odin')); - for (const name of odinFiles) { - const src = readFileSync(join(runtimeDir, name), 'utf8'); - for (const m of src.matchAll(/#load_directory\(\s*"([^"]+)"/g)) { - const dir = m[1].replace(/\/+$/, ''); - loadDirs.add(dir); - const inside = dir.slice('js/'.length); - if (!MODULE_DIRS.some((d) => inside === d || inside.startsWith(d + '/'))) { - add( - 'unembedded', - `pkg/runtime/${name}`, - `#load_directory("${dir}") is outside js/node and js/internal`, - ); - } - } - for (const m of src.matchAll(/#load\(\s*"([^"]+\.js)"/g)) loadFiles.add(m[1]); - for (const m of src.matchAll(/set_named\(ctx, natives, "([^"]+)"/g)) { - nativeKeys.push({ id: m[1], where: `pkg/runtime/${name}` }); - } - } - // The parser and module table are shared with the pollution ratchet (lib/js-graph.mjs), - // which reads the same edges to decide which modules run before user code. - const graph = moduleGraph(root); - const { modules, allJs } = graph; - for (const file of allJs) { - const rel = relJs(file); - const dir = rel.slice(0, rel.lastIndexOf('/')); - if (!loadDirs.has(dir) && !loadFiles.has(rel)) { - add( - 'unembedded', - `pkg/runtime/${rel}`, - `no #load_directory("${dir}") or #load("${rel}") embeds it`, - ); - } - } - - // --- the module table: dynamic and unresolved requires found while building it --- - violations.push(...graph.violations); - const where = (mod, line) => `pkg/runtime/${relJs(mod.file)}:${line}`; - - // --- roots: every public module, plus whatever the loader asks for ------------- - const roots = [...modules.keys()].filter((id) => id.startsWith('node/')); - roots.push(...graph.loaderIds); - for (const k of nativeKeys) { - if (!modules.has(k.id)) add('unresolved', k.where, `natives key "${k.id}" names no module`); - } - - const seen = new Set(); - const stack = [...roots]; - while (stack.length > 0) { - const id = stack.pop(); - if (seen.has(id)) continue; - seen.add(id); - for (const e of modules.get(id).edges) stack.push(e.id); - } - for (const [id, mod] of modules) { - if (!seen.has(id)) - add('unreachable', `pkg/runtime/${relJs(mod.file)}`, `${id} is required by nothing`); - } - - // --- cycles through module-eval requires -------------------------------------- - const WHITE = 0; - const GREY = 1; - const BLACK = 2; - const color = new Map([...modules.keys()].map((id) => [id, WHITE])); - const path = []; - const reported = new Set(); - const visit = (id) => { - color.set(id, GREY); - path.push(id); - for (const e of modules.get(id).edges) { - if (!e.topLevel) continue; - if (color.get(e.id) === GREY) { - const cycle = [...path.slice(path.indexOf(e.id)), e.id]; - const key = [...cycle.slice(0, -1)].sort().join(','); - if (!reported.has(key)) { - reported.add(key); - add('cycle', where(modules.get(id), e.line), cycle.join(' -> ')); - } - } else if (color.get(e.id) === WHITE) visit(e.id); - } - path.pop(); - color.set(id, BLACK); - }; - for (const id of modules.keys()) if (color.get(id) === WHITE) visit(id); - - return violations; -} +import { checkJsGraph } from './lib/js-graph-check.mjs'; -if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) { - const violations = checkJsGraph(ROOT); - if (violations.length > 0) { - console.error(`JS module graph: ${violations.length} violation(s):`); - for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`); - process.exit(1); - } - console.log( - 'OK: embedded JS module graph is literal, resolved, reachable, embedded and acyclic at module-eval.', - ); +const violations = checkJsGraph(ROOT); +if (violations.length > 0) { + console.error(`JS module graph: ${violations.length} violation(s):`); + for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`); + process.exit(1); } +console.log( + 'OK: embedded JS module graph is literal, resolved, reachable, embedded and acyclic at module-eval.', +); diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 0d739b8..669c459 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -1,17 +1,19 @@ -// node:test view of scripts/check-js-graph.mjs: one fixture tree per rule, each a -// minimal pkg/runtime layout that is clean except for the ONE defect the rule exists -// to catch. The clean fixture runs first, so a rule that fires on everything cannot -// pass by accident, and every failing fixture asserts the exact rule set — a fixture -// that also tripped a second rule would be testing its own scaffolding. +// node:test view of scripts/check-js-graph.mjs, whose rules live in +// lib/js-graph-check.mjs: one fixture tree per rule, each a minimal pkg/runtime layout +// that is clean except for the ONE defect the rule exists to catch. The clean fixture +// runs first, so a rule that fires on everything cannot pass by accident, and every +// failing fixture asserts the exact rule set — a fixture that also tripped a second +// rule would be testing its own scaffolding. // // Run: node --test scripts/check-js-graph.test.mjs (or `make test-scripts`) import test from 'node:test'; import assert from 'node:assert/strict'; -import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { spawnSync } from 'node:child_process'; +import { mkdtempSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; -import { checkJsGraph } from './check-js-graph.mjs'; +import { checkJsGraph } from './lib/js-graph-check.mjs'; import { eagerModules } from './lib/js-graph.mjs'; import { ROOT } from './lib/primordials-detect.mjs'; @@ -84,6 +86,35 @@ test('the real tree is clean', () => { assert.deepEqual(checkJsGraph(ROOT), []); }); +// The CLI itself must report. It once sat behind an "am I the entry point?" guard so +// this file could import checkJsGraph, and that guard failed OPEN: run through a +// symlink, argv[1] is the link while import.meta.url is the realpath, the body never +// ran, and the gate exited 0 having printed nothing. The rules now live in lib/ and +// the CLI has no guard; this pins both the plain invocation and the symlinked one, +// since an exit code alone cannot tell "checked and passed" from "never checked". +test('graph CLI reports OK on the real tree, also when invoked through a symlink', () => { + const cli = join(import.meta.dirname, 'check-js-graph.mjs'); + const dir = mkdtempSync(join(tmpdir(), 'js-graph-cli-')); + try { + const link = join(dir, 'check-js-graph.mjs'); + symlinkSync(cli, link); + for (const [how, path] of [ + ['direct path', cli], + ['symlink', link], + ]) { + const r = spawnSync(process.execPath, [path], { encoding: 'utf8' }); + assert.equal(r.status, 0, `graph CLI (${how}) exited ${r.status}: ${r.stderr}`); + assert.match( + r.stdout, + /^OK: embedded JS module graph is literal/m, + `graph CLI (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, + ); + } + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + test('dynamic: a non-literal require argument', () => { const vs = rules({ 'pkg/runtime/js/node/buffer.js': diff --git a/scripts/lib/js-graph-check.mjs b/scripts/lib/js-graph-check.mjs new file mode 100644 index 0000000..d4814bc --- /dev/null +++ b/scripts/lib/js-graph-check.mjs @@ -0,0 +1,120 @@ +// Rule set of the embedded JS module-graph check (scripts/check-js-graph.mjs documents +// the rules). The parser and module table it reads are shared with the pollution +// ratchet and live in js-graph.mjs; this file is the graph CHECK's own consumer of +// them, kept apart so js-graph.mjs stays the one neutral builder both gates import. +// +// Pulled out of the CLI for the same reason primordials-scan.mjs was: the test needs +// checkJsGraph over fixture trees, and the CLI used to export it behind an "am I the +// entry point?" guard comparing argv[1] with import.meta.url. Node realpaths the +// module URL but not argv[1], so invoked through a symlink the guard skipped the whole +// report and the gate exited 0 having printed nothing. The CLI stays guard-free and +// the importable part lives here. + +import { readFileSync, readdirSync } from 'node:fs'; +import { join, relative } from 'node:path'; +import { MODULE_DIRS, moduleGraph, posix } from './js-graph.mjs'; + +/** + * Checks the embedded JS module graph rooted at `root` (a repo checkout, or a fixture + * tree with the same pkg/runtime layout). + * @param {string} root + * @returns {{rule: string, where: string, msg: string}[]} + */ +export function checkJsGraph(root) { + const runtimeDir = join(root, 'pkg', 'runtime'); + const violations = []; + const add = (rule, where, msg) => violations.push({ rule, where, msg }); + const relJs = (file) => posix(relative(runtimeDir, file)); + + // --- embedding: what the Odin sources pull in --------------------------------- + const loadDirs = new Set(); + const loadFiles = new Set(); + const nativeKeys = []; + const odinFiles = readdirSync(runtimeDir).filter((n) => n.endsWith('.odin')); + for (const name of odinFiles) { + const src = readFileSync(join(runtimeDir, name), 'utf8'); + for (const m of src.matchAll(/#load_directory\(\s*"([^"]+)"/g)) { + const dir = m[1].replace(/\/+$/, ''); + loadDirs.add(dir); + const inside = dir.slice('js/'.length); + if (!MODULE_DIRS.some((d) => inside === d || inside.startsWith(d + '/'))) { + add( + 'unembedded', + `pkg/runtime/${name}`, + `#load_directory("${dir}") is outside js/node and js/internal`, + ); + } + } + for (const m of src.matchAll(/#load\(\s*"([^"]+\.js)"/g)) loadFiles.add(m[1]); + for (const m of src.matchAll(/set_named\(ctx, natives, "([^"]+)"/g)) { + nativeKeys.push({ id: m[1], where: `pkg/runtime/${name}` }); + } + } + // The parser and module table are shared with the pollution ratchet (lib/js-graph.mjs), + // which reads the same edges to decide which modules run before user code. + const graph = moduleGraph(root); + const { modules, allJs } = graph; + for (const file of allJs) { + const rel = relJs(file); + const dir = rel.slice(0, rel.lastIndexOf('/')); + if (!loadDirs.has(dir) && !loadFiles.has(rel)) { + add( + 'unembedded', + `pkg/runtime/${rel}`, + `no #load_directory("${dir}") or #load("${rel}") embeds it`, + ); + } + } + + // --- the module table: dynamic and unresolved requires found while building it --- + violations.push(...graph.violations); + const where = (mod, line) => `pkg/runtime/${relJs(mod.file)}:${line}`; + + // --- roots: every public module, plus whatever the loader asks for ------------- + const roots = [...modules.keys()].filter((id) => id.startsWith('node/')); + roots.push(...graph.loaderIds); + for (const k of nativeKeys) { + if (!modules.has(k.id)) add('unresolved', k.where, `natives key "${k.id}" names no module`); + } + + const seen = new Set(); + const stack = [...roots]; + while (stack.length > 0) { + const id = stack.pop(); + if (seen.has(id)) continue; + seen.add(id); + for (const e of modules.get(id).edges) stack.push(e.id); + } + for (const [id, mod] of modules) { + if (!seen.has(id)) + add('unreachable', `pkg/runtime/${relJs(mod.file)}`, `${id} is required by nothing`); + } + + // --- cycles through module-eval requires -------------------------------------- + const WHITE = 0; + const GREY = 1; + const BLACK = 2; + const color = new Map([...modules.keys()].map((id) => [id, WHITE])); + const path = []; + const reported = new Set(); + const visit = (id) => { + color.set(id, GREY); + path.push(id); + for (const e of modules.get(id).edges) { + if (!e.topLevel) continue; + if (color.get(e.id) === GREY) { + const cycle = [...path.slice(path.indexOf(e.id)), e.id]; + const key = [...cycle.slice(0, -1)].sort().join(','); + if (!reported.has(key)) { + reported.add(key); + add('cycle', where(modules.get(id), e.line), cycle.join(' -> ')); + } + } else if (color.get(e.id) === WHITE) visit(e.id); + } + path.pop(); + color.set(id, BLACK); + }; + for (const id of modules.keys()) if (color.get(id) === WHITE) visit(id); + + return violations; +} diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 03f5191..fa36669 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1961,7 +1961,7 @@ { "name": "check-js-graph fixtures die when natives keys are not resolved", "why": "natives is keyed by module id; a key left on the old spelling (\"buffer\") hands the module undefined and the factory fails closed or worse.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph-check.mjs", "find": "/set_named\\(ctx, natives, \"([^\"]+)\"/g", "replace": "/NEVER_MATCH_([^\"]+)/g", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -1971,7 +1971,7 @@ { "name": "check-js-graph fixtures die when unreachable modules pass", "why": "A file nothing reaches is dead code that still ships and still gets evaluated at every context init.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph-check.mjs", "find": "add('unreachable',", "replace": "if (false) add('unreachable',", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -1981,7 +1981,7 @@ { "name": "check-js-graph fixtures die when an unembedded directory passes", "why": "#load_directory is not recursive, so a new subdirectory under node/ silently never ships without its own row.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph-check.mjs", "find": "if (!loadDirs.has(dir) && !loadFiles.has(rel)) {", "replace": "if (false) {", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -1991,7 +1991,7 @@ { "name": "check-js-graph fixtures die when #load_directory may read bootstrap/", "why": "esm.js is kept off the module table on purpose (CLAUDE.md §5); a #load_directory over bootstrap/ would hand it to the loader as a factory.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph-check.mjs", "find": "if (!MODULE_DIRS.some((d) => inside === d || inside.startsWith(d + '/'))) {", "replace": "if (false) {", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -2001,7 +2001,7 @@ { "name": "check-js-graph fixtures die when module-eval cycles pass", "why": "A module-eval cycle hands one side the other's PARTIAL exports, which reads as undefined members at run time.", - "source": "scripts/check-js-graph.mjs", + "source": "scripts/lib/js-graph-check.mjs", "find": "add('cycle', where(modules.get(id), e.line), cycle.join(' -> '));", "replace": "", "gate": "node-test:scripts/check-js-graph.test.mjs", @@ -2018,6 +2018,26 @@ "test": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)", "expect_detail": "clean fixture has no violations (late-bound stream <-> stream/web is not a cycle)" }, + { + "name": "the graph CLI dies when it exits 0 without reporting", + "why": "An exit code alone cannot tell a checked tree from an unchecked one: an entry-point guard once made the CLI skip its whole body when invoked through a symlink and exit 0 silently. Silencing the CLI ahead of the check must fail on the missing OK line.", + "source": "scripts/check-js-graph.mjs", + "find": "const violations = checkJsGraph(ROOT);", + "replace": "process.exit(0);\nconst violations = checkJsGraph(ROOT);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI (direct path) did not print its OK line" + }, + { + "name": "the graph CLI dies when an entry-point guard skips it under a symlink", + "why": "The CLI shipped `if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1])` around its report, which fails OPEN: run through a symlink argv[1] is the link, the body never runs and the gate exits 0 silently. A direct invocation still passes that guard, so only the symlink arm of the CLI spawn test sees it.", + "source": "scripts/check-js-graph.mjs", + "find": "const violations = checkJsGraph(ROOT);", + "replace": "if (process.argv[1] !== new URL(import.meta.url).pathname) process.exit(0);\nconst violations = checkJsGraph(ROOT);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI (symlink) did not print its OK line" + }, { "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", From 19a86920b78a68358c459a71a930771a20ef43f5 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:04:19 +0200 Subject: [PATCH 48/61] test(scripts): pin the graph CLI's failing branch on a one-defect tree The CLI spawn test only ran on the clean real tree, so a CLI that exited 0 on violations, printed OK without checking, or dropped its violation lines passed it. The CLI now takes an optional root (for the test only), and a new case runs it over the natives-key fixture: no OK line, exit 1, the violation header and the rule's line on stderr. Both mutants are recorded with distinct details. The recorded entry-point guard mutation now decodes import.meta.url's pathname, so a checkout path with a space still trips only the symlink arm. Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.mjs | 8 ++++++-- scripts/check-js-graph.test.mjs | 29 +++++++++++++++++++++++++++++ tests/mutation-manifest.json | 28 ++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/scripts/check-js-graph.mjs b/scripts/check-js-graph.mjs index 1df15b9..6064de5 100644 --- a/scripts/check-js-graph.mjs +++ b/scripts/check-js-graph.mjs @@ -32,12 +32,16 @@ // trees. This file is only the CLI and deliberately has no "am I the entry point?" // guard: one that compared argv[1] with import.meta.url failed open under a symlink. // -// Usage: node scripts/check-js-graph.mjs (exit 1 on any violation) +// Usage: node scripts/check-js-graph.mjs [root] (exit 1 on any violation) +import { resolve } from 'node:path'; import { ROOT } from './lib/primordials-detect.mjs'; import { checkJsGraph } from './lib/js-graph-check.mjs'; -const violations = checkJsGraph(ROOT); +// `root` exists for the test, which runs the CLI over a one-defect fixture tree to pin +// the failing exit, header and violation lines; every real caller passes nothing. +const root = process.argv[2] ? resolve(process.argv[2]) : ROOT; +const violations = checkJsGraph(root); if (violations.length > 0) { console.error(`JS module graph: ${violations.length} violation(s):`); for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`); diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 669c459..15fe197 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -115,6 +115,35 @@ test('graph CLI reports OK on the real tree, also when invoked through a symlink } }); +// The other half of the CLI contract: on a violating tree it must FAIL, and say why. +// Run only on the clean real tree, a CLI that exited 0 on violations, printed OK +// without checking, or dropped the violation lines would all have passed above. The +// CLI takes an optional root for exactly this; the fixture is the one-defect natives +// tree the rule test below uses. +test('graph CLI fails on a violating tree and names the violation', () => { + const root = tree({ 'pkg/runtime/globals.odin': ODIN.replace('"node/buffer"', '"buffer"') }); + try { + const cli = join(import.meta.dirname, 'check-js-graph.mjs'); + const r = spawnSync(process.execPath, [cli, root], { encoding: 'utf8' }); + // OK first: a CLI that skips the check also exits 0, and this keeps the two + // mutants (wrong exit code, result discarded) reporting distinct failures. + assert.doesNotMatch(r.stdout, /^OK:/m, `graph CLI printed OK on a violating tree: ${r.stdout}`); + assert.equal(r.status, 1, `graph CLI exited ${r.status} on a violating tree: ${r.stderr}`); + assert.match( + r.stderr, + /^JS module graph: 1 violation\(s\):$/m, + `graph CLI header: ${r.stderr}`, + ); + assert.match( + r.stderr, + /^ unresolved: pkg\/runtime\/globals\.odin natives key "buffer" names no module$/m, + `graph CLI violation line: ${r.stderr}`, + ); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + test('dynamic: a non-literal require argument', () => { const vs = rules({ 'pkg/runtime/js/node/buffer.js': diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index fa36669..9b97994 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2022,8 +2022,8 @@ "name": "the graph CLI dies when it exits 0 without reporting", "why": "An exit code alone cannot tell a checked tree from an unchecked one: an entry-point guard once made the CLI skip its whole body when invoked through a symlink and exit 0 silently. Silencing the CLI ahead of the check must fail on the missing OK line.", "source": "scripts/check-js-graph.mjs", - "find": "const violations = checkJsGraph(ROOT);", - "replace": "process.exit(0);\nconst violations = checkJsGraph(ROOT);", + "find": "const violations = checkJsGraph(root);", + "replace": "process.exit(0);\nconst violations = checkJsGraph(root);", "gate": "node-test:scripts/check-js-graph.test.mjs", "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", "expect_detail": "graph CLI (direct path) did not print its OK line" @@ -2032,12 +2032,32 @@ "name": "the graph CLI dies when an entry-point guard skips it under a symlink", "why": "The CLI shipped `if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1])` around its report, which fails OPEN: run through a symlink argv[1] is the link, the body never runs and the gate exits 0 silently. A direct invocation still passes that guard, so only the symlink arm of the CLI spawn test sees it.", "source": "scripts/check-js-graph.mjs", - "find": "const violations = checkJsGraph(ROOT);", - "replace": "if (process.argv[1] !== new URL(import.meta.url).pathname) process.exit(0);\nconst violations = checkJsGraph(ROOT);", + "find": "const violations = checkJsGraph(root);", + "replace": "if (process.argv[1] !== decodeURIComponent(new URL(import.meta.url).pathname)) process.exit(0);\nconst violations = checkJsGraph(root);", "gate": "node-test:scripts/check-js-graph.test.mjs", "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", "expect_detail": "graph CLI (symlink) did not print its OK line" }, + { + "name": "the graph CLI dies when it exits 0 on a violating tree", + "why": "A gate that prints its violations and still exits 0 is green in CI. The real tree is clean, so only the spawn over a one-defect fixture sees the exit code of the failing branch.", + "source": "scripts/check-js-graph.mjs", + "find": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(1);", + "replace": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(0);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI fails on a violating tree and names the violation", + "expect_detail": "graph CLI exited 0 on a violating tree" + }, + { + "name": "the graph CLI dies when it prints OK without checking", + "why": "Discarding the check's result prints the OK line on every tree. On the clean real tree that is indistinguishable from a pass; only the violating fixture shows the OK line where a failure belongs.", + "source": "scripts/check-js-graph.mjs", + "find": "const violations = checkJsGraph(root);", + "replace": "const violations = [];", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI fails on a violating tree and names the violation", + "expect_detail": "graph CLI printed OK on a violating tree" + }, { "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", From 19dd2ade29bea0cadaf004807e56b8635943780f Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:07:58 +0200 Subject: [PATCH 49/61] docs(scripts): name lib/js-graph-check.mjs as the graph rules' home Since f707936 the graph rules live in scripts/lib/js-graph-check.mjs and scripts/check-js-graph.mjs only reports. The js-graph.mjs header and ARCHITECTURE.md still named the CLI as the parser's consumer. Co-Authored-By: Claude Opus 5.5 --- docs/ARCHITECTURE.md | 5 +++-- scripts/lib/js-graph.mjs | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index f09b9f8..bcb3173 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -530,8 +530,9 @@ pollutable site, each baselined separately in A `global` read at module-eval is exempt only in a module instantiated before user code — the loader's `req()` roots closed over top-level requires, computed by -`scripts/lib/js-graph.mjs` (the parser `check-js-graph.mjs` also uses). In a lazy -module the factory runs on the user's first `require()`, so its capture counts (#333). +`scripts/lib/js-graph.mjs` (the parser the graph rules in `lib/js-graph-check.mjs` +also use). In a lazy module the factory runs on the user's first `require()`, so its +capture counts (#333). `cmd/lava/lazy_builtins_test.odin` checks that set against what context init compiles. `make check-primordials` (part of `make check-js`) fails on any per-class diff --git a/scripts/lib/js-graph.mjs b/scripts/lib/js-graph.mjs index c942455..ff81708 100644 --- a/scripts/lib/js-graph.mjs +++ b/scripts/lib/js-graph.mjs @@ -1,6 +1,7 @@ // The embedded runtime JS module graph — ONE parser and graph builder, two consumers: // -// scripts/check-js-graph.mjs literal, resolved, reachable, embedded, acyclic +// scripts/lib/js-graph-check.mjs literal, resolved, reachable, embedded, acyclic +// (reported by the scripts/check-js-graph.mjs CLI) // scripts/check-primordials.mjs which modules are EAGER (instantiated before user // code), because only there is a module-eval // capture pristine (CLAUDE.md §5) From 0784f7a02cd5f5042f8086579f6b2ebbbc706be5 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:10:56 +0200 Subject: [PATCH 50/61] test(scripts): run the graph CLI spawn tests under bun as well as node The CLI spawn tests ran only under node, but the real gate runs the CLI under bun (make check-js -> js:graph -> `bun run scripts/check-js-graph.mjs`). Each CLI case (clean tree direct and through a symlink, and the one-defect fixture through the optional root) now runs once per runtime, named [node] / [bun]. bun comes from PATH and is required: a missing bun fails the bun arms with a clear message instead of skipping them. Mutation manifest: the four CLI entries now target the [node] test names and details; two new entries pin the [bun] arms (exit 0 before reporting, exit 0 on a violating tree). Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.test.mjs | 142 ++++++++++++++++++++------------ tests/mutation-manifest.json | 36 ++++++-- 2 files changed, 116 insertions(+), 62 deletions(-) diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 15fe197..33778ad 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -86,63 +86,97 @@ test('the real tree is clean', () => { assert.deepEqual(checkJsGraph(ROOT), []); }); -// The CLI itself must report. It once sat behind an "am I the entry point?" guard so -// this file could import checkJsGraph, and that guard failed OPEN: run through a -// symlink, argv[1] is the link while import.meta.url is the realpath, the body never -// ran, and the gate exited 0 having printed nothing. The rules now live in lib/ and -// the CLI has no guard; this pins both the plain invocation and the symlinked one, -// since an exit code alone cannot tell "checked and passed" from "never checked". -test('graph CLI reports OK on the real tree, also when invoked through a symlink', () => { - const cli = join(import.meta.dirname, 'check-js-graph.mjs'); - const dir = mkdtempSync(join(tmpdir(), 'js-graph-cli-')); - try { - const link = join(dir, 'check-js-graph.mjs'); - symlinkSync(cli, link); - for (const [how, path] of [ - ['direct path', cli], - ['symlink', link], - ]) { - const r = spawnSync(process.execPath, [path], { encoding: 'utf8' }); - assert.equal(r.status, 0, `graph CLI (${how}) exited ${r.status}: ${r.stderr}`); - assert.match( - r.stdout, - /^OK: embedded JS module graph is literal/m, - `graph CLI (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, +// The CLI runs under two runtimes: the tests below spawn it with this node, and the +// real gate runs it under bun (make check-js -> package.json js:graph -> `bun run +// scripts/check-js-graph.mjs`). Bun's argv handling (the optional root, a symlinked +// script path) is its own, so every CLI case runs once per runtime. bun is resolved +// from PATH and is NOT optional: make check-js already requires it, and a skipped bun +// arm would read as a pass of the gate's real runtime. +const CLI = join(import.meta.dirname, 'check-js-graph.mjs'); +const RUNTIMES = [ + ['node', () => process.execPath], + [ + 'bun', + () => { + const r = spawnSync('bun', ['--version'], { encoding: 'utf8' }); + assert.ok( + !r.error && r.status === 0, + `bun not runnable from PATH (${r.error?.code ?? `exit ${r.status}`}); make check-js runs the graph CLI under bun, so it is required here`, ); + return 'bun'; + }, + ], +]; + +for (const [rt, exe] of RUNTIMES) { + // The CLI itself must report. It once sat behind an "am I the entry point?" guard so + // this file could import checkJsGraph, and that guard failed OPEN: run through a + // symlink under node, argv[1] is the link while import.meta.url is the realpath, the + // body never ran, and the gate exited 0 having printed nothing. The rules now live in + // lib/ and the CLI has no guard; this pins both the plain invocation and the symlinked + // one, since an exit code alone cannot tell "checked and passed" from "never checked". + // (bun realpaths argv[1] as well, so that guard would have held under bun; the bun + // arm still pins that the gate's real runtime reaches the OK line both ways.) + test(`graph CLI [${rt}] reports OK on the real tree, also when invoked through a symlink`, () => { + const bin = exe(); + const dir = mkdtempSync(join(tmpdir(), 'js-graph-cli-')); + try { + const link = join(dir, 'check-js-graph.mjs'); + symlinkSync(CLI, link); + for (const [how, path] of [ + ['direct path', CLI], + ['symlink', link], + ]) { + const r = spawnSync(bin, [path], { encoding: 'utf8' }); + assert.equal(r.status, 0, `graph CLI [${rt}] (${how}) exited ${r.status}: ${r.stderr}`); + assert.match( + r.stdout, + /^OK: embedded JS module graph is literal/m, + `graph CLI [${rt}] (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, + ); + } + } finally { + rmSync(dir, { recursive: true, force: true }); } - } finally { - rmSync(dir, { recursive: true, force: true }); - } -}); + }); -// The other half of the CLI contract: on a violating tree it must FAIL, and say why. -// Run only on the clean real tree, a CLI that exited 0 on violations, printed OK -// without checking, or dropped the violation lines would all have passed above. The -// CLI takes an optional root for exactly this; the fixture is the one-defect natives -// tree the rule test below uses. -test('graph CLI fails on a violating tree and names the violation', () => { - const root = tree({ 'pkg/runtime/globals.odin': ODIN.replace('"node/buffer"', '"buffer"') }); - try { - const cli = join(import.meta.dirname, 'check-js-graph.mjs'); - const r = spawnSync(process.execPath, [cli, root], { encoding: 'utf8' }); - // OK first: a CLI that skips the check also exits 0, and this keeps the two - // mutants (wrong exit code, result discarded) reporting distinct failures. - assert.doesNotMatch(r.stdout, /^OK:/m, `graph CLI printed OK on a violating tree: ${r.stdout}`); - assert.equal(r.status, 1, `graph CLI exited ${r.status} on a violating tree: ${r.stderr}`); - assert.match( - r.stderr, - /^JS module graph: 1 violation\(s\):$/m, - `graph CLI header: ${r.stderr}`, - ); - assert.match( - r.stderr, - /^ unresolved: pkg\/runtime\/globals\.odin natives key "buffer" names no module$/m, - `graph CLI violation line: ${r.stderr}`, - ); - } finally { - rmSync(root, { recursive: true, force: true }); - } -}); + // The other half of the CLI contract: on a violating tree it must FAIL, and say why. + // Run only on the clean real tree, a CLI that exited 0 on violations, printed OK + // without checking, or dropped the violation lines would all have passed above. The + // CLI takes an optional root for exactly this; the fixture is the one-defect natives + // tree the rule test below uses. + test(`graph CLI [${rt}] fails on a violating tree and names the violation`, () => { + const bin = exe(); + const root = tree({ 'pkg/runtime/globals.odin': ODIN.replace('"node/buffer"', '"buffer"') }); + try { + const r = spawnSync(bin, [CLI, root], { encoding: 'utf8' }); + // OK first: a CLI that skips the check also exits 0, and this keeps the two + // mutants (wrong exit code, result discarded) reporting distinct failures. + assert.doesNotMatch( + r.stdout, + /^OK:/m, + `graph CLI [${rt}] printed OK on a violating tree: ${r.stdout}`, + ); + assert.equal( + r.status, + 1, + `graph CLI [${rt}] exited ${r.status} on a violating tree: ${r.stderr}`, + ); + assert.match( + r.stderr, + /^JS module graph: 1 violation\(s\):$/m, + `graph CLI [${rt}] header: ${r.stderr}`, + ); + assert.match( + r.stderr, + /^ unresolved: pkg\/runtime\/globals\.odin natives key "buffer" names no module$/m, + `graph CLI [${rt}] violation line: ${r.stderr}`, + ); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); +} test('dynamic: a non-literal require argument', () => { const vs = rules({ diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 9b97994..23c10ea 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2025,8 +2025,8 @@ "find": "const violations = checkJsGraph(root);", "replace": "process.exit(0);\nconst violations = checkJsGraph(root);", "gate": "node-test:scripts/check-js-graph.test.mjs", - "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", - "expect_detail": "graph CLI (direct path) did not print its OK line" + "test": "graph CLI [node] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [node] (direct path) did not print its OK line" }, { "name": "the graph CLI dies when an entry-point guard skips it under a symlink", @@ -2035,8 +2035,8 @@ "find": "const violations = checkJsGraph(root);", "replace": "if (process.argv[1] !== decodeURIComponent(new URL(import.meta.url).pathname)) process.exit(0);\nconst violations = checkJsGraph(root);", "gate": "node-test:scripts/check-js-graph.test.mjs", - "test": "graph CLI reports OK on the real tree, also when invoked through a symlink", - "expect_detail": "graph CLI (symlink) did not print its OK line" + "test": "graph CLI [node] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [node] (symlink) did not print its OK line" }, { "name": "the graph CLI dies when it exits 0 on a violating tree", @@ -2045,8 +2045,8 @@ "find": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(1);", "replace": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(0);", "gate": "node-test:scripts/check-js-graph.test.mjs", - "test": "graph CLI fails on a violating tree and names the violation", - "expect_detail": "graph CLI exited 0 on a violating tree" + "test": "graph CLI [node] fails on a violating tree and names the violation", + "expect_detail": "graph CLI [node] exited 0 on a violating tree" }, { "name": "the graph CLI dies when it prints OK without checking", @@ -2055,8 +2055,28 @@ "find": "const violations = checkJsGraph(root);", "replace": "const violations = [];", "gate": "node-test:scripts/check-js-graph.test.mjs", - "test": "graph CLI fails on a violating tree and names the violation", - "expect_detail": "graph CLI printed OK on a violating tree" + "test": "graph CLI [node] fails on a violating tree and names the violation", + "expect_detail": "graph CLI [node] printed OK on a violating tree" + }, + { + "name": "the graph CLI under bun dies when it exits 0 without reporting", + "why": "The real gate runs the CLI under bun (package.json js:graph), not node. The [node] entries above kill only the node arms; this keeps the bun arm of the clean-tree spawn test wired to the CLI, so dropping or detaching it fails here as a stale entry.", + "source": "scripts/check-js-graph.mjs", + "find": "const violations = checkJsGraph(root);", + "replace": "process.exit(0);\nconst violations = checkJsGraph(root);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI [bun] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [bun] (direct path) did not print its OK line" + }, + { + "name": "the graph CLI under bun dies when it exits 0 on a violating tree", + "why": "Under bun the fixture root reaches the CLI through bun's own argv handling; only the bun arm of the violating-tree spawn sees whether the failing branch still exits non-zero in the gate's real runtime.", + "source": "scripts/check-js-graph.mjs", + "find": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(1);", + "replace": " for (const v of violations) console.error(` ${v.rule}: ${v.where} ${v.msg}`);\n process.exit(0);", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI [bun] fails on a violating tree and names the violation", + "expect_detail": "graph CLI [bun] exited 0 on a violating tree" }, { "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", From 80cac0014b2ba3f9d55c4c4d7e05a269c42f9645 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:15:25 +0200 Subject: [PATCH 51/61] test(scripts): prove each graph CLI arm runs the runtime it names Every CLI mutant fails node and bun with the same message, so a [bun] arm resolving to node (or a [node] arm running bun behind a shim) passed every recorded mutation. Each resolved binary is now asked `typeof Bun` and must answer 'object' for [bun] and 'undefined' for [node]; the mutation `return 'bun'` -> `return process.execPath` is recorded. The symlink-arm comment no longer implies the [bun] arm pins the entry-point guard: bun realpaths argv[1] (bun 1.4.4), so only the [node] symlink arm does. Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.test.mjs | 23 +++++++++++++++++++---- tests/mutation-manifest.json | 10 ++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 33778ad..d34b0f1 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -92,9 +92,14 @@ test('the real tree is clean', () => { // script path) is its own, so every CLI case runs once per runtime. bun is resolved // from PATH and is NOT optional: make check-js already requires it, and a skipped bun // arm would read as a pass of the gate's real runtime. +// +// Each resolved binary is also asked who it is. The CLI mutants below fail the same +// way under either runtime, so without this a [bun] arm quietly running node (or a +// [node] arm running bun, under `bun run js:scripttests` behind a node shim) would +// pass every recorded mutation while testing the wrong runtime. const CLI = join(import.meta.dirname, 'check-js-graph.mjs'); const RUNTIMES = [ - ['node', () => process.execPath], + ['node', () => process.execPath, 'undefined'], [ 'bun', () => { @@ -105,18 +110,28 @@ const RUNTIMES = [ ); return 'bun'; }, + 'object', ], ]; -for (const [rt, exe] of RUNTIMES) { +for (const [rt, resolveBin, typeofBun] of RUNTIMES) { + const exe = () => { + const bin = resolveBin(); + const r = spawnSync(bin, ['-e', 'process.stdout.write(typeof Bun)'], { encoding: 'utf8' }); + assert.equal(r.stdout, typeofBun, `graph CLI [${rt}] binary is not ${rt}: ${bin}`); + return bin; + }; + // The CLI itself must report. It once sat behind an "am I the entry point?" guard so // this file could import checkJsGraph, and that guard failed OPEN: run through a // symlink under node, argv[1] is the link while import.meta.url is the realpath, the // body never ran, and the gate exited 0 having printed nothing. The rules now live in // lib/ and the CLI has no guard; this pins both the plain invocation and the symlinked // one, since an exit code alone cannot tell "checked and passed" from "never checked". - // (bun realpaths argv[1] as well, so that guard would have held under bun; the bun - // arm still pins that the gate's real runtime reaches the OK line both ways.) + // The guard regression is pinned by the [node] symlink arm only. The [bun] symlink + // arm mirrors the gate's own invocation but is NOT mutation-pinned: bun realpaths + // argv[1] (verified on bun 1.4.4), so that guard would have held under bun and no + // recorded mutation can kill this arm. test(`graph CLI [${rt}] reports OK on the real tree, also when invoked through a symlink`, () => { const bin = exe(); const dir = mkdtempSync(join(tmpdir(), 'js-graph-cli-')); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 23c10ea..5cd07fc 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2078,6 +2078,16 @@ "test": "graph CLI [bun] fails on a violating tree and names the violation", "expect_detail": "graph CLI [bun] exited 0 on a violating tree" }, + { + "name": "the graph CLI [bun] arm dies when it runs node instead of bun", + "why": "Every CLI mutant fails node and bun with the same message, so a [bun] arm resolving to node would pass both [bun] entries above while never running the gate's real runtime. The identity probe (typeof Bun) is the only thing that tells the runtimes apart.", + "source": "scripts/check-js-graph.test.mjs", + "find": " return 'bun';", + "replace": " return process.execPath;", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI [bun] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [bun] binary is not bun" + }, { "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", From adfe58524d823dde3ec2fbe0aad9b1dda9457634 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:40:41 +0200 Subject: [PATCH 52/61] refactor(events): rewrite node:events in the new JS design (pilot) Plain language on internal data, captured apply for listener calls (safe under the #485 lock, matches node's ReflectApply), null-proto on() queue, and module-eval captures instead of P.* reads in bodies (35 -> 0). No behavior change vs master; events.js ratchet 12 -> 2. Linux emit with one listener 96.4 -> 2.5 ms/1e6. New Lava-only test events_pollution_immunity. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/primordials_test.odin | 141 ++++++++ docs/ARCHITECTURE.md | 4 +- pkg/runtime/js/internal/primordials.js | 15 + pkg/runtime/js/node/events.js | 365 ++++++++++++-------- tests/mutation-manifest.json | 60 +++- tests/node-compat/cases/72-events-parity.js | 32 ++ tests/node-compat/pollution-baseline.json | 2 +- 7 files changed, 472 insertions(+), 147 deletions(-) diff --git a/cmd/lava/primordials_test.odin b/cmd/lava/primordials_test.odin index b74483d..c46cfda 100644 --- a/cmd/lava/primordials_test.odin +++ b/cmd/lava/primordials_test.odin @@ -91,6 +91,147 @@ primordials_pollution_immunity :: proc(t: ^testing.T) { testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) } +// The events-module invariants the post-#485 rewrite added on top of the test above, +// one row each so a mutation's red output names what it broke (expect_detail matches +// the row). Lava-only for the same reason: node fails on-queue (its FixedQueue slots +// are index writes into a holey Array, so an Array.prototype index setter sees them). +// The other four rows also hold on node 26.13 (probed with this script) and were red +// on the pre-rewrite events.js: +// setter0/1 emit()'s snapshot was built by index writes into [], so an +// Array.prototype[0|1] setter threw or silently DROPPED a listener. +// promise-statics once()/on() settled through the live Promise.reject/resolve, +// which a script can replace. +// options-proto on() without options read signal off Object.prototype. +// on-queue events.on() queued through push/shift into an Array. +EVENTS_POLLUTION_SCRIPT :: ` +'use strict'; +const EventEmitter = require('node:events'); +const results = []; +function record(name, fn) { + let out; + try { + out = String(fn()); + } catch (e) { + out = 'THREW:' + e.name; + } + results[results.length] = name + '=' + out; +} + +// emit()'s snapshot of a listener array is an array literal, so an index accessor +// on Array.prototype never sees it. Index 0 and 1 are the slots the copy writes +// first; a copy built by index writes into [] hands every listener to the setter. +for (const idx of ['0', '1']) { + record('setter' + idx, function () { + let stolen = 0; + Object.defineProperty(Array.prototype, idx, { + configurable: true, + set: function () { stolen++; }, + get: function () { return undefined; }, + }); + try { + let calls = 0; + const ee = new EventEmitter(); + ee.on('e', function () { calls++; }); + ee.on('e', function () { calls++; }); + ee.emit('e'); + return calls + ',' + stolen + ',' + ee.listeners('e').length + ',' + ee.rawListeners('e').length; + } finally { + delete Array.prototype[idx]; + } + }); +} + +// once() and events.on() reject/resolve through captured Promise statics, not the +// live Promise.reject / Promise.resolve a script can replace. +record('promise-statics', function () { + const realResolve = Promise.resolve; + const realReject = Promise.reject; + Promise.resolve = function () { throw new Error('Promise.resolve leaked'); }; + Promise.reject = function () { throw new Error('Promise.reject leaked'); }; + try { + const a = EventEmitter.once(new EventEmitter(), 'x', 5); + const it = EventEmitter.on(new EventEmitter(), 'x'); + const b = it.return(); + a.then(null, function () {}); + return (a instanceof Promise) + ',' + (b instanceof Promise); + } finally { + Promise.resolve = realResolve; + Promise.reject = realReject; + } +}); + +// events.on() with no options must not read 'signal' off Object.prototype. +record('options-proto', function () { + Object.prototype.signal = 5; + try { + const it = EventEmitter.on(new EventEmitter(), 'x'); + return typeof it.next; + } finally { + delete Object.prototype.signal; + } +}); + +// events.on()'s queues are not Arrays: neither a replaced push/shift nor an index +// setter on Array.prototype can drop or read a queued event. +const asyncChecks = (async function () { + const realPush = Array.prototype.push; + const realShift = Array.prototype.shift; + let stolen = 0; + Array.prototype.push = function () { throw new Error('push leaked'); }; + Array.prototype.shift = function () { throw new Error('shift leaked'); }; + Object.defineProperty(Array.prototype, '0', { + configurable: true, + set: function () { stolen++; }, + get: function () { return undefined; }, + }); + let got; + try { + const ee = new EventEmitter(); + const it = EventEmitter.on(ee, 'v'); + ee.emit('v', 'a'); + ee.emit('v', 'b'); + const first = await it.next(); + const second = await it.next(); + await it.return(); + got = first.value + '|' + second.value + ',' + stolen; + } catch (e) { + got = 'THREW:' + e.name; + } finally { + Array.prototype.push = realPush; + Array.prototype.shift = realShift; + delete Array.prototype[0]; + } + results[results.length] = 'on-queue=' + got; +})(); + +asyncChecks.then(function () { + const want = ['setter0=2,0,2,2', 'setter1=2,0,2,2', 'promise-statics=true,true', 'options-proto=function', 'on-queue=a|b,0']; + const bad = []; + for (let i = 0; i < want.length; i++) { + if (results[i] !== want[i]) bad[bad.length] = 'want ' + want[i] + ' got ' + results[i]; + } + if (bad.length !== 0) throw new Error(bad.join('; ')); + console.log('events-pollution ok'); +}); +` + +@(test) +events_pollution_immunity :: proc(t: ^testing.T) { + loop := eventloop.init() + // eval consumes (destroys) the loop on every path; do not destroy it here. + result := lava.eval(EVENTS_POLLUTION_SCRIPT, "", &loop, false) + defer lava.result_destroy(&result) + + testing.expectf( + t, + result.status == .Ok, + "eval did not complete cleanly: status=%v message=%q", + result.status, + result.message, + ) + testing.expectf(t, result.exit_code == 0, "eval exit code=%d (want 0)", result.exit_code) +} + // primordials is an internal-only module: internal factories require() it, but it // must not leak through the public resolver (it would shadow a user package named // "primordials" and wrongly answer require('node:primordials')). This evals user diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index c882d0c..0e48eaf 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -554,8 +554,8 @@ is deliberately stronger than Node. A baseline of 0 in a class means "no **counted** site of that class remains", not "immune" — the ratchet is a floor, not a proof. Per-class counts are what make -that readable: `events.js` is at 0 `method` and 0 `accessor` while still carrying -`invoke` and `global` sites, which a single total hid. Do not restate the numbers +that readable: `events.js` is at 0 `method`, `accessor` and `global` while still +carrying `invoke` sites, which a single total hid. Do not restate the numbers here — `pollution-baseline.json` is the source and `make check-primordials` prints the live split; the counts copied into prose went stale twice. `.call`/`.apply` and live globals used to be the author's problem and are now gated classes; diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index 18ddb8e..35d39c9 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -129,6 +129,15 @@ // re-read off Number at call time. Same reason parseInt is captured above. var numberIsInteger = Number.isInteger; var numberIsSafeInteger = Number.isSafeInteger; + // Promise.resolve / Promise.reject are writable statics, so capturing `Promise` + // does not capture them (the SymbolFor note below): `P.Promise.reject(e)` in a + // consumer reads whatever user code left there. Exported as PromiseResolve / + // PromiseReject, node's primordial names. Reflect.apply rather than a `.call` + // wrapper only because neither is ever per-element hot; it keeps this file's + // `invoke` column where it is. + var PromiseCtor = Promise; + var promiseResolve = Promise.resolve; + var promiseReject = Promise.reject; // brandedAs is the UNFORGEABLE replacement for `value instanceof Ctor` on a // hardening path. `instanceof` dispatches through `Ctor[Symbol.hasInstance]`, a @@ -296,6 +305,12 @@ // writable property on it, so a lazily-evaluated module reading `P.Symbol.for` at its // own module-eval still reads whatever user code left there. SymbolFor: Symbol.for, + PromiseResolve: function (value) { + return reflectApply(promiseResolve, PromiseCtor, [value]); + }, + PromiseReject: function (reason) { + return reflectApply(promiseReject, PromiseCtor, [reason]); + }, Reflect: Reflect, JSON: JSON, Math: Math, diff --git a/pkg/runtime/js/node/events.js b/pkg/runtime/js/node/events.js index 144e737..f46006c 100644 --- a/pkg/runtime/js/node/events.js +++ b/pkg/runtime/js/node/events.js @@ -1,39 +1,74 @@ -// node:events — EventEmitter plus the static once(emitter, name) helper. -// Pure JS; the common synchronous + Promise-based surface real code relies on. +// node:events — EventEmitter plus the static helpers (once, on, getEventListeners, +// get/setMaxListeners, listenerCount, addAbortListener). +// +// How this module stays immune to user code that mutates intrinsics: +// * It is EAGER — the loader instantiates it before user code (it is in +// lazy_builtins_test's STATIC_EAGER set) — so every capture below is pristine. +// * A LISTENER is caller-supplied and may carry its own `call`/`apply` or a swapped +// prototype. node invokes it with ReflectApply, which reads neither (node 26.13, +// probed; pinned by tests/node-compat/cases/72-events-parity.js "invoke:"). +// Lava spells the same thing `apply.call(listener, thisArg, args)` on the +// CAPTURED apply: Function.prototype.call/apply/bind are locked before user code +// (#450), so neither read can be steered and the listener's own properties are +// never consulted. Not ReflectApply and not the FunctionPrototypeApply wrapper, +// because both stop JSC from sinking emit()'s rest array on the no-listener +// path. Scratch micro, Linux arm64 JSC 2.52.6, best of 5 x 1e6 emit() calls per +// launch, 2-3 launches per variant: a miss took 1.6 ms with `apply.call` and +// 20-25 ms with either alternative; one listener 2.5 ms vs 3.8-4.1 through the +// wrapper. The ratchet still counts these sites as `invoke`; each carries a +// marker until the detector can tell a captured callee from a caller-supplied one. +// * Data this module owns is handled with plain language on objects it created: +// `_events` is a null-prototype object, listener arrays are written by index and +// copied by array literal, so a replaced Array.prototype.push/unshift/splice/ +// slice or a poisoned Symbol.species never reaches emit(). Pinned, Lava-only, by +// cmd/lava/primordials_test.odin (primordials_ and events_pollution_immunity) — +// node's own emitter uses a live push and is not immune, so no oracle can. +// * Per-instance null-prototype objects come from ObjectCreate(null), not a +// `{ __proto__: null }` literal: JSC sinks the first and not the second (0.3 vs +// 31.9 ms per 1e6 allocations, Linux arm64 JSC 2.52.6, best of 5, 3 launches). +// The literal is fine for a once-per-call options bag. (function (require, module) { 'use strict'; - // Pristine intrinsics (see primordials.js) so a script that mutates - // Array.prototype/Object/etc. cannot alter EventEmitter's internal bookkeeping. var P = require('internal/primordials'); + var ReflectOwnKeys = P.ReflectOwnKeys; + var ObjectCreate = P.ObjectCreate; + var ObjectGetPrototypeOf = P.ObjectGetPrototypeOf; + var ArrayIsArray = P.ArrayIsArray; + var ArrayPrototypePop = P.ArrayPrototypePop; + var ArrayPrototypeUnshift = P.ArrayPrototypeUnshift; + var PromiseResolve = P.PromiseResolve; + var PromiseReject = P.PromiseReject; + var PromiseCtor = P.Promise; + var ErrorCtor = P.Error; + var TypeErrorCtor = P.TypeError; + var SymbolAsyncIterator = P.Symbol.asyncIterator; + // Symbol.dispose is absent from older JSC builds; the disposable then has no key. + var SymbolDispose = P.Symbol.dispose; + + // The captured apply, invoked as `apply.call(listener, thisArg, args)`. See the + // header for why this and not ReflectApply or the FunctionPrototypeApply wrapper. + var apply = Function.prototype.apply; + var errors = require('internal/errors'); var ERR_INVALID_ARG_TYPE = errors.ERR_INVALID_ARG_TYPE; - var defaultMaxListeners = 10; - - // Species-free array helpers. Array.prototype.slice/map/splice consult - // constructor[Symbol.species] (ArraySpeciesCreate), so a polluted species could - // throw inside emit()/listeners() before any listener runs. These copy/remove via - // an array literal and index access only — no prototype method, no species — so a - // listener array cannot be hijacked through @@species (matches Node's arrayClone/ - // spliceOne). - function arrayClone(arr) { - var n = arr.length; - var copy = []; - for (var i = 0; i < n; i++) copy[i] = arr[i]; - return copy; - } - function spliceOne(list, index) { - for (; index + 1 < list.length; index++) list[index] = list[index + 1]; - P.ArrayPrototypePop(list); - } + var ERR_OUT_OF_RANGE = errors.ERR_OUT_OF_RANGE; + + // `options` stand-in for on(): a missing options object must not inherit `signal` + // from a poisoned Object.prototype. node defaults to its frozen kEmptyObject too. + var kEmptyObject = P.ObjectFreeze({ __proto__: null }); function EventEmitter() { + // A live read of EventEmitter.init, and a plain `.call` on it, because node does + // both: a program that replaces EventEmitter.init (or gives the replacement its + // own `call`) sees it run on every construction, on node and here. The ratchet + // counts this `invoke`, correctly: it IS steerable, by design. EventEmitter.init.call(this); } EventEmitter.init = function () { - if (this._events === undefined || this._events === P.ObjectGetPrototypeOf(this)._events) { - this._events = P.ObjectCreate(null); + if (this._events === undefined || this._events === ObjectGetPrototypeOf(this)._events) { + this._events = ObjectCreate(null); this._eventsCount = 0; } this._maxListeners = this._maxListeners || undefined; @@ -42,13 +77,49 @@ EventEmitter.prototype._events = undefined; EventEmitter.prototype._maxListeners = undefined; - function validateMaxListeners(n) { - if (typeof n !== 'number') { - throw ERR_INVALID_ARG_TYPE('setMaxListeners', 'number', n); + // The argument checks, each made once at the public entry; everything below them + // trusts its data. The listener message is not node's yet (no code, no "Received" + // tail; #432) and stays as it was: this rewrite changes no observable behavior. + function checkListener(listener) { + if (typeof listener !== 'function') { + throw new TypeErrorCtor('The "listener" argument must be of type function'); } - if (n < 0 || n !== n) { - throw errors.ERR_OUT_OF_RANGE('setMaxListeners', '>= 0', n); + } + + function validateMaxListeners(n) { + if (typeof n !== 'number') throw ERR_INVALID_ARG_TYPE('setMaxListeners', 'number', n); + if (n < 0 || n !== n) throw ERR_OUT_OF_RANGE('setMaxListeners', '>= 0', n); + } + + // node's arrayClone: an array literal for the common sizes, an index loop past + // them. A literal DEFINES its elements, so neither a replaced Array.prototype + // method, Symbol.species, nor an index setter on Array.prototype sees the copy; + // the loop's writes walk the prototype chain like any push does. A literal is also + // the cheapest copy on JSC, which matters because emit() takes one per call. + function arrayClone(list) { + switch (list.length) { + case 2: + return [list[0], list[1]]; + case 3: + return [list[0], list[1], list[2]]; + case 4: + return [list[0], list[1], list[2], list[3]]; + case 5: + return [list[0], list[1], list[2], list[3], list[4]]; + case 6: + return [list[0], list[1], list[2], list[3], list[4], list[5]]; } + var copy = []; + for (var i = 0; i < list.length; i++) copy[i] = list[i]; + return copy; + } + + // node's spliceOne: shift the tail down over `index`, then drop the last slot. The + // captured pop, not `list.length -= 1`: the length write measured 1.5x pop on + // Linux arm64 JSC (35.8 vs 24.1 ms per 1e6 two-element splices, best of 5, 3 launches). + function spliceOne(list, index) { + for (; index + 1 < list.length; index++) list[index] = list[index + 1]; + ArrayPrototypePop(list); } EventEmitter.prototype.setMaxListeners = function (n) { @@ -64,9 +135,8 @@ }; function addListener(target, type, listener, prepend) { - if (typeof listener !== 'function') { - throw new TypeError('The "listener" argument must be of type function'); - } + checkListener(listener); + // Live init, as in the constructor; an emitter built by Object.create(proto) lands here. if (target._events === undefined) EventEmitter.init.call(target); var events = target._events; if (events.newListener !== undefined) { @@ -75,18 +145,20 @@ type, listener.listener !== undefined ? listener.listener : listener, ); + // A newListener handler may have rebuilt _events (removeAllListeners does). events = target._events; } var existing = events[type]; if (existing === undefined) { + // One listener is stored bare; the array only appears with the second. events[type] = listener; target._eventsCount++; } else if (typeof existing === 'function') { events[type] = prepend ? [listener, existing] : [existing, listener]; } else if (prepend) { - P.ArrayPrototypeUnshift(existing, listener); + ArrayPrototypeUnshift(existing, listener); } else { - P.ArrayPrototypePush(existing, listener); + existing[existing.length] = listener; } return target; } @@ -106,16 +178,15 @@ if (fired) return; fired = true; target.removeListener(type, wrapped); - return P.FunctionPrototypeApply(listener, target, arguments); + // A ratchet false positive, like the two in emit(): captured `apply`, locked `.call`. + return apply.call(listener, target, arguments); // primordials-ok: invoke } wrapped.listener = listener; return wrapped; } EventEmitter.prototype.once = function (type, listener) { - if (typeof listener !== 'function') { - throw new TypeError('The "listener" argument must be of type function'); - } + checkListener(listener); // Route through this.on (not the internal addListener): Node does the same, // and subclasses that override on() to observe listener registration — // node:stream Readable arms flow/'readable' plumbing there — must see @@ -125,20 +196,18 @@ }; EventEmitter.prototype.prependOnceListener = function (type, listener) { - if (typeof listener !== 'function') { - throw new TypeError('The "listener" argument must be of type function'); - } + checkListener(listener); this.prependListener(type, onceWrap(this, type, listener)); return this; }; EventEmitter.prototype.removeListener = function (type, listener) { - if (this._events === undefined) return this; var events = this._events; + if (events === undefined) return this; var list = events[type]; if (list === undefined) return this; if (list === listener || list.listener === listener) { - if (--this._eventsCount === 0) this._events = P.ObjectCreate(null); + if (--this._eventsCount === 0) this._events = ObjectCreate(null); else { delete events[type]; if (events.removeListener !== undefined) { @@ -146,6 +215,8 @@ } } } else if (typeof list !== 'function') { + // Last match wins, as in node: removing one of two identical registrations + // takes the most recent. var position = -1; for (var i = list.length - 1; i >= 0; i--) { if (list[i] === listener || list[i].listener === listener) { @@ -168,59 +239,59 @@ EventEmitter.prototype.removeAllListeners = function (type) { var events = this._events; if (events === undefined) return this; + // Without a removeListener listener nobody can observe the order, so drop in bulk. if (events.removeListener === undefined) { if (arguments.length === 0) { - this._events = P.ObjectCreate(null); + this._events = ObjectCreate(null); this._eventsCount = 0; } else if (events[type] !== undefined) { - if (--this._eventsCount === 0) this._events = P.ObjectCreate(null); + if (--this._eventsCount === 0) this._events = ObjectCreate(null); else delete events[type]; } return this; } if (arguments.length === 0) { - var keys = P.ReflectOwnKeys(events); + var keys = ReflectOwnKeys(events); for (var k = 0; k < keys.length; k++) { - var key = keys[k]; - if (key === 'removeListener') continue; - this.removeAllListeners(key); + if (keys[k] !== 'removeListener') this.removeAllListeners(keys[k]); } this.removeAllListeners('removeListener'); - this._events = P.ObjectCreate(null); + this._events = ObjectCreate(null); this._eventsCount = 0; return this; } + // LIFO, so 'removeListener' fires in reverse registration order, as in node. var listeners = events[type]; if (typeof listeners === 'function') { this.removeListener(type, listeners); } else if (listeners !== undefined) { - for (var i = listeners.length - 1; i >= 0; i--) { - this.removeListener(type, listeners[i]); - } + for (var i = listeners.length - 1; i >= 0; i--) this.removeListener(type, listeners[i]); } return this; }; - EventEmitter.prototype.emit = function (type) { + EventEmitter.prototype.emit = function (type, ...args) { var events = this._events; var handler = events === undefined ? undefined : events[type]; if (handler === undefined) { if (type === 'error') { - var err = arguments[1]; - if (err instanceof Error) throw err; - var e = new Error('Unhandled error.' + (err !== undefined ? ' (' + err + ')' : '')); + var err = args[0]; + if (err instanceof ErrorCtor) throw err; + var e = new ErrorCtor('Unhandled error.' + (err !== undefined ? ' (' + err + ')' : '')); e.context = err; throw e; } return false; } - var args = P.ArrayPrototypeSlice(arguments, 1); + // Both `apply.call` sites are false positives for the ratchet: `apply` is the + // captured Function.prototype.apply and `.call` is locked (#450). if (typeof handler === 'function') { - P.FunctionPrototypeApply(handler, this, args); + apply.call(handler, this, args); // primordials-ok: invoke } else { + // A snapshot: listeners added or removed by a listener take effect next emit. var listeners = arrayClone(handler); for (var i = 0; i < listeners.length; i++) { - P.FunctionPrototypeApply(listeners[i], this, args); + apply.call(listeners[i], this, args); // primordials-ok: invoke } } return true; @@ -228,8 +299,7 @@ EventEmitter.prototype.listeners = function (type) { var events = this._events; - if (events === undefined) return []; - var handler = events[type]; + var handler = events === undefined ? undefined : events[type]; if (handler === undefined) return []; if (typeof handler === 'function') return [handler.listener || handler]; var copy = arrayClone(handler); @@ -239,37 +309,44 @@ EventEmitter.prototype.rawListeners = function (type) { var events = this._events; - if (events === undefined) return []; - var handler = events[type]; + var handler = events === undefined ? undefined : events[type]; if (handler === undefined) return []; return typeof handler === 'function' ? [handler] : arrayClone(handler); }; EventEmitter.prototype.listenerCount = function (type, listener) { var events = this._events; - if (events === undefined) return 0; - var handler = events[type]; + var handler = events === undefined ? undefined : events[type]; if (handler === undefined) return 0; if (typeof handler === 'function') { - if (listener !== undefined) { - return handler === listener || handler.listener === listener ? 1 : 0; - } - return 1; + if (listener === undefined) return 1; + return handler === listener || handler.listener === listener ? 1 : 0; } - if (listener !== undefined) { - var count = 0; - for (var i = 0; i < handler.length; i++) { - if (handler[i] === listener || handler[i].listener === listener) count++; - } - return count; + if (listener === undefined) return handler.length; + var count = 0; + for (var i = 0; i < handler.length; i++) { + if (handler[i] === listener || handler[i].listener === listener) count++; } - return handler.length; + return count; }; EventEmitter.prototype.eventNames = function () { - return this._eventsCount > 0 ? P.ReflectOwnKeys(this._events) : []; + return this._eventsCount > 0 ? ReflectOwnKeys(this._events) : []; }; + // --- static helpers (Node parity) --- + + function isAbortSignal(s) { + return s != null && typeof s.addEventListener === 'function' && 'aborted' in s; + } + + function makeAbortError() { + var e = new ErrorCtor('The operation was aborted'); + e.name = 'AbortError'; + e.code = 'ABORT_ERR'; + return e; + } + // once(emitter, name[, options]) — resolves with the emitted arguments on the next // matching event, or rejects if 'error' fires first or options.signal aborts. function once(emitter, name, options) { @@ -279,20 +356,18 @@ // throws because node's once is an async function. if ( options !== undefined && - (options === null || typeof options !== 'object' || P.ArrayIsArray(options)) + (options === null || typeof options !== 'object' || ArrayIsArray(options)) ) { - return P.Promise.reject(ERR_INVALID_ARG_TYPE('options', 'object', options)); + return PromiseReject(ERR_INVALID_ARG_TYPE('options', 'object', options)); } var signal = options && options.signal; if (signal !== undefined) { if (!isAbortSignal(signal)) { - return P.Promise.reject(ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal)); - } - if (signal.aborted) { - return P.Promise.reject(makeAbortError()); + return PromiseReject(ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal)); } + if (signal.aborted) return PromiseReject(makeAbortError()); } - return new P.Promise(function (resolve, reject) { + return new PromiseCtor(function (resolve, reject) { var errorListener; var abortListener; function cleanup() { @@ -302,20 +377,18 @@ } else if (typeof emitter.removeEventListener === 'function') { emitter.removeEventListener(name, eventListener); } - if (abortListener !== undefined) { - signal.removeEventListener('abort', abortListener); - } + if (abortListener !== undefined) signal.removeEventListener('abort', abortListener); } - function eventListener() { + function eventListener(...args) { cleanup(); - resolve(P.ArrayPrototypeSlice(arguments)); + resolve(args); } if (signal !== undefined) { abortListener = function () { cleanup(); reject(makeAbortError()); }; - signal.addEventListener('abort', abortListener, { once: true }); + signal.addEventListener('abort', abortListener, { __proto__: null, once: true }); } if (typeof emitter.once === 'function') { if (name !== 'error') { @@ -327,7 +400,7 @@ } emitter.once(name, eventListener); } else if (typeof emitter.addEventListener === 'function') { - emitter.addEventListener(name, eventListener, { once: true }); + emitter.addEventListener(name, eventListener, { __proto__: null, once: true }); } else { cleanup(); reject(ERR_INVALID_ARG_TYPE('emitter', 'EventEmitter', emitter)); @@ -335,19 +408,6 @@ }); } - // --- static helpers (Node parity) --- - - function isAbortSignal(s) { - return s != null && typeof s.addEventListener === 'function' && 'aborted' in s; - } - - function makeAbortError() { - var e = new Error('The operation was aborted'); - e.name = 'AbortError'; - e.code = 'ABORT_ERR'; - return e; - } - // events.getEventListeners(emitter, name) — the listeners for `name`. function getEventListeners(emitterOrTarget, name) { if (typeof emitterOrTarget.listeners === 'function') { @@ -365,16 +425,15 @@ } // events.setMaxListeners(n[, ...targets]) — with no targets, sets the global default. - function setMaxListeners(n) { + function setMaxListeners(n, ...targets) { n = n === undefined ? EventEmitter.defaultMaxListeners : n; validateMaxListeners(n); - var targets = P.ArrayPrototypeSlice(arguments, 1); if (targets.length === 0) { EventEmitter.defaultMaxListeners = n; - } else { - for (var i = 0; i < targets.length; i++) { - if (typeof targets[i].setMaxListeners === 'function') targets[i].setMaxListeners(n); - } + return; + } + for (var i = 0; i < targets.length; i++) { + if (typeof targets[i].setMaxListeners === 'function') targets[i].setMaxListeners(n); } } @@ -387,40 +446,67 @@ // if the signal is already aborted; returns a { [Symbol.dispose]() } to remove it. function addAbortListener(signal, listener) { if (!isAbortSignal(signal)) throw ERR_INVALID_ARG_TYPE('signal', 'AbortSignal', signal); - if (typeof listener !== 'function') + if (typeof listener !== 'function') { throw ERR_INVALID_ARG_TYPE('listener', 'function', listener); + } var removeEventListener; if (signal.aborted) { + // Read live, not captured: bootstrap/microtasks.js installs queueMicrotask + // AFTER the loader has instantiated this module (globals.odin install_microtasks), + // so a module-eval capture would be undefined. queueMicrotask(function () { listener(); }); } else { - signal.addEventListener('abort', listener, { once: true }); + signal.addEventListener('abort', listener, { __proto__: null, once: true }); removeEventListener = function () { signal.removeEventListener('abort', listener); }; } var disposable = {}; - if (typeof Symbol !== 'undefined' && Symbol.dispose) { - disposable[Symbol.dispose] = function () { + if (SymbolDispose !== undefined) { + disposable[SymbolDispose] = function () { if (removeEventListener) removeEventListener(); }; } return disposable; } + // The FIFO behind each events.on() iterator's two queues. Never handed to user + // code, so its slots live in a null-prototype object: an index write there walks no + // prototype chain, where an Array's push/shift could be replaced and an + // Array.prototype index setter would see every queued value. + class Fifo { + constructor() { + this.slots = ObjectCreate(null); + this.head = 0; + this.tail = 0; + } + get size() { + return this.tail - this.head; + } + enqueue(value) { + this.slots[this.tail++] = value; + } + dequeue() { + var value = this.slots[this.head]; + delete this.slots[this.head++]; + return value; + } + } + // events.on(emitter, eventName[, options]) — an async iterator that yields the args of // each matching event. 'error' rejects; options.signal aborts; return()/throw() clean up. function on(emitter, event, options) { - options = options || {}; + options = options || kEmptyObject; var signal = options.signal; if (signal !== undefined && !isAbortSignal(signal)) { throw ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); } if (signal && signal.aborted) throw makeAbortError(); - var unconsumedEvents = []; - var unconsumedPromises = []; + var unconsumedEvents = new Fifo(); + var unconsumedPromises = new Fifo(); var error = null; var finished = false; @@ -440,20 +526,13 @@ function result(value, done) { return { value: value, done: done }; } - function eventHandler() { - var args = P.ArrayPrototypeSlice(arguments); - if (unconsumedPromises.length > 0) { - P.ArrayPrototypeShift(unconsumedPromises).resolve(result(args, false)); - } else { - P.ArrayPrototypePush(unconsumedEvents, args); - } + function eventHandler(...args) { + if (unconsumedPromises.size > 0) unconsumedPromises.dequeue().resolve(result(args, false)); + else unconsumedEvents.enqueue(args); } function errorHandler(err) { - if (unconsumedPromises.length > 0) { - P.ArrayPrototypeShift(unconsumedPromises).reject(err); - } else { - error = err; - } + if (unconsumedPromises.size > 0) unconsumedPromises.dequeue().reject(err); + else error = err; closeHandler(); } function abortHandler() { @@ -467,40 +546,40 @@ function closeHandler() { removeAll(); finished = true; - while (unconsumedPromises.length > 0) { - P.ArrayPrototypeShift(unconsumedPromises).resolve(result(undefined, true)); + while (unconsumedPromises.size > 0) { + unconsumedPromises.dequeue().resolve(result(undefined, true)); } } addListener(event, eventHandler); if (event !== 'error' && useEmitter) addListener('error', errorHandler); - if (signal) signal.addEventListener('abort', abortHandler, { once: true }); + if (signal) signal.addEventListener('abort', abortHandler, { __proto__: null, once: true }); var iterator = { next: function () { - if (unconsumedEvents.length > 0) { - return P.Promise.resolve(result(P.ArrayPrototypeShift(unconsumedEvents), false)); + if (unconsumedEvents.size > 0) { + return PromiseResolve(result(unconsumedEvents.dequeue(), false)); } if (error) { var e = error; error = null; - return P.Promise.reject(e); + return PromiseReject(e); } - if (finished) return P.Promise.resolve(result(undefined, true)); - return new P.Promise(function (resolve, reject) { - P.ArrayPrototypePush(unconsumedPromises, { resolve: resolve, reject: reject }); + if (finished) return PromiseResolve(result(undefined, true)); + return new PromiseCtor(function (resolve, reject) { + unconsumedPromises.enqueue({ resolve: resolve, reject: reject }); }); }, return: function () { closeHandler(); - return P.Promise.resolve(result(undefined, true)); + return PromiseResolve(result(undefined, true)); }, throw: function (err) { closeHandler(); - return P.Promise.reject(err); + return PromiseReject(err); }, }; - iterator[Symbol.asyncIterator] = function () { + iterator[SymbolAsyncIterator] = function () { return this; }; return iterator; @@ -514,7 +593,7 @@ EventEmitter.setMaxListeners = setMaxListeners; EventEmitter.listenerCount = staticListenerCount; EventEmitter.addAbortListener = addAbortListener; - EventEmitter.defaultMaxListeners = defaultMaxListeners; + EventEmitter.defaultMaxListeners = 10; module.exports = EventEmitter; module.exports.EventEmitter = EventEmitter; diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 99e6be4..b6104b6 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -1785,11 +1785,69 @@ "lava: write(hex, 5, 8, \"hex\") => THREW RangeError" ] }, + { + "name": "72-events-parity dies when emit() invokes a listener through its own apply", + "why": "node invokes listeners with ReflectApply, so a listener's own call/apply is never consulted; handler.apply(this, args) runs the listener's own apply instead of its body.", + "source": "pkg/runtime/js/node/events.js", + "find": " apply.call(handler, this, args); // primordials-ok: invoke", + "replace": " handler.apply(this, args);", + "gate": "compat:tests/node-compat/cases/72-events-parity.js", + "expect_detail": [ + "own.apply" + ] + }, + { + "name": "72-events-parity dies when a once() wrapper invokes the listener through its own apply", + "why": "The once() wrapper is the other listener-invocation site; node ignores the listener's own apply there too.", + "source": "pkg/runtime/js/node/events.js", + "find": " return apply.call(listener, target, arguments); // primordials-ok: invoke", + "replace": " return listener.apply(target, arguments);", + "gate": "compat:tests/node-compat/cases/72-events-parity.js", + "expect_detail": [ + "own.apply" + ] + }, + { + "name": "events_pollution_immunity dies when emit()'s two-listener snapshot is built by index writes", + "why": "An array literal defines its elements; index writes into [] walk Array.prototype, so a setter at [0] throws inside emit() or silently drops a listener where node emits to both.", + "source": "pkg/runtime/js/node/events.js", + "find": " case 2:\n return [list[0], list[1]];\n", + "replace": "", + "gate": "odin:cmd/lava:main.events_pollution_immunity", + "expect_detail": "want setter0=2,0,2,2 got setter0=THREW:TypeError" + }, + { + "name": "events_pollution_immunity dies when once() rejects through the live Promise.reject", + "why": "Promise.reject is a writable static of the Promise constructor; capturing Promise does not capture it, so a replaced Promise.reject steers once()'s argument errors.", + "source": "pkg/runtime/js/node/events.js", + "find": " var PromiseReject = P.PromiseReject;", + "replace": " var PromiseReject = function (e) { return P.Promise.reject(e); };", + "gate": "odin:cmd/lava:main.events_pollution_immunity", + "expect_detail": "got promise-statics=THREW:Error" + }, + { + "name": "events_pollution_immunity dies when events.on() defaults options to a fresh {}", + "why": "A {} default inherits signal from a poisoned Object.prototype, so on(emitter, name) throws ERR_INVALID_ARG_TYPE where node (frozen null-prototype default) does not.", + "source": "pkg/runtime/js/node/events.js", + "find": " options = options || kEmptyObject;", + "replace": " options = options || {};", + "gate": "odin:cmd/lava:main.events_pollution_immunity", + "expect_detail": "want options-proto=function got options-proto=THREW:TypeError" + }, + { + "name": "events_pollution_immunity dies when events.on()'s queue keeps its slots in an Array", + "why": "Queued events written into an Array pass through an Array.prototype index setter, which can read or drop them; the null-prototype slots object walks no chain.", + "source": "pkg/runtime/js/node/events.js", + "find": " this.slots = ObjectCreate(null);", + "replace": " this.slots = [];", + "gate": "odin:cmd/lava:main.events_pollution_immunity", + "expect_detail": "want on-queue=a|b,0 got on-queue=" + }, { "name": "72-events-parity dies when once() stops validating options", "why": "node's events.once rejects a non-object options with ERR_INVALID_ARG_TYPE; without the check once(ee, 'foo', 5) just waits.", "source": "pkg/runtime/js/node/events.js", - "find": " return P.Promise.reject(ERR_INVALID_ARG_TYPE('options', 'object', options));", + "find": " return PromiseReject(ERR_INVALID_ARG_TYPE('options', 'object', options));", "replace": " options = undefined;", "gate": "compat:tests/node-compat/cases/72-events-parity.js", "expect_detail": [ diff --git a/tests/node-compat/cases/72-events-parity.js b/tests/node-compat/cases/72-events-parity.js index 4dc8403..68910f2 100644 --- a/tests/node-compat/cases/72-events-parity.js +++ b/tests/node-compat/cases/72-events-parity.js @@ -123,7 +123,39 @@ function testMetaEvents() { console.log('meta:', JSON.stringify(log)); } +// A listener is caller-supplied, so it can carry its own call/apply or a swapped +// prototype. node invokes it with ReflectApply, which reads none of them: the real +// body runs, with the emitter as `this` and the emitted arguments, on emit for one +// listener, for several, and when a once() wrapper fires. +function testListenerInvocation() { + const ee = new EventEmitter(); + const seen = []; + function own(a, b) { + seen.push('own ' + a + b + ' ' + (this === ee)); + } + own.call = () => seen.push('own.call'); + own.apply = () => seen.push('own.apply'); + function swapped(a) { + seen.push('swapped ' + a + ' ' + (this === ee)); + } + Object.setPrototypeOf(swapped, { + call: () => seen.push('proto.call'), + apply: () => seen.push('proto.apply'), + }); + ee.on('one', own); + ee.emit('one', 1, 2); + ee.on('many', own); + ee.on('many', swapped); + ee.emit('many', 3, 4); + ee.once('once', own); + ee.emit('once', 5, 6); + ee.prependOnceListener('once2', swapped); + ee.emit('once2', 7); + console.log('invoke:', seen.join('; ')); +} + testSetMaxListeners(); testListenerCount(); testMetaEvents(); +testListenerInvocation(); testOnceSignal(); diff --git a/tests/node-compat/pollution-baseline.json b/tests/node-compat/pollution-baseline.json index ef4e1ae..455acd1 100644 --- a/tests/node-compat/pollution-baseline.json +++ b/tests/node-compat/pollution-baseline.json @@ -381,7 +381,7 @@ "method": 0, "invoke": 2, "accessor": 0, - "global": 10 + "global": 0 }, "node/fs.js": { "method": 0, From 2cf0187d0ec24011055a55cf015b370b3adbcab6 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:33:01 +0200 Subject: [PATCH 53/61] build(js): ratchet exempts only provably private function-declaration callees; marker takes a reason MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #485 lock does not protect .call on captured intrinsics (an own call on the intrinsic steers it, #494), so module-eval captures and primordials exports stay counted. Exempt only a strict function declaration the file never lets go of. '// primordials-ok[: classes] — reason' now suppresses. Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 33 ++-- ROADMAP.md | 12 +- docs/ARCHITECTURE.md | 15 +- scripts/lib/primordials-detect.mjs | 216 ++++++++++++++++++++++++++- scripts/lib/primordials-fixtures.mjs | 122 +++++++++++++++ tests/mutation-manifest.json | 72 +++++++++ 6 files changed, 449 insertions(+), 21 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index bbe47af..6ce2d63 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -226,12 +226,20 @@ Rules that keep this from becoming ceremony: accessors, so `Function.prototype.call = x` throws in both modes while `f.call = x` on an ordinary function still defines an own property, as in node. That is a §1 deviation, pinned by `cmd/lava/function_call_lock_test.odin`. What - it buys: `fn.call(t, a)` on a function WE captured can no longer be steered, so - the `callerN` wrappers keep their live `.call` at no security cost. What it does - not: a CALLER-SUPPLIED function can still carry an own `call` or a re-pointed - prototype, and `cb.call(t, x)` then runs the caller's `call` where node invokes - `cb` directly — still `ReflectApply` there. The `invoke` class keeps counting - both until the ratchet can tell them apart. + it buys: `Function.prototype.call = x` can no longer steer every `.call` at once. + What it does NOT buy: `.call` on any function user code can HOLD. That is a + caller-supplied callback (own `call`, re-pointed prototype), and it is also every + captured INTRINSIC — `var apply = Function.prototype.apply` is the very object + user code reaches, so `Function.prototype.apply.call = evil` steers + `apply.call(listener, …)`, and `Array.prototype.push.call = evil` steers the + `callerN` wrappers' `fn.call` (both probed on `bin/lava`; node runs neither). + The ratchet therefore counts every `.call`/`.apply` (`invoke`) and `.bind` + (`method`) except on ONE receiver: a strict function declaration the file never + lets go of — exactly one binding of the name in the file, never written, + referenced only as `g(…)` or `g.call/apply/bind(…)` inside its declaring scope, + no direct `eval` in the file (`capturedCallees`, `scripts/lib/primordials-detect.mjs`). + A module-eval capture of an intrinsic or a primordials export is NOT exempt, by + design: a false negative there is a hole. **Module-eval capture is pristine only in a module the loader instantiates BEFORE user code** — the eager list in `bootstrap/loader.js`, plus `stream`/`stdio` via `installStdio`. A LAZY module's factory runs on the user's first `require()`, so a capture there is a live read @@ -245,7 +253,10 @@ Rules that keep this from becoming ceremony: - A false positive takes `// primordials-ok` on the line. On a line carrying candidates from **more than one class** the bare marker suppresses nothing — name it, `// primordials-ok: method` (comma-separated list allowed). An - unrecognized class name suppresses nothing, so a typo fails loud. + unrecognized class name suppresses nothing, so a typo fails loud. Either form may + end in a reason after ONE separator, an em dash — + `// primordials-ok: invoke — receiver is our own closure`; `--`, `-` or an + empty reason suppress nothing. - `UPDATE=1` only _lowers_ a baseline. Raising one needs `make check-primordials UPDATE=1 RAISE=--allow-raise` and a stated reason (a newly scanned file, or a new class). A corrupt baseline fails hard rather than @@ -280,10 +291,10 @@ Rules that keep this from becoming ceremony: readable rather than reassuring. It earned that caveat: `encoding.js` stood at 0 while `units.buffer` went through the live `%TypedArray%.prototype.buffer` getter, which is why the accessor class exists. And a high number is not - automatically work: `primordials.js` reads 9 in `invoke` **by construction**, - because every `callerN` wrapper is a `fn.call` on a captured method, which the - lock above makes safe — its `global` column is 0, since the module-eval rule - already exempts the capture table. + automatically work: `primordials.js`'s `global` column is 0 because the + module-eval rule exempts the capture table. Its 9 `invoke` sites, though, are + real: every `callerN` wrapper is a `fn.call` on a captured intrinsic, which the + lock above does not protect (an own `call` on the intrinsic steers it). - Pick the primordial by **arity**. `callerN` (`ArrayPrototypePush`) carries an `arguments` switch and belongs at cold call sites; per-element loops use the fixed-arity wrappers (`ArrayPrototypePush1`/`Push2`) or plain indexed writes diff --git a/ROADMAP.md b/ROADMAP.md index 466a1b4..2b7462d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -688,7 +688,9 @@ the index, the issue is the detail. Ordered by what it unblocks. `global` column to 0, which `encoding.js` already demonstrates. Note `primordials.js` reads 9 in `invoke` **by construction** — every `callerN` wrapper is a `fn.call` — while its `global` column is 0, because the - module-eval rule already exempts the capture table. Neither is work. + module-eval rule already exempts the capture table. The `global` 0 is not + work; the `invoke` 9 IS — those `fn.call` sites are steerable through an own + `call` on the captured intrinsic (see the open item below). **Done ([#450](https://github.com/x-ror/lava/issues/450))**: `lockIntrinsics()` is wired — `bootstrap/loader.js` runs it right after primordials is captured, making `Function.prototype.call/apply/bind` non-configurable accessors in @@ -728,8 +730,12 @@ the index, the issue is the detail. Ordered by what it unblocks. `.call` and `Reflect.apply` — check for that before reaching for either. Still open: `.call` on a CALLER-SUPPLIED function (a listener, a callback) is outside the lock — the function can carry its own `call` — so those sites - still want `ReflectApply`, and the `invoke` class cannot be retired until the - ratchet can tell a captured receiver from a caller-supplied one. + still want `ReflectApply`. Nor is a captured INTRINSIC safe: user code holds + the same object and can give it an own `call`. An own `call` on + `Function.prototype.apply` steers `events.js`'s `apply.call(listener, …)`, + and one on `Array.prototype.push` steers the `callerN` wrappers. The ratchet + now exempts only a strict, non-escaping function declaration + (`capturedCallees`), so both stay counted. - [x] **`run_until_idle` ended the drive on one no-progress tick** — fixed: it now keeps polling while `active_io_count` or `active_async` is nonzero, the invariant `run()` has carried since #113 (ba12bd3) and which that fix reached diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 0e48eaf..4584b56 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -533,10 +533,17 @@ the loader calls `lockIntrinsics()` right after the primordials table is capture turning each into a non-configurable accessor in the shape of node's `--frozen-intrinsics` (assignment on `Function.prototype` throws in both modes; `f.call = x` on an ordinary function still defines an own property). That is a -deliberate node deviation (#450), pinned by `cmd/lava/function_call_lock_test.odin`, -and it is what lets the `callerN` wrappers keep a live `.call` on a captured method. -It does not cover `.call` on a caller-supplied function, which can carry its own -`call`, so the `invoke` class still counts every site. +deliberate node deviation (#450), pinned by `cmd/lava/function_call_lock_test.odin`. +It does **not** make `.call` safe on a captured INTRINSIC: user code holds the same +object, so `Function.prototype.apply.call = evil` defines an own `call` that +`apply.call(listener, …)` in `events.js` then runs, and `Array.prototype.push.call = evil` +reaches the `callerN` wrappers' `fn.call` (both probed on `bin/lava`; node runs +neither). So the `invoke` class counts every `.call`/`.apply` (and `method` every +`.bind`) except on one receiver: a strict function declaration the file never lets +go of — one binding of the name in the file, never written, referenced only as +`g(…)` or `g.call/apply/bind(…)` inside its declaring scope, and no direct `eval` +(`capturedCallees` in `primordials-detect.mjs`). A suppression marker may carry its +reason after an em dash: `// primordials-ok: invoke — why`. A `global` read at module-eval is exempt only in a module instantiated before user code — the loader's `req()` roots closed over top-level requires, computed by diff --git a/scripts/lib/primordials-detect.mjs b/scripts/lib/primordials-detect.mjs index 29554d5..5a200a4 100644 --- a/scripts/lib/primordials-detect.mjs +++ b/scripts/lib/primordials-detect.mjs @@ -47,7 +47,13 @@ // real line comment, and only when every candidate on the line shares one // class. On a mixed line, name it: `// primordials-ok: method`, or a // comma-separated list. An unrecognized class name suppresses nothing, so a typo -// fails loud. +// fails loud. Either form may end in a reason after an em dash: +// `// primordials-ok: invoke — why` (see MARKER). +// +// `invoke` (and `.bind` in `method`) is not counted on ONE receiver shape: a +// strict function declaration the file never lets go of (capturedCallees). It is +// deliberately NOT exempt on an intrinsic captured at module-eval or a primordials +// export — user code holds those same objects and can give them an own `call`. // // Per-class baselines are what make "0" mean something: a file may be at 0 // globals because it captures them and still carry accessor reads, and mixing @@ -309,6 +315,194 @@ function shadowedByParam(name, ancestors) { return false; } +// Every node under `node`, depth-first, with its ancestor chain (the node itself +// last). Generic over the ESTree shape rather than acorn-walk's visitor table on +// purpose: capturedCallees has to see binding and write positions that +// acorn-walk reports under override names (`VariablePattern`), and a missed +// position here is an exemption that should not have been granted. +function eachNode(node, visit, ancestors = []) { + ancestors.push(node); + visit(node, ancestors); + for (const key of Object.keys(node)) { + const v = node[key]; + if (Array.isArray(v)) { + for (const c of v) if (c && typeof c.type === 'string') eachNode(c, visit, ancestors); + } else if (v && typeof v.type === 'string') { + eachNode(v, visit, ancestors); + } + } + ancestors.pop(); +} + +// The identifiers a binding/assignment target introduces or writes. +function patternIds(p, out = []) { + if (!p) return out; + if (p.type === 'Identifier') out.push(p); + else if (p.type === 'ObjectPattern') { + for (const prop of p.properties) + patternIds(prop.type === 'RestElement' ? prop : prop.value, out); + } else if (p.type === 'ArrayPattern') for (const e of p.elements) patternIds(e, out); + else if (p.type === 'RestElement') patternIds(p.argument, out); + else if (p.type === 'AssignmentPattern') patternIds(p.left, out); + return out; +} + +function hasUseStrict(body) { + for (const s of body) { + if (s.type !== 'ExpressionStatement' || typeof s.directive !== 'string') break; + if (s.directive === 'use strict') return true; + } + return false; +} + +// capturedCallees returns the names whose `.call`/`.apply`/`.bind` the detector +// does NOT count: function declarations no user code can ever hold. +// +// Why that is the whole rule. The #485 lock makes Function.prototype.call/apply/ +// bind unreplaceable, but its setter still lets `g.call = x` define an OWN +// property on any other function — node's override-mistake shape, deliberately +// kept. So `X.call(...)` is safe exactly when no user code can reach X to give it +// one. That rules out the two shapes a "captured at module-eval" rule would +// exempt, and both were probed on bin/lava at c3b35ab: +// * an intrinsic captured off a prototype (`var apply = Function.prototype.apply`) +// is the SAME object user code reaches as `Function.prototype.apply`; +// `Function.prototype.apply.call = evil` made events.js's +// `apply.call(listener, …)` run `evil` instead of the listener, and +// `Array.prototype.push.call = evil` ran 7 times under one URLSearchParams +// append through primordials' `fn.call` wrappers. node runs neither. +// * a primordials export: the table re-exports raw intrinsics (`ReflectApply`, +// `ObjectKeys`, `String`, …), which are the same reachable objects. +// What remains sound is a function the file declares and never lets go of: +// 1. exactly ONE binding of the name anywhere in the file — so no parameter, +// local, catch or inner declaration can shadow it at some use site — and +// that binding is a FunctionDeclaration directly in a Program or function +// body, with every reference lexically inside that scope (outside it the +// name resolves to a replaceable global); +// 2. never written (`=`, `++`, destructuring or for-in/of target); +// 3. every reference is a direct call `g(…)` or the receiver of +// `g.call/apply/bind(…)` — anything else (passing it, exporting it, `new g`, +// `g.prototype`, `typeof g`) is treated as an escape, since `new g()` alone +// hands it out through `instance.constructor`; +// 4. strict, because a sloppy function is reachable as `callee.caller` from any +// user callback it invokes; +// 5. no direct `eval` in the file, which could rebind it. +// Anything else stays counted. A false negative here is a hole, so every doubt +// resolves to "count it". +function capturedCallees(ast) { + const bindings = new Set(); + const writes = new Set(); + eachNode(ast, (n) => { + if (n.type === 'VariableDeclarator') for (const id of patternIds(n.id)) bindings.add(id); + else if (n.type === 'CatchClause') for (const id of patternIds(n.param)) bindings.add(id); + else if (n.type === 'ClassDeclaration' || n.type === 'ClassExpression') { + if (n.id) bindings.add(n.id); + } else if (FUNCTION_TYPES.has(n.type) && Array.isArray(n.params)) { + if (n.id) bindings.add(n.id); + for (const p of n.params) for (const id of patternIds(p)) bindings.add(id); + } else if (n.type === 'AssignmentExpression') { + for (const id of patternIds(n.left)) writes.add(id); + } else if (n.type === 'UpdateExpression') { + for (const id of patternIds(n.argument)) writes.add(id); + } else if (n.type === 'ForInStatement' || n.type === 'ForOfStatement') { + if (n.left.type !== 'VariableDeclaration') + for (const id of patternIds(n.left)) writes.add(id); + } + }); + + const info = new Map(); // name -> { binds, decl, scope, strict, bad } + const entry = (name) => { + if (!info.has(name)) info.set(name, { binds: 0, scope: null, strict: false, bad: false }); + return info.get(name); + }; + const refs = []; // [name, ancestors snapshot] + let sawEval = false; + + eachNode(ast, (n, ancestors) => { + if (n.type !== 'Identifier') return; + const parent = ancestors[ancestors.length - 2]; + if (bindings.has(n)) { + const e = entry(n.name); + e.binds++; + const scope = + parent && parent.type === 'FunctionDeclaration' && parent.id === n + ? ancestors[ancestors.length - 3] + : null; + const owner = ancestors[ancestors.length - 4]; + const inBody = + scope && + (scope.type === 'Program' || + (owner && FUNCTION_TYPES.has(owner.type) && owner.body === scope)); + if (!inBody) { + e.bad = true; + return; + } + e.scope = scope; + // Strict when the declaration itself or any enclosing function/program says + // so; `ancestors` includes the declaration. + for (const a of ancestors) { + if (a.type === 'Program' && hasUseStrict(a.body)) e.strict = true; + else if ( + FUNCTION_TYPES.has(a.type) && + a.body?.type === 'BlockStatement' && + hasUseStrict(a.body.body) + ) { + e.strict = true; + } + } + return; + } + if (writes.has(n)) { + entry(n.name).bad = true; + return; + } + // Positions that name a property or label rather than read the binding. + if (!parent) return; + if (parent.type === 'MemberExpression' && parent.property === n && !parent.computed) return; + if ( + (parent.type === 'Property' || + parent.type === 'MethodDefinition' || + parent.type === 'PropertyDefinition') && + parent.key === n && + !parent.computed + ) { + return; + } + if ( + parent.type === 'LabeledStatement' || + parent.type === 'BreakStatement' || + parent.type === 'ContinueStatement' + ) + return; + if (parent.type === 'MetaProperty') return; + if (n.name === 'eval') sawEval = true; + + const grand = ancestors[ancestors.length - 3]; + const directCall = parent.type === 'CallExpression' && parent.callee === n; + const invokeReceiver = + parent.type === 'MemberExpression' && + parent.object === n && + (propName(parent) === 'call' || + propName(parent) === 'apply' || + propName(parent) === 'bind') && + grand && + grand.type === 'CallExpression' && + grand.callee === parent; + if (!directCall && !invokeReceiver) entry(n.name).bad = true; + else refs.push([n.name, ancestors.slice()]); + }); + + const out = new Set(); + if (sawEval) return out; + for (const [name, e] of info) { + if (e.binds === 1 && e.scope && e.strict && !e.bad) out.add(name); + } + // Rule 1's second half: a reference outside the declaring scope is a global. + for (const [name, ancestors] of refs) { + if (out.has(name) && !ancestors.includes(info.get(name).scope)) out.delete(name); + } + return out; +} + // countSource parses `src` and reports every pollutable site, per class, with the // line each sits on. `eager: false` scans it as a module instantiated after user // code can run (see exemptAsPristine); the default is eager, which is what a bare @@ -346,6 +540,7 @@ function countSource(src, label = '', { eager = true } = {}) { const hits = []; const at = (node, kind, name) => hits.push({ line: node.loc.start.line, kind, name }); + const captured = capturedCallees(ast); walk.ancestor(ast, { MemberExpression(node, _state, ancestors) { @@ -391,7 +586,14 @@ function countSource(src, label = '', { eager = true } = {}) { (parent.type === 'CallExpression' || parent.type === 'NewExpression') && parent.callee === node; - if (isCallee) { + // `.bind` is counted in `method`, `.call`/`.apply` in `invoke`; all three + // resolve through the locked Function.prototype, so a receiver no user code + // can reach is exempt from both (capturedCallees has the rule and why). + const capturedReceiver = + (name === 'bind' || INVOKE.has(name)) && + node.object.type === 'Identifier' && + captured.has(node.object.name); + if (isCallee && !capturedReceiver) { if (POLLUTABLE.has(name)) at(node, 'method', `.${name}()`); else if (INVOKE.has(name)) at(node, 'invoke', `.${name}()`); } @@ -466,6 +668,14 @@ function countSource(src, label = '', { eager = true } = {}) { // author never looked at. A mixed line must name its class: // `// primordials-ok: method` (or a comma-separated list). An unrecognized name // suppresses nothing, so a typo fails loud rather than quiet. +// +// Either form may carry a reason after ONE separator, an em dash: +// `// primordials-ok: invoke — receiver is our own closure`. One spelling, not +// three: a second accepted separator is a second grammar to keep in sync, and a +// wrong one (`--`, `-`, a bare `: reason`) suppresses nothing, which fails loud +// the same way a class typo does. The reason must be non-empty. Before this the +// reason had to sit on the line above, away from the site it justified. +const MARKER = /^\s*primordials-ok(?::\s*([\w,\s]+?))?\s*(?:—\s*\S.*)?$/; function applySuppression(hits, lineComments) { const byLine = new Map(); for (const h of hits) { @@ -475,7 +685,7 @@ function applySuppression(hits, lineComments) { const kept = []; for (const [line, lineHits] of byLine) { const comment = lineComments.get(line); - const marker = comment ? /^\s*primordials-ok(?::\s*([\w,\s]+))?\s*$/.exec(comment) : null; + const marker = comment ? MARKER.exec(comment) : null; if (!marker) { kept.push(...lineHits); continue; diff --git a/scripts/lib/primordials-fixtures.mjs b/scripts/lib/primordials-fixtures.mjs index 1cc36df..433d4f0 100644 --- a/scripts/lib/primordials-fixtures.mjs +++ b/scripts/lib/primordials-fixtures.mjs @@ -25,6 +25,7 @@ import { KINDS, countSource } from './primordials-detect.mjs'; // directory would be counted as production source. const E = (o = {}) => ({ method: 0, invoke: 0, accessor: 0, global: 0, ...o }); const WRAP = (body) => `(function (require, module) {\n${body}\n})`; +const STRICT = (body) => WRAP(`'use strict';\n${body}`); const SELF_TEST = [ // --- the four classes, each in its plainest form --- @@ -386,6 +387,127 @@ const SELF_TEST = [ src: 'q.push(x); // primordials-ok: methods', expect: E({ method: 1 }), }, + + // --- marker with a reason: ` — ` (an em dash) and nothing else --- + { + name: 'named marker with a reason suppresses', + src: 'fn.call(t, a); // primordials-ok: invoke — receiver is our own closure', + expect: E(), + }, + { + name: 'bare marker with a reason suppresses', + src: 'q.push(x); // primordials-ok — q is a Fifo, not an Array', + expect: E(), + }, + { + name: 'typo class with a reason still suppresses nothing', + src: 'fn.call(t, a); // primordials-ok: invok — receiver is our own closure', + expect: E({ invoke: 1 }), + }, + { + name: 'a separator other than the em dash suppresses nothing', + src: 'fn.call(t, a); // primordials-ok: invoke -- receiver is our own closure', + expect: E({ invoke: 1 }), + }, + { + name: 'an empty reason suppresses nothing', + src: 'fn.call(t, a); // primordials-ok: invoke —', + expect: E({ invoke: 1 }), + }, + + // --- invoke: the captured-callee exemption (see capturedCallees) --- + // The one shape that is exempt: a strict, file-private function declaration + // that never escapes, so no user code can hold it to give it an own `call`. + { + name: 'private function declaration: .call/.apply/.bind exempt', + src: STRICT( + 'function g(a) { return a; }\n' + + 'function f(t, x) { g.call(t, x); g.apply(t, [x]); return g.bind(t); }\n' + + 'f(1, 2);', + ), + expect: E(), + }, + { + name: 'parameter receiver is counted', + src: STRICT('function f(cb, t) { return cb.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'reassigned declaration is counted', + src: STRICT('function g() {}\ng = h;\nfunction f(t) { return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'local alias of a callback is counted', + src: STRICT('function f(cb, t) { var g = cb; return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'shadowing parameter inside a function is counted', + src: STRICT('function g() {}\nfunction f(g, t) { return g.call(t); }\nf(1, 2);'), + expect: E({ invoke: 1 }), + }, + { + name: 'shadowing local inside a function is counted', + src: STRICT('function g() {}\nfunction f(t) { var g = t.cb; return g.call(t); }\nf(1);'), + expect: E({ invoke: 1 }), + }, + // Rule 1 is "one binding", not a scope analysis: a second DECLARATION of the + // name is counted even where it would resolve to another private function. + // This is the only fixture binds === 1 alone pins — a parameter or local + // shadow is also refused by the not-a-function-declaration check. + { + name: 'second declaration of the name is counted', + src: STRICT('function g() {}\nfunction f(t) { function g() {} return g.call(t); }\nf(1);'), + expect: E({ invoke: 1 }), + }, + { + name: 'exported declaration is counted', + src: STRICT('function g() {}\nmodule.exports = g;\nfunction f(t) { return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'constructed declaration is counted (instance.constructor leaks it)', + src: STRICT('function g() {}\nvar o = new g();\nfunction f(t) { return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'sloppy declaration is counted (callee.caller leaks it)', + src: WRAP('function g() {}\nfunction f(t) { return g.call(t); }\nf(1);'), + expect: E({ invoke: 1 }), + }, + { + name: 'declaration out of the reference scope is counted', + src: STRICT('(function () { function g() {} g(); })();\nfunction f(t) { return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + { + name: 'direct eval disqualifies every declaration', + src: STRICT('function g() {}\nfunction f(t, s) { eval(s); return g.call(t); }'), + expect: E({ invoke: 1 }), + }, + // NOT exempt, deliberately: a captured INTRINSIC. The #485 lock stops + // `Function.prototype.call = x` but not `Function.prototype.apply.call = x`, + // which defines an own `call` on the very object `apply` captured; Lava then + // runs it where node invokes the listener (probed: an emit printed the + // attacker's marker). Same for every raw intrinsic the primordials table + // re-exports (`ReflectApply`, `ObjectKeys`, …). + { + name: 'captured intrinsic receiver is counted', + src: STRICT( + 'var apply = Function.prototype.apply;\nfunction f(cb, t, a) { return apply.call(cb, t, a); }', + ), + expect: E({ invoke: 1 }), + }, + { + name: 'destructured primordial receiver is counted', + src: STRICT( + "var { ReflectApply } = require('internal/primordials');\n" + + 'var P = require("internal/primordials");\nvar push = P.ArrayPrototypePush;\n' + + 'function f(t, a) { ReflectApply.call(null, t, a); return push.call(t, a); }', + ), + expect: E({ invoke: 2 }), + }, ]; function selfTest() { diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 06b06de..7f5bebb 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2112,6 +2112,78 @@ "gate": "node-test:scripts/check-primordials.test.mjs", "expect_detail": "lazy module: module-eval capture is a live read" }, + { + "name": "the detector's fixtures die when the captured-callee rule ignores a reassignment", + "why": "A reassigned name can hold a caller-supplied function by the time `g.call(\u2026)` runs, so exempting it scores a steerable invoke as 0.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (writes.has(n)) {\n entry(n.name).bad = true;\n return;\n }", + "replace": " if (writes.has(n)) {\n return;\n }", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "reassigned declaration is counted" + }, + { + "name": "the detector's fixtures die when the captured-callee rule stops treating other uses as escapes", + "why": "Once a function is exported, passed or constructed, user code can define an own `call` on it (the #485 setter allows it), so `.call` on it is steerable.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (!directCall && !invokeReceiver) entry(n.name).bad = true;", + "replace": " if (false) entry(n.name).bad = true;", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "exported declaration is counted" + }, + { + "name": "the detector's fixtures die when the captured-callee rule tolerates a second binding", + "why": "One binding is the rule, not a scope analysis; a second declaration of the name must not be resolved by guessing which one a use site sees.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (e.binds === 1 && e.scope && e.strict && !e.bad) out.add(name);", + "replace": " if (e.binds >= 1 && e.scope && e.strict && !e.bad) out.add(name);", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "second declaration of the name is counted" + }, + { + "name": "the detector's fixtures die when the captured-callee rule exempts a sloppy declaration", + "why": "A sloppy function is reachable as `callback.caller` from any user callback it invokes, after which its own `call` can be set.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (e.binds === 1 && e.scope && e.strict && !e.bad) out.add(name);", + "replace": " if (e.binds === 1 && e.scope && !e.bad) out.add(name);", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "sloppy declaration is counted" + }, + { + "name": "the detector's fixtures die when the captured-callee rule ignores the declaring scope", + "why": "Outside the declaring scope the same name resolves to a global, which user code can replace.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (out.has(name) && !ancestors.includes(info.get(name).scope)) out.delete(name);", + "replace": " if (false) out.delete(name);", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "declaration out of the reference scope is counted" + }, + { + "name": "the detector's fixtures die when the captured-callee rule ignores direct eval", + "why": "Direct eval in strict code can still assign an outer binding, so no declaration in such a file is provably fixed.", + "source": "scripts/lib/primordials-detect.mjs", + "find": " if (sawEval) return out;\n", + "replace": "", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "direct eval disqualifies every declaration" + }, + { + "name": "the detector's fixtures die when the marker rejects a trailing reason again", + "why": "A reason after the em dash must suppress like the bare marker; before, the reason had to sit on the line above.", + "source": "scripts/lib/primordials-detect.mjs", + "find": "const MARKER = /^\\s*primordials-ok(?::\\s*([\\w,\\s]+?))?\\s*(?:\u2014\\s*\\S.*)?$/;", + "replace": "const MARKER = /^\\s*primordials-ok(?::\\s*([\\w,\\s]+))?\\s*$/;", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "named marker with a reason suppresses" + }, + { + "name": "the detector's fixtures die when the marker accepts a second separator", + "why": "One separator is the grammar; a second is a second spelling to keep in sync, and a wrong one must suppress nothing so it fails loud.", + "source": "scripts/lib/primordials-detect.mjs", + "find": "const MARKER = /^\\s*primordials-ok(?::\\s*([\\w,\\s]+?))?\\s*(?:\u2014\\s*\\S.*)?$/;", + "replace": "const MARKER = /^\\s*primordials-ok(?::\\s*([\\w,\\s]+?))?\\s*(?:(?:\u2014|--)\\s*\\S.*)?$/;", + "gate": "node-test:scripts/check-primordials.test.mjs", + "expect_detail": "a separator other than the em dash suppresses nothing" + }, { "name": "the ratchet dies when every module is scanned as eager", "why": "Classifying a lazy module as eager re-exempts its module-eval captures, so a live `var S = String` in a lazily required module scores 0. Gated on a fixture tree rather than the real one: since #430 no lazy module in the tree holds such a capture, so the mutant changed no count there and survived.", From 84e3911131f5d05302c9d3823fb8cc8bce395b36 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:13:04 +0200 Subject: [PATCH 54/61] test(scripts): bound CLI spawns, pin clean stderr and ratchet OK figures - js-graph.mjs header names lib/primordials-scan.mjs as the eager-set consumer (the scan moved there in #484), CLI in parentheses. - check-js-graph.test.mjs / check-primordials.test.mjs: every spawnSync goes through one run() helper with a 60 s timeout that fails the test, by arm, on a timeout or signal before any other assertion. - graph CLI clean-tree runs assert empty stderr. - ratchet CLI test asserts the OK line's figures equal an in-process scanTree(ROOT), so printing OK without scanning no longer passes. - mutation manifest: [node] arm runs bun; graph CLI writes stderr on a clean run; ratchet CLI prints OK without scanning. Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.test.mjs | 39 +++++++++++++++++++++++++++--- scripts/check-primordials.test.mjs | 35 +++++++++++++++++++++++++-- scripts/lib/js-graph.mjs | 11 +++++---- tests/mutation-manifest.json | 30 +++++++++++++++++++++++ 4 files changed, 104 insertions(+), 11 deletions(-) diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index d34b0f1..ba54a23 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -98,12 +98,32 @@ test('the real tree is clean', () => { // [node] arm running bun, under `bun run js:scripttests` behind a node shim) would // pass every recorded mutation while testing the wrong runtime. const CLI = join(import.meta.dirname, 'check-js-graph.mjs'); + +// Every spawn below goes through here so a hung runtime fails ITS test, by name, +// instead of hanging the whole suite with nothing attached. The bound is generous on +// purpose: the CLI finishes in well under a second on a dev box (~0.1 s wall under +// node and bun, `time` over one run each), so 60 s only ever trips on a real hang, +// never on a slow CI core. A timeout and an outside kill both leave `signal` set; +// checking it before anything else keeps either from surfacing as a confusing +// "did not print its OK line". +const SPAWN_TIMEOUT_MS = 60_000; +function run(label, bin, args) { + const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); + assert.equal( + r.signal, + null, + r.error?.code === 'ETIMEDOUT' + ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` + : `${label} was killed by ${r.signal}`, + ); + return r; +} const RUNTIMES = [ ['node', () => process.execPath, 'undefined'], [ 'bun', () => { - const r = spawnSync('bun', ['--version'], { encoding: 'utf8' }); + const r = run('graph CLI [bun] version probe', 'bun', ['--version']); assert.ok( !r.error && r.status === 0, `bun not runnable from PATH (${r.error?.code ?? `exit ${r.status}`}); make check-js runs the graph CLI under bun, so it is required here`, @@ -117,7 +137,10 @@ const RUNTIMES = [ for (const [rt, resolveBin, typeofBun] of RUNTIMES) { const exe = () => { const bin = resolveBin(); - const r = spawnSync(bin, ['-e', 'process.stdout.write(typeof Bun)'], { encoding: 'utf8' }); + const r = run(`graph CLI [${rt}] identity probe`, bin, [ + '-e', + 'process.stdout.write(typeof Bun)', + ]); assert.equal(r.stdout, typeofBun, `graph CLI [${rt}] binary is not ${rt}: ${bin}`); return bin; }; @@ -142,13 +165,21 @@ for (const [rt, resolveBin, typeofBun] of RUNTIMES) { ['direct path', CLI], ['symlink', link], ]) { - const r = spawnSync(bin, [path], { encoding: 'utf8' }); + const r = run(`graph CLI [${rt}] (${how})`, bin, [path]); assert.equal(r.status, 0, `graph CLI [${rt}] (${how}) exited ${r.status}: ${r.stderr}`); assert.match( r.stdout, /^OK: embedded JS module graph is literal/m, `graph CLI [${rt}] (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, ); + // A clean run says OK and nothing else. CI shows stderr as the failure channel, + // so a warning or a half-printed violation list beside a 0 exit is a gate + // whose output contradicts its verdict. + assert.equal( + r.stderr, + '', + `graph CLI [${rt}] (${how}) wrote to stderr on a clean run: ${JSON.stringify(r.stderr)}`, + ); } } finally { rmSync(dir, { recursive: true, force: true }); @@ -164,7 +195,7 @@ for (const [rt, resolveBin, typeofBun] of RUNTIMES) { const bin = exe(); const root = tree({ 'pkg/runtime/globals.odin': ODIN.replace('"node/buffer"', '"buffer"') }); try { - const r = spawnSync(bin, [CLI, root], { encoding: 'utf8' }); + const r = run(`graph CLI [${rt}] (violating tree)`, bin, [CLI, root]); // OK first: a CLI that skips the check also exits 0, and this keeps the two // mutants (wrong exit code, result discarded) reporting distinct failures. assert.doesNotMatch( diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index ce4786f..1eee5b0 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -19,9 +19,10 @@ import { spawnSync } from 'node:child_process'; import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; -import { KINDS, countSource, countFile, walkDir, JS_DIR } from './lib/primordials-detect.mjs'; +import { KINDS, countSource, countFile, walkDir, JS_DIR, ROOT } from './lib/primordials-detect.mjs'; import { SELF_TEST, E } from './lib/primordials-fixtures.mjs'; import { scanTree } from './lib/primordials-scan.mjs'; +import { totals } from './lib/primordials-baseline.mjs'; test('detector fixtures', async (t) => { for (const fixture of SELF_TEST) { @@ -118,8 +119,34 @@ test('ratchet scan: a lazy module-eval capture counts as global, an eager one do // gate exited 0 having printed nothing. The scan now lives in lib/ and the CLI has // no guard; this pins both the plain invocation and the symlinked one, since an // exit code alone cannot tell "checked and passed" from "never checked". +// +// Nor can the OK line by itself: a CLI that printed it without scanning passed this +// test too. The CLI takes no root, so there is no violating fixture to run it over +// (the graph CLI's answer); instead the line's figures must be the ones this process +// scans off the same tree. What that still cannot see is a CLI that scans nothing +// and echoes the baseline's own totals: on a tree that passes, the two are equal by +// definition. +// +// The spawn is bounded so a hung CLI fails this test by name rather than hanging the +// suite: the CLI takes ~0.2 s wall here (`time`, three runs), so 60 s only trips on a +// real hang. A timeout and an outside kill both leave `signal` set. +const SPAWN_TIMEOUT_MS = 60_000; +function run(label, bin, args) { + const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); + assert.equal( + r.signal, + null, + r.error?.code === 'ETIMEDOUT' + ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` + : `${label} was killed by ${r.signal}`, + ); + return r; +} + test('ratchet CLI reports OK on the real tree, also when invoked through a symlink', () => { const cli = join(import.meta.dirname, 'check-primordials.mjs'); + const { perKind, total } = totals(scanTree(ROOT).counts); + const okLine = `OK: pollution ratchet holds (${total} sites: ${KINDS.map((k) => `${k} ${perKind[k]}`).join(', ')}).`; const dir = mkdtempSync(join(tmpdir(), 'primordials-cli-')); try { const link = join(dir, 'check-primordials.mjs'); @@ -128,13 +155,17 @@ test('ratchet CLI reports OK on the real tree, also when invoked through a symli ['direct path', cli], ['symlink', link], ]) { - const r = spawnSync(process.execPath, [path], { encoding: 'utf8' }); + const r = run(`ratchet CLI (${how})`, process.execPath, [path]); assert.equal(r.status, 0, `ratchet CLI (${how}) exited ${r.status}: ${r.stderr}`); assert.match( r.stdout, /^OK: pollution ratchet holds/m, `ratchet CLI (${how}) did not print its OK line; stdout: ${JSON.stringify(r.stdout)}`, ); + assert.ok( + r.stdout.split('\n').includes(okLine), + `ratchet CLI (${how}) OK line does not report the in-process scan; want ${JSON.stringify(okLine)}, stdout: ${JSON.stringify(r.stdout)}`, + ); } } finally { rmSync(dir, { recursive: true, force: true }); diff --git a/scripts/lib/js-graph.mjs b/scripts/lib/js-graph.mjs index ff81708..8b9a72d 100644 --- a/scripts/lib/js-graph.mjs +++ b/scripts/lib/js-graph.mjs @@ -1,10 +1,11 @@ // The embedded runtime JS module graph — ONE parser and graph builder, two consumers: // -// scripts/lib/js-graph-check.mjs literal, resolved, reachable, embedded, acyclic -// (reported by the scripts/check-js-graph.mjs CLI) -// scripts/check-primordials.mjs which modules are EAGER (instantiated before user -// code), because only there is a module-eval -// capture pristine (CLAUDE.md §5) +// scripts/lib/js-graph-check.mjs literal, resolved, reachable, embedded, acyclic +// (reported by the scripts/check-js-graph.mjs CLI) +// scripts/lib/primordials-scan.mjs which modules are EAGER (instantiated before user +// code), because only there is a module-eval +// capture pristine (CLAUDE.md §5) +// (reported by the scripts/check-primordials.mjs CLI) // // A module id is its path under pkg/runtime/js without `.js` (`node/buffer`, // `internal/primordials`). An edge is TOP-LEVEL when the require runs at module-eval: diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index ec2acf7..910bfff 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2155,6 +2155,26 @@ "test": "graph CLI [bun] reports OK on the real tree, also when invoked through a symlink", "expect_detail": "graph CLI [bun] binary is not bun" }, + { + "name": "the graph CLI [node] arm dies when it runs bun instead of node", + "why": "The mirror of the [bun] identity entry: under `bun run js:scripttests` behind a node shim the [node] arm could resolve to bun, and every CLI mutant fails both runtimes with the same message, so the [node] entries above would pass while the node arm never ran node. The identity probe (typeof Bun must be 'undefined') is the only thing that tells the runtimes apart.", + "source": "scripts/check-js-graph.test.mjs", + "find": " ['node', () => process.execPath, 'undefined'],", + "replace": " ['node', () => 'bun', 'undefined'],", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI [node] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [node] binary is not node" + }, + { + "name": "the graph CLI dies when it writes to stderr on a clean run", + "why": "CI reads stderr as the failure channel. A clean run that exits 0 and prints OK while also writing a warning or a stray violation header is a gate whose output contradicts its verdict; status and the OK line both still pass, so only the empty-stderr assertion sees it.", + "source": "scripts/check-js-graph.mjs", + "find": "console.log(\n 'OK: embedded JS module graph", + "replace": "console.error('JS module graph: 0 violation(s):');\nconsole.log(\n 'OK: embedded JS module graph", + "gate": "node-test:scripts/check-js-graph.test.mjs", + "test": "graph CLI [node] reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "graph CLI [node] (direct path) wrote to stderr on a clean run" + }, { "name": "builtin_factories_compile_on_first_require dies when a lazy builtin is compiled before user code", "why": "Restores eager compilation for the one module the test watches (node:os): if the loader compiles it at init, its first require compiles nothing and the startup win this pins is gone without any visible behavior change.", @@ -2284,6 +2304,16 @@ "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", "expect_detail": "ratchet CLI (symlink) did not print its OK line" }, + { + "name": "the ratchet CLI dies when it prints OK without scanning", + "why": "An OK line and exit 0 were all the CLI spawn test checked, so a CLI that printed the line without running the self-test, the scan or the comparison passed. The line's figures must now equal what the test scans in-process over the same tree. (Echoing the baseline's own totals is still invisible here: on a passing tree they equal the scan by definition.)", + "source": "scripts/check-primordials.mjs", + "find": "const selfTestFailures = selfTest();", + "replace": "console.log('OK: pollution ratchet holds.');\nprocess.exit(0);\nconst selfTestFailures = selfTest();", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", + "expect_detail": "ratchet CLI (direct path) OK line does not report the in-process scan" + }, { "name": "check-js-graph fixtures die when a function-body require makes its target eager", "why": "A require inside a function runs when that function is called, possibly after user code; propagating eagerness through it exempts a lazy module's captures.", From b854f7b344e085ee7db77b81599645d918a1e371 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:19:19 +0200 Subject: [PATCH 55/61] test(scripts): name spawn errors before signals; narrow the ratchet OK claim - run() in both CLI tests reports r.error first (ETIMEDOUT as a timeout, anything else by code, e.g. ENOBUFS, which also leaves SIGTERM), then the signal; ENOENT still reaches the bun arm's PATH message. - The ratchet CLI test comment and its manifest `why` claim only what is pinned (OK figures equal the in-process scan) and list the survivors: echoing baseline totals, skipping selfTest(), skipping compare(). Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.test.mjs | 25 ++++++++++++---------- scripts/check-primordials.test.mjs | 34 ++++++++++++++++++------------ tests/mutation-manifest.json | 2 +- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index ba54a23..5e4e016 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -102,20 +102,23 @@ const CLI = join(import.meta.dirname, 'check-js-graph.mjs'); // Every spawn below goes through here so a hung runtime fails ITS test, by name, // instead of hanging the whole suite with nothing attached. The bound is generous on // purpose: the CLI finishes in well under a second on a dev box (~0.1 s wall under -// node and bun, `time` over one run each), so 60 s only ever trips on a real hang, -// never on a slow CI core. A timeout and an outside kill both leave `signal` set; -// checking it before anything else keeps either from surfacing as a confusing -// "did not print its OK line". +// node and bun, `time` over three runs each), so 60 s only ever trips on a real +// hang, never on a slow CI core. Checking the spawn before anything else keeps a +// timeout or a kill from surfacing as a confusing "did not print its OK line". const SPAWN_TIMEOUT_MS = 60_000; function run(label, bin, args) { const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); - assert.equal( - r.signal, - null, - r.error?.code === 'ETIMEDOUT' - ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` - : `${label} was killed by ${r.signal}`, - ); + // The error first: a timeout AND an overflowed maxBuffer (ENOBUFS) both kill the + // child with SIGTERM, so reading the signal alone misnames the second as an outside + // kill. ENOENT passes through untouched, for the caller's own missing-binary message. + if (r.error && r.error.code !== 'ENOENT') { + assert.fail( + r.error.code === 'ETIMEDOUT' + ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` + : `${label} failed to run: ${r.error.code ?? r.error.message}`, + ); + } + assert.equal(r.signal, null, `${label} was killed by ${r.signal}`); return r; } const RUNTIMES = [ diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index 1eee5b0..f39ab42 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -120,26 +120,32 @@ test('ratchet scan: a lazy module-eval capture counts as global, an eager one do // no guard; this pins both the plain invocation and the symlinked one, since an // exit code alone cannot tell "checked and passed" from "never checked". // -// Nor can the OK line by itself: a CLI that printed it without scanning passed this -// test too. The CLI takes no root, so there is no violating fixture to run it over -// (the graph CLI's answer); instead the line's figures must be the ones this process -// scans off the same tree. What that still cannot see is a CLI that scans nothing -// and echoes the baseline's own totals: on a tree that passes, the two are equal by -// definition. +// Nor can the OK line by itself: a CLI that printed a bare OK passed this test +// too. What is pinned now is narrow: the figures in the OK line equal what this +// process scans off the same tree. NOT pinned, each checked by hand to pass every +// scripts test: a CLI that echoes the baseline's own totals instead of scanning (on +// a passing tree they are equal by definition), one that skips selfTest(), and one +// that skips compare(). The real tree is clean and the fixtures pass, so neither +// step's failing branch is ever reached. Closing them needs a root/baseline override +// on the CLI so a fixture can drive those branches, as the graph CLI's root does. // // The spawn is bounded so a hung CLI fails this test by name rather than hanging the // suite: the CLI takes ~0.2 s wall here (`time`, three runs), so 60 s only trips on a -// real hang. A timeout and an outside kill both leave `signal` set. +// real hang. const SPAWN_TIMEOUT_MS = 60_000; function run(label, bin, args) { const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); - assert.equal( - r.signal, - null, - r.error?.code === 'ETIMEDOUT' - ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` - : `${label} was killed by ${r.signal}`, - ); + // The error first: a timeout AND an overflowed maxBuffer (ENOBUFS) both kill the + // child with SIGTERM, so reading the signal alone misnames the second as an outside + // kill. ENOENT passes through untouched, for the caller's own missing-binary message. + if (r.error && r.error.code !== 'ENOENT') { + assert.fail( + r.error.code === 'ETIMEDOUT' + ? `${label} timed out after ${SPAWN_TIMEOUT_MS} ms` + : `${label} failed to run: ${r.error.code ?? r.error.message}`, + ); + } + assert.equal(r.signal, null, `${label} was killed by ${r.signal}`); return r; } diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 910bfff..870b5de 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2306,7 +2306,7 @@ }, { "name": "the ratchet CLI dies when it prints OK without scanning", - "why": "An OK line and exit 0 were all the CLI spawn test checked, so a CLI that printed the line without running the self-test, the scan or the comparison passed. The line's figures must now equal what the test scans in-process over the same tree. (Echoing the baseline's own totals is still invisible here: on a passing tree they equal the scan by definition.)", + "why": "An OK line and exit 0 were all the CLI spawn test checked, so a CLI that printed a bare OK line passed. Pinned now: the OK line's figures equal what the test scans in-process over the same tree, and nothing more. Still surviving every scripts test: echoing the baseline's own totals instead of scanning (equal on a passing tree by definition), skipping selfTest(), and skipping compare(). Closing those needs a root/baseline override on the CLI.", "source": "scripts/check-primordials.mjs", "find": "const selfTestFailures = selfTest();", "replace": "console.log('OK: pollution ratchet holds.');\nprocess.exit(0);\nconst selfTestFailures = selfTest();", From f9eb1b85550d192a52aeb478a6775ef01f8bfefa Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:28:13 +0200 Subject: [PATCH 56/61] test(scripts): pin the ratchet CLI's echo, self-test and compare branches Drive the real check-primordials.mjs over a disposable copy of the tree (ROOT follows the CLI's own location), so a new method site and a failing self-test reach the branches the clean real tree never does. Three recorded mutations: echo the baseline, skip selfTest(), skip compare(). Co-Authored-By: Claude Opus 5.5 --- scripts/check-primordials.test.mjs | 108 ++++++++++++++++++++++++++--- tests/mutation-manifest.json | 32 ++++++++- 2 files changed, 131 insertions(+), 9 deletions(-) diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index f39ab42..9036591 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -16,7 +16,16 @@ import test from 'node:test'; import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; -import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'node:fs'; +import { + appendFileSync, + cpSync, + mkdtempSync, + mkdirSync, + readFileSync, + rmSync, + symlinkSync, + writeFileSync, +} from 'node:fs'; import { tmpdir } from 'node:os'; import { dirname, join } from 'node:path'; import { KINDS, countSource, countFile, walkDir, JS_DIR, ROOT } from './lib/primordials-detect.mjs'; @@ -121,13 +130,13 @@ test('ratchet scan: a lazy module-eval capture counts as global, an eager one do // exit code alone cannot tell "checked and passed" from "never checked". // // Nor can the OK line by itself: a CLI that printed a bare OK passed this test -// too. What is pinned now is narrow: the figures in the OK line equal what this -// process scans off the same tree. NOT pinned, each checked by hand to pass every -// scripts test: a CLI that echoes the baseline's own totals instead of scanning (on -// a passing tree they are equal by definition), one that skips selfTest(), and one -// that skips compare(). The real tree is clean and the fixtures pass, so neither -// step's failing branch is ever reached. Closing them needs a root/baseline override -// on the CLI so a fixture can drive those branches, as the graph CLI's root does. +// too. What this test pins is narrow: the figures in the OK line equal what this +// process scans off the same tree. The real tree is clean and the fixtures pass, so +// no failing branch is reached here; a CLI that echoes the baseline's own totals +// instead of scanning (equal on a passing tree by definition), skips selfTest(), or +// skips compare() passes it. Those three are pinned by the sandbox tests below: +// "...fails and names the file when a scanned module gains a site" (echo, compare) +// and "...refuses to report when its self-test fails" (selfTest). // // The spawn is bounded so a hung CLI fails this test by name rather than hanging the // suite: the CLI takes ~0.2 s wall here (`time`, three runs), so 60 s only trips on a @@ -177,3 +186,86 @@ test('ratchet CLI reports OK on the real tree, also when invoked through a symli rmSync(dir, { recursive: true, force: true }); } }); + +// The spawn above runs on the real tree, which is clean and whose fixtures pass, so +// no failing branch of the CLI is ever reached there: a CLI that skipped selfTest(), +// skipped compare(), or skipped the scan and echoed the baseline's own totals (equal +// to the scan on a passing tree, by definition) passed it. These drive those branches +// through the REAL CLI source rather than a root override on it: ROOT is derived from +// the CLI's own location (lib/primordials-detect.mjs), so a copy of the CLI, its lib/, +// the baseline and the scanned tree inside a scratch dir scans that copy. Copied at +// test time from the working tree, so a mutated check-primordials.mjs is what runs; +// node_modules is symlinked in so acorn resolves. +function ratchetSandbox() { + const repo = join(import.meta.dirname, '..'); + const root = mkdtempSync(join(tmpdir(), 'primordials-sandbox-')); + for (const rel of [ + 'scripts/check-primordials.mjs', + 'scripts/lib', + 'pkg/runtime/js', + 'tests/node-compat/pollution-baseline.json', + ]) { + cpSync(join(repo, rel), join(root, rel), { recursive: true }); + } + symlinkSync(join(repo, 'node_modules'), join(root, 'node_modules')); + return { root, cli: join(root, 'scripts', 'check-primordials.mjs') }; +} + +// One pollutable method call added to a lazy module must fail the CLI and name the +// file. A CLI that echoes the baseline instead of scanning, or never runs compare(), +// exits 0 with its OK line here. +test('ratchet CLI fails and names the file when a scanned module gains a site', () => { + const { root, cli } = ratchetSandbox(); + try { + const key = 'node/net.js'; + const base = JSON.parse( + readFileSync(join(root, 'tests', 'node-compat', 'pollution-baseline.json'), 'utf8'), + )[key].method; + appendFileSync( + join(root, 'pkg', 'runtime', 'js', key), + '\nfunction injectedRatchetSite(a) { return a.push(1); }\n', + ); + const r = run('ratchet CLI (sandbox, +1 site)', process.execPath, [cli]); + assert.doesNotMatch( + r.stdout, + /^OK: pollution ratchet holds/m, + `ratchet CLI printed OK over a tree with a new site; stdout: ${JSON.stringify(r.stdout)}`, + ); + assert.equal(r.status, 1, `ratchet CLI exited ${r.status} over a tree with a new site`); + assert.ok( + r.stderr.includes(`${key}: ${base + 1} method sites, baseline ${base} (+1):`), + `ratchet CLI did not report ${key} +1 method; stderr: ${JSON.stringify(r.stderr)}`, + ); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +// A detector whose self-test fails must not report on the tree. The sandbox's +// fixtures are replaced by a stub with the same exports whose selfTest() fails; a CLI +// that skips selfTest() scans the clean copy and prints OK. +test('ratchet CLI refuses to report when its self-test fails', () => { + const { root, cli } = ratchetSandbox(); + try { + writeFileSync( + join(root, 'scripts', 'lib', 'primordials-fixtures.mjs'), + 'export const E = () => ({});\nexport const WRAP = (s) => s;\nexport const SELF_TEST = [];\n' + + "export function selfTest() { return ['injected detector failure']; }\n", + ); + const r = run('ratchet CLI (sandbox, failing self-test)', process.execPath, [cli]); + assert.doesNotMatch( + r.stdout, + /^OK: pollution ratchet holds/m, + `ratchet CLI printed OK with a failing self-test; stdout: ${JSON.stringify(r.stdout)}`, + ); + assert.equal(r.status, 1, `ratchet CLI exited ${r.status} with a failing self-test`); + assert.match( + r.stderr, + /SELF-TEST FAILED/, + `no SELF-TEST FAILED; stderr: ${JSON.stringify(r.stderr)}`, + ); + assert.match(r.stderr, /injected detector failure/, 'self-test failure text not reported'); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index 870b5de..d0861ad 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -2306,7 +2306,7 @@ }, { "name": "the ratchet CLI dies when it prints OK without scanning", - "why": "An OK line and exit 0 were all the CLI spawn test checked, so a CLI that printed a bare OK line passed. Pinned now: the OK line's figures equal what the test scans in-process over the same tree, and nothing more. Still surviving every scripts test: echoing the baseline's own totals instead of scanning (equal on a passing tree by definition), skipping selfTest(), and skipping compare(). Closing those needs a root/baseline override on the CLI.", + "why": "An OK line and exit 0 were all the CLI spawn test checked, so a CLI that printed a bare OK line passed. Pinned now: the OK line's figures equal what the test scans in-process over the same tree. A real tree that passes cannot see a CLI that echoes the baseline, skips selfTest() or skips compare(); the three sandbox entries below pin those through a disposable copy of the tree.", "source": "scripts/check-primordials.mjs", "find": "const selfTestFailures = selfTest();", "replace": "console.log('OK: pollution ratchet holds.');\nprocess.exit(0);\nconst selfTestFailures = selfTest();", @@ -2314,6 +2314,36 @@ "test": "ratchet CLI reports OK on the real tree, also when invoked through a symlink", "expect_detail": "ratchet CLI (direct path) OK line does not report the in-process scan" }, + { + "name": "the ratchet CLI dies when it echoes the baseline instead of scanning", + "why": "On a passing tree the baseline's totals equal the scan's by definition, so the real-tree OK line cannot tell an echo from a scan. A sandbox copy of the tree with one new method site in node/net.js must fail and name the file; a CLI that reads its counts off the baseline prints OK.", + "source": "scripts/check-primordials.mjs", + "find": "const { files, counts, allHits } = scanTree(ROOT);", + "replace": "const { files, counts, allHits } = { files: [], counts: JSON.parse(readFileSync(BASELINE, 'utf8')), allHits: {} };", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI fails and names the file when a scanned module gains a site", + "expect_detail": "ratchet CLI printed OK over a tree with a new site" + }, + { + "name": "the ratchet CLI dies when it skips its self-test", + "why": "A detector gone blind must not report on the tree. The real fixtures pass, so skipping selfTest() is invisible there; a sandbox whose fixtures module fails its self-test must exit 1 with SELF-TEST FAILED, not print OK.", + "source": "scripts/check-primordials.mjs", + "find": "const selfTestFailures = selfTest();", + "replace": "const selfTestFailures = [];", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI refuses to report when its self-test fails", + "expect_detail": "ratchet CLI printed OK with a failing self-test" + }, + { + "name": "the ratchet CLI dies when it skips the baseline comparison", + "why": "The real tree matches its baseline, so a CLI that never compares still prints OK with the right figures there. A sandbox copy with one new method site must exit 1 and name the file.", + "source": "scripts/check-primordials.mjs", + "find": "const { failures, improvements, stale } = compare(counts, baseline);", + "replace": "const { failures, improvements, stale } = { failures: [], improvements: [], stale: [] };", + "gate": "node-test:scripts/check-primordials.test.mjs", + "test": "ratchet CLI fails and names the file when a scanned module gains a site", + "expect_detail": "ratchet CLI printed OK over a tree with a new site" + }, { "name": "check-js-graph fixtures die when a function-body require makes its target eager", "why": "A require inside a function runs when that function is called, possibly after user code; propagating eagerness through it exempts a lazy module's captures.", From a55a4b0836885f5a409367de7d4503c320b06709 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:44:55 +0200 Subject: [PATCH 57/61] test(scripts): SIGKILL bounded CLI spawns; clean up and guard the ratchet sandbox run() in both CLI test files now kills with SIGKILL on timeout: a child trapping SIGTERM held a 500 ms bound for 8 s. ratchetSandbox() removes its temp dir when a copy step throws, and asserts node_modules/acorn up front instead of failing on a misleading missing-line message. Co-Authored-By: Claude Opus 5.5 --- scripts/check-js-graph.test.mjs | 10 +++++++- scripts/check-primordials.test.mjs | 40 +++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/scripts/check-js-graph.test.mjs b/scripts/check-js-graph.test.mjs index 5e4e016..f5a4902 100644 --- a/scripts/check-js-graph.test.mjs +++ b/scripts/check-js-graph.test.mjs @@ -107,7 +107,15 @@ const CLI = join(import.meta.dirname, 'check-js-graph.mjs'); // timeout or a kill from surfacing as a confusing "did not print its OK line". const SPAWN_TIMEOUT_MS = 60_000; function run(label, bin, args) { - const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); + // SIGKILL, not the default SIGTERM: a child that traps SIGTERM outlives the timeout + // and spawnSync waits on it, so the bound would not bound anything (measured: a + // SIGTERM-trapping child with an 8 s sleep held a 500 ms timeout for 8020 ms; with + // SIGKILL it returned ETIMEDOUT at 504 ms). + const r = spawnSync(bin, args, { + encoding: 'utf8', + timeout: SPAWN_TIMEOUT_MS, + killSignal: 'SIGKILL', + }); // The error first: a timeout AND an overflowed maxBuffer (ENOBUFS) both kill the // child with SIGTERM, so reading the signal alone misnames the second as an outside // kill. ENOENT passes through untouched, for the caller's own missing-binary message. diff --git a/scripts/check-primordials.test.mjs b/scripts/check-primordials.test.mjs index 9036591..091210c 100644 --- a/scripts/check-primordials.test.mjs +++ b/scripts/check-primordials.test.mjs @@ -19,6 +19,7 @@ import { spawnSync } from 'node:child_process'; import { appendFileSync, cpSync, + existsSync, mkdtempSync, mkdirSync, readFileSync, @@ -143,7 +144,15 @@ test('ratchet scan: a lazy module-eval capture counts as global, an eager one do // real hang. const SPAWN_TIMEOUT_MS = 60_000; function run(label, bin, args) { - const r = spawnSync(bin, args, { encoding: 'utf8', timeout: SPAWN_TIMEOUT_MS }); + // SIGKILL, not the default SIGTERM: a child that traps SIGTERM outlives the timeout + // and spawnSync waits on it, so the bound would not bound anything (measured: a + // SIGTERM-trapping child with an 8 s sleep held a 500 ms timeout for 8020 ms; with + // SIGKILL it returned ETIMEDOUT at 504 ms). + const r = spawnSync(bin, args, { + encoding: 'utf8', + timeout: SPAWN_TIMEOUT_MS, + killSignal: 'SIGKILL', + }); // The error first: a timeout AND an overflowed maxBuffer (ENOBUFS) both kill the // child with SIGTERM, so reading the signal alone misnames the second as an outside // kill. ENOENT passes through untouched, for the caller's own missing-binary message. @@ -198,16 +207,29 @@ test('ratchet CLI reports OK on the real tree, also when invoked through a symli // node_modules is symlinked in so acorn resolves. function ratchetSandbox() { const repo = join(import.meta.dirname, '..'); + // Without installed deps the symlink below dangles, the copied CLI dies resolving + // acorn, and the tests fail on a missing "+1" line that says nothing about why. + assert.ok( + existsSync(join(repo, 'node_modules', 'acorn')), + 'sandbox needs installed deps: node_modules/acorn missing — run bun install', + ); const root = mkdtempSync(join(tmpdir(), 'primordials-sandbox-')); - for (const rel of [ - 'scripts/check-primordials.mjs', - 'scripts/lib', - 'pkg/runtime/js', - 'tests/node-compat/pollution-baseline.json', - ]) { - cpSync(join(repo, rel), join(root, rel), { recursive: true }); + // The caller's finally only exists once this returns; a failed copy (EACCES, + // ENOSPC) would otherwise leave the temp dir behind. + try { + for (const rel of [ + 'scripts/check-primordials.mjs', + 'scripts/lib', + 'pkg/runtime/js', + 'tests/node-compat/pollution-baseline.json', + ]) { + cpSync(join(repo, rel), join(root, rel), { recursive: true }); + } + symlinkSync(join(repo, 'node_modules'), join(root, 'node_modules')); + } catch (err) { + rmSync(root, { recursive: true, force: true }); + throw err; } - symlinkSync(join(repo, 'node_modules'), join(root, 'node_modules')); return { root, cli: join(root, 'scripts', 'check-primordials.mjs') }; } From 87688b0e4e5e3cee396aa5c446bb8ffdec8345db Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:58:25 +0200 Subject: [PATCH 58/61] bench(gate): widen the url-parse-ipv4 cap to 22.5 for bimodal CI runners Across 28 ubuntu-latest CI bench-gate runs (median of 3 launches per side, Node v26.13.0) url-parse-ipv4 measured 10.37x-16.06x and breached the 14.7x cap 3 times with no runtime change: Node's time on that bench is either ~12 ms or ~5-7 ms depending on the runner, Lava's moves less. Cap set at ~1.4x the max observed; measurements and the detection cost are recorded in thresholds.json. Co-Authored-By: Claude Opus 5.5 --- bench/thresholds.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bench/thresholds.json b/bench/thresholds.json index a5a3a01..a9570ce 100644 --- a/bench/thresholds.json +++ b/bench/thresholds.json @@ -32,9 +32,9 @@ "encode-utf8": 9.5, "encode-into": 11.5, "new-textdecoder": 20.0, - "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%.", + "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%. Recalibrated 2026-11-17: url-parse-ipv4 widened 14.7 -> 22.5 (~1.4x the max observed, the #412 convention). Across 28 ubuntu-latest 4-core CI `make bench-gate` runs (median of 3 launches per side, Node v26.13.0 vs bin/lava -o:speed; runs 43938992938 through 44067776896) it measured 10.37x-16.06x and breached 14.7x in 3 (15.22x, 15.01x, 16.06x) with no runtime change between them. The runners are bimodal: Node's time is either ~11.7-12.1 ms (ratio 10.4-11.3x) or ~4.9-7.5 ms (ratio 11.9-16.1x), and Lava's moves less (~78-134 ms), so the ratio follows the runner, not Lava. The cost: the gate now trips only past 22.5x, i.e. a ~2x regression on the slow-Node runners (~11x) or ~1.4-1.9x on the fast ones (~12-16x); a single cap cannot do better while the two runner populations differ by ~1.4x on their own.", "url-parse-special": 28.0, - "url-parse-ipv4": 14.7, + "url-parse-ipv4": 22.5, "url-parse-octal": 13.5, "url-relative": 12.0, "//urlsearchparams": "sp-parse at ~26-40x is construction cost; lookup is fast (0.62x-0.90x in the 4 ubuntu-latest 4-core CI runs listed above, 0.98x median-of-3 in #403, 1.00x in #336 after url.js primordials hardening). Widened urlsearchparams-get 0.9 -> 1.5 (#336, #362) so CI and unpinned dev runs do not flake at 0.9x-1.0x while still catching multi-fold lookup regressions.", From d3fce9826959d31ffe820c9770ddb79ab99a263e Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:03:08 +0200 Subject: [PATCH 59/61] bench(gate): set the url-parse-ipv4 cap to 20.0 and mark the url caps' fire-proof stale perf-memory-auditor re-read all 28 CI rows: 20.0 (~1.25x the max, >5 sd above the fast-runner mean) has no false positive among them and keeps more detection than 22.5. The note no longer claims no runtime change between breaches (5228c42 moved the parser) and gives the evidence that the breaches track the runner instead. bench/micro/url.js no longer claims the caps are proven to fire: the old hand slowdown breaches none of the current caps. Co-Authored-By: Claude Opus 5.5 --- bench/micro/url.js | 14 +++++++++----- bench/thresholds.json | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bench/micro/url.js b/bench/micro/url.js index 48da4fb..0f57bc4 100644 --- a/bench/micro/url.js +++ b/bench/micro/url.js @@ -30,11 +30,15 @@ // before being benched — a ratio between two runtimes doing different work means // nothing. // -// THE CAPS WERE PROVEN TO FIRE, by hand, and are NOT wired into the mutation gate. -// Slowing basicURLParse's shared entry with ~45 redundant stripChars passes moved -// special 11.34x -> 17.42x, ipv4 -> 15.35x and octal -> 11.68x, all three breaching -// and each named individually, while urlsearchparams-* stayed put — so the caps -// fire and they name the right bench. (The first attempt discarded the redundant +// THE CAPS WERE ONCE PROVEN TO FIRE, by hand, and are NOT wired into the mutation +// gate. Slowing basicURLParse's shared entry with ~45 redundant stripChars passes +// moved special 11.34x -> 17.42x, ipv4 -> 15.35x and octal -> 11.68x, all three +// breaching the caps of that time and each named individually, while +// urlsearchparams-* stayed put. That proof is STALE: the caps have since been +// widened for CI runner noise (bench/thresholds.json "//url": special 28.0, +// ipv4 20.0, octal 13.5), and that same slowdown would breach none of them. No +// current url-* cap has been shown to fire; re-prove one with a slowdown large +// enough to clear it before trusting it as a regression detector. (The first attempt discarded the redundant // result and JIT dead-code elimination removed it: MORE work measured FASTER, 40 // passes 16.70x vs 120 passes 13.90x. A mutation that can be optimised away proves // nothing.) diff --git a/bench/thresholds.json b/bench/thresholds.json index a9570ce..6a168eb 100644 --- a/bench/thresholds.json +++ b/bench/thresholds.json @@ -32,9 +32,9 @@ "encode-utf8": 9.5, "encode-into": 11.5, "new-textdecoder": 20.0, - "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%. Recalibrated 2026-11-17: url-parse-ipv4 widened 14.7 -> 22.5 (~1.4x the max observed, the #412 convention). Across 28 ubuntu-latest 4-core CI `make bench-gate` runs (median of 3 launches per side, Node v26.13.0 vs bin/lava -o:speed; runs 43938992938 through 44067776896) it measured 10.37x-16.06x and breached 14.7x in 3 (15.22x, 15.01x, 16.06x) with no runtime change between them. The runners are bimodal: Node's time is either ~11.7-12.1 ms (ratio 10.4-11.3x) or ~4.9-7.5 ms (ratio 11.9-16.1x), and Lava's moves less (~78-134 ms), so the ratio follows the runner, not Lava. The cost: the gate now trips only past 22.5x, i.e. a ~2x regression on the slow-Node runners (~11x) or ~1.4-1.9x on the fast ones (~12-16x); a single cap cannot do better while the two runner populations differ by ~1.4x on their own.", + "//url": "Added 2026-07-30 with bench/micro/url.js. Recalibrated 2026-11-15 (#362, #403) before enabling `make bench-gate` in CI, after url.js primordials hardening (118c8b5, 5466d59, 2f43897). Measured across 4 ubuntu-latest 4-core CI single-launch runs (runs 43719701306, 43725488015, 43742288199, 43743582297; Node v24.22.2 vs bin/lava -o:speed): url-parse-special 15.24x, 16.84x, 17.20x, 18.45x; url-parse-octal 7.00x, 8.45x, 8.62x, 8.67x; url-relative 6.64x, 7.12x, 7.19x, 7.28x; plus 2 shared-container runs in #403 (median-of-3 `make bench-gate` at cf850ca and 1-launch `make bench` at 49bc489, Node 24.22.1 vs bin/lava -o:speed): special 23.53x/26.69x, relative 9.99x/11.16x, octal 11.47x. Caps give headroom above both environments (special 16.0 -> 28.0, octal 11.4 -> 13.5, relative 7.9 -> 12.0). Do not re-pin to reduce variance; run.mjs records that pinning shifts the level 25-40%. Recalibrated 2026-11-17: url-parse-ipv4 widened 14.7 -> 20.0. Measured across all 28 ubuntu-latest 4-core CI runs with a bench-gate row between runs 43938992938 and 44067776896 (median of 3 launches per side, Node v26.13.0 vs bin/lava -o:speed): 10.37x-16.06x, breaching 14.7x in 3 (15.22x at aea9da8, 15.01x and 16.06x at f9eb1b8/ac9e124). The parser this bench runs did move between those breaches (5228c42 moved it into internal/url), but the breaches track the runner, not Lava: the 3 breaching runs are the 3 with the LOWEST Lava times of all 28 (77-83 ms of a 77-134 ms range), and they are breaches only because Node ran faster still. Node's time clusters at ~11.7-12.1 ms (ratio 10.4-11.3x) or ~4.9-7.5 ms (17 runs under 10.5 ms: mean 13.59x, sd 1.06), with 2 runs between (8.98 ms 13.33x, 10.05 ms 12.79x). 20.0 is ~1.25x the max observed and >5 sd above the fast-cluster mean; it would have had 0 false positives across the 28, while #412's ~1.4x-of-max (22.5) was set from only 3 samples and would give up detection here. Tripping 20.0 without a Lava change needs Node at ~4 ms against Lava's fastest ~77 ms, ~19% faster than the fastest Node of the 28 (4.89 ms): not observed, so medium confidence. The cost: the gate trips on a ~1.8x regression on slow-Node runners (~11x) and ~1.25-1.7x on fast ones (~12-16x). bench/micro/url.js runs 50k iterations, so Node finishes in ~5 ms and launch noise dominates; more iterations may allow a tighter cap (untested).", "url-parse-special": 28.0, - "url-parse-ipv4": 22.5, + "url-parse-ipv4": 20.0, "url-parse-octal": 13.5, "url-relative": 12.0, "//urlsearchparams": "sp-parse at ~26-40x is construction cost; lookup is fast (0.62x-0.90x in the 4 ubuntu-latest 4-core CI runs listed above, 0.98x median-of-3 in #403, 1.00x in #336 after url.js primordials hardening). Widened urlsearchparams-get 0.9 -> 1.5 (#336, #362) so CI and unpinned dev runs do not flake at 0.9x-1.0x while still catching multi-fold lookup regressions.", From 8f5db09a2ebbc6922ba9b7431e1f4234d0f0cdfb Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:54:11 +0200 Subject: [PATCH 60/61] harden(js): lock call/apply/bind and wrapped intrinsics against own-property steering (#494) Function.prototype.call/apply/bind were locked on Function.prototype (#450), but the held functions themselves still took an own `call` or a new prototype: `Function.prototype.apply.call = evil` hijacked every `apply.call(listener, ...)` in events.js, and `Array.prototype.push.call = evil` hijacked the callerN `fn.call(t, ...)` wrappers. node runs neither. lockIntrinsics() now makes call/apply/bind, and exactly the set of methods a primordials wrapper closes over, non-extensible. The set is recorded by the wrapper factories themselves, so a newly wrapped method is covered; a factory call after the lock (the loader's pristine-Buffer uncurryThis) locks on the spot. The wrappers keep `fn.call`: `call.call(fn, ...)` was measured slower than Reflect.apply on Linux JSC (new URL 2.64x). Deviation: the locked functions answer as under node --frozen-intrinsics (isExtensible false, own props and setPrototypeOf throw) but are not frozen. Pinned by cmd/lava/function_call_lock_test.odin. Co-Authored-By: Claude Opus 5.5 --- cmd/lava/function_call_lock_test.odin | 70 ++++++++++++++++++++ pkg/runtime/js/bootstrap/loader.js | 6 +- pkg/runtime/js/internal/primordials.js | 92 +++++++++++++++++++++++++- pkg/runtime/js/node/events.js | 14 ++-- tests/mutation-manifest.json | 31 ++++++++- 5 files changed, 201 insertions(+), 12 deletions(-) diff --git a/cmd/lava/function_call_lock_test.odin b/cmd/lava/function_call_lock_test.odin index 651c83f..6869df4 100644 --- a/cmd/lava/function_call_lock_test.odin +++ b/cmd/lava/function_call_lock_test.odin @@ -103,6 +103,70 @@ record('q2', function () { return s.length + ':' + s.charCodeAt(0) + ':' + s.charCodeAt(8192) + ':' + s.charCodeAt(8999); }); +// #494: the captured intrinsics themselves. Locking the three slots on +// Function.prototype does not stop a script from giving the HELD function an own +// 'call': 'Function.prototype.apply.call = evil' steered every +// 'apply.call(listener, …)' in events.js (node runs the listener). lockIntrinsics() +// now makes call/apply/bind non-extensible, so neither an own property nor a +// re-pointed prototype can change what a read off them finds. +// The same hole on every OTHER method primordials wraps: 'Array.prototype.push.call +// = evil' steered the callerN 'fn.call(t, …)' wrappers. lockIntrinsics() makes +// exactly the wrapped set non-extensible too (the wrappers keep 'fn.call', which is +// the only spelling JSC inlines); a method nobody wraps (Array.prototype.at) keeps +// node's default behavior. Buffer.prototype.toString is wrapped by the loader AFTER +// the lock (pristineBuffer), so it pins the lock-on-wrap path. +// The deviation rows (ext … setproto) are node --frozen-intrinsics' answers (node +// 26.13, probed); default node answers true / length|name / assigned / assigned / +// assigned / 1 / assigned / set. +const evil = function () { return 'HIJACK'; }; +record('ext', function () { + return [Object.isExtensible(real.call), Object.isExtensible(real.apply), Object.isExtensible(real.bind)].join(','); +}); +record('keys', function () { + return [real.call, real.apply, real.bind].map(function (f) { return Object.getOwnPropertyNames(f).join('|'); }).join(','); +}); +// Through lockMethod's setter, which defines an own property: on a non-extensible +// receiver that throws in sloppy code too, as under --frozen-intrinsics. +record('own-call-sloppy', function () { FP.apply.call = evil; return 'assigned'; }); +record('own-apply-strict', function () { 'use strict'; FP.call.apply = evil; return 'assigned'; }); +record('own-bind-strict', function () { 'use strict'; FP.bind.bind = evil; return 'assigned'; }); +// A key with no inherited setter: plain [[Set]] on a non-extensible object. +record('new-key-sloppy', function () { FP.bind.x = 1; return FP.bind.x; }); +record('new-key-strict', function () { 'use strict'; FP.bind.x = 1; return 'assigned'; }); +// Re-pointing the prototype is the other way to change what 'apply.call' reads. +record('setproto', function () { + Object.setPrototypeOf(real.apply, { __proto__: FP, call: evil }); + return 'set'; +}); +record('wrapped-ext', function () { + return [Object.isExtensible(Array.prototype.push), Object.isExtensible(String.prototype.charCodeAt), + Object.isExtensible(Buffer.prototype.toString), Object.isExtensible(Array.prototype.at)].join(','); +}); +record('push-own-sloppy', function () { Array.prototype.push.call = evil; return 'assigned'; }); +record('push-fresh-sloppy', function () { Array.prototype.push.x = 1; return Array.prototype.push.x; }); +record('at-own', function () { + Array.prototype.at.call = evil; + return Object.getOwnPropertyDescriptor(Array.prototype.at, 'call').value === evil; +}); +// The #494 probe: every attempt is made (and may throw on Lava), then the wrappers +// must still run the real push/charCodeAt. +record('push-hijack', function () { + let hit = 0; + const bump = function () { hit++; return 0; }; + try { Array.prototype.push.call = bump; } catch (e) {} + try { Object.setPrototypeOf(String.prototype.charCodeAt, { __proto__: FP, call: bump }); } catch (e) {} + const u = new URLSearchParams('a=1&b=2'); + u.append('c', '3'); + return [hit, u.toString(), new URL('http://a/b?c').href].join(' '); +}); +record('emit-hijack', function () { + const EE = require('events'); + const e = new EE(); + const got = []; + e.on('x', function (a) { got[got.length] = 'listener:' + a; }); + e.emit('x', 1); + return got.join(','); +}); const want = [ 'sloppy-call=THREW:TypeError', 'strict-call=THREW:TypeError', 'define-call=THREW:TypeError', 'intact-call=true', 'sloppy-apply=THREW:TypeError', 'strict-apply=THREW:TypeError', 'define-apply=THREW:TypeError', 'intact-apply=true', @@ -113,6 +177,12 @@ const want = [ 'usp=1', 'format=x', 'buffer=6869', 'url=/b', 'decode=hi', 'decode16=AB', 'h1=http://xn--ber-goa.example/', 'h2=true', 'h3=true', 'h4=195,188,226,130,172,240,159,152,128', 'q2=9000:65:67:68', + 'ext=false,false,false', 'keys=length|name,length|name,length|name', + 'own-call-sloppy=THREW:TypeError', 'own-apply-strict=THREW:TypeError', 'own-bind-strict=THREW:TypeError', + 'new-key-sloppy=undefined', 'new-key-strict=THREW:TypeError', 'setproto=THREW:TypeError', + 'wrapped-ext=false,false,false,true', 'push-own-sloppy=THREW:TypeError', 'push-fresh-sloppy=undefined', 'at-own=true', + 'push-hijack=0 a=1&b=2&c=3 http://a/b?c', + 'emit-hijack=listener:1', ]; const bad = []; for (let i = 0; i < want.length; i++) { diff --git a/pkg/runtime/js/bootstrap/loader.js b/pkg/runtime/js/bootstrap/loader.js index c254ca9..e9fdb35 100644 --- a/pkg/runtime/js/bootstrap/loader.js +++ b/pkg/runtime/js/bootstrap/loader.js @@ -101,8 +101,10 @@ // before user code, so it captures pristine intrinsics (see primordials.js). // Other modules require('internal/primordials') and get this cached, pristine table. // - // Then lock Function.prototype.call/apply/bind (#450; the deviation is documented on - // lockIntrinsics). Here and nowhere else, for two reasons. It must follow the capture, + // Then lock Function.prototype.call/apply/bind (#450) and make them plus every method + // a primordials wrapper holds non-extensible (#494; the deviation is documented on + // lockIntrinsics). The `uncurryThis` calls below run after this line and lock their + // Buffer.prototype methods as they wrap them. Here and nowhere else, for two reasons. It must follow the capture, // which reads the three as plain values. And the loader is the one choke point every // user-code entry runs behind: there is exactly one JSGlobalContextCreate that hosts a // script (runtime.odin eval), install_globals runs this loader before the entry diff --git a/pkg/runtime/js/internal/primordials.js b/pkg/runtime/js/internal/primordials.js index 35d39c9..3176d14 100644 --- a/pkg/runtime/js/internal/primordials.js +++ b/pkg/runtime/js/internal/primordials.js @@ -30,6 +30,13 @@ // construction, Buffer.from, buf.toString, Buffer.concat and new URL throw where // node succeeds, and URLSearchParams.get answer `null` and util.format // `'undefined'` where node is right. +// The lock alone did not close the other half: `fn.call` reads `call` off `fn` +// FIRST, and `fn` is the very intrinsic user code can hold, so +// `Array.prototype.push.call = evil` (an own property) or a setPrototypeOf on `push` +// steered its wrapper where node is unaffected. lockIntrinsics() therefore also makes +// every WRAPPED method non-extensible (#494; the `.call` section below says why that +// and not a different spelling). Methods captured raw OUTSIDE this table and invoked +// with `.call` elsewhere are not covered. // The accessor axis (a setter on a numeric-index property of Array/Object.proto) // is not a method concern and is handled per-hot-array with null prototypes in // the consuming modules. @@ -45,6 +52,25 @@ var ArrayProto = Array.prototype; var StringProto = String.prototype; + // The wrapped set (#494). Every wrapper below reads `fn.call` with `fn` the very + // method user code can hold, so an own `call` on it, or a re-pointed prototype, + // steers the wrapper even with Function.prototype.call locked. lockIntrinsics() + // makes exactly this set non-extensible; a factory call after the lock (the + // loader's pristine-Buffer `uncurryThis` sites run after it, still before user + // code) locks its method on the spot. Recorded here rather than listed by hand in + // lockIntrinsics, so a newly wrapped method cannot be missed. Index writes into a + // plain array are safe: every write happens before user code can plant an index + // setter, and the list is dropped at the lock. Declared up here because the + // first factory call (stringCharCodeAt, below) runs at module eval. + var preventExtensions = Object.preventExtensions; + var wrappedFns = []; + var wrappedCount = 0; + var intrinsicsLocked = false; + function track(fn) { + if (intrinsicsLocked) preventExtensions(fn); + else wrappedFns[wrappedCount++] = fn; + } + // --- the `.call` read, and why the wrappers may keep it --------------- // // Every wrapper below invokes through `fn.call(t, …)`, which READS @@ -87,6 +113,20 @@ // where a forged answer reads another request's bytes. // (safeGetter predates the lock and is kept: it is cold, so reverting it to // `.call` would win nothing measurable.) + // + // The remaining hole (#494) is an own `call` on — or a re-pointed prototype of — + // the HELD method itself, which the lock on Function.prototype cannot see. The + // obvious spelling that reads `.call` only off the locked, non-extensible `call`, + // `call.call(fn, t, …)`, was measured and is WORSE than Reflect.apply: on JSC 2.52.6, + // Linux arm64 (Docker), best of 7 in-process reps, a caller1 over charCodeAt costs + // 0.8-1.0 ns/op as `fn.call`, 52.6-56.6 as `call.call`, 44.8-50.4 as reflectApply + // (three launches each; scratch variants.js), and end to end, medians of 9 + // interleaved launches per arm, new URL went 130.5 -> 344.2 ms per 50k (2.64x) and + // URLSearchParams.get 2.4 -> 9.0 ms per 200k (3.81x). Neither spelling inlines + // through the builtin `call`; only `fn.call` lets the DFG call `fn` directly. So + // the wrappers keep `fn.call`, and the hole is closed on the other side instead: + // lockIntrinsics() makes every wrapped method non-extensible (the wrapped set, + // above), which leaves no own `call` and no prototype re-point for the read to find. var reflectApply = Reflect.apply; var NO_ARGS = Object.freeze([]); function safeGetter(fn) { @@ -172,21 +212,25 @@ // Fixed-arity `.call` wrappers — the fast path on JSC. `fn` is the captured // pristine method; the closure forwards a known number of positional args. function caller0(fn) { + track(fn); return function (t) { return fn.call(t); }; } function caller1(fn) { + track(fn); return function (t, a) { return fn.call(t, a); }; } function caller2(fn) { + track(fn); return function (t, a, b) { return fn.call(t, a, b); }; } function caller3(fn) { + track(fn); return function (t, a, b, c) { return fn.call(t, a, b, c); }; @@ -195,6 +239,7 @@ // more than a fixed-arity closure on JSC, but push/unshift/splice/concat are // never in a per-character loop, so it does not matter in practice. function callerN(fn) { + track(fn); return function (t) { switch (arguments.length) { case 1: @@ -232,14 +277,41 @@ * returns the original method and whose setter refuses Function.prototype itself * but fixes the "override mistake" for every other receiver. Called ONCE, by * bootstrap/loader.js, right after this table is captured and before user code. + * Also makes call/apply/bind and every method a wrapper below holds + * non-extensible (#494). * @throws {TypeError} `Function.prototype.call = x`, sloppy or strict, and any * Object.defineProperty of the three on Function.prototype. + * Also any own property added to the three functions themselves + * (`Function.prototype.apply.call = x`, sloppy or strict; a fresh key only in + * strict code) and Object.setPrototypeOf on them to a different prototype. * @node node 26 lets a script replace all three (assignment succeeds in both * modes). Under `node --frozen-intrinsics` it behaves as here: both modes * throw, defineProperty throws, and `f.call = x` on an ordinary function `f` * defines an own writable/enumerable/configurable property (all four rows - * probed on node 26.13). - * @deviates Default node allows the replacement; Lava throws. Traded for: the + * probed on node 26.13). The same flag makes the three functions + * non-extensible; this matches its answers for isExtensible, an own `call`/ + * `apply`/`bind` (throws in both modes), a fresh key (sloppy no-op, strict + * throws) and setPrototypeOf (throws) — probed on node 26.13. It also FREEZES + * them; Lava does not (`name`/`length` stay configurable, isFrozen false). + * The same for every method a primordials wrapper closes over (the wrapped + * set: Array.prototype.push, String.prototype.charCodeAt, Buffer.prototype. + * toString, …): `Array.prototype.push.call = x` throws in both modes, a fresh + * key throws in strict code, defineProperty of a new key and setPrototypeOf + * throw. + * @deviates Default node allows the replacement; Lava throws. Default node also + * leaves call/apply/bind AND the wrapped set extensible; Lava does not (#494), + * so the own-property and setPrototypeOf writes above throw where node + * succeeds. `node --frozen-intrinsics` gives the same answers for the wrapped + * set too (isExtensible false, own `call` throws in both modes, fresh key + * sloppy no-op / strict throw, defineProperty and setPrototypeOf throw; probed + * on node 26.13 with Array.prototype.push) but additionally freezes, so there + * redefining `name` throws and isFrozen is true; on Lava both stay as in + * default node. A method no wrapper holds (Array.prototype.at) is untouched. + * Traded for: events.js's `apply.call(listener, …)` and every callerN + * wrapper's `fn.call` cannot be steered by an own `call` on, or a re-pointed + * prototype of, the held method (node runs the real method; Lava ran the + * attacker's function), while the wrappers keep the only spelling JSC inlines + * (`.call` section above). The first trade, from #450: the * `callerN` wrappers keep their live `fn.call` (25x cheaper than Reflect.apply * on the bare wrapper, table above) without Function.prototype.call being a * way to break or steer URL, URLSearchParams, util.format, Buffer and @@ -250,6 +322,22 @@ lockMethod('call', call); lockMethod('apply', apply); lockMethod('bind', bind); + // Then the HELD functions (#494): every method a wrapper closes over, and + // nothing else — an intrinsic no wrapper holds (Array.prototype.at) keeps node's + // default extensibility. Locking the slots on Function.prototype does not stop + // `Array.prototype.push.call = evil` or `Function.prototype.apply.call = evil`: + // an own property on the very object a `fn.call` / `apply.call(fn, …)` reads + // `.call` off. Nor a setPrototypeOf that re-points what that read inherits. + // Non-extensible closes both without adding a key: getOwnPropertyNames is + // unchanged and `name`/`length` keep their default attributes. An own locked + // accessor per method (lockMethod's shape) was the fallback and is strictly + // worse: new visible keys on every method, and the prototype re-point left open. + // call/apply/bind are in the set through FunctionPrototypeCall/Apply/Bind below, + // which is also what events.js's `apply.call(listener, …)` relies on; the test's + // `ext` row goes red if one of them ever drops out. + for (var i = 0; i < wrappedCount; i++) preventExtensions(wrappedFns[i]); + wrappedFns = null; + intrinsicsLocked = true; } // An accessor, not a non-writable data property, because of the override mistake: diff --git a/pkg/runtime/js/node/events.js b/pkg/runtime/js/node/events.js index f46006c..8bc5858 100644 --- a/pkg/runtime/js/node/events.js +++ b/pkg/runtime/js/node/events.js @@ -9,8 +9,9 @@ // probed; pinned by tests/node-compat/cases/72-events-parity.js "invoke:"). // Lava spells the same thing `apply.call(listener, thisArg, args)` on the // CAPTURED apply: Function.prototype.call/apply/bind are locked before user code -// (#450), so neither read can be steered and the listener's own properties are -// never consulted. Not ReflectApply and not the FunctionPrototypeApply wrapper, +// (#450) and the three functions are non-extensible (#494), so neither read can +// be steered — not even by an own `call` on the held apply — and the listener's +// own properties are never consulted. Not ReflectApply and not the FunctionPrototypeApply wrapper, // because both stop JSC from sinking emit()'s rest array on the no-listener // path. Scratch micro, Linux arm64 JSC 2.52.6, best of 5 x 1e6 emit() calls per // launch, 2-3 launches per variant: a miss took 1.6 ms with `apply.call` and @@ -179,7 +180,7 @@ fired = true; target.removeListener(type, wrapped); // A ratchet false positive, like the two in emit(): captured `apply`, locked `.call`. - return apply.call(listener, target, arguments); // primordials-ok: invoke + return apply.call(listener, target, arguments); // primordials-ok: invoke — receiver is the locked Function.prototype.apply } wrapped.listener = listener; return wrapped; @@ -284,14 +285,15 @@ return false; } // Both `apply.call` sites are false positives for the ratchet: `apply` is the - // captured Function.prototype.apply and `.call` is locked (#450). + // captured Function.prototype.apply, `.call` is locked (#450) and `apply` cannot + // take an own `call` or a new prototype (#494). if (typeof handler === 'function') { - apply.call(handler, this, args); // primordials-ok: invoke + apply.call(handler, this, args); // primordials-ok: invoke — receiver is the locked Function.prototype.apply } else { // A snapshot: listeners added or removed by a listener take effect next emit. var listeners = arrayClone(handler); for (var i = 0; i < listeners.length; i++) { - apply.call(listeners[i], this, args); // primordials-ok: invoke + apply.call(listeners[i], this, args); // primordials-ok: invoke — receiver is the locked Function.prototype.apply } } return true; diff --git a/tests/mutation-manifest.json b/tests/mutation-manifest.json index d0861ad..6aa4600 100644 --- a/tests/mutation-manifest.json +++ b/tests/mutation-manifest.json @@ -46,6 +46,33 @@ "gate": "odin:cmd/lava:main.function_call_lock", "expect_detail": "got own-strict=THREW:TypeError" }, + { + "name": "function_call_lock dies when Function.prototype.apply drops out of the wrapped set", + "why": "An extensible captured apply takes an own call, and every apply.call(listener, this, args) in events.js then runs the attacker's function instead of the listener; node runs the listener (#494).", + "source": "pkg/runtime/js/internal/primordials.js", + "find": " function track(fn) {\n", + "replace": " function track(fn) {\n if (fn === apply) return;\n", + "gate": "odin:cmd/lava:main.function_call_lock", + "expect_detail": "want emit-hijack=listener:1 got emit-hijack=" + }, + { + "name": "function_call_lock dies when the wrapped set stays extensible", + "why": "Every callerN wrapper reads fn.call off the held method, so an extensible Array.prototype.push takes an own call and URLSearchParams.append runs the attacker's function instead of push; node is unaffected (#494).", + "source": "pkg/runtime/js/internal/primordials.js", + "find": " for (var i = 0; i < wrappedCount; i++) preventExtensions(wrappedFns[i]);\n", + "replace": "", + "gate": "odin:cmd/lava:main.function_call_lock", + "expect_detail": "want push-hijack=0 a=1&b=2&c=3 http://a/b?c got push-hijack=" + }, + { + "name": "function_call_lock dies when a method wrapped after the lock is not locked on the spot", + "why": "The loader wraps Buffer.prototype.toString/indexOf/slice with uncurryThis AFTER lockIntrinsics (pristineBuffer, the http framing path); without lock-on-wrap those held methods stay steerable through an own call.", + "source": "pkg/runtime/js/internal/primordials.js", + "find": " if (intrinsicsLocked) preventExtensions(fn);", + "replace": " if (intrinsicsLocked) return;", + "gate": "odin:cmd/lava:main.function_call_lock", + "expect_detail": "want wrapped-ext=false,false,false,true got wrapped-ext=false,false,true,true" + }, { "name": "55-encoding-pollution U dies when toBytes reads the view window live", "why": "A poisoned byteOffset/byteLength pair selects a window across the shared allocUnsafe pool, so decode() returns a neighbouring Buffer's bytes.", @@ -1789,7 +1816,7 @@ "name": "72-events-parity dies when emit() invokes a listener through its own apply", "why": "node invokes listeners with ReflectApply, so a listener's own call/apply is never consulted; handler.apply(this, args) runs the listener's own apply instead of its body.", "source": "pkg/runtime/js/node/events.js", - "find": " apply.call(handler, this, args); // primordials-ok: invoke", + "find": " apply.call(handler, this, args); // primordials-ok: invoke — receiver is the locked Function.prototype.apply", "replace": " handler.apply(this, args);", "gate": "compat:tests/node-compat/cases/72-events-parity.js", "expect_detail": [ @@ -1800,7 +1827,7 @@ "name": "72-events-parity dies when a once() wrapper invokes the listener through its own apply", "why": "The once() wrapper is the other listener-invocation site; node ignores the listener's own apply there too.", "source": "pkg/runtime/js/node/events.js", - "find": " return apply.call(listener, target, arguments); // primordials-ok: invoke", + "find": " return apply.call(listener, target, arguments); // primordials-ok: invoke — receiver is the locked Function.prototype.apply", "replace": " return listener.apply(target, arguments);", "gate": "compat:tests/node-compat/cases/72-events-parity.js", "expect_detail": [ From 68ee7b8dc15c1fe475de5f0bbb62d071b6e07281 Mon Sep 17 00:00:00 2001 From: x_ror <16686943+x-ror@users.noreply.github.com> Date: Tue, 17 Nov 2026 13:30:23 +0200 Subject: [PATCH 61/61] docs(claude): state what the #494 lock protects and what it still does not Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 6ce2d63..137081c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -227,12 +227,23 @@ Rules that keep this from becoming ceremony: `f.call = x` on an ordinary function still defines an own property, as in node. That is a §1 deviation, pinned by `cmd/lava/function_call_lock_test.odin`. What it buys: `Function.prototype.call = x` can no longer steer every `.call` at once. - What it does NOT buy: `.call` on any function user code can HOLD. That is a - caller-supplied callback (own `call`, re-pointed prototype), and it is also every - captured INTRINSIC — `var apply = Function.prototype.apply` is the very object - user code reaches, so `Function.prototype.apply.call = evil` steers - `apply.call(listener, …)`, and `Array.prototype.push.call = evil` steers the - `callerN` wrappers' `fn.call` (both probed on `bin/lava`; node runs neither). + The held functions are locked too (#494): `call`/`apply`/`bind` and every + method a primordials wrapper closes over (the set the `callerN`/fixed-arity + factories record, so a newly wrapped method is covered automatically) are + non-extensible, so neither an own `call` nor a re-pointed prototype on them + reaches a wrapper's `fn.call` or events.js's `apply.call(listener, …)`. Also a + §1 deviation — those functions answer as under `node --frozen-intrinsics` + (isExtensible false; own property, defineProperty and setPrototypeOf throw) but + are not frozen — pinned by the same test. Intrinsics no wrapper holds keep + node's extensibility. `call.call(fn, …)` routing was measured and rejected: + JSC inlines only `fn.call` (caller1 ~1 ns vs ~55 ns, Linux arm64), costing + `new URL` 2.64x and `URLSearchParams.get` 3.81x end to end. + What it does NOT buy: `.call` on a function user code can HOLD outside that + set — every caller-supplied callback (own `call`, re-pointed prototype), every + intrinsic captured raw OUTSIDE primordials and invoked with `.call` + (`toString.call` in structured_clone.js, `u8Fill.call` in buffer.js), and every + live prototype read (`Array.prototype.slice.call(…)`), which plain replacement + already steers. Route those through a primordials wrapper. The ratchet therefore counts every `.call`/`.apply` (`invoke`) and `.bind` (`method`) except on ONE receiver: a strict function declaration the file never lets go of — exactly one binding of the name in the file, never written,