1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 15:49:44 -05:00

feat: Add Deno.FsFile, deprecate Deno.File (#13660)

This commit is contained in:
Bartek Iwańczuk 2022-02-15 13:59:04 +01:00 committed by GitHub
parent bdc8006a36
commit 9b5e336c3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 16 deletions

View file

@ -795,7 +795,7 @@ declare namespace Deno {
},
): IterableIterator<Uint8Array>;
/** Synchronously open a file and return an instance of `Deno.File`. The
/** Synchronously open a file and return an instance of `Deno.FsFile`. The
* file does not need to previously exist if using the `create` or `createNew`
* open options. It is the callers responsibility to close the file when finished
* with it.
@ -808,9 +808,9 @@ declare namespace Deno {
*
* Requires `allow-read` and/or `allow-write` permissions depending on options.
*/
export function openSync(path: string | URL, options?: OpenOptions): File;
export function openSync(path: string | URL, options?: OpenOptions): FsFile;
/** Open a file and resolve to an instance of `Deno.File`. The
/** Open a file and resolve to an instance of `Deno.FsFile`. The
* file does not need to previously exist if using the `create` or `createNew`
* open options. It is the callers responsibility to close the file when finished
* with it.
@ -826,10 +826,10 @@ declare namespace Deno {
export function open(
path: string | URL,
options?: OpenOptions,
): Promise<File>;
): Promise<FsFile>;
/** Creates a file if none exists or truncates an existing file and returns
* an instance of `Deno.File`.
* an instance of `Deno.FsFile`.
*
* ```ts
* const file = Deno.createSync("/foo/bar.txt");
@ -837,10 +837,10 @@ declare namespace Deno {
*
* Requires `allow-read` and `allow-write` permissions.
*/
export function createSync(path: string | URL): File;
export function createSync(path: string | URL): FsFile;
/** Creates a file if none exists or truncates an existing file and resolves to
* an instance of `Deno.File`.
* an instance of `Deno.FsFile`.
*
* ```ts
* const file = await Deno.create("/foo/bar.txt");
@ -848,7 +848,7 @@ declare namespace Deno {
*
* Requires `allow-read` and `allow-write` permissions.
*/
export function create(path: string | URL): Promise<File>;
export function create(path: string | URL): Promise<FsFile>;
/** Synchronously read from a resource ID (`rid`) into an array buffer (`buffer`).
*
@ -1070,6 +1070,35 @@ declare namespace Deno {
export function close(rid: number): void;
/** The Deno abstraction for reading and writing files. */
export class FsFile
implements
Reader,
ReaderSync,
Writer,
WriterSync,
Seeker,
SeekerSync,
Closer {
readonly rid: number;
constructor(rid: number);
write(p: Uint8Array): Promise<number>;
writeSync(p: Uint8Array): number;
truncate(len?: number): Promise<void>;
truncateSync(len?: number): void;
read(p: Uint8Array): Promise<number | null>;
readSync(p: Uint8Array): number | null;
seek(offset: number, whence: SeekMode): Promise<number>;
seekSync(offset: number, whence: SeekMode): number;
stat(): Promise<FileInfo>;
statSync(): FileInfo;
close(): void;
}
/**
* @deprecated Use `Deno.FsFile` instead. `Deno.File` will be removed in Deno 2.0.
*
* The Deno abstraction for reading and writing files.
*/
export class File
implements
Reader,

View file

@ -41,7 +41,7 @@
{ path: pathFromURL(path), options, mode },
);
return new File(rid);
return new FsFile(rid);
}
async function open(
@ -55,7 +55,7 @@
{ path: pathFromURL(path), options, mode },
);
return new File(rid);
return new FsFile(rid);
}
function createSync(path) {
@ -76,7 +76,7 @@
});
}
class File {
class FsFile {
#rid = 0;
#readable;
@ -272,7 +272,8 @@
stdin,
stdout,
stderr,
File,
File: FsFile,
FsFile,
create,
createSync,
open,

View file

@ -3,7 +3,7 @@
((window) => {
const core = window.Deno.core;
const { File } = window.__bootstrap.files;
const { FsFile } = window.__bootstrap.files;
const { readAll } = window.__bootstrap.io;
const { assert, pathFromURL } = window.__bootstrap.util;
const {
@ -46,15 +46,15 @@
this.pid = res.pid;
if (res.stdinRid && res.stdinRid > 0) {
this.stdin = new File(res.stdinRid);
this.stdin = new FsFile(res.stdinRid);
}
if (res.stdoutRid && res.stdoutRid > 0) {
this.stdout = new File(res.stdoutRid);
this.stdout = new FsFile(res.stdoutRid);
}
if (res.stderrRid && res.stderrRid > 0) {
this.stderr = new File(res.stderrRid);
this.stderr = new FsFile(res.stderrRid);
}
}

View file

@ -76,6 +76,7 @@
write: __bootstrap.io.write,
writeSync: __bootstrap.io.writeSync,
File: __bootstrap.files.File,
FsFile: __bootstrap.files.FsFile,
open: __bootstrap.files.open,
openSync: __bootstrap.files.openSync,
create: __bootstrap.files.create,