2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
// Interfaces 100% copied from Go.
|
|
|
|
// Documentation liberally lifted from them too.
|
|
|
|
// Thank you! We love Go! <3
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2021-03-20 12:51:08 -04:00
|
|
|
const core = window.Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-07-03 10:58:08 -04:00
|
|
|
const {
|
|
|
|
Uint8Array,
|
|
|
|
ArrayPrototypePush,
|
2021-09-16 14:28:15 -04:00
|
|
|
MathMin,
|
2021-07-03 10:58:08 -04:00
|
|
|
TypedArrayPrototypeSubarray,
|
|
|
|
TypedArrayPrototypeSet,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-07-03 15:32:28 -04:00
|
|
|
|
2020-07-19 13:49:44 -04: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 10:58:08 -04:00
|
|
|
nwritten += await dst.write(
|
|
|
|
TypedArrayPrototypeSubarray(b, nwritten, result),
|
|
|
|
);
|
2020-07-19 13:49:44 -04: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 10:58:08 -04:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
2020-07-19 13:49:44 -04: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 10:58:08 -04:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function readSync(rid, buffer) {
|
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:56:56 -04:00
|
|
|
const nread = ops.op_read_sync(rid, buffer);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:28:15 -04:00
|
|
|
async function read(rid, buffer) {
|
2020-07-19 13:49:44 -04:00
|
|
|
if (buffer.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:26:17 -05:00
|
|
|
const nread = await core.read(rid, buffer);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeSync(rid, data) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_write_sync(rid, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-11-09 13:26:17 -05:00
|
|
|
function write(rid, data) {
|
|
|
|
return core.write(rid, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-04-19 10:41:31 -04:00
|
|
|
const READ_PER_ITER = 64 * 1024; // 64kb
|
2021-04-05 18:05:36 -04:00
|
|
|
|
2021-09-16 14:28:15 -04:00
|
|
|
function readAll(r) {
|
|
|
|
return readAllInner(r);
|
2021-06-22 11:45:26 -04:00
|
|
|
}
|
|
|
|
async function readAllInner(r, options) {
|
2021-04-05 18:05:36 -04:00
|
|
|
const buffers = [];
|
2021-06-22 11:45:26 -04:00
|
|
|
const signal = options?.signal ?? null;
|
2021-12-16 06:57:26 -05:00
|
|
|
while (true) {
|
|
|
|
signal?.throwIfAborted();
|
2021-04-05 18:05:36 -04:00
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = await r.read(buf);
|
|
|
|
if (typeof read == "number") {
|
2021-07-03 10:58:08 -04:00
|
|
|
ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read));
|
2021-04-05 18:05:36 -04:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-16 06:57:26 -05:00
|
|
|
signal?.throwIfAborted();
|
2021-04-05 18:05:36 -04:00
|
|
|
|
2021-09-16 14:28:15 -04:00
|
|
|
return concatBuffers(buffers);
|
2021-04-05 18:05:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function readAllSync(r) {
|
|
|
|
const buffers = [];
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = r.readSync(buf);
|
|
|
|
if (typeof read == "number") {
|
2022-09-15 04:05:41 -04:00
|
|
|
ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read));
|
2021-04-05 18:05:36 -04:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:28:15 -04:00
|
|
|
return concatBuffers(buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
function concatBuffers(buffers) {
|
2021-04-05 18:05:36 -04: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 10:58:08 -04:00
|
|
|
TypedArrayPrototypeSet(contents, buf, n);
|
2021-04-05 18:05:36 -04:00
|
|
|
n += buf.byteLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:28:15 -04:00
|
|
|
function readAllSyncSized(r, size) {
|
|
|
|
const buf = new Uint8Array(size + 1); // 1B to detect extended files
|
|
|
|
let cursor = 0;
|
|
|
|
|
|
|
|
while (cursor < size) {
|
|
|
|
const sliceEnd = MathMin(size + 1, cursor + READ_PER_ITER);
|
2022-09-15 04:05:41 -04:00
|
|
|
const slice = TypedArrayPrototypeSubarray(buf, cursor, sliceEnd);
|
2021-09-16 14:28:15 -04:00
|
|
|
const read = r.readSync(slice);
|
|
|
|
if (typeof read == "number") {
|
|
|
|
cursor += read;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle truncated or extended files during read
|
|
|
|
if (cursor > size) {
|
|
|
|
// Read remaining and concat
|
|
|
|
return concatBuffers([buf, readAllSync(r)]);
|
|
|
|
} else { // cursor == size
|
2022-09-15 04:05:41 -04:00
|
|
|
return TypedArrayPrototypeSubarray(buf, 0, cursor);
|
2021-09-16 14:28:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readAllInnerSized(r, size, options) {
|
|
|
|
const buf = new Uint8Array(size + 1); // 1B to detect extended files
|
|
|
|
let cursor = 0;
|
|
|
|
const signal = options?.signal ?? null;
|
2021-12-16 06:57:26 -05:00
|
|
|
while (cursor < size) {
|
|
|
|
signal?.throwIfAborted();
|
2021-09-16 14:28:15 -04:00
|
|
|
const sliceEnd = MathMin(size + 1, cursor + READ_PER_ITER);
|
2022-09-15 04:05:41 -04:00
|
|
|
const slice = TypedArrayPrototypeSubarray(buf, cursor, sliceEnd);
|
2021-09-16 14:28:15 -04:00
|
|
|
const read = await r.read(slice);
|
|
|
|
if (typeof read == "number") {
|
|
|
|
cursor += read;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-12-16 06:57:26 -05:00
|
|
|
signal?.throwIfAborted();
|
2021-09-16 14:28:15 -04:00
|
|
|
|
|
|
|
// Handle truncated or extended files during read
|
|
|
|
if (cursor > size) {
|
|
|
|
// Read remaining and concat
|
|
|
|
return concatBuffers([buf, await readAllInner(r, options)]);
|
|
|
|
} else {
|
2022-09-15 04:05:41 -04:00
|
|
|
return TypedArrayPrototypeSubarray(buf, 0, cursor);
|
2021-09-16 14:28:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.io = {
|
|
|
|
iterSync,
|
|
|
|
iter,
|
|
|
|
copy,
|
|
|
|
SeekMode,
|
|
|
|
read,
|
|
|
|
readSync,
|
|
|
|
write,
|
|
|
|
writeSync,
|
2021-04-05 18:05:36 -04:00
|
|
|
readAll,
|
2021-06-22 11:45:26 -04:00
|
|
|
readAllInner,
|
2021-04-05 18:05:36 -04:00
|
|
|
readAllSync,
|
2021-09-16 14:28:15 -04:00
|
|
|
readAllSyncSized,
|
|
|
|
readAllInnerSized,
|
2020-07-19 13:49:44 -04:00
|
|
|
};
|
|
|
|
})(this);
|