2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-08-20 15:14:37 -04:00
|
|
|
|
|
|
|
// deno-lint-ignore-file no-console
|
|
|
|
|
2023-03-19 12:10:39 -04:00
|
|
|
import { write, writeSync } from "node:fs";
|
2024-07-25 01:30:28 -04:00
|
|
|
import { assertEquals } from "@std/assert";
|
2023-03-19 12:10:39 -04:00
|
|
|
import { Buffer } from "node:buffer";
|
|
|
|
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Data is written to the file with the correct length",
|
2024-08-28 20:09:06 -04:00
|
|
|
// TODO(bartlomieju): this test is broken in Deno 2, because `file.rid` is undefined.
|
|
|
|
// The fs APIs should be rewritten to use actual FDs, not RIDs
|
|
|
|
ignore: true,
|
2023-03-19 12:10:39 -04:00
|
|
|
async fn() {
|
|
|
|
const tempFile: string = await Deno.makeTempFile();
|
2024-01-24 09:59:55 -05:00
|
|
|
using file = await Deno.open(tempFile, {
|
2023-03-19 12:10:39 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
|
|
|
const buffer = Buffer.from("hello world");
|
|
|
|
const bytesWrite = await new Promise((resolve, reject) => {
|
2024-09-10 17:19:34 -04:00
|
|
|
// @ts-ignore (iuioiua) `file.rid` should no longer be needed once FDs are used
|
2023-03-19 12:10:39 -04:00
|
|
|
write(file.rid, buffer, 0, 5, (err: unknown, nwritten: number) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(nwritten);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const data = await Deno.readFile(tempFile);
|
|
|
|
await Deno.remove(tempFile);
|
|
|
|
|
|
|
|
assertEquals(bytesWrite, 5);
|
|
|
|
assertEquals(decoder.decode(data), "hello");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Data is written synchronously to the file with the correct length",
|
2024-08-28 20:09:06 -04:00
|
|
|
// TODO(bartlomieju): this test is broken in Deno 2, because `file.rid` is undefined.
|
|
|
|
// The fs APIs should be rewritten to use actual FDs, not RIDs
|
|
|
|
ignore: true,
|
2023-03-19 12:10:39 -04:00
|
|
|
fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
2024-01-24 09:59:55 -05:00
|
|
|
using file = Deno.openSync(tempFile, {
|
2023-03-19 12:10:39 -04:00
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
|
|
|
const buffer = Buffer.from("hello world");
|
2024-09-10 17:19:34 -04:00
|
|
|
// @ts-ignore (iuioiua) `file.rid` should no longer be needed once FDs are used
|
2023-03-19 12:10:39 -04:00
|
|
|
const bytesWrite = writeSync(file.rid, buffer, 0, 5);
|
|
|
|
|
|
|
|
const data = Deno.readFileSync(tempFile);
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
|
|
|
|
assertEquals(bytesWrite, 5);
|
|
|
|
assertEquals(decoder.decode(data), "hello");
|
|
|
|
},
|
|
|
|
});
|
2024-05-28 08:30:31 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "Data is padded if position > length",
|
2024-08-28 20:09:06 -04:00
|
|
|
// TODO(bartlomieju): this test is broken in Deno 2, because `file.rid` is undefined.
|
|
|
|
// The fs APIs should be rewritten to use actual FDs, not RIDs
|
|
|
|
ignore: true,
|
2024-05-28 08:30:31 -04:00
|
|
|
async fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
|
|
|
|
using file = await Deno.open(tempFile, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const str = "hello world";
|
|
|
|
const buffer = Buffer.from(str);
|
2024-09-10 17:19:34 -04:00
|
|
|
// @ts-ignore (iuioiua) `file.rid` should no longer be needed once FDs are used
|
2024-05-28 08:30:31 -04:00
|
|
|
const bytesWritten = writeSync(file.rid, buffer, 0, str.length, 4);
|
|
|
|
|
|
|
|
const data = Deno.readFileSync(tempFile);
|
|
|
|
Deno.removeSync(tempFile);
|
|
|
|
|
|
|
|
assertEquals(bytesWritten, str.length);
|
|
|
|
// Check if result is padded
|
|
|
|
assertEquals(decoder.decode(data), "\x00\x00\x00\x00hello world");
|
|
|
|
},
|
|
|
|
});
|
2024-08-14 12:42:31 -04:00
|
|
|
|
|
|
|
Deno.test({
|
2024-08-16 12:48:57 -04:00
|
|
|
name: "write with offset TypedArray buffers",
|
2024-08-28 20:09:06 -04:00
|
|
|
// TODO(bartlomieju): this test is broken in Deno 2, because `file.rid` is undefined.
|
|
|
|
// The fs APIs should be rewritten to use actual FDs, not RIDs
|
|
|
|
ignore: true,
|
2024-08-14 12:42:31 -04:00
|
|
|
async fn() {
|
|
|
|
const tempFile: string = Deno.makeTempFileSync();
|
|
|
|
using file = Deno.openSync(tempFile, {
|
|
|
|
create: true,
|
|
|
|
write: true,
|
|
|
|
read: true,
|
|
|
|
});
|
2024-08-16 12:48:57 -04:00
|
|
|
const arrayBuffer = new ArrayBuffer(128);
|
|
|
|
const resetBuffer = () => {
|
|
|
|
new Uint8Array(arrayBuffer).fill(0);
|
|
|
|
};
|
|
|
|
const bufConstructors = [
|
|
|
|
Int8Array,
|
|
|
|
Uint8Array,
|
|
|
|
];
|
|
|
|
const offsets = [0, 24, 48];
|
2024-08-14 12:42:31 -04:00
|
|
|
const bytes = [0, 1, 2, 3, 4];
|
2024-08-16 12:48:57 -04:00
|
|
|
for (const constr of bufConstructors) {
|
|
|
|
// test combinations of buffers internally offset from their backing array buffer,
|
|
|
|
// and also offset in the write call
|
|
|
|
for (const innerOffset of offsets) {
|
|
|
|
for (const offset of offsets) {
|
|
|
|
resetBuffer();
|
|
|
|
const buffer = new constr(
|
|
|
|
arrayBuffer,
|
|
|
|
innerOffset,
|
|
|
|
offset + bytes.length,
|
|
|
|
);
|
|
|
|
for (let i = 0; i < bytes.length; i++) {
|
|
|
|
buffer[offset + i] = i;
|
|
|
|
}
|
2024-09-10 17:19:34 -04:00
|
|
|
// @ts-ignore (iuioiua) `file.rid` should no longer be needed once FDs are used
|
2024-08-16 12:48:57 -04:00
|
|
|
let nWritten = writeSync(file.rid, buffer, offset, bytes.length, 0);
|
|
|
|
|
|
|
|
let data = Deno.readFileSync(tempFile);
|
|
|
|
|
|
|
|
assertEquals(nWritten, bytes.length);
|
|
|
|
console.log(constr, innerOffset, offset);
|
|
|
|
assertEquals(data, new Uint8Array(bytes));
|
|
|
|
nWritten = await new Promise((resolve, reject) =>
|
|
|
|
write(
|
2024-09-10 17:19:34 -04:00
|
|
|
// @ts-ignore (iuioiua) `file.rid` should no longer be needed once FDs are used
|
2024-08-16 12:48:57 -04:00
|
|
|
file.rid,
|
|
|
|
buffer,
|
|
|
|
offset,
|
|
|
|
bytes.length,
|
|
|
|
0,
|
|
|
|
(err: unknown, nwritten: number) => {
|
|
|
|
if (err) return reject(err);
|
|
|
|
resolve(nwritten);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
data = Deno.readFileSync(tempFile);
|
|
|
|
assertEquals(nWritten, 5);
|
|
|
|
assertEquals(data, new Uint8Array(bytes));
|
|
|
|
}
|
|
|
|
}
|
2024-08-14 12:42:31 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|