Skip to content

Commit

Permalink
feat(node/fs): add fstat and fstatSync (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
majidsajadi authored Apr 15, 2021
1 parent 7e4d6ef commit d0843ea
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
59 changes: 59 additions & 0 deletions node/_fs/_fs_fstat.ts
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);
}
82 changes: 82 additions & 0 deletions node/_fs/_fs_fstat_test.ts
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);
}
},
});

0 comments on commit d0843ea

Please sign in to comment.