MCPcopy Index your code
hub / github.com/nodejs/node / newWritableStreamFromStreamWritable

Function newWritableStreamFromStreamWritable

lib/internal/webstreams/adapters.js:160–283  ·  view source on GitHub ↗

* @param {Writable} streamWritable * @param {object} [options] * @returns {WritableStream}

(streamWritable, options = kEmptyObject)

Source from the content-addressed store, hash-verified

158 * @returns {WritableStream}
159 */
160function newWritableStreamFromStreamWritable(streamWritable, options = kEmptyObject) {
161 // Not using the internal/streams/utils isWritableNodeStream utility
162 // here because it will return false if streamWritable is a Duplex
163 // whose writable option is false. For a Duplex that is not writable,
164 // we want it to pass this check but return a closed WritableStream.
165 // We check if the given stream is a stream.Writable or http.OutgoingMessage
166 const checkIfWritableOrOutgoingMessage =
167 streamWritable &&
168 typeof streamWritable?.write === 'function' &&
169 typeof streamWritable?.on === 'function';
170 if (!checkIfWritableOrOutgoingMessage) {
171 throw new ERR_INVALID_ARG_TYPE(
172 'streamWritable',
173 'stream.Writable',
174 streamWritable,
175 );
176 }
177
178 if (isDestroyed(streamWritable) || !isWritable(streamWritable)) {
179 const writable = new WritableStream();
180 writable.close();
181 return writable;
182 }
183
184 const highWaterMark = streamWritable.writableHighWaterMark;
185 const strategy =
186 streamWritable.writableObjectMode ?
187 new CountQueuingStrategy({ highWaterMark }) :
188 {
189 highWaterMark,
190 size(chunk) {
191 return chunk?.byteLength ?? chunk?.length ?? 1;
192 },
193 };
194
195 let controller;
196 let backpressurePromise;
197 let closed;
198
199 function onDrain() {
200 backpressurePromise?.resolve();
201 }
202
203 const cleanup = eos(streamWritable, (error) => {
204 error = handleKnownInternalErrors(error);
205
206 cleanup();
207 // This is a protection against non-standard, legacy streams
208 // that happen to emit an error event again after finished is called.
209 streamWritable.on('error', () => {});
210 if (error != null) {
211 backpressurePromise?.reject(error);
212 // If closed is not undefined, the error is happening
213 // after the WritableStream close has already started.
214 // We need to reject it here.
215 if (closed !== undefined) {
216 closed.reject(error);
217 closed = undefined;

Calls 10

closeMethod · 0.95
eosFunction · 0.85
rejectMethod · 0.80
isDestroyedFunction · 0.50
isWritableFunction · 0.50
cleanupFunction · 0.50
onMethod · 0.45
errorMethod · 0.45
resolveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…