@modern-js/prod-server 2.47.1 → 2.48.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 (56) hide show
  1. package/dist/cjs/libs/context/context.js +4 -8
  2. package/dist/cjs/libs/hook-api/base.js +0 -11
  3. package/dist/cjs/libs/hook-api/index.worker.js +0 -3
  4. package/dist/cjs/libs/hook-api/route.js +0 -4
  5. package/dist/cjs/libs/hook-api/template.js +0 -2
  6. package/dist/cjs/libs/render/ssr.js +7 -3
  7. package/dist/cjs/libs/render/ssrCache/cacheMod.js +0 -5
  8. package/dist/cjs/libs/render/ssrCache/index.js +10 -3
  9. package/dist/cjs/libs/render/ssrCache/manager.js +25 -11
  10. package/dist/cjs/libs/route/index.js +1 -3
  11. package/dist/cjs/libs/route/matcher.js +1 -5
  12. package/dist/cjs/libs/route/route.js +1 -10
  13. package/dist/cjs/libs/serverTiming.js +0 -3
  14. package/dist/cjs/renderHtml.js +0 -7
  15. package/dist/cjs/server/index.js +1 -7
  16. package/dist/cjs/server/modernServer.js +11 -21
  17. package/dist/cjs/workerServer.js +4 -8
  18. package/dist/esm/libs/context/context.js +66 -98
  19. package/dist/esm/libs/hook-api/base.js +36 -71
  20. package/dist/esm/libs/hook-api/index.worker.js +15 -29
  21. package/dist/esm/libs/hook-api/route.js +12 -27
  22. package/dist/esm/libs/hook-api/template.js +33 -61
  23. package/dist/esm/libs/render/ssr.js +8 -4
  24. package/dist/esm/libs/render/ssrCache/cacheMod.js +10 -18
  25. package/dist/esm/libs/render/ssrCache/index.js +40 -21
  26. package/dist/esm/libs/render/ssrCache/manager.js +149 -141
  27. package/dist/esm/libs/route/index.js +62 -96
  28. package/dist/esm/libs/route/matcher.js +58 -94
  29. package/dist/esm/libs/route/route.js +1 -10
  30. package/dist/esm/libs/serverTiming.js +8 -16
  31. package/dist/esm/renderHtml.js +22 -39
  32. package/dist/esm/server/index.js +307 -383
  33. package/dist/esm/server/modernServer.js +802 -936
  34. package/dist/esm/server/modernServerSplit.js +61 -86
  35. package/dist/esm/workerServer.js +87 -82
  36. package/dist/esm-node/libs/context/context.js +4 -8
  37. package/dist/esm-node/libs/hook-api/base.js +0 -11
  38. package/dist/esm-node/libs/hook-api/index.worker.js +0 -3
  39. package/dist/esm-node/libs/hook-api/route.js +0 -4
  40. package/dist/esm-node/libs/hook-api/template.js +0 -2
  41. package/dist/esm-node/libs/render/ssr.js +7 -3
  42. package/dist/esm-node/libs/render/ssrCache/cacheMod.js +0 -5
  43. package/dist/esm-node/libs/render/ssrCache/index.js +10 -3
  44. package/dist/esm-node/libs/render/ssrCache/manager.js +25 -11
  45. package/dist/esm-node/libs/route/index.js +1 -3
  46. package/dist/esm-node/libs/route/matcher.js +1 -5
  47. package/dist/esm-node/libs/route/route.js +1 -10
  48. package/dist/esm-node/libs/serverTiming.js +0 -3
  49. package/dist/esm-node/renderHtml.js +0 -7
  50. package/dist/esm-node/server/index.js +1 -7
  51. package/dist/esm-node/server/modernServer.js +11 -21
  52. package/dist/esm-node/workerServer.js +4 -8
  53. package/dist/types/libs/render/ssrCache/index.d.ts +2 -3
  54. package/dist/types/libs/render/ssrCache/manager.d.ts +7 -1
  55. package/dist/types/type.d.ts +1 -0
  56. package/package.json +8 -8
@@ -11,7 +11,7 @@ function ssrCache(req, render, ssrContext) {
11
11
  }
