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

@wasmcloud/wasmcloud-js

v1.0.6

Published

wasmcloud host in JavaScript/Browser

Downloads

4

Readme

wasmCloud Host in JavaScript/Browser

This is the JavaScript implementation of the wasmCloud host for the browser (NodeJS support in progress). The library runs a host inside a web browser/application that connects to a remote lattice via NATS and can run wasmCloud actors in the browser. The host will automatically listen for actor start/stop from NATS and will initialize the actor using wapcJS. Any invocations will be handled by the browser actor and returned to the requesting host via NATS. Users can pass callbacks to handle invocation and event data.

Demonstration Video

In this demonstration video we will demonstration the following:

  • Load an HTTP Server capability into a wasmCloud Host running on a machine
  • Load an the wasmcloud-js host into a web browser
  • Load an 'echo' actor into the web browser
  • seamlessly bind the actor to the capability provider through Lattice
  • Access the webserver, which in turn delivers the request to the actor, processes it, and returns it to the requestion client via the capability
  • Unload the actor

https://user-images.githubusercontent.com/1530656/130013412-b9a9daa6-fc71-424b-814c-2ca400926794.mp4

Prerequisities

  • NATS with WebSockets enabled

  • wasmCloud lattice (OTP Version)

  • (OPTIONAL) Docker Registry with CORS configured

    • If launching actors from remote registries in the browser host, CORS must be configured on the registry server

Development Prerequisities

  • NodeJS, npm

  • rust, cargo, wasm-pack

    • Used to port the rust versions of wascap, nkeys to JS

Installation

$ npm install @wasmcloud/wasmcloud-js

Usage

More examples can be found in the examples directory, including sample webpack and esbuild configurations

Browser

<script src="https://unpkg.com/@wasmcloud/wasmcloud-js@<VERSION>/dist/wasmcloud.js"></script>
<script>
  (async () => {
    // Start the host passing the name, registry tls enabled, a list of nats ws/wss hosts or the natsConnection object, and an optional host heartbeat interval (default is 30 seconds)
    const host = await wasmcloudjs.startHost("default", false, ["ws://localhost:4222"], 30000);
    // The host will automatically listen for actors start & stop messages, to manually listen for these messages the following methods are exposed
    // only call these methods if your host is not listening for actor start/stop
    // actor invocations are automatically returned to the host. if a user wants to handle the data, they can pass a map of callbacks using the actor ref/wasm file name as the key with a callback(data, result) function. The data contains the invocation data and the result contains the invocation result
    // (async() => {
    //     await host.listenLaunchActor(
    //         {
    //             "localhost:5000/echo:0.2.2": (data, result) => console.log(data.operation, result);
    //         }
    //     );
    //     await host.listenStopActor();
    // })();
    // To launch an actor manually from the library from a registry, optionally a callback can be passed to handle the invocation results. In addition, a hostCall callback and writer can be passed.
    // The hostCallback format is as follows:
    // ```
    // (binding, namespace, operation, payload) => {
    //    return Uint8Array(payload);
    // })
    // ```
    await host.launchActor("registry.com/actor:0.1.1", (data) => { /* handle data */})
    // Launch an actor with the hostCallback
    await host.launchActor("registry.com/actor:0.1.1", (data) => { /* handle data */}, (binding, namespace, operation, payload) => {
        // decode payload via messagepack
        // const decoded = decode(payload);
        return new Uint8Array(payload);
    })
    // To launch an actrom manually from local disk (note the .wasm is required)
    await host.launchActor("./actor.wasm");
    // To listen for events, you can call the subscribeToEvents and pass an optional callback to handle the event data
    await host.subscribeToEvents((eventData) => console.log(eventData, eventData.source));
    // To unsubscribe, call the unsubscribeEvents
    await host.unsubscribeEvents();
    // To start & stop the heartbeat events
    await host.startHeartbeat();
    await host.stopHeartbeat();
    // The host will automatically connect to nats on start. to connect/reconnect to nats
    await host.connectNATS();
    // To close/drain all connections from nats, call the disconnectNATS() method
    await host.disconnectNATS();
    // Stop the host
    await host.stopHost();
    // Restart the host (this only needs to be called if the host is stopped, it is automatically called on the constructor)
    await host.startHost();
  })();
</script>

With a bundler

There are some caveats to using with a bundler:

  • The module contains .wasm files that need to be present alongside the final build output. Using webpack-copy-plugin (or fs.copyFile with other bundlers) can solve this issue.

  • If using with create-react-app, the webpack config will need to be ejected via npm run eject OR an npm library like react-app-rewired can handle the config injection.

// as esm -- this will grant you access to the types/params
import { startHost } from '@wasmcloud/wasmcloud-js';
// as cjs
// const wasmcloudjs = require('@wasmcloud/wasmcloud-js);

async function cjsHost() {
    const host = await wasmcloudjs.startHost('default', false, ['ws://localhost:4222'])
    console.log(host);
}

async function esmHost() {
    const host = await startHost('default', false, ['ws://localhost:4222'])
    console.log(host);
}

cjsHost();
esmHost();
// webpack config, add this to the plugin section
plugins: [
    new CopyPlugin({
        patterns: [
            {
                from: 'node_modules/@wasmcloud/wasmcloud-js/dist/wasmcloud-rs-js/pkg/*.wasm',
                to: '[name].wasm'
            }
        ]
    }),
]

Node

IN PROGRESS - NodeJS does not support WebSockets natively (required by nats.ws)

Contributing

Running tests

$ npm run test

Building

$ npm run build