Skip to content

Commit be2c78a

Browse files
committed
Fix comlink serialization by converting Buffer to Uint8Array at worker boundary
1 parent f80db24 commit be2c78a

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

packages/sdk/src/encryption/EncryptionService.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ export class EncryptionService {
3636
* Note: The input data buffer is transferred to the worker and becomes unusable after this call.
3737
*/
3838
async encryptWithAES(data: Uint8Array, cipherKey: Uint8Array): Promise<Uint8Array> {
39+
// Ensure we have plain Uint8Array instances for worker communication (not Buffer subclass)
40+
const dataArray = new Uint8Array(data)
41+
const keyArray = new Uint8Array(cipherKey)
3942
const result = await this.getWorkerApi().encrypt(
40-
transfer({ data, cipherKey }, [data.buffer])
43+
transfer({ data: dataArray, cipherKey: keyArray }, [dataArray.buffer])
4144
)
4245
if (result.type === 'error') {
4346
throw new Error(`AES encryption failed: ${result.message}`)
@@ -49,10 +52,11 @@ export class EncryptionService {
4952
* Encrypt the next group key using the current group key.
5053
*/
5154
async encryptNextGroupKey(currentKey: GroupKey, nextKey: GroupKey): Promise<EncryptedGroupKey> {
55+
// Convert Buffer to Uint8Array for worker communication
5256
const result = await this.getWorkerApi().encryptGroupKey({
5357
nextGroupKeyId: nextKey.id,
54-
nextGroupKeyData: nextKey.data,
55-
currentGroupKeyData: currentKey.data
58+
nextGroupKeyData: new Uint8Array(nextKey.data),
59+
currentGroupKeyData: new Uint8Array(currentKey.data)
5660
})
5761
if (result.type === 'error') {
5862
throw new Error(`Group key encryption failed: ${result.message}`)
@@ -67,10 +71,11 @@ export class EncryptionService {
6771
* Decrypt an encrypted group key using the current group key.
6872
*/
6973
async decryptNextGroupKey(currentKey: GroupKey, encryptedKey: EncryptedGroupKey): Promise<GroupKey> {
74+
// Convert Buffer to Uint8Array for worker communication
7075
const result = await this.getWorkerApi().decryptGroupKey({
7176
encryptedGroupKeyId: encryptedKey.id,
72-
encryptedGroupKeyData: encryptedKey.data,
73-
currentGroupKeyData: currentKey.data
77+
encryptedGroupKeyData: new Uint8Array(encryptedKey.data),
78+
currentGroupKeyData: new Uint8Array(currentKey.data)
7479
})
7580
if (result.type === 'error') {
7681
throw new Error(`Group key decryption failed: ${result.message}`)
@@ -88,12 +93,13 @@ export class EncryptionService {
8893
groupKey: GroupKey,
8994
encryptedNewGroupKey?: EncryptedGroupKey
9095
): Promise<[Uint8Array, GroupKey?]> {
96+
// Convert Buffer to Uint8Array for worker communication
9197
const request = {
9298
content,
93-
groupKeyData: groupKey.data,
99+
groupKeyData: new Uint8Array(groupKey.data),
94100
newGroupKey: encryptedNewGroupKey ? {
95101
id: encryptedNewGroupKey.id,
96-
data: encryptedNewGroupKey.data
102+
data: new Uint8Array(encryptedNewGroupKey.data)
97103
} : undefined
98104
}
99105
const result = await this.getWorkerApi().decryptStreamMessage(

0 commit comments

Comments
 (0)