@antelopejs/payment-subscriptions 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +190 -0
  2. package/README.md +102 -2
  3. package/dist/calendar.d.ts +2 -0
  4. package/dist/calendar.js +33 -0
  5. package/dist/calendar.js.map +1 -0
  6. package/dist/clock.d.ts +7 -0
  7. package/dist/clock.js +18 -0
  8. package/dist/clock.js.map +1 -0
  9. package/dist/config.d.ts +13 -0
  10. package/dist/config.js +28 -0
  11. package/dist/config.js.map +1 -0
  12. package/dist/engine.d.ts +38 -0
  13. package/dist/engine.js +250 -0
  14. package/dist/engine.js.map +1 -0
  15. package/dist/events.d.ts +6 -0
  16. package/dist/events.js +47 -0
  17. package/dist/events.js.map +1 -0
  18. package/dist/implementation.d.ts +19 -0
  19. package/dist/implementation.js +233 -0
  20. package/dist/implementation.js.map +1 -0
  21. package/dist/index.d.ts +8 -0
  22. package/dist/index.js +75 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/initial.d.ts +11 -0
  25. package/dist/initial.js +86 -0
  26. package/dist/initial.js.map +1 -0
  27. package/dist/records.d.ts +60 -0
  28. package/dist/records.js +68 -0
  29. package/dist/records.js.map +1 -0
  30. package/dist/runtime.d.ts +10 -0
  31. package/dist/runtime.js +14 -0
  32. package/dist/runtime.js.map +1 -0
  33. package/dist/schedule.d.ts +15 -0
  34. package/dist/schedule.js +35 -0
  35. package/dist/schedule.js.map +1 -0
  36. package/dist/transitions.d.ts +25 -0
  37. package/dist/transitions.js +197 -0
  38. package/dist/transitions.js.map +1 -0
  39. package/dist/triggers.d.ts +10 -0
  40. package/dist/triggers.js +129 -0
  41. package/dist/triggers.js.map +1 -0
  42. package/dist/view.d.ts +3 -0
  43. package/dist/view.js +34 -0
  44. package/dist/view.js.map +1 -0
  45. package/docs/1.introduction.md +61 -0
  46. package/docs/2.renewal-clock.md +95 -0
  47. package/docs/3.failed-renewals.md +126 -0
  48. package/docs/4.provider-schedule.md +31 -0
  49. package/package.json +85 -4
  50. package/skills/payment-subscriptions/SKILL.md +73 -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 (c) 2025-present - AntelopeJS Team
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 CHANGED
@@ -1,3 +1,103 @@
1
- # Temporary Holding Version
1
+ # @antelopejs/payment-subscriptions
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm version](https://img.shields.io/npm/v/@antelopejs/payment-subscriptions.svg)](https://www.npmjs.com/package/@antelopejs/payment-subscriptions)
4
+ [![license](https://img.shields.io/npm/l/@antelopejs/payment-subscriptions.svg)](./LICENSE)
5
+
6
+ Subscriptions for any AntelopeJS payment provider. Implements
7
+ `@antelopejs/interface-subscriptions` on top of `@antelopejs/interface-payment`,
8
+ using saved payment methods and off-session charges, and runs the renewal clock
9
+ itself — for the providers that leave each renewal to the merchant (Adyen,
10
+ Checkout.com, Mangopay), and for any provider at all.
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ ajs project modules add @antelopejs/payment-subscriptions
16
+ ```
17
+
18
+ Wire it alongside:
19
+
20
+ - a module implementing `@antelopejs/interface-payment` (the one that charges);
21
+ - a module implementing `@antelopejs/interface-database` (where every
22
+ subscription lives — `@antelopejs/mongodb`, for instance);
23
+ - optionally, `@antelopejs/redis`, for renewals charged on time rather than
24
+ within one poll.
25
+
26
+ ```ts
27
+ export default defineConfig({
28
+ modules: {
29
+ subscriptions: { source: { type: "package", package: "@antelopejs/payment-subscriptions" } },
30
+ payments: { source: { type: "package", package: "<your payment-interface module>" }, config: { ... } },
31
+ mongodb: { source: { type: "package", package: "@antelopejs/mongodb" }, config: { ... } },
32
+ },
33
+ });
34
+ ```
35
+
36
+ ### With a payment module that has subscriptions of its own
37
+
38
+ `@antelopejs/stripe` implements `@antelopejs/interface-subscriptions` natively.
39
+ Wire it alone for Stripe's own subscriptions. Wiring this module beside it makes
40
+ two providers of the interface, and the core picks one by module id, not by
41
+ intent. To use this module's clock with Stripe charges, say so: add
42
+ `disabledExports: ["@antelopejs/interface-subscriptions"]` to the Stripe module's
43
+ entry, or an `importOverrides` entry on each consumer.
44
+
45
+ ## How it works
46
+
47
+ - **The database is the only source of truth.** Each subscription is one record
48
+ holding its period, its status, when it next needs attention and the events it
49
+ has yet to deliver. A renewal is recoverable from the database alone.
50
+ - **The clock is only a trigger.** With a Redis scheduler wired, each subscription
51
+ is one task fired when it is due, and a slow database sweep catches anything
52
+ Redis loses. Without one, the database is polled. Redis is optional, and nothing
53
+ ever waits on it: a module with no Redis wired starts and renews. The suite
54
+ verifies normal Redis interface routing under AntelopeJS core 1.16.0.
55
+ - **Charge recovery is bounded.** An instance claims a due renewal with a
56
+ conditional write before charging it, so of several racing instances one
57
+ charges and the rest stand down. The charge also carries an idempotency key
58
+ derived from the subscription, the period and the attempt, so an instance that
59
+ takes over an abandoned claim can replay only within the configured provider
60
+ guarantee. Without that protection, an unknown result halts for reconciliation.
61
+
62
+ ## Configuration
63
+
64
+ Every field is optional.
65
+
66
+ | Field | Default | Meaning |
67
+ | -------------------- | ------------------------- | ----------------------------------------------------------------------------------- |
68
+ | `pollIntervalMs` | 60 000 | How often the database is polled when no Redis scheduler is wired |
69
+ | `sweepIntervalMs` | 900 000 | How often the database is swept when one is |
70
+ | `leaseMs` | 300 000 | How long a claim on a renewal lasts before another instance may take it over |
71
+ | `retryDelaysMs` | one, three and seven days | The failed-renewal policy: one retry per entry, then the subscription ends |
72
+ | `settlementPollMs` | 3 600 000 | How often a charge that is still settling is read again |
73
+ | `errorBackoffMs` | 300 000 | How long to wait when the payment provider cannot be reached at all |
74
+ | `batchSize` | 100 | How many due subscriptions one sweep takes on |
75
+ | `providerSchedule` | `payment-interface` | How the schedule is mirrored with the provider; see the provider schedule document |
76
+ | `guaranteedReplayMs` | `0` | Guaranteed provider idempotency retention, with safety margin; zero disables replay |
77
+
78
+ Use Stripe's native subscriptions by default for Stripe users; this module is an
79
+ explicit fallback. Before enabling retries of uncertain requests, verify the
80
+ provider's retention guarantee for every wired account. There is no portable
81
+ capability API and no assumed Stripe-style 24-hour window. See
82
+ [safe recovery](./docs/3.failed-renewals.md#reconciliation-and-operational-recovery).
83
+
84
+ ## Documentation
85
+
86
+ | Document | Contents |
87
+ | ------------------------------------------------------ | ----------------------------------------------------------- |
88
+ | [Introduction](./docs/1.introduction.md) | What it implements, what it needs, what it records |
89
+ | [The renewal clock](./docs/2.renewal-clock.md) | Polling and Redis, claims, idempotency, several instances |
90
+ | [Failed renewals](./docs/3.failed-renewals.md) | The retry policy, settling charges, an unreachable provider |
91
+ | [The provider schedule](./docs/4.provider-schedule.md) | The seam for providers that store a schedule (Mangopay) |
92
+
93
+ ## Testing
94
+
95
+ `pnpm test` runs the suite twice, once per clock, against a real MongoDB and —
96
+ for the second run — a real Redis, both started by the suite. The interface's
97
+ conformance suite runs in both. A test-only payment provider in
98
+ `test/payment-provider` stands in for the payment module; it is never published.
99
+ The scripts use the local AntelopeJS core 1.16.0 runner (`ajs module test .`).
100
+
101
+ ## License
102
+
103
+ Apache-2.0
@@ -0,0 +1,2 @@
1
+ import type { SubscriptionInterval } from "@antelopejs/interface-subscriptions";
2
+ export declare function PeriodBoundary(anchor: number, interval: SubscriptionInterval, intervalCount: number, index: number): number;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PeriodBoundary = PeriodBoundary;
4
+ const DAY_MS = 24 * 60 * 60 * 1000;
5
+ const MONTHS_PER_YEAR = 12;
6
+ function addDays(days) {
7
+ return (anchor, steps) => anchor + steps * days * DAY_MS;
8
+ }
9
+ function lastDayOfMonth(year, month) {
10
+ return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
11
+ }
12
+ function addMonths(months) {
13
+ return (anchor, steps) => {
14
+ const start = new Date(anchor);
15
+ const target = start.getUTCMonth() + steps * months;
16
+ const year = start.getUTCFullYear() + Math.floor(target / MONTHS_PER_YEAR);
17
+ const month = ((target % MONTHS_PER_YEAR) + MONTHS_PER_YEAR) % MONTHS_PER_YEAR;
18
+ const day = Math.min(start.getUTCDate(), lastDayOfMonth(year, month));
19
+ const shifted = new Date(anchor);
20
+ shifted.setUTCFullYear(year, month, day);
21
+ return shifted.getTime();
22
+ };
23
+ }
24
+ const STEPS = {
25
+ day: addDays(1),
26
+ week: addDays(7),
27
+ month: addMonths(1),
28
+ year: addMonths(MONTHS_PER_YEAR),
29
+ };
30
+ function PeriodBoundary(anchor, interval, intervalCount, index) {
31
+ return STEPS[interval](anchor, index * intervalCount);
32
+ }
33
+ //# sourceMappingURL=calendar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calendar.js","sourceRoot":"","sources":["../src/calendar.ts"],"names":[],"mappings":";;AAoCA,wCAOC;AAzCD,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AACnC,MAAM,eAAe,GAAG,EAAE,CAAC;AAI3B,SAAS,OAAO,CAAC,IAAY;IAC3B,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,MAAM,GAAG,KAAK,GAAG,IAAI,GAAG,MAAM,CAAC;AAC3D,CAAC;AAED,SAAS,cAAc,CAAC,IAAY,EAAE,KAAa;IACjD,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC;AAC7D,CAAC;AAED,SAAS,SAAS,CAAC,MAAc;IAC/B,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;QAC/B,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,EAAE,GAAG,KAAK,GAAG,MAAM,CAAC;QACpD,MAAM,IAAI,GAAG,KAAK,CAAC,cAAc,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,eAAe,CAAC,CAAC;QAC3E,MAAM,KAAK,GACT,CAAC,CAAC,MAAM,GAAG,eAAe,CAAC,GAAG,eAAe,CAAC,GAAG,eAAe,CAAC;QACnE,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,EAAE,cAAc,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;QACtE,MAAM,OAAO,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC,CAAC;QACjC,OAAO,CAAC,cAAc,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QACzC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,KAAK,GAAuC;IAChD,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;IACf,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IAChB,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;IACnB,IAAI,EAAE,SAAS,CAAC,eAAe,CAAC;CACjC,CAAC;AAEF,SAAgB,cAAc,CAC5B,MAAc,EACd,QAA8B,EAC9B,aAAqB,EACrB,KAAa;IAEb,OAAO,KAAK,CAAC,QAAQ,CAAC,CAAC,MAAM,EAAE,KAAK,GAAG,aAAa,CAAC,CAAC;AACxD,CAAC"}
@@ -0,0 +1,7 @@
1
+ export interface Clock {
2
+ now(): number;
3
+ }
4
+ export declare const SYSTEM_CLOCK: Clock;
5
+ export declare function Now(): number;
6
+ export declare function UseClock(clock: Clock): void;
7
+ export declare function RealDelayUntil(time: number): number;
package/dist/clock.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SYSTEM_CLOCK = void 0;
4
+ exports.Now = Now;
5
+ exports.UseClock = UseClock;
6
+ exports.RealDelayUntil = RealDelayUntil;
7
+ exports.SYSTEM_CLOCK = Object.freeze({ now: () => Date.now() });
8
+ let current = exports.SYSTEM_CLOCK;
9
+ function Now() {
10
+ return current.now();
11
+ }
12
+ function UseClock(clock) {
13
+ current = clock;
14
+ }
15
+ function RealDelayUntil(time) {
16
+ return Math.max(0, time - Now());
17
+ }
18
+ //# sourceMappingURL=clock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"clock.js","sourceRoot":"","sources":["../src/clock.ts"],"names":[],"mappings":";;;AAQA,kBAEC;AAED,4BAEC;AAED,wCAEC;AAdY,QAAA,YAAY,GAAU,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;AAE5E,IAAI,OAAO,GAAU,oBAAY,CAAC;AAElC,SAAgB,GAAG;IACjB,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC;AACvB,CAAC;AAED,SAAgB,QAAQ,CAAC,KAAY;IACnC,OAAO,GAAG,KAAK,CAAC;AAClB,CAAC;AAED,SAAgB,cAAc,CAAC,IAAY;IACzC,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,GAAG,EAAE,CAAC,CAAC;AACnC,CAAC"}
@@ -0,0 +1,13 @@
1
+ export interface Config {
2
+ guaranteedReplayMs?: number;
3
+ pollIntervalMs?: number;
4
+ sweepIntervalMs?: number;
5
+ leaseMs?: number;
6
+ retryDelaysMs?: number[];
7
+ settlementPollMs?: number;
8
+ errorBackoffMs?: number;
9
+ batchSize?: number;
10
+ providerSchedule?: string;
11
+ }
12
+ export type Settings = Required<Config>;
13
+ export declare function ResolveSettings(config?: Config): Settings;
package/dist/config.js ADDED
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ResolveSettings = ResolveSettings;
4
+ const SECOND_MS = 1000;
5
+ const MINUTE_MS = 60 * SECOND_MS;
6
+ const HOUR_MS = 60 * MINUTE_MS;
7
+ const DAY_MS = 24 * HOUR_MS;
8
+ const DEFAULT_SETTINGS = Object.freeze({
9
+ guaranteedReplayMs: 0,
10
+ pollIntervalMs: MINUTE_MS,
11
+ sweepIntervalMs: 15 * MINUTE_MS,
12
+ leaseMs: 5 * MINUTE_MS,
13
+ retryDelaysMs: Object.freeze([DAY_MS, 3 * DAY_MS, 7 * DAY_MS]),
14
+ settlementPollMs: HOUR_MS,
15
+ errorBackoffMs: 5 * MINUTE_MS,
16
+ batchSize: 100,
17
+ providerSchedule: "payment-interface",
18
+ });
19
+ function ResolveSettings(config = {}) {
20
+ if (config.guaranteedReplayMs !== undefined &&
21
+ (!Number.isFinite(config.guaranteedReplayMs) ||
22
+ config.guaranteedReplayMs < 0)) {
23
+ throw new Error("guaranteedReplayMs must be a finite non-negative duration");
24
+ }
25
+ const defined = Object.entries(config).filter(([, value]) => value !== undefined);
26
+ return { ...DEFAULT_SETTINGS, ...Object.fromEntries(defined) };
27
+ }
28
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":";;AAwEA,0CAcC;AAtFD,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,MAAM,SAAS,GAAG,EAAE,GAAG,SAAS,CAAC;AACjC,MAAM,OAAO,GAAG,EAAE,GAAG,SAAS,CAAC;AAC/B,MAAM,MAAM,GAAG,EAAE,GAAG,OAAO,CAAC;AAyD5B,MAAM,gBAAgB,GAAa,MAAM,CAAC,MAAM,CAAC;IAC/C,kBAAkB,EAAE,CAAC;IACrB,cAAc,EAAE,SAAS;IACzB,eAAe,EAAE,EAAE,GAAG,SAAS;IAC/B,OAAO,EAAE,CAAC,GAAG,SAAS;IACtB,aAAa,EAAE,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,GAAG,MAAM,CAAC,CAAa;IAC1E,gBAAgB,EAAE,OAAO;IACzB,cAAc,EAAE,CAAC,GAAG,SAAS;IAC7B,SAAS,EAAE,GAAG;IACd,gBAAgB,EAAE,mBAAmB;CACtC,CAAC,CAAC;AAEH,SAAgB,eAAe,CAAC,SAAiB,EAAE;IACjD,IACE,MAAM,CAAC,kBAAkB,KAAK,SAAS;QACvC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC;YAC1C,MAAM,CAAC,kBAAkB,GAAG,CAAC,CAAC,EAChC,CAAC;QACD,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;IACJ,CAAC;IACD,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,CAC3C,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CACnC,CAAC;IACF,OAAO,EAAE,GAAG,gBAAgB,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC;AACjE,CAAC"}
@@ -0,0 +1,38 @@
1
+ import type { Settings } from "./config";
2
+ import type { ProviderSchedule } from "./schedule";
3
+ import { type RecordPatch, type SubscriptionRecord } from "./records";
4
+ import { type Decision, type Outcome, type Transition } from "./transitions";
5
+ export type WriteListener = (record: SubscriptionRecord) => void;
6
+ export interface EngineStats {
7
+ claimed: number;
8
+ claimsLost: number;
9
+ charges: number;
10
+ }
11
+ export declare function Finalize(record: SubscriptionRecord, transition: Transition, now: number): RecordPatch;
12
+ export declare class Engine {
13
+ private readonly settings;
14
+ private readonly schedule;
15
+ readonly instanceId: `${string}-${string}-${string}-${string}-${string}`;
16
+ readonly stats: EngineStats;
17
+ private listener;
18
+ private readonly inFlight;
19
+ constructor(settings: Settings, schedule: ProviderSchedule);
20
+ onWrite(listener: WriteListener): void;
21
+ notify(record: SubscriptionRecord): void;
22
+ sweep(): Promise<void>;
23
+ process(id: string): Promise<void>;
24
+ private processClaimed;
25
+ processInBackground(id: string): void;
26
+ drain(): Promise<void>;
27
+ private claim;
28
+ private act;
29
+ pinAttemptMethod(record: SubscriptionRecord, decided: Decision): Promise<SubscriptionRecord | undefined>;
30
+ perform(record: SubscriptionRecord, decided: Decision): Promise<Outcome>;
31
+ private charge;
32
+ commit(record: SubscriptionRecord, decided: Decision, outcome: Outcome, extra?: Partial<Transition>): Promise<SubscriptionRecord>;
33
+ write(record: SubscriptionRecord, patch: RecordPatch): Promise<SubscriptionRecord | undefined>;
34
+ release(record: SubscriptionRecord): Promise<void>;
35
+ private releasePatch;
36
+ releaseScheduleFor(id: string): Promise<void>;
37
+ private releaseSchedule;
38
+ }