@temporalio/common 1.11.2 → 1.11.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 (68) hide show
  1. package/lib/activity-options.d.ts +8 -6
  2. package/lib/activity-options.js +13 -12
  3. package/lib/activity-options.js.map +1 -1
  4. package/lib/converter/failure-converter.js +8 -6
  5. package/lib/converter/failure-converter.js.map +1 -1
  6. package/lib/converter/payload-converter.js +7 -7
  7. package/lib/converter/payload-converter.js.map +1 -1
  8. package/lib/deprecated-time.js +9 -10
  9. package/lib/deprecated-time.js.map +1 -1
  10. package/lib/encoding.js +3 -3
  11. package/lib/encoding.js.map +1 -1
  12. package/lib/failure.d.ts +51 -23
  13. package/lib/failure.js +65 -32
  14. package/lib/failure.js.map +1 -1
  15. package/lib/index.js +4 -5
  16. package/lib/index.js.map +1 -1
  17. package/lib/interceptors.js +1 -2
  18. package/lib/interceptors.js.map +1 -1
  19. package/lib/interfaces.d.ts +5 -4
  20. package/lib/interfaces.js +4 -5
  21. package/lib/interfaces.js.map +1 -1
  22. package/lib/internal-non-workflow/codec-helpers.js +22 -23
  23. package/lib/internal-non-workflow/codec-helpers.js.map +1 -1
  24. package/lib/internal-non-workflow/data-converter-helpers.js +3 -4
  25. package/lib/internal-non-workflow/data-converter-helpers.js.map +1 -1
  26. package/lib/internal-non-workflow/parse-host-uri.d.ts +5 -4
  27. package/lib/internal-non-workflow/parse-host-uri.js +9 -9
  28. package/lib/internal-non-workflow/parse-host-uri.js.map +1 -1
  29. package/lib/internal-non-workflow/proxy-config.js +1 -2
  30. package/lib/internal-non-workflow/proxy-config.js.map +1 -1
  31. package/lib/internal-non-workflow/tls-config.d.ts +2 -3
  32. package/lib/internal-non-workflow/tls-config.js +1 -2
  33. package/lib/internal-non-workflow/tls-config.js.map +1 -1
  34. package/lib/internal-non-workflow/utils.js +1 -2
  35. package/lib/internal-non-workflow/utils.js.map +1 -1
  36. package/lib/internal-workflow/enums-helpers.d.ts +170 -0
  37. package/lib/internal-workflow/enums-helpers.js +172 -0
  38. package/lib/internal-workflow/enums-helpers.js.map +1 -0
  39. package/lib/internal-workflow/index.d.ts +1 -0
  40. package/lib/internal-workflow/index.js +18 -0
  41. package/lib/internal-workflow/index.js.map +1 -0
  42. package/lib/proto-utils.js +4 -5
  43. package/lib/proto-utils.js.map +1 -1
  44. package/lib/retry-policy.js +2 -3
  45. package/lib/retry-policy.js.map +1 -1
  46. package/lib/time.js +12 -13
  47. package/lib/time.js.map +1 -1
  48. package/lib/type-helpers.d.ts +18 -0
  49. package/lib/type-helpers.js +12 -13
  50. package/lib/type-helpers.js.map +1 -1
  51. package/lib/versioning-intent-enum.js +2 -2
  52. package/lib/versioning-intent-enum.js.map +1 -1
  53. package/lib/workflow-options.d.ts +69 -17
  54. package/lib/workflow-options.js +64 -23
  55. package/lib/workflow-options.js.map +1 -1
  56. package/package.json +3 -3
  57. package/src/activity-options.ts +24 -13
  58. package/src/converter/failure-converter.ts +10 -6
  59. package/src/converter/payload-converter.ts +1 -1
  60. package/src/failure.ts +95 -28
  61. package/src/interfaces.ts +5 -4
  62. package/src/internal-non-workflow/data-converter-helpers.ts +1 -1
  63. package/src/internal-non-workflow/parse-host-uri.ts +6 -5
  64. package/src/internal-non-workflow/tls-config.ts +2 -2
  65. package/src/internal-workflow/enums-helpers.ts +301 -0
  66. package/src/internal-workflow/index.ts +1 -0
  67. package/src/type-helpers.ts +79 -1
  68. package/src/workflow-options.ts +110 -23
