@restatedev/restate-sdk 1.11.1 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/_virtual/rolldown_runtime.cjs +9 -0
  2. package/dist/common_api.cjs +2 -1
  3. package/dist/common_api.d.cts +6 -2
  4. package/dist/common_api.d.cts.map +1 -1
  5. package/dist/common_api.d.ts +6 -2
  6. package/dist/common_api.d.ts.map +1 -1
  7. package/dist/common_api.js +2 -1
  8. package/dist/common_api.js.map +1 -1
  9. package/dist/context.cjs +13 -9
  10. package/dist/context.d.cts +36 -29
  11. package/dist/context.d.cts.map +1 -1
  12. package/dist/context.d.ts +36 -29
  13. package/dist/context.d.ts.map +1 -1
  14. package/dist/context.js +13 -9
  15. package/dist/context.js.map +1 -1
  16. package/dist/context_impl.cjs +150 -91
  17. package/dist/context_impl.d.cts +8 -0
  18. package/dist/context_impl.d.ts +8 -72
  19. package/dist/context_impl.d.ts.map +1 -1
  20. package/dist/context_impl.js +151 -93
  21. package/dist/context_impl.js.map +1 -1
  22. package/dist/endpoint/components.cjs +35 -20
  23. package/dist/endpoint/components.d.cts +5 -0
  24. package/dist/endpoint/components.d.ts +5 -97
  25. package/dist/endpoint/components.d.ts.map +1 -1
  26. package/dist/endpoint/components.js +35 -20
  27. package/dist/endpoint/components.js.map +1 -1
  28. package/dist/endpoint/endpoint.cjs +2 -2
  29. package/dist/endpoint/endpoint.d.cts +5 -0
  30. package/dist/endpoint/endpoint.d.ts +5 -39
  31. package/dist/endpoint/endpoint.js +2 -2
  32. package/dist/endpoint/endpoint.js.map +1 -1
  33. package/dist/endpoint/handlers/generic.cjs +115 -39
  34. package/dist/endpoint/handlers/generic.d.ts.map +1 -1
  35. package/dist/endpoint/handlers/generic.js +115 -39
  36. package/dist/endpoint/handlers/generic.js.map +1 -1
  37. package/dist/endpoint/handlers/types.d.cts +1 -0
  38. package/dist/endpoint/handlers/types.d.ts +1 -41
  39. package/dist/endpoint/handlers/utils.cjs +1 -1
  40. package/dist/endpoint/handlers/utils.js +1 -1
  41. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.cjs +43 -3
  42. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +19 -1
  43. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts.map +1 -1
  44. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js +43 -3
  45. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.js.map +1 -1
  46. package/dist/endpoint/node_endpoint.cjs +28 -12
  47. package/dist/endpoint/node_endpoint.d.ts +14 -2
  48. package/dist/endpoint/node_endpoint.d.ts.map +1 -1
  49. package/dist/endpoint/node_endpoint.js +27 -11
  50. package/dist/endpoint/node_endpoint.js.map +1 -1
  51. package/dist/endpoint/types.d.cts +1 -1
  52. package/dist/endpoint/types.d.ts +1 -1
  53. package/dist/endpoint.d.cts +87 -5
  54. package/dist/endpoint.d.cts.map +1 -1
  55. package/dist/endpoint.d.ts +87 -5
  56. package/dist/endpoint.d.ts.map +1 -1
  57. package/dist/error_sanitization.cjs +26 -0
  58. package/dist/error_sanitization.d.ts +13 -0
  59. package/dist/error_sanitization.d.ts.map +1 -0
  60. package/dist/error_sanitization.js +26 -0
  61. package/dist/error_sanitization.js.map +1 -0
  62. package/dist/fetch.cjs +3 -1
  63. package/dist/fetch.d.cts +5 -3
  64. package/dist/fetch.d.cts.map +1 -1
  65. package/dist/fetch.d.ts +5 -3
  66. package/dist/fetch.d.ts.map +1 -1
  67. package/dist/fetch.js +3 -2
  68. package/dist/fetch.js.map +1 -1
  69. package/dist/hooks.d.cts +87 -0
  70. package/dist/hooks.d.cts.map +1 -0
  71. package/dist/hooks.d.ts +87 -0
  72. package/dist/hooks.d.ts.map +1 -0
  73. package/dist/hooks.js +2 -0
  74. package/dist/hooks.js.map +1 -0
  75. package/dist/index.cjs +3 -1
  76. package/dist/index.d.cts +6 -4
  77. package/dist/index.d.ts +6 -4
  78. package/dist/index.js +3 -2
  79. package/dist/internal.cjs +3 -1
  80. package/dist/internal.d.cts +186 -2
  81. package/dist/internal.d.cts.map +1 -1
  82. package/dist/internal.d.ts +186 -2
  83. package/dist/internal.d.ts.map +1 -1
  84. package/dist/internal.js +4 -1
  85. package/dist/internal.js.map +1 -1
  86. package/dist/io.d.cts +1 -0
  87. package/dist/io.d.ts +1 -24
  88. package/dist/lambda.cjs +3 -1
  89. package/dist/lambda.d.cts +5 -3
  90. package/dist/lambda.d.cts.map +1 -1
  91. package/dist/lambda.d.ts +5 -3
  92. package/dist/lambda.d.ts.map +1 -1
  93. package/dist/lambda.js +3 -2
  94. package/dist/lambda.js.map +1 -1
  95. package/dist/logging/logger.d.cts +1 -0
  96. package/dist/logging/logger.d.ts +1 -10
  97. package/dist/node.cjs +23 -6
  98. package/dist/node.d.cts +46 -8
  99. package/dist/node.d.cts.map +1 -1
  100. package/dist/node.d.ts +46 -8
  101. package/dist/node.d.ts.map +1 -1
  102. package/dist/node.js +23 -7
  103. package/dist/node.js.map +1 -1
  104. package/dist/package.cjs +1 -1
  105. package/dist/package.js +1 -1
  106. package/dist/package.js.map +1 -1
  107. package/dist/promises.cjs +90 -53
  108. package/dist/promises.d.cts +18 -0
  109. package/dist/promises.d.cts.map +1 -0
  110. package/dist/promises.d.ts +15 -108
  111. package/dist/promises.d.ts.map +1 -1
  112. package/dist/promises.js +85 -48
  113. package/dist/promises.js.map +1 -1
  114. package/dist/types/errors.cjs +13 -0
  115. package/dist/types/errors.d.cts +11 -5
  116. package/dist/types/errors.d.cts.map +1 -1
  117. package/dist/types/errors.d.ts +11 -5
  118. package/dist/types/errors.d.ts.map +1 -1
  119. package/dist/types/errors.js +13 -1
  120. package/dist/types/errors.js.map +1 -1
  121. package/dist/types/rpc.cjs +7 -19
  122. package/dist/types/rpc.d.cts +174 -0
  123. package/dist/types/rpc.d.cts.map +1 -1
  124. package/dist/types/rpc.d.ts +174 -0
  125. package/dist/types/rpc.d.ts.map +1 -1
  126. package/dist/types/rpc.js +7 -19
  127. package/dist/types/rpc.js.map +1 -1
  128. package/package.json +2 -2
