Skip to content

Commit 69d7351

Browse files
committed
Upgrade to v2
1 parent 3329f33 commit 69d7351

1 file changed

Lines changed: 74 additions & 4 deletions

File tree

index.js

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,75 @@
1-
import { gzip, ungzip } from 'pako'
2-
import { fromUint8Array, toUint8Array } from 'js-base64'
1+
let _pako, _jsb //caches
2+
const cdn = (/** @type {string} */ path) => `https://cdn.jsdelivr.net/npm/${path}`
3+
const pako = async () => (_pako ??= await import(cdn('pako@2/dist/pako.esm.mjs')))
4+
const jsb = async () => (_jsb ??= await import(cdn('js-base64@3/base64.mjs')))
5+
const toStream = (/** @type {Uint8Array} */ bytes) =>
6+
new ReadableStream({
7+
start(controller) {
8+
controller.enqueue(bytes)
9+
controller.close()
10+
}
11+
})
312

4-
export const zipurl = (data) => fromUint8Array(gzip(data), true)
5-
export const unzipurl = (data) => ungzip(toUint8Array(data), { to: 'string' })
13+
/**
14+
* @param {Uint8Array} bytes
15+
* @returns {Promise<string>}
16+
*/
17+
async function toBase(bytes) {
18+
if (typeof bytes.toBase64 === 'function')
19+
return bytes.toBase64({ alphabet: 'base64url', omitPadding: true })
20+
if (typeof Buffer !== 'undefined') return Buffer.from(bytes).toString('base64url')
21+
return jsb().then((m) => m.fromUint8Array(bytes, true))
22+
}
23+
24+
/**
25+
* @param {string} str
26+
* @returns {Promise<Uint8Array>}
27+
*/
28+
async function fromBase(str) {
29+
if (typeof Uint8Array.fromBase64 === 'function')
30+
return Uint8Array.fromBase64(str, { alphabet: 'base64url', lastChunkHandling: 'loose' })
31+
if (typeof Buffer !== 'undefined') return new Uint8Array(Buffer.from(str, 'base64url'))
32+
return jsb().then((m) => m.toUint8Array(str))
33+
}
34+
35+
/**
36+
* @param {Uint8Array} bytes
37+
* @returns {Promise<Uint8Array>}
38+
*/
39+
async function gzip(bytes) {
40+
if ('CompressionStream' in globalThis) {
41+
const stream = toStream(bytes).pipeThrough(new CompressionStream('gzip'))
42+
return new Uint8Array(await new Response(stream).arrayBuffer())
43+
}
44+
return pako().then((m) => m.gzip(bytes))
45+
}
46+
47+
/**
48+
* @param {Uint8Array} bytes
49+
* @returns {Promise<Uint8Array>}
50+
*/
51+
async function gunzip(bytes) {
52+
if ('DecompressionStream' in globalThis) {
53+
const stream = toStream(bytes).pipeThrough(new DecompressionStream('gzip'))
54+
return new Uint8Array(await new Response(stream).arrayBuffer())
55+
}
56+
return pako().then((m) => m.ungzip(bytes))
57+
}
58+
59+
/**
60+
* Encode string -> gzip -> base64url
61+
* @param {string} input
62+
* @returns {Promise<string>}
63+
*/
64+
export async function zipurl(input) {
65+
return toBase(await gzip(new TextEncoder().encode(input)))
66+
}
67+
68+
/**
69+
* Decode base64url -> gunzip -> string
70+
* @param {string} input
71+
* @returns {Promise<string>}
72+
*/
73+
export async function unzipurl(input) {
74+
return new TextDecoder().decode(await gunzip(await fromBase(input)))
75+
}

0 commit comments

Comments
 (0)