deno/ext/node/polyfills/internal/streams/add-abort-signal.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

83 lines
2.1 KiB
JavaScript

// deno-lint-ignore-file
// Copyright 2018-2025 the Deno authors. MIT license.
import { primordials } from "ext:core/mod.js";
import imported1 from "ext:deno_node/internal/errors.ts";
import {
isNodeStream,
isWebStream,
kControllerErrorFunction,
} from "ext:deno_node/internal/streams/utils.js";
import eos from "ext:deno_node/internal/streams/end-of-stream.js";
import * as _mod2 from "ext:deno_node/internal/events/abort_listener.mjs";
const {
AbortError,
codes: {
ERR_INVALID_ARG_TYPE,
},
} = imported1;
"use strict";
const {
SymbolDispose,
} = primordials;
let addAbortListener;
// This method is inlined here for readable-stream
// It also does not allow for signal to not exist on the stream
// https://github.com/nodejs/node/pull/36061#discussion_r533718029
const validateAbortSignal = (signal, name) => {
if (
typeof signal !== "object" ||
!("aborted" in signal)
) {
throw new ERR_INVALID_ARG_TYPE(name, "AbortSignal", signal);
}
};
const addAbortSignal = function addAbortSignal(signal, stream) {
validateAbortSignal(signal, "signal");
if (!isNodeStream(stream) && !isWebStream(stream)) {
throw new ERR_INVALID_ARG_TYPE("stream", [
"ReadableStream",
"WritableStream",
"Stream",
], stream);
}
return addAbortSignalNoValidate(signal, stream);
};
export { addAbortSignal };
const addAbortSignalNoValidate = function (signal, stream) {
if (typeof signal !== "object" || !("aborted" in signal)) {
return stream;
}
const onAbort = isNodeStream(stream)
? () => {
stream.destroy(new AbortError(undefined, { cause: signal.reason }));
}
: () => {
stream[kControllerErrorFunction](
new AbortError(undefined, { cause: signal.reason }),
);
};
if (signal.aborted) {
onAbort();
} else {
addAbortListener ??= _mod2.addAbortListener;
const disposable = addAbortListener(signal, onAbort);
eos(stream, disposable[SymbolDispose]);
}
return stream;
};
export { addAbortSignalNoValidate };
export default {
addAbortSignal,
addAbortSignalNoValidate,
};