deno.land / std@0.91.0 / node / _stream / readable_internal.ts

readable_internal.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright Node.js contributors. All rights reserved. MIT License.import { Buffer } from "../buffer.ts";import type Duplex from "./duplex.ts";import { EventEmitter } from "../events.ts";import type Readable from "./readable.ts";import type Writable from "./writable.ts";import type { ReadableState } from "./readable.ts";import { kPaused } from "./symbols.ts";import { ERR_STREAM_PUSH_AFTER_EOF, ERR_STREAM_UNSHIFT_AFTER_END_EVENT,} from "../_errors.ts";
export function _destroy( self: Readable, err?: Error | null, cb?: (error?: Error | null) => void,) { self._destroy(err || null, (err) => { const r = (self as Readable)._readableState;
if (err) { // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 err.stack;
if (!r.errored) { r.errored = err; } }
r.closed = true;
if (typeof cb === "function") { cb(err); }
if (err) { queueMicrotask(() => { if (!r.errorEmitted) { r.errorEmitted = true; self.emit("error", err); } r.closeEmitted = true; if (r.emitClose) { self.emit("close"); } }); } else { queueMicrotask(() => { r.closeEmitted = true; if (r.emitClose) { self.emit("close"); } }); } });}
export function addChunk( stream: Duplex | Readable, state: ReadableState, chunk: string | Buffer | Uint8Array, addToFront: boolean,) { if (state.flowing && state.length === 0 && !state.sync) { if (state.multiAwaitDrain) { (state.awaitDrainWriters as Set<Writable>).clear(); } else { state.awaitDrainWriters = null; } stream.emit("data", chunk); } else { // Update the buffer info. state.length += state.objectMode ? 1 : chunk.length; if (addToFront) { state.buffer.unshift(chunk); } else { state.buffer.push(chunk); }
if (state.needReadable) { emitReadable(stream); } } maybeReadMore(stream, state);}
// Don't raise the hwm > 1GB.const MAX_HWM = 0x40000000;export function computeNewHighWaterMark(n: number) { if (n >= MAX_HWM) { n = MAX_HWM; } else { n--; n |= n >>> 1; n |= n >>> 2; n |= n >>> 4; n |= n >>> 8; n |= n >>> 16; n++; } return n;}
export function emitReadable(stream: Duplex | Readable) { const state = stream._readableState; state.needReadable = false; if (!state.emittedReadable) { state.emittedReadable = true; queueMicrotask(() => emitReadable_(stream)); }}
function emitReadable_(stream: Duplex | Readable) { const state = stream._readableState; if (!state.destroyed && !state.errored && (state.length || state.ended)) { stream.emit("readable"); state.emittedReadable = false; }
state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark; flow(stream);}
export function endReadable(stream: Readable) { const state = stream._readableState;
if (!state.endEmitted) { state.ended = true; queueMicrotask(() => endReadableNT(state, stream)); }}
function endReadableNT(state: ReadableState, stream: Readable) { if ( !state.errorEmitted && !state.closeEmitted && !state.endEmitted && state.length === 0 ) { state.endEmitted = true; stream.emit("end");
if (state.autoDestroy) { stream.destroy(); } }}
export function errorOrDestroy( stream: Duplex | Readable, err: Error, sync = false,) { const r = stream._readableState;
if (r.destroyed) { return stream; }
if (r.autoDestroy) { stream.destroy(err); } else if (err) { // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 err.stack;
if (!r.errored) { r.errored = err; } if (sync) { queueMicrotask(() => { if (!r.errorEmitted) { r.errorEmitted = true; stream.emit("error", err); } }); } else if (!r.errorEmitted) { r.errorEmitted = true; stream.emit("error", err); } }}
function flow(stream: Duplex | Readable) { const state = stream._readableState; while (state.flowing && stream.read() !== null);}
/** Pluck off n bytes from an array of buffers.* Length is the combined lengths of all the buffers in the list.* This function is designed to be inlinable, so please take care when making* changes to the function body.*/export function fromList(n: number, state: ReadableState) { // nothing buffered. if (state.length === 0) { return null; }
let ret; if (state.objectMode) { ret = state.buffer.shift(); } else if (!n || n >= state.length) { if (state.decoder) { ret = state.buffer.join(""); } else if (state.buffer.length === 1) { ret = state.buffer.first(); } else { ret = state.buffer.concat(state.length); } state.buffer.clear(); } else { ret = state.buffer.consume(n, !!state.decoder); }
return ret;}
export function howMuchToRead(n: number, state: ReadableState) { if (n <= 0 || (state.length === 0 && state.ended)) { return 0; } if (state.objectMode) { return 1; } if (Number.isNaN(n)) { // Only flow one buffer at a time. if (state.flowing && state.length) { return state.buffer.first().length; } return state.length; } if (n <= state.length) { return n; } return state.ended ? state.length : 0;}
export function maybeReadMore(stream: Readable, state: ReadableState) { if (!state.readingMore && state.constructed) { state.readingMore = true; queueMicrotask(() => maybeReadMore_(stream, state)); }}
function maybeReadMore_(stream: Readable, state: ReadableState) { while ( !state.reading && !state.ended && (state.length < state.highWaterMark || (state.flowing && state.length === 0)) ) { const len = state.length; stream.read(0); if (len === state.length) { // Didn't get any data, stop spinning. break; } } state.readingMore = false;}
export function nReadingNextTick(self: Duplex | Readable) { self.read(0);}
export function onEofChunk(stream: Duplex | Readable, state: ReadableState) { if (state.ended) return; if (state.decoder) { const chunk = state.decoder.end(); if (chunk && chunk.length) { state.buffer.push(chunk); state.length += state.objectMode ? 1 : chunk.length; } } state.ended = true;
if (state.sync) { emitReadable(stream); } else { state.needReadable = false; state.emittedReadable = true; emitReadable_(stream); }}
export function pipeOnDrain(src: Duplex | Readable, dest: Duplex | Writable) { return function pipeOnDrainFunctionResult() { const state = src._readableState;
if (state.awaitDrainWriters === dest) { state.awaitDrainWriters = null; } else if (state.multiAwaitDrain) { (state.awaitDrainWriters as Set<Duplex | Writable>).delete(dest); }
if ( (!state.awaitDrainWriters || (state.awaitDrainWriters as Set<Writable>).size === 0) && src.listenerCount("data") ) { state.flowing = true; flow(src); } };}
export function prependListener( emitter: EventEmitter, event: string, // deno-lint-ignore no-explicit-any fn: (...args: any[]) => any,) { if (typeof emitter.prependListener === "function") { return emitter.prependListener(event, fn); }
// This is a hack to make sure that our error handler is attached before any // userland ones. NEVER DO THIS. This is here only because this code needs // to continue to work with older versions of Node.js that do not include //the prependListener() method. The goal is to eventually remove this hack. // TODO(Soremwar) // Burn it with fire // deno-lint-ignore ban-ts-comment //@ts-ignore if (emitter._events.get(event)?.length) { // deno-lint-ignore ban-ts-comment //@ts-ignore const listeners = [fn, ...emitter._events.get(event)]; // deno-lint-ignore ban-ts-comment //@ts-ignore emitter._events.set(event, listeners); } else { emitter.on(event, fn); }}
export function readableAddChunk( stream: Duplex | Readable, chunk: string | Buffer | Uint8Array | null, encoding: undefined | string = undefined, addToFront: boolean,) { const state = stream._readableState; let usedEncoding = encoding;
let err; if (!state.objectMode) { if (typeof chunk === "string") { usedEncoding = encoding || state.defaultEncoding; if (state.encoding !== usedEncoding) { if (addToFront && state.encoding) { chunk = Buffer.from(chunk, usedEncoding).toString(state.encoding); } else { chunk = Buffer.from(chunk, usedEncoding); usedEncoding = ""; } } } else if (chunk instanceof Uint8Array) { chunk = Buffer.from(chunk); } }
if (err) { errorOrDestroy(stream, err); } else if (chunk === null) { state.reading = false; onEofChunk(stream, state); } else if (state.objectMode || (chunk.length > 0)) { if (addToFront) { if (state.endEmitted) { errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT()); } else { addChunk(stream, state, chunk, true); } } else if (state.ended) { errorOrDestroy(stream, new ERR_STREAM_PUSH_AFTER_EOF()); } else if (state.destroyed || state.errored) { return false; } else { state.reading = false; if (state.decoder && !usedEncoding) { //TODO(Soremwar) //I don't think this cast is right chunk = state.decoder.write(Buffer.from(chunk as Uint8Array)); if (state.objectMode || chunk.length !== 0) { addChunk(stream, state, chunk, false); } else { maybeReadMore(stream, state); } } else { addChunk(stream, state, chunk, false); } } } else if (!addToFront) { state.reading = false; maybeReadMore(stream, state); }
return !state.ended && (state.length < state.highWaterMark || state.length === 0);}
export function resume(stream: Duplex | Readable, state: ReadableState) { if (!state.resumeScheduled) { state.resumeScheduled = true; queueMicrotask(() => resume_(stream, state)); }}
function resume_(stream: Duplex | Readable, state: ReadableState) { if (!state.reading) { stream.read(0); }
state.resumeScheduled = false; stream.emit("resume"); flow(stream); if (state.flowing && !state.reading) { stream.read(0); }}
export function updateReadableListening(self: Duplex | Readable) { const state = self._readableState; state.readableListening = self.listenerCount("readable") > 0;
if (state.resumeScheduled && state[kPaused] === false) { // Flowing needs to be set to true now, otherwise // the upcoming resume will not flow. state.flowing = true;
// Crude way to check if we should resume. } else if (self.listenerCount("data") > 0) { self.resume(); } else if (!state.readableListening) { state.flowing = null; }}
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