@modern-js/prod-server 2.0.0-beta.0 → 2.0.0-beta.1

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.
Files changed (113) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/dist/js/modern/constants.js +0 -2
  3. package/dist/js/modern/index.js +0 -1
  4. package/dist/js/modern/libs/context/context.js +10 -56
  5. package/dist/js/modern/libs/hook-api/index.js +0 -31
  6. package/dist/js/modern/libs/hook-api/route.js +0 -8
  7. package/dist/js/modern/libs/hook-api/template.js +0 -16
  8. package/dist/js/modern/libs/loadConfig.js +3 -10
  9. package/dist/js/modern/libs/metrics.js +6 -6
  10. package/dist/js/modern/libs/proxy.js +7 -12
  11. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -1
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -9
  13. package/dist/js/modern/libs/render/cache/index.js +8 -16
  14. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -10
  15. package/dist/js/modern/libs/render/cache/spr.js +12 -62
  16. package/dist/js/modern/libs/render/cache/util.js +0 -6
  17. package/dist/js/modern/libs/render/index.js +6 -11
  18. package/dist/js/modern/libs/render/measure.js +1 -11
  19. package/dist/js/modern/libs/render/modern/index.js +2 -13
  20. package/dist/js/modern/libs/render/reader.js +13 -24
  21. package/dist/js/modern/libs/render/ssr.js +5 -6
  22. package/dist/js/modern/libs/render/static.js +6 -9
  23. package/dist/js/modern/libs/render/type.js +0 -1
  24. package/dist/js/modern/libs/route/index.js +8 -19
  25. package/dist/js/modern/libs/route/matcher.js +21 -29
  26. package/dist/js/modern/libs/route/route.js +0 -13
  27. package/dist/js/modern/libs/serve-file.js +5 -6
  28. package/dist/js/modern/server/index.js +19 -41
  29. package/dist/js/modern/server/modern-server-split.js +0 -11
  30. package/dist/js/modern/server/modern-server.js +83 -164
  31. package/dist/js/modern/utils.js +2 -21
  32. package/dist/js/modern/worker-server.js +34 -0
  33. package/dist/js/node/constants.js +0 -2
  34. package/dist/js/node/index.js +0 -10
  35. package/dist/js/node/libs/context/context.js +10 -65
  36. package/dist/js/node/libs/context/index.js +0 -3
  37. package/dist/js/node/libs/hook-api/index.js +0 -42
  38. package/dist/js/node/libs/hook-api/route.js +0 -10
  39. package/dist/js/node/libs/hook-api/template.js +0 -22
  40. package/dist/js/node/libs/loadConfig.js +3 -22
  41. package/dist/js/node/libs/metrics.js +6 -6
  42. package/dist/js/node/libs/proxy.js +7 -17
  43. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -5
  44. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -12
  45. package/dist/js/node/libs/render/cache/index.js +8 -22
  46. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -2
  47. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -14
  48. package/dist/js/node/libs/render/cache/spr.js +12 -71
  49. package/dist/js/node/libs/render/cache/util.js +0 -18
  50. package/dist/js/node/libs/render/index.js +6 -26
  51. package/dist/js/node/libs/render/measure.js +0 -17
  52. package/dist/js/node/libs/render/modern/index.js +2 -20
  53. package/dist/js/node/libs/render/reader.js +12 -39
  54. package/dist/js/node/libs/render/ssr.js +4 -16
  55. package/dist/js/node/libs/render/static.js +6 -18
  56. package/dist/js/node/libs/render/type.js +0 -1
  57. package/dist/js/node/libs/route/index.js +8 -22
  58. package/dist/js/node/libs/route/matcher.js +18 -34
  59. package/dist/js/node/libs/route/route.js +0 -15
  60. package/dist/js/node/libs/serve-file.js +5 -13
  61. package/dist/js/node/server/index.js +19 -61
  62. package/dist/js/node/server/modern-server-split.js +0 -13
  63. package/dist/js/node/server/modern-server.js +83 -193
  64. package/dist/js/node/utils.js +3 -46
  65. package/dist/js/node/worker-server.js +41 -0
  66. package/dist/js/treeshaking/constants.js +28 -0
  67. package/dist/js/treeshaking/index.js +13 -0
  68. package/dist/js/treeshaking/libs/context/context.js +243 -0
  69. package/dist/js/treeshaking/libs/context/index.js +5 -0
  70. package/dist/js/treeshaking/libs/hook-api/index.js +157 -0
  71. package/dist/js/treeshaking/libs/hook-api/route.js +33 -0
  72. package/dist/js/treeshaking/libs/hook-api/template.js +91 -0
  73. package/dist/js/treeshaking/libs/loadConfig.js +39 -0
  74. package/dist/js/treeshaking/libs/metrics.js +12 -0
  75. package/dist/js/treeshaking/libs/proxy.js +80 -0
  76. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +124 -0
  77. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +464 -0
  78. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +53 -0
  79. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +35 -0
  80. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +121 -0
  81. package/dist/js/treeshaking/libs/render/cache/index.js +184 -0
  82. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +30 -0
  83. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +46 -0
  84. package/dist/js/treeshaking/libs/render/cache/spr.js +362 -0
  85. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  86. package/dist/js/treeshaking/libs/render/cache/util.js +101 -0
  87. package/dist/js/treeshaking/libs/render/index.js +100 -0
  88. package/dist/js/treeshaking/libs/render/measure.js +61 -0
  89. package/dist/js/treeshaking/libs/render/modern/browser-list.js +7 -0
  90. package/dist/js/treeshaking/libs/render/modern/index.js +39 -0
  91. package/dist/js/treeshaking/libs/render/reader.js +191 -0
  92. package/dist/js/treeshaking/libs/render/ssr.js +98 -0
  93. package/dist/js/treeshaking/libs/render/static.js +84 -0
  94. package/dist/js/treeshaking/libs/render/type.js +6 -0
  95. package/dist/js/treeshaking/libs/route/index.js +94 -0
  96. package/dist/js/treeshaking/libs/route/matcher.js +113 -0
  97. package/dist/js/treeshaking/libs/route/route.js +26 -0
  98. package/dist/js/treeshaking/libs/serve-file.js +75 -0
  99. package/dist/js/treeshaking/server/index.js +343 -0
  100. package/dist/js/treeshaking/server/modern-server-split.js +152 -0
  101. package/dist/js/treeshaking/server/modern-server.js +945 -0
  102. package/dist/js/treeshaking/type.js +1 -0
  103. package/dist/js/treeshaking/utils.js +87 -0
  104. package/dist/js/treeshaking/worker-server.js +56 -0
  105. package/dist/types/index.d.ts +0 -2
  106. package/dist/types/libs/context/context.d.ts +0 -3
  107. package/dist/types/libs/loadConfig.d.ts +0 -1
  108. package/dist/types/libs/render/cache/index.d.ts +0 -2
  109. package/dist/types/libs/render/cache/spr.d.ts +0 -2
  110. package/dist/types/server/index.d.ts +0 -3
  111. package/dist/types/utils.d.ts +0 -1
  112. package/dist/types/worker-server.d.ts +16 -0
  113. package/package.json +28 -12
