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

Function concatBytes

lib/internal/streams/iter/utils.js:128–163  ·  view source on GitHub ↗

* Concatenate multiple Uint8Arrays into a single Uint8Array. * @param {Uint8Array[]} chunks * @returns {Uint8Array}

(chunks)

Source from the content-addressed store, hash-verified

126 * @returns {Uint8Array}
127 */
128function concatBytes(chunks) {
129 // Empty stream: return zero-length Uint8Array
130 if (chunks.length === 0) {
131 return new Uint8Array(0);
132 }
133 // Single chunk: return directly if it covers the entire backing buffer,
134 // otherwise return a copy
135 if (chunks.length === 1) {
136 const chunk = chunks[0];
137 // If non-zero offset, skip the remaining buffer checks.
138 if (TypedArrayPrototypeGetByteOffset(chunk) === 0) {
139 const buf = TypedArrayPrototypeGetBuffer(chunk);
140 // SharedArrayBuffer is not available in primordials, so use
141 // direct property access for its byteLength.
142 const bufByteLength = isSharedArrayBuffer(buf) ?
143 buf.byteLength :
144 ArrayBufferPrototypeGetByteLength(buf);
145 if (TypedArrayPrototypeGetByteLength(chunk) === bufByteLength) {
146 return chunk;
147 }
148 }
149 return new Uint8Array(chunk);
150 }
151 // Multiple chunks: concatenate
152 let totalByteLength = 0;
153 for (let i = 0; i < chunks.length; i++) {
154 totalByteLength += TypedArrayPrototypeGetByteLength(chunks[i]);
155 }
156 const concatenated = new Uint8Array(totalByteLength);
157 let offset = 0;
158 for (let i = 0; i < chunks.length; i++) {
159 TypedArrayPrototypeSet(concatenated, chunks[i], offset);
160 offset += TypedArrayPrototypeGetByteLength(chunks[i]);
161 }
162 return concatenated;
163}
164
165/**
166 * Convert an array of chunks (strings or Uint8Arrays) to a Uint8Array[].

Callers 6

bytesSyncFunction · 0.85
textSyncFunction · 0.85
arrayBufferSyncFunction · 0.85
bytesFunction · 0.85
textFunction · 0.85
arrayBufferFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…