-
Notifications
You must be signed in to change notification settings - Fork 5.7k
fix(ext/node): fs.mkdtemp
and fs.mkdtempSync
compatibility
#30602
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,134 +1,150 @@ | ||
// Copyright 2018-2025 the Deno authors. MIT license. | ||
// Copyright Node.js contributors. All rights reserved. MIT License. | ||
|
||
import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js"; | ||
import { existsSync } from "ext:deno_node/_fs/_fs_exists.ts"; | ||
import { mkdir, mkdirSync } from "ext:deno_node/_fs/_fs_mkdir.ts"; | ||
import { ERR_INVALID_OPT_VALUE_ENCODING } from "ext:deno_node/internal/errors.ts"; | ||
import { promisify } from "ext:deno_node/internal/util.mjs"; | ||
import { normalizeEncoding, promisify } from "ext:deno_node/internal/util.mjs"; | ||
import { primordials } from "ext:core/mod.js"; | ||
import { makeCallback } from "ext:deno_node/_fs/_fs_common.ts"; | ||
|
||
const { | ||
ObjectPrototypeIsPrototypeOf, | ||
Array, | ||
SafeArrayIterator, | ||
MathRandom, | ||
MathFloor, | ||
ArrayPrototypeJoin, | ||
ArrayPrototypeMap, | ||
ObjectPrototype, | ||
} = primordials; | ||
|
||
export type mkdtempCallback = ( | ||
import { Buffer } from "node:buffer"; | ||
import { | ||
getValidatedPathToString, | ||
warnOnNonPortableTemplate, | ||
} from "ext:deno_node/internal/fs/utils.mjs"; | ||
import { | ||
denoErrorToNodeError, | ||
ERR_INVALID_ARG_TYPE, | ||
} from "ext:deno_node/internal/errors.ts"; | ||
import { op_node_mkdtemp, op_node_mkdtemp_sync } from "ext:core/ops"; | ||
import type { Encoding } from "node:crypto"; | ||
|
||
const { PromisePrototypeThen } = primordials; | ||
|
||
export type MkdtempCallback = ( | ||
err: Error | null, | ||
directory?: string, | ||
) => void; | ||
export type MkdtempBufferCallback = ( | ||
err: Error | null, | ||
directory?: Buffer<ArrayBufferLike>, | ||
) => void; | ||
type MkdTempPromise = ( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options?: { encoding: string } | string, | ||
) => Promise<string>; | ||
type MkdTempPromiseBuffer = ( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options: { encoding: "buffer" } | "buffer", | ||
) => Promise<Buffer<ArrayBufferLike>>; | ||
|
||
// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtemp_prefix_options_callback | ||
export function mkdtemp(prefix: string, callback: mkdtempCallback): void; | ||
export function mkdtemp( | ||
prefix: string, | ||
prefix: string | Buffer | Uint8Array | URL, | ||
callback: MkdtempCallback, | ||
): void; | ||
export function mkdtemp( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options: { encoding: "buffer" } | "buffer", | ||
callback: MkdtempBufferCallback, | ||
): void; | ||
export function mkdtemp( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options: { encoding: string } | string, | ||
callback: mkdtempCallback, | ||
callback: MkdtempCallback, | ||
): void; | ||
export function mkdtemp( | ||
prefix: string, | ||
options: { encoding: string } | string | mkdtempCallback | undefined, | ||
callback?: mkdtempCallback, | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options: { encoding: string } | string | MkdtempCallback | undefined, | ||
callback?: MkdtempCallback | MkdtempBufferCallback, | ||
) { | ||
if (typeof options === "function") { | ||
callback = options; | ||
options = undefined; | ||
} | ||
callback = makeCallback(callback); | ||
|
||
const encoding: string | undefined = parseEncoding(options); | ||
const path = tempDirPath(prefix); | ||
|
||
mkdir( | ||
path, | ||
{ recursive: false, mode: 0o700 }, | ||
(err: Error | null | undefined) => { | ||
if (err) callback(err); | ||
else callback(null, decode(path, encoding)); | ||
}, | ||
const encoding = parseEncoding(options); | ||
prefix = getValidatedPathToString(prefix, "prefix"); | ||
|
||
warnOnNonPortableTemplate(prefix); | ||
|
||
PromisePrototypeThen( | ||
op_node_mkdtemp(prefix), | ||
(path: string) => callback(null, decode(path, encoding)), | ||
(err: Error) => | ||
callback(denoErrorToNodeError(err, { | ||
syscall: "mkdtemp", | ||
path: `${prefix}XXXXXX`, | ||
})), | ||
); | ||
} | ||
|
||
export const mkdtempPromise = promisify(mkdtemp) as ( | ||
prefix: string, | ||
options?: { encoding: string } | string, | ||
) => Promise<string>; | ||
export const mkdtempPromise = promisify(mkdtemp) as | ||
| MkdTempPromise | ||
| MkdTempPromiseBuffer; | ||
|
||
// https://nodejs.org/dist/latest-v15.x/docs/api/fs.html#fs_fs_mkdtempsync_prefix_options | ||
export function mkdtempSync( | ||
prefix: string, | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options?: { encoding: "buffer" } | "buffer", | ||
): Buffer<ArrayBufferLike>; | ||
export function mkdtempSync( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options?: { encoding: string } | string, | ||
): string { | ||
const encoding: string | undefined = parseEncoding(options); | ||
const path = tempDirPath(prefix); | ||
): string; | ||
export function mkdtempSync( | ||
prefix: string | Buffer | Uint8Array | URL, | ||
options?: { encoding: string } | string, | ||
): string | Buffer<ArrayBufferLike> { | ||
const encoding = parseEncoding(options); | ||
prefix = getValidatedPathToString(prefix, "prefix"); | ||
|
||
warnOnNonPortableTemplate(prefix); | ||
|
||
try { | ||
const path = op_node_mkdtemp_sync(prefix) as string; | ||
return decode(path, encoding); | ||
} catch (err) { | ||
throw denoErrorToNodeError(err as Error, { | ||
syscall: "mkdtemp", | ||
path: `${prefix}XXXXXX`, | ||
}); | ||
} | ||
} | ||
|
||
mkdirSync(path, { recursive: false, mode: 0o700 }); | ||
return decode(path, encoding); | ||
function decode(str: string, encoding: Encoding): string; | ||
function decode(str: string, encoding: "buffer"): Buffer<ArrayBufferLike>; | ||
function decode( | ||
str: string, | ||
encoding: Encoding | "buffer", | ||
): string | Buffer<ArrayBufferLike> { | ||
if (encoding === "utf8") return str; | ||
const buffer = Buffer.from(str); | ||
if (encoding === "buffer") return buffer; | ||
// deno-lint-ignore prefer-primordials | ||
return buffer.toString(encoding); | ||
} | ||
|
||
function parseEncoding( | ||
optionsOrCallback?: { encoding: string } | string | mkdtempCallback, | ||
): string | undefined { | ||
options: string | { encoding?: string } | undefined, | ||
): Encoding | "buffer" { | ||
let encoding: string | undefined; | ||
if (typeof optionsOrCallback === "function") { | ||
encoding = undefined; | ||
} else if (isOptionsObject(optionsOrCallback)) { | ||
encoding = optionsOrCallback.encoding; | ||
|
||
if (typeof options === "undefined" || options === null) { | ||
encoding = "utf8"; | ||
} else if (typeof options === "string") { | ||
encoding = options; | ||
} else if (typeof options === "object") { | ||
encoding = options.encoding ?? "utf8"; | ||
} else { | ||
encoding = optionsOrCallback; | ||
throw new ERR_INVALID_ARG_TYPE("options", ["string", "Object"], options); | ||
} | ||
|
||
if (encoding) { | ||
try { | ||
new TextDecoder(encoding); | ||
} catch { | ||
throw new ERR_INVALID_OPT_VALUE_ENCODING(encoding); | ||
} | ||
if (encoding === "buffer") { | ||
return encoding; | ||
} | ||
|
||
return encoding; | ||
} | ||
|
||
function decode(str: string, encoding?: string): string { | ||
if (!encoding) return str; | ||
else { | ||
const decoder = new TextDecoder(encoding); | ||
const encoder = new TextEncoder(); | ||
return decoder.decode(encoder.encode(str)); | ||
const parsedEncoding = normalizeEncoding(encoding); | ||
if (!parsedEncoding) { | ||
throw new ERR_INVALID_ARG_TYPE("encoding", encoding, "is invalid encoding"); | ||
} | ||
} | ||
|
||
const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
function randomName(): string { | ||
return ArrayPrototypeJoin( | ||
ArrayPrototypeMap( | ||
[...new SafeArrayIterator(Array(6))], | ||
() => CHARS[MathFloor(MathRandom() * CHARS.length)], | ||
), | ||
"", | ||
); | ||
} | ||
|
||
function tempDirPath(prefix: string): string { | ||
let path: string; | ||
do { | ||
path = prefix + randomName(); | ||
} while (existsSync(path)); | ||
|
||
return path; | ||
} | ||
|
||
function isOptionsObject(value: unknown): value is { encoding: string } { | ||
return ( | ||
value !== null && | ||
typeof value === "object" && | ||
ObjectPrototypeIsPrototypeOf(ObjectPrototype, value) | ||
); | ||
return parsedEncoding; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chose this approach instead of using the tempfile crate because it relies on fastrand, which is not cryptographically secure. Not that it matters that much, but this method aligns more closely with how libuv handles temp file suffixes https://github.com/nodejs/node/blob/591ba692bfe30408e6a67397e7d18bfa1b9c3561/deps/uv/src/win/fs.c#L1267-L1270
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!