2020-03-09 10:18:02 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts";
|
|
|
|
|
2020-04-28 12:40:43 -04:00
|
|
|
export function readSync(rid: number, buffer: Uint8Array): number | null {
|
2020-07-06 21:45:39 -04:00
|
|
|
if (buffer.length === 0) {
|
2020-03-09 10:18:02 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-06-21 10:34:43 -04:00
|
|
|
const nread = sendSyncMinimal("op_read", rid, buffer);
|
2020-03-09 10:18:02 -04:00
|
|
|
if (nread < 0) {
|
|
|
|
throw new Error("read error");
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
2020-03-09 10:18:02 -04:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:57:33 -04:00
|
|
|
export async function read(
|
|
|
|
rid: number,
|
|
|
|
buffer: Uint8Array
|
2020-04-28 12:40:43 -04:00
|
|
|
): Promise<number | null> {
|
2020-07-06 21:45:39 -04:00
|
|
|
if (buffer.length === 0) {
|
2020-03-09 10:18:02 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-06-21 10:34:43 -04:00
|
|
|
const nread = await sendAsyncMinimal("op_read", rid, buffer);
|
2020-03-09 10:18:02 -04:00
|
|
|
if (nread < 0) {
|
|
|
|
throw new Error("read error");
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
2020-03-09 10:18:02 -04:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:57:33 -04:00
|
|
|
export function writeSync(rid: number, data: Uint8Array): number {
|
2020-06-21 10:34:43 -04:00
|
|
|
const result = sendSyncMinimal("op_write", rid, data);
|
2020-03-09 10:18:02 -04:00
|
|
|
if (result < 0) {
|
|
|
|
throw new Error("write error");
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
|
|
|
return result;
|
2020-03-09 10:18:02 -04:00
|
|
|
}
|
|
|
|
|
2020-04-01 12:57:33 -04:00
|
|
|
export async function write(rid: number, data: Uint8Array): Promise<number> {
|
2020-06-21 10:34:43 -04:00
|
|
|
const result = await sendAsyncMinimal("op_write", rid, data);
|
2020-03-09 10:18:02 -04:00
|
|
|
if (result < 0) {
|
|
|
|
throw new Error("write error");
|
|
|
|
}
|
2020-07-06 21:45:39 -04:00
|
|
|
|
|
|
|
return result;
|
2020-03-09 10:18:02 -04:00
|
|
|
}
|