@@ -1 +1 @@
1
- {"version":3,"file":"context.js","names":[],"sources":["../src/context.ts"],"sourcesContent":["/*\n * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH\n *\n * This file is part of the Restate SDK for Node.js/TypeScript,\n * which is released under the MIT license.\n *\n * You can find a copy of the license in file LICENSE in the root\n * directory of this repository or package, or at\n * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE\n */\n\nimport type { Client, SendClient } from \"./types/rpc.js\";\nimport type {\n RestateContext,\n RestateObjectContext,\n RestateObjectSharedContext,\n RestateWorkflowContext,\n RestateWorkflowSharedContext,\n Service,\n ServiceDefinitionFrom,\n VirtualObject,\n VirtualObjectDefinitionFrom,\n Workflow,\n WorkflowDefinitionFrom,\n Serde,\n Duration,\n} from \"@restatedev/restate-sdk-core\";\nimport { ContextImpl } from \"./context_impl.js\";\nimport type { TerminalError } from \"./types/errors.js\";\n\n/**\n * Represents the original request as sent to this handler.\n *\n * A request object includes the request headers, and the raw unparsed\n * request body.\n */\nexport interface Request {\n /**\n * The unique id that identifies the current function invocation. This id is guaranteed to be\n * unique across invocations, but constant across reties and suspensions.\n */\n readonly id: string;\n\n /**\n * Request headers - the following headers capture the original invocation headers, as provided to\n * the ingress.\n */\n readonly headers: ReadonlyMap<string, string>;\n\n /**\n * Attempt headers - the following headers are sent by the restate runtime.\n * These headers are attempt specific, generated by the restate runtime uniquely for each attempt.\n * These headers might contain information such as the W3C trace context, and attempt specific information.\n */\n readonly attemptHeaders: ReadonlyMap<string, string | string[] | undefined>;\n\n /**\n * Raw unparsed request body\n */\n readonly body: Uint8Array;\n\n /**\n * Extra arguments provided to the request handler:\n * Lambda: [Context]\n * Cloudflare workers: [Env, ExecutionContext]\n * Deno: [ConnInfo]\n * Bun: [Server]\n * These arguments can contain request-specific values that could change after a suspension.\n * Care should be taken to use them deterministically.\n */\n readonly extraArgs: unknown[];\n\n /**\n * This is a signal that is aborted when the current attempt has been completed either successful or unsuccessfully.\n * When the signal is aborted, the current attempt has been completed and the handler should not perform any more work, other\n * than cleanup any external resources that might be shared across attempts (e.g. database connections).\n */\n readonly attemptCompletedSignal: AbortSignal;\n}\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport type TypedState = Record<string, any>;\nexport type UntypedState = { _: never };\n\n/**\n * Key value store operations. Only keyed services have an attached key-value store.\n */\nexport interface KeyValueStore<TState extends TypedState> {\n /**\n * Get/retrieve state from the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to retrieve\n * @returns a Promise that is resolved with the value of the state key\n *\n * @example\n * const state = await ctx.get<string>(\"STATE\");\n */\n get<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): Promise<(TState extends UntypedState ? TValue : TState[TKey]) | null>;\n\n stateKeys(): Promise<Array<string>>;\n\n /**\n * Set/store state in the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to set\n * @param value value to set\n *\n * @example\n * ctx.set(\"STATE\", \"Hello\");\n */\n set<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n value: TState extends UntypedState ? TValue : TState[TKey],\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): void;\n\n /**\n * Clear/delete state in the Restate runtime.\n * @param name key of the state to delete\n *\n * @example\n * ctx.clear(\"STATE\");\n */\n clear<TKey extends keyof TState>(\n name: TState extends UntypedState ? string : TKey\n ): void;\n\n /**\n * Clear/delete all the state entries in the Restate runtime.\n *\n * @example\n * ctx.clearAll();\n */\n clearAll(): void;\n}\n\n/**\n * @deprecated SendOptions on the client factory are deprecated, please use `restate.rpc.sendOpts` instead\n */\nexport interface SendOptions {\n /**\n * @deprecated SendOptions on the client factory are deprecated, please use `restate.rpc.sendOpts` instead\n */\n delay?: number;\n}\n\nexport interface ContextDate {\n /** Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).\n * This is equivalent to Date.now()\n */\n now(): Promise<number>;\n /** Returns the JSON representation of the current date.\n * This is equivalent to new Date().toJSON()\n **/\n toJSON(): Promise<string>;\n}\n\n/**\n * A function that can be run and its result durably persisted by Restate.\n */\nexport type RunAction<T> = (() => Promise<T>) | (() => T);\n\nexport type RunOptions<T> = {\n serde?: Serde<T>;\n\n /**\n * Max number of attempts (including the initial), before giving up.\n *\n * When giving up, `ctx.run` will throw a `TerminalError` wrapping the original error message.\n */\n maxRetryAttempts?: number;\n\n /**\n * @deprecated Use `maxRetryDuration` instead.\n */\n maxRetryDurationMillis?: number;\n\n /**\n * Max duration of retries, before giving up.\n *\n * When giving up, `ctx.run` will throw a `TerminalError` wrapping the original error message.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n maxRetryDuration?: Duration | number;\n\n /**\n * @deprecated Use `initialRetryInterval` instead.\n */\n initialRetryIntervalMillis?: number;\n\n /**\n * Initial interval for the first retry attempt.\n * Retry interval will grow by a factor specified in `retryIntervalFactor`.\n *\n * The default is 50 milliseconds.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n initialRetryInterval?: Duration | number;\n\n /**\n * @deprecated Use `maxRetryInterval` instead.\n */\n maxRetryIntervalMillis?: number;\n\n /**\n * Max interval between retries.\n * Retry interval will grow by a factor specified in `retryIntervalFactor`.\n *\n * The default is 10 seconds.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n maxRetryInterval?: Duration | number;\n\n /**\n * Exponentiation factor to use when computing the next retry delay.\n *\n * The default value is `2`, meaning retry interval will double at each attempt.\n */\n retryIntervalFactor?: number;\n};\n\n/**\n * Call a handler directly avoiding restate's type safety checks.\n * This is a generic mechanism to invoke handlers directly by only knowing\n * the service and handler name, (or key in the case of objects or workflows)\n */\nexport type GenericCall<REQ, RES> = {\n service: string;\n method: string;\n parameter: REQ;\n key?: string;\n headers?: Record<string, string>;\n inputSerde?: Serde<REQ>;\n outputSerde?: Serde<RES>;\n idempotencyKey?: string;\n /**\n * Observability name, recorded in the Restate journal.\n */\n name?: string;\n};\n\n/**\n * Send a message to an handler directly avoiding restate's type safety checks.\n * This is a generic mechanism to invoke handlers directly by only knowing\n * the service and handler name, (or key in the case of objects or workflows)\n */\nexport type GenericSend<REQ> = {\n service: string;\n method: string;\n parameter: REQ;\n key?: string;\n headers?: Record<string, string>;\n inputSerde?: Serde<REQ>;\n delay?: Duration | number;\n idempotencyKey?: string;\n /**\n * Observability name, recorded in the Restate journal.\n */\n name?: string;\n};\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * Virtual objects can also access their key-value store using the {@link ObjectContext}.\n *\n */\nexport interface Context extends RestateContext {\n /**\n * Deterministic random methods; these are inherently predictable (seeded on the invocation ID, which is not secret)\n * and so should not be used for any cryptographic purposes. They are useful for identifiers, idempotency keys,\n * and for uniform sampling from a set of options. If a cryptographically secure value is needed, please generate that\n * externally and capture the result with a side effect.\n *\n * Calls to these methods from inside `ctx.run` are disallowed and will fail - side effects must be idempotent, and\n * these calls are not.\n */\n rand: Rand;\n\n /**\n * Console to use for logging. It attaches to each log message some contextual information,\n * such as invoked service method and invocation id, and automatically excludes logs during replay.\n */\n console: Console;\n\n /**\n * Deterministic date.\n */\n date: ContextDate;\n\n /**\n * Run an operation and store the result in Restate. The operation will thus not\n * be re-run during a later replay, but take the durable result from Restate.\n *\n * This let you capture potentially non-deterministic computation and interaction\n * with external systems in a safe way.\n *\n * Failure semantics are:\n * - If an operation has run and persisted before, the result (value or Error) will be\n * taken from the Restate journal.\n * - There is a small window where an action may be re-run, if a failure\n * occurred between a successful run and persisting the result.\n * - No second action will be run while a previous run's result is not\n * yet durable. That way, effects that build on top of each other can assume\n * deterministic results from previous runs, and at most one run will be\n * re-executed on replay (the latest, if the failure happened in the small windows\n * described above).\n *\n * You can customize retry options by either:\n *\n * - Providing retry policy options in {@link RunOptions}\n * - Throwing {@link RetryableError}, providing `retryAfter` option. This can be especially useful when interacting with HTTP requests returning the `Retry-After` header. You can combine the usage of throwing {@link RetryableError} with the `maxRetryAttempts`/`maxRetryDuration` from {@link RunOptions}.\n *\n * @example Run some external action and persist its result\n * ```ts\n * const result = await ctx.run(someExternalAction)\n *```\n * @example Add some retry options\n * ```ts\n * const result = await ctx.run(\"my action\", someExternalAction, { maxRetryAttempts: 10 })\n * ```\n * @example Terminal errors and retryable errors\n * ```ts\n * await ctx.run(\"payment action\", async () => {\n * const result = await paymentProvider.charge(txId, paymentInfo);\n * if (result.paymentRejected) {\n * // this action will not be retried anymore\n * throw new TerminalError(\"Payment failed\");\n * } else if (result.paymentGatewayBusy) {\n * // restate will retry automatically\n * // to bound retries, use RunOptions\n * throw new Error(\"Payment gateway busy\");\n * } else {\n * // success!\n * }\n * });\n * ```\n * @example Retryable error with custom retry delay\n * ```ts\n * await ctx.run(\"payment action\", async () => {\n * const res = fetch(...);\n * if (!res.ok) {\n * // Read Retry-After header\n * const retryAfterHeader = res.headers['Retry-After']\n *\n * // Use RetryableError to customize in how long to retry\n * throw RetryableError.from(cause, { retryAfter: { seconds: retryAfterHeader } })\n * }\n * }, {\n * // Retry at most ten times\n * maxRetryAttempts: 10\n * });\n * ```\n *\n * @param action The function to run.\n */\n run<T>(action: RunAction<T>): RestatePromise<T>;\n\n /**\n * Same as {@link run}, but providing a name, used for observability purposes.\n */\n run<T>(name: string, action: RunAction<T>): RestatePromise<T>;\n\n /**\n * See {@link run}\n */\n run<T>(\n name: string,\n action: RunAction<T>,\n options: RunOptions<T>\n ): RestatePromise<T>;\n\n /**\n * Register an awakeable and pause the processing until the awakeable ID (and optional payload) have been returned to the service\n * (via ctx.completeAwakeable(...)). The SDK deserializes the payload with `JSON.parse(result.toString()) as T`.\n * @returns\n * - id: the string ID that has to be used to complete the awakaeble by some external service\n * - promise: the Promise that needs to be awaited and that is resolved with the payload that was supplied by the service which completed the awakeable\n * @example Retryable errors and terminal errors\n * const awakeable = ctx.awakeable<string>();\n *\n * // send the awakeable ID to some external service that will wake this one back up\n * // The ID can be retrieved by:\n * const id = awakeable.id;\n *\n * // ... send to external service ...\n *\n * // Wait for the external service to wake this service back up\n * const result = await awakeable.promise;\n */\n awakeable<T>(serde?: Serde<T>): {\n id: string;\n promise: RestatePromise<T>;\n };\n\n /**\n * Resolve an awakeable.\n * @param id the string ID of the awakeable.\n * This is supplied by the service that needs to be woken up.\n * @param payload the payload to pass to the service that is woken up.\n * The SDK serializes the payload with `Buffer.from(JSON.stringify(payload))`\n * and deserializes it in the receiving service with `JSON.parse(result.toString()) as T`.\n * @example Retryable error with custom retry delay\n * // The sleeping service should have sent the awakeableIdentifier string to this service.\n * ctx.resolveAwakeable(awakeableIdentifier, \"hello\");\n */\n resolveAwakeable<T>(id: string, payload?: T, serde?: Serde<T>): void;\n\n /**\n * Reject an awakeable. When rejecting, the service waiting on this awakeable will be woken up with a terminal error with the provided reason.\n * @param id the string ID of the awakeable.\n * This is supplied by the service that needs to be woken up.\n * @param reason the reason of the rejection.\n *\n * @example\n * // The sleeping service should have sent the awakeableIdentifier string to this service.\n * ctx.rejectAwakeable(awakeableIdentifier, \"super bad error\");\n */\n rejectAwakeable(id: string, reason: string): void;\n\n /**\n * Sleep until a timeout has passed.\n * @param duration either Duration type or milliseconds.\n * @param name Observability name. This will be shown in the UI.\n * This is a lower-bound.\n *\n * @example\n * await ctx.sleep(1000);\n */\n sleep(duration: Duration | number, name?: string): RestatePromise<void>;\n\n /**\n * Makes a type-safe request/response RPC to the specified target service.\n *\n * The RPC goes through Restate and is guaranteed to be reliably delivered. The RPC is also\n * journaled for durable execution and will thus not be duplicated when the handler is re-invoked\n * for retries or after suspending.\n *\n * This call will return the result produced by the target handler, or the Error, if the target\n * handler finishes with a Terminal Error.\n *\n * This call is a suspension point: The handler might suspend while awaiting the response and\n * resume once the response is available.\n *\n * @example\n * *Service Side:*\n * ```ts\n * const service = restate.service(\n * name: \"myservice\",\n * handlers: {\n * someAction: async(ctx: restate.Context, req: string) => { ... },\n * anotherAction: async(ctx: restate.Context, count: number) => { ... }\n * });\n *\n * // option 1: export only the type signature\n * export type Service = typeof service;\n *\n *\n * restate.serve({ services: [service], port: 9080 });\n * ```\n * *Client side:*\n * ```ts\n * // option 1: use only types and supply service name separately\n * const result1 = await ctx.serviceClient<Service>({name: \"myservice\"}).someAction(\"hello!\");\n *\n * // option 2: use full API spec\n * type MyService: Service = { name: \"myservice\" };\n * const result2 = await ctx.serviceClient(Service).anotherAction(1337);\n * ```\n */\n serviceClient<D>(opts: ServiceDefinitionFrom<D>): Client<Service<D>>;\n\n /**\n * Same as {@link serviceClient} but for virtual objects.\n *\n * @param opts\n * @param key the virtual object key\n */\n objectClient<D>(\n opts: VirtualObjectDefinitionFrom<D>,\n key: string\n ): Client<VirtualObject<D>>;\n\n /**\n * Same as {@link serviceClient} but for workflows.\n *\n * @param opts\n * @param key the workflow key\n */\n workflowClient<D>(\n opts: WorkflowDefinitionFrom<D>,\n key: string\n ): Client<Workflow<D>>;\n\n /**\n * Same as {@link objectSendClient} but for workflows.\n *\n * @param opts\n * @param key the workflow key\n */\n workflowSendClient<D>(\n opts: WorkflowDefinitionFrom<D>,\n key: string\n ): SendClient<Workflow<D>>;\n\n /**\n * Makes a type-safe one-way RPC to the specified target service. This method effectively behaves\n * like enqueuing the message in a message queue.\n *\n * The message goes through Restate and is guaranteed to be reliably delivered. The RPC is also\n * journaled for durable execution and will thus not be duplicated when the handler is re-invoked\n * for retries or after suspending.\n *\n * This call will return immediately; the message sending happens asynchronously in the background.\n * Despite that, the message is guaranteed to be sent, because the completion of the invocation that\n * triggers the send (calls this function) happens logically after the sending. That means that any\n * failure where the message does not reach Restate also cannot complete this invocation, and will\n * hence recover this handler and (through the durable execution) recover the message to be sent.\n *\n * @example\n * *Service Side:*\n * ```ts\n * const service = restate.service(\n * name: \"myservice\",\n * handlers: {\n * someAction: async(ctx: restate.Context, req: string) => { ... },\n * anotherAction: async(ctx: restate.Context, count: number) => { ... }\n * });\n *\n * // option 1: export only the type signature of the router\n * export type MyApi = typeof service;\n *\n * // option 2: export the API definition with type and name (name)\n * const MyService: MyApi = { name: \"myservice\" };\n *\n * restate.serve({ services: [service], port: 9080 });\n * ```\n * *Client side:*\n * ```ts\n * // option 1: use only types and supply service name separately\n * ctx.serviceSendClient<MyApi>({name: \"myservice\"}).someAction(\"hello!\");\n *\n * // option 2: use full API spec\n * ctx.serviceSendClient(MyService).anotherAction(1337);\n * ```\n */\n serviceSendClient<D>(\n service: ServiceDefinitionFrom<D>,\n opts?: SendOptions\n ): SendClient<Service<D>>;\n\n /**\n * Same as {@link serviceSendClient} but for virtual objects.\n *\n * @param obj\n * @param key the virtual object key\n * @param opts Send options\n */\n objectSendClient<D>(\n obj: VirtualObjectDefinitionFrom<D>,\n key: string,\n opts?: SendOptions\n ): SendClient<VirtualObject<D>>;\n\n genericCall<REQ = Uint8Array, RES = Uint8Array>(\n call: GenericCall<REQ, RES>\n ): InvocationPromise<RES>;\n\n genericSend<REQ = Uint8Array>(call: GenericSend<REQ>): InvocationHandle;\n\n /**\n * Returns the raw request that triggered that handler.\n * Use that object to inspect the original request headers\n */\n request(): Request;\n\n /**\n * Cancel an invocation\n *\n * @param invocationId the invocation id to cancel\n */\n cancel(invocationId: InvocationId): void;\n\n /**\n * Attach to an invocation\n *\n * @param invocationId the invocation id to attach to\n * @param serde the serde to use for the result, default to JSON serde.\n */\n attach<T>(invocationId: InvocationId, serde?: Serde<T>): RestatePromise<T>;\n}\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - access/update state\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * This context can be used only within virtual objects.\n *\n */\nexport interface ObjectContext<TState extends TypedState = UntypedState>\n extends Context, KeyValueStore<TState>, RestateObjectContext {\n key: string;\n}\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * This context can be used only within a shared virtual objects.\n *\n */\nexport interface ObjectSharedContext<TState extends TypedState = UntypedState>\n extends Context, RestateObjectSharedContext {\n key: string;\n\n /**\n * Get/retrieve state from the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to retrieve\n * @returns a Promise that is resolved with the value of the state key\n *\n * @example\n * const state = await ctx.get<string>(\"STATE\");\n */\n get<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): Promise<(TState extends UntypedState ? TValue : TState[TKey]) | null>;\n\n /**\n * Retrieve all the state keys for this object.\n */\n stateKeys(): Promise<Array<string>>;\n}\n\nexport interface Rand {\n /**\n * Equivalent of JS `Math.random()` but deterministic; seeded by the invocation ID of the current invocation,\n * each call will return a new pseudorandom float within the range [0,1)\n */\n random(): number;\n\n /**\n * Using the same random source and seed as random(), produce a UUID version 4 string. This is inherently predictable\n * based on the invocation ID and should not be used in cryptographic contexts\n */\n uuidv4(): string;\n}\n\n/**\n * A promise that can be combined using Promise combinators in RestateContext.\n */\nexport type RestatePromise<T> = Promise<T> & {\n /**\n * Creates a promise that awaits for the current promise up to the specified timeout duration.\n * If the timeout is fired, this Promise will be rejected with a {@link TimeoutError}.\n *\n * @param millis duration of the sleep in millis.\n * This is a lower-bound.\n */\n orTimeout(millis: Duration | number): RestatePromise<T>;\n\n /**\n * Creates a new {@link RestatePromise} that maps the result of this promise with\n * the provided `mapper`, once this promise is fulfilled.\n *\n * **NOTE**: You **MUST** use this API when you need to map the result of a\n * {@link RestatePromise} without `await`ing it, rather than using {@link Promise.then}.\n * {@link Promise.then} is used by Restate to distinguish when awaiting an asynchronous operation,\n * thus calling `.then` on several Restate promises can lead to concurrency issues.\n *\n * @param mapper the function to execute when this promise is fulfilled.\n * If the promise completed successfully, `value` is provided as input, otherwise `failure` is provided as input.\n * If this mapper returns a value, this value will be used to resolve the returned {@link RestatePromise}.\n * If the mapper throws a {@link TerminalError}, this error will be used to reject the returned {@link RestatePromise}.\n */\n map<U>(mapper: (value?: T, failure?: TerminalError) => U): RestatePromise<U>;\n};\n\n/**\n * Represents an invocation id.\n * @see {@link InvocationIdParser}\n */\nexport type InvocationId = string & { __brand: \"InvocationId\" };\n\nexport const InvocationIdParser = {\n /**\n * Creates an invocation id from a string.\n * @param id the string to use as invocation id.\n */\n fromString(id: string): InvocationId {\n if (!id.startsWith(\"inv\")) {\n throw new Error(\n `Expected invocation id to start with 'inv' but got ${id}`\n );\n }\n return id as InvocationId;\n },\n};\n\nexport type InvocationHandle = {\n // The invocation id of the call\n readonly invocationId: Promise<InvocationId>;\n};\n\nexport type InvocationPromise<T> = RestatePromise<T> & InvocationHandle;\n\nexport const RestatePromise = {\n /**\n * Creates a Promise that is resolved with an array of results when all of the provided Promises\n * resolve, or rejected when any Promise is rejected.\n *\n * See {@link Promise.all} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n all<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<{ -readonly [P in keyof T]: Awaited<T[P]> }> {\n if (values.length === 0) {\n throw new Error(\n \"Expected combineable promise to have at least one promise\"\n );\n }\n return ContextImpl.createCombinator(\n (p) => Promise.all(p),\n values\n ) as RestatePromise<{\n -readonly [P in keyof T]: Awaited<T[P]>;\n }>;\n },\n\n /**\n * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved\n * or rejected.\n *\n * See {@link Promise.race} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n race<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<Awaited<T[number]>> {\n if (values.length === 0) {\n throw new Error(\n \"Expected combineable promise to have at least one promise\"\n );\n }\n return ContextImpl.createCombinator(\n (p) => Promise.race(p),\n values\n ) as RestatePromise<Awaited<T[number]>>;\n },\n\n /**\n * Creates a promise that fulfills when any of the input's promises fulfills, with this first fulfillment value.\n * It rejects when all the input's promises reject (including when an empty iterable is passed),\n * with an AggregateError containing an array of rejection reasons.\n *\n * See {@link Promise.any} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n any<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<Awaited<T[number]>> {\n if (values.length === 0) {\n throw new Error(\n \"Expected combineable promise to have at least one promise\"\n );\n }\n return ContextImpl.createCombinator(\n (p) => Promise.any(p),\n values\n ) as RestatePromise<Awaited<T[number]>>;\n },\n\n /**\n * Creates a promise that fulfills when all the input's promises settle (including when an empty iterable is passed),\n * with an array of objects that describe the outcome of each promise.\n *\n * See {@link Promise.allSettled} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n allSettled<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<{\n -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;\n }> {\n if (values.length === 0) {\n throw new Error(\n \"Expected combineable promise to have at least one promise\"\n );\n }\n return ContextImpl.createCombinator(\n (p) => Promise.allSettled(p),\n values\n ) as RestatePromise<{\n -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;\n }>;\n },\n};\n\n/**\n * Workflow bound durable promise\n *\n * See {@link WorkflowSharedContext} promise..\n */\nexport type DurablePromise<T> = Promise<T> & {\n /**\n * Returns the value of the promise, if it has been resolved.\n */\n peek(): Promise<T | undefined>;\n\n /**\n * Resolve the promise with the given value.\n * @param value the value to resolve the promise with\n */\n resolve(value?: T): Promise<void>;\n\n /**\n * Reject the promise with the given error message.\n * @param errorMsg the error message to use for rejection.\n */\n reject(errorMsg: string): Promise<void>;\n\n /**\n * Obtain a {@link RestatePromise} variant of this promise.\n */\n get(): RestatePromise<T>;\n};\n\nexport interface WorkflowSharedContext<TState extends TypedState = UntypedState>\n extends ObjectSharedContext<TState>, RestateWorkflowSharedContext {\n /**\n * Create a durable promise that can be resolved or rejected during the workflow execution.\n * The promise is bound to the workflow and will be persisted across suspensions and retries.\n * @example Add some retry options\n * ```ts\n * const wf = restate.workflow({\n * name: \"myWorkflow\",\n * handlers: {\n * run: async (ctx: restate.WorkflowContext) => {\n * // ... do some work ...\n * const payment = await ctx.promise<Payment>(\"payment.succeeded\");\n * // ... do some more work ...\n * },\n *\n * onPaymentSucceeded: async (ctx: restate.WorkflowContext, payment) => {\n * // ... handle payment succeeded ...\n * await ctx.promise(\"payment.succeeded\").resolve(payment);\n * }\n * });\n * ```\n *\n * @param name the name of the durable promise\n */\n promise<T>(name: string, serde?: Serde<T>): DurablePromise<T>;\n}\n\nexport interface WorkflowContext<TState extends TypedState = UntypedState>\n extends\n WorkflowSharedContext<TState>,\n ObjectContext<TState>,\n RestateWorkflowContext {}\n"],"mappings":";;;AAusBA,MAAa,qBAAqB,EAKhC,WAAW,IAA0B;AACnC,KAAI,CAAC,GAAG,WAAW,MAAM,CACvB,OAAM,IAAI,MACR,sDAAsD,KACvD;AAEH,QAAO;GAEV;AASD,MAAa,iBAAiB;CAU5B,IACE,QAC6D;AAC7D,MAAI,OAAO,WAAW,EACpB,OAAM,IAAI,MACR,4DACD;AAEH,SAAO,YAAY,kBAChB,MAAM,QAAQ,IAAI,EAAE,EACrB,OACD;;CAcH,KACE,QACoC;AACpC,MAAI,OAAO,WAAW,EACpB,OAAM,IAAI,MACR,4DACD;AAEH,SAAO,YAAY,kBAChB,MAAM,QAAQ,KAAK,EAAE,EACtB,OACD;;CAaH,IACE,QACoC;AACpC,MAAI,OAAO,WAAW,EACpB,OAAM,IAAI,MACR,4DACD;AAEH,SAAO,YAAY,kBAChB,MAAM,QAAQ,IAAI,EAAE,EACrB,OACD;;CAYH,WACE,QAGC;AACD,MAAI,OAAO,WAAW,EACpB,OAAM,IAAI,MACR,4DACD;AAEH,SAAO,YAAY,kBAChB,MAAM,QAAQ,WAAW,EAAE,EAC5B,OACD;;CAIJ"}
1
+ {"version":3,"file":"context.js","names":[],"sources":["../src/context.ts"],"sourcesContent":["/*\n * Copyright (c) 2023-2024 - Restate Software, Inc., Restate GmbH\n *\n * This file is part of the Restate SDK for Node.js/TypeScript,\n * which is released under the MIT license.\n *\n * You can find a copy of the license in file LICENSE in the root\n * directory of this repository or package, or at\n * https://github.com/restatedev/sdk-typescript/blob/main/LICENSE\n */\n\nimport type { Client, SendClient } from \"./types/rpc.js\";\nimport type {\n RestateContext,\n RestateObjectContext,\n RestateObjectSharedContext,\n RestateWorkflowContext,\n RestateWorkflowSharedContext,\n Service,\n ServiceDefinitionFrom,\n VirtualObject,\n VirtualObjectDefinitionFrom,\n Workflow,\n WorkflowDefinitionFrom,\n Serde,\n Duration,\n} from \"@restatedev/restate-sdk-core\";\nimport type { TerminalError } from \"./types/errors.js\";\nimport {\n InternalRestatePromise,\n CombinatorRestatePromise,\n ConstRestatePromise,\n} from \"./promises.js\";\n\nexport interface Target {\n service: string;\n handler: string;\n key?: string;\n toString(): string;\n}\n\n/**\n * Represents the original request as sent to this handler.\n *\n * A request object includes the request headers, and the raw unparsed\n * request body.\n */\nexport interface Request {\n readonly target: Target;\n\n /**\n * The unique id that identifies the current function invocation. This id is guaranteed to be\n * unique across invocations, but constant across reties and suspensions.\n */\n readonly id: InvocationId;\n\n /**\n * Request headers - the following headers capture the original invocation headers, as provided to\n * the ingress.\n */\n readonly headers: ReadonlyMap<string, string>;\n\n /**\n * Attempt headers - the following headers are sent by the restate runtime.\n * These headers are attempt specific, generated by the restate runtime uniquely for each attempt.\n * These headers might contain information such as the W3C trace context, and attempt specific information.\n */\n readonly attemptHeaders: ReadonlyMap<string, string | string[] | undefined>;\n\n /**\n * Raw unparsed request body\n */\n readonly body: Uint8Array;\n\n /**\n * Extra arguments provided to the request handler:\n * Lambda: [Context]\n * Cloudflare workers: [Env, ExecutionContext]\n * Deno: [ConnInfo]\n * Bun: [Server]\n * These arguments can contain request-specific values that could change after a suspension.\n * Care should be taken to use them deterministically.\n */\n readonly extraArgs: unknown[];\n\n /**\n * This is a signal that is aborted when the current attempt has been completed either successful or unsuccessfully.\n * When the signal is aborted, the current attempt has been completed and the handler should not perform any more work, other\n * than cleanup any external resources that might be shared across attempts (e.g. database connections).\n */\n readonly attemptCompletedSignal: AbortSignal;\n}\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport type TypedState = Record<string, any>;\nexport type UntypedState = { _: never };\n\n/**\n * Key value store operations. Only keyed services have an attached key-value store.\n */\nexport interface KeyValueStore<TState extends TypedState> {\n /**\n * Get/retrieve state from the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to retrieve\n * @returns a Promise that is resolved with the value of the state key\n *\n * @example\n * const state = await ctx.get<string>(\"STATE\");\n */\n get<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): Promise<(TState extends UntypedState ? TValue : TState[TKey]) | null>;\n\n stateKeys(): Promise<Array<string>>;\n\n /**\n * Set/store state in the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to set\n * @param value value to set\n *\n * @example\n * ctx.set(\"STATE\", \"Hello\");\n */\n set<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n value: TState extends UntypedState ? TValue : TState[TKey],\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): void;\n\n /**\n * Clear/delete state in the Restate runtime.\n * @param name key of the state to delete\n *\n * @example\n * ctx.clear(\"STATE\");\n */\n clear<TKey extends keyof TState>(\n name: TState extends UntypedState ? string : TKey\n ): void;\n\n /**\n * Clear/delete all the state entries in the Restate runtime.\n *\n * @example\n * ctx.clearAll();\n */\n clearAll(): void;\n}\n\nexport interface ContextDate {\n /** Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).\n * This is equivalent to Date.now()\n */\n now(): Promise<number>;\n /** Returns the JSON representation of the current date.\n * This is equivalent to new Date().toJSON()\n **/\n toJSON(): Promise<string>;\n}\n\n/**\n * A function that can be run and its result durably persisted by Restate.\n */\nexport type RunAction<T> = (() => Promise<T>) | (() => T);\n\nexport type RunOptions<T> = {\n serde?: Serde<T>;\n\n /**\n * Max number of attempts (including the initial), before giving up.\n *\n * When giving up, `ctx.run` will throw a `TerminalError` wrapping the original error message.\n */\n maxRetryAttempts?: number;\n\n /**\n * Max duration of retries, before giving up.\n *\n * When giving up, `ctx.run` will throw a `TerminalError` wrapping the original error message.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n maxRetryDuration?: Duration | number;\n\n /**\n * Initial interval for the first retry attempt.\n * Retry interval will grow by a factor specified in `retryIntervalFactor`.\n *\n * The default is 50 milliseconds.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n initialRetryInterval?: Duration | number;\n\n /**\n * Max interval between retries.\n * Retry interval will grow by a factor specified in `retryIntervalFactor`.\n *\n * The default is 10 seconds.\n *\n * If a number is provided, it will be interpreted as milliseconds.\n */\n maxRetryInterval?: Duration | number;\n\n /**\n * Exponentiation factor to use when computing the next retry delay.\n *\n * The default value is `2`, meaning retry interval will double at each attempt.\n */\n retryIntervalFactor?: number;\n};\n\n/**\n * Call a handler directly avoiding restate's type safety checks.\n * This is a generic mechanism to invoke handlers directly by only knowing\n * the service and handler name, (or key in the case of objects or workflows)\n */\nexport type GenericCall<REQ, RES> = {\n service: string;\n method: string;\n parameter: REQ;\n key?: string;\n headers?: Record<string, string>;\n inputSerde?: Serde<REQ>;\n outputSerde?: Serde<RES>;\n idempotencyKey?: string;\n /**\n * Observability name, recorded in the Restate journal.\n */\n name?: string;\n};\n\n/**\n * Send a message to an handler directly avoiding restate's type safety checks.\n * This is a generic mechanism to invoke handlers directly by only knowing\n * the service and handler name, (or key in the case of objects or workflows)\n */\nexport type GenericSend<REQ> = {\n service: string;\n method: string;\n parameter: REQ;\n key?: string;\n headers?: Record<string, string>;\n inputSerde?: Serde<REQ>;\n delay?: Duration | number;\n idempotencyKey?: string;\n /**\n * Observability name, recorded in the Restate journal.\n */\n name?: string;\n};\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * Virtual objects can also access their key-value store using the {@link ObjectContext}.\n *\n */\nexport interface Context extends RestateContext {\n /**\n * Deterministic random methods; these are inherently predictable (seeded on the invocation ID, which is not secret)\n * and so should not be used for any cryptographic purposes. They are useful for identifiers, idempotency keys,\n * and for uniform sampling from a set of options. If a cryptographically secure value is needed, please generate that\n * externally and capture the result with a side effect.\n *\n * Calls to these methods from inside `ctx.run` are disallowed and will fail - side effects must be idempotent, and\n * these calls are not.\n */\n rand: Rand;\n\n /**\n * Console to use for logging. It attaches to each log message some contextual information,\n * such as invoked service method and invocation id, and automatically excludes logs during replay.\n */\n console: Console;\n\n /**\n * Deterministic date.\n */\n date: ContextDate;\n\n /**\n * Run an operation and store the result in Restate. The operation will thus not\n * be re-run during a later replay, but take the durable result from Restate.\n *\n * This let you capture potentially non-deterministic computation and interaction\n * with external systems in a safe way.\n *\n * Failure semantics are:\n * - If an operation has run and persisted before, the result (value or Error) will be\n * taken from the Restate journal.\n * - There is a small window where an action may be re-run, if a failure\n * occurred between a successful run and persisting the result.\n * - No second action will be run while a previous run's result is not\n * yet durable. That way, effects that build on top of each other can assume\n * deterministic results from previous runs, and at most one run will be\n * re-executed on replay (the latest, if the failure happened in the small windows\n * described above).\n *\n * You can customize retry options by either:\n *\n * - Providing retry policy options in {@link RunOptions}\n * - Throwing {@link RetryableError}, providing `retryAfter` option. This can be especially useful when interacting with HTTP requests returning the `Retry-After` header. You can combine the usage of throwing {@link RetryableError} with the `maxRetryAttempts`/`maxRetryDuration` from {@link RunOptions}.\n *\n * @example Run some external action and persist its result\n * ```ts\n * const result = await ctx.run(someExternalAction)\n *```\n * @example Add some retry options\n * ```ts\n * const result = await ctx.run(\"my action\", someExternalAction, { maxRetryAttempts: 10 })\n * ```\n * @example Terminal errors and retryable errors\n * ```ts\n * await ctx.run(\"payment action\", async () => {\n * const result = await paymentProvider.charge(txId, paymentInfo);\n * if (result.paymentRejected) {\n * // this action will not be retried anymore\n * throw new TerminalError(\"Payment failed\");\n * } else if (result.paymentGatewayBusy) {\n * // restate will retry automatically\n * // to bound retries, use RunOptions\n * throw new Error(\"Payment gateway busy\");\n * } else {\n * // success!\n * }\n * });\n * ```\n * @example Retryable error with custom retry delay\n * ```ts\n * await ctx.run(\"payment action\", async () => {\n * const res = fetch(...);\n * if (!res.ok) {\n * // Read Retry-After header\n * const retryAfterHeader = res.headers['Retry-After']\n *\n * // Use RetryableError to customize in how long to retry\n * throw RetryableError.from(cause, { retryAfter: { seconds: retryAfterHeader } })\n * }\n * }, {\n * // Retry at most ten times\n * maxRetryAttempts: 10\n * });\n * ```\n *\n * @param action The function to run.\n */\n run<T>(action: RunAction<T>): RestatePromise<T>;\n\n /**\n * Same as {@link run}, but providing a name, used for observability purposes.\n */\n run<T>(name: string, action: RunAction<T>): RestatePromise<T>;\n\n /**\n * See {@link run}\n */\n run<T>(\n name: string,\n action: RunAction<T>,\n options: RunOptions<T>\n ): RestatePromise<T>;\n\n /**\n * Register an awakeable and pause the processing until the awakeable ID (and optional payload) have been returned to the service\n * (via ctx.completeAwakeable(...)). The SDK deserializes the payload with `JSON.parse(result.toString()) as T`.\n * @returns\n * - id: the string ID that has to be used to complete the awakaeble by some external service\n * - promise: the Promise that needs to be awaited and that is resolved with the payload that was supplied by the service which completed the awakeable\n * @example Retryable errors and terminal errors\n * const awakeable = ctx.awakeable<string>();\n *\n * // send the awakeable ID to some external service that will wake this one back up\n * // The ID can be retrieved by:\n * const id = awakeable.id;\n *\n * // ... send to external service ...\n *\n * // Wait for the external service to wake this service back up\n * const result = await awakeable.promise;\n */\n awakeable<T>(serde?: Serde<T>): {\n id: string;\n promise: RestatePromise<T>;\n };\n\n /**\n * Resolve an awakeable.\n * @param id the string ID of the awakeable.\n * This is supplied by the service that needs to be woken up.\n * @param payload the payload to pass to the service that is woken up.\n * The SDK serializes the payload with `Buffer.from(JSON.stringify(payload))`\n * and deserializes it in the receiving service with `JSON.parse(result.toString()) as T`.\n * @example Retryable error with custom retry delay\n * // The sleeping service should have sent the awakeableIdentifier string to this service.\n * ctx.resolveAwakeable(awakeableIdentifier, \"hello\");\n */\n resolveAwakeable<T>(id: string, payload?: T, serde?: Serde<T>): void;\n\n /**\n * Reject an awakeable. When rejecting, the service waiting on this awakeable will be woken up with a terminal error with the provided reason.\n * @param id the string ID of the awakeable.\n * This is supplied by the service that needs to be woken up.\n * @param reason the reason of the rejection, either a string message or a {@link TerminalError}.\n *\n * @example\n * // The sleeping service should have sent the awakeableIdentifier string to this service.\n * ctx.rejectAwakeable(awakeableIdentifier, \"super bad error\");\n */\n rejectAwakeable(id: string, reason: string | TerminalError): void;\n\n /**\n * Sleep until a timeout has passed.\n * @param duration either Duration type or milliseconds.\n * @param name Observability name. This will be shown in the UI.\n * This is a lower-bound.\n *\n * @example\n * await ctx.sleep(1000);\n */\n sleep(duration: Duration | number, name?: string): RestatePromise<void>;\n\n /**\n * Makes a type-safe request/response RPC to the specified target service.\n *\n * The RPC goes through Restate and is guaranteed to be reliably delivered. The RPC is also\n * journaled for durable execution and will thus not be duplicated when the handler is re-invoked\n * for retries or after suspending.\n *\n * This call will return the result produced by the target handler, or the Error, if the target\n * handler finishes with a Terminal Error.\n *\n * This call is a suspension point: The handler might suspend while awaiting the response and\n * resume once the response is available.\n *\n * @example\n * *Service Side:*\n * ```ts\n * const service = restate.service(\n * name: \"myservice\",\n * handlers: {\n * someAction: async(ctx: restate.Context, req: string) => { ... },\n * anotherAction: async(ctx: restate.Context, count: number) => { ... }\n * });\n *\n * // option 1: export only the type signature\n * export type Service = typeof service;\n *\n *\n * restate.serve({ services: [service], port: 9080 });\n * ```\n * *Client side:*\n * ```ts\n * // option 1: use only types and supply service name separately\n * const result1 = await ctx.serviceClient<Service>({name: \"myservice\"}).someAction(\"hello!\");\n *\n * // option 2: use full API spec\n * type MyService: Service = { name: \"myservice\" };\n * const result2 = await ctx.serviceClient(Service).anotherAction(1337);\n * ```\n */\n serviceClient<D>(opts: ServiceDefinitionFrom<D>): Client<Service<D>>;\n\n /**\n * Same as {@link serviceClient} but for virtual objects.\n *\n * @param opts\n * @param key the virtual object key\n */\n objectClient<D>(\n opts: VirtualObjectDefinitionFrom<D>,\n key: string\n ): Client<VirtualObject<D>>;\n\n /**\n * Same as {@link serviceClient} but for workflows.\n *\n * @param opts\n * @param key the workflow key\n */\n workflowClient<D>(\n opts: WorkflowDefinitionFrom<D>,\n key: string\n ): Client<Workflow<D>>;\n\n /**\n * Same as {@link objectSendClient} but for workflows.\n *\n * @param opts\n * @param key the workflow key\n */\n workflowSendClient<D>(\n opts: WorkflowDefinitionFrom<D>,\n key: string\n ): SendClient<Workflow<D>>;\n\n /**\n * Makes a type-safe one-way RPC to the specified target service. This method effectively behaves\n * like enqueuing the message in a message queue.\n *\n * The message goes through Restate and is guaranteed to be reliably delivered. The RPC is also\n * journaled for durable execution and will thus not be duplicated when the handler is re-invoked\n * for retries or after suspending.\n *\n * This call will return immediately; the message sending happens asynchronously in the background.\n * Despite that, the message is guaranteed to be sent, because the completion of the invocation that\n * triggers the send (calls this function) happens logically after the sending. That means that any\n * failure where the message does not reach Restate also cannot complete this invocation, and will\n * hence recover this handler and (through the durable execution) recover the message to be sent.\n *\n * @example\n * *Service Side:*\n * ```ts\n * const service = restate.service(\n * name: \"myservice\",\n * handlers: {\n * someAction: async(ctx: restate.Context, req: string) => { ... },\n * anotherAction: async(ctx: restate.Context, count: number) => { ... }\n * });\n *\n * // option 1: export only the type signature of the router\n * export type MyApi = typeof service;\n *\n * // option 2: export the API definition with type and name (name)\n * const MyService: MyApi = { name: \"myservice\" };\n *\n * restate.serve({ services: [service], port: 9080 });\n * ```\n * *Client side:*\n * ```ts\n * // option 1: use only types and supply service name separately\n * ctx.serviceSendClient<MyApi>({name: \"myservice\"}).someAction(\"hello!\");\n *\n * // option 2: use full API spec\n * ctx.serviceSendClient(MyService).anotherAction(1337);\n * ```\n */\n serviceSendClient<D>(\n service: ServiceDefinitionFrom<D>\n ): SendClient<Service<D>>;\n\n /**\n * Same as {@link serviceSendClient} but for virtual objects.\n *\n * @param obj\n * @param key the virtual object key\n * @param opts Send options\n */\n objectSendClient<D>(\n obj: VirtualObjectDefinitionFrom<D>,\n key: string\n ): SendClient<VirtualObject<D>>;\n\n genericCall<REQ = Uint8Array, RES = Uint8Array>(\n call: GenericCall<REQ, RES>\n ): InvocationPromise<RES>;\n\n genericSend<REQ = Uint8Array>(call: GenericSend<REQ>): InvocationHandle;\n\n /**\n * Returns the raw request that triggered that handler.\n * Use that object to inspect the original request headers\n */\n request(): Request;\n\n /**\n * Cancel an invocation\n *\n * @param invocationId the invocation id to cancel\n */\n cancel(invocationId: InvocationId): void;\n\n /**\n * Attach to an invocation\n *\n * @param invocationId the invocation id to attach to\n * @param serde the serde to use for the result, default to JSON serde.\n */\n attach<T>(invocationId: InvocationId, serde?: Serde<T>): RestatePromise<T>;\n}\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - access/update state\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * This context can be used only within virtual objects.\n *\n */\nexport interface ObjectContext<TState extends TypedState = UntypedState>\n extends Context, KeyValueStore<TState>, RestateObjectContext {\n key: string;\n}\n\n/**\n * The context that gives access to all Restate-backed operations, for example\n * - sending reliable messages / RPC through Restate\n * - execute non-deterministic closures and memoize their result\n * - sleeps and delayed calls\n * - awakeables\n * - ...\n *\n * This context can be used only within a shared virtual objects.\n *\n */\nexport interface ObjectSharedContext<TState extends TypedState = UntypedState>\n extends Context, RestateObjectSharedContext {\n key: string;\n\n /**\n * Get/retrieve state from the Restate runtime.\n * Note that state objects are serialized with `Buffer.from(JSON.stringify(theObject))`\n * and deserialized with `JSON.parse(value.toString()) as T`.\n *\n * @param name key of the state to retrieve\n * @returns a Promise that is resolved with the value of the state key\n *\n * @example\n * const state = await ctx.get<string>(\"STATE\");\n */\n get<TValue, TKey extends keyof TState = string>(\n name: TState extends UntypedState ? string : TKey,\n serde?: Serde<TState extends UntypedState ? TValue : TState[TKey]>\n ): Promise<(TState extends UntypedState ? TValue : TState[TKey]) | null>;\n\n /**\n * Retrieve all the state keys for this object.\n */\n stateKeys(): Promise<Array<string>>;\n}\n\nexport interface Rand {\n /**\n * Equivalent of JS `Math.random()` but deterministic; seeded by the invocation ID of the current invocation,\n * each call will return a new pseudorandom float within the range [0,1)\n */\n random(): number;\n\n /**\n * Using the same random source and seed as random(), produce a UUID version 4 string. This is inherently predictable\n * based on the invocation ID and should not be used in cryptographic contexts\n */\n uuidv4(): string;\n}\n\n/**\n * A promise that can be combined using Promise combinators in RestateContext.\n *\n * To check at runtime whether a value is a {@link RestatePromise}, use {@link isRestatePromise}.\n */\nexport type RestatePromise<T> = Promise<T> & {\n /**\n * Creates a promise that awaits for the current promise up to the specified timeout duration.\n * If the timeout is fired, this Promise will be rejected with a {@link TimeoutError}.\n *\n * @param millis duration of the sleep in millis.\n * This is a lower-bound.\n */\n orTimeout(millis: Duration | number): RestatePromise<T>;\n\n /**\n * Creates a new {@link RestatePromise} that maps the result of this promise with\n * the provided `mapper`, once this promise is fulfilled.\n *\n * **NOTE**: You **MUST** use this API when you need to map the result of a\n * {@link RestatePromise} without `await`ing it, rather than using {@link Promise.then}.\n * {@link Promise.then} is used by Restate to distinguish when awaiting an asynchronous operation,\n * thus calling `.then` on several Restate promises can lead to concurrency issues.\n *\n * **NOTE**: Contrary to `.then`, the closure is executed ONLY when the promise is awaited,\n * and not eagerly as soon as the promise completes.\n *\n * @param mapper the function to execute when this promise is fulfilled.\n * If the promise completed successfully, `value` is provided as input, otherwise `failure` is provided as input.\n * If this mapper returns a value, this value will be used to resolve the returned {@link RestatePromise}.\n * If the mapper throws a {@link TerminalError}, this error will be used to reject the returned {@link RestatePromise}.\n */\n map<U>(mapper: (value?: T, failure?: TerminalError) => U): RestatePromise<U>;\n};\n\n/**\n * Represents an invocation id.\n * @see {@link InvocationIdParser}\n */\nexport type InvocationId = string & { __brand: \"InvocationId\" };\n\nexport const InvocationIdParser = {\n /**\n * Creates an invocation id from a string.\n * @param id the string to use as invocation id.\n */\n fromString(id: string): InvocationId {\n if (!id.startsWith(\"inv\")) {\n throw new Error(\n `Expected invocation id to start with 'inv' but got ${id}`\n );\n }\n return id as InvocationId;\n },\n};\n\nexport type InvocationHandle = {\n // The invocation id of the call\n readonly invocationId: Promise<InvocationId>;\n};\n\nexport type InvocationPromise<T> = RestatePromise<T> & InvocationHandle;\n\nexport const RestatePromise = {\n /**\n * Creates a {@link RestatePromise} that is resolved with the given value.\n *\n * If the value is already a {@link RestatePromise}, it is returned as-is (mirroring\n * the behavior of {@link Promise.resolve} when given a native {@link Promise}).\n *\n * @param value The value to resolve, or an existing {@link RestatePromise} to return.\n * @returns A resolved {@link RestatePromise}, or the input promise unchanged.\n */\n resolve<T>(value: T): RestatePromise<Awaited<T>> {\n if (value instanceof InternalRestatePromise) {\n return value as unknown as RestatePromise<Awaited<T>>;\n }\n return ConstRestatePromise.resolve(value);\n },\n\n /**\n * Creates a {@link RestatePromise} that is rejected with the given {@link TerminalError}.\n *\n * @param reason The {@link TerminalError} to reject with.\n * @returns A rejected {@link RestatePromise}.\n */\n reject<T = never>(reason: TerminalError): RestatePromise<T> {\n return ConstRestatePromise.reject(reason);\n },\n\n /**\n * Creates a Promise that is resolved with an array of results when all of the provided Promises\n * resolve, or rejected when any Promise is rejected.\n *\n * See {@link Promise.all} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n all<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<{ -readonly [P in keyof T]: Awaited<T[P]> }> {\n return CombinatorRestatePromise.fromPromises(\n (p) => Promise.all(p),\n values\n ) as RestatePromise<{\n -readonly [P in keyof T]: Awaited<T[P]>;\n }>;\n },\n\n /**\n * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved\n * or rejected.\n *\n * See {@link Promise.race} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n race<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<Awaited<T[number]>> {\n if (values.length === 0) {\n return ConstRestatePromise.pending();\n }\n return CombinatorRestatePromise.fromPromises(\n (p) => Promise.race(p),\n values\n ) as RestatePromise<Awaited<T[number]>>;\n },\n\n /**\n * Creates a promise that fulfills when any of the input's promises fulfills, with this first fulfillment value.\n * It rejects when all the input's promises reject (including when an empty iterable is passed),\n * with an AggregateError containing an array of rejection reasons.\n *\n * See {@link Promise.any} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n any<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<Awaited<T[number]>> {\n return CombinatorRestatePromise.fromPromises(\n (p) => Promise.any(p),\n values\n ) as RestatePromise<Awaited<T[number]>>;\n },\n\n /**\n * Creates a promise that fulfills when all the input's promises settle (including when an empty iterable is passed),\n * with an array of objects that describe the outcome of each promise.\n *\n * See {@link Promise.allSettled} for more details.\n *\n * @param values An iterable of Promises.\n * @returns A new Promise.\n */\n allSettled<const T extends readonly RestatePromise<unknown>[]>(\n values: T\n ): RestatePromise<{\n -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;\n }> {\n return CombinatorRestatePromise.fromPromises(\n (p) => Promise.allSettled(p),\n values\n ) as RestatePromise<{\n -readonly [P in keyof T]: PromiseSettledResult<Awaited<T[P]>>;\n }>;\n },\n};\n\n/**\n * Workflow bound durable promise\n *\n * See {@link WorkflowSharedContext}.\n */\nexport type DurablePromise<T> = Promise<T> & {\n /**\n * Returns the value of the promise, if it has been resolved.\n */\n peek(): Promise<T | undefined>;\n\n /**\n * Resolve the promise with the given value.\n * @param value the value to resolve the promise with\n */\n resolve(value?: T): Promise<void>;\n\n /**\n * Reject the promise with the given error message.\n * @param errorMsg the error message to use for rejection.\n */\n reject(errorMsg: string): Promise<void>;\n\n /**\n * Obtain a {@link RestatePromise} variant of this promise.\n */\n get(): RestatePromise<T>;\n};\n\nexport interface WorkflowSharedContext<TState extends TypedState = UntypedState>\n extends ObjectSharedContext<TState>, RestateWorkflowSharedContext {\n /**\n * Create a durable promise that can be resolved or rejected during the workflow execution.\n * The promise is bound to the workflow and will be persisted across suspensions and retries.\n * @example Add some retry options\n * ```ts\n * const wf = restate.workflow({\n * name: \"myWorkflow\",\n * handlers: {\n * run: async (ctx: restate.WorkflowContext) => {\n * // ... do some work ...\n * const payment = await ctx.promise<Payment>(\"payment.succeeded\");\n * // ... do some more work ...\n * },\n *\n * onPaymentSucceeded: async (ctx: restate.WorkflowContext, payment) => {\n * // ... handle payment succeeded ...\n * await ctx.promise(\"payment.succeeded\").resolve(payment);\n * }\n * });\n * ```\n *\n * @param name the name of the durable promise\n */\n promise<T>(name: string, serde?: Serde<T>): DurablePromise<T>;\n}\n\nexport interface WorkflowContext<TState extends TypedState = UntypedState>\n extends\n WorkflowSharedContext<TState>,\n ObjectContext<TState>,\n RestateWorkflowContext {}\n"],"mappings":";;;AA8rBA,MAAa,qBAAqB,EAKhC,WAAW,IAA0B;AACnC,KAAI,CAAC,GAAG,WAAW,MAAM,CACvB,OAAM,IAAI,MACR,sDAAsD,KACvD;AAEH,QAAO;GAEV;AASD,MAAa,iBAAiB;CAU5B,QAAW,OAAsC;AAC/C,MAAI,iBAAiB,uBACnB,QAAO;AAET,SAAO,oBAAoB,QAAQ,MAAM;;CAS3C,OAAkB,QAA0C;AAC1D,SAAO,oBAAoB,OAAO,OAAO;;CAY3C,IACE,QAC6D;AAC7D,SAAO,yBAAyB,cAC7B,MAAM,QAAQ,IAAI,EAAE,EACrB,OACD;;CAcH,KACE,QACoC;AACpC,MAAI,OAAO,WAAW,EACpB,QAAO,oBAAoB,SAAS;AAEtC,SAAO,yBAAyB,cAC7B,MAAM,QAAQ,KAAK,EAAE,EACtB,OACD;;CAaH,IACE,QACoC;AACpC,SAAO,yBAAyB,cAC7B,MAAM,QAAQ,IAAI,EAAE,EACrB,OACD;;CAYH,WACE,QAGC;AACD,SAAO,yBAAyB,cAC7B,MAAM,QAAQ,WAAW,EAAE,EAC5B,OACD;;CAIJ"}
@@ -1,10 +1,10 @@
1
1
  const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
