From da60e2afcbd4b28e3b8ba69b5e38d4ff173ddbe1 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Tue, 6 Apr 2021 00:05:36 +0200 Subject: 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. --- runtime/js/12_io.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ runtime/js/40_process.js | 2 +- runtime/js/40_read_file.js | 2 +- runtime/js/40_write_file.js | 13 +++++++--- 4 files changed, 74 insertions(+), 5 deletions(-) (limited to 'runtime/js') 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); diff --git a/runtime/js/40_process.js b/runtime/js/40_process.js index cd8015e94..a93818b95 100644 --- a/runtime/js/40_process.js +++ b/runtime/js/40_process.js @@ -4,7 +4,7 @@ ((window) => { const core = window.Deno.core; const { File } = window.__bootstrap.files; - const { readAll } = window.__bootstrap.buffer; + const { readAll } = window.__bootstrap.io; const { assert, pathFromURL } = window.__bootstrap.util; function opKill(pid, signo) { diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 63b05b877..0ca8f56e9 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -3,7 +3,7 @@ ((window) => { const { open, openSync } = window.__bootstrap.files; - const { readAll, readAllSync } = window.__bootstrap.buffer; + const { readAll, readAllSync } = window.__bootstrap.io; function readFileSync(path) { const file = openSync(path); diff --git a/runtime/js/40_write_file.js b/runtime/js/40_write_file.js index 5964dec5f..4662849a5 100644 --- a/runtime/js/40_write_file.js +++ b/runtime/js/40_write_file.js @@ -3,7 +3,6 @@ ((window) => { const { stat, statSync, chmod, chmodSync } = window.__bootstrap.fs; const { open, openSync } = window.__bootstrap.files; - const { writeAll, writeAllSync } = window.__bootstrap.buffer; const { build } = window.__bootstrap.build; function writeFileSync( @@ -32,7 +31,11 @@ chmodSync(path, options.mode); } - writeAllSync(file, data); + let nwritten = 0; + while (nwritten < data.length) { + nwritten += file.writeSync(data.subarray(nwritten)); + } + file.close(); } @@ -62,7 +65,11 @@ await chmod(path, options.mode); } - await writeAll(file, data); + let nwritten = 0; + while (nwritten < data.length) { + nwritten += await file.write(data.subarray(nwritten)); + } + file.close(); } -- cgit v1.2.3