Skip to content

Commit 180037b

Browse files
authored
feat: extend Deno.FsFile from FsFileWrapper (#190)
1 parent 31a8812 commit 180037b

File tree

1 file changed

+11
-76
lines changed

1 file changed

+11
-76
lines changed

src/path.ts

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -865,12 +865,12 @@ export class PathRef {
865865
/** Creates a new file or opens the existing one. */
866866
create(): Promise<FsFileWrapper> {
867867
return Deno.create(this.#path)
868-
.then((file) => new FsFileWrapper(file));
868+
.then((file) => createFsFileWrapper(file));
869869
}
870870

871871
/** Synchronously creates a new file or opens the existing one. */
872872
createSync(): FsFileWrapper {
873-
return new FsFileWrapper(Deno.createSync(this.#path));
873+
return createFsFileWrapper(Deno.createSync(this.#path));
874874
}
875875

876876
/** Creates a file throwing if a file previously existed. */
@@ -894,12 +894,12 @@ export class PathRef {
894894
/** Opens a file. */
895895
open(options?: Deno.OpenOptions): Promise<FsFileWrapper> {
896896
return Deno.open(this.#path, options)
897-
.then((file) => new FsFileWrapper(file));
897+
.then((file) => createFsFileWrapper(file));
898898
}
899899

900900
/** Opens a file synchronously. */
901901
openSync(options?: Deno.OpenOptions): FsFileWrapper {
902-
return new FsFileWrapper(Deno.openSync(this.#path, options));
902+
return createFsFileWrapper(Deno.openSync(this.#path, options));
903903
}
904904

905905
/** Removes the file or directory from the file system. */
@@ -1124,18 +1124,12 @@ function createSymlinkSync(opts: CreateSymlinkOpts) {
11241124
);
11251125
}
11261126

1127-
export class FsFileWrapper implements Deno.FsFile {
1128-
#file: Deno.FsFile;
1129-
1130-
constructor(file: Deno.FsFile) {
1131-
this.#file = file;
1132-
}
1133-
1134-
/** Gets the inner `Deno.FsFile` that this wraps. */
1135-
get inner(): Deno.FsFile {
1136-
return this.#file;
1137-
}
1127+
function createFsFileWrapper(file: Deno.FsFile): FsFileWrapper {
1128+
Object.setPrototypeOf(file, FsFileWrapper.prototype);
1129+
return file as FsFileWrapper;
1130+
}
11381131

1132+
export class FsFileWrapper extends Deno.FsFile {
11391133
/** Writes the provided text to this file. */
11401134
writeText(text: string): Promise<this> {
11411135
return this.writeBytes(new TextEncoder().encode(text));
@@ -1148,74 +1142,15 @@ export class FsFileWrapper implements Deno.FsFile {
11481142

11491143
/** Writes the provided bytes to the file. */
11501144
async writeBytes(bytes: Uint8Array): Promise<this> {
1151-
await writeAll(this.#file, bytes);
1145+
await writeAll(this, bytes);
11521146
return this;
11531147
}
11541148

11551149
/** Synchronously writes the provided bytes to the file. */
11561150
writeBytesSync(bytes: Uint8Array): this {
1157-
writeAllSync(this.#file, bytes);
1151+
writeAllSync(this, bytes);
11581152
return this;
11591153
}
1160-
1161-
// below is Deno.FsFile implementation... could probably be something
1162-
// done in the constructor instead.
1163-
1164-
get rid(): number {
1165-
return this.#file.rid;
1166-
}
1167-
1168-
get readable(): ReadableStream<Uint8Array> {
1169-
return this.#file.readable;
1170-
}
1171-
1172-
get writable(): WritableStream<Uint8Array> {
1173-
return this.#file.writable;
1174-
}
1175-
1176-
write(p: Uint8Array): Promise<number> {
1177-
return this.#file.write(p);
1178-
}
1179-
1180-
writeSync(p: Uint8Array): number {
1181-
return this.#file.writeSync(p);
1182-
}
1183-
1184-
truncate(len?: number | undefined): Promise<void> {
1185-
return this.#file.truncate(len);
1186-
}
1187-
1188-
truncateSync(len?: number | undefined): void {
1189-
return this.#file.truncateSync(len);
1190-
}
1191-
1192-
read(p: Uint8Array): Promise<number | null> {
1193-
return this.#file.read(p);
1194-
}
1195-
1196-
readSync(p: Uint8Array): number | null {
1197-
return this.#file.readSync(p);
1198-
}
1199-
1200-
seek(offset: number, whence: Deno.SeekMode): Promise<number> {
1201-
return this.#file.seek(offset, whence);
1202-
}
1203-
1204-
seekSync(offset: number, whence: Deno.SeekMode): number {
1205-
return this.#file.seekSync(offset, whence);
1206-
}
1207-
1208-
stat(): Promise<Deno.FileInfo> {
1209-
return this.#file.stat();
1210-
}
1211-
1212-
statSync(): Deno.FileInfo {
1213-
return this.#file.statSync();
1214-
}
1215-
1216-
close(): void {
1217-
return this.#file.close();
1218-
}
12191154
}
12201155

12211156
async function notFoundToUndefined<T>(action: () => Promise<T>) {

0 commit comments

Comments
 (0)