- const require_sdk_shared_core_wasm_bindings = require('./endpoint/handlers/vm/sdk_shared_core_wasm_bindings.cjs');
3
2
  const require_errors = require('./types/errors.cjs');
4
- const require_rpc = require('./types/rpc.cjs');
5
- const require_rand = require('./utils/rand.cjs');
6
3
  const require_completable_promise = require('./utils/completable_promise.cjs');
7
4
  const require_promises = require('./promises.cjs');
5
+ const require_rpc = require('./types/rpc.cjs');
6
+ const require_sdk_shared_core_wasm_bindings = require('./endpoint/handlers/vm/sdk_shared_core_wasm_bindings.cjs');
7
+ const require_rand = require('./utils/rand.cjs');
8
8
  const require_io = require('./io.cjs');
9
9
  let __restatedev_restate_sdk_core = require("@restatedev/restate-sdk-core");
10
10
  __restatedev_restate_sdk_core = require_rolldown_runtime.__toESM(__restatedev_restate_sdk_core);
@@ -23,9 +23,13 @@ var ContextImpl = class {
23
23
  outputPump;
24
24
  runClosuresTracker;
25
25
  promisesExecutor;
26
- defaultSerde;
27
26
  serviceKey;
28
- constructor(coreVm, input, console, handlerKind, vmLogger, invocationRequest, invocationEndPromise, inputReader, outputWriter, journalValueCodec, defaultSerde, asTerminalError) {
27
+ runInterceptor;
28
+ cancellationPromise;
29
+ defaultSerde;
30
+ asTerminalError;
31
+ trackedInvocationIdPromises;
32
+ constructor(coreVm, input, console, handlerKind, vmLogger, invocationRequest, invocationEndPromise, inputReader, outputWriter, journalValueCodec, executionOptions) {
29
33
  this.coreVm = coreVm;
30
34
  this.console = console;
31
35
  this.handlerKind = handlerKind;
@@ -33,18 +37,26 @@ var ContextImpl = class {
33
37
  this.invocationRequest = invocationRequest;
34
38
  this.invocationEndPromise = invocationEndPromise;
35
39
  this.journalValueCodec = journalValueCodec;
36
- this.asTerminalError = asTerminalError;
37
40
  this.rand = new require_rand.RandImpl(input.random_seed, () => {});
38
41
  this.outputPump = new require_io.OutputPump(coreVm, outputWriter);
39
42
  this.runClosuresTracker = new RunClosuresTracker();
40
- this.promisesExecutor = new require_promises.PromisesExecutor(coreVm, new require_io.InputPump(coreVm, inputReader, this.handleInvocationEndError.bind(this)), this.outputPump, this.runClosuresTracker, this.promiseExecutorErrorCallback.bind(this));
41
- this.defaultSerde = defaultSerde ?? __restatedev_restate_sdk_core.serde.json;
43
+ this.promisesExecutor = new require_promises.PromisesExecutor(coreVm, new require_io.InputPump(coreVm, inputReader, this.abortAttempt.bind(this)), this.outputPump, this.runClosuresTracker, this.abortAttempt.bind(this));
42
44
  this.serviceKey = input.key;
45
+ this.runInterceptor = (_name, runner) => runner();
46
+ this.defaultSerde = executionOptions?.defaultSerde ?? __restatedev_restate_sdk_core.serde.json;
47
+ this.asTerminalError = executionOptions?.asTerminalError;
48
+ this.trackedInvocationIdPromises = executionOptions?.explicitCancellation ? [] : void 0;
49
+ }
50
+ setRunInterceptor(interceptor) {
51
+ this.runInterceptor = interceptor;
43
52
  }
44
53
  isProcessing() {
45
54
  return this.coreVm.is_processing();
46
55
  }
47
56
  cancel(invocationId) {
57
+ this._cancel(invocationId);
58
+ }
59
+ _cancel(invocationId) {
48
60
  this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.CancelInvocation, () => {}, (vm) => vm.sys_cancel_invocation(invocationId));
49
61
  }
50
62
  attach(invocationId, serde$1) {
@@ -83,17 +95,18 @@ var ContextImpl = class {
83
95
  try {
84
96
  parameter = this.journalValueCodec.encode(requestSerde.serialize(call.parameter));
85
97
  } catch (e) {
86
- this.handleInvocationEndError(e, (vm, error) => vm.notify_error_for_next_command(error.message, error.stack, require_sdk_shared_core_wasm_bindings.WasmCommandType.Call));
87
- return new require_promises.InvocationPendingPromise(this);
98
+ this.abortAttempt(e, require_sdk_shared_core_wasm_bindings.WasmCommandType.Call);
99
+ return Object.assign(require_promises.ConstRestatePromise.pending(), { invocationId: require_promises.pendingPromise() });
88
100
  }
89
101
  try {
90
102
  const call_handles = this.coreVm.sys_call(call.service, call.method, parameter, call.key, call.headers ? Object.entries(call.headers).map(([key, value]) => new require_sdk_shared_core_wasm_bindings.WasmHeader(key, value)) : [], call.idempotencyKey, call.name);
91
103
  const commandIndex = this.coreVm.last_command_index();
92
- const invocationIdPromise = new require_promises.RestateSinglePromise(this, call_handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Call, commandIndex, InvocationIdCompleter));
93
- return new require_promises.RestateInvocationPromise(this, call_handles.call_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Call, commandIndex, SuccessWithSerde(responseSerde, this.journalValueCodec), Failure), invocationIdPromise);
104
+ const invocationIdPromise = new require_promises.SingleRestatePromise(this, call_handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Call, commandIndex, InvocationIdCompleter));
105
+ this.trackedInvocationIdPromises?.push(invocationIdPromise);
106
+ return new require_promises.InvocationRestatePromise(this, call_handles.call_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Call, commandIndex, SuccessWithSerde(responseSerde, this.journalValueCodec), Failure), invocationIdPromise);
94
107
  } catch (e) {
95
- this.handleInvocationEndError(e);
96
- return new require_promises.InvocationPendingPromise(this);
108
+ this.abortAttempt(e);
109
+ return Object.assign(require_promises.ConstRestatePromise.pending(), { invocationId: require_promises.pendingPromise() });
97
110
  }
