mirror of
https://github.com/extism/extism.git
synced 2026-01-10 22:37:58 -05:00
feat: update node sdk to use async call (#17)
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
import { Plugin } from "./index.js";
|
||||
import { readFileSync } from "fs";
|
||||
import { Plugin } from './index.js';
|
||||
import { readFileSync } from 'fs';
|
||||
|
||||
let wasm = readFileSync("../wasm/code.wasm");
|
||||
let wasm = readFileSync('../wasm/code.wasm');
|
||||
let p = new Plugin(wasm);
|
||||
|
||||
if (!p.function_exists("count_vowels")) {
|
||||
console.log("no function 'count_vowels' in wasm");
|
||||
process.exit(1);
|
||||
if (!p.function_exists('count_vowels')) {
|
||||
console.log("no function 'count_vowels' in wasm");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
let buf = p.call("count_vowels", process.argv[2] || "this is a test");
|
||||
console.log(JSON.parse(buf.toString())['count']);
|
||||
let buf = await p.call('count_vowels', process.argv[2] || 'this is a test');
|
||||
console.log(JSON.parse(buf.toString())['count']);
|
||||
|
||||
@@ -19,39 +19,40 @@ let _functions = {
|
||||
function locate(paths) {
|
||||
for (var i = 0; i < paths.length; i++) {
|
||||
try {
|
||||
return ffi.Library(
|
||||
path.join(paths[i], 'libextism'),
|
||||
_functions
|
||||
)
|
||||
return ffi.Library(path.join(paths[i], 'libextism'), _functions);
|
||||
} catch (exn) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw "Unable to locate libextism";
|
||||
throw 'Unable to locate libextism';
|
||||
}
|
||||
|
||||
var searchPath =
|
||||
[__dirname, "/usr/local/lib", "/usr/lib", path.join(process.env.HOME, ".local")]
|
||||
var searchPath = [
|
||||
__dirname,
|
||||
'/usr/local/lib',
|
||||
'/usr/lib',
|
||||
path.join(process.env.HOME, '.local'),
|
||||
];
|
||||
|
||||
if (process.env.EXTISM_PATH) {
|
||||
searchPath.unshift(path.join(process.env.EXTISM_PATH, "lib"));
|
||||
searchPath.unshift(path.join(process.env.EXTISM_PATH, 'lib'));
|
||||
}
|
||||
|
||||
var lib = locate(searchPath);
|
||||
|
||||
export function set_log_file(filename, level = null) {
|
||||
lib.extism_log_file(filename, level)
|
||||
lib.extism_log_file(filename, level);
|
||||
}
|
||||
|
||||
export class Plugin {
|
||||
constructor(data, wasi = false, config = null) {
|
||||
if (typeof data === "object" && data.wasm) {
|
||||
if (typeof data === 'object' && data.wasm) {
|
||||
data = JSON.stringify(data);
|
||||
}
|
||||
let plugin = lib.extism_plugin_register(data, data.length, wasi);
|
||||
if (plugin < 0) {
|
||||
throw "Unable to load plugin";
|
||||
throw 'Unable to load plugin';
|
||||
}
|
||||
this.id = plugin;
|
||||
|
||||
@@ -62,7 +63,7 @@ export class Plugin {
|
||||
}
|
||||
|
||||
update(data, wasi = false, config = null) {
|
||||
if (typeof data === "object" && data.wasm) {
|
||||
if (typeof data === 'object' && data.wasm) {
|
||||
data = JSON.stringify(data);
|
||||
}
|
||||
const ok = lib.extism_plugin_update(this.id, data, data.length, wasi);
|
||||
@@ -79,22 +80,23 @@ export class Plugin {
|
||||
}
|
||||
|
||||
function_exists(name) {
|
||||
return lib.extism_function_exists(this.id, name)
|
||||
return lib.extism_function_exists(this.id, name);
|
||||
}
|
||||
|
||||
call(name, input) {
|
||||
var rc = lib.extism_call(this.id, name, input, input.length);
|
||||
if (rc != 0) {
|
||||
var err = lib.extism_error(this.id);
|
||||
if (err.length == 0) {
|
||||
throw `extism_call: "${name}" failed`;
|
||||
async call(name, input) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var rc = lib.extism_call(this.id, name, input, input.length);
|
||||
if (rc != 0) {
|
||||
var err = lib.extism_error(this.id);
|
||||
if (err.length == 0) {
|
||||
reject(`extism_call: "${name}" failed`);
|
||||
}
|
||||
reject(`Plugin error: ${err.toString()}, code: ${rc}`);
|
||||
}
|
||||
throw `Plugin error: ${err.toString()}, code: ${rc}`;
|
||||
}
|
||||
|
||||
var out_len = lib.extism_output_length(this.id);
|
||||
var buf = Buffer.from(lib.extism_output_get(this.id).buffer, 0, out_len);
|
||||
return buf;
|
||||
var out_len = lib.extism_output_length(this.id);
|
||||
var buf = Buffer.from(lib.extism_output_get(this.id).buffer, 0, out_len);
|
||||
resolve(buf);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
10
node/package-lock.json
generated
10
node/package-lock.json
generated
@@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "node",
|
||||
"version": "1.0.0",
|
||||
"name": "@extism/extism",
|
||||
"version": "0.0.1-rc.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "node",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"name": "@extism/extism",
|
||||
"version": "0.0.1-rc.3",
|
||||
"license": "BSD-3-Clause",
|
||||
"dependencies": {
|
||||
"ffi-napi": "^4.0.3"
|
||||
}
|
||||
|
||||
@@ -2,13 +2,7 @@
|
||||
"name": "@extism/extism",
|
||||
"version": "0.0.1-rc.3",
|
||||
"description": "Extism Host SDK for Node",
|
||||
"keywords": [
|
||||
"extism",
|
||||
"webassembly",
|
||||
"wasm",
|
||||
"plugins",
|
||||
"extension"
|
||||
],
|
||||
"keywords": ["extism", "webassembly", "wasm", "plugins", "extension"],
|
||||
"author": "The Extism Authors <oss@extism.org>",
|
||||
"license": "BSD-3-Clause",
|
||||
"private": false,
|
||||
@@ -28,4 +22,4 @@
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user