2018-09-27 00:56:39 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
import { Reader, Writer, Closer, ReadResult } from "./io";
|
|
|
|
import * as dispatch from "./dispatch";
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-09-27 00:56:39 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
import { flatbuffers } from "flatbuffers";
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** The Deno abstraction for reading and writing files. */
|
2018-09-27 00:56:39 -04:00
|
|
|
export class File implements Reader, Writer, Closer {
|
2018-10-10 11:59:36 -04:00
|
|
|
constructor(readonly rid: number) {}
|
2018-09-27 00:56:39 -04:00
|
|
|
|
|
|
|
write(p: ArrayBufferView): Promise<number> {
|
2018-10-10 11:59:36 -04:00
|
|
|
return write(this.rid, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
read(p: ArrayBufferView): Promise<ReadResult> {
|
2018-10-10 11:59:36 -04:00
|
|
|
return read(this.rid, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
2018-10-10 11:59:36 -04:00
|
|
|
close(this.rid);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** An instance of `File` for stdin. */
|
2018-09-27 00:56:39 -04:00
|
|
|
export const stdin = new File(0);
|
2018-10-14 16:29:50 -04:00
|
|
|
/** An instance of `File` for stdout. */
|
2018-09-27 00:56:39 -04:00
|
|
|
export const stdout = new File(1);
|
2018-10-14 16:29:50 -04:00
|
|
|
/** An instance of `File` for stderr. */
|
2018-09-27 00:56:39 -04:00
|
|
|
export const stderr = new File(2);
|
|
|
|
|
|
|
|
// TODO This is just a placeholder - not final API.
|
|
|
|
export type OpenMode = "r" | "w" | "w+" | "x";
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** A factory function for creating instances of `File` associated with the
|
|
|
|
* supplied file name.
|
|
|
|
*/
|
2018-09-27 00:56:39 -04:00
|
|
|
export function create(filename: string): Promise<File> {
|
|
|
|
return open(filename, "x");
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Open a file and return an instance of the `File` object.
|
|
|
|
*
|
|
|
|
* import * as deno from "deno";
|
|
|
|
* (async () => {
|
|
|
|
* const file = await deno.open("/foo/bar.txt");
|
|
|
|
* })();
|
|
|
|
*/
|
2018-09-27 00:56:39 -04:00
|
|
|
export async function open(
|
|
|
|
filename: string,
|
|
|
|
mode: OpenMode = "r"
|
|
|
|
): Promise<File> {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Open.startOpen(builder);
|
|
|
|
msg.Open.addFilename(builder, filename_);
|
|
|
|
const inner = msg.Open.endOpen(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, msg.Any.Open, inner);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.OpenRes === baseRes!.innerType());
|
|
|
|
const res = new msg.OpenRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-10-10 11:59:36 -04:00
|
|
|
const rid = res.rid();
|
|
|
|
return new File(rid);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Read from a file ID into an array buffer.
|
|
|
|
*
|
|
|
|
* Resolves with the `ReadResult` for the operation.
|
|
|
|
*/
|
2018-09-27 00:56:39 -04:00
|
|
|
export async function read(
|
2018-10-10 11:59:36 -04:00
|
|
|
rid: number,
|
2018-09-27 00:56:39 -04:00
|
|
|
p: ArrayBufferView
|
|
|
|
): Promise<ReadResult> {
|
|
|
|
const builder = new flatbuffers.Builder();
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Read.startRead(builder);
|
2018-10-10 11:59:36 -04:00
|
|
|
msg.Read.addRid(builder, rid);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.Read.endRead(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, msg.Any.Read, inner, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.ReadRes === baseRes!.innerType());
|
|
|
|
const res = new msg.ReadRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-09-27 00:56:39 -04:00
|
|
|
return { nread: res.nread(), eof: res.eof() };
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Write to the file ID the contents of the array buffer.
|
|
|
|
*
|
|
|
|
* Resolves with the number of bytes written.
|
|
|
|
*/
|
2018-10-10 11:59:36 -04:00
|
|
|
export async function write(rid: number, p: ArrayBufferView): Promise<number> {
|
2018-09-27 00:56:39 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Write.startWrite(builder);
|
2018-10-10 11:59:36 -04:00
|
|
|
msg.Write.addRid(builder, rid);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.Write.endWrite(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, msg.Any.Write, inner, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.WriteRes === baseRes!.innerType());
|
|
|
|
const res = new msg.WriteRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-09-27 00:56:39 -04:00
|
|
|
return res.nbyte();
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-27 00:56:39 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.Close.startClose(builder);
|
2018-10-10 11:59:36 -04:00
|
|
|
msg.Close.addRid(builder, rid);
|
2018-10-03 21:18:23 -04:00
|
|
|
const inner = msg.Close.endClose(builder);
|
|
|
|
dispatch.sendSync(builder, msg.Any.Close, inner);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|