deno/ext/node/polyfills/stream/consumers.js
Divy Srivastava 01b6da9d9b
fix(ext/node): upgrade node:stream (#28855)
Ref https://github.com/denoland/deno/issues/28836

This PR replaces the _stream.mjs bundle with a file-by-file port instead. A codemod transpiles Node.js internals to ESM. The codemod performs three tasks: translating CJS to ESM, remapping internal dependencies, and hoisting lazy requires as imports.

The process is fully automated through the `update_node_stream.ts` script, simplifying future internal updates. The script checks out Node.js from a specific tag defined in the `tests/node_compat/runner`.

Additionally, the update enables new tests in our Node test runner and adds features (like compose()) that were missing from the outdated bundle.

## Performance

There is a 140KB+ binary size increase on aarch64-apple-darwin and nop startup time stays the same.
2025-04-14 21:35:34 +05:30

87 lines
1.9 KiB
JavaScript

// deno-lint-ignore-file
// Copyright 2018-2025 the Deno authors. MIT license.
import { primordials } from "ext:core/mod.js";
import { TextDecoder } from "ext:deno_web/08_text_encoding.js";
import { Blob } from "ext:deno_web/09_file.js";
import { Buffer } from "node:buffer";
"use strict";
const {
JSONParse,
} = primordials;
/**
* @typedef {import('../internal/webstreams/readablestream').ReadableStream
* } ReadableStream
* @typedef {import('../internal/streams/readable')} Readable
*/
/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<Blob>}
*/
async function blob(stream) {
const chunks = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
return new Blob(chunks);
}
/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<ArrayBuffer>}
*/
async function arrayBuffer(stream) {
const ret = await blob(stream);
return ret.arrayBuffer();
}
/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<Buffer>}
*/
async function buffer(stream) {
return Buffer.from(await arrayBuffer(stream));
}
/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<string>}
*/
async function text(stream) {
const dec = new TextDecoder();
let str = "";
for await (const chunk of stream) {
if (typeof chunk === "string") {
str += chunk;
} else {
str += dec.decode(chunk, { stream: true });
}
}
// Flush the streaming TextDecoder so that any pending
// incomplete multibyte characters are handled.
str += dec.decode(undefined, { stream: false });
return str;
}
/**
* @param {AsyncIterable|ReadableStream|Readable} stream
* @returns {Promise<any>}
*/
async function json(stream) {
const str = await text(stream);
return JSONParse(str);
}
const _defaultExport1 = {
arrayBuffer,
blob,
buffer,
text,
json,
};
export default _defaultExport1;
export { arrayBuffer, blob, buffer, json, text };