commitrail 0.1.0-alpha.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 (41) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +12 -0
  3. package/dist/cjs/envelope.d.ts +91 -0
  4. package/dist/cjs/envelope.js +54 -0
  5. package/dist/cjs/envelope.js.map +1 -0
  6. package/dist/cjs/index.d.ts +20 -0
  7. package/dist/cjs/index.js +29 -0
  8. package/dist/cjs/index.js.map +1 -0
  9. package/dist/cjs/package.json +3 -0
  10. package/dist/cjs/postgres.d.ts +152 -0
  11. package/dist/cjs/postgres.js +373 -0
  12. package/dist/cjs/postgres.js.map +1 -0
  13. package/dist/cjs/signing.d.ts +66 -0
  14. package/dist/cjs/signing.js +108 -0
  15. package/dist/cjs/signing.js.map +1 -0
  16. package/dist/cjs/subjects.d.ts +53 -0
  17. package/dist/cjs/subjects.js +88 -0
  18. package/dist/cjs/subjects.js.map +1 -0
  19. package/dist/cjs/webhooks.d.ts +100 -0
  20. package/dist/cjs/webhooks.js +214 -0
  21. package/dist/cjs/webhooks.js.map +1 -0
  22. package/dist/esm/envelope.d.ts +91 -0
  23. package/dist/esm/envelope.js +50 -0
  24. package/dist/esm/envelope.js.map +1 -0
  25. package/dist/esm/index.d.ts +20 -0
  26. package/dist/esm/index.js +21 -0
  27. package/dist/esm/index.js.map +1 -0
  28. package/dist/esm/package.json +3 -0
  29. package/dist/esm/postgres.d.ts +152 -0
  30. package/dist/esm/postgres.js +366 -0
  31. package/dist/esm/postgres.js.map +1 -0
  32. package/dist/esm/signing.d.ts +66 -0
  33. package/dist/esm/signing.js +101 -0
  34. package/dist/esm/signing.js.map +1 -0
  35. package/dist/esm/subjects.d.ts +53 -0
  36. package/dist/esm/subjects.js +83 -0
  37. package/dist/esm/subjects.js.map +1 -0
  38. package/dist/esm/webhooks.d.ts +100 -0
  39. package/dist/esm/webhooks.js +209 -0
  40. package/dist/esm/webhooks.js.map +1 -0
  41. package/package.json +108 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
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
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Syrenox Systems Ltd
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # commitrail
2
+
3
+ **CommitRail is not publicly available.**
4
+
5
+ This package is published as a prerelease. It is undocumented, it is not on the `latest` tag,
6
+ and its API may change without notice.
7
+
8
+ **Please do not use it in production.**
9
+
10
+ ---
11
+
12
+ Apache-2.0 — © Syrenox Systems Ltd
@@ -0,0 +1,91 @@
1
+ /**
2
+ * The wire contract.
3
+ *
4
+ * This package is the single definition of it. CommitRail signs and sends using exactly
5
+ * what customers import to receive and verify, so the two cannot drift — the same reason
6
+ * the control plane serialises through its route schemas rather than describing them
7
+ * separately.
8
+ */
9
+ import type { EventSubject } from './subjects.js';
10
+ export declare const SPEC_VERSION = "1";
11
+ export interface CommitRailEvent<TData = unknown> {
12
+ specVersion: string;
13
+ /**
14
+ * The producer's own event identity, carried through untouched.
15
+ *
16
+ * Unique within its source, and not beyond it: a destination fed by two sources can see
17
+ * the same id from unrelated events. Deduplicate on `delivery.id`.
18
+ */
19
+ id: string;
20
+ type: string;
21
+ version: number;
22
+ /** When the event happened, as the producer recorded it. */
23
+ occurredAt: string;
24
+ /**
25
+ * The logical operation this event belongs to, and the event that caused it.
26
+ *
27
+ * Absent when the producer named neither — an absent member says "this event names no
28
+ * operation" more clearly than a null does. Put the same `correlationId` on whatever you
29
+ * emit in response and CommitRail joins the chain without being told anything else.
30
+ */
31
+ correlationId?: string;
32
+ causationId?: string;
33
+ /**
34
+ * The business identities the producer declared this event concerns.
35
+ *
36
+ * Absent when none were declared, for the same reason as the members above — and never
37
+ * inferred: these are exactly what the producer wrote, so a consumer can route or index
38
+ * on them with the same trust the producer's own code would get.
39
+ */
40
+ subjects?: EventSubject[];
41
+ delivery: {
42
+ /**
43
+ * The obligation this request is fulfilling, stable across every retry.
44
+ *
45
+ * **This is the idempotency key.** One event legitimately becomes two obligations when
46
+ * two routes point at the same destination, so deduplicating on `id` would silently
47
+ * drop the second.
48
+ */
49
+ id: string;
50
+ attempt: number;
51
+ };
52
+ data: TData;
53
+ }
54
+ /** Header names CommitRail sends. Lowercase, as Node normalises them. */
55
+ export declare const HEADERS: {
56
+ readonly specVersion: "commitrail-spec-version";
57
+ readonly deliveryId: "commitrail-delivery-id";
58
+ readonly idempotencyKey: "commitrail-idempotency-key";
59
+ readonly attemptId: "commitrail-attempt-id";
60
+ readonly attemptNumber: "commitrail-attempt-number";
61
+ readonly eventId: "commitrail-event-id";
62
+ readonly eventType: "commitrail-event-type";
63
+ readonly timestamp: "commitrail-timestamp";
64
+ readonly signature: "commitrail-signature";
65
+ };
66
+ /**
67
+ * Serialise the wire envelope.
68
+ *
69
+ * Built by hand rather than with `JSON.stringify` on an object, for one reason: `data` is
70
+ * the customer's payload as PostgreSQL rendered it, and it must be spliced in as text.
71
+ * Parsing it to put it in an object would run it through `JSON.parse`, which is float64 and
72
+ * silently rewrites any integer past 2^53 — an order id of 12345678901234567890 came back
73
+ * as 12345678901234567000. See docs/defects.md.
74
+ *
75
+ * Every other member still goes through `JSON.stringify`, which is what escapes them. The
76
+ * field order is fixed and visible on purpose: these bytes are what gets signed, so the
77
+ * order is part of the contract rather than an implementation detail of an object literal.
78
+ */
79
+ export declare function serialiseEnvelope(input: {
80
+ id: string;
81
+ type: string;
82
+ version: number;
83
+ occurredAt: string;
84
+ correlationId?: string | null;
85
+ causationId?: string | null;
86
+ subjects?: EventSubject[] | null;
87
+ deliveryId: string;
88
+ attempt: number;
89
+ /** JSON text. Never a parsed value — see the note above. */
90
+ data: string;
91
+ }): string;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HEADERS = exports.SPEC_VERSION = void 0;
4
+ exports.serialiseEnvelope = serialiseEnvelope;
5
+ exports.SPEC_VERSION = '1';
6
+ /** Header names CommitRail sends. Lowercase, as Node normalises them. */
7
+ exports.HEADERS = {
8
+ specVersion: 'commitrail-spec-version',
9
+ deliveryId: 'commitrail-delivery-id',
10
+ idempotencyKey: 'commitrail-idempotency-key',
11
+ attemptId: 'commitrail-attempt-id',
12
+ attemptNumber: 'commitrail-attempt-number',
13
+ eventId: 'commitrail-event-id',
14
+ eventType: 'commitrail-event-type',
15
+ timestamp: 'commitrail-timestamp',
16
+ signature: 'commitrail-signature',
17
+ };
18
+ /**
19
+ * Serialise the wire envelope.
20
+ *
21
+ * Built by hand rather than with `JSON.stringify` on an object, for one reason: `data` is
22
+ * the customer's payload as PostgreSQL rendered it, and it must be spliced in as text.
23
+ * Parsing it to put it in an object would run it through `JSON.parse`, which is float64 and
24
+ * silently rewrites any integer past 2^53 — an order id of 12345678901234567890 came back
25
+ * as 12345678901234567000. See docs/defects.md.
26
+ *
27
+ * Every other member still goes through `JSON.stringify`, which is what escapes them. The
28
+ * field order is fixed and visible on purpose: these bytes are what gets signed, so the
29
+ * order is part of the contract rather than an implementation detail of an object literal.
30
+ */
31
+ function serialiseEnvelope(input) {
32
+ // Omitted rather than sent as null, so the envelope keeps the shape it has always had for
33
+ // events that name no operation.
34
+ const optional = (name, value) => value === null || value === undefined ? '' : `"${name}":${JSON.stringify(value)},`;
35
+ return ('{' +
36
+ `"specVersion":${JSON.stringify(exports.SPEC_VERSION)},` +
37
+ `"id":${JSON.stringify(input.id)},` +
38
+ `"type":${JSON.stringify(input.type)},` +
39
+ `"version":${JSON.stringify(input.version)},` +
40
+ `"occurredAt":${JSON.stringify(input.occurredAt)},` +
41
+ optional('correlationId', input.correlationId) +
42
+ optional('causationId', input.causationId) +
43
+ // A new member is added to the fixed order, never inserted into it: envelopes without
44
+ // subjects keep exactly the bytes they have always had, which is what lets deployed
45
+ // verifiers stay deployed. `JSON.stringify` of the array is safe — subjects are strings
46
+ // by contract, so there is no float64 hazard here, unlike `data`.
47
+ (input.subjects === null || input.subjects === undefined || input.subjects.length === 0
48
+ ? ''
49
+ : `"subjects":${JSON.stringify(input.subjects)},`) +
50
+ `"delivery":{"id":${JSON.stringify(input.deliveryId)},"attempt":${JSON.stringify(input.attempt)}},` +
51
+ `"data":${input.data}` +
52
+ '}');
53
+ }
54
+ //# sourceMappingURL=envelope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"envelope.js","sourceRoot":"","sources":["../../src/envelope.ts"],"names":[],"mappings":";;;AAyFA,8CAsCC;AArHY,QAAA,YAAY,GAAG,GAAG,CAAC;AAqDhC,yEAAyE;AAC5D,QAAA,OAAO,GAAG;IACrB,WAAW,EAAE,yBAAyB;IACtC,UAAU,EAAE,wBAAwB;IACpC,cAAc,EAAE,4BAA4B;IAC5C,SAAS,EAAE,uBAAuB;IAClC,aAAa,EAAE,2BAA2B;IAC1C,OAAO,EAAE,qBAAqB;IAC9B,SAAS,EAAE,uBAAuB;IAClC,SAAS,EAAE,sBAAsB;IACjC,SAAS,EAAE,sBAAsB;CACzB,CAAC;AAEX;;;;;;;;;;;;GAYG;AACH,SAAgB,iBAAiB,CAAC,KAYjC;IACC,0FAA0F;IAC1F,iCAAiC;IACjC,MAAM,QAAQ,GAAG,CAAC,IAAY,EAAE,KAAgC,EAAE,EAAE,CAClE,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,KAAK,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;IAErF,OAAO,CACL,GAAG;QACH,iBAAiB,IAAI,CAAC,SAAS,CAAC,oBAAY,CAAC,GAAG;QAChD,QAAQ,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG;QACnC,UAAU,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG;QACvC,aAAa,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG;QAC7C,gBAAgB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG;QACnD,QAAQ,CAAC,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC;QAC9C,QAAQ,CAAC,aAAa,EAAE,KAAK,CAAC,WAAW,CAAC;QAC1C,sFAAsF;QACtF,oFAAoF;QACpF,wFAAwF;QACxF,kEAAkE;QAClE,CAAC,KAAK,CAAC,QAAQ,KAAK,IAAI,IAAI,KAAK,CAAC,QAAQ,KAAK,SAAS,IAAI,KAAK,CAAC,QAAQ,CAAC,MAAM,KAAK,CAAC;YACrF,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,cAAc,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;QACpD,oBAAoB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,cAAc,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI;QACnG,UAAU,KAAK,CAAC,IAAI,EAAE;QACtB,GAAG,CACJ,CAAC;AACJ,CAAC"}
@@ -0,0 +1,20 @@
1
+ /**
2
+ * The protocol, as a customer needs it.
3
+ *
4
+ * Everything here is something you use to integrate with CommitRail. Nothing is here because
5
+ * CommitRail's own server happens to need it: the server implements this same protocol from its
6
+ * own private code and imports nothing from this package, so the public surface is a decision
7
+ * about your integration rather than a projection of our internals.
8
+ *
9
+ * What that costs is the ability to say "we sign with exactly the function you verify with".
10
+ * What it buys is that every export here is one we intend to support, and the guarantee is kept
11
+ * where it can actually be kept for every language — by conformance vectors both implementations
12
+ * are tested against, and by end-to-end tests that verify what CommitRail sends using this
13
+ * verifier. A Go verifier could never have shared our functions either.
14
+ *
15
+ * Each side has a subpath of its own: `commitrail/postgres` produces, `commitrail/webhooks`
16
+ * receives, and neither is re-exported here.
17
+ */
18
+ export { SPEC_VERSION, HEADERS, type CommitRailEvent } from './envelope.js';
19
+ export { InvalidSubjectsError, SUBJECT_LIMITS, type EventSubject } from './subjects.js';
20
+ export { verifySignatureHeader } from './signing.js';
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.verifySignatureHeader = exports.SUBJECT_LIMITS = exports.InvalidSubjectsError = exports.HEADERS = exports.SPEC_VERSION = void 0;
4
+ /**
5
+ * The protocol, as a customer needs it.
6
+ *
7
+ * Everything here is something you use to integrate with CommitRail. Nothing is here because
8
+ * CommitRail's own server happens to need it: the server implements this same protocol from its
9
+ * own private code and imports nothing from this package, so the public surface is a decision
10
+ * about your integration rather than a projection of our internals.
11
+ *
12
+ * What that costs is the ability to say "we sign with exactly the function you verify with".
13
+ * What it buys is that every export here is one we intend to support, and the guarantee is kept
14
+ * where it can actually be kept for every language — by conformance vectors both implementations
15
+ * are tested against, and by end-to-end tests that verify what CommitRail sends using this
16
+ * verifier. A Go verifier could never have shared our functions either.
17
+ *
18
+ * Each side has a subpath of its own: `commitrail/postgres` produces, `commitrail/webhooks`
19
+ * receives, and neither is re-exported here.
20
+ */
21
+ var envelope_js_1 = require("./envelope.js");
22
+ Object.defineProperty(exports, "SPEC_VERSION", { enumerable: true, get: function () { return envelope_js_1.SPEC_VERSION; } });
23
+ Object.defineProperty(exports, "HEADERS", { enumerable: true, get: function () { return envelope_js_1.HEADERS; } });
24
+ var subjects_js_1 = require("./subjects.js");
25
+ Object.defineProperty(exports, "InvalidSubjectsError", { enumerable: true, get: function () { return subjects_js_1.InvalidSubjectsError; } });
26
+ Object.defineProperty(exports, "SUBJECT_LIMITS", { enumerable: true, get: function () { return subjects_js_1.SUBJECT_LIMITS; } });
27
+ var signing_js_1 = require("./signing.js");
28
+ Object.defineProperty(exports, "verifySignatureHeader", { enumerable: true, get: function () { return signing_js_1.verifySignatureHeader; } });
29
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,6CAA4E;AAAnE,2GAAA,YAAY,OAAA;AAAE,sGAAA,OAAO,OAAA;AAC9B,6CAAwF;AAA/E,mHAAA,oBAAoB,OAAA;AAAE,6GAAA,cAAc,OAAA;AAC7C,2CAAqD;AAA5C,mHAAA,qBAAqB,OAAA"}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,152 @@
1
+ import { type EventSubject } from './subjects.js';
2
+ /** Registered globally by description, so every copy of this package agrees on it. */
3
+ declare const CONFLICTING_EVENT_BRAND: unique symbol;
4
+ /**
5
+ * The minimum CommitRail needs to write a row.
6
+ *
7
+ * Structural on purpose, with no dependency on any driver: a `pg` `PoolClient` satisfies
8
+ * it as-is, and `fromPrisma` adapts a Prisma transaction client. Requiring a particular
9
+ * driver would make adoption a migration.
10
+ */
11
+ export interface OutboxWriter {
12
+ query(sql: string, params: unknown[]): Promise<unknown>;
13
+ }
14
+ export interface EmitEvent<TData = unknown> {
15
+ type: string;
16
+ data: TData;
17
+ version?: number;
18
+ occurredAt?: Date;
19
+ /** Supply one to make emitting idempotent under your own retries. */
20
+ eventId?: string;
21
+ /**
22
+ * The logical operation this event belongs to — an order number, a checkout id, whatever
23
+ * your application already calls it.
24
+ *
25
+ * CommitRail groups events that share one and never invents one. Propagate it into the
26
+ * events your consumers emit in turn, and the chain joins up on its own.
27
+ */
28
+ correlationId?: string;
29
+ /** The event that caused this one, if your application knows. */
30
+ causationId?: string;
31
+ /**
32
+ * The business identities this event concerns — `{ type: 'order', id: 'order_1264' }`.
33
+ *
34
+ * Distinct from everything above: the event type says *what happened*, the correlation id
35
+ * says *which operation it belongs to*, the causation id says *what caused it* — subjects
36
+ * say *what it was about*. Declare every relevant identity; CommitRail indexes them so an
37
+ * investigation can start from an order or customer id rather than an event id. Exact
38
+ * duplicates are dropped; order carries no meaning.
39
+ */
40
+ subjects?: EventSubject[];
41
+ }
42
+ /**
43
+ * The outbox schema, as an append-only list of migrations.
44
+ *
45
+ * A list rather than one script, because the alternative already caused a problem. A single
46
+ * converge-to-latest constant changes when the package changes, so a customer who pasted it
47
+ * into `001_add_commitrail.sql` finds that migration meaning something different a year later
48
+ * — which is the one thing migrations exist not to do. Pinning a version instead gives them an
49
+ * immutable statement of what they applied.
50
+ *
51
+ * **Every entry here is frozen once released.** A released migration has run against databases
52
+ * we do not own and cannot re-run; editing one changes what a version *means* while leaving
53
+ * every already-migrated database untouched. Fix a mistake by appending, never by amending.
54
+ *
55
+ * Each is written to be safely re-runnable — `IF NOT EXISTS`, `ADD COLUMN IF NOT EXISTS` — so
56
+ * applying the whole list to any database converges it, whatever it started from.
57
+ */
58
+ export declare const OUTBOX_MIGRATIONS: readonly {
59
+ readonly version: number;
60
+ readonly sql: string;
61
+ }[];
62
+ /** The newest schema this package knows how to write. */
63
+ export declare const OUTBOX_SCHEMA_VERSION: number;
64
+ /**
65
+ * Every migration, in order, for a fresh install or to bring an old outbox up to date.
66
+ *
67
+ * Safe to run against any database at any version: every statement is conditional, and the
68
+ * marker only moves forward. This is the convenience; `OUTBOX_MIGRATIONS` is the contract.
69
+ *
70
+ * Versions before the marker table existed record nothing — there is nowhere to record it. The
71
+ * migration that creates the table sets the version to its own, which is sound because the list
72
+ * is ordered and everything before it has just run.
73
+ */
74
+ export declare const OUTBOX_SCHEMA_SQL: string;
75
+ /**
76
+ * A different event was already written under this `eventId`.
77
+ *
78
+ * Supplying an `eventId` is a claim that two calls describe the same event. When they do not,
79
+ * one of them is a mistake — a reused id, or a collision — and CommitRail has no way to know
80
+ * which. Discarding the second silently would leave the producer believing it was written.
81
+ *
82
+ * Thrown from inside the caller's transaction. **When the exception is allowed to propagate
83
+ * out of a `transaction()` callback, the transaction rolls back** and nothing half-happens.
84
+ * That is the intended handling and the reason this throws rather than returning a flag.
85
+ *
86
+ * Note what a throw does not do: it does not poison the PostgreSQL transaction by itself. A
87
+ * caller managing `BEGIN`/`COMMIT` themselves can catch this and commit anyway, and would then
88
+ * have committed business state describing an event that was never written. There is no way for
89
+ * an SDK to prevent that; enforcing it against every writer would mean putting the rule in the
90
+ * customer's database, which is a schema-versioning cost not worth paying yet. Catching this and
91
+ * continuing is almost certainly wrong.
92
+ */
93
+ export declare class EventIdConflictError extends Error {
94
+ readonly eventId: string;
95
+ static readonly brand: symbol;
96
+ readonly [CONFLICTING_EVENT_BRAND] = true;
97
+ constructor(eventId: string);
98
+ static is(error: unknown): error is EventIdConflictError;
99
+ }
100
+ /**
101
+ * Write an event to the outbox **inside the caller's transaction**.
102
+ *
103
+ * That is the entire point, and the only thing that can go wrong here. Pass a transaction
104
+ * and the event commits with your business state or not at all. Pass a pool — which also
105
+ * has `.query` — and you have written the event on a separate connection, which is the
106
+ * dual write CommitRail exists to eliminate. Nothing at runtime can tell the two apart, so
107
+ * prefer `transaction()` below, which does not give you the chance.
108
+ *
109
+ * If you already manage transactions yourself, call `emit` on your transaction-scoped client.
110
+ * The example is deliberately not a `BEGIN`/`COMMIT` pair: written short it has no rollback
111
+ * path, and `emit` throwing would leave the connection inside a failed transaction.
112
+ *
113
+ * ```ts
114
+ * // `tx` is whatever your code already uses for the surrounding transaction.
115
+ * await emit(tx, { type: 'order.created', data: { orderId } });
116
+ * ```
117
+ */
118
+ export declare function emit<TData>(writer: OutboxWriter, event: EmitEvent<TData>): Promise<string>;
119
+ export interface TransactionalWriter extends OutboxWriter {
120
+ emit<TData>(event: EmitEvent<TData>): Promise<string>;
121
+ }
122
+ interface Pool {
123
+ connect(): Promise<OutboxWriter & {
124
+ release(): void;
125
+ }>;
126
+ }
127
+ /**
128
+ * Run work in a transaction, with `emit` bound to it.
129
+ *
130
+ * The recommended shape, because it removes the one mistake available: the writer handed
131
+ * to the callback is the transaction, so an event cannot accidentally be written outside
132
+ * it.
133
+ *
134
+ * ```ts
135
+ * await transaction(pool, async (tx) => {
136
+ * await tx.query('INSERT INTO orders (id, total) VALUES ($1, $2)', [id, total]);
137
+ * await tx.emit({ type: 'order.created', data: { orderId: id } });
138
+ * });
139
+ * ```
140
+ */
141
+ export declare function transaction<T>(pool: Pool, work: (tx: TransactionalWriter) => Promise<T>): Promise<T>;
142
+ /**
143
+ * Adapt a Prisma transaction client.
144
+ *
145
+ * Typed structurally so this package does not depend on Prisma. Uses `$executeRawUnsafe`
146
+ * because the SQL is a constant defined above and the values are parameterised — the
147
+ * "unsafe" in the name refers to interpolating the statement, which never happens here.
148
+ */
149
+ export declare function fromPrisma(tx: {
150
+ $executeRawUnsafe(sql: string, ...values: unknown[]): Promise<number>;
151
+ }): OutboxWriter;
152
+ export {};