@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,40 +1,43 @@
1
- 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; }
2
- 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; }
3
- 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; }
4
- /* eslint-disable no-param-reassign */
5
-
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
6
20
  import { headersWithoutCookie } from "../../utils";
7
- export const createMetrics = (context, metrics) => {
8
- const {
9
- entryName: entry,
10
- request
11
- } = context;
12
- const {
13
- pathname = ''
14
- } = request || {};
21
+ const createMetrics = (context, metrics) => {
22
+ const { entryName: entry, request } = context;
23
+ const { pathname = "" } = request || {};
15
24
  const emitTimer = (name, cost, tags = {}) => {
16
- metrics.emitTimer(name, cost, _objectSpread(_objectSpread({}, tags), {}, {
25
+ metrics.emitTimer(name, cost, __spreadProps(__spreadValues({}, tags), {
17
26
  pathname,
18
27
  entry
19
28
  }));
20
29
  };
21
30
  const emitCounter = (name, counter, tags = {}) => {
22
- metrics.emitCounter(name, counter, _objectSpread(_objectSpread({}, tags), {}, {
31
+ metrics.emitCounter(name, counter, __spreadProps(__spreadValues({}, tags), {
23
32
  pathname,
24
33
  entry
25
34
  }));
26
35
  };
27
- return {
28
- emitTimer,
29
- emitCounter
30
- };
36
+ return { emitTimer, emitCounter };
31
37
  };
32
- export const createLogger = (serverContext, logger) => {
38
+ const createLogger = (serverContext, logger) => {
33
39
  const request = serverContext.request || {};
34
- const {
35
- headers = {},
36
- pathname = ''
37
- } = request;
40
+ const { headers = {}, pathname = "" } = request;
38
41
  const debug = (message, ...args) => {
39
42
  logger.debug(`SSR Debug - ${message}, req.url = %s`, ...args, pathname);
40
43
  };
@@ -44,9 +47,14 @@ export const createLogger = (serverContext, logger) => {
44
47
  const error = (message, e) => {
45
48
  if (!e) {
46
49
  e = message;
47
- message = '';
50
+ message = "";
48
51
  }
49
- logger.error(`SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`, e instanceof Error ? e.stack || e.message : e, pathname, headersWithoutCookie(headers));
52
+ logger.error(
53
+ `SSR Error - ${message}, error = %s, req.url = %s, req.headers = %o`,
54
+ e instanceof Error ? e.stack || e.message : e,
55
+ pathname,
56
+ headersWithoutCookie(headers)
57
+ );
50
58
  };
51
59
  return {
52
60
  error,
@@ -54,4 +62,7 @@ export const createLogger = (serverContext, logger) => {
54
62
  debug
55
63
  };
56
64
  };
57
- /* eslint-enable no-param-reassign */
65
+ export {
66
+ createLogger,
67
+ createMetrics
68
+ };
@@ -1,66 +1,71 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
- import { fs } from '@modern-js/utils';
3
- import LRU from 'lru-cache';
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import { fs } from "@modern-js/utils";
22
+ import LRU from "lru-cache";
4
23
  const Byte = 1;
5
24
  const KB = 1024 * Byte;
6
25
  const MB = 1024 * KB;
7
- const getContentLength = cache => cache.content.length;
8
- const createCacheItem = async (filepath, mtime) => {
9
- const content = await fs.readFile(filepath);
26
+ const getContentLength = (cache) => cache.content.length;
27
+ const createCacheItem = (filepath, mtime) => __async(void 0, null, function* () {
28
+ const content = yield fs.readFile(filepath);
10
29
  return {
11
30
  content,
12
31
  mtime
13
32
  };
14
- };
15
- export class LruReader {
16
- // private timer?: NodeJS.Timeout;
17
-
33
+ });
34
+ class LruReader {
18
35
  constructor() {
19
- _defineProperty(this, "cache", void 0);
20
36
  this.cache = new LRU({
21
37
  max: 256 * MB,
22
38
  length: getContentLength,
23
- maxAge: 5 * 60 * 5000 // 60s
39
+ maxAge: 5 * 60 * 5e3
24
40
  });
25
41
  }
26
-
27
42
  init() {
28
- // this.timeTask();
29
43
  }
30
44
  close() {
31
- // if (this.timer) {
32
- // clearInterval(this.timer);
33
- // }
34
45
  }
35
- async read(filepath) {
36
- if (this.cache.has(filepath)) {
37
- const {
38
- content
39
- } = this.cache.get(filepath);
40
- return {
41
- content
42
- };
43
- }
44
- if (!fs.existsSync(filepath)) {
45
- return null;
46
- }
47
- const stat = fs.statSync(filepath);
48
- if (stat.isDirectory()) {
49
- return null;
50
- }
51
-
52
- // if file more than 20 MB
53
- if (stat.size > 20 * MB) {
54
- return null;
55
- }
56
- const item = await createCacheItem(filepath, stat.mtime);
57
- this.cache.set(filepath, item);
58
- return item;
46
+ read(filepath) {
47
+ return __async(this, null, function* () {
48
+ if (this.cache.has(filepath)) {
49
+ const { content } = this.cache.get(filepath);
50
+ return { content };
51
+ }
52
+ if (!fs.existsSync(filepath)) {
53
+ return null;
54
+ }
55
+ const stat = fs.statSync(filepath);
56
+ if (stat.isDirectory()) {
57
+ return null;
58
+ }
59
+ if (stat.size > 20 * MB) {
60
+ return null;
61
+ }
62
+ const item = yield createCacheItem(filepath, stat.mtime);
63
+ this.cache.set(filepath, item);
64
+ return item;
65
+ });
59
66
  }
60
67
  update() {
61
- const {
62
- cache
63
- } = this;
68
+ const { cache } = this;
64
69
  const files = cache.keys();
65
70
  for (const filepath of files) {
66
71
  if (!fs.existsSync(filepath)) {
@@ -69,36 +74,34 @@ export class LruReader {
69
74
  try {
70
75
  const item = cache.get(filepath);
71
76
  const stat = fs.statSync(filepath);
72
- const {
73
- mtime
74
- } = stat;
75
- // file is modify
77
+ const { mtime } = stat;
76
78
  if (item.mtime < mtime) {
77
79
  cache.del(filepath);
78
80
  }
79
81
  } catch (e) {
80
- // for safe
81
82
  cache.del(filepath);
82
83
  }
83
84
  }
84
85
  }
85
-
86
- // private timeTask() {
87
- // this.timer = setInterval(() => this.update, 5 * 60 * 1000).unref();
88
- // }
89
86
  }
90
-
91
87
  const reader = new LruReader();
92
- export const readFile = async filepath => {
93
- const file = await reader.read(filepath);
94
- return file === null || file === void 0 ? void 0 : file.content;
95
- };
96
- export const updateFile = () => {
88
+ const readFile = (filepath) => __async(void 0, null, function* () {
89
+ const file = yield reader.read(filepath);
90
+ return file == null ? void 0 : file.content;
91
+ });
92
+ const updateFile = () => {
97
93
  reader.update();
98
94
  };
99
- export const init = () => {
95
+ const init = () => {
100
96
  reader.init();
101
97
  };
102
- export const close = () => {
98
+ const close = () => {
103
99
  reader.close();
104
- };
100
+ };
101
+ export {
102
+ LruReader,
103
+ close,
104
+ init,
105
+ readFile,
106
+ updateFile
107
+ };
@@ -1,23 +1,42 @@
1
- import path from 'path';
2
- import { fs, LOADABLE_STATS_FILE, mime, ROUTE_MINIFEST_FILE, SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
3
- import cookie from 'cookie';
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import path from "path";
22
+ import {
23
+ fs,
24
+ LOADABLE_STATS_FILE,
25
+ mime,
26
+ ROUTE_MINIFEST_FILE,
27
+ SERVER_RENDER_FUNCTION_NAME
28
+ } from "@modern-js/utils";
29
+ import cookie from "cookie";
4
30
  import cache from "./cache";
5
31
  import { createLogger, createMetrics } from "./measure";
6
- export const render = async (ctx, renderOptions, runner) => {
7
- var _ctx$res;
8
- const {
9
- urlPath,
10
- bundle,
11
- distDir,
12
- template,
13
- entryName,
14
- staticGenerate
15
- } = renderOptions;
32
+ const render = (ctx, renderOptions, runner) => __async(void 0, null, function* () {
33
+ var _a;
34
+ const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
16
35
  const bundleJS = path.join(distDir, bundle);
17
36
  const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
18
- const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : '';
37
+ const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
19
38
  const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
20
- const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : undefined;
39
+ const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
21
40
  const context = {
22
41
  request: {
23
42
  baseUrl: urlPath,
@@ -26,27 +45,26 @@ export const render = async (ctx, renderOptions, runner) => {
26
45
  host: ctx.host,
27
46
  query: ctx.query,
28
47
  url: ctx.href,
29
- cookieMap: cookie.parse(ctx.headers.cookie || ''),
48
+ cookieMap: cookie.parse(ctx.headers.cookie || ""),
30
49
  headers: ctx.headers
31
50
  },
32
51
  response: {
33
52
  setHeader: (key, value) => {
34
53
  return ctx.res.setHeader(key, value);
35
54
  },
36
- status: code => {
55
+ status: (code) => {
37
56
  ctx.res.statusCode = code;
38
57
  },
39
- locals: ((_ctx$res = ctx.res) === null || _ctx$res === void 0 ? void 0 : _ctx$res.locals) || {}
58
+ locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
40
59
  },
41
60
  redirection: {},
42
61
  template,
43
62
  loadableStats,
44
63
  routeManifest,
45
- // for streaming ssr
46
64
  entryName,
47
65
  staticGenerate,
48
- logger: undefined,
49
- metrics: undefined,
66
+ logger: void 0,
67
+ metrics: void 0,
50
68
  req: ctx.req,
51
69
  res: ctx.res
52
70
  };
@@ -54,29 +72,29 @@ export const render = async (ctx, renderOptions, runner) => {
54
72
  context.metrics = createMetrics(context, ctx.metrics);
55
73
  runner.extendSSRContext(context);
56
74
  const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
57
- const content = await cache(serverRender, ctx)(context);
58
- const {
59
- url,
60
- status = 302
61
- } = context.redirection;
75
+ const content = yield cache(serverRender, ctx)(context);
76
+ const { url, status = 302 } = context.redirection;
62
77
  if (url) {
63
78
  return {
64
79
  content: url,
65
- contentType: '',
80
+ contentType: "",
66
81
  statusCode: status,
67
82
  redirect: true
68
83
  };
69
84
  }
70
- if (typeof content === 'string') {
85
+ if (typeof content === "string") {
71
86
  return {
72
87
  content,
73
- contentType: mime.contentType('html')
88
+ contentType: mime.contentType("html")
74
89
  };
75
90
  } else {
76
91
  return {
77
- content: '',
92
+ content: "",
78
93
  contentStream: content,
79
- contentType: mime.contentType('html')
94
+ contentType: mime.contentType("html")
80
95
  };
81
96
  }
82
- };
97
+ });
98
+ export {
99
+ render
100
+ };
@@ -1,43 +1,60 @@
1
- import path from 'path';
2
- import { mime } from '@modern-js/utils';
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import path from "path";
22
+ import { mime } from "@modern-js/utils";
3
23
  import { readFile } from "./reader";
4
- export async function handleDirectory(ctx, entryPath, urlPath) {
5
- const {
6
- path: pathname
7
- } = ctx;
8
- const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
9
-
10
- // If can match accurately, always return the one that matches accurately
11
- let content = await readFile(filepath);
12
- let contentType = mime.contentType(path.extname(filepath) || '');
13
-
14
- // automatic addressing
15
- if (!content) {
16
- if (pathname.endsWith('/')) {
17
- content = await readFile(`${filepath}index.html`);
18
- } else if (!pathname.includes('.')) {
19
- content = await readFile(`${filepath}.html`);
20
- if (!content) {
21
- content = await readFile(`${filepath}/index.html`);
24
+ function handleDirectory(ctx, entryPath, urlPath) {
25
+ return __async(this, null, function* () {
26
+ const { path: pathname } = ctx;
27
+ const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
28
+ let content = yield readFile(filepath);
29
+ let contentType = mime.contentType(path.extname(filepath) || "");
30
+ if (!content) {
31
+ if (pathname.endsWith("/")) {
32
+ content = yield readFile(`${filepath}index.html`);
33
+ } else if (!pathname.includes(".")) {
34
+ content = yield readFile(`${filepath}.html`);
35
+ if (!content) {
36
+ content = yield readFile(`${filepath}/index.html`);
37
+ }
38
+ }
39
+ if (content) {
40
+ contentType = mime.contentType("html");
22
41
  }
23
42
  }
24
-
25
- // set content-type as html
26
- if (content) {
27
- contentType = mime.contentType('html');
43
+ if (!content) {
44
+ return null;
28
45
  }
29
- }
30
- if (!content) {
31
- return null;
32
- }
33
- return {
34
- content,
35
- contentType: contentType || ''
36
- };
46
+ return {
47
+ content,
48
+ contentType: contentType || ""
49
+ };
50
+ });
37
51
  }
38
52
  const trimLeft = (str, prefix) => {
39
53
  if (str.startsWith(prefix)) {
40
54
  return str.substring(prefix.length);
41
55
  }
42
56
  return str;
43
- };
57
+ };
58
+ export {
59
+ handleDirectory
60
+ };
@@ -1,6 +1,9 @@
1
- export let RenderLevel;
2
- (function (RenderLevel) {
3
- RenderLevel[RenderLevel["CLIENT_RENDER"] = 0] = "CLIENT_RENDER";
4
- RenderLevel[RenderLevel["SERVER_PREFETCH"] = 1] = "SERVER_PREFETCH";
5
- RenderLevel[RenderLevel["SERVER_RENDER"] = 2] = "SERVER_RENDER";
6
- })(RenderLevel || (RenderLevel = {}));
1
+ var RenderLevel = /* @__PURE__ */ ((RenderLevel2) => {
2
+ RenderLevel2[RenderLevel2["CLIENT_RENDER"] = 0] = "CLIENT_RENDER";
3
+ RenderLevel2[RenderLevel2["SERVER_PREFETCH"] = 1] = "SERVER_PREFETCH";
4
+ RenderLevel2[RenderLevel2["SERVER_RENDER"] = 2] = "SERVER_RENDER";
5
+ return RenderLevel2;
6
+ })(RenderLevel || {});
7
+ export {
8
+ RenderLevel
9
+ };
@@ -1,13 +1,9 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
1
  import { RouteMatcher } from "./matcher";
3
- export class RouteMatchManager {
2
+ class RouteMatchManager {
4
3
  constructor() {
5
- _defineProperty(this, "matchers", void 0);
6
- _defineProperty(this, "specs", []);
4
+ this.specs = [];
7
5
  this.matchers = [];
8
6
  }
9
-
10
- // get all routes matches pathname
11
7
  filter(pathname) {
12
8
  return this.matchers.reduce((matches, matcher) => {
13
9
  if (matcher.matchUrlPath(pathname)) {
@@ -16,8 +12,6 @@ export class RouteMatchManager {
16
12
  return matches;
17
13
  }, []);
18
14
  }
19
-
20
- // get best match from a set of matches
21
15
  best(pathname, matches) {
22
16
  let best;
23
17
  let matchedLen = 0;
@@ -33,8 +27,6 @@ export class RouteMatchManager {
33
27
  }
34
28
  return best;
35
29
  }
36
-
37
- // reset routes matcher
38
30
  reset(specs) {
39
31
  this.specs = specs;
40
32
  const matchers = specs.reduce((ms, spec) => {
@@ -43,19 +35,20 @@ export class RouteMatchManager {
43
35
  }, []);
44
36
  this.matchers = matchers;
45
37
  }
46
-
47
- // get best match from all matcher in manager
48
38
  match(pathname) {
49
39
  const matches = this.filter(pathname);
50
40
  const best = this.best(pathname, matches);
51
41
  return best;
52
42
  }
53
43
  matchEntry(entryname) {
54
- return this.matchers.find(matcher => matcher.matchEntry(entryname));
44
+ return this.matchers.find((matcher) => matcher.matchEntry(entryname));
55
45
  }
56
46
  getBundles() {
57
- const bundles = this.specs.filter(route => route.isSSR).map(route => route.bundle);
47
+ const bundles = this.specs.filter((route) => route.isSSR).map((route) => route.bundle);
58
48
  return bundles;
59
49
  }
60
50
  }
61
- export { RouteMatcher };
51
+ export {
52
+ RouteMatchManager,
53
+ RouteMatcher
54
+ };
@@ -1,29 +1,21 @@
1
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
2
- import { match, pathToRegexp, compile } from 'path-to-regexp';
1
+ import {
2
+ match,
3
+ pathToRegexp,
4
+ compile
5
+ } from "path-to-regexp";
3
6
  import { ModernRoute } from "./route";
4
-
5
- // avoid import @modern-js/utils
6
- const removeTailSlash = s => s.replace(/\/+$/, '');
7
+ const removeTailSlash = (s) => s.replace(/\/+$/, "");
7
8
  const toPath = (reg, params) => {
8
- const fn = compile(reg, {
9
- encode: encodeURIComponent
10
- });
9
+ const fn = compile(reg, { encode: encodeURIComponent });
11
10
  return fn(params);
12
11
  };
13
-
14
- // eslint-disable-next-line no-useless-escape
15
12
  const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
16
- export class RouteMatcher {
13
+ class RouteMatcher {
17
14
  constructor(spec) {
18
- _defineProperty(this, "spec", void 0);
19
- _defineProperty(this, "urlPath", '');
20
- _defineProperty(this, "urlMatcher", void 0);
21
- _defineProperty(this, "urlReg", void 0);
15
+ this.urlPath = "";
22
16
  this.spec = spec;
23
17
  this.setupUrlPath();
24
18
  }
25
-
26
- // generate modern route object
27
19
  generate(url) {
28
20
  const route = new ModernRoute(this.spec);
29
21
  if (this.urlPath) {
@@ -41,30 +33,25 @@ export class RouteMatcher {
41
33
  return matchResult.params;
42
34
  }
43
35
  }
44
-
45
- // get match url length
46
36
  matchLength(pathname) {
37
+ var _a;
47
38
  if (!this.urlReg) {
48
39
  return this.urlPath.length;
49
40
  } else {
50
- var _result$;
51
41
  const result = this.urlReg.exec(pathname);
52
- return (result === null || result === void 0 ? void 0 : (_result$ = result[0]) === null || _result$ === void 0 ? void 0 : _result$.length) || null;
42
+ return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
53
43
  }
54
44
  }
55
-
56
- // if match url path
57
45
  matchUrlPath(requestUrl) {
58
- let urlWithoutSlash = requestUrl.endsWith('/') && requestUrl !== '/' ? requestUrl.slice(0, -1) : requestUrl;
59
- if (urlWithoutSlash.endsWith('.html')) {
46
+ let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
47
+ if (urlWithoutSlash.endsWith(".html")) {
60
48
  urlWithoutSlash = urlWithoutSlash.slice(0, -5);
61
49
  }
62
50
  if (this.urlMatcher) {
63
51
  return Boolean(this.urlMatcher(urlWithoutSlash));
64
52
  } else {
65
53
  if (urlWithoutSlash.startsWith(this.urlPath)) {
66
- // avoid /abcd match /a
67
- if (this.urlPath !== '/' && urlWithoutSlash.length > this.urlPath.length && !urlWithoutSlash.startsWith(`${this.urlPath}/`)) {
54
+ if (this.urlPath !== "/" && urlWithoutSlash.length > this.urlPath.length && !urlWithoutSlash.startsWith(`${this.urlPath}/`)) {
68
55
  return false;
69
56
  }
70
57
  return true;
@@ -75,13 +62,9 @@ export class RouteMatcher {
75
62
  matchEntry(entryName) {
76
63
  return this.spec.entryName === entryName;
77
64
  }
78
-
79
- // compiler urlPath to regexp if necessary
80
65
  setupUrlPath() {
81
- const {
82
- urlPath
83
- } = this.spec;
84
- this.urlPath = urlPath === '/' ? urlPath : removeTailSlash(urlPath);
66
+ const { urlPath } = this.spec;
67
+ this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);
85
68
  const useReg = regCharsDetector.test(urlPath);
86
69
  if (useReg) {
87
70
  this.urlMatcher = match(urlPath, {
@@ -93,4 +76,7 @@ export class RouteMatcher {
93
76
  });
94
77
  }
95
78
  }
96
- }
79
+ }
80
+ export {
81
+ RouteMatcher
82
+ };