@stigmer/temporal-codecs 3.12.9

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 (42) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +48 -0
  3. package/claimcheck/compressor.d.ts +12 -0
  4. package/claimcheck/compressor.d.ts.map +1 -0
  5. package/claimcheck/compressor.js +17 -0
  6. package/claimcheck/compressor.js.map +1 -0
  7. package/claimcheck/config.d.ts +17 -0
  8. package/claimcheck/config.d.ts.map +1 -0
  9. package/claimcheck/config.js +19 -0
  10. package/claimcheck/config.js.map +1 -0
  11. package/claimcheck/payload-codec.d.ts +29 -0
  12. package/claimcheck/payload-codec.d.ts.map +1 -0
  13. package/claimcheck/payload-codec.js +110 -0
  14. package/claimcheck/payload-codec.js.map +1 -0
  15. package/claimcheck/storage.d.ts +20 -0
  16. package/claimcheck/storage.d.ts.map +1 -0
  17. package/claimcheck/storage.js +2 -0
  18. package/claimcheck/storage.js.map +1 -0
  19. package/encryption/config.d.ts +82 -0
  20. package/encryption/config.d.ts.map +1 -0
  21. package/encryption/config.js +119 -0
  22. package/encryption/config.js.map +1 -0
  23. package/encryption/payload-codec.d.ts +46 -0
  24. package/encryption/payload-codec.d.ts.map +1 -0
  25. package/encryption/payload-codec.js +134 -0
  26. package/encryption/payload-codec.js.map +1 -0
  27. package/index.d.ts +28 -0
  28. package/index.d.ts.map +1 -0
  29. package/index.js +25 -0
  30. package/index.js.map +1 -0
  31. package/package.json +36 -0
  32. package/src/__test-utils__/fake-claimcheck-storage.ts +54 -0
  33. package/src/__tests__/claimcheck-codec.test.ts +256 -0
  34. package/src/__tests__/encryption-codec.test.ts +292 -0
  35. package/src/__tests__/fixtures/encrypted-payload-fixture.json +15 -0
  36. package/src/claimcheck/compressor.ts +19 -0
  37. package/src/claimcheck/config.ts +30 -0
  38. package/src/claimcheck/payload-codec.ts +144 -0
  39. package/src/claimcheck/storage.ts +19 -0
  40. package/src/encryption/config.ts +174 -0
  41. package/src/encryption/payload-codec.ts +156 -0
  42. package/src/index.ts +34 -0
