m06_task3.js 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/.idea/.name +1 -0
- package/.idea/inspectionProfiles/Project_Default.xml +10 -0
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/node_lab_6.iml +12 -0
- package/06_task2.html +73 -0
- package/06_task2.js +52 -0
- package/06_task3.js +3 -0
- package/06_task4.js +3 -0
- package/m06_task3.js +28 -0
- package/package/index.js +28 -0
- package/package/node_modules/.package-lock.json +31 -0
- package/package/node_modules/@types/node/LICENSE +21 -0
- package/package/node_modules/@types/node/README.md +16 -0
- package/package/node_modules/@types/node/assert/strict.d.ts +8 -0
- package/package/node_modules/@types/node/assert.d.ts +961 -0
- package/package/node_modules/@types/node/async_hooks.d.ts +501 -0
- package/package/node_modules/@types/node/buffer.d.ts +2258 -0
- package/package/node_modules/@types/node/child_process.d.ts +1369 -0
- package/package/node_modules/@types/node/cluster.d.ts +410 -0
- package/package/node_modules/@types/node/console.d.ts +412 -0
- package/package/node_modules/@types/node/constants.d.ts +18 -0
- package/package/node_modules/@types/node/crypto.d.ts +3964 -0
- package/package/node_modules/@types/node/dgram.d.ts +545 -0
- package/package/node_modules/@types/node/diagnostics_channel.d.ts +153 -0
- package/package/node_modules/@types/node/dns/promises.d.ts +370 -0
- package/package/node_modules/@types/node/dns.d.ts +659 -0
- package/package/node_modules/@types/node/dom-events.d.ts +126 -0
- package/package/node_modules/@types/node/domain.d.ts +170 -0
- package/package/node_modules/@types/node/events.d.ts +678 -0
- package/package/node_modules/@types/node/fs/promises.d.ts +1138 -0
- package/package/node_modules/@types/node/fs.d.ts +3872 -0
- package/package/node_modules/@types/node/globals.d.ts +300 -0
- package/package/node_modules/@types/node/globals.global.d.ts +1 -0
- package/package/node_modules/@types/node/http.d.ts +1614 -0
- package/package/node_modules/@types/node/http2.d.ts +2134 -0
- package/package/node_modules/@types/node/https.d.ts +541 -0
- package/package/node_modules/@types/node/index.d.ts +133 -0
- package/package/node_modules/@types/node/inspector.d.ts +2741 -0
- package/package/node_modules/@types/node/module.d.ts +114 -0
- package/package/node_modules/@types/node/net.d.ts +869 -0
- package/package/node_modules/@types/node/os.d.ts +466 -0
- package/package/node_modules/@types/node/package.json +232 -0
- package/package/node_modules/@types/node/path.d.ts +191 -0
- package/package/node_modules/@types/node/perf_hooks.d.ts +625 -0
- package/package/node_modules/@types/node/process.d.ts +1482 -0
- package/package/node_modules/@types/node/punycode.d.ts +117 -0
- package/package/node_modules/@types/node/querystring.d.ts +131 -0
- package/package/node_modules/@types/node/readline/promises.d.ts +143 -0
- package/package/node_modules/@types/node/readline.d.ts +653 -0
- package/package/node_modules/@types/node/repl.d.ts +424 -0
- package/package/node_modules/@types/node/stream/consumers.d.ts +12 -0
- package/package/node_modules/@types/node/stream/promises.d.ts +42 -0
- package/package/node_modules/@types/node/stream/web.d.ts +330 -0
- package/package/node_modules/@types/node/stream.d.ts +1340 -0
- package/package/node_modules/@types/node/string_decoder.d.ts +67 -0
- package/package/node_modules/@types/node/test.d.ts +314 -0
- package/package/node_modules/@types/node/timers/promises.d.ts +68 -0
- package/package/node_modules/@types/node/timers.d.ts +94 -0
- package/package/node_modules/@types/node/tls.d.ts +1028 -0
- package/package/node_modules/@types/node/trace_events.d.ts +171 -0
- package/package/node_modules/@types/node/ts4.8/assert/strict.d.ts +8 -0
- package/package/node_modules/@types/node/ts4.8/assert.d.ts +961 -0
- package/package/node_modules/@types/node/ts4.8/async_hooks.d.ts +501 -0
- package/package/node_modules/@types/node/ts4.8/buffer.d.ts +2259 -0
- package/package/node_modules/@types/node/ts4.8/child_process.d.ts +1369 -0
- package/package/node_modules/@types/node/ts4.8/cluster.d.ts +410 -0
- package/package/node_modules/@types/node/ts4.8/console.d.ts +412 -0
- package/package/node_modules/@types/node/ts4.8/constants.d.ts +18 -0
- package/package/node_modules/@types/node/ts4.8/crypto.d.ts +3964 -0
- package/package/node_modules/@types/node/ts4.8/dgram.d.ts +545 -0
- package/package/node_modules/@types/node/ts4.8/diagnostics_channel.d.ts +153 -0
- package/package/node_modules/@types/node/ts4.8/dns/promises.d.ts +370 -0
- package/package/node_modules/@types/node/ts4.8/dns.d.ts +659 -0
- package/package/node_modules/@types/node/ts4.8/dom-events.d.ts +126 -0
- package/package/node_modules/@types/node/ts4.8/domain.d.ts +170 -0
- package/package/node_modules/@types/node/ts4.8/events.d.ts +678 -0
- package/package/node_modules/@types/node/ts4.8/fs/promises.d.ts +1138 -0
- package/package/node_modules/@types/node/ts4.8/fs.d.ts +3872 -0
- package/package/node_modules/@types/node/ts4.8/globals.d.ts +294 -0
- package/package/node_modules/@types/node/ts4.8/globals.global.d.ts +1 -0
- package/package/node_modules/@types/node/ts4.8/http.d.ts +1614 -0
- package/package/node_modules/@types/node/ts4.8/http2.d.ts +2134 -0
- package/package/node_modules/@types/node/ts4.8/https.d.ts +541 -0
- package/package/node_modules/@types/node/ts4.8/index.d.ts +88 -0
- package/package/node_modules/@types/node/ts4.8/inspector.d.ts +2741 -0
- package/package/node_modules/@types/node/ts4.8/module.d.ts +114 -0
- package/package/node_modules/@types/node/ts4.8/net.d.ts +869 -0
- package/package/node_modules/@types/node/ts4.8/os.d.ts +466 -0
- package/package/node_modules/@types/node/ts4.8/path.d.ts +191 -0
- package/package/node_modules/@types/node/ts4.8/perf_hooks.d.ts +625 -0
- package/package/node_modules/@types/node/ts4.8/process.d.ts +1482 -0
- package/package/node_modules/@types/node/ts4.8/punycode.d.ts +117 -0
- package/package/node_modules/@types/node/ts4.8/querystring.d.ts +131 -0
- package/package/node_modules/@types/node/ts4.8/readline/promises.d.ts +143 -0
- package/package/node_modules/@types/node/ts4.8/readline.d.ts +653 -0
- package/package/node_modules/@types/node/ts4.8/repl.d.ts +424 -0
- package/package/node_modules/@types/node/ts4.8/stream/consumers.d.ts +12 -0
- package/package/node_modules/@types/node/ts4.8/stream/promises.d.ts +42 -0
- package/package/node_modules/@types/node/ts4.8/stream/web.d.ts +330 -0
- package/package/node_modules/@types/node/ts4.8/stream.d.ts +1340 -0
- package/package/node_modules/@types/node/ts4.8/string_decoder.d.ts +67 -0
- package/package/node_modules/@types/node/ts4.8/test.d.ts +314 -0
- package/package/node_modules/@types/node/ts4.8/timers/promises.d.ts +68 -0
- package/package/node_modules/@types/node/ts4.8/timers.d.ts +94 -0
- package/package/node_modules/@types/node/ts4.8/tls.d.ts +1028 -0
- package/package/node_modules/@types/node/ts4.8/trace_events.d.ts +171 -0
- package/package/node_modules/@types/node/ts4.8/tty.d.ts +206 -0
- package/package/node_modules/@types/node/ts4.8/url.d.ts +897 -0
- package/package/node_modules/@types/node/ts4.8/util.d.ts +1850 -0
- package/package/node_modules/@types/node/ts4.8/v8.d.ts +396 -0
- package/package/node_modules/@types/node/ts4.8/vm.d.ts +509 -0
- package/package/node_modules/@types/node/ts4.8/wasi.d.ts +158 -0
- package/package/node_modules/@types/node/ts4.8/worker_threads.d.ts +689 -0
- package/package/node_modules/@types/node/ts4.8/zlib.d.ts +517 -0
- package/package/node_modules/@types/node/tty.d.ts +206 -0
- package/package/node_modules/@types/node/url.d.ts +897 -0
- package/package/node_modules/@types/node/util.d.ts +1850 -0
- package/package/node_modules/@types/node/v8.d.ts +396 -0
- package/package/node_modules/@types/node/vm.d.ts +509 -0
- package/package/node_modules/@types/node/wasi.d.ts +158 -0
- package/package/node_modules/@types/node/worker_threads.d.ts +689 -0
- package/package/node_modules/@types/node/zlib.d.ts +517 -0
- package/package/node_modules/@types/nodemailer/LICENSE +21 -0
- package/package/node_modules/@types/nodemailer/README.md +16 -0
- package/package/node_modules/@types/nodemailer/index.d.ts +83 -0
- package/package/node_modules/@types/nodemailer/lib/addressparser/index.d.ts +31 -0
- package/package/node_modules/@types/nodemailer/lib/base64/index.d.ts +22 -0
- package/package/node_modules/@types/nodemailer/lib/dkim/index.d.ts +45 -0
- package/package/node_modules/@types/nodemailer/lib/dkim/message-parser.d.ts +75 -0
- package/package/node_modules/@types/nodemailer/lib/dkim/relaxed-body.d.ts +75 -0
- package/package/node_modules/@types/nodemailer/lib/dkim/sign.d.ts +11 -0
- package/package/node_modules/@types/nodemailer/lib/fetch/cookies.d.ts +54 -0
- package/package/node_modules/@types/nodemailer/lib/fetch/index.d.ts +38 -0
- package/package/node_modules/@types/nodemailer/lib/json-transport/index.d.ts +50 -0
- package/package/node_modules/@types/nodemailer/lib/mail-composer/index.d.ts +25 -0
- package/package/node_modules/@types/nodemailer/lib/mailer/index.d.ts +230 -0
- package/package/node_modules/@types/nodemailer/lib/mailer/mail-message.d.ts +28 -0
- package/package/node_modules/@types/nodemailer/lib/mime-funcs/index.d.ts +87 -0
- package/package/node_modules/@types/nodemailer/lib/mime-funcs/mime-types.d.ts +2 -0
- package/package/node_modules/@types/nodemailer/lib/mime-node/index.d.ts +137 -0
- package/package/node_modules/@types/nodemailer/lib/mime-node/last-newline.d.ts +9 -0
- package/package/node_modules/@types/nodemailer/lib/qp/index.d.ts +23 -0
- package/package/node_modules/@types/nodemailer/lib/sendmail-transport/index.d.ts +53 -0
- package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-unix.d.ts +7 -0
- package/package/node_modules/@types/nodemailer/lib/sendmail-transport/le-windows.d.ts +7 -0
- package/package/node_modules/@types/nodemailer/lib/ses-transport/index.d.ts +136 -0
- package/package/node_modules/@types/nodemailer/lib/shared/index.d.ts +51 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-connection/data-stream.d.ts +11 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-connection/http-proxy-client.d.ts +11 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-connection/index.d.ts +254 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-pool/index.d.ts +90 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-pool/pool-resource.d.ts +66 -0
- package/package/node_modules/@types/nodemailer/lib/smtp-transport/index.d.ts +115 -0
- package/package/node_modules/@types/nodemailer/lib/stream-transport/index.d.ts +56 -0
- package/package/node_modules/@types/nodemailer/lib/well-known/index.d.ts +6 -0
- package/package/node_modules/@types/nodemailer/lib/xoauth2/index.d.ts +110 -0
- package/package/node_modules/@types/nodemailer/package.json +37 -0
- package/package/node_modules/nodemailer/.gitattributes +6 -0
- package/package/node_modules/nodemailer/.prettierrc.js +8 -0
- package/package/node_modules/nodemailer/CHANGELOG.md +715 -0
- package/package/node_modules/nodemailer/CODE_OF_CONDUCT.md +76 -0
- package/package/node_modules/nodemailer/CONTRIBUTING.md +67 -0
- package/package/node_modules/nodemailer/LICENSE +16 -0
- package/package/node_modules/nodemailer/README.md +93 -0
- package/package/node_modules/nodemailer/SECURITY.txt +22 -0
- package/package/node_modules/nodemailer/lib/addressparser/index.js +313 -0
- package/package/node_modules/nodemailer/lib/base64/index.js +142 -0
- package/package/node_modules/nodemailer/lib/dkim/index.js +251 -0
- package/package/node_modules/nodemailer/lib/dkim/message-parser.js +155 -0
- package/package/node_modules/nodemailer/lib/dkim/relaxed-body.js +154 -0
- package/package/node_modules/nodemailer/lib/dkim/sign.js +117 -0
- package/package/node_modules/nodemailer/lib/fetch/cookies.js +281 -0
- package/package/node_modules/nodemailer/lib/fetch/index.js +269 -0
- package/package/node_modules/nodemailer/lib/json-transport/index.js +82 -0
- package/package/node_modules/nodemailer/lib/mail-composer/index.js +558 -0
- package/package/node_modules/nodemailer/lib/mailer/index.js +427 -0
- package/package/node_modules/nodemailer/lib/mailer/mail-message.js +315 -0
- package/package/node_modules/nodemailer/lib/mime-funcs/index.js +619 -0
- package/package/node_modules/nodemailer/lib/mime-funcs/mime-types.js +2102 -0
- package/package/node_modules/nodemailer/lib/mime-node/index.js +1290 -0
- package/package/node_modules/nodemailer/lib/mime-node/last-newline.js +33 -0
- package/package/node_modules/nodemailer/lib/mime-node/le-unix.js +43 -0
- package/package/node_modules/nodemailer/lib/mime-node/le-windows.js +52 -0
- package/package/node_modules/nodemailer/lib/nodemailer.js +143 -0
- package/package/node_modules/nodemailer/lib/qp/index.js +219 -0
- package/package/node_modules/nodemailer/lib/sendmail-transport/index.js +210 -0
- package/package/node_modules/nodemailer/lib/ses-transport/index.js +349 -0
- package/package/node_modules/nodemailer/lib/shared/index.js +639 -0
- package/package/node_modules/nodemailer/lib/smtp-connection/data-stream.js +108 -0
- package/package/node_modules/nodemailer/lib/smtp-connection/http-proxy-client.js +143 -0
- package/package/node_modules/nodemailer/lib/smtp-connection/index.js +1786 -0
- package/package/node_modules/nodemailer/lib/smtp-pool/index.js +648 -0
- package/package/node_modules/nodemailer/lib/smtp-pool/pool-resource.js +253 -0
- package/package/node_modules/nodemailer/lib/smtp-transport/index.js +416 -0
- package/package/node_modules/nodemailer/lib/stream-transport/index.js +135 -0
- package/package/node_modules/nodemailer/lib/well-known/index.js +47 -0
- package/package/node_modules/nodemailer/lib/well-known/services.json +286 -0
- package/package/node_modules/nodemailer/lib/xoauth2/index.js +376 -0
- package/package/node_modules/nodemailer/package.json +46 -0
- package/package/node_modules/nodemailer/postinstall.js +101 -0
- package/package/package-lock.json +60 -0
- package/package/package.json +6 -0
- package/package.json +19 -0
@@ -0,0 +1,1340 @@
|
|
1
|
+
/**
|
2
|
+
* A stream is an abstract interface for working with streaming data in Node.js.
|
3
|
+
* The `stream` module provides an API for implementing the stream interface.
|
4
|
+
*
|
5
|
+
* There are many stream objects provided by Node.js. For instance, a `request to an HTTP server` and `process.stdout` are both stream instances.
|
6
|
+
*
|
7
|
+
* Streams can be readable, writable, or both. All streams are instances of `EventEmitter`.
|
8
|
+
*
|
9
|
+
* To access the `stream` module:
|
10
|
+
*
|
11
|
+
* ```js
|
12
|
+
* const stream = require('stream');
|
13
|
+
* ```
|
14
|
+
*
|
15
|
+
* The `stream` module is useful for creating new types of stream instances. It is
|
16
|
+
* usually not necessary to use the `stream` module to consume streams.
|
17
|
+
* @see [source](https://github.com/nodejs/node/blob/v18.0.0/lib/stream.js)
|
18
|
+
*/
|
19
|
+
declare module 'stream' {
|
20
|
+
import { EventEmitter, Abortable } from 'node:events';
|
21
|
+
import { Blob as NodeBlob } from "node:buffer";
|
22
|
+
import * as streamPromises from 'node:stream/promises';
|
23
|
+
import * as streamConsumers from 'node:stream/consumers';
|
24
|
+
import * as streamWeb from 'node:stream/web';
|
25
|
+
class internal extends EventEmitter {
|
26
|
+
pipe<T extends NodeJS.WritableStream>(
|
27
|
+
destination: T,
|
28
|
+
options?: {
|
29
|
+
end?: boolean | undefined;
|
30
|
+
}
|
31
|
+
): T;
|
32
|
+
}
|
33
|
+
namespace internal {
|
34
|
+
class Stream extends internal {
|
35
|
+
constructor(opts?: ReadableOptions);
|
36
|
+
}
|
37
|
+
interface StreamOptions<T extends Stream> extends Abortable {
|
38
|
+
emitClose?: boolean | undefined;
|
39
|
+
highWaterMark?: number | undefined;
|
40
|
+
objectMode?: boolean | undefined;
|
41
|
+
construct?(this: T, callback: (error?: Error | null) => void): void;
|
42
|
+
destroy?(this: T, error: Error | null, callback: (error: Error | null) => void): void;
|
43
|
+
autoDestroy?: boolean | undefined;
|
44
|
+
}
|
45
|
+
interface ReadableOptions extends StreamOptions<Readable> {
|
46
|
+
encoding?: BufferEncoding | undefined;
|
47
|
+
read?(this: Readable, size: number): void;
|
48
|
+
}
|
49
|
+
/**
|
50
|
+
* @since v0.9.4
|
51
|
+
*/
|
52
|
+
class Readable extends Stream implements NodeJS.ReadableStream {
|
53
|
+
/**
|
54
|
+
* A utility method for creating Readable Streams out of iterators.
|
55
|
+
*/
|
56
|
+
static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
|
57
|
+
/**
|
58
|
+
* A utility method for creating a `Readable` from a web `ReadableStream`.
|
59
|
+
* @since v17.0.0
|
60
|
+
* @experimental
|
61
|
+
*/
|
62
|
+
static fromWeb(readableStream: streamWeb.ReadableStream, options?: Pick<ReadableOptions, 'encoding' | 'highWaterMark' | 'objectMode' | 'signal'>): Readable;
|
63
|
+
/**
|
64
|
+
* Returns whether the stream has been read from or cancelled.
|
65
|
+
* @since v16.8.0
|
66
|
+
*/
|
67
|
+
static isDisturbed(stream: Readable | NodeJS.ReadableStream): boolean;
|
68
|
+
/**
|
69
|
+
* A utility method for creating a web `ReadableStream` from a `Readable`.
|
70
|
+
* @since v17.0.0
|
71
|
+
* @experimental
|
72
|
+
*/
|
73
|
+
static toWeb(streamReadable: Readable): streamWeb.ReadableStream;
|
74
|
+
/**
|
75
|
+
* Returns whether the stream was destroyed or errored before emitting `'end'`.
|
76
|
+
* @since v16.8.0
|
77
|
+
* @experimental
|
78
|
+
*/
|
79
|
+
readonly readableAborted: boolean;
|
80
|
+
/**
|
81
|
+
* Is `true` if it is safe to call `readable.read()`, which means
|
82
|
+
* the stream has not been destroyed or emitted `'error'` or `'end'`.
|
83
|
+
* @since v11.4.0
|
84
|
+
*/
|
85
|
+
readable: boolean;
|
86
|
+
/**
|
87
|
+
* Returns whether `'data'` has been emitted.
|
88
|
+
* @since v16.7.0, v14.18.0
|
89
|
+
* @experimental
|
90
|
+
*/
|
91
|
+
readonly readableDidRead: boolean;
|
92
|
+
/**
|
93
|
+
* Getter for the property `encoding` of a given `Readable` stream. The `encoding`property can be set using the `readable.setEncoding()` method.
|
94
|
+
* @since v12.7.0
|
95
|
+
*/
|
96
|
+
readonly readableEncoding: BufferEncoding | null;
|
97
|
+
/**
|
98
|
+
* Becomes `true` when `'end'` event is emitted.
|
99
|
+
* @since v12.9.0
|
100
|
+
*/
|
101
|
+
readonly readableEnded: boolean;
|
102
|
+
/**
|
103
|
+
* This property reflects the current state of a `Readable` stream as described
|
104
|
+
* in the `Three states` section.
|
105
|
+
* @since v9.4.0
|
106
|
+
*/
|
107
|
+
readonly readableFlowing: boolean | null;
|
108
|
+
/**
|
109
|
+
* Returns the value of `highWaterMark` passed when creating this `Readable`.
|
110
|
+
* @since v9.3.0
|
111
|
+
*/
|
112
|
+
readonly readableHighWaterMark: number;
|
113
|
+
/**
|
114
|
+
* This property contains the number of bytes (or objects) in the queue
|
115
|
+
* ready to be read. The value provides introspection data regarding
|
116
|
+
* the status of the `highWaterMark`.
|
117
|
+
* @since v9.4.0
|
118
|
+
*/
|
119
|
+
readonly readableLength: number;
|
120
|
+
/**
|
121
|
+
* Getter for the property `objectMode` of a given `Readable` stream.
|
122
|
+
* @since v12.3.0
|
123
|
+
*/
|
124
|
+
readonly readableObjectMode: boolean;
|
125
|
+
/**
|
126
|
+
* Is `true` after `readable.destroy()` has been called.
|
127
|
+
* @since v8.0.0
|
128
|
+
*/
|
129
|
+
destroyed: boolean;
|
130
|
+
/**
|
131
|
+
* Is true after 'close' has been emitted.
|
132
|
+
* @since v18.0.0
|
133
|
+
*/
|
134
|
+
readonly closed: boolean;
|
135
|
+
/**
|
136
|
+
* Returns error if the stream has been destroyed with an error.
|
137
|
+
* @since v18.0.0
|
138
|
+
*/
|
139
|
+
readonly errored: Error | null;
|
140
|
+
constructor(opts?: ReadableOptions);
|
141
|
+
_construct?(callback: (error?: Error | null) => void): void;
|
142
|
+
_read(size: number): void;
|
143
|
+
/**
|
144
|
+
* The `readable.read()` method reads data out of the internal buffer and
|
145
|
+
* returns it. If no data is available to be read, `null` is returned. By default,
|
146
|
+
* the data is returned as a `Buffer` object unless an encoding has been
|
147
|
+
* specified using the `readable.setEncoding()` method or the stream is operating
|
148
|
+
* in object mode.
|
149
|
+
*
|
150
|
+
* The optional `size` argument specifies a specific number of bytes to read. If`size` bytes are not available to be read, `null` will be returned _unless_the stream has ended, in which
|
151
|
+
* case all of the data remaining in the internal
|
152
|
+
* buffer will be returned.
|
153
|
+
*
|
154
|
+
* If the `size` argument is not specified, all of the data contained in the
|
155
|
+
* internal buffer will be returned.
|
156
|
+
*
|
157
|
+
* The `size` argument must be less than or equal to 1 GiB.
|
158
|
+
*
|
159
|
+
* The `readable.read()` method should only be called on `Readable` streams
|
160
|
+
* operating in paused mode. In flowing mode, `readable.read()` is called
|
161
|
+
* automatically until the internal buffer is fully drained.
|
162
|
+
*
|
163
|
+
* ```js
|
164
|
+
* const readable = getReadableStreamSomehow();
|
165
|
+
*
|
166
|
+
* // 'readable' may be triggered multiple times as data is buffered in
|
167
|
+
* readable.on('readable', () => {
|
168
|
+
* let chunk;
|
169
|
+
* console.log('Stream is readable (new data received in buffer)');
|
170
|
+
* // Use a loop to make sure we read all currently available data
|
171
|
+
* while (null !== (chunk = readable.read())) {
|
172
|
+
* console.log(`Read ${chunk.length} bytes of data...`);
|
173
|
+
* }
|
174
|
+
* });
|
175
|
+
*
|
176
|
+
* // 'end' will be triggered once when there is no more data available
|
177
|
+
* readable.on('end', () => {
|
178
|
+
* console.log('Reached end of stream.');
|
179
|
+
* });
|
180
|
+
* ```
|
181
|
+
*
|
182
|
+
* Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
|
183
|
+
* are not concatenated. A `while` loop is necessary to consume all data
|
184
|
+
* currently in the buffer. When reading a large file `.read()` may return `null`,
|
185
|
+
* having consumed all buffered content so far, but there is still more data to
|
186
|
+
* come not yet buffered. In this case a new `'readable'` event will be emitted
|
187
|
+
* when there is more data in the buffer. Finally the `'end'` event will be
|
188
|
+
* emitted when there is no more data to come.
|
189
|
+
*
|
190
|
+
* Therefore to read a file's whole contents from a `readable`, it is necessary
|
191
|
+
* to collect chunks across multiple `'readable'` events:
|
192
|
+
*
|
193
|
+
* ```js
|
194
|
+
* const chunks = [];
|
195
|
+
*
|
196
|
+
* readable.on('readable', () => {
|
197
|
+
* let chunk;
|
198
|
+
* while (null !== (chunk = readable.read())) {
|
199
|
+
* chunks.push(chunk);
|
200
|
+
* }
|
201
|
+
* });
|
202
|
+
*
|
203
|
+
* readable.on('end', () => {
|
204
|
+
* const content = chunks.join('');
|
205
|
+
* });
|
206
|
+
* ```
|
207
|
+
*
|
208
|
+
* A `Readable` stream in object mode will always return a single item from
|
209
|
+
* a call to `readable.read(size)`, regardless of the value of the`size` argument.
|
210
|
+
*
|
211
|
+
* If the `readable.read()` method returns a chunk of data, a `'data'` event will
|
212
|
+
* also be emitted.
|
213
|
+
*
|
214
|
+
* Calling {@link read} after the `'end'` event has
|
215
|
+
* been emitted will return `null`. No runtime error will be raised.
|
216
|
+
* @since v0.9.4
|
217
|
+
* @param size Optional argument to specify how much data to read.
|
218
|
+
*/
|
219
|
+
read(size?: number): any;
|
220
|
+
/**
|
221
|
+
* The `readable.setEncoding()` method sets the character encoding for
|
222
|
+
* data read from the `Readable` stream.
|
223
|
+
*
|
224
|
+
* By default, no encoding is assigned and stream data will be returned as`Buffer` objects. Setting an encoding causes the stream data
|
225
|
+
* to be returned as strings of the specified encoding rather than as `Buffer`objects. For instance, calling `readable.setEncoding('utf8')` will cause the
|
226
|
+
* output data to be interpreted as UTF-8 data, and passed as strings. Calling`readable.setEncoding('hex')` will cause the data to be encoded in hexadecimal
|
227
|
+
* string format.
|
228
|
+
*
|
229
|
+
* The `Readable` stream will properly handle multi-byte characters delivered
|
230
|
+
* through the stream that would otherwise become improperly decoded if simply
|
231
|
+
* pulled from the stream as `Buffer` objects.
|
232
|
+
*
|
233
|
+
* ```js
|
234
|
+
* const readable = getReadableStreamSomehow();
|
235
|
+
* readable.setEncoding('utf8');
|
236
|
+
* readable.on('data', (chunk) => {
|
237
|
+
* assert.equal(typeof chunk, 'string');
|
238
|
+
* console.log('Got %d characters of string data:', chunk.length);
|
239
|
+
* });
|
240
|
+
* ```
|
241
|
+
* @since v0.9.4
|
242
|
+
* @param encoding The encoding to use.
|
243
|
+
*/
|
244
|
+
setEncoding(encoding: BufferEncoding): this;
|
245
|
+
/**
|
246
|
+
* The `readable.pause()` method will cause a stream in flowing mode to stop
|
247
|
+
* emitting `'data'` events, switching out of flowing mode. Any data that
|
248
|
+
* becomes available will remain in the internal buffer.
|
249
|
+
*
|
250
|
+
* ```js
|
251
|
+
* const readable = getReadableStreamSomehow();
|
252
|
+
* readable.on('data', (chunk) => {
|
253
|
+
* console.log(`Received ${chunk.length} bytes of data.`);
|
254
|
+
* readable.pause();
|
255
|
+
* console.log('There will be no additional data for 1 second.');
|
256
|
+
* setTimeout(() => {
|
257
|
+
* console.log('Now data will start flowing again.');
|
258
|
+
* readable.resume();
|
259
|
+
* }, 1000);
|
260
|
+
* });
|
261
|
+
* ```
|
262
|
+
*
|
263
|
+
* The `readable.pause()` method has no effect if there is a `'readable'`event listener.
|
264
|
+
* @since v0.9.4
|
265
|
+
*/
|
266
|
+
pause(): this;
|
267
|
+
/**
|
268
|
+
* The `readable.resume()` method causes an explicitly paused `Readable` stream to
|
269
|
+
* resume emitting `'data'` events, switching the stream into flowing mode.
|
270
|
+
*
|
271
|
+
* The `readable.resume()` method can be used to fully consume the data from a
|
272
|
+
* stream without actually processing any of that data:
|
273
|
+
*
|
274
|
+
* ```js
|
275
|
+
* getReadableStreamSomehow()
|
276
|
+
* .resume()
|
277
|
+
* .on('end', () => {
|
278
|
+
* console.log('Reached the end, but did not read anything.');
|
279
|
+
* });
|
280
|
+
* ```
|
281
|
+
*
|
282
|
+
* The `readable.resume()` method has no effect if there is a `'readable'`event listener.
|
283
|
+
* @since v0.9.4
|
284
|
+
*/
|
285
|
+
resume(): this;
|
286
|
+
/**
|
287
|
+
* The `readable.isPaused()` method returns the current operating state of the`Readable`. This is used primarily by the mechanism that underlies the`readable.pipe()` method. In most
|
288
|
+
* typical cases, there will be no reason to
|
289
|
+
* use this method directly.
|
290
|
+
*
|
291
|
+
* ```js
|
292
|
+
* const readable = new stream.Readable();
|
293
|
+
*
|
294
|
+
* readable.isPaused(); // === false
|
295
|
+
* readable.pause();
|
296
|
+
* readable.isPaused(); // === true
|
297
|
+
* readable.resume();
|
298
|
+
* readable.isPaused(); // === false
|
299
|
+
* ```
|
300
|
+
* @since v0.11.14
|
301
|
+
*/
|
302
|
+
isPaused(): boolean;
|
303
|
+
/**
|
304
|
+
* The `readable.unpipe()` method detaches a `Writable` stream previously attached
|
305
|
+
* using the {@link pipe} method.
|
306
|
+
*
|
307
|
+
* If the `destination` is not specified, then _all_ pipes are detached.
|
308
|
+
*
|
309
|
+
* If the `destination` is specified, but no pipe is set up for it, then
|
310
|
+
* the method does nothing.
|
311
|
+
*
|
312
|
+
* ```js
|
313
|
+
* const fs = require('fs');
|
314
|
+
* const readable = getReadableStreamSomehow();
|
315
|
+
* const writable = fs.createWriteStream('file.txt');
|
316
|
+
* // All the data from readable goes into 'file.txt',
|
317
|
+
* // but only for the first second.
|
318
|
+
* readable.pipe(writable);
|
319
|
+
* setTimeout(() => {
|
320
|
+
* console.log('Stop writing to file.txt.');
|
321
|
+
* readable.unpipe(writable);
|
322
|
+
* console.log('Manually close the file stream.');
|
323
|
+
* writable.end();
|
324
|
+
* }, 1000);
|
325
|
+
* ```
|
326
|
+
* @since v0.9.4
|
327
|
+
* @param destination Optional specific stream to unpipe
|
328
|
+
*/
|
329
|
+
unpipe(destination?: NodeJS.WritableStream): this;
|
330
|
+
/**
|
331
|
+
* Passing `chunk` as `null` signals the end of the stream (EOF) and behaves the
|
332
|
+
* same as `readable.push(null)`, after which no more data can be written. The EOF
|
333
|
+
* signal is put at the end of the buffer and any buffered data will still be
|
334
|
+
* flushed.
|
335
|
+
*
|
336
|
+
* The `readable.unshift()` method pushes a chunk of data back into the internal
|
337
|
+
* buffer. This is useful in certain situations where a stream is being consumed by
|
338
|
+
* code that needs to "un-consume" some amount of data that it has optimistically
|
339
|
+
* pulled out of the source, so that the data can be passed on to some other party.
|
340
|
+
*
|
341
|
+
* The `stream.unshift(chunk)` method cannot be called after the `'end'` event
|
342
|
+
* has been emitted or a runtime error will be thrown.
|
343
|
+
*
|
344
|
+
* Developers using `stream.unshift()` often should consider switching to
|
345
|
+
* use of a `Transform` stream instead. See the `API for stream implementers` section for more information.
|
346
|
+
*
|
347
|
+
* ```js
|
348
|
+
* // Pull off a header delimited by \n\n.
|
349
|
+
* // Use unshift() if we get too much.
|
350
|
+
* // Call the callback with (error, header, stream).
|
351
|
+
* const { StringDecoder } = require('string_decoder');
|
352
|
+
* function parseHeader(stream, callback) {
|
353
|
+
* stream.on('error', callback);
|
354
|
+
* stream.on('readable', onReadable);
|
355
|
+
* const decoder = new StringDecoder('utf8');
|
356
|
+
* let header = '';
|
357
|
+
* function onReadable() {
|
358
|
+
* let chunk;
|
359
|
+
* while (null !== (chunk = stream.read())) {
|
360
|
+
* const str = decoder.write(chunk);
|
361
|
+
* if (str.includes('\n\n')) {
|
362
|
+
* // Found the header boundary.
|
363
|
+
* const split = str.split(/\n\n/);
|
364
|
+
* header += split.shift();
|
365
|
+
* const remaining = split.join('\n\n');
|
366
|
+
* const buf = Buffer.from(remaining, 'utf8');
|
367
|
+
* stream.removeListener('error', callback);
|
368
|
+
* // Remove the 'readable' listener before unshifting.
|
369
|
+
* stream.removeListener('readable', onReadable);
|
370
|
+
* if (buf.length)
|
371
|
+
* stream.unshift(buf);
|
372
|
+
* // Now the body of the message can be read from the stream.
|
373
|
+
* callback(null, header, stream);
|
374
|
+
* return;
|
375
|
+
* }
|
376
|
+
* // Still reading the header.
|
377
|
+
* header += str;
|
378
|
+
* }
|
379
|
+
* }
|
380
|
+
* }
|
381
|
+
* ```
|
382
|
+
*
|
383
|
+
* Unlike {@link push}, `stream.unshift(chunk)` will not
|
384
|
+
* end the reading process by resetting the internal reading state of the stream.
|
385
|
+
* This can cause unexpected results if `readable.unshift()` is called during a
|
386
|
+
* read (i.e. from within a {@link _read} implementation on a
|
387
|
+
* custom stream). Following the call to `readable.unshift()` with an immediate {@link push} will reset the reading state appropriately,
|
388
|
+
* however it is best to simply avoid calling `readable.unshift()` while in the
|
389
|
+
* process of performing a read.
|
390
|
+
* @since v0.9.11
|
391
|
+
* @param chunk Chunk of data to unshift onto the read queue. For streams not operating in object mode, `chunk` must be a string, `Buffer`, `Uint8Array` or `null`. For object mode
|
392
|
+
* streams, `chunk` may be any JavaScript value.
|
393
|
+
* @param encoding Encoding of string chunks. Must be a valid `Buffer` encoding, such as `'utf8'` or `'ascii'`.
|
394
|
+
*/
|
395
|
+
unshift(chunk: any, encoding?: BufferEncoding): void;
|
396
|
+
/**
|
397
|
+
* Prior to Node.js 0.10, streams did not implement the entire `stream` module API
|
398
|
+
* as it is currently defined. (See `Compatibility` for more information.)
|
399
|
+
*
|
400
|
+
* When using an older Node.js library that emits `'data'` events and has a {@link pause} method that is advisory only, the`readable.wrap()` method can be used to create a `Readable`
|
401
|
+
* stream that uses
|
402
|
+
* the old stream as its data source.
|
403
|
+
*
|
404
|
+
* It will rarely be necessary to use `readable.wrap()` but the method has been
|
405
|
+
* provided as a convenience for interacting with older Node.js applications and
|
406
|
+
* libraries.
|
407
|
+
*
|
408
|
+
* ```js
|
409
|
+
* const { OldReader } = require('./old-api-module.js');
|
410
|
+
* const { Readable } = require('stream');
|
411
|
+
* const oreader = new OldReader();
|
412
|
+
* const myReader = new Readable().wrap(oreader);
|
413
|
+
*
|
414
|
+
* myReader.on('readable', () => {
|
415
|
+
* myReader.read(); // etc.
|
416
|
+
* });
|
417
|
+
* ```
|
418
|
+
* @since v0.9.4
|
419
|
+
* @param stream An "old style" readable stream
|
420
|
+
*/
|
421
|
+
wrap(stream: NodeJS.ReadableStream): this;
|
422
|
+
push(chunk: any, encoding?: BufferEncoding): boolean;
|
423
|
+
_destroy(error: Error | null, callback: (error?: Error | null) => void): void;
|
424
|
+
/**
|
425
|
+
* Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the readable
|
426
|
+
* stream will release any internal resources and subsequent calls to `push()`will be ignored.
|
427
|
+
*
|
428
|
+
* Once `destroy()` has been called any further calls will be a no-op and no
|
429
|
+
* further errors except from `_destroy()` may be emitted as `'error'`.
|
430
|
+
*
|
431
|
+
* Implementors should not override this method, but instead implement `readable._destroy()`.
|
432
|
+
* @since v8.0.0
|
433
|
+
* @param error Error which will be passed as payload in `'error'` event
|
434
|
+
*/
|
435
|
+
destroy(error?: Error): this;
|
436
|
+
/**
|
437
|
+
* Event emitter
|
438
|
+
* The defined events on documents including:
|
439
|
+
* 1. close
|
440
|
+
* 2. data
|
441
|
+
* 3. end
|
442
|
+
* 4. error
|
443
|
+
* 5. pause
|
444
|
+
* 6. readable
|
445
|
+
* 7. resume
|
446
|
+
*/
|
447
|
+
addListener(event: 'close', listener: () => void): this;
|
448
|
+
addListener(event: 'data', listener: (chunk: any) => void): this;
|
449
|
+
addListener(event: 'end', listener: () => void): this;
|
450
|
+
addListener(event: 'error', listener: (err: Error) => void): this;
|
451
|
+
addListener(event: 'pause', listener: () => void): this;
|
452
|
+
addListener(event: 'readable', listener: () => void): this;
|
453
|
+
addListener(event: 'resume', listener: () => void): this;
|
454
|
+
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
455
|
+
emit(event: 'close'): boolean;
|
456
|
+
emit(event: 'data', chunk: any): boolean;
|
457
|
+
emit(event: 'end'): boolean;
|
458
|
+
emit(event: 'error', err: Error): boolean;
|
459
|
+
emit(event: 'pause'): boolean;
|
460
|
+
emit(event: 'readable'): boolean;
|
461
|
+
emit(event: 'resume'): boolean;
|
462
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
463
|
+
on(event: 'close', listener: () => void): this;
|
464
|
+
on(event: 'data', listener: (chunk: any) => void): this;
|
465
|
+
on(event: 'end', listener: () => void): this;
|
466
|
+
on(event: 'error', listener: (err: Error) => void): this;
|
467
|
+
on(event: 'pause', listener: () => void): this;
|
468
|
+
on(event: 'readable', listener: () => void): this;
|
469
|
+
on(event: 'resume', listener: () => void): this;
|
470
|
+
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
471
|
+
once(event: 'close', listener: () => void): this;
|
472
|
+
once(event: 'data', listener: (chunk: any) => void): this;
|
473
|
+
once(event: 'end', listener: () => void): this;
|
474
|
+
once(event: 'error', listener: (err: Error) => void): this;
|
475
|
+
once(event: 'pause', listener: () => void): this;
|
476
|
+
once(event: 'readable', listener: () => void): this;
|
477
|
+
once(event: 'resume', listener: () => void): this;
|
478
|
+
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
479
|
+
prependListener(event: 'close', listener: () => void): this;
|
480
|
+
prependListener(event: 'data', listener: (chunk: any) => void): this;
|
481
|
+
prependListener(event: 'end', listener: () => void): this;
|
482
|
+
prependListener(event: 'error', listener: (err: Error) => void): this;
|
483
|
+
prependListener(event: 'pause', listener: () => void): this;
|
484
|
+
prependListener(event: 'readable', listener: () => void): this;
|
485
|
+
prependListener(event: 'resume', listener: () => void): this;
|
486
|
+
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
487
|
+
prependOnceListener(event: 'close', listener: () => void): this;
|
488
|
+
prependOnceListener(event: 'data', listener: (chunk: any) => void): this;
|
489
|
+
prependOnceListener(event: 'end', listener: () => void): this;
|
490
|
+
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
491
|
+
prependOnceListener(event: 'pause', listener: () => void): this;
|
492
|
+
prependOnceListener(event: 'readable', listener: () => void): this;
|
493
|
+
prependOnceListener(event: 'resume', listener: () => void): this;
|
494
|
+
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
495
|
+
removeListener(event: 'close', listener: () => void): this;
|
496
|
+
removeListener(event: 'data', listener: (chunk: any) => void): this;
|
497
|
+
removeListener(event: 'end', listener: () => void): this;
|
498
|
+
removeListener(event: 'error', listener: (err: Error) => void): this;
|
499
|
+
removeListener(event: 'pause', listener: () => void): this;
|
500
|
+
removeListener(event: 'readable', listener: () => void): this;
|
501
|
+
removeListener(event: 'resume', listener: () => void): this;
|
502
|
+
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
503
|
+
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
|
504
|
+
}
|
505
|
+
interface WritableOptions extends StreamOptions<Writable> {
|
506
|
+
decodeStrings?: boolean | undefined;
|
507
|
+
defaultEncoding?: BufferEncoding | undefined;
|
508
|
+
write?(this: Writable, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
509
|
+
writev?(
|
510
|
+
this: Writable,
|
511
|
+
chunks: Array<{
|
512
|
+
chunk: any;
|
513
|
+
encoding: BufferEncoding;
|
514
|
+
}>,
|
515
|
+
callback: (error?: Error | null) => void
|
516
|
+
): void;
|
517
|
+
final?(this: Writable, callback: (error?: Error | null) => void): void;
|
518
|
+
}
|
519
|
+
/**
|
520
|
+
* @since v0.9.4
|
521
|
+
*/
|
522
|
+
class Writable extends Stream implements NodeJS.WritableStream {
|
523
|
+
/**
|
524
|
+
* A utility method for creating a `Writable` from a web `WritableStream`.
|
525
|
+
* @since v17.0.0
|
526
|
+
* @experimental
|
527
|
+
*/
|
528
|
+
static fromWeb(writableStream: streamWeb.WritableStream, options?: Pick<WritableOptions, 'decodeStrings' | 'highWaterMark' | 'objectMode' | 'signal'>): Writable;
|
529
|
+
/**
|
530
|
+
* A utility method for creating a web `WritableStream` from a `Writable`.
|
531
|
+
* @since v17.0.0
|
532
|
+
* @experimental
|
533
|
+
*/
|
534
|
+
static toWeb(streamWritable: Writable): streamWeb.WritableStream;
|
535
|
+
/**
|
536
|
+
* Is `true` if it is safe to call `writable.write()`, which means
|
537
|
+
* the stream has not been destroyed, errored or ended.
|
538
|
+
* @since v11.4.0
|
539
|
+
*/
|
540
|
+
readonly writable: boolean;
|
541
|
+
/**
|
542
|
+
* Is `true` after `writable.end()` has been called. This property
|
543
|
+
* does not indicate whether the data has been flushed, for this use `writable.writableFinished` instead.
|
544
|
+
* @since v12.9.0
|
545
|
+
*/
|
546
|
+
readonly writableEnded: boolean;
|
547
|
+
/**
|
548
|
+
* Is set to `true` immediately before the `'finish'` event is emitted.
|
549
|
+
* @since v12.6.0
|
550
|
+
*/
|
551
|
+
readonly writableFinished: boolean;
|
552
|
+
/**
|
553
|
+
* Return the value of `highWaterMark` passed when creating this `Writable`.
|
554
|
+
* @since v9.3.0
|
555
|
+
*/
|
556
|
+
readonly writableHighWaterMark: number;
|
557
|
+
/**
|
558
|
+
* This property contains the number of bytes (or objects) in the queue
|
559
|
+
* ready to be written. The value provides introspection data regarding
|
560
|
+
* the status of the `highWaterMark`.
|
561
|
+
* @since v9.4.0
|
562
|
+
*/
|
563
|
+
readonly writableLength: number;
|
564
|
+
/**
|
565
|
+
* Getter for the property `objectMode` of a given `Writable` stream.
|
566
|
+
* @since v12.3.0
|
567
|
+
*/
|
568
|
+
readonly writableObjectMode: boolean;
|
569
|
+
/**
|
570
|
+
* Number of times `writable.uncork()` needs to be
|
571
|
+
* called in order to fully uncork the stream.
|
572
|
+
* @since v13.2.0, v12.16.0
|
573
|
+
*/
|
574
|
+
readonly writableCorked: number;
|
575
|
+
/**
|
576
|
+
* Is `true` after `writable.destroy()` has been called.
|
577
|
+
* @since v8.0.0
|
578
|
+
*/
|
579
|
+
destroyed: boolean;
|
580
|
+
/**
|
581
|
+
* Is true after 'close' has been emitted.
|
582
|
+
* @since v18.0.0
|
583
|
+
*/
|
584
|
+
readonly closed: boolean;
|
585
|
+
/**
|
586
|
+
* Returns error if the stream has been destroyed with an error.
|
587
|
+
* @since v18.0.0
|
588
|
+
*/
|
589
|
+
readonly errored: Error | null;
|
590
|
+
/**
|
591
|
+
* Is `true` if the stream's buffer has been full and stream will emit 'drain'.
|
592
|
+
* @since v15.2.0, v14.17.0
|
593
|
+
*/
|
594
|
+
readonly writableNeedDrain: boolean;
|
595
|
+
constructor(opts?: WritableOptions);
|
596
|
+
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
597
|
+
_writev?(
|
598
|
+
chunks: Array<{
|
599
|
+
chunk: any;
|
600
|
+
encoding: BufferEncoding;
|
601
|
+
}>,
|
602
|
+
callback: (error?: Error | null) => void
|
603
|
+
): void;
|
604
|
+
_construct?(callback: (error?: Error | null) => void): void;
|
605
|
+
_destroy(error: Error | null, callback: (error?: Error | null) => void): void;
|
606
|
+
_final(callback: (error?: Error | null) => void): void;
|
607
|
+
/**
|
608
|
+
* The `writable.write()` method writes some data to the stream, and calls the
|
609
|
+
* supplied `callback` once the data has been fully handled. If an error
|
610
|
+
* occurs, the `callback` will be called with the error as its
|
611
|
+
* first argument. The `callback` is called asynchronously and before `'error'` is
|
612
|
+
* emitted.
|
613
|
+
*
|
614
|
+
* The return value is `true` if the internal buffer is less than the`highWaterMark` configured when the stream was created after admitting `chunk`.
|
615
|
+
* If `false` is returned, further attempts to write data to the stream should
|
616
|
+
* stop until the `'drain'` event is emitted.
|
617
|
+
*
|
618
|
+
* While a stream is not draining, calls to `write()` will buffer `chunk`, and
|
619
|
+
* return false. Once all currently buffered chunks are drained (accepted for
|
620
|
+
* delivery by the operating system), the `'drain'` event will be emitted.
|
621
|
+
* Once `write()` returns false, do not write more chunks
|
622
|
+
* until the `'drain'` event is emitted. While calling `write()` on a stream that
|
623
|
+
* is not draining is allowed, Node.js will buffer all written chunks until
|
624
|
+
* maximum memory usage occurs, at which point it will abort unconditionally.
|
625
|
+
* Even before it aborts, high memory usage will cause poor garbage collector
|
626
|
+
* performance and high RSS (which is not typically released back to the system,
|
627
|
+
* even after the memory is no longer required). Since TCP sockets may never
|
628
|
+
* drain if the remote peer does not read the data, writing a socket that is
|
629
|
+
* not draining may lead to a remotely exploitable vulnerability.
|
630
|
+
*
|
631
|
+
* Writing data while the stream is not draining is particularly
|
632
|
+
* problematic for a `Transform`, because the `Transform` streams are paused
|
633
|
+
* by default until they are piped or a `'data'` or `'readable'` event handler
|
634
|
+
* is added.
|
635
|
+
*
|
636
|
+
* If the data to be written can be generated or fetched on demand, it is
|
637
|
+
* recommended to encapsulate the logic into a `Readable` and use {@link pipe}. However, if calling `write()` is preferred, it is
|
638
|
+
* possible to respect backpressure and avoid memory issues using the `'drain'` event:
|
639
|
+
*
|
640
|
+
* ```js
|
641
|
+
* function write(data, cb) {
|
642
|
+
* if (!stream.write(data)) {
|
643
|
+
* stream.once('drain', cb);
|
644
|
+
* } else {
|
645
|
+
* process.nextTick(cb);
|
646
|
+
* }
|
647
|
+
* }
|
648
|
+
*
|
649
|
+
* // Wait for cb to be called before doing any other write.
|
650
|
+
* write('hello', () => {
|
651
|
+
* console.log('Write completed, do more writes now.');
|
652
|
+
* });
|
653
|
+
* ```
|
654
|
+
*
|
655
|
+
* A `Writable` stream in object mode will always ignore the `encoding` argument.
|
656
|
+
* @since v0.9.4
|
657
|
+
* @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
|
658
|
+
* JavaScript value other than `null`.
|
659
|
+
* @param [encoding='utf8'] The encoding, if `chunk` is a string.
|
660
|
+
* @param callback Callback for when this chunk of data is flushed.
|
661
|
+
* @return `false` if the stream wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
|
662
|
+
*/
|
663
|
+
write(chunk: any, callback?: (error: Error | null | undefined) => void): boolean;
|
664
|
+
write(chunk: any, encoding: BufferEncoding, callback?: (error: Error | null | undefined) => void): boolean;
|
665
|
+
/**
|
666
|
+
* The `writable.setDefaultEncoding()` method sets the default `encoding` for a `Writable` stream.
|
667
|
+
* @since v0.11.15
|
668
|
+
* @param encoding The new default encoding
|
669
|
+
*/
|
670
|
+
setDefaultEncoding(encoding: BufferEncoding): this;
|
671
|
+
/**
|
672
|
+
* Calling the `writable.end()` method signals that no more data will be written
|
673
|
+
* to the `Writable`. The optional `chunk` and `encoding` arguments allow one
|
674
|
+
* final additional chunk of data to be written immediately before closing the
|
675
|
+
* stream.
|
676
|
+
*
|
677
|
+
* Calling the {@link write} method after calling {@link end} will raise an error.
|
678
|
+
*
|
679
|
+
* ```js
|
680
|
+
* // Write 'hello, ' and then end with 'world!'.
|
681
|
+
* const fs = require('fs');
|
682
|
+
* const file = fs.createWriteStream('example.txt');
|
683
|
+
* file.write('hello, ');
|
684
|
+
* file.end('world!');
|
685
|
+
* // Writing more now is not allowed!
|
686
|
+
* ```
|
687
|
+
* @since v0.9.4
|
688
|
+
* @param chunk Optional data to write. For streams not operating in object mode, `chunk` must be a string, `Buffer` or `Uint8Array`. For object mode streams, `chunk` may be any
|
689
|
+
* JavaScript value other than `null`.
|
690
|
+
* @param encoding The encoding if `chunk` is a string
|
691
|
+
* @param callback Callback for when the stream is finished.
|
692
|
+
*/
|
693
|
+
end(cb?: () => void): this;
|
694
|
+
end(chunk: any, cb?: () => void): this;
|
695
|
+
end(chunk: any, encoding: BufferEncoding, cb?: () => void): this;
|
696
|
+
/**
|
697
|
+
* The `writable.cork()` method forces all written data to be buffered in memory.
|
698
|
+
* The buffered data will be flushed when either the {@link uncork} or {@link end} methods are called.
|
699
|
+
*
|
700
|
+
* The primary intent of `writable.cork()` is to accommodate a situation in which
|
701
|
+
* several small chunks are written to the stream in rapid succession. Instead of
|
702
|
+
* immediately forwarding them to the underlying destination, `writable.cork()`buffers all the chunks until `writable.uncork()` is called, which will pass them
|
703
|
+
* all to `writable._writev()`, if present. This prevents a head-of-line blocking
|
704
|
+
* situation where data is being buffered while waiting for the first small chunk
|
705
|
+
* to be processed. However, use of `writable.cork()` without implementing`writable._writev()` may have an adverse effect on throughput.
|
706
|
+
*
|
707
|
+
* See also: `writable.uncork()`, `writable._writev()`.
|
708
|
+
* @since v0.11.2
|
709
|
+
*/
|
710
|
+
cork(): void;
|
711
|
+
/**
|
712
|
+
* The `writable.uncork()` method flushes all data buffered since {@link cork} was called.
|
713
|
+
*
|
714
|
+
* When using `writable.cork()` and `writable.uncork()` to manage the buffering
|
715
|
+
* of writes to a stream, defer calls to `writable.uncork()` using`process.nextTick()`. Doing so allows batching of all`writable.write()` calls that occur within a given Node.js event
|
716
|
+
* loop phase.
|
717
|
+
*
|
718
|
+
* ```js
|
719
|
+
* stream.cork();
|
720
|
+
* stream.write('some ');
|
721
|
+
* stream.write('data ');
|
722
|
+
* process.nextTick(() => stream.uncork());
|
723
|
+
* ```
|
724
|
+
*
|
725
|
+
* If the `writable.cork()` method is called multiple times on a stream, the
|
726
|
+
* same number of calls to `writable.uncork()` must be called to flush the buffered
|
727
|
+
* data.
|
728
|
+
*
|
729
|
+
* ```js
|
730
|
+
* stream.cork();
|
731
|
+
* stream.write('some ');
|
732
|
+
* stream.cork();
|
733
|
+
* stream.write('data ');
|
734
|
+
* process.nextTick(() => {
|
735
|
+
* stream.uncork();
|
736
|
+
* // The data will not be flushed until uncork() is called a second time.
|
737
|
+
* stream.uncork();
|
738
|
+
* });
|
739
|
+
* ```
|
740
|
+
*
|
741
|
+
* See also: `writable.cork()`.
|
742
|
+
* @since v0.11.2
|
743
|
+
*/
|
744
|
+
uncork(): void;
|
745
|
+
/**
|
746
|
+
* Destroy the stream. Optionally emit an `'error'` event, and emit a `'close'`event (unless `emitClose` is set to `false`). After this call, the writable
|
747
|
+
* stream has ended and subsequent calls to `write()` or `end()` will result in
|
748
|
+
* an `ERR_STREAM_DESTROYED` error.
|
749
|
+
* This is a destructive and immediate way to destroy a stream. Previous calls to`write()` may not have drained, and may trigger an `ERR_STREAM_DESTROYED` error.
|
750
|
+
* Use `end()` instead of destroy if data should flush before close, or wait for
|
751
|
+
* the `'drain'` event before destroying the stream.
|
752
|
+
*
|
753
|
+
* Once `destroy()` has been called any further calls will be a no-op and no
|
754
|
+
* further errors except from `_destroy()` may be emitted as `'error'`.
|
755
|
+
*
|
756
|
+
* Implementors should not override this method,
|
757
|
+
* but instead implement `writable._destroy()`.
|
758
|
+
* @since v8.0.0
|
759
|
+
* @param error Optional, an error to emit with `'error'` event.
|
760
|
+
*/
|
761
|
+
destroy(error?: Error): this;
|
762
|
+
/**
|
763
|
+
* Event emitter
|
764
|
+
* The defined events on documents including:
|
765
|
+
* 1. close
|
766
|
+
* 2. drain
|
767
|
+
* 3. error
|
768
|
+
* 4. finish
|
769
|
+
* 5. pipe
|
770
|
+
* 6. unpipe
|
771
|
+
*/
|
772
|
+
addListener(event: 'close', listener: () => void): this;
|
773
|
+
addListener(event: 'drain', listener: () => void): this;
|
774
|
+
addListener(event: 'error', listener: (err: Error) => void): this;
|
775
|
+
addListener(event: 'finish', listener: () => void): this;
|
776
|
+
addListener(event: 'pipe', listener: (src: Readable) => void): this;
|
777
|
+
addListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
778
|
+
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
779
|
+
emit(event: 'close'): boolean;
|
780
|
+
emit(event: 'drain'): boolean;
|
781
|
+
emit(event: 'error', err: Error): boolean;
|
782
|
+
emit(event: 'finish'): boolean;
|
783
|
+
emit(event: 'pipe', src: Readable): boolean;
|
784
|
+
emit(event: 'unpipe', src: Readable): boolean;
|
785
|
+
emit(event: string | symbol, ...args: any[]): boolean;
|
786
|
+
on(event: 'close', listener: () => void): this;
|
787
|
+
on(event: 'drain', listener: () => void): this;
|
788
|
+
on(event: 'error', listener: (err: Error) => void): this;
|
789
|
+
on(event: 'finish', listener: () => void): this;
|
790
|
+
on(event: 'pipe', listener: (src: Readable) => void): this;
|
791
|
+
on(event: 'unpipe', listener: (src: Readable) => void): this;
|
792
|
+
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
793
|
+
once(event: 'close', listener: () => void): this;
|
794
|
+
once(event: 'drain', listener: () => void): this;
|
795
|
+
once(event: 'error', listener: (err: Error) => void): this;
|
796
|
+
once(event: 'finish', listener: () => void): this;
|
797
|
+
once(event: 'pipe', listener: (src: Readable) => void): this;
|
798
|
+
once(event: 'unpipe', listener: (src: Readable) => void): this;
|
799
|
+
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
800
|
+
prependListener(event: 'close', listener: () => void): this;
|
801
|
+
prependListener(event: 'drain', listener: () => void): this;
|
802
|
+
prependListener(event: 'error', listener: (err: Error) => void): this;
|
803
|
+
prependListener(event: 'finish', listener: () => void): this;
|
804
|
+
prependListener(event: 'pipe', listener: (src: Readable) => void): this;
|
805
|
+
prependListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
806
|
+
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
807
|
+
prependOnceListener(event: 'close', listener: () => void): this;
|
808
|
+
prependOnceListener(event: 'drain', listener: () => void): this;
|
809
|
+
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
|
810
|
+
prependOnceListener(event: 'finish', listener: () => void): this;
|
811
|
+
prependOnceListener(event: 'pipe', listener: (src: Readable) => void): this;
|
812
|
+
prependOnceListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
813
|
+
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
814
|
+
removeListener(event: 'close', listener: () => void): this;
|
815
|
+
removeListener(event: 'drain', listener: () => void): this;
|
816
|
+
removeListener(event: 'error', listener: (err: Error) => void): this;
|
817
|
+
removeListener(event: 'finish', listener: () => void): this;
|
818
|
+
removeListener(event: 'pipe', listener: (src: Readable) => void): this;
|
819
|
+
removeListener(event: 'unpipe', listener: (src: Readable) => void): this;
|
820
|
+
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
821
|
+
}
|
822
|
+
interface DuplexOptions extends ReadableOptions, WritableOptions {
|
823
|
+
allowHalfOpen?: boolean | undefined;
|
824
|
+
readableObjectMode?: boolean | undefined;
|
825
|
+
writableObjectMode?: boolean | undefined;
|
826
|
+
readableHighWaterMark?: number | undefined;
|
827
|
+
writableHighWaterMark?: number | undefined;
|
828
|
+
writableCorked?: number | undefined;
|
829
|
+
construct?(this: Duplex, callback: (error?: Error | null) => void): void;
|
830
|
+
read?(this: Duplex, size: number): void;
|
831
|
+
write?(this: Duplex, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
832
|
+
writev?(
|
833
|
+
this: Duplex,
|
834
|
+
chunks: Array<{
|
835
|
+
chunk: any;
|
836
|
+
encoding: BufferEncoding;
|
837
|
+
}>,
|
838
|
+
callback: (error?: Error | null) => void
|
839
|
+
): void;
|
840
|
+
final?(this: Duplex, callback: (error?: Error | null) => void): void;
|
841
|
+
destroy?(this: Duplex, error: Error | null, callback: (error: Error | null) => void): void;
|
842
|
+
}
|
843
|
+
/**
|
844
|
+
* Duplex streams are streams that implement both the `Readable` and `Writable` interfaces.
|
845
|
+
*
|
846
|
+
* Examples of `Duplex` streams include:
|
847
|
+
*
|
848
|
+
* * `TCP sockets`
|
849
|
+
* * `zlib streams`
|
850
|
+
* * `crypto streams`
|
851
|
+
* @since v0.9.4
|
852
|
+
*/
|
853
|
+
class Duplex extends Readable implements Writable {
|
854
|
+
readonly writable: boolean;
|
855
|
+
readonly writableEnded: boolean;
|
856
|
+
readonly writableFinished: boolean;
|
857
|
+
readonly writableHighWaterMark: number;
|
858
|
+
readonly writableLength: number;
|
859
|
+
readonly writableObjectMode: boolean;
|
860
|
+
readonly writableCorked: number;
|
861
|
+
readonly writableNeedDrain: boolean;
|
862
|
+
readonly closed: boolean;
|
863
|
+
readonly errored: Error | null;
|
864
|
+
/**
|
865
|
+
* If `false` then the stream will automatically end the writable side when the
|
866
|
+
* readable side ends. Set initially by the `allowHalfOpen` constructor option,
|
867
|
+
* which defaults to `false`.
|
868
|
+
*
|
869
|
+
* This can be changed manually to change the half-open behavior of an existing`Duplex` stream instance, but must be changed before the `'end'` event is
|
870
|
+
* emitted.
|
871
|
+
* @since v0.9.4
|
872
|
+
*/
|
873
|
+
allowHalfOpen: boolean;
|
874
|
+
constructor(opts?: DuplexOptions);
|
875
|
+
/**
|
876
|
+
* A utility method for creating duplex streams.
|
877
|
+
*
|
878
|
+
* - `Stream` converts writable stream into writable `Duplex` and readable stream
|
879
|
+
* to `Duplex`.
|
880
|
+
* - `Blob` converts into readable `Duplex`.
|
881
|
+
* - `string` converts into readable `Duplex`.
|
882
|
+
* - `ArrayBuffer` converts into readable `Duplex`.
|
883
|
+
* - `AsyncIterable` converts into a readable `Duplex`. Cannot yield `null`.
|
884
|
+
* - `AsyncGeneratorFunction` converts into a readable/writable transform
|
885
|
+
* `Duplex`. Must take a source `AsyncIterable` as first parameter. Cannot yield
|
886
|
+
* `null`.
|
887
|
+
* - `AsyncFunction` converts into a writable `Duplex`. Must return
|
888
|
+
* either `null` or `undefined`
|
889
|
+
* - `Object ({ writable, readable })` converts `readable` and
|
890
|
+
* `writable` into `Stream` and then combines them into `Duplex` where the
|
891
|
+
* `Duplex` will write to the `writable` and read from the `readable`.
|
892
|
+
* - `Promise` converts into readable `Duplex`. Value `null` is ignored.
|
893
|
+
*
|
894
|
+
* @since v16.8.0
|
895
|
+
*/
|
896
|
+
static from(src: Stream | NodeBlob | ArrayBuffer | string | Iterable<any> | AsyncIterable<any> | AsyncGeneratorFunction | Promise<any> | Object): Duplex;
|
897
|
+
_write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
898
|
+
_writev?(
|
899
|
+
chunks: Array<{
|
900
|
+
chunk: any;
|
901
|
+
encoding: BufferEncoding;
|
902
|
+
}>,
|
903
|
+
callback: (error?: Error | null) => void
|
904
|
+
): void;
|
905
|
+
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
|
906
|
+
_final(callback: (error?: Error | null) => void): void;
|
907
|
+
write(chunk: any, encoding?: BufferEncoding, cb?: (error: Error | null | undefined) => void): boolean;
|
908
|
+
write(chunk: any, cb?: (error: Error | null | undefined) => void): boolean;
|
909
|
+
setDefaultEncoding(encoding: BufferEncoding): this;
|
910
|
+
end(cb?: () => void): this;
|
911
|
+
end(chunk: any, cb?: () => void): this;
|
912
|
+
end(chunk: any, encoding?: BufferEncoding, cb?: () => void): this;
|
913
|
+
cork(): void;
|
914
|
+
uncork(): void;
|
915
|
+
}
|
916
|
+
type TransformCallback = (error?: Error | null, data?: any) => void;
|
917
|
+
interface TransformOptions extends DuplexOptions {
|
918
|
+
construct?(this: Transform, callback: (error?: Error | null) => void): void;
|
919
|
+
read?(this: Transform, size: number): void;
|
920
|
+
write?(this: Transform, chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
|
921
|
+
writev?(
|
922
|
+
this: Transform,
|
923
|
+
chunks: Array<{
|
924
|
+
chunk: any;
|
925
|
+
encoding: BufferEncoding;
|
926
|
+
}>,
|
927
|
+
callback: (error?: Error | null) => void
|
928
|
+
): void;
|
929
|
+
final?(this: Transform, callback: (error?: Error | null) => void): void;
|
930
|
+
destroy?(this: Transform, error: Error | null, callback: (error: Error | null) => void): void;
|
931
|
+
transform?(this: Transform, chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
932
|
+
flush?(this: Transform, callback: TransformCallback): void;
|
933
|
+
}
|
934
|
+
/**
|
935
|
+
* Transform streams are `Duplex` streams where the output is in some way
|
936
|
+
* related to the input. Like all `Duplex` streams, `Transform` streams
|
937
|
+
* implement both the `Readable` and `Writable` interfaces.
|
938
|
+
*
|
939
|
+
* Examples of `Transform` streams include:
|
940
|
+
*
|
941
|
+
* * `zlib streams`
|
942
|
+
* * `crypto streams`
|
943
|
+
* @since v0.9.4
|
944
|
+
*/
|
945
|
+
class Transform extends Duplex {
|
946
|
+
constructor(opts?: TransformOptions);
|
947
|
+
_transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
|
948
|
+
_flush(callback: TransformCallback): void;
|
949
|
+
}
|
950
|
+
/**
|
951
|
+
* The `stream.PassThrough` class is a trivial implementation of a `Transform` stream that simply passes the input bytes across to the output. Its purpose is
|
952
|
+
* primarily for examples and testing, but there are some use cases where`stream.PassThrough` is useful as a building block for novel sorts of streams.
|
953
|
+
*/
|
954
|
+
class PassThrough extends Transform {}
|
955
|
+
/**
|
956
|
+
* Attaches an AbortSignal to a readable or writeable stream. This lets code
|
957
|
+
* control stream destruction using an `AbortController`.
|
958
|
+
*
|
959
|
+
* Calling `abort` on the `AbortController` corresponding to the passed`AbortSignal` will behave the same way as calling `.destroy(new AbortError())`on the stream.
|
960
|
+
*
|
961
|
+
* ```js
|
962
|
+
* const fs = require('fs');
|
963
|
+
*
|
964
|
+
* const controller = new AbortController();
|
965
|
+
* const read = addAbortSignal(
|
966
|
+
* controller.signal,
|
967
|
+
* fs.createReadStream(('object.json'))
|
968
|
+
* );
|
969
|
+
* // Later, abort the operation closing the stream
|
970
|
+
* controller.abort();
|
971
|
+
* ```
|
972
|
+
*
|
973
|
+
* Or using an `AbortSignal` with a readable stream as an async iterable:
|
974
|
+
*
|
975
|
+
* ```js
|
976
|
+
* const controller = new AbortController();
|
977
|
+
* setTimeout(() => controller.abort(), 10_000); // set a timeout
|
978
|
+
* const stream = addAbortSignal(
|
979
|
+
* controller.signal,
|
980
|
+
* fs.createReadStream(('object.json'))
|
981
|
+
* );
|
982
|
+
* (async () => {
|
983
|
+
* try {
|
984
|
+
* for await (const chunk of stream) {
|
985
|
+
* await process(chunk);
|
986
|
+
* }
|
987
|
+
* } catch (e) {
|
988
|
+
* if (e.name === 'AbortError') {
|
989
|
+
* // The operation was cancelled
|
990
|
+
* } else {
|
991
|
+
* throw e;
|
992
|
+
* }
|
993
|
+
* }
|
994
|
+
* })();
|
995
|
+
* ```
|
996
|
+
* @since v15.4.0
|
997
|
+
* @param signal A signal representing possible cancellation
|
998
|
+
* @param stream a stream to attach a signal to
|
999
|
+
*/
|
1000
|
+
function addAbortSignal<T extends Stream>(signal: AbortSignal, stream: T): T;
|
1001
|
+
interface FinishedOptions extends Abortable {
|
1002
|
+
error?: boolean | undefined;
|
1003
|
+
readable?: boolean | undefined;
|
1004
|
+
writable?: boolean | undefined;
|
1005
|
+
}
|
1006
|
+
/**
|
1007
|
+
* A function to get notified when a stream is no longer readable, writable
|
1008
|
+
* or has experienced an error or a premature close event.
|
1009
|
+
*
|
1010
|
+
* ```js
|
1011
|
+
* const { finished } = require('stream');
|
1012
|
+
*
|
1013
|
+
* const rs = fs.createReadStream('archive.tar');
|
1014
|
+
*
|
1015
|
+
* finished(rs, (err) => {
|
1016
|
+
* if (err) {
|
1017
|
+
* console.error('Stream failed.', err);
|
1018
|
+
* } else {
|
1019
|
+
* console.log('Stream is done reading.');
|
1020
|
+
* }
|
1021
|
+
* });
|
1022
|
+
*
|
1023
|
+
* rs.resume(); // Drain the stream.
|
1024
|
+
* ```
|
1025
|
+
*
|
1026
|
+
* Especially useful in error handling scenarios where a stream is destroyed
|
1027
|
+
* prematurely (like an aborted HTTP request), and will not emit `'end'`or `'finish'`.
|
1028
|
+
*
|
1029
|
+
* The `finished` API provides promise version:
|
1030
|
+
*
|
1031
|
+
* ```js
|
1032
|
+
* const { finished } = require('stream/promises');
|
1033
|
+
*
|
1034
|
+
* const rs = fs.createReadStream('archive.tar');
|
1035
|
+
*
|
1036
|
+
* async function run() {
|
1037
|
+
* await finished(rs);
|
1038
|
+
* console.log('Stream is done reading.');
|
1039
|
+
* }
|
1040
|
+
*
|
1041
|
+
* run().catch(console.error);
|
1042
|
+
* rs.resume(); // Drain the stream.
|
1043
|
+
* ```
|
1044
|
+
*
|
1045
|
+
* `stream.finished()` leaves dangling event listeners (in particular`'error'`, `'end'`, `'finish'` and `'close'`) after `callback` has been
|
1046
|
+
* invoked. The reason for this is so that unexpected `'error'` events (due to
|
1047
|
+
* incorrect stream implementations) do not cause unexpected crashes.
|
1048
|
+
* If this is unwanted behavior then the returned cleanup function needs to be
|
1049
|
+
* invoked in the callback:
|
1050
|
+
*
|
1051
|
+
* ```js
|
1052
|
+
* const cleanup = finished(rs, (err) => {
|
1053
|
+
* cleanup();
|
1054
|
+
* // ...
|
1055
|
+
* });
|
1056
|
+
* ```
|
1057
|
+
* @since v10.0.0
|
1058
|
+
* @param stream A readable and/or writable stream.
|
1059
|
+
* @param callback A callback function that takes an optional error argument.
|
1060
|
+
* @return A cleanup function which removes all registered listeners.
|
1061
|
+
*/
|
1062
|
+
function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, options: FinishedOptions, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
|
1063
|
+
function finished(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, callback: (err?: NodeJS.ErrnoException | null) => void): () => void;
|
1064
|
+
namespace finished {
|
1065
|
+
function __promisify__(stream: NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream, options?: FinishedOptions): Promise<void>;
|
1066
|
+
}
|
1067
|
+
type PipelineSourceFunction<T> = () => Iterable<T> | AsyncIterable<T>;
|
1068
|
+
type PipelineSource<T> = Iterable<T> | AsyncIterable<T> | NodeJS.ReadableStream | PipelineSourceFunction<T>;
|
1069
|
+
type PipelineTransform<S extends PipelineTransformSource<any>, U> =
|
1070
|
+
| NodeJS.ReadWriteStream
|
1071
|
+
| ((source: S extends (...args: any[]) => Iterable<infer ST> | AsyncIterable<infer ST> ? AsyncIterable<ST> : S) => AsyncIterable<U>);
|
1072
|
+
type PipelineTransformSource<T> = PipelineSource<T> | PipelineTransform<any, T>;
|
1073
|
+
type PipelineDestinationIterableFunction<T> = (source: AsyncIterable<T>) => AsyncIterable<any>;
|
1074
|
+
type PipelineDestinationPromiseFunction<T, P> = (source: AsyncIterable<T>) => Promise<P>;
|
1075
|
+
type PipelineDestination<S extends PipelineTransformSource<any>, P> = S extends PipelineTransformSource<infer ST>
|
1076
|
+
? NodeJS.WritableStream | PipelineDestinationIterableFunction<ST> | PipelineDestinationPromiseFunction<ST, P>
|
1077
|
+
: never;
|
1078
|
+
type PipelineCallback<S extends PipelineDestination<any, any>> = S extends PipelineDestinationPromiseFunction<any, infer P>
|
1079
|
+
? (err: NodeJS.ErrnoException | null, value: P) => void
|
1080
|
+
: (err: NodeJS.ErrnoException | null) => void;
|
1081
|
+
type PipelinePromise<S extends PipelineDestination<any, any>> = S extends PipelineDestinationPromiseFunction<any, infer P> ? Promise<P> : Promise<void>;
|
1082
|
+
interface PipelineOptions {
|
1083
|
+
signal: AbortSignal;
|
1084
|
+
}
|
1085
|
+
/**
|
1086
|
+
* A module method to pipe between streams and generators forwarding errors and
|
1087
|
+
* properly cleaning up and provide a callback when the pipeline is complete.
|
1088
|
+
*
|
1089
|
+
* ```js
|
1090
|
+
* const { pipeline } = require('stream');
|
1091
|
+
* const fs = require('fs');
|
1092
|
+
* const zlib = require('zlib');
|
1093
|
+
*
|
1094
|
+
* // Use the pipeline API to easily pipe a series of streams
|
1095
|
+
* // together and get notified when the pipeline is fully done.
|
1096
|
+
*
|
1097
|
+
* // A pipeline to gzip a potentially huge tar file efficiently:
|
1098
|
+
*
|
1099
|
+
* pipeline(
|
1100
|
+
* fs.createReadStream('archive.tar'),
|
1101
|
+
* zlib.createGzip(),
|
1102
|
+
* fs.createWriteStream('archive.tar.gz'),
|
1103
|
+
* (err) => {
|
1104
|
+
* if (err) {
|
1105
|
+
* console.error('Pipeline failed.', err);
|
1106
|
+
* } else {
|
1107
|
+
* console.log('Pipeline succeeded.');
|
1108
|
+
* }
|
1109
|
+
* }
|
1110
|
+
* );
|
1111
|
+
* ```
|
1112
|
+
*
|
1113
|
+
* The `pipeline` API provides a promise version, which can also
|
1114
|
+
* receive an options argument as the last parameter with a`signal` `AbortSignal` property. When the signal is aborted,`destroy` will be called on the underlying pipeline, with
|
1115
|
+
* an`AbortError`.
|
1116
|
+
*
|
1117
|
+
* ```js
|
1118
|
+
* const { pipeline } = require('stream/promises');
|
1119
|
+
*
|
1120
|
+
* async function run() {
|
1121
|
+
* await pipeline(
|
1122
|
+
* fs.createReadStream('archive.tar'),
|
1123
|
+
* zlib.createGzip(),
|
1124
|
+
* fs.createWriteStream('archive.tar.gz')
|
1125
|
+
* );
|
1126
|
+
* console.log('Pipeline succeeded.');
|
1127
|
+
* }
|
1128
|
+
*
|
1129
|
+
* run().catch(console.error);
|
1130
|
+
* ```
|
1131
|
+
*
|
1132
|
+
* To use an `AbortSignal`, pass it inside an options object,
|
1133
|
+
* as the last argument:
|
1134
|
+
*
|
1135
|
+
* ```js
|
1136
|
+
* const { pipeline } = require('stream/promises');
|
1137
|
+
*
|
1138
|
+
* async function run() {
|
1139
|
+
* const ac = new AbortController();
|
1140
|
+
* const signal = ac.signal;
|
1141
|
+
*
|
1142
|
+
* setTimeout(() => ac.abort(), 1);
|
1143
|
+
* await pipeline(
|
1144
|
+
* fs.createReadStream('archive.tar'),
|
1145
|
+
* zlib.createGzip(),
|
1146
|
+
* fs.createWriteStream('archive.tar.gz'),
|
1147
|
+
* { signal },
|
1148
|
+
* );
|
1149
|
+
* }
|
1150
|
+
*
|
1151
|
+
* run().catch(console.error); // AbortError
|
1152
|
+
* ```
|
1153
|
+
*
|
1154
|
+
* The `pipeline` API also supports async generators:
|
1155
|
+
*
|
1156
|
+
* ```js
|
1157
|
+
* const { pipeline } = require('stream/promises');
|
1158
|
+
* const fs = require('fs');
|
1159
|
+
*
|
1160
|
+
* async function run() {
|
1161
|
+
* await pipeline(
|
1162
|
+
* fs.createReadStream('lowercase.txt'),
|
1163
|
+
* async function* (source, { signal }) {
|
1164
|
+
* source.setEncoding('utf8'); // Work with strings rather than `Buffer`s.
|
1165
|
+
* for await (const chunk of source) {
|
1166
|
+
* yield await processChunk(chunk, { signal });
|
1167
|
+
* }
|
1168
|
+
* },
|
1169
|
+
* fs.createWriteStream('uppercase.txt')
|
1170
|
+
* );
|
1171
|
+
* console.log('Pipeline succeeded.');
|
1172
|
+
* }
|
1173
|
+
*
|
1174
|
+
* run().catch(console.error);
|
1175
|
+
* ```
|
1176
|
+
*
|
1177
|
+
* Remember to handle the `signal` argument passed into the async generator.
|
1178
|
+
* Especially in the case where the async generator is the source for the
|
1179
|
+
* pipeline (i.e. first argument) or the pipeline will never complete.
|
1180
|
+
*
|
1181
|
+
* ```js
|
1182
|
+
* const { pipeline } = require('stream/promises');
|
1183
|
+
* const fs = require('fs');
|
1184
|
+
*
|
1185
|
+
* async function run() {
|
1186
|
+
* await pipeline(
|
1187
|
+
* async function* ({ signal }) {
|
1188
|
+
* await someLongRunningfn({ signal });
|
1189
|
+
* yield 'asd';
|
1190
|
+
* },
|
1191
|
+
* fs.createWriteStream('uppercase.txt')
|
1192
|
+
* );
|
1193
|
+
* console.log('Pipeline succeeded.');
|
1194
|
+
* }
|
1195
|
+
*
|
1196
|
+
* run().catch(console.error);
|
1197
|
+
* ```
|
1198
|
+
*
|
1199
|
+
* `stream.pipeline()` will call `stream.destroy(err)` on all streams except:
|
1200
|
+
*
|
1201
|
+
* * `Readable` streams which have emitted `'end'` or `'close'`.
|
1202
|
+
* * `Writable` streams which have emitted `'finish'` or `'close'`.
|
1203
|
+
*
|
1204
|
+
* `stream.pipeline()` leaves dangling event listeners on the streams
|
1205
|
+
* after the `callback` has been invoked. In the case of reuse of streams after
|
1206
|
+
* failure, this can cause event listener leaks and swallowed errors. If the last
|
1207
|
+
* stream is readable, dangling event listeners will be removed so that the last
|
1208
|
+
* stream can be consumed later.
|
1209
|
+
*
|
1210
|
+
* `stream.pipeline()` closes all the streams when an error is raised.
|
1211
|
+
* The `IncomingRequest` usage with `pipeline` could lead to an unexpected behavior
|
1212
|
+
* once it would destroy the socket without sending the expected response.
|
1213
|
+
* See the example below:
|
1214
|
+
*
|
1215
|
+
* ```js
|
1216
|
+
* const fs = require('fs');
|
1217
|
+
* const http = require('http');
|
1218
|
+
* const { pipeline } = require('stream');
|
1219
|
+
*
|
1220
|
+
* const server = http.createServer((req, res) => {
|
1221
|
+
* const fileStream = fs.createReadStream('./fileNotExist.txt');
|
1222
|
+
* pipeline(fileStream, res, (err) => {
|
1223
|
+
* if (err) {
|
1224
|
+
* console.log(err); // No such file
|
1225
|
+
* // this message can't be sent once `pipeline` already destroyed the socket
|
1226
|
+
* return res.end('error!!!');
|
1227
|
+
* }
|
1228
|
+
* });
|
1229
|
+
* });
|
1230
|
+
* ```
|
1231
|
+
* @since v10.0.0
|
1232
|
+
* @param callback Called when the pipeline is fully done.
|
1233
|
+
*/
|
1234
|
+
function pipeline<A extends PipelineSource<any>, B extends PipelineDestination<A, any>>(
|
1235
|
+
source: A,
|
1236
|
+
destination: B,
|
1237
|
+
callback?: PipelineCallback<B>
|
1238
|
+
): B extends NodeJS.WritableStream ? B : NodeJS.WritableStream;
|
1239
|
+
function pipeline<A extends PipelineSource<any>, T1 extends PipelineTransform<A, any>, B extends PipelineDestination<T1, any>>(
|
1240
|
+
source: A,
|
1241
|
+
transform1: T1,
|
1242
|
+
destination: B,
|
1243
|
+
callback?: PipelineCallback<B>
|
1244
|
+
): B extends NodeJS.WritableStream ? B : NodeJS.WritableStream;
|
1245
|
+
function pipeline<A extends PipelineSource<any>, T1 extends PipelineTransform<A, any>, T2 extends PipelineTransform<T1, any>, B extends PipelineDestination<T2, any>>(
|
1246
|
+
source: A,
|
1247
|
+
transform1: T1,
|
1248
|
+
transform2: T2,
|
1249
|
+
destination: B,
|
1250
|
+
callback?: PipelineCallback<B>
|
1251
|
+
): B extends NodeJS.WritableStream ? B : NodeJS.WritableStream;
|
1252
|
+
function pipeline<
|
1253
|
+
A extends PipelineSource<any>,
|
1254
|
+
T1 extends PipelineTransform<A, any>,
|
1255
|
+
T2 extends PipelineTransform<T1, any>,
|
1256
|
+
T3 extends PipelineTransform<T2, any>,
|
1257
|
+
B extends PipelineDestination<T3, any>
|
1258
|
+
>(source: A, transform1: T1, transform2: T2, transform3: T3, destination: B, callback?: PipelineCallback<B>): B extends NodeJS.WritableStream ? B : NodeJS.WritableStream;
|
1259
|
+
function pipeline<
|
1260
|
+
A extends PipelineSource<any>,
|
1261
|
+
T1 extends PipelineTransform<A, any>,
|
1262
|
+
T2 extends PipelineTransform<T1, any>,
|
1263
|
+
T3 extends PipelineTransform<T2, any>,
|
1264
|
+
T4 extends PipelineTransform<T3, any>,
|
1265
|
+
B extends PipelineDestination<T4, any>
|
1266
|
+
>(source: A, transform1: T1, transform2: T2, transform3: T3, transform4: T4, destination: B, callback?: PipelineCallback<B>): B extends NodeJS.WritableStream ? B : NodeJS.WritableStream;
|
1267
|
+
function pipeline(
|
1268
|
+
streams: ReadonlyArray<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>,
|
1269
|
+
callback?: (err: NodeJS.ErrnoException | null) => void
|
1270
|
+
): NodeJS.WritableStream;
|
1271
|
+
function pipeline(
|
1272
|
+
stream1: NodeJS.ReadableStream,
|
1273
|
+
stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
|
1274
|
+
...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream | ((err: NodeJS.ErrnoException | null) => void)>
|
1275
|
+
): NodeJS.WritableStream;
|
1276
|
+
namespace pipeline {
|
1277
|
+
function __promisify__<A extends PipelineSource<any>, B extends PipelineDestination<A, any>>(source: A, destination: B, options?: PipelineOptions): PipelinePromise<B>;
|
1278
|
+
function __promisify__<A extends PipelineSource<any>, T1 extends PipelineTransform<A, any>, B extends PipelineDestination<T1, any>>(
|
1279
|
+
source: A,
|
1280
|
+
transform1: T1,
|
1281
|
+
destination: B,
|
1282
|
+
options?: PipelineOptions
|
1283
|
+
): PipelinePromise<B>;
|
1284
|
+
function __promisify__<A extends PipelineSource<any>, T1 extends PipelineTransform<A, any>, T2 extends PipelineTransform<T1, any>, B extends PipelineDestination<T2, any>>(
|
1285
|
+
source: A,
|
1286
|
+
transform1: T1,
|
1287
|
+
transform2: T2,
|
1288
|
+
destination: B,
|
1289
|
+
options?: PipelineOptions
|
1290
|
+
): PipelinePromise<B>;
|
1291
|
+
function __promisify__<
|
1292
|
+
A extends PipelineSource<any>,
|
1293
|
+
T1 extends PipelineTransform<A, any>,
|
1294
|
+
T2 extends PipelineTransform<T1, any>,
|
1295
|
+
T3 extends PipelineTransform<T2, any>,
|
1296
|
+
B extends PipelineDestination<T3, any>
|
1297
|
+
>(source: A, transform1: T1, transform2: T2, transform3: T3, destination: B, options?: PipelineOptions): PipelinePromise<B>;
|
1298
|
+
function __promisify__<
|
1299
|
+
A extends PipelineSource<any>,
|
1300
|
+
T1 extends PipelineTransform<A, any>,
|
1301
|
+
T2 extends PipelineTransform<T1, any>,
|
1302
|
+
T3 extends PipelineTransform<T2, any>,
|
1303
|
+
T4 extends PipelineTransform<T3, any>,
|
1304
|
+
B extends PipelineDestination<T4, any>
|
1305
|
+
>(source: A, transform1: T1, transform2: T2, transform3: T3, transform4: T4, destination: B, options?: PipelineOptions): PipelinePromise<B>;
|
1306
|
+
function __promisify__(streams: ReadonlyArray<NodeJS.ReadableStream | NodeJS.WritableStream | NodeJS.ReadWriteStream>, options?: PipelineOptions): Promise<void>;
|
1307
|
+
function __promisify__(
|
1308
|
+
stream1: NodeJS.ReadableStream,
|
1309
|
+
stream2: NodeJS.ReadWriteStream | NodeJS.WritableStream,
|
1310
|
+
...streams: Array<NodeJS.ReadWriteStream | NodeJS.WritableStream | PipelineOptions>
|
1311
|
+
): Promise<void>;
|
1312
|
+
}
|
1313
|
+
interface Pipe {
|
1314
|
+
close(): void;
|
1315
|
+
hasRef(): boolean;
|
1316
|
+
ref(): void;
|
1317
|
+
unref(): void;
|
1318
|
+
}
|
1319
|
+
|
1320
|
+
/**
|
1321
|
+
* Returns whether the stream has encountered an error.
|
1322
|
+
* @since v17.3.0
|
1323
|
+
*/
|
1324
|
+
function isErrored(stream: Readable | Writable | NodeJS.ReadableStream | NodeJS.WritableStream): boolean;
|
1325
|
+
|
1326
|
+
/**
|
1327
|
+
* Returns whether the stream is readable.
|
1328
|
+
* @since v17.4.0
|
1329
|
+
*/
|
1330
|
+
function isReadable(stream: Readable | NodeJS.ReadableStream): boolean;
|
1331
|
+
|
1332
|
+
const promises: typeof streamPromises;
|
1333
|
+
const consumers: typeof streamConsumers;
|
1334
|
+
}
|
1335
|
+
export = internal;
|
1336
|
+
}
|
1337
|
+
declare module 'node:stream' {
|
1338
|
+
import stream = require('stream');
|
1339
|
+
export = stream;
|
1340
|
+
}
|