Skip to content

Commit

Permalink
Merge pull request #429 from wasmerio/multiple-changes
Browse files Browse the repository at this point in the history
Multiple changes
  • Loading branch information
syrusakbary authored Aug 8, 2024
2 parents 0ad706a + 86a0a38 commit 3dfb587
Show file tree
Hide file tree
Showing 41 changed files with 3,434 additions and 1,645 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
lazy_static = "1.5"
anyhow = "1"
async-trait = "0.1"
bincode = "1"
Expand Down
231 changes: 107 additions & 124 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,24 @@ console.log(`Python exited with ${code}: ${stdout}`);
```
### Use with a `<script>` tag (without bundler)

It is possible to avoid needing to use a bundler by importing `@wasmer/sdk` as
a UMD module.

By adding the following `<script>` tag to your `index.html` file, the library
will be available as the `WasmerSDK` global variable.

```html
<script src="https://unpkg.com/@wasmer/sdk@latest"></script>
<script>
const { init, Wasmer } = WasmerSDK;
async function runPython() {
await init();
const packageName = "python/python";
const pkg = await Wasmer.fromRegistry(packageName);
const instance = await pkg.entrypoint.run({
args: ["-c", "print('Hello, World!')"],
});
const { code, stdout } = await instance.wait();
console.log(`Python exited with ${code}: ${stdout}`);
}
runPython();
</script>
```

Alternatively, the package can be imported directly from the CDN by turning the
script into a module.
It is possible to avoid needing to use a bundler by importing `@wasmer/sdk` from your script tag in unpkg.

```html
<script defer type="module">
import { init, Wasmer } from "https://unpkg.com/@wasmer/sdk@latest?module";
import { init, Wasmer } from "https://unpkg.com/@wasmer/sdk@latest";
async function runPython() {
await init();
...
const packageName = "python/python";
const pkg = await Wasmer.fromRegistry(packageName);
const instance = await pkg.entrypoint.run({
args: ["-c", "print('Hello, World!')"],
});
const { code, stdout } = await instance.wait();
console.log(`Python exited with ${code}: ${stdout}`);
}
runPython();
Expand All @@ -84,27 +62,28 @@ script into a module.

### Using a custom Wasm file

By default, `WasmerSDK.init` will load the Wasmer SDK WebAssembly file from unpkg.com.
By default, `init` will load the Wasmer SDK WebAssembly file from the package.
If you want to customize this behavior you can pass a custom url to the init, so the the wasm file
of the Wasmer SDK can ve served by your HTTP server instead:

```js
import { init, Wasmer } from "@wasmer/sdk";
import wasmUrl from "@wasmer/sdk/dist/wasmer_js_bg.wasm?url";
import wasmerSDKModule from "@wasmer/sdk/wasm?url";

await init({module: wasmUrl}); // This inits the SDK with a custom URL
await init({ module: wasmUrl }); // This inits the SDK with a custom URL
```


### Using a JS with the Wasm bundled

You can also load Wasmer-JS with a js file with the Wasmer SDK WebAssembly file bundled into it (using base64 encoding),
so no extra requests are required. If that's your use case, you can simply import `@wasmer/sdk/dist/WasmerSDKBundled.mjs` instead:
so no extra requests are required. If that's your use case, you can simply import `@wasmer/sdk/wasm-inline`:

```js
import { init, Wasmer } from "@wasmer/sdk/dist/WasmerSDKBundled.mjs";
import { init, Wasmer } from "@wasmer/sdk";
import wasmerSDKModule from "@wasmer/sdk/wasm-inline";

await init(); // This inits the SDK in the bundled version
await init({ module: wasmerSDKModule }); // This uses the inline wasmer SDK version
```


Expand Down Expand Up @@ -136,30 +115,31 @@ the *Troubleshooting Common Problems* docs for more.
### Creating packages

Users can create packages providing a manifest and using the `Wasmer.createPackage()` function:

```js
import wasmUrl from "@wasmer/sdk";
import { init, Wasmer } from "@wasmer/sdk";
await init({token: "YOUR_TOKEN"});
await init({ token: "YOUR_TOKEN" });
const manifest = {
command: [
{
module: "wasmer/python:python",
name: "hello",
runner: "wasi",
annotations: {
wasi: {
"main-args": [
"-c",
"print('Hello, js!'); ",
],
},
},
},
],
dependencies: {
"wasmer/python": "3.12.9+build.9",
}
command: [
{
module: "wasmer/python:python",
name: "hello",
runner: "wasi",
annotations: {
wasi: {
"main-args": [
"-c",
"print('Hello, js!'); ",
],
},
},
},
],
dependencies: {
"wasmer/python": "3.12.9+build.9",
}
};
let pkg = await Wasmer.createPackage(manifest);
Expand All @@ -170,34 +150,35 @@ console.log(output)
```

