/
_fs_read.ts
178 lines (155 loc) · 4.1 KB
/
_fs_read.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { Buffer } from "../buffer.ts";
import { assert } from "../../testing/asserts.ts";
import { ERR_INVALID_ARG_VALUE } from "../internal/errors.ts";
type readOptions = {
buffer: Buffer | Uint8Array;
offset: number;
length: number;
position: number | null;
};
type readSyncOptions = {
offset: number;
length: number;
position: number | null;
};
type BinaryCallback = (
err: Error | null,
bytesRead: number | null,
data?: Buffer,
) => void;
type Callback = BinaryCallback;
export function read(fd: number, callback: Callback): void;
export function read(
fd: number,
options: readOptions,
callback: Callback,
): void;
export function read(
fd: number,
buffer: Buffer | Uint8Array,
offset: number,
length: number,
position: number | null,
callback: Callback,
): void;
export function read(
fd: number,
optOrBuffer?: Buffer | Uint8Array | readOptions | Callback,
offsetOrCallback?: number | Callback,
length?: number,
position?: number | null,
callback?: Callback,
): void {
let cb: Callback | undefined;
let offset = 0,
buffer: Buffer | Uint8Array;
if (length == null) {
length = 0;
}
if (typeof offsetOrCallback === "function") {
cb = offsetOrCallback;
} else if (typeof optOrBuffer === "function") {
cb = optOrBuffer;
} else {
offset = offsetOrCallback as number;
cb = callback;
}
if (!cb) throw new Error("No callback function supplied");
if (optOrBuffer instanceof Buffer || optOrBuffer instanceof Uint8Array) {
buffer = optOrBuffer;
} else if (typeof optOrBuffer === "function") {
offset = 0;
buffer = Buffer.alloc(16384);
length = buffer.byteLength;
position = null;
} else {
const opt = optOrBuffer as readOptions;
offset = opt.offset ?? 0;
buffer = opt.buffer ?? Buffer.alloc(16384);
length = opt.length ?? buffer.byteLength;
position = opt.position ?? null;
}
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 (buffer.byteLength == 0) {
throw new ERR_INVALID_ARG_VALUE(
"buffer",
buffer,
"is empty and cannot be written",
);
}
let err: Error | null = null,
numberOfBytesRead: number | null = null;
if (position) {
Deno.seekSync(fd, position, Deno.SeekMode.Current);
}
try {
numberOfBytesRead = Deno.readSync(fd, buffer);
} catch (error) {
err = error instanceof Error ? error : new Error("[non-error thrown]");
}
if (err) {
(callback as (err: Error) => void)(err);
} else {
const data = Buffer.from(buffer.buffer, offset, length);
cb(null, numberOfBytesRead, data);
}
return;
}
export function readSync(
fd: number,
buffer: Buffer | Uint8Array,
offset: number,
length: number,
position: number | null,
): number;
export function readSync(
fd: number,
buffer: Buffer | Uint8Array,
opt: readSyncOptions,
): number;
export function readSync(
fd: number,
buffer: Buffer | Uint8Array,
offsetOrOpt?: number | readSyncOptions,
length?: number,
position?: number | null,
): number {
let offset = 0;
if (length == null) {
length = 0;
}
if (buffer.byteLength == 0) {
throw new ERR_INVALID_ARG_VALUE(
"buffer",
buffer,
"is empty and cannot be written",
);
}
if (typeof offsetOrOpt === "number") {
offset = offsetOrOpt;
} else {
const opt = offsetOrOpt as readSyncOptions;
offset = opt.offset ?? 0;
length = opt.length ?? buffer.byteLength;
position = opt.position ?? null;
}
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 numberOfBytesRead = Deno.readSync(fd, buffer);
return numberOfBytesRead ?? 0;
}