From d47147fb6ad229b1c039aff9d0959b6e281f4df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 14 Feb 2023 17:38:45 +0100 Subject: feat(ext/node): embed std/node into the snapshot (#17724) This commit moves "deno_std/node" in "ext/node" crate. The code is transpiled and snapshotted during the build process. During the first pass a minimal amount of work was done to create the snapshot, a lot of code in "ext/node" depends on presence of "Deno" global. This code will be gradually fixed in the follow up PRs to migrate it to import relevant APIs from "internal:" modules. Currently the code from snapshot is not used in any way, and all Node/npm compatibility still uses code from "https://deno.land/std/node" (or from the location specified by "DENO_NODE_COMPAT_URL"). This will also be handled in a follow up PRs. --------- Co-authored-by: crowlkats Co-authored-by: Divy Srivastava Co-authored-by: Yoshiya Hinosawa --- ext/node/polyfills/stream/consumers.mjs | 78 +++++++++++++++++++++++++++++++++ ext/node/polyfills/stream/promises.mjs | 12 +++++ ext/node/polyfills/stream/web.ts | 51 +++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 ext/node/polyfills/stream/consumers.mjs create mode 100644 ext/node/polyfills/stream/promises.mjs create mode 100644 ext/node/polyfills/stream/web.ts (limited to 'ext/node/polyfills/stream') diff --git a/ext/node/polyfills/stream/consumers.mjs b/ext/node/polyfills/stream/consumers.mjs new file mode 100644 index 000000000..61fe27020 --- /dev/null +++ b/ext/node/polyfills/stream/consumers.mjs @@ -0,0 +1,78 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright Joyent and Node contributors. All rights reserved. MIT license. + +import { TextDecoder } from "internal:deno_web/08_text_encoding.js"; +import { Buffer } from "internal:deno_node/polyfills/buffer.ts"; + +/** + * @typedef {import('../_global.d.ts').ReadableStream + * } ReadableStream + * @typedef {import('../_stream.d.ts')} Readable + */ + +/** + * @param {AsyncIterable|ReadableStream|Readable} stream + * @returns {Promise} + */ +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} + */ +async function arrayBuffer(stream) { + const ret = await blob(stream); + return ret.arrayBuffer(); +} + +/** + * @param {AsyncIterable|ReadableStream|Readable} stream + * @returns {Promise} + */ +async function buffer(stream) { + return Buffer.from(await arrayBuffer(stream)); +} + +/** + * @param {AsyncIterable|ReadableStream|Readable} stream + * @returns {Promise} + */ +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} + */ +async function json(stream) { + const str = await text(stream); + return JSON.parse(str); +} + +export default { + arrayBuffer, + blob, + buffer, + json, + text, +}; +export { arrayBuffer, blob, buffer, json, text }; diff --git a/ext/node/polyfills/stream/promises.mjs b/ext/node/polyfills/stream/promises.mjs new file mode 100644 index 000000000..8c1f7439b --- /dev/null +++ b/ext/node/polyfills/stream/promises.mjs @@ -0,0 +1,12 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +// Copyright Joyent and Node contributors. All rights reserved. MIT license. + +import stream from "internal:deno_node/polyfills/_stream.mjs"; + +const { finished, pipeline } = stream.promises; + +export default { + finished, + pipeline, +}; +export { finished, pipeline }; diff --git a/ext/node/polyfills/stream/web.ts b/ext/node/polyfills/stream/web.ts new file mode 100644 index 000000000..d97a6b0f1 --- /dev/null +++ b/ext/node/polyfills/stream/web.ts @@ -0,0 +1,51 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +import { + ByteLengthQueuingStrategy, + CountQueuingStrategy, + ReadableByteStreamController, + ReadableStream, + ReadableStreamDefaultController, + ReadableStreamDefaultReader, + TransformStream, + TransformStreamDefaultController, + WritableStream, + WritableStreamDefaultController, + WritableStreamDefaultWriter, +} from "internal:deno_web/06_streams.js"; +import { + TextDecoderStream, + TextEncoderStream, +} from "internal:deno_web/08_text_encoding.js"; + +export { + ByteLengthQueuingStrategy, + CountQueuingStrategy, + ReadableByteStreamController, + ReadableStream, + ReadableStreamDefaultController, + ReadableStreamDefaultReader, + TextDecoderStream, + TextEncoderStream, + TransformStream, + TransformStreamDefaultController, + WritableStream, + WritableStreamDefaultController, + WritableStreamDefaultWriter, +}; + +export default { + ReadableStream, + ReadableStreamDefaultReader, + ReadableByteStreamController, + ReadableStreamDefaultController, + TransformStream, + TransformStreamDefaultController, + WritableStream, + WritableStreamDefaultWriter, + WritableStreamDefaultController, + ByteLengthQueuingStrategy, + CountQueuingStrategy, + TextEncoderStream, + TextDecoderStream, +}; -- cgit v1.2.3