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";
|
|
|
|
import * as fbs from "gen/msg_generated";
|
|
|
|
import { assert } from "./util";
|
|
|
|
import { flatbuffers } from "flatbuffers";
|
|
|
|
|
|
|
|
export class File implements Reader, Writer, Closer {
|
|
|
|
constructor(readonly fd: number) {}
|
|
|
|
|
|
|
|
write(p: ArrayBufferView): Promise<number> {
|
|
|
|
return write(this.fd, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
read(p: ArrayBufferView): Promise<ReadResult> {
|
|
|
|
return read(this.fd, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
2018-10-03 23:58:29 -04:00
|
|
|
close(this.fd);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const stdin = new File(0);
|
|
|
|
export const stdout = new File(1);
|
|
|
|
export const stderr = new File(2);
|
|
|
|
|
|
|
|
// TODO This is just a placeholder - not final API.
|
|
|
|
export type OpenMode = "r" | "w" | "w+" | "x";
|
|
|
|
|
|
|
|
export function create(filename: string): Promise<File> {
|
|
|
|
return open(filename, "x");
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function open(
|
|
|
|
filename: string,
|
|
|
|
mode: OpenMode = "r"
|
|
|
|
): Promise<File> {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const filename_ = builder.createString(filename);
|
|
|
|
fbs.Open.startOpen(builder);
|
|
|
|
fbs.Open.addFilename(builder, filename_);
|
2018-10-03 21:12:23 -04:00
|
|
|
const inner = fbs.Open.endOpen(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, fbs.Any.Open, inner);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(fbs.Any.OpenRes === baseRes!.innerType());
|
2018-09-27 00:56:39 -04:00
|
|
|
const res = new fbs.OpenRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-10-01 19:37:18 -04:00
|
|
|
const fd = res.rid();
|
2018-09-27 00:56:39 -04:00
|
|
|
return new File(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function read(
|
|
|
|
fd: number,
|
|
|
|
p: ArrayBufferView
|
|
|
|
): Promise<ReadResult> {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Read.startRead(builder);
|
2018-10-01 19:37:18 -04:00
|
|
|
fbs.Read.addRid(builder, fd);
|
2018-10-03 21:12:23 -04:00
|
|
|
const inner = fbs.Read.endRead(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, fbs.Any.Read, inner, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(fbs.Any.ReadRes === baseRes!.innerType());
|
2018-09-27 00:56:39 -04:00
|
|
|
const res = new fbs.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() };
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function write(fd: number, p: ArrayBufferView): Promise<number> {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Write.startWrite(builder);
|
2018-10-01 19:37:18 -04:00
|
|
|
fbs.Write.addRid(builder, fd);
|
2018-10-03 21:12:23 -04:00
|
|
|
const inner = fbs.Write.endWrite(builder);
|
|
|
|
const baseRes = await dispatch.sendAsync(builder, fbs.Any.Write, inner, p);
|
2018-09-27 00:56:39 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(fbs.Any.WriteRes === baseRes!.innerType());
|
2018-09-27 00:56:39 -04:00
|
|
|
const res = new fbs.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function close(fd: number): void {
|
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
fbs.Close.startClose(builder);
|
2018-10-01 19:37:18 -04:00
|
|
|
fbs.Close.addRid(builder, fd);
|
2018-10-03 21:12:23 -04:00
|
|
|
const inner = fbs.Close.endClose(builder);
|
|
|
|
dispatch.sendSync(builder, fbs.Any.Close, inner);
|
2018-09-27 00:56:39 -04:00
|
|
|
}
|