@lad-tech/nsc-toolkit 1.28.0 → 1.28.2
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/.vscode/launch.json +77 -0
- package/.vscode/tasks.json +25 -0
- package/package.json +1 -1
- package/CHANGELOG.md +0 -6
- package/dist/Client.js +0 -292
- package/dist/Container.js +0 -124
- package/dist/Meter.js +0 -58
- package/dist/Method.js +0 -7
- package/dist/Root.js +0 -120
- package/dist/Service.js +0 -677
- package/dist/StreamBatchMsgFetcher.js +0 -17
- package/dist/StreamManager.js +0 -173
- package/dist/StreamOptions/BufferToJsonTransform.js +0 -34
- package/dist/StreamOptions/JsonToBufferTransform.js +0 -11
- package/dist/StreamOptions/index.js +0 -19
- package/dist/StreamSingleMsgFetcher.js +0 -28
- package/dist/Union/Broker.js +0 -95
- package/dist/Union/ConsumerApi.js +0 -28
- package/dist/Union/JetStreamClient.js +0 -40
- package/dist/Union/JetStreamManager.js +0 -26
- package/dist/Union/StreamApi.js +0 -46
- package/dist/Union/Subscription.js +0 -47
- package/dist/Union/index.js +0 -18
- package/dist/index.js +0 -24
- package/dist/injector.js +0 -65
- package/dist/interfaces.js +0 -16
- package/dist/types/Client.d.ts +0 -32
- package/dist/types/Container.d.ts +0 -54
- package/dist/types/Meter.d.ts +0 -11
- package/dist/types/Method.d.ts +0 -6
- package/dist/types/Root.d.ts +0 -29
- package/dist/types/Service.d.ts +0 -140
- package/dist/types/StreamBatchMsgFetcher.d.ts +0 -13
- package/dist/types/StreamManager.d.ts +0 -26
- package/dist/types/StreamOptions/BufferToJsonTransform.d.ts +0 -14
- package/dist/types/StreamOptions/JsonToBufferTransform.d.ts +0 -6
- package/dist/types/StreamOptions/index.d.ts +0 -2
- package/dist/types/StreamSingleMsgFetcher.d.ts +0 -16
- package/dist/types/Union/Broker.d.ts +0 -32
- package/dist/types/Union/ConsumerApi.d.ts +0 -16
- package/dist/types/Union/JetStreamClient.d.ts +0 -18
- package/dist/types/Union/JetStreamManager.d.ts +0 -12
- package/dist/types/Union/StreamApi.d.ts +0 -16
- package/dist/types/Union/Subscription.d.ts +0 -19
- package/dist/types/Union/index.d.ts +0 -1
- package/dist/types/index.d.ts +0 -7
- package/dist/types/injector.d.ts +0 -14
- package/dist/types/interfaces.d.ts +0 -181
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "Launch NSC_Example_Gate_Micro",
|
|
11
|
+
"skipFiles": ["<node_internals>/**"],
|
|
12
|
+
"program": "${workspaceFolder}/examples/HttpGate/dist/examples/HttpGate/start.micro.js",
|
|
13
|
+
"outFiles": ["${workspaceFolder}/examples/HttpGate/dist/**/*.js"],
|
|
14
|
+
"preLaunchTask": "buildGateMicro",
|
|
15
|
+
"outputCapture": "std",
|
|
16
|
+
"env": {
|
|
17
|
+
"NODE_PATH": "${workspaceFolder}/examples/HttpGate/dist",
|
|
18
|
+
"LOG_LEVEL": "info",
|
|
19
|
+
"NATS_HOST": "nats://localhost:4222",
|
|
20
|
+
"DEFAULT_RESPONSE_TIMEOUT": "50000",
|
|
21
|
+
"ENVIRONMENT": "local",
|
|
22
|
+
"OTEL_AGENT": "http://localhost:4318/v1/traces"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"type": "node",
|
|
27
|
+
"request": "launch",
|
|
28
|
+
"name": "Launch NSC_Example_Gate_Mono",
|
|
29
|
+
"skipFiles": ["<node_internals>/**"],
|
|
30
|
+
"program": "${workspaceFolder}/examples/HttpGate/dist/examples/HttpGate/start.mono.js",
|
|
31
|
+
"outFiles": ["${workspaceFolder}/examples/HttpGate/dist/**/*.js"],
|
|
32
|
+
"preLaunchTask": "buildGateMono",
|
|
33
|
+
"outputCapture": "std",
|
|
34
|
+
"env": {
|
|
35
|
+
"NODE_PATH": "${workspaceFolder}/examples/HttpGate/dist",
|
|
36
|
+
"LOG_LEVEL": "info",
|
|
37
|
+
"NATS_HOST": "nats://localhost:4222",
|
|
38
|
+
"DEFAULT_RESPONSE_TIMEOUT": "50000",
|
|
39
|
+
"ENVIRONMENT": "local"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
"type": "node",
|
|
44
|
+
"request": "launch",
|
|
45
|
+
"name": "Launch NSC_Example_Logic",
|
|
46
|
+
"skipFiles": ["<node_internals>/**"],
|
|
47
|
+
"program": "${workspaceFolder}/examples/LogicService",
|
|
48
|
+
"outFiles": ["${workspaceFolder}/examples/LogicService/dist/**/*.js"],
|
|
49
|
+
"preLaunchTask": "buildLogic",
|
|
50
|
+
"outputCapture": "std",
|
|
51
|
+
"env": {
|
|
52
|
+
"NODE_PATH": "${workspaceFolder}/examples/LogicService/dist",
|
|
53
|
+
"LOG_LEVEL": "info",
|
|
54
|
+
"NATS_HOST": "nats://localhost:4222",
|
|
55
|
+
"ENVIRONMENT": "local",
|
|
56
|
+
"OTEL_AGENT": "http://localhost:4318/v1/traces"
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"type": "node",
|
|
61
|
+
"request": "launch",
|
|
62
|
+
"name": "Launch NSC_Example_Math",
|
|
63
|
+
"skipFiles": ["<node_internals>/**"],
|
|
64
|
+
"program": "${workspaceFolder}/examples/MathService",
|
|
65
|
+
"outFiles": ["${workspaceFolder}/examples/MathService/dist/**/*.js"],
|
|
66
|
+
"preLaunchTask": "buildMath",
|
|
67
|
+
"outputCapture": "std",
|
|
68
|
+
"env": {
|
|
69
|
+
"NODE_PATH": "${workspaceFolder}/examples/MathService/dist",
|
|
70
|
+
"LOG_LEVEL": "info",
|
|
71
|
+
"NATS_HOST": "nats://localhost:4222",
|
|
72
|
+
"ENVIRONMENT": "local",
|
|
73
|
+
"OTEL_AGENT": "http://localhost:4318/v1/traces"
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "2.0.0",
|
|
3
|
+
"tasks": [
|
|
4
|
+
{
|
|
5
|
+
"label": "buildGateMicro",
|
|
6
|
+
"type": "shell",
|
|
7
|
+
"command": "cd ${workspaceFolder}/examples/HttpGate/ && npm run buildMicro",
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"label": "buildGateMono",
|
|
11
|
+
"type": "shell",
|
|
12
|
+
"command": "cd ${workspaceFolder}/examples/HttpGate/ && npm run buildMono",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"label": "buildLogic",
|
|
16
|
+
"type": "shell",
|
|
17
|
+
"command": "cd ${workspaceFolder}/examples/LogicService/ && npm run build",
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"label": "buildMath",
|
|
21
|
+
"type": "shell",
|
|
22
|
+
"command": "cd ${workspaceFolder}/examples/MathService/ && npm run build",
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
package/package.json
CHANGED
package/CHANGELOG.md
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
# [1.28.0](https://github.com/lad-tech/nsc-toolkit/compare/v1.27.0...v1.28.0) (2025-05-12)
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
### Features
|
|
5
|
-
|
|
6
|
-
* Async event traces and tags ([#145](https://github.com/lad-tech/nsc-toolkit/issues/145)) ([02c3560](https://github.com/lad-tech/nsc-toolkit/commit/02c356051d295d528c4cc7758b2a310d37074424))
|
package/dist/Client.js
DELETED
|
@@ -1,292 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Client = void 0;
|
|
4
|
-
const opentelemetry = require("@opentelemetry/api");
|
|
5
|
-
const ajv_1 = require("ajv");
|
|
6
|
-
const nats_1 = require("nats");
|
|
7
|
-
const node_crypto_1 = require("node:crypto");
|
|
8
|
-
const http = require("node:http");
|
|
9
|
-
const node_stream_1 = require("node:stream");
|
|
10
|
-
const promises_1 = require("node:timers/promises");
|
|
11
|
-
const Root_1 = require("./Root");
|
|
12
|
-
const StreamManager_1 = require("./StreamManager");
|
|
13
|
-
const Meter_1 = require("./Meter");
|
|
14
|
-
class Client extends Root_1.Root {
|
|
15
|
-
constructor({ broker, events, loggerOutputFormatter, serviceName, baggage, cache, Ref }) {
|
|
16
|
-
super(broker, loggerOutputFormatter);
|
|
17
|
-
this.subscriptions = new Map();
|
|
18
|
-
this.REQUEST_HTTP_SETTINGS_TIMEOUT = 1000; // ms
|
|
19
|
-
this.logger.setLocation(serviceName);
|
|
20
|
-
this.serviceName = serviceName;
|
|
21
|
-
this.baggage = baggage;
|
|
22
|
-
this.cache = cache;
|
|
23
|
-
this.events = events;
|
|
24
|
-
this.Ref = Ref;
|
|
25
|
-
}
|
|
26
|
-
async startWatch(subscription, listener, eventName) {
|
|
27
|
-
for await (const event of subscription) {
|
|
28
|
-
if (!event) {
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
let data;
|
|
32
|
-
try {
|
|
33
|
-
data = (0, nats_1.JSONCodec)().decode(event.data);
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
data = (0, nats_1.StringCodec)().decode(event.data);
|
|
37
|
-
}
|
|
38
|
-
const message = { data };
|
|
39
|
-
let baggage;
|
|
40
|
-
if (event.headers) {
|
|
41
|
-
baggage = this.getBaggageFromNATSHeader(event.headers);
|
|
42
|
-
}
|
|
43
|
-
message.meter = new Meter_1.Meter(eventName, baggage);
|
|
44
|
-
if (this.isJsMessage(event)) {
|
|
45
|
-
message.ack = event.ack.bind(event);
|
|
46
|
-
message.nak = event.nak.bind(event);
|
|
47
|
-
}
|
|
48
|
-
listener.emit(eventName, message);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
async startBatchWatch(fetcher, listener, eventName) {
|
|
52
|
-
while (true) {
|
|
53
|
-
const batch = [];
|
|
54
|
-
const events = await fetcher.fetch();
|
|
55
|
-
for await (const event of events) {
|
|
56
|
-
let data;
|
|
57
|
-
try {
|
|
58
|
-
data = (0, nats_1.JSONCodec)().decode(event.data);
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
data = (0, nats_1.StringCodec)().decode(event.data);
|
|
62
|
-
}
|
|
63
|
-
const message = { data };
|
|
64
|
-
message.ack = event.ack.bind(event);
|
|
65
|
-
message.nak = event.nak.bind(event);
|
|
66
|
-
batch.push(message);
|
|
67
|
-
}
|
|
68
|
-
if (batch.length > 0)
|
|
69
|
-
listener.emit(eventName, batch);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
getListener(serviceNameFrom, options) {
|
|
73
|
-
if (!this.events) {
|
|
74
|
-
throw new Error('The service does not generate events');
|
|
75
|
-
}
|
|
76
|
-
const listener = new node_stream_1.EventEmitter();
|
|
77
|
-
return new Proxy(listener, {
|
|
78
|
-
get: (target, prop, receiver) => {
|
|
79
|
-
const method = Reflect.get(target, prop, receiver);
|
|
80
|
-
if (prop === 'on') {
|
|
81
|
-
return async (eventName, handler) => {
|
|
82
|
-
var _a, _b;
|
|
83
|
-
try {
|
|
84
|
-
this.logger.info('Subscribe', eventName);
|
|
85
|
-
const action = (_a = this.events) === null || _a === void 0 ? void 0 : _a.list[eventName];
|
|
86
|
-
if (!action) {
|
|
87
|
-
throw new Error(`The service does not generate ${String(eventName)} event`);
|
|
88
|
-
}
|
|
89
|
-
const isStream = (_b = action.options) === null || _b === void 0 ? void 0 : _b.stream;
|
|
90
|
-
let subscription;
|
|
91
|
-
if (isStream) {
|
|
92
|
-
subscription = await new StreamManager_1.StreamManager({
|
|
93
|
-
broker: this.broker,
|
|
94
|
-
options: this.events.streamOptions,
|
|
95
|
-
serviceName: this.serviceName,
|
|
96
|
-
}).createConsumer(serviceNameFrom, String(eventName), options);
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
const queue = (options === null || options === void 0 ? void 0 : options.queue) ? { queue: options.queue } : {};
|
|
100
|
-
subscription = this.broker.subscribe(`${this.serviceName}.${eventName}`, queue);
|
|
101
|
-
}
|
|
102
|
-
this.subscriptions.set(eventName, subscription);
|
|
103
|
-
if (StreamManager_1.StreamManager.isStreamFetcher(subscription) && StreamManager_1.StreamManager.isPullConsumerOptions(options)) {
|
|
104
|
-
this.startBatchWatch(subscription, listener, String(eventName));
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
this.startWatch(subscription, listener, String(eventName));
|
|
108
|
-
}
|
|
109
|
-
return method.call(target, eventName, handler);
|
|
110
|
-
}
|
|
111
|
-
catch (error) {
|
|
112
|
-
const errorMessage = 'Failed subscribe to subject';
|
|
113
|
-
this.logger.error(errorMessage, error);
|
|
114
|
-
throw new Error(`${errorMessage} ${this.serviceName}.${String(eventName)}`);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
if (prop === 'off') {
|
|
119
|
-
return (eventName, listener) => {
|
|
120
|
-
this.logger.info('Unsubscribe', eventName);
|
|
121
|
-
const subscription = this.subscriptions.get(eventName);
|
|
122
|
-
subscription === null || subscription === void 0 ? void 0 : subscription.unsubscribe();
|
|
123
|
-
this.subscriptions.delete(eventName);
|
|
124
|
-
return method.call(target, eventName, listener);
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
return method;
|
|
128
|
-
},
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
createCacheKey(subject, data) {
|
|
132
|
-
const dataHash = (0, node_crypto_1.createHash)('sha1').update(JSON.stringify(data)).digest('hex');
|
|
133
|
-
return `${this.CACHE_SERVICE_KEY}:${subject}:${dataHash}`;
|
|
134
|
-
}
|
|
135
|
-
validate(data, schema) {
|
|
136
|
-
const ajv = new ajv_1.default();
|
|
137
|
-
if (this.Ref) {
|
|
138
|
-
ajv.addSchema(this.Ref);
|
|
139
|
-
}
|
|
140
|
-
const requestValidator = ajv.compile(schema);
|
|
141
|
-
const valid = requestValidator(data);
|
|
142
|
-
if (!valid) {
|
|
143
|
-
throw new Error(JSON.stringify(requestValidator.errors));
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
async request(subject, data, { options, request, response }) {
|
|
147
|
-
var _a, _b, _c, _d, _e;
|
|
148
|
-
const tracer = opentelemetry.trace.getTracer('');
|
|
149
|
-
const span = tracer.startSpan(subject, undefined, this.getContext(this.baggage));
|
|
150
|
-
try {
|
|
151
|
-
if (((_a = options === null || options === void 0 ? void 0 : options.runTimeValidation) === null || _a === void 0 ? void 0 : _a.request) && request) {
|
|
152
|
-
this.validate(data, request);
|
|
153
|
-
}
|
|
154
|
-
const { spanId, traceId, traceFlags } = span.spanContext();
|
|
155
|
-
const expired = this.getExpired((_b = this.baggage) === null || _b === void 0 ? void 0 : _b.expired, options === null || options === void 0 ? void 0 : options.timeout);
|
|
156
|
-
const requestId = (_c = this.baggage) === null || _c === void 0 ? void 0 : _c.requestId;
|
|
157
|
-
const message = { payload: data, baggage: { expired, traceId, spanId, traceFlags, requestId } };
|
|
158
|
-
const timeout = expired - Date.now();
|
|
159
|
-
if (timeout <= 0) {
|
|
160
|
-
throw new Error('Timeout request service ' + subject);
|
|
161
|
-
}
|
|
162
|
-
let key = '';
|
|
163
|
-
if ((options === null || options === void 0 ? void 0 : options.cache) && !this.isStream(data) && this.cache) {
|
|
164
|
-
try {
|
|
165
|
-
key = this.createCacheKey(subject, data);
|
|
166
|
-
const result = await Promise.race([this.cache.service.get(key), (0, promises_1.setTimeout)(this.cache.timeout, null)]);
|
|
167
|
-
if (result) {
|
|
168
|
-
return JSON.parse(result);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
this.logger.warn('get cache: ', error);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const result = (options === null || options === void 0 ? void 0 : options.useStream)
|
|
176
|
-
? await this.makeHttpRequest(subject, message, options, timeout)
|
|
177
|
-
: await this.makeBrokerRequest(subject, message, timeout);
|
|
178
|
-
if (result.error) {
|
|
179
|
-
throw new Error((_d = result.error.message) !== null && _d !== void 0 ? _d : result.error);
|
|
180
|
-
}
|
|
181
|
-
if (((_e = options === null || options === void 0 ? void 0 : options.runTimeValidation) === null || _e === void 0 ? void 0 : _e.response) && response) {
|
|
182
|
-
this.validate(result.payload, response);
|
|
183
|
-
}
|
|
184
|
-
if ((options === null || options === void 0 ? void 0 : options.cache) && !this.isStream(result.payload) && this.cache) {
|
|
185
|
-
this.cache.service.set(key, JSON.stringify(result.payload), options.cache);
|
|
186
|
-
}
|
|
187
|
-
return result.payload;
|
|
188
|
-
}
|
|
189
|
-
catch (error) {
|
|
190
|
-
span.setAttribute('error', true);
|
|
191
|
-
span.setAttribute('error.kind', error);
|
|
192
|
-
this.logger.error(error);
|
|
193
|
-
throw error;
|
|
194
|
-
}
|
|
195
|
-
finally {
|
|
196
|
-
span.end();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
async getHTTPSettingsFromRemoteService() {
|
|
200
|
-
const subject = `${this.serviceName}.${this.SERVICE_SUBJECT_FOR_GET_HTTP_SETTINGS}`;
|
|
201
|
-
const result = await this.broker.request(subject, Buffer.from(JSON.stringify('')), {
|
|
202
|
-
timeout: this.REQUEST_HTTP_SETTINGS_TIMEOUT,
|
|
203
|
-
});
|
|
204
|
-
const { ip, port } = (0, nats_1.JSONCodec)().decode(result.data);
|
|
205
|
-
if (!ip || !port) {
|
|
206
|
-
throw new Error(`Remote service ${this.serviceName} did not return http settings`);
|
|
207
|
-
}
|
|
208
|
-
return { ip, port };
|
|
209
|
-
}
|
|
210
|
-
isStream(data) {
|
|
211
|
-
return data instanceof node_stream_1.Readable;
|
|
212
|
-
}
|
|
213
|
-
async makeBrokerRequest(subject, message, timeout) {
|
|
214
|
-
try {
|
|
215
|
-
const result = await this.broker.request(subject, Buffer.from(JSON.stringify(message)), { timeout });
|
|
216
|
-
return (0, nats_1.JSONCodec)().decode(result.data);
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
const errorMessage = new Error(`${error === null || error === void 0 ? void 0 : error.message}. Subject: ${subject} `);
|
|
220
|
-
return this.buildErrorMessage(errorMessage);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
async makeHttpRequest(subject, message, options, timeout) {
|
|
224
|
-
return new Promise(async (resolve) => {
|
|
225
|
-
const { ip, port } = await this.getHTTPSettingsFromRemoteService();
|
|
226
|
-
const serviceActionPath = subject.split('.');
|
|
227
|
-
const headersFromBaggage = this.convertBaggaggeToExternalHeader(message.baggage);
|
|
228
|
-
const headers = {
|
|
229
|
-
'Content-Type': this.isStream(message.payload) ? 'application/octet-stream' : 'application/json',
|
|
230
|
-
...headersFromBaggage,
|
|
231
|
-
};
|
|
232
|
-
if (!this.isStream(message.payload)) {
|
|
233
|
-
headers['Content-Length'] = Buffer.byteLength(JSON.stringify(message.payload));
|
|
234
|
-
}
|
|
235
|
-
const request = http.request({
|
|
236
|
-
host: ip,
|
|
237
|
-
port,
|
|
238
|
-
path: `/${serviceActionPath.join('/')}`,
|
|
239
|
-
method: 'POST',
|
|
240
|
-
headers,
|
|
241
|
-
timeout,
|
|
242
|
-
}, async (response) => {
|
|
243
|
-
var _a;
|
|
244
|
-
if (((_a = options === null || options === void 0 ? void 0 : options.useStream) === null || _a === void 0 ? void 0 : _a.response) && response.statusCode !== 500) {
|
|
245
|
-
resolve({ payload: response });
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
const data = [];
|
|
249
|
-
for await (const chunk of response) {
|
|
250
|
-
data.push(chunk);
|
|
251
|
-
}
|
|
252
|
-
const responseDataString = Buffer.concat(data).toString();
|
|
253
|
-
try {
|
|
254
|
-
resolve(JSON.parse(responseDataString));
|
|
255
|
-
}
|
|
256
|
-
catch (error) {
|
|
257
|
-
const errorMessage = new Error(`${error === null || error === void 0 ? void 0 : error.message}. Subject: ${subject} `);
|
|
258
|
-
resolve(this.buildErrorMessage(errorMessage));
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
request.on('error', error => {
|
|
262
|
-
const errorMessage = new Error(`${error === null || error === void 0 ? void 0 : error.message}. Subject: ${subject} `);
|
|
263
|
-
resolve(this.buildErrorMessage(errorMessage));
|
|
264
|
-
});
|
|
265
|
-
if (this.isStream(message.payload)) {
|
|
266
|
-
message.payload.pipe(request);
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
request.write(JSON.stringify(message.payload));
|
|
270
|
-
request.end();
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
isJsMessage(message) {
|
|
274
|
-
return !!message.ack && !!message.nak;
|
|
275
|
-
}
|
|
276
|
-
getBaggageFromNATSHeader(headers) {
|
|
277
|
-
const traceId = headers.get('nsc-trace-id');
|
|
278
|
-
const spanId = headers.get('nsc-span-id');
|
|
279
|
-
const traceFlags = headers.has('nsc-trace-flags') ? +headers.get('nsc-trace-flags') : undefined;
|
|
280
|
-
const requestId = headers.has('x-request-id') ? String(headers.get('x-request-id')) : undefined;
|
|
281
|
-
if (traceId && spanId && traceFlags) {
|
|
282
|
-
return {
|
|
283
|
-
traceId,
|
|
284
|
-
spanId,
|
|
285
|
-
traceFlags,
|
|
286
|
-
requestId,
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
exports.Client = Client;
|
|
292
|
-
//# sourceMappingURL=Client.js.map
|
package/dist/Container.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.container = void 0;
|
|
4
|
-
const _1 = require(".");
|
|
5
|
-
class Container {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.container = new Map();
|
|
8
|
-
this.singltons = new Map();
|
|
9
|
-
}
|
|
10
|
-
buildDependency(key) {
|
|
11
|
-
const deepDependency = this.get(key);
|
|
12
|
-
if (this.isAdapterDependency(deepDependency.dependency)) {
|
|
13
|
-
return new deepDependency.dependency.value(...deepDependency.constructor);
|
|
14
|
-
}
|
|
15
|
-
if (this.isConstantDependency(deepDependency.dependency)) {
|
|
16
|
-
return deepDependency.dependency.value;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
inject(dependency) {
|
|
20
|
-
if (this.isServiceDependency(dependency)) {
|
|
21
|
-
return { dependency, constructor: [] };
|
|
22
|
-
}
|
|
23
|
-
const deepDependencies = Reflect.getMetadata(_1.dependencyStorageMetaKey, dependency.value);
|
|
24
|
-
if (deepDependencies && deepDependencies.size) {
|
|
25
|
-
const constructor = [];
|
|
26
|
-
deepDependencies.forEach((key, propertyName) => {
|
|
27
|
-
if (Array.isArray(key)) {
|
|
28
|
-
key.forEach((item, index) => {
|
|
29
|
-
constructor[index] = this.buildDependency(item);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
dependency.value.prototype[propertyName] = this.buildDependency(key);
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
return { dependency, constructor };
|
|
37
|
-
}
|
|
38
|
-
return { dependency, constructor: [] };
|
|
39
|
-
}
|
|
40
|
-
isServiceDependency(dependency) {
|
|
41
|
-
return dependency.type === _1.DependencyType.SERVICE;
|
|
42
|
-
}
|
|
43
|
-
isAdapterDependency(dependency) {
|
|
44
|
-
return dependency.type === _1.DependencyType.ADAPTER;
|
|
45
|
-
}
|
|
46
|
-
isConstantDependency(dependency) {
|
|
47
|
-
return dependency.type === _1.DependencyType.CONSTANT;
|
|
48
|
-
}
|
|
49
|
-
bind(key, type, value, options) {
|
|
50
|
-
this.container.set(key, { type, value, options });
|
|
51
|
-
}
|
|
52
|
-
symbol(key) {
|
|
53
|
-
return {
|
|
54
|
-
to: {
|
|
55
|
-
Adapter: (value, options) => {
|
|
56
|
-
this.container.set(key, { type: _1.DependencyType.ADAPTER, value, options });
|
|
57
|
-
},
|
|
58
|
-
Singlton: (value) => {
|
|
59
|
-
this.container.set(key, { type: _1.DependencyType.ADAPTER, value, options: { singlton: true } });
|
|
60
|
-
},
|
|
61
|
-
Constant: (value) => {
|
|
62
|
-
this.container.set(key, { type: _1.DependencyType.CONSTANT, value, options: { singlton: true } });
|
|
63
|
-
},
|
|
64
|
-
Initializable: (value) => {
|
|
65
|
-
this.container.set(key, { type: _1.DependencyType.ADAPTER, value, options: { init: true } });
|
|
66
|
-
},
|
|
67
|
-
Service: (value) => {
|
|
68
|
-
this.container.set(key, { type: _1.DependencyType.SERVICE, value });
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
async unbind(key) {
|
|
74
|
-
this.container.delete(key);
|
|
75
|
-
const instance = this.singltons.get(key);
|
|
76
|
-
if (instance === null || instance === void 0 ? void 0 : instance.init) {
|
|
77
|
-
await instance.value.close();
|
|
78
|
-
}
|
|
79
|
-
this.singltons.delete(key);
|
|
80
|
-
}
|
|
81
|
-
get(key) {
|
|
82
|
-
const dependency = this.container.get(key);
|
|
83
|
-
if (!dependency) {
|
|
84
|
-
throw new Error(`Dependency ${key.toString()} is not bound to the container`);
|
|
85
|
-
}
|
|
86
|
-
return this.inject(dependency);
|
|
87
|
-
}
|
|
88
|
-
getInstance(key) {
|
|
89
|
-
var _a, _b, _c;
|
|
90
|
-
const { dependency, constructor } = this.get(key);
|
|
91
|
-
if (this.isServiceDependency(dependency)) {
|
|
92
|
-
throw new Error(`Unable to get service instance`);
|
|
93
|
-
}
|
|
94
|
-
if (this.isConstantDependency(dependency)) {
|
|
95
|
-
return dependency.value;
|
|
96
|
-
}
|
|
97
|
-
if (this.isAdapterDependency(dependency)) {
|
|
98
|
-
if (this.singltons.has(key)) {
|
|
99
|
-
return this.singltons.get(key).value;
|
|
100
|
-
}
|
|
101
|
-
const adapter = new dependency.value(...constructor);
|
|
102
|
-
if (((_a = dependency.options) === null || _a === void 0 ? void 0 : _a.singlton) || ((_b = dependency.options) === null || _b === void 0 ? void 0 : _b.init)) {
|
|
103
|
-
this.singltons.set(key, { value: adapter, init: (_c = dependency.options) === null || _c === void 0 ? void 0 : _c.init });
|
|
104
|
-
}
|
|
105
|
-
return adapter;
|
|
106
|
-
}
|
|
107
|
-
throw new Error(`Unknown dependency type for key ${key.toString()}`);
|
|
108
|
-
}
|
|
109
|
-
async initDependencies() {
|
|
110
|
-
var _a, _b;
|
|
111
|
-
const initialized = [];
|
|
112
|
-
for await (const [key, dependency] of this.container) {
|
|
113
|
-
if (this.isAdapterDependency(dependency) && ((_a = dependency.options) === null || _a === void 0 ? void 0 : _a.init) && !this.singltons.has(key)) {
|
|
114
|
-
const instance = this.getInstance(key);
|
|
115
|
-
await (instance === null || instance === void 0 ? void 0 : instance.init());
|
|
116
|
-
initialized.push(instance);
|
|
117
|
-
this.singltons.set(key, { value: instance, init: (_b = dependency.options) === null || _b === void 0 ? void 0 : _b.init });
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return initialized;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
exports.container = new Container();
|
|
124
|
-
//# sourceMappingURL=Container.js.map
|
package/dist/Meter.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Meter = void 0;
|
|
4
|
-
const interfaces_1 = require("./interfaces");
|
|
5
|
-
const opentelemetry = require("@opentelemetry/api");
|
|
6
|
-
const Root_1 = require("./Root");
|
|
7
|
-
class Meter extends Root_1.Root {
|
|
8
|
-
constructor(name, baggage) {
|
|
9
|
-
super();
|
|
10
|
-
this.name = name;
|
|
11
|
-
this.baggage = baggage;
|
|
12
|
-
}
|
|
13
|
-
start() {
|
|
14
|
-
const tracer = opentelemetry.trace.getTracer('');
|
|
15
|
-
this.span = tracer.startSpan(this.name, { kind: opentelemetry.SpanKind.CONSUMER }, this.getContext(this.baggage));
|
|
16
|
-
}
|
|
17
|
-
end(error) {
|
|
18
|
-
if (!this.span) {
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
if (error) {
|
|
22
|
-
this.span.setAttribute('error', true);
|
|
23
|
-
this.span.setAttribute('error.kind', error.message);
|
|
24
|
-
}
|
|
25
|
-
this.span.end();
|
|
26
|
-
}
|
|
27
|
-
measure(func, arg, context, tag) {
|
|
28
|
-
const tracer = opentelemetry.trace.getTracer('');
|
|
29
|
-
let spanContext;
|
|
30
|
-
if (this.span) {
|
|
31
|
-
spanContext = opentelemetry.trace.setSpan(opentelemetry.context.active(), this.span);
|
|
32
|
-
}
|
|
33
|
-
const options = { kind: opentelemetry.SpanKind.INTERNAL };
|
|
34
|
-
if ((tag === null || tag === void 0 ? void 0 : tag[interfaces_1.TagKey.LOCATION]) === 'external') {
|
|
35
|
-
options.kind = opentelemetry.SpanKind.CLIENT;
|
|
36
|
-
}
|
|
37
|
-
const span = tracer.startSpan(func.name, options, spanContext);
|
|
38
|
-
this.applyTag(span, tag);
|
|
39
|
-
const result = func.apply(context, arg);
|
|
40
|
-
if (result.then) {
|
|
41
|
-
return result.then((result) => {
|
|
42
|
-
span.end();
|
|
43
|
-
return result;
|
|
44
|
-
}, (error) => {
|
|
45
|
-
span.setAttribute('error', true);
|
|
46
|
-
span.setAttribute('error.kind', error.message);
|
|
47
|
-
span.end();
|
|
48
|
-
throw error;
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
span.end();
|
|
53
|
-
}
|
|
54
|
-
return result;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
exports.Meter = Meter;
|
|
58
|
-
//# sourceMappingURL=Meter.js.map
|