Skip to content

Commit 69b2d02

Browse files
authored
Created a QuickStart example/test, to ensure README QuickStart is working (#83)
* Updated the README and added tests for the quickstart code * Added quickstart example tests
1 parent 5f277be commit 69b2d02

File tree

10 files changed

+286
-4
lines changed

10 files changed

+286
-4
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ For **browser** JavaScript. We can do the following:
7373

7474
```javascript
7575
// If you are using a Javascript bundler, use the ESM bundle with import syntax
76-
import { AsBind } from "as-bind";
76+
import * as AsBind from "as-bind";
7777

7878
// If you are not using a bundler add a <script> tag to your HTML
7979
// Where the `src` points to the iife bundle (as-bind.iife.js), for example: https://unpkg.com/as-bind
@@ -93,10 +93,14 @@ const asyncTask = async () => {
9393
asyncTask();
9494
```
9595

96-
For **Node** JavaScript, we would use the CommonJS bundle by do the following:
96+
For **Node** JavaScript, we would use the CommonJS bundle by doing the following:
9797

9898
```javascript
99-
const { AsBind } = require("as-bind");
99+
// We need to import the direct as-bind.cjs.js for Node applications.
100+
// This is because the default "main" key in the `package.json`,
101+
// is the as-bind transform script
102+
const AsBind = require("as-bind/dist/as-bind.cjs.js");
103+
100104
const fs = require("fs");
101105

102106
const wasm = fs.readFileSync("./path-to-my-wasm.wasm");
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Use pupeteer to run in the browser
2+
const puppeteer = require("puppeteer");
3+
4+
// Require rollup to compile our browser.js
5+
const rollup = require("rollup");
6+
const { nodeResolve } = require("@rollup/plugin-node-resolve");
7+
8+
// Get some native node libs, in order to host a static server
9+
const path = require("path");
10+
const fs = require("fs");
11+
const http = require("http");
12+
13+
// Host a static server of the local directory
14+
// https://nodejs.org/en/knowledge/HTTP/servers/how-to-serve-static-files/
15+
http
16+
.createServer(function(req, res) {
17+
fs.readFile(__dirname + req.url, function(err, data) {
18+
if (err) {
19+
res.writeHead(404);
20+
res.end(JSON.stringify(err));
21+
return;
22+
}
23+
res.writeHead(200);
24+
res.end(data);
25+
});
26+
})
27+
.listen(8000);
28+
29+
(async () => {
30+
// Create a rollup bundle and get our compiled browser.js as a string
31+
const bundle = await rollup.rollup({
32+
input: "./browser.js",
33+
plugins: [nodeResolve()]
34+
});
35+
const { output } = await bundle.generate({
36+
format: "iife"
37+
});
38+
const browserQuickstartJs = output[0].code;
39+
40+
// Launch the pupeteer browser and page
41+
const browser = await puppeteer.launch();
42+
const page = await browser.newPage();
43+
await page.goto("http://localhost:8000/browser.html");
44+
45+
// Create a promise that we will resolve or reject once we see any expected behavior
46+
let pageResolve;
47+
let pageReject;
48+
const pageResultPromise = new Promise((resolve, reject) => {
49+
pageResolve = resolve;
50+
pageReject = reject;
51+
});
52+
53+
// Listen to JS Console messages, log them, and resolve our promise on an expected message
54+
page.on("console", message => {
55+
console.log(
56+
`${message
57+
.type()
58+
.substr(0, 3)
59+
.toUpperCase()} ${message.text()}`
60+
);
61+
62+
if (message.text() === "AsBind: Hello World!") {
63+
pageResolve();
64+
return;
65+
}
66+
});
67+
68+
// Listen to JS / Page errors, log them, and reject our promise
69+
page.on("pageerror", err => {
70+
theTempValue = err.toString();
71+
console.log("Error: " + theTempValue);
72+
73+
console.log("Browser Quickstart Failed.");
74+
pageReject();
75+
return;
76+
});
77+
78+
// Run the compiled browser.js code
79+
await page.evaluate(browserQuickstartJs);
80+
81+
// Wait for the promise, and then close everything out
82+
await pageResultPromise;
83+
await browser.close();
84+
process.exit();
85+
})();

examples/quickstart/browser.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>title</title>
6+
</head>
7+
<body></body>
8+
</html>

examples/quickstart/browser.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// If you are using a Javascript bundler, use the ESM bundle with import syntax
2+
import * as AsBind from "as-bind";
3+
4+
// If you are not using a bundler add a <script> tag to your HTML
5+
// Where the `src` points to the iife bundle (as-bind.iife.js), for example: https://unpkg.com/as-bind
6+
// Then, INSTEAD of using the import syntax, do: `const { AsBind } = AsBindIIFE;`
7+
8+
const wasm = fetch("./path-to-my-wasm.wasm");
9+
10+
const asyncTask = async () => {
11+
// There is a buge with WebAssembly.compileStreaming with pupeteer, so going to
12+
// Get the Wasm Binary here and instantiate sync. This is slightly different
13+
// from the README unfortunately
14+
const wasmResponse = await wasm;
15+
const wasmBuffer = await wasmResponse.arrayBuffer();
16+
17+
const asBindInstance = await AsBind.instantiate(wasmBuffer);
18+
19+
// You can now use your wasm / as-bind instance!
20+
const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
21+
"Hello World!"
22+
);
23+
console.log(response); // AsBind: Hello World!
24+
};
25+
asyncTask();

examples/quickstart/nodejs.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// We need to import the direct as-bind.cjs.js for Node applications.
2+
// This is because the default "main" key in the `package.json`,
3+
// is the as-bind transform script
4+
const AsBind = require("as-bind/dist/as-bind.cjs.js");
5+
6+
const fs = require("fs");
7+
8+
const wasm = fs.readFileSync("./path-to-my-wasm.wasm");
9+
10+
const asyncTask = async () => {
11+
const asBindInstance = await AsBind.instantiate(wasm);
12+
13+
// You can now use your wasm / as-bind instance!
14+
const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
15+
"Hello World!"
16+
);
17+
console.log(response); // AsBind: Hello World!
18+
};
19+
asyncTask();

examples/quickstart/package-lock.json

Lines changed: 119 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/quickstart/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "as-bind-quickstart",
3+
"version": "0.0.1",
4+
"description": "Project that tests the README QuickStart examples to ensure that the README works. This depends on node modules from the root package.json",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "asc your-entryfile.ts --exportRuntime --transform as-bind -b path-to-my-wasm.wasm",
8+
"test": "npm run test:node && npm run test:browser",
9+
"test:node": "node nodejs.js",
10+
"test:browser": "node browser-puppeteer.js"
11+
},
12+
"author": "Aaron Turner",
13+
"license": "MIT",
14+
"dependencies": {
15+
"as-bind": "file:../.."
16+
}
17+
}
8.48 KB
Binary file not shown.

examples/quickstart/your-entryfile.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function myExportedFunctionThatTakesAString(value: string): string {
2+
return "AsBind: " + value;
3+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
"build": "run-s lib:js:build",
1111
"dev": "run-p lib:watch lib:test:watch",
1212
"serve": "serve dist -p 8080",
13-
"test": "node ./test/test-runner.js",
13+
"test": "run-s test:lib test:quickstart",
14+
"test:lib": "node ./test/test-runner.js",
15+
"test:quickstart": "(cd examples/quickstart && npm install && npm run test)",
1416
"lint": "prettier --write **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
1517
"lint:ci": "prettier --check **/*.js **/*.ts **/*.json !build/**/* !dist/**/*",
1618
"lib:watch": "chokidar --initial \"lib/**/*\" -c \"run-s lib:js:build:dev test\"",

0 commit comments

Comments
 (0)