@lakutata/nats 1.5.2 → 1.6.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.
- package/CHANGELOG.md +20 -0
- package/dist/CommonExports.d.ts +3 -0
- package/dist/CommonExports.d.ts.map +1 -1
- package/dist/CommonExports.js +12 -3
- package/dist/components/NATS.d.ts +8 -0
- package/dist/components/NATS.d.ts.map +1 -1
- package/dist/components/NATS.js +20 -0
- package/dist/providers/Task.d.ts +48 -0
- package/dist/providers/Task.d.ts.map +1 -0
- package/dist/providers/Task.js +175 -0
- package/dist/tests/NatsTest.spec.js +11 -1
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,26 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [1.6.1](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.6.0...@lakutata/nats@1.6.1) (2026-01-17)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @lakutata/nats
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [1.6.0](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.5.2...@lakutata/nats@1.6.0) (2026-01-16)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* **nats:** add task provider and update NATS component ([3ec7114](https://github.com/lakutata/lakutata-packages/commit/3ec7114af0aed0e548728339b44d21bc42e0e210))
|
|
20
|
+
* **nats:** add Task provider to CommonExports ([ad07ee9](https://github.com/lakutata/lakutata-packages/commit/ad07ee9cff6306bbc77ccc9ce1fa3c025df04ee5))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
6
26
|
## [1.5.2](https://github.com/lakutata/lakutata-packages/compare/@lakutata/nats@1.5.1...@lakutata/nats@1.5.2) (2026-01-11)
|
|
7
27
|
|
|
8
28
|
**Note:** Version bump only for package @lakutata/nats
|
package/dist/CommonExports.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
export { SetupNatsServiceEntrypoint } from './entrypoints/SetupNatsServiceEntrypoint';
|
|
2
2
|
export { ServiceProxy, BuildServiceProxy, type BuildServiceProxyOptions } from './providers/ServiceProxy';
|
|
3
|
+
export { TaskOptions } from './providers/Task';
|
|
4
|
+
export { default as Task } from './providers/Task';
|
|
5
|
+
export { RetentionPolicy } from 'nats';
|
|
3
6
|
export { NATS, buildNatsClientOptions } from './components/NATS';
|
|
4
7
|
export { JSONCodec } from './codecs/JSONCodec';
|
|
5
8
|
export { MessagePackCodec } from './codecs/MessagePackCodec';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CommonExports.d.ts","sourceRoot":"","sources":["../src/CommonExports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,0BAA0B,EAAC,MAAM,0CAA0C,CAAA;AACnF,OAAO,EAAC,YAAY,EAAE,iBAAiB,EAAE,KAAK,wBAAwB,EAAC,MAAM,0BAA0B,CAAA;AACvG,OAAO,EAAC,IAAI,EAAE,sBAAsB,EAAC,MAAM,mBAAmB,CAAA;AAC9D,OAAO,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAA;AAC1D,OAAO,EAAC,WAAW,EAAC,MAAM,MAAM,CAAA;AAChC,YAAY,EAAC,gBAAgB,EAAC,MAAM,0BAA0B,CAAA;AAC9D,YAAY,EAAC,YAAY,EAAE,KAAK,EAAC,MAAM,MAAM,CAAA;AAC7C,YAAY,EAAC,iBAAiB,EAAC,MAAM,gCAAgC,CAAA;AACrE,OAAO,EAAC,uBAAuB,EAAC,MAAM,sCAAsC,CAAA;AAC5E,OAAO,EAAC,sBAAsB,EAAC,MAAM,qCAAqC,CA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AAC,MAAM,oCAAoC,CAAA;AACxE,OAAO,EAAC,2BAA2B,EAAC,MAAM,0CAA0C,CAAA"}
|
package/dist/CommonExports.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.NatsRequestTimeoutException = exports.NatsNotFoundException = exports.NatsNoRespondersAvailableException = exports.NatsInternalServerException = exports.NatsForbiddenException = exports.NatsBadRequestException = exports.StringCodec = exports.MessagePackCodec = exports.JSONCodec = exports.buildNatsClientOptions = exports.NATS = exports.BuildServiceProxy = exports.ServiceProxy = exports.SetupNatsServiceEntrypoint = void 0;
|
|
6
|
+
exports.NatsRequestTimeoutException = exports.NatsNotFoundException = exports.NatsNoRespondersAvailableException = exports.NatsInternalServerException = exports.NatsForbiddenException = exports.NatsBadRequestException = exports.StringCodec = exports.MessagePackCodec = exports.JSONCodec = exports.buildNatsClientOptions = exports.NATS = exports.RetentionPolicy = exports.Task = exports.TaskOptions = exports.BuildServiceProxy = exports.ServiceProxy = exports.SetupNatsServiceEntrypoint = void 0;
|
|
4
7
|
var SetupNatsServiceEntrypoint_1 = require("./entrypoints/SetupNatsServiceEntrypoint");
|
|
5
8
|
Object.defineProperty(exports, "SetupNatsServiceEntrypoint", { enumerable: true, get: function () { return SetupNatsServiceEntrypoint_1.SetupNatsServiceEntrypoint; } });
|
|
6
9
|
var ServiceProxy_1 = require("./providers/ServiceProxy");
|
|
7
10
|
Object.defineProperty(exports, "ServiceProxy", { enumerable: true, get: function () { return ServiceProxy_1.ServiceProxy; } });
|
|
8
11
|
Object.defineProperty(exports, "BuildServiceProxy", { enumerable: true, get: function () { return ServiceProxy_1.BuildServiceProxy; } });
|
|
12
|
+
var Task_1 = require("./providers/Task");
|
|
13
|
+
Object.defineProperty(exports, "TaskOptions", { enumerable: true, get: function () { return Task_1.TaskOptions; } });
|
|
14
|
+
var Task_2 = require("./providers/Task");
|
|
15
|
+
Object.defineProperty(exports, "Task", { enumerable: true, get: function () { return __importDefault(Task_2).default; } });
|
|
16
|
+
var nats_1 = require("nats");
|
|
17
|
+
Object.defineProperty(exports, "RetentionPolicy", { enumerable: true, get: function () { return nats_1.RetentionPolicy; } });
|
|
9
18
|
var NATS_1 = require("./components/NATS");
|
|
10
19
|
Object.defineProperty(exports, "NATS", { enumerable: true, get: function () { return NATS_1.NATS; } });
|
|
11
20
|
Object.defineProperty(exports, "buildNatsClientOptions", { enumerable: true, get: function () { return NATS_1.buildNatsClientOptions; } });
|
|
@@ -13,8 +22,8 @@ var JSONCodec_1 = require("./codecs/JSONCodec");
|
|
|
13
22
|
Object.defineProperty(exports, "JSONCodec", { enumerable: true, get: function () { return JSONCodec_1.JSONCodec; } });
|
|
14
23
|
var MessagePackCodec_1 = require("./codecs/MessagePackCodec");
|
|
15
24
|
Object.defineProperty(exports, "MessagePackCodec", { enumerable: true, get: function () { return MessagePackCodec_1.MessagePackCodec; } });
|
|
16
|
-
var
|
|
17
|
-
Object.defineProperty(exports, "StringCodec", { enumerable: true, get: function () { return
|
|
25
|
+
var nats_2 = require("nats");
|
|
26
|
+
Object.defineProperty(exports, "StringCodec", { enumerable: true, get: function () { return nats_2.StringCodec; } });
|
|
18
27
|
var NatsBadRequestException_1 = require("./exceptions/NatsBadRequestException");
|
|
19
28
|
Object.defineProperty(exports, "NatsBadRequestException", { enumerable: true, get: function () { return NatsBadRequestException_1.NatsBadRequestException; } });
|
|
20
29
|
var NatsForbiddenException_1 = require("./exceptions/NatsForbiddenException");
|
|
@@ -2,6 +2,7 @@ import { Application, Component, ComponentOptionsBuilder } from 'lakutata';
|
|
|
2
2
|
import { type Codec, Subscription } from 'nats';
|
|
3
3
|
import { SubscribeOptions } from '../types/SubscribeOptions';
|
|
4
4
|
import { NatsClientOptions } from '../interfaces/NatsClientOptions';
|
|
5
|
+
import Task, { TaskOptions } from '../providers/Task';
|
|
5
6
|
export declare const buildNatsClientOptions: ComponentOptionsBuilder<NatsClientOptions>;
|
|
6
7
|
export declare class NATS extends Component {
|
|
7
8
|
#private;
|
|
@@ -109,6 +110,13 @@ export declare class NATS extends Component {
|
|
|
109
110
|
* @protected
|
|
110
111
|
*/
|
|
111
112
|
protected destroy(): Promise<void>;
|
|
113
|
+
/**
|
|
114
|
+
* Create task provider
|
|
115
|
+
* @param subject
|
|
116
|
+
* @param handler
|
|
117
|
+
* @param options
|
|
118
|
+
*/
|
|
119
|
+
createTask(subject: string, handler?: (data: any) => void | Promise<void>, options?: TaskOptions): Promise<Task>;
|
|
112
120
|
/**
|
|
113
121
|
* Publishes the specified data to the specified subject.
|
|
114
122
|
* @param subject
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NATS.d.ts","sourceRoot":"","sources":["../../src/components/NATS.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,SAAS,EAET,uBAAuB,EAE1B,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAC,KAAK,KAAK,EAAgC,YAAY,EAAC,MAAM,MAAM,CAAA;AAC3E,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAA;AAO1D,OAAO,EAAC,iBAAiB,EAAC,MAAM,iCAAiC,CAAA;
|
|
1
|
+
{"version":3,"file":"NATS.d.ts","sourceRoot":"","sources":["../../src/components/NATS.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,SAAS,EAET,uBAAuB,EAE1B,MAAM,UAAU,CAAA;AAEjB,OAAO,EAAC,KAAK,KAAK,EAAgC,YAAY,EAAC,MAAM,MAAM,CAAA;AAC3E,OAAO,EAAC,gBAAgB,EAAC,MAAM,2BAA2B,CAAA;AAO1D,OAAO,EAAC,iBAAiB,EAAC,MAAM,iCAAiC,CAAA;AAGjE,OAAO,IAAI,EAAE,EAAC,WAAW,EAAC,MAAM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IAAI,EAAE,wBAAwB,GAAE,OAAe,GAAG,IAAI;IAYhJ;;;;;OAKG;IACI,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAavG;;;;;OAKG;IACI,eAAe,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;CAW1G"}
|
package/dist/components/NATS.js
CHANGED
|
@@ -8,6 +8,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.NATS = exports.buildNatsClientOptions = void 0;
|
|
13
16
|
const lakutata_1 = require("lakutata");
|
|
@@ -21,6 +24,7 @@ const NatsNoRespondersAvailableException_1 = require("../exceptions/NatsNoRespon
|
|
|
21
24
|
const NatsInternalServerException_1 = require("../exceptions/NatsInternalServerException");
|
|
22
25
|
const ServiceEventCodec_1 = require("../lib/ServiceEventCodec");
|
|
23
26
|
const MessagePackCodec_1 = require("../codecs/MessagePackCodec");
|
|
27
|
+
const Task_1 = __importDefault(require("../providers/Task"));
|
|
24
28
|
const buildNatsClientOptions = (options) => {
|
|
25
29
|
return {
|
|
26
30
|
class: NATS,
|
|
@@ -83,6 +87,22 @@ class NATS extends lakutata_1.Component {
|
|
|
83
87
|
async destroy() {
|
|
84
88
|
await this.#conn.close();
|
|
85
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Create task provider
|
|
92
|
+
* @param subject
|
|
93
|
+
* @param handler
|
|
94
|
+
* @param options
|
|
95
|
+
*/
|
|
96
|
+
async createTask(subject, handler, options) {
|
|
97
|
+
return await this.buildObject(Task_1.default, {
|
|
98
|
+
jetStreamManager: await this.#conn.jetstreamManager(),
|
|
99
|
+
jetStream: this.#conn.jetstream(),
|
|
100
|
+
codec: this.codec,
|
|
101
|
+
options: options || {},
|
|
102
|
+
subject: subject,
|
|
103
|
+
handler: handler
|
|
104
|
+
});
|
|
105
|
+
}
|
|
86
106
|
/**
|
|
87
107
|
* Publishes the specified data to the specified subject.
|
|
88
108
|
* @param subject
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DTO, Provider } from 'lakutata';
|
|
2
|
+
import { type Codec, RetentionPolicy, type JetStreamClient, type JetStreamManager } from 'nats';
|
|
3
|
+
export declare class TaskOptions extends DTO {
|
|
4
|
+
retention?: RetentionPolicy;
|
|
5
|
+
maxMessages?: number;
|
|
6
|
+
maxAgeMs?: number;
|
|
7
|
+
maxBytes?: number;
|
|
8
|
+
maxMessageSize?: number;
|
|
9
|
+
duplicateWindowMs?: number;
|
|
10
|
+
concurrentTask?: number;
|
|
11
|
+
}
|
|
12
|
+
declare class Task extends Provider {
|
|
13
|
+
#private;
|
|
14
|
+
protected readonly jetStreamManager: JetStreamManager;
|
|
15
|
+
protected readonly jetStream: JetStreamClient;
|
|
16
|
+
protected readonly subject: string;
|
|
17
|
+
protected readonly options: TaskOptions;
|
|
18
|
+
protected readonly handler?: (data: any) => void | Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* NATS message codec
|
|
21
|
+
* @protected
|
|
22
|
+
*/
|
|
23
|
+
protected readonly codec: Codec<unknown>;
|
|
24
|
+
protected get streamName(): string;
|
|
25
|
+
/**
|
|
26
|
+
* Initializer
|
|
27
|
+
* @protected
|
|
28
|
+
*/
|
|
29
|
+
protected init(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Destroyer
|
|
32
|
+
* @protected
|
|
33
|
+
*/
|
|
34
|
+
protected destroy(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Get exists stream name
|
|
37
|
+
* @protected
|
|
38
|
+
*/
|
|
39
|
+
protected getExistsStreamName(): Promise<string | null>;
|
|
40
|
+
/**
|
|
41
|
+
* Publish task
|
|
42
|
+
* @param payload
|
|
43
|
+
* @param id
|
|
44
|
+
*/
|
|
45
|
+
publish(payload: any, id?: string): Promise<void>;
|
|
46
|
+
}
|
|
47
|
+
export default Task;
|
|
48
|
+
//# sourceMappingURL=Task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/providers/Task.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAE,QAAQ,EAAC,MAAM,UAAU,CAAA;AAEtC,OAAO,EAEH,KAAK,KAAK,EAEV,eAAe,EAEf,KAAK,eAAe,EACpB,KAAK,gBAAgB,EAMxB,MAAM,MAAM,CAAA;AAIb,qBAAa,WAAY,SAAQ,GAAG;IAEzB,SAAS,CAAC,EAAE,eAAe,CAAA;IAG3B,WAAW,CAAC,EAAE,MAAM,CAAA;IAGpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IAGjB,QAAQ,CAAC,EAAE,MAAM,CAAA;IAGjB,cAAc,CAAC,EAAE,MAAM,CAAA;IAGvB,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAG1B,cAAc,CAAC,EAAE,MAAM,CAAA;CACjC;AAED,cAAM,IAAK,SAAQ,QAAQ;;IAKvB,SAAS,CAAC,QAAQ,CAAC,gBAAgB,EAAE,gBAAgB,CAAA;IAGrD,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,eAAe,CAAA;IAG7C,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IAGlC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAA;IAGvC,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAEhE;;;OAGG;IAKH,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,OAAO,CAAC,CAAA;IAExC,SAAS,KAAK,UAAU,IAAI,MAAM,CAEjC;IAED;;;OAGG;cACa,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IA6DrC;;;OAGG;cACa,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxC;;;OAGG;cACa,mBAAmB,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAQ7D;;;;OAIG;IACU,OAAO,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGjE;AAED,eAAe,IAAI,CAAA"}
|
|
@@ -0,0 +1,175 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TaskOptions = void 0;
|
|
13
|
+
const lakutata_1 = require("lakutata");
|
|
14
|
+
const di_1 = require("lakutata/decorator/di");
|
|
15
|
+
const nats_1 = require("nats");
|
|
16
|
+
const helper_1 = require("lakutata/helper");
|
|
17
|
+
const dto_1 = require("lakutata/decorator/dto");
|
|
18
|
+
class TaskOptions extends lakutata_1.DTO {
|
|
19
|
+
}
|
|
20
|
+
exports.TaskOptions = TaskOptions;
|
|
21
|
+
__decorate([
|
|
22
|
+
(0, dto_1.Expect)(lakutata_1.DTO.String().allow('limits', 'interest', 'workqueue').only().optional()),
|
|
23
|
+
__metadata("design:type", String)
|
|
24
|
+
], TaskOptions.prototype, "retention", void 0);
|
|
25
|
+
__decorate([
|
|
26
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().integer().allow(0).min(-1).optional()),
|
|
27
|
+
__metadata("design:type", Number)
|
|
28
|
+
], TaskOptions.prototype, "maxMessages", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().integer().allow(0).min(0).optional()),
|
|
31
|
+
__metadata("design:type", Number)
|
|
32
|
+
], TaskOptions.prototype, "maxAgeMs", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().integer().allow(0).min(-1).optional()),
|
|
35
|
+
__metadata("design:type", Number)
|
|
36
|
+
], TaskOptions.prototype, "maxBytes", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().integer().allow(0).min(-1).optional()),
|
|
39
|
+
__metadata("design:type", Number)
|
|
40
|
+
], TaskOptions.prototype, "maxMessageSize", void 0);
|
|
41
|
+
__decorate([
|
|
42
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().allow(0).min(0).optional()),
|
|
43
|
+
__metadata("design:type", Number)
|
|
44
|
+
], TaskOptions.prototype, "duplicateWindowMs", void 0);
|
|
45
|
+
__decorate([
|
|
46
|
+
(0, dto_1.Expect)(lakutata_1.DTO.Number().integer().min(1).optional()),
|
|
47
|
+
__metadata("design:type", Number)
|
|
48
|
+
], TaskOptions.prototype, "concurrentTask", void 0);
|
|
49
|
+
class Task extends lakutata_1.Provider {
|
|
50
|
+
#requestTask = true;
|
|
51
|
+
get streamName() {
|
|
52
|
+
return (0, helper_1.MD5)(this.subject).toString('hex');
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Initializer
|
|
56
|
+
* @protected
|
|
57
|
+
*/
|
|
58
|
+
async init() {
|
|
59
|
+
let streamName = await this.getExistsStreamName();
|
|
60
|
+
const streamConfig = {
|
|
61
|
+
subjects: [this.subject],
|
|
62
|
+
retention: this.options.retention || nats_1.RetentionPolicy.Workqueue,
|
|
63
|
+
max_msgs: this.options.maxMessages !== undefined ? this.options.maxMessages : -1,
|
|
64
|
+
max_age: (0, nats_1.nanos)(this.options.maxAgeMs || 0),
|
|
65
|
+
max_bytes: this.options.maxBytes !== undefined ? this.options.maxBytes : -1,
|
|
66
|
+
max_msg_size: this.options.maxMessageSize !== undefined ? this.options.maxMessageSize : -1,
|
|
67
|
+
no_ack: false,
|
|
68
|
+
duplicate_window: (0, nats_1.nanos)(this.options.duplicateWindowMs ? this.options.duplicateWindowMs : 0)
|
|
69
|
+
};
|
|
70
|
+
if (!streamName) {
|
|
71
|
+
//Add stream
|
|
72
|
+
const streamInfo = await this.jetStreamManager.streams.add({
|
|
73
|
+
name: this.streamName,
|
|
74
|
+
...streamConfig
|
|
75
|
+
});
|
|
76
|
+
streamName = streamInfo.config.name;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
//Update stream
|
|
80
|
+
let streamInfo = await this.jetStreamManager.streams.info(streamName);
|
|
81
|
+
const updateConfig = {
|
|
82
|
+
...streamInfo.config,
|
|
83
|
+
...streamConfig
|
|
84
|
+
};
|
|
85
|
+
streamInfo = await this.jetStreamManager.streams.update(streamName, updateConfig);
|
|
86
|
+
streamName = streamInfo.config.name;
|
|
87
|
+
}
|
|
88
|
+
if (this.handler) {
|
|
89
|
+
const consumerInfo = await this.jetStreamManager.consumers.add(streamName, {
|
|
90
|
+
durable_name: this.streamName,
|
|
91
|
+
deliver_policy: nats_1.DeliverPolicy.All,
|
|
92
|
+
ack_policy: nats_1.AckPolicy.Explicit,
|
|
93
|
+
deliver_group: this.streamName
|
|
94
|
+
});
|
|
95
|
+
const consumer = await this.jetStream.consumers.get(consumerInfo.stream_name, consumerInfo.name);
|
|
96
|
+
const concurrentTask = this.options.concurrentTask || 1;
|
|
97
|
+
setImmediate(async () => {
|
|
98
|
+
while (this.#requestTask) {
|
|
99
|
+
const msgs = await consumer.consume({ max_messages: concurrentTask });
|
|
100
|
+
const batchTasks = [];
|
|
101
|
+
for await (const msg of msgs) {
|
|
102
|
+
batchTasks.push(new Promise(async (resolve, reject) => {
|
|
103
|
+
try {
|
|
104
|
+
await this.handler(this.codec.decode(msg.data));
|
|
105
|
+
msg.ack();
|
|
106
|
+
return resolve();
|
|
107
|
+
}
|
|
108
|
+
catch (e) {
|
|
109
|
+
msg.nak();
|
|
110
|
+
return reject(e);
|
|
111
|
+
}
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
await Promise.all(batchTasks);
|
|
115
|
+
}
|
|
116
|
+
await consumer.delete();
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Destroyer
|
|
122
|
+
* @protected
|
|
123
|
+
*/
|
|
124
|
+
async destroy() {
|
|
125
|
+
this.#requestTask = false;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Get exists stream name
|
|
129
|
+
* @protected
|
|
130
|
+
*/
|
|
131
|
+
async getExistsStreamName() {
|
|
132
|
+
try {
|
|
133
|
+
return await this.jetStreamManager.streams.find(this.subject);
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Publish task
|
|
141
|
+
* @param payload
|
|
142
|
+
* @param id
|
|
143
|
+
*/
|
|
144
|
+
async publish(payload, id) {
|
|
145
|
+
await this.jetStream.publish(this.subject, this.codec.encode(payload), { msgID: id });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
__decorate([
|
|
149
|
+
(0, di_1.Configurable)(),
|
|
150
|
+
__metadata("design:type", Object)
|
|
151
|
+
], Task.prototype, "jetStreamManager", void 0);
|
|
152
|
+
__decorate([
|
|
153
|
+
(0, di_1.Configurable)(),
|
|
154
|
+
__metadata("design:type", Object)
|
|
155
|
+
], Task.prototype, "jetStream", void 0);
|
|
156
|
+
__decorate([
|
|
157
|
+
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
|
|
158
|
+
__metadata("design:type", String)
|
|
159
|
+
], Task.prototype, "subject", void 0);
|
|
160
|
+
__decorate([
|
|
161
|
+
(0, di_1.Configurable)(TaskOptions.Schema().default({})),
|
|
162
|
+
__metadata("design:type", TaskOptions)
|
|
163
|
+
], Task.prototype, "options", void 0);
|
|
164
|
+
__decorate([
|
|
165
|
+
(0, di_1.Configurable)(lakutata_1.DTO.Function().optional()),
|
|
166
|
+
__metadata("design:type", Function)
|
|
167
|
+
], Task.prototype, "handler", void 0);
|
|
168
|
+
__decorate([
|
|
169
|
+
(0, di_1.Configurable)(lakutata_1.DTO.Object({
|
|
170
|
+
encode: lakutata_1.DTO.Function().arity(1).required(),
|
|
171
|
+
decode: lakutata_1.DTO.Function().arity(1).required()
|
|
172
|
+
}).required()),
|
|
173
|
+
__metadata("design:type", Object)
|
|
174
|
+
], Task.prototype, "codec", void 0);
|
|
175
|
+
exports.default = Task;
|
|
@@ -62,6 +62,15 @@ class TestComponent extends lakutata_1.Component {
|
|
|
62
62
|
// console.error(JSON.parse(JSON.stringify(e)))
|
|
63
63
|
console.error(e);
|
|
64
64
|
}
|
|
65
|
+
try {
|
|
66
|
+
const task = await this.nats.createTask('tasks.test1', async (data) => {
|
|
67
|
+
console.log('task2:', data);
|
|
68
|
+
});
|
|
69
|
+
await task.publish({ time: Date.now() });
|
|
70
|
+
}
|
|
71
|
+
catch (e) {
|
|
72
|
+
console.error(e);
|
|
73
|
+
}
|
|
65
74
|
}
|
|
66
75
|
}
|
|
67
76
|
__decorate([
|
|
@@ -112,7 +121,8 @@ lakutata_1.Application.run({
|
|
|
112
121
|
service: (0, CommonExports_1.SetupNatsServiceEntrypoint)('nats')
|
|
113
122
|
}),
|
|
114
123
|
nats: (0, CommonExports_1.buildNatsClientOptions)({
|
|
115
|
-
servers: '127.0.0.1:4222'
|
|
124
|
+
// servers: '127.0.0.1:4222'
|
|
125
|
+
servers: '10.11.11.21:30422'
|
|
116
126
|
}),
|
|
117
127
|
test: {
|
|
118
128
|
class: TestComponent
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lakutata/nats",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.1",
|
|
4
4
|
"description": "Lakutata NATS Client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lakutata",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"url": "https://github.com/lakutata/lakutata-packages/issues"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"lakutata": "^2.0.
|
|
32
|
+
"lakutata": "^2.0.119",
|
|
33
33
|
"nats": "^2.29.3"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "86e17bba9fee65388de9e6ae8343fbded81404a0"
|
|
36
36
|
}
|