-
Notifications
You must be signed in to change notification settings - Fork 576
/
fs.ts
74 lines (71 loc) · 2.31 KB
/
fs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
import { assert } from "../../testing/asserts.ts";
// This module implements functions in https://github.com/nodejs/node/blob/master/src/node_file.cc
/**
* Write to the given file from the given buffer asynchronously.
*
* Implements async part of WriteBuffer in src/node_file.cc
* See: https://github.com/nodejs/node/blob/e9ed113/src/node_file.cc#L1818
*
* @param fs file descriptor
* @param buffer the data to write
* @param offset where in the buffer to start from
* @param length how much to write
* @param position if integer, position to write at in the file. if null, write from the current position
* @param callback callback function
*/
export function writeBuffer(
_fd: number,
_buffer: Uint8Array,
_offset: number,
_length: number,
_position: number | null,
_callback?: (err: Error, n: number) => void,
): void {
throw new Error("unimplemented!");
}
/**
* Write to the given file from the given buffer synchronously.
*
* Implements sync part of WriteBuffer in src/node_file.cc
* See: https://github.com/nodejs/node/blob/e9ed113/src/node_file.cc#L1818
*
* @param fs file descriptor
* @param buffer the data to write
* @param offset where in the buffer to start from
* @param length how much to write
* @param position if integer, position to write at in the file. if null, write from the current position
* @param context context object for passing error number
*/
export function writeBufferSync(
fd: number,
buffer: Uint8Array,
offset: number,
length: number,
position: number | null,
ctx: { errno?: number },
) {
assert(offset >= 0, "offset should be greater or equal to 0");
assert(
offset + length <= buffer.byteLength,
`buffer doesn't have enough data: byteLength = ${buffer.byteLength}, offset + length = ${offset +
length}`,
);
if (position) {
Deno.seekSync(fd, position, Deno.SeekMode.Current);
}
const subarray = buffer.subarray(offset, offset + length);
try {
return Deno.writeSync(fd, subarray);
} catch (e) {
ctx.errno = extractOsErrorNumberFromErrorMessage(e);
return 0;
}
}
function extractOsErrorNumberFromErrorMessage(e: Error): number {
const match = e.message.match(/\(os error (\d+)\)/);
if (match) {
return +match[1];
}
return 255; // Unknown error
}