Skip to content

Latest commit

 

History

History
61 lines (43 loc) · 1.05 KB

09-extism-js-plug-in.md

File metadata and controls

61 lines (43 loc) · 1.05 KB

Create a JavaScript plug-in

https://github.com/extism/js-pdk

Create a directory 09-extism-js-plug-in with two files: index.js and index.d.ts:

mkdir 09-extism-js-plug-in
cd 09-extism-js-plug-in
touch index.js
touch index.d.ts

Content of index.js

The program exports a hello function which will take a data as a string and return a result string:

function hello() {
	let data = Host.inputString()

	let text = "💛 Hello " + data

    console.log(text)

	let result = {
		message: text,
        from: "Extism"
	}
    
	Host.outputString(JSON.stringify(result))
}

module.exports = { hello }

Content of index.d.ts

Describe the Wasm interface for the plug-in in a typescript module DTS file:

declare module 'main' {
    export function hello(): I32;
}

Build the plug-in

extism-js index.js -i index.d.ts -o hello-js.wasm

Run the plug-in (and call the hello function)

extism call hello-js.wasm \
  hello \
  --input "👩 Jane Doe" \
  --log-level "info" \
  --wasi