fix(ext/node): use primordials in ext/node/polyfills/internal_binding/stream_wrap.ts (#29542)

This commit is contained in:
James Bronder 2025-06-10 23:40:09 -07:00 committed by GitHub
parent b49523780d
commit 6367a54b61
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -27,11 +27,17 @@
// - https://github.com/nodejs/node/blob/master/src/stream_wrap.h // - https://github.com/nodejs/node/blob/master/src/stream_wrap.h
// - https://github.com/nodejs/node/blob/master/src/stream_wrap.cc // - https://github.com/nodejs/node/blob/master/src/stream_wrap.cc
// TODO(petamoriken): enable prefer-primordials for node polyfills import { core, primordials } from "ext:core/mod.js";
// deno-lint-ignore-file prefer-primordials
import { core } from "ext:core/mod.js";
const { internalRidSymbol } = core; const { internalRidSymbol } = core;
const {
Array,
MapPrototypeGet,
ObjectPrototypeIsPrototypeOf,
PromisePrototypeThen,
Symbol,
TypedArrayPrototypeSlice,
Uint8Array,
} = primordials;
import { op_can_write_vectored, op_raw_write_vectored } from "ext:core/ops"; import { op_can_write_vectored, op_raw_write_vectored } from "ext:core/ops";
import { TextEncoder } from "ext:deno_web/08_text_encoding.js"; import { TextEncoder } from "ext:deno_web/08_text_encoding.js";
@ -222,11 +228,13 @@ export class LibuvStreamWrap extends HandleWrap {
if (typeof chunks[0] === "string") chunks[0] = Buffer.from(chunks[0]); if (typeof chunks[0] === "string") chunks[0] = Buffer.from(chunks[0]);
if (typeof chunks[1] === "string") chunks[1] = Buffer.from(chunks[1]); if (typeof chunks[1] === "string") chunks[1] = Buffer.from(chunks[1]);
PromisePrototypeThen(
op_raw_write_vectored( op_raw_write_vectored(
rid, rid,
chunks[0], chunks[0],
chunks[1], chunks[1],
).then((nwritten) => { ),
(nwritten) => {
try { try {
req.oncomplete(0); req.oncomplete(0);
} catch { } catch {
@ -235,7 +243,8 @@ export class LibuvStreamWrap extends HandleWrap {
streamBaseState[kBytesWritten] = nwritten; streamBaseState[kBytesWritten] = nwritten;
this.bytesWritten += nwritten; this.bytesWritten += nwritten;
}); },
);
return 0; return 0;
} }
@ -261,6 +270,9 @@ export class LibuvStreamWrap extends HandleWrap {
} }
} }
// Ignoring primordial lint here since the static method `concat` is invoked
// via the Node.js `Buffer` class instead of a JS builtin.
// deno-lint-ignore prefer-primordials
return this.writeBuffer(req, Buffer.concat(buffers)); return this.writeBuffer(req, Buffer.concat(buffers));
} }
@ -308,7 +320,7 @@ export class LibuvStreamWrap extends HandleWrap {
try { try {
this[kStreamBaseField]?.close(); this[kStreamBaseField]?.close();
} catch { } catch {
status = codeMap.get("ENOTCONN")!; status = MapPrototypeGet(codeMap, "ENOTCONN")!;
} }
return status; return status;
@ -359,22 +371,25 @@ export class LibuvStreamWrap extends HandleWrap {
if (e.message === "cancelled") return null; if (e.message === "cancelled") return null;
if ( if (
e instanceof Deno.errors.Interrupted || ObjectPrototypeIsPrototypeOf(Deno.errors.Interrupted.prototype, e) ||
e instanceof Deno.errors.BadResource ObjectPrototypeIsPrototypeOf(Deno.errors.BadResource.prototype, e)
) { ) {
nread = codeMap.get("EOF")!; nread = MapPrototypeGet(codeMap, "EOF")!;
} else if ( } else if (
e instanceof Deno.errors.ConnectionReset || ObjectPrototypeIsPrototypeOf(
e instanceof Deno.errors.ConnectionAborted Deno.errors.ConnectionReset.prototype,
e,
) ||
ObjectPrototypeIsPrototypeOf(Deno.errors.ConnectionAborted.prototype, e)
) { ) {
nread = codeMap.get("ECONNRESET")!; nread = MapPrototypeGet(codeMap, "ECONNRESET")!;
} else { } else {
this[ownerSymbol].destroy(e); this[ownerSymbol].destroy(e);
return; return;
} }
} }
nread ??= codeMap.get("EOF")!; nread ??= MapPrototypeGet(codeMap, "EOF")!;
streamBaseState[kReadBytesOrError] = nread; streamBaseState[kReadBytesOrError] = nread;
@ -382,7 +397,7 @@ export class LibuvStreamWrap extends HandleWrap {
this.bytesRead += nread; this.bytesRead += nread;
} }
buf = buf.slice(0, nread); buf = TypedArrayPrototypeSlice(buf, 0, nread);
streamBaseState[kArrayBufferOffset] = 0; streamBaseState[kArrayBufferOffset] = 0;
@ -432,12 +447,12 @@ export class LibuvStreamWrap extends HandleWrap {
let status: number; let status: number;
// TODO(cmorten): map err to status codes // TODO(cmorten): map err to status codes
if ( if (
e instanceof Deno.errors.BadResource || ObjectPrototypeIsPrototypeOf(Deno.errors.BadResource.prototype, e) ||
e instanceof Deno.errors.BrokenPipe ObjectPrototypeIsPrototypeOf(Deno.errors.BrokenPipe.prototype, e)
) { ) {
status = codeMap.get("EBADF")!; status = MapPrototypeGet(codeMap, "EBADF")!;
} else { } else {
status = codeMap.get("UNKNOWN")!; status = MapPrototypeGet(codeMap, "UNKNOWN")!;
} }
try { try {