@modern-js/prod-server 2.0.0-beta.3 → 2.0.0-beta.6

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 (130) hide show
  1. package/CHANGELOG.md +139 -0
  2. package/dist/js/modern/constants.js +32 -25
  3. package/dist/js/modern/index.js +11 -6
  4. package/dist/js/modern/libs/context/context.js +52 -84
  5. package/dist/js/modern/libs/context/index.js +5 -2
  6. package/dist/js/modern/libs/hook-api/index.js +44 -35
  7. package/dist/js/modern/libs/hook-api/route.js +6 -7
  8. package/dist/js/modern/libs/hook-api/template.js +20 -34
  9. package/dist/js/modern/libs/loadConfig.js +45 -24
  10. package/dist/js/modern/libs/metrics.js +3 -4
  11. package/dist/js/modern/libs/proxy.js +68 -37
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +112 -67
  13. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +246 -216
  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 +88 -54
  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 -6
  20. package/dist/js/modern/libs/render/cache/spr.js +133 -117
  21. package/dist/js/modern/libs/render/cache/type.js +0 -1
  22. package/dist/js/modern/libs/render/cache/util.js +71 -39
  23. package/dist/js/modern/libs/render/index.js +76 -56
  24. package/dist/js/modern/libs/render/measure.js +38 -27
  25. package/dist/js/modern/libs/render/reader.js +65 -62
  26. package/dist/js/modern/libs/render/ssr.js +50 -32
  27. package/dist/js/modern/libs/render/static.js +50 -33
  28. package/dist/js/modern/libs/render/type.js +9 -6
  29. package/dist/js/modern/libs/route/index.js +8 -15
  30. package/dist/js/modern/libs/route/matcher.js +20 -34
  31. package/dist/js/modern/libs/route/route.js +9 -18
  32. package/dist/js/modern/libs/serve-file.js +33 -20
  33. package/dist/js/modern/server/index.js +150 -145
  34. package/dist/js/modern/server/modern-server-split.js +46 -12
  35. package/dist/js/modern/server/modern-server.js +384 -422
  36. package/dist/js/modern/type.js +0 -1
  37. package/dist/js/modern/utils.js +62 -43
  38. package/dist/js/modern/worker-server.js +34 -14
  39. package/dist/js/node/constants.js +53 -30
  40. package/dist/js/node/index.js +37 -57
  41. package/dist/js/node/libs/context/context.js +83 -94
  42. package/dist/js/node/libs/context/index.js +28 -13
  43. package/dist/js/node/libs/hook-api/index.js +76 -48
  44. package/dist/js/node/libs/hook-api/route.js +26 -11
  45. package/dist/js/node/libs/hook-api/template.js +41 -39
  46. package/dist/js/node/libs/loadConfig.js +73 -35
  47. package/dist/js/node/libs/metrics.js +25 -9
  48. package/dist/js/node/libs/proxy.js +89 -44
  49. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +99 -56
  50. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +147 -128
  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 +110 -64
  55. package/dist/js/node/libs/render/cache/page-caches/index.js +54 -14
  56. package/dist/js/node/libs/render/cache/page-caches/lru.js +33 -12
  57. package/dist/js/node/libs/render/cache/spr.js +161 -129
  58. package/dist/js/node/libs/render/cache/type.js +15 -5
  59. package/dist/js/node/libs/render/cache/util.js +99 -45
  60. package/dist/js/node/libs/render/index.js +106 -67
  61. package/dist/js/node/libs/render/measure.js +58 -31
  62. package/dist/js/node/libs/render/reader.js +95 -70
  63. package/dist/js/node/libs/render/ssr.js +80 -47
  64. package/dist/js/node/libs/render/static.js +79 -40
  65. package/dist/js/node/libs/render/type.js +31 -12
  66. package/dist/js/node/libs/route/index.js +31 -26
  67. package/dist/js/node/libs/route/matcher.js +40 -41
  68. package/dist/js/node/libs/route/route.js +29 -22
  69. package/dist/js/node/libs/serve-file.js +66 -32
  70. package/dist/js/node/server/index.js +168 -160
  71. package/dist/js/node/server/modern-server-split.js +72 -22
  72. package/dist/js/node/server/modern-server.js +403 -445
  73. package/dist/js/node/type.js +15 -3
  74. package/dist/js/node/utils.js +85 -52
  75. package/dist/js/node/worker-server.js +57 -21
  76. package/dist/js/treeshaking/constants.js +26 -25
  77. package/dist/js/treeshaking/index.js +10 -10
  78. package/dist/js/treeshaking/libs/context/context.js +268 -237
  79. package/dist/js/treeshaking/libs/context/index.js +3 -3
  80. package/dist/js/treeshaking/libs/hook-api/index.js +267 -143
  81. package/dist/js/treeshaking/libs/hook-api/route.js +65 -30
  82. package/dist/js/treeshaking/libs/hook-api/template.js +121 -85
  83. package/dist/js/treeshaking/libs/loadConfig.js +80 -37
  84. package/dist/js/treeshaking/libs/metrics.js +4 -10
  85. package/dist/js/treeshaking/libs/proxy.js +240 -76
  86. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +288 -121
  87. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +772 -455
  88. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +65 -51
  89. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -35
  90. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +144 -118
  91. package/dist/js/treeshaking/libs/render/cache/index.js +337 -175
  92. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +151 -27
  93. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +80 -42
  94. package/dist/js/treeshaking/libs/render/cache/spr.js +470 -340
  95. package/dist/js/treeshaking/libs/render/cache/type.js +1 -1
  96. package/dist/js/treeshaking/libs/render/cache/util.js +271 -92
  97. package/dist/js/treeshaking/libs/render/index.js +228 -95
  98. package/dist/js/treeshaking/libs/render/measure.js +142 -57
  99. package/dist/js/treeshaking/libs/render/reader.js +325 -177
  100. package/dist/js/treeshaking/libs/render/ssr.js +220 -95
  101. package/dist/js/treeshaking/libs/render/static.js +210 -78
  102. package/dist/js/treeshaking/libs/render/type.js +7 -6
  103. package/dist/js/treeshaking/libs/route/index.js +125 -89
  104. package/dist/js/treeshaking/libs/route/matcher.js +132 -107
  105. package/dist/js/treeshaking/libs/route/route.js +40 -26
  106. package/dist/js/treeshaking/libs/serve-file.js +177 -68
  107. package/dist/js/treeshaking/server/index.js +493 -327
  108. package/dist/js/treeshaking/server/modern-server-split.js +352 -144
  109. package/dist/js/treeshaking/server/modern-server.js +1048 -909
  110. package/dist/js/treeshaking/type.js +1 -1
  111. package/dist/js/treeshaking/utils.js +138 -81
  112. package/dist/js/treeshaking/worker-server.js +176 -55
  113. package/dist/types/index.d.ts +2 -0
  114. package/dist/types/libs/context/context.d.ts +4 -1
  115. package/dist/types/libs/loadConfig.d.ts +1 -0
  116. package/dist/types/libs/render/cache/index.d.ts +2 -0
  117. package/dist/types/libs/render/cache/spr.d.ts +2 -0
  118. package/dist/types/libs/route/route.d.ts +0 -1
  119. package/dist/types/server/index.d.ts +3 -0
  120. package/dist/types/utils.d.ts +1 -1
  121. package/dist/types/worker-server.d.ts +1 -2
  122. package/package.json +7 -14
  123. package/dist/js/modern/libs/render/modern/browser-list.js +0 -7
  124. package/dist/js/modern/libs/render/modern/index.js +0 -37
  125. package/dist/js/node/libs/render/modern/browser-list.js +0 -14
  126. package/dist/js/node/libs/render/modern/index.js +0 -46
  127. package/dist/js/treeshaking/libs/render/modern/browser-list.js +0 -7
  128. package/dist/js/treeshaking/libs/render/modern/index.js +0 -39
  129. package/dist/types/libs/render/modern/browser-list.d.ts +0 -1
  130. package/dist/types/libs/render/modern/index.d.ts +0 -3
