-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ff253a
commit bc6402e
Showing
4 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
export function b64EncodeUnicode(str: string) { | ||
// first we use encodeURIComponent to get percent-encoded UTF-8, | ||
// then we convert the percent encodings into raw bytes which | ||
// can be fed into btoa. | ||
return btoa( | ||
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) { | ||
return String.fromCharCode(parseInt(p1, 16)); | ||
}) | ||
); | ||
// https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem | ||
|
||
export function b64EncodeUnicode(str: string): string { | ||
return bytesToBase64(new TextEncoder().encode(str)); | ||
} | ||
|
||
export function b64DecodeUnicode(str: string): string { | ||
return new TextDecoder().decode(base64ToBytes(str)); | ||
} | ||
|
||
export function base64ToBytes(base64: string): Uint8Array { | ||
const binString = atob(base64); | ||
return Uint8Array.from(binString, (m) => m.codePointAt(0)); | ||
} | ||
|
||
export function b64DecodeUnicode(str: string) { | ||
return decodeURIComponent( | ||
[...atob(str)] | ||
.map(function (c) { | ||
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); | ||
}) | ||
.join("") | ||
); | ||
export function bytesToBase64(bytes: Uint8Array): string { | ||
const binString = Array.from(bytes, (x) => String.fromCodePoint(x)).join(""); | ||
return btoa(binString); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { base64ToBytes, bytesToBase64 } from "./b64Unicode"; | ||
|
||
export const GZIP_HASH_PREFIX = "#gzip,"; | ||
|
||
export async function compress(str: string) { | ||
const blob = new Blob([str], { type: "text/plain" }); | ||
const stream = blob.stream().pipeThrough(new CompressionStream("gzip")); | ||
const compressedBlob = await new Response(stream).blob(); | ||
return bytesToBase64(new Uint8Array(await compressedBlob.arrayBuffer())); | ||
} | ||
|
||
export async function decompress(str: string) { | ||
const bytes = base64ToBytes(str); | ||
const blob = new Blob([bytes], { type: "text/plain" }); | ||
const stream = blob.stream().pipeThrough(new DecompressionStream("gzip")); | ||
const decompressedBlob = await new Response(stream).blob(); | ||
return decompressedBlob.text(); | ||
} |