js-sdk icon indicating copy to clipboard operation
js-sdk copied to clipboard

http_request is not enabled

Open 7aGiven opened this issue 2 years ago • 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

7aGiven avatar Feb 16 '24 08:02 7aGiven

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()

chrisdickinson avatar Feb 16 '24 21:02 chrisdickinson

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

7aGiven avatar Feb 17 '24 00:02 7aGiven