Debian Codegraph @

packages/babel-register/src/worker/cache.js

raw symbols SCIP
1 "use strict";
2
3 const path = require("path");
4 const fs = require("fs");
5 const os = require("os");
6 const findCacheDir = require("find-cache-dir");
7
8 let FILENAME = process.env.BABEL_CACHE_PATH;
9
10 // This function needs to be exported before requiring ./babel-core, because
11 // there is a circular dependency between these two files.
12 exports.initializeCacheFilename = function () {
13 FILENAME ||= path.join(
14 findCacheDir({ name: "@babel/register" }) || os.homedir() || os.tmpdir(),
15 `.babel.${babel.version}.${babel.getEnv()}.json`,
16 );
17 };
18
19 const babel = require("./babel-core");
20
21 let data = {};
22
23 let cacheDirty = false;
24
25 let cacheDisabled = false;
26
27 function isCacheDisabled() {
28 return process.env.BABEL_DISABLE_CACHE ?? cacheDisabled;
29 }
30
31 exports.save = save;
32 /**
33 * Write stringified cache to disk.
34 */
35 function save() {
36 if (isCacheDisabled() || !cacheDirty) return;
37 cacheDirty = false;
38
39 let serialised = "{}";
40
41 try {
42 serialised = JSON.stringify(data);
43 } catch (err) {
44 if (err.message === "Invalid string length") {
45 err.message = "Cache too large so it's been cleared.";
46 console.error(err.stack);
47 } else {
48 throw err;
49 }
50 }
51
52 try {
53 fs.mkdirSync(path.dirname(FILENAME), { recursive: true });
54 fs.writeFileSync(FILENAME, serialised);
55 } catch (e) {
56 switch (e.code) {
57 // workaround https://github.com/nodejs/node/issues/31481
58 // todo: remove the ENOENT error check when we drop node.js 13 support
59 case "ENOENT":
60 case "EACCES":
61 case "EPERM":
62 console.warn(
63 `Babel could not write cache to file: ${FILENAME}
64 due to a permission issue. Cache is disabled.`,
65 );
66 cacheDisabled = true;
67 break;
68 case "EROFS":
69 console.warn(
70 `Babel could not write cache to file: ${FILENAME}
71 because it resides in a readonly filesystem. Cache is disabled.`,
72 );
73 cacheDisabled = true;
74 break;
75 default:
76 throw e;
77 }
78 }
79 }
80
81 /**
82 * Load cache from disk and parse.
83 */
84
85 exports.load = function load() {
86 if (isCacheDisabled()) {
87 data = {};
88 return;
89 }
90
91 process.on("exit", save);
92 process.nextTick(save);
93
94 let cacheContent;
95
96 try {
97 cacheContent = fs.readFileSync(FILENAME);
98 } catch (e) {
99 switch (e.code) {
100 // check EACCES only as fs.readFileSync will never throw EPERM on Windows
101 // https://github.com/libuv/libuv/blob/076df64dbbda4320f93375913a728efc40e12d37/src/win/fs.c#L735
102 case "EACCES":
103 console.warn(
104 `Babel could not read cache file: ${FILENAME}
105 due to a permission issue. Cache is disabled.`,
106 );
107 cacheDisabled = true;
108 /* fall through */
109 default:
110 return;
111 }
112 }
113
114 try {
115 data = JSON.parse(cacheContent);
116 } catch {}
117 };
118
119 /**
120 * Retrieve data from cache.
121 */
122 exports.get = function get() {
123 return data;
124 };
125
126 /**
127 * Set the cache dirty bit.
128 */
129 exports.setDirty = function setDirty() {
130 cacheDirty = true;
131 };
132
133 /**
134 * Clear the cache object.
135 */
136 exports.clear = function clear() {
137 data = {};
138 };