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

Function newWritableStreamFromStreamBase

lib/internal/webstreams/adapters.js:958–1028  ·  view source on GitHub ↗

* @typedef {import('./queuingstrategies').QueuingStrategy} QueuingStrategy * @param {object} streamBase * @param {QueuingStrategy} strategy * @returns {WritableStream}

(streamBase, strategy)

Source from the content-addressed store, hash-verified

956 * @returns {WritableStream}
957 */
958function newWritableStreamFromStreamBase(streamBase, strategy) {
959 validateObject(streamBase, 'streamBase');
960
961 let current;
962
963 function createWriteWrap(controller, promise) {
964 const req = new WriteWrap();
965 req.handle = streamBase;
966 req.oncomplete = onWriteComplete;
967 req.async = false;
968 req.bytes = 0;
969 req.buffer = null;
970 req.controller = controller;
971 req.promise = promise;
972 return req;
973 }
974
975 function onWriteComplete(status) {
976 if (status < 0) {
977 const error = new ErrnoException(status, 'write', this.error);
978 this.promise.reject(error);
979 this.controller.error(error);
980 return;
981 }
982 this.promise.resolve();
983 }
984
985 function doWrite(chunk, controller) {
986 const promise = PromiseWithResolvers();
987 let ret;
988 let req;
989 try {
990 req = createWriteWrap(controller, promise);
991 ret = streamBase.writeBuffer(req, chunk);
992 if (streamBaseState[kLastWriteWasAsync])
993 req.buffer = chunk;
994 req.async = !!streamBaseState[kLastWriteWasAsync];
995 } catch (error) {
996 promise.reject(error);
997 }
998
999 if (ret !== 0)
1000 promise.reject(new ErrnoException(ret, 'write', req));
1001 else if (!req.async)
1002 promise.resolve();
1003
1004 return promise.promise;
1005 }
1006
1007 return new WritableStream({
1008 write(chunk, controller) {
1009 current = current !== undefined ?
1010 PromisePrototypeThen(
1011 current,
1012 () => doWrite(chunk, controller),
1013 (error) => controller.error(error)) :
1014 doWrite(chunk, controller);
1015 return current;

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…