UNPKG

inngest

Version:

Official SDK for Inngest.com. Inngest is the reliability layer for modern applications. Inngest combines durable execution, events, and queues into a zero-infra platform with built-in observability.

185 lines (183 loc) 5.66 kB
const require_consts = require('../helpers/consts.cjs'); const require_types = require('../types.cjs'); const require_strings = require('../helpers/strings.cjs'); const require_triggers = require('./triggers/triggers.cjs'); const require_engine = require('./execution/engine.cjs'); //#region src/components/InngestFunction.ts /** * A stateless Inngest function, wrapping up function configuration and any * in-memory steps to run when triggered. * * This function can be "registered" to create a handler that Inngest can * trigger remotely. * * @public */ var InngestFunction = class InngestFunction { static stepId = "step"; static failureSuffix = "-failure"; get [Symbol.toStringTag]() { return InngestFunction.Tag; } opts; fn; onFailureFn; client; /** * A stateless Inngest function, wrapping up function configuration and any * in-memory steps to run when triggered. * * This function can be "registered" to create a handler that Inngest can * trigger remotely. */ constructor(client, opts, fn) { this.client = client; this.opts = opts; this.fn = fn; this.onFailureFn = this.opts.onFailure; } /** * The generated or given ID for this function. */ id(prefix) { return [prefix, this.opts.id].filter(Boolean).join("-"); } /** * The generated or given ID for this function, prefixed with the app ID. This * is used for routing invokes and identifying the function across apps. */ get absoluteId() { return this.id(this.client.id); } /** * The name of this function as it will appear in the Inngest Cloud UI. */ get name() { return this.opts.name || this.id(); } /** * The description of this function. */ get description() { return this.opts.description; } /** * Retrieve the Inngest config for this function. */ getConfig({ baseUrl, appPrefix, isConnect }) { const fnId = this.id(appPrefix); const stepUrl = new URL(baseUrl.href); stepUrl.searchParams.set(require_consts.queryKeys.FnId, fnId); stepUrl.searchParams.set(require_consts.queryKeys.StepId, InngestFunction.stepId); const { retries: attempts, cancelOn, idempotency, batchEvents, rateLimit, throttle, concurrency, debounce, timeouts, priority, singleton } = this.opts; /** * Convert retries into the format required when defining function * configuration. */ const retries = typeof attempts === "undefined" ? void 0 : { attempts }; const triggers = []; for (const trigger of this.opts.triggers ?? []) { if (trigger.cron) { triggers.push({ cron: trigger.cron }); continue; } if (!trigger.event) continue; let eventName = trigger.event; if (eventName instanceof require_triggers.EventType) eventName = eventName.name; if (eventName === require_consts.internalEvents.FunctionInvoked) continue; triggers.push({ event: eventName, expression: trigger.if }); } const fn = { id: fnId, name: this.name, triggers, steps: { [InngestFunction.stepId]: { id: InngestFunction.stepId, name: InngestFunction.stepId, runtime: { type: isConnect ? "ws" : "http", url: stepUrl.href }, retries } }, idempotency, batchEvents, rateLimit, throttle, concurrency, debounce, priority, timeouts, singleton }; if (cancelOn) fn.cancel = cancelOn.map(({ event, timeout, if: ifStr, match }) => { let eventName; if (typeof event === "string") eventName = event; else eventName = event.name; const ret = { event: eventName }; if (timeout) ret.timeout = require_strings.timeStr(timeout); if (match) ret.if = `event.${match} == async.${match}`; else if (ifStr) ret.if = ifStr; return ret; }, []); const config = [fn]; if (this.onFailureFn) { const id = `${fn.id}${InngestFunction.failureSuffix}`; const name = `${fn.name ?? fn.id} (failure)`; const failureStepUrl = new URL(stepUrl.href); failureStepUrl.searchParams.set(require_consts.queryKeys.FnId, id); config.push({ id, name, triggers: [{ event: require_consts.internalEvents.FunctionFailed, expression: `event.data.function_id == '${fnId}'` }], steps: { [InngestFunction.stepId]: { id: InngestFunction.stepId, name: InngestFunction.stepId, runtime: { type: "http", url: failureStepUrl.href }, retries: { attempts: 1 } } } }); } return config; } createExecution(opts) { return require_engine.createExecutionEngine({ fn: this, ...opts.partialOptions }); } shouldOptimizeParallelism() { return this.opts.optimizeParallelism ?? this.client["options"].optimizeParallelism ?? true; } shouldAsyncCheckpoint(requestedRunStep, internalFnId, disableImmediateExecution) { if (requestedRunStep || !internalFnId || disableImmediateExecution) return; const userCfg = this.opts.checkpointing ?? this.client["options"].checkpointing ?? this.opts.experimentalCheckpointing ?? this.client["options"].experimentalCheckpointing ?? true; if (!userCfg) return; if (userCfg === true) return require_types.defaultCheckpointingOptions; return { bufferedSteps: userCfg.bufferedSteps ?? require_types.defaultCheckpointingOptions.bufferedSteps, maxRuntime: userCfg.maxRuntime ?? require_types.defaultCheckpointingOptions.maxRuntime, maxInterval: userCfg.maxInterval ?? require_types.defaultCheckpointingOptions.maxInterval }; } }; (function(_InngestFunction) { _InngestFunction.Tag = "Inngest.Function"; })(InngestFunction || (InngestFunction = {})); //#endregion Object.defineProperty(exports, 'InngestFunction', { enumerable: true, get: function () { return InngestFunction; } }); //# sourceMappingURL=InngestFunction.cjs.map