Add Extism JS Async Lib and State #1
@@ -40,6 +40,12 @@
|
||||
<button onclick="runAllTests()" style="margin-left: 1rem;">Run All Tests</button>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>ping(message)</h2>
|
||||
<input type="text" id="ping-msg" value="hello from browser" placeholder="Message to echo">
|
||||
<button onclick="testPing()">Run</button>
|
||||
</div>
|
||||
|
||||
<div class="card">
|
||||
<h2>generate(credential)</h2>
|
||||
<input type="text" id="credential" value="dGVzdC1jcmVkZW50aWFs" placeholder="Base64 credential">
|
||||
|
||||
@@ -40,6 +40,26 @@ async function init() {
|
||||
}
|
||||
}
|
||||
|
||||
window.testPing = async function() {
|
||||
if (!enclave) return log(LogLevel.ERR, 'ping', 'Plugin not loaded');
|
||||
|
||||
const message = document.getElementById('ping-msg').value || 'hello';
|
||||
log(LogLevel.INFO, 'ping', `message="${message}"`);
|
||||
|
||||
try {
|
||||
const result = await enclave.ping(message);
|
||||
if (result.success) {
|
||||
log(LogLevel.OK, 'ping', `echo="${result.echo}"`, result);
|
||||
} else {
|
||||
log(LogLevel.ERR, 'ping', result.message, result);
|
||||
}
|
||||
return result;
|
||||
} catch (err) {
|
||||
log(LogLevel.ERR, 'ping', err?.message || String(err));
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
window.testGenerate = async function() {
|
||||
if (!enclave) return log(LogLevel.ERR, 'generate', 'Plugin not loaded');
|
||||
|
||||
@@ -137,6 +157,7 @@ window.runAllTests = async function() {
|
||||
log(LogLevel.INFO, null, '=== Running all tests ===');
|
||||
|
||||
try {
|
||||
await testPing();
|
||||
await testGenerate();
|
||||
await testLoad();
|
||||
await testExec();
|
||||
|
||||
9
go.mod
9
go.mod
@@ -2,4 +2,11 @@ module enclave
|
||||
|
||||
go 1.25.5
|
||||
|
||||
require github.com/extism/go-pdk v1.1.3 // indirect
|
||||
require github.com/extism/go-pdk v1.1.3
|
||||
|
||||
require (
|
||||
github.com/ncruces/go-sqlite3 v0.30.4 // indirect
|
||||
github.com/ncruces/julianday v1.0.0 // indirect
|
||||
github.com/tetratelabs/wazero v1.11.0 // indirect
|
||||
golang.org/x/sys v0.39.0 // indirect
|
||||
)
|
||||
|
||||
8
go.sum
8
go.sum
@@ -1,2 +1,10 @@
|
||||
github.com/extism/go-pdk v1.1.3 h1:hfViMPWrqjN6u67cIYRALZTZLk/enSPpNKa+rZ9X2SQ=
|
||||
github.com/extism/go-pdk v1.1.3/go.mod h1:Gz+LIU/YCKnKXhgge8yo5Yu1F/lbv7KtKFkiCSzW/P4=
|
||||
github.com/ncruces/go-sqlite3 v0.30.4 h1:j9hEoOL7f9ZoXl8uqXVniaq1VNwlWAXihZbTvhqPPjA=
|
||||
github.com/ncruces/go-sqlite3 v0.30.4/go.mod h1:7WR20VSC5IZusKhUdiR9y1NsUqnZgqIYCmKKoMEYg68=
|
||||
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
|
||||
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
|
||||
github.com/tetratelabs/wazero v1.11.0 h1:+gKemEuKCTevU4d7ZTzlsvgd1uaToIDtlQlmNbwqYhA=
|
||||
github.com/tetratelabs/wazero v1.11.0/go.mod h1:eV28rsN8Q+xwjogd7f4/Pp4xFxO7uOGbLcD/LzB1wiU=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
|
||||
41
main.go
41
main.go
@@ -108,6 +108,47 @@ var enclave = &Enclave{}
|
||||
|
||||
func main() {}
|
||||
|
||||
// PingInput represents the input for the ping function
|
||||
type PingInput struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// PingOutput represents the output of the ping function
|
||||
type PingOutput struct {
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Echo string `json:"echo"`
|
||||
}
|
||||
|
||||
//go:wasmexport ping
|
||||
func ping() int32 {
|
||||
pdk.Log(pdk.LogInfo, "ping: received request")
|
||||
|
||||
var input PingInput
|
||||
if err := pdk.InputJSON(&input); err != nil {
|
||||
output := PingOutput{
|
||||
Success: false,
|
||||
Message: fmt.Sprintf("failed to parse input: %s", err),
|
||||
}
|
||||
pdk.OutputJSON(output)
|
||||
return 0
|
||||
}
|
||||
|
||||
output := PingOutput{
|
||||
Success: true,
|
||||
Message: "pong",
|
||||
Echo: input.Message,
|
||||
}
|
||||
|
||||
if err := pdk.OutputJSON(output); err != nil {
|
||||
pdk.Log(pdk.LogError, fmt.Sprintf("ping: failed to output: %s", err))
|
||||
return 1
|
||||
}
|
||||
|
||||
pdk.Log(pdk.LogInfo, fmt.Sprintf("ping: responded with echo=%s", input.Message))
|
||||
return 0
|
||||
}
|
||||
|
||||
//go:wasmexport generate
|
||||
func generate() int32 {
|
||||
pdk.Log(pdk.LogInfo, "generate: starting database initialization")
|
||||
|
||||
@@ -157,6 +157,18 @@ export class Enclave {
|
||||
return output;
|
||||
}
|
||||
|
||||
async ping(message: string = 'hello'): Promise<{ success: boolean; message: string; echo: string }> {
|
||||
this.log(`ping: sending "${message}"`);
|
||||
|
||||
const input = JSON.stringify({ message });
|
||||
const result = await this.plugin.call('ping', input);
|
||||
if (!result) throw new Error('ping: plugin returned no output');
|
||||
const output = result.json() as { success: boolean; message: string; echo: string };
|
||||
|
||||
this.log(`ping: received ${output.success ? 'pong' : 'error'}`);
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset plugin state
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user