2024-01-01 19:58:21 +00:00
|
|
|
// Copyright 2018-2024 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
|
|
|
|
|
2023-12-07 13:21:01 +00:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 22:37:25 +00:00
|
|
|
const {
|
|
|
|
op_stdin_set_raw,
|
|
|
|
} = core.ensureFastOps(true);
|
2023-02-07 19:22:46 +00:00
|
|
|
const {
|
|
|
|
Uint8Array,
|
|
|
|
ArrayPrototypePush,
|
|
|
|
TypedArrayPrototypeSubarray,
|
|
|
|
TypedArrayPrototypeSet,
|
2023-04-02 17:41:41 +00:00
|
|
|
TypedArrayPrototypeGetByteLength,
|
2023-02-07 19:22:46 +00:00
|
|
|
} = primordials;
|
|
|
|
|
2024-01-10 22:37:25 +00:00
|
|
|
import {
|
|
|
|
readableStreamForRid,
|
|
|
|
writableStreamForRid,
|
|
|
|
} from "ext:deno_web/06_streams.js";
|
|
|
|
|
2023-02-07 19:22:46 +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) {
|
|
|
|
nwritten += await dst.write(
|
|
|
|
TypedArrayPrototypeSubarray(b, nwritten, result),
|
|
|
|
);
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
n += nwritten;
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 19:22:46 +00:00
|
|
|
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;
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2020-07-19 17:49:44 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
yield TypedArrayPrototypeSubarray(b, 0, result);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
function readSync(rid, buffer) {
|
2023-04-05 22:14:16 +00:00
|
|
|
if (buffer.length === 0) return 0;
|
|
|
|
const nread = core.readSync(rid, buffer);
|
2023-02-07 19:22:46 +00:00
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
async function read(rid, buffer) {
|
2023-04-05 22:14:16 +00:00
|
|
|
if (buffer.length === 0) return 0;
|
2023-02-07 19:22:46 +00:00
|
|
|
const nread = await core.read(rid, buffer);
|
|
|
|
return nread === 0 ? null : nread;
|
|
|
|
}
|
2020-07-19 17:49:44 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
function writeSync(rid, data) {
|
2023-04-05 22:14:16 +00:00
|
|
|
return core.writeSync(rid, data);
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
function write(rid, data) {
|
|
|
|
return core.write(rid, data);
|
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
const READ_PER_ITER = 64 * 1024; // 64kb
|
|
|
|
|
2024-01-09 12:53:28 +00:00
|
|
|
async function readAll(r) {
|
2023-02-07 19:22:46 +00:00
|
|
|
const buffers = [];
|
2024-01-09 12:53:28 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
while (true) {
|
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = await r.read(buf);
|
|
|
|
if (typeof read == "number") {
|
2024-01-09 12:53:28 +00:00
|
|
|
ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read));
|
2023-02-07 19:22:46 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
return concatBuffers(buffers);
|
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
function readAllSync(r) {
|
|
|
|
const buffers = [];
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
while (true) {
|
|
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
|
|
const read = r.readSync(buf);
|
|
|
|
if (typeof read == "number") {
|
|
|
|
ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2021-09-16 18:28:15 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
return concatBuffers(buffers);
|
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
function concatBuffers(buffers) {
|
|
|
|
let totalLen = 0;
|
|
|
|
for (let i = 0; i < buffers.length; ++i) {
|
2023-04-02 17:41:41 +00:00
|
|
|
totalLen += TypedArrayPrototypeGetByteLength(buffers[i]);
|
2023-02-07 19:22:46 +00:00
|
|
|
}
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
const contents = new Uint8Array(totalLen);
|
2021-04-05 22:05:36 +00:00
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
let n = 0;
|
|
|
|
for (let i = 0; i < buffers.length; ++i) {
|
|
|
|
const buf = buffers[i];
|
|
|
|
TypedArrayPrototypeSet(contents, buf, n);
|
2023-04-02 17:41:41 +00:00
|
|
|
n += TypedArrayPrototypeGetByteLength(buf);
|
2021-04-05 22:05:36 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
return contents;
|
|
|
|
}
|
|
|
|
|
2023-03-05 03:37:37 +00:00
|
|
|
class Stdin {
|
|
|
|
#readable;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
read(p) {
|
|
|
|
return read(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
readSync(p) {
|
|
|
|
return readSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2023-11-20 13:27:18 +00:00
|
|
|
core.tryClose(this.rid);
|
2023-03-05 03:37:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get readable() {
|
|
|
|
if (this.#readable === undefined) {
|
|
|
|
this.#readable = readableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRaw(mode, options = {}) {
|
|
|
|
const cbreak = !!(options.cbreak ?? false);
|
2024-01-10 22:37:25 +00:00
|
|
|
op_stdin_set_raw(mode, cbreak);
|
2023-03-05 03:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Stdout {
|
|
|
|
#writable;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Stderr {
|
|
|
|
#writable;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
get rid() {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
write(p) {
|
|
|
|
return write(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSync(p) {
|
|
|
|
return writeSync(this.rid, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
core.close(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
|
|
|
if (this.#writable === undefined) {
|
|
|
|
this.#writable = writableStreamForRid(this.rid);
|
|
|
|
}
|
|
|
|
return this.#writable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const stdin = new Stdin();
|
|
|
|
const stdout = new Stdout();
|
|
|
|
const stderr = new Stderr();
|
|
|
|
|
2023-02-07 19:22:46 +00:00
|
|
|
export {
|
|
|
|
copy,
|
|
|
|
iter,
|
|
|
|
iterSync,
|
|
|
|
read,
|
|
|
|
readAll,
|
|
|
|
readAllSync,
|
|
|
|
readSync,
|
|
|
|
SeekMode,
|
2023-03-05 03:37:37 +00:00
|
|
|
stderr,
|
|
|
|
stdin,
|
|
|
|
stdout,
|
2023-02-07 19:22:46 +00:00
|
|
|
write,
|
|
|
|
writeSync,
|
|
|
|
};
|