2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 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";
|
|
|
|
import * as dispatch from "./dispatch.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";
|
2019-05-03 00:06:43 -04:00
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
/** Open a file and return an instance of the `File` object
|
|
|
|
* synchronously.
|
2018-10-14 16:29:50 -04:00
|
|
|
*
|
2019-03-27 23:29:36 -04:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt");
|
2018-10-14 16:29:50 -04:00
|
|
|
*/
|
2019-03-27 23:29:36 -04:00
|
|
|
export function openSync(filename: string, mode: OpenMode = "r"): File {
|
2019-08-26 08:50:21 -04:00
|
|
|
const rid = sendSyncJson(dispatch.OP_OPEN, { filename, mode });
|
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Open a file and return an instance of the `File` object.
|
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const file = await Deno.open("/foo/bar.txt");
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
|
|
|
export async function open(
|
|
|
|
filename: string,
|
|
|
|
mode: OpenMode = "r"
|
|
|
|
): Promise<File> {
|
2019-08-26 08:50:21 -04:00
|
|
|
const rid = await sendAsyncJson(dispatch.OP_OPEN, { filename, mode });
|
|
|
|
return new File(rid);
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Read synchronously from a file ID into an array buffer.
|
|
|
|
*
|
2019-07-06 10:16:03 -04:00
|
|
|
* Return `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-08-26 10:58:44 -04:00
|
|
|
const nread = sendSyncMinimal(dispatch.OP_READ, rid, p);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/** Read from a file ID into an array buffer.
|
|
|
|
*
|
2019-07-06 10:16:03 -04:00
|
|
|
* Resolves with 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-08-26 10:58:44 -04:00
|
|
|
const nread = await sendAsyncMinimal(dispatch.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
|
|
|
}
|
|
|
|
|
|
|
|
/** Write synchronously to the file ID the contents of the array buffer.
|
2019-02-18 18:26:41 -05:00
|
|
|
*
|
2019-03-27 23:29:36 -04:00
|
|
|
* Resolves with the number of bytes written.
|
|
|
|
*
|
|
|
|
* const encoder = new TextEncoder();
|
|
|
|
* const data = encoder.encode("Hello world\n");
|
|
|
|
* const file = Deno.openSync("/foo/bar.txt");
|
|
|
|
* 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 {
|
2019-08-26 10:58:44 -04:00
|
|
|
const result = sendSyncMinimal(dispatch.OP_WRITE, rid, p);
|
|
|
|
if (result < 0) {
|
|
|
|
throw new Error("write error");
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
2019-03-27 23:29:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Write to the file ID the contents of the array buffer.
|
|
|
|
*
|
|
|
|
* Resolves with the number of bytes written.
|
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const encoder = new TextEncoder();
|
|
|
|
* const data = encoder.encode("Hello world\n");
|
|
|
|
* const file = await Deno.open("/foo/bar.txt");
|
|
|
|
* await Deno.write(file.rid, data);
|
2019-03-27 23:29:36 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
export async function write(rid: number, p: Uint8Array): Promise<number> {
|
2019-09-07 12:27:18 -04:00
|
|
|
const result = await sendAsyncMinimal(dispatch.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
|
|
|
}
|
|
|
|
|
|
|
|
/** Seek a file ID synchronously to the given offset under mode given by `whence`.
|
|
|
|
*
|
2019-04-03 08:53:54 -04:00
|
|
|
* const file = Deno.openSync("/foo/bar.txt");
|
|
|
|
* Deno.seekSync(file.rid, 0, 0);
|
2019-03-27 23:29:36 -04:00
|
|
|
*/
|
|
|
|
export function seekSync(rid: number, offset: number, whence: SeekMode): void {
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSyncJson(dispatch.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`.
|
|
|
|
*
|
2019-10-29 17:11:41 -04:00
|
|
|
* const file = await Deno.open("/foo/bar.txt");
|
|
|
|
* 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
|
|
|
|
): Promise<void> {
|
2019-08-26 08:50:21 -04:00
|
|
|
await sendAsyncJson(dispatch.OP_SEEK, { rid, offset, whence });
|
2019-02-18 18:26:41 -05:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Close the file ID. */
|
2018-10-10 11:59:36 -04:00
|
|
|
export function close(rid: number): void {
|
2019-08-26 08:50:21 -04:00
|
|
|
sendSyncJson(dispatch.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);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
seek(offset: number, whence: SeekMode): Promise<void> {
|
|
|
|
return seek(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2019-03-27 23:29:36 -04:00
|
|
|
seekSync(offset: number, whence: SeekMode): void {
|
|
|
|
return seekSync(this.rid, offset, whence);
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** An instance of `File` for stdin. */
|
|
|
|
export const stdin = new File(0);
|
|
|
|
/** An instance of `File` for stdout. */
|
|
|
|
export const stdout = new File(1);
|
|
|
|
/** An instance of `File` for stderr. */
|
|
|
|
export const stderr = new File(2);
|
|
|
|
|
|
|
|
export type OpenMode =
|
|
|
|
/** Read-only. Default. Starts at beginning of file. */
|
|
|
|
| "r"
|
|
|
|
/** Read-write. Start at beginning of file. */
|
|
|
|
| "r+"
|
|
|
|
/** 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.
|
|
|
|
*/
|
|
|
|
| "w+"
|
|
|
|
/** 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. */
|
|
|
|
| "a+"
|
|
|
|
/** Write-only. Exclusive create - creates new file only if one doesn't exist
|
|
|
|
* already.
|
|
|
|
*/
|
|
|
|
| "x"
|
|
|
|
/** Read-write. Behaves like `x` and allows to read from file. */
|
|
|
|
| "x+";
|
|
|
|
|
|
|
|
/** A factory function for creating instances of `File` associated with the
|
|
|
|
* supplied file name.
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
export function create(filename: string): Promise<File> {
|
|
|
|
return open(filename, "w+");
|
|
|
|
}
|