From 510db0a86e639c3962d76359a20f76f0d32b1073 Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Thu, 20 Jun 2024 17:26:34 +1000 Subject: [PATCH] fix(ext/node): use primordials in `ext/node/polyfills/_process/` (#24282) --- ext/node/polyfills/_process/process.ts | 36 +++++++++++++------- ext/node/polyfills/_process/streams.mjs | 44 ++++++++++++++++--------- 2 files changed, 54 insertions(+), 26 deletions(-) diff --git a/ext/node/polyfills/_process/process.ts b/ext/node/polyfills/_process/process.ts index 046169b08a..5abde1cf0e 100644 --- a/ext/node/polyfills/_process/process.ts +++ b/ext/node/polyfills/_process/process.ts @@ -1,13 +1,23 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license. -// TODO(petamoriken): enable prefer-primordials for node polyfills -// deno-lint-ignore-file prefer-primordials - // The following are all the process APIs that don't depend on the stream module // They have to be split this way to prevent a circular dependency -import { core } from "ext:core/mod.js"; +import { core, primordials } from "ext:core/mod.js"; +const { + Error, + ObjectGetOwnPropertyNames, + String, + ReflectOwnKeys, + ArrayPrototypeIncludes, + Object, + Proxy, + ObjectPrototype, + ObjectPrototypeIsPrototypeOf, + TypeErrorPrototype, +} = primordials; +const { build } = core; import { nextTick as _nextTick } from "ext:deno_node/_next_tick.ts"; import { _exiting } from "ext:deno_node/_process/exiting.ts"; @@ -15,11 +25,11 @@ import * as fs from "ext:deno_fs/30_fs.js"; /** Returns the operating system CPU architecture for which the Deno binary was compiled */ export function arch(): string { - if (core.build.arch == "x86_64") { + if (build.arch == "x86_64") { return "x64"; - } else if (core.build.arch == "aarch64") { + } else if (build.arch == "aarch64") { return "arm64"; - } else if (core.build.arch == "riscv64gc") { + } else if (build.arch == "riscv64gc") { return "riscv64"; } else { throw Error("unreachable"); @@ -41,14 +51,18 @@ function denoEnvGet(name: string) { try { return Deno.env.get(name); } catch (e) { - if (e instanceof TypeError || e instanceof Deno.errors.PermissionDenied) { + if ( + ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e) || + // TODO(iuioiua): Use `PermissionDeniedPrototype` when it's available + ObjectPrototypeIsPrototypeOf(Deno.errors.PermissionDenied.prototype, e) + ) { return undefined; } throw e; } } -const OBJECT_PROTO_PROP_NAMES = Object.getOwnPropertyNames(Object.prototype); +const OBJECT_PROTO_PROP_NAMES = ObjectGetOwnPropertyNames(ObjectPrototype); /** * https://nodejs.org/api/process.html#process_process_env * Requires env permissions @@ -66,13 +80,13 @@ export const env: InstanceType & Record = return envValue; } - if (OBJECT_PROTO_PROP_NAMES.includes(prop)) { + if (ArrayPrototypeIncludes(OBJECT_PROTO_PROP_NAMES, prop)) { return target[prop]; } return envValue; }, - ownKeys: () => Reflect.ownKeys(Deno.env.toObject()), + ownKeys: () => ReflectOwnKeys(Deno.env.toObject()), getOwnPropertyDescriptor: (_target, name) => { const value = denoEnvGet(String(name)); if (value) { diff --git a/ext/node/polyfills/_process/streams.mjs b/ext/node/polyfills/_process/streams.mjs index f50e205888..6f289fb776 100644 --- a/ext/node/polyfills/_process/streams.mjs +++ b/ext/node/polyfills/_process/streams.mjs @@ -1,8 +1,17 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright Joyent, Inc. and Node.js contributors. All rights reserved. MIT license. -// TODO(petamoriken): enable prefer-primordials for node polyfills -// deno-lint-ignore-file prefer-primordials +import { primordials } from "ext:core/mod.js"; +const { + Uint8ArrayPrototype, + Error, + ObjectDefineProperties, + ObjectDefineProperty, + TypedArrayPrototypeSlice, + PromisePrototypeThen, + ObjectValues, + ObjectPrototypeIsPrototypeOf, +} = primordials; import { Buffer } from "node:buffer"; import { @@ -26,7 +35,11 @@ export function createWritableStdioStream(writer, name, warmup = false) { ); return; } - writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc)); + writer.writeSync( + ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, buf) + ? buf + : Buffer.from(buf, enc), + ); cb(); }, destroy(err, cb) { @@ -39,8 +52,10 @@ export function createWritableStdioStream(writer, name, warmup = false) { }); let fd = -1; + // deno-lint-ignore prefer-primordials if (writer instanceof io.Stdout) { fd = io.STDOUT_RID; + // deno-lint-ignore prefer-primordials } else if (writer instanceof io.Stderr) { fd = io.STDERR_RID; } @@ -48,7 +63,7 @@ export function createWritableStdioStream(writer, name, warmup = false) { stream.destroySoon = stream.destroy; stream._isStdio = true; stream.once("close", () => writer?.close()); - Object.defineProperties(stream, { + ObjectDefineProperties(stream, { columns: { enumerable: true, configurable: true, @@ -69,7 +84,7 @@ export function createWritableStdioStream(writer, name, warmup = false) { enumerable: true, configurable: true, value: () => - writer?.isTerminal() ? Object.values(Deno.consoleSize?.()) : undefined, + writer?.isTerminal() ? ObjectValues(Deno.consoleSize?.()) : undefined, }, }); @@ -107,14 +122,12 @@ function _guessStdinType(fd) { const _read = function (size) { const p = Buffer.alloc(size || 16 * 1024); - io.stdin?.read(p).then( - (length) => { - this.push(length === null ? null : p.slice(0, length)); - }, - (error) => { - this.destroy(error); - }, - ); + PromisePrototypeThen(io.stdin?.read(p), (length) => { + // deno-lint-ignore prefer-primordials + this.push(length === null ? null : TypedArrayPrototypeSlice(p, 0, length)); + }, (error) => { + this.destroy(error); + }); }; let readStream; @@ -182,13 +195,14 @@ export const initStdin = (warmup = false) => { // Provide a dummy contentless input for e.g. non-console // Windows applications. stdin = new Readable({ read() {} }); + // deno-lint-ignore prefer-primordials stdin.push(null); } } stdin.on("close", () => io.stdin?.close()); stdin.fd = io.stdin ? io.STDIN_RID : -1; - Object.defineProperty(stdin, "isTTY", { + ObjectDefineProperty(stdin, "isTTY", { enumerable: true, configurable: true, get() { @@ -201,7 +215,7 @@ export const initStdin = (warmup = false) => { stdin._isRawMode = enable; return stdin; }; - Object.defineProperty(stdin, "isRaw", { + ObjectDefineProperty(stdin, "isRaw", { enumerable: true, configurable: true, get() {