diff options
author | Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> | 2024-08-14 09:42:31 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-14 09:42:31 -0700 |
commit | 1f2d48cd975b719f0248e471f3b503cb01398dfb (patch) | |
tree | f9ac89ff011566aad45456e6d49e2802ce6f345b /tests/unit_node/_fs | |
parent | c765d9ad2fbd82be1b025cae3930fdfe8e30f9e2 (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')
-rw-r--r-- | tests/unit_node/_fs/_fs_read_test.ts | 39 | ||||
-rw-r--r-- | tests/unit_node/_fs/_fs_write_test.ts | 39 |
2 files changed, 75 insertions, 3 deletions
diff --git a/tests/unit_node/_fs/_fs_read_test.ts b/tests/unit_node/_fs/_fs_read_test.ts index 42e8fed73..288e4a57c 100644 --- a/tests/unit_node/_fs/_fs_read_test.ts +++ b/tests/unit_node/_fs/_fs_read_test.ts @@ -1,4 +1,5 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +/// <reference types="npm:@types/node" /> import { assertEquals, assertFalse, @@ -13,21 +14,21 @@ import { closeSync } from "node:fs"; async function readTest( testData: string, - buffer: Buffer, + buffer: NodeJS.ArrayBufferView, offset: number, length: number, position: number | null = null, expected: ( fd: number, bytesRead: number | null, - data: Buffer | undefined, + data: ArrayBufferView | undefined, ) => void, ) { let fd1 = 0; await new Promise<{ fd: number; bytesRead: number | null; - data: Buffer | undefined; + data: ArrayBufferView | undefined; }>((resolve, reject) => { open(testData, "r", (err, fd) => { if (err) reject(err); @@ -320,3 +321,35 @@ Deno.test({ closeSync(fd); }, }); + +Deno.test({ + name: "accepts non Uint8Array buffer", + async fn() { + const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); + const testData = path.resolve(moduleDir, "testdata", "hello.txt"); + const buffer = new ArrayBuffer(1024); + const buf = new Int8Array(buffer); + await readTest( + testData, + buf, + buf.byteOffset, + buf.byteLength, + null, + (_fd, bytesRead, data) => { + assertStrictEquals(bytesRead, 11); + assertEquals(data instanceof Buffer, true); + assertMatch((data as Buffer).toString(), /hello world/); + }, + ); + const fd = openSync(testData, "r"); + + try { + const nRead = readSync(fd, buf); + const expected = new TextEncoder().encode("hello world"); + + assertEquals(buf.slice(0, nRead), new Int8Array(expected.buffer)); + } finally { + closeSync(fd); + } + }, +}); 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)); + }, +}); |