Use a single file delimited with null characters

Reviewed By: rafeca

Differential Revision: D7232425

fbshipit-source-id: c51387b94cd990ed3ba9be5c82b14c2a7773c7e7
This commit is contained in:
Miguel Jimenez Esun 2018-03-13 06:41:01 -07:00 committed by Facebook Github Bot
parent 601f718dd6
commit cf5a431388
1 changed files with 18 additions and 2 deletions

View File

@ -20,6 +20,9 @@ export type Options = {|
root: string,
|};
const JOINER_DATA = '\0\0';
const JOINER_LIST = '\0';
class FileStore {
_root: string;
@ -35,7 +38,14 @@ class FileStore {
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) {
if (err.code === 'ENOENT') {
return null;
@ -46,7 +56,13 @@ class FileStore {
}
set(key: Buffer, value: TransformedCode): void {
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);
}
_getFilePath(key: Buffer): string {