@modern-js/prod-server 1.0.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 (173) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/LICENSE +21 -0
  3. package/README.md +30 -0
  4. package/dist/js/modern/constants.js +26 -0
  5. package/dist/js/modern/index.js +14 -0
  6. package/dist/js/modern/libs/context/context.js +180 -0
  7. package/dist/js/modern/libs/context/index.js +3 -0
  8. package/dist/js/modern/libs/hook-api/route.js +39 -0
  9. package/dist/js/modern/libs/hook-api/template.js +61 -0
  10. package/dist/js/modern/libs/metrics.js +12 -0
  11. package/dist/js/modern/libs/proxy.js +33 -0
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +70 -0
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +233 -0
  14. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +53 -0
  15. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +35 -0
  16. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +121 -0
  17. package/dist/js/modern/libs/render/cache/index.js +74 -0
  18. package/dist/js/modern/libs/render/cache/page-caches/index.js +9 -0
  19. package/dist/js/modern/libs/render/cache/page-caches/lru.js +35 -0
  20. package/dist/js/modern/libs/render/cache/spr.js +280 -0
  21. package/dist/js/modern/libs/render/cache/type.js +1 -0
  22. package/dist/js/modern/libs/render/cache/util.js +79 -0
  23. package/dist/js/modern/libs/render/index.js +65 -0
  24. package/dist/js/modern/libs/render/modern/browser-list.js +7 -0
  25. package/dist/js/modern/libs/render/modern/index.js +42 -0
  26. package/dist/js/modern/libs/render/reader.js +112 -0
  27. package/dist/js/modern/libs/render/ssr.js +58 -0
  28. package/dist/js/modern/libs/render/static.js +46 -0
  29. package/dist/js/modern/libs/render/type.js +7 -0
  30. package/dist/js/modern/libs/route/index.js +68 -0
  31. package/dist/js/modern/libs/route/matcher.js +94 -0
  32. package/dist/js/modern/libs/route/route.js +24 -0
  33. package/dist/js/modern/libs/serve-file.js +28 -0
  34. package/dist/js/modern/server/index.js +120 -0
  35. package/dist/js/modern/server/modern-server-split.js +81 -0
  36. package/dist/js/modern/server/modern-server.js +576 -0
  37. package/dist/js/modern/type.js +1 -0
  38. package/dist/js/modern/utils.js +112 -0
  39. package/dist/js/node/constants.js +36 -0
  40. package/dist/js/node/index.js +74 -0
  41. package/dist/js/node/libs/context/context.js +194 -0
  42. package/dist/js/node/libs/context/index.js +18 -0
  43. package/dist/js/node/libs/hook-api/route.js +48 -0
  44. package/dist/js/node/libs/hook-api/template.js +69 -0
  45. package/dist/js/node/libs/metrics.js +18 -0
  46. package/dist/js/node/libs/proxy.js +44 -0
  47. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +77 -0
  48. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +238 -0
  49. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +60 -0
  50. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +42 -0
  51. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +128 -0
  52. package/dist/js/node/libs/render/cache/index.js +86 -0
  53. package/dist/js/node/libs/render/cache/page-caches/index.js +17 -0
  54. package/dist/js/node/libs/render/cache/page-caches/lru.js +47 -0
  55. package/dist/js/node/libs/render/cache/spr.js +298 -0
  56. package/dist/js/node/libs/render/cache/type.js +5 -0
  57. package/dist/js/node/libs/render/cache/util.js +105 -0
  58. package/dist/js/node/libs/render/index.js +91 -0
  59. package/dist/js/node/libs/render/modern/browser-list.js +14 -0
  60. package/dist/js/node/libs/render/modern/index.js +58 -0
  61. package/dist/js/node/libs/render/reader.js +139 -0
  62. package/dist/js/node/libs/render/ssr.js +76 -0
  63. package/dist/js/node/libs/render/static.js +62 -0
  64. package/dist/js/node/libs/render/type.js +14 -0
  65. package/dist/js/node/libs/route/index.js +83 -0
  66. package/dist/js/node/libs/route/matcher.js +108 -0
  67. package/dist/js/node/libs/route/route.js +33 -0
  68. package/dist/js/node/libs/serve-file.js +41 -0
  69. package/dist/js/node/server/index.js +142 -0
  70. package/dist/js/node/server/modern-server-split.js +97 -0
  71. package/dist/js/node/server/modern-server.js +614 -0
  72. package/dist/js/node/type.js +5 -0
  73. package/dist/js/node/utils.js +143 -0
  74. package/dist/js/styles/tsconfig.json +12 -0
  75. package/dist/types/constants.d.ts +20 -0
  76. package/dist/types/index.d.ts +11 -0
  77. package/dist/types/libs/context/context.d.ts +61 -0
  78. package/dist/types/libs/context/index.d.ts +4 -0
  79. package/dist/types/libs/hook-api/route.d.ts +14 -0
  80. package/dist/types/libs/hook-api/template.d.ts +14 -0
  81. package/dist/types/libs/metrics.d.ts +3 -0
  82. package/dist/types/libs/proxy.d.ts +4 -0
  83. package/dist/types/libs/render/cache/__tests__/cache.fun.test.d.ts +1 -0
  84. package/dist/types/libs/render/cache/__tests__/cache.test.d.ts +1 -0
  85. package/dist/types/libs/render/cache/__tests__/cacheable.d.ts +62 -0
  86. package/dist/types/libs/render/cache/__tests__/error-configuration.d.ts +28 -0
  87. package/dist/types/libs/render/cache/__tests__/matched-cache.d.ts +124 -0
  88. package/dist/types/libs/render/cache/index.d.ts +6 -0
  89. package/dist/types/libs/render/cache/page-caches/index.d.ts +2 -0
  90. package/dist/types/libs/render/cache/page-caches/lru.d.ts +15 -0
  91. package/dist/types/libs/render/cache/spr.d.ts +24 -0
  92. package/dist/types/libs/render/cache/type.d.ts +48 -0
  93. package/dist/types/libs/render/cache/util.d.ts +17 -0
  94. package/dist/types/libs/render/index.d.ts +18 -0
  95. package/dist/types/libs/render/modern/browser-list.d.ts +1 -0
  96. package/dist/types/libs/render/modern/index.d.ts +3 -0
  97. package/dist/types/libs/render/reader.d.ts +18 -0
  98. package/dist/types/libs/render/ssr.d.ts +10 -0
  99. package/dist/types/libs/render/static.d.ts +3 -0
  100. package/dist/types/libs/render/type.d.ts +33 -0
  101. package/dist/types/libs/route/index.d.ts +15 -0
  102. package/dist/types/libs/route/matcher.d.ts +15 -0
  103. package/dist/types/libs/route/route.d.ts +14 -0
  104. package/dist/types/libs/serve-file.d.ts +8 -0
  105. package/dist/types/server/index.d.ts +20 -0
  106. package/dist/types/server/modern-server-split.d.ts +26 -0
  107. package/dist/types/server/modern-server.d.ts +72 -0
  108. package/dist/types/type.d.ts +56 -0
  109. package/dist/types/utils.d.ts +19 -0
  110. package/jest.config.js +9 -0
  111. package/modern.config.js +2 -0
  112. package/package.json +82 -0
  113. package/src/constants.ts +26 -0
  114. package/src/index.ts +18 -0
  115. package/src/libs/context/context.ts +183 -0
  116. package/src/libs/context/index.ts +7 -0
  117. package/src/libs/hook-api/route.ts +42 -0
  118. package/src/libs/hook-api/template.ts +53 -0
  119. package/src/libs/metrics.ts +15 -0
  120. package/src/libs/proxy.ts +42 -0
  121. package/src/libs/render/cache/__tests__/cache.fun.test.ts +94 -0
  122. package/src/libs/render/cache/__tests__/cache.test.ts +240 -0
  123. package/src/libs/render/cache/__tests__/cacheable.ts +44 -0
  124. package/src/libs/render/cache/__tests__/error-configuration.ts +34 -0
  125. package/src/libs/render/cache/__tests__/matched-cache.ts +88 -0
  126. package/src/libs/render/cache/index.ts +75 -0
  127. package/src/libs/render/cache/page-caches/index.ts +11 -0
  128. package/src/libs/render/cache/page-caches/lru.ts +38 -0
  129. package/src/libs/render/cache/spr.ts +301 -0
  130. package/src/libs/render/cache/type.ts +59 -0
  131. package/src/libs/render/cache/util.ts +97 -0
  132. package/src/libs/render/index.ts +79 -0
  133. package/src/libs/render/modern/browser-list.ts +7 -0
  134. package/src/libs/render/modern/index.ts +41 -0
  135. package/src/libs/render/modern/module.d.ts +4 -0
  136. package/src/libs/render/reader.ts +119 -0
  137. package/src/libs/render/ssr.ts +67 -0
  138. package/src/libs/render/static.ts +52 -0
  139. package/src/libs/render/type.ts +38 -0
  140. package/src/libs/route/index.ts +76 -0
  141. package/src/libs/route/matcher.ts +108 -0
  142. package/src/libs/route/route.ts +34 -0
  143. package/src/libs/serve-file.ts +34 -0
  144. package/src/server/index.ts +147 -0
  145. package/src/server/modern-server-split.ts +97 -0
  146. package/src/server/modern-server.ts +613 -0
  147. package/src/tsconfig.json +12 -0
  148. package/src/type.ts +61 -0
  149. package/src/utils.ts +122 -0
  150. package/tests/.eslintrc.js +6 -0
  151. package/tests/context.test.ts +52 -0
  152. package/tests/fixtures/hosting-files/static/index.js +1 -0
  153. package/tests/fixtures/pure/modern.config.js +5 -0
  154. package/tests/fixtures/pure/package.json +21 -0
  155. package/tests/fixtures/pure/src/App.css +119 -0
  156. package/tests/fixtures/pure/src/App.tsx +43 -0
  157. package/tests/fixtures/pure/tsconfig.json +12 -0
  158. package/tests/fixtures/reader/index.ts +3 -0
  159. package/tests/fixtures/route-spec/dynamic.json +13 -0
  160. package/tests/fixtures/route-spec/index.json +29 -0
  161. package/tests/fixtures/ssr/bundle.js +5 -0
  162. package/tests/fixtures/static-dir/bar.html +11 -0
  163. package/tests/fixtures/static-dir/baz/index.html +11 -0
  164. package/tests/fixtures/static-dir/foo/index.html +11 -0
  165. package/tests/helper.ts +8 -0
  166. package/tests/hook.test.ts +44 -0
  167. package/tests/middleware.test.ts +179 -0
  168. package/tests/render.test.ts +102 -0
  169. package/tests/route.test.ts +77 -0
  170. package/tests/server.test.ts +101 -0
  171. package/tests/tsconfig.json +12 -0
  172. package/tests/utils.test.ts +106 -0
  173. package/tsconfig.json +11 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ # @modern-js/prod-server
2
+
3
+ ## 1.0.1
4
+ ### Patch Changes
5
+
6
+ - 02fb4146: support product server
7
+ - Updated dependencies [969f172f]
8
+ - Updated dependencies [4c792f68]
9
+ - Updated dependencies [4b5d4bf4]
10
+ - Updated dependencies [62f5b8c8]
11
+ - Updated dependencies [55e18278]
12
+ - Updated dependencies [4499a674]
13
+ - Updated dependencies [403f5169]
14
+ - Updated dependencies [a7f42f48]
15
+ - @modern-js/core@1.4.4
16
+ - @modern-js/utils@1.3.3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Modern.js
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,30 @@
1
+
2
+ <p align="center">
3
+ <a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
4
+ </p>
5
+ <p align="center">
6
+ 现代 Web 工程体系
7
+ <br/>
8
+ <a href="https://modernjs.dev" target="blank">
9
+ modernjs.dev
10
+ </a>
11
+ </p>
12
+ <p align="center">
13
+ The meta-framework suite designed from scratch for frontend-focused modern web development
14
+ </p>
15
+
16
+ # Introduction
17
+
18
+ > The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.
19
+
20
+ - [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)
21
+
22
+ ## Getting Started
23
+
24
+ - [Quick Start](https://modernjs.dev/docs/start)
25
+ - [Guides](https://modernjs.dev/docs/guides)
26
+ - [API References](https://modernjs.dev/docs/apis)
27
+
28
+ ## Contributing
29
+
30
+ - [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)
@@ -0,0 +1,26 @@
1
+ export const AGGRED_DIR = {
2
+ mock: 'config/mock',
3
+ server: 'server',
4
+ api: 'api',
5
+ shared: 'shared',
6
+ lambda: 'lambda'
7
+ };
8
+ export let ApiServerMode;
9
+
10
+ (function (ApiServerMode) {
11
+ ApiServerMode["func"] = "function";
12
+ ApiServerMode["frame"] = "framework";
13
+ })(ApiServerMode || (ApiServerMode = {}));
14
+
15
+ export const ERROR_DIGEST = {
16
+ INIT: 'Server init error',
17
+ ENOTF: 'Page could not be found',
18
+ WARMUP: 'SSR warmup failed',
19
+ EINTER: 'Internal server error',
20
+ ERENDER: 'SSR render failed',
21
+ EMICROINJ: 'Get micro-frontend info failed'
22
+ };
23
+ export const ERROR_PAGE_TEXT = {
24
+ 404: 'This page could not be found.',
25
+ 500: 'Internal Server Error.'
26
+ };
@@ -0,0 +1,14 @@
1
+ import { Server } from "./server";
2
+ export { Server };
3
+ export { ModernServer } from "./server/modern-server";
4
+ export { createProxyHandler } from "./libs/proxy";
5
+ export * from "./type";
6
+ export * from "./constants";
7
+ export default (options => {
8
+ if (options == null) {
9
+ throw new Error('can not start mserver without options');
10
+ }
11
+
12
+ const server = new Server(options);
13
+ return server.init();
14
+ });
@@ -0,0 +1,180 @@
1
+ import { URL } from 'url';
2
+ import qs from 'querystring';
3
+ import { toMessage } from "../../utils";
4
+ export class ModernServerContext {
5
+ /**
6
+ * http request
7
+ */
8
+
9
+ /**
10
+ * http response
11
+ */
12
+
13
+ /**
14
+ * url params
15
+ */
16
+ constructor(req, res) {
17
+ this.req = void 0;
18
+ this.res = void 0;
19
+ this.params = {};
20
+ this.logger = void 0;
21
+ this.metrics = void 0;
22
+ this.serverData = void 0;
23
+ this.req = req;
24
+ this.res = res;
25
+ this.logger = req.logger;
26
+ this.metrics = req.metrics;
27
+ this.serverData = {};
28
+ this.bind();
29
+ }
30
+
31
+ bind() {
32
+ const {
33
+ req,
34
+ res
35
+ } = this;
36
+
37
+ req.get = key => this.getReqHeader(key);
38
+
39
+ res.set = (key, value) => this.res.setHeader(key, value);
40
+ }
41
+
42
+ setParams(params) {
43
+ this.params = params;
44
+ }
45
+
46
+ setServerData(key, value) {
47
+ this.serverData[key] = value;
48
+ }
49
+
50
+ getReqHeader(key) {
51
+ const {
52
+ req
53
+ } = this;
54
+ const field = key.toLowerCase();
55
+
56
+ switch (field) {
57
+ case 'referer':
58
+ case 'referrer':
59
+ return req.headers.referrer || req.headers.referer || '';
60
+
61
+ default:
62
+ return req.headers[field] || '';
63
+ }
64
+ }
65
+ /* request property */
66
+
67
+
68
+ get headers() {
69
+ return this.req.headers;
70
+ }
71
+
72
+ get method() {
73
+ return this.req.method;
74
+ }
75
+
76
+ get url() {
77
+ return this.req.url || '';
78
+ }
79
+
80
+ set url(val) {
81
+ this.req.url = val;
82
+ }
83
+
84
+ get host() {
85
+ let host = this.getReqHeader('X-Forwarded-Host');
86
+
87
+ if (!host) {
88
+ host = this.getReqHeader('Host');
89
+ }
90
+
91
+ return host.split(/\s*,\s*/, 1)[0] || '';
92
+ }
93
+
94
+ get protocol() {
95
+ if (this.req.socket.encrypted) {
96
+ return 'https';
97
+ }
98
+
99
+ const proto = this.getReqHeader('X-Forwarded-Proto');
100
+ return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
101
+ }
102
+
103
+ get origin() {
104
+ return `${this.protocol}://${this.host}`;
105
+ }
106
+
107
+ get href() {
108
+ return this.origin + this.url;
109
+ }
110
+
111
+ get parsedURL() {
112
+ const url = new URL(this.req.url, this.origin);
113
+ return url;
114
+ }
115
+
116
+ get path() {
117
+ return this.parsedURL.pathname;
118
+ }
119
+
120
+ set path(p) {
121
+ const url = new URL(this.req.url, this.origin); // this should never happend
122
+
123
+ if (!url || !p) {
124
+ return;
125
+ }
126
+
127
+ if (url.pathname === p) {
128
+ return;
129
+ }
130
+
131
+ url.pathname = p;
132
+ this.url = url.toString();
133
+ }
134
+
135
+ get querystring() {
136
+ if (!this.req) {
137
+ return '';
138
+ }
139
+
140
+ return this.parsedURL.search.replace(/^\?/, '') || '';
141
+ }
142
+
143
+ get query() {
144
+ const str = this.querystring;
145
+ return qs.parse(str);
146
+ }
147
+ /* response property */
148
+
149
+
150
+ get status() {
151
+ return this.res.statusCode;
152
+ }
153
+
154
+ set status(statusCode) {
155
+ this.res.statusCode = statusCode;
156
+ }
157
+ /**
158
+ * 判断链接是否已经关闭
159
+ */
160
+
161
+
162
+ resHasHandled() {
163
+ return this.res.writableEnded;
164
+ }
165
+
166
+ logInfo() {
167
+ return {
168
+ headers: this.headers,
169
+ href: this.href,
170
+ url: this.url
171
+ };
172
+ }
173
+
174
+ error(dig, err = '') {
175
+ const message = toMessage(dig, err);
176
+ const reqInfo = this.logInfo();
177
+ this.logger.error(`${reqInfo.url} - ${message}`, reqInfo);
178
+ }
179
+
180
+ }
@@ -0,0 +1,3 @@
1
+ import { ModernServerContext } from "./context";
2
+ export const createContext = (req, res) => new ModernServerContext(req, res);
3
+ export { ModernServerContext };
@@ -0,0 +1,39 @@
1
+ class RouteAPI {
2
+ constructor(matched, router, url) {
3
+ this.router = void 0;
4
+ this.current = void 0;
5
+ this.url = void 0;
6
+ this.current = matched;
7
+ this.router = router;
8
+ this.url = url;
9
+ }
10
+
11
+ cur() {
12
+ return this.current.generate(this.url);
13
+ }
14
+
15
+ get(entryName) {
16
+ const {
17
+ router
18
+ } = this;
19
+ const matched = router.matchEntry(entryName);
20
+ return matched ? matched.generate(this.url) : null;
21
+ }
22
+
23
+ use(entryName) {
24
+ const {
25
+ router
26
+ } = this;
27
+ const matched = router.matchEntry(entryName);
28
+
29
+ if (matched) {
30
+ this.current = matched;
31
+ return true;
32
+ } else {
33
+ return false;
34
+ }
35
+ }
36
+
37
+ }
38
+
39
+ export const createRouteAPI = (matched, router, url) => new RouteAPI(matched, router, url);
@@ -0,0 +1,61 @@
1
+ const RegList = {
2
+ before: {
3
+ head: '<head>',
4
+ body: '<body>'
5
+ },
6
+ after: {
7
+ head: '</head>',
8
+ body: '</body>'
9
+ }
10
+ };
11
+
12
+ class TemplateAPI {
13
+ constructor(content) {
14
+ this.content = void 0;
15
+ this.content = content;
16
+ }
17
+
18
+ get() {
19
+ return this.content;
20
+ }
21
+
22
+ set(content) {
23
+ this.content = content;
24
+ }
25
+
26
+ prependHead(fragment) {
27
+ const {
28
+ head
29
+ } = RegList.before;
30
+ return this.replace(head, `${head}${fragment}`);
31
+ }
32
+
33
+ appendHead(fragment) {
34
+ const {
35
+ head
36
+ } = RegList.after;
37
+ return this.replace(head, `${fragment}${head}`);
38
+ }
39
+
40
+ prependBody(fragment) {
41
+ const {
42
+ body
43
+ } = RegList.before;
44
+ return this.replace(body, `${body}${fragment}`);
45
+ }
46
+
47
+ appendBody(fragment) {
48
+ const {
49
+ body
50
+ } = RegList.after;
51
+ return this.replace(body, `${fragment}${body}`);
52
+ }
53
+
54
+ replace(reg, text) {
55
+ this.content = this.content.replace(reg, text);
56
+ return this;
57
+ }
58
+
59
+ }
60
+
61
+ export const createTemplateAPI = content => new TemplateAPI(content);
@@ -0,0 +1,12 @@
1
+ const metrics = {
2
+ gauges() {// no impl
3
+ },
4
+
5
+ counter() {// no impl
6
+ },
7
+
8
+ timer() {// no impl
9
+ }
10
+
11
+ };
12
+ export { metrics };
@@ -0,0 +1,33 @@
1
+ import { createProxyMiddleware } from 'http-proxy-middleware';
2
+ import { formatProxyOptions } from '@modern-js/utils';
3
+ export const createProxyHandler = proxyOptions => {
4
+ if (!proxyOptions) {
5
+ return null;
6
+ } // If it is not an array, it may be an object that uses the context attribute
7
+ // or an object in the form of { source: ProxyDetail }
8
+
9
+
10
+ const formatedProxy = formatProxyOptions(proxyOptions);
11
+ const middlewares = formatedProxy.map(option => {
12
+ const middleware = createProxyMiddleware(option.context, option); // eslint-disable-next-line consistent-return
13
+
14
+ return async (ctx, next) => {
15
+ const {
16
+ req,
17
+ res
18
+ } = ctx;
19
+ const bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null; // only false, no true
20
+
21
+ if (typeof bypassUrl === 'boolean') {
22
+ ctx.status = 404;
23
+ return next();
24
+ } else if (typeof bypassUrl === 'string') {
25
+ ctx.url = bypassUrl;
26
+ return next();
27
+ }
28
+
29
+ middleware(req, res, next);
30
+ };
31
+ });
32
+ return middlewares;
33
+ };
@@ -0,0 +1,70 @@
1
+ import url from 'url';
2
+ import { createCache, destroyCache } from "../spr";
3
+ import { cacheAddition, connectFactor, fname, maybeSync, namespaceHash, valueFactory, withCoalescedInvoke } from "../util";
4
+ describe('test spr util functions', () => {
5
+ it('should return value correctly', () => {
6
+ expect(connectFactor('bar', 'foo')).toBe('bar-foo');
7
+ expect(fname(1)).toBe('f1');
8
+ expect(namespaceHash('modern', '!@#$%^&')).toBe('modern/!@#$%^&');
9
+ });
10
+ it('should create or destroy instance correctly', () => {
11
+ const ins1 = createCache();
12
+ const ins2 = createCache();
13
+ expect(ins1 === ins2).toBe(true);
14
+ destroyCache();
15
+ const ins3 = createCache();
16
+ expect(ins1 === ins3).toBe(false);
17
+ expect(ins2 === ins3).toBe(false);
18
+ });
19
+ it('should return function correctly', () => {
20
+ const urlParams = (() => new url.URLSearchParams())();
21
+
22
+ urlParams.set('name', 'modern');
23
+ const getParam = valueFactory(urlParams);
24
+ expect(getParam('name')).toBe('modern');
25
+ const headers = {
26
+ age: '12345'
27
+ };
28
+ const getHeader = valueFactory(headers);
29
+ expect(getHeader('age')).toBe('12345');
30
+ });
31
+ it('should add target html content', () => {
32
+ const contentNoHead = '<div>123</div>';
33
+ const html = cacheAddition(contentNoHead, Math.random().toString());
34
+ expect(html).toBe(contentNoHead);
35
+ const contentWithHead = '<head></head><div>123</div>';
36
+ const hash = Math.random().toString();
37
+ const htmlWithHead = cacheAddition(contentWithHead, hash);
38
+ expect(htmlWithHead).toBe(`<head><meta name="x-moden-spr" content="${hash}"></head><div>123</div>`);
39
+ });
40
+ it('should only invoke func one time', async () => {
41
+ let index = 0;
42
+ const fn = withCoalescedInvoke(async () => new Promise(resolve => {
43
+ setTimeout(() => {
44
+ index += 1;
45
+ resolve(index);
46
+ }, 500);
47
+ }));
48
+ const key = 'test';
49
+ const [res1, res2] = await Promise.all([fn(key, []), fn(key, [])]);
50
+ expect(res1.isOrigin && res2.isOrigin).toBe(false);
51
+ expect(res1.isOrigin || res2.isOrigin).toBe(true);
52
+ expect(res1.value).toBe(1);
53
+ expect(res2.value).toBe(1);
54
+ });
55
+ it('should invoke sync or async', async () => {
56
+ const foo = '';
57
+ const async = await maybeSync(() => new Promise(resolve => {
58
+ setTimeout(() => {
59
+ resolve(foo);
60
+ }, 100);
61
+ }))(false);
62
+ expect(async).toBeUndefined();
63
+ const sync = await maybeSync(() => new Promise(resolve => {
64
+ setTimeout(() => {
65
+ resolve(foo);
66
+ }, 100);
67
+ }))(true);
68
+ expect(sync).toBe(foo);
69
+ });
70
+ });