@foxtware/mineral 0.1.32 → 0.1.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/utils.js +19 -4
- package/package.json +1 -1
package/api/utils.js
CHANGED
|
@@ -43,18 +43,33 @@ const getWithLocalCachedFile = async (
|
|
|
43
43
|
const directory = `${ getWorkspace() }/api/_temp`;
|
|
44
44
|
const filepath = `${ directory }/${ filename }`;
|
|
45
45
|
|
|
46
|
+
let cachedFile;
|
|
46
47
|
try {
|
|
47
|
-
|
|
48
|
-
return JSON.parse(cachedFile);
|
|
48
|
+
cachedFile = await fs.readFile(filepath, 'utf8');
|
|
49
49
|
} catch (error) {
|
|
50
|
-
if (error.code
|
|
50
|
+
if (error.code === 'ENOENT') {
|
|
51
|
+
cachedFile = null;
|
|
52
|
+
} else {
|
|
51
53
|
throw error;
|
|
52
54
|
}
|
|
53
55
|
}
|
|
54
56
|
|
|
57
|
+
if (cachedFile) {
|
|
58
|
+
try {
|
|
59
|
+
return JSON.parse(cachedFile);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.warn('Invalid local cache, refetching', {
|
|
62
|
+
filename,
|
|
63
|
+
error: error.message,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
55
68
|
const response = await getFunction();
|
|
56
69
|
await fs.mkdir(directory, { recursive: true });
|
|
57
|
-
|
|
70
|
+
const temporaryFilepath = `${ filepath }.tmp-${ process.pid }-${ Date.now() }`;
|
|
71
|
+
await fs.writeFile(temporaryFilepath, JSON.stringify(response, null, 2), 'utf8');
|
|
72
|
+
await fs.rename(temporaryFilepath, filepath);
|
|
58
73
|
|
|
59
74
|
return response;
|
|
60
75
|
};
|