@dignetwork/dig-sdk 0.0.1-alpha.18 → 0.0.1-alpha.181

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.
Files changed (91) hide show
  1. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +10 -1
  2. package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +1 -1
  3. package/dist/DataIntegrityTree/DataIntegrityTree.js +77 -10
  4. package/dist/DigNetwork/ContentServer.d.ts +22 -5
  5. package/dist/DigNetwork/ContentServer.d.ts.map +1 -1
  6. package/dist/DigNetwork/ContentServer.js +226 -28
  7. package/dist/DigNetwork/DigNetwork.d.ts +15 -9
  8. package/dist/DigNetwork/DigNetwork.d.ts.map +1 -1
  9. package/dist/DigNetwork/DigNetwork.js +129 -189
  10. package/dist/DigNetwork/DigPeer.d.ts +8 -3
  11. package/dist/DigNetwork/DigPeer.d.ts.map +1 -1
  12. package/dist/DigNetwork/DigPeer.js +72 -115
  13. package/dist/DigNetwork/IncentiveServer.d.ts.map +1 -1
  14. package/dist/DigNetwork/IncentiveServer.js +6 -5
  15. package/dist/DigNetwork/PropagationServer.d.ts +89 -33
  16. package/dist/DigNetwork/PropagationServer.d.ts.map +1 -1
  17. package/dist/DigNetwork/PropagationServer.js +625 -358
  18. package/dist/blockchain/DataStore.d.ts +25 -7
  19. package/dist/blockchain/DataStore.d.ts.map +1 -1
  20. package/dist/blockchain/DataStore.js +117 -159
  21. package/dist/blockchain/DataStoreSerializer.d.ts +1 -1
  22. package/dist/blockchain/DataStoreSerializer.d.ts.map +1 -1
  23. package/dist/blockchain/FullNodePeer.d.ts +95 -10
  24. package/dist/blockchain/FullNodePeer.d.ts.map +1 -1
  25. package/dist/blockchain/FullNodePeer.js +325 -115
  26. package/dist/blockchain/ServerCoin.d.ts +13 -5
  27. package/dist/blockchain/ServerCoin.d.ts.map +1 -1
  28. package/dist/blockchain/ServerCoin.js +89 -28
  29. package/dist/blockchain/StoreMonitorRegistry.d.ts +85 -0
  30. package/dist/blockchain/StoreMonitorRegistry.d.ts.map +1 -0
  31. package/dist/blockchain/StoreMonitorRegistry.js +238 -0
  32. package/dist/blockchain/Wallet.d.ts +2 -2
  33. package/dist/blockchain/Wallet.d.ts.map +1 -1
  34. package/dist/blockchain/Wallet.js +50 -18
  35. package/dist/blockchain/coins.d.ts +1 -2
  36. package/dist/blockchain/coins.d.ts.map +1 -1
  37. package/dist/blockchain/coins.js +1 -55
  38. package/dist/blockchain/index.d.ts +1 -0
  39. package/dist/blockchain/index.d.ts.map +1 -1
  40. package/dist/blockchain/index.js +1 -0
  41. package/dist/types.d.ts +2 -0
  42. package/dist/types.d.ts.map +1 -1
  43. package/dist/utils/ContentScanner.d.ts +63 -0
  44. package/dist/utils/ContentScanner.d.ts.map +1 -0
  45. package/dist/utils/ContentScanner.js +175 -0
  46. package/dist/utils/DigCache.d.ts +38 -0
  47. package/dist/utils/DigCache.d.ts.map +1 -0
  48. package/dist/utils/DigCache.js +236 -0
  49. package/dist/utils/Environment.d.ts +21 -0
  50. package/dist/utils/Environment.d.ts.map +1 -0
  51. package/dist/utils/Environment.js +93 -0
  52. package/dist/utils/FileCache.d.ts +1 -1
  53. package/dist/utils/FileCache.d.ts.map +1 -1
  54. package/dist/utils/FileCache.js +2 -2
  55. package/dist/utils/FileTransfer.d.ts +47 -0
  56. package/dist/utils/FileTransfer.d.ts.map +1 -0
  57. package/dist/utils/FileTransfer.js +209 -0
  58. package/dist/utils/NconfManager.d.ts.map +1 -1
  59. package/dist/utils/NconfManager.js +16 -7
  60. package/dist/utils/PeerRanker.d.ts +55 -0
  61. package/dist/utils/PeerRanker.d.ts.map +1 -0
  62. package/dist/utils/PeerRanker.js +186 -0
  63. package/dist/utils/StoreArchiveManager.d.ts +45 -0
  64. package/dist/utils/StoreArchiveManager.d.ts.map +1 -0
  65. package/dist/utils/StoreArchiveManager.js +153 -0
  66. package/dist/utils/Udi.d.ts +28 -0
  67. package/dist/utils/Udi.d.ts.map +1 -0
  68. package/dist/utils/Udi.js +145 -0
  69. package/dist/utils/config.d.ts +4 -3
  70. package/dist/utils/config.d.ts.map +1 -1
  71. package/dist/utils/config.js +13 -8
  72. package/dist/utils/directoryUtils.d.ts +16 -0
  73. package/dist/utils/directoryUtils.d.ts.map +1 -1
  74. package/dist/utils/directoryUtils.js +66 -0
  75. package/dist/utils/index.d.ts +5 -1
  76. package/dist/utils/index.d.ts.map +1 -1
  77. package/dist/utils/index.js +5 -1
  78. package/dist/utils/merkle.d.ts +2 -0
  79. package/dist/utils/merkle.d.ts.map +1 -0
  80. package/dist/utils/merkle.js +28 -0
  81. package/dist/utils/network.d.ts +2 -1
  82. package/dist/utils/network.d.ts.map +1 -1
  83. package/dist/utils/network.js +40 -15
  84. package/dist/utils/promiseUtils.d.ts +27 -0
  85. package/dist/utils/promiseUtils.d.ts.map +1 -0
  86. package/dist/utils/promiseUtils.js +69 -0
  87. package/dist/utils/ssl.js +2 -2
  88. package/package.json +31 -13
  89. package/dist/utils/deltaUtils.d.ts +0 -2
  90. package/dist/utils/deltaUtils.d.ts.map +0 -1
  91. package/dist/utils/deltaUtils.js +0 -83
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DigCache = void 0;
7
+ // src/cache/DigCache.ts
8
+ const node_cache_1 = __importDefault(require("node-cache"));
9
+ const redis_1 = require("redis");
10
+ class DigCache {
11
+ /**
12
+ * Constructor with the same signature as NodeCache.
13
+ * @param options - NodeCache options
14
+ */
15
+ constructor(options) {
16
+ // Determine whether to use Redis based on the environment variable
17
+ this.useRedis = process.env.USE_REDIS === 'true';
18
+ if (this.useRedis) {
19
+ // Ensure REDIS_URL is provided
20
+ const redisUrl = process.env.REDIS_URL;
21
+ if (!redisUrl) {
22
+ throw new Error("REDIS_URL environment variable is not set.");
23
+ }
24
+ const client = (0, redis_1.createClient)({ url: redisUrl });
25
+ client.on("error", (err) => console.error("Redis Client Error", err));
26
+ // Initialize connection in the initialize method
27
+ this.cache = client;
28
+ }
29
+ else {
30
+ // Initialize NodeCache with provided options
31
+ this.cache = new node_cache_1.default(options);
32
+ }
33
+ }
34
+ /**
35
+ * Initializes the Redis connection if Redis is being used.
36
+ * Call this method before performing any cache operations.
37
+ */
38
+ async initialize() {
39
+ if (this.useRedis) {
40
+ try {
41
+ await this.cache.connect();
42
+ console.log("Connected to Redis successfully.");
43
+ }
44
+ catch (error) {
45
+ console.error("Failed to connect to Redis:", error);
46
+ throw error;
47
+ }
48
+ }
49
+ }
50
+ /**
51
+ * Gracefully disconnects the Redis client if Redis is being used.
52
+ * Call this method when your application is shutting down.
53
+ */
54
+ async disconnect() {
55
+ if (this.useRedis) {
56
+ try {
57
+ await this.cache.quit();
58
+ console.log("Disconnected from Redis successfully.");
59
+ }
60
+ catch (error) {
61
+ console.error("Error disconnecting Redis:", error);
62
+ }
63
+ }
64
+ }
65
+ async set(key, value, ttl) {
66
+ if (this.useRedis) {
67
+ try {
68
+ const serializedValue = JSON.stringify(value);
69
+ if (ttl !== undefined) {
70
+ // Redis TTL is in seconds
71
+ const result = await this.cache.set(key, serializedValue, { EX: ttl });
72
+ return result === "OK";
73
+ }
74
+ else {
75
+ const result = await this.cache.set(key, serializedValue);
76
+ return result === "OK";
77
+ }
78
+ }
79
+ catch (error) {
80
+ console.error(`Redis set error for key "${key}":`, error);
81
+ return false;
82
+ }
83
+ }
84
+ else {
85
+ try {
86
+ const result = this.cache.set(key, value, ttl ?? 0);
87
+ return result;
88
+ }
89
+ catch (error) {
90
+ console.error(`NodeCache set error for key "${key}":`, error);
91
+ return false;
92
+ }
93
+ }
94
+ }
95
+ async get(key) {
96
+ if (this.useRedis) {
97
+ try {
98
+ const value = await this.cache.get(key);
99
+ if (value === null) {
100
+ return undefined;
101
+ }
102
+ return JSON.parse(value);
103
+ }
104
+ catch (error) {
105
+ console.error(`Redis get error for key "${key}":`, error);
106
+ return undefined;
107
+ }
108
+ }
109
+ else {
110
+ try {
111
+ const value = this.cache.get(key);
112
+ return value;
113
+ }
114
+ catch (error) {
115
+ console.error(`NodeCache get error for key "${key}":`, error);
116
+ return undefined;
117
+ }
118
+ }
119
+ }
120
+ async del(key) {
121
+ if (this.useRedis) {
122
+ try {
123
+ const result = await this.cache.del(key);
124
+ return result;
125
+ }
126
+ catch (error) {
127
+ console.error(`Redis del error for key "${key}":`, error);
128
+ return 0;
129
+ }
130
+ }
131
+ else {
132
+ try {
133
+ const result = this.cache.del(key);
134
+ return result;
135
+ }
136
+ catch (error) {
137
+ console.error(`NodeCache del error for key "${key}":`, error);
138
+ return 0;
139
+ }
140
+ }
141
+ }
142
+ async has(key) {
143
+ if (this.useRedis) {
144
+ try {
145
+ const result = await this.cache.exists(key);
146
+ return result > 0;
147
+ }
148
+ catch (error) {
149
+ console.error(`Redis exists error for key "${key}":`, error);
150
+ return false;
151
+ }
152
+ }
153
+ else {
154
+ try {
155
+ const result = this.cache.has(key);
156
+ return result;
157
+ }
158
+ catch (error) {
159
+ console.error(`NodeCache has error for key "${key}":`, error);
160
+ return false;
161
+ }
162
+ }
163
+ }
164
+ async flushAll() {
165
+ if (this.useRedis) {
166
+ try {
167
+ await this.cache.flushAll();
168
+ return true;
169
+ }
170
+ catch (error) {
171
+ console.error("Redis flushAll error:", error);
172
+ return false;
173
+ }
174
+ }
175
+ else {
176
+ try {
177
+ this.cache.flushAll();
178
+ return true;
179
+ }
180
+ catch (error) {
181
+ console.error("NodeCache flushAll error:", error);
182
+ return false;
183
+ }
184
+ }
185
+ }
186
+ async keys(pattern = "*") {
187
+ if (this.useRedis) {
188
+ try {
189
+ const result = await this.cache.keys(pattern);
190
+ return result;
191
+ }
192
+ catch (error) {
193
+ console.error(`Redis keys error with pattern "${pattern}":`, error);
194
+ return [];
195
+ }
196
+ }
197
+ else {
198
+ try {
199
+ const result = this.cache.keys();
200
+ return result;
201
+ }
202
+ catch (error) {
203
+ return [];
204
+ }
205
+ }
206
+ }
207
+ async ttl(key, ttl) {
208
+ if (this.useRedis) {
209
+ try {
210
+ if (ttl === undefined || ttl === 0) {
211
+ return (await this.cache.del(key)) > 0;
212
+ }
213
+ else {
214
+ return await this.cache.expire(key, ttl);
215
+ }
216
+ }
217
+ catch (error) {
218
+ console.error(`Redis ttl error for key "${key}":`, error);
219
+ return false;
220
+ }
221
+ }
222
+ else {
223
+ try {
224
+ if (ttl === undefined || ttl === 0) {
225
+ return this.cache.del(key) > 0;
226
+ }
227
+ return this.cache.ttl(key, ttl);
228
+ }
229
+ catch (error) {
230
+ console.error(`NodeCache ttl error for key "${key}":`, error);
231
+ return false;
232
+ }
233
+ }
234
+ }
235
+ }
236
+ exports.DigCache = DigCache;
@@ -0,0 +1,21 @@
1
+ export declare class Environment {
2
+ private static cliMode;
3
+ private static isValidIp;
4
+ private static isValidHostnameOrIp;
5
+ private static isValidPort;
6
+ private static isValidFilePath;
7
+ private static getNumberEnvVar;
8
+ static get DIG_USERNAME(): string | undefined;
9
+ static get DIG_PASSWORD(): string | undefined;
10
+ static get TRUSTED_FULLNODE(): string | undefined;
11
+ static get TRUSTED_FULLNODE_PORT(): number | undefined;
12
+ static get PUBLIC_IP(): string | undefined;
13
+ static get DISK_SPACE_LIMIT_BYTES(): number | undefined;
14
+ static get MERCENARY_MODE(): boolean | undefined;
15
+ static get DEBUG(): boolean | undefined;
16
+ static get DIG_FOLDER_PATH(): string | undefined;
17
+ static get REMOTE_NODE(): boolean | undefined;
18
+ static set CLI_MODE(mode: boolean);
19
+ static get CLI_MODE(): boolean | undefined;
20
+ }
21
+ //# sourceMappingURL=Environment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Environment.d.ts","sourceRoot":"","sources":["../../src/utils/Environment.ts"],"names":[],"mappings":"AAGA,qBAAa,WAAW;IACtB,OAAO,CAAC,MAAM,CAAC,OAAO,CAAkB;IAGxC,OAAO,CAAC,MAAM,CAAC,SAAS;IAOxB,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAUlC,OAAO,CAAC,MAAM,CAAC,WAAW;IAK1B,OAAO,CAAC,MAAM,CAAC,eAAe;IAK9B,OAAO,CAAC,MAAM,CAAC,eAAe;IAO9B,MAAM,KAAK,YAAY,IAAI,MAAM,GAAG,SAAS,CAG5C;IAGD,MAAM,KAAK,YAAY,IAAI,MAAM,GAAG,SAAS,CAG5C;IAGD,MAAM,KAAK,gBAAgB,IAAI,MAAM,GAAG,SAAS,CAGhD;IAGD,MAAM,KAAK,qBAAqB,IAAI,MAAM,GAAG,SAAS,CAGrD;IAGD,MAAM,KAAK,SAAS,IAAI,MAAM,GAAG,SAAS,CAGzC;IAGD,MAAM,KAAK,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAEtD;IAGD,MAAM,KAAK,cAAc,IAAI,OAAO,GAAG,SAAS,CAG/C;IAGD,MAAM,KAAK,KAAK,IAAI,OAAO,GAAG,SAAS,CAGtC;IAGD,MAAM,KAAK,eAAe,IAAI,MAAM,GAAG,SAAS,CAG/C;IAGD,MAAM,KAAK,WAAW,IAAI,OAAO,GAAG,SAAS,CAG5C;IAED,MAAM,KAAK,QAAQ,CAAC,IAAI,EAAE,OAAO,EAEhC;IAED,MAAM,KAAK,QAAQ,IAAI,OAAO,GAAG,SAAS,CAEzC;CACF"}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Environment = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const path_1 = __importDefault(require("path"));
9
+ class Environment {
10
+ // Helper to validate if a string is a valid IP address (IPv4)
11
+ static isValidIp(ip) {
12
+ const ipPattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
13
+ return ipPattern.test(ip);
14
+ }
15
+ // Helper to validate if a string is a valid hostname or IP address
16
+ static isValidHostnameOrIp(hostname) {
17
+ // Hostname regex (simple, allows subdomains but not special characters)
18
+ const hostnamePattern = /^(([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.)*([a-zA-Z0-9](-*[a-zA-Z0-9])*)\.?$/;
19
+ const ipv6Pattern = /^(([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}|:)|(([0-9a-fA-F]{1,4}:){1,7}|:):(([0-9a-fA-F]{1,4}:){1,6}|:):([0-9a-fA-F]{1,4}|:):([0-9a-fA-F]{1,4}|:)|::)$/;
20
+ return this.isValidIp(hostname) || ipv6Pattern.test(hostname) || hostnamePattern.test(hostname);
21
+ }
22
+ // Helper to validate if a number is a valid port (between 1 and 65535)
23
+ static isValidPort(port) {
24
+ return port > 0 && port <= 65535;
25
+ }
26
+ // Helper to validate if a string is a valid file path
27
+ static isValidFilePath(filePath) {
28
+ return fs_1.default.existsSync(filePath) && path_1.default.isAbsolute(filePath);
29
+ }
30
+ // Helper to get and validate number env variables
31
+ static getNumberEnvVar(envVar) {
32
+ const value = process.env[envVar];
33
+ const parsedValue = Number(value);
34
+ return !isNaN(parsedValue) ? parsedValue : undefined;
35
+ }
36
+ // Static getter for DIG_USERNAME (string, non-empty)
37
+ static get DIG_USERNAME() {
38
+ const value = process.env["DIG_USERNAME"];
39
+ return value && value.trim().length > 0 ? value : undefined;
40
+ }
41
+ // Static getter for DIG_PASSWORD (string, non-empty)
42
+ static get DIG_PASSWORD() {
43
+ const value = process.env["DIG_PASSWORD"];
44
+ return value && value.trim().length > 0 ? value : undefined;
45
+ }
46
+ // Static getter for TRUSTED_FULLNODE (valid IP or hostname)
47
+ static get TRUSTED_FULLNODE() {
48
+ const value = process.env["TRUSTED_FULLNODE"];
49
+ return value && this.isValidHostnameOrIp(value) ? value : undefined;
50
+ }
51
+ // Static getter for TRUSTED_FULLNODE_PORT (valid port)
52
+ static get TRUSTED_FULLNODE_PORT() {
53
+ const port = this.getNumberEnvVar("TRUSTED_FULLNODE_PORT");
54
+ return port && this.isValidPort(port) ? port : undefined;
55
+ }
56
+ // Static getter for PUBLIC_IP (valid IP)
57
+ static get PUBLIC_IP() {
58
+ const value = process.env["PUBLIC_IP"];
59
+ return value;
60
+ }
61
+ // Static getter for DISK_SPACE_LIMIT_BYTES (number, optional)
62
+ static get DISK_SPACE_LIMIT_BYTES() {
63
+ return this.getNumberEnvVar("DISK_SPACE_LIMIT_BYTES");
64
+ }
65
+ // Static getter for MERCENARY_MODE (boolean: "true" or "false")
66
+ static get MERCENARY_MODE() {
67
+ const value = process.env["MERCENARY_MODE"];
68
+ return value === "true" ? true : value === "false" ? false : undefined;
69
+ }
70
+ // Static getter for MERCENARY_MODE (boolean: "true" or "false")
71
+ static get DEBUG() {
72
+ const value = process.env["DIG_DEBUG"];
73
+ return value === "true" ? true : value === "false" ? false : undefined;
74
+ }
75
+ // Static getter for DIG_FOLDER_PATH (valid file path)
76
+ static get DIG_FOLDER_PATH() {
77
+ const value = process.env["DIG_FOLDER_PATH"];
78
+ return value && this.isValidFilePath(value) ? value : undefined;
79
+ }
80
+ // Static getter for REMOTE_NODE (boolean: "1" or "0", mapped to true/false)
81
+ static get REMOTE_NODE() {
82
+ const value = process.env["REMOTE_NODE"];
83
+ return value === "1" ? true : value === "0" ? false : undefined;
84
+ }
85
+ static set CLI_MODE(mode) {
86
+ this.cliMode = mode;
87
+ }
88
+ static get CLI_MODE() {
89
+ return Environment.cliMode;
90
+ }
91
+ }
92
+ exports.Environment = Environment;
93
+ Environment.cliMode = false;
@@ -1,6 +1,6 @@
1
1
  export declare class FileCache<T> {
2
2
  private cacheDir;
3
- constructor(relativeFilePath: string);
3
+ constructor(relativeFilePath: string, baseDir?: string);
4
4
  private ensureDirectoryExists;
5
5
  private getCacheFilePath;
6
6
  get(key: string): T | null;
@@ -1 +1 @@
1
- {"version":3,"file":"FileCache.d.ts","sourceRoot":"","sources":["../../src/utils/FileCache.ts"],"names":[],"mappings":"AAIA,qBAAa,SAAS,CAAC,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAS;gBAEb,gBAAgB,EAAE,MAAM;IAMpC,OAAO,CAAC,qBAAqB;IAO7B,OAAO,CAAC,gBAAgB;IAKjB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAY1B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI;IAO/B,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IASzB,aAAa,IAAI,MAAM,EAAE;CAUjC"}
1
+ {"version":3,"file":"FileCache.d.ts","sourceRoot":"","sources":["../../src/utils/FileCache.ts"],"names":[],"mappings":"AAIA,qBAAa,SAAS,CAAC,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAS;gBAEb,gBAAgB,EAAE,MAAM,EAAE,OAAO,GAAE,MAAwB;IAMvE,OAAO,CAAC,qBAAqB;IAO7B,OAAO,CAAC,gBAAgB;IAKjB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,IAAI;IAY1B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI;IAO/B,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IASzB,aAAa,IAAI,MAAM,EAAE;CAUjC"}
@@ -8,8 +8,8 @@ const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const config_1 = require("./config");
10
10
  class FileCache {
11
- constructor(relativeFilePath) {
12
- this.cacheDir = path_1.default.join(config_1.DIG_FOLDER_PATH, relativeFilePath);
11
+ constructor(relativeFilePath, baseDir = config_1.DIG_FOLDER_PATH) {
12
+ this.cacheDir = path_1.default.join(baseDir, relativeFilePath);
13
13
  this.ensureDirectoryExists();
14
14
  }
15
15
  // Ensure the directory exists or create it
@@ -0,0 +1,47 @@
1
+ import { Readable } from "stream";
2
+ export declare class FileTransfer {
3
+ private static certPath;
4
+ private static keyPath;
5
+ constructor();
6
+ /** ------------------------ UPLOAD FUNCTIONALITY ------------------------ **/
7
+ /**
8
+ * Upload a file to the server using a stream.
9
+ * @param filePath - Local path of the file.
10
+ * @param uploadUrl - Server URL to upload the file.
11
+ * @param headers - Additional headers for the request.
12
+ * @returns Promise<void>
13
+ */
14
+ uploadFile(filePath: string, uploadUrl: string, headers: Record<string, string>): Promise<void>;
15
+ /**
16
+ * Perform a POST request.
17
+ * @param url - The URL to POST to.
18
+ * @param data - Data to send.
19
+ * @param headers - Headers for the POST request.
20
+ * @returns Promise<void>
21
+ */
22
+ postRequest(url: string, data: string, headers: Record<string, string>): Promise<void>;
23
+ /** ------------------------ DOWNLOAD FUNCTIONALITY ------------------------ **/
24
+ /**
25
+ * Download data from a URL.
26
+ * @param url - The URL to download data from.
27
+ * @returns Promise<string>
28
+ */
29
+ downloadData(url: string): Promise<string>;
30
+ /**
31
+ * Stream data from a URL.
32
+ * @param url - The URL to stream data from.
33
+ * @returns Promise<Readable>
34
+ */
35
+ streamData(url: string): Promise<Readable>;
36
+ /** ------------------------ HEAD REQUEST FUNCTIONALITY ------------------------ **/
37
+ /**
38
+ * Perform a HEAD request to check if a resource exists on the server.
39
+ * @param url - The URL to check.
40
+ * @returns Promise<{ success: boolean; headers?: Record<string, string> }>
41
+ */
42
+ headRequest(url: string): Promise<{
43
+ success: boolean;
44
+ headers?: Record<string, string>;
45
+ }>;
46
+ }
47
+ //# sourceMappingURL=FileTransfer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileTransfer.d.ts","sourceRoot":"","sources":["../../src/utils/FileTransfer.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAS;IAChC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAS;;IAU/B,8EAA8E;IAE9E;;;;;;OAMG;IACU,UAAU,CACrB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,OAAO,CAAC,IAAI,CAAC;IA0ChB;;;;;;OAMG;IACU,WAAW,CACtB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC9B,OAAO,CAAC,IAAI,CAAC;IAsChB,gFAAgF;IAEhF;;;;OAIG;IACU,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IA0CvD;;;;OAIG;IACU,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAuCvD,oFAAoF;IAEpF;;;;OAIG;IACU,WAAW,CACtB,GAAG,EAAE,MAAM,GACV,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,CAAC;CA8BnE"}
@@ -0,0 +1,209 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FileTransfer = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const https_1 = __importDefault(require("https"));
9
+ const url_1 = require("url");
10
+ const ssl_1 = require("../utils/ssl");
11
+ class FileTransfer {
12
+ constructor() {
13
+ if (!FileTransfer.certPath || !FileTransfer.keyPath) {
14
+ const { certPath, keyPath } = (0, ssl_1.getOrCreateSSLCerts)();
15
+ FileTransfer.certPath = certPath;
16
+ FileTransfer.keyPath = keyPath;
17
+ }
18
+ }
19
+ /** ------------------------ UPLOAD FUNCTIONALITY ------------------------ **/
20
+ /**
21
+ * Upload a file to the server using a stream.
22
+ * @param filePath - Local path of the file.
23
+ * @param uploadUrl - Server URL to upload the file.
24
+ * @param headers - Additional headers for the request.
25
+ * @returns Promise<void>
26
+ */
27
+ async uploadFile(filePath, uploadUrl, headers) {
28
+ const fileStream = fs_1.default.createReadStream(filePath);
29
+ const fileSize = fs_1.default.statSync(filePath).size;
30
+ return new Promise((resolve, reject) => {
31
+ const url = new url_1.URL(uploadUrl);
32
+ const options = {
33
+ hostname: url.hostname,
34
+ port: url.port || 443,
35
+ path: url.pathname,
36
+ method: "PUT",
37
+ headers: {
38
+ ...headers,
39
+ "Content-Type": "application/octet-stream",
40
+ "Content-Length": fileSize,
41
+ },
42
+ key: fs_1.default.readFileSync(FileTransfer.keyPath),
43
+ cert: fs_1.default.readFileSync(FileTransfer.certPath),
44
+ rejectUnauthorized: false,
45
+ };
46
+ const req = https_1.default.request(options, (res) => {
47
+ if (res.statusCode === 200) {
48
+ resolve();
49
+ }
50
+ else {
51
+ reject(new Error(`Upload failed with status ${res.statusCode}: ${res.statusMessage}`));
52
+ }
53
+ });
54
+ req.on("error", (err) => reject(err));
55
+ fileStream.pipe(req);
56
+ fileStream.on("error", (err) => reject(err));
57
+ });
58
+ }
59
+ /**
60
+ * Perform a POST request.
61
+ * @param url - The URL to POST to.
62
+ * @param data - Data to send.
63
+ * @param headers - Headers for the POST request.
64
+ * @returns Promise<void>
65
+ */
66
+ async postRequest(url, data, headers) {
67
+ return new Promise((resolve, reject) => {
68
+ const urlObj = new url_1.URL(url);
69
+ const options = {
70
+ hostname: urlObj.hostname,
71
+ port: urlObj.port || 443,
72
+ path: urlObj.pathname,
73
+ method: "POST",
74
+ headers: {
75
+ ...headers,
76
+ "Content-Type": "application/json",
77
+ "Content-Length": Buffer.byteLength(data),
78
+ },
79
+ key: fs_1.default.readFileSync(FileTransfer.keyPath),
80
+ cert: fs_1.default.readFileSync(FileTransfer.certPath),
81
+ rejectUnauthorized: false,
82
+ };
83
+ const req = https_1.default.request(options, (res) => {
84
+ if (res.statusCode === 200) {
85
+ resolve();
86
+ }
87
+ else {
88
+ reject(new Error(`POST request failed with status ${res.statusCode}: ${res.statusMessage}`));
89
+ }
90
+ });
91
+ req.on("error", (err) => reject(err));
92
+ req.write(data);
93
+ req.end();
94
+ });
95
+ }
96
+ /** ------------------------ DOWNLOAD FUNCTIONALITY ------------------------ **/
97
+ /**
98
+ * Download data from a URL.
99
+ * @param url - The URL to download data from.
100
+ * @returns Promise<string>
101
+ */
102
+ async downloadData(url) {
103
+ return new Promise((resolve, reject) => {
104
+ const urlObj = new url_1.URL(url);
105
+ const options = {
106
+ hostname: urlObj.hostname,
107
+ port: urlObj.port || 443,
108
+ path: urlObj.pathname + urlObj.search,
109
+ method: "GET",
110
+ key: fs_1.default.readFileSync(FileTransfer.keyPath),
111
+ cert: fs_1.default.readFileSync(FileTransfer.certPath),
112
+ rejectUnauthorized: false,
113
+ };
114
+ const req = https_1.default.request(options, (res) => {
115
+ let data = "";
116
+ if (res.statusCode === 200) {
117
+ res.on("data", (chunk) => (data += chunk));
118
+ res.on("end", () => resolve(data));
119
+ }
120
+ else if (res.statusCode === 301 || res.statusCode === 302) {
121
+ const redirectUrl = res.headers.location;
122
+ if (redirectUrl) {
123
+ this.downloadData(redirectUrl).then(resolve).catch(reject);
124
+ }
125
+ else {
126
+ reject(new Error("Redirected without a location header"));
127
+ }
128
+ }
129
+ else {
130
+ reject(new Error(`Failed to retrieve data from ${url}. Status code: ${res.statusCode}`));
131
+ }
132
+ });
133
+ req.on("error", (error) => reject(error));
134
+ req.end();
135
+ });
136
+ }
137
+ /**
138
+ * Stream data from a URL.
139
+ * @param url - The URL to stream data from.
140
+ * @returns Promise<Readable>
141
+ */
142
+ async streamData(url) {
143
+ return new Promise((resolve, reject) => {
144
+ const urlObj = new url_1.URL(url);
145
+ const options = {
146
+ hostname: urlObj.hostname,
147
+ port: urlObj.port || 443,
148
+ path: urlObj.pathname + urlObj.search,
149
+ method: "GET",
150
+ key: fs_1.default.readFileSync(FileTransfer.keyPath),
151
+ cert: fs_1.default.readFileSync(FileTransfer.certPath),
152
+ rejectUnauthorized: false,
153
+ };
154
+ const req = https_1.default.request(options, (res) => {
155
+ if (res.statusCode === 200) {
156
+ resolve(res); // Return the readable stream
157
+ }
158
+ else if (res.statusCode === 301 || res.statusCode === 302) {
159
+ const redirectUrl = res.headers.location;
160
+ if (redirectUrl) {
161
+ this.streamData(redirectUrl).then(resolve).catch(reject);
162
+ }
163
+ else {
164
+ reject(new Error("Redirected without a location header"));
165
+ }
166
+ }
167
+ else {
168
+ reject(new Error(`Failed to retrieve stream from ${url}. Status code: ${res.statusCode}`));
169
+ }
170
+ });
171
+ req.on("error", (error) => reject(error));
172
+ req.end();
173
+ });
174
+ }
175
+ /** ------------------------ HEAD REQUEST FUNCTIONALITY ------------------------ **/
176
+ /**
177
+ * Perform a HEAD request to check if a resource exists on the server.
178
+ * @param url - The URL to check.
179
+ * @returns Promise<{ success: boolean; headers?: Record<string, string> }>
180
+ */
181
+ async headRequest(url) {
182
+ return new Promise((resolve, reject) => {
183
+ const urlObj = new url_1.URL(url);
184
+ const options = {
185
+ hostname: urlObj.hostname,
186
+ port: urlObj.port || 443,
187
+ path: urlObj.pathname,
188
+ method: "HEAD",
189
+ key: fs_1.default.readFileSync(FileTransfer.keyPath),
190
+ cert: fs_1.default.readFileSync(FileTransfer.certPath),
191
+ rejectUnauthorized: false,
192
+ };
193
+ const req = https_1.default.request(options, (res) => {
194
+ if (res.statusCode === 200) {
195
+ resolve({
196
+ success: true,
197
+ headers: res.headers,
198
+ });
199
+ }
200
+ else {
201
+ resolve({ success: false });
202
+ }
203
+ });
204
+ req.on("error", (err) => reject(err));
205
+ req.end();
206
+ });
207
+ }
208
+ }
209
+ exports.FileTransfer = FileTransfer;
@@ -1 +1 @@
1
- {"version":3,"file":"NconfManager.d.ts","sourceRoot":"","sources":["../../src/utils/NconfManager.ts"],"names":[],"mappings":"AAQA,qBAAa,YAAY;IACvB,OAAO,CAAC,cAAc,CAAS;gBAEnB,YAAY,EAAE,MAAM;YAKlB,gBAAgB;IAejB,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAMjD,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAStD,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAS7C,YAAY,IAAI,OAAO,CAAC,OAAO,CAAC;IAKhC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAI3D"}
1
+ {"version":3,"file":"NconfManager.d.ts","sourceRoot":"","sources":["../../src/utils/NconfManager.ts"],"names":[],"mappings":"AAUA,qBAAa,YAAY;IACvB,OAAO,CAAC,cAAc,CAAS;gBAEnB,YAAY,EAAE,MAAM;YAKpB,gBAAgB;IAmBf,cAAc,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAMjD,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,IAAI,CAAC;IAStD,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAS7C,YAAY,IAAI,OAAO,CAAC,OAAO,CAAC;IAKhC,aAAa,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAI3D"}