summaryrefslogtreecommitdiff
path: root/js/files.ts
diff options
context:
space:
mode:
Diffstat (limited to 'js/files.ts')
-rw-r--r--js/files.ts180
1 files changed, 156 insertions, 24 deletions
diff --git a/js/files.ts b/js/files.ts
index 9ea378735..9da9fbe0a 100644
--- a/js/files.ts
+++ b/js/files.ts
@@ -1,20 +1,24 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { Reader, Writer, Seeker, Closer, ReadResult, SeekMode } from "./io";
+import {
+ Reader,
+ Writer,
+ Seeker,
+ Closer,
+ ReadResult,
+ SeekMode,
+ SyncReader,
+ SyncWriter,
+ SyncSeeker
+} from "./io";
import * as dispatch from "./dispatch";
import * as msg from "gen/msg_generated";
import { assert } from "./util";
import * as flatbuffers from "./flatbuffers";
-/** Open a file and return an instance of the `File` object.
- *
- * (async () => {
- * const file = await Deno.open("/foo/bar.txt");
- * })();
- */
-export async function open(
+function reqOpen(
filename: string,
- mode: OpenMode = "r"
-): Promise<File> {
+ mode: OpenMode
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
const mode_ = builder.createString(mode);
@@ -22,7 +26,10 @@ export async function open(
msg.Open.addFilename(builder, filename_);
msg.Open.addMode(builder, mode_);
const inner = msg.Open.endOpen(builder);
- const baseRes = await dispatch.sendAsync(builder, msg.Any.Open, inner);
+ return [builder, msg.Any.Open, inner];
+}
+
+function resOpen(baseRes: null | msg.Base): File {
assert(baseRes != null);
assert(msg.Any.OpenRes === baseRes!.innerType());
const res = new msg.OpenRes();
@@ -32,16 +39,40 @@ export async function open(
return new File(rid);
}
-/** Read from a file ID into an array buffer.
+/** Open a file and return an instance of the `File` object
+ * synchronously.
*
- * Resolves with the `ReadResult` for the operation.
+ * const file = Deno.openSync("/foo/bar.txt");
*/
-export async function read(rid: number, p: Uint8Array): Promise<ReadResult> {
+export function openSync(filename: string, mode: OpenMode = "r"): File {
+ return resOpen(dispatch.sendSync(...reqOpen(filename, mode)));
+}
+
+/** Open a file and return an instance of the `File` object.
+ *
+ * (async () => {
+ * const file = await Deno.open("/foo/bar.txt");
+ * })();
+ */
+export async function open(
+ filename: string,
+ mode: OpenMode = "r"
+): Promise<File> {
+ return resOpen(await dispatch.sendAsync(...reqOpen(filename, mode)));
+}
+
+function reqRead(
+ rid: number,
+ p: Uint8Array
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
const builder = flatbuffers.createBuilder();
msg.Read.startRead(builder);
msg.Read.addRid(builder, rid);
const inner = msg.Read.endRead(builder);
- const baseRes = await dispatch.sendAsync(builder, msg.Any.Read, inner, p);
+ return [builder, msg.Any.Read, inner, p];
+}
+
+function resRead(baseRes: null | msg.Base): ReadResult {
assert(baseRes != null);
assert(msg.Any.ReadRes === baseRes!.innerType());
const res = new msg.ReadRes();
@@ -49,16 +80,47 @@ export async function read(rid: number, p: Uint8Array): Promise<ReadResult> {
return { nread: res.nread(), eof: res.eof() };
}
-/** Write to the file ID the contents of the array buffer.
+/** Read synchronously from a file ID into an array buffer.
+ *
+ * Return `ReadResult` for the operation.
+ *
+ * const file = Deno.openSync("/foo/bar.txt");
+ * const buf = new Uint8Array(100);
+ * const { nread, eof } = Deno.readSync(file.rid, buf);
+ * const text = new TextDecoder.decode(buf);
*
- * Resolves with the number of bytes written.
*/
-export async function write(rid: number, p: Uint8Array): Promise<number> {
+export function readSync(rid: number, p: Uint8Array): ReadResult {
+ return resRead(dispatch.sendSync(...reqRead(rid, p)));
+}
+
+/** Read from a file ID into an array buffer.
+ *
+ * Resolves with the `ReadResult` for the operation.
+ *
+ * (async () => {
+ * const file = await Deno.open("/foo/bar.txt");
+ * const buf = new Uint8Array(100);
+ * const { nread, eof } = await Deno.read(file.rid, buf);
+ * const text = new TextDecoder.decode(buf);
+ * })();
+ */
+export async function read(rid: number, p: Uint8Array): Promise<ReadResult> {
+ return resRead(await dispatch.sendAsync(...reqRead(rid, p)));
+}
+
+function reqWrite(
+ rid: number,
+ p: Uint8Array
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, Uint8Array] {
const builder = flatbuffers.createBuilder();
msg.Write.startWrite(builder);
msg.Write.addRid(builder, rid);
const inner = msg.Write.endWrite(builder);
- const baseRes = await dispatch.sendAsync(builder, msg.Any.Write, inner, p);
+ return [builder, msg.Any.Write, inner, p];
+}
+
+function resWrite(baseRes: null | msg.Base): number {
assert(baseRes != null);
assert(msg.Any.WriteRes === baseRes!.innerType());
const res = new msg.WriteRes();
@@ -66,21 +128,71 @@ export async function write(rid: number, p: Uint8Array): Promise<number> {
return res.nbyte();
}
-/** Seek a file ID to the given offset under mode given by `whence`.
+/** Write synchronously to the file ID the contents of the array buffer.
*
+ * 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);
*/
-export async function seek(
+export function writeSync(rid: number, p: Uint8Array): number {
+ return resWrite(dispatch.sendSync(...reqWrite(rid, p)));
+}
+
+/** Write to the file ID the contents of the array buffer.
+ *
+ * Resolves with the number of bytes written.
+ *
+ * (async () => {
+ * 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);
+ * })();
+ *
+ */
+export async function write(rid: number, p: Uint8Array): Promise<number> {
+ return resWrite(await dispatch.sendAsync(...reqWrite(rid, p)));
+}
+
+function reqSeek(
rid: number,
offset: number,
whence: SeekMode
-): Promise<void> {
+): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const builder = flatbuffers.createBuilder();
msg.Seek.startSeek(builder);
msg.Seek.addRid(builder, rid);
msg.Seek.addOffset(builder, offset);
msg.Seek.addWhence(builder, whence);
const inner = msg.Seek.endSeek(builder);
- await dispatch.sendAsync(builder, msg.Any.Seek, inner);
+ return [builder, msg.Any.Seek, inner];
+}
+
+/** Seek a file ID synchronously to the given offset under mode given by `whence`.
+ *
+ * const file = Deno.openSync("/foo/bar.txt");
+ * Deno.seekSync(file.rid, 0, 0);
+ */
+export function seekSync(rid: number, offset: number, whence: SeekMode): void {
+ dispatch.sendSync(...reqSeek(rid, offset, whence));
+}
+
+/** Seek a file ID to the given offset under mode given by `whence`.
+ *
+ * (async () => {
+ * const file = await Deno.open("/foo/bar.txt");
+ * await Deno.seek(file.rid, 0, 0);
+ * })();
+ */
+export async function seek(
+ rid: number,
+ offset: number,
+ whence: SeekMode
+): Promise<void> {
+ await dispatch.sendAsync(...reqSeek(rid, offset, whence));
}
/** Close the file ID. */
@@ -93,21 +205,41 @@ export function close(rid: number): void {
}
/** The Deno abstraction for reading and writing files. */
-export class File implements Reader, Writer, Seeker, Closer {
+export class File
+ implements
+ Reader,
+ SyncReader,
+ Writer,
+ SyncWriter,
+ Seeker,
+ SyncSeeker,
+ Closer {
constructor(readonly rid: number) {}
write(p: Uint8Array): Promise<number> {
return write(this.rid, p);
}
+ writeSync(p: Uint8Array): number {
+ return writeSync(this.rid, p);
+ }
+
read(p: Uint8Array): Promise<ReadResult> {
return read(this.rid, p);
}
+ readSync(p: Uint8Array): ReadResult {
+ return readSync(this.rid, p);
+ }
+
seek(offset: number, whence: SeekMode): Promise<void> {
return seek(this.rid, offset, whence);
}
+ seekSync(offset: number, whence: SeekMode): void {
+ return seekSync(this.rid, offset, whence);
+ }
+
close(): void {
close(this.rid);
}