Skip to content

Commit cf8b977

Browse files
authored
fix(ext/node): use primordials in ext/node/polyfills/_fs/_fs_mkdtemp.ts (#29072)
Towards #24236
1 parent e498e7d commit cf8b977

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

ext/node/polyfills/_fs/_fs_mkdtemp.ts

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright 2018-2025 the Deno authors. MIT license.
22
// Copyright Node.js contributors. All rights reserved. MIT License.
33

4-
// TODO(petamoriken): enable prefer-primordials for node polyfills
5-
// deno-lint-ignore-file prefer-primordials
6-
74
import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js";
85
import { existsSync } from "ext:deno_node/_fs/_fs_exists.ts";
96
import { mkdir, mkdirSync } from "ext:deno_node/_fs/_fs_mkdir.ts";
@@ -12,6 +9,18 @@ import {
129
ERR_INVALID_OPT_VALUE_ENCODING,
1310
} from "ext:deno_node/internal/errors.ts";
1411
import { promisify } from "ext:deno_node/internal/util.mjs";
12+
import { primordials } from "ext:core/mod.js";
13+
14+
const {
15+
ObjectPrototypeIsPrototypeOf,
16+
Array,
17+
SafeArrayIterator,
18+
MathRandom,
19+
MathFloor,
20+
ArrayPrototypeJoin,
21+
ArrayPrototypeMap,
22+
ObjectPrototype,
23+
} = primordials;
1524

1625
export type mkdtempCallback = (
1726
err: Error | null,
@@ -70,10 +79,13 @@ function parseEncoding(
7079
optionsOrCallback?: { encoding: string } | string | mkdtempCallback,
7180
): string | undefined {
7281
let encoding: string | undefined;
73-
if (typeof optionsOrCallback == "function") encoding = undefined;
74-
else if (optionsOrCallback instanceof Object) {
75-
encoding = optionsOrCallback?.encoding;
76-
} else encoding = optionsOrCallback;
82+
if (typeof optionsOrCallback === "function") {
83+
encoding = undefined;
84+
} else if (isOptionsObject(optionsOrCallback)) {
85+
encoding = optionsOrCallback.encoding;
86+
} else {
87+
encoding = optionsOrCallback;
88+
}
7789

7890
if (encoding) {
7991
try {
@@ -97,9 +109,13 @@ function decode(str: string, encoding?: string): string {
97109

98110
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
99111
function randomName(): string {
100-
return [...Array(6)].map(() =>
101-
CHARS[Math.floor(Math.random() * CHARS.length)]
102-
).join("");
112+
return ArrayPrototypeJoin(
113+
ArrayPrototypeMap(
114+
[...new SafeArrayIterator(Array(6))],
115+
() => CHARS[MathFloor(MathRandom() * CHARS.length)],
116+
),
117+
"",
118+
);
103119
}
104120

105121
function tempDirPath(prefix: string): string {
@@ -110,3 +126,11 @@ function tempDirPath(prefix: string): string {
110126

111127
return path;
112128
}
129+
130+
function isOptionsObject(value: unknown): value is { encoding: string } {
131+
return (
132+
value !== null &&
133+
typeof value === "object" &&
134+
ObjectPrototypeIsPrototypeOf(ObjectPrototype, value)
135+
);
136+
}

0 commit comments

Comments
 (0)