dt-common-device 3.0.10 → 3.0.11
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/dist/config/config.d.ts +1 -2
- package/dist/config/config.js +4 -5
- package/dist/device/local/repository/Schedule.repository.d.ts +0 -1
- package/dist/device/local/repository/Schedule.repository.js +6 -6
- package/dist/queue/entities/HybridHttpQueue.d.ts +4 -14
- package/dist/queue/entities/HybridHttpQueue.js +31 -119
- package/dist/queue/interfaces/IHybridHttpQueue.d.ts +2 -12
- package/dist/queue/interfaces/IJobResult.d.ts +1 -8
- package/dist/queue/interfaces/index.d.ts +0 -1
- package/dist/queue/interfaces/index.js +0 -1
- package/dist/queue/services/QueueService.d.ts +2 -12
- package/dist/queue/types/queue.types.d.ts +10 -29
- package/dist/queue/utils/jobUtils.d.ts +0 -3
- package/dist/queue/utils/jobUtils.js +0 -48
- package/dist/queue/utils/queueUtils.d.ts +7 -0
- package/dist/queue/utils/queueUtils.js +113 -4
- package/package.json +6 -1
- package/.eslintrc.js +0 -44
- package/src/alerts/Alert.model.ts +0 -289
- package/src/alerts/Alert.repository.ts +0 -487
- package/src/alerts/Alert.service.ts +0 -711
- package/src/alerts/AlertBuilder.example.ts +0 -126
- package/src/alerts/AlertBuilder.ts +0 -208
- package/src/alerts/AlertService.example.ts +0 -232
- package/src/alerts/alert.types.ts +0 -64
- package/src/alerts/index.ts +0 -3
- package/src/config/config.ts +0 -202
- package/src/config/config.types.ts +0 -21
- package/src/connection/Connection.repository.ts +0 -52
- package/src/connection/Connection.service.ts +0 -39
- package/src/connection/IConnection.ts +0 -27
- package/src/connection/index.ts +0 -3
- package/src/constants/ConnectionProviders.ts +0 -11
- package/src/constants/Event.ts +0 -89
- package/src/constants/Service.ts +0 -17
- package/src/constants/index.ts +0 -3
- package/src/db/db.ts +0 -24
- package/src/db/index.ts +0 -2
- package/src/db/redis.ts +0 -20
- package/src/device/cloud/entities/CloudDevice.ts +0 -40
- package/src/device/cloud/entities/CloudDeviceService.ts +0 -8
- package/src/device/cloud/entities/DeviceFactory.ts +0 -27
- package/src/device/cloud/entities/index.ts +0 -3
- package/src/device/cloud/interfaces/ICloudDevice.ts +0 -14
- package/src/device/cloud/interfaces/ICloudDeviceService.ts +0 -6
- package/src/device/cloud/interfaces/IDeviceFactory.ts +0 -5
- package/src/device/cloud/interfaces/IRawDataTransformer.ts +0 -5
- package/src/device/cloud/interfaces/IRawDevice.ts +0 -19
- package/src/device/cloud/interfaces/index.ts +0 -5
- package/src/device/local/interfaces/IDevice.ts +0 -62
- package/src/device/local/interfaces/IDtDevice.ts +0 -16
- package/src/device/local/interfaces/ISchedule.ts +0 -40
- package/src/device/local/interfaces/index.ts +0 -3
- package/src/device/local/repository/Device.repository.ts +0 -368
- package/src/device/local/repository/Hub.repository.ts +0 -107
- package/src/device/local/repository/Schedule.repository.ts +0 -72
- package/src/device/local/services/Device.service.ts +0 -436
- package/src/device/local/services/Hub.service.ts +0 -57
- package/src/device/local/services/Schedule.service.ts +0 -26
- package/src/device/local/services/index.ts +0 -3
- package/src/docs/Alert.model.md +0 -319
- package/src/docs/Alerts&IssuesModel.md +0 -312
- package/src/docs/Issue.model.md +0 -386
- package/src/docs/SECURITY.md +0 -67
- package/src/docs/TROUBLESHOOTING.md +0 -184
- package/src/events/BaseEventHandler.ts +0 -145
- package/src/events/BaseEventTransformer.ts +0 -97
- package/src/events/DeviceEventHandler.ts +0 -213
- package/src/events/DeviceEventTransformerFactory.ts +0 -77
- package/src/events/EventHandler.ts +0 -124
- package/src/events/EventHandlerOrchestrator.ts +0 -119
- package/src/events/EventProcessingService.ts +0 -248
- package/src/events/InternalEventSubscription.ts +0 -194
- package/src/events/index.ts +0 -9
- package/src/events/interfaces/DeviceEvent.ts +0 -56
- package/src/events/interfaces/IEventHandler.ts +0 -28
- package/src/events/interfaces/IEventTransformer.ts +0 -8
- package/src/events/interfaces/IInternalEvent.ts +0 -33
- package/src/events/interfaces/index.ts +0 -4
- package/src/index.ts +0 -43
- package/src/issues/Issue.model.ts +0 -350
- package/src/issues/Issue.repository.ts +0 -517
- package/src/issues/Issue.service.ts +0 -932
- package/src/issues/IssueBuilder.example.ts +0 -210
- package/src/issues/IssueBuilder.ts +0 -263
- package/src/issues/IssueService.example.ts +0 -310
- package/src/issues/index.ts +0 -2
- package/src/issues/issue.types.ts +0 -98
- package/src/property/IProperty.ts +0 -30
- package/src/property/Property.repository.ts +0 -53
- package/src/property/Property.service.ts +0 -38
- package/src/property/index.ts +0 -2
- package/src/queue/entities/HybridHttpQueue.ts +0 -274
- package/src/queue/entities/index.ts +0 -1
- package/src/queue/index.ts +0 -6
- package/src/queue/interfaces/IHttpRequestJob.ts +0 -10
- package/src/queue/interfaces/IHybridHttpQueue.ts +0 -25
- package/src/queue/interfaces/IJobResult.ts +0 -15
- package/src/queue/interfaces/IRateLimitConfig.ts +0 -5
- package/src/queue/interfaces/index.ts +0 -4
- package/src/queue/services/QueueService.ts +0 -40
- package/src/queue/services/index.ts +0 -1
- package/src/queue/types/http.types.ts +0 -23
- package/src/queue/types/index.ts +0 -2
- package/src/queue/types/queue.types.ts +0 -21
- package/src/queue/utils/index.ts +0 -3
- package/src/queue/utils/jobUtils.ts +0 -79
- package/src/queue/utils/queueUtils.ts +0 -84
- package/src/queue/utils/rateLimit.utils.ts +0 -131
- package/src/utils/http.utils.ts +0 -143
- package/src/utils/index.ts +0 -2
- package/src/utils/redis.utils.ts +0 -74
- package/tsconfig.json +0 -20
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { IRateLimitConfig } from "../interfaces";
|
|
2
|
-
import { getRedisClient } from "../../db/redis";
|
|
3
|
-
import { getConfig } from "../../config/config";
|
|
4
|
-
|
|
5
|
-
export class RateLimitUtils {
|
|
6
|
-
private static redisClient = getRedisClient();
|
|
7
|
-
|
|
8
|
-
static async checkRateLimit(
|
|
9
|
-
connectionId: string,
|
|
10
|
-
provider: string,
|
|
11
|
-
rateLimitConfigs: Map<string, IRateLimitConfig>
|
|
12
|
-
): Promise<boolean> {
|
|
13
|
-
const config = rateLimitConfigs.get(provider);
|
|
14
|
-
if (!config) {
|
|
15
|
-
getConfig().LOGGER.warn(
|
|
16
|
-
`No rate limit config found for provider: ${provider}`
|
|
17
|
-
);
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const key = `rate_limit:${provider}:${connectionId}`;
|
|
22
|
-
const now = Date.now();
|
|
23
|
-
const windowStart = now - config.windowMs;
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
const data = await this.redisClient.get(key);
|
|
27
|
-
const requests = data
|
|
28
|
-
? JSON.parse(data).filter((t: number) => t > windowStart)
|
|
29
|
-
: [];
|
|
30
|
-
|
|
31
|
-
if (requests.length >= config.maxRequests) return false;
|
|
32
|
-
|
|
33
|
-
requests.push(now);
|
|
34
|
-
await this.redisClient.setex(
|
|
35
|
-
key,
|
|
36
|
-
Math.ceil(config.windowMs / 1000),
|
|
37
|
-
JSON.stringify(requests)
|
|
38
|
-
);
|
|
39
|
-
return true;
|
|
40
|
-
} catch (error) {
|
|
41
|
-
getConfig().LOGGER.error(`Rate limit check error: ${error}`);
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
static initializeRateLimitConfigs(): Map<string, IRateLimitConfig> {
|
|
47
|
-
const configs = new Map<string, IRateLimitConfig>();
|
|
48
|
-
|
|
49
|
-
// Configure rate limits for different providers
|
|
50
|
-
configs.set("Sensibo", {
|
|
51
|
-
maxRequests: 5,
|
|
52
|
-
windowMs: 60000,
|
|
53
|
-
provider: "Sensibo",
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
return configs;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
static async isRateLimitAllowed(
|
|
60
|
-
connectionId: string,
|
|
61
|
-
provider: string,
|
|
62
|
-
rateLimitConfigs: Map<string, IRateLimitConfig>
|
|
63
|
-
): Promise<boolean> {
|
|
64
|
-
const config = rateLimitConfigs.get(provider);
|
|
65
|
-
if (!config) {
|
|
66
|
-
getConfig().LOGGER.warn(
|
|
67
|
-
`No rate limit config found for provider: ${provider}`
|
|
68
|
-
);
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const key = `rate_limit:${provider}:${connectionId}`;
|
|
73
|
-
const now = Date.now();
|
|
74
|
-
const windowStart = now - config.windowMs;
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
const data = await this.redisClient.get(key);
|
|
78
|
-
const requests = data
|
|
79
|
-
? JSON.parse(data).filter((t: number) => t > windowStart)
|
|
80
|
-
: [];
|
|
81
|
-
|
|
82
|
-
return requests.length < config.maxRequests;
|
|
83
|
-
} catch (error) {
|
|
84
|
-
getConfig().LOGGER.error(`Rate limit check error: ${error}`);
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
static async recordRequest(
|
|
89
|
-
connectionId: string,
|
|
90
|
-
provider: string
|
|
91
|
-
): Promise<void> {
|
|
92
|
-
const config = this.getRateLimitConfig(provider);
|
|
93
|
-
if (!config) return;
|
|
94
|
-
|
|
95
|
-
const key = `rate_limit:${provider}:${connectionId}`;
|
|
96
|
-
const now = Date.now();
|
|
97
|
-
const windowStart = now - config.windowMs;
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
const data = await this.redisClient.get(key);
|
|
101
|
-
const requests = data
|
|
102
|
-
? JSON.parse(data).filter((t: number) => t > windowStart)
|
|
103
|
-
: [];
|
|
104
|
-
|
|
105
|
-
requests.push(now);
|
|
106
|
-
await this.redisClient.setex(
|
|
107
|
-
key,
|
|
108
|
-
Math.ceil(config.windowMs / 1000),
|
|
109
|
-
JSON.stringify(requests)
|
|
110
|
-
);
|
|
111
|
-
} catch (error) {
|
|
112
|
-
getConfig().LOGGER.error(`Rate limit record error: ${error}`);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
static async getRawRequestTimestamps(key: string): Promise<number[]> {
|
|
117
|
-
try {
|
|
118
|
-
const data = await this.redisClient.get(key);
|
|
119
|
-
return data ? JSON.parse(data) : [];
|
|
120
|
-
} catch (error) {
|
|
121
|
-
getConfig().LOGGER.error(
|
|
122
|
-
`Error fetching raw request timestamps: ${error}`
|
|
123
|
-
);
|
|
124
|
-
return [];
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
static getRateLimitConfig(provider: string): IRateLimitConfig | undefined {
|
|
129
|
-
return this.initializeRateLimitConfigs().get(provider);
|
|
130
|
-
}
|
|
131
|
-
}
|
package/src/utils/http.utils.ts
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { getConfig } from "../config/config";
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Validates if a URL is properly formatted and accessible
|
|
6
|
-
*/
|
|
7
|
-
export function validateServiceUrl(url: string): boolean {
|
|
8
|
-
try {
|
|
9
|
-
const parsedUrl = new URL(url);
|
|
10
|
-
return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
|
|
11
|
-
} catch (error) {
|
|
12
|
-
getConfig().LOGGER.error(`Invalid service URL: ${url}`, error);
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Creates a properly configured axios instance with error handling
|
|
19
|
-
*/
|
|
20
|
-
export function createAxiosInstance(baseURL?: string) {
|
|
21
|
-
const instance = axios.create({
|
|
22
|
-
baseURL,
|
|
23
|
-
timeout: 30000, // 30 seconds timeout
|
|
24
|
-
maxRedirects: 5,
|
|
25
|
-
validateStatus: (status: number) => status < 500, // Don't throw on 4xx errors
|
|
26
|
-
headers: {
|
|
27
|
-
"Content-Type": "application/json",
|
|
28
|
-
"User-Agent": "dt-common-device/1.3.0",
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
// Add request interceptor for logging
|
|
33
|
-
instance.interceptors.request.use(
|
|
34
|
-
(config: any) => {
|
|
35
|
-
const logger = getConfig().LOGGER;
|
|
36
|
-
logger.info(
|
|
37
|
-
`Making request to: ${config.method?.toUpperCase()} ${config.url}`,
|
|
38
|
-
{
|
|
39
|
-
baseURL: config.baseURL,
|
|
40
|
-
timeout: config.timeout,
|
|
41
|
-
}
|
|
42
|
-
);
|
|
43
|
-
return config;
|
|
44
|
-
},
|
|
45
|
-
(error: any) => {
|
|
46
|
-
getConfig().LOGGER.error("Request interceptor error:", error);
|
|
47
|
-
return Promise.reject(error);
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
// Add response interceptor for error handling
|
|
52
|
-
instance.interceptors.response.use(
|
|
53
|
-
(response: any) => {
|
|
54
|
-
return response;
|
|
55
|
-
},
|
|
56
|
-
(error: any) => {
|
|
57
|
-
const logger = getConfig().LOGGER;
|
|
58
|
-
const errorInfo = {
|
|
59
|
-
url: error.config?.url,
|
|
60
|
-
method: error.config?.method,
|
|
61
|
-
status: error.response?.status,
|
|
62
|
-
statusText: error.response?.statusText,
|
|
63
|
-
message: error.message,
|
|
64
|
-
code: error.code,
|
|
65
|
-
baseURL: error.config?.baseURL,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
logger.error("HTTP request failed:", errorInfo);
|
|
69
|
-
|
|
70
|
-
// Log additional details for network errors
|
|
71
|
-
if (error.code === "ECONNREFUSED" || error.code === "ENOTFOUND") {
|
|
72
|
-
logger.error("Network connectivity issue detected. Please check:", {
|
|
73
|
-
serviceUrl: error.config?.baseURL,
|
|
74
|
-
errorCode: error.code,
|
|
75
|
-
errorMessage: error.message,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return Promise.reject(error);
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
return instance;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Centralized axios instance for device service
|
|
88
|
-
*/
|
|
89
|
-
let deviceServiceAxiosInstance: any = null;
|
|
90
|
-
|
|
91
|
-
export function getDeviceServiceAxiosInstance(): any {
|
|
92
|
-
if (!deviceServiceAxiosInstance) {
|
|
93
|
-
const { DEVICE_SERVICE } = getConfig();
|
|
94
|
-
if (!DEVICE_SERVICE) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
"DEVICE_SERVICE is not configured. Call initialize() first with DEVICE_SERVICE."
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
deviceServiceAxiosInstance = createAxiosInstance(DEVICE_SERVICE);
|
|
100
|
-
}
|
|
101
|
-
return deviceServiceAxiosInstance;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Retry function for failed HTTP requests
|
|
106
|
-
*/
|
|
107
|
-
export async function retryRequest<T>(
|
|
108
|
-
requestFn: () => Promise<T>,
|
|
109
|
-
maxRetries: number = 3,
|
|
110
|
-
delay: number = 1000
|
|
111
|
-
): Promise<T> {
|
|
112
|
-
let lastError: any;
|
|
113
|
-
|
|
114
|
-
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
115
|
-
try {
|
|
116
|
-
return await requestFn();
|
|
117
|
-
} catch (error: any) {
|
|
118
|
-
lastError = error;
|
|
119
|
-
|
|
120
|
-
if (attempt === maxRetries) {
|
|
121
|
-
getConfig().LOGGER.error(
|
|
122
|
-
`Request failed after ${maxRetries} attempts:`,
|
|
123
|
-
error
|
|
124
|
-
);
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
getConfig().LOGGER.warn(
|
|
129
|
-
`Request attempt ${attempt} failed, retrying in ${delay}ms:`,
|
|
130
|
-
{
|
|
131
|
-
error: error.message,
|
|
132
|
-
attempt,
|
|
133
|
-
maxRetries,
|
|
134
|
-
}
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
138
|
-
delay *= 2; // Exponential backoff
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
throw lastError;
|
|
143
|
-
}
|
package/src/utils/index.ts
DELETED
package/src/utils/redis.utils.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { Service } from "typedi";
|
|
2
|
-
import { getRedisClient } from "../db/redis";
|
|
3
|
-
|
|
4
|
-
@Service()
|
|
5
|
-
export class RedisUtils {
|
|
6
|
-
private readonly client = getRedisClient(); // singleton Redis client instance
|
|
7
|
-
|
|
8
|
-
async hget(key: string, field: string): Promise<any> {
|
|
9
|
-
try {
|
|
10
|
-
const value: string | null = await this.client.hget(key, field);
|
|
11
|
-
if (!value) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
return JSON.parse(value);
|
|
15
|
-
} catch (error) {
|
|
16
|
-
console.error(`Error getting value for key ${key}:`, error);
|
|
17
|
-
throw error;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async hset(key: string, field: string, value: string): Promise<number> {
|
|
22
|
-
try {
|
|
23
|
-
return await this.client.hset(key, field, value);
|
|
24
|
-
} catch (error) {
|
|
25
|
-
console.error(`Error setting value for key ${key}:`, error);
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// async del(key: string): Promise<number> {
|
|
31
|
-
// return this.client.del(key);
|
|
32
|
-
// }
|
|
33
|
-
|
|
34
|
-
async hdel(key: string, ...fields: string[]): Promise<number> {
|
|
35
|
-
return await this.client.hdel(key, ...fields);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// async get(key: string): Promise<string | null> {
|
|
39
|
-
// try {
|
|
40
|
-
// return await this.client.get(key);
|
|
41
|
-
// } catch (error) {
|
|
42
|
-
// console.error(`Error getting value for key ${key}:`, error);
|
|
43
|
-
// throw error;
|
|
44
|
-
// }
|
|
45
|
-
// }
|
|
46
|
-
|
|
47
|
-
/* async set(key: string, value: string): Promise<boolean> {
|
|
48
|
-
try {
|
|
49
|
-
await this.client.set(key, value);
|
|
50
|
-
return true;
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error(`Error setting value for key ${key}:`, error);
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
} */
|
|
56
|
-
|
|
57
|
-
async exists(key: string): Promise<number> {
|
|
58
|
-
try {
|
|
59
|
-
return await this.client.exists(key);
|
|
60
|
-
} catch (error) {
|
|
61
|
-
console.error(`Error checking existence for key ${key}:`, error);
|
|
62
|
-
throw error;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
async expire(key: string, seconds: number): Promise<any> {
|
|
67
|
-
try {
|
|
68
|
-
return await this.client.expire(key, seconds);
|
|
69
|
-
} catch (error) {
|
|
70
|
-
console.error(`Error setting expiration for key ${key}:`, error);
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"lib": ["ES2020"],
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"moduleResolution": "node",
|
|
14
|
-
"resolveJsonModule": true,
|
|
15
|
-
"isolatedModules": true,
|
|
16
|
-
"noEmit": false
|
|
17
|
-
},
|
|
18
|
-
"include": ["src/**/*"],
|
|
19
|
-
"exclude": ["node_modules", "dist"]
|
|
20
|
-
}
|