Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-dots-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@open-iga/aws': minor
---

Adjust fetch polyfill to use host fetch instead of default fetch from Extism
2 changes: 1 addition & 1 deletion connectors/aws/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/connector-sdk/extism-jspdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions packages/connector-sdk/lib/bun-plugin/codegen.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const staticTypeFile = `
declare module 'extism:host' {
interface user {
sendEmail(offset: I64): I64
fetch(offset: I64): I64
}
}
`;
Expand Down
41 changes: 28 additions & 13 deletions packages/connector-sdk/lib/polyfills/fetch.ts
Original file line number Diff line number Diff line change
@@ -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<string, string | number | boolean> },
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;
Expand Down Expand Up @@ -42,15 +43,29 @@ const toHeaderRecord = (headers: FetchInit['headers']): Record<string, string> =
};

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<FetchResponse> => {
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<HostFetchResponse>();
} finally {
responseMemory.free();
}
} finally {
request.free();
}
if (response.error) throw new Error(response.error);

const status = response.status;

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/connector-sdk/lib/runtime-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// oxlint-disable-next-line typescript/triple-slash-reference
/// <reference path="../../../node_modules/@extism/js-pdk/types/polyfills.d.ts" />
// 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
/// <reference path="../extism-jspdk.d.ts" />

// --- 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;
Expand Down
23 changes: 21 additions & 2 deletions packages/connector-sdk/lib/test/mock-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,33 @@ export const createMockedHost = async <const Config extends ConnectorConfig>({

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<string, string>; 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);
Expand Down
Loading