| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7fd5f66 commit d4dfb66
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,8 @@ const { | |||
| 22 | 22 | TypedArrayPrototypeGetBuffer, | |
| 23 | 23 | TypedArrayPrototypeGetByteLength, | |
| 24 | 24 | TypedArrayPrototypeGetByteOffset, | |
| 25 | + TypedArrayPrototypeSet, | ||
| 26 | + Uint8Array, | ||
| 25 | 27 | } = primordials; | |
| 26 | 28 | ||
| 27 | 29 | const { | |
@@ -164,21 +166,17 @@ async function collectAsync(source, signal, limit) { | |||
| 164 | 166 | ||
| 165 | 167 | /** | |
| 166 | 168 | * Convert a Uint8Array to its backing ArrayBuffer, slicing if necessary. | |
| 167 | - * Handles both ArrayBuffer and SharedArrayBuffer backing stores. | ||
| 168 | 169 | * @param {Uint8Array} data | |
| 169 | - * @returns {ArrayBuffer|SharedArrayBuffer} | ||
| 170 | + * @returns {ArrayBuffer} | ||
| 170 | 171 | */ | |
| 171 | 172 | function toArrayBuffer(data) { | |
| 172 | 173 | const byteOffset = TypedArrayPrototypeGetByteOffset(data); | |
| 173 | 174 | const byteLength = TypedArrayPrototypeGetByteLength(data); | |
| 174 | 175 | const buffer = TypedArrayPrototypeGetBuffer(data); | |
| 175 | - // SharedArrayBuffer is not available in primordials, so use | ||
| 176 | - // direct property access for its byteLength and slice. | ||
| 177 | 176 | if (isSharedArrayBuffer(buffer)) { | |
| 178 | - if (byteOffset === 0 && byteLength === buffer.byteLength) { | ||
| 179 | - return buffer; | ||
| 180 | - } | ||
| 181 | - return buffer.slice(byteOffset, byteOffset + byteLength); | ||
| 177 | + const copy = new Uint8Array(byteLength); | ||
| 178 | + TypedArrayPrototypeSet(copy, data); | ||
| 179 | + return TypedArrayPrototypeGetBuffer(copy); | ||
| 182 | 180 | } | |
| 183 | 181 | if (byteOffset === 0 && | |
| 184 | 182 | byteLength === ArrayBufferPrototypeGetByteLength(buffer)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -203,6 +203,12 @@ function allUint8Array(chunks) { | |||
| 203 | 203 | return true; | |
| 204 | 204 | } | |
| 205 | 205 | ||
| 206 | + function copyBytes(chunk) { | ||
| 207 | + const copy = new Uint8Array(TypedArrayPrototypeGetByteLength(chunk)); | ||
| 208 | + TypedArrayPrototypeSet(copy, chunk); | ||
| 209 | + return copy; | ||
| 210 | + } | ||
| 211 | + | ||
| 206 | 212 | /** | |
| 207 | 213 | * Concatenate multiple Uint8Arrays into a single Uint8Array. | |
| 208 | 214 | * @param {Uint8Array[]} chunks | |
@@ -220,16 +226,15 @@ function concatBytes(chunks) { | |||
| 220 | 226 | // If non-zero offset, skip the remaining buffer checks. | |
| 221 | 227 | if (TypedArrayPrototypeGetByteOffset(chunk) === 0) { | |
| 222 | 228 | const buf = TypedArrayPrototypeGetBuffer(chunk); | |
| 223 | - // SharedArrayBuffer is not available in primordials, so use | ||
| 224 | - // direct property access for its byteLength. | ||
| 225 | - const bufByteLength = isSharedArrayBuffer(buf) ? | ||
| 226 | - buf.byteLength : | ||
| 227 | - ArrayBufferPrototypeGetByteLength(buf); | ||
| 228 | - if (TypedArrayPrototypeGetByteLength(chunk) === bufByteLength) { | ||
| 229 | + if ( | ||
| 230 | + !isSharedArrayBuffer(buf) && | ||
| 231 | + TypedArrayPrototypeGetByteLength(chunk) === | ||
| 232 | + ArrayBufferPrototypeGetByteLength(buf) | ||
| 233 | + ) { | ||
| 229 | 234 | return chunk; | |
| 230 | 235 | } | |
| 231 | 236 | } | |
| 232 | - return new Uint8Array(chunk); | ||
| 237 | + return copyBytes(chunk); | ||
| 233 | 238 | } | |
| 234 | 239 | // Multiple chunks: concatenate | |
| 235 | 240 | let totalByteLength = 0; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -52,13 +52,15 @@ function testBytesSyncSAB() { | |||
| 52 | 52 | const sab = new SharedArrayBuffer(5); | |
| 53 | 53 | new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' | |
| 54 | 54 | const data = bytesSync(fromSync(sab)); | |
| 55 | + assert.ok(data.buffer instanceof ArrayBuffer); | ||
| 55 | 56 | assert.deepStrictEqual(data, new Uint8Array([104, 101, 108, 108, 111])); | |
| 56 | 57 | } | |
| 57 | 58 | ||
| 58 | 59 | async function testBytesAsyncSAB() { | |
| 59 | 60 | const sab = new SharedArrayBuffer(5); | |
| 60 | 61 | new Uint8Array(sab).set([104, 101, 108, 108, 111]); // 'hello' | |
| 61 | 62 | const data = await bytes(from(sab)); | |
| 63 | + assert.ok(data.buffer instanceof ArrayBuffer); | ||
| 62 | 64 | assert.deepStrictEqual(data, new Uint8Array([104, 101, 108, 108, 111])); | |
| 63 | 65 | } | |
| 64 | 66 | ||
@@ -80,15 +82,15 @@ function testArrayBufferSyncSAB() { | |||
| 80 | 82 | const sab = new SharedArrayBuffer(4); | |
| 81 | 83 | new Uint8Array(sab).set([1, 2, 3, 4]); | |
| 82 | 84 | const result = arrayBufferSync(fromSync(sab)); | |
| 83 | - assert.ok(result instanceof SharedArrayBuffer || result instanceof ArrayBuffer); | ||
| 85 | + assert.ok(result instanceof ArrayBuffer); | ||
| 84 | 86 | assert.deepStrictEqual(new Uint8Array(result), new Uint8Array([1, 2, 3, 4])); | |
| 85 | 87 | } | |
| 86 | 88 | ||
| 87 | 89 | async function testArrayBufferAsyncSAB() { | |
| 88 | 90 | const sab = new SharedArrayBuffer(4); | |
| 89 | 91 | new Uint8Array(sab).set([1, 2, 3, 4]); | |
| 90 | 92 | const result = await arrayBuffer(from(sab)); | |
| 91 | - assert.ok(result instanceof SharedArrayBuffer || result instanceof ArrayBuffer); | ||
| 93 | + assert.ok(result instanceof ArrayBuffer); | ||
| 92 | 94 | assert.deepStrictEqual(new Uint8Array(result), new Uint8Array([1, 2, 3, 4])); | |
| 93 | 95 | } | |
| 94 | 96 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments