extism/js-sdk

http_request is not enabled

Closed this issue · 2 comments

Enviroment

platform: Ubuntu 22.04.3 / Windows 10

CPU: x64

Runtime: Nodejs 20

Issue

If run with js-sdk and use extism_http_request API, showhttp_request is not enabledmessage,

If run with extism cli, run success.

js code

const createPlugin = require("@extism/extism")

async function main() {
    const plugin = await createPlugin("./a.wasm",{
        useWasi: true,
        withAllowedHosts: ['www.google.com'],
        allowedHosts: ['www.google.com']
    })
    const ret = await plugin.call("hello")
    console.log(ret)
}
main()

c code

#define EXTISM_IMPLEMENTATION
#include "extism-pdk.h"
int32_t EXTISM_EXPORTED_FUNCTION(hello) {
	char req_str[] = "{\"method\":\"GET\",\"url\":\"http://www.google.com\"}";
	ExtismHandle req = extism_alloc_buf_from_sz(req_str);
	ExtismHandle res = extism_http_request(req, 0);
	extism_output_set_from_handle(res, 0, extism_length(res));
	return 0;
}

build and run

/opt/wasi-sdk/bin/clang -o a.wasm --target=wasm32-unknown-unknown -nostdlib -Wl,--no-entry a.c
node c.js

run result

截图 2024-02-16 16-22-39
截图 2024-02-16 16-15-14

Ah – since HTTP requests are asynchronous, http_request is only enabled when runInWorker is enabled. You might try passing that during your initial setup:

const createPlugin = require("@extism/extism")

async function main() {
    const plugin = await createPlugin("./a.wasm",{
        useWasi: true,
        withAllowedHosts: ['www.google.com'],
        allowedHosts: ['www.google.com'],
        runInWorker: true
    })
    const ret = await plugin.call("hello")
    console.log(ret)
}
main()

Thank you very much. And you fix js-sdk referencewithAllowedHost.