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.

1 lines 6.45 kB
{"version":3,"file":"index.cjs","names":["BaseStrategy","ConnectionCore","ConnectionState","SDKResponse","MessageBuffer"],"sources":["../../../../../src/components/connect/strategies/sameThread/index.ts"],"sourcesContent":["import { SDKResponse } from \"../../../../proto/src/components/connect/protobuf/connect.ts\";\nimport { MessageBuffer } from \"../../buffer.ts\";\nimport { type ConnectDebugState, ConnectionState } from \"../../types.ts\";\nimport { BaseStrategy } from \"../core/BaseStrategy.ts\";\nimport { ConnectionCore } from \"../core/connection.ts\";\nimport type { StrategyConfig } from \"../core/types.ts\";\n\nconst ResponseAcknowledgeDeadline = 5_000;\n\n/**\n * Same-thread connection strategy.\n *\n * This strategy runs the WebSocket connection, heartbeater, and lease extender\n * in the same thread as user code execution. This is the default strategy.\n */\nexport class SameThreadStrategy extends BaseStrategy {\n private readonly config: StrategyConfig;\n private readonly messageBuffer: MessageBuffer;\n private readonly core: ConnectionCore;\n\n constructor(config: StrategyConfig) {\n super({ logger: config.internalLogger });\n this.config = config;\n\n // Create the connection core with callbacks\n this.core = new ConnectionCore(\n {\n apiBaseUrl: config.apiBaseUrl,\n appIds: Object.keys(config.requestHandlers),\n connectionData: config.connectionData,\n envName: config.envName,\n hashedFallbackKey: config.hashedFallbackKey,\n hashedSigningKey: config.hashedSigningKey,\n instanceId: config.options.instanceId,\n maxWorkerConcurrency: config.options.maxWorkerConcurrency,\n mode: config.mode,\n gatewayUrl: config.options.gatewayUrl,\n },\n {\n logger: this.internalLogger,\n onStateChange: (state) => {\n // Don't allow state to regress from CLOSING/CLOSED (e.g. if a\n // drain reconnect triggers ACTIVE during graceful shutdown).\n if (\n this._state === ConnectionState.CLOSING ||\n this._state === ConnectionState.CLOSED\n ) {\n return;\n }\n this._state = state;\n },\n getState: () => this._state,\n handleExecutionRequest: async (request) => {\n const handler = this.config.requestHandlers[request.appName];\n if (!handler) {\n throw new Error(`No handler for app: ${request.appName}`);\n }\n const response = await handler(request);\n const responseBytes = SDKResponse.encode(response).finish();\n\n // Add to pending with deadline\n this.messageBuffer.addPending(\n request.requestId,\n responseBytes,\n ResponseAcknowledgeDeadline,\n );\n\n return responseBytes;\n },\n onReplyAck: (requestId) => {\n this.messageBuffer.acknowledgePending(requestId);\n },\n onBufferResponse: (requestId, responseBytes) => {\n this.messageBuffer.append(requestId, responseBytes);\n },\n onConnectionActive: (signingKey) => {\n this.messageBuffer.flush(signingKey);\n },\n },\n );\n\n this.messageBuffer = new MessageBuffer({\n envName: config.envName,\n getApiBaseUrl: () => this.core.getApiBaseUrl(),\n logger: this.internalLogger,\n });\n }\n\n get connectionId(): string | undefined {\n return this.core.connectionId;\n }\n\n getDebugState(): ConnectDebugState {\n return this.core.getDebugState();\n }\n\n async connect(attempt = 0): Promise<void> {\n this.throwIfClosingOrClosed();\n this.setupShutdownSignalIfConfigured(\n this.config.options.handleShutdownSignals,\n );\n\n // Flush any pending messages before connecting\n try {\n await this.messageBuffer.flush(this.config.hashedSigningKey);\n } catch (err) {\n this.internalLogger.debug(\n { err },\n \"Failed to flush messages, using fallback key\",\n );\n await this.messageBuffer.flush(this.config.hashedFallbackKey);\n }\n\n await this.core.start(attempt);\n }\n\n async close(): Promise<void> {\n this.cleanupShutdown();\n this.setClosing();\n\n await this.core.close();\n\n this.internalLogger.debug(\"Flushing messages before closing\");\n\n try {\n await this.messageBuffer.flush(this.config.hashedSigningKey);\n } catch (err) {\n this.internalLogger.debug(\n { err },\n \"Failed to flush messages, using fallback key\",\n );\n await this.messageBuffer.flush(this.config.hashedFallbackKey);\n }\n\n this.setClosed();\n this.internalLogger.debug(\"Fully closed\");\n }\n}\n"],"mappings":";;;;;;;AAOA,MAAM,8BAA8B;;;;;;;AAQpC,IAAa,qBAAb,cAAwCA,kCAAa;CACnD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,QAAwB;AAClC,QAAM,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