Skip to content

Commit d21237f

Browse files
authored
Merge pull request #3 from Paca-AI/feature/implement-fetch-function-for-http-request
feat: implement Fetch function and FetchResponse type for HTTP requests
2 parents 7211c0c + 30ccea6 commit d21237f

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

native_backends.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ func EmitEvent(_ string, _ any) {}
4848
// RecordActivity is a no-op outside WASM.
4949
func RecordActivity(_, _, _, _ string, _ any) {}
5050

51+
// FetchResponse is also defined in wasm_backends.go (wasip1); provide the
52+
// type here so non-WASM consumers can reference it.
53+
type FetchResponse struct {
54+
Status int `json:"status"`
55+
Body string `json:"body"`
56+
Headers map[string]string `json:"headers"`
57+
Error string `json:"error"`
58+
}
59+
60+
// Fetch always returns errNotWASM outside a WASM module.
61+
func Fetch(_, _ string, _ map[string]string, _ string) (*FetchResponse, error) {
62+
return nil, errNotWASM
63+
}
64+
5165
// ptrOf and hostError are used by wasm_backends.go (wasip1 only); provide
5266
// stubs here so the non-WASM build does not need them.
5367
//

wasm_backends.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package plugin
44

55
import (
66
"encoding/json"
7+
"fmt"
78
"unsafe"
89
)
910

@@ -206,6 +207,56 @@ func RecordActivity(taskID, projectID, actorUserID, activityType string, content
206207
hostActivityRecord(int64(ptrOf(payloadBytes)), int64(len(payloadBytes)))
207208
}
208209

210+
// ── Fetch ─────────────────────────────────────────────────────────────────────
211+
212+
// FetchResponse is the result of a Fetch call.
213+
type FetchResponse struct {
214+
Status int `json:"status"`
215+
Body string `json:"body"`
216+
Headers map[string]string `json:"headers"`
217+
Error string `json:"error"`
218+
}
219+
220+
// Fetch makes an outbound HTTP request via the paca.fetch host function.
221+
// The URL's domain must be listed in the plugin manifest's allowedOutboundDomains.
222+
func Fetch(method, rawURL string, headers map[string]string, body string) (*FetchResponse, error) {
223+
req := struct {
224+
Method string `json:"method"`
225+
URL string `json:"url"`
226+
Headers map[string]string `json:"headers"`
227+
Body string `json:"body"`
228+
}{
229+
Method: method,
230+
URL: rawURL,
231+
Headers: headers,
232+
Body: body,
233+
}
234+
reqJSON, err := json.Marshal(req)
235+
if err != nil {
236+
return nil, err
237+
}
238+
outputBuf := make([]byte, 8)
239+
hostFetch(
240+
int64(ptrOf(reqJSON)), int64(len(reqJSON)),
241+
int64(ptrOf(outputBuf)), int64(ptrOf(outputBuf[4:])),
242+
)
243+
resPtr := int32(uint32(outputBuf[0]) | uint32(outputBuf[1])<<8 | uint32(outputBuf[2])<<16 | uint32(outputBuf[3])<<24)
244+
resLen := int32(uint32(outputBuf[4]) | uint32(outputBuf[5])<<8 | uint32(outputBuf[6])<<16 | uint32(outputBuf[7])<<24)
245+
if resLen == 0 {
246+
return nil, fmt.Errorf("plugin: fetch: empty response from host")
247+
}
248+
resBytes := append([]byte(nil), wasmSlice(resPtr, resLen)...)
249+
wasmResetAllocator()
250+
var resp FetchResponse
251+
if err := json.Unmarshal(resBytes, &resp); err != nil {
252+
return nil, fmt.Errorf("plugin: fetch: decode response: %w", err)
253+
}
254+
if resp.Error != "" {
255+
return nil, fmt.Errorf("plugin: fetch: %s", resp.Error)
256+
}
257+
return &resp, nil
258+
}
259+
209260
// ── Helpers ───────────────────────────────────────────────────────────────────
210261

211262
//go:nocheckptr

wasm_imports.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ func hostStorageDelete(keyPtr, keyLen int64) int32
4949
//go:noescape
5050
func hostEventEmit(topicPtr, topicLen, payloadPtr, payloadLen int64) int32
5151

52+
// paca.fetch(reqPtr i64, reqLen i64, resPtrPtr i64, resLenPtr i64)
53+
//
54+
//go:wasmimport paca fetch
55+
//go:noescape
56+
func hostFetch(reqPtr, reqLen, resPtrPtr, resLenPtr int64)
57+
5258
// paca.activity_record(payloadPtr i64, payloadLen i64) -> ok i32
5359
//
5460
//go:wasmimport paca activity_record

0 commit comments

Comments
 (0)