Repository: denoland/deno
Pull request: fix(ext/node): use primordials in `ext/node/polyfills/internal_binding/pipe_wrap.ts`

Towards #24236. Replaces JS builtins with equivalent primordials.

<!--
Before submitting a PR, please read https://docs.deno.com/runtime/manual/references/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench' label.
-->


Changed files:
filename: ext/node/polyfills/internal_binding/pipe_wrap.ts
patch: @@ -24,9 +24,6 @@
 // - https://github.com/nodejs/node/blob/master/src/pipe_wrap.cc
 // - https://github.com/nodejs/node/blob/master/src/pipe_wrap.h
 
-// TODO(petamoriken): enable prefer-primordials for node polyfills
-// deno-lint-ignore-file prefer-primordials
-
 import { notImplemented } from "ext:deno_node/_utils.ts";
 import { unreachable } from "ext:deno_node/_util/asserts.ts";
 import { ConnectionWrap } from "ext:deno_node/internal_binding/connection_wrap.ts";
@@ -48,6 +45,15 @@ import {
 } from "ext:deno_node/internal_binding/_listen.ts";
 import { isWindows } from "ext:deno_node/_util/os.ts";
 import { fs } from "ext:deno_node/internal_binding/constants.ts";
+import { primordials } from "ext:core/mod.js";
+
+const {
+  FunctionPrototypeCall,
+  MapPrototypeGet,
+  ObjectPrototypeIsPrototypeOf,
+  PromisePrototypeThen,
+  ReflectHas,
+} = primordials;
 
 export enum socketType {
   SOCKET,
@@ -103,7 +109,10 @@ export class Pipe extends ConnectionWrap {
 
     this.ipc = ipc;
 
-    if (conn && provider === providerType.PIPEWRAP && "localAddr" in conn) {
+    if (
+      conn && provider === providerType.PIPEWRAP &&
+      ReflectHas(conn, "localAddr")
+    ) {
       const localAddr = conn.localAddr;
       this.#address = localAddr.path;
     }
@@ -146,7 +155,8 @@ export class Pipe extends ConnectionWrap {
       transport: "unix",
     };
 
-    Deno.connect(connectOptions).then(
+    PromisePrototypeThen(
+      Deno.connect(connectOptions),
       (conn: Deno.UnixConn) => {
         const localAddr = conn.localAddr as Deno.UnixAddr;
 
@@ -160,8 +170,8 @@ export class Pipe extends ConnectionWrap {
         }
       },
       (e) => {
-        const code = codeMap.get(e.code ?? "UNKNOWN") ??
-          codeMap.get("UNKNOWN")!;
+        const code = MapPrototypeGet(codeMap, e.code ?? "UNKNOWN") ??
+          MapPrototypeGet(codeMap, "UNKNOWN")!;
 
         try {
           this.afterConnect(req, code);
@@ -199,10 +209,11 @@ export class Pipe extends ConnectionWrap {
     try {
       listener = Deno.listen(listenOptions);
     } catch (e) {
-      if (e instanceof Deno.errors.NotCapable) {
+      if (ObjectPrototypeIsPrototypeOf(Deno.errors.NotCapable.prototype, e)) {
         throw e;
       }
-      return codeMap.get(e.code ?? "UNKNOWN") ?? codeMap.get("UNKNOWN")!;
+      return MapPrototypeGet(codeMap, e.code ?? "UNKNOWN") ??
+        MapPrototypeGet(codeMap, "UNKNOWN")!;
     }
 
     const address = listener.addr as Deno.UnixAddr;
@@ -249,7 +260,7 @@ export class Pipe extends ConnectionWrap {
       mode != constants.UV_WRITABLE &&
       mode != (constants.UV_WRITABLE | constants.UV_READABLE)
     ) {
-      return codeMap.get("EINVAL");
+      return MapPrototypeGet(codeMap, "EINVAL");
     }
 
     let desiredMode = 0;
@@ -267,7 +278,7 @@ export class Pipe extends ConnectionWrap {
       Deno.chmodSync(this.#address!, desiredMode);
     } catch {
       // TODO(cmorten): map errors to appropriate error codes.
-      return codeMap.get("UNKNOWN")!;
+      return MapPrototypeGet(codeMap, "UNKNOWN")!;
     }
 
     return 0;
@@ -310,14 +321,17 @@ export class Pipe extends ConnectionWrap {
     try {
       connection = await this.#listener.accept();
     } catch (e) {
-      if (e instanceof Deno.errors.BadResource && this.#closed) {
+      if (
+        ObjectPrototypeIsPrototypeOf(Deno.errors.BadResource.prototype, e) &&
+        this.#closed
+      ) {
         // Listener and server has closed.
         return;
       }
 
       try {
         // TODO(cmorten): map errors to appropriate error codes.
-        this.onconnection!(codeMap.get("UNKNOWN")!, undefined);
+        this.onconnection!(MapPrototypeGet(codeMap, "UNKNOWN")!, undefined);
       } catch {
         // swallow callback errors.
       }
@@ -361,7 +375,7 @@ export class Pipe extends ConnectionWrap {
       }
     }
 
-    return LibuvStreamWrap.prototype._onClose.call(this);
+    return FunctionPrototypeCall(LibuvStreamWrap.prototype._onClose, this);
   }
 }