@temporalio/common 1.3.0 → 1.4.1

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 (116) hide show
  1. package/lib/activity-options.d.ts +138 -0
  2. package/lib/activity-options.js +15 -0
  3. package/lib/activity-options.js.map +1 -0
  4. package/lib/converter/data-converter.d.ts +20 -1
  5. package/lib/converter/data-converter.js +14 -3
  6. package/lib/converter/data-converter.js.map +1 -1
  7. package/lib/converter/failure-converter.d.ts +75 -0
  8. package/lib/converter/failure-converter.js +239 -0
  9. package/lib/converter/failure-converter.js.map +1 -0
  10. package/lib/converter/payload-codec.d.ts +1 -1
  11. package/lib/converter/payload-converter.d.ts +85 -1
  12. package/lib/converter/payload-converter.js +210 -1
  13. package/lib/converter/payload-converter.js.map +1 -1
  14. package/lib/converter/protobuf-payload-converters.d.ts +3 -3
  15. package/lib/converter/protobuf-payload-converters.js +20 -19
  16. package/lib/converter/protobuf-payload-converters.js.map +1 -1
  17. package/lib/converter/types.d.ts +0 -6
  18. package/lib/converter/types.js +7 -18
  19. package/lib/converter/types.js.map +1 -1
  20. package/lib/deprecated-time.d.ts +52 -0
  21. package/lib/deprecated-time.js +106 -0
  22. package/lib/deprecated-time.js.map +1 -0
  23. package/lib/{converter/encoding.d.ts → encoding.d.ts} +8 -0
  24. package/lib/{converter/encoding.js → encoding.js} +15 -1
  25. package/lib/encoding.js.map +1 -0
  26. package/lib/errors.d.ts +47 -0
  27. package/lib/errors.js +68 -0
  28. package/lib/errors.js.map +1 -0
  29. package/lib/failure.d.ts +0 -27
  30. package/lib/failure.js +9 -205
  31. package/lib/failure.js.map +1 -1
  32. package/lib/index.d.ts +39 -8
  33. package/lib/index.js +64 -7
  34. package/lib/index.js.map +1 -1
  35. package/lib/interceptors.d.ts +18 -0
  36. package/lib/interceptors.js +24 -0
  37. package/lib/interceptors.js.map +1 -0
  38. package/lib/interfaces.d.ts +52 -0
  39. package/lib/interfaces.js +3 -0
  40. package/lib/interfaces.js.map +1 -0
  41. package/lib/internal-non-workflow/codec-helpers.d.ts +82 -0
  42. package/lib/internal-non-workflow/codec-helpers.js +295 -0
  43. package/lib/internal-non-workflow/codec-helpers.js.map +1 -0
  44. package/lib/internal-non-workflow/codec-types.d.ts +22 -0
  45. package/lib/internal-non-workflow/codec-types.js +3 -0
  46. package/lib/internal-non-workflow/codec-types.js.map +1 -0
  47. package/lib/internal-non-workflow/data-converter-helpers.d.ts +11 -0
  48. package/lib/internal-non-workflow/data-converter-helpers.js +66 -0
  49. package/lib/internal-non-workflow/data-converter-helpers.js.map +1 -0
  50. package/lib/internal-non-workflow/index.d.ts +10 -0
  51. package/lib/internal-non-workflow/index.js +27 -0
  52. package/lib/internal-non-workflow/index.js.map +1 -0
  53. package/lib/internal-non-workflow/tls-config.d.ts +32 -0
  54. package/lib/internal-non-workflow/tls-config.js +11 -0
  55. package/lib/internal-non-workflow/tls-config.js.map +1 -0
  56. package/lib/internal-non-workflow/utils.d.ts +4 -0
  57. package/lib/internal-non-workflow/utils.js +11 -0
  58. package/lib/internal-non-workflow/utils.js.map +1 -0
  59. package/lib/otel.d.ts +1 -1
  60. package/lib/otel.js +2 -2
  61. package/lib/otel.js.map +1 -1
  62. package/lib/retry-policy.d.ts +48 -0
  63. package/lib/retry-policy.js +62 -0
  64. package/lib/retry-policy.js.map +1 -0
  65. package/lib/time.d.ts +18 -0
  66. package/lib/time.js +79 -0
  67. package/lib/time.js.map +1 -0
  68. package/lib/type-helpers.d.ts +21 -0
  69. package/lib/type-helpers.js +46 -0
  70. package/lib/type-helpers.js.map +1 -0
  71. package/lib/workflow-handle.d.ts +27 -0
  72. package/lib/workflow-handle.js +3 -0
  73. package/lib/workflow-handle.js.map +1 -0
  74. package/lib/workflow-options.d.ts +118 -0
  75. package/lib/workflow-options.js +53 -0
  76. package/lib/workflow-options.js.map +1 -0
  77. package/package.json +5 -4
  78. package/src/activity-options.ts +159 -0
  79. package/src/converter/data-converter.ts +24 -3
  80. package/src/converter/failure-converter.ts +355 -0
  81. package/src/converter/payload-codec.ts +1 -1
  82. package/src/converter/payload-converter.ts +246 -1
  83. package/src/converter/protobuf-payload-converters.ts +14 -25
  84. package/src/converter/types.ts +6 -19
  85. package/src/deprecated-time.ts +80 -0
  86. package/src/{converter/encoding.ts → encoding.ts} +14 -0
  87. package/src/errors.ts +55 -0
  88. package/src/failure.ts +1 -251
  89. package/src/index.ts +55 -8
  90. package/src/interceptors.ts +32 -0
  91. package/src/interfaces.ts +64 -0
  92. package/src/internal-non-workflow/codec-helpers.ts +348 -0
  93. package/src/internal-non-workflow/codec-types.ts +34 -0
  94. package/src/internal-non-workflow/data-converter-helpers.ts +81 -0
  95. package/src/internal-non-workflow/index.ts +10 -0
  96. package/src/internal-non-workflow/tls-config.ts +35 -0
  97. package/src/internal-non-workflow/utils.ts +6 -0
  98. package/src/otel.ts +2 -2
  99. package/src/retry-policy.ts +101 -0
  100. package/src/time.ts +79 -0
  101. package/src/type-helpers.ts +64 -0
  102. package/src/workflow-handle.ts +30 -0
  103. package/src/workflow-options.ts +156 -0
  104. package/lib/converter/encoding.js.map +0 -1
  105. package/lib/converter/json-payload-converter.d.ts +0 -10
  106. package/lib/converter/json-payload-converter.js +0 -39
  107. package/lib/converter/json-payload-converter.js.map +0 -1
  108. package/lib/converter/payload-converters.d.ts +0 -67
  109. package/lib/converter/payload-converters.js +0 -124
  110. package/lib/converter/payload-converters.js.map +0 -1
  111. package/lib/converter/search-attribute-payload-converter.d.ts +0 -12
  112. package/lib/converter/search-attribute-payload-converter.js +0 -64
  113. package/lib/converter/search-attribute-payload-converter.js.map +0 -1
  114. package/src/converter/json-payload-converter.ts +0 -37
  115. package/src/converter/payload-converters.ts +0 -148
  116. package/src/converter/search-attribute-payload-converter.ts +0 -71