@@ -5,104 +5,75 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.createCache = createCache;
7
7
  exports.destroyCache = destroyCache;
8
-
9
8
  var _crypto = _interopRequireDefault(require("crypto"));
10
-
11
9
  var _lruCache = _interopRequireDefault(require("lru-cache"));
12
-
13
10
  var _utils = require("@modern-js/utils");
14
-
15
11
  var _util = require("./util");
16
-
17
12
  var _pageCaches = require("./page-caches");
18
-
19
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
-
21
14
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
22
-
23
15
  const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
24
16
  const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
25
17
  const BASE_LEVEL = 0;
26
18
  const QUERY_LEVEL = 1;
27
19
  const HEADER_LEVEL = 2;
28
20
  const QUERY_HEADER_LEVEL = 3;
29
-
30
21
  class CacheManager {
31
22
  constructor(cacheOptions) {
32
23
  _defineProperty(this, "cache", void 0);
33
-
34
24
  _defineProperty(this, "cacheOptions", void 0);
35
-
36
25
  _defineProperty(this, "find", (() => {
37
26
  {
38
27
  // eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
39
28
  const _this = this;
40
-
41
29
  return {
42
- [(0, _util.fname)(BASE_LEVEL)](context, cacheKey // data: CacheContent,
30
+ [(0, _util.fname)(BASE_LEVEL)](context, cacheKey
31
+ // data: CacheContent,
43
32
  ) {
44
33
  return _this.md5(cacheKey);
45
34
  },
46
-
47
35
  [(0, _util.fname)(QUERY_LEVEL)](context, cacheKey, data) {
48
36
  const queryFactor = _this.queryFactor(context, data);
49
-
50
37
  if (!queryFactor) {
51
38
  return null;
52
39
  }
53
-
54
40
  return _this.md5((0, _util.connectFactor)(cacheKey, queryFactor));
55
41
  },
56
-
57
42
  [(0, _util.fname)(HEADER_LEVEL)](context, cacheKey, data) {
58
43
  const headerFactor = _this.headerFactor(context, data);
59
-
60
44
  if (!headerFactor) {
61
45
  return null;
62
46
  }
63
-
64
47
  return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor));
65
48
  },
66
-
67
49
  [(0, _util.fname)(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
68
50
  const queryFactor = _this.queryFactor(context, data);
69
-
70
51
  const headerFactor = _this.headerFactor(context, data);
71
-
72
52
  if (!queryFactor || !headerFactor) {
73
53
  return null;
74
54
  }
75
-
76
55
  return _this.md5((0, _util.connectFactor)(cacheKey, headerFactor, queryFactor));
77
56
  }
78
-
79
57
  };
80
58
  }
81
59
  })());
82
-
83
60
  this.cacheOptions = cacheOptions;
84
61
  this.cache = new _lruCache.default({
85
62
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
86
-
87
63
  // 默认存 100M,最大 600M
88
64
  length(n) {
89
65
  const len = n.caches.keys().reduce((total, cur) => {
90
66
  var _n$caches$peek;
91
-
92
67
  return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
93
68
  }, 1);
94
69
  return len;
95
70
  }
96
-
97
71
  });
98
72
  }
99
-
100
73
  md5(content) {
101
74
  const md5 = _crypto.default.createHash('md5');
102
-
103
75
  return md5.update(content).digest('hex');
104
76
  }
105
-
106
77
  generateRequestKey(context) {
107
78
  const {
108
79
  pathname,
@@ -110,81 +81,63 @@ class CacheManager {
110
81
  } = context;
111
82
  return this.md5(`${pathname}_${entry}`);
112
83
  }
113
-
114
84
  replaceValue(value, matcher) {
115
85
  let final = value;
116
86
  Object.keys(matcher).some(replacer => {
117
87
  const reg = new RegExp(matcher[replacer]);
118
-
119
88
  if (reg.test(value)) {
120
89
  final = replacer;
121
90
  return true;
122
91
  }
123
-
124
92
  return false;
125
93
  });
126
94
  return final;
127
95
  }
128
-
129
96
  factor(keys, obj, matches = {}) {
130
97
  keys.sort();
131
98
  const getValue = (0, _util.valueFactory)(obj);
132
99
  const factorAry = keys.reduce((ary, key) => {
133
100
  let value = getValue(key) || '';
134
101
  const matcher = matches[key];
135
-
136
102
  if (matcher) {
137
103
  value = this.replaceValue(value, matcher);
138
104
  }
139
-
140
105
  return ary.concat([key, value]);
141
106
  }, []);
142
107
  return factorAry.join(',');
143
108
  }
144
-
145
109
  queryFactor(context, data) {
146
110
  var _data$includes, _data$matches;
147
-
148
111
  const queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
149
112
  const queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
150
-
151
113
  if (!queryKeys || queryKeys.length === 0) {
152
114
  return null;
153
115
  }
154
-
155
116
  const requestQuery = context.query;
156
117
  const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
157
118
  return queryFactor;
158
119
  }
159
-
160
120
  headerFactor(context, data) {
161
121
  var _data$includes2, _data$matches2;
162
-
163
122
  const headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
164
123
  const headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
165
-
166
124
  if (!headerKeys || headerKeys.length === 0) {
167
125
  return null;
168
126
  }
169
-
170
127
  const requestHeader = context.headers;
171
128
  const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
172
129
  return headerFactor;
173
130
  }
174
-
175
131
  async best(context, cacheKey, data) {
176
132
  const {
177
133
  level
178
134
  } = data;
179
135
  const cacheHash = this.find[(0, _util.fname)(level)](context, cacheKey, data);
180
-
181
136
  if (!cacheHash) {
182
137
  return null;
183
138
  }
184
-
185
139
  return data.caches.get(cacheHash);
186
140
  }
187
-
188
141
  createCacheContent(config, caches) {
189
142
  return {
190
143
  level: config.level,
@@ -195,21 +148,19 @@ class CacheManager {
195
148
  caches
196
149
  };
197
150
  }
198
-
199
151
  async get(context) {
200
152
  const cacheKey = this.generateRequestKey(context);
201
- const data = this.cache.get(cacheKey); // no cache key matched
153
+ const data = this.cache.get(cacheKey);
202
154
 
155
+ // no cache key matched
203
156
  if (!data) {
204
157
  return null;
205
158
  }
206
-
207
- const dest = await this.best(context, cacheKey, data); // no cache for current page with current config
208
-
159
+ const dest = await this.best(context, cacheKey, data);
160
+ // no cache for current page with current config
209
161
  if (!dest) {
210
162
  return null;
211
163
  }
212
-
213
164
  const {
214
165
  expireTime,
215
166
  limitTime,
@@ -226,27 +177,24 @@ class CacheManager {
226
177
  hash: cacheHash
227
178
  };
228
179
  }
229
-
230
180
  async set(context, html, cacheConfig, sync = false) {
231
181
  if (!cacheConfig) {
232
182
  return false;
233
- } // each version with route is a separate cache
234
-
183
+ }
235
184
 
185
+ // each version with route is a separate cache
236
186
  const cacheKey = this.generateRequestKey(context);
237
187
  let data = this.cache.get(cacheKey);
238
-
239
188
  if (!data) {
240
189
  const caches = await (0, _pageCaches.createPageCaches)(MAX_CACHE_EACH_REQ);
241
190
  data = this.createCacheContent(cacheConfig, caches);
242
191
  }
192
+ const cacheHash = this.find[(0, _util.fname)(cacheConfig.level)](context, cacheKey, data);
243
193
 
244
- const cacheHash = this.find[(0, _util.fname)(cacheConfig.level)](context, cacheKey, data); // if cacheHash is null, maybe level not match meta key, do not cache
245
-
194
+ // if cacheHash is null, maybe level not match meta key, do not cache
246
195
  if (!cacheHash) {
247
196
  return false;
248
197
  }
249
-
250
198
  const cacheSyncOrAsync = async () => {
251
199
  const next = data;
252
200
  const limit = cacheConfig.staleLimit;
@@ -261,37 +209,30 @@ class CacheManager {
261
209
  });
262
210
  this.cache.set(cacheKey, next);
263
211
  return true;
264
- }; // cache set is async, each hash is cached only once at the same time
265
-
212
+ };
266
213
 
214
+ // cache set is async, each hash is cached only once at the same time
267
215
  const doCache = (0, _util.withCoalescedInvoke)(cacheSyncOrAsync).bind(null, (0, _util.namespaceHash)('stream', cacheHash), []);
268
216
  return (0, _util.maybeSync)(doCache)(sync);
269
217
  }
270
-
271
218
  async del(context, cacheHash) {
272
219
  const cacheKey = this.generateRequestKey(context);
273
220
  const data = this.cache.get(cacheKey);
274
221
  data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
275
222
  }
276
-
277
223
  }
