mirror of
https://github.com/denoland/deno.git
synced 2024-12-28 01:59:06 -05:00
fix(ext/node): add fs.promises.fstat
and FileHandle#stat
(#26719)
Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
parent
12b377247b
commit
6b7e4c331b
5 changed files with 57 additions and 2 deletions
|
@ -63,3 +63,24 @@ export function fstatSync(
|
||||||
const origin = new FsFile(fd, Symbol.for("Deno.internal.FsFile")).statSync();
|
const origin = new FsFile(fd, Symbol.for("Deno.internal.FsFile")).statSync();
|
||||||
return CFISBIS(origin, options?.bigint || false);
|
return CFISBIS(origin, options?.bigint || false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function fstatPromise(fd: number): Promise<Stats>;
|
||||||
|
export function fstatPromise(
|
||||||
|
fd: number,
|
||||||
|
options: { bigint: false },
|
||||||
|
): Promise<Stats>;
|
||||||
|
export function fstatPromise(
|
||||||
|
fd: number,
|
||||||
|
options: { bigint: true },
|
||||||
|
): Promise<BigIntStats>;
|
||||||
|
export function fstatPromise(
|
||||||
|
fd: number,
|
||||||
|
options?: statOptions,
|
||||||
|
): Stats | BigIntStats {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fstat(fd, options, (err, stats) => {
|
||||||
|
if (err) reject(err);
|
||||||
|
else resolve(stats);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ import Dir from "ext:deno_node/_fs/_fs_dir.ts";
|
||||||
import Dirent from "ext:deno_node/_fs/_fs_dirent.ts";
|
import Dirent from "ext:deno_node/_fs/_fs_dirent.ts";
|
||||||
import { exists, existsSync } from "ext:deno_node/_fs/_fs_exists.ts";
|
import { exists, existsSync } from "ext:deno_node/_fs/_fs_exists.ts";
|
||||||
import { fdatasync, fdatasyncSync } from "ext:deno_node/_fs/_fs_fdatasync.ts";
|
import { fdatasync, fdatasyncSync } from "ext:deno_node/_fs/_fs_fdatasync.ts";
|
||||||
import { fstat, fstatSync } from "ext:deno_node/_fs/_fs_fstat.ts";
|
import { fstat, fstatPromise, fstatSync } from "ext:deno_node/_fs/_fs_fstat.ts";
|
||||||
import { fsync, fsyncSync } from "ext:deno_node/_fs/_fs_fsync.ts";
|
import { fsync, fsyncSync } from "ext:deno_node/_fs/_fs_fsync.ts";
|
||||||
import { ftruncate, ftruncateSync } from "ext:deno_node/_fs/_fs_ftruncate.ts";
|
import { ftruncate, ftruncateSync } from "ext:deno_node/_fs/_fs_ftruncate.ts";
|
||||||
import { futimes, futimesSync } from "ext:deno_node/_fs/_fs_futimes.ts";
|
import { futimes, futimesSync } from "ext:deno_node/_fs/_fs_futimes.ts";
|
||||||
|
@ -174,6 +174,7 @@ const promises = {
|
||||||
lstat: lstatPromise,
|
lstat: lstatPromise,
|
||||||
stat: statPromise,
|
stat: statPromise,
|
||||||
statfs: statfsPromise,
|
statfs: statfsPromise,
|
||||||
|
fstat: fstatPromise,
|
||||||
link: linkPromise,
|
link: linkPromise,
|
||||||
unlink: unlinkPromise,
|
unlink: unlinkPromise,
|
||||||
chmod: chmodPromise,
|
chmod: chmodPromise,
|
||||||
|
|
|
@ -16,6 +16,7 @@ export const readlink = fsPromises.readlink;
|
||||||
export const symlink = fsPromises.symlink;
|
export const symlink = fsPromises.symlink;
|
||||||
export const lstat = fsPromises.lstat;
|
export const lstat = fsPromises.lstat;
|
||||||
export const stat = fsPromises.stat;
|
export const stat = fsPromises.stat;
|
||||||
|
export const fstat = fsPromises.fstat;
|
||||||
export const link = fsPromises.link;
|
export const link = fsPromises.link;
|
||||||
export const unlink = fsPromises.unlink;
|
export const unlink = fsPromises.unlink;
|
||||||
export const chmod = fsPromises.chmod;
|
export const chmod = fsPromises.chmod;
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
import { EventEmitter } from "node:events";
|
import { EventEmitter } from "node:events";
|
||||||
import { Buffer } from "node:buffer";
|
import { Buffer } from "node:buffer";
|
||||||
import { promises, read, write } from "node:fs";
|
import { promises, read, write } from "node:fs";
|
||||||
|
export type { BigIntStats, Stats } from "ext:deno_node/_fs/_fs_stat.ts";
|
||||||
import {
|
import {
|
||||||
BinaryOptionsArgument,
|
BinaryOptionsArgument,
|
||||||
FileOptionsArgument,
|
FileOptionsArgument,
|
||||||
|
@ -141,6 +142,13 @@ export class FileHandle extends EventEmitter {
|
||||||
// Note that Deno.close is not async
|
// Note that Deno.close is not async
|
||||||
return Promise.resolve(core.close(this.fd));
|
return Promise.resolve(core.close(this.fd));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stat(): Promise<Stats>;
|
||||||
|
stat(options: { bigint: false }): Promise<Stats>;
|
||||||
|
stat(options: { bigint: true }): Promise<BigIntStats>;
|
||||||
|
stat(options?: { bigint: boolean }): Promise<Stats | BigIntStats> {
|
||||||
|
return fsCall(promises.fstat, this, options);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fsCall(fn, handle, ...args) {
|
function fsCall(fn, handle, ...args) {
|
||||||
|
@ -152,7 +160,7 @@ function fsCall(fn, handle, ...args) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return fn(handle, ...args);
|
return fn(handle.fd, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
|
@ -93,3 +93,27 @@ Deno.test("[node/fs filehandle.write] Write from string", async function () {
|
||||||
assertEquals(res.bytesWritten, 11);
|
assertEquals(res.bytesWritten, 11);
|
||||||
assertEquals(decoder.decode(data), "hello world");
|
assertEquals(decoder.decode(data), "hello world");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("[node/fs filehandle.stat] Get file status", async function () {
|
||||||
|
const fileHandle = await fs.open(testData);
|
||||||
|
const stat = await fileHandle.stat();
|
||||||
|
|
||||||
|
assertEquals(stat.isFile(), true);
|
||||||
|
assertEquals(stat.size, "hello world".length);
|
||||||
|
|
||||||
|
await fileHandle.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("[node/fs filehandle.writeFile] Write to file", async function () {
|
||||||
|
const tempFile: string = await Deno.makeTempFile();
|
||||||
|
const fileHandle = await fs.open(tempFile, "w");
|
||||||
|
|
||||||
|
const str = "hello world";
|
||||||
|
await fileHandle.writeFile(str);
|
||||||
|
|
||||||
|
const data = Deno.readFileSync(tempFile);
|
||||||
|
await Deno.remove(tempFile);
|
||||||
|
await fileHandle.close();
|
||||||
|
|
||||||
|
assertEquals(decoder.decode(data), "hello world");
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue