npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tybys/wasm-util

v0.9.0

Published

WASI polyfill for browser and some wasm util

Downloads

7,939,522

Readme

@tybys/wasm-util

WebAssembly related utils for browser environment

The output code is ES2019

Features

All example code below need to be bundled by ES module bundlers like webpack / rollup, or specify import map in browser native ES module runtime.

WASI polyfill for browser

The API is similar to the require('wasi').WASI in Node.js.

You can use memfs-browser to provide filesystem capability.

import { load, WASI } from '@tybys/wasm-util'
import { Volume, createFsFromVolume } from 'memfs-browser'

const fs = createFsFromVolume(Volume.fromJSON({
  '/home/wasi': null
}))

const wasi = new WASI({
  args: ['chrome', 'file.wasm'],
  env: {
    NODE_ENV: 'development',
    WASI_SDK_PATH: '/opt/wasi-sdk'
  },
  preopens: {
    '/': '/'
  },
  fs,

  // redirect stdout / stderr

  // print (text) { console.log(text) },
  // printErr (text) { console.error(text) }
})

const imports = {
  wasi_snapshot_preview1: wasi.wasiImport
}

const { module, instance } = await load('/path/to/file.wasm', imports)
wasi.start(instance)
// wasi.initialize(instance)

Implemented syscalls: wasi_snapshot_preview1

load / loadSync

loadSync has 4KB wasm size limit in browser.

// bundler
import { load, loadSync } from '@tybys/wasm-util'

const imports = { /* ... */ }

// using path
const { module, instance } = await load('/path/to/file.wasm', imports)
const { module, instance } = loadSync('/path/to/file.wasm', imports)

// using URL
const { module, instance } = await load(new URL('./file.wasm', import.meta.url), imports)
const { module, instance } = loadSync(new URL('./file.wasm', import.meta.url), imports)

// using Uint8Array
const buffer = new Uint8Array([
  0x00, 0x61, 0x73, 0x6d,
  0x01, 0x00, 0x00, 0x00
])
const { module, instance } = await load(buffer, imports)
const { module, instance } = loadSync(buffer, imports)

// auto asyncify
const {
  module,
  instance: asyncifiedInstance
} = await load(buffer, imports, { /* asyncify options */})
asyncifiedInstance.exports.fn() // => return Promise

Extend Memory instance

import { Memory, extendMemory } from '@tybys/wasm-util'

const memory = new WebAssembly.Memory({ initial: 256 })
// const memory = instance.exports.memory

extendMemory(memory)
console.log(memory instanceof Memory)
console.log(memory instanceof WebAssembly.Memory)
// expose memory view getters like Emscripten
const { HEAPU8, HEAPU32, view } = memory

Asyncify wrap

Build the C code using clang, wasm-ld and wasm-opt

void async_sleep(int ms);

int main() {
  async_sleep(200);
  return 0;
}
import { Asyncify } from '@tybys/wasm-util'

const asyncify = new Asyncify()

const imports = {
  env: {
    async_sleep: asyncify.wrapImportFunction(function (ms) {
      return new Promise((resolve) => {
        setTimeout(resolve, ms)
      })
    })
  }
}

// async_sleep(200)
const bytes = await (await fetch('/asyncfied_by_wasm-opt.wasm')).arrayBuffer()
const { instance } = await WebAssembly.instantiate(bytes, imports)
const asyncifiedInstance = asyncify.init(instance.exports.memory, instance, {
  wrapExports: ['_start']
})

const p = asyncifedInstance._start()
console.log(typeof p.then === 'function')
const now = Date.now()
await p
console.log(Date.now() - now >= 200)

wasi_snapshot_preview1

  • [x] args_get
  • [x] args_sizes_get
  • [x] environ_get
  • [x] environ_sizes_get
  • [x] clock_res_get
  • [x] clock_time_get
  • [ ] ~~fd_advise~~
  • [x] fd_allocate
  • [x] fd_close
  • [x] fd_datasync
  • [x] fd_fdstat_get
  • [ ] ~~fd_fdstat_set_flags~~
  • [x] fd_fdstat_set_rights
  • [x] fd_filestat_get
  • [x] fd_filestat_set_size
  • [x] fd_filestat_set_times
  • [x] fd_pread
  • [x] fd_prestat_get
  • [x] fd_prestat_dir_name
  • [x] fd_pwrite
  • [x] fd_read
  • [x] fd_readdir
  • [x] fd_renumber
  • [x] fd_seek
  • [x] fd_sync
  • [x] fd_tell
  • [x] fd_write
  • [x] path_create_directory
  • [x] path_filestat_get
  • [x] path_filestat_set_times
  • [x] path_link
  • [x] path_open
  • [x] path_readlink
  • [x] path_remove_directory
  • [x] path_rename
  • [x] path_symlink
  • [x] path_unlink_file
  • [x] poll_oneoff (timer only)
  • [x] proc_exit
  • [ ] ~~proc_raise~~
  • [x] sched_yield
  • [x] random_get
  • [ ] ~~sock_recv~~
  • [ ] ~~sock_send~~
  • [ ] ~~sock_shutdown~~