@restatedev/restate-sdk-cloudflare-workers 1.17.2 → 1.17.4

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 (62) hide show
  1. package/dist/common_api.d.cts +1 -1
  2. package/dist/common_api.d.cts.map +1 -1
  3. package/dist/common_api.d.ts +1 -1
  4. package/dist/common_api.d.ts.map +1 -1
  5. package/dist/common_api.js.map +1 -1
  6. package/dist/context.d.cts +25 -8
  7. package/dist/context.d.cts.map +1 -1
  8. package/dist/context.d.ts +25 -8
  9. package/dist/context.d.ts.map +1 -1
  10. package/dist/context.js.map +1 -1
  11. package/dist/context_impl.cjs +84 -27
  12. package/dist/context_impl.js +84 -27
  13. package/dist/context_impl.js.map +1 -1
  14. package/dist/endpoint/components.cjs +11 -1
  15. package/dist/endpoint/components.js +11 -2
  16. package/dist/endpoint/components.js.map +1 -1
  17. package/dist/endpoint/endpoint.cjs +1 -1
  18. package/dist/endpoint/endpoint.js +1 -1
  19. package/dist/endpoint/endpoint.js.map +1 -1
  20. package/dist/endpoint/handlers/generic.cjs +1 -1
  21. package/dist/endpoint/handlers/generic.js +1 -1
  22. package/dist/endpoint/handlers/generic.js.map +1 -1
  23. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings.d.ts +9 -7
  24. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings_bg.js +54 -12
  25. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings_bg.wasm +0 -0
  26. package/dist/endpoint/handlers/vm/sdk_shared_core_wasm_bindings_bg.wasm.d.ts +9 -7
  27. package/dist/fetch.d.cts +2 -2
  28. package/dist/fetch.d.ts +2 -2
  29. package/dist/index.d.cts +2 -2
  30. package/dist/index.d.ts +2 -2
  31. package/dist/internal.cjs +46 -1
  32. package/dist/internal.d.cts +96 -4
  33. package/dist/internal.d.cts.map +1 -1
  34. package/dist/internal.d.ts +96 -4
  35. package/dist/internal.d.ts.map +1 -1
  36. package/dist/internal.js +46 -1
  37. package/dist/internal.js.map +1 -1
  38. package/dist/lambda.d.cts +2 -2
  39. package/dist/lambda.d.ts +2 -2
  40. package/dist/node.d.cts +2 -2
  41. package/dist/node.d.ts +2 -2
  42. package/dist/package.cjs +1 -1
  43. package/dist/package.js +1 -1
  44. package/dist/package.js.map +1 -1
  45. package/dist/promises.cjs +5 -1
  46. package/dist/promises.d.cts.map +1 -1
  47. package/dist/promises.d.ts.map +1 -1
  48. package/dist/promises.js +5 -1
  49. package/dist/promises.js.map +1 -1
  50. package/dist/types/errors.cjs +2 -2
  51. package/dist/types/errors.d.cts +1 -1
  52. package/dist/types/errors.d.cts.map +1 -1
  53. package/dist/types/errors.d.ts +1 -1
  54. package/dist/types/errors.d.ts.map +1 -1
  55. package/dist/types/errors.js +2 -2
  56. package/dist/types/errors.js.map +1 -1
  57. package/dist/types/rpc.d.cts +3 -11
  58. package/dist/types/rpc.d.cts.map +1 -1
  59. package/dist/types/rpc.d.ts +3 -11
  60. package/dist/types/rpc.d.ts.map +1 -1
  61. package/dist/types/rpc.js.map +1 -1
  62. package/package.json +3 -3
