summaryrefslogtreecommitdiff
path: root/tests/unit_node/_fs/_fs_write_test.ts
diff options
context:
space:
mode:
authorNathan Whitaker <17734409+nathanwhit@users.noreply.github.com>2024-08-14 09:42:31 -0700
committerGitHub <noreply@github.com>2024-08-14 09:42:31 -0700
commit1f2d48cd975b719f0248e471f3b503cb01398dfb (patch)
treef9ac89ff011566aad45456e6d49e2802ce6f345b /tests/unit_node/_fs/_fs_write_test.ts
parentc765d9ad2fbd82be1b025cae3930fdfe8e30f9e2 (diff)
fix(node/fs): node:fs.read and write should accept typed arrays other than Uint8Array (#25030)
Part of #25028. Our underlying read/write operations in `io` assume the buffer is a Uint8Array, but we were passing in other typed arrays (in the case above it was `Int8Array`).
Diffstat (limited to 'tests/unit_node/_fs/_fs_write_test.ts')
-rw-r--r--tests/unit_node/_fs/_fs_write_test.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/unit_node/_fs/_fs_write_test.ts b/tests/unit_node/_fs/_fs_write_test.ts
index ab82d4f5b..3ce030bc6 100644
--- a/tests/unit_node/_fs/_fs_write_test.ts
+++ b/tests/unit_node/_fs/_fs_write_test.ts
@@ -73,3 +73,42 @@ Deno.test({
assertEquals(decoder.decode(data), "\x00\x00\x00\x00hello world");
},
});
+
+Deno.test({
+ name: "Accepts non Uint8Array buffer",
+ async fn() {
+ const tempFile: string = Deno.makeTempFileSync();
+ using file = Deno.openSync(tempFile, {
+ create: true,
+ write: true,
+ read: true,
+ });
+ const bytes = [0, 1, 2, 3, 4];
+ const buffer = new Int8Array(bytes);
+ for (let i = 0; i < buffer.length; i++) {
+ buffer[i] = i;
+ }
+ let nWritten = writeSync(file.rid, buffer);
+
+ const data = Deno.readFileSync(tempFile);
+
+ assertEquals(nWritten, 5);
+ assertEquals(data, new Uint8Array(bytes));
+
+ nWritten = await new Promise((resolve, reject) =>
+ write(
+ file.rid,
+ buffer,
+ 0,
+ 5,
+ (err: unknown, nwritten: number) => {
+ if (err) return reject(err);
+ resolve(nwritten);
+ },
+ )
+ );
+
+ assertEquals(nWritten, 5);
+ assertEquals(data, new Uint8Array(bytes));
+ },
+});