mirror of
https://github.com/denoland/deno.git
synced 2024-11-01 09:24:20 -04:00
da60e2afcb
This commit marks the `Deno.Buffer` / `Deno.readAll` / `Deno.readAllSync` / `Deno.writeAll` / `Deno.writeAllSync` utils as deprecated, and schedules them for removal in Deno 2.0. These utilities are implemented in pure JS, so should not be part of the Deno namespace. These utilities are now available in std/io/buffer and std/io/util: https://github.com/denoland/deno_std/pull/808. This additionallty removes all internal dependance on Deno.Buffer.
198 lines
3.8 KiB
JavaScript
198 lines
3.8 KiB
JavaScript
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Interfaces 100% copied from Go.
|
|
// Documentation liberally lifted from them too.
|
|
// Thank you! We love Go! <3
|
|
"use strict";
|
|
|
|
((window) => {
|
|
const core = window.Deno.core;
|
|
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(b.subarray(nwritten, result));
|
|
}
|
|
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;
|
|
}
|
|
|
|
yield b.subarray(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;
|
|
}
|
|
|
|
yield b.subarray(0, result);
|
|
}
|
|
}
|
|
|
|
function readSync(rid, buffer) {
|
|
if (buffer.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
const nread = core.binOpSync("op_read_sync", rid, buffer);
|
|
if (nread < 0) {
|
|
throw new Error("read error");
|
|
}
|
|
|
|
return nread === 0 ? null : nread;
|
|
}
|
|
|
|
async function read(
|
|
rid,
|
|
buffer,
|
|
) {
|
|
if (buffer.length === 0) {
|
|
return 0;
|
|
}
|
|
|
|
const nread = await core.binOpAsync("op_read_async", rid, buffer);
|
|
if (nread < 0) {
|
|
throw new Error("read error");
|
|
}
|
|
|
|
return nread === 0 ? null : nread;
|
|
}
|
|
|
|
function writeSync(rid, data) {
|
|
const result = core.binOpSync("op_write_sync", rid, data);
|
|
if (result < 0) {
|
|
throw new Error("write error");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
async function write(rid, data) {
|
|
const result = await core.binOpAsync("op_write_async", rid, data);
|
|
if (result < 0) {
|
|
throw new Error("write error");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
const READ_PER_ITER = 32 * 1024;
|
|
|
|
async function readAll(r) {
|
|
const buffers = [];
|
|
|
|
while (true) {
|
|
const buf = new Uint8Array(READ_PER_ITER);
|
|
const read = await r.read(buf);
|
|
if (typeof read == "number") {
|
|
buffers.push(new Uint8Array(buf.buffer, 0, read));
|
|
} 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) {
|
|
contents.set(buf, n);
|
|
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") {
|
|
buffers.push(new Uint8Array(buf.buffer, 0, read));
|
|
} 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) {
|
|
contents.set(buf, n);
|
|
n += buf.byteLength;
|
|
}
|
|
|
|
return contents;
|
|
}
|
|
|
|
window.__bootstrap.io = {
|
|
iterSync,
|
|
iter,
|
|
copy,
|
|
SeekMode,
|
|
read,
|
|
readSync,
|
|
write,
|
|
writeSync,
|
|
readAll,
|
|
readAllSync,
|
|
};
|
|
})(this);
|