metro-cache
Advanced tools
Comparing version 0.80.9 to 0.80.10
{ | ||
"name": "metro-cache", | ||
"version": "0.80.9", | ||
"version": "0.80.10", | ||
"description": "🚇 Cache layers for Metro.", | ||
@@ -15,7 +15,8 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"metro-core": "0.80.9", | ||
"rimraf": "^3.0.2" | ||
"exponential-backoff": "^3.1.1", | ||
"flow-enums-runtime": "^0.0.6", | ||
"metro-core": "0.80.10" | ||
}, | ||
"devDependencies": { | ||
"metro-memory-fs": "0.80.9" | ||
"metro-memory-fs": "0.80.10" | ||
}, | ||
@@ -22,0 +23,0 @@ "license": "MIT", |
@@ -14,3 +14,4 @@ "use strict"; | ||
const store = stores[i]; | ||
const name = store.constructor.name + "::" + key.toString("hex"); | ||
const storeName = store.name ?? store.constructor.name; | ||
const name = storeName + "::" + key.toString("hex"); | ||
let value = null; | ||
@@ -31,6 +32,10 @@ const logStart = Logger.log( | ||
} finally { | ||
Logger.log(Logger.createActionEndEntry(logStart)); | ||
const hitOrMiss = value != null ? "hit" : "miss"; | ||
Logger.log({ | ||
...Logger.createActionEndEntry(logStart), | ||
action_result: hitOrMiss, | ||
}); | ||
Logger.log( | ||
Logger.createEntry({ | ||
action_name: "Cache " + (value == null ? "miss" : "hit"), | ||
action_name: "Cache " + hitOrMiss, | ||
log_entry_label: name, | ||
@@ -52,7 +57,10 @@ }) | ||
const promises = []; | ||
const writeErrors = []; | ||
const storesWithErrors = new Set(); | ||
for (let i = 0; i < length && stores[i] !== stop; i++) { | ||
const store = stores[i]; | ||
const name = store.constructor.name + "::" + key.toString("hex"); | ||
Logger.log( | ||
Logger.createEntry({ | ||
const storeName = store.name ?? store.constructor.name; | ||
const name = storeName + "::" + key.toString("hex"); | ||
const logStart = Logger.log( | ||
Logger.createActionStartEntry({ | ||
action_name: "Cache set", | ||
@@ -62,5 +70,28 @@ log_entry_label: name, | ||
); | ||
promises.push(stores[i].set(key, value)); | ||
promises.push( | ||
(async () => { | ||
try { | ||
await stores[i].set(key, value); | ||
Logger.log(Logger.createActionEndEntry(logStart)); | ||
} catch (e) { | ||
Logger.log(Logger.createActionEndEntry(logStart, e)); | ||
storesWithErrors.add(storeName); | ||
writeErrors.push( | ||
new Error(`Cache write failed for ${name}`, { | ||
cause: e, | ||
}) | ||
); | ||
} | ||
})() | ||
); | ||
} | ||
await Promise.all(promises); | ||
await Promise.allSettled(promises); | ||
if (writeErrors.length > 0) { | ||
throw new AggregateError( | ||
writeErrors, | ||
`Cache write failed for store(s): ${Array.from(storesWithErrors).join( | ||
", " | ||
)}` | ||
); | ||
} | ||
} | ||
@@ -67,0 +98,0 @@ get isDisabled() { |
@@ -5,3 +5,2 @@ "use strict"; | ||
const path = require("path"); | ||
const rimraf = require("rimraf"); | ||
const NULL_BYTE = 0x00; | ||
@@ -72,3 +71,6 @@ const NULL_BYTE_BUFFER = Buffer.from([NULL_BYTE]); | ||
for (let i = 0; i < 256; i++) { | ||
rimraf.sync(path.join(this._root, ("0" + i.toString(16)).slice(-2))); | ||
fs.rmSync(path.join(this._root, ("0" + i.toString(16)).slice(-2)), { | ||
force: true, | ||
recursive: true, | ||
}); | ||
} | ||
@@ -75,0 +77,0 @@ } |
@@ -5,2 +5,3 @@ "use strict"; | ||
const NetworkError = require("./NetworkError"); | ||
const { backOff } = require("exponential-backoff"); | ||
const http = require("http"); | ||
@@ -61,5 +62,11 @@ const https = require("https"); | ||
debug: options.debug ?? false, | ||
maxAttempts: options.maxAttempts ?? 1, | ||
retryStatuses: new Set(options.retryStatuses ?? []), | ||
retryNetworkErrors: options.retryNetworkErrors ?? false, | ||
}; | ||
} | ||
get(key) { | ||
return this.#withRetries(() => this.#getOnce(key), this._getEndpoint); | ||
} | ||
#getOnce(key) { | ||
return new Promise((resolve, reject) => { | ||
@@ -165,2 +172,8 @@ let searchParamsString = this._getEndpoint.params.toString(); | ||
set(key, value) { | ||
return this.#withRetries( | ||
() => this.#setOnce(key, value), | ||
this._setEndpoint | ||
); | ||
} | ||
#setOnce(key, value) { | ||
return new Promise((resolve, reject) => { | ||
@@ -253,3 +266,21 @@ const gzip = zlib.createGzip(ZLIB_OPTIONS); | ||
clear() {} | ||
#withRetries(fn, endpoint) { | ||
if (endpoint.maxAttempts === 1) { | ||
return fn(); | ||
} | ||
return backOff(fn, { | ||
jitter: "full", | ||
maxDelay: 30000, | ||
numOfAttempts: this._getEndpoint.maxAttempts || Number.POSITIVE_INFINITY, | ||
retry: (e) => { | ||
if (e instanceof HttpError) { | ||
return this._getEndpoint.retryStatuses.has(e.code); | ||
} | ||
return ( | ||
e instanceof NetworkError && this._getEndpoint.retryNetworkErrors | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
module.exports = HttpStore; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45899
751
3
+ Addedexponential-backoff@^3.1.1
+ Addedflow-enums-runtime@^0.0.6
+ Addedexponential-backoff@3.1.1(transitive)
+ Addedflow-enums-runtime@0.0.6(transitive)
+ Addedmetro-core@0.80.10(transitive)
+ Addedmetro-resolver@0.80.10(transitive)
- Removedrimraf@^3.0.2
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedglob@7.2.3(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedmetro-core@0.80.9(transitive)
- Removedmetro-resolver@0.80.9(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedrimraf@3.0.2(transitive)
- Removedwrappy@1.0.2(transitive)
Updatedmetro-core@0.80.10