-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
and persist config in url
- Loading branch information
Showing
4 changed files
with
116 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Compress a string with browser native APIs into a binary string representation | ||
* | ||
* @param data - Input string that should be compressed | ||
* @param encoding - Compression algorithm to use | ||
* @returns The compressed binary string | ||
*/ | ||
export async function compressRaw(data: string, encoding: CompressionFormat): Promise<string> { | ||
// stream the string through the compressor | ||
const stream = new Blob([new TextEncoder().encode(data)]) | ||
.stream() | ||
.pipeThrough(new CompressionStream(encoding)); | ||
// convert the stream to an array buffer | ||
const buffer = await new Response(stream).arrayBuffer(); | ||
// convert the array buffer to a binary string | ||
return Array.from(new Uint8Array(buffer), x => String.fromCodePoint(x)).join(''); | ||
} | ||
|
||
/** | ||
* Compress a string with browser native APIs into a string representation | ||
* | ||
* @param data - Input string that should be compressed | ||
* @param encoding - Compression algorithm to use | ||
* @returns The compressed string | ||
*/ | ||
export async function compress(data: string, encoding: CompressionFormat): Promise<string> { | ||
return btoa(await compressRaw(data, encoding)); | ||
} |
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,26 @@ | ||
/** | ||
* Decompress a binary string representation with browser native APIs in to a normal js string | ||
* | ||
* @param binary - Binary string that should be decompressed, e.g. the output from `atob` | ||
* @param encoding - Decompression algorithm to use | ||
* @returns The decompressed string | ||
*/ | ||
export async function decompressRaw(binary: string, encoding: CompressionFormat): Promise<string> { | ||
// stream the string through the decompressor | ||
const stream = new Blob([Uint8Array.from(binary, m => m.codePointAt(0)!)]) | ||
.stream() | ||
.pipeThrough(new DecompressionStream(encoding)); | ||
// convert the stream to a string | ||
return new Response(stream).text(); | ||
} | ||
|
||
/** | ||
* Decompress a string representation with browser native APIs in to a normal js string | ||
* | ||
* @param data - String that should be decompressed | ||
* @param encoding - Decompression algorithm to use | ||
* @returns The decompressed string | ||
*/ | ||
export async function decompress(data: string, encoding: CompressionFormat): Promise<string> { | ||
return decompressRaw(atob(data), encoding); | ||
} |