@modern-js/prod-server 1.21.6 → 2.0.0-alpha.0

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 (132) hide show
  1. package/CHANGELOG.md +274 -13
  2. package/dist/js/modern/constants.js +32 -27
  3. package/dist/js/modern/index.js +11 -7
  4. package/dist/js/modern/libs/context/context.js +54 -132
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +134 -0
  7. package/dist/js/modern/libs/hook-api/route.js +13 -37
  8. package/dist/js/modern/libs/hook-api/template.js +41 -32
  9. package/dist/js/modern/libs/loadConfig.js +46 -32
  10. package/dist/js/modern/libs/metrics.js +6 -7
  11. package/dist/js/modern/libs/proxy.js +70 -44
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -68
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -225
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +43 -49
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +36 -34
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +83 -113
  17. package/dist/js/modern/libs/render/cache/index.js +93 -50
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +31 -8
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +6 -16
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -167
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -45
  23. package/dist/js/modern/libs/render/index.js +76 -61
  24. package/dist/js/modern/libs/render/measure.js +42 -34
  25. package/dist/js/modern/libs/render/reader.js +68 -76
  26. package/dist/js/modern/libs/render/ssr.js +63 -33
  27. package/dist/js/modern/libs/render/static.js +51 -37
  28. package/dist/js/modern/libs/render/type.js +9 -7
  29. package/dist/js/modern/libs/route/index.js +12 -30
  30. package/dist/js/modern/libs/route/matcher.js +28 -50
  31. package/dist/js/modern/libs/route/route.js +9 -31
  32. package/dist/js/modern/libs/serve-file.js +40 -20
  33. package/dist/js/modern/server/index.js +152 -168
  34. package/dist/js/modern/server/modern-server-split.js +44 -60
  35. package/dist/js/modern/server/modern-server.js +416 -526
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -58
  38. package/dist/js/modern/worker-server.js +54 -0
  39. package/dist/js/node/constants.js +53 -32
  40. package/dist/js/node/index.js +37 -67
  41. package/dist/js/node/libs/context/context.js +84 -150
  42. package/dist/js/node/libs/context/index.js +28 -16
  43. package/dist/js/node/libs/hook-api/index.js +164 -0
  44. package/dist/js/node/libs/hook-api/route.js +35 -45
  45. package/dist/js/node/libs/hook-api/template.js +64 -40
  46. package/dist/js/node/libs/loadConfig.js +74 -55
  47. package/dist/js/node/libs/metrics.js +28 -12
  48. package/dist/js/node/libs/proxy.js +90 -55
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -61
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -140
  51. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +65 -55
  52. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +58 -40
  53. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +105 -119
  54. package/dist/js/node/libs/render/cache/index.js +115 -65
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -16
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -26
  57. package/dist/js/node/libs/render/cache/spr.js +161 -188
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -63
  60. package/dist/js/node/libs/render/index.js +106 -87
  61. package/dist/js/node/libs/render/measure.js +61 -44
  62. package/dist/js/node/libs/render/reader.js +98 -100
  63. package/dist/js/node/libs/render/ssr.js +92 -58
  64. package/dist/js/node/libs/render/static.js +80 -53
  65. package/dist/js/node/libs/render/type.js +31 -13
  66. package/dist/js/node/libs/route/index.js +35 -44
  67. package/dist/js/node/libs/route/matcher.js +48 -65
  68. package/dist/js/node/libs/route/route.js +29 -37
  69. package/dist/js/node/libs/serve-file.js +74 -37
  70. package/dist/js/node/server/index.js +170 -203
  71. package/dist/js/node/server/modern-server-split.js +70 -73
  72. package/dist/js/node/server/modern-server.js +432 -577
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +87 -93
  75. package/dist/js/node/worker-server.js +77 -0
  76. package/dist/js/treeshaking/constants.js +29 -0
  77. package/dist/js/treeshaking/index.js +13 -0
  78. package/dist/js/treeshaking/libs/context/context.js +274 -0
  79. package/dist/js/treeshaking/libs/context/index.js +5 -0
  80. package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
  81. package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
  82. package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
  83. package/dist/js/treeshaking/libs/loadConfig.js +82 -0
  84. package/dist/js/treeshaking/libs/metrics.js +6 -0
  85. package/dist/js/treeshaking/libs/proxy.js +244 -0
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
  91. package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  96. package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
  97. package/dist/js/treeshaking/libs/render/index.js +233 -0
  98. package/dist/js/treeshaking/libs/render/measure.js +146 -0
  99. package/dist/js/treeshaking/libs/render/reader.js +339 -0
  100. package/dist/js/treeshaking/libs/render/ssr.js +223 -0
  101. package/dist/js/treeshaking/libs/render/static.js +216 -0
  102. package/dist/js/treeshaking/libs/render/type.js +7 -0
  103. package/dist/js/treeshaking/libs/route/index.js +130 -0
  104. package/dist/js/treeshaking/libs/route/matcher.js +138 -0
  105. package/dist/js/treeshaking/libs/route/route.js +40 -0
  106. package/dist/js/treeshaking/libs/serve-file.js +184 -0
  107. package/dist/js/treeshaking/server/index.js +505 -0
  108. package/dist/js/treeshaking/server/modern-server-split.js +360 -0
  109. package/dist/js/treeshaking/server/modern-server.js +1083 -0
  110. package/dist/js/treeshaking/type.js +1 -0
  111. package/dist/js/treeshaking/utils.js +147 -0
  112. package/dist/js/treeshaking/worker-server.js +177 -0
  113. package/dist/types/libs/context/context.d.ts +1 -1
  114. package/dist/types/libs/hook-api/index.d.ts +5 -0
  115. package/dist/types/libs/hook-api/route.d.ts +9 -14
  116. package/dist/types/libs/hook-api/template.d.ts +19 -9
  117. package/dist/types/libs/render/cache/index.d.ts +4 -2
  118. package/dist/types/libs/render/type.d.ts +3 -1
  119. package/dist/types/libs/route/route.d.ts +0 -1
  120. package/dist/types/libs/serve-file.d.ts +2 -1
  121. package/dist/types/server/index.d.ts +2 -0
  122. package/dist/types/server/modern-server.d.ts +11 -11
  123. package/dist/types/type.d.ts +8 -10
  124. package/dist/types/utils.d.ts +3 -4
  125. package/dist/types/worker-server.d.ts +15 -0
  126. package/package.json +31 -45
  127. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/modern/libs/render/modern/index.js +0 -48
  129. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  130. package/dist/js/node/libs/render/modern/index.js +0 -64
  131. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  132. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,12 +1,35 @@
