diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2019-07-23 08:16:39 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-07-23 11:16:39 -0400 |
commit | e49d1e16ca2fca45e959c1add9b5a1d6866dbb90 (patch) | |
tree | 1415190fcd43e18e0994eba5f13b9babf2b474db /js | |
parent | 70de8dd51d465ea2016d6bb7c0728111f4493668 (diff) |
feat: expose writeAll() and writeAllSync() (#2298)
Symmetric with `readAll()` and `readAllSync()`. Also used in `xeval`.
Also correct usage in `writeFile()`/`writeFileSync()`.
Diffstat (limited to 'js')
-rw-r--r-- | js/buffer.ts | 18 | ||||
-rw-r--r-- | js/buffer_test.ts | 24 | ||||
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/write_file.ts | 5 | ||||
-rw-r--r-- | js/xeval.ts | 14 |
5 files changed, 46 insertions, 17 deletions
diff --git a/js/buffer.ts b/js/buffer.ts index 1f597282d..9525e6954 100644 --- a/js/buffer.ts +++ b/js/buffer.ts @@ -274,3 +274,21 @@ export function readAllSync(r: SyncReader): Uint8Array { buf.readFromSync(r); return buf.bytes(); } + +/** Write all the content of `arr` to `w`. + */ +export async function writeAll(w: Writer, arr: Uint8Array): Promise<void> { + let nwritten = 0; + while (nwritten < arr.length) { + nwritten += await w.write(arr.subarray(nwritten)); + } +} + +/** Write synchronously all the content of `arr` to `w`. + */ +export function writeAllSync(w: SyncWriter, arr: Uint8Array): void { + let nwritten = 0; + while (nwritten < arr.length) { + nwritten += w.writeSync(arr.subarray(nwritten)); + } +} diff --git a/js/buffer_test.ts b/js/buffer_test.ts index 3b2f5d312..911750bb1 100644 --- a/js/buffer_test.ts +++ b/js/buffer_test.ts @@ -5,7 +5,7 @@ // https://github.com/golang/go/blob/master/LICENSE import { assertEquals, test } from "./test_util.ts"; -const { Buffer, readAll, readAllSync } = Deno; +const { Buffer, readAll, readAllSync, writeAll, writeAllSync } = Deno; type Buffer = Deno.Buffer; // N controls how many iterations of certain checks are performed. @@ -253,3 +253,25 @@ test(function testReadAllSync(): void { assertEquals(testBytes[i], actualBytes[i]); } }); + +test(async function testWriteAll(): Promise<void> { + init(); + const writer = new Buffer(); + await writeAll(writer, testBytes); + const actualBytes = writer.bytes(); + assertEquals(testBytes.byteLength, actualBytes.byteLength); + for (let i = 0; i < testBytes.length; ++i) { + assertEquals(testBytes[i], actualBytes[i]); + } +}); + +test(function testWriteAllSync(): void { + init(); + const writer = new Buffer(); + writeAllSync(writer, testBytes); + const actualBytes = writer.bytes(); + assertEquals(testBytes.byteLength, actualBytes.byteLength); + for (let i = 0; i < testBytes.length; ++i) { + assertEquals(testBytes[i], actualBytes[i]); + } +}); diff --git a/js/deno.ts b/js/deno.ts index f20b6eff1..32899e045 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -38,7 +38,7 @@ export { ReadWriteCloser, ReadWriteSeeker } from "./io"; -export { Buffer, readAll, readAllSync } from "./buffer"; +export { Buffer, readAll, readAllSync, writeAll, writeAllSync } from "./buffer"; export { mkdirSync, mkdir } from "./mkdir"; export { makeTempDirSync, diff --git a/js/write_file.ts b/js/write_file.ts index 39372a27e..c7b71725c 100644 --- a/js/write_file.ts +++ b/js/write_file.ts @@ -2,6 +2,7 @@ import { stat, statSync } from "./stat"; import { open, openSync } from "./files"; import { chmod, chmodSync } from "./chmod"; +import { writeAll, writeAllSync } from "./buffer"; /** Options for writing to a file. * `perm` would change the file's permission if set. @@ -40,7 +41,7 @@ export function writeFileSync( chmodSync(filename, options.perm); } - file.writeSync(data); + writeAllSync(file, data); file.close(); } @@ -70,6 +71,6 @@ export async function writeFile( await chmod(filename, options.perm); } - await file.write(data); + await writeAll(file, data); file.close(); } diff --git a/js/xeval.ts b/js/xeval.ts index 81e79f590..fa706ae20 100644 --- a/js/xeval.ts +++ b/js/xeval.ts @@ -1,22 +1,10 @@ -import { Buffer } from "./buffer"; +import { Buffer, writeAll } from "./buffer"; import { stdin } from "./files"; import { TextEncoder, TextDecoder } from "./text_encoding"; import { Reader, EOF } from "./io"; export type XevalFunc = (v: string) => void; -async function writeAll(buffer: Buffer, arr: Uint8Array): Promise<void> { - let bytesWritten = 0; - while (bytesWritten < arr.length) { - try { - const nwritten = await buffer.write(arr.subarray(bytesWritten)); - bytesWritten += nwritten; - } catch { - return; - } - } -} - // TODO(kevinkassimo): Move this utility to deno_std. // Import from there once doable. // Read from reader until EOF and emit string chunks separated |