| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 53f9a90 commit ba7000e
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | 14 | const { | |
| 15 | 15 | ArrayIsArray, | |
| 16 | + ArrayPrototypePush, | ||
| 16 | 17 | MathMax, | |
| 17 | 18 | NumberMAX_SAFE_INTEGER, | |
| 18 | 19 | Promise, | |
@@ -107,14 +108,14 @@ async function normalizeBatch(raw) { | |||
| 107 | 108 | for (let i = 0; i < raw.length; i++) { | |
| 108 | 109 | const value = raw[i]; | |
| 109 | 110 | if (isUint8Array(value)) { | |
| 110 | - batch.push(value); | ||
| 111 | + ArrayPrototypePush(batch, value); | ||
| 111 | 112 | } else { | |
| 112 | 113 | // normalizeAsyncValue may await for async protocols (e.g. | |
| 113 | 114 | // toAsyncStreamable on yielded objects). Stream events during | |
| 114 | 115 | // the suspension are queued, not lost -- errors will surface | |
| 115 | 116 | // on the next loop iteration after this yield completes. | |
| 116 | 117 | for await (const normalized of normalizeAsyncValue(value)) { | |
| 117 | - batch.push(normalized); | ||
| 118 | + ArrayPrototypePush(batch, normalized); | ||
| 118 | 119 | } | |
| 119 | 120 | } | |
| 120 | 121 | } | |
@@ -163,7 +164,7 @@ async function* createBatchedAsyncIterator(stream, normalize) { | |||
| 163 | 164 | stream._readableState?.length > 0) { | |
| 164 | 165 | const c = stream.read(); | |
| 165 | 166 | if (c === null) break; | |
| 166 | - batch.push(c); | ||
| 167 | + ArrayPrototypePush(batch, c); | ||
| 167 | 168 | } | |
| 168 | 169 | if (normalize !== null) { | |
| 169 | 170 | const result = await normalize(batch); | |
@@ -495,7 +496,7 @@ function fromWritable(writable, options = kNullPrototype) { | |||
| 495 | 496 | ||
| 496 | 497 | function waitForDrain() { | |
| 497 | 498 | const { promise, resolve, reject } = PromiseWithResolvers(); | |
| 498 | - waiters.push({ __proto__: null, resolve, reject }); | ||
| 499 | + ArrayPrototypePush(waiters, { __proto__: null, resolve, reject }); | ||
| 499 | 500 | installListeners(); | |
| 500 | 501 | return promise; | |
| 501 | 502 | } | |
@@ -686,7 +687,7 @@ function fromWritable(writable, options = kNullPrototype) { | |||
| 686 | 687 | return PromiseResolve(true); | |
| 687 | 688 | } | |
| 688 | 689 | const { promise, resolve } = PromiseWithResolvers(); | |
| 689 | - waiters.push({ | ||
| 690 | + ArrayPrototypePush(waiters, { | ||
| 690 | 691 | __proto__: null, | |
| 691 | 692 | resolve() { resolve(true); }, | |
| 692 | 693 | reject() { resolve(false); }, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ const { | |||
| 12 | 12 | ArrayBufferPrototypeSlice, | |
| 13 | 13 | ArrayPrototypeMap, | |
| 14 | 14 | ArrayPrototypePush, | |
| 15 | + ArrayPrototypeShift, | ||
| 15 | 16 | ArrayPrototypeSlice, | |
| 16 | 17 | Promise, | |
| 17 | 18 | PromisePrototypeThen, | |
@@ -477,7 +478,7 @@ function merge(...args) { | |||
| 477 | 478 | ||
| 478 | 479 | // Drain ready queue synchronously | |
| 479 | 480 | while (ready.length > 0) { | |
| 480 | - const item = ready.shift(); | ||
| 481 | + const item = ArrayPrototypeShift(ready); | ||
| 481 | 482 | if (item?.error) { | |
| 482 | 483 | throw item.error; | |
| 483 | 484 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,8 +31,8 @@ const { | |||
| 31 | 31 | ||
| 32 | 32 | const { | |
| 33 | 33 | isAnyArrayBuffer, | |
| 34 | - isDataView, | ||
| 35 | 34 | isPromise, | |
| 35 | + isTypedArray, | ||
| 36 | 36 | isUint8Array, | |
| 37 | 37 | } = require('internal/util/types'); | |
| 38 | 38 | ||
@@ -106,17 +106,21 @@ function primitiveToUint8Array(chunk) { | |||
| 106 | 106 | return chunk; | |
| 107 | 107 | } | |
| 108 | 108 | // Other ArrayBufferView types (Int8Array, DataView, etc.) | |
| 109 | - if (isDataView(chunk)) { | ||
| 109 | + return arrayBufferViewToUint8Array(chunk); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + function arrayBufferViewToUint8Array(chunk) { | ||
| 113 | + if (isTypedArray(chunk)) { | ||
| 110 | 114 | return new Uint8Array( | |
| 111 | - DataViewPrototypeGetBuffer(chunk), | ||
| 112 | - DataViewPrototypeGetByteOffset(chunk), | ||
| 113 | - DataViewPrototypeGetByteLength(chunk), | ||
| 115 | + TypedArrayPrototypeGetBuffer(chunk), | ||
| 116 | + TypedArrayPrototypeGetByteOffset(chunk), | ||
| 117 | + TypedArrayPrototypeGetByteLength(chunk), | ||
| 114 | 118 | ); | |
| 115 | 119 | } | |
| 116 | 120 | return new Uint8Array( | |
| 117 | - TypedArrayPrototypeGetBuffer(chunk), | ||
| 118 | - TypedArrayPrototypeGetByteOffset(chunk), | ||
| 119 | - TypedArrayPrototypeGetByteLength(chunk), | ||
| 121 | + DataViewPrototypeGetBuffer(chunk), | ||
| 122 | + DataViewPrototypeGetByteOffset(chunk), | ||
| 123 | + DataViewPrototypeGetByteLength(chunk), | ||
| 120 | 124 | ); | |
| 121 | 125 | } | |
| 122 | 126 | ||
@@ -580,6 +584,7 @@ function from(input) { | |||
| 580 | 584 | // ============================================================================= | |
| 581 | 585 | ||
| 582 | 586 | module.exports = { | |
| 587 | + arrayBufferViewToUint8Array, | ||
| 583 | 588 | from, | |
| 584 | 589 | fromSync, | |
| 585 | 590 | isAsyncIterable, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,9 +33,9 @@ const { | |||
| 33 | 33 | const { AbortController } = require('internal/abort_controller'); | |
| 34 | 34 | ||
| 35 | 35 | const { | |
| 36 | + arrayBufferViewToUint8Array, | ||
| 36 | 37 | from, | |
| 37 | 38 | fromSync, | |
| 38 | - primitiveToUint8Array, | ||
| 39 | 39 | isSyncIterable, | |
| 40 | 40 | isAsyncIterable, | |
| 41 | 41 | isUint8ArrayBatch, | |
@@ -136,7 +136,7 @@ function* flattenTransformYieldSync(value) { | |||
| 136 | 136 | return; | |
| 137 | 137 | } | |
| 138 | 138 | if (ArrayBufferIsView(value)) { | |
| 139 | - yield primitiveToUint8Array(value); | ||
| 139 | + yield arrayBufferViewToUint8Array(value); | ||
| 140 | 140 | return; | |
| 141 | 141 | } | |
| 142 | 142 | // Must be Iterable<TransformYield> | |
@@ -170,7 +170,7 @@ async function* flattenTransformYieldAsync(value) { | |||
| 170 | 170 | return; | |
| 171 | 171 | } | |
| 172 | 172 | if (ArrayBufferIsView(value)) { | |
| 173 | - yield primitiveToUint8Array(value); | ||
| 173 | + yield arrayBufferViewToUint8Array(value); | ||
| 174 | 174 | return; | |
| 175 | 175 | } | |
| 176 | 176 | // Check for async iterable first | |
@@ -180,10 +180,10 @@ async function* flattenTransformYieldAsync(value) { | |||
| 180 | 180 | } | |
| 181 | 181 | return; | |
| 182 | 182 | } | |
| 183 | - // Must be sync Iterable<TransformYield> | ||
| 183 | + // Must be sync Iterable<TransformYield>, no nested async iterables | ||
| 184 | 184 | if (isSyncIterable(value)) { | |
| 185 | 185 | for (const item of value) { | |
| 186 | - yield* flattenTransformYieldAsync(item); | ||
| 186 | + yield* flattenTransformYieldSync(item); | ||
| 187 | 187 | } | |
| 188 | 188 | return; | |
| 189 | 189 | } | |
@@ -218,7 +218,7 @@ function* processTransformResultSync(result) { | |||
| 218 | 218 | return; | |
| 219 | 219 | } | |
| 220 | 220 | if (ArrayBufferIsView(result)) { | |
| 221 | - yield [primitiveToUint8Array(result)]; | ||
| 221 | + yield [arrayBufferViewToUint8Array(result)]; | ||
| 222 | 222 | return; | |
| 223 | 223 | } | |
| 224 | 224 | // Uint8Array[] batch | |
@@ -278,7 +278,7 @@ async function* processTransformResultAsync(result) { | |||
| 278 | 278 | return; | |
| 279 | 279 | } | |
| 280 | 280 | if (ArrayBufferIsView(result)) { | |
| 281 | - yield [primitiveToUint8Array(result)]; | ||
| 281 | + yield [arrayBufferViewToUint8Array(result)]; | ||
| 282 | 282 | return; | |
| 283 | 283 | } | |
| 284 | 284 | // Uint8Array[] batch | |
@@ -313,7 +313,9 @@ async function* processTransformResultAsync(result) { | |||
| 313 | 313 | ArrayPrototypePush(batch, item); | |
| 314 | 314 | continue; | |
| 315 | 315 | } | |
| 316 | - for await (const chunk of flattenTransformYieldAsync(item)) { | ||
| 316 | + // Note: This iteration is synchronous, since async iterables | ||
| 317 | + // may not be nested within sync iterables. | ||
| 318 | + for (const chunk of flattenTransformYieldSync(item)) { | ||
| 317 | 319 | ArrayPrototypePush(batch, chunk); | |
| 318 | 320 | } | |
| 319 | 321 | } | |
@@ -366,7 +368,7 @@ function* applyFusedStatelessSyncTransforms(source, run) { | |||
| 366 | 368 | } else if (isAnyArrayBuffer(current)) { | |
| 367 | 369 | yield [new Uint8Array(current)]; | |
| 368 | 370 | } else if (ArrayBufferIsView(current)) { | |
| 369 | - yield [primitiveToUint8Array(current)]; | ||
| 371 | + yield [arrayBufferViewToUint8Array(current)]; | ||
| 370 | 372 | } else { | |
| 371 | 373 | yield* processTransformResultSync(current); | |
| 372 | 374 | } | |
@@ -428,7 +430,7 @@ function* createSyncPipeline(source, transforms) { | |||
| 428 | 430 | } | |
| 429 | 431 | current = applyStatefulSyncTransform(current, transform.transform); | |
| 430 | 432 | } else { | |
| 431 | - statelessRun.push(transform); | ||
| 433 | + ArrayPrototypePush(statelessRun, transform); | ||
| 432 | 434 | } | |
| 433 | 435 | } | |
| 434 | 436 | if (statelessRun.length > 0) { | |
@@ -490,7 +492,7 @@ async function* applyFusedStatelessAsyncTransforms(source, run, signal) { | |||
| 490 | 492 | } else if (isAnyArrayBuffer(current)) { | |
| 491 | 493 | yield [new Uint8Array(current)]; | |
| 492 | 494 | } else if (ArrayBufferIsView(current)) { | |
| 493 | - yield [primitiveToUint8Array(current)]; | ||
| 495 | + yield [arrayBufferViewToUint8Array(current)]; | ||
| 494 | 496 | } else { | |
| 495 | 497 | yield* processTransformResultAsync(current); | |
| 496 | 498 | } | |
@@ -531,9 +533,7 @@ async function* applyFusedStatelessAsyncTransforms(source, run, signal) { | |||
| 531 | 533 | * @yields {Uint8Array[]} | |
| 532 | 534 | */ | |
| 533 | 535 | async function* withFlushAsync(source) { | |
| 534 | - for await (const batch of source) { | ||
| 535 | - yield batch; | ||
| 536 | - } | ||
| 536 | + yield* source; | ||
| 537 | 537 | yield null; | |
| 538 | 538 | } | |
| 539 | 539 | ||
@@ -647,7 +647,7 @@ async function* createAsyncPipeline(source, transforms, signal) { | |||
| 647 | 647 | current, transform.transform, opts); | |
| 648 | 648 | } | |
| 649 | 649 | } else { | |
| 650 | - statelessRun.push(transform); | ||
| 650 | + ArrayPrototypePush(statelessRun, transform); | ||
| 651 | 651 | } | |
| 652 | 652 | } | |
| 653 | 653 | // Flush remaining stateless run | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,6 +12,7 @@ const { | |||
| 12 | 12 | TypedArrayPrototypeGetBuffer, | |
| 13 | 13 | TypedArrayPrototypeGetByteLength, | |
| 14 | 14 | TypedArrayPrototypeGetByteOffset, | |
| 15 | + TypedArrayPrototypeSet, | ||
| 15 | 16 | Uint8Array, | |
| 16 | 17 | } = primordials; | |
| 17 | 18 | ||
@@ -24,8 +25,6 @@ const { | |||
| 24 | 25 | } = require('internal/errors'); | |
| 25 | 26 | const { isError } = require('internal/util'); | |
| 26 | 27 | ||
| 27 | - const { Buffer } = require('buffer'); | ||
| 28 | - | ||
| 29 | 28 | const { isSharedArrayBuffer, isUint8Array } = require('internal/util/types'); | |
| 30 | 29 | ||
| 31 | 30 | const { validateOneOf } = require('internal/validators'); | |
@@ -127,26 +126,36 @@ function concatBytes(chunks) { | |||
| 127 | 126 | if (chunks.length === 0) { | |
| 128 | 127 | return new Uint8Array(0); | |
| 129 | 128 | } | |
| 130 | - // Single chunk: return directly if it covers the entire backing buffer | ||
| 129 | + // Single chunk: return directly if it covers the entire backing buffer, | ||
| 130 | + // otherwise return a copy | ||
| 131 | 131 | if (chunks.length === 1) { | |
| 132 | 132 | const chunk = chunks[0]; | |
| 133 | - const buf = TypedArrayPrototypeGetBuffer(chunk); | ||
| 134 | - // SharedArrayBuffer is not available in primordials, so use | ||
| 135 | - // direct property access for its byteLength. | ||
| 136 | - const bufByteLength = isSharedArrayBuffer(buf) ? | ||
| 137 | - buf.byteLength : | ||
| 138 | - ArrayBufferPrototypeGetByteLength(buf); | ||
| 139 | - if (TypedArrayPrototypeGetByteOffset(chunk) === 0 && | ||
| 140 | - TypedArrayPrototypeGetByteLength(chunk) === bufByteLength) { | ||
| 141 | - return chunk; | ||
| 133 | + // If non-zero offset, skip the remaining buffer checks. | ||
| 134 | + if (TypedArrayPrototypeGetByteOffset(chunk) === 0) { | ||
| 135 | + const buf = TypedArrayPrototypeGetBuffer(chunk); | ||
| 136 | + // SharedArrayBuffer is not available in primordials, so use | ||
| 137 | + // direct property access for its byteLength. | ||
| 138 | + const bufByteLength = isSharedArrayBuffer(buf) ? | ||
| 139 | + buf.byteLength : | ||
| 140 | + ArrayBufferPrototypeGetByteLength(buf); | ||
| 141 | + if (TypedArrayPrototypeGetByteLength(chunk) === bufByteLength) { | ||
| 142 | + return chunk; | ||
| 143 | + } | ||
| 142 | 144 | } | |
| 145 | + return new Uint8Array(chunk); | ||
| 146 | + } | ||
| 147 | + // Multiple chunks: concatenate | ||
| 148 | + let totalByteLength = 0; | ||
| 149 | + for (let i = 0; i < chunks.length; i++) { | ||
| 150 | + totalByteLength += TypedArrayPrototypeGetByteLength(chunks[i]); | ||
| 151 | + } | ||
| 152 | + const concatenated = new Uint8Array(totalByteLength); | ||
| 153 | + let offset = 0; | ||
| 154 | + for (let i = 0; i < chunks.length; i++) { | ||
| 155 | + TypedArrayPrototypeSet(concatenated, chunks[i], offset); | ||
| 156 | + offset += TypedArrayPrototypeGetByteLength(chunks[i]); | ||
| 143 | 157 | } | |
| 144 | - // Multiple chunks or shared buffer: concatenate | ||
| 145 | - const buf = Buffer.concat(chunks); | ||
| 146 | - return new Uint8Array( | ||
| 147 | - TypedArrayPrototypeGetBuffer(buf), | ||
| 148 | - TypedArrayPrototypeGetByteOffset(buf), | ||
| 149 | - TypedArrayPrototypeGetByteLength(buf)); | ||
| 158 | + return concatenated; | ||
| 150 | 159 | } | |
| 151 | 160 | ||
| 152 | 161 | /** | |
| Back | FazBrowse Home | New Git URL |
0 commit comments