Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
fix: support large array for base64 encoding (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSnow authored May 3, 2024
1 parent 3afed4f commit bc6bb26
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["@storipress/karbon-playground"]
"ignore": ["@storipress/karbon-playground", "worker-playground"]
}
5 changes: 5 additions & 0 deletions .changeset/wet-geckos-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@storipress/karbon-utils': patch
---

fix: support large array for base64 encoding
1 change: 1 addition & 0 deletions packages/karbon-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@noble/ciphers": "^0.5.2",
"@storipress/jose-browser": "workspace:*",
"base64-js": "^1.5.1",
"entities": "^4.5.0"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion packages/karbon-utils/src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { fromByteArray } from 'base64-js'

export function base64ToUint8Array(s: string) {
return Uint8Array.from(atob(s), (c) => c.charCodeAt(0))
}

export function uint8ArrayToBase64(array: Uint8Array) {
return btoa(String.fromCharCode(...array))
// Need to use third-party implement or it will fail on large array
// ref: https://stackoverflow.com/a/57111228/3775132
return fromByteArray(array)
}

export function textToUint8Array(text: string) {
Expand Down
14 changes: 14 additions & 0 deletions packages/worker-playground/tests/encoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ describe('base64ToUint8Array & uint8ArrayToBase64', () => {
expect(uint8ArrayToBase64(base64ToUint8Array(base64))).toEqual(base64)
})
})

describe('uint8ArrayToBase64 & base64ToUint8Array', () => {
it.prop(
{
array: fc.uint8Array({
minLength: 157223,
}),
},
// This is a large test, make it faster
{ numRuns: 5 },
)('can convert base64 string from and to uint8array', ({ array }) => {
expect(base64ToUint8Array(uint8ArrayToBase64(array))).toEqual(array)
})
})
49 changes: 6 additions & 43 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ __metadata:
languageName: node
linkType: hard

"@cloudflare/kv-asset-handler@npm:0.3.2":
"@cloudflare/kv-asset-handler@npm:0.3.2, @cloudflare/kv-asset-handler@npm:^0.3.1":
version: 0.3.2
resolution: "@cloudflare/kv-asset-handler@npm:0.3.2"
dependencies:
Expand All @@ -858,15 +858,6 @@ __metadata:
languageName: node
linkType: hard

"@cloudflare/kv-asset-handler@npm:^0.3.1":
version: 0.3.1
resolution: "@cloudflare/kv-asset-handler@npm:0.3.1"
dependencies:
mime: "npm:^3.0.0"
checksum: 10/4238acf8ecaf61ab2350f686e2a0e4b011b14fe9a657960063c0064557a7c42f39f1c3db4ff01516697466f583b08ac34b16c319f6556385aac3108cc388a23d
languageName: node
linkType: hard

"@cloudflare/vitest-pool-workers@npm:0.2.6":
version: 0.2.6
resolution: "@cloudflare/vitest-pool-workers@npm:0.2.6"
Expand Down Expand Up @@ -3579,6 +3570,7 @@ __metadata:
"@moonrepo/cli": "npm:1.24.3"
"@noble/ciphers": "npm:^0.5.2"
"@storipress/jose-browser": "workspace:*"
base64-js: "npm:^1.5.1"
entities: "npm:^4.5.0"
fast-check: "npm:3.18.0"
happy-dom: "npm:14.7.1"
Expand Down Expand Up @@ -5859,7 +5851,7 @@ __metadata:
languageName: node
linkType: hard

"base64-js@npm:^1.3.1":
"base64-js@npm:^1.3.1, base64-js@npm:^1.5.1":
version: 1.5.1
resolution: "base64-js@npm:1.5.1"
checksum: 10/669632eb3745404c2f822a18fc3a0122d2f9a7a13f7fb8b5823ee19d1d2ff9ee5b52c53367176ea4ad093c332fd5ab4bd0ebae5a8e27917a4105a4cfc86b1005
Expand Down Expand Up @@ -7547,20 +7539,13 @@ __metadata:
languageName: node
linkType: hard

"devalue@npm:^4.3.0":
"devalue@npm:^4.3.0, devalue@npm:^4.3.2":
version: 4.3.3
resolution: "devalue@npm:4.3.3"
checksum: 10/b53ab9fe1b7b9e45c3eeebe8ff59ef294aaa8f8206c61fa49dedcccb65622882e68b69f1c1c8e7129ff5590a405cd6fb5dbe88afd6fb0f6861d5393cdce93c76
languageName: node
linkType: hard

"devalue@npm:^4.3.2":
version: 4.3.2
resolution: "devalue@npm:4.3.2"
checksum: 10/479cb13090adc3a75dd9277c6b5c81861bd9856e553254f5cc0dce3e8f62f397d2291563dacd36a1cb5723fdc6704d051a7e88c12fab71923f7653ca3ea5169c
languageName: node
linkType: hard

"didyoumean@npm:^1.2.2":
version: 1.2.2
resolution: "didyoumean@npm:1.2.2"
Expand Down Expand Up @@ -16405,20 +16390,13 @@ __metadata:
languageName: node
linkType: hard

"tinypool@npm:^0.8.2":
"tinypool@npm:^0.8.2, tinypool@npm:^0.8.3":
version: 0.8.4
resolution: "tinypool@npm:0.8.4"
checksum: 10/7365944c2532f240111443e7012be31a634faf1a02db08a91db3aa07361c26a374d0be00a0f2ea052c4bee39c107ba67f1f814c108d9d51dfc725c559c1a9c03
languageName: node
linkType: hard

"tinypool@npm:^0.8.3":
version: 0.8.3
resolution: "tinypool@npm:0.8.3"
checksum: 10/0372a8de2a0f792a36f23e199809a1b635ba1470df8be7a7183e382af55518a6f7c9d75e776952598955c1e8c54ea1157b0c680288edd0554aac4722f3900fa2
languageName: node
linkType: hard

"tinyspy@npm:^2.2.0":
version: 2.2.1
resolution: "tinyspy@npm:2.2.1"
Expand Down Expand Up @@ -18346,7 +18324,7 @@ __metadata:
languageName: node
linkType: hard

"ws@npm:^8.11.0":
"ws@npm:^8.11.0, ws@npm:^8.13.0":
version: 8.17.0
resolution: "ws@npm:8.17.0"
peerDependencies:
Expand All @@ -18361,21 +18339,6 @@ __metadata:
languageName: node
linkType: hard

"ws@npm:^8.13.0":
version: 8.13.0
resolution: "ws@npm:8.13.0"
peerDependencies:
bufferutil: ^4.0.1
utf-8-validate: ">=5.0.2"
peerDependenciesMeta:
bufferutil:
optional: true
utf-8-validate:
optional: true
checksum: 10/1769532b6fdab9ff659f0b17810e7501831d34ecca23fd179ee64091dd93a51f42c59f6c7bb4c7a384b6c229aca8076fb312aa35626257c18081511ef62a161d
languageName: node
linkType: hard

"xior@npm:0.3.13":
version: 0.3.13
resolution: "xior@npm:0.3.13"
Expand Down

0 comments on commit bc6bb26

Please sign in to comment.