Skip to content
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

Feat/add node fstat and fstat sync #847

Merged
merged 3 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Comment on lines +18 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice overload 👍

): 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);
}
},
});