278
-
279
224
  let manager;
280
-
281
225
  function createCache() {
282
226
  if (manager) {
283
227
  return manager;
284
228
  }
285
-
286
229
  manager = new CacheManager({
287
230
  max: 0
288
231
  });
289
232
  return manager;
290
233
  }
291
-
292
234
  function destroyCache() {
293
235
  manager = null;
294
236
  }
295
237
  /* eslint-enable no-lone-blocks */
296
-
297
238
  /* eslint-enable @typescript-eslint/member-ordering */
@@ -11,23 +11,17 @@ exports.maybeSync = maybeSync;
11
11
  exports.namespaceHash = namespaceHash;
12
12
  exports.valueFactory = valueFactory;
13
13
  exports.withCoalescedInvoke = withCoalescedInvoke;
14
-
15
14
  var _url = _interopRequireDefault(require("url"));
16
-
17
15
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
-
19
16
  function namespaceHash(namespace, hash) {
20
17
  return `${namespace}/${hash}`;
21
18
  }
22
-
23
19
  function fname(lv) {
24
20
  return `f${lv}`;
25
21
  }
26
-
27
22
  function connectFactor(...args) {
28
23
  return args.join('-');
29
24
  }
30
-
31
25
  function valueFactory(obj) {
32
26
  if (obj instanceof _url.default.URLSearchParams) {
33
27
  return function (key) {
@@ -36,44 +30,34 @@ function valueFactory(obj) {
36
30
  } else {
37
31
  return function (key) {
38
32
  const value = obj[key];
39
-
40
33
  if (Array.isArray(value)) {
41
34
  return value.join(',');
42
35
  }
43
-
44
36
  return value;
45
37
  };
46
38
  }
47
39
  }
48
-
49
40
  function getTime([s, ns]) {
50
41
  return Math.floor(s * 1e3 + ns / 1e6);
51
42
  }
52
-
53
43
  const RE_START_IN_HEAD = /<head>/;
54
-
55
44
  function cacheAddition(html, hash) {
56
45
  const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
57
46
  return additionHtml;
58
47
  }
59
-
60
48
  const globalInvokeCache = new Map();
61
-
62
49
  function withCoalescedInvoke(func) {
63
50
  return async function (key, args) {
64
51
  const entry = globalInvokeCache.get(key);
65
-
66
52
  if (entry) {
67
53
  return entry.then(res => ({
68
54
  isOrigin: false,
69
55
  value: res.value
70
56
  }));
71
57
  }
72
-
73
58
  function __wrapper() {
74
59
  return func(...args);
75
60
  }
76
-
77
61
  const future = __wrapper().then(res => {
78
62
  globalInvokeCache.delete(key);
79
63
  return {
@@ -84,12 +68,10 @@ function withCoalescedInvoke(func) {
84
68
  globalInvokeCache.delete(key);
85
69
  throw err;
86
70
  });
87
-
88
71
  globalInvokeCache.set(key, future);
89
72
  return future;
90
73
  };
91
74
  }
92
-
93
75
  function maybeSync(fn) {
94
76
  return sync => {
95
77
  if (sync) {
@@ -4,27 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createRenderHandler = void 0;
7
-
8
7
  var _path = _interopRequireDefault(require("path"));
9
-
10
8
  var _utils = require("@modern-js/utils");
11
-
12
9
  var _constants = require("../../constants");
13
-
14
10
  var _static = require("./static");
15
-
16
11
  var _reader = require("./reader");
17
-
18
12
  var ssr = _interopRequireWildcard(require("./ssr"));
19
-
20
13
  var _modern = require("./modern");
21
-
22
14
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
23
-
24
15
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
25
-
26
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
-
28
17
  const createRenderHandler = ({
29
18
  distDir,
30
19
  staticGenerate
@@ -36,38 +25,31 @@ const createRenderHandler = ({
36
25
  if (ctx.resHasHandled()) {
37
26
  return null;
38
27
  }
39
-
40
28
  const {
41
29
  entryPath,
42
30
  urlPath
43
31
  } = route;
44
-
45
32
  const entry = _path.default.join(distDir, entryPath);
46
-
47
33
  if (!route.isSPA) {
48
34
  const result = await (0, _static.handleDirectory)(ctx, entry, urlPath);
49
35
  return result;
50
- } // only spa can use es6-html
51
-
36
+ }
52
37
 
38
+ // only spa can use es6-html
53
39
  const modernEntry = (0, _modern.getModernEntry)(entry);
54
-
55
- const useModern = // route.enableModernMode &&
40
+ const useModern =
41
+ // route.enableModernMode &&
56
42
  (0, _modern.supportModern)(ctx) && _utils.fs.existsSync(modernEntry);
57
-
58
43
  const templatePath = useModern ? modernEntry : entry;
59
-
60
44
  if (!_utils.fs.existsSync(templatePath)) {
61
45
  throw new Error(`Could not find template file: ${templatePath}`);
62
46
  }
63
-
64
47
  const content = await (0, _reader.readFile)(templatePath);
65
-
66
48
  if (!content) {
67
49
  return null;
68
- } // handles ssr first
69
-
50
+ }
70
51
 
52
+ // handles ssr first
71
53
  if (route.isSSR) {
72
54
  try {
73
55
  const result = await ssr.render(ctx, {
@@ -84,11 +66,9 @@ const createRenderHandler = ({
84
66
  ctx.res.setHeader('x-modern-ssr-fallback', '1');
85
67
  }
86
68
  }
87
-
88
69
  return {
89
70
  content,
90
71
  contentType: _utils.mime.contentType(_path.default.extname(templatePath))
91
72
  };
92
73
  };
93
-
94
74
  exports.createRenderHandler = createRenderHandler;
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.createMetrics = exports.createLogger = void 0;
7
-
8
7
  var _utils = require("../../utils");
9
-
10
8
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
11
-
12
9
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
13
-
14
10
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
15
-
16
11
  const createMetrics = (context, metrics) => {
17
12
  const {
18
13
  entryName: entry,
@@ -21,53 +16,43 @@ const createMetrics = (context, metrics) => {
21
16
  const {
22
17
  pathname = ''
23
18
  } = request || {};
24
-
25
19
  const emitTimer = (name, cost, tags = {}) => {
26
20
  metrics.emitTimer(name, cost, _objectSpread(_objectSpread({}, tags), {}, {
27
21
  pathname,
28
22
  entry
29
23
  }));
30
24
  };
31
-
32
25
  const emitCounter = (name, counter, tags = {}) => {
33
26
  metrics.emitCounter(name, counter, _objectSpread(_objectSpread({}, tags), {}, {
34
27
  pathname,
35
28
  entry
36
29
  }));
37
30
  };
38
-
39
31
  return {
40
32
  emitTimer,
41
33
  emitCounter
42
34
  };
43
35
  };
44
-
45
36
  exports.createMetrics = createMetrics;
46
-
47
37
  const createLogger = (serverContext, logger) => {
48
38
  const request = serverContext.request || {};
49
39
  const {
50
40
  headers = {},
51
41
  pathname = ''
52
42
  } = request;
53
-
54
43
  const debug = (message, ...args) => {
55
44
  logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
56
45
  };
57
-
58
46
  const info = (message, ...args) => {
59
47
  logger.info(`SSR Info - ${message}, req.url = %s`, ...args, pathname);
60
48
  };
61
-
62
49
  const error = (message, e) => {
63
50
  if (!e) {
64
51
  e = message;
65
52
  message = '';
66
53
  }
67
-
68
54
  logger.error(`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, pathname, (0, _utils.headersWithoutCookie)(headers));
69
55
  };
70
-
71
56
  return {
72
57
  error,
73
58
  info,
@@ -75,6 +60,4 @@ const createLogger = (serverContext, logger) => {
75
60
  };
76
61
  };
77
62
  /* eslint-enable no-param-reassign */
78
-
79
-
80
63
  exports.createLogger = createLogger;
@@ -4,51 +4,36 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.supportModern = exports.getModernEntry = void 0;
7
-
8
7
  var _uaParserJs = _interopRequireDefault(require("ua-parser-js"));
9
-
10
8
  var _compareVersions = _interopRequireDefault(require("compare-versions"));
11
-
12
9
  var _browserList = require("./browser-list");
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  const nativeModules = require('@babel/compat-data/native-modules');
17
-
18
12
  const supportModern = context => {
19
13
  var _context$query;
20
-
21
14
  if ((_context$query = context.query) !== null && _context$query !== void 0 && _context$query.modern_es6) {
22
15
  return true;
23
- } // no ua in request headers
24
-
16
+ }
25
17
 
18
+ // no ua in request headers
26
19
  const userAgent = context.headers['user-agent'];
27
-
28
20
  if (!userAgent || typeof userAgent !== 'string') {
29
21
  return false;
30
22
  }
31
-
32
23
  const parsedUA = (0, _uaParserJs.default)(userAgent);
33
24
  const browserName = parsedUA.browser.name;
34
25
  const browserVersion = parsedUA.browser.version;
35
-
36
26
  if (!browserName || !browserVersion) {
37
27
  return false;
38
28
  }
39
-
40
29
  const nativeUAName = _browserList.NativeModuleNameMap[browserName];
41
-
42
30
  if (!nativeUAName) {
43
31
  return false;
44
32
  }
45
-
46
33
  const version = nativeModules['es6.module'][nativeUAName];
47
-
48
34
  if (!version) {
49
35
  return false;
50
36
  }
51
-
52
37
  try {
53
38
  const result = (0, _compareVersions.default)(browserVersion, version);
54
39
  return result >= 0;
@@ -56,9 +41,6 @@ const supportModern = context => {
56
41
  return false;
57
42
  }
58
43
  };
59
-
60
44
  exports.supportModern = supportModern;
61
-
62
45
  const getModernEntry = filepath => filepath.replace(/\.html$/, '-es6.html');
63
-
64
46
  exports.getModernEntry = getModernEntry;