@temporalio/common 0.23.0 → 1.0.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 (59) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +3 -3
  3. package/lib/converter/data-converter.d.ts +5 -5
  4. package/lib/converter/data-converter.js +1 -2
  5. package/lib/converter/data-converter.js.map +1 -1
  6. package/lib/converter/json-payload-converter.d.ts +10 -0
  7. package/lib/converter/json-payload-converter.js +39 -0
  8. package/lib/converter/json-payload-converter.js.map +1 -0
  9. package/lib/converter/payload-converter.d.ts +11 -15
  10. package/lib/converter/payload-converter.js +5 -14
  11. package/lib/converter/payload-converter.js.map +1 -1
  12. package/lib/converter/payload-converters.d.ts +19 -15
  13. package/lib/converter/payload-converters.js +10 -41
  14. package/lib/converter/payload-converters.js.map +1 -1
  15. package/lib/converter/protobuf-payload-converters.js +2 -1
  16. package/lib/converter/protobuf-payload-converters.js.map +1 -1
  17. package/lib/converter/search-attribute-payload-converter.d.ts +12 -0
  18. package/lib/converter/search-attribute-payload-converter.js +64 -0
  19. package/lib/converter/search-attribute-payload-converter.js.map +1 -0
  20. package/lib/converter/types.d.ts +1 -2
  21. package/lib/converter/types.js.map +1 -1
  22. package/lib/failure.d.ts +58 -46
  23. package/lib/failure.js +85 -56
  24. package/lib/failure.js.map +1 -1
  25. package/lib/index.d.ts +1 -0
  26. package/lib/index.js +1 -0
  27. package/lib/index.js.map +1 -1
  28. package/lib/otel.d.ts +26 -0
  29. package/lib/otel.js +87 -0
  30. package/lib/otel.js.map +1 -0
  31. package/lib/proto-utils.d.ts +28 -0
  32. package/lib/proto-utils.js +85 -0
  33. package/lib/proto-utils.js.map +1 -0
  34. package/lib/protobufs.d.ts +2 -2
  35. package/lib/protobufs.js +4 -2
  36. package/lib/protobufs.js.map +1 -1
  37. package/package.json +12 -9
  38. package/src/converter/data-converter.ts +6 -6
  39. package/src/converter/json-payload-converter.ts +37 -0
  40. package/src/converter/payload-converter.ts +15 -29
  41. package/src/converter/payload-converters.ts +24 -43
  42. package/src/converter/protobuf-payload-converters.ts +1 -1
  43. package/src/converter/search-attribute-payload-converter.ts +71 -0
  44. package/src/converter/types.ts +1 -2
  45. package/src/failure.ts +92 -62
  46. package/src/index.ts +1 -0
  47. package/src/otel.ts +63 -0
  48. package/src/proto-utils.ts +103 -0
  49. package/src/protobufs.ts +2 -2
  50. package/lib/converter/patch-protobuf-root.d.ts +0 -1
  51. package/lib/converter/patch-protobuf-root.js +0 -6
  52. package/lib/converter/patch-protobuf-root.js.map +0 -1
  53. package/lib/converter/wrapped-payload-converter.d.ts +0 -12
  54. package/lib/converter/wrapped-payload-converter.js +0 -28
  55. package/lib/converter/wrapped-payload-converter.js.map +0 -1
  56. package/src/converter/patch-protobuf-root.ts +0 -1
  57. package/src/converter/wrapped-payload-converter.ts +0 -31
  58. package/tsconfig.json +0 -9
  59. package/tsconfig.tsbuildinfo +0 -1
