Files
Matteo Collina 2bbcfa0718 worker: preload streams into startup snapshot
Worker stdio loads stream after restoring the main-thread context,
initializing its constructors and prototype methods on every startup.
Preload stream during snapshot construction so workers can restore that
initialized state instead. Worker-specific stdio instances and message
ports are still created at runtime.

Verify snapshot membership, stdio methods, and isolation from
parent-thread stream mutations.

Assisted-by: pi
Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/66311
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
2026-11-11 17:20:33 +01:00

56 lines
1.2 KiB
JavaScript

'use strict';
const {
ArrayIsArray,
ArrayPrototypePop,
Promise,
} = primordials;
const {
isIterable,
isNodeStream,
isWebStream,
} = require('internal/streams/utils');
let pl;
const { finished } = require('internal/streams/end-of-stream');
require('stream');
function pipeline(...streams) {
pl ??= require('internal/streams/pipeline').pipelineImpl;
return new Promise((resolve, reject) => {
let signal;
let end;
const lastArg = streams[streams.length - 1];
if (lastArg && typeof lastArg === 'object' &&
!isNodeStream(lastArg) && !isIterable(lastArg) && !isWebStream(lastArg)) {
const options = ArrayPrototypePop(streams);
signal = options.signal;
end = options.end;
}
let lastStream = streams[streams.length - 1];
if (streams.length === 1 && ArrayIsArray(streams[0])) {
lastStream = streams[0][streams[0].length - 1];
}
const stream = pl(streams, (err, value) => {
if (err) {
reject(err);
} else {
resolve(value);
}
}, { signal, end });
if (typeof lastStream === 'function' && stream.readable) {
stream.resume();
}
});
}
module.exports = {
finished,
pipeline,
};