Skip to content

Instantly share code, notes, and snippets.

@emlyn
Created January 22, 2024 11:29
Show Gist options
  • Save emlyn/e30ff8fa5b9f785db91a069fca3bf728 to your computer and use it in GitHub Desktop.
Save emlyn/e30ff8fa5b9f785db91a069fca3bf728 to your computer and use it in GitHub Desktop.
Decode base64 encoded gzipped string in Javascript
const decodePart = async function(data) {
const ds = new DecompressionStream("gzip");
const resp = await fetch(`data:application/octet-stream;base64,${data}`);
const blob = await resp.blob();
return new Response(blob.stream().pipeThrough(ds)).text();
}
const decodeLine = async function(line) {
const GzipCompressionMagicHeader = 'H4sI';
for (const part of line.split(',')) {
if (part.startsWith(GzipCompressionMagicHeader)) {
console.log(await decodePart(split));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment