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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +110 -0
  2. package/dist/js/modern/constants.js +0 -2
  3. package/dist/js/modern/index.js +0 -1
  4. package/dist/js/modern/libs/context/context.js +10 -56
  5. package/dist/js/modern/libs/hook-api/index.js +0 -31
  6. package/dist/js/modern/libs/hook-api/route.js +0 -8
  7. package/dist/js/modern/libs/hook-api/template.js +0 -16
  8. package/dist/js/modern/libs/loadConfig.js +3 -10
  9. package/dist/js/modern/libs/metrics.js +6 -6
  10. package/dist/js/modern/libs/proxy.js +7 -12
  11. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +0 -1
  12. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +0 -9
  13. package/dist/js/modern/libs/render/cache/index.js +8 -16
  14. package/dist/js/modern/libs/render/cache/page-caches/lru.js +0 -10
  15. package/dist/js/modern/libs/render/cache/spr.js +12 -62
  16. package/dist/js/modern/libs/render/cache/util.js +0 -6
  17. package/dist/js/modern/libs/render/index.js +6 -11
  18. package/dist/js/modern/libs/render/measure.js +1 -11
  19. package/dist/js/modern/libs/render/modern/index.js +2 -13
  20. package/dist/js/modern/libs/render/reader.js +13 -24
  21. package/dist/js/modern/libs/render/ssr.js +5 -6
  22. package/dist/js/modern/libs/render/static.js +6 -9
  23. package/dist/js/modern/libs/render/type.js +0 -1
  24. package/dist/js/modern/libs/route/index.js +8 -19
  25. package/dist/js/modern/libs/route/matcher.js +21 -29
  26. package/dist/js/modern/libs/route/route.js +0 -13
  27. package/dist/js/modern/libs/serve-file.js +5 -6
  28. package/dist/js/modern/server/index.js +25 -52
  29. package/dist/js/modern/server/modern-server-split.js +0 -11
  30. package/dist/js/modern/server/modern-server.js +87 -167
  31. package/dist/js/modern/utils.js +12 -27
  32. package/dist/js/modern/worker-server.js +34 -0
  33. package/dist/js/node/constants.js +0 -2
  34. package/dist/js/node/index.js +0 -10
  35. package/dist/js/node/libs/context/context.js +10 -65
  36. package/dist/js/node/libs/context/index.js +0 -3
  37. package/dist/js/node/libs/hook-api/index.js +0 -42
  38. package/dist/js/node/libs/hook-api/route.js +0 -10
  39. package/dist/js/node/libs/hook-api/template.js +0 -22
  40. package/dist/js/node/libs/loadConfig.js +3 -22
  41. package/dist/js/node/libs/metrics.js +6 -6
  42. package/dist/js/node/libs/proxy.js +7 -17
  43. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +0 -5
  44. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +0 -12
  45. package/dist/js/node/libs/render/cache/index.js +8 -22
  46. package/dist/js/node/libs/render/cache/page-caches/index.js +0 -2
  47. package/dist/js/node/libs/render/cache/page-caches/lru.js +0 -14
  48. package/dist/js/node/libs/render/cache/spr.js +12 -71
  49. package/dist/js/node/libs/render/cache/util.js +0 -18
  50. package/dist/js/node/libs/render/index.js +6 -26
  51. package/dist/js/node/libs/render/measure.js +0 -17
  52. package/dist/js/node/libs/render/modern/index.js +2 -20
  53. package/dist/js/node/libs/render/reader.js +12 -39
  54. package/dist/js/node/libs/render/ssr.js +4 -16
  55. package/dist/js/node/libs/render/static.js +6 -18
  56. package/dist/js/node/libs/render/type.js +0 -1
  57. package/dist/js/node/libs/route/index.js +8 -22
  58. package/dist/js/node/libs/route/matcher.js +18 -34
  59. package/dist/js/node/libs/route/route.js +0 -15
  60. package/dist/js/node/libs/serve-file.js +5 -13
  61. package/dist/js/node/server/index.js +25 -72
  62. package/dist/js/node/server/modern-server-split.js +0 -13
  63. package/dist/js/node/server/modern-server.js +87 -196
  64. package/dist/js/node/utils.js +13 -52
  65. package/dist/js/node/worker-server.js +41 -0
  66. package/dist/js/treeshaking/constants.js +28 -0
  67. package/dist/js/treeshaking/index.js +13 -0
  68. package/dist/js/treeshaking/libs/context/context.js +243 -0
  69. package/dist/js/treeshaking/libs/context/index.js +5 -0
  70. package/dist/js/treeshaking/libs/hook-api/index.js +157 -0
  71. package/dist/js/treeshaking/libs/hook-api/route.js +33 -0
  72. package/dist/js/treeshaking/libs/hook-api/template.js +91 -0
  73. package/dist/js/treeshaking/libs/loadConfig.js +39 -0
  74. package/dist/js/treeshaking/libs/metrics.js +12 -0
  75. package/dist/js/treeshaking/libs/proxy.js +80 -0
  76. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +124 -0
  77. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +464 -0
  78. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +53 -0
  79. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +35 -0
  80. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +121 -0
  81. package/dist/js/treeshaking/libs/render/cache/index.js +184 -0
  82. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +30 -0
  83. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +46 -0
  84. package/dist/js/treeshaking/libs/render/cache/spr.js +362 -0
  85. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  86. package/dist/js/treeshaking/libs/render/cache/util.js +101 -0
  87. package/dist/js/treeshaking/libs/render/index.js +100 -0
  88. package/dist/js/treeshaking/libs/render/measure.js +61 -0
  89. package/dist/js/treeshaking/libs/render/modern/browser-list.js +7 -0
  90. package/dist/js/treeshaking/libs/render/modern/index.js +39 -0
  91. package/dist/js/treeshaking/libs/render/reader.js +191 -0
  92. package/dist/js/treeshaking/libs/render/ssr.js +98 -0
  93. package/dist/js/treeshaking/libs/render/static.js +84 -0
  94. package/dist/js/treeshaking/libs/render/type.js +6 -0
  95. package/dist/js/treeshaking/libs/route/index.js +94 -0
  96. package/dist/js/treeshaking/libs/route/matcher.js +113 -0
  97. package/dist/js/treeshaking/libs/route/route.js +26 -0
  98. package/dist/js/treeshaking/libs/serve-file.js +75 -0
  99. package/dist/js/treeshaking/server/index.js +339 -0
  100. package/dist/js/treeshaking/server/modern-server-split.js +152 -0
  101. package/dist/js/treeshaking/server/modern-server.js +946 -0
  102. package/dist/js/treeshaking/type.js +1 -0
  103. package/dist/js/treeshaking/utils.js +90 -0
  104. package/dist/js/treeshaking/worker-server.js +56 -0
  105. package/dist/types/index.d.ts +0 -2
  106. package/dist/types/libs/context/context.d.ts +0 -3
  107. package/dist/types/libs/loadConfig.d.ts +0 -1
  108. package/dist/types/libs/render/cache/index.d.ts +0 -2
  109. package/dist/types/libs/render/cache/spr.d.ts +0 -2
  110. package/dist/types/server/index.d.ts +0 -3
  111. package/dist/types/utils.d.ts +2 -3
  112. package/dist/types/worker-server.d.ts +16 -0
  113. package/package.json +31 -15
