@temporalio/common 1.3.0 → 1.4.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.
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 +25 -8
  33. package/lib/index.js +43 -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 +34 -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
package/src/failure.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { checkExtends, hasOwnProperties, isRecord } from '@temporalio/internal-workflow-common';
2
1
  import type { temporal } from '@temporalio/proto';
3
- import { arrayFromPayloads, fromPayloadsAtIndex, PayloadConverter, toPayloads } from './converter/payload-converter';
2
+ import { checkExtends, isRecord } from './type-helpers';
4
3
 
5
4
  export const FAILURE_SOURCE = 'TypeScriptSDK';
6
5
  export type ProtoFailure = temporal.api.failure.v1.IFailure;
@@ -263,153 +262,6 @@ export class ChildWorkflowFailure extends TemporalFailure {
263
262
  }
264
263
  }
265
264
 
266
- /**
267
- * Converts an error to a Failure proto message if defined or returns undefined
268
- */
269
- export function optionalErrorToOptionalFailure(
270
- err: unknown,
271
- payloadConverter: PayloadConverter
272
- ): ProtoFailure | undefined {
273
- return err ? errorToFailure(err, payloadConverter) : undefined;
274
- }
275
-
276
- /**
277
- * Stack traces will be cutoff when on of these patterns is matched
278
- */
279
- const CUTOFF_STACK_PATTERNS = [
280
- /** Activity execution */
281
- /\s+at Activity\.execute \(.*[\\/]worker[\\/](?:src|lib)[\\/]activity\.[jt]s:\d+:\d+\)/,
282
- /** Workflow activation */
283
- /\s+at Activator\.\S+NextHandler \(.*[\\/]workflow[\\/](?:src|lib)[\\/]internals\.[jt]s:\d+:\d+\)/,
284
- /** Workflow run anything in context */
285
- /\s+at Script\.runInContext \((?:node:vm|vm\.js):\d+:\d+\)/,
286
- ];
287
-
288
- /**
289
- * Cuts out the framework part of a stack trace, leaving only user code entries
290
- */
291
- export function cutoffStackTrace(stack?: string): string {
292
- const lines = (stack ?? '').split(/\r?\n/);
293
- const acc = Array<string>();
294
- lineLoop: for (const line of lines) {
295
- for (const pattern of CUTOFF_STACK_PATTERNS) {
296
- if (pattern.test(line)) break lineLoop;
297
- }
298
- acc.push(line);
299
- }
300
- return acc.join('\n');
301
- }
302
-
303
- /**
304
- * Converts a caught error to a Failure proto message
305
- */
306
- export function errorToFailure(err: unknown, payloadConverter: PayloadConverter): ProtoFailure {
307
- if (err instanceof TemporalFailure) {
308
- if (err.failure) return err.failure;
309
-
310
- const base = {
311
- message: err.message,
312
- stackTrace: cutoffStackTrace(err.stack),
313
- cause: optionalErrorToOptionalFailure(err.cause, payloadConverter),
314
- source: FAILURE_SOURCE,
315
- };
316
- if (err instanceof ActivityFailure) {
317
- return {
318
- ...base,
319
- activityFailureInfo: {
320
- ...err,
321
- activityType: { name: err.activityType },
322
- },
323
- };
324
- }
325
- if (err instanceof ChildWorkflowFailure) {
326
- return {
327
- ...base,
328
- childWorkflowExecutionFailureInfo: {
329
- ...err,
330
- workflowExecution: err.execution,
331
- workflowType: { name: err.workflowType },
332
- },
333
- };
334
- }
335
- if (err instanceof ApplicationFailure) {
336
- return {
337
- ...base,
338
- applicationFailureInfo: {
339
- type: err.type,
340
- nonRetryable: err.nonRetryable,
341
- details:
342
- err.details && err.details.length ? { payloads: toPayloads(payloadConverter, ...err.details) } : undefined,
343
- },
344
- };
345
- }
346
- if (err instanceof CancelledFailure) {
347
- return {
348
- ...base,
349
- canceledFailureInfo: {
350
- details:
351
- err.details && err.details.length ? { payloads: toPayloads(payloadConverter, ...err.details) } : undefined,
352
- },
353
- };
354
- }
355
- if (err instanceof TimeoutFailure) {
356
- return {
357
- ...base,
358
- timeoutFailureInfo: {
359
- timeoutType: err.timeoutType,
360
- lastHeartbeatDetails: err.lastHeartbeatDetails
361
- ? { payloads: toPayloads(payloadConverter, err.lastHeartbeatDetails) }
362
- : undefined,
363
- },
364
- };
365
- }
366
- if (err instanceof TerminatedFailure) {
367
- return {
368
- ...base,
369
- terminatedFailureInfo: {},
370
- };
371
- }
372
- if (err instanceof ServerFailure) {
373
- return {
374
- ...base,
375
- serverFailureInfo: { nonRetryable: err.nonRetryable },
376
- };
377
- }
378
- // Just a TemporalFailure
379
- return base;
380
- }
381
-
382
- const base = {
383
- source: FAILURE_SOURCE,
384
- };
385
-
386
- if (isRecord(err) && hasOwnProperties(err, ['message', 'stack'])) {
387
- return {
388
- ...base,
389
- message: String(err.message) ?? '',
390
- stackTrace: cutoffStackTrace(String(err.stack)),
391
- cause: optionalErrorToOptionalFailure(err.cause, payloadConverter),
392
- };
393
- }
394
-
395
- const recommendation = ` [A non-Error value was thrown from your code. We recommend throwing Error objects so that we can provide a stack trace]`;
396
-
397
- if (typeof err === 'string') {
398
- return { ...base, message: err + recommendation };
399
- }
400
- if (typeof err === 'object') {
401
- let message = '';
402
- try {
403
- message = JSON.stringify(err);
404
- } catch (_err) {
405
- message = String(err);
406
- }
407
- return { ...base, message: message + recommendation };
408
- }
409
-
410
- return { ...base, message: String(err) + recommendation };
411
- }
412
-
413
265
  /**
414
266
  * If `error` is already an `ApplicationFailure`, returns `error`.
415
267
  *
@@ -445,108 +297,6 @@ export function ensureTemporalFailure(err: unknown): TemporalFailure {
445
297
  return ensureApplicationFailure(err);
446
298
  }
447
299
 
448
- /**
449
- * Converts a Failure proto message to a JS Error object if defined or returns undefined.
450
- */
451
- export function optionalFailureToOptionalError(
452
- failure: ProtoFailure | undefined | null,
453
- payloadConverter: PayloadConverter
454
- ): TemporalFailure | undefined {
455
- return failure ? failureToError(failure, payloadConverter) : undefined;
456
- }
457
-
458
- /**
459
- * Converts a Failure proto message to a JS Error object.
460
- *
461
- * Does not set common properties, that is done in {@link failureToError}.
462
- */
463
- export function failureToErrorInner(failure: ProtoFailure, payloadConverter: PayloadConverter): TemporalFailure {
464
- if (failure.applicationFailureInfo) {
465
- return new ApplicationFailure(
466
- failure.message ?? undefined,
467
- failure.applicationFailureInfo.type,
468
- Boolean(failure.applicationFailureInfo.nonRetryable),
469
- arrayFromPayloads(payloadConverter, failure.applicationFailureInfo.details?.payloads),
470
- optionalFailureToOptionalError(failure.cause, payloadConverter)
471
- );
472
- }
473
- if (failure.serverFailureInfo) {
474
- return new ServerFailure(
475
- failure.message ?? undefined,
476
- Boolean(failure.serverFailureInfo.nonRetryable),
477
- optionalFailureToOptionalError(failure.cause, payloadConverter)
478
- );
479
- }
480
- if (failure.timeoutFailureInfo) {
481
- return new TimeoutFailure(
482
- failure.message ?? undefined,
483
- fromPayloadsAtIndex(payloadConverter, 0, failure.timeoutFailureInfo.lastHeartbeatDetails?.payloads),
484
- failure.timeoutFailureInfo.timeoutType ?? TimeoutType.TIMEOUT_TYPE_UNSPECIFIED
485
- );
486
- }
487
- if (failure.terminatedFailureInfo) {
488
- return new TerminatedFailure(
489
- failure.message ?? undefined,
490
- optionalFailureToOptionalError(failure.cause, payloadConverter)
491
- );
492
- }
493
- if (failure.canceledFailureInfo) {
494
- return new CancelledFailure(
495
- failure.message ?? undefined,
496
- arrayFromPayloads(payloadConverter, failure.canceledFailureInfo.details?.payloads),
497
- optionalFailureToOptionalError(failure.cause, payloadConverter)
498
- );
499
- }
500
- if (failure.resetWorkflowFailureInfo) {
501
- return new ApplicationFailure(
502
- failure.message ?? undefined,
503
- 'ResetWorkflow',
504
- false,
505
- arrayFromPayloads(payloadConverter, failure.resetWorkflowFailureInfo.lastHeartbeatDetails?.payloads),
506
- optionalFailureToOptionalError(failure.cause, payloadConverter)
507
- );
508
- }
509
- if (failure.childWorkflowExecutionFailureInfo) {
510
- const { namespace, workflowType, workflowExecution, retryState } = failure.childWorkflowExecutionFailureInfo;
511
- if (!(workflowType?.name && workflowExecution)) {
512
- throw new TypeError('Missing attributes on childWorkflowExecutionFailureInfo');
513
- }
514
- return new ChildWorkflowFailure(
515
- namespace ?? undefined,
516
- workflowExecution,
517
- workflowType.name,
518
- retryState ?? RetryState.RETRY_STATE_UNSPECIFIED,
519
- optionalFailureToOptionalError(failure.cause, payloadConverter)
520
- );
521
- }
522
- if (failure.activityFailureInfo) {
523
- if (!failure.activityFailureInfo.activityType?.name) {
524
- throw new TypeError('Missing activityType?.name on activityFailureInfo');
525
- }
526
- return new ActivityFailure(
527
- failure.activityFailureInfo.activityType.name,
528
- failure.activityFailureInfo.activityId ?? undefined,
529
- failure.activityFailureInfo.retryState ?? RetryState.RETRY_STATE_UNSPECIFIED,
530
- failure.activityFailureInfo.identity ?? undefined,
531
- optionalFailureToOptionalError(failure.cause, payloadConverter)
532
- );
533
- }
534
- return new TemporalFailure(
535
- failure.message ?? undefined,
536
- optionalFailureToOptionalError(failure.cause, payloadConverter)
537
- );
538
- }
539
-
540
- /**
541
- * Converts a Failure proto message to a JS Error object.
542
- */
543
- export function failureToError(failure: ProtoFailure, payloadConverter: PayloadConverter): TemporalFailure {
544
- const err = failureToErrorInner(failure, payloadConverter);
545
- err.stack = failure.stackTrace ?? '';
546
- err.failure = failure;
547
- return err;
548
- }
549
-
550
300
  /**
551
301
  * Get the root cause message of given `error`.
552
302
  *
package/src/index.ts CHANGED
@@ -3,16 +3,42 @@
3
3
  *
4
4
  * @module
5
5
  */
