-
Notifications
You must be signed in to change notification settings - Fork 6
Compress share links using Zstandard #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| "skipLibCheck": true, | ||
| "jsx": "preserve", | ||
| "module": "preserve", | ||
| "target": "esnext", | ||
| "lib": [ | ||
| "ESNext", | ||
| "DOM", | ||
| ], | ||
| }, | ||
| } |
This file contains hidden or 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,31 @@ | ||
| interface Uint8Array<TArrayBuffer extends ArrayBufferLike> { | ||
| /** | ||
| * Converts the `Uint8Array` to a base64-encoded string. | ||
| * @param options If provided, sets the alphabet and padding behavior used. | ||
| * @returns A base64-encoded string. | ||
| */ | ||
| toBase64( | ||
| options?: { | ||
| alphabet?: "base64" | "base64url" | undefined; | ||
| omitPadding?: boolean | undefined; | ||
| }, | ||
| ): string; | ||
| } | ||
|
|
||
| interface Uint8ArrayConstructor { | ||
| /** | ||
| * Creates a new `Uint8Array` from a base64-encoded string. | ||
| * @param string The base64-encoded string. | ||
| * @param options If provided, specifies the alphabet and handling of the last chunk. | ||
| * @returns A new `Uint8Array` instance. | ||
| * @throws {SyntaxError} If the input string contains characters outside the specified alphabet, or if the last | ||
| * chunk is inconsistent with the `lastChunkHandling` option. | ||
| */ | ||
| fromBase64( | ||
| string: string, | ||
| options?: { | ||
| alphabet?: "base64" | "base64url" | undefined; | ||
| lastChunkHandling?: "loose" | "strict" | "stop-before-partial" | undefined; | ||
| }, | ||
| ): Uint8Array<ArrayBuffer>; | ||
| } | ||
Binary file not shown.
This file contains hidden or 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,106 @@ | ||
| let modules = new Map<string, Promise<WebAssembly.Module>>(); | ||
|
|
||
| async function createWasmInstance(moduleURL: string) { | ||
| let instance: WebAssembly.Instance; | ||
| if (!modules.has(moduleURL)) { | ||
| let promiseWithResolvers = Promise.withResolvers<WebAssembly.Module>(); | ||
| modules.set(moduleURL, promiseWithResolvers.promise); | ||
| let result = await WebAssembly.instantiateStreaming(fetch(moduleURL)); | ||
| instance = result.instance; | ||
| promiseWithResolvers.resolve(result.module); | ||
| } else { | ||
| instance = await WebAssembly.instantiate(await modules.get(moduleURL)); | ||
| } | ||
| return { | ||
| instance, | ||
| exports: instance.exports as { | ||
| memory: WebAssembly.Memory; | ||
|
|
||
| allocate: (size: number) => number; | ||
|
|
||
| compress: ( | ||
| decompressed_data: number, decompressed_size: number, | ||
| dictionary_data: number, dictionary_size: number, | ||
| ) => number; | ||
|
|
||
| decompress: ( | ||
| compressed_data: number, compressed_size: number, | ||
| dictionary_data: number, dictionary_size: number, | ||
| ) => number; | ||
|
|
||
| get_result_error: (result: number) => number; | ||
| get_result_size: (result: number) => number; | ||
| get_result_data: (result: number) => number; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export async function compress(data: Uint8Array, dictionaryPromise: Promise<Uint8Array>) { | ||
| let [{ exports }, dictionary] = await Promise.all([ | ||
| createWasmInstance(new URL('zstd-wrapper.wasm', import.meta.url).toString()), | ||
| dictionaryPromise, | ||
| ]); | ||
|
|
||
| let { | ||
| memory, allocate, compress, | ||
| get_result_error, | ||
| get_result_size, | ||
| get_result_data, | ||
| } = exports; | ||
|
|
||
| let dictionaryPtr = allocate(dictionary.byteLength); | ||
| new Uint8Array(memory.buffer, dictionaryPtr, dictionary.byteLength).set(dictionary); | ||
|
|
||
| let dataPtr = allocate(data.byteLength); | ||
| new Uint8Array(memory.buffer, dataPtr, data.byteLength).set(data); | ||
|
|
||
| let resultPtr = compress( | ||
| dataPtr, data.byteLength, | ||
| dictionaryPtr, dictionary.byteLength, | ||
| ); | ||
|
|
||
| let error = get_result_error(resultPtr); | ||
| let size = get_result_size(resultPtr); | ||
| let compressed_data = get_result_data(resultPtr); | ||
|
|
||
| if (error !== 0) { | ||
| throw new Error(`Zstandard compression error ${error}`); | ||
| } | ||
|
|
||
| return new Uint8Array(memory.buffer, compressed_data, size); | ||
| } | ||
|
|
||
| export async function decompress(data: Uint8Array, dictionaryPromise: Promise<Uint8Array>) { | ||
| let [{ exports }, dictionary] = await Promise.all([ | ||
| createWasmInstance(new URL('zstd-wrapper.wasm', import.meta.url).toString()), | ||
| dictionaryPromise, | ||
| ]); | ||
|
|
||
| let { | ||
| memory, allocate, decompress, | ||
| get_result_error, | ||
| get_result_size, | ||
| get_result_data, | ||
| } = exports; | ||
|
|
||
| let dictionaryPtr = allocate(dictionary.byteLength); | ||
| new Uint8Array(memory.buffer, dictionaryPtr, dictionary.byteLength).set(dictionary); | ||
|
|
||
| let dataPtr = allocate(data.byteLength); | ||
| new Uint8Array(memory.buffer, dataPtr, data.byteLength).set(data); | ||
|
|
||
| let resultPtr = decompress( | ||
| dataPtr, data.byteLength, | ||
| dictionaryPtr, dictionary.byteLength, | ||
| ); | ||
|
|
||
| let error = get_result_error(resultPtr); | ||
| let size = get_result_size(resultPtr); | ||
| let compressed_data = get_result_data(resultPtr); | ||
|
|
||
| if (error !== 0) { | ||
| throw new Error(`Zstandard decompression error ${error}`); | ||
| } | ||
|
|
||
| return new Uint8Array(memory.buffer, compressed_data, size); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.