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.
110 lines (108 loc) • 3.41 kB
JavaScript
import { StepOpCode } from "../../types.js";
import { isRecord } from "../../helpers/types.js";
//#region src/components/middleware/utils.ts
function isTimeStrInput(value) {
return typeof value === "string" || typeof value === "number" || value instanceof Date;
}
/**
* Build an onion-style middleware chain for `wrapSendEvent`.
*
* Same pattern as `buildWrapRequestChain` but wraps the outgoing HTTP call
* in `client.send()` instead of the incoming execution request.
*/
function buildWrapSendEventChain(middleware, handler, payloads, fn) {
let chain = handler;
for (let i = middleware.length - 1; i >= 0; i--) {
const mw = middleware[i];
if (mw?.wrapSendEvent) {
const next = chain;
chain = () => mw.wrapSendEvent({
next,
events: payloads,
fn
});
}
}
return chain;
}
/**
* Build an onion-style middleware chain for `wrapRequest`.
*
* Iterates in reverse order (so first middleware is outermost)
* and returns a zero-arg function that kicks off the chain.
*/
function buildWrapRequestChain({ fn, handler, middleware, requestArgs, requestInfo, runId }) {
let chain = handler;
for (let i = middleware.length - 1; i >= 0; i--) {
const mw = middleware[i];
if (mw?.wrapRequest) {
const next = chain;
chain = () => mw.wrapRequest({
next,
requestArgs,
requestInfo,
runId,
fn
});
}
}
return chain;
}
/**
* Convert an opcode (from the op) to a step type.
*/
function stepTypeFromOpCode(op, opts, logger) {
if (op === StepOpCode.AiGateway) {
if (opts?.type === "step.ai.infer") return "ai.infer";
if (opts?.type === "step.ai.wrap") return "ai.wrap";
} else if (op === StepOpCode.Gateway) return "fetch";
else if (op === StepOpCode.InvokeFunction) return "invoke";
else if (op === StepOpCode.StepPlanned) {
if (opts?.type === void 0) return "run";
if (opts?.type === "step.sendEvent") return "sendEvent";
if (opts?.type === "step.realtime.publish") return "realtime.publish";
if (opts?.type === "group.experiment") return "group.experiment";
} else if (op === StepOpCode.Sleep) return "sleep";
else if (op === StepOpCode.WaitForEvent) return "waitForEvent";
logger.warn({
op,
type: opts?.type
}, "Unknown step type");
return "unknown";
}
/**
* Convert the opts object (from the op) to a step input array.
*
* Paired with `optsFromStepInput` which reverses this for step kinds that
* wrap the entire opts as `[opts]`.
*/
function stepInputFromOpts(stepType, opts) {
if (stepType === "invoke" || stepType === "waitForEvent") return [opts];
if (Array.isArray(opts?.input)) return opts.input;
}
/**
* Reverse of `stepInputFromOpts`: given middleware-transformed input, derive
* the opts to use in the outgoing op.
*
* Returns undefined when the step kind doesn't derive opts from input.
*/
function optsFromStepInput(stepType, input) {
if (input === void 0) return;
if (stepType === "invoke" || stepType === "waitForEvent") {
const opts = input[0];
if (isRecord(opts)) return opts;
}
}
/**
* An error that is thrown when a code path is unreachable. Should never be
* thrown at runtime.
*/
var UnreachableError = class extends Error {
constructor(...args) {
super(...args);
this.name = this.constructor.name;
}
};
//#endregion
export { UnreachableError, buildWrapRequestChain, buildWrapSendEventChain, isTimeStrInput, optsFromStepInput, stepInputFromOpts, stepTypeFromOpCode };
//# sourceMappingURL=utils.js.map