6
- export { Headers, Next, RetryPolicy } from '@temporalio/internal-workflow-common';
7
- export * from '@temporalio/internal-workflow-common/lib/activity-options';
8
- export * from '@temporalio/internal-workflow-common/lib/errors';
9
- export * from '@temporalio/internal-workflow-common/lib/interfaces';
10
- export * from '@temporalio/internal-workflow-common/lib/time';
11
- export * from '@temporalio/internal-workflow-common/lib/workflow-options';
6
+ export { Headers, Next } from './interceptors';
7
+
8
+ export * from './activity-options';
12
9
  export * from './converter/data-converter';
10
+ export * from './converter/failure-converter';
13
11
  export * from './converter/payload-codec';
14
12
  export * from './converter/payload-converter';
15
- export * from './converter/payload-converters';
16
- export * from './converter/json-payload-converter';
17
13
  export * from './converter/types';
14
+ export * from './errors';
18
15
  export * from './failure';
16
+ export * from './failure';
17
+ export * from './interfaces';
18
+ export * from './retry-policy';
19
+ export { Timestamp } from './time';
20
+ export * from './workflow-options';
21
+ export * from './workflow-handle';
22
+ export * from './deprecated-time';
23
+
24
+ import * as encoding from './encoding';
25
+
26
+ /**
27
+ * Encode a UTF-8 string into a Uint8Array
28
+ *
29
+ * @hidden
30
+ * @deprecated - meant for internal use only
31
+ */
32
+ export function u8(s: string): Uint8Array {
33
+ return encoding.encode(s);
34
+ }
35
+
36
+ /**
37
+ * Decode a Uint8Array into a UTF-8 string
38
+ *
39
+ * @hidden
40
+ * @deprecated - meant for internal use only
41
+ */
42
+ export function str(arr: Uint8Array): string {
43
+ return encoding.decode(arr);
44
+ }
@@ -0,0 +1,32 @@
1
+ import { AnyFunc, OmitLastParam } from './type-helpers';
2
+ import { Payload } from './interfaces';
3
+
4
+ /**
5
+ * Type of the next function for a given interceptor function
6
+ *
7
+ * Called from an interceptor to continue the interception chain
8
+ */
9
+ export type Next<IF, FN extends keyof IF> = Required<IF>[FN] extends AnyFunc ? OmitLastParam<Required<IF>[FN]> : never;
10
+
11
+ /** Headers are just a mapping of header name to Payload */
12
+ export type Headers = Record<string, Payload>;
13
+
14
+ /**
15
+ * Composes all interceptor methods into a single function
16
+ *
17
+ * @param interceptors a list of interceptors
18
+ * @param method the name of the interceptor method to compose
19
+ * @param next the original function to be executed at the end of the interception chain
20
+ */
21
+ export function composeInterceptors<I, M extends keyof I>(interceptors: I[], method: M, next: Next<I, M>): Next<I, M> {
22
+ for (let i = interceptors.length - 1; i >= 0; --i) {
23
+ const interceptor = interceptors[i];
24
+ if (interceptor[method] !== undefined) {
25
+ const prev = next;
26
+ // We loose type safety here because Typescript can't deduce that interceptor[method] is a function that returns
27
+ // the same type as Next<I, M>
28
+ next = ((input: any) => (interceptor[method] as any)(input, prev)) as any;
29
+ }
30
+ }
31
+ return next;
32
+ }
@@ -0,0 +1,64 @@
1
+ import type { temporal } from '@temporalio/proto';
2
+
3
+ export type Payload = temporal.api.common.v1.IPayload;
4
+
5
+ /** Type that can be returned from a Workflow `execute` function */
6
+ export type WorkflowReturnType = Promise<any>;
7
+ export type WorkflowSignalType = (...args: any[]) => Promise<void> | void;
8
+ export type WorkflowQueryType = (...args: any[]) => any;
9
+
10
+ /**
11
+ * Broad Workflow function definition, specific Workflows will typically use a narrower type definition, e.g:
12
+ * ```ts
13
+ * export async function myWorkflow(arg1: number, arg2: string): Promise<string>;
14
+ * ```
15
+ */
16
+ export type Workflow = (...args: any[]) => WorkflowReturnType;
17
+
18
+ /**
19
+ * An interface representing a Workflow signal definition, as returned from {@link defineSignal}
20
+ *
21
+ * @remarks `_Args` can be used for parameter type inference in handler functions and *WorkflowHandle methods.
22
+ */
23
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
24
+ export interface SignalDefinition<_Args extends any[] = []> {
25
+ type: 'signal';
26
+ name: string;
27
+ }
28
+
29
+ /**
30
+ * An interface representing a Workflow query definition as returned from {@link defineQuery}
31
+ *
32
+ * @remarks `_Args` and `_Ret` can be used for parameter type inference in handler functions and *WorkflowHandle methods.
33
+ */
34
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
35
+ export interface QueryDefinition<_Ret, _Args extends any[] = []> {
36
+ type: 'query';
37
+ name: string;
38
+ }
39
+
40
+ /** Get the "unwrapped" return type (without Promise) of the execute handler from Workflow type `W` */
41
+ export type WorkflowResultType<W extends Workflow> = ReturnType<W> extends Promise<infer R> ? R : never;
42
+
43
+ /**
44
+ * If another SDK creates a Search Attribute that's not an array, we wrap it in an array.
45
+ *
46
+ * Dates are serialized as ISO strings.
47
+ */
48
+ export type SearchAttributes = Record<string, SearchAttributeValue | undefined>;
49
+ export type SearchAttributeValue = string[] | number[] | boolean[] | Date[];
50
+
51
+ export interface ActivityFunction<P extends any[] = any[], R = any> {
52
+ (...args: P): Promise<R>;
53
+ }
54
+
55
+ /**
56
+ * Mapping of Activity name to function
57
+ * @deprecated not required anymore, for untyped activities use {@link UntypedActivities}
58
+ */
59
+ export type ActivityInterface = Record<string, ActivityFunction>;
60
+
61
+ /**
62
+ * Mapping of Activity name to function
63
+ */
64
+ export type UntypedActivities = Record<string, ActivityFunction>;