@@ -23,6 +23,7 @@ var ContextImpl = class {
23
23
  outputPump;
24
24
  inputPump;
25
25
  runClosuresTracker;
26
+ ephemeralRequestTracker;
26
27
  promisesExecutor;
27
28
  serviceKey;
28
29
  runInterceptor;
@@ -42,8 +43,9 @@ var ContextImpl = class {
42
43
  this.outputPump = new require_io.OutputPump(coreVm, outputWriter);
43
44
  const externalProgressChannel = new require_external_progress_channel.ExternalProgressChannel();
44
45
  this.runClosuresTracker = new RunClosuresTracker(externalProgressChannel);
46
+ this.ephemeralRequestTracker = new EphemeralRequestTracker(externalProgressChannel);
45
47
  this.inputPump = new require_io.InputPump(coreVm, inputReader, externalProgressChannel, this.abortAttempt.bind(this));
46
- this.promisesExecutor = new require_promises.PromisesExecutor(coreVm, this.outputPump, this.runClosuresTracker, externalProgressChannel, this.abortAttempt.bind(this));
48
+ this.promisesExecutor = new require_promises.PromisesExecutor(coreVm, this.outputPump, this.runClosuresTracker, this.ephemeralRequestTracker, externalProgressChannel, this.abortAttempt.bind(this));
47
49
  this.serviceKey = input.key;
48
50
  this.runInterceptor = (_name, runner) => runner();
49
51
  this.defaultSerde = executionOptions?.defaultSerde ?? __restatedev_restate_sdk_core.serde.json;
@@ -76,20 +78,40 @@ var ContextImpl = class {
76
78
  request() {
77
79
  return this.invocationRequest;
78
80
  }
79
- get(name, serde$1) {
80
- return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.GetState, () => {}, (vm) => vm.sys_get_state(name), VoidAsNull, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec));
81
+ get(name, serde$1, options) {
82
+ return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.GetState, () => {}, (vm) => vm.sys_get_state(name, options?.name), VoidAsNull, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec));
83
+ }
84
+ getProject(name, projection, options) {
85
+ const stateSerde = options?.serde ?? this.defaultSerde;
86
+ return this.run(`project:${name}`, async () => projection(await this.ephemeralStateGet(name, stateSerde)), { serde: options?.resultSerde ?? options?.serde });
87
+ }
88
+ /**
89
+ * Get the state value with an ephemeral command: neither the command nor its notification are recorded in the journal.
90
+ * Because of this, the returned value MUST be used only within a run closure.
91
+ *
92
+ * The returned promise is completed only if a Restate's DurablePromise is being awaited.
93
+ */
94
+ ephemeralStateGet(name, serde$1) {
95
+ let ephemeralCompletionId;
96
+ try {
97
+ ephemeralCompletionId = this.coreVm.ephemeral_state_get(name);
98
+ } catch (e) {
99
+ this.abortAttempt(e);
100
+ return require_promises.pendingPromise();
101
+ }
102
+ return this.ephemeralRequestTracker.register(ephemeralCompletionId, completeUsing({}, VoidAsNull, SuccessWithSerde(serde$1, this.journalValueCodec)));
81
103
  }
82
- stateKeys() {
83
- return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.GetStateKeys, () => {}, (vm) => vm.sys_get_state_keys(), StateKeys);
104
+ stateKeys(options) {
105
+ return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.GetStateKeys, () => {}, (vm) => vm.sys_get_state_keys(options?.name), StateKeys);
84
106
  }
85
- set(name, value, serde$1) {
86
- this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.SetState, () => this.journalValueCodec.encode((serde$1 ?? this.defaultSerde).serialize(value)), (vm, bytes) => vm.sys_set_state(name, bytes));
107
+ set(name, value, serde$1, options) {
108
+ this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.SetState, () => this.journalValueCodec.encode((serde$1 ?? this.defaultSerde).serialize(value)), (vm, bytes) => vm.sys_set_state(name, bytes, options?.name));
87
109
  }
88
- clear(name) {
89
- this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.ClearState, () => {}, (vm) => vm.sys_clear_state(name));
110
+ clear(name, options) {
111
+ this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.ClearState, () => {}, (vm) => vm.sys_clear_state(name, options?.name));
90
112
  }
91
- clearAll() {
92
- this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.ClearAllState, () => {}, (vm) => vm.sys_clear_all_state());
113
+ clearAll(options) {
114
+ this.processNonCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.ClearAllState, () => {}, (vm) => vm.sys_clear_all_state(options?.name));
93
115
  }
