From 8077a8004650d9a177f61383d53490beda0cd63c Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 14 Nov 2023 18:39:23 -0500 Subject: [PATCH] Better. --- src/path.ts | 87 +++++++---------------------------------------------- 1 file changed, 11 insertions(+), 76 deletions(-) diff --git a/src/path.ts b/src/path.ts index c4c2da9..da054ae 100644 --- a/src/path.ts +++ b/src/path.ts @@ -865,12 +865,12 @@ export class PathRef { /** Creates a new file or opens the existing one. */ create(): Promise { return Deno.create(this.#path) - .then((file) => new FsFileWrapper(file)); + .then((file) => createFsFileWrapper(file)); } /** Synchronously creates a new file or opens the existing one. */ createSync(): FsFileWrapper { - return new FsFileWrapper(Deno.createSync(this.#path)); + return createFsFileWrapper(Deno.createSync(this.#path)); } /** Creates a file throwing if a file previously existed. */ @@ -894,12 +894,12 @@ export class PathRef { /** Opens a file. */ open(options?: Deno.OpenOptions): Promise { return Deno.open(this.#path, options) - .then((file) => new FsFileWrapper(file)); + .then((file) => createFsFileWrapper(file)); } /** Opens a file synchronously. */ openSync(options?: Deno.OpenOptions): FsFileWrapper { - return new FsFileWrapper(Deno.openSync(this.#path, options)); + return createFsFileWrapper(Deno.openSync(this.#path, options)); } /** Removes the file or directory from the file system. */ @@ -1124,18 +1124,12 @@ function createSymlinkSync(opts: CreateSymlinkOpts) { ); } -export class FsFileWrapper implements Deno.FsFile { - #file: Deno.FsFile; - - constructor(file: Deno.FsFile) { - this.#file = file; - } - - /** Gets the inner `Deno.FsFile` that this wraps. */ - get inner(): Deno.FsFile { - return this.#file; - } +function createFsFileWrapper(file: Deno.FsFile): FsFileWrapper { + Object.setPrototypeOf(file, FsFileWrapper.prototype); + return file as FsFileWrapper; +} +export class FsFileWrapper extends Deno.FsFile { /** Writes the provided text to this file. */ writeText(text: string): Promise { return this.writeBytes(new TextEncoder().encode(text)); @@ -1148,74 +1142,15 @@ export class FsFileWrapper implements Deno.FsFile { /** Writes the provided bytes to the file. */ async writeBytes(bytes: Uint8Array): Promise { - await writeAll(this.#file, bytes); + await writeAll(this, bytes); return this; } /** Synchronously writes the provided bytes to the file. */ writeBytesSync(bytes: Uint8Array): this { - writeAllSync(this.#file, bytes); + writeAllSync(this, bytes); return this; } - - // below is Deno.FsFile implementation... could probably be something - // done in the constructor instead. - - get rid(): number { - return this.#file.rid; - } - - get readable(): ReadableStream { - return this.#file.readable; - } - - get writable(): WritableStream { - return this.#file.writable; - } - - write(p: Uint8Array): Promise { - return this.#file.write(p); - } - - writeSync(p: Uint8Array): number { - return this.#file.writeSync(p); - } - - truncate(len?: number | undefined): Promise { - return this.#file.truncate(len); - } - - truncateSync(len?: number | undefined): void { - return this.#file.truncateSync(len); - } - - read(p: Uint8Array): Promise { - return this.#file.read(p); - } - - readSync(p: Uint8Array): number | null { - return this.#file.readSync(p); - } - - seek(offset: number, whence: Deno.SeekMode): Promise { - return this.#file.seek(offset, whence); - } - - seekSync(offset: number, whence: Deno.SeekMode): number { - return this.#file.seekSync(offset, whence); - } - - stat(): Promise { - return this.#file.stat(); - } - - statSync(): Deno.FileInfo { - return this.#file.statSync(); - } - - close(): void { - return this.#file.close(); - } } async function notFoundToUndefined(action: () => Promise) {