Skip to content

Commit

Permalink
Sync wasm loading
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Feb 27, 2024
1 parent dbfb45e commit 4ef1e6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
14 changes: 8 additions & 6 deletions hysnappy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* @param {Uint8Array} input
* @param {Uint8Array} output
*/
export async function snappyUncompress(input, output) {
export function snappyUncompress(input, output) {
// Load the WASM module
const snappyModule = await instantiateWasm()
const wasm = instantiateWasm()
// const { memory, uncompress } = snappyModule.instance.exports
/** @type {WebAssembly.Memory} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const memory = snappyModule.instance.exports.memory
const memory = wasm.exports.memory
/** @type {Function} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const uncompress = snappyModule.instance.exports.uncompress
const uncompress = wasm.exports.uncompress

// Input data is passed into wasm memory at inputStart
// Output data is expected to be written to wasm memory at outputStart
Expand Down Expand Up @@ -58,15 +58,17 @@ export async function snappyUncompress(input, output) {
/**
* Instantiate WASM module from a base64 string.
*
* @returns {Promise<WebAssembly.WebAssemblyInstantiatedSource>}
* @returns {WebAssembly.Instance}
*/
function instantiateWasm() {
const binaryString = atob(wasm64)
const byteArray = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i += 1) {
byteArray[i] = binaryString.charCodeAt(i)
}
return WebAssembly.instantiate(byteArray)
// only works for payload less than 4kb:
const mod = new WebAssembly.Module(byteArray)
return new WebAssembly.Instance(mod)
}

// Base64 encoded hysnappy.wasm
Expand Down
22 changes: 11 additions & 11 deletions test/decompress.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ describe('snappy uncompress', () => {
})

it('throws for invalid input', () => {
const outputArray = new Uint8Array(10)
expect(snappyUncompress(new Uint8Array([]), outputArray))
.rejects.toThrow('invalid snappy length header')
expect(snappyUncompress(new Uint8Array([0xff]), outputArray))
.rejects.toThrow('invalid snappy length header')
expect(snappyUncompress(new Uint8Array([0x03, 0x61]), outputArray))
.rejects.toThrow('missing eof marker')
expect(snappyUncompress(new Uint8Array([0x03, 0xf1]), outputArray))
.rejects.toThrow('missing eof marker')
expect(snappyUncompress(new Uint8Array([0x02, 0x00, 0x68]), outputArray))
.rejects.toThrow('premature end of input')
const output = new Uint8Array(10)
expect(() => snappyUncompress(new Uint8Array([]), output))
.toThrow('invalid snappy length header')
expect(() => snappyUncompress(new Uint8Array([0xff]), output))
.toThrow('invalid snappy length header')
expect(() => snappyUncompress(new Uint8Array([0x03, 0x61]), output))
.toThrow('missing eof marker')
expect(() => snappyUncompress(new Uint8Array([0x03, 0xf1]), output))
.toThrow('missing eof marker')
expect(() => snappyUncompress(new Uint8Array([0x02, 0x00, 0x68]), output))
.toThrow('premature end of input')
})
})

0 comments on commit 4ef1e6c

Please sign in to comment.