From cf5a431388bded8f47d560528175a43dd89fba13 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Esun Date: Tue, 13 Mar 2018 06:41:01 -0700 Subject: [PATCH] Use a single file delimited with null characters Reviewed By: rafeca Differential Revision: D7232425 fbshipit-source-id: c51387b94cd990ed3ba9be5c82b14c2a7773c7e7 --- packages/metro-cache/src/FileStore.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/packages/metro-cache/src/FileStore.js b/packages/metro-cache/src/FileStore.js index 835a1d95..d8611fb5 100644 --- a/packages/metro-cache/src/FileStore.js +++ b/packages/metro-cache/src/FileStore.js @@ -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 {