MCPcopy
hub / github.com/axios/axios / trackStream

Function trackStream

lib/helpers/trackStream.js:45–89  ·  view source on GitHub ↗
(stream, chunkSize, onProgress, onFinish)

Source from the content-addressed store, hash-verified

43};
44
45export const trackStream = (stream, chunkSize, onProgress, onFinish) => {
46 const iterator = readBytes(stream, chunkSize);
47
48 let bytes = 0;
49 let done;
50 let _onFinish = (e) => {
51 if (!done) {
52 done = true;
53 onFinish && onFinish(e);
54 }
55 };
56
57 return new ReadableStream(
58 {
59 async pull(controller) {
60 try {
61 const { done, value } = await iterator.next();
62
63 if (done) {
64 _onFinish();
65 controller.close();
66 return;
67 }
68
69 let len = value.byteLength;
70 if (onProgress) {
71 let loadedBytes = (bytes += len);
72 onProgress(loadedBytes);
73 }
74 controller.enqueue(new Uint8Array(value));
75 } catch (err) {
76 _onFinish(err);
77 throw err;
78 }
79 },
80 cancel(reason) {
81 _onFinish(reason);
82 return iterator.return();
83 },
84 },
85 {
86 highWaterMark: 2,
87 }
88 );
89};

Callers 2

trackRequestStreamFunction · 0.90
factoryFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected