Skip to content

Commit

Permalink
Merge pull request #26 from extism/update-namespace3
Browse files Browse the repository at this point in the history
chore: run formatting
  • Loading branch information
nilslice authored Nov 12, 2023
2 parents 19daea0 + db7fee0 commit bcdcd4a
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 42 deletions.
2 changes: 1 addition & 1 deletion examples/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
Expand Down
2 changes: 1 addition & 1 deletion src/background-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ class HttpContext {
}

let { header, url: rawUrl, method } = req.json();
method ??= "GET";
method ??= 'GET';
const url = new URL(rawUrl);

const isAllowed = this.allowedHosts.some((allowedHost) => {
Expand Down
10 changes: 3 additions & 7 deletions src/foreground-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export async function createForegroundPlugin(
const imports: Record<string, Record<string, any>> = {
...(wasi ? { wasi_snapshot_preview1: await wasi.importObject() } : {}),
[EXTISM_ENV]: context[ENV],
"env": {}
env: {},
};

for (const namespace in opts.functions) {
Expand All @@ -178,19 +178,15 @@ export async function createForegroundPlugin(
await wasi?.initialize(module.instance);
}

const guestType =
module.instance.exports.hs_init
const guestType = module.instance.exports.hs_init
? 'haskell'
: module.instance.exports._initialize
? 'reactor'
: module.instance.exports._start
? 'command'
: 'none';

const initRuntime: any =
module.instance.exports.hs_init
? module.instance.exports.hs_init
: () => {};
const initRuntime: any = module.instance.exports.hs_init ? module.instance.exports.hs_init : () => {};
initRuntime();

return { module, guestType };
Expand Down
47 changes: 27 additions & 20 deletions src/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ if (typeof WebAssembly === 'undefined') {
'store_u64',
'store_u8',
'var_get',
'var_set'
'var_set',
].sort(),
);
} finally {
Expand All @@ -132,7 +132,11 @@ if (typeof WebAssembly === 'undefined') {
const result = await plugin.call('count_vowels', 'hello world');
assert(result, 'result is not null');

assert.deepEqual(JSON.parse(new TextDecoder().decode(result.buffer)), { count: 3, total: 3, vowels: 'aeiouAEIOU' });
assert.deepEqual(JSON.parse(new TextDecoder().decode(result.buffer)), {
count: 3,
total: 3,
vowels: 'aeiouAEIOU',
});
} finally {
await plugin.close();
}
Expand Down Expand Up @@ -169,7 +173,7 @@ if (typeof WebAssembly === 'undefined') {
test('host functions may read info from context and return values', async () => {
let executed: any;
const functions = {
"extism:host/user": {
'extism:host/user': {
hello_world(context: CallContext, off: bigint) {
executed = context.read(off)?.string();
return context.store('wow okay then');
Expand All @@ -195,7 +199,7 @@ if (typeof WebAssembly === 'undefined') {
let callContext: CallContext | null = null;

const functions = {
"extism:host/user": {
'extism:host/user': {
hello_world(context: CallContext, off: bigint) {
callContext = context;

Expand Down Expand Up @@ -229,7 +233,7 @@ if (typeof WebAssembly === 'undefined') {
test('host functions reject original promise when throwing', async () => {
const expected = String(Math.random());
const functions = {
"extism:host/user": {
'extism:host/user': {
hello_world(_context: CallContext, _off: bigint) {
throw new Error(expected);
},
Expand Down Expand Up @@ -271,7 +275,7 @@ if (typeof WebAssembly === 'undefined') {
if (CAPABILITIES.hasWorkerCapability) {
test('host functions may be async if worker is off-main-thread', async () => {
const functions = {
"extism:host/user": {
'extism:host/user': {
async hello_world(context: CallContext, _off: bigint) {
await new Promise((resolve) => setTimeout(resolve, 100));
return context.store('it works');
Expand All @@ -296,7 +300,7 @@ if (typeof WebAssembly === 'undefined') {
const res = await fetch('http://localhost:8124/src/mod.test.ts');
const result = await res.text();
const functions = {
"extism:host/user": {
'extism:host/user': {
async hello_world(context: CallContext, _off: bigint) {
context.setVariable('hmmm okay storing a variable', 'hello world hello.');
const res = await fetch('http://localhost:8124/src/mod.test.ts');
Expand Down Expand Up @@ -324,7 +328,7 @@ if (typeof WebAssembly === 'undefined') {

test('host functions may not be reentrant off-main-thread', async () => {
const functions = {
"extism:host/user": {
'extism:host/user': {
async hello_world(context: CallContext, _off: bigint) {
await plugin?.call('count_vowels', 'hello world');
return context.store('it works');
Expand Down Expand Up @@ -353,7 +357,7 @@ if (typeof WebAssembly === 'undefined') {
if (!CAPABILITIES.crossOriginChecksEnforced)
test('http fails as expected when no allowed hosts match', async () => {
const functions = {
"extism:host/user": {
'extism:host/user': {
async hello_world(context: CallContext, _off: bigint) {
await new Promise((resolve) => setTimeout(resolve, 100));
return context.store('it works');
Expand All @@ -367,10 +371,12 @@ if (typeof WebAssembly === 'undefined') {
);

try {
const [err, data] = await plugin.call('http_get', '{"url": "https://jsonplaceholder.typicode.com/todos/1"}').then(
(data) => [null, data],
(err) => [err, null],
);
const [err, data] = await plugin
.call('http_get', '{"url": "https://jsonplaceholder.typicode.com/todos/1"}')
.then(
(data) => [null, data],
(err) => [err, null],
);

assert(data === null);
assert.equal(
Expand All @@ -384,12 +390,12 @@ if (typeof WebAssembly === 'undefined') {

test('http works as expected when host is allowed', async () => {
const functions = {
"extism:host/user": {
'extism:host/user': {
async hello_world(context: CallContext, _off: bigint) {
await new Promise((resolve) => setTimeout(resolve, 100));
return context.store('it works');
},
}
},
};

const plugin = await createPlugin(
Expand All @@ -398,10 +404,12 @@ if (typeof WebAssembly === 'undefined') {
);

try {
const [err, data] = await plugin.call('http_get', '{"url": "https://jsonplaceholder.typicode.com/todos/1"}').then(
(data) => [null, data],
(err) => [err, null],
);
const [err, data] = await plugin
.call('http_get', '{"url": "https://jsonplaceholder.typicode.com/todos/1"}')
.then(
(data) => [null, data],
(err) => [err, null],
);
assert(err === null);
assert.deepEqual(data.json(), {
userId: 1,
Expand Down Expand Up @@ -454,7 +462,6 @@ if (typeof WebAssembly === 'undefined') {
}
});


if (CAPABILITIES.supportsWasiPreview1) {
test('can initialize Haskell runtime', async () => {
const plugin = await createPlugin('http://localhost:8124/wasm/hello_haskell.wasm', {
Expand Down
6 changes: 3 additions & 3 deletions src/polyfills/browser-wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export async function loadWasi(_allowedPaths: { [from: string]: string }): Promi

if (instance.exports._initialize) {
const init = instance.exports._initialize as CallableFunction;
if (context.initialize){
if (context.initialize) {
context.initialize({
exports: {
memory,
_initialize: () => {
init();
}
}
},
},
});
} else {
init();
Expand Down
5 changes: 2 additions & 3 deletions src/polyfills/deno-wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export async function loadWasi(allowedPaths: { [from: string]: string }): Promis
async initialize(instance: WebAssembly.Instance) {
const memory = instance.exports.memory as WebAssembly.Memory;


if (!memory) {
throw new Error('The module has to export a default memory.');
}
Expand All @@ -28,8 +27,8 @@ export async function loadWasi(allowedPaths: { [from: string]: string }): Promis
memory,
_initialize: () => {
init();
}
}
},
},
});
} else {
init();
Expand Down
6 changes: 3 additions & 3 deletions src/polyfills/node-wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ export async function loadWasi(allowedPaths: { [from: string]: string }): Promis
throw new Error('The module has to export a default memory.');
}

if (instance.exports._initialize){
if (instance.exports._initialize) {
const init = instance.exports._initialize as CallableFunction;
if (context.initialize) {
context.initialize({
exports: {
memory,
_initialize: () => {
init();
}
}
},
},
});
} else {
init();
Expand Down
5 changes: 1 addition & 4 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { parentPort } from 'node:worker_threads';

import {
ForegroundPlugin,
createForegroundPlugin as _createForegroundPlugin,
} from './foreground-plugin.ts';
import { ForegroundPlugin, createForegroundPlugin as _createForegroundPlugin } from './foreground-plugin.ts';
import { CallContext, EXPORT_STATE, CallState, IMPORT_STATE } from './call-context.ts';
import { type InternalConfig } from './interfaces.ts';

Expand Down

0 comments on commit bcdcd4a

Please sign in to comment.