Skip to content

Commit 0c5eb47

Browse files
committed
Add zlib compression with fflate
1 parent 973d657 commit 0c5eb47

4 files changed

Lines changed: 29 additions & 10 deletions

File tree

packages/client/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"scripts": {
2626
"test": "jest",
2727
"test:coverage": "yarn test:unit && open coverage/lcov-report/index.html",
28-
"prebuild": "rm -rf ./build && node -p \"'export const LIB_VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts",
28+
"prebuild": "rm -rf ./dist && node -p \"'export const LIB_VERSION = \\'' + require('./package.json').version + '\\';'\" > src/version.ts",
2929
"build": "tsc -p ./tsconfig.build.json && tsc-alias",
3030
"dev": "tsc --watch & nodemon --watch dist --delay 1 --exec tsc-alias",
3131
"typecheck": "tsc --noEmit",
@@ -35,7 +35,8 @@
3535
"@noble/ciphers": "^0.5.3",
3636
"@noble/curves": "^1.4.2",
3737
"@noble/hashes": "^1.4.0",
38-
"eventemitter3": "^5.0.1"
38+
"eventemitter3": "^5.0.1",
39+
"fflate": "^0.8.2"
3940
},
4041
"peerDependencies": {
4142
"@react-native-async-storage/async-storage": "*",
@@ -81,4 +82,4 @@
8182
"tslib": "^2.6.0",
8283
"typescript": "^5.1.6"
8384
}
84-
}
85+
}

packages/client/src/core/cipher/cipher.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { unzlibSync, zlibSync } from 'fflate';
2+
13
import {
24
decrypt,
35
decryptContent,
@@ -13,10 +15,12 @@ import { hexStringToUint8Array, uint8ArrayToHex } from ':core/type/util';
1315

1416
async function webEncrypt(sharedSecret: CryptoKey, plainText: string): Promise<EncryptedData> {
1517
const iv = crypto.getRandomValues(new Uint8Array(12));
18+
const plainTextBytes = new TextEncoder().encode(plainText);
19+
const compressedBytes = zlibSync(plainTextBytes);
1620
const cipherText = await crypto.subtle.encrypt(
1721
{ name: 'AES-GCM', iv },
1822
sharedSecret,
19-
new TextEncoder().encode(plainText)
23+
compressedBytes
2024
);
2125

2226
return { iv: new Uint8Array(iv), cipherText: new Uint8Array(cipherText) };
@@ -26,9 +30,13 @@ async function webDecrypt(
2630
sharedSecret: CryptoKey,
2731
{ iv, cipherText }: EncryptedData
2832
): Promise<string> {
29-
const plainText = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, sharedSecret, cipherText);
30-
31-
return new TextDecoder().decode(plainText);
33+
const compressedBytes = await crypto.subtle.decrypt(
34+
{ name: 'AES-GCM', iv },
35+
sharedSecret,
36+
cipherText
37+
);
38+
const decompressedBytes = unzlibSync(Buffer.from(compressedBytes));
39+
return new TextDecoder().decode(decompressedBytes);
3240
}
3341

3442
function getFormat(keyType: 'public' | 'private') {

packages/client/src/core/cipher/cipher.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { gcm } from '@noble/ciphers/aes';
22
import { randomBytes } from '@noble/ciphers/webcrypto';
33
import { secp256r1 } from '@noble/curves/p256';
44
import { Buffer } from 'buffer';
5+
import { unzlibSync, zlibSync } from 'fflate';
56

67
import { CryptoKey, CryptoKeyPair } from './types';
78
import { EncryptedData, RPCRequest, RPCResponse } from ':core/message';
@@ -61,7 +62,8 @@ export async function encrypt(sharedSecret: CryptoKey, plainText: string): Promi
6162
const iv = randomBytes(12);
6263
const stream = gcm(sharedSecret._key, iv);
6364
const plainTextBytes = new Uint8Array(Buffer.from(plainText, 'utf8'));
64-
const cipherText = stream.encrypt(plainTextBytes);
65+
const compressedBytes = zlibSync(plainTextBytes);
66+
const cipherText = stream.encrypt(compressedBytes);
6567

6668
return {
6769
iv,
@@ -74,8 +76,8 @@ export async function decrypt(
7476
{ iv, cipherText }: EncryptedData
7577
): Promise<string> {
7678
const stream = gcm(new Uint8Array(sharedSecret._key), new Uint8Array(iv));
77-
const plainTextBytes = stream.decrypt(new Uint8Array(cipherText));
78-
79+
const compressedBytes = stream.decrypt(new Uint8Array(cipherText));
80+
const plainTextBytes = unzlibSync(compressedBytes);
7981
return Buffer.from(plainTextBytes).toString('utf8');
8082
}
8183

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,7 @@ __metadata:
20692069
eslint-plugin-unused-imports: "npm:^3.0.0"
20702070
eventemitter3: "npm:^5.0.1"
20712071
expo-web-browser: "npm:^13.0.3"
2072+
fflate: "npm:^0.8.2"
20722073
jest: "npm:^27.5.1"
20732074
jest-chrome: "npm:^0.7.2"
20742075
jest-websocket-mock: "npm:^2.4.0"
@@ -4623,6 +4624,13 @@ __metadata:
46234624
languageName: node
46244625
linkType: hard
46254626

4627+
"fflate@npm:^0.8.2":
4628+
version: 0.8.2
4629+
resolution: "fflate@npm:0.8.2"
4630+
checksum: 10c0/03448d630c0a583abea594835a9fdb2aaf7d67787055a761515bf4ed862913cfd693b4c4ffd5c3f3b355a70cf1e19033e9ae5aedcca103188aaff91b8bd6e293
4631+
languageName: node
4632+
linkType: hard
4633+
46264634
"file-entry-cache@npm:^6.0.1":
46274635
version: 6.0.1
46284636
resolution: "file-entry-cache@npm:6.0.1"

0 commit comments

Comments
 (0)