@mnci/az-durable 0.1.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.
@@ -0,0 +1,1424 @@
1
+ import * as df from 'durable-functions';
2
+ import { RetryOptions } from 'durable-functions';
3
+
4
+ /**
5
+ * Duplicate-name detection for activities and orchestrations.
6
+ *
7
+ * @remarks
8
+ * Activity and orchestration names are **global to the Function App** and are
9
+ * baked into orchestration history in the task hub. Two features registering
10
+ * the same string is a silent misbinding: the second registration wins, and the
11
+ * first feature's calls quietly execute the wrong handler. Nothing surfaces
12
+ * until replay, by which point the history already refers to the wrong thing.
13
+ *
14
+ * Failing loudly at startup is strictly better, so registration throws.
15
+ */ /** Every name registered so far, and where. Module-level, matching the SDK's own global scope. */ var registered = new Map();
16
+ /**
17
+ * Records a name, throwing if it was already taken.
18
+ *
19
+ * @remarks
20
+ * The error names **both** call sites when the stack makes them available. A
21
+ * bare "duplicate name" message sends the reader hunting through a Function App
22
+ * for the other registration, which is the slowest part of fixing this.
23
+ *
24
+ * @param kind - `activity` or `orchestration`, for the message.
25
+ * @param name - The name being registered.
26
+ * @returns Nothing.
27
+ * @throws Error when `name` has already been registered.
28
+ * @typeParam None - this function has no generic type parameters.
29
+ */ function claimName(kind, name) {
30
+ var previous = registered.get(name);
31
+ if (previous !== undefined) {
32
+ throw new Error("Duplicate ".concat(kind, " name '").concat(name, "'. Names are global to the Function App and are ") + 'baked into orchestration history, so two registrations silently misbind.\n' + " first registered at: ".concat(previous, "\n") + " registered again at: ".concat(callSite()));
33
+ }
34
+ registered.set(name, callSite());
35
+ }
36
+ /**
37
+ * The caller's source location, as best the stack can tell.
38
+ *
39
+ * @remarks
40
+ * Best-effort by design: stack formats differ across runtimes, and a bundled or
41
+ * minified app may yield nothing useful. A vague location beats throwing while
42
+ * building an error message, so an unreadable stack degrades to a placeholder
43
+ * rather than failing.
44
+ *
45
+ * @returns A `file:line:col` string, or `<unknown location>`.
46
+ * @throws Never - falls back to a placeholder.
47
+ * @typeParam None - this function has no generic type parameters.
48
+ */ function callSite() {
49
+ var stack = new Error('locate').stack;
50
+ if (stack === undefined) {
51
+ return '<unknown location>';
52
+ }
53
+ // [0] is the Error line, [1] is callSite, [2] is claimName, [3] is
54
+ // defineActivity/defineOrchestration, [4] is the consumer — the one we want.
55
+ var frame = stack.split('\n', 5)[4];
56
+ return frame === undefined ? '<unknown location>' : frame.trim().replace(/^at\s+/, '');
57
+ }
58
+
59
+ function _ts_generator$5(thisArg, body) {
60
+ var f, y, t, _ = {
61
+ label: 0,
62
+ sent: function() {
63
+ if (t[0] & 1) throw t[1];
64
+ return t[1];
65
+ },
66
+ trys: [],
67
+ ops: []
68
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
69
+ return d(g, "next", {
70
+ value: verb(0)
71
+ }), d(g, "throw", {
72
+ value: verb(1)
73
+ }), d(g, "return", {
74
+ value: verb(2)
75
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
76
+ value: function() {
77
+ return this;
78
+ }
79
+ }), g;
80
+ function verb(n) {
81
+ return function(v) {
82
+ return step([
83
+ n,
84
+ v
85
+ ]);
86
+ };
87
+ }
88
+ function step(op) {
89
+ if (f) throw new TypeError("Generator is already executing.");
90
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
91
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
92
+ if (y = 0, t) op = [
93
+ op[0] & 2,
94
+ t.value
95
+ ];
96
+ switch(op[0]){
97
+ case 0:
98
+ case 1:
99
+ t = op;
100
+ break;
101
+ case 4:
102
+ _.label++;
103
+ return {
104
+ value: op[1],
105
+ done: false
106
+ };
107
+ case 5:
108
+ _.label++;
109
+ y = op[1];
110
+ op = [
111
+ 0
112
+ ];
113
+ continue;
114
+ case 7:
115
+ op = _.ops.pop();
116
+ _.trys.pop();
117
+ continue;
118
+ default:
119
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
120
+ _ = 0;
121
+ continue;
122
+ }
123
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
124
+ _.label = op[1];
125
+ break;
126
+ }
127
+ if (op[0] === 6 && _.label < t[1]) {
128
+ _.label = t[1];
129
+ t = op;
130
+ break;
131
+ }
132
+ if (t && _.label < t[2]) {
133
+ _.label = t[2];
134
+ _.ops.push(op);
135
+ break;
136
+ }
137
+ if (t[2]) _.ops.pop();
138
+ _.trys.pop();
139
+ continue;
140
+ }
141
+ op = body.call(thisArg, _);
142
+ } catch (e) {
143
+ op = [
144
+ 6,
145
+ e
146
+ ];
147
+ y = 0;
148
+ } finally{
149
+ f = t = 0;
150
+ }
151
+ if (op[0] & 5) throw op[1];
152
+ return {
153
+ value: op[0] ? op[1] : void 0,
154
+ done: true
155
+ };
156
+ }
157
+ }
158
+ /**
159
+ * Registers an activity and remembers its input and output types.
160
+ *
161
+ * @remarks
162
+ * **Do not annotate `handler` as `ActivityHandler`.** That type is an alias for
163
+ * `FunctionHandler`, which the SDK declares as
164
+ * `(triggerInput: any, context: InvocationContext) => FunctionResult<any>` — so
165
+ * annotating it discards the very signature this function exists to capture and
166
+ * silently reduces the activity to `any` in and `any` out. The same applies to
167
+ * any middleware wrapper typed `(h: ActivityHandler) => ActivityHandler`; make
168
+ * such wrappers generic instead. Both traps are lintable — see the
169
+ * `no-untyped-activity-handler` rule.
170
+ *
171
+ * `TOutput` is wrapped in `Awaited` so an `async` handler contributes its
172
+ * resolved type rather than a `Promise`.
173
+ *
174
+ * @param name - The activity name, a literal. Never derived from a variable or
175
+ * file name: it is baked into orchestration history, so a rename breaks every
176
+ * in-flight instance.
177
+ * @param handler - The activity implementation.
178
+ * @returns The activity, carrying its input and output types.
179
+ * @throws Error when `name` is already registered.
180
+ * @typeParam TInput - The JSON-serialisable input.
181
+ * @typeParam TOutput - The handler's return type, awaited.
182
+ */ function defineActivity(name, handler) {
183
+ claimName('activity', name);
184
+ var registered = df.app.activity(name, {
185
+ handler: handler
186
+ });
187
+ return {
188
+ name: name,
189
+ registered: registered
190
+ };
191
+ }
192
+ /**
193
+ * Schedules an activity without yielding it, for fan-out.
194
+ *
195
+ * @remarks
196
+ * The single place in this package that schedules an activity. `callActivity`
197
+ * is implemented in terms of it, so there is exactly one line to audit against
198
+ * an SDK change.
199
+ *
200
+ * **Scheduled through `context`, not through `activity.registered`,** and the
201
+ * two are equivalent — verified in the SDK source rather than assumed:
202
+ *
203
+ * ```
204
+ * registered(input) -> new AtomicTask(false, new CallActivityAction(name, input))
205
+ * context.df.callActivity(...) -> new AtomicTask(false, new CallActivityAction(name, input))
206
+ * ```
207
+ *
208
+ * `RegisteredActivityTask` is an `AtomicTask` subclass that only ADDS
209
+ * `withRetry`; the retry paths are identical too, both producing
210
+ * `RetryableTask(AtomicTask(CallActivityWithRetryAction(...)))`. The action is
211
+ * what enters orchestration history, so replay is unaffected.
212
+ *
213
+ * Routing through `context` is what makes {@link runWorkflow} possible without
214
+ * reading `task.action.functionName` — an undocumented internal the package's
215
+ * non-goals forbid depending on. It also makes every helper here uniformly
216
+ * context-first.
217
+ *
218
+ * @param context - The orchestration context.
219
+ * @param activity - The activity to schedule.
220
+ * @param input - The input, checked against the activity's declared type.
221
+ * @param retry - Optional retry policy.
222
+ * @returns A scheduled task, for `all`/`any`.
223
+ * @throws Never - scheduling is synchronous and cannot fail here.
224
+ * @typeParam TInput - The activity's input type.
225
+ * @typeParam TOutput - The activity's output type.
226
+ */ function activityTask(context, activity, input, retry) {
227
+ var task = retry === undefined ? context.df.callActivity(activity.name, input) : context.df.callActivityWithRetry(activity.name, retry, input);
228
+ return {
229
+ task: task
230
+ };
231
+ }
232
+ /**
233
+ * Calls an activity and returns its typed result.
234
+ *
235
+ * @remarks
236
+ * **Must be invoked with `yield*`, not `yield`.** The delegation is what carries
237
+ * the type: `yield*` returns this generator's `TReturn`, which is per-call
238
+ * generic, whereas a generator's `TNext` is shared by every `yield` and so can
239
+ * never be typed per call. A bare `yield` is a compile error rather than a
240
+ * silent `any` — `callActivity` returns a `Generator`, and yielding one where a
241
+ * `Task` is expected does not typecheck — but the error message is obscure, so
242
+ * prefer the lint rule's.
243
+ *
244
+ * Determinism is unaffected. The task yielded up to the Durable driver is the
245
+ * identical object a hand-written call would yield, so replay history and
246
+ * in-flight instances are untouched. This is a type-level change only.
247
+ *
248
+ * @param context - The orchestration context.
249
+ * @param activity - The activity to call.
250
+ * @param input - The input, checked against the activity's declared type.
251
+ * @param retry - Optional retry policy.
252
+ * @returns A generator to delegate to; its return value is the activity output.
253
+ * @throws Whatever the activity threw, once the driver resumes with a failure.
254
+ * @typeParam TInput - The activity's input type.
255
+ * @typeParam TOutput - The activity's output type.
256
+ */ function callActivity(context, activity, input, retry) {
257
+ var result;
258
+ return _ts_generator$5(this, function(_state) {
259
+ switch(_state.label){
260
+ case 0:
261
+ return [
262
+ 4,
263
+ activityTask(context, activity, input, retry).task
264
+ ];
265
+ case 1:
266
+ result = _state.sent();
267
+ // The one cast in the package. The SDK resumes the generator with the
268
+ // activity's result typed `any`; `TOutput` is the claim `defineActivity`
269
+ // captured from the handler's real signature.
270
+ return [
271
+ 2,
272
+ result
273
+ ];
274
+ }
275
+ });
276
+ }
277
+
278
+ function _ts_generator$4(thisArg, body) {
279
+ var f, y, t, _ = {
280
+ label: 0,
281
+ sent: function() {
282
+ if (t[0] & 1) throw t[1];
283
+ return t[1];
284
+ },
285
+ trys: [],
286
+ ops: []
287
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
288
+ return d(g, "next", {
289
+ value: verb(0)
290
+ }), d(g, "throw", {
291
+ value: verb(1)
292
+ }), d(g, "return", {
293
+ value: verb(2)
294
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
295
+ value: function() {
296
+ return this;
297
+ }
298
+ }), g;
299
+ function verb(n) {
300
+ return function(v) {
301
+ return step([
302
+ n,
303
+ v
304
+ ]);
305
+ };
306
+ }
307
+ function step(op) {
308
+ if (f) throw new TypeError("Generator is already executing.");
309
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
310
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
311
+ if (y = 0, t) op = [
312
+ op[0] & 2,
313
+ t.value
314
+ ];
315
+ switch(op[0]){
316
+ case 0:
317
+ case 1:
318
+ t = op;
319
+ break;
320
+ case 4:
321
+ _.label++;
322
+ return {
323
+ value: op[1],
324
+ done: false
325
+ };
326
+ case 5:
327
+ _.label++;
328
+ y = op[1];
329
+ op = [
330
+ 0
331
+ ];
332
+ continue;
333
+ case 7:
334
+ op = _.ops.pop();
335
+ _.trys.pop();
336
+ continue;
337
+ default:
338
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
339
+ _ = 0;
340
+ continue;
341
+ }
342
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
343
+ _.label = op[1];
344
+ break;
345
+ }
346
+ if (op[0] === 6 && _.label < t[1]) {
347
+ _.label = t[1];
348
+ t = op;
349
+ break;
350
+ }
351
+ if (t && _.label < t[2]) {
352
+ _.label = t[2];
353
+ _.ops.push(op);
354
+ break;
355
+ }
356
+ if (t[2]) _.ops.pop();
357
+ _.trys.pop();
358
+ continue;
359
+ }
360
+ op = body.call(thisArg, _);
361
+ } catch (e) {
362
+ op = [
363
+ 6,
364
+ e
365
+ ];
366
+ y = 0;
367
+ } finally{
368
+ f = t = 0;
369
+ }
370
+ if (op[0] & 5) throw op[1];
371
+ return {
372
+ value: op[0] ? op[1] : void 0,
373
+ done: true
374
+ };
375
+ }
376
+ }
377
+ function _ts_values$1(o) {
378
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
379
+ if (m) {
380
+ return m.call(o);
381
+ }
382
+ if (o && typeof o.length === "number") {
383
+ return {
384
+ next: function() {
385
+ if (o && i >= o.length) {
386
+ o = void 0;
387
+ }
388
+ return {
389
+ value: o && o[i++],
390
+ done: !o
391
+ };
392
+ }
393
+ };
394
+ }
395
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
396
+ }
397
+ /**
398
+ * Registers an orchestration, handing the handler its deserialised input.
399
+ *
400
+ * @remarks
401
+ * The SDK's `OrchestrationHandler` takes **only** `context` — there is no input
402
+ * parameter — so this wrapper calls `getInput` itself and passes the result as a
403
+ * second argument. That is why consumers never write
404
+ * `context.df.getInput() as SomeType`.
405
+ *
406
+ * @param name - The orchestration name, a literal. Baked into history; never derive it.
407
+ * @param handler - The orchestration generator, receiving context and input.
408
+ * @param options - Optional input validation. See {@link DefineOrchestrationOptions}.
409
+ * @returns The orchestration, carrying its input and output types.
410
+ * @throws Error when `name` is already registered.
411
+ * @typeParam TInput - The JSON-serialisable input.
412
+ * @typeParam TOutput - The value the orchestration returns.
413
+ */ function defineOrchestration(name, handler, options) {
414
+ claimName('orchestration', name);
415
+ var parse = options === null || options === void 0 ? void 0 : options.parse;
416
+ var bind = function bind(context) {
417
+ return {
418
+ name: name,
419
+ continueAsNew: function continueAsNew(next) {
420
+ context.df.continueAsNew(next);
421
+ }
422
+ };
423
+ };
424
+ var registered = df.app.orchestration(name, function(context) {
425
+ var raw, input;
426
+ return _ts_generator$4(this, function(_state) {
427
+ switch(_state.label){
428
+ case 0:
429
+ raw = context.df.getInput();
430
+ input = parse === undefined ? raw : parse(raw);
431
+ return [
432
+ 5,
433
+ _ts_values$1(handler(context, input, bind(context)))
434
+ ];
435
+ case 1:
436
+ return [
437
+ 2,
438
+ _state.sent()
439
+ ];
440
+ }
441
+ });
442
+ });
443
+ return {
444
+ name: name,
445
+ registered: registered,
446
+ handler: function handler1(context, input) {
447
+ return handler(context, input, bind(context));
448
+ }
449
+ };
450
+ }
451
+ /**
452
+ * Calls a sub-orchestration and returns its typed result.
453
+ *
454
+ * @remarks
455
+ * **Must be invoked with `yield*`.** See `callActivity` for why delegation is
456
+ * what carries the type.
457
+ *
458
+ * @param orchestration - The sub-orchestration to call.
459
+ * @param input - The input, checked against its declared type.
460
+ * @param options - Optional instance id and retry policy.
461
+ * @returns A generator to delegate to; its return value is the sub-orchestration output.
462
+ * @throws Whatever the sub-orchestration threw, once the driver resumes with a failure.
463
+ * @typeParam TInput - The sub-orchestration's input type.
464
+ * @typeParam TOutput - The sub-orchestration's output type.
465
+ */ function callSubOrchestration(context, orchestration, input, options) {
466
+ var result;
467
+ return _ts_generator$4(this, function(_state) {
468
+ switch(_state.label){
469
+ case 0:
470
+ return [
471
+ 4,
472
+ subOrchestrationTask(context, orchestration, input, options).task
473
+ ];
474
+ case 1:
475
+ result = _state.sent();
476
+ return [
477
+ 2,
478
+ result
479
+ ];
480
+ }
481
+ });
482
+ }
483
+ /**
484
+ * Schedules a sub-orchestration without yielding it.
485
+ *
486
+ * @remarks
487
+ * The task form of {@link callSubOrchestration}, so several sub-orchestrations
488
+ * can run concurrently through `all`. Fanning out over sub-orchestrations is
489
+ * the standard way to bound a large batch — each child gets its own history,
490
+ * so the parent's history does not grow with the batch size.
491
+ *
492
+ * @param context - The orchestration context.
493
+ * @param orchestration - The sub-orchestration to schedule.
494
+ * @param input - Its input, checked against its declared type.
495
+ * @param options - Optional fixed instance id and retry policy.
496
+ * @returns A task carrying the sub-orchestration's output type.
497
+ * @throws Never - scheduling only.
498
+ * @typeParam TInput - The sub-orchestration's input type.
499
+ * @typeParam TOutput - The sub-orchestration's output type.
500
+ */ function subOrchestrationTask(context, orchestration, input, options) {
501
+ var retry = options === null || options === void 0 ? void 0 : options.retry;
502
+ var task = retry === undefined ? context.df.callSubOrchestrator(orchestration.name, input, options === null || options === void 0 ? void 0 : options.instanceId) : context.df.callSubOrchestratorWithRetry(orchestration.name, retry, input, options === null || options === void 0 ? void 0 : options.instanceId);
503
+ return {
504
+ task: task
505
+ };
506
+ }
507
+
508
+ function asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, key, arg) {
509
+ try {
510
+ var info = gen[key](arg);
511
+ var value = info.value;
512
+ } catch (error) {
513
+ reject(error);
514
+ return;
515
+ }
516
+ if (info.done) resolve(value);
517
+ else Promise.resolve(value).then(_next, _throw);
518
+ }
519
+ function _async_to_generator$1(fn) {
520
+ return function() {
521
+ var self = this, args = arguments;
522
+ return new Promise(function(resolve, reject) {
523
+ var gen = fn.apply(self, args);
524
+ function _next(value) {
525
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "next", value);
526
+ }
527
+ function _throw(err) {
528
+ asyncGeneratorStep$1(gen, resolve, reject, _next, _throw, "throw", err);
529
+ }
530
+ _next(undefined);
531
+ });
532
+ };
533
+ }
534
+ function _define_property(obj, key, value) {
535
+ if (key in obj) {
536
+ Object.defineProperty(obj, key, {
537
+ value: value,
538
+ enumerable: true,
539
+ configurable: true,
540
+ writable: true
541
+ });
542
+ } else obj[key] = value;
543
+ return obj;
544
+ }
545
+ function _object_spread(target) {
546
+ for(var i = 1; i < arguments.length; i++){
547
+ var source = arguments[i] != null ? arguments[i] : {};
548
+ var ownKeys = Object.keys(source);
549
+ if (typeof Object.getOwnPropertySymbols === "function") {
550
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
551
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
552
+ }));
553
+ }
554
+ ownKeys.forEach(function(key) {
555
+ _define_property(target, key, source[key]);
556
+ });
557
+ }
558
+ return target;
559
+ }
560
+ function _ts_generator$3(thisArg, body) {
561
+ var f, y, t, _ = {
562
+ label: 0,
563
+ sent: function() {
564
+ if (t[0] & 1) throw t[1];
565
+ return t[1];
566
+ },
567
+ trys: [],
568
+ ops: []
569
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
570
+ return d(g, "next", {
571
+ value: verb(0)
572
+ }), d(g, "throw", {
573
+ value: verb(1)
574
+ }), d(g, "return", {
575
+ value: verb(2)
576
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
577
+ value: function() {
578
+ return this;
579
+ }
580
+ }), g;
581
+ function verb(n) {
582
+ return function(v) {
583
+ return step([
584
+ n,
585
+ v
586
+ ]);
587
+ };
588
+ }
589
+ function step(op) {
590
+ if (f) throw new TypeError("Generator is already executing.");
591
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
592
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
593
+ if (y = 0, t) op = [
594
+ op[0] & 2,
595
+ t.value
596
+ ];
597
+ switch(op[0]){
598
+ case 0:
599
+ case 1:
600
+ t = op;
601
+ break;
602
+ case 4:
603
+ _.label++;
604
+ return {
605
+ value: op[1],
606
+ done: false
607
+ };
608
+ case 5:
609
+ _.label++;
610
+ y = op[1];
611
+ op = [
612
+ 0
613
+ ];
614
+ continue;
615
+ case 7:
616
+ op = _.ops.pop();
617
+ _.trys.pop();
618
+ continue;
619
+ default:
620
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
621
+ _ = 0;
622
+ continue;
623
+ }
624
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
625
+ _.label = op[1];
626
+ break;
627
+ }
628
+ if (op[0] === 6 && _.label < t[1]) {
629
+ _.label = t[1];
630
+ t = op;
631
+ break;
632
+ }
633
+ if (t && _.label < t[2]) {
634
+ _.label = t[2];
635
+ _.ops.push(op);
636
+ break;
637
+ }
638
+ if (t[2]) _.ops.pop();
639
+ _.trys.pop();
640
+ continue;
641
+ }
642
+ op = body.call(thisArg, _);
643
+ } catch (e) {
644
+ op = [
645
+ 6,
646
+ e
647
+ ];
648
+ y = 0;
649
+ } finally{
650
+ f = t = 0;
651
+ }
652
+ if (op[0] & 5) throw op[1];
653
+ return {
654
+ value: op[0] ? op[1] : void 0,
655
+ done: true
656
+ };
657
+ }
658
+ }
659
+ /**
660
+ * Starts an orchestration with an input checked against its declared type.
661
+ *
662
+ * @remarks
663
+ * `DurableClient.startNew` takes the orchestration **name** and an options
664
+ * object carrying `input`, both untyped. This narrows the pair so a caller
665
+ * cannot start an orchestration with the wrong payload shape.
666
+ *
667
+ * @param client - The Durable client, from `df.getClient(context)`.
668
+ * @param orchestration - The orchestration to start.
669
+ * @param input - The input, checked against its declared type.
670
+ * @param options - Optional instance id.
671
+ * @returns The new instance id.
672
+ * @throws Propagates whatever the client throws.
673
+ * @typeParam TInput - The orchestration's input type.
674
+ * @typeParam TOutput - The orchestration's output type, unused at runtime.
675
+ */ function startOrchestration(client, orchestration, input, options) {
676
+ return _async_to_generator$1(function() {
677
+ var instanceId;
678
+ return _ts_generator$3(this, function(_state) {
679
+ switch(_state.label){
680
+ case 0:
681
+ instanceId = options === null || options === void 0 ? void 0 : options.instanceId;
682
+ return [
683
+ 4,
684
+ client.startNew(orchestration.name, _object_spread({
685
+ input: input
686
+ }, instanceId !== undefined && {
687
+ instanceId: instanceId
688
+ }))
689
+ ];
690
+ case 1:
691
+ return [
692
+ 2,
693
+ _state.sent()
694
+ ];
695
+ }
696
+ });
697
+ })();
698
+ }
699
+
700
+ /**
701
+ * Builds a real SDK `RetryOptions` from a plain object.
702
+ *
703
+ * @remarks
704
+ * Returns a genuine class instance rather than a structurally-similar literal,
705
+ * deliberately: handing `callActivityWithRetry` a plain object would depend on
706
+ * the SDK reading it structurally, which is undocumented and exactly the kind
707
+ * of internal this package refuses to rely on.
708
+ *
709
+ * Only the properties actually supplied are assigned, so the SDK's own
710
+ * defaults stand for the rest instead of being overwritten with `undefined`.
711
+ *
712
+ * @param policy - The retry settings.
713
+ * @returns An SDK `RetryOptions` instance.
714
+ * @throws Whatever the SDK constructor throws for an invalid interval.
715
+ * @typeParam None - this function has no generic type parameters.
716
+ */ function retryPolicy(policy) {
717
+ var options = new RetryOptions(policy.firstRetryIntervalInMilliseconds, policy.maxNumberOfAttempts);
718
+ if (policy.backoffCoefficient !== undefined) {
719
+ options.backoffCoefficient = policy.backoffCoefficient;
720
+ }
721
+ if (policy.maxRetryIntervalInMilliseconds !== undefined) {
722
+ options.maxRetryIntervalInMilliseconds = policy.maxRetryIntervalInMilliseconds;
723
+ }
724
+ if (policy.retryTimeoutInMilliseconds !== undefined) {
725
+ options.retryTimeoutInMilliseconds = policy.retryTimeoutInMilliseconds;
726
+ }
727
+ return options;
728
+ }
729
+
730
+ function _ts_generator$2(thisArg, body) {
731
+ var f, y, t, _ = {
732
+ label: 0,
733
+ sent: function() {
734
+ if (t[0] & 1) throw t[1];
735
+ return t[1];
736
+ },
737
+ trys: [],
738
+ ops: []
739
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
740
+ return d(g, "next", {
741
+ value: verb(0)
742
+ }), d(g, "throw", {
743
+ value: verb(1)
744
+ }), d(g, "return", {
745
+ value: verb(2)
746
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
747
+ value: function() {
748
+ return this;
749
+ }
750
+ }), g;
751
+ function verb(n) {
752
+ return function(v) {
753
+ return step([
754
+ n,
755
+ v
756
+ ]);
757
+ };
758
+ }
759
+ function step(op) {
760
+ if (f) throw new TypeError("Generator is already executing.");
761
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
762
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
763
+ if (y = 0, t) op = [
764
+ op[0] & 2,
765
+ t.value
766
+ ];
767
+ switch(op[0]){
768
+ case 0:
769
+ case 1:
770
+ t = op;
771
+ break;
772
+ case 4:
773
+ _.label++;
774
+ return {
775
+ value: op[1],
776
+ done: false
777
+ };
778
+ case 5:
779
+ _.label++;
780
+ y = op[1];
781
+ op = [
782
+ 0
783
+ ];
784
+ continue;
785
+ case 7:
786
+ op = _.ops.pop();
787
+ _.trys.pop();
788
+ continue;
789
+ default:
790
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
791
+ _ = 0;
792
+ continue;
793
+ }
794
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
795
+ _.label = op[1];
796
+ break;
797
+ }
798
+ if (op[0] === 6 && _.label < t[1]) {
799
+ _.label = t[1];
800
+ t = op;
801
+ break;
802
+ }
803
+ if (t && _.label < t[2]) {
804
+ _.label = t[2];
805
+ _.ops.push(op);
806
+ break;
807
+ }
808
+ if (t[2]) _.ops.pop();
809
+ _.trys.pop();
810
+ continue;
811
+ }
812
+ op = body.call(thisArg, _);
813
+ } catch (e) {
814
+ op = [
815
+ 6,
816
+ e
817
+ ];
818
+ y = 0;
819
+ } finally{
820
+ f = t = 0;
821
+ }
822
+ if (op[0] & 5) throw op[1];
823
+ return {
824
+ value: op[0] ? op[1] : void 0,
825
+ done: true
826
+ };
827
+ }
828
+ }
829
+ /**
830
+ * Waits for every task, preserving tuple positions.
831
+ *
832
+ * @remarks
833
+ * **Must be invoked with `yield *`.** Takes `context` because `Task.all` is an
834
+ * instance member of `context.df`, not a static — the build plan's
835
+ * context-free signature cannot reach it.
836
+ *
837
+ * @param context - The orchestration context.
838
+ * @param tasks - The scheduled tasks, as a tuple.
839
+ * @returns A generator whose return value is the outputs, in input order.
840
+ * @throws `AggregatedError` when any task failed, matching the SDK.
841
+ * @typeParam T - The tuple of tasks.
842
+ */ function all(context, tasks) {
843
+ var results;
844
+ return _ts_generator$2(this, function(_state) {
845
+ switch(_state.label){
846
+ case 0:
847
+ return [
848
+ 4,
849
+ context.df.Task.all(tasks.map(function(t) {
850
+ return t.task;
851
+ }))
852
+ ];
853
+ case 1:
854
+ results = _state.sent();
855
+ return [
856
+ 2,
857
+ results
858
+ ];
859
+ }
860
+ });
861
+ }
862
+ /**
863
+ * Waits for the first task to complete and returns **which one won**.
864
+ *
865
+ * @remarks
866
+ * Returns the winning task, not its result, because that is what the SDK does:
867
+ * `Task.any` is documented as returning "the first Task from tasks to
868
+ * complete", and the SDK's own example compares it by identity
869
+ * (`if (winner === otherTask)`). The build plan's signature returned the
870
+ * output type instead, which would hand back a `Task` at runtime while the
871
+ * compiler believed it was the output — the exact class of silent mistyping
872
+ * this package exists to prevent.
873
+ *
874
+ * The winner is mapped back to the `TypedTask` the caller passed, so `===`
875
+ * against the original works. Read its value with {@link resultOf}.
876
+ *
877
+ * **Must be invoked with `yield *`.**
878
+ *
879
+ * @param context - The orchestration context.
880
+ * @param tasks - The scheduled tasks.
881
+ * @returns A generator whose return value is the winning task.
882
+ * @throws Error when the SDK returns a task that was not one of the inputs.
883
+ * @typeParam T - The tuple of tasks.
884
+ */ function any(context, tasks) {
885
+ var won, winner;
886
+ return _ts_generator$2(this, function(_state) {
887
+ switch(_state.label){
888
+ case 0:
889
+ return [
890
+ 4,
891
+ context.df.Task.any(tasks.map(function(t) {
892
+ return t.task;
893
+ }))
894
+ ];
895
+ case 1:
896
+ won = _state.sent();
897
+ winner = tasks.find(function(t) {
898
+ return t.task === won;
899
+ });
900
+ if (winner === undefined) {
901
+ // Not defensive padding: if this ever fires, the SDK returned something
902
+ // other than one of the tasks handed to it, and silently returning the
903
+ // wrong element would misroute the branch the caller takes next.
904
+ throw new Error('Task.any returned a task that was not one of the inputs.');
905
+ }
906
+ return [
907
+ 2,
908
+ winner
909
+ ];
910
+ }
911
+ });
912
+ }
913
+ /**
914
+ * Reads a completed task's result, typed.
915
+ *
916
+ * @remarks
917
+ * `Task.result` is declared `unknown` by the SDK. This applies the output type
918
+ * the `TypedTask` was carrying all along. Only meaningful after the task has
919
+ * completed — typically on the winner from {@link any}.
920
+ *
921
+ * @param task - A completed task.
922
+ * @returns Its result, typed as the task's output.
923
+ * @throws Never - reads a property.
924
+ * @typeParam TOutput - The task's output type.
925
+ */ function resultOf(task) {
926
+ return task.task.result;
927
+ }
928
+
929
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
930
+ try {
931
+ var info = gen[key](arg);
932
+ var value = info.value;
933
+ } catch (error) {
934
+ reject(error);
935
+ return;
936
+ }
937
+ if (info.done) resolve(value);
938
+ else Promise.resolve(value).then(_next, _throw);
939
+ }
940
+ function _async_to_generator(fn) {
941
+ return function() {
942
+ var self = this, args = arguments;
943
+ return new Promise(function(resolve, reject) {
944
+ var gen = fn.apply(self, args);
945
+ function _next(value) {
946
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
947
+ }
948
+ function _throw(err) {
949
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
950
+ }
951
+ _next(undefined);
952
+ });
953
+ };
954
+ }
955
+ function _ts_generator$1(thisArg, body) {
956
+ var f, y, t, _ = {
957
+ label: 0,
958
+ sent: function() {
959
+ if (t[0] & 1) throw t[1];
960
+ return t[1];
961
+ },
962
+ trys: [],
963
+ ops: []
964
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
965
+ return d(g, "next", {
966
+ value: verb(0)
967
+ }), d(g, "throw", {
968
+ value: verb(1)
969
+ }), d(g, "return", {
970
+ value: verb(2)
971
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
972
+ value: function() {
973
+ return this;
974
+ }
975
+ }), g;
976
+ function verb(n) {
977
+ return function(v) {
978
+ return step([
979
+ n,
980
+ v
981
+ ]);
982
+ };
983
+ }
984
+ function step(op) {
985
+ if (f) throw new TypeError("Generator is already executing.");
986
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
987
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
988
+ if (y = 0, t) op = [
989
+ op[0] & 2,
990
+ t.value
991
+ ];
992
+ switch(op[0]){
993
+ case 0:
994
+ case 1:
995
+ t = op;
996
+ break;
997
+ case 4:
998
+ _.label++;
999
+ return {
1000
+ value: op[1],
1001
+ done: false
1002
+ };
1003
+ case 5:
1004
+ _.label++;
1005
+ y = op[1];
1006
+ op = [
1007
+ 0
1008
+ ];
1009
+ continue;
1010
+ case 7:
1011
+ op = _.ops.pop();
1012
+ _.trys.pop();
1013
+ continue;
1014
+ default:
1015
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
1016
+ _ = 0;
1017
+ continue;
1018
+ }
1019
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
1020
+ _.label = op[1];
1021
+ break;
1022
+ }
1023
+ if (op[0] === 6 && _.label < t[1]) {
1024
+ _.label = t[1];
1025
+ t = op;
1026
+ break;
1027
+ }
1028
+ if (t && _.label < t[2]) {
1029
+ _.label = t[2];
1030
+ _.ops.push(op);
1031
+ break;
1032
+ }
1033
+ if (t[2]) _.ops.pop();
1034
+ _.trys.pop();
1035
+ continue;
1036
+ }
1037
+ op = body.call(thisArg, _);
1038
+ } catch (e) {
1039
+ op = [
1040
+ 6,
1041
+ e
1042
+ ];
1043
+ y = 0;
1044
+ } finally{
1045
+ f = t = 0;
1046
+ }
1047
+ if (op[0] & 5) throw op[1];
1048
+ return {
1049
+ value: op[0] ? op[1] : void 0,
1050
+ done: true
1051
+ };
1052
+ }
1053
+ }
1054
+ /**
1055
+ * Declares an external event and its payload type.
1056
+ *
1057
+ * @remarks
1058
+ * Deliberately does not register anything — external events have no
1059
+ * registration step in Durable Functions. This exists only to pair a name with
1060
+ * a payload type so the waiter and the raiser cannot disagree.
1061
+ *
1062
+ * @param name - The event name, a literal.
1063
+ * @returns The event reference.
1064
+ * @throws Never - constructs an object.
1065
+ * @typeParam TPayload - The payload type.
1066
+ */ function defineEvent(name) {
1067
+ return {
1068
+ name: name
1069
+ };
1070
+ }
1071
+ /**
1072
+ * Waits for an external event and returns its typed payload.
1073
+ *
1074
+ * @remarks
1075
+ * **Must be invoked with `yield *`.**
1076
+ *
1077
+ * @param context - The orchestration context.
1078
+ * @param event - The event to wait for.
1079
+ * @returns A generator whose return value is the event payload.
1080
+ * @throws Never - resolves when the event arrives.
1081
+ * @typeParam TPayload - The payload type.
1082
+ */ function waitForEvent(context, event) {
1083
+ var payload;
1084
+ return _ts_generator$1(this, function(_state) {
1085
+ switch(_state.label){
1086
+ case 0:
1087
+ return [
1088
+ 4,
1089
+ eventTask(context, event).task
1090
+ ];
1091
+ case 1:
1092
+ payload = _state.sent();
1093
+ return [
1094
+ 2,
1095
+ payload
1096
+ ];
1097
+ }
1098
+ });
1099
+ }
1100
+ /**
1101
+ * Schedules a wait for an external event, without yielding it.
1102
+ *
1103
+ * @remarks
1104
+ * The task form of {@link waitForEvent}, and the reason it exists is a gap the
1105
+ * reconstructed workflows found: `any` and `all` take `TypedTask`s, so with
1106
+ * only the generator form the single most common Durable Functions pattern —
1107
+ * **wait for human approval, or time out** — could not be expressed at all.
1108
+ *
1109
+ * Pair it with {@link timerTask} and hand both to `any`.
1110
+ *
1111
+ * @param context - The orchestration context.
1112
+ * @param event - The event to wait for.
1113
+ * @returns A task carrying the event's payload type.
1114
+ * @throws Never - scheduling only.
1115
+ * @typeParam TPayload - The payload type.
1116
+ */ function eventTask(context, event) {
1117
+ return {
1118
+ task: context.df.waitForExternalEvent(event.name)
1119
+ };
1120
+ }
1121
+ /**
1122
+ * Raises an external event to a waiting instance, with a checked payload.
1123
+ *
1124
+ * @remarks
1125
+ * The client half of {@link waitForEvent}. Pairing both sides through the same
1126
+ * `EventRef` is what stops the raiser and the waiter disagreeing about the
1127
+ * payload shape — the SDK types `eventData` as `unknown`, so nothing else would.
1128
+ *
1129
+ * @param client - The Durable client.
1130
+ * @param instanceId - The instance to signal.
1131
+ * @param event - The event being raised.
1132
+ * @param payload - The payload, checked against the event's declared type.
1133
+ * @returns A promise resolving when the event is enqueued.
1134
+ * @throws Propagates whatever the client throws.
1135
+ * @typeParam TPayload - The payload type.
1136
+ */ function raiseEvent(client, instanceId, event, payload) {
1137
+ return _async_to_generator(function() {
1138
+ return _ts_generator$1(this, function(_state) {
1139
+ switch(_state.label){
1140
+ case 0:
1141
+ return [
1142
+ 4,
1143
+ client.raiseEvent(instanceId, event.name, payload)
1144
+ ];
1145
+ case 1:
1146
+ _state.sent();
1147
+ return [
1148
+ 2
1149
+ ];
1150
+ }
1151
+ });
1152
+ })();
1153
+ }
1154
+
1155
+ function _ts_generator(thisArg, body) {
1156
+ var f, y, t, _ = {
1157
+ label: 0,
1158
+ sent: function() {
1159
+ if (t[0] & 1) throw t[1];
1160
+ return t[1];
1161
+ },
1162
+ trys: [],
1163
+ ops: []
1164
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
1165
+ return d(g, "next", {
1166
+ value: verb(0)
1167
+ }), d(g, "throw", {
1168
+ value: verb(1)
1169
+ }), d(g, "return", {
1170
+ value: verb(2)
1171
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
1172
+ value: function() {
1173
+ return this;
1174
+ }
1175
+ }), g;
1176
+ function verb(n) {
1177
+ return function(v) {
1178
+ return step([
1179
+ n,
1180
+ v
1181
+ ]);
1182
+ };
1183
+ }
1184
+ function step(op) {
1185
+ if (f) throw new TypeError("Generator is already executing.");
1186
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
1187
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
1188
+ if (y = 0, t) op = [
1189
+ op[0] & 2,
1190
+ t.value
1191
+ ];
1192
+ switch(op[0]){
1193
+ case 0:
1194
+ case 1:
1195
+ t = op;
1196
+ break;
1197
+ case 4:
1198
+ _.label++;
1199
+ return {
1200
+ value: op[1],
1201
+ done: false
1202
+ };
1203
+ case 5:
1204
+ _.label++;
1205
+ y = op[1];
1206
+ op = [
1207
+ 0
1208
+ ];
1209
+ continue;
1210
+ case 7:
1211
+ op = _.ops.pop();
1212
+ _.trys.pop();
1213
+ continue;
1214
+ default:
1215
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
1216
+ _ = 0;
1217
+ continue;
1218
+ }
1219
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
1220
+ _.label = op[1];
1221
+ break;
1222
+ }
1223
+ if (op[0] === 6 && _.label < t[1]) {
1224
+ _.label = t[1];
1225
+ t = op;
1226
+ break;
1227
+ }
1228
+ if (t && _.label < t[2]) {
1229
+ _.label = t[2];
1230
+ _.ops.push(op);
1231
+ break;
1232
+ }
1233
+ if (t[2]) _.ops.pop();
1234
+ _.trys.pop();
1235
+ continue;
1236
+ }
1237
+ op = body.call(thisArg, _);
1238
+ } catch (e) {
1239
+ op = [
1240
+ 6,
1241
+ e
1242
+ ];
1243
+ y = 0;
1244
+ } finally{
1245
+ f = t = 0;
1246
+ }
1247
+ if (op[0] & 5) throw op[1];
1248
+ return {
1249
+ value: op[0] ? op[1] : void 0,
1250
+ done: true
1251
+ };
1252
+ }
1253
+ }
1254
+ function _ts_values(o) {
1255
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
1256
+ if (m) {
1257
+ return m.call(o);
1258
+ }
1259
+ if (o && typeof o.length === "number") {
1260
+ return {
1261
+ next: function() {
1262
+ if (o && i >= o.length) {
1263
+ o = void 0;
1264
+ }
1265
+ return {
1266
+ value: o && o[i++],
1267
+ done: !o
1268
+ };
1269
+ }
1270
+ };
1271
+ }
1272
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
1273
+ }
1274
+ /**
1275
+ * The current time, safely for replay.
1276
+ *
1277
+ * @remarks
1278
+ * `new Date()` and `Date.now()` return a different value on every replay, which
1279
+ * silently corrupts orchestration output rather than failing. `currentUtcDateTime`
1280
+ * is derived from orchestration history and returns the same value at the same
1281
+ * point every time. This is the replacement the lint rule suggests.
1282
+ *
1283
+ * @param context - The orchestration context.
1284
+ * @returns The replay-safe current time.
1285
+ * @throws Never - reads a property.
1286
+ * @typeParam None - this function has no generic type parameters.
1287
+ */ function now(context) {
1288
+ return context.df.currentUtcDateTime;
1289
+ }
1290
+ /**
1291
+ * Sleeps until an absolute time.
1292
+ *
1293
+ * @remarks
1294
+ * **Must be invoked with `yield *`.**
1295
+ *
1296
+ * @param context - The orchestration context.
1297
+ * @param when - The absolute time to wake at.
1298
+ * @returns A generator that completes when the timer fires.
1299
+ * @throws Never - the timer either fires or the instance ends.
1300
+ * @typeParam None - this function has no generic type parameters.
1301
+ */ function sleepUntil(context, when) {
1302
+ return _ts_generator(this, function(_state) {
1303
+ switch(_state.label){
1304
+ case 0:
1305
+ return [
1306
+ 4,
1307
+ context.df.createTimer(when)
1308
+ ];
1309
+ case 1:
1310
+ _state.sent();
1311
+ return [
1312
+ 2
1313
+ ];
1314
+ }
1315
+ });
1316
+ }
1317
+ /**
1318
+ * Sleeps for a duration.
1319
+ *
1320
+ * @remarks
1321
+ * The deadline is computed from {@link now}, **never** `Date.now()`. Using wall
1322
+ * clock here would make the deadline move on every replay, so a timer could fire
1323
+ * early, late, or repeatedly. This is the single most common determinism bug in
1324
+ * hand-written orchestrations.
1325
+ *
1326
+ * **Must be invoked with `yield *`.**
1327
+ *
1328
+ * @param context - The orchestration context.
1329
+ * @param ms - How long to sleep, in milliseconds.
1330
+ * @returns A generator that completes when the timer fires.
1331
+ * @throws Never - the timer either fires or the instance ends.
1332
+ * @typeParam None - this function has no generic type parameters.
1333
+ */ function sleepFor(context, ms) {
1334
+ return _ts_generator(this, function(_state) {
1335
+ switch(_state.label){
1336
+ case 0:
1337
+ return [
1338
+ 5,
1339
+ _ts_values(sleepUntil(context, new Date(now(context).getTime() + ms)))
1340
+ ];
1341
+ case 1:
1342
+ _state.sent();
1343
+ return [
1344
+ 2
1345
+ ];
1346
+ }
1347
+ });
1348
+ }
1349
+ /**
1350
+ * Schedules a durable timer for an absolute instant, without yielding it.
1351
+ *
1352
+ * @remarks
1353
+ * The task form of {@link sleepUntil}, so a timer can race an event or an
1354
+ * activity through `any`. See {@link TypedTimerTask} for why the returned
1355
+ * value carries `cancel` — **a pending timer keeps the instance alive**, so the
1356
+ * loser of a race must be cancelled.
1357
+ *
1358
+ * @param context - The orchestration context.
1359
+ * @param when - The instant to fire at.
1360
+ * @returns A cancellable timer task.
1361
+ * @throws Never - scheduling only.
1362
+ * @typeParam None - this function has no generic type parameters.
1363
+ */ function timerTaskUntil(context, when) {
1364
+ var task = context.df.createTimer(when);
1365
+ return {
1366
+ task: task,
1367
+ cancel: function cancel() {
1368
+ task.cancel();
1369
+ },
1370
+ isCompleted: function isCompleted() {
1371
+ return task.isCompleted;
1372
+ }
1373
+ };
1374
+ }
1375
+ /**
1376
+ * Schedules a durable timer a fixed duration ahead, without yielding it.
1377
+ *
1378
+ * @remarks
1379
+ * Computes the deadline from `context.df.currentUtcDateTime`, never
1380
+ * `Date.now()` — the same replay-safety reason {@link sleepFor} does.
1381
+ *
1382
+ * @param context - The orchestration context.
1383
+ * @param ms - How far ahead to fire, in milliseconds.
1384
+ * @returns A cancellable timer task.
1385
+ * @throws Never - scheduling only.
1386
+ * @typeParam None - this function has no generic type parameters.
1387
+ */ function timerTask(context, ms) {
1388
+ return timerTaskUntil(context, new Date(now(context).getTime() + ms));
1389
+ }
1390
+
1391
+ /**
1392
+ * Declares the custom statuses an orchestration can report.
1393
+ *
1394
+ * @remarks
1395
+ * `const` on the type parameter preserves the literal types, so `setStatus`
1396
+ * can check the key against the actual set rather than against `string`. The
1397
+ * object is returned unchanged — this is a typing device, not a transform.
1398
+ *
1399
+ * @param statuses - The status map.
1400
+ * @returns The same object, with its literal types preserved.
1401
+ * @throws Never - returns its argument.
1402
+ * @typeParam T - The status map's literal type.
1403
+ */ function defineStatuses(statuses) {
1404
+ return statuses;
1405
+ }
1406
+ /**
1407
+ * Sets the orchestration's custom status from a declared set.
1408
+ *
1409
+ * @remarks
1410
+ * `setCustomStatus` accepts `unknown`, so a typo in a status string is invisible
1411
+ * until someone reads the instance's status and finds a value nothing produces.
1412
+ * Constraining `key` to the declared map is the whole point.
1413
+ *
1414
+ * @param context - The orchestration context.
1415
+ * @param statuses - The declared status map.
1416
+ * @param key - Which status to set; checked against the map.
1417
+ * @returns Nothing.
1418
+ * @throws Never - delegates to the SDK.
1419
+ * @typeParam T - The status map's literal type.
1420
+ */ function setStatus(context, statuses, key) {
1421
+ context.df.setCustomStatus(statuses[key]);
1422
+ }
1423
+
1424
+ export { activityTask, all, any, callActivity, callSubOrchestration, defineActivity, defineEvent, defineOrchestration, defineStatuses, eventTask, now, raiseEvent, resultOf, retryPolicy, setStatus, sleepFor, sleepUntil, startOrchestration, subOrchestrationTask, timerTask, timerTaskUntil, waitForEvent };