-
Notifications
You must be signed in to change notification settings - Fork 623
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node/fs): add fstat and fstatSync (#847)
- Loading branch information
1 parent
7e4d6ef
commit d0843ea
Showing
2 changed files
with
141 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 fstat(fd: number, callback: statCallback): void; | ||
export function fstat( | ||
fd: number, | ||
options: { bigint: false }, | ||
callback: statCallback, | ||
): void; | ||
export function fstat( | ||
fd: number, | ||
options: { bigint: true }, | ||
callback: statCallbackBigInt, | ||
): void; | ||
export function fstat( | ||
fd: number, | ||
optionsOrCallback: statCallback | statCallbackBigInt | statOptions, | ||
maybeCallback?: statCallback | statCallbackBigInt, | ||
) { | ||
const callback = | ||
(typeof optionsOrCallback === "function" | ||
? optionsOrCallback | ||
: maybeCallback) as ( | ||
...args: [Error] | [null, BigIntStats | Stats] | ||
) => void; | ||
const options = typeof optionsOrCallback === "object" | ||
? optionsOrCallback | ||
: { bigint: false }; | ||
|
||
if (!callback) throw new Error("No callback function supplied"); | ||
|
||
Deno.fstat(fd).then( | ||
(stat) => callback(null, CFISBIS(stat, options.bigint)), | ||
(err) => callback(err), | ||
); | ||
} | ||
|
||
export function fstatSync(fd: number): Stats; | ||
export function fstatSync( | ||
fd: number, | ||
options: { bigint: false }, | ||
): Stats; | ||
export function fstatSync( | ||
fd: number, | ||
options: { bigint: true }, | ||
): BigIntStats; | ||
export function fstatSync( | ||
fd: number, | ||
options?: statOptions, | ||
): Stats | BigIntStats { | ||
const origin = Deno.fstatSync(fd); | ||
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,82 @@ | ||
import { fstat, fstatSync } from "./_fs_fstat.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", | ||
async fn() { | ||
const file = await Deno.makeTempFile(); | ||
const { rid } = await Deno.open(file); | ||
|
||
await new Promise<Stats>((resolve, reject) => { | ||
fstat(rid, (err: Error | null, stat: Stats) => { | ||
if (err) reject(err); | ||
resolve(stat); | ||
}); | ||
}) | ||
.then( | ||
(stat) => { | ||
assertStats(stat, Deno.fstatSync(rid)); | ||
}, | ||
() => fail(), | ||
) | ||
.finally(() => { | ||
Deno.removeSync(file); | ||
Deno.close(rid); | ||
}); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "ASYNC: get a file BigInt Stats", | ||
async fn() { | ||
const file = await Deno.makeTempFile(); | ||
const { rid } = await Deno.open(file); | ||
|
||
await new Promise<BigIntStats>((resolve, reject) => { | ||
fstat(rid, { bigint: true }, (err: Error | null, stat: BigIntStats) => { | ||
if (err) reject(err); | ||
resolve(stat); | ||
}); | ||
}) | ||
.then( | ||
(stat) => assertStatsBigInt(stat, Deno.fstatSync(rid)), | ||
() => fail(), | ||
) | ||
.finally(() => { | ||
Deno.removeSync(file); | ||
Deno.close(rid); | ||
}); | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: get a file Stats", | ||
fn() { | ||
const file = Deno.makeTempFileSync(); | ||
const { rid } = Deno.openSync(file); | ||
|
||
try { | ||
assertStats(fstatSync(rid), Deno.fstatSync(rid)); | ||
} finally { | ||
Deno.removeSync(file); | ||
Deno.close(rid); | ||
} | ||
}, | ||
}); | ||
|
||
Deno.test({ | ||
name: "SYNC: get a file BigInt Stats", | ||
fn() { | ||
const file = Deno.makeTempFileSync(); | ||
const { rid } = Deno.openSync(file); | ||
|
||
try { | ||
assertStatsBigInt(fstatSync(rid, { bigint: true }), Deno.fstatSync(rid)); | ||
} finally { | ||
Deno.removeSync(file); | ||
Deno.close(rid); | ||
} | ||
}, | ||
}); |