deno.land / x / deno@v1.28.2 / runtime / js / 12_io.js

نووسراو ببینە
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Interfaces 100% copied from Go.// Documentation liberally lifted from them too.// Thank you! We love Go! <3"use strict";
((window) => { const core = window.Deno.core; const ops = core.ops; const { Uint8Array, ArrayPrototypePush, MathMin, TypedArrayPrototypeSubarray, TypedArrayPrototypeSet, } = window.__bootstrap.primordials;
const DEFAULT_BUFFER_SIZE = 32 * 1024; // Seek whence values. // https://golang.org/pkg/io/#pkg-constants const SeekMode = { 0: "Start", 1: "Current", 2: "End",
Start: 0, Current: 1, End: 2, };
async function copy( src, dst, options, ) { let n = 0; const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE; const b = new Uint8Array(bufSize); let gotEOF = false; while (gotEOF === false) { const result = await src.read(b); if (result === null) { gotEOF = true; } else { let nwritten = 0; while (nwritten < result) { nwritten += await dst.write( TypedArrayPrototypeSubarray(b, nwritten, result), ); } n += nwritten; } } return n; }
async function* iter( r, options, ) { const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE; const b = new Uint8Array(bufSize); while (true) { const result = await r.read(b); if (result === null) { break; }
yield TypedArrayPrototypeSubarray(b, 0, result); } }
function* iterSync( r, options, ) { const bufSize = options?.bufSize ?? DEFAULT_BUFFER_SIZE; const b = new Uint8Array(bufSize); while (true) { const result = r.readSync(b); if (result === null) { break; }
yield TypedArrayPrototypeSubarray(b, 0, result); } }
function readSync(rid, buffer) { if (buffer.length === 0) { return 0; }
const nread = ops.op_read_sync(rid, buffer);
return nread === 0 ? null : nread; }
async function read(rid, buffer) { if (buffer.length === 0) { return 0; }
const nread = await core.read(rid, buffer);
return nread === 0 ? null : nread; }
function writeSync(rid, data) { return ops.op_write_sync(rid, data); }
function write(rid, data) { return core.write(rid, data); }
const READ_PER_ITER = 64 * 1024; // 64kb
function readAll(r) { return readAllInner(r); } async function readAllInner(r, options) { const buffers = []; const signal = options?.signal ?? null; while (true) { signal?.throwIfAborted(); const buf = new Uint8Array(READ_PER_ITER); const read = await r.read(buf); if (typeof read == "number") { ArrayPrototypePush(buffers, new Uint8Array(buf.buffer, 0, read)); } else { break; } } signal?.throwIfAborted();
return concatBuffers(buffers); }
function readAllSync(r) { const buffers = [];
while (true) { const buf = new Uint8Array(READ_PER_ITER); const read = r.readSync(buf); if (typeof read == "number") { ArrayPrototypePush(buffers, TypedArrayPrototypeSubarray(buf, 0, read)); } else { break; } }
return concatBuffers(buffers); }
function concatBuffers(buffers) { let totalLen = 0; for (const buf of buffers) { totalLen += buf.byteLength; }
const contents = new Uint8Array(totalLen);
let n = 0; for (const buf of buffers) { TypedArrayPrototypeSet(contents, buf, n); n += buf.byteLength; }
return contents; }
function readAllSyncSized(r, size) { const buf = new Uint8Array(size + 1); // 1B to detect extended files let cursor = 0;
while (cursor < size) { const sliceEnd = MathMin(size + 1, cursor + READ_PER_ITER); const slice = TypedArrayPrototypeSubarray(buf, cursor, sliceEnd); const read = r.readSync(slice); if (typeof read == "number") { cursor += read; } else { break; } }
// Handle truncated or extended files during read if (cursor > size) { // Read remaining and concat return concatBuffers([buf, readAllSync(r)]); } else { // cursor == size return TypedArrayPrototypeSubarray(buf, 0, cursor); } }
async function readAllInnerSized(r, size, options) { const buf = new Uint8Array(size + 1); // 1B to detect extended files let cursor = 0; const signal = options?.signal ?? null; while (cursor < size) { signal?.throwIfAborted(); const sliceEnd = MathMin(size + 1, cursor + READ_PER_ITER); const slice = TypedArrayPrototypeSubarray(buf, cursor, sliceEnd); const read = await r.read(slice); if (typeof read == "number") { cursor += read; } else { break; } } signal?.throwIfAborted();
// Handle truncated or extended files during read if (cursor > size) { // Read remaining and concat return concatBuffers([buf, await readAllInner(r, options)]); } else { return TypedArrayPrototypeSubarray(buf, 0, cursor); } }
window.__bootstrap.io = { iterSync, iter, copy, SeekMode, read, readSync, write, writeSync, readAll, readAllInner, readAllSync, readAllSyncSized, readAllInnerSized, };})(this);
deno

Version Info

Tagged at
a year ago