-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/config-api
- Loading branch information
Showing
11 changed files
with
218 additions
and
102 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
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
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
115 changes: 63 additions & 52 deletions
115
..._packages/catalyst_compression/catalyst_compression_web/assets/js/catalyst_compression.js
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,73 +1,84 @@ | ||
const brotli = await import("https://unpkg.com/[email protected]/index.web.js?module").then(m => m.default); | ||
const zstd = await import("https://unpkg.com/@oneidentity/[email protected]/wasm/index.js?module"); | ||
// Initialize a web worker for compression works. | ||
// This is a persistent worker, will last for life of the app. | ||
const compressionWorker = new Worker(new URL('./catalyst_compression_worker.js', import.meta.url)); | ||
|
||
// Initializes the zstd module, must be called before it can be used. | ||
await zstd.ZstdInit(); | ||
let idCounter = 0; | ||
|
||
/// Compresses hex bytes using brotli compression algorithm and returns compressed hex bytes. | ||
function _brotliCompress(bytesHex) { | ||
const bytes = _hexStringToUint8Array(bytesHex); | ||
const compressedBytes = brotli.compress(bytes); | ||
return _uint8ArrayToHexString(compressedBytes); | ||
} | ||
// A simple id generator function. | ||
function generateId() { | ||
const thisId = idCounter; | ||
const nextId = idCounter + 1; | ||
|
||
/// Decompresses hex bytes using brotli compression algorithm and returns decompressed hex bytes. | ||
function _brotliDecompress(bytesHex) { | ||
const bytes = _hexStringToUint8Array(bytesHex); | ||
const decompressedBytes = brotli.decompress(bytes); | ||
return _uint8ArrayToHexString(decompressedBytes); | ||
} | ||
idCounter = nextId >= Number.MAX_SAFE_INTEGER ? 0 : nextId; | ||
|
||
/// Compresses hex bytes using zstd compression algorithm and returns compressed hex bytes. | ||
function _zstdCompress(bytesHex) { | ||
const bytes = _hexStringToUint8Array(bytesHex); | ||
const compressedBytes = zstd.ZstdSimple.compress(bytes); | ||
return _uint8ArrayToHexString(compressedBytes); | ||
return thisId; | ||
} | ||
|
||
/// Decompresses hex bytes using zstd compression algorithm and returns decompressed hex bytes. | ||
function _zstdDecompress(bytesHex) { | ||
const bytes = _hexStringToUint8Array(bytesHex); | ||
const decompressedBytes = zstd.ZstdSimple.decompress(bytes); | ||
return _uint8ArrayToHexString(decompressedBytes); | ||
} | ||
function registerWorkerEventHandler(worker, handleMessage, handleError) { | ||
const wrappedHandleMessage = (event) => handleMessage(event, complete); | ||
const wrappedHandleError = (error) => handleError(error, complete); | ||
|
||
// Converts a hex string into a byte array. | ||
function _hexStringToUint8Array(hexString) { | ||
// Ensure the hex string length is even | ||
if (hexString.length % 2 !== 0) { | ||
throw new Error('Invalid hex string'); | ||
function complete() { | ||
worker.removeEventListener("message", wrappedHandleMessage); | ||
worker.removeEventListener("error", wrappedHandleError); | ||
} | ||
|
||
// Create a Uint8Array | ||
const byteArray = new Uint8Array(hexString.length / 2); | ||
worker.addEventListener("message", wrappedHandleMessage); | ||
worker.addEventListener("error", wrappedHandleError); | ||
} | ||
|
||
// Parse the hex string into byte values | ||
for (let i = 0; i < hexString.length; i += 2) { | ||
byteArray[i / 2] = parseInt(hexString.substr(i, 2), 16); | ||
} | ||
// A function to create a compression function according to its name. | ||
function runCompressionInWorker(fnName) { | ||
return (data) => { | ||
return new Promise((resolve, reject) => { | ||
const id = generateId(); | ||
|
||
return byteArray; | ||
} | ||
registerWorkerEventHandler( | ||
compressionWorker, | ||
(event, complete) => { | ||
const { | ||
id: responseId, | ||
result, | ||
error, | ||
initialized | ||
} = event.data; | ||
|
||
// skip the initializing completion event, | ||
// and the id that is not itself. | ||
if (initialized || responseId !== id) { | ||
return; | ||
} | ||
|
||
if (result) { | ||
resolve(result); | ||
} else { | ||
reject(error || 'Unexpected error'); | ||
} | ||
|
||
// Converts a byte array into a hex string. | ||
function _uint8ArrayToHexString(uint8Array) { | ||
return Array.from(uint8Array) | ||
.map(byte => byte.toString(16).padStart(2, '0')) | ||
.join(''); | ||
} | ||
complete(); | ||
}, | ||
(error, complete) => { | ||
reject(error); | ||
|
||
complete(); | ||
} | ||
); | ||
|
||
compressionWorker.postMessage({ id, action: fnName, bytesHex: data }); | ||
}); | ||
} | ||
} | ||
|
||
// A namespace containing the JS functions that | ||
// can be executed from dart side | ||
const catalyst_compression = { | ||
brotliCompress: _brotliCompress, | ||
brotliDecompress: _brotliDecompress, | ||
zstdCompress: _zstdCompress, | ||
zstdDecompress: _zstdDecompress, | ||
} | ||
brotliCompress: runCompressionInWorker("brotliCompress"), | ||
brotliDecompress: runCompressionInWorker("brotliDecompress"), | ||
zstdCompress: runCompressionInWorker("zstdCompress"), | ||
zstdDecompress: runCompressionInWorker("zstdDecompress"), | ||
}; | ||
|
||
// Expose catalyst compression as globally accessible | ||
// so that we can call it via catalyst_compression.function_name() from | ||
// other scripts or dart without needing to care about module imports | ||
window.catalyst_compression = catalyst_compression; | ||
window.catalyst_compression = catalyst_compression; |
Oops, something went wrong.