@@ -1,4 +1,7 @@
1
- import { Payload } from './types';
1
+ import { decode, encode } from '../encoding';
2
+ import { IllegalStateError, PayloadConverterError, ValueError } from '../errors';
3
+ import { Payload } from '../interfaces';
4
+ import { encodingKeys, encodingTypes, METADATA_ENCODING_KEY } from './types';
2
5
 
3
6
  /**
4
7
  * Used by the framework to serialize/deserialize data like parameters and return values.
@@ -93,3 +96,245 @@ export function mapFromPayloads<K extends string>(
93
96
  })
94
97
  ) as Record<K, unknown>;
95
98
  }
99
+
100
+ export interface PayloadConverterWithEncoding {
101
+ /**
102
+ * Converts a value to a {@link Payload}.
103
+ *
104
+ * @param value The value to convert. Example values include the Workflow args sent from the Client and the values returned by a Workflow or Activity.
105
+ * @returns The {@link Payload}, or `undefined` if unable to convert.
106
+ */
107
+ toPayload<T>(value: T): Payload | undefined;
108
+
109
+ /**
110
+ * Converts a {@link Payload} back to a value.
111
+ */
112
+ fromPayload<T>(payload: Payload): T;
113
+
114
+ readonly encodingType: string;
115
+ }
116
+
117
+ /**
118
+ * Tries to convert values to {@link Payload}s using the {@link PayloadConverterWithEncoding}s provided to the constructor, in the order provided.
119
+ *
120
+ * Converts Payloads to values based on the `Payload.metadata.encoding` field, which matches the {@link PayloadConverterWithEncoding.encodingType}
121
+ * of the converter that created the Payload.
122
+ */
123
+ export class CompositePayloadConverter implements PayloadConverter {
124
+ readonly converters: PayloadConverterWithEncoding[];
125
+ readonly converterByEncoding: Map<string, PayloadConverterWithEncoding> = new Map();
126
+
127
+ constructor(...converters: PayloadConverterWithEncoding[]) {
128
+ if (converters.length === 0) {
129
+ throw new PayloadConverterError('Must provide at least one PayloadConverterWithEncoding');
130
+ }
131
+
132
+ this.converters = converters;
133
+ for (const converter of converters) {
134
+ this.converterByEncoding.set(converter.encodingType, converter);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Tries to run `.toPayload(value)` on each converter in the order provided at construction.
140
+ * Returns the first successful result, throws {@link ValueError} if there is no converter that can handle the value.
141
+ */
142
+ public toPayload<T>(value: T): Payload {
143
+ for (const converter of this.converters) {
144
+ const result = converter.toPayload(value);
145
+ if (result !== undefined) {
146
+ return result;
147
+ }
148
+ }
149
+
150
+ throw new ValueError(`Unable to convert ${value} to payload`);
151
+ }
152
+
153
+ /**
154
+ * Run {@link PayloadConverterWithEncoding.fromPayload} based on the `encoding` metadata of the {@link Payload}.
155
+ */
156
+ public fromPayload<T>(payload: Payload): T {
157
+ if (payload.metadata === undefined || payload.metadata === null) {
158
+ throw new ValueError('Missing payload metadata');
159
+ }
160
+ const encoding = decode(payload.metadata[METADATA_ENCODING_KEY]);
161
+ const converter = this.converterByEncoding.get(encoding);
162
+ if (converter === undefined) {
163
+ throw new ValueError(`Unknown encoding: ${encoding}`);
164
+ }
165
+ return converter.fromPayload(payload);
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Converts between JS undefined and NULL Payload
171
+ */
172
+ export class UndefinedPayloadConverter implements PayloadConverterWithEncoding {
173
+ public encodingType = encodingTypes.METADATA_ENCODING_NULL;
174
+
175
+ public toPayload(value: unknown): Payload | undefined {
176
+ if (value !== undefined) {
177
+ return undefined;
178
+ }
179
+
180
+ return {
181
+ metadata: {
182
+ [METADATA_ENCODING_KEY]: encodingKeys.METADATA_ENCODING_NULL,
183
+ },
184
+ };
185
+ }
186
+
187
+ public fromPayload<T>(_content: Payload): T {
188
+ return undefined as any; // Just return undefined
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Converts between binary data types and RAW Payload
194
+ */
195
+ export class BinaryPayloadConverter implements PayloadConverterWithEncoding {
196
+ public encodingType = encodingTypes.METADATA_ENCODING_RAW;
197
+
198
+ public toPayload(value: unknown): Payload | undefined {
199
+ if (!(value instanceof Uint8Array)) {
200
+ return undefined;
201
+ }
202
+
203
+ return {
204
+ metadata: {
205
+ [METADATA_ENCODING_KEY]: encodingKeys.METADATA_ENCODING_RAW,
206
+ },
207
+ data: value,
208
+ };
209
+ }
210
+
211
+ public fromPayload<T>(content: Payload): T {
212
+ return content.data as any;
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Converts between non-undefined values and serialized JSON Payload
218
+ */
219
+ export class JsonPayloadConverter implements PayloadConverterWithEncoding {
220
+ public encodingType = encodingTypes.METADATA_ENCODING_JSON;
221
+
222
+ public toPayload(value: unknown): Payload | undefined {
223
+ if (value === undefined) {
224
+ return undefined;
225
+ }
226
+
227
+ let json;
228
+ try {
229
+ json = JSON.stringify(value);
230
+ } catch (e) {
231
+ return undefined;
232
+ }
233
+
234
+ return {
235
+ metadata: {
236
+ [METADATA_ENCODING_KEY]: encodingKeys.METADATA_ENCODING_JSON,
237
+ },
238
+ data: encode(json),
239
+ };
240
+ }
241
+
242
+ public fromPayload<T>(content: Payload): T {
243
+ if (content.data === undefined || content.data === null) {
244
+ throw new ValueError('Got payload with no data');
245
+ }
246
+ return JSON.parse(decode(content.data));
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Converts Search Attribute values using JsonPayloadConverter
252
+ */
253
+ export class SearchAttributePayloadConverter implements PayloadConverter {
254
+ jsonConverter = new JsonPayloadConverter();
255
+ validNonDateTypes = ['string', 'number', 'boolean'];
256
+
257
+ public toPayload(values: unknown): Payload {
258
+ if (!(values instanceof Array)) {
259
+ throw new ValueError(`SearchAttribute value must be an array`);
260
+ }
261
+
262
+ if (values.length > 0) {
263
+ const firstValue = values[0];
264
+ const firstType = typeof firstValue;
265
+ if (firstType === 'object') {
266
+ for (const idx in values) {
267
+ const value = values[idx];
268
+ if (!(value instanceof Date)) {
269
+ throw new ValueError(
270
+ `SearchAttribute values must arrays of strings, numbers, booleans, or Dates. The value ${value} at index ${idx} is of type ${typeof value}`
271
+ );
272
+ }
273
+ }
274
+ } else {
275
+ if (!this.validNonDateTypes.includes(firstType)) {
276
+ throw new ValueError(`SearchAttribute array values must be: string | number | boolean | Date`);
277
+ }
278
+
279
+ for (const idx in values) {
280
+ const value = values[idx];
281
+ if (typeof value !== firstType) {
282
+ throw new ValueError(
283
+ `All SearchAttribute array values must be of the same type. The first value ${firstValue} of type ${firstType} doesn't match value ${value} of type ${typeof value} at index ${idx}`
284
+ );
285
+ }
286
+ }
287
+ }
288
+ }
289
+
290
+ // JSON.stringify takes care of converting Dates to ISO strings
291
+ const ret = this.jsonConverter.toPayload(values);
292
+ if (ret === undefined) {
293
+ throw new IllegalStateError('Could not convert search attributes to payloads');
294
+ }
295
+ return ret;
296
+ }
297
+
298
+ /**
299
+ * Datetime Search Attribute values are converted to `Date`s
300
+ */
301
+ public fromPayload<T>(payload: Payload): T {
302
+ if (payload.metadata === undefined || payload.metadata === null) {
303
+ throw new ValueError('Missing payload metadata');
304
+ }
305
+
306
+ const value = this.jsonConverter.fromPayload(payload);
307
+ let arrayWrappedValue = value instanceof Array ? value : [value];
308
+
309
+ const searchAttributeType = decode(payload.metadata.type);
310
+ if (searchAttributeType === 'Datetime') {
311
+ arrayWrappedValue = arrayWrappedValue.map((dateString) => new Date(dateString));
312
+ }
313
+ return arrayWrappedValue as unknown as T;
314
+ }
315
+ }
316
+
317
+ export const searchAttributePayloadConverter = new SearchAttributePayloadConverter();
318
+
319
+ export class DefaultPayloadConverter extends CompositePayloadConverter {
320
+ // Match the order used in other SDKs, but exclude Protobuf converters so that the code, including
321
+ // `proto3-json-serializer`, doesn't take space in Workflow bundles that don't use Protobufs. To use Protobufs, use
322
+ // {@link DefaultPayloadConverterWithProtobufs}.
323
+ //
324
+ // Go SDK:
325
+ // https://github.com/temporalio/sdk-go/blob/5e5645f0c550dcf717c095ae32c76a7087d2e985/converter/default_data_converter.go#L28
326
+ constructor() {
327
+ super(new UndefinedPayloadConverter(), new BinaryPayloadConverter(), new JsonPayloadConverter());
328
+ }
329
+ }
330
+
331
+ /**
332
+ * The default {@link PayloadConverter} used by the SDK. Supports `Uint8Array` and JSON serializables (so if
333
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description | `JSON.stringify(yourArgOrRetval)`}
334
+ * works, the default payload converter will work).
335
+ *
336
+ * To also support Protobufs, create a custom payload converter with {@link DefaultPayloadConverter}:
337
+ *
338
+ * `const myConverter = new DefaultPayloadConverter({ protobufRoot })`
339
+ */
340
+ export const defaultPayloadConverter = new DefaultPayloadConverter();
@@ -1,33 +1,22 @@
1
- import {
2
- errorMessage,
3
- hasOwnProperties,
4
- hasOwnProperty,
5
- isRecord,
6
- PayloadConverterError,
7
- ValueError,
8
- } from '@temporalio/internal-workflow-common';
9
1
  import * as protoJsonSerializer from 'proto3-json-serializer';
10
2
  import type { Message, Namespace, Root, Type } from 'protobufjs';
11
- import { JsonPayloadConverter } from './json-payload-converter';
3
+ import { decode, encode } from '../encoding';
4
+ import { PayloadConverterError, ValueError } from '../errors';
5
+ import { Payload } from '../interfaces';
6
+ import { errorMessage, hasOwnProperties, hasOwnProperty, isRecord } from '../type-helpers';
12
7
  import {
13
8
  BinaryPayloadConverter,
14
9
  CompositePayloadConverter,
10
+ JsonPayloadConverter,
15
11
  PayloadConverterWithEncoding,
16
12
  UndefinedPayloadConverter,
17
- } from './payload-converters';
18
- import {
19
- EncodingType,
20
- encodingTypes,
21
- METADATA_ENCODING_KEY,
22
- METADATA_MESSAGE_TYPE_KEY,
23
- Payload,
24
- str,
25
- u8,
26
- } from './types';
13
+ } from './payload-converter';
14
+
15
+ import { encodingTypes, METADATA_ENCODING_KEY, METADATA_MESSAGE_TYPE_KEY } from './types';
27
16
 
28
17
  abstract class ProtobufPayloadConverter implements PayloadConverterWithEncoding {
29
18
  protected readonly root: Root | undefined;
30
- public abstract encodingType: EncodingType;
19
+ public abstract encodingType: string;
31
20
 
32
21
  public abstract toPayload<T>(value: T): Payload | undefined;
33
22
  public abstract fromPayload<T>(payload: Payload): T;
@@ -54,7 +43,7 @@ abstract class ProtobufPayloadConverter implements PayloadConverterWithEncoding
54
43
  throw new PayloadConverterError('Unable to deserialize protobuf message without `root` being provided');
55
44
  }
56
45
 
57
- const messageTypeName = str(content.metadata[METADATA_MESSAGE_TYPE_KEY]);
46
+ const messageTypeName = decode(content.metadata[METADATA_MESSAGE_TYPE_KEY]);
58
47
  let messageType;
59
48
  try {
60
49
  messageType = this.root.lookupType(messageTypeName);
@@ -74,8 +63,8 @@ abstract class ProtobufPayloadConverter implements PayloadConverterWithEncoding
74
63
  protected constructPayload({ messageTypeName, message }: { messageTypeName: string; message: Uint8Array }): Payload {
75
64
  return {
76
65
  metadata: {
77
- [METADATA_ENCODING_KEY]: u8(this.encodingType),
78
- [METADATA_MESSAGE_TYPE_KEY]: u8(messageTypeName),
66
+ [METADATA_ENCODING_KEY]: encode(this.encodingType),
67
+ [METADATA_MESSAGE_TYPE_KEY]: encode(messageTypeName),
79
68
  },
80
69
  data: message,
81
70
  };
@@ -134,13 +123,13 @@ export class ProtobufJsonPayloadConverter extends ProtobufPayloadConverter {
134
123
 
135
124
  return this.constructPayload({
136
125
  messageTypeName: getNamespacedTypeName(value.$type),
137
- message: u8(JSON.stringify(jsonValue)),
126
+ message: encode(JSON.stringify(jsonValue)),
138
127
  });
139
128
  }
140
129
 
141
130
  public fromPayload<T>(content: Payload): T {
142
131
  const { messageType, data } = this.validatePayload(content);
143
- return protoJsonSerializer.fromProto3JSON(messageType, JSON.parse(str(data))) as unknown as T;
132
+ return protoJsonSerializer.fromProto3JSON(messageType, JSON.parse(decode(data))) as unknown as T;
144
133
  }
145
134
  }
146
135
 
@@ -1,17 +1,4 @@
1
- import { TextDecoder, TextEncoder } from './encoding';
2
-
3
- export { Payload } from '@temporalio/internal-workflow-common';
4
-
5
- /**
6
- * Transform an *ascii* string into a Uint8Array
7
- */
8
- export function u8(s: string): Uint8Array {
9
- return new TextEncoder().encode(s);
10
- }
11
-
12
- export function str(a: Uint8Array): string {
13
- return new TextDecoder().decode(a);
14
- }
1
+ import { encode } from '../encoding';
15
2
 
16
3
  export const METADATA_ENCODING_KEY = 'encoding';
17
4
  export const encodingTypes = {
@@ -24,11 +11,11 @@ export const encodingTypes = {
24
11
  export type EncodingType = typeof encodingTypes[keyof typeof encodingTypes];
25
12
 
26
13
  export const encodingKeys = {
27
- METADATA_ENCODING_NULL: u8(encodingTypes.METADATA_ENCODING_NULL),
28
- METADATA_ENCODING_RAW: u8(encodingTypes.METADATA_ENCODING_RAW),
29
- METADATA_ENCODING_JSON: u8(encodingTypes.METADATA_ENCODING_JSON),
30
- METADATA_ENCODING_PROTOBUF_JSON: u8(encodingTypes.METADATA_ENCODING_PROTOBUF_JSON),
31
- METADATA_ENCODING_PROTOBUF: u8(encodingTypes.METADATA_ENCODING_PROTOBUF),
14
+ METADATA_ENCODING_NULL: encode(encodingTypes.METADATA_ENCODING_NULL),
15
+ METADATA_ENCODING_RAW: encode(encodingTypes.METADATA_ENCODING_RAW),
16
+ METADATA_ENCODING_JSON: encode(encodingTypes.METADATA_ENCODING_JSON),
17
+ METADATA_ENCODING_PROTOBUF_JSON: encode(encodingTypes.METADATA_ENCODING_PROTOBUF_JSON),
18
+ METADATA_ENCODING_PROTOBUF: encode(encodingTypes.METADATA_ENCODING_PROTOBUF),
32
19
  } as const;
33
20
 
34
21
  export const METADATA_MESSAGE_TYPE_KEY = 'messageType';
@@ -0,0 +1,80 @@
1
+ import * as time from './time';
2
+ import { Timestamp } from './time';
3
+
4
+ /**
5
+ * Lossy conversion function from Timestamp to number due to possible overflow.
6
+ * If ts is null or undefined returns undefined.
7
+ *
8
+ * @hidden
9
+ * @deprecated - meant for internal use only
10
+ */
11
+ export function optionalTsToMs(ts: Timestamp | null | undefined): number | undefined {
12
+ return time.optionalTsToMs(ts);
13
+ }
14
+
15
+ /**
16
+ * Lossy conversion function from Timestamp to number due to possible overflow
17
+ *
18
+ * @hidden
19
+ * @deprecated - meant for internal use only
20
+ * @deprecated - meant for internal use only
21
+ */
22
+ export function tsToMs(ts: Timestamp | null | undefined): number {
23
+ return time.tsToMs(ts);
24
+ }
25
+
26
+ /**
27
+ * @hidden
28
+ * @deprecated - meant for internal use only
29
+ */
30
+ export function msNumberToTs(millis: number): Timestamp {
31
+ return time.msNumberToTs(millis);
32
+ }
33
+
34
+ /**
35
+ * @hidden
36
+ * @deprecated - meant for internal use only
37
+ */
38
+ export function msToTs(str: string | number): Timestamp {
39
+ return time.msToTs(str);
40
+ }
41
+
42
+ /**
43
+ * @hidden
44
+ * @deprecated - meant for internal use only
45
+ */
46
+ export function msOptionalToTs(str: string | number | undefined): Timestamp | undefined {
47
+ return time.msOptionalToTs(str);
48
+ }
49
+
50
+ /**
51
+ * @hidden
52
+ * @deprecated - meant for internal use only
53
+ */
54
+ export function msOptionalToNumber(val: string | number | undefined): number | undefined {
55
+ return time.msOptionalToNumber(val);
56
+ }
57
+
58
+ /**
59
+ * @hidden
60
+ * @deprecated - meant for internal use only
61
+ */
62
+ export function msToNumber(val: string | number): number {
63
+ return time.msToNumber(val);
64
+ }
65
+
66
+ /**
67
+ * @hidden
68
+ * @deprecated - meant for internal use only
69
+ */
70
+ export function tsToDate(ts: Timestamp): Date {
71
+ return time.tsToDate(ts);
72
+ }
73
+
74
+ /**
75
+ * @hidden
76
+ * @deprecated - meant for internal use only
77
+ */
78
+ export function optionalTsToDate(ts: Timestamp | null | undefined): Date | undefined {
79
+ return time.optionalTsToDate(ts);
80
+ }
@@ -310,3 +310,17 @@ export class TextEncoder {
310
310
  return { written: i, read: inputLength < read ? inputLength : read };
311
311
  }
312
312
  }
313
+
314
+ /**
315
+ * Encode a UTF-8 string into a Uint8Array
316
+ */
317
+ export function encode(s: string): Uint8Array {
318
+ return TextEncoder.prototype.encode(s);
319
+ }
320
+
321
+ /**
322
+ * Decode a Uint8Array into a UTF-8 string
323
+ */
324
+ export function decode(a: Uint8Array): string {
325
+ return TextDecoder.prototype.decode(a);
326
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Thrown from code that receives a value that is unexpected or that it's unable to handle.
3
+ */
4
+ export class ValueError extends Error {
5
+ public readonly name: string = 'ValueError';
6
+
7
+ constructor(message: string | undefined, public readonly cause?: unknown) {
8
+ super(message ?? undefined);
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Thrown when a Payload Converter is misconfigured.
14
+ */
15
+ export class PayloadConverterError extends ValueError {
16
+ public readonly name: string = 'PayloadConverterError';
17
+ }
18
+
19
+ /**
20
+ * Used in different parts of the SDK to note that something unexpected has happened.
21
+ */
22
+ export class IllegalStateError extends Error {
23
+ public readonly name: string = 'IllegalStateError';
24
+ }
25
+
26
+ /**
27
+ * This exception is thrown in the following cases:
28
+ * - Workflow with the same Workflow Id is currently running
29
+ * - There is a closed Workflow with the same Workflow Id and the {@link WorkflowOptions.workflowIdReusePolicy}
30
+ * is `WORKFLOW_ID_REUSE_POLICY_REJECT_DUPLICATE`
31
+ * - There is closed Workflow in the `Completed` state with the same Workflow Id and the {@link WorkflowOptions.workflowIdReusePolicy}
32
+ * is `WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY`
33
+ */
34
+ export class WorkflowExecutionAlreadyStartedError extends Error {
35
+ public readonly name: string = 'WorkflowExecutionAlreadyStartedError';
36
+
37
+ constructor(message: string, public readonly workflowId: string, public readonly workflowType: string) {
38
+ super(message);
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Thrown when a Workflow with the given Id is not known to Temporal Server.
44
+ * It could be because:
45
+ * - Id passed is incorrect
46
+ * - Workflow is closed (for some calls, e.g. `terminate`)
47
+ * - Workflow was deleted from the Server after reaching its retention limit
48
+ */
49
+ export class WorkflowNotFoundError extends Error {
50
+ public readonly name: string = 'WorkflowNotFoundError';
51
+
52
+ constructor(message: string, public readonly workflowId: string, public readonly runId: string | undefined) {
53
+ super(message);
54
+ }
55
+ }