more serialization

This commit is contained in:
2023-07-08 16:05:49 +02:00
parent 1d7c573985
commit c771706353
5 changed files with 67 additions and 109 deletions

View File

@@ -0,0 +1,33 @@
/**
* Compress JSON.stringify with gzip
*/
export async function stringifyCompressed(chords: any): Promise<Blob> {
const stream = new Blob([JSON.stringify(chords)]).stream().pipeThrough(new CompressionStream("gzip"))
return await new Response(stream).blob()
}
/**
* Decompress JSON.parse with gzip
*/
export async function parseCompressed<T>(blob: Blob): Promise<T> {
const stream = blob.stream().pipeThrough(new DecompressionStream("gzip"))
return await new Response(stream).json()
}
/**
* Share JS object as url query param
*/
export async function getSharableUrl(name: string, data: any, baseHref = window.location.href): Promise<URL> {
return new Promise(async resolve => {
const reader = new FileReader()
reader.onloadend = function () {
const base64String = (reader.result as string)
.replace(/^data:application\/octet-stream;base64,/, "")
.replace(/==$/, "")
const url = new URL(baseHref)
url.searchParams.set(name, base64String)
resolve(url)
}
reader.readAsDataURL(await stringifyCompressed(data))
})
}