ag-common 0.0.854 → 0.0.855
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/api/helpers/google/apikey.js +2 -5
- package/dist/node/helpers/fetch.d.ts +1 -0
- package/dist/node/helpers/fetch.js +30 -0
- package/dist/node/helpers/index.d.ts +1 -0
- package/dist/node/helpers/index.js +1 -0
- package/dist/node/helpers/node-cache.d.ts +71 -0
- package/dist/node/helpers/node-cache.js +97 -0
- package/package.json +1 -1
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.blockKeyService = exports.getAvailableCombinations = void 0;
|
|
7
|
-
const node_cache_1 = __importDefault(require("node-cache"));
|
|
8
4
|
const common_1 = require("../../../common");
|
|
9
5
|
const log_1 = require("../../../common/helpers/log");
|
|
10
6
|
const truncate_1 = require("../../../common/helpers/string/truncate");
|
|
7
|
+
const helpers_1 = require("../../../node/helpers");
|
|
11
8
|
// Initialize NodeCache with 1 hour TTL for blocklisted key combinations
|
|
12
|
-
const blocklist = new
|
|
9
|
+
const blocklist = new helpers_1.TypedNodeCache({ stdTTL: 3600 });
|
|
13
10
|
// Helper to generate blocklist key
|
|
14
11
|
const getBlocklistKey = (apiKey, service) => `${(0, truncate_1.truncate)(apiKey, 10)}_${service}`;
|
|
15
12
|
// Helper to check if a key+service combination is blocklisted
|
|
@@ -14,9 +14,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.fetchFile = fetchFile;
|
|
16
16
|
exports.fetchToMemory = fetchToMemory;
|
|
17
|
+
exports.fetchExists = fetchExists;
|
|
17
18
|
const fs_1 = __importDefault(require("fs"));
|
|
19
|
+
const withRetry_1 = require("../../api/helpers/withRetry");
|
|
18
20
|
const log_1 = require("../../common/helpers/log");
|
|
19
21
|
const headers_1 = require("./headers");
|
|
22
|
+
const node_cache_1 = require("./node-cache");
|
|
20
23
|
/**
|
|
21
24
|
* download(fetch) file
|
|
22
25
|
* @param p
|
|
@@ -72,3 +75,30 @@ function fetchToMemory(imageUrl) {
|
|
|
72
75
|
}
|
|
73
76
|
});
|
|
74
77
|
}
|
|
78
|
+
const imageExistsCache = new node_cache_1.TypedNodeCache({ stdTTL: 60 * 60 * 24 });
|
|
79
|
+
function fetchExists(imageUrl) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const cached = imageExistsCache.get(imageUrl);
|
|
82
|
+
if (typeof cached === 'boolean') {
|
|
83
|
+
return cached;
|
|
84
|
+
}
|
|
85
|
+
const response = yield (0, withRetry_1.withRetry)(() => __awaiter(this, void 0, void 0, function* () {
|
|
86
|
+
const response = yield fetch(imageUrl, {
|
|
87
|
+
method: 'HEAD',
|
|
88
|
+
headers: (0, headers_1.getFetchHeaders)(),
|
|
89
|
+
});
|
|
90
|
+
if (response.status === 200)
|
|
91
|
+
return true;
|
|
92
|
+
if (response.status === 404)
|
|
93
|
+
return false;
|
|
94
|
+
throw new Error(`Unexpected status: ${response.status}`);
|
|
95
|
+
}), 'imageExistsAtUrl');
|
|
96
|
+
if (response === false) {
|
|
97
|
+
imageExistsCache.set(imageUrl, false, 1);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
imageExistsCache.set(imageUrl, true);
|
|
101
|
+
}
|
|
102
|
+
return response;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import NodeCache from 'node-cache';
|
|
2
|
+
export interface CacheOptions {
|
|
3
|
+
stdTTL?: number;
|
|
4
|
+
checkperiod?: number;
|
|
5
|
+
useClones?: boolean;
|
|
6
|
+
deleteOnExpire?: boolean;
|
|
7
|
+
enableLegacyCallbacks?: boolean;
|
|
8
|
+
maxKeys?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class TypedNodeCache<T = any> {
|
|
11
|
+
private cache;
|
|
12
|
+
constructor(options?: CacheOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Normalizes the key to lowercase for case-insensitive operations
|
|
15
|
+
*/
|
|
16
|
+
private normalizeKey;
|
|
17
|
+
/**
|
|
18
|
+
* Get a value from the cache
|
|
19
|
+
*/
|
|
20
|
+
get(key: string): T | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Set a value in the cache
|
|
23
|
+
*/
|
|
24
|
+
set(key: string, value: T, ttl?: number): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Delete a key from the cache
|
|
27
|
+
*/
|
|
28
|
+
del(key: string): number;
|
|
29
|
+
/**
|
|
30
|
+
* Check if a key exists in the cache
|
|
31
|
+
*/
|
|
32
|
+
has(key: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Get multiple values from the cache
|
|
35
|
+
*/
|
|
36
|
+
mget(keys: string[]): {
|
|
37
|
+
[key: string]: T;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Set multiple values in the cache
|
|
41
|
+
*/
|
|
42
|
+
mset(keyValuePairs: Array<{
|
|
43
|
+
key: string;
|
|
44
|
+
val: T;
|
|
45
|
+
ttl?: number;
|
|
46
|
+
}>): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Get all keys in the cache
|
|
49
|
+
*/
|
|
50
|
+
keys(): string[];
|
|
51
|
+
/**
|
|
52
|
+
* Get cache statistics
|
|
53
|
+
*/
|
|
54
|
+
getStats(): NodeCache.Stats;
|
|
55
|
+
/**
|
|
56
|
+
* Flush all data from the cache
|
|
57
|
+
*/
|
|
58
|
+
flushAll(): void;
|
|
59
|
+
/**
|
|
60
|
+
* Close the cache and clear all timers
|
|
61
|
+
*/
|
|
62
|
+
close(): void;
|
|
63
|
+
/**
|
|
64
|
+
* Get the TTL for a key
|
|
65
|
+
*/
|
|
66
|
+
getTtl(key: string): number | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Set the TTL for a key
|
|
69
|
+
*/
|
|
70
|
+
ttl(key: string, ttl: number): boolean;
|
|
71
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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.TypedNodeCache = void 0;
|
|
7
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
class TypedNodeCache {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.cache = new node_cache_1.default(Object.assign({ stdTTL: 3600, checkperiod: 120 }, options));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Normalizes the key to lowercase for case-insensitive operations
|
|
15
|
+
*/
|
|
16
|
+
normalizeKey(key) {
|
|
17
|
+
return key.toLowerCase();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get a value from the cache
|
|
21
|
+
*/
|
|
22
|
+
get(key) {
|
|
23
|
+
return this.cache.get(this.normalizeKey(key));
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Set a value in the cache
|
|
27
|
+
*/
|
|
28
|
+
set(key, value, ttl) {
|
|
29
|
+
if (ttl !== undefined) {
|
|
30
|
+
return this.cache.set(this.normalizeKey(key), value, ttl);
|
|
31
|
+
}
|
|
32
|
+
return this.cache.set(this.normalizeKey(key), value);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Delete a key from the cache
|
|
36
|
+
*/
|
|
37
|
+
del(key) {
|
|
38
|
+
return this.cache.del(this.normalizeKey(key));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if a key exists in the cache
|
|
42
|
+
*/
|
|
43
|
+
has(key) {
|
|
44
|
+
return this.cache.has(this.normalizeKey(key));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get multiple values from the cache
|
|
48
|
+
*/
|
|
49
|
+
mget(keys) {
|
|
50
|
+
const normalizedKeys = keys.map((key) => this.normalizeKey(key));
|
|
51
|
+
return this.cache.mget(normalizedKeys);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Set multiple values in the cache
|
|
55
|
+
*/
|
|
56
|
+
mset(keyValuePairs) {
|
|
57
|
+
const normalizedPairs = keyValuePairs.map((pair) => (Object.assign(Object.assign({}, pair), { key: this.normalizeKey(pair.key) })));
|
|
58
|
+
return this.cache.mset(normalizedPairs);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get all keys in the cache
|
|
62
|
+
*/
|
|
63
|
+
keys() {
|
|
64
|
+
return this.cache.keys();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get cache statistics
|
|
68
|
+
*/
|
|
69
|
+
getStats() {
|
|
70
|
+
return this.cache.getStats();
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Flush all data from the cache
|
|
74
|
+
*/
|
|
75
|
+
flushAll() {
|
|
76
|
+
this.cache.flushAll();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Close the cache and clear all timers
|
|
80
|
+
*/
|
|
81
|
+
close() {
|
|
82
|
+
this.cache.close();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the TTL for a key
|
|
86
|
+
*/
|
|
87
|
+
getTtl(key) {
|
|
88
|
+
return this.cache.getTtl(this.normalizeKey(key));
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Set the TTL for a key
|
|
92
|
+
*/
|
|
93
|
+
ttl(key, ttl) {
|
|
94
|
+
return this.cache.ttl(this.normalizeKey(key), ttl);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.TypedNodeCache = TypedNodeCache;
|
package/package.json
CHANGED