diff options
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); |