@metamask-previews/phishing-controller 13.1.0-preview-5a701133 → 14.0.0-preview-c20b7569
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/CHANGELOG.md +14 -1
- package/dist/CacheManager.cjs +177 -0
- package/dist/CacheManager.cjs.map +1 -0
- package/dist/CacheManager.d.cts +104 -0
- package/dist/CacheManager.d.cts.map +1 -0
- package/dist/CacheManager.d.mts +104 -0
- package/dist/CacheManager.d.mts.map +1 -0
- package/dist/CacheManager.mjs +173 -0
- package/dist/CacheManager.mjs.map +1 -0
- package/dist/PhishingController.cjs +186 -8
- package/dist/PhishingController.cjs.map +1 -1
- package/dist/PhishingController.d.cts +41 -6
- package/dist/PhishingController.d.cts.map +1 -1
- package/dist/PhishingController.d.mts +41 -6
- package/dist/PhishingController.d.mts.map +1 -1
- package/dist/PhishingController.mjs +186 -8
- package/dist/PhishingController.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/tests/utils.cjs +79 -1
- package/dist/tests/utils.cjs.map +1 -1
- package/dist/tests/utils.d.cts +59 -0
- package/dist/tests/utils.d.cts.map +1 -1
- package/dist/tests/utils.d.mts +59 -0
- package/dist/tests/utils.d.mts.map +1 -1
- package/dist/tests/utils.mjs +75 -0
- package/dist/tests/utils.mjs.map +1 -1
- package/dist/types.cjs +35 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +68 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +68 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +34 -0
- package/dist/types.mjs.map +1 -1
- package/dist/utils.cjs +54 -1
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +55 -0
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +55 -0
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +50 -0
- package/dist/utils.mjs.map +1 -1
- package/package.json +5 -1
- package/dist/UrlScanCache.cjs +0 -127
- package/dist/UrlScanCache.cjs.map +0 -1
- package/dist/UrlScanCache.d.cts +0 -67
- package/dist/UrlScanCache.d.cts.map +0 -1
- package/dist/UrlScanCache.d.mts +0 -67
- package/dist/UrlScanCache.d.mts.map +0 -1
- package/dist/UrlScanCache.mjs +0 -123
- package/dist/UrlScanCache.mjs.map +0 -1
package/dist/UrlScanCache.mjs
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6
|
-
};
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
-
};
|
12
|
-
var _UrlScanCache_instances, _UrlScanCache_cacheTTL, _UrlScanCache_maxCacheSize, _UrlScanCache_cache, _UrlScanCache_updateState, _UrlScanCache_persistCache, _UrlScanCache_evictEntries;
|
13
|
-
import { fetchTimeNow } from "./utils.mjs";
|
14
|
-
/**
|
15
|
-
* Default values for URL scan cache
|
16
|
-
*/
|
17
|
-
export const DEFAULT_URL_SCAN_CACHE_TTL = 300; // 5 minutes in seconds
|
18
|
-
export const DEFAULT_URL_SCAN_CACHE_MAX_SIZE = 100;
|
19
|
-
/**
|
20
|
-
* UrlScanCache class
|
21
|
-
*
|
22
|
-
* Handles caching of URL scan results with TTL and size limits
|
23
|
-
*/
|
24
|
-
export class UrlScanCache {
|
25
|
-
/**
|
26
|
-
* Constructor for UrlScanCache
|
27
|
-
*
|
28
|
-
* @param options - Cache configuration options
|
29
|
-
* @param options.cacheTTL - Time to live in seconds for cached entries
|
30
|
-
* @param options.maxCacheSize - Maximum number of entries in the cache
|
31
|
-
* @param options.initialCache - Initial cache state
|
32
|
-
* @param options.updateState - Function to update the state when cache changes
|
33
|
-
*/
|
34
|
-
constructor({ cacheTTL = DEFAULT_URL_SCAN_CACHE_TTL, maxCacheSize = DEFAULT_URL_SCAN_CACHE_MAX_SIZE, initialCache = {}, updateState, }) {
|
35
|
-
_UrlScanCache_instances.add(this);
|
36
|
-
_UrlScanCache_cacheTTL.set(this, void 0);
|
37
|
-
_UrlScanCache_maxCacheSize.set(this, void 0);
|
38
|
-
_UrlScanCache_cache.set(this, void 0);
|
39
|
-
_UrlScanCache_updateState.set(this, void 0);
|
40
|
-
__classPrivateFieldSet(this, _UrlScanCache_cacheTTL, cacheTTL, "f");
|
41
|
-
__classPrivateFieldSet(this, _UrlScanCache_maxCacheSize, maxCacheSize, "f");
|
42
|
-
__classPrivateFieldSet(this, _UrlScanCache_cache, new Map(Object.entries(initialCache)), "f");
|
43
|
-
__classPrivateFieldSet(this, _UrlScanCache_updateState, updateState, "f");
|
44
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_evictEntries).call(this);
|
45
|
-
}
|
46
|
-
/**
|
47
|
-
* Set the time-to-live for cached entries
|
48
|
-
*
|
49
|
-
* @param ttl - The TTL in seconds
|
50
|
-
*/
|
51
|
-
setTTL(ttl) {
|
52
|
-
__classPrivateFieldSet(this, _UrlScanCache_cacheTTL, ttl, "f");
|
53
|
-
}
|
54
|
-
/**
|
55
|
-
* Set the maximum cache size
|
56
|
-
*
|
57
|
-
* @param maxSize - The maximum cache size
|
58
|
-
*/
|
59
|
-
setMaxSize(maxSize) {
|
60
|
-
__classPrivateFieldSet(this, _UrlScanCache_maxCacheSize, maxSize, "f");
|
61
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_evictEntries).call(this);
|
62
|
-
}
|
63
|
-
/**
|
64
|
-
* Clear the cache
|
65
|
-
*/
|
66
|
-
clear() {
|
67
|
-
__classPrivateFieldGet(this, _UrlScanCache_cache, "f").clear();
|
68
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_persistCache).call(this);
|
69
|
-
}
|
70
|
-
/**
|
71
|
-
* Get a cached result if it exists and is not expired
|
72
|
-
*
|
73
|
-
* @param hostname - The hostname to check
|
74
|
-
* @returns The cached scan result or undefined if not found or expired
|
75
|
-
*/
|
76
|
-
get(hostname) {
|
77
|
-
const cacheEntry = __classPrivateFieldGet(this, _UrlScanCache_cache, "f").get(hostname);
|
78
|
-
if (!cacheEntry) {
|
79
|
-
return undefined;
|
80
|
-
}
|
81
|
-
// Check if the entry is expired
|
82
|
-
const now = fetchTimeNow();
|
83
|
-
if (now - cacheEntry.timestamp > __classPrivateFieldGet(this, _UrlScanCache_cacheTTL, "f")) {
|
84
|
-
// Entry expired, remove it from cache
|
85
|
-
__classPrivateFieldGet(this, _UrlScanCache_cache, "f").delete(hostname);
|
86
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_persistCache).call(this);
|
87
|
-
return undefined;
|
88
|
-
}
|
89
|
-
return cacheEntry.result;
|
90
|
-
}
|
91
|
-
/**
|
92
|
-
* Add an entry to the cache, evicting oldest entries if necessary
|
93
|
-
*
|
94
|
-
* @param hostname - The hostname to cache
|
95
|
-
* @param result - The scan result to cache
|
96
|
-
*/
|
97
|
-
add(hostname, result) {
|
98
|
-
__classPrivateFieldGet(this, _UrlScanCache_cache, "f").set(hostname, {
|
99
|
-
result,
|
100
|
-
timestamp: fetchTimeNow(),
|
101
|
-
});
|
102
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_evictEntries).call(this);
|
103
|
-
__classPrivateFieldGet(this, _UrlScanCache_instances, "m", _UrlScanCache_persistCache).call(this);
|
104
|
-
}
|
105
|
-
}
|
106
|
-
_UrlScanCache_cacheTTL = new WeakMap(), _UrlScanCache_maxCacheSize = new WeakMap(), _UrlScanCache_cache = new WeakMap(), _UrlScanCache_updateState = new WeakMap(), _UrlScanCache_instances = new WeakSet(), _UrlScanCache_persistCache = function _UrlScanCache_persistCache() {
|
107
|
-
__classPrivateFieldGet(this, _UrlScanCache_updateState, "f").call(this, Object.fromEntries(__classPrivateFieldGet(this, _UrlScanCache_cache, "f")));
|
108
|
-
}, _UrlScanCache_evictEntries = function _UrlScanCache_evictEntries() {
|
109
|
-
if (__classPrivateFieldGet(this, _UrlScanCache_cache, "f").size <= __classPrivateFieldGet(this, _UrlScanCache_maxCacheSize, "f")) {
|
110
|
-
return;
|
111
|
-
}
|
112
|
-
const entriesToRemove = __classPrivateFieldGet(this, _UrlScanCache_cache, "f").size - __classPrivateFieldGet(this, _UrlScanCache_maxCacheSize, "f");
|
113
|
-
let count = 0;
|
114
|
-
// Delete the oldest entries
|
115
|
-
for (const key of __classPrivateFieldGet(this, _UrlScanCache_cache, "f").keys()) {
|
116
|
-
if (count >= entriesToRemove) {
|
117
|
-
break;
|
118
|
-
}
|
119
|
-
__classPrivateFieldGet(this, _UrlScanCache_cache, "f").delete(key);
|
120
|
-
count += 1;
|
121
|
-
}
|
122
|
-
};
|
123
|
-
//# sourceMappingURL=UrlScanCache.mjs.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"UrlScanCache.mjs","sourceRoot":"","sources":["../src/UrlScanCache.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,YAAY,EAAE,oBAAgB;AAUvC;;GAEG;AACH,MAAM,CA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\nimport { fetchTimeNow } from './utils';\n\n/**\n * Cache entry for URL scan results\n */\nexport type UrlScanCacheEntry = {\n result: PhishingDetectionScanResult;\n timestamp: number;\n};\n\n/**\n * Default values for URL scan cache\n */\nexport const DEFAULT_URL_SCAN_CACHE_TTL = 300; // 5 minutes in seconds\nexport const DEFAULT_URL_SCAN_CACHE_MAX_SIZE = 100;\n\n/**\n * UrlScanCache class\n *\n * Handles caching of URL scan results with TTL and size limits\n */\nexport class UrlScanCache {\n #cacheTTL: number;\n\n #maxCacheSize: number;\n\n readonly #cache: Map<string, UrlScanCacheEntry>;\n\n readonly #updateState: (cache: Record<string, UrlScanCacheEntry>) => void;\n\n /**\n * Constructor for UrlScanCache\n *\n * @param options - Cache configuration options\n * @param options.cacheTTL - Time to live in seconds for cached entries\n * @param options.maxCacheSize - Maximum number of entries in the cache\n * @param options.initialCache - Initial cache state\n * @param options.updateState - Function to update the state when cache changes\n */\n constructor({\n cacheTTL = DEFAULT_URL_SCAN_CACHE_TTL,\n maxCacheSize = DEFAULT_URL_SCAN_CACHE_MAX_SIZE,\n initialCache = {},\n updateState,\n }: {\n cacheTTL?: number;\n maxCacheSize?: number;\n initialCache?: Record<string, UrlScanCacheEntry>;\n updateState: (cache: Record<string, UrlScanCacheEntry>) => void;\n }) {\n this.#cacheTTL = cacheTTL;\n this.#maxCacheSize = maxCacheSize;\n this.#cache = new Map(Object.entries(initialCache));\n this.#updateState = updateState;\n this.#evictEntries();\n }\n\n /**\n * Set the time-to-live for cached entries\n *\n * @param ttl - The TTL in seconds\n */\n setTTL(ttl: number): void {\n this.#cacheTTL = ttl;\n }\n\n /**\n * Set the maximum cache size\n *\n * @param maxSize - The maximum cache size\n */\n setMaxSize(maxSize: number): void {\n this.#maxCacheSize = maxSize;\n this.#evictEntries();\n }\n\n /**\n * Clear the cache\n */\n clear(): void {\n this.#cache.clear();\n this.#persistCache();\n }\n\n /**\n * Get a cached result if it exists and is not expired\n *\n * @param hostname - The hostname to check\n * @returns The cached scan result or undefined if not found or expired\n */\n get(hostname: string): PhishingDetectionScanResult | undefined {\n const cacheEntry = this.#cache.get(hostname);\n if (!cacheEntry) {\n return undefined;\n }\n\n // Check if the entry is expired\n const now = fetchTimeNow();\n if (now - cacheEntry.timestamp > this.#cacheTTL) {\n // Entry expired, remove it from cache\n this.#cache.delete(hostname);\n this.#persistCache();\n return undefined;\n }\n\n return cacheEntry.result;\n }\n\n /**\n * Add an entry to the cache, evicting oldest entries if necessary\n *\n * @param hostname - The hostname to cache\n * @param result - The scan result to cache\n */\n add(hostname: string, result: PhishingDetectionScanResult): void {\n this.#cache.set(hostname, {\n result,\n timestamp: fetchTimeNow(),\n });\n\n this.#evictEntries();\n\n this.#persistCache();\n }\n\n /**\n * Persist the current cache state\n */\n #persistCache(): void {\n this.#updateState(Object.fromEntries(this.#cache));\n }\n\n /**\n * Evict oldest entries if cache exceeds max size\n */\n #evictEntries(): void {\n if (this.#cache.size <= this.#maxCacheSize) {\n return;\n }\n\n const entriesToRemove = this.#cache.size - this.#maxCacheSize;\n let count = 0;\n // Delete the oldest entries\n for (const key of this.#cache.keys()) {\n if (count >= entriesToRemove) {\n break;\n }\n this.#cache.delete(key);\n count += 1;\n }\n }\n}\n"]}
|