@quilted/threads
Advanced tools
Comparing version 2.3.0 to 2.4.0
# @quilted/threads | ||
## 2.4.0 | ||
### Minor Changes | ||
- [`004395d`](https://github.com/lemonmade/quilt/commit/004395de11b0f1ebd0afd0d66c845e619ab92ab0) Thanks [@lemonmade](https://github.com/lemonmade)! - Add support for serializing unsigned integer arrays | ||
## 2.3.0 | ||
@@ -4,0 +10,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"type": "module", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "engines": { |
@@ -20,2 +20,5 @@ import {ENCODE_METHOD, TRANSFERABLE} from '../constants.ts'; | ||
const ASYNC_ITERATOR = '_@i'; | ||
const UINT8_ARRAY = '_@u8'; | ||
const UINT16_ARRAY = '_@u16'; | ||
const UINT32_ARRAY = '_@u32'; | ||
@@ -128,2 +131,23 @@ export interface ThreadEncoderOptions { | ||
if (value instanceof Uint8Array) { | ||
const result = {[UINT8_ARRAY]: encodeUintArray(value)}; | ||
const fullResult: EncodeResult = [result, transferables]; | ||
seen.set(value, fullResult); | ||
return fullResult; | ||
} | ||
if (value instanceof Uint16Array) { | ||
const result = {[UINT16_ARRAY]: encodeUintArray(value)}; | ||
const fullResult: EncodeResult = [result, transferables]; | ||
seen.set(value, fullResult); | ||
return fullResult; | ||
} | ||
if (value instanceof Uint32Array) { | ||
const result = {[UINT32_ARRAY]: encodeUintArray(value)}; | ||
const fullResult: EncodeResult = [result, transferables]; | ||
seen.set(value, fullResult); | ||
return fullResult; | ||
} | ||
// TODO: avoid this if using a `structuredClone` postMessage-ing object? | ||
@@ -248,2 +272,22 @@ if (value instanceof RegExp) { | ||
if (UINT8_ARRAY in value) { | ||
return decodeUintArray((value as {[UINT8_ARRAY]: string})[UINT8_ARRAY]); | ||
} | ||
if (UINT16_ARRAY in value) { | ||
return new Uint16Array( | ||
decodeUintArray( | ||
(value as {[UINT16_ARRAY]: string})[UINT16_ARRAY], | ||
).buffer, | ||
); | ||
} | ||
if (UINT32_ARRAY in value) { | ||
return new Uint32Array( | ||
decodeUintArray( | ||
(value as {[UINT32_ARRAY]: string})[UINT32_ARRAY], | ||
).buffer, | ||
); | ||
} | ||
if (REGEXP in value) { | ||
@@ -324,1 +368,24 @@ return new RegExp(...(value as {[REGEXP]: [string, string]})[REGEXP]); | ||
} | ||
function encodeUintArray(array: Uint8Array | Uint16Array | Uint32Array) { | ||
let binary = ''; | ||
const bytes = new Uint8Array(array.buffer); | ||
const length = bytes.byteLength; | ||
for (let i = 0; i < length; i++) { | ||
binary += String.fromCharCode(bytes[i]!); | ||
} | ||
return btoa(binary); | ||
} | ||
function decodeUintArray(base64String: string) { | ||
const binary = atob(base64String); | ||
const result = new Uint8Array(binary.length); | ||
for (let i = 0; i < binary.length; i++) { | ||
result[i] = binary.charCodeAt(i); | ||
} | ||
return result; | ||
} |
@@ -116,2 +116,5 @@ import {describe, it, expect, vi} from 'vitest'; | ||
error: Error; | ||
uint8Array: Uint8Array; | ||
uint16Array: Uint16Array; | ||
uint32Array: Uint32Array; | ||
} | ||
@@ -153,2 +156,5 @@ | ||
error: new Error('error'), | ||
uint8Array: new Uint8Array([1]), | ||
uint16Array: new Uint16Array([257]), | ||
uint32Array: new Uint32Array([65536]), | ||
}; | ||
@@ -155,0 +161,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
250005
3801