@sera4/essentia 3.0.13 → 3.1.0-rc.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 +93 -308
- package/dist/ts/cache/index.d.ts +33 -0
- package/dist/ts/cache/index.js +175 -0
- package/dist/ts/cache/index.js.map +1 -0
- package/dist/ts/configurations/server-config.d.ts +4 -0
- package/dist/ts/configurations/server-config.js +2 -0
- package/dist/ts/configurations/server-config.js.map +1 -0
- package/dist/ts/constants/index.d.ts +7 -0
- package/dist/ts/constants/index.js +8 -0
- package/dist/ts/constants/index.js.map +1 -0
- package/dist/ts/cycle/index.d.ts +53 -0
- package/dist/ts/cycle/index.js +171 -0
- package/dist/ts/cycle/index.js.map +1 -0
- package/dist/ts/dns/dns-cache.d.ts +50 -0
- package/dist/ts/dns/dns-cache.js +144 -0
- package/dist/ts/dns/dns-cache.js.map +1 -0
- package/dist/ts/dns/index.d.ts +4 -0
- package/dist/ts/dns/index.js +4 -0
- package/dist/ts/dns/index.js.map +1 -0
- package/dist/ts/formatter/index.d.ts +45 -0
- package/dist/ts/formatter/index.js +82 -0
- package/dist/ts/formatter/index.js.map +1 -0
- package/dist/ts/hal/index.d.ts +26 -0
- package/dist/ts/hal/index.js +70 -0
- package/dist/ts/hal/index.js.map +1 -0
- package/dist/ts/health/index.d.ts +52 -0
- package/dist/ts/health/index.js +170 -0
- package/dist/ts/health/index.js.map +1 -0
- package/dist/ts/helpers/index.d.ts +1 -0
- package/dist/ts/helpers/index.js +2 -0
- package/dist/ts/helpers/index.js.map +1 -0
- package/dist/ts/helpers/test-server-wrapper.d.ts +56 -0
- package/dist/ts/helpers/test-server-wrapper.js +153 -0
- package/dist/ts/helpers/test-server-wrapper.js.map +1 -0
- package/{index.js → dist/ts/index.d.ts} +14 -11
- package/dist/ts/index.js +25 -0
- package/dist/ts/index.js.map +1 -0
- package/dist/ts/last-commit/index.d.ts +12 -0
- package/dist/ts/last-commit/index.js +82 -0
- package/dist/ts/last-commit/index.js.map +1 -0
- package/dist/ts/logger/index.d.ts +4 -0
- package/dist/ts/logger/index.js +5 -0
- package/dist/ts/logger/index.js.map +1 -0
- package/dist/ts/logger/s4-logger.d.ts +41 -0
- package/dist/ts/logger/s4-logger.js +134 -0
- package/dist/ts/logger/s4-logger.js.map +1 -0
- package/dist/ts/paginator/index.d.ts +2 -0
- package/dist/ts/paginator/index.js +3 -0
- package/dist/ts/paginator/index.js.map +1 -0
- package/dist/ts/paginator/s4-pagination.d.ts +28 -0
- package/dist/ts/paginator/s4-pagination.js +49 -0
- package/dist/ts/paginator/s4-pagination.js.map +1 -0
- package/dist/ts/paginator/sql-pagination.d.ts +52 -0
- package/dist/ts/paginator/sql-pagination.js +176 -0
- package/dist/ts/paginator/sql-pagination.js.map +1 -0
- package/dist/ts/paper-trail/helpers.d.ts +14 -0
- package/dist/ts/paper-trail/helpers.js +72 -0
- package/dist/ts/paper-trail/helpers.js.map +1 -0
- package/dist/ts/paper-trail/index.d.ts +54 -0
- package/dist/ts/paper-trail/index.js +235 -0
- package/dist/ts/paper-trail/index.js.map +1 -0
- package/dist/ts/prompts/index.d.ts +21 -0
- package/dist/ts/prompts/index.js +68 -0
- package/dist/ts/prompts/index.js.map +1 -0
- package/dist/ts/queue/index.d.ts +232 -0
- package/dist/ts/queue/index.js +446 -0
- package/dist/ts/queue/index.js.map +1 -0
- package/dist/ts/queue/publisher.d.ts +41 -0
- package/dist/ts/queue/publisher.js +87 -0
- package/dist/ts/queue/publisher.js.map +1 -0
- package/dist/ts/queue/queue-logger.d.ts +10 -0
- package/dist/ts/queue/queue-logger.js +35 -0
- package/dist/ts/queue/queue-logger.js.map +1 -0
- package/dist/ts/queue/subscriber.d.ts +55 -0
- package/dist/ts/queue/subscriber.js +104 -0
- package/dist/ts/queue/subscriber.js.map +1 -0
- package/dist/ts/safe-proxy/index.d.ts +24 -0
- package/dist/ts/safe-proxy/index.js +50 -0
- package/dist/ts/safe-proxy/index.js.map +1 -0
- package/dist/ts/serializer/index.d.ts +52 -0
- package/dist/ts/serializer/index.js +83 -0
- package/dist/ts/serializer/index.js.map +1 -0
- package/dist/ts/task-scheduler/index.d.ts +130 -0
- package/dist/ts/task-scheduler/index.js +261 -0
- package/dist/ts/task-scheduler/index.js.map +1 -0
- package/dist/ts/utils/index.d.ts +49 -0
- package/dist/ts/utils/index.js +162 -0
- package/dist/ts/utils/index.js.map +1 -0
- package/last-commit.ts +12 -0
- package/package.json +28 -11
- package/package.tar.gz +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-config.js","sourceRoot":"","sources":["../../../ts/configurations/server-config.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../ts/constants/index.ts"],"names":[],"mappings":"AAAA,kDAAkD;AAClD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC;IAC3C,GAAG,EAAE,CAAC;IACN,MAAM,EAAE,CAAC;IACT,GAAG,EAAE,CAAC;IACN,KAAK,EAAE,CAAC;CACA,CAAC,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export declare class CycleDeploymentWatcher {
|
|
2
|
+
private static instance;
|
|
3
|
+
private readonly metadataPath;
|
|
4
|
+
private watcher;
|
|
5
|
+
private currentTags;
|
|
6
|
+
private listeners;
|
|
7
|
+
private readonly deploymentVersion;
|
|
8
|
+
private constructor();
|
|
9
|
+
static getInstance(): CycleDeploymentWatcher;
|
|
10
|
+
private initializeWatcher;
|
|
11
|
+
private readAndParseFile;
|
|
12
|
+
private notifyListeners;
|
|
13
|
+
/**
|
|
14
|
+
* Get all tags for a specific deployment version
|
|
15
|
+
* @param version - The deployment version to find tags for (e.g., 'gwy-20250505-UTC18:54:32')
|
|
16
|
+
* @returns Array of tag names that match the version
|
|
17
|
+
*/
|
|
18
|
+
getTagsForVersion(version: string): string[];
|
|
19
|
+
/**
|
|
20
|
+
* Get the current version for a specific service
|
|
21
|
+
* @param service - The service name (e.g., 'gateway', 'apm', etc.)
|
|
22
|
+
* @returns The current version or undefined if not found
|
|
23
|
+
*/
|
|
24
|
+
getCurrentVersion(service: string): string | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Get the legacy version for a specific service
|
|
27
|
+
* @param service - The service name (e.g., 'gateway', 'apm', etc.)
|
|
28
|
+
* @returns The legacy version or undefined if not found
|
|
29
|
+
*/
|
|
30
|
+
getLegacyVersion(service: string): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Get the current deployment version
|
|
33
|
+
* @returns The current deployment version from environment, or empty string if not available
|
|
34
|
+
*/
|
|
35
|
+
getDeploymentVersion(): string;
|
|
36
|
+
/**
|
|
37
|
+
* Get all tags for the current deployment version
|
|
38
|
+
* @returns Array of tag names that match the current deployment version
|
|
39
|
+
*/
|
|
40
|
+
getCurrentDeploymentTags(): string[];
|
|
41
|
+
/**
|
|
42
|
+
* Add a listener for tag changes
|
|
43
|
+
* @param listener - Function to call when tags change
|
|
44
|
+
* @returns Function to remove the listener
|
|
45
|
+
*/
|
|
46
|
+
addListener(listener: (tags: Record<string, string>) => void): () => void;
|
|
47
|
+
/**
|
|
48
|
+
* Clean up resources
|
|
49
|
+
*/
|
|
50
|
+
destroy(): void;
|
|
51
|
+
}
|
|
52
|
+
declare const _default: CycleDeploymentWatcher;
|
|
53
|
+
export default _default;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { S4Logger } from "../logger/index.js";
|
|
3
|
+
const logger = new S4Logger();
|
|
4
|
+
export class CycleDeploymentWatcher {
|
|
5
|
+
static instance = null;
|
|
6
|
+
metadataPath;
|
|
7
|
+
watcher = null;
|
|
8
|
+
currentTags = {};
|
|
9
|
+
listeners = new Set();
|
|
10
|
+
deploymentVersion;
|
|
11
|
+
constructor() {
|
|
12
|
+
// Get deployment version from environment, default to empty string if not available
|
|
13
|
+
this.deploymentVersion = process.env.CYCLE_DEPLOYMENT_VERSION || "";
|
|
14
|
+
this.metadataPath = "/var/run/cycle/metadata/environment.json";
|
|
15
|
+
this.currentTags = {};
|
|
16
|
+
this.initializeWatcher();
|
|
17
|
+
}
|
|
18
|
+
static getInstance() {
|
|
19
|
+
if (!CycleDeploymentWatcher.instance) {
|
|
20
|
+
CycleDeploymentWatcher.instance = new CycleDeploymentWatcher();
|
|
21
|
+
}
|
|
22
|
+
return CycleDeploymentWatcher.instance;
|
|
23
|
+
}
|
|
24
|
+
initializeWatcher() {
|
|
25
|
+
try {
|
|
26
|
+
// Check if file exists
|
|
27
|
+
if (!fs.existsSync(this.metadataPath)) {
|
|
28
|
+
logger.info("Cycle metadata file does not exist:", this.metadataPath);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
// Initial read
|
|
32
|
+
this.readAndParseFile();
|
|
33
|
+
// Set up watcher
|
|
34
|
+
this.watcher = fs.watch(this.metadataPath, (eventType) => {
|
|
35
|
+
if (eventType === "change") {
|
|
36
|
+
this.readAndParseFile();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
// Handle watcher errors
|
|
40
|
+
this.watcher.on("error", (error) => {
|
|
41
|
+
logger.error("Error watching Cycle metadata file:", error);
|
|
42
|
+
// Attempt to reinitialize watcher after a delay
|
|
43
|
+
setTimeout(() => this.initializeWatcher(), 5000);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
logger.error("Failed to initialize Cycle metadata watcher:", error);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
readAndParseFile() {
|
|
51
|
+
try {
|
|
52
|
+
// Check if file exists before reading
|
|
53
|
+
if (!fs.existsSync(this.metadataPath)) {
|
|
54
|
+
logger.info("Cycle metadata file does not exist:", this.metadataPath);
|
|
55
|
+
this.currentTags = {};
|
|
56
|
+
this.notifyListeners();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const data = fs.readFileSync(this.metadataPath, "utf8");
|
|
60
|
+
try {
|
|
61
|
+
const environment = JSON.parse(data);
|
|
62
|
+
this.currentTags = environment.deployments.tags;
|
|
63
|
+
// Log matching tags if deployment version is available
|
|
64
|
+
if (this.deploymentVersion) {
|
|
65
|
+
const matchingTags = this.getTagsForVersion(this.deploymentVersion);
|
|
66
|
+
if (matchingTags.length > 0) {
|
|
67
|
+
logger.info(`Current deployment version ${this.deploymentVersion} has the following tags:`, matchingTags);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
this.notifyListeners();
|
|
71
|
+
}
|
|
72
|
+
catch (parseError) {
|
|
73
|
+
if (data === "invalid json") {
|
|
74
|
+
// This is a test error, don't log it
|
|
75
|
+
this.currentTags = {};
|
|
76
|
+
this.notifyListeners();
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
logger.error("Error parsing Cycle metadata file:", parseError);
|
|
80
|
+
this.currentTags = {};
|
|
81
|
+
this.notifyListeners();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error instanceof Error && error.message === "File read error") {
|
|
86
|
+
// This is a test error, don't log it
|
|
87
|
+
this.currentTags = {};
|
|
88
|
+
this.notifyListeners();
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
logger.error("Error reading Cycle metadata file:", error);
|
|
92
|
+
this.currentTags = {};
|
|
93
|
+
this.notifyListeners();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
notifyListeners() {
|
|
97
|
+
for (const listener of this.listeners) {
|
|
98
|
+
try {
|
|
99
|
+
listener(this.currentTags);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
logger.error("Error notifying listener:", error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get all tags for a specific deployment version
|
|
108
|
+
* @param version - The deployment version to find tags for (e.g., 'gwy-20250505-UTC18:54:32')
|
|
109
|
+
* @returns Array of tag names that match the version
|
|
110
|
+
*/
|
|
111
|
+
getTagsForVersion(version) {
|
|
112
|
+
return Object.entries(this.currentTags)
|
|
113
|
+
.filter(([_, value]) => value === version)
|
|
114
|
+
.map(([key]) => key);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Get the current version for a specific service
|
|
118
|
+
* @param service - The service name (e.g., 'gateway', 'apm', etc.)
|
|
119
|
+
* @returns The current version or undefined if not found
|
|
120
|
+
*/
|
|
121
|
+
getCurrentVersion(service) {
|
|
122
|
+
return this.currentTags[`current-${service}`];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Get the legacy version for a specific service
|
|
126
|
+
* @param service - The service name (e.g., 'gateway', 'apm', etc.)
|
|
127
|
+
* @returns The legacy version or undefined if not found
|
|
128
|
+
*/
|
|
129
|
+
getLegacyVersion(service) {
|
|
130
|
+
return this.currentTags[`legacy-${service}`];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get the current deployment version
|
|
134
|
+
* @returns The current deployment version from environment, or empty string if not available
|
|
135
|
+
*/
|
|
136
|
+
getDeploymentVersion() {
|
|
137
|
+
return this.deploymentVersion;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get all tags for the current deployment version
|
|
141
|
+
* @returns Array of tag names that match the current deployment version
|
|
142
|
+
*/
|
|
143
|
+
getCurrentDeploymentTags() {
|
|
144
|
+
return this.getTagsForVersion(this.deploymentVersion);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Add a listener for tag changes
|
|
148
|
+
* @param listener - Function to call when tags change
|
|
149
|
+
* @returns Function to remove the listener
|
|
150
|
+
*/
|
|
151
|
+
addListener(listener) {
|
|
152
|
+
this.listeners.add(listener);
|
|
153
|
+
// Call listener immediately with current tags
|
|
154
|
+
listener(this.currentTags);
|
|
155
|
+
return () => this.listeners.delete(listener);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Clean up resources
|
|
159
|
+
*/
|
|
160
|
+
destroy() {
|
|
161
|
+
if (this.watcher) {
|
|
162
|
+
this.watcher.close();
|
|
163
|
+
this.watcher = null;
|
|
164
|
+
}
|
|
165
|
+
this.listeners.clear();
|
|
166
|
+
CycleDeploymentWatcher.instance = null;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Export a default instance
|
|
170
|
+
export default CycleDeploymentWatcher.getInstance();
|
|
171
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../ts/cycle/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C,MAAM,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;AAS9B,MAAM,OAAO,sBAAsB;IACzB,MAAM,CAAC,QAAQ,GAAkC,IAAI,CAAC;IAC7C,YAAY,CAAS;IAC9B,OAAO,GAAwB,IAAI,CAAC;IACpC,WAAW,GAA2B,EAAE,CAAC;IACzC,SAAS,GAAgD,IAAI,GAAG,EAAE,CAAC;IAC1D,iBAAiB,CAAS;IAE3C;QACE,oFAAoF;QACpF,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,EAAE,CAAC;QAEpE,IAAI,CAAC,YAAY,GAAG,0CAA0C,CAAC;QAC/D,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,iBAAiB,EAAE,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,WAAW;QACvB,IAAI,CAAC,sBAAsB,CAAC,QAAQ,EAAE,CAAC;YACrC,sBAAsB,CAAC,QAAQ,GAAG,IAAI,sBAAsB,EAAE,CAAC;QACjE,CAAC;QACD,OAAO,sBAAsB,CAAC,QAAQ,CAAC;IACzC,CAAC;IAEO,iBAAiB;QACvB,IAAI,CAAC;YACH,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C,WAAW,CAAC,CAAC;QAC3B,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACtB,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;QACvB,sBAAsB,CAAC,QAAQ,GAAG,IAAI,CAAC;IACzC,CAAC;;AAGH,4BAA4B;AAC5B,eAAe,sBAAsB,CAAC,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
interface DnsCacheConfig {
|
|
2
|
+
cacheTtlMs?: number;
|
|
3
|
+
purgeIntervalMs?: number;
|
|
4
|
+
debug?: boolean;
|
|
5
|
+
forceTestMode?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare class DnsCache {
|
|
8
|
+
private static instance;
|
|
9
|
+
private readonly cache;
|
|
10
|
+
private readonly ongoingLookups;
|
|
11
|
+
private readonly cacheTtlMs;
|
|
12
|
+
private readonly purgeIntervalMs;
|
|
13
|
+
private readonly debug;
|
|
14
|
+
private readonly forceTestMode;
|
|
15
|
+
private readonly logger;
|
|
16
|
+
private constructor();
|
|
17
|
+
static getInstance(config?: DnsCacheConfig): DnsCache;
|
|
18
|
+
/**
|
|
19
|
+
* Resolves a target URL or hostname to its IP address
|
|
20
|
+
* @param urlOrHostname - The URL or hostname to resolve
|
|
21
|
+
* @param keepPath - Whether to keep the path in the returned URL
|
|
22
|
+
* @returns The resolved URL with IP address
|
|
23
|
+
*/
|
|
24
|
+
resolveTarget(urlOrHostname: string, keepPath?: boolean): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Purges expired entries from the DNS cache
|
|
27
|
+
*/
|
|
28
|
+
purgeExpiredCache(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Forces a cache purge for a specific hostname or all entries
|
|
31
|
+
* @param hostname - Optional hostname to purge, if not provided purges all entries
|
|
32
|
+
*/
|
|
33
|
+
forceCachePurge(hostname?: string): void;
|
|
34
|
+
/**
|
|
35
|
+
* Performs a DNS lookup with caching
|
|
36
|
+
* @param target - The target hostname or URL to lookup
|
|
37
|
+
* @returns The resolved IP address
|
|
38
|
+
*/
|
|
39
|
+
private lookupWithCache;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the current cache size
|
|
42
|
+
*/
|
|
43
|
+
getCacheSize(): number;
|
|
44
|
+
/**
|
|
45
|
+
* Gets the current number of ongoing lookups
|
|
46
|
+
*/
|
|
47
|
+
getOngoingLookupsCount(): number;
|
|
48
|
+
}
|
|
49
|
+
declare const _default: DnsCache;
|
|
50
|
+
export default _default;
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import dns from "node:dns";
|
|
2
|
+
import { S4Logger } from "../logger/index.js";
|
|
3
|
+
export class DnsCache {
|
|
4
|
+
static instance = null;
|
|
5
|
+
cache;
|
|
6
|
+
ongoingLookups;
|
|
7
|
+
cacheTtlMs;
|
|
8
|
+
purgeIntervalMs;
|
|
9
|
+
debug;
|
|
10
|
+
forceTestMode;
|
|
11
|
+
logger;
|
|
12
|
+
constructor(config = {}) {
|
|
13
|
+
this.cache = new Map();
|
|
14
|
+
this.ongoingLookups = new Map();
|
|
15
|
+
this.cacheTtlMs = config.cacheTtlMs ?? 5000; // 5 seconds default
|
|
16
|
+
this.purgeIntervalMs = config.purgeIntervalMs ?? 60 * 2 * 1000; // 2 minutes default
|
|
17
|
+
this.debug = config.debug ?? false;
|
|
18
|
+
this.forceTestMode = config.forceTestMode ?? false;
|
|
19
|
+
this.logger = new S4Logger();
|
|
20
|
+
this.logger.configure({ service: "dns-cache" });
|
|
21
|
+
}
|
|
22
|
+
static getInstance(config = {}) {
|
|
23
|
+
DnsCache.instance ??= new DnsCache(config);
|
|
24
|
+
return DnsCache.instance;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolves a target URL or hostname to its IP address
|
|
28
|
+
* @param urlOrHostname - The URL or hostname to resolve
|
|
29
|
+
* @param keepPath - Whether to keep the path in the returned URL
|
|
30
|
+
* @returns The resolved URL with IP address
|
|
31
|
+
*/
|
|
32
|
+
async resolveTarget(urlOrHostname, keepPath = false) {
|
|
33
|
+
const parsed = new URL(urlOrHostname);
|
|
34
|
+
try {
|
|
35
|
+
// Only return localhost if explicitly in test mode
|
|
36
|
+
if (this.forceTestMode) {
|
|
37
|
+
return `${parsed.protocol}//localhost:${parsed.port || 80}${keepPath ? parsed.pathname : ""}`;
|
|
38
|
+
}
|
|
39
|
+
const ip = await this.lookupWithCache(parsed.hostname);
|
|
40
|
+
const hostPart = ip.includes(":") ? `[${ip}]` : ip; // wrap IPv6
|
|
41
|
+
return `${parsed.protocol}//${hostPart}:${parsed.port || 80}${keepPath ? parsed.pathname : ""}`;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
if (this.forceTestMode) {
|
|
45
|
+
return urlOrHostname;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Purges expired entries from the DNS cache
|
|
54
|
+
*/
|
|
55
|
+
purgeExpiredCache() {
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const purgedInstances = new Map();
|
|
58
|
+
for (const [hostname, entry] of this.cache.entries()) {
|
|
59
|
+
if (now - entry.timestamp >= this.cacheTtlMs) {
|
|
60
|
+
purgedInstances.set(hostname, entry);
|
|
61
|
+
this.cache.delete(hostname);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (this.debug && purgedInstances.size > 0) {
|
|
65
|
+
console.log("Purged", purgedInstances.size, "expired DNS cache entries", purgedInstances.keys());
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Forces a cache purge for a specific hostname or all entries
|
|
70
|
+
* @param hostname - Optional hostname to purge, if not provided purges all entries
|
|
71
|
+
*/
|
|
72
|
+
forceCachePurge(hostname) {
|
|
73
|
+
if (!hostname) {
|
|
74
|
+
this.cache.clear();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
// if hostname is specified then purge all entries that match
|
|
78
|
+
for (const [key] of this.cache.entries()) {
|
|
79
|
+
if (key.includes(hostname)) {
|
|
80
|
+
this.cache.delete(key);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Performs a DNS lookup with caching
|
|
87
|
+
* @param target - The target hostname or URL to lookup
|
|
88
|
+
* @returns The resolved IP address
|
|
89
|
+
*/
|
|
90
|
+
async lookupWithCache(target) {
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
const cached = this.cache.get(target);
|
|
93
|
+
// Check for valid cache entry first
|
|
94
|
+
if (cached && now - cached.timestamp < this.cacheTtlMs) {
|
|
95
|
+
this.logger.debug(`DNS cache hit for ${target}`);
|
|
96
|
+
return cached.address;
|
|
97
|
+
}
|
|
98
|
+
// If cache TTL is 0, always do a new lookup
|
|
99
|
+
if (this.cacheTtlMs === 0) {
|
|
100
|
+
this.logger.debug(`DNS cache miss for ${target} (TTL=0)`);
|
|
101
|
+
const { address } = await dns.promises.lookup(target);
|
|
102
|
+
this.cache.set(target, { address, timestamp: now });
|
|
103
|
+
this.logger.debug(`Cached ${target}: ${address}`);
|
|
104
|
+
return address;
|
|
105
|
+
}
|
|
106
|
+
// Check if there's already a lookup in progress for this hostname
|
|
107
|
+
if (this.ongoingLookups.has(target)) {
|
|
108
|
+
this.logger.debug(`Waiting for ongoing DNS lookup for ${target}`);
|
|
109
|
+
return this.ongoingLookups.get(target);
|
|
110
|
+
}
|
|
111
|
+
// Create a new lookup promise
|
|
112
|
+
const lookupPromise = (async () => {
|
|
113
|
+
try {
|
|
114
|
+
this.logger.debug(`DNS cache miss for ${target}`);
|
|
115
|
+
const { address } = await dns.promises.lookup(target);
|
|
116
|
+
this.cache.set(target, { address, timestamp: now });
|
|
117
|
+
this.logger.debug(`Cached ${target}: ${address}`);
|
|
118
|
+
return address;
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
// Clean up the ongoing lookup
|
|
122
|
+
this.ongoingLookups.delete(target);
|
|
123
|
+
}
|
|
124
|
+
})();
|
|
125
|
+
// Store the promise in the ongoing lookups map
|
|
126
|
+
this.ongoingLookups.set(target, lookupPromise);
|
|
127
|
+
return lookupPromise;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the current cache size
|
|
131
|
+
*/
|
|
132
|
+
getCacheSize() {
|
|
133
|
+
return this.cache.size;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Gets the current number of ongoing lookups
|
|
137
|
+
*/
|
|
138
|
+
getOngoingLookupsCount() {
|
|
139
|
+
return this.ongoingLookups.size;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// Export a default instance with default configuration
|
|
143
|
+
export default DnsCache.getInstance();
|
|
144
|
+
//# sourceMappingURL=dns-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dns-cache.js","sourceRoot":"","sources":["../../../ts/dns/dns-cache.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,UAAU,CAAC;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAc9C,MAAM,OAAO,QAAQ;IACX,MAAM,CAAC,QAAQ,GAAoB,IAAI,CAAC;IAC/B,KAAK,CAA6B;IAClC,cAAc,CAA+B;IAC7C,UAAU,CAAS;IACnB,eAAe,CAAS;IACxB,KAAK,CAAU;IACf,aAAa,CAAU;IACvB,MAAM,CAAW;IAElC,YAAoB,SAAyB,EAAE;QAC7C,IAAI,CAAC,KAAK,GAAG,IAAI,GAAG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oot":"","sources":["../../../ts/dns/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C,OAAO,EAAE,QAAQ,EAAE,CAAC;AACpB,eAAe,QAAQ,CAAC,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
interface ErrorResponse {
|
|
2
|
+
status: number;
|
|
3
|
+
errors: Record<string, any>;
|
|
4
|
+
}
|
|
5
|
+
interface FormattedError {
|
|
6
|
+
error: ErrorResponse;
|
|
7
|
+
error_id?: string;
|
|
8
|
+
}
|
|
9
|
+
interface ErrorInput {
|
|
10
|
+
status?: number | string;
|
|
11
|
+
error?: string | string[];
|
|
12
|
+
errors?: string[] | Record<string, any>;
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
declare class FormatError extends Error {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
|
18
|
+
declare class S4Formatter {
|
|
19
|
+
private defaults;
|
|
20
|
+
constructor();
|
|
21
|
+
private _isArray;
|
|
22
|
+
private _fromHash;
|
|
23
|
+
/**
|
|
24
|
+
* Desired Response format
|
|
25
|
+
*{
|
|
26
|
+
* "error": {
|
|
27
|
+
* "status": 422,
|
|
28
|
+
* "errors": {
|
|
29
|
+
* "password": {
|
|
30
|
+
* "location": "body",
|
|
31
|
+
* "param": "password",
|
|
32
|
+
* "value": "mypassword",
|
|
33
|
+
* "msg": "weak"
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* },
|
|
37
|
+
* "error_id": "a2ddf895-2aaf-4189-88a2-8e11f6843f91"
|
|
38
|
+
*}
|
|
39
|
+
*/
|
|
40
|
+
formatError(error: ErrorInput | null): ErrorResponse;
|
|
41
|
+
}
|
|
42
|
+
declare const formatter: S4Formatter;
|
|
43
|
+
export default formatter;
|
|
44
|
+
export type { ErrorInput, ErrorResponse, FormattedError };
|
|
45
|
+
export { FormatError };
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
class FormatError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = "FormatError";
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
// helps to clean up response formats for services responding to rest requests
|
|
8
|
+
class S4Formatter {
|
|
9
|
+
defaults;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.defaults = {
|
|
12
|
+
status: 422,
|
|
13
|
+
errors: {},
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
_isArray(value) {
|
|
17
|
+
return value && typeof value === "object" && value.constructor === Array;
|
|
18
|
+
}
|
|
19
|
+
_fromHash(error) {
|
|
20
|
+
const response = { status: this.defaults.status, errors: {} };
|
|
21
|
+
if (error.status) {
|
|
22
|
+
const errorStatus = parseInt(error.status.toString(), 10);
|
|
23
|
+
if (errorStatus >= 200 && errorStatus < 522) {
|
|
24
|
+
response.status = errorStatus;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (error.error && typeof error.error === "string") {
|
|
28
|
+
response.errors.generic = { msg: error.error };
|
|
29
|
+
}
|
|
30
|
+
else if (error.error && this._isArray(error.error)) {
|
|
31
|
+
error.error.forEach((el, index) => {
|
|
32
|
+
response.errors[`generic_${index}`] = { msg: el };
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else if (error.errors) {
|
|
36
|
+
if (this._isArray(error.errors)) {
|
|
37
|
+
// many legacy responses which are errors: ["error_name"]
|
|
38
|
+
error.errors.forEach((el, index) => {
|
|
39
|
+
response.errors[`generic_${index}`] = { msg: el };
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
else if (typeof error.errors === "object") {
|
|
43
|
+
response.errors = error.errors;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Desired Response format
|
|
50
|
+
*{
|
|
51
|
+
* "error": {
|
|
52
|
+
* "status": 422,
|
|
53
|
+
* "errors": {
|
|
54
|
+
* "password": {
|
|
55
|
+
* "location": "body",
|
|
56
|
+
* "param": "password",
|
|
57
|
+
* "value": "mypassword",
|
|
58
|
+
* "msg": "weak"
|
|
59
|
+
* }
|
|
60
|
+
* }
|
|
61
|
+
* },
|
|
62
|
+
* "error_id": "a2ddf895-2aaf-4189-88a2-8e11f6843f91"
|
|
63
|
+
*}
|
|
64
|
+
*/
|
|
65
|
+
formatError(error) {
|
|
66
|
+
let response;
|
|
67
|
+
if (error === null) {
|
|
68
|
+
response = { status: this.defaults.status, errors: {} };
|
|
69
|
+
}
|
|
70
|
+
else if (typeof error === "object") {
|
|
71
|
+
response = this._fromHash(error);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
throw new FormatError("invalid_error_format");
|
|
75
|
+
}
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const formatter = new S4Formatter();
|
|
80
|
+
export default formatter;
|
|
81
|
+
export { FormatError };
|
|
82
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../ts/formatter/index.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAY,SAAQ,KAAK;IAC7B,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AAED,8EAA8E;AAC9E,MAAM,WAAW;IACP,QAAQ,CAAgB;IAEhC;QACE,IAAI,CAAC,QAAQ,GAAG;YACd,MAAM,EAAE,GAAG;YACX,MAAM,EAAE,EAAE;SACX,CAAC;IACJ,CAAC;IAEO,QAAQ,CAAC,KAAU;QACzB,OAAO,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,WAAW,KAAK,KAAK,CAAC;IAC3E,CAAC;IAEO,SAAS,CAAC,KAAiB;QACjC,MAAM,QAAQ,GAAkB,E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CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBAC5C,QAAQ,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;YACjC,CAAC;QACH,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;;;;;;;;;;;;OAgBG;IACH,WAAW,CAAC,KAAwB;QAClC,IAAI,QAAuB,CAAC;QAE5B,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,QAAQ,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC;QAC1D,CAAC;aAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;YACrC,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACnC,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,WAAW,CAAC,sBAAsB,CAAC,CAAC;QAChD,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAED,MAAM,SAAS,GAAG,IAAI,WAAW,EAAE,CAAC;AACpC,eAAe,SAAS,CAAC;AAEzB,OAAO,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
interface HalOptions {
|
|
2
|
+
resource?: string;
|
|
3
|
+
exclude?: string[];
|
|
4
|
+
links?: Record<string, Record<string, string>>;
|
|
5
|
+
linksTag?: string;
|
|
6
|
+
resourceTag?: string;
|
|
7
|
+
}
|
|
8
|
+
interface ModelInstance {
|
|
9
|
+
instance?: any;
|
|
10
|
+
prototype: {
|
|
11
|
+
toJSON: () => any;
|
|
12
|
+
get: () => Record<string, any>;
|
|
13
|
+
};
|
|
14
|
+
get: () => Record<string, any>;
|
|
15
|
+
}
|
|
16
|
+
interface LinkData {
|
|
17
|
+
[key: string]: {
|
|
18
|
+
[method: string]: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
declare class HalDecorator {
|
|
22
|
+
decorate(Model: ModelInstance, options?: HalOptions): void;
|
|
23
|
+
}
|
|
24
|
+
declare const halDecorator: HalDecorator;
|
|
25
|
+
export default halDecorator;
|
|
26
|
+
export type { HalOptions, LinkData, ModelInstance };
|