94
116
  genericCall(call) {
95
117
  const requestSerde = call.inputSerde ?? __restatedev_restate_sdk_core.serde.binary;
@@ -104,9 +126,15 @@ var ContextImpl = class {
104
126
  try {
105
127
  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.scope, call.limitKey, call.name);
106
128
  const commandIndex = this.coreVm.last_command_index();
107
- const invocationIdPromise = new require_promises.SingleRestatePromise(this, call_handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Call, commandIndex, InvocationIdCompleter));
129
+ const invocationIdPromise = new require_promises.SingleRestatePromise(this, call_handles.invocation_id_completion_id, completeUsing({ command: {
130
+ type: require_sdk_shared_core_wasm_bindings.WasmCommandType.Call,
131
+ index: commandIndex
132
+ } }, InvocationIdCompleter));
108
133
  this.trackedInvocationIdPromises?.push(invocationIdPromise);
109
- 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);
134
+ return new require_promises.InvocationRestatePromise(this, call_handles.call_completion_id, completeUsing({ command: {
135
+ type: require_sdk_shared_core_wasm_bindings.WasmCommandType.Call,
136
+ index: commandIndex
137
+ } }, SuccessWithSerde(responseSerde, this.journalValueCodec), Failure), invocationIdPromise);
110
138
  } catch (e) {
111
139
  this.abortAttempt(e);
112
140
  return Object.assign(require_promises.ConstRestatePromise.pending(), { invocationId: require_promises.pendingPromise() });
@@ -125,7 +153,10 @@ var ContextImpl = class {
125
153
  const delay = send.delay !== void 0 ? (0, __restatedev_restate_sdk_core.millisOrDurationToMillis)(send.delay) : void 0;
126
154
  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.scope, send.limitKey, send.name);
127
155
  const commandIndex = this.coreVm.last_command_index();
128
- return { invocationId: new require_promises.SingleRestatePromise(this, handles.invocation_id_completion_id, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall, commandIndex, InvocationIdCompleter)) };
156
+ return { invocationId: new require_promises.SingleRestatePromise(this, handles.invocation_id_completion_id, completeUsing({ command: {
157
+ type: require_sdk_shared_core_wasm_bindings.WasmCommandType.OneWayCall,
158
+ index: commandIndex
159
+ } }, InvocationIdCompleter)) };
129
160
  } catch (e) {
130
161
  this.abortAttempt(e);
131
162
  return { invocationId: require_promises.pendingPromise() };
@@ -228,7 +259,10 @@ var ContextImpl = class {
228
259
  };
229
260
  this.runClosuresTracker.registerRunClosure(handle, doRun);
230
261
  }
