@nathapp/nestjs-outbox 3.3.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.
- package/.eslintrc.js +8 -0
- package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +878 -0
- package/.turbo/turbo-build.log +4 -0
- package/CHANGELOG.md +15 -0
- package/README.md +50 -0
- package/dist/constant/index.d.ts +1 -0
- package/dist/constant/index.js +18 -0
- package/dist/constant/index.js.map +1 -0
- package/dist/constant/outbox.constants.d.ts +3 -0
- package/dist/constant/outbox.constants.js +7 -0
- package/dist/constant/outbox.constants.js.map +1 -0
- package/dist/exception/index.d.ts +3 -0
- package/dist/exception/index.js +20 -0
- package/dist/exception/index.js.map +1 -0
- package/dist/exception/outbox-exception-code.enum.d.ts +4 -0
- package/dist/exception/outbox-exception-code.enum.js +9 -0
- package/dist/exception/outbox-exception-code.enum.js.map +1 -0
- package/dist/exception/outbox-invalid-event.exception.d.ts +4 -0
- package/dist/exception/outbox-invalid-event.exception.js +13 -0
- package/dist/exception/outbox-invalid-event.exception.js.map +1 -0
- package/dist/exception/outbox-misconfigured.exception.d.ts +4 -0
- package/dist/exception/outbox-misconfigured.exception.js +13 -0
- package/dist/exception/outbox-misconfigured.exception.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/interface/index.d.ts +6 -0
- package/dist/interface/index.js +6 -0
- package/dist/interface/index.js.map +1 -0
- package/dist/interface/outbox-module-options.interface.d.ts +16 -0
- package/dist/interface/outbox-module-options.interface.js +3 -0
- package/dist/interface/outbox-module-options.interface.js.map +1 -0
- package/dist/interface/outbox-publisher.interface.d.ts +4 -0
- package/dist/interface/outbox-publisher.interface.js +3 -0
- package/dist/interface/outbox-publisher.interface.js.map +1 -0
- package/dist/interface/outbox-record.interface.d.ts +21 -0
- package/dist/interface/outbox-record.interface.js +3 -0
- package/dist/interface/outbox-record.interface.js.map +1 -0
- package/dist/interface/outbox-status.enum.d.ts +6 -0
- package/dist/interface/outbox-status.enum.js +11 -0
- package/dist/interface/outbox-status.enum.js.map +1 -0
- package/dist/interface/outbox-store.interface.d.ts +8 -0
- package/dist/interface/outbox-store.interface.js +3 -0
- package/dist/interface/outbox-store.interface.js.map +1 -0
- package/dist/module/index.d.ts +1 -0
- package/dist/module/index.js +18 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/outbox.module.d.ts +10 -0
- package/dist/module/outbox.module.js +63 -0
- package/dist/module/outbox.module.js.map +1 -0
- package/dist/provider/in-memory-outbox-store.d.ts +12 -0
- package/dist/provider/in-memory-outbox-store.js +97 -0
- package/dist/provider/in-memory-outbox-store.js.map +1 -0
- package/dist/provider/index.d.ts +4 -0
- package/dist/provider/index.js +21 -0
- package/dist/provider/index.js.map +1 -0
- package/dist/provider/outbox-relay.d.ts +20 -0
- package/dist/provider/outbox-relay.js +129 -0
- package/dist/provider/outbox-relay.js.map +1 -0
- package/dist/provider/outbox.service.d.ts +12 -0
- package/dist/provider/outbox.service.js +68 -0
- package/dist/provider/outbox.service.js.map +1 -0
- package/dist/provider/resolve-outbox-options.d.ts +29 -0
- package/dist/provider/resolve-outbox-options.js +34 -0
- package/dist/provider/resolve-outbox-options.js.map +1 -0
- package/dist/util/compute-backoff.d.ts +1 -0
- package/dist/util/compute-backoff.js +8 -0
- package/dist/util/compute-backoff.js.map +1 -0
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +18 -0
- package/dist/util/index.js.map +1 -0
- package/lib/constant/index.ts +1 -0
- package/lib/constant/outbox.constants.ts +3 -0
- package/lib/exception/index.ts +3 -0
- package/lib/exception/outbox-exception-code.enum.ts +4 -0
- package/lib/exception/outbox-invalid-event.exception.ts +9 -0
- package/lib/exception/outbox-misconfigured.exception.ts +9 -0
- package/lib/index.ts +6 -0
- package/lib/interface/index.ts +18 -0
- package/lib/interface/outbox-module-options.interface.ts +18 -0
- package/lib/interface/outbox-publisher.interface.ts +5 -0
- package/lib/interface/outbox-record.interface.ts +23 -0
- package/lib/interface/outbox-status.enum.ts +6 -0
- package/lib/interface/outbox-store.interface.ts +23 -0
- package/lib/module/index.ts +1 -0
- package/lib/module/outbox.module.ts +64 -0
- package/lib/provider/in-memory-outbox-store.ts +114 -0
- package/lib/provider/index.ts +4 -0
- package/lib/provider/outbox-relay.ts +147 -0
- package/lib/provider/outbox.service.ts +70 -0
- package/lib/provider/resolve-outbox-options.ts +51 -0
- package/lib/util/compute-backoff.ts +4 -0
- package/lib/util/index.ts +1 -0
- package/package.json +48 -0
- package/test/barrel/index.spec.ts +45 -0
- package/test/constant/outbox.constants.spec.ts +20 -0
- package/test/e2e/outbox.module.spec.ts +371 -0
- package/test/exception/outbox.exceptions.spec.ts +38 -0
- package/test/interface/outbox-status.enum.spec.ts +19 -0
- package/test/jest-nax-setup.js +31 -0
- package/test/jest-nax.json +30 -0
- package/test/jest-test.json +28 -0
- package/test/module/outbox.module.spec.ts +139 -0
- package/test/provider/in-memory-outbox-store.spec.ts +216 -0
- package/test/provider/outbox-relay.spec.ts +565 -0
- package/test/provider/outbox-service.spec.ts +229 -0
- package/test/provider/resolve-outbox-options.spec.ts +50 -0
- package/test/util/compute-backoff.spec.ts +25 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./in-memory-outbox-store"), exports);
|
|
18
|
+
__exportStar(require("./outbox-relay"), exports);
|
|
19
|
+
__exportStar(require("./outbox.service"), exports);
|
|
20
|
+
__exportStar(require("./resolve-outbox-options"), exports);
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/provider/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2DAAyC;AACzC,iDAA+B;AAC/B,mDAAiC;AACjC,2DAAyC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import { IClock, IIdGenerator } from '@nathapp/nestjs-common';
|
|
3
|
+
import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
|
|
4
|
+
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
5
|
+
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
6
|
+
export declare class OutboxRelay implements OnApplicationBootstrap, OnModuleDestroy {
|
|
7
|
+
private readonly clock;
|
|
8
|
+
private readonly idGenerator;
|
|
9
|
+
private readonly store;
|
|
10
|
+
private readonly publisher;
|
|
11
|
+
private readonly options;
|
|
12
|
+
private inFlight;
|
|
13
|
+
private interval;
|
|
14
|
+
private readonly logger;
|
|
15
|
+
constructor(rawOptions: OutboxModuleOptions | undefined, store: IOutboxStore, publisher?: IOutboxPublisher, clock?: IClock, idGenerator?: IIdGenerator);
|
|
16
|
+
dispatchPendingBatch(): Promise<void>;
|
|
17
|
+
private runCycle;
|
|
18
|
+
onApplicationBootstrap(): Promise<void>;
|
|
19
|
+
onModuleDestroy(): Promise<void>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
var OutboxRelay_1;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.OutboxRelay = void 0;
|
|
17
|
+
const common_1 = require("@nestjs/common");
|
|
18
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
19
|
+
const outbox_constants_1 = require("../constant/outbox.constants");
|
|
20
|
+
const outbox_misconfigured_exception_1 = require("../exception/outbox-misconfigured.exception");
|
|
21
|
+
const resolve_outbox_options_1 = require("./resolve-outbox-options");
|
|
22
|
+
const compute_backoff_1 = require("../util/compute-backoff");
|
|
23
|
+
let OutboxRelay = OutboxRelay_1 = class OutboxRelay {
|
|
24
|
+
constructor(rawOptions, store, publisher, clock, idGenerator) {
|
|
25
|
+
var _a;
|
|
26
|
+
this.inFlight = null;
|
|
27
|
+
this.interval = null;
|
|
28
|
+
this.logger = new common_1.Logger(OutboxRelay_1.name);
|
|
29
|
+
this.store = store;
|
|
30
|
+
this.publisher = (_a = rawOptions === null || rawOptions === void 0 ? void 0 : rawOptions.publisher) !== null && _a !== void 0 ? _a : publisher;
|
|
31
|
+
this.options = (0, resolve_outbox_options_1.resolveOutboxOptions)(rawOptions).relay;
|
|
32
|
+
this.clock = clock !== null && clock !== void 0 ? clock : new nestjs_common_1.SystemClock();
|
|
33
|
+
this.idGenerator = idGenerator !== null && idGenerator !== void 0 ? idGenerator : new nestjs_common_1.UuidIdGenerator();
|
|
34
|
+
}
|
|
35
|
+
async dispatchPendingBatch() {
|
|
36
|
+
if (this.publisher === undefined) {
|
|
37
|
+
throw new outbox_misconfigured_exception_1.OutboxMisconfiguredException();
|
|
38
|
+
}
|
|
39
|
+
if (this.inFlight !== null) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const publisher = this.publisher;
|
|
43
|
+
const cycle = this.runCycle(publisher);
|
|
44
|
+
this.inFlight = cycle;
|
|
45
|
+
try {
|
|
46
|
+
await cycle;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
this.inFlight = null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async runCycle(publisher) {
|
|
53
|
+
const now = this.clock.now();
|
|
54
|
+
const owner = this.idGenerator.generate();
|
|
55
|
+
const batch = await this.store.claimBatch(this.options.batchSize, this.options.leaseMs, now, owner);
|
|
56
|
+
if (batch.length === 0) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
for (const record of batch) {
|
|
60
|
+
try {
|
|
61
|
+
await publisher.publish(record);
|
|
62
|
+
try {
|
|
63
|
+
await this.store.markPublished(record.id, this.clock.now(), owner);
|
|
64
|
+
}
|
|
65
|
+
catch (_a) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (_b) {
|
|
70
|
+
const attempts = record.attempts + 1;
|
|
71
|
+
try {
|
|
72
|
+
if (attempts >= this.options.maxAttempts) {
|
|
73
|
+
await this.store.markDead(record.id, attempts, owner);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const failureNow = this.clock.now();
|
|
77
|
+
const backoffMs = (0, compute_backoff_1.computeBackoff)(attempts, this.options.backoffBaseMs, this.options.backoffCapMs);
|
|
78
|
+
const nextAttemptAt = new Date(failureNow.getTime() + backoffMs);
|
|
79
|
+
await this.store.markRetry(record.id, attempts, nextAttemptAt, owner);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (_c) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async onApplicationBootstrap() {
|
|
89
|
+
if (!this.options.enabled) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (this.publisher === undefined) {
|
|
93
|
+
throw new outbox_misconfigured_exception_1.OutboxMisconfiguredException();
|
|
94
|
+
}
|
|
95
|
+
this.interval = setInterval(() => {
|
|
96
|
+
this.dispatchPendingBatch().catch((err) => {
|
|
97
|
+
var _a;
|
|
98
|
+
this.logger.error(`Outbox relay cycle failed: ${(_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : err}`, err === null || err === void 0 ? void 0 : err.stack);
|
|
99
|
+
});
|
|
100
|
+
}, this.options.pollIntervalMs);
|
|
101
|
+
}
|
|
102
|
+
async onModuleDestroy() {
|
|
103
|
+
if (this.interval !== null) {
|
|
104
|
+
clearInterval(this.interval);
|
|
105
|
+
this.interval = null;
|
|
106
|
+
}
|
|
107
|
+
if (this.inFlight !== null) {
|
|
108
|
+
try {
|
|
109
|
+
await this.inFlight;
|
|
110
|
+
}
|
|
111
|
+
catch (_a) {
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
exports.OutboxRelay = OutboxRelay;
|
|
117
|
+
exports.OutboxRelay = OutboxRelay = OutboxRelay_1 = __decorate([
|
|
118
|
+
(0, common_1.Injectable)(),
|
|
119
|
+
__param(0, (0, common_1.Inject)(outbox_constants_1.OUTBOX_MODULE_OPTIONS)),
|
|
120
|
+
__param(1, (0, common_1.Inject)(outbox_constants_1.OUTBOX_STORE)),
|
|
121
|
+
__param(2, (0, common_1.Optional)()),
|
|
122
|
+
__param(2, (0, common_1.Inject)(outbox_constants_1.OUTBOX_PUBLISHER)),
|
|
123
|
+
__param(3, (0, common_1.Optional)()),
|
|
124
|
+
__param(3, (0, common_1.Inject)(nestjs_common_1.CLOCK)),
|
|
125
|
+
__param(4, (0, common_1.Optional)()),
|
|
126
|
+
__param(4, (0, common_1.Inject)(nestjs_common_1.ID_GENERATOR)),
|
|
127
|
+
__metadata("design:paramtypes", [Object, Object, Object, Object, Object])
|
|
128
|
+
], OutboxRelay);
|
|
129
|
+
//# sourceMappingURL=outbox-relay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outbox-relay.js","sourceRoot":"","sources":["../../lib/provider/outbox-relay.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,0DAOgC;AAChC,mEAIsC;AACtC,gGAA2F;AAI3F,qEAA4F;AAC5F,6DAAyD;AAGlD,IAAM,WAAW,mBAAjB,MAAM,WAAW;IAUtB,YACiC,UAA2C,EACpD,KAAmB,EACH,SAA4B,EACvC,KAAc,EACP,WAA0B;;QATtD,aAAQ,GAAyB,IAAI,CAAC;QACtC,aAAQ,GAA0C,IAAI,CAAC;QAC9C,WAAM,GAAG,IAAI,eAAM,CAAC,aAAW,CAAC,IAAI,CAAC,CAAC;QASrD,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,MAAA,UAAU,aAAV,UAAU,uBAAV,UAAU,CAAE,SAAS,mCAAI,SAAS,CAAC;QACpD,IAAI,CAAC,OAAO,GAAG,IAAA,6CAAoB,EAAC,UAAU,CAAC,CAAC,KAAK,CAAC;QACtD,IAAI,CAAC,KA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 @@
|
|
|
1
|
+
import { IClock, IIdGenerator } from '@nathapp/nestjs-common';
|
|
2
|
+
import { ITransactionManager } from '@nathapp/nestjs-data';
|
|
3
|
+
import { OutboxEventInput, OutboxRecord } from '../interface/outbox-record.interface';
|
|
4
|
+
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
5
|
+
export declare class OutboxService {
|
|
6
|
+
private readonly store;
|
|
7
|
+
private readonly txManager?;
|
|
8
|
+
private readonly clock;
|
|
9
|
+
private readonly idGenerator;
|
|
10
|
+
constructor(store: IOutboxStore, txManager?: ITransactionManager, clock?: IClock, idGenerator?: IIdGenerator);
|
|
11
|
+
record(event: OutboxEventInput): Promise<OutboxRecord>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.OutboxService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const nestjs_common_1 = require("@nathapp/nestjs-common");
|
|
18
|
+
const nestjs_data_1 = require("@nathapp/nestjs-data");
|
|
19
|
+
const outbox_constants_1 = require("../constant/outbox.constants");
|
|
20
|
+
const outbox_invalid_event_exception_1 = require("../exception/outbox-invalid-event.exception");
|
|
21
|
+
const outbox_status_enum_1 = require("../interface/outbox-status.enum");
|
|
22
|
+
let OutboxService = class OutboxService {
|
|
23
|
+
constructor(store, txManager, clock, idGenerator) {
|
|
24
|
+
this.store = store;
|
|
25
|
+
this.txManager = txManager;
|
|
26
|
+
this.clock = clock !== null && clock !== void 0 ? clock : new nestjs_common_1.SystemClock();
|
|
27
|
+
this.idGenerator = idGenerator !== null && idGenerator !== void 0 ? idGenerator : new nestjs_common_1.UuidIdGenerator();
|
|
28
|
+
}
|
|
29
|
+
async record(event) {
|
|
30
|
+
var _a;
|
|
31
|
+
if (event === null || typeof event !== 'object') {
|
|
32
|
+
throw new outbox_invalid_event_exception_1.OutboxInvalidEventException();
|
|
33
|
+
}
|
|
34
|
+
const rawType = event.type;
|
|
35
|
+
if (typeof rawType !== 'string' || rawType.trim().length === 0) {
|
|
36
|
+
throw new outbox_invalid_event_exception_1.OutboxInvalidEventException();
|
|
37
|
+
}
|
|
38
|
+
const type = rawType.trim();
|
|
39
|
+
const now = this.clock.now();
|
|
40
|
+
const record = {
|
|
41
|
+
id: this.idGenerator.generate(),
|
|
42
|
+
type,
|
|
43
|
+
payload: event.payload,
|
|
44
|
+
status: outbox_status_enum_1.OutboxStatus.PENDING,
|
|
45
|
+
attempts: 0,
|
|
46
|
+
createdAt: now,
|
|
47
|
+
nextAttemptAt: now,
|
|
48
|
+
headers: event.headers,
|
|
49
|
+
metadata: event.metadata,
|
|
50
|
+
};
|
|
51
|
+
const client = (_a = this.txManager) === null || _a === void 0 ? void 0 : _a.getClient();
|
|
52
|
+
await this.store.save(record, client);
|
|
53
|
+
return record;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.OutboxService = OutboxService;
|
|
57
|
+
exports.OutboxService = OutboxService = __decorate([
|
|
58
|
+
(0, common_1.Injectable)(),
|
|
59
|
+
__param(0, (0, common_1.Inject)(outbox_constants_1.OUTBOX_STORE)),
|
|
60
|
+
__param(1, (0, common_1.Optional)()),
|
|
61
|
+
__param(1, (0, common_1.Inject)(nestjs_data_1.TRANSACTION_MANAGER)),
|
|
62
|
+
__param(2, (0, common_1.Optional)()),
|
|
63
|
+
__param(2, (0, common_1.Inject)(nestjs_common_1.CLOCK)),
|
|
64
|
+
__param(3, (0, common_1.Optional)()),
|
|
65
|
+
__param(3, (0, common_1.Inject)(nestjs_common_1.ID_GENERATOR)),
|
|
66
|
+
__metadata("design:paramtypes", [Object, Object, Object, Object])
|
|
67
|
+
], OutboxService);
|
|
68
|
+
//# sourceMappingURL=outbox.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outbox.service.js","sourceRoot":"","sources":["../../lib/provider/outbox.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8D;AAC9D,0DAOgC;AAChC,sDAG8B;AAC9B,mEAA4D;AAC5D,gGAA0F;AAK1F,wEAA+D;AAIxD,IAAM,aAAa,GAAnB,MAAM,aAAa;IAIxB,YAEmB,KAAmB,EAEnB,SAA+B,EACrB,KAAc,EACP,WAA0B;QAJ3C,UAAK,GAAL,KAAK,CAAc;QAEnB,cAAS,GAAT,SAAS,CAAsB;QAIhD,IAAI,CAAC,KAAK,GAAG,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,IAAI,2BAAW,EAAE,CAAC;QACxC,IAAI,CAAC,WAAW,GAAG,WAAW,aAAX,WAAW,cAAX,WAAW,GAAI,IAAI,+BAAe,EAAE,CAAC;IAC1D,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,KAAuB;;QAClC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YAChD,MAAM,IAAI,4DAA2B,EAAE,CAAC;QAC1C,CAAC;QAED,MAAM,OAA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B"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
2
|
+
import { IOutboxStore } from '../interface/outbox-store.interface';
|
|
3
|
+
import { IOutboxPublisher } from '../interface/outbox-publisher.interface';
|
|
4
|
+
export interface ResolvedOutboxRelayOptions {
|
|
5
|
+
enabled: boolean;
|
|
6
|
+
pollIntervalMs: number;
|
|
7
|
+
batchSize: number;
|
|
8
|
+
leaseMs: number;
|
|
9
|
+
maxAttempts: number;
|
|
10
|
+
backoffBaseMs: number;
|
|
11
|
+
backoffCapMs: number;
|
|
12
|
+
}
|
|
13
|
+
export interface ResolvedOutboxOptions {
|
|
14
|
+
store: IOutboxStore;
|
|
15
|
+
publisher: IOutboxPublisher | undefined;
|
|
16
|
+
relay: ResolvedOutboxRelayOptions;
|
|
17
|
+
}
|
|
18
|
+
export declare const OUTBOX_DEFAULTS: {
|
|
19
|
+
readonly relay: {
|
|
20
|
+
readonly enabled: true;
|
|
21
|
+
readonly pollIntervalMs: 5000;
|
|
22
|
+
readonly batchSize: 10;
|
|
23
|
+
readonly leaseMs: 30000;
|
|
24
|
+
readonly maxAttempts: 10;
|
|
25
|
+
readonly backoffBaseMs: 1000;
|
|
26
|
+
readonly backoffCapMs: 300000;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export declare function resolveOutboxOptions(options: OutboxModuleOptions | undefined): ResolvedOutboxOptions;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OUTBOX_DEFAULTS = void 0;
|
|
4
|
+
exports.resolveOutboxOptions = resolveOutboxOptions;
|
|
5
|
+
const in_memory_outbox_store_1 = require("./in-memory-outbox-store");
|
|
6
|
+
exports.OUTBOX_DEFAULTS = {
|
|
7
|
+
relay: {
|
|
8
|
+
enabled: true,
|
|
9
|
+
pollIntervalMs: 5000,
|
|
10
|
+
batchSize: 10,
|
|
11
|
+
leaseMs: 30000,
|
|
12
|
+
maxAttempts: 10,
|
|
13
|
+
backoffBaseMs: 1000,
|
|
14
|
+
backoffCapMs: 300000,
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
function resolveOutboxOptions(options) {
|
|
18
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
19
|
+
const relay = (_a = options === null || options === void 0 ? void 0 : options.relay) !== null && _a !== void 0 ? _a : {};
|
|
20
|
+
return {
|
|
21
|
+
store: (_b = options === null || options === void 0 ? void 0 : options.store) !== null && _b !== void 0 ? _b : new in_memory_outbox_store_1.InMemoryOutboxStore(),
|
|
22
|
+
publisher: options === null || options === void 0 ? void 0 : options.publisher,
|
|
23
|
+
relay: {
|
|
24
|
+
enabled: (_c = relay.enabled) !== null && _c !== void 0 ? _c : exports.OUTBOX_DEFAULTS.relay.enabled,
|
|
25
|
+
pollIntervalMs: (_d = relay.pollIntervalMs) !== null && _d !== void 0 ? _d : exports.OUTBOX_DEFAULTS.relay.pollIntervalMs,
|
|
26
|
+
batchSize: (_e = relay.batchSize) !== null && _e !== void 0 ? _e : exports.OUTBOX_DEFAULTS.relay.batchSize,
|
|
27
|
+
leaseMs: (_f = relay.leaseMs) !== null && _f !== void 0 ? _f : exports.OUTBOX_DEFAULTS.relay.leaseMs,
|
|
28
|
+
maxAttempts: (_g = relay.maxAttempts) !== null && _g !== void 0 ? _g : exports.OUTBOX_DEFAULTS.relay.maxAttempts,
|
|
29
|
+
backoffBaseMs: (_h = relay.backoffBaseMs) !== null && _h !== void 0 ? _h : exports.OUTBOX_DEFAULTS.relay.backoffBaseMs,
|
|
30
|
+
backoffCapMs: (_j = relay.backoffCapMs) !== null && _j !== void 0 ? _j : exports.OUTBOX_DEFAULTS.relay.backoffCapMs,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=resolve-outbox-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function computeBackoff(attempts: number, baseMs: number, capMs: number): number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeBackoff = computeBackoff;
|
|
4
|
+
function computeBackoff(attempts, baseMs, capMs) {
|
|
5
|
+
const raw = baseMs * 2 ** (attempts - 1);
|
|
6
|
+
return Math.min(capMs, raw);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=compute-backoff.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compute-backoff.js","sourceRoot":"","sources":["../../lib/util/compute-backoff.ts"],"names":[],"mappings":";;AAAA,wCAGC;AAHD,SAAgB,cAAc,CAAC,QAAgB,EAAE,MAAc,EAAE,KAAa;IAC5E,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;IACzC,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './compute-backoff';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./compute-backoff"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/util/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './outbox.constants';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HttpStatus } from '@nestjs/common';
|
|
2
|
+
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
+
import { OutboxExceptionCode } from './outbox-exception-code.enum';
|
|
4
|
+
|
|
5
|
+
export class OutboxInvalidEventException extends AppException {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(OutboxExceptionCode.INVALID_EVENT_TYPE, undefined, undefined, HttpStatus.BAD_REQUEST);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { HttpStatus } from '@nestjs/common';
|
|
2
|
+
import { AppException } from '@nathapp/nestjs-common';
|
|
3
|
+
import { OutboxExceptionCode } from './outbox-exception-code.enum';
|
|
4
|
+
|
|
5
|
+
export class OutboxMisconfiguredException extends AppException {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(OutboxExceptionCode.PUBLISHER_NOT_CONFIGURED, undefined, undefined, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
8
|
+
}
|
|
9
|
+
}
|
package/lib/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { OutboxRecord, OutboxEventInput } from './outbox-record.interface';
|
|
2
|
+
import { OutboxStatus } from './outbox-status.enum';
|
|
3
|
+
import { IOutboxStore } from './outbox-store.interface';
|
|
4
|
+
import { IOutboxPublisher } from './outbox-publisher.interface';
|
|
5
|
+
import {
|
|
6
|
+
OutboxRelayOptions,
|
|
7
|
+
OutboxModuleOptions,
|
|
8
|
+
} from './outbox-module-options.interface';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
OutboxStatus,
|
|
12
|
+
OutboxRecord,
|
|
13
|
+
OutboxEventInput,
|
|
14
|
+
IOutboxStore,
|
|
15
|
+
IOutboxPublisher,
|
|
16
|
+
OutboxRelayOptions,
|
|
17
|
+
OutboxModuleOptions,
|
|
18
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { IOutboxStore } from './outbox-store.interface';
|
|
2
|
+
import { IOutboxPublisher } from './outbox-publisher.interface';
|
|
3
|
+
|
|
4
|
+
export interface OutboxRelayOptions {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
pollIntervalMs?: number;
|
|
7
|
+
batchSize?: number;
|
|
8
|
+
leaseMs?: number;
|
|
9
|
+
maxAttempts?: number;
|
|
10
|
+
backoffBaseMs?: number;
|
|
11
|
+
backoffCapMs?: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface OutboxModuleOptions {
|
|
15
|
+
store?: IOutboxStore;
|
|
16
|
+
publisher?: IOutboxPublisher;
|
|
17
|
+
relay?: OutboxRelayOptions;
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { OutboxStatus } from './outbox-status.enum';
|
|
2
|
+
|
|
3
|
+
export interface OutboxRecord {
|
|
4
|
+
id: string;
|
|
5
|
+
type: string;
|
|
6
|
+
payload: unknown;
|
|
7
|
+
status: OutboxStatus;
|
|
8
|
+
attempts: number;
|
|
9
|
+
createdAt: Date;
|
|
10
|
+
nextAttemptAt: Date;
|
|
11
|
+
leaseUntil?: Date;
|
|
12
|
+
owner?: string;
|
|
13
|
+
publishedAt?: Date;
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
metadata?: Record<string, unknown>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface OutboxEventInput {
|
|
19
|
+
type: string;
|
|
20
|
+
payload: unknown;
|
|
21
|
+
headers?: Record<string, string>;
|
|
22
|
+
metadata?: Record<string, unknown>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { OutboxRecord } from './outbox-record.interface';
|
|
2
|
+
|
|
3
|
+
export interface IOutboxStore {
|
|
4
|
+
save(record: OutboxRecord, client: unknown): Promise<void>;
|
|
5
|
+
|
|
6
|
+
claimBatch(
|
|
7
|
+
limit: number,
|
|
8
|
+
leaseMs: number,
|
|
9
|
+
now: Date,
|
|
10
|
+
owner: string,
|
|
11
|
+
): Promise<OutboxRecord[]>;
|
|
12
|
+
|
|
13
|
+
markPublished(id: string, publishedAt: Date, owner: string): Promise<void>;
|
|
14
|
+
|
|
15
|
+
markRetry(
|
|
16
|
+
id: string,
|
|
17
|
+
attempts: number,
|
|
18
|
+
nextAttemptAt: Date,
|
|
19
|
+
owner: string,
|
|
20
|
+
): Promise<void>;
|
|
21
|
+
|
|
22
|
+
markDead(id: string, attempts: number, owner: string): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './outbox.module';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { DynamicModule, Module, Provider } from '@nestjs/common';
|
|
2
|
+
import { ConfigurableModuleClass } from '@nathapp/nestjs-common';
|
|
3
|
+
import { OutboxModuleOptions } from '../interface/outbox-module-options.interface';
|
|
4
|
+
import { OutboxRelay } from '../provider/outbox-relay';
|
|
5
|
+
import { OutboxService } from '../provider/outbox.service';
|
|
6
|
+
import { resolveOutboxOptions, ResolvedOutboxOptions } from '../provider/resolve-outbox-options';
|
|
7
|
+
import {
|
|
8
|
+
OUTBOX_MODULE_OPTIONS,
|
|
9
|
+
OUTBOX_PUBLISHER,
|
|
10
|
+
OUTBOX_STORE,
|
|
11
|
+
} from '../constant/outbox.constants';
|
|
12
|
+
|
|
13
|
+
export type OutboxRegisterAsyncOptions = Parameters<
|
|
14
|
+
ReturnType<typeof ConfigurableModuleClass<OutboxModuleOptions>>['registerAsync']
|
|
15
|
+
>[0];
|
|
16
|
+
|
|
17
|
+
const OUTBOX_RESOLVED_OPTIONS = Symbol.for('OUTBOX_RESOLVED_OPTIONS');
|
|
18
|
+
|
|
19
|
+
@Module({})
|
|
20
|
+
export class OutboxModule extends ConfigurableModuleClass<OutboxModuleOptions>(
|
|
21
|
+
OUTBOX_MODULE_OPTIONS,
|
|
22
|
+
) {
|
|
23
|
+
static register(options?: OutboxModuleOptions): DynamicModule {
|
|
24
|
+
return buildDynamicModule(super.register(options));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
static registerAsync(options: OutboxRegisterAsyncOptions): DynamicModule {
|
|
28
|
+
return buildDynamicModule(super.registerAsync(options));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const buildDynamicModule = (base: DynamicModule): DynamicModule => ({
|
|
33
|
+
...base,
|
|
34
|
+
module: OutboxModule,
|
|
35
|
+
global: true,
|
|
36
|
+
providers: [...(base.providers ?? []), ...outboxOptionProviders(), OutboxService, OutboxRelay],
|
|
37
|
+
exports: [
|
|
38
|
+
...(base.exports ?? []),
|
|
39
|
+
OUTBOX_MODULE_OPTIONS,
|
|
40
|
+
OUTBOX_STORE,
|
|
41
|
+
OUTBOX_PUBLISHER,
|
|
42
|
+
OutboxService,
|
|
43
|
+
OutboxRelay,
|
|
44
|
+
],
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const outboxOptionProviders = (): Provider[] => [
|
|
48
|
+
{
|
|
49
|
+
provide: OUTBOX_RESOLVED_OPTIONS,
|
|
50
|
+
useFactory: (raw: OutboxModuleOptions | undefined): ResolvedOutboxOptions =>
|
|
51
|
+
resolveOutboxOptions(raw),
|
|
52
|
+
inject: [OUTBOX_MODULE_OPTIONS],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
provide: OUTBOX_STORE,
|
|
56
|
+
useFactory: (resolved: ResolvedOutboxOptions) => resolved.store,
|
|
57
|
+
inject: [OUTBOX_RESOLVED_OPTIONS],
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
provide: OUTBOX_PUBLISHER,
|
|
61
|
+
useFactory: (resolved: ResolvedOutboxOptions) => resolved.publisher,
|
|
62
|
+
inject: [OUTBOX_RESOLVED_OPTIONS],
|
|
63
|
+
},
|
|
64
|
+
];
|