@@ -3,35 +3,24 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useLocalPrefix = exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.isRedirect = exports.headersWithoutCookie = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
7
-
8
- var _pathToRegexp = require("path-to-regexp");
9
-
6
+ exports.useLocalPrefix = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.isRedirect = exports.headersWithoutCookie = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
10
7
  var _utils = require("@modern-js/utils");
11
-
12
8
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
13
-
14
9
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
15
-
16
10
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
17
-
18
11
  const debug = (0, _utils.createDebugger)('prod-server');
19
12
  exports.debug = debug;
20
-
21
13
  const mergeExtension = users => {
22
14
  const output = [];
23
15
  return {
24
16
  middleware: output.concat(users)
25
17
  };
26
18
  };
27
-
28
19
  exports.mergeExtension = mergeExtension;
29
-
30
- const noop = () => {// noop
20
+ const noop = () => {
21
+ // noop
31
22
  };
32
-
33
23
  exports.noop = noop;
34
-
35
24
  const createErrorDocument = (status, text) => {
36
25
  const title = `${status}: ${text}`;
37
26
  return `<!DOCTYPE html>
@@ -65,59 +54,44 @@ const createErrorDocument = (status, text) => {
65
54
  </html>
66
55
  `;
67
56
  };
68
-
69
57
  exports.createErrorDocument = createErrorDocument;
70
-
71
58
  const createMiddlewareCollecter = () => {
72
59
  const webMiddlewares = [];
73
60
  const apiMiddlewares = [];
74
-
75
61
  const addWebMiddleware = input => {
76
62
  webMiddlewares.push(input);
77
63
  };
78
-
79
64
  const addAPIMiddleware = input => {
80
65
  apiMiddlewares.push(input);
81
66
  };
82
-
83
67
  const getMiddlewares = () => ({
84
68
  web: webMiddlewares,
85
69
  api: apiMiddlewares
86
70
  });
87
-
88
71
  return {
89
72
  getMiddlewares,
90
73
  addWebMiddleware,
91
74
  addAPIMiddleware
92
75
  };
93
76
  };
94
-
95
77
  exports.createMiddlewareCollecter = createMiddlewareCollecter;
96
-
97
- const toPath = (reg, params) => {
98
- const fn = (0, _pathToRegexp.compile)(reg, {
99
- encode: encodeURIComponent
100
- });
101
- return fn(params);
102
- };
103
-
104
- exports.toPath = toPath;
105
-
106
78
  const useLocalPrefix = url => {
107
79
  return (0, _utils.isProd)() && !url.includes('.');
108
80
  };
109
-
110
81
  exports.useLocalPrefix = useLocalPrefix;
111
-
112
- const getStaticReg = (output = {}) => {
82
+ const getStaticReg = (output = {}, html = {}) => {
113
83
  const {
114
- favicon,
115
- faviconByEntries,
116
- cssPath,
117
- jsPath,
118
- mediaPath,
84
+ distPath: {
85
+ css: cssPath,
86
+ js: jsPath,
87
+ media: mediaPath
88
+ } = {},
119
89
  assetPrefix = '/'
120
90
  } = output;
91
+ const {
92
+ favicon,
93
+ faviconByEntries
94
+ } = html;
121
95
  const prefix = useLocalPrefix(assetPrefix) ? assetPrefix : '';
122
96
  const favicons = prepareFavicons(favicon, faviconByEntries);
123
97
  const staticFiles = [cssPath, jsPath, mediaPath].filter(v => Boolean(v));
@@ -127,46 +101,33 @@ const getStaticReg = (output = {}) => {
127
101
  const staticPathRegExp = new RegExp(`^${regPrefix}(${[...staticReg, ...iconReg].join('|')})`);
128
102
  return staticPathRegExp;
129
103
  };
130
-
131
104
  exports.getStaticReg = getStaticReg;
132
-
133
105
  const prepareFavicons = (favicon, faviconByEntries) => {
134
106
  const faviconNames = [];
135
-
136
107
  if (favicon) {
137
108
  faviconNames.push(favicon.substring(favicon.lastIndexOf('/') + 1));
138
109
  }
139
-
140
110
  if (faviconByEntries) {
141
111
  Object.keys(faviconByEntries).forEach(f => {
142
112
  const curFavicon = faviconByEntries[f];
143
-
144
113
  if (curFavicon) {
145
114
  faviconNames.push(curFavicon.substring(curFavicon.lastIndexOf('/') + 1));
146
115
  }
147
116
  });
148
117
  }
149
-
150
118
  return faviconNames;
151
119
  };
152
-
153
120
  exports.prepareFavicons = prepareFavicons;
154
-
155
121
  const headersWithoutCookie = headers => {
156
122
  if (typeof headers.cookie !== 'undefined') {
157
123
  const safeHeaders = _objectSpread({}, headers);
158
-
159
124
  delete safeHeaders.cookie;
160
125
  return safeHeaders;
161
126
  }
162
-
163
127
  return headers;
164
128
  };
165
-
166
129
  exports.headersWithoutCookie = headersWithoutCookie;
167
-
168
130
  const isRedirect = code => {
169
131
  return [301, 302, 307, 308].includes(code);
170
132
  };
171
-
172
133
  exports.isRedirect = isRedirect;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createHandler = void 0;
7
+ var _route = require("./libs/route");
8
+ const createHandler = manifest => {
9
+ const routeMgr = new _route.RouteMatchManager();
10
+ const {
11
+ pages,
12
+ routes
13
+ } = manifest;
14
+ routeMgr.reset(routes);
15
+ return async ctx => {
16
+ var _ctx$request, _ctx$request$query, _ctx$request2, _ctx$request2$pathnam, _ctx$request3, _ctx$request3$params;
17
+ const pageMatch = routeMgr.match(ctx.url);
18
+ if (!pageMatch) {
19
+ ctx.body = '404: Page not found';
20
+ ctx.status = 404;
21
+ return;
22
+ }
23
+ const page = pages[pageMatch.spec.urlPath];
24
+ (_ctx$request$query = (_ctx$request = ctx.request).query) !== null && _ctx$request$query !== void 0 ? _ctx$request$query : _ctx$request.query = ctx.query;
25
+ (_ctx$request2$pathnam = (_ctx$request2 = ctx.request).pathname) !== null && _ctx$request2$pathnam !== void 0 ? _ctx$request2$pathnam : _ctx$request2.pathname = ctx.pathname;
26
+ (_ctx$request3$params = (_ctx$request3 = ctx.request).params) !== null && _ctx$request3$params !== void 0 ? _ctx$request3$params : _ctx$request3.params = ctx.params;
27
+ const params = pageMatch.parseURLParams(ctx.url);
28
+ ctx.body = await page.serverRender({
29
+ template: page.template,
30
+ query: ctx.query,
31
+ request: ctx.request,
32
+ response: ctx.response,
33
+ pathname: ctx.pathname,
34
+ req: ctx.request,
35
+ res: ctx.response,
36
+ params: ctx.params || params || {}
37
+ });
38
+ ctx.status = 200;
39
+ };
40
+ };
41
+ exports.createHandler = createHandler;
@@ -0,0 +1,28 @@
1
+ export var AGGRED_DIR = {
2
+ mock: 'config/mock',
3
+ server: 'server',
4
+ api: 'api',
5
+ shared: 'shared',
6
+ lambda: 'lambda'
7
+ };
8
+ export var ApiServerMode;
9
+ (function (ApiServerMode) {
10
+ ApiServerMode["func"] = "function";
11
+ ApiServerMode["frame"] = "framework";
12
+ })(ApiServerMode || (ApiServerMode = {}));
13
+ export var ERROR_DIGEST = {
14
+ INIT: 'Server init error',
15
+ ENOTF: 'Page could not be found',
16
+ WARMUP: 'SSR warmup failed',
17
+ EINTER: 'Internal server error',
18
+ ERENDER: 'SSR render failed',
19
+ EMICROINJ: 'Get micro-frontend info failed'
20
+ };
21
+ export var ERROR_PAGE_TEXT = {
22
+ 404: 'This page could not be found.',
23
+ 500: 'Internal Server Error.'
24
+ };
25
+ export var RUN_MODE = {
26
+ FULL: 'full',
27
+ TYPE: 'type'
28
+ };
@@ -0,0 +1,13 @@
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 (function (options) {
8
+ if (options == null) {
9
+ throw new Error('can not start mserver without options');
10
+ }
11
+ var server = new Server(options);
12
+ return server.init();
13
+ });
@@ -0,0 +1,243 @@
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 { URL } from 'url';
5
+ import qs from 'querystring';
6
+ import { Buffer } from 'buffer';
7
+ import createEtag from 'etag';
8
+ import fresh from 'fresh';
9
+ import { headersWithoutCookie } from "../../utils";
10
+ export var ModernServerContext = /*#__PURE__*/function () {
11
+ function ModernServerContext(req, res, options) {
12
+ _classCallCheck(this, ModernServerContext);
13
+ _defineProperty(this, "req", void 0);
14
+ _defineProperty(this, "res", void 0);
15
+ _defineProperty(this, "params", {});
16
+ _defineProperty(this, "serverData", void 0);
17
+ _defineProperty(this, "options", {});
18
+ this.req = req;
19
+ this.res = res;
20
+ this.options = options || {};
21
+ this.serverData = {};
22
+ this.bind();
23
+ }
24
+ _createClass(ModernServerContext, [{
25
+ key: "logger",
26
+ get:
27
+ /**
28
+ * http request
29
+ */
30
+
31
+ /**
32
+ * http response
33
+ */
34
+
35
+ /**
36
+ * url params
37
+ */
38
+
39
+ function get() {
40
+ return this.req.logger;
41
+ }
42
+ }, {
43
+ key: "metrics",
44
+ get: function get() {
45
+ return this.req.metrics;
46
+ }
47
+ }, {
48
+ key: "bind",
49
+ value: function bind() {
50
+ var _this = this;
51
+ var _ref = this,
52
+ req = _ref.req,
53
+ res = _ref.res;
54
+ req.get = function (key) {
55
+ return _this.getReqHeader(key);
56
+ };
57
+ res.set = function (key, value) {
58
+ return _this.res.setHeader(key, value);
59
+ };
60
+ res.send = function (body) {
61
+ _this.send(body);
62
+ };
63
+ }
64
+
65
+ // compat express res.send, only support etag now
66
+ }, {
67
+ key: "send",
68
+ value: function send(body) {
69
+ try {
70
+ var generateETag = !this.res.getHeader('ETag') && this.options.etag;
71
+ if (body !== undefined && generateETag) {
72
+ var encoding = typeof body === 'string' ? 'utf-8' : undefined;
73
+ var buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
74
+ var etag = createEtag(buf, {
75
+ weak: true
76
+ });
77
+ if (etag) {
78
+ this.res.setHeader('ETag', etag);
79
+ }
80
+ }
81
+ if (this.fresh) {
82
+ this.status = 304;
83
+ }
84
+ } catch (e) {
85
+ this.logger.error(e.message);
86
+ }
87
+ this.res.end(body);
88
+ }
89
+ }, {
90
+ key: "setParams",
91
+ value: function setParams(params) {
92
+ this.params = params;
93
+ }
94
+ }, {
95
+ key: "setServerData",
96
+ value: function setServerData(key, value) {
97
+ this.serverData[key] = value;
98
+ }
99
+ }, {
100
+ key: "getReqHeader",
101
+ value: function getReqHeader(key) {
102
+ var req = this.req;
103
+ var field = key.toLowerCase();
104
+ switch (field) {
105
+ case 'referer':
106
+ case 'referrer':
107
+ return req.headers.referrer || req.headers.referer || '';
108
+ default:
109
+ return req.headers[field] || '';
110
+ }
111
+ }
112
+ }, {
113
+ key: "fresh",
114
+ get: function get() {
115
+ var status = this.status,
116
+ res = this.res,
117
+ method = this.method;
118
+
119
+ // GET or HEAD for weak freshness validation only
120
+ if ('GET' !== method && 'HEAD' !== method) {
121
+ return false;
122
+ }
123
+ if (status >= 200 && status < 300 || 304 === status) {
124
+ return fresh(this.headers, {
125
+ etag: res.getHeader('ETag'),
126
+ 'last-modified': res.getHeader('Last-Modified')
127
+ });
128
+ }
129
+ return false;
130
+ }
131
+
132
+ /* request property */
133
+ }, {
134
+ key: "headers",
135
+ get: function get() {
136
+ return this.req.headers;
137
+ }
138
+ }, {
139
+ key: "method",
140
+ get: function get() {
141
+ return this.req.method;
142
+ }
143
+ }, {
144
+ key: "url",
145
+ get: function get() {
146
+ return this.req.url || '';
147
+ },
148
+ set: function set(val) {
149
+ this.req.url = val;
150
+ }
151
+ }, {
152
+ key: "host",
153
+ get: function get() {
154
+ var host = this.getReqHeader('X-Forwarded-Host');
155
+ if (!host) {
156
+ host = this.getReqHeader('Host');
157
+ }
158
+ return host.split(/\s*,\s*/, 1)[0] || '';
159
+ }
160
+ }, {
161
+ key: "protocol",
162
+ get: function get() {
163
+ if (this.req.socket.encrypted) {
164
+ return 'https';
165
+ }
166
+ var proto = this.getReqHeader('X-Forwarded-Proto');
167
+ return proto ? proto.split(/\s*,\s*/, 1)[0] : 'http';
168
+ }
169
+ }, {
170
+ key: "origin",
171
+ get: function get() {
172
+ return "".concat(this.protocol, "://").concat(this.host);
173
+ }
174
+ }, {
175
+ key: "href",
176
+ get: function get() {
177
+ return this.origin + this.url;
178
+ }
179
+ }, {
180
+ key: "parsedURL",
181
+ get: function get() {
182
+ var url = new URL(this.req.url, this.origin);
183
+ return url;
184
+ }
185
+ }, {
186
+ key: "path",
187
+ get: function get() {
188
+ return this.parsedURL.pathname;
189
+ },
190
+ set: function set(p) {
191
+ var url = new URL(this.req.url, this.origin);
192
+ // this should never happened
193
+ if (!url || !p) {
194
+ return;
195
+ }
196
+ if (url.pathname === p) {
197
+ return;
198
+ }
199
+ url.pathname = p;
200
+ this.url = url.toString();
201
+ }
202
+ }, {
203
+ key: "querystring",
204
+ get: function get() {
205
+ if (!this.req) {
206
+ return '';
207
+ }
208
+ return this.parsedURL.search.replace(/^\?/, '') || '';
209
+ }
210
+ }, {
211
+ key: "query",
212
+ get: function get() {
213
+ var str = this.querystring;
214
+ return qs.parse(str);
215
+ }
216
+
217
+ /* response property */
218
+ }, {
219
+ key: "status",
220
+ get: function get() {
221
+ return this.res.statusCode;
222
+ },
223
+ set: function set(statusCode) {
224
+ this.res.statusCode = statusCode;
225
+ }
226
+
227
+ /**
228
+ * 判断链接是否已经关闭
229
+ */
230
+ }, {
231
+ key: "resHasHandled",
232
+ value: function resHasHandled() {
233
+ return this.res.writableEnded;
234
+ }
235
+ }, {
236
+ key: "error",
237
+ value: function error(dig) {
238
+ var e = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
239
+ this.logger.error("Web Server Error - ".concat(dig, ", error = %s, req.url = %s, req.headers = %o"), e instanceof Error ? e.stack || e.message : e, this.path, headersWithoutCookie(this.headers));
240
+ }
241
+ }]);
242
+ return ModernServerContext;
243
+ }();
@@ -0,0 +1,5 @@
1
+ import { ModernServerContext } from "./context";
2
+ export var createContext = function createContext(req, res, options) {
3
+ return new ModernServerContext(req, res, options);
4
+ };
5
+ export { ModernServerContext };
@@ -0,0 +1,157 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
4
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
5
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
6
+ import cookie from 'cookie';
7
+ import { RouteAPI } from "./route";
8
+ import { TemplateAPI } from "./template";
9
+ var Response = /*#__PURE__*/function () {
10
+ function Response(res) {
11
+ _classCallCheck(this, Response);
12
+ _defineProperty(this, "cookies", void 0);
13
+ _defineProperty(this, "res", void 0);
14
+ _defineProperty(this, "_cookie", void 0);
15
+ this.res = res;
16
+ this._cookie = cookie.parse(res.getHeader('set-cookie') || '');
17
+ this.cookies = {
18
+ get: this.getCookie.bind(this),
19
+ set: this.setCookie.bind(this),
20
+ "delete": this.deleteCookie.bind(this),
21
+ clear: this.clearCookie.bind(this),
22
+ apply: this.applyCookie.bind(this)
23
+ };
24
+ }
25
+ _createClass(Response, [{
26
+ key: "get",
27
+ value: function get(key) {
28
+ return this.res.getHeader(key);
29
+ }
30
+ }, {
31
+ key: "set",
32
+ value: function set(key, value) {
33
+ return this.res.setHeader(key, value);
34
+ }
35
+ }, {
36
+ key: "status",
37
+ value: function status(code) {
38
+ this.res.statusCode = code;
39
+ }
40
+ }, {
41
+ key: "getCookie",
42
+ value: function getCookie(key) {
43
+ return this._cookie[key];
44
+ }
45
+ }, {
46
+ key: "setCookie",
47
+ value: function setCookie(key, value) {
48
+ this._cookie[key] = value;
49
+ }
50
+ }, {
51
+ key: "deleteCookie",
52
+ value: function deleteCookie(key) {
53
+ if (this._cookie[key]) {
54
+ delete this._cookie[key];
55
+ }
56
+ }
57
+ }, {
58
+ key: "clearCookie",
59
+ value: function clearCookie() {
60
+ this._cookie = {};
61
+ }
62
+ }, {
63
+ key: "applyCookie",
64
+ value: function applyCookie() {
65
+ var str = Object.entries(this._cookie).map(function (_ref) {
66
+ var _ref2 = _slicedToArray(_ref, 2),
67
+ key = _ref2[0],
68
+ value = _ref2[1];
69
+ return cookie.serialize(key, value);
70
+ }).join('; ');
71
+ if (str) {
72
+ this.res.setHeader('set-cookie', str);
73
+ } else {
74
+ this.res.removeHeader('set-cookie');
75
+ }
76
+ }
77
+ }, {
78
+ key: "raw",
79
+ value: function raw(body, _ref3) {
80
+ var _this = this;
81
+ var status = _ref3.status,
82
+ headers = _ref3.headers;
83
+ Object.entries(headers).forEach(function (_ref4) {
84
+ var _ref5 = _slicedToArray(_ref4, 2),
85
+ key = _ref5[0],
86
+ value = _ref5[1];
87
+ _this.res.setHeader(key, value);
88
+ });
89
+ if (status) {
90
+ this.res.statusCode = status;
91
+ }
92
+ this.res.end(body);
93
+ }
94
+ }]);
95
+ return Response;
96
+ }();
97
+ var Request = /*#__PURE__*/function () {
98
+ function Request(ctx) {
99
+ _classCallCheck(this, Request);
100
+ _defineProperty(this, "host", void 0);
101
+ _defineProperty(this, "pathname", void 0);
102
+ _defineProperty(this, "query", void 0);
103
+ _defineProperty(this, "headers", void 0);
104
+ _defineProperty(this, "cookie", void 0);
105
+ _defineProperty(this, "cookies", void 0);
106
+ _defineProperty(this, "_cookie", void 0);
107
+ this.host = ctx.host;
108
+ this.pathname = ctx.path;
109
+ this.query = ctx.query;
110
+ this.headers = ctx.headers;
111
+ this.cookie = ctx.headers.cookie || '';
112
+ this._cookie = cookie.parse(this.cookie);
113
+ this.cookies = {
114
+ get: this.getCookie.bind(this)
115
+ };
116
+ }
117
+ _createClass(Request, [{
118
+ key: "getCookie",
119
+ value: function getCookie(key) {
120
+ return this._cookie[key];
121
+ }
122
+ }]);
123
+ return Request;
124
+ }();
125
+ export var base = function base(context) {
126
+ var res = context.res;
127
+ return {
128
+ response: new Response(res),
129
+ request: new Request(context),
130
+ logger: context.logger,
131
+ metrics: context.metrics
132
+ };
133
+ };
134
+ export var createAfterMatchContext = function createAfterMatchContext(context, entryName) {
135
+ var baseContext = base(context);
136
+ return _objectSpread(_objectSpread({}, baseContext), {}, {
137
+ router: new RouteAPI(entryName)
138
+ });
139
+ };
140
+ export var createAfterRenderContext = function createAfterRenderContext(context, content) {
141
+ var baseContext = base(context);
142
+ return _objectSpread(_objectSpread({}, baseContext), {}, {
143
+ template: new TemplateAPI(content)
144
+ });
145
+ };
146
+ export var createMiddlewareContext = function createMiddlewareContext(context) {
147
+ var baseContext = base(context);
148
+ return _objectSpread(_objectSpread({}, baseContext), {}, {
149
+ response: _objectSpread(_objectSpread({}, baseContext.response), {}, {
150
+ locals: context.res.locals || {}
151
+ }),
152
+ source: {
153
+ req: context.req,
154
+ res: context.res
155
+ }
156
+ });
157
+ };
@@ -0,0 +1,33 @@
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
+ export var RouteAPI = /*#__PURE__*/function () {
5
+ function RouteAPI(entryName) {
6
+ _classCallCheck(this, RouteAPI);
7
+ _defineProperty(this, "current", void 0);
8
+ _defineProperty(this, "status", void 0);
9
+ _defineProperty(this, "url", void 0);
10
+ this.current = entryName;
11
+ this.status = 200;
12
+ this.url = '';
13
+ }
14
+ _createClass(RouteAPI, [{
15
+ key: "redirect",
16
+ value: function redirect(url) {
17
+ var status = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 302;
18
+ this.url = url;
19
+ this.status = status;
20
+ }
21
+ }, {
22
+ key: "rewrite",
23
+ value: function rewrite(entryName) {
24
+ this.current = entryName;
25
+ }
26
+ }, {
27
+ key: "use",
28
+ value: function use(entryName) {
29
+ this.rewrite(entryName);
30
+ }
31
+ }]);
32
+ return RouteAPI;
33
+ }();