@@ -17,6 +17,84 @@ export function checkExtends<_Orig, _Copy extends _Orig>(): void {
17
17
 
18
18
  export type Replace<Base, New> = Omit<Base, keyof New> & New;
19
19
 
20
+ // From https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts
21
+ // MIT or CC0-1.0 — It is meant to be copied into your codebase rather than being used as a dependency.
22
+ export type UnionToIntersection<Union> =
23
+ // `extends unknown` is always going to be the case and is used to convert the `Union` into a
24
+ // [distributive conditional type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
25
+ (
26
+ Union extends unknown
27
+ ? // The union type is used as the only argument to a function since the union
28
+ // of function arguments is an intersection.
29
+ (distributedUnion: Union) => void
30
+ : // This won't happen.
31
+ never
32
+ ) extends // Infer the `Intersection` type since TypeScript represents the positional
33
+ // arguments of unions of functions as an intersection of the union.
34
+ (mergedIntersection: infer Intersection) => void
35
+ ? // The `& Union` is to allow indexing by the resulting type
36
+ Intersection & Union
37
+ : never;
38
+
39
+ type IsEqual<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2 ? true : false;
40
+
41
+ type Primitive = null | undefined | string | number | boolean | symbol | bigint;
42
+
43
+ type IsNull<T> = [T] extends [null] ? true : false;
44
+
45
+ type IsUnknown<T> = unknown extends T // `T` can be `unknown` or `any`
46
+ ? IsNull<T> extends false // `any` can be `null`, but `unknown` can't be
47
+ ? true
48
+ : false
49
+ : false;
50
+
51
+ type ObjectValue<T, K> = K extends keyof T
52
+ ? T[K]
53
+ : ToString<K> extends keyof T
54
+ ? T[ToString<K>]
55
+ : K extends `${infer NumberK extends number}`
56
+ ? NumberK extends keyof T
57
+ ? T[NumberK]
58
+ : never
59
+ : never;
60
+
61
+ type ToString<T> = T extends string | number ? `${T}` : never;
62
+
63
+ type KeysOfUnion<ObjectType> = ObjectType extends unknown ? keyof ObjectType : never;
64
+
65
+ type ArrayElement<T> = T extends readonly unknown[] ? T[0] : never;
66
+
67
+ type ExactObject<ParameterType, InputType> = {
68
+ [Key in keyof ParameterType]: Exact<ParameterType[Key], ObjectValue<InputType, Key>>;
69
+ } & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>;
70
+
71
+ export type Exact<ParameterType, InputType> =
72
+ // Before distributing, check if the two types are equal and if so, return the parameter type immediately
73
+ IsEqual<ParameterType, InputType> extends true
74
+ ? ParameterType
75
+ : // If the parameter is a primitive, return it as is immediately to avoid it being converted to a complex type
76
+ ParameterType extends Primitive
77
+ ? ParameterType
78
+ : // If the parameter is an unknown, return it as is immediately to avoid it being converted to a complex type
79
+ IsUnknown<ParameterType> extends true
80
+ ? unknown
81
+ : // If the parameter is a Function, return it as is because this type is not capable of handling function, leave it to TypeScript
82
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
83
+ ParameterType extends Function
84
+ ? ParameterType
85
+ : // Convert union of array to array of union: A[] & B[] => (A & B)[]
86
+ ParameterType extends unknown[]
87
+ ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
88
+ : // In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.
89
+ ParameterType extends readonly unknown[]
90
+ ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
91
+ : ExactObject<ParameterType, InputType>;
92
+ // End of borrow from https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts
93
+
94
+ export type RemovePrefix<Prefix extends string, Keys extends string> = {
95
+ [k in Keys]: k extends `${Prefix}${infer Suffix}` ? Suffix : never;
96
+ }[Keys];
97
+
20
98
  export function isRecord(value: unknown): value is Record<string, unknown> {
21
99
  return typeof value === 'object' && value !== null;
22
100
  }
@@ -153,7 +231,7 @@ export function deepFreeze<T>(object: T): T {
153
231
  if (value && typeof value === 'object') {
154
232
  try {
155
233
  deepFreeze(value);
156
- } catch (err) {
234
+ } catch (_err) {
157
235
  // This is okay, there are some typed arrays that cannot be frozen (encodingKeys)
158
236
  }
159
237
  } else if (typeof value === 'function') {
@@ -2,60 +2,147 @@ import type { temporal } from '@temporalio/proto';
2
2
  import { SearchAttributes, Workflow } from './interfaces';
3
3
  import { RetryPolicy } from './retry-policy';
4
4
  import { Duration } from './time';
5
- import { checkExtends } from './type-helpers';
5
+ import { makeProtoEnumConverters } from './internal-workflow';
6
6
 
7
- // Avoid importing the proto implementation to reduce workflow bundle size
8
- // Copied from temporal.api.enums.v1.WorkflowIdReusePolicy
9
7
  /**
8
+ * Defines what happens when trying to start a Workflow with the same ID as a *Closed* Workflow.
9
+ *
10
+ * See {@link WorkflowOptions.workflowIdConflictPolicy} for what happens when trying to start a
11
+ * Workflow with the same ID as a *Running* Workflow.
12
+ *
10
13
  * Concept: {@link https://docs.temporal.io/concepts/what-is-a-workflow-id-reuse-policy/ | Workflow Id Reuse Policy}
11
14
  *
12
- * Whether a Workflow can be started with a Workflow Id of a Closed Workflow.
15
+ * *Note: It is not possible to have two actively running Workflows with the same ID.*
13
16
  *
14
- * *Note: A Workflow can never be started with a Workflow Id of a Running Workflow.*
15
17
  */
16
- export enum WorkflowIdReusePolicy {
17
- /**
18
- * No need to use this.
19
- *
20
- * (If a `WorkflowIdReusePolicy` is set to this, or is not set at all, the default value will be used.)
21
- */
22
- WORKFLOW_ID_REUSE_POLICY_UNSPECIFIED = 0,
23
-
18
+ export const WorkflowIdReusePolicy = {
24
19
  /**
25
20
  * The Workflow can be started if the previous Workflow is in a Closed state.
26
21
  * @default
27
22
  */
28
- WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE = 1,
23
+ ALLOW_DUPLICATE: 'ALLOW_DUPLICATE',
29
24
 
30
25
  /**
31
26
  * The Workflow can be started if the previous Workflow is in a Closed state that is not Completed.
32
27
  */
33
- WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY = 2,
28
+ ALLOW_DUPLICATE_FAILED_ONLY: 'ALLOW_DUPLICATE_FAILED_ONLY',
34
29
 
35
30
  /**
36
31
  * The Workflow cannot be started.
37
32
  */
38
- WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE = 3,
33
+ REJECT_DUPLICATE: 'REJECT_DUPLICATE',
39
34
 
40
35
  /**
41
- * Terminate the current workflow if one is already running.
36
+ * Terminate the current Workflow if one is already running; otherwise allow reusing the Workflow ID.
37
+ *
38
+ * @deprecated Use {@link WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE} instead, and
39
+ * set `WorkflowOptions.workflowIdConflictPolicy` to
40
+ * {@link WorkflowIdConflictPolicy.WORKFLOW_ID_CONFLICT_POLICY_TERMINATE_EXISTING}.
41
+ * When using this option, `WorkflowOptions.workflowIdConflictPolicy` must be left unspecified.
42
42
  */
43
- WORKFLOW_ID_REUSE_POLICY_TERMINATE_IF_RUNNING = 4,
44
- }
43
+ TERMINATE_IF_RUNNING: 'TERMINATE_IF_RUNNING', // eslint-disable-line deprecation/deprecation
44
+
45
+ /// Anything below this line has been deprecated
46
+
47
+ /**
48
+ * No need to use this. If a `WorkflowIdReusePolicy` is set to this, or is not set at all, the default value will be used.
49
+ *
50
+ * @deprecated Either leave property `undefined`, or use {@link ALLOW_DUPLICATE} instead.
51
+ */
52
+ WORKFLOW_ID_REUSE_POLICY_UNSPECIFIED: undefined, // eslint-disable-line deprecation/deprecation
53
+
54
+ /** @deprecated Use {@link ALLOW_DUPLICATE} instead. */
55
+ WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE: 'ALLOW_DUPLICATE', // eslint-disable-line deprecation/deprecation
56
+
57
+ /** @deprecated Use {@link ALLOW_DUPLICATE_FAILED_ONLY} instead. */
58
+ WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY: 'ALLOW_DUPLICATE_FAILED_ONLY', // eslint-disable-line deprecation/deprecation
59
+
60
+ /** @deprecated Use {@link REJECT_DUPLICATE} instead. */
61
+ WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE: 'REJECT_DUPLICATE', // eslint-disable-line deprecation/deprecation
62
+
63
+ /** @deprecated Use {@link TERMINATE_IF_RUNNING} instead. */
64
+ WORKFLOW_ID_REUSE_POLICY_TERMINATE_IF_RUNNING: 'TERMINATE_IF_RUNNING', // eslint-disable-line deprecation/deprecation
65
+ } as const;
66
+ export type WorkflowIdReusePolicy = (typeof WorkflowIdReusePolicy)[keyof typeof WorkflowIdReusePolicy];
67
+
68
+ export const [encodeWorkflowIdReusePolicy, decodeWorkflowIdReusePolicy] = makeProtoEnumConverters<
69
+ temporal.api.enums.v1.WorkflowIdReusePolicy,
70
+ typeof temporal.api.enums.v1.WorkflowIdReusePolicy,
71
+ keyof typeof temporal.api.enums.v1.WorkflowIdReusePolicy,
72
+ typeof WorkflowIdReusePolicy,
73
+ 'WORKFLOW_ID_REUSE_POLICY_'
74
+ >(
75
+ {
76
+ [WorkflowIdReusePolicy.ALLOW_DUPLICATE]: 1,
77
+ [WorkflowIdReusePolicy.ALLOW_DUPLICATE_FAILED_ONLY]: 2,
78
+ [WorkflowIdReusePolicy.REJECT_DUPLICATE]: 3,
79
+ [WorkflowIdReusePolicy.TERMINATE_IF_RUNNING]: 4, // eslint-disable-line deprecation/deprecation
80
+ UNSPECIFIED: 0,
81
+ } as const,
82
+ 'WORKFLOW_ID_REUSE_POLICY_'
83
+ );
84
+
85
+ /**
86
+ * Defines what happens when trying to start a Workflow with the same ID as a *Running* Workflow.
87
+ *
88
+ * See {@link WorkflowOptions.workflowIdReusePolicy} for what happens when trying to start a Workflow
89
+ * with the same ID as a *Closed* Workflow.
90
+ *
91
+ * *Note: It is never possible to have two _actively running_ Workflows with the same ID.*
92
+ */
93
+ export type WorkflowIdConflictPolicy = (typeof WorkflowIdConflictPolicy)[keyof typeof WorkflowIdConflictPolicy];
94
+ export const WorkflowIdConflictPolicy = {
95
+ /**
96
+ * Do not start a new Workflow. Instead raise a `WorkflowExecutionAlreadyStartedError`.
97
+ */
98
+ FAIL: 'FAIL',
45
99
 
46
- checkExtends<temporal.api.enums.v1.WorkflowIdReusePolicy, WorkflowIdReusePolicy>();
47
- checkExtends<WorkflowIdReusePolicy, temporal.api.enums.v1.WorkflowIdReusePolicy>();
100
+ /**
101
+ * Do not start a new Workflow. Instead return a Workflow Handle for the already Running Workflow.
102
+ */
103
+ USE_EXISTING: 'USE_EXISTING',
104
+
105
+ /**
106
+ * Start a new Workflow, terminating the current workflow if one is already running.
107
+ */
108
+ TERMINATE_EXISTING: 'TERMINATE_EXISTING',
109
+ } as const;
110
+
111
+ export const [encodeWorkflowIdConflictPolicy, decodeWorkflowIdConflictPolicy] = makeProtoEnumConverters<
112
+ temporal.api.enums.v1.WorkflowIdConflictPolicy,
113
+ typeof temporal.api.enums.v1.WorkflowIdConflictPolicy,
114
+ keyof typeof temporal.api.enums.v1.WorkflowIdConflictPolicy,
115
+ typeof WorkflowIdConflictPolicy,
116
+ 'WORKFLOW_ID_CONFLICT_POLICY_'
117
+ >(
118
+ {
119
+ [WorkflowIdConflictPolicy.FAIL]: 1,
120
+ [WorkflowIdConflictPolicy.USE_EXISTING]: 2,
121
+ [WorkflowIdConflictPolicy.TERMINATE_EXISTING]: 3,
122
+ UNSPECIFIED: 0,
123
+ } as const,
124
+ 'WORKFLOW_ID_CONFLICT_POLICY_'
125
+ );
48
126
 
49
127
  export interface BaseWorkflowOptions {
50
128
  /**
51
- * Whether a Workflow can be started with a Workflow Id of a Closed Workflow.
129
+ * Defines what happens when trying to start a Workflow with the same ID as a *Closed* Workflow.
52
130
  *
53
- * *Note: A Workflow can never be started with a Workflow Id of a Running Workflow.*
131
+ * *Note: It is not possible to have two actively running Workflows with the same ID.*
54
132
  *
55
133
  * @default {@link WorkflowIdReusePolicy.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE}
56
134
  */
57
135
  workflowIdReusePolicy?: WorkflowIdReusePolicy;
58
136
 
137
+ /**
138
+ * Defines what happens when trying to start a Workflow with the same ID as a *Running* Workflow.
139
+ *
140
+ * *Note: It is not possible to have two actively running Workflows with the same ID.*
141
+ *
142
+ * @default {@link WorkflowIdConflictPolicy.WORKFLOW_ID_CONFLICT_POLICY_UNSPECIFIED}
143
+ */
144
+ workflowIdConflictPolicy?: WorkflowIdConflictPolicy;
145
+
59
146
  /**
60
147
  * Controls how a Workflow Execution is retried.
61
148
  *