1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | exports.__esModule = true;
|
5 | exports.guessSafeChunkSize = guessSafeChunkSize;
|
6 | exports.readFromCache = readFromCache;
|
7 | exports.writeToCache = writeToCache;
|
8 | var _path = _interopRequireDefault(require("path"));
|
9 | var _os = _interopRequireDefault(require("os"));
|
10 | var _v = _interopRequireDefault(require("v8"));
|
11 | var _fsExtra = require("fs-extra");
|
12 | var _glob = require("glob");
|
13 | var _gatsbyCoreUtils = require("gatsby-core-utils");
|
14 | var _reporter = _interopRequireDefault(require("gatsby-cli/lib/reporter"));
|
15 | const getReduxCacheFolder = () =>
|
16 |
|
17 | _path.default.join(process.cwd(), `.cache/redux`);
|
18 | const getWorkerSlicesFolder = () =>
|
19 |
|
20 | _path.default.join(process.cwd(), `.cache/worker`);
|
21 | function reduxSharedFile(dir) {
|
22 | return _path.default.join(dir, `redux.rest.state`);
|
23 | }
|
24 | function reduxChunkedNodesFilePrefix(dir) {
|
25 | return _path.default.join(dir, `redux.node.state_`);
|
26 | }
|
27 | function reduxChunkedPagesFilePrefix(dir) {
|
28 | return _path.default.join(dir, `redux.page.state_`);
|
29 | }
|
30 | function reduxWorkerSlicesPrefix(dir) {
|
31 | return _path.default.join(dir, `redux.worker.slices_`);
|
32 | }
|
33 | function readFromCache(slices, optionalPrefix = ``) {
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | let cacheFolder = getReduxCacheFolder();
|
41 | if (slices) {
|
42 | cacheFolder = getWorkerSlicesFolder();
|
43 | return _v.default.deserialize((0, _fsExtra.readFileSync)(reduxWorkerSlicesPrefix(cacheFolder) + `${optionalPrefix}_` + (0, _gatsbyCoreUtils.createContentDigest)(slices)));
|
44 | }
|
45 | const obj = _v.default.deserialize((0, _fsExtra.readFileSync)(reduxSharedFile(cacheFolder)));
|
46 |
|
47 |
|
48 | const nodesChunks = (0, _glob.sync)(reduxChunkedNodesFilePrefix(cacheFolder) + `*`).map(file => _v.default.deserialize((0, _fsExtra.readFileSync)(file)));
|
49 | const nodes = [].concat(...nodesChunks);
|
50 | if (!nodesChunks.length) {
|
51 | _reporter.default.info(`Cache exists but contains no nodes. There should be at least some nodes available so it seems the cache was corrupted. Disregarding the cache and proceeding as if there was none.`);
|
52 | return {};
|
53 | }
|
54 | obj.nodes = new Map(nodes);
|
55 |
|
56 |
|
57 | const pagesChunks = (0, _glob.sync)(reduxChunkedPagesFilePrefix(cacheFolder) + `*`).map(file => _v.default.deserialize((0, _fsExtra.readFileSync)(file)));
|
58 | const pages = [].concat(...pagesChunks);
|
59 | obj.pages = new Map(pages);
|
60 | return obj;
|
61 | }
|
62 | function guessSafeChunkSize(values, showMaxSizeWarning = false) {
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | const nodesToTest = 11;
|
70 | const valueCount = values.length;
|
71 | const step = Math.max(1, Math.ceil(valueCount / nodesToTest));
|
72 | let maxSize = 0;
|
73 | for (let i = 0; i < valueCount; i += step) {
|
74 | const size = _v.default.serialize(values[i]).length;
|
75 | maxSize = Math.max(size, maxSize);
|
76 | }
|
77 |
|
78 |
|
79 | if (showMaxSizeWarning && maxSize > 500000) {
|
80 | _reporter.default.warn(`The size of at least one page context chunk exceeded 500kb, which could lead to degraded performance. Consider putting less data in the page context.`);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | return Math.floor(1.5 * 1024 * 1024 * 1024 / maxSize);
|
87 | }
|
88 | function prepareCacheFolder(targetDir, contents) {
|
89 |
|
90 |
|
91 | const nodesMap = contents.nodes;
|
92 | contents.nodes = undefined;
|
93 | const pagesMap = contents.pages;
|
94 | contents.pages = undefined;
|
95 | (0, _fsExtra.writeFileSync)(reduxSharedFile(targetDir), _v.default.serialize(contents));
|
96 |
|
97 |
|
98 | contents.nodes = nodesMap;
|
99 | contents.pages = pagesMap;
|
100 | if (nodesMap) {
|
101 | if (nodesMap.size === 0) {
|
102 |
|
103 |
|
104 |
|
105 | const dummyNode = {
|
106 | id: `dummy-node-id`,
|
107 | parent: ``,
|
108 | children: [],
|
109 | internal: {
|
110 | type: `DummyNode`,
|
111 | contentDigest: `dummy-node`,
|
112 | counter: 0,
|
113 | owner: ``
|
114 | },
|
115 | fields: []
|
116 | };
|
117 | nodesMap.set(dummyNode.id, dummyNode);
|
118 | }
|
119 |
|
120 | const values = [...nodesMap.entries()];
|
121 | const chunkSize = guessSafeChunkSize(values);
|
122 | const chunks = Math.ceil(values.length / chunkSize);
|
123 | for (let i = 0; i < chunks; ++i) {
|
124 | (0, _fsExtra.writeFileSync)(reduxChunkedNodesFilePrefix(targetDir) + i, _v.default.serialize(values.slice(i * chunkSize, i * chunkSize + chunkSize)));
|
125 | }
|
126 | }
|
127 | if (pagesMap) {
|
128 |
|
129 | const values = [...pagesMap.entries()];
|
130 | const chunkSize = guessSafeChunkSize(values, true);
|
131 | const chunks = Math.ceil(values.length / chunkSize);
|
132 | for (let i = 0; i < chunks; ++i) {
|
133 | (0, _fsExtra.writeFileSync)(reduxChunkedPagesFilePrefix(targetDir) + i, _v.default.serialize(values.slice(i * chunkSize, i * chunkSize + chunkSize)));
|
134 | }
|
135 | }
|
136 | }
|
137 | function safelyRenameToBak(cacheFolder) {
|
138 |
|
139 |
|
140 | const tmpSuffix = `.bak`;
|
141 | let suffixCounter = 0;
|
142 | let bakName = cacheFolder + tmpSuffix;
|
143 |
|
144 | while ((0, _fsExtra.existsSync)(bakName)) {
|
145 | ++suffixCounter;
|
146 | bakName = cacheFolder + tmpSuffix + suffixCounter;
|
147 | }
|
148 | (0, _fsExtra.moveSync)(cacheFolder, bakName);
|
149 | return bakName;
|
150 | }
|
151 | function writeToCache(contents, slices, optionalPrefix = ``) {
|
152 |
|
153 |
|
154 |
|
155 | if (slices) {
|
156 | const cacheFolder = getWorkerSlicesFolder();
|
157 | (0, _fsExtra.outputFileSync)(reduxWorkerSlicesPrefix(cacheFolder) + `${optionalPrefix}_` + (0, _gatsbyCoreUtils.createContentDigest)(slices), _v.default.serialize(contents));
|
158 | return;
|
159 | }
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | const tmpDir = (0, _fsExtra.mkdtempSync)(_path.default.join(_os.default.tmpdir(), `reduxcache`));
|
165 |
|
166 | prepareCacheFolder(tmpDir, contents);
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | const reduxCacheFolder = getReduxCacheFolder();
|
173 | let bakName = ``;
|
174 | if ((0, _fsExtra.existsSync)(reduxCacheFolder)) {
|
175 |
|
176 | bakName = safelyRenameToBak(reduxCacheFolder);
|
177 | }
|
178 |
|
179 |
|
180 | (0, _fsExtra.moveSync)(tmpDir, reduxCacheFolder);
|
181 |
|
182 |
|
183 | try {
|
184 | if (bakName !== ``) {
|
185 | (0, _fsExtra.removeSync)(bakName);
|
186 | }
|
187 | } catch (e) {
|
188 | _reporter.default.warn(`Non-fatal: Deleting the old cache folder failed, left behind in \`${bakName}\`. Rimraf reported this error: ${e}`);
|
189 | }
|
190 | }
|
191 |
|
\ | No newline at end of file |