UNPKG

@resonatehq/aws

Version:

Resonate FaaS handler for AWS Lambda Functions (TypeScript)

129 lines (128 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Resonate = void 0; const sdk_1 = require("@resonatehq/sdk"); class Resonate { constructor({ verbose = false } = {}) { this.registry = new sdk_1.Registry(); this.verbose = verbose; } register(nameOrFunc, funcOrOptions, maybeOptions = {}) { const { version = 1 } = (typeof funcOrOptions === "object" ? funcOrOptions : maybeOptions) ?? {}; const func = typeof nameOrFunc === "function" ? nameOrFunc : funcOrOptions; const name = typeof nameOrFunc === "string" ? nameOrFunc : func.name; this.registry.add(func, name, version); } httpHandler() { return async (event, _context, _callback) => { try { if (event.requestContext.http.method !== "POST") { return { statusCode: 405, body: JSON.stringify({ error: "Method not allowed. Use POST." }), }; } // Ensure required headers const proto = event.headers["x-forwarded-proto"]; const host = event.headers.host; if (!proto || !host) { return { statusCode: 400, body: JSON.stringify({ error: "Missing required headers: x-forwarded-proto or host.", }), }; } // Construct full invocation URL const url = `${proto}://${host}${event.requestContext.http.path ?? ""}`; // Ensure body exists if (!event.body) { return { statusCode: 400, body: JSON.stringify({ error: "Request body missing." }), }; } // Parse JSON body const body = JSON.parse(event.body); // Validate task structure if (!body || !(body.type === "invoke" || body.type === "resume") || !body.task) { return { statusCode: 400, body: JSON.stringify({ error: 'Request body must contain "type" and "task" for Resonate invocation.', }), }; } const encoder = new sdk_1.JsonEncoder(); const network = new sdk_1.HttpNetwork({ headers: {}, timeout: 60 * 1000, url: body.href.base, verbose: this.verbose, }); const resonateInner = new sdk_1.ResonateInner({ anycastNoPreference: url, anycastPreference: url, clock: new sdk_1.WallClock(), dependencies: new Map(), handler: new sdk_1.Handler(network, encoder), heartbeat: new sdk_1.NoopHeartbeat(), network, pid: `pid-${Math.random().toString(36).substring(7)}`, registry: this.registry, ttl: 30 * 1000, unicast: url, verbose: this.verbose, }); // Create unclaimed task const task = { kind: "unclaimed", task: body.task }; // Process the task and await result const result = new Promise((resolve) => { resonateInner.process(task, (error, status) => { if (error || !status) { resolve({ statusCode: 500, body: JSON.stringify({ error: "Task processing failed", details: { error, status }, }), }); return; } if (status.kind === "completed") { resolve({ statusCode: 200, body: JSON.stringify({ status: "completed", result: status.promise.value, requestUrl: url, }), }); } else { resolve({ statusCode: 200, body: JSON.stringify({ status: "suspended", requestUrl: url, }), }); } }); }); return result; } catch (error) { return { statusCode: 500, body: JSON.stringify({ error: `Handler failed: ${error}`, }), }; } }; } } exports.Resonate = Resonate;