@temporalio/workflow 1.17.3 → 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.
@@ -1,132 +0,0 @@
1
- import { assertInWorkflowContext } from './global-attributes';
2
- import type { Activator } from './internals';
3
- import { fillWithRandom, uuid4FromRandom } from './random-helpers';
4
-
5
- /**
6
- * A deterministic PRNG stream scoped to the current Workflow execution.
7
- *
8
- * Workflow code already gets a deterministic default stream through `Math.random()`.
9
- * This interface exposes additional named streams that are derived from the workflow
10
- * seed without consuming that default stream. Repeated calls to
11
- * {@link getRandomStream} with the same name refer to the same logical stream state
12
- * for the current workflow execution, and that state is preserved across activations.
13
- *
14
- * The primary use case is workflow plugins and interceptors that need private,
15
- * replay-stable entropy without perturbing user workflow randomness.
16
- *
17
- * @experimental This API may be removed or changed in the future.
18
- */
19
- export interface WorkflowRandomStream {
20
- /**
21
- * Draw the next deterministic pseudo-random number from this stream.
22
- *
23
- * This is equivalent to `Math.random()`, but isolated from the workflow's main
24
- * random stream and from other named streams.
25
- */
26
- random(): number;
27
-
28
- /**
29
- * Generate a deterministic UUIDv4 backed by this stream.
30
- */
31
- uuid4(): string;
32
-
33
- /**
34
- * Fill a byte array deterministically from this stream.
35
- */
36
- fill(bytes: Uint8Array): Uint8Array;
37
-
38
- /**
39
- * Run `fn` with scoped workflow-random helpers such as `Math.random()` and `uuid4()`
40
- * routed through this stream.
41
- *
42
- * This is the scoped override API for workflow random streams. It is intended for
43
- * bounded plugin/interceptor code that needs existing calls to `Math.random()` or
44
- * `uuid4()` to use this stream without perturbing the workflow's default random
45
- * sequence or any other named stream.
46
- *
47
- * The override follows async continuations started by `fn`. Workflow interceptor
48
- * `next(...)` continuations restore the downstream workflow random scope before
49
- * entering the rest of the interceptor chain or workflow code, so a temporary
50
- * plugin scope does not leak downstream unless that downstream code explicitly
51
- * establishes its own scope.
52
- *
53
- * Prefer explicit `stream.random()` / `stream.uuid4()` calls when that is practical.
54
- * Use `stream.with(...)` when a temporary scoped override is the better fit, or
55
- * when you want to keep the stream instance in module scope and reuse it directly.
56
- */
57
- with<T>(fn: () => T): T;
58
- }
59
-
60
- class ActivatorRandomStream implements WorkflowRandomStream {
61
- constructor(
62
- protected readonly activator: Activator,
63
- protected readonly name: string
64
- ) {}
65
-
66
- random(): number {
67
- return this.activator.getNamedRandom(this.name)();
68
- }
69
-
70
- uuid4(): string {
71
- return uuid4FromRandom(() => this.random());
72
- }
73
-
74
- fill(bytes: Uint8Array): Uint8Array {
75
- return fillWithRandom(() => this.random(), bytes);
76
- }
77
-
78
- with<T>(fn: () => T): T {
79
- return this.activator.withCurrentRandom(this, fn);
80
- }
81
- }
82
-
83
- class DefaultWorkflowRandomStream implements WorkflowRandomStream {
84
- random(): number {
85
- return assertInWorkflowContext('Workflow.workflowRandom may only be used from workflow context.').random();
86
- }
87
-
88
- uuid4(): string {
89
- return uuid4FromRandom(() => this.random());
90
- }
91
-
92
- fill(bytes: Uint8Array): Uint8Array {
93
- return fillWithRandom(() => this.random(), bytes);
94
- }
95
-
96
- with<T>(fn: () => T): T {
97
- const activator = assertInWorkflowContext('Workflow.workflowRandom may only be used from workflow context.');
98
- return activator.withCurrentRandom(this, fn);
99
- }
100
- }
101
-
102
- /**
103
- * The default deterministic random stream for the current workflow execution.
104
- *
105
- * This exposes the same underlying sequence used by workflow-level `Math.random()`
106
- * when no named override is active. It can be useful for plugin/interceptor code
107
- * that wants an explicit handle to the main workflow random stream, including from
108
- * inside a temporary named scope established by another `WorkflowRandomStream`.
109
- *
110
- * @experimental This API may be removed or changed in the future.
111
- */
112
- export const workflowRandom: WorkflowRandomStream = new DefaultWorkflowRandomStream();
113
-
114
- /**
115
- * Get a named deterministic random stream for the current workflow execution.
116
- *
117
- * Named streams are derived from the workflow seed and a stable stream name,
118
- * without consuming the workflow's default `Math.random()` stream. Repeated
119
- * calls with the same `name` within a workflow execution refer to the same
120
- * logical stream state, including across activations.
121
- *
122
- * This is the preferred entry point for workflow plugins and interceptors that
123
- * need their own deterministic entropy. Use stable package- or module-style
124
- * names so the stream identity remains replay-safe, then keep the returned
125
- * `WorkflowRandomStream` around and call its methods directly.
126
- *
127
- * @experimental This API may be removed or changed in the future.
128
- */
129
- export function getRandomStream(name: string): WorkflowRandomStream {
130
- const activator = assertInWorkflowContext('Workflow.getRandomStream(...) may only be used from workflow context.');
131
- return new ActivatorRandomStream(activator, name);
132
- }