From ff535061077d2b67e20154a7dfefe8ca92502c5a Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Mon, 24 Jun 2024 16:22:55 +1000 Subject: [PATCH] fix(ext/node): use primordials in `ext/node/polyfills/_fs/_fs_dir.ts` (#24319) Towards #24236 --------- Signed-off-by: Asher Gomez Co-authored-by: Satya Rohith --- ext/node/polyfills/_fs/_fs_dir.ts | 37 ++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/ext/node/polyfills/_fs/_fs_dir.ts b/ext/node/polyfills/_fs/_fs_dir.ts index acb56e84b7..ed051fb0bf 100644 --- a/ext/node/polyfills/_fs/_fs_dir.ts +++ b/ext/node/polyfills/_fs/_fs_dir.ts @@ -1,13 +1,22 @@ // Copyright 2018-2024 the Deno authors. 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"; import Dirent from "ext:deno_node/_fs/_fs_dirent.ts"; import { assert } from "ext:deno_node/_util/asserts.ts"; import { ERR_MISSING_ARGS } from "ext:deno_node/internal/errors.ts"; import { TextDecoder } from "ext:deno_web/08_text_encoding.js"; +const { + Promise, + ObjectPrototypeIsPrototypeOf, + Uint8ArrayPrototype, + PromisePrototypeThen, + SymbolAsyncIterator, + ArrayIteratorPrototypeNext, + AsyncGeneratorPrototypeNext, + SymbolIterator, +} = primordials; + export default class Dir { #dirPath: string | Uint8Array; #syncIterator!: Iterator | null; @@ -21,7 +30,7 @@ export default class Dir { } get path(): string { - if (this.#dirPath instanceof Uint8Array) { + if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, this.#dirPath)) { return new TextDecoder().decode(this.#dirPath); } return this.#dirPath; @@ -31,12 +40,12 @@ export default class Dir { read(callback?: (...args: any[]) => void): Promise { return new Promise((resolve, reject) => { if (!this.#asyncIterator) { - this.#asyncIterator = Deno.readDir(this.path)[Symbol.asyncIterator](); + this.#asyncIterator = Deno.readDir(this.path)[SymbolAsyncIterator](); } assert(this.#asyncIterator); - this.#asyncIterator - .next() - .then((iteratorResult) => { + PromisePrototypeThen( + AsyncGeneratorPrototypeNext(this.#asyncIterator), + (iteratorResult) => { resolve( iteratorResult.done ? null : new Dirent(iteratorResult.value), ); @@ -46,21 +55,23 @@ export default class Dir { iteratorResult.done ? null : new Dirent(iteratorResult.value), ); } - }, (err) => { + }, + (err) => { if (callback) { callback(err); } reject(err); - }); + }, + ); }); } readSync(): Dirent | null { if (!this.#syncIterator) { - this.#syncIterator = Deno.readDirSync(this.path)![Symbol.iterator](); + this.#syncIterator = Deno.readDirSync(this.path)![SymbolIterator](); } - const iteratorResult = this.#syncIterator.next(); + const iteratorResult = ArrayIteratorPrototypeNext(this.#syncIterator); if (iteratorResult.done) { return null; } else { @@ -92,7 +103,7 @@ export default class Dir { //No op } - async *[Symbol.asyncIterator](): AsyncIterableIterator { + async *[SymbolAsyncIterator](): AsyncIterableIterator { try { while (true) { const dirent: Dirent | null = await this.read();