2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-27 23:29:36 -04:00
|
|
|
import {
|
2019-07-06 10:16:03 -04:00
|
|
|
EOF,
|
2019-03-27 23:29:36 -04:00
|
|
|
Reader,
|
|
|
|
Writer,
|
|
|
|
Seeker,
|
|
|
|
Closer,
|
|
|
|
SeekMode,
|
|
|
|
SyncReader,
|
|
|
|
SyncWriter,
|
|
|
|
SyncSeeker
|
2019-09-02 17:07:11 -04:00
|
|
|
} from "./io.ts";
|
|
|
|
import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts";
|
2019-08-26 08:50:21 -04:00
|
|
|
import {
|
|
|
|
sendSync as sendSyncJson,
|
|
|
|
sendAsync as sendAsyncJson
|
2019-09-02 17:07:11 -04:00
|
|
|
} from "./dispatch_json.ts";
|
2020-02-25 09:14:27 -05:00
|
|
|
import { OPS_CACHE } from "./runtime.ts";
|
|
|
|
|
|
|
|
// This is done because read/write are extremely performance sensitive.
|
|
|
|
let OP_READ = -1;
|
|
|
|
let OP_WRITE = -1;
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Synchronously open a file and return an instance of the `File` object.
|
2018-10-14 16:29:50 -04:00
|
|
|
*
|
2020-01-21 04:49:42 -05:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt", { read: true, write: true });
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions depending on mode.
|
2020-01-21 04:49:42 -05:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export function openSync(path: string, mode?: OpenOptions): File;
|
2020-03-02 10:19:42 -05:00
|
|
|
|
|
|
|
/** Synchronously open a file and return an instance of the `File` object.
|
2020-01-21 04:49:42 -05:00
|
|
|
*
|
|
|
|
* const file = Deno.openSync("/foo/bar.txt", "r");
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions depending on mode.
|
2018-10-14 16:29:50 -04:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export function openSync(path: string, mode?: OpenMode): File;
|
2020-01-21 04:49:42 -05:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/**@internal*/
|
2020-01-21 04:49:42 -05:00
|
|
|
export function openSync(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2020-01-21 04:49:42 -05:00
|
|
|
modeOrOptions: OpenOptions | OpenMode = "r"
|
|
|
|
): File {
|
|
|
|
let mode = null;
|
|
|
|
let options = null;
|
|
|
|
|
|
|
|
if (typeof modeOrOptions === "string") {
|
|
|
|
mode = modeOrOptions;
|
|
|
|
} else {
|
|
|
|
checkOpenOptions(modeOrOptions);
|
|
|
|
options = modeOrOptions;
|
|
|
|
}
|
|
|
|
|
2020-03-06 11:29:23 -05:00
|
|
|
const rid = sendSyncJson("op_open", { path, options, mode });
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Open a file and resolve to an instance of the `File` object.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2020-01-21 04:49:42 -05:00
|
|
|
* const file = await Deno.open("/foo/bar.txt", { read: true, write: true });
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions depending on mode.
|
2020-01-21 04:49:42 -05:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export async function open(path: string, options?: OpenOptions): Promise<File>;
|
2020-01-21 04:49:42 -05:00
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Open a file and resolves to an instance of `Deno.File`.
|
2020-01-21 04:49:42 -05:00
|
|
|
*
|
|
|
|
* const file = await Deno.open("/foo/bar.txt, "w+");
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions depending on mode.
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export async function open(path: string, mode?: OpenMode): Promise<File>;
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
/**@internal*/
|
2019-03-27 23:29:36 -04:00
|
|
|
export async function open(
|
2020-03-06 11:29:23 -05:00
|
|
|
path: string,
|
2020-01-21 04:49:42 -05:00
|
|
|
modeOrOptions: OpenOptions | OpenMode = "r"
|
2019-03-27 23:29:36 -04:00
|
|
|
): Promise<File> {
|
2020-01-21 04:49:42 -05:00
|
|
|
let mode = null;
|
|
|
|
let options = null;
|
|
|
|
|
|
|
|
if (typeof modeOrOptions === "string") {
|
|
|
|
mode = modeOrOptions;
|
|
|
|
} else {
|
|
|
|
checkOpenOptions(modeOrOptions);
|
|
|
|
options = modeOrOptions;
|
|
|
|
}
|
|
|
|
|
2020-02-25 09:14:27 -05:00
|
|
|
const rid = await sendAsyncJson("op_open", {
|
2020-03-06 11:29:23 -05:00
|
|
|
path,
|
2020-01-21 04:49:42 -05:00
|
|
|
options,
|
|
|
|
mode
|
|
|
|
});
|
2019-08-26 08:50:21 -04:00
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-01-08 17:07:03 -05:00
|
|
|
/** Creates a file if none exists or truncates an existing file and returns
|
2020-03-02 10:19:42 -05:00
|
|
|
* an instance of `Deno.File`.
|
2020-01-08 17:07:03 -05:00
|
|
|
*
|
|
|
|
* const file = Deno.createSync("/foo/bar.txt");
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions.
|
2020-01-08 17:07:03 -05:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export function createSync(path: string): File {
|
|
|
|
return openSync(path, "w+");
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Creates a file if none exists or truncates an existing file and resolves to
|
|
|
|
* an instance of `Deno.File`.
|
2020-01-08 17:07:03 -05:00
|
|
|
*
|
|
|
|
* const file = await Deno.create("/foo/bar.txt");
|
2020-03-02 10:19:42 -05:00
|
|
|
*
|
|
|
|
* Requires `allow-read` and `allow-write` permissions.
|
2020-01-08 17:07:03 -05:00
|
|
|
*/
|
2020-03-06 11:29:23 -05:00
|
|
|
export function create(path: string): Promise<File> {
|
|
|
|
return open(path, "w+");
|
2020-01-08 17:07:03 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Synchronously read from a file ID into an array buffer.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2020-03-02 10:19:42 -05:00
|
|
|
* Returns `number | EOF` for the operation.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2019-04-03 08:53:54 -04:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt");
|
|
|
|
* const buf = new Uint8Array(100);
|
2019-07-06 10:16:03 -04:00
|
|
|
* const nread = Deno.readSync(file.rid, buf);
|
2019-04-04 05:34:00 -04:00
|
|
|
* const text = new TextDecoder().decode(buf);
|
2018-10-14 16:29:50 -04:00
|
|
|
*/
|
2019-07-06 10:16:03 -04:00
|
|
|
export function readSync(rid: number, p: Uint8Array): number | EOF {
|
2019-12-28 08:48:36 -05:00
|
|
|
if (p.length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-25 09:14:27 -05:00
|
|
|
if (OP_READ < 0) {
|
|
|
|
OP_READ = OPS_CACHE["op_read"];
|
|
|
|
}
|
|
|
|
const nread = sendSyncMinimal(OP_READ, rid, p);
|
2019-08-26 10:58:44 -04:00
|
|
|
if (nread < 0) {
|
|
|
|
throw new Error("read error");
|
|
|
|
} else if (nread == 0) {
|
|
|
|
return EOF;
|
|
|
|
} else {
|
|
|
|
return nread;
|
|
|
|
}
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Read from a resource ID into an array buffer.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2020-03-02 10:19:42 -05:00
|
|
|
* Resolves to the `number | EOF` for the operation.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const file = await Deno.open("/foo/bar.txt");
|
|
|
|
* const buf = new Uint8Array(100);
|
|
|
|
* const nread = await Deno.read(file.rid, buf);
|
|
|
|
* const text = new TextDecoder().decode(buf);
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
2019-07-06 10:16:03 -04:00
|
|
|
export async function read(rid: number, p: Uint8Array): Promise<number | EOF> {
|
2019-12-28 08:48:36 -05:00
|
|
|
if (p.length == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2020-02-25 09:14:27 -05:00
|
|
|
if (OP_READ < 0) {
|
|
|
|
OP_READ = OPS_CACHE["op_read"];
|
|
|
|
}
|
|
|
|
const nread = await sendAsyncMinimal(OP_READ, rid, p);
|
2019-05-03 00:06:43 -04:00
|
|
|
if (nread < 0) {
|
|
|
|
throw new Error("read error");
|
|
|
|
} else if (nread == 0) {
|
2019-07-06 10:16:03 -04:00
|
|
|
return EOF;
|
2019-05-03 00:06:43 -04:00
|
|
|
} else {
|
2019-07-06 10:16:03 -04:00
|
|
|
return nread;
|
2019-05-03 00:06:43 -04:00
|
|
|
}
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Synchronously write to the resource ID the contents of the array buffer.
|
2019-02-18 18:26:41 -05:00
|
|
|
*
|
2020-03-02 10:19:42 -05:00
|
|
|
* Resolves to the number of bytes written.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
|
|
|
* const encoder = new TextEncoder();
|
|
|
|
* const data = encoder.encode("Hello world\n");
|
2020-02-29 14:37:32 -05:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt", {create: true, write: true});
|
2019-03-27 23:29:36 -04:00
|
|
|
* Deno.writeSync(file.rid, data);
|
2019-02-18 18:26:41 -05:00
|
|
|
*/
|
2019-03-27 23:29:36 -04:00
|
|
|
export function writeSync(rid: number, p: Uint8Array): number {
|
2020-02-25 09:14:27 -05:00
|
|
|
if (OP_WRITE < 0) {
|
|
|
|
OP_WRITE = OPS_CACHE["op_write"];
|
|
|
|
}
|
|
|
|
const result = sendSyncMinimal(OP_WRITE, rid, p);
|
2019-08-26 10:58:44 -04:00
|
|
|
if (result < 0) {
|
|
|
|
throw new Error("write error");
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Write to the resource ID the contents of the array buffer.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2020-03-02 10:19:42 -05:00
|
|
|
* Resolves to the number of bytes written.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const encoder = new TextEncoder();
|
|
|
|
* const data = encoder.encode("Hello world\n");
|
2020-02-29 14:37:32 -05:00
|
|
|
* const file = await Deno.open("/foo/bar.txt", {create: true, write: true});
|
2019-10-29 17:11:41 -04:00
|
|
|
* await Deno.write(file.rid, data);
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
|
|
|
export async function write(rid: number, p: Uint8Array): Promise<number> {
|
2020-02-25 09:14:27 -05:00
|
|
|
if (OP_WRITE < 0) {
|
|
|
|
OP_WRITE = OPS_CACHE["op_write"];
|
|
|
|
}
|
|
|
|
const result = await sendAsyncMinimal(OP_WRITE, rid, p);
|
2019-05-03 00:06:43 -04:00
|
|
|
if (result < 0) {
|
|
|
|
throw new Error("write error");
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Synchronously seek a file ID to the given offset under mode given by `whence`.
|
2020-03-02 11:44:46 -05:00
|
|
|
*
|
|
|
|
* Returns the number of cursor position.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2019-04-03 08:53:54 -04:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt");
|
2020-03-02 11:44:46 -05:00
|
|
|
* const position = Deno.seekSync(file.rid, 0, 0);
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
2020-03-02 11:44:46 -05:00
|
|
|
export function seekSync(
|
|
|
|
rid: number,
|
|
|
|
offset: number,
|
|
|
|
whence: SeekMode
|
|
|
|
): number {
|
|
|
|
return sendSyncJson("op_seek", { rid, offset, whence });
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Seek a file ID to the given offset under mode given by `whence`.
|
2020-03-02 11:44:46 -05:00
|
|
|
*
|
|
|
|
* Resolves with the number of cursor position.
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const file = await Deno.open("/foo/bar.txt");
|
2020-03-02 11:44:46 -05:00
|
|
|
* const position = await Deno.seek(file.rid, 0, 0);
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
|
|
|
export async function seek(
|
|
|
|
rid: number,
|
|
|
|
offset: number,
|
|
|
|
whence: SeekMode
|
2020-03-02 11:44:46 -05:00
|
|
|
): Promise<number> {
|
|
|
|
return await sendAsyncJson("op_seek", { rid, offset, whence });
|
2019-02-18 18:26:41 -05:00
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Close the given resource ID. */
|
2018-10-10 11:59:36 -04:00
|
|
|
export function close(rid: number): void {
|
2020-02-25 09:14:27 -05:00
|
|
|
sendSyncJson("op_close", { rid });
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
2019-03-09 12:30:38 -05:00
|
|
|
|
|
|
|
/** The Deno abstraction for reading and writing files. */
|
2019-03-27 23:29:36 -04:00
|
|
|
export class File
|
|
|
|
implements
|
|
|
|
Reader,
|
|
|
|
SyncReader,
|
|
|
|
Writer,
|
|
|
|
SyncWriter,
|
|
|
|
Seeker,
|
|
|
|
SyncSeeker,
|
|
|
|
Closer {
|
2019-03-09 12:30:38 -05:00
|
|
|
constructor(readonly rid: number) {}
|
|
|
|
|
|
|
|
write(p: Uint8Array): Promise<number> {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
writeSync(p: Uint8Array): number {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
read(p: Uint8Array): Promise<number | EOF> {
|
2019-03-09 12:30:38 -05:00
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
readSync(p: Uint8Array): number | EOF {
|
2019-03-27 23:29:36 -04:00
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
2020-03-02 11:44:46 -05:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<number> {
|
2019-03-09 12:30:38 -05:00
|
|
|
return seek(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2020-03-02 11:44:46 -05:00
|
|
|
seekSync(offset: number, whence: SeekMode): number {
|
2019-03-27 23:29:36 -04:00
|
|
|
return seekSync(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** An instance of `Deno.File` for `stdin`. */
|
2019-03-09 12:30:38 -05:00
|
|
|
export const stdin = new File(0);
|
2020-03-02 10:19:42 -05:00
|
|
|
/** An instance of `Deno.File` for `stdout`. */
|
2019-03-09 12:30:38 -05:00
|
|
|
export const stdout = new File(1);
|
2020-03-02 10:19:42 -05:00
|
|
|
/** An instance of `Deno.File` for `stderr`. */
|
2019-03-09 12:30:38 -05:00
|
|
|
export const stderr = new File(2);
|
|
|
|
|
2020-01-21 04:49:42 -05:00
|
|
|
export interface OpenOptions {
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Sets the option for read access. This option, when `true`, means that the
|
|
|
|
* file should be read-able if opened. */
|
2020-01-21 04:49:42 -05:00
|
|
|
read?: boolean;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Sets the option for write access. This option, when `true`, means that
|
|
|
|
* the file should be write-able if opened. If the file already exists,
|
|
|
|
* any write calls on it will overwrite its contents, by default without
|
|
|
|
* truncating it. */
|
2020-01-21 04:49:42 -05:00
|
|
|
write?: boolean;
|
2020-03-02 10:19:42 -05:00
|
|
|
/**Sets the option for the append mode. This option, when `true`, means that
|
|
|
|
* writes will append to a file instead of overwriting previous contents.
|
|
|
|
* Note that setting `{ write: true, append: true }` has the same effect as
|
|
|
|
* setting only `{ append: true }`. */
|
2020-01-21 04:49:42 -05:00
|
|
|
append?: boolean;
|
2020-03-02 10:19:42 -05:00
|
|
|
/** Sets the option for truncating a previous file. If a file is
|
|
|
|
* successfully opened with this option set it will truncate the file to `0`
|
|
|
|
* length if it already exists. The file must be opened with write access
|
|
|
|
* for truncate to work. */
|
|
|
|
truncate?: boolean;
|
|
|
|
/** Sets the option to allow creating a new file, if one doesn't already
|
|
|
|
* exist at the specified path. Requires write or append access to be
|
|
|
|
* used. */
|
|
|
|
create?: boolean;
|
|
|
|
/** Defaults to `false`. If set to `true`, no file, directory, or symlink is
|
|
|
|
* allowed to exist at the target location. Requires write or append
|
|
|
|
* access to be used. When createNew is set to `true`, create and truncate
|
|
|
|
* are ignored. */
|
2020-01-21 04:49:42 -05:00
|
|
|
createNew?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:19:42 -05:00
|
|
|
/** A set of string literals which specify the open mode of a file.
|
|
|
|
*
|
|
|
|
* |Value |Description |
|
|
|
|
* |------|--------------------------------------------------------------------------------------------------|
|
|
|
|
* |`"r"` |Read-only. Default. Starts at beginning of file. |
|
|
|
|
* |`"r+"`|Read-write. Start at beginning of file. |
|
|
|
|
* |`"w"` |Write-only. Opens and truncates existing file or creates new one for writing only. |
|
|
|
|
* |`"w+"`|Read-write. Opens and truncates existing file or creates new one for writing and reading. |
|
|
|
|
* |`"a"` |Write-only. Opens existing file or creates new one. Each write appends content to the end of file.|
|
|
|
|
* |`"a+"`|Read-write. Behaves like `"a"` and allows to read from file. |
|
|
|
|
* |`"x"` |Write-only. Exclusive create - creates new file only if one doesn't exist already. |
|
|
|
|
* |`"x+"`|Read-write. Behaves like `x` and allows reading from file. |
|
|
|
|
*/
|
|
|
|
export type OpenMode = "r" | "r+" | "w" | "w+" | "a" | "a+" | "x" | "x+";
|
2020-01-21 04:49:42 -05:00
|
|
|
|
|
|
|
/** Check if OpenOptions is set to valid combination of options.
|
|
|
|
* @returns Tuple representing if openMode is valid and error message if it's not
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
function checkOpenOptions(options: OpenOptions): void {
|
|
|
|
if (Object.values(options).filter(val => val === true).length === 0) {
|
|
|
|
throw new Error("OpenOptions requires at least one option to be true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.truncate && !options.write) {
|
|
|
|
throw new Error("'truncate' option requires 'write' option");
|
|
|
|
}
|
|
|
|
|
|
|
|
const createOrCreateNewWithoutWriteOrAppend =
|
|
|
|
(options.create || options.createNew) && !(options.write || options.append);
|
|
|
|
|
|
|
|
if (createOrCreateNewWithoutWriteOrAppend) {
|
|
|
|
throw new Error(
|
|
|
|
"'create' or 'createNew' options require 'write' or 'append' option"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|