2021-01-11 17:13:41 +00:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 17:49:44 +00:00
|
|
|
|
|
|
|
// Interfaces 100% copied from Go.
|
|
|
|
// Documentation liberally lifted from them too.
|
|
|
|
// Thank you! We love Go! <3
|
2021-02-04 22:18:32 +00:00
|
|
|
"use strict";
|
2020-07-19 17:49:44 +00:00
|
|
|
|
|
|
|
((window) => {
|
2021-03-20 16:51:08 +00:00
|
|
|
const core = window.Deno.core;
|
2021-07-03 19:32:28 +00:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2021-07-03 14:58:08 +00:00
|
|
|
const {
|
|
|
|
Uint8Array,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
TypedArrayPrototypeSubarray,
|
|
|
|
TypedArrayPrototypeSet,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-07-03 19:32:28 +00:00
|
|
|
|
2020-07-19 17:49:44 +00:00
|
|
|
const DEFAULT_BUFFER_SIZE = 32 * 1024;
|
|
|
|
// Seek whence values.
|
|
|
|
// https://golang.org/pkg/io/#pkg-constants
|
|
|
|
const SeekMode = {
|
|
|
|
0: "Start",
|
|
|
|
1: "Current",
|
|
|
|
2: "End",
|
|
|
|
|
|
|
|
Start: 0,
|
|
|
|
Current: 1,
|
|
|
|
End: 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
async function copy(
|
|
|
|
src,
|
|
|
|
dst,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
let n = 0;
|
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
|
|
|
let gotEOF = false;
|
|
|
|
while (gotEOF === false) {
|
|
|
|
const result = await src.read(b);
|
|
|
|
if (result === null) {
|
|
|
|
gotEOF = true;
|
|
|
|
} else {
|
|
|
|
let nwritten = 0;
|
|
|
|
while (nwritten < result) {
|
2021-07-03 14:58:08 +00:00
|
|
|
nwritten += await dst.write(
|
|
|
|
TypedArrayPrototypeSubarray(b, nwritten, result),
|
|
|
|
);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
n += nwritten;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function* iter(
|
|
|
|
r,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
|
|
|
while (true) {
|
|
|
|
const result = await r.read(b);
|
|
|
|
if (result === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:58:08 +00:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function* iterSync(
|
|
|
|
r,
|
|
|
|
options,
|
|
|
|
) {
|
|
|
|
const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE;
|
|
|
|
const b = new Uint8Array(bufSize);
|
|
|
|
while (true) {
|
|
|
|
const result = r.readSync(b);
|
|
|
|
if (result === null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-03 14:58:08 +00:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readSync(rid, buffer) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:55:05 +00:00
|
|
|
const nread = core.opSync("op_read_sync", rid, buffer);
|
2020-07-19 17:49:44 +00:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function read(
|
|
|
|
rid,
|
|
|
|
buffer,
|
|
|
|
) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-12 19:55:05 +00:00
|
|
|
const nread = await core.opAsync("op_read_async", rid, buffer);
|
2020-07-19 17:49:44 +00:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeSync(rid, data) {
|
2021-04-12 19:55:05 +00:00
|
|
|
return core.opSync("op_write_sync", rid, data);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function write(rid, data) {
|
2021-04-12 19:55:05 +00:00
|
|
|
return await core.opAsync("op_write_async", rid, data);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 22:05:36 +00:00
|
|
|
const READ_PER_ITER = 32 * 1024;
|
|
|
|
|
|
|
|
async function readAll(r) {
|
2021-06-22 15:45:26 +00:00
|
|
|
return await readAllInner(r);
|
|
|
|
}
|
|
|
|
async function readAllInner(r, options) {
|
2021-04-05 22:05:36 +00:00
|
|
|
const buffers = [];
|
2021-06-22 15:45:26 +00:00
|
|
|
const signal = options?.signal ?? null;
|
|
|
|
while (!signal?.aborted) {
|
2021-04-05 22:05:36 +00:00
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = await r.read(buf);
|
|
|
|
if (typeof read == "number") {
|
2021-07-03 14:58:08 +00:00
|
|
|
ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read));
|
2021-04-05 22:05:36 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 15:45:26 +00:00
|
|
|
if (signal?.aborted) {
|
|
|
|
throw new DOMException("The read operation was aborted.", "AbortError");
|
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
|
|
|
let totalLen = 0;
|
|
|
|
for (const buf of buffers) {
|
|
|
|
totalLen += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contents = new Uint8Array(totalLen);
|
|
|
|
|
|
|
|
let n = 0;
|
|
|
|
for (const buf of buffers) {
|
2021-07-03 14:58:08 +00:00
|
|
|
TypedArrayPrototypeSet(contents, buf, n);
|
2021-04-05 22:05:36 +00:00
|
|
|
n += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
function readAllSync(r) {
|
|
|
|
const buffers = [];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = r.readSync(buf);
|
|
|
|
if (typeof read == "number") {
|
2021-07-03 14:58:08 +00:00
|
|
|
ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read));
|
2021-04-05 22:05:36 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let totalLen = 0;
|
|
|
|
for (const buf of buffers) {
|
|
|
|
totalLen += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
const contents = new Uint8Array(totalLen);
|
|
|
|
|
|
|
|
let n = 0;
|
|
|
|
for (const buf of buffers) {
|
2021-07-03 14:58:08 +00:00
|
|
|
TypedArrayPrototypeSet(contents, buf, n);
|
2021-04-05 22:05:36 +00:00
|
|
|
n += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2020-07-19 17:49:44 +00:00
|
|
|
window.__bootstrap.io = {
|
|
|
|
iterSync,
|
|
|
|
iter,
|
|
|
|
copy,
|
|
|
|
SeekMode,
|
|
|
|
read,
|
|
|
|
readSync,
|
|
|
|
write,
|
|
|
|
writeSync,
|
2021-04-05 22:05:36 +00:00
|
|
|
readAll,
|
2021-06-22 15:45:26 +00:00
|
|
|
readAllInner,
|
2021-04-05 22:05:36 +00:00
|
|
|
readAllSync,
|
2020-07-19 17:49:44 +00:00
|
|
|
};
|
|
|
|
})(this);
|