98
111
  }
99
112
  genericSend(send) {
@@ -102,16 +115,16 @@ var ContextImpl = class {
102
115
  try {
103
116
  parameter = this.journalValueCodec.encode(requestSerde.serialize(send.parameter));
104
117
  } catch (e) {
105
- this.handleInvocationEndError(e, (vm, error) => vm.notify_error_for_next_command(error.message, error.stack, require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall));
106
- return new require_promises.InvocationPendingPromise(this);
118
+ this.abortAttempt(e, require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall);
119
+ return Object.assign(require_promises.ConstRestatePromise.pending(), { invocationId: require_promises.pendingPromise() });
107
120
  }
108
121
  try {
109
122
  const delay = send.delay !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(send.delay) : void 0;
110
123
  const handles = this.coreVm.sys_send(send.service, send.method, parameter, send.key, send.headers ? Object.entries(send.headers).map(([key, value]) => new require_sdk_shared_core_wasm_bindings.WasmHeader(key, value)) : [], delay !== void 0 && delay > 0 ? BigInt(delay) : void 0, send.idempotencyKey, send.name);
111
124
  const commandIndex = this.coreVm.last_command_index();
112
- return { invocationId: new require_promises.RestateSinglePromise(this, handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall, commandIndex, InvocationIdCompleter)) };
125
+ return { invocationId: new require_promises.SingleRestatePromise(this, handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall, commandIndex, InvocationIdCompleter)) };
113
126
  } catch (e) {
114
- this.handleInvocationEndError(e);
127
+ this.abortAttempt(e);
115
128
  return { invocationId: require_promises.pendingPromise() };
116
129
  }
