1
1
// Copyright 2018-2025 the Deno authors. MIT license.
2
2
// Copyright Node.js contributors. All rights reserved. MIT License.
3
3
4
- // TODO(petamoriken): enable prefer-primordials for node polyfills
5
- // deno-lint-ignore-file prefer-primordials
6
-
7
4
import { TextDecoder , TextEncoder } from "ext:deno_web/08_text_encoding.js" ;
8
5
import { existsSync } from "ext:deno_node/_fs/_fs_exists.ts" ;
9
6
import { mkdir , mkdirSync } from "ext:deno_node/_fs/_fs_mkdir.ts" ;
@@ -12,6 +9,18 @@ import {
12
9
ERR_INVALID_OPT_VALUE_ENCODING ,
13
10
} from "ext:deno_node/internal/errors.ts" ;
14
11
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 ;
15
24
16
25
export type mkdtempCallback = (
17
26
err : Error | null ,
@@ -70,10 +79,13 @@ function parseEncoding(
70
79
optionsOrCallback ?: { encoding : string } | string | mkdtempCallback ,
71
80
) : string | undefined {
72
81
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
+ }
77
89
78
90
if ( encoding ) {
79
91
try {
@@ -97,9 +109,13 @@ function decode(str: string, encoding?: string): string {
97
109
98
110
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
99
111
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
+ ) ;
103
119
}
104
120
105
121
function tempDirPath ( prefix : string ) : string {
@@ -110,3 +126,11 @@ function tempDirPath(prefix: string): string {
110
126
111
127
return path ;
112
128
}
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