231
- return new require_promises.SingleRestatePromise(this, handle, completeCommandPromiseUsing(require_sdk_shared_core_wasm_bindings.WasmCommandType.Run, commandIndex, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
262
+ return new require_promises.SingleRestatePromise(this, handle, completeUsing({ command: {
263
+ type: require_sdk_shared_core_wasm_bindings.WasmCommandType.Run,
264
+ index: commandIndex
265
+ } }, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
232
266
  }
233
267
  sleep(duration, name) {
234
268
  return this.processCompletableEntry(require_sdk_shared_core_wasm_bindings.WasmCommandType.Sleep, () => {
@@ -251,7 +285,7 @@ var ContextImpl = class {
251
285
  }
252
286
  return {
253
287
  id: awakeable.id,
254
- promise: new require_promises.SingleRestatePromise(this, awakeable.handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
288
+ promise: new require_promises.SingleRestatePromise(this, awakeable.handle, completeUsing({}, VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
255
289
  };
256
290
  }
257
291
  resolveAwakeable(id, payload, serde$1) {
@@ -275,7 +309,7 @@ var ContextImpl = class {
275
309
  this.abortAttempt(e);
276
310
  return require_promises.ConstRestatePromise.pending();
277
311
  }
278
- return new require_promises.SingleRestatePromise(this, handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure));
312
+ return new require_promises.SingleRestatePromise(this, handle, completeUsing({}, VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure));
279
313
  }
280
314
  invocation(invocationId) {
281
315
  return new InvocationReferenceImpl(this, invocationId);
@@ -284,7 +318,7 @@ var ContextImpl = class {
284
318
  return new DurablePromiseImpl(this, name, serde$1);
285
319
  }
286
320
  cancellation() {
287
- if (!this.cancellationPromise || this.cancellationPromise.isCompleted()) this.cancellationPromise = new require_promises.SingleRestatePromise(this, 1, completeSignalPromiseUsing(VoidAsUndefined));
321
+ if (!this.cancellationPromise || this.cancellationPromise.isCompleted()) this.cancellationPromise = new require_promises.SingleRestatePromise(this, 1, completeUsing({}, VoidAsUndefined));
288
322
  return this.cancellationPromise;
289
323
  }
290
324
  cancelPreviousCalls() {
@@ -329,7 +363,10 @@ var ContextImpl = class {
329
363
  return require_promises.ConstRestatePromise.pending();
330
364
  }
331
365
  const commandIndex = this.coreVm.last_command_index();
332
- return new require_promises.SingleRestatePromise(this, handle, completeCommandPromiseUsing(commandType, commandIndex, ...completers));
366
+ return new require_promises.SingleRestatePromise(this, handle, completeUsing({ command: {
367
+ type: commandType,
368
+ index: commandIndex
369
+ } }, ...completers));
333
370
  }
334
371
  abortAttempt(e, commandType) {
335
372
  this.invocationEndPromise.reject(commandType !== void 0 ? new CommandError(e, commandType) : require_errors.ensureError(e));
@@ -428,6 +465,31 @@ var DurablePromiseImpl = class {
428
465
  }), VoidAsUndefined, Failure);
429
466
  }
430
467
  };
468
+ var EphemeralRequestTracker = class {
469
+ pending = /* @__PURE__ */ new Map();
470
+ constructor(channel) {
471
+ this.channel = channel;
472
+ }
473
+ register(completionId, completer) {
474
+ const prom = Promise.withResolvers();
475
+ this.pending.set(completionId, async (value) => {
476
+ try {
477
+ await completer(value, prom);
478
+ } catch (e) {
479
+ prom.reject(e);
480
+ }
481
+ });
482
+ this.channel.signal();
483
+ return prom.promise;
484
+ }
485
+ async complete(completionId, value) {
486
+ const complete = this.pending.get(completionId);
487
+ if (complete === void 0) throw new Error(`Ephemeral request with completion id ${completionId} doesn't exist`);
488
+ if (value === "NotReady") throw new Error(`Notification for ephemeral request with completion id ${completionId} is not ready. This is unexpected behavior.`);
489
+ this.pending.delete(completionId);
490
+ await complete(value);
491
+ }
492
+ };
431
493
  var RunClosuresTracker = class {
432
494
  runsToExecute = /* @__PURE__ */ new Map();
433
495
  constructor(channel) {
@@ -456,22 +518,17 @@ var CommandError = class extends Error {
456
518
  return this.commandIndex !== void 0;
457
519
  }
458
520
  };
459
- function completeCommandPromiseUsing(commandType, commandIndex, ...completers) {
521
+ function completeUsing(meta, ...completers) {
460
522
  return async (value, prom) => {
461
523
  try {
462
524
  for (const completer of completers) if (await completer(value, prom)) return;
463
525
  } catch (e) {
464
- throw new CommandError(e, commandType, commandIndex);
526
+ if (meta.command !== void 0) throw new CommandError(e, meta.command.type, meta.command.index);
527
+ throw e;
465
528
  }
466
529
  throw new Error(`Unexpected variant in async result: ${JSON.stringify(value)}`);
467
530
  };
468
531
  }
469
- function completeSignalPromiseUsing(...completers) {
470
- return async (value, prom) => {
471
- for (const completer of completers) if (await completer(value, prom)) return;
472
- throw new Error(`Unexpected variant in async result: ${JSON.stringify(value)}`);
473
- };
474
- }
475
532
  const VoidAsNull = (value, prom) => {
476
533
  if (value === "Empty") {
477
534
  prom.resolve(null);
@@ -21,6 +21,7 @@ var ContextImpl = class {
21
21
  outputPump;
22
22
  inputPump;
23
23
  runClosuresTracker;
24
+ ephemeralRequestTracker;
24
25
  promisesExecutor;
25
26
  serviceKey;
26
27
  runInterceptor;
@@ -40,8 +41,9 @@ var ContextImpl = class {
40
41
  this.outputPump = new OutputPump(coreVm, outputWriter);
41
42
  const externalProgressChannel = new ExternalProgressChannel();
42
43
  this.runClosuresTracker = new RunClosuresTracker(externalProgressChannel);
44
+ this.ephemeralRequestTracker = new EphemeralRequestTracker(externalProgressChannel);
43
45
  this.inputPump = new InputPump(coreVm, inputReader, externalProgressChannel, this.abortAttempt.bind(this));
44
- this.promisesExecutor = new PromisesExecutor(coreVm, this.outputPump, this.runClosuresTracker, externalProgressChannel, this.abortAttempt.bind(this));
46
+ this.promisesExecutor = new PromisesExecutor(coreVm, this.outputPump, this.runClosuresTracker, this.ephemeralRequestTracker, externalProgressChannel, this.abortAttempt.bind(this));
45
47
  this.serviceKey = input.key;
46
48
  this.runInterceptor = (_name, runner) => runner();
47
49
  this.defaultSerde = executionOptions?.defaultSerde ?? serde.json;
@@ -74,20 +76,40 @@ var ContextImpl = class {
74
76
  request() {
75
77
  return this.invocationRequest;
76
78
  }
77
- get(name, serde$1) {
78
- return this.processCompletableEntry(WasmCommandType.GetState, () => {}, (vm) => vm.sys_get_state(name), VoidAsNull, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec));
79
+ get(name, serde$1, options) {
80
+ return this.processCompletableEntry(WasmCommandType.GetState, () => {}, (vm) => vm.sys_get_state(name, options?.name), VoidAsNull, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec));
81
+ }
82
+ getProject(name, projection, options) {
83
+ const stateSerde = options?.serde ?? this.defaultSerde;
84
+ return this.run(`project:${name}`, async () => projection(await this.ephemeralStateGet(name, stateSerde)), { serde: options?.resultSerde ?? options?.serde });
85
+ }
86
+ /**
87
+ * Get the state value with an ephemeral command: neither the command nor its notification are recorded in the journal.
88
+ * Because of this, the returned value MUST be used only within a run closure.
89
+ *
90
+ * The returned promise is completed only if a Restate's DurablePromise is being awaited.
91
+ */
92
+ ephemeralStateGet(name, serde$1) {
93
+ let ephemeralCompletionId;
94
+ try {
95
+ ephemeralCompletionId = this.coreVm.ephemeral_state_get(name);
96
+ } catch (e) {
97
+ this.abortAttempt(e);
98
+ return pendingPromise();
99
+ }
100
+ return this.ephemeralRequestTracker.register(ephemeralCompletionId, completeUsing({}, VoidAsNull, SuccessWithSerde(serde$1, this.journalValueCodec)));
79
101
  }
80
- stateKeys() {
81
- return this.processCompletableEntry(WasmCommandType.GetStateKeys, () => {}, (vm) => vm.sys_get_state_keys(), StateKeys);
102
+ stateKeys(options) {
103
+ return this.processCompletableEntry(WasmCommandType.GetStateKeys, () => {}, (vm) => vm.sys_get_state_keys(options?.name), StateKeys);
82
104
  }
83
- set(name, value, serde$1) {
84
- this.processNonCompletableEntry(WasmCommandType.SetState, () => this.journalValueCodec.encode((serde$1 ?? this.defaultSerde).serialize(value)), (vm, bytes) => vm.sys_set_state(name, bytes));
105
+ set(name, value, serde$1, options) {
106
+ this.processNonCompletableEntry(WasmCommandType.SetState, () => this.journalValueCodec.encode((serde$1 ?? this.defaultSerde).serialize(value)), (vm, bytes) => vm.sys_set_state(name, bytes, options?.name));
85
107
  }
86
- clear(name) {
87
- this.processNonCompletableEntry(WasmCommandType.ClearState, () => {}, (vm) => vm.sys_clear_state(name));
108
+ clear(name, options) {
109
+ this.processNonCompletableEntry(WasmCommandType.ClearState, () => {}, (vm) => vm.sys_clear_state(name, options?.name));
88
110
  }
89
- clearAll() {
90
- this.processNonCompletableEntry(WasmCommandType.ClearAllState, () => {}, (vm) => vm.sys_clear_all_state());
111
+ clearAll(options) {
112
+ this.processNonCompletableEntry(WasmCommandType.ClearAllState, () => {}, (vm) => vm.sys_clear_all_state(options?.name));
91
113
  }
92
114
  genericCall(call) {
93
115
  const requestSerde = call.inputSerde ?? serde.binary;
@@ -102,9 +124,15 @@ var ContextImpl = class {
102
124
  try {
103
125
  const call_handles = this.coreVm.sys_call(call.service, call.method, parameter, call.key, call.headers ? Object.entries(call.headers).map(([key, value]) => new WasmHeader(key, value)) : [], call.idempotencyKey, call.scope, call.limitKey, call.name);
104
126
  const commandIndex = this.coreVm.last_command_index();
105
- const invocationIdPromise = new SingleRestatePromise(this, call_handles.invocation_id_completion_id, completeCommandPromiseUsing(WasmCommandType.Call, commandIndex, InvocationIdCompleter));
127
+ const invocationIdPromise = new SingleRestatePromise(this, call_handles.invocation_id_completion_id, completeUsing({ command: {
128
+ type: WasmCommandType.Call,
129
+ index: commandIndex
130
+ } }, InvocationIdCompleter));
106
131
  this.trackedInvocationIdPromises?.push(invocationIdPromise);
107
- return new InvocationRestatePromise(this, call_handles.call_completion_id, completeCommandPromiseUsing(WasmCommandType.Call, commandIndex, SuccessWithSerde(responseSerde, this.journalValueCodec), Failure), invocationIdPromise);
132
+ return new InvocationRestatePromise(this, call_handles.call_completion_id, completeUsing({ command: {
133
+ type: WasmCommandType.Call,
134
+ index: commandIndex
135
+ } }, SuccessWithSerde(responseSerde, this.journalValueCodec), Failure), invocationIdPromise);
108
136
  } catch (e) {
109
137
  this.abortAttempt(e);
110
138
  return Object.assign(ConstRestatePromise.pending(), { invocationId: pendingPromise() });
@@ -123,7 +151,10 @@ var ContextImpl = class {
123
151
  const delay = send.delay !== void 0 ? millisOrDurationToMillis(send.delay) : void 0;
124
152
  const handles = this.coreVm.sys_send(send.service, send.method, parameter, send.key, send.headers ? Object.entries(send.headers).map(([key, value]) => new WasmHeader(key, value)) : [], delay !== void 0 && delay > 0 ? BigInt(delay) : void 0, send.idempotencyKey, send.scope, send.limitKey, send.name);
125
153
  const commandIndex = this.coreVm.last_command_index();
126
- return { invocationId: new SingleRestatePromise(this, handles.invocation_id_completion_id, completeCommandPromiseUsing(WasmCommandType.OneWayCall, commandIndex, InvocationIdCompleter)) };
154
+ return { invocationId: new SingleRestatePromise(this, handles.invocation_id_completion_id, completeUsing({ command: {
155
+ type: WasmCommandType.OneWayCall,
156
+ index: commandIndex
157
+ } }, InvocationIdCompleter)) };
127
158
  } catch (e) {
128
159
  this.abortAttempt(e);
129
160
  return { invocationId: pendingPromise() };
@@ -226,7 +257,10 @@ var ContextImpl = class {
226
257
  };
227
258
  this.runClosuresTracker.registerRunClosure(handle, doRun);
228
259
  }
229
- return new SingleRestatePromise(this, handle, completeCommandPromiseUsing(WasmCommandType.Run, commandIndex, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
260
+ return new SingleRestatePromise(this, handle, completeUsing({ command: {
261
+ type: WasmCommandType.Run,
262
+ index: commandIndex
263
+ } }, SuccessWithSerde(serde$1, this.journalValueCodec), Failure));
230
264
  }
231
265
  sleep(duration, name) {
232
266
  return this.processCompletableEntry(WasmCommandType.Sleep, () => {
@@ -249,7 +283,7 @@ var ContextImpl = class {
249
283
  }
250
284
  return {
251
285
  id: awakeable.id,
252
- promise: new SingleRestatePromise(this, awakeable.handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
286
+ promise: new SingleRestatePromise(this, awakeable.handle, completeUsing({}, VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure))
253
287
  };
254
288
  }
255
289
  resolveAwakeable(id, payload, serde$1) {
@@ -273,7 +307,7 @@ var ContextImpl = class {
273
307
  this.abortAttempt(e);
274
308
  return ConstRestatePromise.pending();
275
309
  }
276
- return new SingleRestatePromise(this, handle, completeSignalPromiseUsing(VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure));
310
+ return new SingleRestatePromise(this, handle, completeUsing({}, VoidAsUndefined, SuccessWithSerde(serde$1 ?? this.defaultSerde, this.journalValueCodec), Failure));
277
311
  }
278
312
  invocation(invocationId) {
279
313
  return new InvocationReferenceImpl(this, invocationId);
@@ -282,7 +316,7 @@ var ContextImpl = class {
282
316
  return new DurablePromiseImpl(this, name, serde$1);
283
317
  }
284
318
  cancellation() {
285
- if (!this.cancellationPromise || this.cancellationPromise.isCompleted()) this.cancellationPromise = new SingleRestatePromise(this, 1, completeSignalPromiseUsing(VoidAsUndefined));
319
+ if (!this.cancellationPromise || this.cancellationPromise.isCompleted()) this.cancellationPromise = new SingleRestatePromise(this, 1, completeUsing({}, VoidAsUndefined));
286
320
  return this.cancellationPromise;
287
321
  }
288
322
  cancelPreviousCalls() {
@@ -327,7 +361,10 @@ var ContextImpl = class {
327
361
  return ConstRestatePromise.pending();
328
362
  }
329
363
  const commandIndex = this.coreVm.last_command_index();
330
- return new SingleRestatePromise(this, handle, completeCommandPromiseUsing(commandType, commandIndex, ...completers));
364
+ return new SingleRestatePromise(this, handle, completeUsing({ command: {
365
+ type: commandType,
366
+ index: commandIndex
367
+ } }, ...completers));
331
368
  }
332
369
  abortAttempt(e, commandType) {
333
370
  this.invocationEndPromise.reject(commandType !== void 0 ? new CommandError(e, commandType) : ensureError(e));
@@ -426,6 +463,31 @@ var DurablePromiseImpl = class {
426
463
  }), VoidAsUndefined, Failure);
427
464
  }
428
465
  };
466
+ var EphemeralRequestTracker = class {
467
+ pending = /* @__PURE__ */ new Map();
468
+ constructor(channel) {
469
+ this.channel = channel;
470
+ }
471
+ register(completionId, completer) {
472
+ const prom = Promise.withResolvers();
473
+ this.pending.set(completionId, async (value) => {
474
+ try {
475
+ await completer(value, prom);
476
+ } catch (e) {
477
+ prom.reject(e);
478
+ }
479
+ });
480
+ this.channel.signal();
481
+ return prom.promise;
482
+ }
483
+ async complete(completionId, value) {
484
+ const complete = this.pending.get(completionId);
485
+ if (complete === void 0) throw new Error(`Ephemeral request with completion id ${completionId} doesn't exist`);
486
+ if (value === "NotReady") throw new Error(`Notification for ephemeral request with completion id ${completionId} is not ready. This is unexpected behavior.`);
487
+ this.pending.delete(completionId);
488
+ await complete(value);
489
+ }
490
+ };
429
491
  var RunClosuresTracker = class {
430
492
  runsToExecute = /* @__PURE__ */ new Map();
431
493
  constructor(channel) {
@@ -454,22 +516,17 @@ var CommandError = class extends Error {
454
516
  return this.commandIndex !== void 0;
455
517
  }
456
518
  };
457
- function completeCommandPromiseUsing(commandType, commandIndex, ...completers) {
519
+ function completeUsing(meta, ...completers) {
458
520
  return async (value, prom) => {
459
521
  try {
460
522
  for (const completer of completers) if (await completer(value, prom)) return;
461
523
  } catch (e) {
462
- throw new CommandError(e, commandType, commandIndex);
524
+ if (meta.command !== void 0) throw new CommandError(e, meta.command.type, meta.command.index);
525
+ throw e;
463
526
  }
464
527
  throw new Error(`Unexpected variant in async result: ${JSON.stringify(value)}`);
465
528
  };
466
529
  }
467
- function completeSignalPromiseUsing(...completers) {
468
- return async (value, prom) => {
469
- for (const completer of completers) if (await completer(value, prom)) return;
470
- throw new Error(`Unexpected variant in async result: ${JSON.stringify(value)}`);
471
- };
472
- }
473
530
  const VoidAsNull = (value, prom) => {
474
531
  if (value === "Empty") {
475
532
  prom.resolve(null);