Skip to content

BioforestChain/zstd-wasm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zstd-wasm

build with zstd-rs

how to use within vite

  1. fetch wasm file

    Vite will automatically pack the wasm file into the dist/assets folder. On the website, it will download this wasm file using network.

    import init, { compress, decompress } from "@dweb-browser/zstd-wasm";
    import zstd_wasm_url from "@dweb-browser/zstd-wasm/zstd_wasm_bg.wasm?url";
    // Modern browsers can directly use `await init(zstd_wasm_url);`
    init(zstd_wasm_url).then(() => {
      /// compress or decompress
      const output = compress(new Uint8Array(100), 10);
      const input = decompress(output);
      console.log(input, output);
    });
  2. bundle wasm into js with base64 encoding

    import { compress, decompress, initSync } from "@dweb-browser/zstd-wasm";
    import get_zstd_wasm_binary from "@dweb-browser/zstd-wasm/zstd_wasm_bg_wasm";
    initSync(get_zstd_wasm_binary());
    
    /// compress or decompress

how to use in nodejs

  1. commonjs

    const { compress, decompress } = require("@dweb-browser/zstd-wasm");
    
    /// compress or decompress
  2. esmodule

    import fs from "node:fs";
    import url from "node:url";
    import { compress, decompress, initSync } from "@dweb-browser/zstd-wasm";
    const zstd_wasm_binary = fs.readFileSync(
      url.fileURLToPath(
        import.meta.resolve("@dweb-browser/zstd-wasm/zstd_wasm_bg.wasm")
      )
    );
    
    initSync({ module: zstd_wasm_binary });
    
    /// compress or decompress

how to use in deno

import { compress, decompress, initSync } from "@dweb-browser/zstd-wasm";
import zstd_wasm_binary from "@dweb-browser/zstd-wasm/zstd_wasm_bg_wasm";
initSync(get_zstd_wasm_binary());

/// compress or decompress
const output = compress(new Uint8Array(100), 10);
const input = decompress(output);
console.log(input, output);

how to build

  1. read https://github.com/gyscos/zstd-rs/wiki/Compile-for-WASM
  2. install wasm-bindgen cargo install wasm-bindgen-cli

    checkout wasm-bindgen -V shoule equals the value in cargo.toml

  3. install wasm-pack
  4. install esbuild npm install -g esbuild
  5. install deno
    curl -fsSL https://deno.land/install.sh | sh # macos or linux
    irm https://deno.land/install.ps1 | iex # windows
    
  6. run script: deno task build

    output to pkg folder

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published