package/LICENSE ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2026 Stigmer Contributors
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @stigmer/temporal-codecs
2
+
3
+ Temporal payload codecs shared by Stigmer's TypeScript Temporal processes:
4
+
5
+ - **`EncryptionPayloadCodec`** — AES-256-GCM encryption of payloads at rest
6
+ in workflow history, with key-id-addressed rotation.
7
+ - **`ClaimcheckPayloadCodec`** — transparent offload of large payloads to
8
+ blob storage (with optional gzip), replaced in history by a small marker.
9
+
10
+ ## Why this is a library
11
+
12
+ The encryption envelope is a **cross-language wire contract**: the Java
13
+ decode-only codec in stigmer-cloud's `temporal-starter` must decrypt these
14
+ payloads byte-for-byte, and the Stigmer TypeScript server installs the same
15
+ codecs on its Temporal clients. One implementation, many consumers — the
16
+ contract must never fork. The committed fixture in
17
+ `src/__tests__/fixtures/encrypted-payload-fixture.json` pins the envelope;
18
+ never regenerate it casually (changing it means changing the cross-SDK
19
+ contract, which requires every implementation to move in lockstep).
20
+
21
+ Extracted from `backend/services/runner` (its `src/encryption/` and
22
+ `src/claimcheck/` modules) when the TypeScript server became the second
23
+ consumer.
24
+
25
+ ## Consumers
26
+
27
+ - `@stigmer/runner` (`backend/services/runner`) — encode + decode on its
28
+ Temporal workers.
29
+ - The Stigmer TypeScript server — decode for history reads, encode for
30
+ payloads it writes.
31
+
32
+ ## Dependency policy
33
+
34
+ `@temporalio/common` and `@temporalio/proto` are pinned **exact and
35
+ identical to the runner's pins** and must be bumped in lockstep with it:
36
+ the runner's consumer-install gate (stigmer/stigmer#786) fails on any
37
+ `@temporalio/*` version mix, because a split dependency tree registers the
38
+ Temporal core-sdk protobuf namespace twice and crashes worker init.
39
+
40
+ ## Design notes
41
+
42
+ - The codecs perform no environment or secret reads of their own.
43
+ `loadPayloadEncryptionConfig` takes an injected secret reader, so each
44
+ consumer applies its own secret-custody policy (the runner routes reads
45
+ through its boot-capture credential store; see stigmer/stigmer#508).
46
+ - The claim-check codec depends on the minimal `ClaimcheckStorage` port
47
+ declared here — two methods, `upload` and `download` — which consumers
48
+ satisfy structurally with their own storage clients.
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Gzip helpers for claim-checked blobs. Synchronous by design: the codec
3
+ * runs on the Temporal worker's payload path where the blobs are bounded
4
+ * by the claim-check threshold, and gzip's format is part of the marker
5
+ * contract (`compressed: true` blobs must gunzip on any consumer).
6
+ *
7
+ * Moved from backend/services/runner/src/claimcheck/compressor.ts when the
8
+ * codecs became @stigmer/temporal-codecs.
9
+ */
10
+ export declare function compress(data: Buffer): Buffer<ArrayBuffer>;
11
+ export declare function decompress(data: Buffer): Buffer<ArrayBuffer>;
12
+ //# sourceMappingURL=compressor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compressor.d.ts","sourceRoot":"","sources":["../../src/claimcheck/compressor.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAE1D;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAE5D"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Gzip helpers for claim-checked blobs. Synchronous by design: the codec
3
+ * runs on the Temporal worker's payload path where the blobs are bounded
4
+ * by the claim-check threshold, and gzip's format is part of the marker
5
+ * contract (`compressed: true` blobs must gunzip on any consumer).
6
+ *
7
+ * Moved from backend/services/runner/src/claimcheck/compressor.ts when the
8
+ * codecs became @stigmer/temporal-codecs.
9
+ */
10
+ import { gzipSync, gunzipSync } from "node:zlib";
11
+ export function compress(data) {
12
+ return gzipSync(data);
13
+ }
14
+ export function decompress(data) {
15
+ return gunzipSync(data);
16
+ }
17
+ //# sourceMappingURL=compressor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compressor.js","sourceRoot":"","sources":["../../src/claimcheck/compressor.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,OAAO,QAAQ,CAAC,IAAI,CAAwB,CAAC;AAC/C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,UAAU,CAAC,IAAI,CAAwB,CAAC;AACjD,CAAC"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Claim-check configuration, env-driven with an enabled-iff-configured
3
+ * gate (CLAIMCHECK_ENABLED) — unlike encryption, the offload threshold and
4
+ * key prefix are operational tuning, not secrets, so plain env reads are
5
+ * the whole policy.
6
+ *
7
+ * Moved from backend/services/runner/src/claimcheck/config.ts when the
8
+ * codecs became @stigmer/temporal-codecs.
9
+ */
10
+ export interface ClaimcheckConfig {
11
+ readonly enabled: boolean;
12
+ readonly thresholdBytes: number;
13
+ readonly compressionEnabled: boolean;
14
+ readonly keyPrefix: string;
15
+ }
16
+ export declare function loadClaimcheckConfig(): ClaimcheckConfig;
17
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/claimcheck/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,kBAAkB,EAAE,OAAO,CAAC;IACrC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAID,wBAAgB,oBAAoB,IAAI,gBAAgB,CAUvD"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Claim-check configuration, env-driven with an enabled-iff-configured
3
+ * gate (CLAIMCHECK_ENABLED) — unlike encryption, the offload threshold and
4
+ * key prefix are operational tuning, not secrets, so plain env reads are
5
+ * the whole policy.
6
+ *
7
+ * Moved from backend/services/runner/src/claimcheck/config.ts when the
8
+ * codecs became @stigmer/temporal-codecs.
9
+ */
10
+ const DEFAULT_THRESHOLD_BYTES = 128 * 1024; // 128KB
11
+ export function loadClaimcheckConfig() {
12
+ return {
13
+ enabled: process.env.CLAIMCHECK_ENABLED === "true",
14
+ thresholdBytes: parseInt(process.env.CLAIMCHECK_THRESHOLD_BYTES ?? String(DEFAULT_THRESHOLD_BYTES), 10),
15
+ compressionEnabled: process.env.CLAIMCHECK_COMPRESSION_ENABLED !== "false",
16
+ keyPrefix: process.env.CLAIMCHECK_KEY_PREFIX ?? "claimcheck/",
17
+ };
18
+ }
19
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/claimcheck/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AASH,MAAM,uBAAuB,GAAG,GAAG,GAAG,IAAI,CAAC,CAAC,QAAQ;AAEpD,MAAM,UAAU,oBAAoB;IAClC,OAAO;QACL,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,kBAAkB,KAAK,MAAM;QAClD,cAAc,EAAE,QAAQ,CACtB,OAAO,CAAC,GAAG,CAAC,0BAA0B,IAAI,MAAM,CAAC,uBAAuB,CAAC,EACzE,EAAE,CACH;QACD,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,8BAA8B,KAAK,OAAO;QAC1E,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB,IAAI,aAAa;KAC9D,CAAC;AACJ,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Temporal PayloadCodec that transparently offloads large payloads to
3
+ * external storage ({@link ClaimcheckStorage}). Payloads below the
4
+ * threshold pass through unchanged. Payloads at or above the threshold
5
+ * are compressed (optional), uploaded, and replaced with a small
6
+ * reference marker.
7
+ *
8
+ * On decode, markers are detected, the original payload is downloaded
9
+ * and decompressed, and the original bytes are restored — transparent
10
+ * to workflow/activity code.
11
+ *
12
+ * Moved from backend/services/runner/src/claimcheck/payload-codec.ts when
13
+ * the codecs became @stigmer/temporal-codecs (one home for the
14
+ * cross-language envelope contract; the TS server is the second consumer).
15
+ */
16
+ import type { Payload, PayloadCodec } from "@temporalio/common";
17
+ import type { ClaimcheckStorage } from "./storage.js";
18
+ import type { ClaimcheckConfig } from "./config.js";
19
+ export declare class ClaimcheckPayloadCodec implements PayloadCodec {
20
+ private readonly storage;
21
+ private readonly config;
22
+ constructor(storage: ClaimcheckStorage, config: ClaimcheckConfig);
23
+ encode(payloads: Payload[]): Promise<Payload[]>;
24
+ decode(payloads: Payload[]): Promise<Payload[]>;
25
+ private encodePayload;
26
+ private decodePayload;
27
+ private isClaimcheckPayload;
28
+ }
29
+ //# sourceMappingURL=payload-codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payload-codec.d.ts","sourceRoot":"","sources":["../../src/claimcheck/payload-codec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAoBpD,qBAAa,sBAAuB,YAAW,YAAY;IAEvD,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,MAAM;gBADN,OAAO,EAAE,iBAAiB,EAC1B,MAAM,EAAE,gBAAgB;IAGrC,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;IAI/C,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;YAIvC,aAAa;YAoCb,aAAa;IA4B3B,OAAO,CAAC,mBAAmB;CAK5B"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Temporal PayloadCodec that transparently offloads large payloads to
3
+ * external storage ({@link ClaimcheckStorage}). Payloads below the
4
+ * threshold pass through unchanged. Payloads at or above the threshold
5
+ * are compressed (optional), uploaded, and replaced with a small
6
+ * reference marker.
7
+ *
8
+ * On decode, markers are detected, the original payload is downloaded
9
+ * and decompressed, and the original bytes are restored — transparent
10
+ * to workflow/activity code.
11
+ *
12
+ * Moved from backend/services/runner/src/claimcheck/payload-codec.ts when
13
+ * the codecs became @stigmer/temporal-codecs (one home for the
14
+ * cross-language envelope contract; the TS server is the second consumer).
15
+ */
16
+ import { randomUUID } from "node:crypto";
17
+ import { compress, decompress } from "./compressor.js";
18
+ const MARKER_METADATA_KEY = "encoding";
19
+ const MARKER_ENCODING_VALUE = "binary/claimcheck";
20
+ export class ClaimcheckPayloadCodec {
21
+ storage;
22
+ config;
23
+ constructor(storage, config) {
24
+ this.storage = storage;
25
+ this.config = config;
26
+ }
27
+ async encode(payloads) {
28
+ return Promise.all(payloads.map((p) => this.encodePayload(p)));
29
+ }
30
+ async decode(payloads) {
31
+ return Promise.all(payloads.map((p) => this.decodePayload(p)));
32
+ }
33
+ async encodePayload(payload) {
34
+ const data = payload.data;
35
+ if (!data || data.length < this.config.thresholdBytes) {
36
+ return payload;
37
+ }
38
+ const originalBuf = Buffer.from(data);
39
+ let uploadBuf = originalBuf;
40
+ let compressed = false;
41
+ if (this.config.compressionEnabled) {
42
+ const compressedBuf = compress(originalBuf);
43
+ if (compressedBuf.length < originalBuf.length) {
44
+ uploadBuf = compressedBuf;
45
+ compressed = true;
46
+ }
47
+ }
48
+ const key = `${this.config.keyPrefix}${randomUUID()}`;
49
+ await this.storage.upload(key, uploadBuf, "application/octet-stream");
50
+ const marker = {
51
+ key,
52
+ size: data.length,
53
+ compressed,
54
+ metadata: serializeMetadata(payload.metadata),
55
+ };
56
+ return {
57
+ metadata: {
58
+ [MARKER_METADATA_KEY]: Buffer.from(MARKER_ENCODING_VALUE),
59
+ },
60
+ data: Buffer.from(JSON.stringify(marker)),
61
+ };
62
+ }
63
+ async decodePayload(payload) {
64
+ if (!this.isClaimcheckPayload(payload)) {
65
+ return payload;
66
+ }
67
+ const marker = JSON.parse(Buffer.from(payload.data).toString("utf-8"));
68
+ let rawBuf;
69
+ try {
70
+ rawBuf = await this.storage.download(marker.key);
71
+ }
72
+ catch (err) {
73
+ // Preserve the claimcheck-scoped error contract; the shared download error
74
+ // already carries the HTTP status (proxy) or the miss (local) as the cause.
75
+ const cause = err instanceof Error ? err.message : String(err);
76
+ throw new Error(`Claimcheck retrieve failed for key ${marker.key}: ${cause}`);
77
+ }
78
+ const dataBuf = marker.compressed ? decompress(rawBuf) : rawBuf;
79
+ return {
80
+ metadata: marker.metadata
81
+ ? deserializeMetadata(marker.metadata)
82
+ : payload.metadata,
83
+ data: dataBuf,
84
+ };
85
+ }
86
+ isClaimcheckPayload(payload) {
87
+ const encoding = payload.metadata?.[MARKER_METADATA_KEY];
88
+ if (!encoding)
89
+ return false;
90
+ return Buffer.from(encoding).toString("utf-8") === MARKER_ENCODING_VALUE;
91
+ }
92
+ }
93
+ function serializeMetadata(metadata) {
94
+ if (!metadata)
95
+ return undefined;
96
+ const out = {};
97
+ for (const [key, value] of Object.entries(metadata)) {
98
+ if (value)
99
+ out[key] = Buffer.from(value).toString("base64");
100
+ }
101
+ return out;
102
+ }
103
+ function deserializeMetadata(metadata) {
104
+ const out = {};
105
+ for (const [key, value] of Object.entries(metadata)) {
106
+ out[key] = Buffer.from(value, "base64");
107
+ }
108
+ return out;
109
+ }
110
+ //# sourceMappingURL=payload-codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payload-codec.js","sourceRoot":"","sources":["../../src/claimcheck/payload-codec.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAIzC,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEvD,MAAM,mBAAmB,GAAG,UAAU,CAAC;AACvC,MAAM,qBAAqB,GAAG,mBAAmB,CAAC;AAgBlD,MAAM,OAAO,sBAAsB;IAEd;IACA;IAFnB,YACmB,OAA0B,EAC1B,MAAwB;QADxB,YAAO,GAAP,OAAO,CAAmB;QA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+ *
4
+ * Deliberately minimal (upload + download by key) so any consumer's
5
+ * storage client satisfies it structurally: the runner's ArtifactStorage
6
+ * (a richer interface with presigned-URL minting) matches as-is, and the
7
+ * TS server brings its own. Widening this port widens the wire contract's
8
+ * dependency surface for every consumer — do not add methods the codec
9
+ * itself does not call.
10
+ */
11
+ export interface ClaimcheckStorage {
12
+ /** Store `content` under `key`; resolves once the blob is durable. */
13
+ upload(key: string, content: Buffer, contentType?: string): Promise<string>;
14
+ /**
15
+ * Return the exact bytes stored under `key`, or throw a descriptive,
16
+ * key-scoped `Error` when the object is missing or the transport fails.
17
+ */
18
+ download(key: string): Promise<Buffer>;
19
+ }
20
+ //# sourceMappingURL=storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/claimcheck/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,WAAW,iBAAiB;IAChC,sEAAsE;IACtE,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5E;;;OAGG;IACH,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACxC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/claimcheck/storage.ts"],"names":[],"mappings":""}
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Payload-encryption configuration (stigmer-cloud#227, stigmer#398).
3
+ *
4
+ * Two key sources, strict precedence:
5
+ *
6
+ * 1. Environment (STIGMER_PAYLOAD_ENCRYPTION_KEY(_ID)) — the operator's
7
+ * explicit choice: self-hosted deployments sharing one key with their
8
+ * server, and cloud sandboxes injected with the platform key. When the
9
+ * env key is set, bootstrap-delivered material is ignored entirely.
10
+ * 2. Bootstrap-delivered — server-managed per-identity keys handed to
11
+ * desktop-class runners by getRunnerBootstrapConfig. Held in memory
12
+ * only; persistence lives server-side, which is what makes Temporal
13
+ * replay work across runner restarts (every boot re-fetches the SAME
14
+ * key).
15
+ *
16
+ * Encryption is enabled iff a key is present from either source — the same
17
+ * enabled-iff-configured pattern as the claim-check codec. A malformed key
18
+ * fails the boot rather than silently running plaintext: an operator who set
19
+ * the key (or a server that minted one) intended history to be encrypted.
20
+ *
21
+ * Key rotation: payloads carry the id of the key that encrypted them.
22
+ * During a rotation window the previous key stays readable via the
23
+ * secondary pair while new payloads are written under the primary key.
24
+ * Workers capture keys at construction, so a rotated bootstrap key lands
25
+ * on the next runner boot — there is no live re-key.
26
+ *
27
+ * Moved from backend/services/runner/src/encryption/config.ts when the
28
+ * codecs became @stigmer/temporal-codecs (one home for the cross-language
29
+ * envelope contract; the TS server is the second consumer). Key VALUES are
30
+ * read through an injected {@link SecretReader} because secret custody is
31
+ * consumer policy, not codec policy: the runner routes reads through its
32
+ * boot-capture credential store (stigmer#508 — secrets must not live in
33
+ * process.env where agent shells could read them), while other consumers
34
+ * read their own stores. No process.env default is provided for the
35
+ * VALUES: a forgotten injection must fail to compile, not silently bypass
36
+ * a consumer's secret custody.
37
+ */
38
+ export interface EncryptionKey {
39
+ readonly keyId: string;
40
+ /** 32-byte AES-256 key. */
41
+ readonly key: Buffer;
42
+ }
43
+ export interface PayloadEncryptionConfig {
44
+ /** Key used to encrypt outgoing payloads (and decrypt its own). */
45
+ readonly primary: EncryptionKey;
46
+ /** Decrypt-only key accepted during rotation windows. */
47
+ readonly secondary?: EncryptionKey;
48
+ }
49
+ /**
50
+ * Reads one secret VALUE by its env-var name. Consumers inject their own
51
+ * custody policy (see the module doc); the *_KEY_ID companions are
52
+ * rotation bookkeeping, not secrets, and stay plain `process.env` reads.
53
+ */
54
+ export type SecretReader = (name: string) => string | undefined;
55
+ /**
56
+ * Server-managed key material delivered by the runner's
57
+ * getRunnerBootstrapConfig. Structurally mirrors
58
+ * {@link BootstrapPayloadEncryptionKeys} in the runner's stigmer-client.ts
59
+ * — declared here so the library stays free of client imports.
60
+ */
61
+ export interface BootstrapKeyMaterial {
62
+ readonly key: string;
63
+ readonly keyId?: string;
64
+ readonly secondaryKey?: string;
65
+ readonly secondaryKeyId?: string;
66
+ }
67
+ /**
68
+ * Returns the encryption config, or undefined when encryption is not
69
+ * configured (the codec is then simply not installed).
70
+ *
71
+ * Source precedence: the env key wins outright; bootstrap-delivered
72
+ * material applies only when no env key is set (see the module doc).
73
+ *
74
+ * @throws when a key is present but malformed, or a key id is missing —
75
+ * key misconfiguration must stop the boot, not degrade to plaintext.
76
+ * This applies equally to bootstrap material: a server that hands out
77
+ * a bad key or omits its id has broken the protocol contract, and
78
+ * running plaintext against a server that manages keys would silently
79
+ * defeat the feature.
80
+ */
81
+ export declare function loadPayloadEncryptionConfig(readSecret: SecretReader, bootstrap?: BootstrapKeyMaterial): PayloadEncryptionConfig | undefined;
82
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/encryption/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AAEH,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,2BAA2B;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,uBAAuB;IACtC,mEAAmE;IACnE,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;IAChC,yDAAyD;IACzD,QAAQ,CAAC,SAAS,CAAC,EAAE,aAAa,CAAC;CACpC;AAED;;;;GAIG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;CAClC;AASD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2BAA2B,CACzC,UAAU,EAAE,YAAY,EACxB,SAAS,CAAC,EAAE,oBAAoB,GAC/B,uBAAuB,GAAG,SAAS,CAuCrC"}