@defillama/sdk 5.0.9 → 5.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/build/Balances.d.ts +5 -0
- package/build/Balances.js +47 -107
- package/build/ChainApi.d.ts +1 -0
- package/build/ChainApi.js +152 -253
- package/build/abi/abi2.js +47 -144
- package/build/abi/convertResults.js +9 -29
- package/build/abi/index.js +220 -326
- package/build/abi/multicall.js +115 -217
- package/build/abi/multicall3.js +59 -123
- package/build/abi/rpcCall.js +43 -106
- package/build/abi/tron.js +153 -283
- package/build/api.js +3 -4
- package/build/computeTVL/humanizeNumber.js +6 -7
- package/build/computeTVL/prices.js +55 -145
- package/build/erc20/index.js +34 -122
- package/build/eth/index.js +37 -107
- package/build/general.js +22 -23
- package/build/generalUtil.js +72 -130
- package/build/index.js +2 -2
- package/build/providers.json +1 -1
- package/build/util/blocks.js +212 -371
- package/build/util/blockscout.js +13 -58
- package/build/util/cache.js +112 -274
- package/build/util/computeTVL.js +70 -150
- package/build/util/cosmos.js +21 -75
- package/build/util/debugLog.js +6 -14
- package/build/util/env.d.ts +1 -1
- package/build/util/env.js +24 -35
- package/build/util/index.js +72 -132
- package/build/util/internal-cache.js +7 -8
- package/build/util/logs.d.ts +1 -2
- package/build/util/logs.js +152 -225
- package/build/util/promisePool.js +11 -62
- package/build/util/r2.js +36 -95
- package/build/util/updateProviderList.js +23 -83
- package/package.json +2 -3
package/build/util/blockscout.js
CHANGED
|
@@ -1,42 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
4
|
};
|
|
38
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
6
|
exports.getBlockscoutBlock = exports.blockscoutEndpoints = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
40
8
|
exports.blockscoutEndpoints = {
|
|
41
9
|
celo: 'https://explorer.celo.org',
|
|
42
10
|
kava: 'https://explorer.kava.io',
|
|
@@ -44,28 +12,15 @@ exports.blockscoutEndpoints = {
|
|
|
44
12
|
base: 'https://base.blockscout.com',
|
|
45
13
|
scroll: 'https://blockscout.scroll.io',
|
|
46
14
|
};
|
|
47
|
-
function getBlockscoutBlock(timestamp, chain) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return [4 /*yield*/, res.json()];
|
|
58
|
-
case 2:
|
|
59
|
-
data = _a.sent();
|
|
60
|
-
if (data.status !== '1')
|
|
61
|
-
throw new Error(data.message);
|
|
62
|
-
return [2 /*return*/, {
|
|
63
|
-
timestamp: timestamp,
|
|
64
|
-
block: +data.result.blockNumber,
|
|
65
|
-
number: +data.result.blockNumber,
|
|
66
|
-
}];
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
});
|
|
15
|
+
async function getBlockscoutBlock(timestamp, chain) {
|
|
16
|
+
const api = `${exports.blockscoutEndpoints[chain]}/api?module=block&action=getblocknobytime×tamp=${timestamp}&closest=before`;
|
|
17
|
+
const { data } = await (0, axios_1.default)(api);
|
|
18
|
+
if (data.status !== '1')
|
|
19
|
+
throw new Error(data.message);
|
|
20
|
+
return {
|
|
21
|
+
timestamp,
|
|
22
|
+
block: +data.result.blockNumber,
|
|
23
|
+
number: +data.result.blockNumber,
|
|
24
|
+
};
|
|
70
25
|
}
|
|
71
26
|
exports.getBlockscoutBlock = getBlockscoutBlock;
|
package/build/util/cache.js
CHANGED
|
@@ -1,252 +1,124 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var _a;
|
|
39
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
3
|
exports.writeExpiringJsonCache = exports.readExpiringJsonCache = exports.deleteCache = exports.getTempLocalCache = exports.compressCache = exports.parseCache = exports.writeCache = exports.readCache = exports.currentVersion = void 0;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
4
|
+
const debugLog_1 = require("./debugLog");
|
|
5
|
+
const r2_1 = require("./r2");
|
|
6
|
+
const zlib_1 = require("zlib");
|
|
7
|
+
const util_1 = require("util");
|
|
8
|
+
const env_1 = require("./env");
|
|
9
|
+
const brotliOptions = {
|
|
10
|
+
[zlib_1.constants.BROTLI_PARAM_MODE]: zlib_1.constants.BROTLI_MODE_TEXT,
|
|
11
|
+
[zlib_1.constants.BROTLI_PARAM_QUALITY]: zlib_1.constants.BROTLI_MAX_QUALITY,
|
|
12
|
+
};
|
|
50
13
|
function zipAsync(data) {
|
|
51
14
|
return (0, util_1.promisify)(zlib_1.brotliCompress)(data, brotliOptions);
|
|
52
15
|
}
|
|
53
16
|
function unzipAsync(data) {
|
|
54
|
-
return (0, util_1.promisify)(zlib_1.brotliDecompress)(data, brotliOptions).then(
|
|
17
|
+
return (0, util_1.promisify)(zlib_1.brotliDecompress)(data, brotliOptions).then(r => r.toString());
|
|
55
18
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
19
|
+
const _fs = require('fs');
|
|
20
|
+
const fs = _fs.promises;
|
|
21
|
+
const path = require('path');
|
|
22
|
+
const foldersCreated = {};
|
|
60
23
|
exports.currentVersion = 'zlib-1.0';
|
|
61
24
|
function getCacheRootFolder() {
|
|
62
|
-
|
|
25
|
+
const defaultCacheRootFolder = path.join(__dirname, 'local_cache');
|
|
63
26
|
return path.join((0, env_1.getEnvCacheFolder)(defaultCacheRootFolder), exports.currentVersion);
|
|
64
27
|
}
|
|
65
28
|
function getFilePath(file) {
|
|
66
29
|
return path.join(getCacheRootFolder(), file);
|
|
67
30
|
}
|
|
68
|
-
function createSubPath(folderPath) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
_a.trys.push([1, 3, , 4]);
|
|
81
|
-
return [4 /*yield*/, fs.mkdir(path.join(datRoot, folderPath), { recursive: true })];
|
|
82
|
-
case 2:
|
|
83
|
-
_a.sent();
|
|
84
|
-
return [3 /*break*/, 4];
|
|
85
|
-
case 3:
|
|
86
|
-
error_1 = _a.sent();
|
|
87
|
-
return [3 /*break*/, 4];
|
|
88
|
-
case 4:
|
|
89
|
-
foldersCreated[folderPath] = true;
|
|
90
|
-
return [2 /*return*/];
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
});
|
|
31
|
+
async function createSubPath(folderPath) {
|
|
32
|
+
const datRoot = getCacheRootFolder();
|
|
33
|
+
folderPath = folderPath.replace(datRoot, '');
|
|
34
|
+
if (foldersCreated[folderPath])
|
|
35
|
+
return;
|
|
36
|
+
try {
|
|
37
|
+
await fs.mkdir(path.join(datRoot, folderPath), { recursive: true });
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
// sdk.log('Error creating folder path:', error)
|
|
41
|
+
}
|
|
42
|
+
foldersCreated[folderPath] = true;
|
|
94
43
|
}
|
|
95
|
-
function readCache(file, options) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return [4 /*yield*/, fs.readFile(filePath)];
|
|
112
|
-
case 2:
|
|
113
|
-
data = _a.sent();
|
|
114
|
-
return [2 /*return*/, data];
|
|
115
|
-
case 3:
|
|
116
|
-
error_3 = _a.sent();
|
|
117
|
-
// debugLog('Error reading cache:', error)
|
|
118
|
-
if (options.skipR2Cache)
|
|
119
|
-
return [2 /*return*/];
|
|
120
|
-
return [4 /*yield*/, (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file)];
|
|
121
|
-
case 4:
|
|
122
|
-
r2Data = _a.sent();
|
|
123
|
-
if (!r2Data) return [3 /*break*/, 6];
|
|
124
|
-
return [4 /*yield*/, writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true })];
|
|
125
|
-
case 5:
|
|
126
|
-
_a.sent();
|
|
127
|
-
return [2 /*return*/, r2Data];
|
|
128
|
-
case 6: return [3 /*break*/, 7];
|
|
129
|
-
case 7: return [2 /*return*/];
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
});
|
|
44
|
+
async function readCache(file, options = {}) {
|
|
45
|
+
try {
|
|
46
|
+
const data = await readFile(file, options);
|
|
47
|
+
return await parseCache(data);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
// debugLog('Error reading cache:', error)
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
async function readFile(file, options = {}) {
|
|
54
|
+
const filePath = getFilePath(file);
|
|
55
|
+
try {
|
|
56
|
+
if (options.readFromR2Cache)
|
|
57
|
+
throw new Error('Read from R2 cache');
|
|
58
|
+
const data = await fs.readFile(filePath);
|
|
59
|
+
return data;
|
|
133
60
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return [4 /*yield*/, parseCache(data)];
|
|
143
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
144
|
-
case 3:
|
|
145
|
-
error_2 = _a.sent();
|
|
146
|
-
// debugLog('Error reading cache:', error)
|
|
147
|
-
return [2 /*return*/, {}];
|
|
148
|
-
case 4: return [2 /*return*/];
|
|
61
|
+
catch (error) {
|
|
62
|
+
// debugLog('Error reading cache:', error)
|
|
63
|
+
if (options.skipR2Cache)
|
|
64
|
+
return;
|
|
65
|
+
const r2Data = await (0, r2_1.getR2JSONString)(exports.currentVersion + '/' + file);
|
|
66
|
+
if (r2Data) {
|
|
67
|
+
await writeCache(file, r2Data, { alreadyCompressed: true, skipR2CacheWrite: true });
|
|
68
|
+
return r2Data;
|
|
149
69
|
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
152
72
|
}
|
|
153
73
|
exports.readCache = readCache;
|
|
154
|
-
function writeCache(file, data, options) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
_b = isSameData;
|
|
176
|
-
_c = [data];
|
|
177
|
-
return [4 /*yield*/, readCache(file, { skipR2Cache: true })];
|
|
178
|
-
case 4:
|
|
179
|
-
if (_b.apply(void 0, _c.concat([_d.sent()])))
|
|
180
|
-
return [2 /*return*/];
|
|
181
|
-
filePath = getFilePath(file);
|
|
182
|
-
return [4 /*yield*/, createSubPath(path.dirname(filePath))];
|
|
183
|
-
case 5:
|
|
184
|
-
_d.sent();
|
|
185
|
-
return [4 /*yield*/, fs.writeFile(filePath, fileData)];
|
|
186
|
-
case 6:
|
|
187
|
-
_d.sent();
|
|
188
|
-
if (!!options.skipR2CacheWrite) return [3 /*break*/, 8];
|
|
189
|
-
return [4 /*yield*/, (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData)];
|
|
190
|
-
case 7:
|
|
191
|
-
_d.sent();
|
|
192
|
-
_d.label = 8;
|
|
193
|
-
case 8: return [2 /*return*/, fileData];
|
|
194
|
-
case 9:
|
|
195
|
-
error_4 = _d.sent();
|
|
196
|
-
(0, debugLog_1.debugLog)('Error writing cache:', error_4);
|
|
197
|
-
return [3 /*break*/, 10];
|
|
198
|
-
case 10: return [2 /*return*/];
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
});
|
|
74
|
+
async function writeCache(file, data, options = {}) {
|
|
75
|
+
try {
|
|
76
|
+
const fileData = options.alreadyCompressed ? data : await compressCache(data);
|
|
77
|
+
if (!data || (typeof data === 'string' && data.length < 20) || fileData.length < 20) {
|
|
78
|
+
(0, debugLog_1.debugLog)('Data too small to cache: ', file);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (isSameData(data, await readCache(file, { skipR2Cache: true })))
|
|
82
|
+
return;
|
|
83
|
+
const filePath = getFilePath(file);
|
|
84
|
+
await createSubPath(path.dirname(filePath));
|
|
85
|
+
await fs.writeFile(filePath, fileData);
|
|
86
|
+
if (!options.skipR2CacheWrite) {
|
|
87
|
+
await (0, r2_1.storeR2JSONString)(exports.currentVersion + '/' + file, fileData);
|
|
88
|
+
}
|
|
89
|
+
return fileData;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
(0, debugLog_1.debugLog)('Error writing cache:', error);
|
|
93
|
+
}
|
|
202
94
|
}
|
|
203
95
|
exports.writeCache = writeCache;
|
|
204
|
-
function parseCache(dataString) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
case 0:
|
|
210
|
-
if (typeof dataString === 'string')
|
|
211
|
-
dataString = Buffer.from(dataString, 'base64');
|
|
212
|
-
return [4 /*yield*/, unzipAsync(dataString)];
|
|
213
|
-
case 1:
|
|
214
|
-
decompressed = _a.sent();
|
|
215
|
-
return [2 /*return*/, JSON.parse(decompressed).llamaWrapped];
|
|
216
|
-
}
|
|
217
|
-
});
|
|
218
|
-
});
|
|
96
|
+
async function parseCache(dataString) {
|
|
97
|
+
if (typeof dataString === 'string')
|
|
98
|
+
dataString = Buffer.from(dataString, 'base64');
|
|
99
|
+
const decompressed = await unzipAsync(dataString);
|
|
100
|
+
return JSON.parse(decompressed).llamaWrapped;
|
|
219
101
|
}
|
|
220
102
|
exports.parseCache = parseCache;
|
|
221
|
-
function compressCache(data) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return __generator(this, function (_a) {
|
|
225
|
-
switch (_a.label) {
|
|
226
|
-
case 0: return [4 /*yield*/, zipAsync(JSON.stringify({ version: exports.currentVersion, llamaWrapped: data }))];
|
|
227
|
-
case 1:
|
|
228
|
-
comressedCache = _a.sent();
|
|
229
|
-
return [2 /*return*/, comressedCache];
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
});
|
|
103
|
+
async function compressCache(data) {
|
|
104
|
+
const comressedCache = await zipAsync(JSON.stringify({ version: exports.currentVersion, llamaWrapped: data }));
|
|
105
|
+
return comressedCache;
|
|
233
106
|
}
|
|
234
107
|
exports.compressCache = compressCache;
|
|
235
108
|
function isSameData(data1, data2) {
|
|
236
109
|
return JSON.stringify(data1) === JSON.stringify(data2);
|
|
237
110
|
}
|
|
238
|
-
|
|
239
|
-
function getTempLocalCache(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
var saveCacheFlag = false;
|
|
111
|
+
const ONE_WEEK = 1000 * 60 * 60 * 24 * 7;
|
|
112
|
+
function getTempLocalCache({ file, defaultData = {}, clearAfter = ONE_WEEK }) {
|
|
113
|
+
let cache = defaultData;
|
|
114
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
115
|
+
let emptyFile = { created: now, data: defaultData };
|
|
116
|
+
let fileData = emptyFile;
|
|
117
|
+
const filePath = getFilePath(file);
|
|
118
|
+
let saveCacheFlag = false;
|
|
247
119
|
createSubPath(path.dirname(filePath)); // create folder if not exists, this is async but we don't care
|
|
248
120
|
try {
|
|
249
|
-
|
|
121
|
+
const data = _fs.readFileSync(filePath);
|
|
250
122
|
fileData = JSON.parse(data.toString());
|
|
251
123
|
if (now - fileData.created > clearAfter)
|
|
252
124
|
fileData = emptyFile; // clear cache
|
|
@@ -272,7 +144,7 @@ function getTempLocalCache(_a) {
|
|
|
272
144
|
}
|
|
273
145
|
function removePromisesAndFunctions(data) {
|
|
274
146
|
if (typeof data === 'object') {
|
|
275
|
-
for (
|
|
147
|
+
for (const key in data) {
|
|
276
148
|
if (typeof data[key] === 'function')
|
|
277
149
|
delete data[key];
|
|
278
150
|
if (data[key] instanceof Promise)
|
|
@@ -285,64 +157,30 @@ function getTempLocalCache(_a) {
|
|
|
285
157
|
}
|
|
286
158
|
exports.getTempLocalCache = getTempLocalCache;
|
|
287
159
|
// this removes data only from the file cache, not from the R2 cache
|
|
288
|
-
function deleteCache(file) {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
return __generator(this, function (_a) {
|
|
292
|
-
switch (_a.label) {
|
|
293
|
-
case 0:
|
|
294
|
-
filePath = getFilePath(file);
|
|
295
|
-
return [4 /*yield*/, fs.unlink(filePath)];
|
|
296
|
-
case 1:
|
|
297
|
-
_a.sent();
|
|
298
|
-
return [2 /*return*/];
|
|
299
|
-
}
|
|
300
|
-
});
|
|
301
|
-
});
|
|
160
|
+
async function deleteCache(file) {
|
|
161
|
+
const filePath = getFilePath(file);
|
|
162
|
+
await fs.unlink(filePath);
|
|
302
163
|
}
|
|
303
164
|
exports.deleteCache = deleteCache;
|
|
304
|
-
function readExpiringJsonCache(file) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
if (!data || Object.keys(data).length === 0)
|
|
316
|
-
return [2 /*return*/, null];
|
|
317
|
-
if (!(data.expiryTimestamp < (Date.now() / 1e3))) return [3 /*break*/, 3];
|
|
318
|
-
return [4 /*yield*/, deleteCache(file)];
|
|
319
|
-
case 2:
|
|
320
|
-
_a.sent();
|
|
321
|
-
return [2 /*return*/, null];
|
|
322
|
-
case 3: return [2 /*return*/, data.data];
|
|
323
|
-
}
|
|
324
|
-
});
|
|
325
|
-
});
|
|
165
|
+
async function readExpiringJsonCache(file) {
|
|
166
|
+
const options = { readFromR2Cache: false, skipR2Cache: true };
|
|
167
|
+
file = 'expiring/' + file;
|
|
168
|
+
const data = await readCache(file, options);
|
|
169
|
+
if (!data || Object.keys(data).length === 0)
|
|
170
|
+
return null;
|
|
171
|
+
if (data.expiryTimestamp < (Date.now() / 1e3)) {
|
|
172
|
+
await deleteCache(file);
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
return data.data;
|
|
326
176
|
}
|
|
327
177
|
exports.readExpiringJsonCache = readExpiringJsonCache;
|
|
328
|
-
function writeExpiringJsonCache(file, data,
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
case 0:
|
|
336
|
-
file = 'expiring/' + file;
|
|
337
|
-
if (!expiryTimestamp)
|
|
338
|
-
expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
|
|
339
|
-
options = { skipR2CacheWrite: true };
|
|
340
|
-
return [4 /*yield*/, writeCache(file, { data: data, expiryTimestamp: expiryTimestamp }, options)];
|
|
341
|
-
case 1:
|
|
342
|
-
_c.sent();
|
|
343
|
-
return [2 /*return*/];
|
|
344
|
-
}
|
|
345
|
-
});
|
|
346
|
-
});
|
|
178
|
+
async function writeExpiringJsonCache(file, data, { expireAfter = 60 * 60 * 24, // cache for 1 day by default
|
|
179
|
+
expiryTimestamp, }) {
|
|
180
|
+
file = 'expiring/' + file;
|
|
181
|
+
if (!expiryTimestamp)
|
|
182
|
+
expiryTimestamp = Math.floor(Date.now() / 1e3) + expireAfter;
|
|
183
|
+
const options = { skipR2CacheWrite: true };
|
|
184
|
+
await writeCache(file, { data, expiryTimestamp }, options);
|
|
347
185
|
}
|
|
348
186
|
exports.writeExpiringJsonCache = writeExpiringJsonCache;
|