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/buffer.ts | |
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/buffer.ts')
-rw-r--r-- | js/buffer.ts | 18 |
1 files changed, 18 insertions, 0 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)); + } +} |