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

duplex_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
// Copyright Node.js contributors. All rights reserved. MIT License.import type { ReadableState } from "./readable.ts";import { addChunk, maybeReadMore, onEofChunk } from "./readable_internal.ts";import type Writable from "./writable.ts";import type { WritableState } from "./writable.ts";import { afterWrite, AfterWriteTick, afterWriteTick, clearBuffer, errorBuffer, kOnFinished, needFinish, prefinish,} from "./writable_internal.ts";import { Buffer } from "../buffer.ts";import type Duplex from "./duplex.ts";import { ERR_MULTIPLE_CALLBACK, ERR_STREAM_PUSH_AFTER_EOF, ERR_STREAM_UNSHIFT_AFTER_END_EVENT,} from "../_errors.ts";
export function endDuplex(stream: Duplex) { const state = stream._readableState;
if (!state.endEmitted) { state.ended = true; queueMicrotask(() => endReadableNT(state, stream)); }}
function endReadableNT(state: ReadableState, stream: Duplex) { // Check that we didn't get one last unshift. if ( !state.errorEmitted && !state.closeEmitted && !state.endEmitted && state.length === 0 ) { state.endEmitted = true; stream.emit("end");
if (stream.writable && stream.allowHalfOpen === false) { queueMicrotask(() => endWritableNT(state, stream)); } else if (state.autoDestroy) { // In case of duplex streams we need a way to detect // if the writable side is ready for autoDestroy as well. const wState = stream._writableState; const autoDestroy = !wState || ( wState.autoDestroy && // We don't expect the writable to ever 'finish' // if writable is explicitly set to false. (wState.finished || wState.writable === false) );
if (autoDestroy) { stream.destroy(); } } }}
function endWritableNT(_state: ReadableState, stream: Duplex) { const writable = stream.writable && !stream.writableEnded && !stream.destroyed; if (writable) { stream.end(); }}
export function errorOrDestroy( // deno-lint-ignore no-explicit-any this: any, stream: Duplex, err: Error, sync = false,) { const r = stream._readableState; const w = stream._writableState;
if (w.destroyed || r.destroyed) { return this; }
if (r.autoDestroy || w.autoDestroy) { stream.destroy(err); } else if (err) { // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 err.stack;
if (w && !w.errored) { w.errored = err; } if (r && !r.errored) { r.errored = err; }
if (sync) { queueMicrotask(() => { if (w.errorEmitted || r.errorEmitted) { return; }
w.errorEmitted = true; r.errorEmitted = true;
stream.emit("error", err); }); } else { if (w.errorEmitted || r.errorEmitted) { return; }
w.errorEmitted = true; r.errorEmitted = true;
stream.emit("error", err); } }}
function finish(stream: Duplex, state: WritableState) { state.pendingcb--; if (state.errorEmitted || state.closeEmitted) { return; }
state.finished = true;
for (const callback of state[kOnFinished].splice(0)) { callback(); }
stream.emit("finish");
if (state.autoDestroy) { stream.destroy(); }}
export function finishMaybe( stream: Duplex, state: WritableState, sync?: boolean,) { if (needFinish(state)) { prefinish(stream as Writable, state); if (state.pendingcb === 0 && needFinish(state)) { state.pendingcb++; if (sync) { queueMicrotask(() => finish(stream, state)); } else { finish(stream, state); } } }}
export function onwrite(stream: Duplex, er?: Error | null) { const state = stream._writableState; const sync = state.sync; const cb = state.writecb;
if (typeof cb !== "function") { errorOrDestroy(stream, new ERR_MULTIPLE_CALLBACK()); return; }
state.writing = false; state.writecb = null; state.length -= state.writelen; state.writelen = 0;
if (er) { // Avoid V8 leak, https://github.com/nodejs/node/pull/34103#issuecomment-652002364 er.stack;
if (!state.errored) { state.errored = er; }
if (stream._readableState && !stream._readableState.errored) { stream._readableState.errored = er; }
if (sync) { queueMicrotask(() => onwriteError(stream, state, er, cb)); } else { onwriteError(stream, state, er, cb); } } else { if (state.buffered.length > state.bufferedIndex) { clearBuffer(stream, state); }
if (sync) { if ( state.afterWriteTickInfo !== null && state.afterWriteTickInfo.cb === cb ) { state.afterWriteTickInfo.count++; } else { state.afterWriteTickInfo = { count: 1, cb: (cb as (error?: Error) => void), stream: stream as Writable, state, }; queueMicrotask(() => afterWriteTick(state.afterWriteTickInfo as AfterWriteTick) ); } } else { afterWrite(stream as Writable, state, 1, cb as (error?: Error) => void); } }}
function onwriteError( stream: Duplex, state: WritableState, er: Error, cb: (error: Error) => void,) { --state.pendingcb;
cb(er); errorBuffer(state); errorOrDestroy(stream, er);}
export function readableAddChunk( stream: Duplex, 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);}
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