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

workers-zig

v0.2.0

Published

Write Cloudflare Workers in Zig via WebAssembly

Downloads

19

Readme

Workers-Zig

workers-zig

Workers Zig is a light weight Zig bindings for the Cloudflare Workers environment via WebAssembly.

Why Zig?

  • Zig is a language that is designed to be a small, fast, and portable.
  • The language already supports WASM and WASI.
  • Small builds are easy to achieve. To expound on this, the basic example provided is 5.8Kb of WASM code and 6.8Kb javascript code.
  • I wanted a tool that made it easy for both WASM and JS code to work in tandem.
  • I didn't like that rust wasm bindings would grow the more code you wrote. I came up with a strategy that covers 90% use cases, JS glue doesn't grow, and you can add the 10% as needed.
  • I prefer Zig's memory model over Rust.

Be sure to read the Documentation for guidance on usage.

Features

  • 🔗 Zero dependencies
  • 🤝 Use in tandem with Javascript or 100% Zig WebAssembly
  • 🎮 JS bindings with support to write your own - List of supported bindings here
  • 📨 Fetch bindings
  • ⏰ Scheduled bindings
  • 🔑 Supports Variables and Secrets from env
  • ✨ Cache bindings
  • 📦 KV bindings
  • 🪣 R2 bindings
  • 💾 D1 bindings
  • 🔐 Web-Crypto bindings [partially complete]
  • 💪 Uses TypeScript

Features coming soon

  • 🗿 WASI support
  • 📌 Durable Objects bindings
  • ✉️ WebSockets bindings
  • once CF lands dynamic imports: Only load wasm when needed.

Install

Step 1: Install Zig

Follow the instructions to install Zig

Release used: 0.10.0-dev.3838+77f31ebbb

Step 2a: Use the skeleton project provided

Follow the steps provided by the skeleton project

# in one go
git clone --recurse-submodules -j8 [email protected]:CraigglesO/worker-zig-template.git

# OR

# clone
git clone [email protected]:CraigglesO/worker-zig-template.git
# enter
cd worker-zig-template
# Pull in the submodule
git submodule update --init --recursive

Step 2b: Install the workers-zig package

# NPM
npm install --save workers-zig
# Yarn
yarn add workers-zig
# PNPM
pnpm add workers-zig
# BUN
bun add workers-zig

Step 3: Add workers-zig as a submodule to your project

git submodule add https://github.com/CraigglesO/workers-zig

Step 4: Setup a build.zig script

const std = @import("std");
const Builder = std.build.Builder;

pub fn build(b: *Builder) void {
    b.is_release = true;
    b.cache_root = "cache";
    b.global_cache_root = "cache";
    b.use_stage1 = true;

    const wasm_build = b.addSharedLibrary("zig", "lib/main.zig", .unversioned);
    wasm_build.setOutputDir("dist");
    wasm_build.setTarget(std.zig.CrossTarget {
        .cpu_arch = .wasm32,
        .os_tag = .freestanding,
    });
    wasm_build.build_mode = std.builtin.Mode.ReleaseSmall;
    wasm_build.strip = true;
    wasm_build.linkage = std.build.LibExeObjStep.Linkage.dynamic;
    wasm_build.addPackagePath("workers-zig", "workers-zig/lib/main.zig");
    wasm_build.install();
}

Step 5: Recommended wrangler configuration

name = "zig-worker-template"
main = "dist/worker.mjs"
compatibility_date = "2022-07-29"
usage_model = "bundled" # or unbound
account_id = ""

[build]
command = "zig build && npm run esbuild"
watch_dir = [
  "src",
  "lib"
]

[[build.upload.rules]]
type = "CompiledWasm"
globs = ["**/*.wasm"]
[[build.upload.rules]]
type = "ESModule"
globs = ["**/*.mjs"]