summaryrefslogtreecommitdiff
path: root/runtime/js
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/js')
-rw-r--r--runtime/js/12_io.js62
-rw-r--r--runtime/js/40_process.js2
-rw-r--r--runtime/js/40_read_file.js2
-rw-r--r--runtime/js/40_write_file.js13
4 files changed, 74 insertions, 5 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);
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();
}