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

Towards #24236. Replaces JS intrinsics with primordial equivalents. Lint "ignore" statements were added for `Readable` stream instances.

<!--
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/stream_base_commons.ts
patch: @@ -20,9 +20,6 @@
 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 
-// TODO(petamoriken): enable prefer-primordials for node polyfills
-// deno-lint-ignore-file prefer-primordials
-
 import { ownerSymbol } from "ext:deno_node/internal/async_hooks.ts";
 import {
   kArrayBufferOffset,
@@ -38,8 +35,17 @@ import { getTimerDuration, kTimeout } from "ext:deno_node/internal/timers.mjs";
 import { clearTimeout, setUnrefTimeout } from "node:timers";
 import { validateFunction } from "ext:deno_node/internal/validators.mjs";
 import { codeMap } from "ext:deno_node/internal_binding/uv.ts";
+import { primordials } from "ext:core/mod.js";
 import { Buffer } from "node:buffer";
 
+const {
+  Array,
+  FunctionPrototypeBind,
+  MapPrototypeGet,
+  Symbol,
+  TypedArrayPrototypeGetBuffer,
+} = primordials;
+
 export const kMaybeDestroy = Symbol("kMaybeDestroy");
 export const kUpdateTimer = Symbol("kUpdateTimer");
 export const kAfterAsyncWrite = Symbol("kAfterAsyncWrite");
@@ -255,7 +261,14 @@ export function onStreamRead(
       const offset = streamBaseState[kArrayBufferOffset];
       // Performance note: Pass ArrayBuffer to Buffer#from to avoid
       // copy.
-      const buf = Buffer.from(arrayBuffer.buffer, offset, nread);
+      const buf = Buffer.from(
+        TypedArrayPrototypeGetBuffer(arrayBuffer),
+        offset,
+        nread,
+      );
+      // Ignore use of primordial here. The `push` method is from a `Readable`
+      // stream instance.
+      // deno-lint-ignore prefer-primordials
       result = stream.push(buf);
     }
 
@@ -278,7 +291,7 @@ export function onStreamRead(
     return;
   }
 
-  if (nread !== codeMap.get("EOF")) {
+  if (nread !== MapPrototypeGet(codeMap, "EOF")) {
     // CallJSOnreadMethod expects the return value to be a buffer.
     // Ref: https://github.com/nodejs/node/pull/34375
     stream.destroy(errnoException(nread, "read"));
@@ -311,6 +324,9 @@ export function onStreamRead(
     // Push a null to signal the end of data.
     // Do it before `maybeDestroy` for correct order of events:
     // `end` -> `close`
+    // Ignore use of primordial. The `push` method is from a `Readable` stream
+    // instance.
+    // deno-lint-ignore prefer-primordials
     stream.push(null);
     stream.read(0);
   }
@@ -341,7 +357,10 @@ export function setStreamTimeout(
       this.removeListener("timeout", callback);
     }
   } else {
-    this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
+    this[kTimeout] = setUnrefTimeout(
+      FunctionPrototypeBind(this._onTimeout, this),
+      msecs,
+    );
 
     if (this[kSession]) {
       this[kSession][kUpdateTimer]();