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

Function from

lib/internal/streams/iter/broadcast.js:792–846  ·  view source on GitHub ↗
(input, options)

Source from the content-addressed store, hash-verified

790const Broadcast = {
791 __proto__: null,
792 from(input, options) {
793 if (isBroadcastable(input)) {
794 const bc = input[broadcastProtocol](options);
795 if (bc === null || typeof bc !== 'object') {
796 throw new ERR_INVALID_RETURN_VALUE(
797 'an object', '[Symbol.for(\'Stream.broadcastProtocol\')]', bc);
798 }
799 return { __proto__: null, writer: { __proto__: null }, broadcast: bc };
800 }
801
802 if (!isAsyncIterable(input) && !isSyncIterable(input)) {
803 throw new ERR_INVALID_ARG_TYPE(
804 'input', ['Broadcastable', 'AsyncIterable', 'Iterable'], input);
805 }
806
807 const result = broadcast(options);
808 const signal = options?.signal;
809
810 const pump = async () => {
811 const w = result.writer;
812 try {
813 if (isAsyncIterable(input)) {
814 for await (const chunks of input) {
815 signal?.throwIfAborted();
816 if (ArrayIsArray(chunks)) {
817 if (!w.writevSync(chunks)) {
818 await w.writev(chunks, signal ? { signal } : undefined);
819 }
820 } else if (!w.writeSync(chunks)) {
821 await w.write(chunks, signal ? { signal } : undefined);
822 }
823 }
824 } else if (isSyncIterable(input)) {
825 for (const chunks of input) {
826 signal?.throwIfAborted();
827 if (ArrayIsArray(chunks)) {
828 if (!w.writevSync(chunks)) {
829 await w.writev(chunks, signal ? { signal } : undefined);
830 }
831 } else if (!w.writeSync(chunks)) {
832 await w.write(chunks, signal ? { signal } : undefined);
833 }
834 }
835 }
836 if (w.endSync() < 0) {
837 await w.end(signal ? { signal } : undefined);
838 }
839 } catch (error) {
840 w.fail(wrapError(error));
841 }
842 };
843 PromisePrototypeThen(pump(), undefined, () => {});
844
845 return result;
846 },
847};
848
849module.exports = {

Callers

nothing calls this directly

Calls 5

isBroadcastableFunction · 0.85
isAsyncIterableFunction · 0.70
isSyncIterableFunction · 0.70
broadcastFunction · 0.70
pumpFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…