1
- 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; }
2
-
3
- /* eslint-disable @typescript-eslint/member-ordering */
4
-
5
- /* eslint-disable no-lone-blocks */
6
- import crypto from 'crypto';
7
- import LRUCache from 'lru-cache';
8
- import { mime } from '@modern-js/utils';
9
- import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "./util";
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import crypto from "crypto";
22
+ import LRUCache from "lru-cache";
23
+ import { mime } from "@modern-js/utils";
24
+ import {
25
+ cacheAddition,
26
+ connectFactor,
27
+ fname,
28
+ maybeSync,
29
+ namespaceHash,
30
+ valueFactory,
31
+ withCoalescedInvoke
32
+ } from "./util";
10
33
  import { createPageCaches } from "./page-caches";
11
34
  const MAX_CACHE_EACH_REQ = Number(process.env.ROUTE_CACHE_LIMIT) || 10;
12
35
  const MAX_SIZE_EACH_CLUSTER = Number(process.env.CLUSTER_CACHE_LIMIT) || 100;
@@ -14,164 +37,117 @@ const BASE_LEVEL = 0;
14
37
  const QUERY_LEVEL = 1;
15
38
  const HEADER_LEVEL = 2;
16
39
  const QUERY_HEADER_LEVEL = 3;
17
-
18
40
  class CacheManager {
19
41
  constructor(cacheOptions) {
20
- _defineProperty(this, "cache", void 0);
21
-
22
- _defineProperty(this, "cacheOptions", void 0);
23
-
24
- _defineProperty(this, "find", (() => {
42
+ this.find = (() => {
25
43
  {
26
- // eslint-disable-next-line consistent-this,@typescript-eslint/no-this-alias
27
44
  const _this = this;
28
-
29
45
  return {
30
- [fname(BASE_LEVEL)](context, cacheKey // data: CacheContent,
31
- ) {
46
+ [fname(BASE_LEVEL)](context, cacheKey) {
32
47
  return _this.md5(cacheKey);
33
48
  },
34
-
35
49
  [fname(QUERY_LEVEL)](context, cacheKey, data) {
36
50
  const queryFactor = _this.queryFactor(context, data);
37
-
38
51
  if (!queryFactor) {
39
52
  return null;
40
53
  }
41
-
42
54
  return _this.md5(connectFactor(cacheKey, queryFactor));
43
55
  },
44
-
45
56
  [fname(HEADER_LEVEL)](context, cacheKey, data) {
46
57
  const headerFactor = _this.headerFactor(context, data);
47
-
48
58
  if (!headerFactor) {
49
59
  return null;
50
60
  }
51
-
52
61
  return _this.md5(connectFactor(cacheKey, headerFactor));
53
62
  },
54
-
55
63
  [fname(QUERY_HEADER_LEVEL)](context, cacheKey, data) {
56
64
  const queryFactor = _this.queryFactor(context, data);
57
-
58
65
  const headerFactor = _this.headerFactor(context, data);
59
-
60
66
  if (!queryFactor || !headerFactor) {
61
67
  return null;
62
68
  }
63
-
64
69
  return _this.md5(connectFactor(cacheKey, headerFactor, queryFactor));
65
70
  }
66
-
67
71
  };
68
72
  }
69
- })());
70
-
73
+ })();
71
74
  this.cacheOptions = cacheOptions;
72
75
  this.cache = new LRUCache({
73
76
  max: Math.min(MAX_SIZE_EACH_CLUSTER, 600) * 1024 * 1024,
74
-
75
- // 默认存 100M,最大 600M
76
77
  length(n) {
77
78
  const len = n.caches.keys().reduce((total, cur) => {
78
- var _n$caches$peek;
79
-
80
- return total + (((_n$caches$peek = n.caches.peek(cur)) === null || _n$caches$peek === void 0 ? void 0 : _n$caches$peek.size) || 0);
79
+ var _a;
80
+ return total + (((_a = n.caches.peek(cur)) == null ? void 0 : _a.size) || 0);
81
81
  }, 1);
82
82
  return len;
83
83
  }
84
-
85
84
  });
86
85
  }
87
-
88
86
  md5(content) {
89
- const md5 = crypto.createHash('md5');
90
- return md5.update(content).digest('hex');
87
+ const md5 = crypto.createHash("md5");
88
+ return md5.update(content).digest("hex");
91
89
  }
92
-
93
90
  generateRequestKey(context) {
94
- const {
95
- pathname,
96
- entry
97
- } = context;
91
+ const { pathname, entry } = context;
98
92
  return this.md5(`${pathname}_${entry}`);
99
93
  }
100
-
101
94
  replaceValue(value, matcher) {
102
95
  let final = value;
103
- Object.keys(matcher).some(replacer => {
96
+ Object.keys(matcher).some((replacer) => {
104
97
  const reg = new RegExp(matcher[replacer]);
105
-
106
98
  if (reg.test(value)) {
107
99
  final = replacer;
108
100
  return true;
109
101
  }
110
-
111
102
  return false;
112
103
  });
113
104
  return final;
114
105
  }
115
-
116
106
  factor(keys, obj, matches = {}) {
117
107
  keys.sort();
118
108
  const getValue = valueFactory(obj);
119
109
  const factorAry = keys.reduce((ary, key) => {
120
- let value = getValue(key) || '';
110
+ let value = getValue(key) || "";
121
111
  const matcher = matches[key];
122
-
123
112
  if (matcher) {
124
113
  value = this.replaceValue(value, matcher);
125
114
  }
126
-
127
115
  return ary.concat([key, value]);
128
116
  }, []);
129
- return factorAry.join(',');
117
+ return factorAry.join(",");
130
118
  }
131
-
132
119
  queryFactor(context, data) {
133
- var _data$includes, _data$matches;
134
-
135
- const queryKeys = (_data$includes = data.includes) === null || _data$includes === void 0 ? void 0 : _data$includes.query;
136
- const queryMatches = (_data$matches = data.matches) === null || _data$matches === void 0 ? void 0 : _data$matches.query;
137
-
120
+ var _a, _b;
121
+ const queryKeys = (_a = data.includes) == null ? void 0 : _a.query;
122
+ const queryMatches = (_b = data.matches) == null ? void 0 : _b.query;
138
123
  if (!queryKeys || queryKeys.length === 0) {
139
124
  return null;
140
125
  }
141
-
142
126
  const requestQuery = context.query;
143
127
  const queryFactor = this.factor(queryKeys, requestQuery, queryMatches);
144
128
  return queryFactor;
145
129
  }
146
-
147
130
  headerFactor(context, data) {
148
- var _data$includes2, _data$matches2;
149
-
150
- const headerKeys = (_data$includes2 = data.includes) === null || _data$includes2 === void 0 ? void 0 : _data$includes2.header;
151
- const headerMatches = (_data$matches2 = data.matches) === null || _data$matches2 === void 0 ? void 0 : _data$matches2.header;
152
-
131
+ var _a, _b;
132
+ const headerKeys = (_a = data.includes) == null ? void 0 : _a.header;
133
+ const headerMatches = (_b = data.matches) == null ? void 0 : _b.header;
153
134
  if (!headerKeys || headerKeys.length === 0) {
154
135
  return null;
155
136
  }
156
-
157
137
  const requestHeader = context.headers;
158
138
  const headerFactor = this.factor(headerKeys, requestHeader, headerMatches);
159
139
  return headerFactor;
160
140
  }
161
-
162
- async best(context, cacheKey, data) {
163
- const {
164
- level
165
- } = data;
166
- const cacheHash = this.find[fname(level)](context, cacheKey, data);
167
-
168
- if (!cacheHash) {
169
- return null;
170
- }
171
-
172
- return data.caches.get(cacheHash);
141
+ best(context, cacheKey, data) {
142
+ return __async(this, null, function* () {
143
+ const { level } = data;
144
+ const cacheHash = this.find[fname(level)](context, cacheKey, data);
145
+ if (!cacheHash) {
146
+ return null;
147
+ }
148
+ return data.caches.get(cacheHash);
149
+ });
173
150
  }
174
-
175
151
  createCacheContent(config, caches) {
176
152
  return {
177
153
  level: config.level,
@@ -182,101 +158,91 @@ class CacheManager {
182
158
  caches
183
159
  };
184
160
  }
185
-
186
- async get(context) {
187
- const cacheKey = this.generateRequestKey(context);
188
- const data = this.cache.get(cacheKey); // no cache key matched
189
-
190
- if (!data) {
191
- return null;
192
- }
193
-
194
- const dest = await this.best(context, cacheKey, data); // no cache for current page with current config
195
-
196
- if (!dest) {
197
- return null;
198
- }
199
-
200
- const {
201
- expireTime,
202
- limitTime,
203
- html,
204
- cacheHash
205
- } = dest;
206
- const isStale = Date.now() - expireTime > 0;
207
- const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
208
- return {
209
- content: html || '',
210
- contentType: mime.contentType('html'),
211
- isStale,
212
- isGarbage,
213
- hash: cacheHash
214
- };
161
+ get(context) {
162
+ return __async(this, null, function* () {
163
+ const cacheKey = this.generateRequestKey(context);
164
+ const data = this.cache.get(cacheKey);
165
+ if (!data) {
166
+ return null;
167
+ }
168
+ const dest = yield this.best(context, cacheKey, data);
169
+ if (!dest) {
170
+ return null;
171
+ }
172
+ const { expireTime, limitTime, html, cacheHash } = dest;
173
+ const isStale = Date.now() - expireTime > 0;
174
+ const isGarbage = limitTime ? Date.now() - limitTime > 0 : false;
175
+ return {
176
+ content: html || "",
177
+ contentType: mime.contentType("html"),
178
+ isStale,
179
+ isGarbage,
180
+ hash: cacheHash
181
+ };
182
+ });
215
183
  }
216
-
217
- async set(context, html, cacheConfig, sync = false) {
218
- if (!cacheConfig) {
219
- return false;
220
- } // each version with route is a separate cache
221
-
222
-
223
- const cacheKey = this.generateRequestKey(context);
224
- let data = this.cache.get(cacheKey);
225
-
226
- if (!data) {
227
- const caches = await createPageCaches(MAX_CACHE_EACH_REQ);
228
- data = this.createCacheContent(cacheConfig, caches);
229
- }
230
-
231
- const cacheHash = this.find[fname(cacheConfig.level)](context, cacheKey, data); // if cacheHash is null, maybe level not match meta key, do not cache
232
-
233
- if (!cacheHash) {
234
- return false;
235
- }
236
-
237
- const cacheSyncOrAsync = async () => {
238
- const next = data;
239
- const limit = cacheConfig.staleLimit;
240
- const storeHTML = cacheAddition(html, cacheHash);
241
- const size = storeHTML.length;
242
- await next.caches.set(cacheHash, {
243
- expireTime: Date.now() + cacheConfig.interval * 1000,
244
- limitTime: typeof limit === 'number' ? Date.now() + limit * 1000 : null,
245
- cacheHash,
246
- html: storeHTML,
247
- size
184
+ set(context, html, cacheConfig, sync = false) {
185
+ return __async(this, null, function* () {
186
+ if (!cacheConfig) {
187
+ return false;
188
+ }
189
+ const cacheKey = this.generateRequestKey(context);
190
+ let data = this.cache.get(cacheKey);
191
+ if (!data) {
192
+ const caches = yield createPageCaches(MAX_CACHE_EACH_REQ);
193
+ data = this.createCacheContent(cacheConfig, caches);
194
+ }
195
+ const cacheHash = this.find[fname(cacheConfig.level)](
196
+ context,
197
+ cacheKey,
198
+ data
199
+ );
200
+ if (!cacheHash) {
201
+ return false;
202
+ }
203
+ const cacheSyncOrAsync = () => __async(this, null, function* () {
204
+ const next = data;
205
+ const limit = cacheConfig.staleLimit;
206
+ const storeHTML = cacheAddition(html, cacheHash);
207
+ const size = storeHTML.length;
208
+ yield next.caches.set(cacheHash, {
209
+ expireTime: Date.now() + cacheConfig.interval * 1e3,
210
+ limitTime: typeof limit === "number" ? Date.now() + limit * 1e3 : null,
211
+ cacheHash,
212
+ html: storeHTML,
213
+ size
214
+ });
215
+ this.cache.set(cacheKey, next);
216
+ return true;
248
217
  });
249
- this.cache.set(cacheKey, next);
250
- return true;
251
- }; // cache set is async, each hash is cached only once at the same time
252
-
253
-
254
- const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(null, namespaceHash('stream', cacheHash), []);
255
- return maybeSync(doCache)(sync);
218
+ const doCache = withCoalescedInvoke(cacheSyncOrAsync).bind(
219
+ null,
220
+ namespaceHash("stream", cacheHash),
221
+ []
222
+ );
223
+ return maybeSync(doCache)(sync);
224
+ });
256
225
  }
257
-
258
- async del(context, cacheHash) {
259
- const cacheKey = this.generateRequestKey(context);
260
- const data = this.cache.get(cacheKey);
261
- data === null || data === void 0 ? void 0 : data.caches.del(cacheHash);
226
+ del(context, cacheHash) {
227
+ return __async(this, null, function* () {
228
+ const cacheKey = this.generateRequestKey(context);
229
+ const data = this.cache.get(cacheKey);
230
+ data == null ? void 0 : data.caches.del(cacheHash);
231
+ });
262
232
  }
263
-
264
233
  }
265
-
266
234
  let manager;
267
- export function createCache() {
235
+ function createCache() {
268
236
  if (manager) {
269
237
  return manager;
270
238
  }
271
-
272
- manager = new CacheManager({
273
- max: 0
274
- });
239
+ manager = new CacheManager({ max: 0 });
275
240
  return manager;
276
241
  }
277
- export function destroyCache() {
242
+ function destroyCache() {
278
243
  manager = null;
279
244
  }
280
- /* eslint-enable no-lone-blocks */
281
-
282
- /* eslint-enable @typescript-eslint/member-ordering */
245
+ export {
246
+ createCache,
247
+ destroyCache
248
+ };
@@ -1 +0,0 @@
1
- export {};
@@ -1,71 +1,87 @@
1
- import url from 'url';
2
- export function namespaceHash(namespace, hash) {
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import url from "url";
22
+ function namespaceHash(namespace, hash) {
3
23
  return `${namespace}/${hash}`;
4
24
  }
5
- export function fname(lv) {
25
+ function fname(lv) {
6
26
  return `f${lv}`;
7
27
  }
8
- export function connectFactor(...args) {
9
- return args.join('-');
28
+ function connectFactor(...args) {
29
+ return args.join("-");
10
30
  }
11
- export function valueFactory(obj) {
31
+ function valueFactory(obj) {
12
32
  if (obj instanceof url.URLSearchParams) {
13
- return function (key) {
33
+ return function(key) {
14
34
  return obj.get(key);
15
35
  };
16
36
  } else {
17
- return function (key) {
37
+ return function(key) {
18
38
  const value = obj[key];
19
-
20
39
  if (Array.isArray(value)) {
21
- return value.join(',');
40
+ return value.join(",");
22
41
  }
23
-
24
42
  return value;
25
43
  };
26
44
  }
27
45
  }
28
- export function getTime([s, ns]) {
46
+ function getTime([s, ns]) {
29
47
  return Math.floor(s * 1e3 + ns / 1e6);
30
48
  }
31
49
  const RE_START_IN_HEAD = /<head>/;
32
- export function cacheAddition(html, hash) {
33
- const additionHtml = html.replace(RE_START_IN_HEAD, `<head><meta name="x-moden-spr" content="${hash}">`);
50
+ function cacheAddition(html, hash) {
51
+ const additionHtml = html.replace(
52
+ RE_START_IN_HEAD,
53
+ `<head><meta name="x-moden-spr" content="${hash}">`
54
+ );
34
55
  return additionHtml;
35
56
  }
36
- const globalInvokeCache = new Map();
37
- export function withCoalescedInvoke(func) {
38
- return async function (key, args) {
39
- const entry = globalInvokeCache.get(key);
40
-
41
- if (entry) {
42
- return entry.then(res => ({
43
- isOrigin: false,
44
- value: res.value
45
- }));
46
- }
47
-
48
- function __wrapper() {
49
- return func(...args);
50
- }
51
-
52
- const future = __wrapper().then(res => {
53
- globalInvokeCache.delete(key);
54
- return {
55
- isOrigin: true,
56
- value: res
57
- };
58
- }).catch(err => {
59
- globalInvokeCache.delete(key);
60
- throw err;
57
+ const globalInvokeCache = /* @__PURE__ */ new Map();
58
+ function withCoalescedInvoke(func) {
59
+ return function(key, args) {
60
+ return __async(this, null, function* () {
61
+ const entry = globalInvokeCache.get(key);
62
+ if (entry) {
63
+ return entry.then((res) => ({
64
+ isOrigin: false,
65
+ value: res.value
66
+ }));
67
+ }
68
+ function __wrapper() {
69
+ return func(...args);
70
+ }
71
+ const future = __wrapper().then((res) => {
72
+ globalInvokeCache.delete(key);
73
+ return { isOrigin: true, value: res };
74
+ }).catch((err) => {
75
+ globalInvokeCache.delete(key);
76
+ throw err;
77
+ });
78
+ globalInvokeCache.set(key, future);
79
+ return future;
61
80
  });
62
-
63
- globalInvokeCache.set(key, future);
64
- return future;
65
81
  };
66
82
  }
67
- export function maybeSync(fn) {
68
- return sync => {
83
+ function maybeSync(fn) {
84
+ return (sync) => {
69
85
  if (sync) {
70
86
  return fn();
71
87
  } else {
@@ -73,4 +89,14 @@ export function maybeSync(fn) {
73
89
  return Promise.resolve();
74
90
  }
75
91
  };
76
- }
92
+ }
93
+ export {
94
+ cacheAddition,
95
+ connectFactor,
96
+ fname,
97
+ getTime,
98
+ maybeSync,
99
+ namespaceHash,
100
+ valueFactory,
101
+ withCoalescedInvoke
102
+ };