-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(std/node/fs): adding readdir, rename, and some others (#7666)
- Loading branch information
1 parent
a51408a
commit 40324ff
Showing
17 changed files
with
1,366 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
BigIntStats, | ||
CFISBIS, | ||
statCallback, | ||
statCallbackBigInt, | ||
statOptions, | ||
Stats, | ||
} from "./_fs_stat.ts"; | ||
|
||
export function lstat(path: string | URL, callback: statCallback): void; | ||
export function lstat( | ||
path: string | URL, | ||
options: { bigint: false }, | ||
callback: statCallback, | ||
): void; | ||
export function lstat( | ||
path: string | URL, | ||
options: { bigint: true }, | ||
callback: statCallbackBigInt, | ||
): void; | ||
export function lstat( | ||
path: string | URL, | ||
optionsOrCallback: statCallback | statCallbackBigInt | statOptions, | ||
maybeCallback?: statCallback | statCallbackBigInt, | ||
) { | ||
const callback = | ||
(typeof optionsOrCallback === "function" | ||
? optionsOrCallback | ||
: maybeCallback) as ( | ||
err: Error | undefined, | ||
stat: BigIntStats | Stats, | ||
) => void; | ||
const options = typeof optionsOrCallback === "object" | ||
? optionsOrCallback | ||
: { bigint: false }; | ||
|
||
if (!callback) throw new Error("No callback function supplied"); | ||
|
||
Deno.lstat(path) | ||
.then((stat) => callback(undefined, CFISBIS(stat, options.bigint))) | ||
.catch((err) => callback(err, err)); | ||
} | ||
|
||
export function lstatSync(path: string | URL): Stats; | ||
export function lstatSync( | ||
path: string | URL, | ||
options: { bigint: false }, | ||
): Stats; | ||
export function lstatSync( | ||
path: string | URL, | ||
options: { bigint: true }, | ||
): BigIntStats; | ||
export function lstatSync( | ||
path: string | URL, | ||
options?: statOptions, | ||
): Stats | BigIntStats { | ||
const origin = Deno.lstatSync(path); | ||
return CFISBIS(origin, options?.bigint || false); | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { lstat, lstatSync } from "./_fs_lstat.ts"; | ||
import { fail } from "../../testing/asserts.ts"; | ||
import { assertStats, assertStatsBigInt } from "./_fs_stat_test.ts"; | ||
import type { BigIntStats, Stats } from "./_fs_stat.ts"; | ||
|
||
Deno.test({ | ||
name: "ASYNC: get a file Stats (lstat)", | ||
async fn() { | ||
const file = Deno.makeTempFileSync(); | ||
await new Promise<Stats>((resolve, reject) => { | ||
lstat(file, (err, stat) => { | ||
if (err) reject(err); | ||
resolve(stat); | ||
}); | ||
}) | ||
.then((stat) => { | ||
assertStats(stat, Deno.lstatSync(file)); | ||
}) | ||
.catch(() => fail()) | ||
.finally(() => { | ||
Deno.removeSync(file); | ||
}); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: get a file Stats (lstat)", | ||
fn() { | ||
const file = Deno.makeTempFileSync(); | ||
assertStats(lstatSync(file), Deno.lstatSync(file)); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "ASYNC: get a file BigInt Stats (lstat)", | ||
async fn() { | ||
const file = Deno.makeTempFileSync(); | ||
await new Promise<BigIntStats>((resolve, reject) => { | ||
lstat(file, { bigint: true }, (err, stat) => { | ||
if (err) reject(err); | ||
resolve(stat); | ||
}); | ||
}) | ||
.then((stat) => assertStatsBigInt(stat, Deno.lstatSync(file))) | ||
.catch(() => fail()) | ||
.finally(() => Deno.removeSync(file)); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: BigInt Stats (lstat)", | ||
fn() { | ||
const file = Deno.makeTempFileSync(); | ||
assertStatsBigInt(lstatSync(file, { bigint: true }), Deno.lstatSync(file)); | ||
}, | ||
}); |
This file contains 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 |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { existsSync } from "../../fs/mod.ts"; | ||
import { fromFileUrl } from "../path.ts"; | ||
import { getOpenOptions } from "./_fs_common.ts"; | ||
|
||
type openFlags = | ||
| "a" | ||
| "ax" | ||
| "a+" | ||
| "ax+" | ||
| "as" | ||
| "as+" | ||
| "r" | ||
| "r+" | ||
| "rs+" | ||
| "w" | ||
| "wx" | ||
| "w+" | ||
| "wx+"; | ||
|
||
type openCallback = (err: Error | undefined, fd: number) => void; | ||
|
||
function convertFlagAndModeToOptions( | ||
flag?: openFlags, | ||
mode?: number, | ||
): Deno.OpenOptions | undefined { | ||
if (!flag && !mode) return undefined; | ||
if (!flag && mode) return { mode }; | ||
return { ...getOpenOptions(flag), mode }; | ||
} | ||
|
||
export function open(path: string | URL, callback: openCallback): void; | ||
export function open( | ||
path: string | URL, | ||
flags: openFlags, | ||
callback: openCallback, | ||
): void; | ||
export function open( | ||
path: string | URL, | ||
flags: openFlags, | ||
mode: number, | ||
callback: openCallback, | ||
): void; | ||
export function open( | ||
path: string | URL, | ||
flagsOrCallback: openCallback | openFlags, | ||
callbackOrMode?: openCallback | number, | ||
maybeCallback?: openCallback, | ||
) { | ||
const flags = typeof flagsOrCallback === "string" | ||
? flagsOrCallback | ||
: undefined; | ||
const callback = typeof flagsOrCallback === "function" | ||
? flagsOrCallback | ||
: typeof callbackOrMode === "function" | ||
? callbackOrMode | ||
: maybeCallback; | ||
const mode = typeof callbackOrMode === "number" ? callbackOrMode : undefined; | ||
path = path instanceof URL ? fromFileUrl(path) : path; | ||
|
||
if (!callback) throw new Error("No callback function supplied"); | ||
|
||
if (["ax", "ax+", "wx", "wx+"].includes(flags || "") && existsSync(path)) { | ||
const err = new Error(`EEXIST: file already exists, open '${path}'`); | ||
callback(err, 0); | ||
} else { | ||
if (flags === "as" || flags === "as+") { | ||
try { | ||
const res = openSync(path, flags, mode); | ||
callback(undefined, res); | ||
} catch (error) { | ||
callback(error, error); | ||
} | ||
return; | ||
} | ||
Deno.open(path, convertFlagAndModeToOptions(flags, mode)) | ||
.then((file) => callback(undefined, file.rid)) | ||
.catch((err) => callback(err, err)); | ||
} | ||
} | ||
|
||
export function openSync(path: string | URL): number; | ||
export function openSync(path: string | URL, flags?: openFlags): number; | ||
export function openSync(path: string | URL, mode?: number): number; | ||
export function openSync( | ||
path: string | URL, | ||
flags?: openFlags, | ||
mode?: number, | ||
): number; | ||
export function openSync( | ||
path: string | URL, | ||
flagsOrMode?: openFlags | number, | ||
maybeMode?: number, | ||
) { | ||
const flags = typeof flagsOrMode === "string" ? flagsOrMode : undefined; | ||
const mode = typeof flagsOrMode === "number" ? flagsOrMode : maybeMode; | ||
path = path instanceof URL ? fromFileUrl(path) : path; | ||
|
||
if (["ax", "ax+", "wx", "wx+"].includes(flags || "") && existsSync(path)) { | ||
throw new Error(`EEXIST: file already exists, open '${path}'`); | ||
} | ||
|
||
return Deno.openSync(path, convertFlagAndModeToOptions(flags, mode)).rid; | ||
} |
Oops, something went wrong.