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

end_of_stream.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
// Copyright Node.js contributors. All rights reserved. MIT License.import { once } from "../_utils.ts";import type Duplex from "./duplex.ts";import type Readable from "./readable.ts";import type Stream from "./stream.ts";import type { ReadableState } from "./readable.ts";import type Writable from "./writable.ts";import type { WritableState } from "./writable.ts";import { ERR_INVALID_ARG_TYPE, ERR_STREAM_PREMATURE_CLOSE, NodeErrorAbstraction,} from "../_errors.ts";
export type StreamImplementations = Duplex | Readable | Stream | Writable;
// TODO(Soremwar)// Bring back once requests are implemented// function isRequest(stream: Stream) {// return stream.setHeader && typeof stream.abort === "function";// }
// deno-lint-ignore no-explicit-anyfunction isReadable(stream: any) { return typeof stream.readable === "boolean" || typeof stream.readableEnded === "boolean" || !!stream._readableState;}
// deno-lint-ignore no-explicit-anyfunction isWritable(stream: any) { return typeof stream.writable === "boolean" || typeof stream.writableEnded === "boolean" || !!stream._writableState;}
function isWritableFinished(stream: Writable) { if (stream.writableFinished) return true; const wState = stream._writableState; if (!wState || wState.errored) return false; return wState.finished || (wState.ended && wState.length === 0);}
function nop() {}
function isReadableEnded(stream: Readable) { if (stream.readableEnded) return true; const rState = stream._readableState; if (!rState || rState.errored) return false; return rState.endEmitted || (rState.ended && rState.length === 0);}
export interface FinishedOptions { error?: boolean; readable?: boolean; writable?: boolean;}
/** * Appends an ending callback triggered when a stream is no longer readable, * writable or has experienced an error or a premature close event*/export default function eos( stream: StreamImplementations, options: FinishedOptions | null, callback: (err?: NodeErrorAbstraction | null) => void,): () => void;export default function eos( stream: StreamImplementations, callback: (err?: NodeErrorAbstraction | null) => void,): () => void;export default function eos( stream: StreamImplementations, x: FinishedOptions | ((err?: NodeErrorAbstraction | null) => void) | null, y?: (err?: NodeErrorAbstraction | null) => void,) { let opts: FinishedOptions; let callback: (err?: NodeErrorAbstraction | null) => void;
if (!y) { if (typeof x !== "function") { throw new ERR_INVALID_ARG_TYPE("callback", "function", x); } opts = {}; callback = x; } else { if (!x || Array.isArray(x) || typeof x !== "object") { throw new ERR_INVALID_ARG_TYPE("opts", "object", x); } opts = x;
if (typeof y !== "function") { throw new ERR_INVALID_ARG_TYPE("callback", "function", y); } callback = y; }
callback = once(callback);
const readable = opts.readable ?? isReadable(stream); const writable = opts.writable ?? isWritable(stream);
// deno-lint-ignore no-explicit-any const wState: WritableState | undefined = (stream as any)._writableState; // deno-lint-ignore no-explicit-any const rState: ReadableState | undefined = (stream as any)._readableState; const validState = wState || rState;
const onlegacyfinish = () => { if (!(stream as Writable).writable) { onfinish(); } };
let willEmitClose = ( validState?.autoDestroy && validState?.emitClose && validState?.closed === false && isReadable(stream) === readable && isWritable(stream) === writable );
let writableFinished = (stream as Writable).writableFinished || wState?.finished; const onfinish = () => { writableFinished = true; // deno-lint-ignore no-explicit-any if ((stream as any).destroyed) { willEmitClose = false; }
if (willEmitClose && (!(stream as Readable).readable || readable)) { return; } if (!readable || readableEnded) { callback.call(stream); } };
let readableEnded = (stream as Readable).readableEnded || rState?.endEmitted; const onend = () => { readableEnded = true; // deno-lint-ignore no-explicit-any if ((stream as any).destroyed) { willEmitClose = false; }
if (willEmitClose && (!(stream as Writable).writable || writable)) { return; } if (!writable || writableFinished) { callback.call(stream); } };
const onerror = (err: NodeErrorAbstraction) => { callback.call(stream, err); };
const onclose = () => { if (readable && !readableEnded) { if (!isReadableEnded(stream as Readable)) { return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE()); } } if (writable && !writableFinished) { if (!isWritableFinished(stream as Writable)) { return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE()); } } callback.call(stream); };
// TODO(Soremwar) // Bring back once requests are implemented // const onrequest = () => { // stream.req.on("finish", onfinish); // };
// TODO(Soremwar) // Bring back once requests are implemented // if (isRequest(stream)) { // stream.on("complete", onfinish); // stream.on("abort", onclose); // if (stream.req) { // onrequest(); // } else { // stream.on("request", onrequest); // } // } else if (writable && !wState) { stream.on("end", onlegacyfinish); stream.on("close", onlegacyfinish); }
// TODO(Soremwar) // Bring back once requests are implemented // if (typeof stream.aborted === "boolean") { // stream.on("aborted", onclose); // }
stream.on("end", onend); stream.on("finish", onfinish); if (opts.error !== false) stream.on("error", onerror); stream.on("close", onclose);
const closed = ( wState?.closed || rState?.closed || wState?.errorEmitted || rState?.errorEmitted || // TODO(Soremwar) // Bring back once requests are implemented // (rState && stream.req && stream.aborted) || ( (!writable || wState?.finished) && (!readable || rState?.endEmitted) ) );
if (closed) { queueMicrotask(callback); }
return function () { callback = nop; stream.removeListener("aborted", onclose); stream.removeListener("complete", onfinish); stream.removeListener("abort", onclose); // TODO(Soremwar) // Bring back once requests are implemented // stream.removeListener("request", onrequest); // if (stream.req) stream.req.removeListener("finish", onfinish); stream.removeListener("end", onlegacyfinish); stream.removeListener("close", onlegacyfinish); stream.removeListener("finish", onfinish); stream.removeListener("end", onend); stream.removeListener("error", onerror); stream.removeListener("close", onclose); };}
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