Code Monkey home page Code Monkey logo

spin-js-sdk's Introduction

spin-js-sdk

This is an experimental SDK for building Spin apps using JavaScript (and TypeScript, etc.). It borrows heavily from Javy, using the same approach of providing a CLI utility to convert a JS file into a Wasm file. Ultimately we plan to package this as a Spin plugin which can be invoked from an NPM build script as the final step.

The top level directory contains an example HTTP trigger implemented using NPM, Webpack, etc. See src/index.js for details

Using the SDK

To build spin components from js, the Spin plugin js2wasm needs to be installed. It can be installed with the following commands:

spin plugin update
spin plugin install js2wasm

When prompted, choose "y" after reviewing the license and the source of the package.

Once the plugin is installed, it can be invoked as:

$ spin js2wasm --help
js2wasm 0.1.0
A spin plugin to convert JavaScript files to Spin compatible modules

USAGE:
    js2wasm [OPTIONS] <input>

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
    -o <output>         [default: index.wasm]

ARGS:
    <input>

This plugin takes in a JavaScript file and converts it into a Spin compatible .wasm modules

Installing the templates

The JavaScript and TypeScript templates can be installed using the following command:

$ spin templates install --git https://github.com/fermyon/spin-js-sdk

Copying remote template source
Installing template http-ts...
Installing template http-js...
Installed 2 template(s)

+-------------------------------------------------+
| Name      Description                           |
+=================================================+
| http-js   HTTP request handler using JavaScript |
| http-ts   HTTP request handler using TypeScript |
+-------------------------------------------------+

Once the templates are installed, a new TypeScript project can be instantiated using:

spin new -t http-ts hello_world --accept-defaults

To run the created template:

cd hello-world
npm install
spin build
spin up

Building

You need to build the SDK from source to use it.

Prerequisites

  • Make
  • CMake
  • NPM
  • Rust (including the wasm32-wasi target)
  • WASI SDK (version 16.0) installed at /opt/wasi-sdk

Steps

Setup SDK

npm install --prefix crates/spin-js-engine/src/js_sdk
make

The build produces the spinjs utility, which is used to build wasm modules from JavaScript or TypeScript source. For convenience you can move spinjs to a directory in your path - e.g., sudo cp target/release/spinjs /usr/local/bin/spinjs.

Build and run the examples

There are some examples provided in the examples directory. Each example has a readme on building and running it.

Ensure you have the latest version of Spin and the js2wasm plugin installed.

Note: These examples track Spin's main branch, so you may need to ensure you are using the canary Spin release.

spin-js-sdk's People

Contributors

karthik2804 avatar dicej avatar vdice avatar dependabot[bot] avatar rylev avatar thorstenhans avatar radu-matei avatar itowlson avatar calebschoepp avatar mikkelhegn avatar fibonacci1729 avatar jianghong avatar onstwedder avatar oferh avatar rajatjindal 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.