|
1 | 1 | // Copyright 2018-2025 the Deno authors. MIT license.
|
2 | 2 | // Copyright Node.js contributors. All rights reserved. MIT License.
|
3 | 3 |
|
4 |
| -import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js"; |
5 |
| -import { existsSync } from "ext:deno_node/_fs/_fs_exists.ts"; |
6 |
| -import { mkdir, mkdirSync } from "ext:deno_node/_fs/_fs_mkdir.ts"; |
7 |
| -import { ERR_INVALID_OPT_VALUE_ENCODING } from "ext:deno_node/internal/errors.ts"; |
8 |
| -import { promisify } from "ext:deno_node/internal/util.mjs"; |
| 4 | +import { normalizeEncoding, promisify } from "ext:deno_node/internal/util.mjs"; |
9 | 5 | import { primordials } from "ext:core/mod.js";
|
10 | 6 | import { makeCallback } from "ext:deno_node/_fs/_fs_common.ts";
|
11 |
| - |
12 |
| -const { |
13 |
| - ObjectPrototypeIsPrototypeOf, |
14 |
| - Array, |
15 |
| - SafeArrayIterator, |
16 |
| - MathRandom, |
17 |
| - MathFloor, |
18 |
| - ArrayPrototypeJoin, |
19 |
| - ArrayPrototypeMap, |
20 |
| - ObjectPrototype, |
21 |
| -} = primordials; |
22 |
| - |
23 |
| -export type mkdtempCallback = ( |
| 7 | +import { Buffer } from "node:buffer"; |
| 8 | +import { |
| 9 | + getValidatedPathToString, |
| 10 | + warnOnNonPortableTemplate, |
| 11 | +} from "ext:deno_node/internal/fs/utils.mjs"; |
| 12 | +import { |
| 13 | + denoErrorToNodeError, |
| 14 | + ERR_INVALID_ARG_TYPE, |
| 15 | +} from "ext:deno_node/internal/errors.ts"; |
| 16 | +import { op_node_mkdtemp, op_node_mkdtemp_sync } from "ext:core/ops"; |
| 17 | +import type { Encoding } from "node:crypto"; |
| 18 | + |
| 19 | +const { PromisePrototypeThen } = primordials; |
| 20 | + |
| 21 | +export type MkdtempCallback = ( |
24 | 22 | err: Error | null,
|
25 | 23 | directory?: string,
|
26 | 24 | ) => void;
|
| 25 | +export type MkdtempBufferCallback = ( |
| 26 | + err: Error | null, |
| 27 | + directory?: Buffer<ArrayBufferLike>, |
| 28 | +) => void; |
| 29 | +type MkdTempPromise = ( |
| 30 | + prefix: string | Buffer | Uint8Array | URL, |
| 31 | + options?: { encoding: string } | string, |
| 32 | +) => Promise<string>; |
| 33 | +type MkdTempPromiseBuffer = ( |
| 34 | + prefix: string | Buffer | Uint8Array | URL, |
| 35 | + options: { encoding: "buffer" } | "buffer", |
| 36 | +) => Promise<Buffer<ArrayBufferLike>>; |
27 | 37 |
|
28 | 38 | // https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtemp_prefix_options_callback
|
29 |
| -export function mkdtemp(prefix: string, callback: mkdtempCallback): void; |
30 | 39 | export function mkdtemp(
|
31 |
| - prefix: string, |
| 40 | + prefix: string | Buffer | Uint8Array | URL, |
| 41 | + callback: MkdtempCallback, |
| 42 | +): void; |
| 43 | +export function mkdtemp( |
| 44 | + prefix: string | Buffer | Uint8Array | URL, |
| 45 | + options: { encoding: "buffer" } | "buffer", |
| 46 | + callback: MkdtempBufferCallback, |
| 47 | +): void; |
| 48 | +export function mkdtemp( |
| 49 | + prefix: string | Buffer | Uint8Array | URL, |
32 | 50 | options: { encoding: string } | string,
|
33 |
| - callback: mkdtempCallback, |
| 51 | + callback: MkdtempCallback, |
34 | 52 | ): void;
|
35 | 53 | export function mkdtemp(
|
36 |
| - prefix: string, |
37 |
| - options: { encoding: string } | string | mkdtempCallback | undefined, |
38 |
| - callback?: mkdtempCallback, |
| 54 | + prefix: string | Buffer | Uint8Array | URL, |
| 55 | + options: { encoding: string } | string | MkdtempCallback | undefined, |
| 56 | + callback?: MkdtempCallback | MkdtempBufferCallback, |
39 | 57 | ) {
|
40 | 58 | if (typeof options === "function") {
|
41 | 59 | callback = options;
|
42 | 60 | options = undefined;
|
43 | 61 | }
|
44 | 62 | callback = makeCallback(callback);
|
45 |
| - |
46 |
| - const encoding: string | undefined = parseEncoding(options); |
47 |
| - const path = tempDirPath(prefix); |
48 |
| - |
49 |
| - mkdir( |
50 |
| - path, |
51 |
| - { recursive: false, mode: 0o700 }, |
52 |
| - (err: Error | null | undefined) => { |
53 |
| - if (err) callback(err); |
54 |
| - else callback(null, decode(path, encoding)); |
55 |
| - }, |
| 63 | + const encoding = parseEncoding(options); |
| 64 | + prefix = getValidatedPathToString(prefix, "prefix"); |
| 65 | + |
| 66 | + warnOnNonPortableTemplate(prefix); |
| 67 | + |
| 68 | + PromisePrototypeThen( |
| 69 | + op_node_mkdtemp(prefix), |
| 70 | + (path: string) => callback(null, decode(path, encoding)), |
| 71 | + (err: Error) => |
| 72 | + callback(denoErrorToNodeError(err, { |
| 73 | + syscall: "mkdtemp", |
| 74 | + path: `${prefix}XXXXXX`, |
| 75 | + })), |
56 | 76 | );
|
57 | 77 | }
|
58 | 78 |
|
59 |
| -export const mkdtempPromise = promisify(mkdtemp) as ( |
60 |
| - prefix: string, |
61 |
| - options?: { encoding: string } | string, |
62 |
| -) => Promise<string>; |
| 79 | +export const mkdtempPromise = promisify(mkdtemp) as |
| 80 | + | MkdTempPromise |
| 81 | + | MkdTempPromiseBuffer; |
63 | 82 |
|
64 | 83 | // https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtempsync_prefix_options
|
65 | 84 | export function mkdtempSync(
|
66 |
| - prefix: string, |
| 85 | + prefix: string | Buffer | Uint8Array | URL, |
| 86 | + options?: { encoding: "buffer" } | "buffer", |
| 87 | +): Buffer<ArrayBufferLike>; |
| 88 | +export function mkdtempSync( |
| 89 | + prefix: string | Buffer | Uint8Array | URL, |
67 | 90 | options?: { encoding: string } | string,
|
68 |
| -): string { |
69 |
| - const encoding: string | undefined = parseEncoding(options); |
70 |
| - const path = tempDirPath(prefix); |
| 91 | +): string; |
| 92 | +export function mkdtempSync( |
| 93 | + prefix: string | Buffer | Uint8Array | URL, |
| 94 | + options?: { encoding: string } | string, |
| 95 | +): string | Buffer<ArrayBufferLike> { |
| 96 | + const encoding = parseEncoding(options); |
| 97 | + prefix = getValidatedPathToString(prefix, "prefix"); |
| 98 | + |
| 99 | + warnOnNonPortableTemplate(prefix); |
| 100 | + |
| 101 | + try { |
| 102 | + const path = op_node_mkdtemp_sync(prefix) as string; |
| 103 | + return decode(path, encoding); |
| 104 | + } catch (err) { |
| 105 | + throw denoErrorToNodeError(err as Error, { |
| 106 | + syscall: "mkdtemp", |
| 107 | + path: `${prefix}XXXXXX`, |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
71 | 111 |
|
72 |
| - mkdirSync(path, { recursive: false, mode: 0o700 }); |
73 |
| - return decode(path, encoding); |
| 112 | +function decode(str: string, encoding: Encoding): string; |
| 113 | +function decode(str: string, encoding: "buffer"): Buffer<ArrayBufferLike>; |
| 114 | +function decode( |
| 115 | + str: string, |
| 116 | + encoding: Encoding | "buffer", |
| 117 | +): string | Buffer<ArrayBufferLike> { |
| 118 | + if (encoding === "utf8") return str; |
| 119 | + const buffer = Buffer.from(str); |
| 120 | + if (encoding === "buffer") return buffer; |
| 121 | + // deno-lint-ignore prefer-primordials |
| 122 | + return buffer.toString(encoding); |
74 | 123 | }
|
75 | 124 |
|
76 | 125 | function parseEncoding(
|
77 |
| - optionsOrCallback?: { encoding: string } | string | mkdtempCallback, |
78 |
| -): string | undefined { |
| 126 | + options: string | { encoding?: string } | undefined, |
| 127 | +): Encoding | "buffer" { |
79 | 128 | let encoding: string | undefined;
|
80 |
| - if (typeof optionsOrCallback === "function") { |
81 |
| - encoding = undefined; |
82 |
| - } else if (isOptionsObject(optionsOrCallback)) { |
83 |
| - encoding = optionsOrCallback.encoding; |
| 129 | + |
| 130 | + if (typeof options === "undefined" || options === null) { |
| 131 | + encoding = "utf8"; |
| 132 | + } else if (typeof options === "string") { |
| 133 | + encoding = options; |
| 134 | + } else if (typeof options === "object") { |
| 135 | + encoding = options.encoding ?? "utf8"; |
84 | 136 | } else {
|
85 |
| - encoding = optionsOrCallback; |
| 137 | + throw new ERR_INVALID_ARG_TYPE("options", ["string", "Object"], options); |
86 | 138 | }
|
87 | 139 |
|
88 |
| - if (encoding) { |
89 |
| - try { |
90 |
| - new TextDecoder(encoding); |
91 |
| - } catch { |
92 |
| - throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding); |
93 |
| - } |
| 140 | + if (encoding === "buffer") { |
| 141 | + return encoding; |
94 | 142 | }
|
95 | 143 |
|
96 |
| - return encoding; |
97 |
| -} |
98 |
| - |
99 |
| -function decode(str: string, encoding?: string): string { |
100 |
| - if (!encoding) return str; |
101 |
| - else { |
102 |
| - const decoder = new TextDecoder(encoding); |
103 |
| - const encoder = new TextEncoder(); |
104 |
| - return decoder.decode(encoder.encode(str)); |
| 144 | + const parsedEncoding = normalizeEncoding(encoding); |
| 145 | + if (!parsedEncoding) { |
| 146 | + throw new ERR_INVALID_ARG_TYPE("encoding", encoding, "is invalid encoding"); |
105 | 147 | }
|
106 |
| -} |
107 | 148 |
|
108 |
| -const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
109 |
| -function randomName(): string { |
110 |
| - return ArrayPrototypeJoin( |
111 |
| - ArrayPrototypeMap( |
112 |
| - [...new SafeArrayIterator(Array(6))], |
113 |
| - () => CHARS[MathFloor(MathRandom() * CHARS.length)], |
114 |
| - ), |
115 |
| - "", |
116 |
| - ); |
117 |
| -} |
118 |
| - |
119 |
| -function tempDirPath(prefix: string): string { |
120 |
| - let path: string; |
121 |
| - do { |
122 |
| - path = prefix + randomName(); |
123 |
| - } while (existsSync(path)); |
124 |
| - |
125 |
| - return path; |
126 |
| -} |
127 |
| - |
128 |
| -function isOptionsObject(value: unknown): value is { encoding: string } { |
129 |
| - return ( |
130 |
| - value !== null && |
131 |
| - typeof value === "object" && |
132 |
| - ObjectPrototypeIsPrototypeOf(ObjectPrototype, value) |
133 |
| - ); |
| 149 | + return parsedEncoding; |
134 | 150 | }
|
0 commit comments