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.
108 lines (106 loc) • 3.86 kB
JavaScript
import { envKeys } from "../../helpers/consts.js";
import { getProcessEnv } from "../../helpers/env.js";
import { internalLoggerSymbol } from "../Inngest.js";
import { prepareConnectionConfig } from "./config.js";
import { ConnectionState, DEFAULT_SHUTDOWN_SIGNALS } from "./types.js";
import { createStrategy } from "./strategies/index.js";
//#region src/components/connect/index.ts
/**
* WebSocket worker connection that implements the WorkerConnection interface.
*
* This class acts as a facade that delegates to a connection strategy.
* The strategy determines how the WebSocket connection, heartbeater, and
* lease extender are managed (same thread vs worker thread).
*/
var WebSocketWorkerConnection = class {
inngest;
options;
strategy;
constructor(options) {
if (!Array.isArray(options.apps) || options.apps.length === 0 || !options.apps[0]) throw new Error("No apps provided");
this.inngest = options.apps[0].client;
for (const app of options.apps) {
const client = app.client;
if (client.env !== this.inngest.env) throw new Error(`All apps must be configured to the same environment. ${client.id} is configured to ${client.env} but ${this.inngest.id} is configured to ${this.inngest.env}`);
}
this.options = this.applyDefaults(options);
}
applyDefaults(opts) {
const options = { ...opts };
if (!Array.isArray(options.handleShutdownSignals)) options.handleShutdownSignals = DEFAULT_SHUTDOWN_SIGNALS;
const env = getProcessEnv();
if (options.maxWorkerConcurrency === void 0) {
const envValue = env[envKeys.InngestConnectMaxWorkerConcurrency];
if (envValue) {
const parsed = Number.parseInt(envValue, 10);
if (!Number.isNaN(parsed) && parsed > 0) options.maxWorkerConcurrency = parsed;
}
}
if (options.isolateExecution === void 0) {
const envValue = env[envKeys.InngestConnectIsolateExecution];
if (envValue === "0" || envValue === "false") options.isolateExecution = false;
}
if (options.gatewayUrl === void 0) {
const envValue = env[envKeys.InngestConnectGatewayUrl];
if (envValue) options.gatewayUrl = envValue;
}
return options;
}
get state() {
return this.strategy?.state ?? ConnectionState.CONNECTING;
}
get connectionId() {
if (!this.strategy?.connectionId) throw new Error("Connection not prepared");
return this.strategy.connectionId;
}
get closed() {
if (!this.strategy) throw new Error("No connection established");
return this.strategy.closed;
}
getDebugState() {
if (!this.strategy) return {
state: ConnectionState.CONNECTING,
activeConnectionId: void 0,
drainingConnectionId: void 0,
lastHeartbeatSentAt: void 0,
lastHeartbeatReceivedAt: void 0,
lastMessageReceivedAt: void 0,
shutdownRequested: false,
inFlightRequestCount: 0,
inFlightRequests: []
};
return this.strategy.getDebugState();
}
async close() {
if (!this.strategy) return;
return this.strategy.close();
}
/**
* Establish a persistent connection to the gateway.
*/
async connect(attempt = 0) {
this.inngest[internalLoggerSymbol].debug({ attempt }, "Establishing connection");
const { hashedSigningKey, hashedFallbackKey, envName, connectionData, requestHandlers } = prepareConnectionConfig(this.options.apps, this.inngest);
this.strategy = await createStrategy({
hashedSigningKey,
hashedFallbackKey,
internalLogger: this.inngest[internalLoggerSymbol],
envName,
connectionData,
requestHandlers,
options: this.options,
apiBaseUrl: this.inngest.apiBaseUrl,
mode: this.inngest["mode"]
}, this.options);
await this.strategy.connect(attempt);
}
};
const connect = async (options) => {
if (options.apps.length === 0) throw new Error("No apps provided");
const conn = new WebSocketWorkerConnection(options);
await conn.connect();
return conn;
};
//#endregion
export { connect };
//# sourceMappingURL=index.js.map