metro-cache
Advanced tools
Comparing version 0.30.0 to 0.30.1
{ | ||
"version": "0.30.0", | ||
"version": "0.30.1", | ||
"name": "metro-cache", | ||
@@ -4,0 +4,0 @@ "description": "🚇 Cache layers for Metro", |
@@ -7,16 +7,16 @@ /** | ||
* | ||
* | ||
* @flow | ||
* @format | ||
*/ | ||
'use strict';function _asyncToGenerator(fn) {return function () {var gen = fn.apply(this, arguments);return new Promise(function (resolve, reject) {function step(key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {return Promise.resolve(value).then(function (value) {step("next", value);}, function (err) {step("throw", err);});}}return step("next");});};} | ||
'use strict'; | ||
import type {CacheStore} from 'metro-cache'; | ||
class Cache<T> { | ||
_stores: $ReadOnlyArray<CacheStore<T>>; | ||
class Cache { | ||
_hits: WeakMap<Buffer, CacheStore<T>>; | ||
constructor(stores) { | ||
constructor(stores: $ReadOnlyArray<CacheStore<T>>) { | ||
this._hits = new WeakMap(); | ||
@@ -26,24 +26,24 @@ this._stores = stores; | ||
get(key) {var _this = this;return _asyncToGenerator(function* () { | ||
const stores = _this._stores; | ||
const length = stores.length; | ||
async get(key: Buffer): Promise<?T> { | ||
const stores = this._stores; | ||
const length = stores.length; | ||
for (let i = 0; i < length; i++) { | ||
let value = stores[i].get(key); | ||
for (let i = 0; i < length; i++) { | ||
let value = stores[i].get(key); | ||
if (value instanceof Promise) { | ||
value = yield value; | ||
} | ||
if (value instanceof Promise) { | ||
value = await value; | ||
} | ||
if (value != null) { | ||
_this._hits.set(key, stores[i]); | ||
if (value != null) { | ||
this._hits.set(key, stores[i]); | ||
return value; | ||
} | ||
return value; | ||
} | ||
} | ||
return null;})(); | ||
return null; | ||
} | ||
set(key, value) { | ||
set(key: Buffer, value: T): void { | ||
const stores = this._stores; | ||
@@ -63,5 +63,5 @@ const stop = this._hits.get(key); | ||
}); | ||
}} | ||
} | ||
} | ||
module.exports = Cache; | ||
module.exports = Cache; |
@@ -8,3 +8,3 @@ /** | ||
* @format | ||
* | ||
* @flow | ||
*/ | ||
@@ -18,12 +18,15 @@ | ||
import type {TransformedCode} from 'metro/src/JSTransformer/worker'; | ||
export type Options = {| | ||
root: string, | ||
|}; | ||
const JOINER_DATA = '\0\0'; | ||
const JOINER_LIST = '\0'; | ||
class FileStore { | ||
_root: string; | ||
constructor(options) { | ||
constructor(options: Options) { | ||
const root = options.root; | ||
@@ -38,5 +41,12 @@ | ||
get(key) { | ||
get(key: Buffer): ?TransformedCode { | ||
try { | ||
return JSON.parse(fs.readFileSync(this._getFilePath(key), 'utf-8')); | ||
const data = fs.readFileSync(this._getFilePath(key), 'utf8'); | ||
const [code, dependencies, map] = data.split(JOINER_DATA); | ||
return { | ||
code, | ||
dependencies: dependencies ? dependencies.split(JOINER_LIST) : [], | ||
map: JSON.parse(map), | ||
}; | ||
} catch (err) { | ||
@@ -51,15 +61,21 @@ if (err.code === 'ENOENT') { | ||
set(key, value) { | ||
fs.writeFileSync(this._getFilePath(key), JSON.stringify(value)); | ||
set(key: Buffer, value: TransformedCode): void { | ||
const data = [ | ||
value.code, | ||
value.dependencies.join(JOINER_LIST), | ||
JSON.stringify(value.map), | ||
].join(JOINER_DATA); | ||
fs.writeFileSync(this._getFilePath(key), data); | ||
} | ||
_getFilePath(key) { | ||
_getFilePath(key: Buffer): string { | ||
return path.join( | ||
this._root, | ||
key.slice(0, 1).toString('hex'), | ||
key.slice(1).toString('hex')); | ||
this._root, | ||
key.slice(0, 1).toString('hex'), | ||
key.slice(1).toString('hex'), | ||
); | ||
} | ||
} | ||
}} | ||
module.exports = FileStore; | ||
module.exports = FileStore; |
@@ -7,3 +7,3 @@ /** | ||
* | ||
* | ||
* @flow | ||
* @format | ||
@@ -19,6 +19,6 @@ */ | ||
export type {CacheStore} from './types.flow'; | ||
module.exports.Cache = Cache; | ||
module.exports.FileStore = FileStore; | ||
module.exports.stableHash = stableHash; | ||
module.exports.stableHash = stableHash; |
@@ -8,3 +8,3 @@ /** | ||
* @format | ||
* | ||
* @flow | ||
*/ | ||
@@ -17,15 +17,15 @@ | ||
export type Options = {| | ||
path: string, | ||
writeDelay: ?number, | ||
|}; | ||
class PersistedMapStore { | ||
_map: ?Map<string, mixed>; | ||
_path: string; | ||
_store: () => void; | ||
_timeout: ?TimeoutID; | ||
_writeDelay: number; | ||
constructor(options) { | ||
constructor(options: Options) { | ||
this._path = options.path; | ||
@@ -39,3 +39,3 @@ this._writeDelay = options.writeDelay || 5000; | ||
get(key) { | ||
get(key: Buffer): mixed { | ||
this._getMap(); | ||
@@ -50,3 +50,3 @@ | ||
set(key, value) { | ||
set(key: Buffer, value: mixed) { | ||
this._getMap(); | ||
@@ -76,5 +76,5 @@ | ||
this._timeout = null; | ||
}} | ||
} | ||
} | ||
module.exports = PersistedMapStore; | ||
module.exports = PersistedMapStore; |
@@ -7,3 +7,3 @@ /** | ||
* | ||
* | ||
* @flow | ||
* @format | ||
@@ -16,3 +16,3 @@ */ | ||
function canonicalize(key, value) { | ||
function canonicalize(key: string, value: mixed): mixed { | ||
if (!(value instanceof Object) || value instanceof Array) { | ||
@@ -33,9 +33,9 @@ return value; | ||
function stableHash(value) { | ||
return crypto. | ||
createHash('md4'). | ||
update(JSON.stringify(value, canonicalize)). | ||
digest(); | ||
function stableHash(value: mixed) { | ||
return crypto | ||
.createHash('md4') | ||
.update(JSON.stringify(value, canonicalize)) | ||
.digest(); | ||
} | ||
module.exports = stableHash; | ||
module.exports = stableHash; |
@@ -7,6 +7,11 @@ /** | ||
* | ||
* | ||
* @flow | ||
* @format | ||
*/ | ||
'use strict'; | ||
'use strict'; | ||
export type CacheStore<T> = { | ||
get(key: Buffer): ?T | Promise<?T>, | ||
set(key: Buffer, value: T): void | Promise<void>, | ||
}; |
@@ -11,3 +11,3 @@ /** | ||
'use strict'; | ||
'use strict';var _slicedToArray = function () {function sliceIterator(arr, i) {var _arr = [];var _n = true;var _d = false;var _e = undefined;try {for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {_arr.push(_s.value);if (i && _arr.length === i) break;}} catch (err) {_d = true;_e = err;} finally {try {if (!_n && _i["return"]) _i["return"]();} finally {if (_d) throw _e;}}return _arr;}return function (arr, i) {if (Array.isArray(arr)) {return arr;} else if (Symbol.iterator in Object(arr)) {return sliceIterator(arr, i);} else {throw new TypeError("Invalid attempt to destructure non-iterable instance");}};}(); | ||
@@ -24,2 +24,5 @@ const fs = require('fs'); | ||
const JOINER_DATA = '\0\0'; | ||
const JOINER_LIST = '\0'; | ||
class FileStore { | ||
@@ -40,3 +43,10 @@ | ||
try { | ||
return JSON.parse(fs.readFileSync(this._getFilePath(key), 'utf-8')); | ||
const data = fs.readFileSync(this._getFilePath(key), 'utf8');var _data$split = | ||
data.split(JOINER_DATA),_data$split2 = _slicedToArray(_data$split, 3);const code = _data$split2[0],dependencies = _data$split2[1],map = _data$split2[2]; | ||
return { | ||
code, | ||
dependencies: dependencies ? dependencies.split(JOINER_LIST) : [], | ||
map: JSON.parse(map) }; | ||
} catch (err) { | ||
@@ -52,3 +62,9 @@ if (err.code === 'ENOENT') { | ||
set(key, value) { | ||
fs.writeFileSync(this._getFilePath(key), JSON.stringify(value)); | ||
const data = [ | ||
value.code, | ||
value.dependencies.join(JOINER_LIST), | ||
JSON.stringify(value.map)]. | ||
join(JOINER_DATA); | ||
fs.writeFileSync(this._getFilePath(key), data); | ||
} | ||
@@ -55,0 +71,0 @@ |
Sorry, the diff of this file is not supported yet
252033
666
23
5