Add "FileStore" cache

Reviewed By: davidaurelio

Differential Revision: D7037483

fbshipit-source-id: b7517f2b3c8df3e5653dd1a756dfbee5bb2bd656
This commit is contained in:
Miguel Jimenez Esun 2018-02-25 15:58:58 -08:00 committed by Facebook Github Bot
parent a62eaa0d3b
commit 5edc2acc80
3 changed files with 68 additions and 0 deletions

View File

@ -10,5 +10,9 @@
"scripts": {
"prepare-release": "test -d build && rm -rf src.real && mv src src.real && mv build src",
"cleanup-release": "test ! -e build && mv src build && mv src.real src"
},
"dependencies": {
"jest-serializer": "^22.4.0",
"mkdirp": "^0.5.1"
}
}

View File

@ -0,0 +1,62 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const serializer = require('jest-serializer');
export type Options = {|
root: string,
|};
class FileStore {
_root: string;
constructor(options: Options) {
const root = options.root;
for (let i = 0; i < 256; i++) {
mkdirp.sync(path.join(root, ('0' + i.toString(16)).slice(-2)));
}
this._root = root;
}
get(key: Buffer): mixed {
try {
return serializer.readFileSync(this._getFilePath(key));
} catch (err) {
return null;
}
}
set(key: Buffer, value: mixed): Promise<void> {
return new Promise((resolve, reject) => {
const data = serializer.serialize(value);
fs.writeFile(this._getFilePath(key), data, err => {
err ? reject(err) : resolve();
});
});
}
_getFilePath(key: Buffer): string {
return path.join(
this._root,
key.slice(0, 1).toString('hex'),
key.slice(1).toString('hex'),
);
}
}
module.exports = FileStore;

View File

@ -11,10 +11,12 @@
'use strict';
const Cache = require('./Cache');
const FileStore = require('./FileStore');
const stableHash = require('./stableHash');
export type {CacheStore} from './types.flow';
module.exports.Cache = Cache;
module.exports.FileStore = FileStore;
module.exports.stableHash = stableHash;