-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathssr-caching.js
391 lines (338 loc) · 11.2 KB
/
ssr-caching.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"use strict";
/* eslint-disable no-magic-numbers */
const ReactCompositeComponent = require("react-dom/lib/ReactCompositeComponent");
const DOMPropertyOperations = require("react-dom/lib/DOMPropertyOperations");
const assert = require("assert");
const _ = require("lodash");
const CacheStore = require("./cache-store");
const config = require("./config");
const noOpHashKeyFn = (key) => key;
let hashKeyFn = noOpHashKeyFn;
exports.shouldHashKeys = function (flag, fn) {
if (typeof flag === "boolean") {
config.hashKey = flag;
}
if (config.hashKey) {
if (!fn) {
try {
const FarmHash = require("farmhash"); // eslint-disable-line
hashKeyFn = (s) => FarmHash.hash64(s);
} catch (e) {
console.log("farmhash module not available, turning off hashKey"); // eslint-disable-line
config.hashKey = false;
hashKeyFn = noOpHashKeyFn;
}
} else {
assert(typeof fn === "function", "hashKey function is not a function");
hashKeyFn = fn;
}
} else {
hashKeyFn = noOpHashKeyFn;
}
exports.hashKeyFn = hashKeyFn;
};
exports.shouldHashKeys();
const profileData = {};
const blackListed = {};
let cacheComponents = {};
let cacheStore;
//
// cache key is generated from props
// generate template from props for cache strategy template
//
// Note: It's hard and tricky to template non-string props. Since the code may
// behave differently depending on a boolean being true/false, or a number with different
// values. Even string props the code could behave differently base on what the value is.
// For example, collection status could be "PUBLISHED", "UNPUBLISHED", etc.
//
// Non-string props could also be stringified differently by the component.
//
// returns { template, lookup, cacheKey }
//
function generateTemplate(props, opts) {
const template = {};
const lookup = {};
const path = [];
let index = 0;
const cacheKey = [];
const gen = (obj, tmpl) => { // eslint-disable-line
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const v = obj[k];
if (opts.ignoreKeys.indexOf(k) >= 0) {
tmpl[k] = v;
continue;
}
cacheKey.push(k);
//cacheKey.push(typeof v);
if (opts.preserveKeys.indexOf(k) >= 0) {
tmpl[k] = v;
cacheKey.push(JSON.stringify(v));
} else if (typeof v === "function") {
tmpl[k] = v;
} else if (v && typeof v === "object") {
const isArray = Array.isArray(v);
if (isArray) {
tmpl[k] = [];
cacheKey.push(`[${v.length}`);
} else {
tmpl[k] = {};
}
path.push(k);
gen(v, tmpl[k]);
isArray && cacheKey.push("]"); // eslint-disable-line
path.pop(k);
} else if (typeof v === "string") {
if (!v && opts.preserveEmptyKeys.indexOf(k) >= 0) {
// Sometimes components have logic dependent on strings being empty
// For example: It skips showing the UI to display a message if it's empty
tmpl[k] = v;
} else {
const templateValue = `@'${index}"@`;
if (config.stripUrlProtocol) {
const lv = v.toLowerCase();
if (lv.startsWith("http://")) { // eslint-disable-line
tmpl[k] = `http://${templateValue}`;
} else if (lv.startsWith("https://")) {
tmpl[k] = `https://${templateValue}`;
} else {
tmpl[k] = templateValue;
}
} else {
tmpl[k] = templateValue;
}
const lookupKey = `@${index}@`;
lookup[lookupKey] = path.concat(k);
cacheKey.push(`:${lookupKey}`);
index++;
}
} else if (v && opts.whiteListNonStringKeys.indexOf(k) >= 0) {
tmpl[k] = `@'${index}"@`;
const lookupKey = `@${index}@`;
cacheKey.push(`:${lookupKey}`);
lookup[lookupKey] = path.concat(k);
index++;
} else {
tmpl[k] = v;
cacheKey.push(JSON.stringify(v));
}
}
};
gen(props, template);
return {
template, lookup, cacheKey: cacheKey.join(",")
};
}
const replacements = {
"&": "&",
"<": "<",
">": ">",
"'": "'",
'"': """ // eslint-disable-line
};
ReactCompositeComponent._construct = ReactCompositeComponent.construct;
ReactCompositeComponent._mountComponent = ReactCompositeComponent.mountComponent;
ReactCompositeComponent.construct = function (element) {
if (config.enabled) {
if (config.profiling) {
this.__p = {time: undefined};
this.__profileTime = this.__realProfileTime;
}
this._name = element.type && typeof element.type !== "string" && element.type.name;
this.mountComponent = this._name ? this.mountComponentCache : this._mountComponent;
}
return this._construct(element);
};
ReactCompositeComponent.__profileTime = function () {
};
ReactCompositeComponent.__realProfileTime = function (start) {
const name = this._name;
const d = process.hrtime(start);
const owner = this._currentElement._owner;
if (owner) {
(owner.__p[name] || (owner.__p[name] = [])).push(this.__p);
} else {
(profileData[name] || (profileData[name] = [])).push(this.__p);
}
assert(this.__p.time === undefined);
this.__p.time = d[0] * 1000.0 + d[1] / 1000000.0;
};
const restoreRealProps = (r, lookup, realProps) => {
return r.replace(/(\@\'|\@')([0-9]+)(\"\@|\"\@)/g, (m, a, b) => {
let v = _.get(realProps, lookup[`@${b}@`]);
if (typeof v === "string") {
if (config.stripUrlProtocol) {
const lv = v.toLowerCase();
if (lv.startsWith("http://")) {
v = v.substr(7);
} else if (lv.startsWith("https://")) {
v = v.substr(8);
}
}
if (a === `@'`) {
return v;
}
return v.replace(/([&<'">])/g, (m2, c2) => {
return replacements[c2];
});
} else {
return v;
}
});
};
const MARKUP_FOR_ROOT = DOMPropertyOperations.createMarkupForRoot();
const addRootMarkup = (r) => {
return r.replace(/(<[^ >]*)([ >])/, (m, a, b) => `${a} ${MARKUP_FOR_ROOT}${b}`);
};
const REACT_ID_REPLACE_REGEX = new RegExp(`(data-reactid="|react-text: |react-empty: )[0-9]*`, "g");
const updateReactId = (r, hostContainerInfo) => { // eslint-disable-line
let id = hostContainerInfo._idCounter;
r = r.replace(REACT_ID_REPLACE_REGEX, (m, a) => `${a}${id++}`);
hostContainerInfo._idCounter = id;
return r;
};
const templatePostProcess = (r, lookup, realProps, transaction, hostContainerInfo) => { // eslint-disable-line
r = restoreRealProps(r, lookup, realProps);
return transaction.renderToStaticMarkup ? r : updateReactId(r, hostContainerInfo);
};
ReactCompositeComponent.mountComponentCache = function mountComponentCache(transaction, hostParent, hostContainerInfo, context, parentDebugID) { // eslint-disable-line
let template;
let cached;
let key;
let cacheType;
let opts;
const currentElement = Object.assign({}, this._currentElement);
this._currentElement = currentElement;
const saveProps = currentElement.props;
const name = this._name;
const startTime = config.profiling && process.hrtime();
// if props has children then can't cache
// if owner is already caching, then no need to cache
const canCache = (opts = cacheComponents[name]) && opts.enable && !(transaction._cached ||
_.isEmpty(saveProps) || typeof saveProps.children === "object");
const doCache = config.caching && canCache;
if (doCache) {
const strategy = opts.strategy;
if (strategy === "simple") {
cacheType = "cache";
template = {
cacheKey: opts.genCacheKey ? opts.genCacheKey(saveProps) : JSON.stringify(saveProps)
};
key = hashKeyFn(template.cacheKey);
cached = cacheStore.getEntry(name, key);
if (cached) {
this.__profileTime(startTime);
let r = cached.html;
if (!transaction.renderToStaticMarkup) {
r = updateReactId(cached.html, hostContainerInfo);
}
if (!hostParent) {
r = addRootMarkup(r);
}
return config.debug ?
`<!-- component ${name} cacheType HIT ${key} -->${r}` : r;
}
} else if (strategy === "template") {
cacheType = "cache";
template = generateTemplate(saveProps, opts);
key = hashKeyFn(template.cacheKey);
cached = cacheStore.getEntry(name, key);
if (cached) {
let r = templatePostProcess(cached.html, template.lookup,
saveProps, transaction, hostContainerInfo);
if (!hostParent) {
r = addRootMarkup(r);
}
this.__profileTime(startTime);
return config.debug ? `<!-- component ${name} cacheType HIT ${key} -->${r}` : r;
}
} else {
throw new Error(`Unknown caching strategy ${strategy} for component ${name}`);
}
} else if (transaction._cached) {
cacheType = "byParent";
} else {
cacheType = "NONE";
}
const saveId = hostContainerInfo._idCounter;
if (template) {
if (template.template) {
currentElement.props = template.template;
}
hostContainerInfo._idCounter = 1;
transaction._cached = this._name;
}
let r = this._mountComponent(transaction, hostParent, hostContainerInfo, context, parentDebugID);
if (template) {
hostContainerInfo._idCounter = saveId;
currentElement.props = saveProps;
cacheStore.newEntry(name, key, {html: r.replace(` ${MARKUP_FOR_ROOT}`, "")});
if (template.lookup) {
r = templatePostProcess(r, template.lookup,
saveProps, transaction, hostContainerInfo);
} else if (!transaction.renderToStaticMarkup) {
r = updateReactId(r, hostContainerInfo);
}
transaction._cached = undefined;
}
this.__profileTime(startTime);
if (config.caching && config.debug) {
return `<!-- component ${name} cacheType ${cacheType} ${key} -->${r}`;
} else {
return r;
}
};
exports.enableProfiling = function (flag) {
config.profiling = flag === undefined || !!flag;
config.enabled = config.profiling || config.caching;
};
exports.enableCaching = function (flag) {
config.caching = flag === undefined || !!flag;
config.enabled = config.profiling || config.caching;
};
exports.enableCachingDebug = function (flag) {
config.debug = flag === undefined || !!flag;
};
exports.stripUrlProtocol = function (flag) {
config.stripUrlProtocol = flag;
};
exports.profileData = profileData;
exports.clearProfileData = function () {
Object.keys(profileData).forEach((k) => {
delete profileData[k];
});
};
exports.clearCache = function () {
exports.cacheStore = cacheStore = new CacheStore(config);
};
exports.cacheSize = function () {
return cacheStore.size;
};
exports.cacheEntries = function () {
return cacheStore.entries;
};
exports.cacheHitReport = function () {
const hitReport = {};
Object.keys(cacheStore.cache).forEach((key) => {
const entry = cacheStore.cache[key];
hitReport[key] = {
hits: entry.hits
};
});
return hitReport;
};
exports.config = config;
exports.setCachingConfig = function (cfg) {
cacheComponents = cfg.components || {};
Object.keys(cacheComponents).forEach((k) => {
_.defaults(cacheComponents[k], {
preserveKeys: [],
preserveEmptyKeys: [],
ignoreKeys: [],
whiteListNonStringKeys: []
});
});
};
exports.blackListed = blackListed;
exports.clearCache();