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.
116 lines (114 loc) • 3.72 kB
JavaScript
const require_types = require('../../types.cjs');
const require_types$1 = require('../../helpers/types.cjs');
//#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 === require_types.StepOpCode.AiGateway) {
if (opts?.type === "step.ai.infer") return "ai.infer";
if (opts?.type === "step.ai.wrap") return "ai.wrap";
} else if (op === require_types.StepOpCode.Gateway) return "fetch";
else if (op === require_types.StepOpCode.InvokeFunction) return "invoke";
else if (op === require_types.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 === require_types.StepOpCode.Sleep) return "sleep";
else if (op === require_types.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 (require_types$1.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
exports.UnreachableError = UnreachableError;
exports.buildWrapRequestChain = buildWrapRequestChain;
exports.buildWrapSendEventChain = buildWrapSendEventChain;
exports.isTimeStrInput = isTimeStrInput;
exports.optsFromStepInput = optsFromStepInput;
exports.stepInputFromOpts = stepInputFromOpts;
exports.stepTypeFromOpCode = stepTypeFromOpCode;
//# sourceMappingURL=utils.cjs.map