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.23 kB
{"version":3,"file":"index.cjs","names":["BaseStrategy","ConnectionCore","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 { 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 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 beforeConnect: async (signingKey) => {\n await 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 async close(): Promise<void> {\n this.cleanupShutdown();\n this.setClosing();\n this.internalLogger.debug(\"Cleaning up connection resources\");\n\n await this.core.cleanup();\n\n this.internalLogger.debug(\"Connection closed\");\n this.internalLogger.debug(\"Waiting for in-flight requests to complete\");\n\n await this.core.waitForInProgress();\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 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.connect(attempt);\n }\n}\n"],"mappings":";;;;;;AAMA,MAAM,8BAA8B;;;;;;;AAQpC,IAAa,qBAAb,cAAwCA,kCAAa;CACnD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,QAAwB;AAClC,QAAM,EAAE,QAAQ,OAAO,gBAAgB,CAAC;AACxC,OAAK,SAAS;AAGd,OAAK,OAAO,IAAIC,kCACd;GACE,YAAY,OAAO;GAC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