### Publishing packages

User can publish packages following the same flow used to create a package and then calling the `Wasmer.publishPackage()` function:
```js
import wasmUrl from "@wasmer/sdk";
import { init, Wasmer } from "@wasmer/sdk";
await init({token: "YOUR_TOKEN"});
await init({ token: "YOUR_TOKEN" });
const manifest = {
package: {
name: "<YOUR_NAME>/<YOUR_PACKAGE_NAME>"
}
command: [
{
module: "wasmer/python:python",
name: "hello",
runner: "wasi",
annotations: {
wasi: {
"main-args": [
"-c",
"print('Hello, js!'); ",
],
},
},
},
],
dependencies: {
"wasmer/python": "3.12.9+build.9",
}
command: [
{
module: "wasmer/python:python",
name: "hello",
runner: "wasi",
annotations: {
wasi: {
"main-args": [
"-c",
"print('Hello, js!'); ",
],
},
},
},
],
dependencies: {
"wasmer/python": "3.12.9+build.9",
}
};
let pkg = await Wasmer.createPackage(manifest);
Expand All @@ -208,9 +189,10 @@ Trying to publish packages without a `package.name` property in the manifest wil
### Deploying apps
User can deploy apps by providing an app configuration and calling the `Wasmer.deployApp()` function:
```js
import wasmUrl from "@wasmer/sdk";
import { init, Wasmer } from "@wasmer/sdk";
await init({token: "YOUR_TOKEN"});
// Get your token here: https://wasmer.io/settings/access-tokens
await init({ token: "YOUR_TOKEN" });
let appConfig = {
name: "<YOUR_APP_NAME>",
Expand All @@ -227,60 +209,61 @@ Users can also publish apps with their own packages simply providing the package
```js
import wasmUrl from "@wasmer/sdk";
// Get your token here: https://wasmer.io/settings/access-tokens
await init({token: "YOUR_TOKEN"});
const echo_server_index = `
async function handler(request) {
const out = JSON.stringify({
env: process.env,
});
return new Response(out, {
headers: { "content-type": "application/json" },
});
}
addEventListener("fetch", (fetchEvent) => {
fetchEvent.respondWith(handler(fetchEvent.request));
});
`;
async function handler(request) {
const out = JSON.stringify({
env: process.env,
});
return new Response(out, {
headers: { "content-type": "application/json" },
});
}
addEventListener("fetch", (fetchEvent) => {
fetchEvent.respondWith(handler(fetchEvent.request));
});
`;
const manifest =
{
"command": [
{
"module": "wasmer/winterjs:winterjs",
"name": "script",
"runner": "https://webc.org/runner/wasi",
"annotations": {
"wasi": {
"env": [
"JS_PATH=/src/index.js"
],
"main-args": [
"/src/index.js"
]
}
}
}
],
"dependencies": {
"wasmer/winterjs": "1.2.0"
},
"fs": {
"/src": {
"index.js": echo_server_index
}
},
"command": [
{
"module": "wasmer/winterjs:winterjs",
"name": "script",
"runner": "https://webc.org/runner/wasi",
"annotations": {
"wasi": {
"env": [
"JS_PATH=/src/index.js"
],
"main-args": [
"/src/index.js"
]
}
}
}
],
"dependencies": {
"wasmer/winterjs": "1.2.0"
},
"fs": {
"/src": {
"index.js": echo_server_index
}
},
};
let wasmerPackage = await Wasmer.createPackage(manifest);
let appConfig = {
name: "my-echo-env-app",
owner: "edoardo",
package: wasmerPackage,
default: true,
name: "my-echo-env-app",
owner: "edoardo",
package: wasmerPackage,
default: true,
};
let res = await Wasmer.deployApp(appConfig);
Expand All @@ -303,7 +286,7 @@ The Wasmer SDK Javascript Package supports:
* [X] Running packages from the [Wasmer Registry](https://wasmer.io)
* [X] Platforms
* [X] Browser
* [ ] NodeJS
* [x] NodeJS
* [ ] Deno
* [X] Registry API
* [X] Create a package
Expand Down
2 changes: 1 addition & 1 deletion docs/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"entryPoints": [
"../WasmerSDK.ts"
"../src-js/index.ts"
],
"out": "public",
"$schema": "https://typedoc.org/schema.json"
Expand Down
Loading

0 comments on commit 3dfb587

Please sign in to comment.