dt-common-device 2.0.7 → 3.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.
- package/README.md +352 -98
- package/dist/alerts/Alert.model.d.ts +28 -0
- package/dist/alerts/Alert.model.js +222 -0
- package/dist/alerts/Alert.repository.d.ts +106 -0
- package/dist/alerts/Alert.repository.js +374 -0
- package/dist/alerts/Alert.service.d.ts +137 -0
- package/dist/alerts/Alert.service.js +476 -0
- package/dist/alerts/AlertBuilder.d.ts +87 -0
- package/dist/alerts/AlertBuilder.example.d.ts +11 -0
- package/dist/alerts/AlertBuilder.example.js +117 -0
- package/dist/alerts/AlertBuilder.js +185 -0
- package/dist/alerts/AlertService.example.d.ts +55 -0
- package/dist/alerts/AlertService.example.js +148 -0
- package/dist/alerts/alert.types.d.ts +57 -0
- package/dist/alerts/alert.types.js +22 -0
- package/dist/alerts/index.d.ts +3 -0
- package/dist/alerts/index.js +19 -0
- package/dist/config/config.d.ts +4 -4
- package/dist/config/config.js +3 -3
- package/dist/config/config.types.d.ts +19 -0
- package/dist/config/config.types.js +2 -0
- package/dist/connection/Connection.repository.d.ts +8 -0
- package/dist/connection/Connection.repository.js +92 -0
- package/dist/connection/Connection.service.d.ts +8 -0
- package/dist/connection/Connection.service.js +32 -0
- package/dist/connection/IConnection.d.ts +26 -0
- package/dist/connection/IConnection.js +14 -0
- package/dist/connection/index.d.ts +2 -0
- package/dist/connection/index.js +18 -0
- package/dist/device/cloud/entities/CloudDevice.d.ts +2 -2
- package/dist/device/cloud/entities/CloudDeviceService.d.ts +1 -1
- package/dist/device/cloud/entities/DeviceFactory.d.ts +1 -1
- package/dist/device/cloud/entities/DeviceFactory.js +1 -1
- package/dist/device/cloud/interface.d.ts +101 -0
- package/dist/device/cloud/interface.js +3 -0
- package/dist/device/cloud/interfaces/ICloudDeviceService.d.ts +1 -1
- package/dist/device/cloud/interfaces/IDeviceConnectionService.d.ts +7 -0
- package/dist/device/cloud/interfaces/IDeviceConnectionService.js +3 -0
- package/dist/device/cloud/interfaces/IDevicesService.d.ts +9 -0
- package/dist/device/cloud/interfaces/IDevicesService.js +2 -0
- package/dist/device/cloud/interfaces/IRawDevice.d.ts +1 -1
- package/dist/device/cloud/services/Device.service.d.ts +39 -0
- package/dist/device/cloud/services/Device.service.js +9 -0
- package/dist/device/cloud/services/DeviceCloudService.d.ts +42 -0
- package/dist/device/cloud/services/DeviceCloudService.js +59 -0
- package/dist/device/cloud/services/DeviceHub.service.d.ts +3 -0
- package/dist/device/cloud/services/DeviceHub.service.js +6 -0
- package/dist/device/cloud/services/Hub.service.d.ts +25 -0
- package/dist/device/cloud/services/Hub.service.js +9 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.d.ts +38 -0
- package/dist/device/cloud/services/SmartThingsDeviceService.js +52 -0
- package/dist/device/index.d.ts +4 -0
- package/dist/device/index.js +20 -0
- package/dist/device/local/events/EventHandler.js +6 -6
- package/dist/device/local/events/Events.d.ts +12 -33
- package/dist/device/local/events/Events.js +12 -33
- package/dist/device/local/interface.d.ts +0 -0
- package/dist/device/local/interface.js +1 -0
- package/dist/device/local/interfaces/index.d.ts +2 -3
- package/dist/device/local/interfaces/index.js +2 -3
- package/dist/device/local/repository/Device.repository.js +3 -3
- package/dist/device/local/repository/Hub.repository.js +4 -4
- package/dist/device/local/repository/Schedule.repository.js +2 -2
- package/dist/device/local/services/Device.service.d.ts +2 -2
- package/dist/device/local/services/Device.service.js +1 -1
- package/dist/device/local/services/DeviceHub.service.d.ts +11 -0
- package/dist/device/local/services/DeviceHub.service.js +40 -0
- package/dist/device/local/services/index.d.ts +0 -4
- package/dist/device/local/services/index.js +0 -4
- package/dist/events/BaseEventHandler.d.ts +2 -2
- package/dist/events/BaseEventHandler.js +2 -2
- package/dist/events/BaseEventTransformer.d.ts +1 -1
- package/dist/events/BaseEventTransformer.js +1 -1
- package/dist/events/DeviceEventHandler.d.ts +1 -1
- package/dist/events/DeviceEventHandler.js +2 -2
- package/dist/events/EventHandler.js +1 -1
- package/dist/events/EventHandlerOrchestrator.js +1 -1
- package/dist/events/EventProcessingService.js +1 -1
- package/dist/events/InternalEventSubscription.js +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +16 -13
- package/dist/issues/Issue.model.d.ts +28 -0
- package/dist/issues/Issue.model.js +260 -0
- package/dist/issues/Issue.repository.d.ts +113 -0
- package/dist/issues/Issue.repository.js +401 -0
- package/dist/issues/Issue.service.d.ts +168 -0
- package/dist/issues/Issue.service.js +642 -0
- package/dist/issues/IssueBuilder.d.ts +109 -0
- package/dist/issues/IssueBuilder.example.d.ts +16 -0
- package/dist/issues/IssueBuilder.example.js +196 -0
- package/dist/issues/IssueBuilder.js +237 -0
- package/dist/issues/IssueService.example.d.ts +68 -0
- package/dist/issues/IssueService.example.js +177 -0
- package/dist/issues/index.d.ts +2 -0
- package/dist/issues/index.js +18 -0
- package/dist/issues/issue.types.d.ts +90 -0
- package/dist/issues/issue.types.js +40 -0
- package/dist/property/IProperty.d.ts +29 -0
- package/dist/property/IProperty.js +2 -0
- package/dist/property/Property.repository.d.ts +8 -0
- package/dist/property/Property.repository.js +95 -0
- package/dist/property/Property.service.d.ts +8 -0
- package/dist/property/Property.service.js +36 -0
- package/dist/property/index.d.ts +2 -0
- package/dist/property/index.js +18 -0
- package/dist/queue/entities/HybridHttpQueue.d.ts +24 -0
- package/dist/queue/entities/HybridHttpQueue.js +241 -0
- package/dist/queue/entities/index.d.ts +1 -0
- package/dist/queue/entities/index.js +17 -0
- package/dist/queue/index.d.ts +5 -0
- package/dist/queue/index.js +22 -0
- package/dist/queue/interfaces/IHttpRequestJob.d.ts +9 -0
- package/dist/queue/interfaces/IHttpRequestJob.js +2 -0
- package/dist/queue/interfaces/IHybridHttpQueue.d.ts +17 -0
- package/dist/queue/interfaces/IHybridHttpQueue.js +2 -0
- package/dist/queue/interfaces/IJobResult.d.ts +14 -0
- package/dist/queue/interfaces/IJobResult.js +2 -0
- package/dist/queue/interfaces/IRateLimitConfig.d.ts +5 -0
- package/dist/queue/interfaces/IRateLimitConfig.js +2 -0
- package/dist/queue/interfaces/index.d.ts +4 -0
- package/dist/queue/interfaces/index.js +20 -0
- package/dist/queue/services/QueueService.d.ts +19 -0
- package/dist/queue/services/QueueService.js +73 -0
- package/dist/queue/services/index.d.ts +1 -0
- package/dist/queue/services/index.js +17 -0
- package/dist/queue/types/http.types.d.ts +21 -0
- package/dist/queue/types/http.types.js +2 -0
- package/dist/queue/types/index.d.ts +2 -0
- package/dist/queue/types/index.js +18 -0
- package/dist/queue/types/queue.types.d.ts +31 -0
- package/dist/queue/types/queue.types.js +2 -0
- package/dist/queue/utils/index.d.ts +3 -0
- package/dist/queue/utils/index.js +19 -0
- package/dist/queue/utils/jobUtils.d.ts +10 -0
- package/dist/queue/utils/jobUtils.js +64 -0
- package/dist/queue/utils/queueUtils.d.ts +5 -0
- package/dist/queue/utils/queueUtils.js +60 -0
- package/dist/queue/utils/rateLimit.utils.d.ts +10 -0
- package/dist/queue/utils/rateLimit.utils.js +97 -0
- package/package.json +2 -1
- package/src/{device/local/models → alerts}/Alert.model.ts +1 -1
- package/src/{device/local/repository → alerts}/Alert.repository.ts +2 -2
- package/src/{device/local/services → alerts}/Alert.service.ts +14 -7
- package/src/{device/local/entities → alerts}/AlertBuilder.example.ts +2 -2
- package/src/{device/local/entities → alerts}/AlertBuilder.ts +14 -8
- package/src/{device/local/services → alerts}/AlertService.example.ts +6 -5
- package/src/{types → alerts}/alert.types.ts +2 -2
- package/src/alerts/index.ts +3 -0
- package/src/config/config.ts +7 -7
- package/src/{types → config}/config.types.ts +1 -1
- package/src/{device/local/repository → connection}/Connection.repository.ts +2 -2
- package/src/{device/local/services → connection}/Connection.service.ts +2 -2
- package/src/connection/index.ts +3 -0
- package/src/device/cloud/entities/CloudDevice.ts +2 -2
- package/src/device/cloud/entities/CloudDeviceService.ts +1 -1
- package/src/device/cloud/entities/DeviceFactory.ts +2 -2
- package/src/device/cloud/interfaces/ICloudDeviceService.ts +1 -1
- package/src/device/cloud/interfaces/IRawDevice.ts +1 -1
- package/src/device/local/interfaces/index.ts +2 -3
- package/src/device/local/repository/Device.repository.ts +3 -3
- package/src/device/local/repository/Hub.repository.ts +4 -4
- package/src/device/local/repository/Schedule.repository.ts +2 -2
- package/src/device/local/services/Device.service.ts +1 -1
- package/src/device/local/services/index.ts +0 -4
- package/{TROUBLESHOOTING.md → src/docs/TROUBLESHOOTING.md} +2 -2
- package/src/events/BaseEventHandler.ts +3 -3
- package/src/events/BaseEventTransformer.ts +2 -2
- package/src/events/DeviceEventHandler.ts +3 -3
- package/src/events/EventHandler.ts +1 -1
- package/src/events/EventHandlerOrchestrator.ts +2 -2
- package/src/events/EventProcessingService.ts +2 -2
- package/src/events/InternalEventSubscription.ts +2 -2
- package/src/index.ts +19 -13
- package/src/{device/local/models → issues}/Issue.model.ts +1 -1
- package/src/{device/local/repository → issues}/Issue.repository.ts +2 -2
- package/src/{device/local/services → issues}/Issue.service.ts +4 -4
- package/src/{device/local/entities → issues}/IssueBuilder.example.ts +1 -1
- package/src/{device/local/entities → issues}/IssueBuilder.ts +1 -1
- package/src/{device/local/services → issues}/IssueService.example.ts +6 -5
- package/src/issues/index.ts +2 -0
- package/src/{device/local/repository → property}/Property.repository.ts +2 -2
- package/src/{device/local/services → property}/Property.service.ts +1 -1
- package/src/property/index.ts +2 -0
- package/src/queue/entities/HybridHttpQueue.ts +272 -0
- package/src/queue/entities/index.ts +1 -0
- package/src/queue/index.ts +6 -0
- package/src/queue/interfaces/IHttpRequestJob.ts +10 -0
- package/src/queue/interfaces/IHybridHttpQueue.ts +24 -0
- package/src/queue/interfaces/IJobResult.ts +15 -0
- package/src/queue/interfaces/IRateLimitConfig.ts +5 -0
- package/src/queue/interfaces/index.ts +4 -0
- package/src/queue/services/QueueService.ts +39 -0
- package/src/queue/services/index.ts +1 -0
- package/src/queue/types/http.types.ts +22 -0
- package/src/queue/types/index.ts +2 -0
- package/src/queue/types/queue.types.ts +21 -0
- package/src/queue/utils/index.ts +3 -0
- package/src/queue/utils/jobUtils.ts +80 -0
- package/src/queue/utils/queueUtils.ts +91 -0
- package/src/queue/utils/rateLimit.utils.ts +131 -0
- package/tsconfig.json +4 -0
- package/src/device/local/entities/README.md +0 -173
- package/src/device/local/entities/index.ts +0 -2
- package/src/types/index.ts +0 -3
- /package/src/{device/local/interfaces → connection}/IConnection.ts +0 -0
- /package/src/{device/local/models → docs}/Alert.model.md +0 -0
- /package/src/{device/local/models/README.md → docs/Alerts&IssuesModel.md} +0 -0
- /package/src/{device/local/models → docs}/Issue.model.md +0 -0
- /package/{SECURITY.md → src/docs/SECURITY.md} +0 -0
- /package/src/{types → issues}/issue.types.ts +0 -0
- /package/src/{device/local/interfaces → property}/IProperty.ts +0 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
exports.HybridHttpQueue = void 0;
|
|
45
|
+
const config_1 = require("../../config/config");
|
|
46
|
+
const axios_1 = __importDefault(require("axios"));
|
|
47
|
+
const dt_audit_library_1 = require("dt-audit-library");
|
|
48
|
+
const typedi_1 = require("typedi");
|
|
49
|
+
const rateLimit_utils_1 = require("../utils/rateLimit.utils");
|
|
50
|
+
const jobUtils_1 = require("../utils/jobUtils");
|
|
51
|
+
const queueUtils_1 = require("../utils/queueUtils");
|
|
52
|
+
let HybridHttpQueue = (() => {
|
|
53
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
54
|
+
let _classDescriptor;
|
|
55
|
+
let _classExtraInitializers = [];
|
|
56
|
+
let _classThis;
|
|
57
|
+
var HybridHttpQueue = _classThis = class {
|
|
58
|
+
constructor() {
|
|
59
|
+
this.queues = new Map();
|
|
60
|
+
this.workers = new Map();
|
|
61
|
+
this.jobResults = new Map();
|
|
62
|
+
this.rateLimitConfigs = rateLimit_utils_1.RateLimitUtils.initializeRateLimitConfigs();
|
|
63
|
+
}
|
|
64
|
+
async handleRateLimitAndQueue(url, method, options) {
|
|
65
|
+
const { connectionId, provider, microservice } = jobUtils_1.JobUtils.extractConnectionDetails(options);
|
|
66
|
+
const key = `rate_limit:${provider}:${connectionId}`;
|
|
67
|
+
const config = this.rateLimitConfigs.get(provider);
|
|
68
|
+
const windowMs = config?.windowMs ?? 60000;
|
|
69
|
+
const timestamps = await rateLimit_utils_1.RateLimitUtils.getRawRequestTimestamps(key);
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
const windowStart = now - windowMs;
|
|
72
|
+
const recentRequests = timestamps.filter((t) => t > windowStart);
|
|
73
|
+
const nextAvailableTime = recentRequests.length > 0 ? recentRequests[0] + windowMs : now + 1000;
|
|
74
|
+
const delay = Math.max(nextAvailableTime - now, 1000); // at least 1s delay
|
|
75
|
+
// Create job data
|
|
76
|
+
const jobData = {
|
|
77
|
+
microservice,
|
|
78
|
+
connectionId,
|
|
79
|
+
provider,
|
|
80
|
+
url,
|
|
81
|
+
method,
|
|
82
|
+
options,
|
|
83
|
+
timestamp: Date.now(),
|
|
84
|
+
};
|
|
85
|
+
// Add job to queue with delay (background processing)
|
|
86
|
+
const queueKey = queueUtils_1.QueueUtils.getQueueKey(microservice, connectionId, provider);
|
|
87
|
+
const queue = queueUtils_1.QueueUtils.getOrCreateQueue(queueKey, this.queues);
|
|
88
|
+
queueUtils_1.QueueUtils.getOrCreateWorker(queueKey, this.workers, this.processHttpRequest.bind(this), this.jobResults);
|
|
89
|
+
const job = await queue.add("http-request", jobData, {
|
|
90
|
+
delay,
|
|
91
|
+
attempts: 1,
|
|
92
|
+
removeOnComplete: { age: 300, count: 1 },
|
|
93
|
+
removeOnFail: { age: 300, count: 1 },
|
|
94
|
+
});
|
|
95
|
+
await (0, dt_audit_library_1.publishAudit)({
|
|
96
|
+
eventType: "http.request.rateLimitQueued",
|
|
97
|
+
properties: {
|
|
98
|
+
resource: microservice,
|
|
99
|
+
connectionId,
|
|
100
|
+
provider,
|
|
101
|
+
endpoint: url,
|
|
102
|
+
method,
|
|
103
|
+
timestamp: Date.now(),
|
|
104
|
+
queueId: job.id,
|
|
105
|
+
reason: "rate_limit_exceeded_queued",
|
|
106
|
+
delay,
|
|
107
|
+
estimatedProcessingTime: now + delay,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
// Return immediate response to controller
|
|
111
|
+
return {
|
|
112
|
+
success: true,
|
|
113
|
+
queued: true,
|
|
114
|
+
estimatedProcessingTime: now + delay,
|
|
115
|
+
jobId: job.id,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
async processHttpRequest(job) {
|
|
119
|
+
const { connectionId, provider, url, method, options } = job.data;
|
|
120
|
+
const allowed = await rateLimit_utils_1.RateLimitUtils.isRateLimitAllowed(connectionId, provider, this.rateLimitConfigs);
|
|
121
|
+
if (!allowed) {
|
|
122
|
+
// This shouldn't happen since we check before queuing, but handle it gracefully
|
|
123
|
+
(0, config_1.getConfig)().LOGGER.warn(`Job ${job.id} still rate limited after delay, skipping`);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
await rateLimit_utils_1.RateLimitUtils.recordRequest(connectionId, provider);
|
|
127
|
+
try {
|
|
128
|
+
(0, config_1.getConfig)().LOGGER.info(`Executing HTTP request: ${method} ${url} for ${provider}`);
|
|
129
|
+
const response = await (0, axios_1.default)({
|
|
130
|
+
method: method.toLowerCase(),
|
|
131
|
+
url: url,
|
|
132
|
+
headers: options.headers || {},
|
|
133
|
+
timeout: 30000,
|
|
134
|
+
...(options.body && { data: options.body }),
|
|
135
|
+
...(options.params && { params: options.params }),
|
|
136
|
+
});
|
|
137
|
+
(0, config_1.getConfig)().LOGGER.info(`HTTP request successful: ${method} ${url} for ${provider}`);
|
|
138
|
+
return response.data;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
(0, config_1.getConfig)().LOGGER.error(`HTTP request failed ${job.id}: ${error.message}`);
|
|
142
|
+
await (0, dt_audit_library_1.publishAudit)({
|
|
143
|
+
eventType: "http.request.error",
|
|
144
|
+
properties: {
|
|
145
|
+
connectionId,
|
|
146
|
+
provider,
|
|
147
|
+
endpoint: url,
|
|
148
|
+
method,
|
|
149
|
+
timestamp: Date.now(),
|
|
150
|
+
queueId: job.id,
|
|
151
|
+
reason: "execution_error",
|
|
152
|
+
errorMessage: error.message,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
throw new Error(`HTTP request failed: ${error.message}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
async request(options) {
|
|
159
|
+
const { method, url, body, headers, queueOptions } = options;
|
|
160
|
+
// Create HttpCallOption object
|
|
161
|
+
const httpCallOption = {
|
|
162
|
+
headers,
|
|
163
|
+
body,
|
|
164
|
+
queueOptions,
|
|
165
|
+
};
|
|
166
|
+
// Call handleRequest with the constructed parameters
|
|
167
|
+
return this.handleRequest(url, method, httpCallOption);
|
|
168
|
+
}
|
|
169
|
+
async handleRequest(url, method, options) {
|
|
170
|
+
const { connectionId, provider, microservice } = jobUtils_1.JobUtils.extractConnectionDetails(options);
|
|
171
|
+
// Check rate limit first
|
|
172
|
+
const allowed = await rateLimit_utils_1.RateLimitUtils.isRateLimitAllowed(connectionId, provider, this.rateLimitConfigs);
|
|
173
|
+
if (!allowed) {
|
|
174
|
+
// Rate limited - queue the request and return immediate response
|
|
175
|
+
return this.handleRateLimitAndQueue(url, method, options);
|
|
176
|
+
}
|
|
177
|
+
// Not rate limited - process immediately
|
|
178
|
+
(0, config_1.getConfig)().LOGGER.info(`Processing immediately: ${method} ${url} -> ${provider} [${connectionId}]`);
|
|
179
|
+
try {
|
|
180
|
+
// Record the request first
|
|
181
|
+
await rateLimit_utils_1.RateLimitUtils.recordRequest(connectionId, provider);
|
|
182
|
+
// Execute the HTTP request
|
|
183
|
+
const response = await (0, axios_1.default)({
|
|
184
|
+
method: method.toLowerCase(),
|
|
185
|
+
url: url,
|
|
186
|
+
headers: options.headers || {},
|
|
187
|
+
timeout: 30000,
|
|
188
|
+
...(options.body && { data: options.body }),
|
|
189
|
+
...(options.params && { params: options.params }),
|
|
190
|
+
});
|
|
191
|
+
(0, config_1.getConfig)().LOGGER.info(`HTTP request successful: ${method} ${url} for ${provider}`);
|
|
192
|
+
return {
|
|
193
|
+
success: true,
|
|
194
|
+
data: response.data,
|
|
195
|
+
queued: false,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
(0, config_1.getConfig)().LOGGER.error(`HTTP request failed: ${error.message}`);
|
|
200
|
+
await (0, dt_audit_library_1.publishAudit)({
|
|
201
|
+
eventType: "http.request.error",
|
|
202
|
+
properties: {
|
|
203
|
+
connectionId,
|
|
204
|
+
provider,
|
|
205
|
+
endpoint: url,
|
|
206
|
+
method,
|
|
207
|
+
timestamp: Date.now(),
|
|
208
|
+
reason: "execution_error",
|
|
209
|
+
errorMessage: error.message,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
return {
|
|
213
|
+
success: false,
|
|
214
|
+
error: `HTTP request failed: ${error.message}`,
|
|
215
|
+
queued: false,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async shutdown() {
|
|
220
|
+
(0, config_1.getConfig)().LOGGER.info("Shutting down HTTP queues...");
|
|
221
|
+
await Promise.all([
|
|
222
|
+
...Array.from(this.workers.values()).map((worker) => worker.close()),
|
|
223
|
+
...Array.from(this.queues.values()).map((queue) => queue.close()),
|
|
224
|
+
]);
|
|
225
|
+
this.workers.clear();
|
|
226
|
+
this.queues.clear();
|
|
227
|
+
this.jobResults.clear();
|
|
228
|
+
(0, config_1.getConfig)().LOGGER.info("HTTP queues shutdown complete");
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
__setFunctionName(_classThis, "HybridHttpQueue");
|
|
232
|
+
(() => {
|
|
233
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
234
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
235
|
+
HybridHttpQueue = _classThis = _classDescriptor.value;
|
|
236
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
237
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
238
|
+
})();
|
|
239
|
+
return HybridHttpQueue = _classThis;
|
|
240
|
+
})();
|
|
241
|
+
exports.HybridHttpQueue = HybridHttpQueue;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./HybridHttpQueue";
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./HybridHttpQueue"), exports);
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
// Main queue exports
|
|
18
|
+
__exportStar(require("./entities"), exports);
|
|
19
|
+
__exportStar(require("./interfaces"), exports);
|
|
20
|
+
__exportStar(require("./services"), exports);
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
22
|
+
__exportStar(require("./utils"), exports);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HttpCallOption } from "../types/http.types";
|
|
2
|
+
import { IQueueResponse } from "./IJobResult";
|
|
3
|
+
export interface IHybridHttpQueue {
|
|
4
|
+
request(options: {
|
|
5
|
+
method: string;
|
|
6
|
+
url: string;
|
|
7
|
+
body?: any;
|
|
8
|
+
headers?: Record<string, string>;
|
|
9
|
+
queueOptions?: {
|
|
10
|
+
connectionId: string;
|
|
11
|
+
connectionProvider: string;
|
|
12
|
+
microservice: string;
|
|
13
|
+
};
|
|
14
|
+
}): Promise<IQueueResponse>;
|
|
15
|
+
handleRequest(url: string, method: string, options: HttpCallOption): Promise<IQueueResponse>;
|
|
16
|
+
shutdown(): Promise<void>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface IJobResult {
|
|
2
|
+
result?: any;
|
|
3
|
+
error?: string;
|
|
4
|
+
resolved: boolean;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
}
|
|
7
|
+
export interface IQueueResponse {
|
|
8
|
+
success: boolean;
|
|
9
|
+
data?: any;
|
|
10
|
+
error?: string;
|
|
11
|
+
queued?: boolean;
|
|
12
|
+
estimatedProcessingTime?: number;
|
|
13
|
+
jobId?: string;
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
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("./IHybridHttpQueue"), exports);
|
|
18
|
+
__exportStar(require("./IHttpRequestJob"), exports);
|
|
19
|
+
__exportStar(require("./IJobResult"), exports);
|
|
20
|
+
__exportStar(require("./IRateLimitConfig"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { IHybridHttpQueue, IQueueResponse } from "../interfaces";
|
|
2
|
+
import { HttpCallOption } from "../types/http.types";
|
|
3
|
+
export declare class QueueService implements IHybridHttpQueue {
|
|
4
|
+
private readonly hybridQueue;
|
|
5
|
+
constructor();
|
|
6
|
+
request(options: {
|
|
7
|
+
method: string;
|
|
8
|
+
url: string;
|
|
9
|
+
body?: any;
|
|
10
|
+
headers?: Record<string, string>;
|
|
11
|
+
queueOptions?: {
|
|
12
|
+
connectionId: string;
|
|
13
|
+
connectionProvider: string;
|
|
14
|
+
microservice: string;
|
|
15
|
+
};
|
|
16
|
+
}): Promise<IQueueResponse>;
|
|
17
|
+
handleRequest(url: string, method: string, options: HttpCallOption): Promise<IQueueResponse>;
|
|
18
|
+
shutdown(): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
3
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
4
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
5
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
6
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
7
|
+
var _, done = false;
|
|
8
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
9
|
+
var context = {};
|
|
10
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
11
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
12
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
13
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
14
|
+
if (kind === "accessor") {
|
|
15
|
+
if (result === void 0) continue;
|
|
16
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
17
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
18
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
19
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
20
|
+
}
|
|
21
|
+
else if (_ = accept(result)) {
|
|
22
|
+
if (kind === "field") initializers.unshift(_);
|
|
23
|
+
else descriptor[key] = _;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
27
|
+
done = true;
|
|
28
|
+
};
|
|
29
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
30
|
+
var useValue = arguments.length > 2;
|
|
31
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
32
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
33
|
+
}
|
|
34
|
+
return useValue ? value : void 0;
|
|
35
|
+
};
|
|
36
|
+
var __setFunctionName = (this && this.__setFunctionName) || function (f, name, prefix) {
|
|
37
|
+
if (typeof name === "symbol") name = name.description ? "[".concat(name.description, "]") : "";
|
|
38
|
+
return Object.defineProperty(f, "name", { configurable: true, value: prefix ? "".concat(prefix, " ", name) : name });
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.QueueService = void 0;
|
|
42
|
+
const typedi_1 = require("typedi");
|
|
43
|
+
const HybridHttpQueue_1 = require("../entities/HybridHttpQueue");
|
|
44
|
+
let QueueService = (() => {
|
|
45
|
+
let _classDecorators = [(0, typedi_1.Service)()];
|
|
46
|
+
let _classDescriptor;
|
|
47
|
+
let _classExtraInitializers = [];
|
|
48
|
+
let _classThis;
|
|
49
|
+
var QueueService = _classThis = class {
|
|
50
|
+
constructor() {
|
|
51
|
+
this.hybridQueue = new HybridHttpQueue_1.HybridHttpQueue();
|
|
52
|
+
}
|
|
53
|
+
async request(options) {
|
|
54
|
+
return this.hybridQueue.request(options);
|
|
55
|
+
}
|
|
56
|
+
async handleRequest(url, method, options) {
|
|
57
|
+
return this.hybridQueue.handleRequest(url, method, options);
|
|
58
|
+
}
|
|
59
|
+
async shutdown() {
|
|
60
|
+
return this.hybridQueue.shutdown();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
__setFunctionName(_classThis, "QueueService");
|
|
64
|
+
(() => {
|
|
65
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
66
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
67
|
+
QueueService = _classThis = _classDescriptor.value;
|
|
68
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
69
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
70
|
+
})();
|
|
71
|
+
return QueueService = _classThis;
|
|
72
|
+
})();
|
|
73
|
+
exports.QueueService = QueueService;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./QueueService";
|
|
@@ -0,0 +1,17 @@
|
|
|
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("./QueueService"), exports);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface HttpCallOption {
|
|
2
|
+
headers?: Record<string, string>;
|
|
3
|
+
body?: any;
|
|
4
|
+
params?: Record<string, any>;
|
|
5
|
+
queueOptions?: {
|
|
6
|
+
connectionId: string;
|
|
7
|
+
connectionProvider: string;
|
|
8
|
+
microservice: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface HttpRequestOptions {
|
|
12
|
+
method: string;
|
|
13
|
+
url: string;
|
|
14
|
+
body?: any;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
queueOptions?: {
|
|
17
|
+
connectionId: string;
|
|
18
|
+
connectionProvider: string;
|
|
19
|
+
microservice: string;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -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("./http.types"), exports);
|
|
18
|
+
__exportStar(require("./queue.types"), exports);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface QueueConfig {
|
|
2
|
+
concurrency: number;
|
|
3
|
+
removeOnComplete: {
|
|
4
|
+
age: number;
|
|
5
|
+
count: number;
|
|
6
|
+
};
|
|
7
|
+
removeOnFail: {
|
|
8
|
+
age: number;
|
|
9
|
+
count: number;
|
|
10
|
+
};
|
|
11
|
+
lockDuration: number;
|
|
12
|
+
stalledInterval: number;
|
|
13
|
+
}
|
|
14
|
+
export interface JobOptions {
|
|
15
|
+
attempts: number;
|
|
16
|
+
removeOnComplete: {
|
|
17
|
+
age: number;
|
|
18
|
+
count: number;
|
|
19
|
+
};
|
|
20
|
+
removeOnFail: {
|
|
21
|
+
age: number;
|
|
22
|
+
count: number;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface QueueMetrics {
|
|
26
|
+
totalJobs: number;
|
|
27
|
+
completedJobs: number;
|
|
28
|
+
failedJobs: number;
|
|
29
|
+
pendingJobs: number;
|
|
30
|
+
activeJobs: number;
|
|
31
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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("./rateLimit.utils"), exports);
|
|
18
|
+
__exportStar(require("./jobUtils"), exports);
|
|
19
|
+
__exportStar(require("./queueUtils"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Job } from "bullmq";
|
|
2
|
+
import { IJobResult } from "../interfaces";
|
|
3
|
+
export declare class JobUtils {
|
|
4
|
+
static waitForJobCompletion(job: Job, queueKey: string, jobResults: Map<string, IJobResult>): Promise<any>;
|
|
5
|
+
static extractConnectionDetails(options: any): {
|
|
6
|
+
connectionId: string;
|
|
7
|
+
provider: string;
|
|
8
|
+
microservice: string;
|
|
9
|
+
};
|
|
10
|
+
}
|