diff --git a/.changeset/itchy-dots-decide.md b/.changeset/itchy-dots-decide.md new file mode 100644 index 0000000..eeddd3f --- /dev/null +++ b/.changeset/itchy-dots-decide.md @@ -0,0 +1,5 @@ +--- +'@open-iga/aws': minor +--- + +Adjust fetch polyfill to use host fetch instead of default fetch from Extism diff --git a/connectors/aws/docker-compose.yml b/connectors/aws/docker-compose.yml index 07fff16..20f203e 100644 --- a/connectors/aws/docker-compose.yml +++ b/connectors/aws/docker-compose.yml @@ -8,7 +8,7 @@ services: stackport: image: davireis/stackport:latest ports: - - '8080:8080' + - '8000:8080' environment: AWS_ENDPOINT_URL: http://ministack:4566 AWS_REGION: us-east-1 diff --git a/packages/connector-sdk/extism-jspdk.d.ts b/packages/connector-sdk/extism-jspdk.d.ts index 85b6a0e..12a3f24 100644 --- a/packages/connector-sdk/extism-jspdk.d.ts +++ b/packages/connector-sdk/extism-jspdk.d.ts @@ -9,5 +9,7 @@ declare module 'extism:host' { interface user { // offset of a JSON EmailPayload in wasm memory -> 0 ok / non-zero failure sendEmail(offset: I64): I64; + // offset of a JSON {url, method, headers, body} in wasm memory -> offset of a JSON {status, body, error?} (0 on failure) + fetch(offset: I64): I64; } } diff --git a/packages/connector-sdk/lib/bun-plugin/codegen.plugin.ts b/packages/connector-sdk/lib/bun-plugin/codegen.plugin.ts index 92f355a..ac34945 100644 --- a/packages/connector-sdk/lib/bun-plugin/codegen.plugin.ts +++ b/packages/connector-sdk/lib/bun-plugin/codegen.plugin.ts @@ -20,6 +20,7 @@ const staticTypeFile = ` declare module 'extism:host' { interface user { sendEmail(offset: I64): I64 + fetch(offset: I64): I64 } } `; diff --git a/packages/connector-sdk/lib/polyfills/fetch.ts b/packages/connector-sdk/lib/polyfills/fetch.ts index 02b3e1c..62621dc 100644 --- a/packages/connector-sdk/lib/polyfills/fetch.ts +++ b/packages/connector-sdk/lib/polyfills/fetch.ts @@ -1,18 +1,19 @@ /** - * Guest-runtime `fetch` polyfill. The extism-js (QuickJS) engine performs host-backed - * HTTP through the synchronous `Http.request` global rather than a `fetch` function, so + * Guest-runtime `fetch` polyfill. The extism-js (QuickJS) engine has no `fetch` function, so * fetch-based connector code and libraries (e.g. aws4fetch) have nothing to call. * * This maps the subset of `fetch` connectors use — `(url, { method, headers, body })` - * returning an object with `status`, `ok`, `text()` and `json()` — onto `Http.request`. - * Response headers are not surfaced because `Http.request` does not expose them. + * returning an object with `status`, `ok`, `text()` and `json()` — onto the Host's `fetch` + * capability. The Host owns egress (allowed domains + the operation's declared endpoints); + * Extism's built-in `Http.request` is disabled by the Host. + * Response headers are not surfaced because the Host does not return them. */ -declare const Http: { - request( - req: { url: string; method?: string; headers?: Record }, - body?: string | ArrayBufferLike, - ): { body: string; status: number }; -}; +// JSON the Host `fetch` capability returns; `error` is set when the Host denied or failed the request. +interface HostFetchResponse { + status: number; + body: string; + error?: string; +} type FetchInit = { method?: string; @@ -42,15 +43,29 @@ const toHeaderRecord = (headers: FetchInit['headers']): Record = }; export const installFetch = () => { - if (typeof (globalThis as { fetch?: unknown }).fetch === 'function') return; - + // Always override: newer extism-js ships its own `fetch` over the built-in `Http.request`, which the Host disables. const polyfillFetch = async (input: string | { url?: string }, init: FetchInit = {}): Promise => { const url = typeof input === 'string' ? input : (input?.url ?? ''); const method = String(init.method ?? 'GET').toUpperCase(); const headers = toHeaderRecord(init.headers); const body = init.body ?? undefined; - const response = Http.request({ url, method, headers }, body ?? undefined); + const request = Memory.fromString(JSON.stringify({ url, method, headers, body: body ?? '' })); + let response: HostFetchResponse; + try { + const responseOffset = Host.getFunctions().fetch(request.offset); + if (!responseOffset) throw new Error(`fetch to ${url} failed: the Host returned no response`); + const responseMemory = Memory.find(responseOffset); + try { + response = responseMemory.readJsonObject(); + } finally { + responseMemory.free(); + } + } finally { + request.free(); + } + if (response.error) throw new Error(response.error); + const status = response.status; return { diff --git a/packages/connector-sdk/lib/runtime-env.d.ts b/packages/connector-sdk/lib/runtime-env.d.ts index a0e352c..a0caa7a 100644 --- a/packages/connector-sdk/lib/runtime-env.d.ts +++ b/packages/connector-sdk/lib/runtime-env.d.ts @@ -1,12 +1,12 @@ // oxlint-disable-next-line typescript/triple-slash-reference /// // Pull in the full Extism PDK globals (Host, Memory, getFunctions) plus the SDK's host-function -// augmentation (sendEmail) so connectors consuming this env can call Host capabilities. The SDK's +// augmentation (sendEmail, fetch) so connectors consuming this env can call Host capabilities. The SDK's // own build already loads this file, so the reference simply dedupes there. // oxlint-disable-next-line typescript/triple-slash-reference /// -// --- fetch (GUARANTEED by the SDK polyfill over Http.request; extism omits it) ------ +// --- fetch (GUARANTEED by the SDK polyfill over the Host fetch capability; extism omits it) ------ interface FetchResponse { readonly status: number; readonly ok: boolean; diff --git a/packages/connector-sdk/lib/test/mock-host.ts b/packages/connector-sdk/lib/test/mock-host.ts index 6f10fb1..fc1fe89 100644 --- a/packages/connector-sdk/lib/test/mock-host.ts +++ b/packages/connector-sdk/lib/test/mock-host.ts @@ -72,14 +72,33 @@ export const createMockedHost = async ({ const wasmPlugin = await createPlugin(wasmPath, { useWasi: true, - allowedHosts: plugin.settings.allowedDomains, logLevel: logLevel ?? 'info', // Config is delivered per invocation in the dispatch envelope, not as ambient Extism config. - fetch: proxy.fetch as typeof fetch, functions: { // Namespace must match the guest import (extism:host/user). 'extism:host/user': { // Guest passes the payload by memory offset; read it, record it, report success (0). + // Mirrors the core Host `fetch`: JSON {url, method, headers, body} in, JSON {status, body, error?} out. + // A denied or failed request comes back as `error` rather than throwing into the guest. + fetch: async (callContext, offset: bigint) => { + const request = callContext.read(offset)?.json() as + | { url: string; method?: string; headers?: Record; body?: string } + | undefined; + if (!request) return 0n; + try { + const { url, method, headers, body } = request; + const response = await proxy.fetch(url, { + ...(method && { method }), + ...(headers && { headers }), + ...(body && { body }), + }); + return callContext.store(JSON.stringify({ status: response.status, body: await response.text() })); + } catch (err) { + return callContext.store( + JSON.stringify({ status: 403, body: '', error: err instanceof Error ? err.message : String(err) }), + ); + } + }, sendEmail: (callContext, offset: bigint) => { const payload = callContext.read(offset)?.json() as EmailPayload | undefined; if (payload) sentEmails.push(payload);