117
130
  }
@@ -124,14 +137,14 @@ var ContextImpl = class {
124
137
  workflowClient({ name }, key) {
125
138
  return require_rpc.makeRpcCallProxy((call) => this.genericCall(call), this.defaultSerde, name, key);
126
139
  }
127
- serviceSendClient({ name }, opts) {
128
- return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, void 0, opts?.delay);
140
+ serviceSendClient({ name }) {
141
+ return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, void 0);
129
142
  }
130
- objectSendClient({ name }, key, opts) {
131
- return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, key, opts?.delay);
143
+ objectSendClient({ name }, key) {
144
+ return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, key);
132
145
  }
133
- workflowSendClient({ name }, key, opts) {
134
- return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, key, opts?.delay);
146
+ workflowSendClient({ name }, key) {
147
+ return require_rpc.makeRpcSendProxy((send) => this.genericSend(send), this.defaultSerde, name, key);
135
148
  }
136
149
  run(nameOrAction, actionSecondParameter, options) {
137
150
  const { name, action } = unpackRunParameters(nameOrAction, actionSecondParameter);
@@ -140,8 +153,8 @@ var ContextImpl = class {
140
153
  try {
141
154
  handle = this.coreVm.sys_run(name ?? "");
142
155
  } catch (e) {
143
- this.handleInvocationEndError(e);
144
- return new require_promises.RestatePendingPromise(this);
156
+ this.abortAttempt(e);
157
+ return require_promises.ConstRestatePromise.pending();
145
158
  }
146
159
  const commandIndex = this.coreVm.last_command_index();
147
160
  const doRun = async () => {
@@ -149,7 +162,9 @@ var ContextImpl = class {
149
162
  let res;
150
163
  let err;
151
164
  try {
152
- res = await action();
165
+ await this.runInterceptor(name ?? "", async () => {
166
+ res = await action();
167
+ });
153
168
  } catch (e) {
154
169
  err = require_errors.ensureError(e, this.asTerminalError);
155
170
  }
@@ -163,22 +178,17 @@ var ContextImpl = class {
163
178
  value
164
179
  }))
165
180
  });
