Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): use primordials in ext/node/polyfills/_fs/_fs_dir.ts #24319

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions ext/node/polyfills/_fs/_fs_dir.ts
Original file line number Diff line number Diff line change
@@ -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<Deno.DirEntry, undefined> | null;
Expand All @@ -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;
Expand All @@ -31,12 +40,12 @@ export default class Dir {
read(callback?: (...args: any[]) => void): Promise<Dirent | null> {
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),
);
Expand All @@ -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 {
Expand Down Expand Up @@ -92,7 +103,7 @@ export default class Dir {
//No op
}

async *[Symbol.asyncIterator](): AsyncIterableIterator<Dirent> {
async *[SymbolAsyncIterator](): AsyncIterableIterator<Dirent> {
try {
while (true) {
const dirent: Dirent | null = await this.read();
Expand Down