Files
scriptc/tests/corpus/readable-stream-3/main.cjs
T
Malte UblandClaude Opus 5.5 2081aaa721 Compile readable-stream 3.x as Node's stream module too
readable-stream 3.x is a copy of Node 10's streams. Like 2.x and 4.x it
now compiles as node:stream: readable.js as stream.Readable carrying the
stream classes, Stream, finished and pipeline, and lib/_stream_*.js as the
matching builtin classes. Unlike them, this is not the package's own
READABLE_STREAM=disable mode, which throws on Node 24 (it Object.assigns
every stream export onto stream.Readable, which inherits the getter-only
`promises` accessor). The limitations and dependencies pages and the
compatibility notes say so.

The corpus program readable-stream-3 runs the vendored 3.6.2 under Node and
keeps to behavior its streams share with the builtin.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 07:22:27 -07:00

87 lines
2.3 KiB
JavaScript

// readable-stream 3.x compiles as Node's builtin stream module, a
// documented divergence: Node runs the package's own JavaScript streams (a
// copy of Node 10's), and the package's READABLE_STREAM=disable branch,
// which would hand out the builtin classes, throws on Node 24. This program
// keeps to behavior both share: piping through transforms, pipeline and
// finished callbacks, and error propagation, one step at a time.
// node_modules vendors readable-stream 3.6.2 and its dependencies,
// unmodified (MIT).
"use strict";
const rs = require("readable-stream");
console.log(
typeof rs.Readable,
typeof rs.Writable,
typeof rs.Transform,
typeof rs.PassThrough,
typeof rs.pipeline,
typeof rs.finished,
);
function piped(next) {
const seen = [];
const source = new rs.Readable({ read: function () {} });
const upper = new rs.Transform({
transform: function (chunk, encoding, callback) {
callback(null, String(chunk).toUpperCase());
},
});
const through = new rs.PassThrough();
const sink = new rs.Writable({
write: function (chunk, encoding, callback) {
seen.push(String(chunk));
callback();
},
});
rs.pipeline(source, upper, through, sink, function (err) {
console.log("pipeline", err ? err.message : "ok", seen.join("|"));
next();
});
["a", "bc", "def"].forEach(function (w) {
source.push(w);
});
source.push(null);
}
function failed(next) {
const source = new rs.Readable({ read: function () {} });
const broken = new rs.Transform({
transform: function (chunk, encoding, callback) {
callback(new Error("boom on " + String(chunk)));
},
});
const sink = new rs.Writable({
write: function (chunk, encoding, callback) {
callback();
},
});
rs.pipeline(source, broken, sink, function (err) {
console.log("pipeline", err ? err.message : "ok");
next();
});
source.push("x");
source.push(null);
}
function ended(next) {
const sink = new rs.Writable({
write: function (chunk, encoding, callback) {
callback();
},
});
rs.finished(sink, function (err) {
console.log("finished", err ? err.message : "ok");
next();
});
sink.end("last");
}
piped(function () {
failed(function () {
ended(function () {
console.log("done");
});
});
});
console.log("sync done");