166
- else if (err instanceof require_errors.RetryableError) {
167
- const maxRetryDuration = options?.maxRetryDuration ?? options?.maxRetryDurationMillis;
168
- this.coreVm.propose_run_completion_failure_transient_with_delay_override(handle, err.message, err.stack, BigInt(attemptDuration), err.retryAfter !== void 0 ? BigInt((0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(err.retryAfter)) : void 0, options?.maxRetryAttempts, maxRetryDuration !== void 0 ? BigInt((0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(maxRetryDuration)) : void 0);
169
- } else {
181
+ else if (err instanceof require_errors.RetryableError) this.coreVm.propose_run_completion_failure_transient_with_delay_override(handle, err.message, err.stack, BigInt(attemptDuration), err.retryAfter !== void 0 ? BigInt((0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(err.retryAfter)) : void 0, options?.maxRetryAttempts, options?.maxRetryDuration !== void 0 ? BigInt((0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.maxRetryDuration)) : void 0);
182
+ else {
170
183
  this.vmLogger.warn(`Error when processing ctx.run '${name}'.\n`, err);
171
184
  let retryPolicy;
172
- if (options?.retryIntervalFactor !== void 0 || options?.maxRetryAttempts !== void 0 || options?.initialRetryInterval !== void 0 || options?.initialRetryIntervalMillis !== void 0 || options?.maxRetryDuration !== void 0 || options?.maxRetryDurationMillis !== void 0 || options?.maxRetryInterval !== void 0 || options?.maxRetryIntervalMillis !== void 0) {
173
- const maxRetryDuration = options?.maxRetryDuration ?? options?.maxRetryDurationMillis;
174
- retryPolicy = {
175
- factor: options?.retryIntervalFactor ?? 2,
176
- initial_interval: (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.initialRetryInterval ?? options?.initialRetryIntervalMillis ?? 50),
177
- max_attempts: options?.maxRetryAttempts,
178
- max_duration: maxRetryDuration === void 0 ? void 0 : (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(maxRetryDuration),
179
- max_interval: (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.maxRetryInterval ?? options?.maxRetryIntervalMillis ?? { seconds: 10 })
180
- };
181
- }
185
+ if (options?.retryIntervalFactor !== void 0 || options?.maxRetryAttempts !== void 0 || options?.initialRetryInterval !== void 0 || options?.maxRetryDuration !== void 0 || options?.maxRetryInterval !== void 0) retryPolicy = {
186
+ factor: options?.retryIntervalFactor ?? 2,
187
+ initial_interval: (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.initialRetryInterval ?? 50),
188
+ max_attempts: options?.maxRetryAttempts,
189
+ max_duration: options?.maxRetryDuration === void 0 ? void 0 : (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.maxRetryDuration),
190
+ max_interval: (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(options?.maxRetryInterval ?? { seconds: 10 })
191
+ };
182
192
  this.coreVm.propose_run_completion_failure_transient(handle, err.message, err.stack, BigInt(attemptDuration), retryPolicy);
183
193
  }
184
194
  else {
@@ -187,13 +197,13 @@ var ContextImpl = class {
187
197
  this.coreVm.propose_run_completion_success(handle, encodedRes);
188
198
  }
189
199
  } catch (e) {
190
- this.handleInvocationEndError(e);
200
+ this.abortAttempt(e);
191
201
  return require_promises.pendingPromise();
192
202
  }
193
203
  await this.outputPump.awaitNextProgress();
194
204
  };
195
205
  this.runClosuresTracker.registerRunClosure(handle, doRun);
196
- return new require_promises.RestateSinglePromise(this, handle, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Run, commandIndex, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
206
+ return new require_promises.SingleRestatePromise(this, handle, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Run, commandIndex, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
197
207
  }
198
208
  sleep(duration, name) {
199
209
  return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.Sleep, () => {
@@ -208,15 +218,15 @@ var ContextImpl = class {
208
218
  try {
209
219
  awakeable = this.coreVm.sys_awakeable();
210
220
  } catch (e) {
211
- this.handleInvocationEndError(e);
221
+ this.abortAttempt(e);
212
222
  return {
213
223
  id: "invalid",
214
- promise: new require_promises.RestatePendingPromise(this)
224
+ promise: require_promises.ConstRestatePromise.pending()
215
225
  };
216
226
  }
217
227
  return {
218
228
  id: awakeable.id,
219
- promise: new require_promises.RestateSinglePromise(this, awakeable.handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
229
+ promise: new require_promises.SingleRestatePromise(this, awakeable.handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
220
230
  };
221
231
  }
222
232
  resolveAwakeable(id, payload, serde$1) {
@@ -229,41 +239,53 @@ var ContextImpl = class {
229
239
  }
230
240
  rejectAwakeable(id, reason) {
231
241
  this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.CompleteAwakeable, () => {}, (vm) => {
232
- vm.sys_complete_awakeable_failure(id, {
233
- code: require_errors.UNKNOWN_ERROR_CODE,
234
- message: reason,
235
- metadata: []
236
- });
242
+ vm.sys_complete_awakeable_failure(id, toWasmFailure(reason));
237
243
  });
238
244
  }
245
+ signal(name, serde$1) {
246
+ let handle;
247
+ try {
248
+ handle = this.coreVm.sys_signal(name);
249
+ } catch (e) {
250
+ this.abortAttempt(e);
251
+ return require_promises.ConstRestatePromise.pending();
252
+ }
253
+ return new require_promises.SingleRestatePromise(this, handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure));
254
+ }
255
+ invocation(invocationId) {
256
+ return new InvocationReferenceImpl(this, invocationId);
257
+ }
239
258
  promise(name, serde$1) {
240
259
  return new DurablePromiseImpl(this, name, serde$1);
241
260
  }
242
- static createCombinator(combinatorConstructor, promises) {
243
- const self = require_promises.extractContext(promises[0]);
244
- if (!self) throw new Error("Not a combinable promise");
245
- const castedPromises = [];
246
- for (const promise of promises) {
247
- if (require_promises.extractContext(promise) !== self) {
248
- self.handleInvocationEndError(/* @__PURE__ */ new Error("You're mixing up RestatePromises from different RestateContext. This is not supported."));
249
- return new require_promises.RestatePendingPromise(self);
250
- }
251
- castedPromises.push(promise);
252
- }
253
- return new require_promises.RestateCombinatorPromise(self, combinatorConstructor, castedPromises);
261
+ cancellation() {
262
+ if (!this.cancellationPromise || this.cancellationPromise.isCompleted()) this.cancellationPromise = new require_promises.SingleRestatePromise(this, 1, completeSignalPromiseUsing(VoidAsUndefined));
263
+ return this.cancellationPromise;
264
+ }
265
+ cancelPreviousCalls() {
266
+ if (!this.trackedInvocationIdPromises) return require_promises.ConstRestatePromise.resolve([]);
267
+ return require_promises.CombinatorRestatePromise.fromPromises((p) => Promise.allSettled(p), this.trackedInvocationIdPromises.splice(0)).map((results, failure) => {
268
+ if (failure) throw failure;
269
+ const cancelled = [];
270
+ for (const result of results) if (result.status === "fulfilled") {
271
+ this._cancel(result.value);
272
+ cancelled.push(result.value);
273
+ } else this.console.warn(`Error when trying to get invocation id: ${result.reason}`);
274
+ return cancelled;
275
+ });
254
276
  }
255
277
  processNonCompletableEntry(commandType, prepare, vmCall) {
256
278
  let input;
257
279
  try {
258
280
  input = prepare();
259
281
  } catch (e) {
260
- this.handleInvocationEndError(e, (vm, error) => vm.notify_error_for_next_command(error.message, error.stack, commandType));
282
+ this.abortAttempt(e, commandType);
261
283
  return;
262
284
  }
263
285
  try {
264
286
  vmCall(this.coreVm, input);
265
287
  } catch (e) {
266
- this.handleInvocationEndError(e);
288
+ this.abortAttempt(e);
267
289
  }
268
290
  }
269
291
  processCompletableEntry(commandType, prepare, vmCall, ...completers) {
@@ -271,40 +293,38 @@ var ContextImpl = class {
271
293
  try {
272
294
  input = prepare();
273
295
  } catch (e) {
274
- this.handleInvocationEndError(e, (vm, error) => vm.notify_error_for_next_command(error.message, error.stack, commandType));
275
- return new require_promises.RestatePendingPromise(this);
296
+ this.abortAttempt(e, commandType);
297
+ return require_promises.ConstRestatePromise.pending();
276
298
  }
277
299
  let handle;
278
300
  try {
279
301
  handle = vmCall(this.coreVm, input);
280
302
  } catch (e) {
281
- this.handleInvocationEndError(e);
282
- return new require_promises.RestatePendingPromise(this);
303
+ this.abortAttempt(e);
304
+ return require_promises.ConstRestatePromise.pending();
283
305
  }
284
306
  const commandIndex = this.coreVm.last_command_index();
285
- return new require_promises.RestateSinglePromise(this, handle, completeCommandPromiseUsing(commandType, commandIndex, ...completers));
286
- }
287
- promiseExecutorErrorCallback(e) {
288
- if (e instanceof AsyncCompleterError) {
289
- const cause = require_errors.ensureError(e.cause);
290
- require_errors.logError(this.vmLogger, e.cause);
291
- this.coreVm.notify_error_for_specific_command(cause.message, cause.stack, e.commandType, e.commandIndex, null);
292
- } else {
293
- const error = require_errors.ensureError(e);
294
- require_errors.logError(this.vmLogger, error);
295
- if (!(error instanceof require_errors.RestateError)) this.coreVm.notify_error(error.message, error.stack);
296
- }
297
- this.invocationEndPromise.resolve();
307
+ return new require_promises.SingleRestatePromise(this, handle, completeCommandPromiseUsing(commandType, commandIndex, ...completers));
298
308
  }
299
- handleInvocationEndError(e, notify_vm_error = (vm, error$1) => {
300
- vm.notify_error(error$1.message, error$1.stack);
301
- }) {
302
- const error = require_errors.ensureError(e);
303
- require_errors.logError(this.vmLogger, error);
304
- notify_vm_error(this.coreVm, error);
305
- this.invocationEndPromise.resolve();
309
+ abortAttempt(e, commandType) {
310
+ this.invocationEndPromise.reject(commandType !== void 0 ? new CommandError(e, commandType) : require_errors.ensureError(e));
306
311
  }
307
312
  };
313
+ function toWasmFailure(reason) {
314
+ if (typeof reason === "string") return {
315
+ code: require_errors.UNKNOWN_ERROR_CODE,
316
+ message: reason,
317
+ metadata: []
318
+ };
319
+ return {
320
+ code: reason.code,
321
+ message: reason.message,
322
+ metadata: Object.entries(reason.metadata ?? {}).map(([key, value]) => ({
323
+ key,
324
+ value
325
+ }))
326
+ };
327
+ }
308
328
  function unpackRunParameters(a, b) {
309
329
  if (typeof a === "string") {
310
330
  if (typeof b !== "function") throw new TypeError("");
@@ -317,6 +337,38 @@ function unpackRunParameters(a, b) {
317
337
  if (b) throw new TypeError("unexpected a function as a second parameter.");
318
338
  return { action: a };
319
339
  }
340
+ var InvocationReferenceImpl = class {
341
+ constructor(ctx, invocationId) {
342
+ this.ctx = ctx;
343
+ this.invocationId = invocationId;
344
+ }
345
+ signal(name, serde$1) {
346
+ return new SignalReferenceImpl(this.ctx, this.invocationId, name, serde$1);
347
+ }
348
+ cancel() {
349
+ this.ctx.cancel(this.invocationId);
350
+ }
351
+ attach(serde$1) {
352
+ return this.ctx.attach(this.invocationId, serde$1);
353
+ }
354
+ };
355
+ var SignalReferenceImpl = class {
356
+ serde;
357
+ constructor(ctx, invocationId, name, serde$1) {
358
+ this.ctx = ctx;
359
+ this.invocationId = invocationId;
360
+ this.name = name;
361
+ this.serde = serde$1 ?? this.ctx.defaultSerde;
362
+ }
363
+ resolve(payload) {
364
+ this.ctx.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.SendSignal, () => this.ctx.journalValueCodec.encode(this.serde.serialize(payload)), (vm, bytes) => vm.sys_complete_signal_success(this.invocationId, this.name, bytes));
365
+ }
366
+ reject(reason) {
367
+ this.ctx.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.SendSignal, () => {}, (vm) => {
368
+ vm.sys_complete_signal_failure(this.invocationId, this.name, toWasmFailure(reason));
369
+ });
370
+ }
371
+ };
320
372
  var DurablePromiseImpl = class {
321
373
  serde;
322
374
  constructor(ctx, name, serde$1) {
@@ -376,19 +428,25 @@ var RunClosuresTracker = class {
376
428
  }
377
429
  }
378
430
  };
379
- var AsyncCompleterError = class {
431
+ var CommandError = class extends Error {
380
432
  constructor(cause, commandType, commandIndex) {
433
+ const msg = cause instanceof Error ? cause.message : String(cause);
434
+ super(msg, { cause });
381
435
  this.cause = cause;
382
436
  this.commandType = commandType;
383
437
  this.commandIndex = commandIndex;
384
438
  }
439
+ /** True when the error is for a specific command that exists in the journal. */
440
+ get hasCommandIndex() {
441
+ return this.commandIndex !== void 0;
442
+ }
385
443
  };
386
444
  function completeCommandPromiseUsing(commandType, commandIndex, ...completers) {
387
445
  return async (value, prom) => {
388
446
  try {
389
447
  for (const completer of completers) if (await completer(value, prom)) return;
390
448
  } catch (e) {
391
- throw new AsyncCompleterError(e, commandType, commandIndex);
449
+ throw new CommandError(e, commandType, commandIndex);
392
450
  }
393
451
  throw new Error(`Unexpected variant in async result: ${JSON.stringify(value)}`);
394
452
  };
@@ -455,4 +513,5 @@ const InvocationIdCompleter = (value, prom) => {
455
513
  };
456
514
 
457
515
  //#endregion
516
+ exports.CommandError = CommandError;
458
517
  exports.ContextImpl = ContextImpl;
@@ -0,0 +1,8 @@
1
+ import "./types/errors.cjs";
2
+ import "./types/rpc.cjs";
3
+ import "./context.cjs";
4
+ import "./internal.cjs";
5
+ import "./endpoint/components.cjs";
6
+ import "./endpoint/handlers/types.cjs";
7
+ import "./promises.cjs";
8
+ import { Duration, JournalValueCodec, Serde, Service, ServiceDefinitionFrom, VirtualObject, VirtualObjectDefinitionFrom, Workflow, WorkflowDefinitionFrom } from "@restatedev/restate-sdk-core";