package/lib/otel.js ADDED
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.linkSpans = exports.headersWithContext = exports.extractSpanContextFromHeaders = exports.extractContextFromHeaders = exports.NUM_JOBS_ATTR_KEY = exports.TASK_TOKEN_ATTR_KEY = exports.RUN_ID_ATTR_KEY = exports.TRACE_HEADER = void 0;
27
+ const otel = __importStar(require("@opentelemetry/api"));
28
+ const payload_converters_1 = require("./converter/payload-converters");
29
+ /** Default trace header for opentelemetry interceptors */
30
+ exports.TRACE_HEADER = '_tracer-data';
31
+ /** As in workflow run id */
32
+ exports.RUN_ID_ATTR_KEY = 'run_id';
33
+ /** For a workflow or activity task */
34
+ exports.TASK_TOKEN_ATTR_KEY = 'task_token';
35
+ /** Number of jobs in a workflow activation */
36
+ exports.NUM_JOBS_ATTR_KEY = 'num_jobs';
37
+ const payloadConverter = payload_converters_1.defaultPayloadConverter;
38
+ /**
39
+ * If found, return an otel Context deserialized from the provided headers
40
+ */
41
+ async function extractContextFromHeaders(headers) {
42
+ const encodedSpanContext = headers[exports.TRACE_HEADER];
43
+ if (encodedSpanContext === undefined) {
44
+ return undefined;
45
+ }
46
+ const textMap = payloadConverter.fromPayload(encodedSpanContext);
47
+ return otel.propagation.extract(otel.context.active(), textMap, otel.defaultTextMapGetter);
48
+ }
49
+ exports.extractContextFromHeaders = extractContextFromHeaders;
50
+ /**
51
+ * If found, return an otel SpanContext deserialized from the provided headers
52
+ */
53
+ async function extractSpanContextFromHeaders(headers) {
54
+ const context = await extractContextFromHeaders(headers);
55
+ if (context === undefined) {
56
+ return undefined;
57
+ }
58
+ return otel.trace.getSpanContext(context);
59
+ }
60
+ exports.extractSpanContextFromHeaders = extractSpanContextFromHeaders;
61
+ /**
62
+ * Given headers, return new headers with the current otel context inserted
63
+ */
64
+ async function headersWithContext(headers) {
65
+ const carrier = {};
66
+ otel.propagation.inject(otel.context.active(), carrier, otel.defaultTextMapSetter);
67
+ return { ...headers, [exports.TRACE_HEADER]: payloadConverter.toPayload(carrier) };
68
+ }
69
+ exports.headersWithContext = headersWithContext;
70
+ /**
71
+ * Link a span to an maybe-existing span context
72
+ */
73
+ function linkSpans(fromSpan, toContext) {
74
+ if (toContext !== undefined) {
75
+ // TODO: I have to go around typescript because otel api 😢
76
+ // See https://github.com/open-telemetry/opentelemetry-js-api/issues/124
77
+ const links = fromSpan.links;
78
+ if (links === undefined) {
79
+ fromSpan.links = [{ context: toContext }];
80
+ }
81
+ else {
82
+ links.push({ context: toContext });
83
+ }
84
+ }
85
+ }
86
+ exports.linkSpans = linkSpans;
87
+ //# sourceMappingURL=otel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel.js","sourceRoot":"","sources":["../src/otel.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yDAA2C;AAE3C,uEAAyE;AAEzE,0DAA0D;AAC7C,QAAA,YAAY,GAAG,cAAc,CAAC;AAC3C,4BAA4B;AACf,QAAA,eAAe,GAAG,QAAQ,CAAC;AACxC,sCAAsC;AACzB,QAAA,mBAAmB,GAAG,YAAY,CAAC;AAChD,8CAA8C;AACjC,QAAA,iBAAiB,GAAG,UAAU,CAAC;AAE5C,MAAM,gBAAgB,GAAG,4CAAuB,CAAC;AAEjD;;GAEG;AACI,KAAK,UAAU,yBAAyB,CAAC,OAAgB;IAC9D,MAAM,kBAAkB,GAAG,OAAO,CAAC,oBAAY,CAAC,CAAC;IACjD,IAAI,kBAAkB,KAAK,SAAS,EAAE;QACpC,OAAO,SAAS,CAAC;KAClB;IACD,MAAM,OAAO,GAA2B,gBAAgB,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAC;IACzF,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;AAC7F,CAAC;AAPD,8DAOC;AAED;;GAEG;AACI,KAAK,UAAU,6BAA6B,CAAC,OAAgB;IAClE,MAAM,OAAO,GAAG,MAAM,yBAAyB,CAAC,OAAO,CAAC,CAAC;IACzD,IAAI,OAAO,KAAK,SAAS,EAAE;QACzB,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;AAC5C,CAAC;AAPD,sEAOC;AAED;;GAEG;AACI,KAAK,UAAU,kBAAkB,CAAC,OAAgB;IACvD,MAAM,OAAO,GAAG,EAAE,CAAC;IACnB,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;IACnF,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC,oBAAY,CAAC,EAAE,gBAAgB,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;AAC7E,CAAC;AAJD,gDAIC;AAED;;GAEG;AACH,SAAgB,SAAS,CAAC,QAAmB,EAAE,SAA4B;IACzE,IAAI,SAAS,KAAK,SAAS,EAAE;QAC3B,2DAA2D;QAC3D,yEAAyE;QACzE,MAAM,KAAK,GAAI,QAAgB,CAAC,KAAK,CAAC;QACtC,IAAI,KAAK,KAAK,SAAS,EAAE;YACtB,QAAgB,CAAC,KAAK,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC;SACpD;aAAM;YACL,KAAK,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC;SACpC;KACF;AACH,CAAC;AAXD,8BAWC"}
@@ -0,0 +1,28 @@
1
+ import proto from '@temporalio/proto';
2
+ export declare type History = proto.temporal.api.history.v1.IHistory;
3
+ export declare type Payload = proto.temporal.api.common.v1.IPayload;
4
+ /**
5
+ * Convert a proto JSON representation of History to a valid History object
6
+ */
7
+ export declare function historyFromJSON(history: unknown): History;
8
+ /**
9
+ * JSON representation of Temporal's {@link Payload} protobuf object
10
+ */
11
+ export interface JSONPayload {
12
+ /**
13
+ * Mapping of key to base64 encoded value
14
+ */
15
+ metadata?: Record<string, string> | null;
16
+ /**
17
+ * base64 encoded value
18
+ */
19
+ data?: string | null;
20
+ }
21
+ /**
22
+ * Convert from protobuf payload to JSON
23
+ */
24
+ export declare function payloadToJSON(payload: Payload): JSONPayload;
25
+ /**
26
+ * Convert from JSON to protobuf payload
27
+ */
28
+ export declare function JSONToPayload(json: JSONPayload): Payload;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.JSONToPayload = exports.payloadToJSON = exports.historyFromJSON = void 0;
7
+ const proto_1 = __importDefault(require("@temporalio/proto"));
8
+ const proto3_json_serializer_1 = require("proto3-json-serializer");
9
+ const patch_protobuf_root_1 = require("@temporalio/proto/lib/patch-protobuf-root");
10
+ // Cast to any because the generated proto module types are missing the lookupType method
11
+ const patched = (0, patch_protobuf_root_1.patchProtobufRoot)(proto_1.default);
12
+ const historyType = patched.lookupType('temporal.api.history.v1.History');
13
+ const payloadType = patched.lookupType('temporal.api.common.v1.Payload');
14
+ function pascalCaseToConstantCase(s) {
15
+ return s.replace(/[^\b][A-Z]/g, (m) => `${m[0]}_${m[1]}`).toUpperCase();
16
+ }
17
+ function fixEnumValue(obj, attr, prefix) {
18
+ return (obj[attr] && {
19
+ [attr]: `${prefix}_${pascalCaseToConstantCase(obj[attr])}`,
20
+ });
21
+ }
22
+ function fixHistoryEvent(e) {
23
+ const type = Object.keys(e).find((k) => k.endsWith('EventAttributes'));
24
+ if (!type) {
25
+ throw new TypeError(`Missing attributes in history event: ${JSON.stringify(e)}`);
26
+ }
27
+ return {
28
+ ...e,
29
+ ...fixEnumValue(e, 'eventType', 'EVENT_TYPE'),
30
+ [type]: {
31
+ ...e[type],
32
+ ...(e[type].taskQueue && {
33
+ taskQueue: { ...e[type].taskQueue, ...fixEnumValue(e[type].taskQueue, 'kind', 'TASK_QUEUE_KIND') },
34
+ }),
35
+ ...fixEnumValue(e[type], 'parentClosePolicy', 'PARENT_CLOSE_POLICY'),
36
+ ...fixEnumValue(e[type], 'workflowIdReusePolicy', 'WORKFLOW_ID_REUSE_POLICY'),
37
+ ...fixEnumValue(e[type], 'initiator', 'CONTINUE_AS_NEW_INITIATOR'),
38
+ ...fixEnumValue(e[type], 'retryState', 'RETRY_STATE'),
39
+ ...(e[type].childWorkflowExecutionFailureInfo && {
40
+ childWorkflowExecutionFailureInfo: {
41
+ ...e[type].childWorkflowExecutionFailureInfo,
42
+ ...fixEnumValue(e[type].childWorkflowExecutionFailureInfo, 'retryState', 'RETRY_STATE'),
43
+ },
44
+ }),
45
+ },
46
+ };
47
+ }
48
+ function fixHistory(h) {
49
+ return {
50
+ events: h.events.map(fixHistoryEvent),
51
+ };
52
+ }
53
+ /**
54
+ * Convert a proto JSON representation of History to a valid History object
55
+ */
56
+ function historyFromJSON(history) {
57
+ if (typeof history !== 'object' || history == null || !Array.isArray(history.events)) {
58
+ throw new TypeError('Invalid history, expected an object with an array of events');
59
+ }
60
+ const loaded = (0, proto3_json_serializer_1.fromProto3JSON)(historyType, fixHistory(history));
61
+ if (loaded === null) {
62
+ throw new TypeError('Invalid history');
63
+ }
64
+ return loaded;
65
+ }
66
+ exports.historyFromJSON = historyFromJSON;
67
+ /**
68
+ * Convert from protobuf payload to JSON
69
+ */
70
+ function payloadToJSON(payload) {
71
+ return (0, proto3_json_serializer_1.toProto3JSON)(patched.temporal.api.common.v1.Payload.create(payload));
72
+ }
73
+ exports.payloadToJSON = payloadToJSON;
74
+ /**
75
+ * Convert from JSON to protobuf payload
76
+ */
77
+ function JSONToPayload(json) {
78
+ const loaded = (0, proto3_json_serializer_1.fromProto3JSON)(payloadType, json);
79
+ if (loaded === null) {
80
+ throw new TypeError('Invalid payload');
81
+ }
82
+ return loaded;
83
+ }
84
+ exports.JSONToPayload = JSONToPayload;
85
+ //# sourceMappingURL=proto-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proto-utils.js","sourceRoot":"","sources":["../src/proto-utils.ts"],"names":[],"mappings":"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"}
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Entry point for classes and utilities related to using
3
- * [Protobufs](https://docs.temporal.io/docs/typescript/data-converters#protobufs) for serialization.
3
+ * {@link https://docs.temporal.io/typescript/data-converters#protobufs | Protobufs} for serialization.
4
4
  *
5
5
  * Import from `@temporalio/common/lib/protobufs`, for example:
6
6
  *
@@ -10,4 +10,4 @@
10
10
  * @module
11
11
  */
12
12
  export * from './converter/protobuf-payload-converters';
13
- export * from './converter/patch-protobuf-root';
13
+ export { patchProtobufRoot } from '@temporalio/proto/lib/patch-protobuf-root';
package/lib/protobufs.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /**
3
3
  * Entry point for classes and utilities related to using
4
- * [Protobufs](https://docs.temporal.io/docs/typescript/data-converters#protobufs) for serialization.
4
+ * {@link https://docs.temporal.io/typescript/data-converters#protobufs | Protobufs} for serialization.
5
5
  *
6
6
  * Import from `@temporalio/common/lib/protobufs`, for example:
7
7
  *
@@ -25,7 +25,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
25
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
26
  };
27
27
  Object.defineProperty(exports, "__esModule", { value: true });
28
+ exports.patchProtobufRoot = void 0;
28
29
  // Don't export from index, so we save space in Workflow bundles of users who don't use Protobufs
29
30
  __exportStar(require("./converter/protobuf-payload-converters"), exports);
30
- __exportStar(require("./converter/patch-protobuf-root"), exports);
31
+ var patch_protobuf_root_1 = require("@temporalio/proto/lib/patch-protobuf-root");
32
+ Object.defineProperty(exports, "patchProtobufRoot", { enumerable: true, get: function () { return patch_protobuf_root_1.patchProtobufRoot; } });
31
33
  //# sourceMappingURL=protobufs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"protobufs.js","sourceRoot":"","sources":["../src/protobufs.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;;;;;;;;;;;AAEH,iGAAiG;AACjG,0EAAwD;AACxD,kEAAgD"}
1
+ {"version":3,"file":"protobufs.js","sourceRoot":"","sources":["../src/protobufs.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;;;;;;;;;;;;;;;AAEH,iGAAiG;AACjG,0EAAwD;AACxD,iFAA8E;AAArE,wHAAA,iBAAiB,OAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temporalio/common",
3
- "version": "0.23.0",
3
+ "version": "1.0.0",
4
4
  "description": "Common library for code that's used across the Client, Worker, and/or Workflow",
5
5
  "main": "lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -9,12 +9,14 @@
9
9
  "workflow",
10
10
  "worker"
11
11
  ],
12
- "author": "Loren Sands-Ramshaw <loren@temporal.io>",
12
+ "author": "Temporal Technologies Inc. <sdk@temporal.io>",
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@temporalio/internal-workflow-common": "^0.23.0",
16
- "@temporalio/proto": "^0.23.0",
17
- "proto3-json-serializer": "^0.1.6"
15
+ "@opentelemetry/api": "^1.0.3",
16
+ "@temporalio/internal-workflow-common": "^1.0.0",
17
+ "@temporalio/proto": "^1.0.0",
18
+ "proto3-json-serializer": "^1.0.2",
19
+ "protobufjs": "6.11.2"
18
20
  },
19
21
  "bugs": {
20
22
  "url": "https://github.com/temporalio/sdk-typescript/issues"
@@ -23,8 +25,9 @@
23
25
  "publishConfig": {
24
26
  "access": "public"
25
27
  },
26
- "devDependencies": {
27
- "protobufjs": "^6.11.2"
28
- },
29
- "gitHead": "81ee3fd09c2fd866b31b1dbfabce7ef221e338ea"
28
+ "files": [
29
+ "src",
30
+ "lib"
31
+ ],
32
+ "gitHead": "c4fc4dc608bf58701c11b6ae02d1d63b4457718d"
30
33
  }
@@ -1,15 +1,15 @@
1
1
  import { PayloadCodec } from './payload-codec';
2
+ import { PayloadConverter } from './payload-converter';
2
3
  import { defaultPayloadConverter } from './payload-converters';
3
- import { WrappedPayloadConverter } from './wrapped-payload-converter';
4
4
 
5
5
  /**
6
6
  * When your data (arguments and return values) is sent over the wire and stored by Temporal Server, it is encoded in
7
7
  * binary in a {@link Payload} Protobuf message.
8
8
  *
9
9
  * The default `DataConverter` supports `undefined`, `Uint8Array`, and JSON serializables (so if
10
- * [`JSON.stringify(yourArgOrRetval)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description)
11
- * works, the default data converter will work). Protobufs are supported via [this
12
- * API](https://docs.temporal.io/docs/typescript/data-converters#protobufs).
10
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description | `JSON.stringify(yourArgOrRetval)`}
11
+ * works, the default data converter will work). Protobufs are supported via
12
+ * {@link https://docs.temporal.io/typescript/data-converters#protobufs | this API}.
13
13
  *
14
14
  * Use a custom `DataConverter` to control the contents of your {@link Payload}s. Common reasons for using a custom
15
15
  * `DataConverter` are:
@@ -48,11 +48,11 @@ export interface DataConverter {
48
48
  * A {@link DataConverter} that has been loaded via {@link loadDataConverter}.
49
49
  */
50
50
  export interface LoadedDataConverter {
51
- payloadConverter: WrappedPayloadConverter;
51
+ payloadConverter: PayloadConverter;
52
52
  payloadCodecs: PayloadCodec[];
53
53
  }
54
54
 
55
55
  export const defaultDataConverter: LoadedDataConverter = {
56
- payloadConverter: new WrappedPayloadConverter(defaultPayloadConverter),
56
+ payloadConverter: defaultPayloadConverter,
57
57
  payloadCodecs: [],
58
58
  };
@@ -0,0 +1,37 @@
1
+ import { ValueError } from '@temporalio/internal-workflow-common';
2
+ import { PayloadConverterWithEncoding } from './payload-converters';
3
+ import { encodingKeys, encodingTypes, METADATA_ENCODING_KEY, Payload, str, u8 } from './types';
4
+
5
+ /**
6
+ * Converts between non-undefined values and serialized JSON Payload
7
+ */
8
+ export class JsonPayloadConverter implements PayloadConverterWithEncoding {
9
+ public encodingType = encodingTypes.METADATA_ENCODING_JSON;
10
+
11
+ public toPayload(value: unknown): Payload | undefined {
12
+ if (value === undefined) {
13
+ return undefined;
14
+ }
15
+
16
+ let json;
17
+ try {
18
+ json = JSON.stringify(value);
19
+ } catch (e) {
20
+ return undefined;
21
+ }
22
+
23
+ return {
24
+ metadata: {
25
+ [METADATA_ENCODING_KEY]: encodingKeys.METADATA_ENCODING_JSON,
26
+ },
27
+ data: u8(json),
28
+ };
29
+ }
30
+
31
+ public fromPayload<T>(content: Payload): T {
32
+ if (content.data === undefined || content.data === null) {
33
+ throw new ValueError('Got payload with no data');
34
+ }
35
+ return JSON.parse(str(content.data));
36
+ }
37
+ }
@@ -1,10 +1,9 @@
1
1
  import { Payload } from './types';
2
- import { WrappedPayloadConverter } from './wrapped-payload-converter';
3
2
 
4
3
  /**
5
4
  * Used by the framework to serialize/deserialize data like parameters and return values.
6
5
  *
7
- * This is called inside the [Workflow isolate](https://docs.temporal.io/docs/typescript/determinism).
6
+ * This is called inside the {@link https://docs.temporal.io/typescript/determinism | Workflow isolate}.
8
7
  * To write async code or use Node APIs (or use packages that use Node APIs), use a {@link PayloadCodec}.
9
8
  */
10
9
  export interface PayloadConverter {
@@ -12,9 +11,12 @@ export interface PayloadConverter {
12
11
  * Converts a value to a {@link Payload}.
13
12
  *
14
13
  * @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.
15
- * @returns The {@link Payload}, or `undefined` if unable to convert.
14
+ *
15
+ * @returns The {@link Payload}.
16
+ *
17
+ * Should throw {@link ValueError} if unable to convert.
16
18
  */
17
- toPayload<T>(value: T): Payload | undefined;
19
+ toPayload<T>(value: T): Payload;
18
20
 
19
21
  /**
20
22
  * Converts a {@link Payload} back to a value.
@@ -22,15 +24,6 @@ export interface PayloadConverter {
22
24
  fromPayload<T>(payload: Payload): T;
23
25
  }
24
26
 
25
- /**
26
- * Tries to convert `value` to a {@link Payload}. Throws if conversion fails.
27
- *
28
- * @throws {@link ValueError}
29
- */
30
- export function toPayload(converter: WrappedPayloadConverter, value: unknown): Payload {
31
- return converter.toPayload(value);
32
- }
33
-
34
27
  /**
35
28
  * Implements conversion of a list of values.
36
29
  *
@@ -40,12 +33,12 @@ export function toPayload(converter: WrappedPayloadConverter, value: unknown): P
40
33
  * @throws {@link ValueError} if conversion of the value passed as parameter failed for any
41
34
  * reason.
42
35
  */
43
- export function toPayloads(converter: WrappedPayloadConverter, ...values: unknown[]): Payload[] | undefined {
36
+ export function toPayloads(converter: PayloadConverter, ...values: unknown[]): Payload[] | undefined {
44
37
  if (values.length === 0) {
45
38
  return undefined;
46
39
  }
47
40
 
48
- return values.map((value) => toPayload(converter, value));
41
+ return values.map((value) => converter.toPayload(value));
49
42
  }
50
43
 
51
44
  /**
@@ -53,12 +46,9 @@ export function toPayloads(converter: WrappedPayloadConverter, ...values: unknow
53
46
  *
54
47
  * @throws {@link ValueError} if conversion of any value in the map fails
55
48
  */
56
- export function mapToPayloads<K extends string>(
57
- converter: WrappedPayloadConverter,
58
- map: Record<K, any>
59
- ): Record<K, Payload> {
49
+ export function mapToPayloads<K extends string>(converter: PayloadConverter, map: Record<K, any>): Record<K, Payload> {
60
50
  return Object.fromEntries(
61
- Object.entries(map).map(([k, v]): [K, Payload] => [k as K, toPayload(converter, v)])
51
+ Object.entries(map).map(([k, v]): [K, Payload] => [k as K, converter.toPayload(v)])
62
52
  ) as Record<K, Payload>;
63
53
  }
64
54
 
@@ -73,11 +63,7 @@ export function mapToPayloads<K extends string>(
73
63
  * @throws {@link PayloadConverterError} if conversion of the data passed as parameter failed for any
74
64
  * reason.
75
65
  */
76
- export function fromPayloadsAtIndex<T>(
77
- converter: WrappedPayloadConverter,
78
- index: number,
79
- payloads?: Payload[] | null
80
- ): T {
66
+ export function fromPayloadsAtIndex<T>(converter: PayloadConverter, index: number, payloads?: Payload[] | null): T {
81
67
  // To make adding arguments a backwards compatible change
82
68
  if (payloads === undefined || payloads === null || index >= payloads.length) {
83
69
  return undefined as any;
@@ -88,7 +74,7 @@ export function fromPayloadsAtIndex<T>(
88
74
  /**
89
75
  * Run {@link PayloadConverter.fromPayload} on each value in the array.
90
76
  */
91
- export function arrayFromPayloads(converter: WrappedPayloadConverter, payloads?: Payload[] | null): unknown[] {
77
+ export function arrayFromPayloads(converter: PayloadConverter, payloads?: Payload[] | null): unknown[] {
92
78
  if (!payloads) {
93
79
  return [];
94
80
  }
@@ -96,10 +82,10 @@ export function arrayFromPayloads(converter: WrappedPayloadConverter, payloads?:
96
82
  }
97
83
 
98
84
  export function mapFromPayloads<K extends string>(
99
- converter: WrappedPayloadConverter,
85
+ converter: PayloadConverter,
100
86
  map?: Record<K, Payload> | null | undefined
101
- ): Record<K, unknown> | undefined {
102
- if (map === undefined || map === null) return undefined;
87
+ ): Record<K, unknown> | undefined | null {
88
+ if (map == null) return map;
103
89
  return Object.fromEntries(
104
90
  Object.entries(map).map(([k, payload]): [K, unknown] => {
105
91
  const value = converter.fromPayload(payload as Payload);
@@ -1,9 +1,23 @@
1
1
  import { PayloadConverterError, ValueError } from '@temporalio/internal-workflow-common';
2
+ import { JsonPayloadConverter } from './json-payload-converter';
2
3
  import { PayloadConverter } from './payload-converter';
3
- import { encodingKeys, encodingTypes, METADATA_ENCODING_KEY, Payload, str, u8 } from './types';
4
- import { WrappedPayloadConverter } from './wrapped-payload-converter';
4
+ import { SearchAttributePayloadConverter } from './search-attribute-payload-converter';
5
+ import { encodingKeys, encodingTypes, METADATA_ENCODING_KEY, Payload, str } from './types';
6
+
7
+ export interface PayloadConverterWithEncoding {
8
+ /**
9
+ * Converts a value to a {@link Payload}.
10
+ *
11
+ * @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.
12
+ * @returns The {@link Payload}, or `undefined` if unable to convert.
13
+ */
14
+ toPayload<T>(value: T): Payload | undefined;
15
+
16
+ /**
17
+ * Converts a {@link Payload} back to a value.
18
+ */
19
+ fromPayload<T>(payload: Payload): T;
5
20
 
6
- export interface PayloadConverterWithEncoding extends PayloadConverter {
7
21
  readonly encodingType: string;
8
22
  }
9
23
 
@@ -30,9 +44,9 @@ export class CompositePayloadConverter implements PayloadConverter {
30
44
 
31
45
  /**
32
46
  * Tries to run `.toPayload(value)` on each converter in the order provided at construction.
33
- * Returns the first successful result, or `undefined` if there is no converter that can handle the value.
47
+ * Returns the first successful result, throws {@link ValueError} if there is no converter that can handle the value.
34
48
  */
35
- public toPayload<T>(value: T): Payload | undefined {
49
+ public toPayload<T>(value: T): Payload {
36
50
  for (const converter of this.converters) {
37
51
  const result = converter.toPayload(value);
38
52
  if (result !== undefined) {
@@ -40,7 +54,7 @@ export class CompositePayloadConverter implements PayloadConverter {
40
54
  }
41
55
  }
42
56
 
43
- return undefined;
57
+ throw new ValueError(`Unable to convert ${value} to payload`);
44
58
  }
45
59
 
46
60
  /**
@@ -82,40 +96,6 @@ export class UndefinedPayloadConverter implements PayloadConverterWithEncoding {
82
96
  }
83
97
  }
84
98
 
85
- /**
86
- * Converts between non-undefined values and serialized JSON Payload
87
- */
88
- export class JsonPayloadConverter implements PayloadConverterWithEncoding {
89
- public encodingType = encodingTypes.METADATA_ENCODING_JSON;
90
-
91
- public toPayload(value: unknown): Payload | undefined {
92
- if (value === undefined) {
93
- return undefined;
94
- }
95
-
96
- let json;
97
- try {
98
- json = JSON.stringify(value);
99
- } catch (e) {
100
- return undefined;
101
- }
102
-
103
- return {
104
- metadata: {
105
- [METADATA_ENCODING_KEY]: encodingKeys.METADATA_ENCODING_JSON,
106
- },
107
- data: u8(json),
108
- };
109
- }
110
-
111
- public fromPayload<T>(content: Payload): T {
112
- if (content.data === undefined || content.data === null) {
113
- throw new ValueError('Got payload with no data');
114
- }
115
- return JSON.parse(str(content.data));
116
- }
117
- }
118
-
119
99
  /**
120
100
  * Converts between binary data types and RAW Payload
121
101
  */
@@ -142,7 +122,7 @@ export class BinaryPayloadConverter implements PayloadConverterWithEncoding {
142
122
  }
143
123
  }
144
124
 
145
- export const searchAttributePayloadConverter = new WrappedPayloadConverter(new JsonPayloadConverter());
125
+ export const searchAttributePayloadConverter = new SearchAttributePayloadConverter();
146
126
 
147
127
  export class DefaultPayloadConverter extends CompositePayloadConverter {
148
128
  // Match the order used in other SDKs, but exclude Protobuf converters so that the code, including
@@ -157,8 +137,9 @@ export class DefaultPayloadConverter extends CompositePayloadConverter {
157
137
  }
158
138
 
159
139
  /**
160
- * The default {@link PayloadConverter} used by the SDK.
161
- * Supports `Uint8Array` and JSON serializables (so if [`JSON.stringify(yourArgOrRetval)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description) works, the default payload converter will work).
140
+ * The default {@link PayloadConverter} used by the SDK. Supports `Uint8Array` and JSON serializables (so if
141
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description | `JSON.stringify(yourArgOrRetval)`}
142
+ * works, the default payload converter will work).
162
143
  *
163
144
  * To also support Protobufs, create a custom payload converter with {@link DefaultPayloadConverter}:
164
145
  *
@@ -8,10 +8,10 @@ import {
8
8
  } from '@temporalio/internal-workflow-common';
9
9
  import * as protoJsonSerializer from 'proto3-json-serializer';
10
10
  import type { Message, Namespace, Root, Type } from 'protobufjs';
11
+ import { JsonPayloadConverter } from './json-payload-converter';
11
12
  import {
12
13
  BinaryPayloadConverter,
13
14
  CompositePayloadConverter,
14
- JsonPayloadConverter,
15
15
  PayloadConverterWithEncoding,
16
16
  UndefinedPayloadConverter,
17
17
  } from './payload-converters';