Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hysnappy

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hysnappy - npm Package Compare versions

Comparing version 0.1.2 to 0.2.0

5

hysnappy.d.ts

@@ -8,1 +8,6 @@ /**

export async function snappyUncompress(input: Uint8Array, output: Uint8Array): Promise<void>
/**
* Load wasm and return uncompressor function.
*/
export function snappyUncompressor(): (input: Uint8Array, output: Uint8Array) => void

95

hysnappy.js

@@ -7,51 +7,56 @@ /**

*/
export async function snappyUncompress(input, output) {
// Load the WASM module
const snappyModule = await instantiateWasm()
// const { memory, uncompress } = snappyModule.instance.exports
/** @type {WebAssembly.Memory} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const memory = snappyModule.instance.exports.memory
/** @type {Function} */
// @ts-ignore
// eslint-disable-next-line prefer-destructuring
const uncompress = snappyModule.instance.exports.uncompress
export function snappyUncompress(input, output) {
snappyUncompressor()(input, output)
}
// Input data is passed into wasm memory at inputStart
// Output data is expected to be written to wasm memory at outputStart
// clang uses some wasm memory, so we need to skip past that
const inputStart = 68000 // 68 kb
const outputStart = inputStart + input.byteLength
/**
* Load wasm and return uncompressor function.
*
* @returns {(input: Uint8Array, output: Uint8Array) => void}
*/
export function snappyUncompressor() {
// Instantiate wasm module
const wasm = instantiateWasm()
// WebAssembly memory
const totalSize = inputStart + input.byteLength + output.byteLength
if (memory.buffer.byteLength < totalSize) {
// Calculate the number of pages needed, rounding up
const pageSize = 64 * 1024 // 64KiB per page
const currentPages = memory.buffer.byteLength / pageSize
const requiredPages = Math.ceil(totalSize / pageSize)
const pagesToGrow = requiredPages - currentPages
memory.grow(pagesToGrow)
}
return (input, output) => {
/** @type {any} */
const { memory, uncompress } = wasm.exports
// Copy the compressed data to WASM memory
const byteArray = new Uint8Array(memory.buffer)
byteArray.set(input, inputStart)
// Input data is passed into wasm memory at inputStart
// Output data is expected to be written to wasm memory at outputStart
// clang uses some wasm memory, so we need to skip past that
const inputStart = 68000 // 68 kb
const outputStart = inputStart + input.byteLength
// Call wasm uncompress function
const result = uncompress(inputStart, input.byteLength, outputStart)
// WebAssembly memory
const totalSize = inputStart + input.byteLength + output.byteLength
if (memory.buffer.byteLength < totalSize) {
// Calculate the number of pages needed, rounding up
const pageSize = 64 * 1024 // 64KiB per page
const currentPages = memory.buffer.byteLength / pageSize
const requiredPages = Math.ceil(totalSize / pageSize)
const pagesToGrow = requiredPages - currentPages
memory.grow(pagesToGrow)
}
// Check for errors
if (result === -1) throw new Error('invalid snappy length header')
if (result === -2) throw new Error('missing eof marker')
if (result === -3) throw new Error('premature end of input')
if (result) throw new Error(`failed to uncompress data ${result}`)
// Copy the compressed data to WASM memory
const byteArray = new Uint8Array(memory.buffer)
byteArray.set(input, inputStart)
// Get uncompressed data from WASM memory
const uncompressed = byteArray.slice(outputStart, outputStart + output.byteLength)
// Call wasm uncompress function
const result = uncompress(inputStart, input.byteLength, outputStart)
// Copy the uncompressed data to the output buffer
// TODO: Return WASM memory buffer instead of copying?
output.set(uncompressed)
// Check for errors
if (result === -1) throw new Error('invalid snappy length header')
if (result === -2) throw new Error('missing eof marker')
if (result === -3) throw new Error('premature end of input')
if (result) throw new Error(`failed to uncompress data ${result}`)
// Get uncompressed data from WASM memory
const uncompressed = byteArray.slice(outputStart, outputStart + output.byteLength)
// Copy the uncompressed data to the output buffer
// TODO: Return WASM memory buffer instead of copying?
output.set(uncompressed)
}
}

@@ -62,3 +67,3 @@

*
* @returns {Promise<WebAssembly.WebAssemblyInstantiatedSource>}
* @returns {WebAssembly.Instance}
*/

@@ -71,3 +76,5 @@ function instantiateWasm() {

}
return WebAssembly.instantiate(byteArray)
// only works for payload less than 4kb:
const mod = new WebAssembly.Module(byteArray)
return new WebAssembly.Instance(mod)
}

@@ -74,0 +81,0 @@

{
"name": "hysnappy",
"version": "0.1.2",
"version": "0.2.0",
"description": "snappy decompressor for wasm",

@@ -33,7 +33,7 @@ "keywords": [

"@babel/eslint-parser": "7.23.10",
"@types/node": "20.11.19",
"@types/node": "20.11.20",
"@vitest/coverage-v8": "1.3.1",
"eslint": "8.56.0",
"eslint": "8.57.0",
"eslint-plugin-import": "2.29.1",
"eslint-plugin-jsdoc": "48.1.0",
"eslint-plugin-jsdoc": "48.2.0",
"snappyjs": "0.7.0",

@@ -40,0 +40,0 @@ "vitest": "1.3.1"

@@ -23,3 +23,3 @@ # HySnappy

const output = new Uint8Array(10)
await snappyUncompress(compressed, output)
snappyUncompress(compressed, output)
```

@@ -26,0 +26,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc