summaryrefslogtreecommitdiff
path: root/cli/js/ops/io.ts
blob: cc3d1d1701181c25e99314cbfe6c7c45bb526d74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

import { sendAsyncMinimal, sendSyncMinimal } from "./dispatch_minimal.ts";

export function readSync(rid: number, buffer: Uint8Array): number | null {
  if (buffer.length == 0) {
    return 0;
  }
  const nread = sendSyncMinimal("op_read", rid, buffer);
  if (nread < 0) {
    throw new Error("read error");
  } else if (nread == 0) {
    return null;
  } else {
    return nread;
  }
}

export async function read(
  rid: number,
  buffer: Uint8Array
): Promise<number | null> {
  if (buffer.length == 0) {
    return 0;
  }
  const nread = await sendAsyncMinimal("op_read", rid, buffer);
  if (nread < 0) {
    throw new Error("read error");
  } else if (nread == 0) {
    return null;
  } else {
    return nread;
  }
}

export function writeSync(rid: number, data: Uint8Array): number {
  const result = sendSyncMinimal("op_write", rid, data);
  if (result < 0) {
    throw new Error("write error");
  } else {
    return result;
  }
}

export async function write(rid: number, data: Uint8Array): Promise<number> {
  const result = await sendAsyncMinimal("op_write", rid, data);
  if (result < 0) {
    throw new Error("write error");
  } else {
    return result;
  }
}