@@ -1,91 +1,127 @@
1
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
2
- import _createClass from "@babel/runtime/helpers/esm/createClass";
3
- import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
4
- import { Transform } from 'stream';
5
- var RegList = {
6
- before: {
7
- head: '<head>',
8
- body: '<body>'
9
- },
10
- after: {
11
- head: '</head>',
12
- body: '</body>'
13
- }
14
- };
15
- export var TemplateAPI = /*#__PURE__*/function () {
16
- function TemplateAPI(content) {
17
- _classCallCheck(this, TemplateAPI);
18
- _defineProperty(this, "content", void 0);
19
- this.content = content;
20
- }
21
- _createClass(TemplateAPI, [{
22
- key: "get",
23
- value: function get() {
24
- return this.content;
25
- }
26
- }, {
27
- key: "set",
28
- value: function set(content) {
29
- this.content = content;
30
- }
31
- }, {
32
- key: "prependHead",
33
- value: function prependHead(fragment) {
34
- var head = RegList.before.head;
35
- return this.replace(head, "".concat(head).concat(fragment));
1
+ function _classCallCheck(instance, Constructor) {
2
+ if (!(instance instanceof Constructor)) {
3
+ throw new TypeError("Cannot call a class as a function");
36
4
  }
37
- }, {
38
- key: "appendHead",
39
- value: function appendHead(fragment) {
40
- var head = RegList.after.head;
41
- return this.replace(head, "".concat(fragment).concat(head));
5
+ }
6
+ function _defineProperties(target, props) {
7
+ for(var i = 0; i < props.length; i++){
8
+ var descriptor = props[i];
9
+ descriptor.enumerable = descriptor.enumerable || false;
10
+ descriptor.configurable = true;
11
+ if ("value" in descriptor) descriptor.writable = true;
12
+ Object.defineProperty(target, descriptor.key, descriptor);
42
13
  }
43
- }, {
44
- key: "prependBody",
45
- value: function prependBody(fragment) {
46
- var body = RegList.before.body;
47
- return this.replace(body, "".concat(body).concat(fragment));
14
+ }
15
+ function _createClass(Constructor, protoProps, staticProps) {
16
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
17
+ if (staticProps) _defineProperties(Constructor, staticProps);
18
+ return Constructor;
19
+ }
20
+ function _defineProperty(obj, key, value) {
21
+ if (key in obj) {
22
+ Object.defineProperty(obj, key, {
23
+ value: value,
24
+ enumerable: true,
25
+ configurable: true,
26
+ writable: true
27
+ });
28
+ } else {
29
+ obj[key] = value;
48
30
  }
49
- }, {
50
- key: "appendBody",
51
- value: function appendBody(fragment) {
52
- var body = RegList.after.body;
53
- return this.replace(body, "".concat(fragment).concat(body));
31
+ return obj;
32
+ }
33
+ import { Transform } from "stream";
34
+ var RegList = {
35
+ before: {
36
+ head: "<head>",
37
+ body: "<body>"
38
+ },
39
+ after: {
40
+ head: "</head>",
41
+ body: "</body>"
54
42
  }
55
- }, {
56
- key: "replace",
57
- value: function replace(reg, text) {
58
- this.content = this.content.replace(reg, text);
43
+ };
44
+ var TemplateAPI = /*#__PURE__*/ function() {
45
+ "use strict";
46
+ function TemplateAPI(content) {
47
+ _classCallCheck(this, TemplateAPI);
48
+ _defineProperty(this, "content", void 0);
49
+ this.content = content;
59
50
  }
60
- }]);
61
- return TemplateAPI;
51
+ _createClass(TemplateAPI, [
52
+ {
53
+ key: "get",
54
+ value: function get() {
55
+ return this.content;
56
+ }
57
+ },
58
+ {
59
+ key: "set",
60
+ value: function set(content) {
61
+ this.content = content;
62
+ }
63
+ },
64
+ {
65
+ key: "prependHead",
66
+ value: function prependHead(fragment) {
67
+ var head = RegList.before.head;
68
+ return this.replace(head, "".concat(head).concat(fragment));
69
+ }
70
+ },
71
+ {
72
+ key: "appendHead",
73
+ value: function appendHead(fragment) {
74
+ var head = RegList.after.head;
75
+ return this.replace(head, "".concat(fragment).concat(head));
76
+ }
77
+ },
78
+ {
79
+ key: "prependBody",
80
+ value: function prependBody(fragment) {
81
+ var body = RegList.before.body;
82
+ return this.replace(body, "".concat(body).concat(fragment));
83
+ }
84
+ },
85
+ {
86
+ key: "appendBody",
87
+ value: function appendBody(fragment) {
88
+ var body = RegList.after.body;
89
+ return this.replace(body, "".concat(fragment).concat(body));
90
+ }
91
+ },
92
+ {
93
+ key: "replace",
94
+ value: function replace(reg, text) {
95
+ this.content = this.content.replace(reg, text);
96
+ }
97
+ }
98
+ ]);
99
+ return TemplateAPI;
62
100
  }();
63
- export var templateInjectableStream = function templateInjectableStream(_ref) {
64
- var prependHead = _ref.prependHead,
65
- appendHead = _ref.appendHead,
66
- prependBody = _ref.prependBody,
67
- appendBody = _ref.appendBody;
68
- return new Transform({
69
- write: function write(chunk, _, callback) {
70
- var chunk_str = chunk.toString();
71
- if (prependHead) {
72
- var head = RegList.before.head;
73
- chunk_str = chunk_str.replace(head, "".concat(head).concat(prependHead));
74
- }
75
- if (appendHead) {
76
- var _head = RegList.after.head;
77
- chunk_str = chunk_str.replace(_head, "".concat(appendHead).concat(_head));
78
- }
79
- if (prependBody) {
80
- var body = RegList.before.body;
81
- chunk_str = chunk_str.replace(body, "".concat(body).concat(prependBody));
82
- }
83
- if (appendBody) {
84
- var _body = RegList.after.body;
85
- chunk_str = chunk_str.replace(_body, "".concat(appendBody).concat(_body));
86
- }
87
- this.push(chunk_str);
88
- callback();
89
- }
90
- });
91
- };
101
+ var templateInjectableStream = function(param) {
102
+ var prependHead = param.prependHead, appendHead = param.appendHead, prependBody = param.prependBody, appendBody = param.appendBody;
103
+ return new Transform({
104
+ write: function write(chunk, _, callback) {
105
+ var chunk_str = chunk.toString();
106
+ if (prependHead) {
107
+ var head = RegList.before.head;
108
+ chunk_str = chunk_str.replace(head, "".concat(head).concat(prependHead));
109
+ }
110
+ if (appendHead) {
111
+ var head1 = RegList.after.head;
112
+ chunk_str = chunk_str.replace(head1, "".concat(appendHead).concat(head1));
113
+ }
114
+ if (prependBody) {
115
+ var body = RegList.before.body;
116
+ chunk_str = chunk_str.replace(body, "".concat(body).concat(prependBody));
117
+ }
118
+ if (appendBody) {
119
+ var body1 = RegList.after.body;
120
+ chunk_str = chunk_str.replace(body1, "".concat(appendBody).concat(body1));
121
+ }
122
+ this.push(chunk_str);
123
+ callback();
124
+ }
125
+ });
126
+ };
127
+ export { TemplateAPI, templateInjectableStream };
@@ -1,39 +1,82 @@
1
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import * as path from 'path';
3
- import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from '@modern-js/utils';
4
- import mergeDeep from 'merge-deep';
5
- export var getServerConfigPath = function getServerConfigPath(distDirectory) {
6
- var serverConfigFile = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_SERVER_CONFIG;
7
- var serverConfigPath = path.join(distDirectory, serverConfigFile);
8
- return "".concat(serverConfigPath, ".js");
1
+ function _defineProperty(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
8
+ });
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _objectSpread(target) {
15
+ for(var i = 1; i < arguments.length; i++){
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
22
+ }
23
+ ownKeys.forEach(function(key) {
24
+ _defineProperty(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
+ }
29
+ function ownKeys(object, enumerableOnly) {
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
37
+ }
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
+ }
42
+ function _objectSpreadProps(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ import * as path from "path";
54
+ import { compatRequire, fs, DEFAULT_SERVER_CONFIG } from "@modern-js/utils";
55
+ import mergeDeep from "merge-deep";
56
+ var getServerConfigPath = function(distDirectory) {
57
+ var serverConfigFile = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : DEFAULT_SERVER_CONFIG;
58
+ var serverConfigPath = path.join(distDirectory, serverConfigFile);
59
+ return "".concat(serverConfigPath, ".js");
9
60
  };
10
- export var requireConfig = function requireConfig(serverConfigPath) {
11
- if (fs.pathExistsSync(serverConfigPath)) {
12
- return compatRequire(serverConfigPath);
13
- }
14
- return {};
61
+ var requireConfig = function(serverConfigPath) {
62
+ if (fs.pathExistsSync(serverConfigPath)) {
63
+ return compatRequire(serverConfigPath);
64
+ }
65
+ return {};
15
66
  };
16
-
17
- /**
18
- * 对配置进行合并,开发环境下,cliConfig 与 serverConfig 进行深合并
19
- * 生产环境下,resolvedConfig serverConfig 进行深合并
20
- * resolvedConfigPath: 构建序列化后的 modern.config.js 文件路径
21
- */
22
- export var loadConfig = function loadConfig(_ref) {
23
- var cliConfig = _ref.cliConfig,
24
- serverConfig = _ref.serverConfig,
25
- resolvedConfigPath = _ref.resolvedConfigPath;
26
- var config = null;
27
- if (process.env.NODE_ENV === 'production') {
28
- var resolvedConfig = requireConfig(resolvedConfigPath);
29
- // cli config has a higher priority,because it's an argument passed in.
30
- config = mergeDeep(_objectSpread(_objectSpread({}, resolvedConfig), {}, {
31
- plugins: [] // filter cli plugins
32
- }), serverConfig, cliConfig);
33
- } else {
34
- config = mergeDeep(_objectSpread(_objectSpread({}, cliConfig), {}, {
35
- plugins: []
36
- }), serverConfig);
37
- }
38
- return config;
39
- };
67
+ var loadConfig = function(param) {
68
+ var cliConfig = param.cliConfig, serverConfig = param.serverConfig, resolvedConfigPath = param.resolvedConfigPath;
69
+ var config = null;
70
+ if (process.env.NODE_ENV === "production") {
71
+ var resolvedConfig = requireConfig(resolvedConfigPath);
72
+ config = mergeDeep(_objectSpreadProps(_objectSpread({}, resolvedConfig), {
73
+ plugins: []
74
+ }), serverConfig, cliConfig);
75
+ } else {
76
+ config = mergeDeep(_objectSpreadProps(_objectSpread({}, cliConfig), {
77
+ plugins: []
78
+ }), serverConfig);
79
+ }
80
+ return config;
81
+ };
82
+ export { getServerConfigPath, loadConfig, requireConfig };
@@ -1,12 +1,6 @@
1
1
  var metrics = {
2
- gauges: function gauges() {
3
- // no impl
4
- },
5
- emitCounter: function emitCounter() {
6
- // no impl
7
- },
8
- emitTimer: function emitTimer() {
9
- // no impl
10
- }
2
+ gauges: function gauges() {},
3
+ emitCounter: function emitCounter() {},
4
+ emitTimer: function emitTimer() {}
11
5
  };
12
- export { metrics };
6
+ export { metrics };
@@ -1,80 +1,244 @@
1
- import _regeneratorRuntime from "@babel/runtime/helpers/esm/regeneratorRuntime";
2
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
- import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
- import { createProxyMiddleware } from 'http-proxy-middleware';
6
- import { debug } from "../utils";
7
- export function formatProxyOptions(proxyOptions) {
8
- var formattedProxy = [];
9
- if (!Array.isArray(proxyOptions)) {
10
- if ('target' in proxyOptions) {
11
- formattedProxy.push(proxyOptions);
1
+ function _arrayLikeToArray(arr, len) {
2
+ if (len == null || len > arr.length) len = arr.length;
3
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
4
+ return arr2;
5
+ }
6
+ function _arrayWithoutHoles(arr) {
7
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
8
+ }
9
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
10
+ try {
11
+ var info = gen[key](arg);
12
+ var value = info.value;
13
+ } catch (error) {
14
+ reject(error);
15
+ return;
16
+ }
17
+ if (info.done) {
18
+ resolve(value);
12
19
  } else {
13
- Array.prototype.push.apply(formattedProxy, Object.keys(proxyOptions).reduce(function (total, source) {
14
- var option = proxyOptions[source];
15
- total.push(_objectSpread({
16
- context: source,
17
- changeOrigin: true,
18
- logLevel: 'warn'
19
- }, typeof option === 'string' ? {
20
- target: option
21
- } : option));
22
- return total;
23
- }, []));
20
+ Promise.resolve(value).then(_next, _throw);
24
21
  }
25
- } else {
26
- formattedProxy.push.apply(formattedProxy, _toConsumableArray(proxyOptions));
27
- }
28
- return formattedProxy;
29
22
  }
30
- export var createProxyHandler = function createProxyHandler(proxyOptions) {
31
- debug('createProxyHandler', proxyOptions);
32
- if (!proxyOptions) {
33
- return null;
34
- }
35
-
36
- // If it is not an array, it may be an object that uses the context attribute
37
- // or an object in the form of { source: ProxyDetail }
38
- var formattedProxy = formatProxyOptions(proxyOptions);
39
- var middlewares = formattedProxy.map(function (option) {
40
- var middleware = createProxyMiddleware(option.context, option);
41
-
42
- // eslint-disable-next-line consistent-return
43
- return /*#__PURE__*/function () {
44
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(ctx, next) {
45
- var req, res, bypassUrl;
46
- return _regeneratorRuntime().wrap(function _callee$(_context) {
47
- while (1) {
48
- switch (_context.prev = _context.next) {
49
- case 0:
50
- req = ctx.req, res = ctx.res;
51
- bypassUrl = typeof option.bypass === 'function' ? option.bypass(req, res, option) : null; // only false, no true
52
- if (!(typeof bypassUrl === 'boolean')) {
53
- _context.next = 7;
54
- break;
55
- }
56
- ctx.status = 404;
57
- return _context.abrupt("return", next());
58
- case 7:
59
- if (!(typeof bypassUrl === 'string')) {
60
- _context.next = 10;
61
- break;
62
- }
63
- ctx.url = bypassUrl;
64
- return _context.abrupt("return", next());
65
- case 10:
66
- middleware(req, res, next);
67
- case 11:
68
- case "end":
69
- return _context.stop();
23
+ function _asyncToGenerator(fn) {
24
+ return function() {
25
+ var self = this, args = arguments;
26
+ return new Promise(function(resolve, reject) {
27
+ var gen = fn.apply(self, args);
28
+ function _next(value) {
29
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30
+ }
31
+ function _throw(err) {
32
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
70
33
  }
71
- }
72
- }, _callee);
73
- }));
74
- return function (_x, _x2) {
75
- return _ref.apply(this, arguments);
76
- };
77
- }();
78
- });
79
- return middlewares;
80
- };
34
+ _next(undefined);
35
+ });
36
+ };
37
+ }
38
+ function _defineProperty(obj, key, value) {
39
+ if (key in obj) {
40
+ Object.defineProperty(obj, key, {
41
+ value: value,
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true
45
+ });
46
+ } else {
47
+ obj[key] = value;
48
+ }
49
+ return obj;
50
+ }
51
+ function _iterableToArray(iter) {
52
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
53
+ }
54
+ function _nonIterableSpread() {
55
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
56
+ }
57
+ function _objectSpread(target) {
58
+ for(var i = 1; i < arguments.length; i++){
59
+ var source = arguments[i] != null ? arguments[i] : {};
60
+ var ownKeys = Object.keys(source);
61
+ if (typeof Object.getOwnPropertySymbols === "function") {
62
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
63
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
64
+ }));
65
+ }
66
+ ownKeys.forEach(function(key) {
67
+ _defineProperty(target, key, source[key]);
68
+ });
69
+ }
70
+ return target;
71
+ }
72
+ function _toConsumableArray(arr) {
73
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
74
+ }
75
+ function _unsupportedIterableToArray(o, minLen) {
76
+ if (!o) return;
77
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
78
+ var n = Object.prototype.toString.call(o).slice(8, -1);
79
+ if (n === "Object" && o.constructor) n = o.constructor.name;
80
+ if (n === "Map" || n === "Set") return Array.from(n);
81
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
82
+ }
83
+ var __generator = this && this.__generator || function(thisArg, body) {
84
+ var f, y, t, g, _ = {
85
+ label: 0,
86
+ sent: function() {
87
+ if (t[0] & 1) throw t[1];
88
+ return t[1];
89
+ },
90
+ trys: [],
91
+ ops: []
92
+ };
93
+ return(g = {
94
+ next: verb(0),
95
+ "throw": verb(1),
96
+ "return": verb(2)
97
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
98
+ return this;
99
+ }), g);
100
+ function verb(n) {
101
+ return function(v) {
102
+ return step([
103
+ n,
104
+ v
105
+ ]);
106
+ };
107
+ }
108
+ function step(op) {
109
+ if (f) throw new TypeError("Generator is already executing.");
110
+ while(_)try {
111
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
112
+ if (y = 0, t) op = [
113
+ op[0] & 2,
114
+ t.value
115
+ ];
116
+ switch(op[0]){
117
+ case 0:
118
+ case 1:
119
+ t = op;
120
+ break;
121
+ case 4:
122
+ _.label++;
123
+ return {
124
+ value: op[1],
125
+ done: false
126
+ };
127
+ case 5:
128
+ _.label++;
129
+ y = op[1];
130
+ op = [
131
+ 0
132
+ ];
133
+ continue;
134
+ case 7:
135
+ op = _.ops.pop();
136
+ _.trys.pop();
137
+ continue;
138
+ default:
139
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
140
+ _ = 0;
141
+ continue;
142
+ }
143
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
144
+ _.label = op[1];
145
+ break;
146
+ }
147
+ if (op[0] === 6 && _.label < t[1]) {
148
+ _.label = t[1];
149
+ t = op;
150
+ break;
151
+ }
152
+ if (t && _.label < t[2]) {
153
+ _.label = t[2];
154
+ _.ops.push(op);
155
+ break;
156
+ }
157
+ if (t[2]) _.ops.pop();
158
+ _.trys.pop();
159
+ continue;
160
+ }
161
+ op = body.call(thisArg, _);
162
+ } catch (e) {
163
+ op = [
164
+ 6,
165
+ e
166
+ ];
167
+ y = 0;
168
+ } finally{
169
+ f = t = 0;
170
+ }
171
+ if (op[0] & 5) throw op[1];
172
+ return {
173
+ value: op[0] ? op[1] : void 0,
174
+ done: true
175
+ };
176
+ }
177
+ };
178
+ import { createProxyMiddleware } from "http-proxy-middleware";
179
+ import { debug } from "../utils";
180
+ function formatProxyOptions(proxyOptions) {
181
+ var formattedProxy = [];
182
+ if (!Array.isArray(proxyOptions)) {
183
+ if ("target" in proxyOptions) {
184
+ formattedProxy.push(proxyOptions);
185
+ } else {
186
+ Array.prototype.push.apply(formattedProxy, Object.keys(proxyOptions).reduce(function(total, source) {
187
+ var option = proxyOptions[source];
188
+ total.push(_objectSpread({
189
+ context: source,
190
+ changeOrigin: true,
191
+ logLevel: "warn"
192
+ }, typeof option === "string" ? {
193
+ target: option
194
+ } : option));
195
+ return total;
196
+ }, []));
197
+ }
198
+ } else {
199
+ var _formattedProxy;
200
+ (_formattedProxy = formattedProxy).push.apply(_formattedProxy, _toConsumableArray(proxyOptions));
201
+ }
202
+ return formattedProxy;
203
+ }
204
+ var createProxyHandler = function(proxyOptions) {
205
+ debug("createProxyHandler", proxyOptions);
206
+ if (!proxyOptions) {
207
+ return null;
208
+ }
209
+ var formattedProxy = formatProxyOptions(proxyOptions);
210
+ var middlewares = formattedProxy.map(function(option) {
211
+ var middleware = createProxyMiddleware(option.context, option);
212
+ return function() {
213
+ var _ref = _asyncToGenerator(function(ctx, next) {
214
+ var req, res, bypassUrl;
215
+ return __generator(this, function(_state) {
216
+ req = ctx.req, res = ctx.res;
217
+ bypassUrl = typeof option.bypass === "function" ? option.bypass(req, res, option) : null;
218
+ if (typeof bypassUrl === "boolean") {
219
+ ctx.status = 404;
220
+ return [
221
+ 2,
222
+ next()
223
+ ];
224
+ } else if (typeof bypassUrl === "string") {
225
+ ctx.url = bypassUrl;
226
+ return [
227
+ 2,
228
+ next()
229
+ ];
230
+ }
231
+ middleware(req, res, next);
232
+ return [
233
+ 2
234
+ ];
235
+ });
236
+ });
237
+ return function(ctx, next) {
238
+ return _ref.apply(this, arguments);
239
+ };
240
+ }();
241
+ });
242
+ return middlewares;
243
+ };
244
+ export { createProxyHandler, formatProxyOptions };