Giter Club home page Giter Club logo

workers-zig's Introduction

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
  • ๐Ÿ—ฟ WASI support
  • ๐Ÿค 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

  • ๐Ÿ“Œ 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"]

workers-zig's People

Contributors

craiggleso avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.