diff options
author | Luca Casonato <lucacasonato@yahoo.com> | 2021-04-06 00:05:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 00:05:36 +0200 |
commit | da60e2afcbd4b28e3b8ba69b5e38d4ff173ddbe1 (patch) | |
tree | 616031ffcb358a1ff3168a96bc95512f3648ff9f /runtime/js/12_io.js | |
parent | 2aed322dd507a8568b6ee6f4897e9a8e3220f763 (diff) |
chore: deprecate Deno.Buffer and read/write utils (#9793)
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.
Diffstat (limited to 'runtime/js/12_io.js')
-rw-r--r-- | runtime/js/12_io.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/runtime/js/12_io.js b/runtime/js/12_io.js index fe815c7ed..aa153e9fd 100644 --- a/runtime/js/12_io.js +++ b/runtime/js/12_io.js @@ -123,6 +123,66 @@ 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, @@ -132,5 +192,7 @@ readSync, write, writeSync, + readAll, + readAllSync, }; })(this); |