12
12
  function _ssrCache() {
13
13
  _ssrCache = _async_to_generator(function(req, render, ssrContext) {
14
- var customContainer, cacheOption, cacheControl, cacheManager, renderResult, stream;
14
+ var customContainer, cacheOption, cacheControl, cacheManager, renderResult, stream, data;
15
15
  return _ts_generator(this, function(_state) {
16
16
  switch (_state.label) {
17
17
  case 0:
@@ -39,30 +39,49 @@ function _ssrCache() {
39
39
  ];
40
40
  case 3:
41
41
  renderResult = _state.sent();
42
- if (!renderResult) {
42
+ if (!!renderResult)
43
43
  return [
44
- 2,
45
- ""
46
- ];
47
- } else if (typeof renderResult === "string") {
48
- return [
49
- 2,
50
- renderResult
44
+ 3,
45
+ 4
51
46
  ];
52
- } else {
53
- stream = new Transform({
54
- write: function write(chunk, _, callback) {
55
- this.push(chunk);
56
- callback();
57
- }
58
- });
47
+ return [
48
+ 2,
49
+ {
50
+ data: ""
51
+ }
52
+ ];
53
+ case 4:
54
+ if (!(typeof renderResult === "string"))
59
55
  return [
60
- 2,
61
- renderResult(stream)
56
+ 3,
57
+ 5
62
58
  ];
63
- }
64
- _state.label = 4;
65
- case 4:
59
+ return [
60
+ 2,
61
+ {
62
+ data: renderResult
63
+ }
64
+ ];
65
+ case 5:
66
+ stream = new Transform({
67
+ write: function write(chunk, _, callback) {
68
+ this.push(chunk);
69
+ callback();
70
+ }
71
+ });
72
+ return [
73
+ 4,
74
+ renderResult(stream)
75
+ ];
76
+ case 6:
77
+ data = _state.sent();
78
+ return [
79
+ 2,
80
+ {
81
+ data
82
+ }
83
+ ];
84
+ case 7:
66
85
  return [
67
86
  2
68
87
  ];
@@ -1,168 +1,176 @@
1
1
  import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
2
2
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
3
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
4
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
5
3
  import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
6
4
  import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
7
5
  import { Transform } from "stream";
6
+ import { normalizePathname } from "@modern-js/runtime-utils/url";
8
7
  var CacheManager = /* @__PURE__ */ function() {
9
8
  "use strict";
10
9
  function CacheManager2(container) {
11
10
  _class_call_check(this, CacheManager2);
12
- _define_property(this, "container", void 0);
13
11
  this.container = container;
14
12
  }
15
- _create_class(CacheManager2, [
16
- {
17
- key: "getCacheResult",
18
- value: function getCacheResult(req, cacheControl, render, ssrContext) {
19
- var _this = this;
20
- return _async_to_generator(function() {
21
- var key, value, maxAge, staleWhileRevalidate, ttl, cache, interval;
22
- return _ts_generator(this, function(_state) {
23
- switch (_state.label) {
24
- case 0:
25
- key = _this.computedKey(req, cacheControl);
26
- return [
27
- 4,
28
- _this.container.get(key)
29
- ];
30
- case 1:
31
- value = _state.sent();
32
- maxAge = cacheControl.maxAge, staleWhileRevalidate = cacheControl.staleWhileRevalidate;
33
- ttl = maxAge + staleWhileRevalidate;
34
- if (value) {
35
- cache = JSON.parse(value);
36
- interval = Date.now() - cache.cursor;
37
- if (interval <= maxAge) {
38
- return [
39
- 2,
40
- cache.val
41
- ];
42
- } else if (interval <= staleWhileRevalidate + maxAge) {
43
- _this.processCache(key, render, ssrContext, ttl);
44
- return [
45
- 2,
46
- cache.val
47
- ];
48
- } else {
49
- return [
50
- 2,
51
- _this.processCache(key, render, ssrContext, ttl)
52
- ];
53
- }
54
- } else {
55
- return [
56
- 2,
57
- _this.processCache(key, render, ssrContext, ttl)
58
- ];
59
- }
60
- return [
61
- 2
62
- ];
63
- }
64
- });
65
- })();
66
- }
67
- },
68
- {
69
- key: "processCache",
70
- value: function processCache(key, render, ssrContext, ttl) {
71
- var _this = this;
72
- return _async_to_generator(function() {
73
- var renderResult, current, cache, html, stream;
74
- return _ts_generator(this, function(_state) {
75
- switch (_state.label) {
76
- case 0:
77
- return [
78
- 4,
79
- render(ssrContext)
80
- ];
81
- case 1:
82
- renderResult = _state.sent();
83
- if (!!renderResult)
84
- return [
85
- 3,
86
- 2
87
- ];
13
+ var _proto = CacheManager2.prototype;
14
+ _proto.getCacheResult = function getCacheResult(req, cacheControl, render, ssrContext) {
15
+ var _this = this;
16
+ return _async_to_generator(function() {
17
+ var key, value, maxAge, staleWhileRevalidate, ttl, cache, interval;
18
+ return _ts_generator(this, function(_state) {
19
+ switch (_state.label) {
20
+ case 0:
21
+ key = _this.computedKey(req, cacheControl);
22
+ return [
23
+ 4,
24
+ _this.container.get(key)
25
+ ];
26
+ case 1:
27
+ value = _state.sent();
28
+ maxAge = cacheControl.maxAge, staleWhileRevalidate = cacheControl.staleWhileRevalidate;
29
+ ttl = maxAge + staleWhileRevalidate;
30
+ if (value) {
31
+ cache = JSON.parse(value);
32
+ interval = Date.now() - cache.cursor;
33
+ if (interval <= maxAge) {
88
34
  return [
89
35
  2,
90
- ""
91
- ];
92
- case 2:
93
- if (!(typeof renderResult === "string"))
94
- return [
95
- 3,
96
- 4
97
- ];
98
- current = Date.now();
99
- cache = {
100
- val: renderResult,
101
- cursor: current
102
- };
103
- return [
104
- 4,
105
- _this.container.set(key, JSON.stringify(cache), {
106
- ttl
107
- })
36
+ {
37
+ data: cache.val,
38
+ status: "hit"
39
+ }
108
40
  ];
109
- case 3:
110
- _state.sent();
41
+ } else if (interval <= staleWhileRevalidate + maxAge) {
42
+ _this.processCache(key, render, ssrContext, ttl);
111
43
  return [
112
44
  2,
113
- renderResult
114
- ];
115
- case 4:
116
- html = "";
117
- stream = new Transform({
118
- write: function write(chunk, _, callback) {
119
- html += chunk.toString();
120
- this.push(chunk);
121
- callback();
45
+ {
46
+ data: cache.val,
47
+ status: "stale"
122
48
  }
123
- });
124
- stream.on("close", function() {
125
- var current2 = Date.now();
126
- var cache2 = {
127
- val: html,
128
- cursor: current2
129
- };
130
- _this.container.set(key, JSON.stringify(cache2), {
131
- ttl
132
- });
133
- });
134
- return [
135
- 2,
136
- renderResult(stream)
137
49
  ];
138
- case 5:
50
+ } else {
139
51
  return [
140
- 2
52
+ 2,
53
+ _this.processCache(key, render, ssrContext, ttl, "expired")
141
54
  ];
55
+ }
56
+ } else {
57
+ return [
58
+ 2,
59
+ _this.processCache(key, render, ssrContext, ttl, "miss")
60
+ ];
142
61
  }
143
- });
144
- })();
145
- }
146
- },
147
- {
148
- key: "computedKey",
149
- value: function computedKey(req, cacheControl) {
150
- var url = req.url;
151
- var _url_split = _sliced_to_array(url.split("?"), 1), pathname = _url_split[0];
152
- var customKey = cacheControl.customKey;
153
- var defaultKey = pathname.replace(/.+\/+$/, "");
154
- if (customKey) {
155
- if (typeof customKey === "string") {
156
- return customKey;
157
- } else {
158
- return customKey(defaultKey);
159
- }
160
- } else {
161
- return defaultKey;
62
+ return [
63
+ 2
64
+ ];
65
+ }
66
+ });
67
+ })();
68
+ };
69
+ _proto.processCache = function processCache(key, render, ssrContext, ttl, status) {
70
+ var _this = this;
71
+ return _async_to_generator(function() {
72
+ var renderResult, current, cache, html, stream, readable;
73
+ return _ts_generator(this, function(_state) {
74
+ switch (_state.label) {
75
+ case 0:
76
+ return [
77
+ 4,
78
+ render(ssrContext)
79
+ ];
80
+ case 1:
81
+ renderResult = _state.sent();
82
+ if (!!renderResult)
83
+ return [
84
+ 3,
85
+ 2
86
+ ];
87
+ return [
88
+ 2,
89
+ {
90
+ data: ""
91
+ }
92
+ ];
93
+ case 2:
94
+ if (!(typeof renderResult === "string"))
95
+ return [
96
+ 3,
97
+ 4
98
+ ];
99
+ current = Date.now();
100
+ cache = {
101
+ val: renderResult,
102
+ cursor: current
103
+ };
104
+ return [
105
+ 4,
106
+ _this.container.set(key, JSON.stringify(cache), {
107
+ ttl
108
+ })
109
+ ];
110
+ case 3:
111
+ _state.sent();
112
+ return [
113
+ 2,
114
+ {
115
+ data: renderResult,
116
+ status
117
+ }
118
+ ];
119
+ case 4:
120
+ html = "";
121
+ stream = new Transform({
122
+ write: function write(chunk, _, callback) {
123
+ html += chunk.toString();
124
+ this.push(chunk);
125
+ callback();
126
+ }
127
+ });
128
+ stream.on("close", function() {
129
+ var current2 = Date.now();
130
+ var cache2 = {
131
+ val: html,
132
+ cursor: current2
133
+ };
134
+ _this.container.set(key, JSON.stringify(cache2), {
135
+ ttl
136
+ });
137
+ });
138
+ return [
139
+ 4,
140
+ renderResult(stream)
141
+ ];
142
+ case 5:
143
+ readable = _state.sent();
144
+ return [
145
+ 2,
146
+ {
147
+ data: readable,
148
+ status
149
+ }
150
+ ];
151
+ case 6:
152
+ return [
153
+ 2
154
+ ];
162
155
  }
156
+ });
157
+ })();
158
+ };
159
+ _proto.computedKey = function computedKey(req, cacheControl) {
160
+ var url = req.url;
161
+ var _url_split = _sliced_to_array(url.split("?"), 1), pathname = _url_split[0];
162
+ var customKey = cacheControl.customKey;
163
+ var defaultKey = normalizePathname(pathname);
164
+ if (customKey) {
165
+ if (typeof customKey === "string") {
166
+ return customKey;
167
+ } else {
168
+ return customKey(defaultKey);
163
169
  }
170
+ } else {
171
+ return defaultKey;
164
172
  }
165
- ]);
173
+ };
166
174
  return CacheManager2;
167
175
  }();
168
176
  export {
@@ -1,113 +1,79 @@
1
1
  import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
2
- import { _ as _create_class } from "@swc/helpers/_/_create_class";
3
- import { _ as _define_property } from "@swc/helpers/_/_define_property";
4
2
  import { RouteMatcher } from "./matcher";
5
3
  var RouteMatchManager = /* @__PURE__ */ function() {
6
4
  "use strict";
7
5
  function RouteMatchManager2() {
8
6
  _class_call_check(this, RouteMatchManager2);
9
- _define_property(this, "matchers", void 0);
10
- _define_property(this, "specs", []);
7
+ this.specs = [];
11
8
  this.matchers = [];
12
9
  }
13
- _create_class(RouteMatchManager2, [
14
- {
15
- key: "filter",
16
- value: (
17
- // get all routes matches pathname
18
- function filter(pathname) {
19
- return this.matchers.reduce(function(matches, matcher) {
20
- if (matcher.matchUrlPath(pathname)) {
21
- matches.push(matcher);
22
- }
23
- return matches;
24
- }, []);
10
+ var _proto = RouteMatchManager2.prototype;
11
+ _proto.filter = function filter(pathname) {
12
+ return this.matchers.reduce(function(matches, matcher) {
13
+ if (matcher.matchUrlPath(pathname)) {
14
+ matches.push(matcher);
15
+ }
16
+ return matches;
17
+ }, []);
18
+ };
19
+ _proto.best = function best(pathname, matches) {
20
+ var best2;
21
+ var matchedLen = 0;
22
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
23
+ try {
24
+ for (var _iterator = matches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
25
+ var match = _step.value;
26
+ var len = match.matchLength(pathname);
27
+ if (len === null) {
28
+ continue;
25
29
  }
26
- )
27
- },
28
- {
29
- key: "best",
30
- value: (
31
- // get best match from a set of matches
32
- function best(pathname, matches) {
33
- var best2;
34
- var matchedLen = 0;
35
- var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
36
- try {
37
- for (var _iterator = matches[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
38
- var match = _step.value;
39
- var len = match.matchLength(pathname);
40
- if (len === null) {
41
- continue;
42
- }
43
- if (len > matchedLen) {
44
- best2 = match;
45
- matchedLen = len;
46
- }
47
- }
48
- } catch (err) {
49
- _didIteratorError = true;
50
- _iteratorError = err;
51
- } finally {
52
- try {
53
- if (!_iteratorNormalCompletion && _iterator.return != null) {
54
- _iterator.return();
55
- }
56
- } finally {
57
- if (_didIteratorError) {
58
- throw _iteratorError;
59
- }
60
- }
61
- }
62
- return best2;
30
+ if (len > matchedLen) {
31
+ best2 = match;
32
+ matchedLen = len;
63
33
  }
64
- )
65
- },
66
- {
67
- key: "reset",
68
- value: (
69
- // reset routes matcher
70
- function reset(specs) {
71
- this.specs = specs;
72
- var matchers = specs.reduce(function(ms, spec) {
73
- ms.push(new RouteMatcher(spec));
74
- return ms;
75
- }, []);
76
- this.matchers = matchers;
34
+ }
35
+ } catch (err) {
36
+ _didIteratorError = true;
37
+ _iteratorError = err;
38
+ } finally {
39
+ try {
40
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
41
+ _iterator.return();
77
42
  }
78
- )
79
- },
80
- {
81
- key: "match",
82
- value: (
83
- // get best match from all matcher in manager
84
- function match(pathname) {
85
- var matches = this.filter(pathname);
86
- var best = this.best(pathname, matches);
87
- return best;
43
+ } finally {
44
+ if (_didIteratorError) {
45
+ throw _iteratorError;
88
46
  }
89
- )
90
- },
91
- {
92
- key: "matchEntry",
93
- value: function matchEntry(entryname) {
94
- return this.matchers.find(function(matcher) {
95
- return matcher.matchEntry(entryname);
96
- });
97
- }
98
- },
99
- {
100
- key: "getBundles",
101
- value: function getBundles() {
102
- var bundles = this.specs.filter(function(route) {
103
- return route.isSSR;
104
- }).map(function(route) {
105
- return route.bundle;
106
- });
107
- return bundles;
108
47
  }
109
48
  }
110
- ]);
49
+ return best2;
50
+ };
51
+ _proto.reset = function reset(specs) {
52
+ this.specs = specs;
53
+ var matchers = specs.reduce(function(ms, spec) {
54
+ ms.push(new RouteMatcher(spec));
55
+ return ms;
56
+ }, []);
57
+ this.matchers = matchers;
58
+ };
59
+ _proto.match = function match(pathname) {
60
+ var matches = this.filter(pathname);
61
+ var best = this.best(pathname, matches);
62
+ return best;
63
+ };
64
+ _proto.matchEntry = function matchEntry(entryname) {
65
+ return this.matchers.find(function(matcher) {
66
+ return matcher.matchEntry(entryname);
67
+ });
68
+ };
69
+ _proto.getBundles = function getBundles() {
70
+ var bundles = this.specs.filter(function(route) {
71
+ return route.isSSR;
72
+ }).map(function(route) {
73
+ return route.bundle;
74
+ });
75
+ return bundles;
76
+ };
111
77
  return RouteMatchManager2;
112
78
  }();
113
79
  export {