From 21f7ee4bc1bb95a7ab7afb5c507f4411afebef69 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Wed, 7 Jan 2026 18:57:11 -0500 Subject: [PATCH] feat(enclave): add ping function for echoing input messages --- example/index.html | 6 ++++++ example/main.js | 21 +++++++++++++++++++++ go.mod | 9 ++++++++- go.sum | 8 ++++++++ main.go | 41 +++++++++++++++++++++++++++++++++++++++++ src/enclave.ts | 12 ++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) diff --git a/example/index.html b/example/index.html index 446067a..4dff646 100644 --- a/example/index.html +++ b/example/index.html @@ -40,6 +40,12 @@ +
+

ping(message)

+ + +
+

generate(credential)

diff --git a/example/main.js b/example/main.js index cfc541e..bfd7928 100644 --- a/example/main.js +++ b/example/main.js @@ -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(); diff --git a/go.mod b/go.mod index b3fa137..de29d03 100644 --- a/go.mod +++ b/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 +) diff --git a/go.sum b/go.sum index c15d382..4e755c3 100644 --- a/go.sum +++ b/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= diff --git a/main.go b/main.go index c222bd8..7575014 100644 --- a/main.go +++ b/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") diff --git a/src/enclave.ts b/src/enclave.ts index 7a00b01..5a3c9dc 100644 --- a/src/enclave.ts +++ b/src/enclave.ts @@ -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 */