@modern-js/prod-server 2.5.0 → 2.5.1-alpha.0

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 (120) hide show
  1. package/dist/cjs/server/modern-server.js +1 -0
  2. package/dist/esm/server/modern-server.js +1 -0
  3. package/dist/esm-node/server/modern-server.js +1 -0
  4. package/dist/js/modern/constants.js +35 -0
  5. package/dist/js/modern/index.js +18 -0
  6. package/dist/js/modern/libs/context/context.js +160 -0
  7. package/dist/js/modern/libs/context/index.js +6 -0
  8. package/dist/js/modern/libs/hook-api/index.js +134 -0
  9. package/dist/js/modern/libs/hook-api/route.js +20 -0
  10. package/dist/js/modern/libs/hook-api/template.js +73 -0
  11. package/dist/js/modern/libs/loadConfig.js +62 -0
  12. package/dist/js/modern/libs/logger.js +111 -0
  13. package/dist/js/modern/libs/metrics.js +11 -0
  14. package/dist/js/modern/libs/proxy.js +92 -0
  15. package/dist/js/modern/libs/render/cache/__tests__/cache.fun.test.js +114 -0
  16. package/dist/js/modern/libs/render/cache/__tests__/cache.test.js +254 -0
  17. package/dist/js/modern/libs/render/cache/__tests__/cacheable.js +47 -0
  18. package/dist/js/modern/libs/render/cache/__tests__/error-configuration.js +37 -0
  19. package/dist/js/modern/libs/render/cache/__tests__/matched-cache.js +91 -0
  20. package/dist/js/modern/libs/render/cache/index.js +115 -0
  21. package/dist/js/modern/libs/render/cache/page-caches/index.js +32 -0
  22. package/dist/js/modern/libs/render/cache/page-caches/lru.js +29 -0
  23. package/dist/js/modern/libs/render/cache/spr.js +248 -0
  24. package/dist/js/modern/libs/render/cache/type.js +0 -0
  25. package/dist/js/modern/libs/render/cache/util.js +102 -0
  26. package/dist/js/modern/libs/render/index.js +86 -0
  27. package/dist/js/modern/libs/render/measure.js +68 -0
  28. package/dist/js/modern/libs/render/reader.js +107 -0
  29. package/dist/js/modern/libs/render/ssr.js +100 -0
  30. package/dist/js/modern/libs/render/static.js +60 -0
  31. package/dist/js/modern/libs/render/type.js +9 -0
  32. package/dist/js/modern/libs/route/index.js +54 -0
  33. package/dist/js/modern/libs/route/matcher.js +87 -0
  34. package/dist/js/modern/libs/route/route.js +16 -0
  35. package/dist/js/modern/libs/serve-file.js +67 -0
  36. package/dist/js/modern/server/index.js +208 -0
  37. package/dist/js/modern/server/modern-server-split.js +74 -0
  38. package/dist/js/modern/server/modern-server.js +554 -0
  39. package/dist/js/modern/type.js +0 -0
  40. package/dist/js/modern/utils.js +136 -0
  41. package/dist/js/modern/worker-server.js +89 -0
  42. package/dist/js/node/constants.js +62 -0
  43. package/dist/js/node/index.js +44 -0
  44. package/dist/js/node/libs/context/context.js +189 -0
  45. package/dist/js/node/libs/context/index.js +30 -0
  46. package/dist/js/node/libs/hook-api/index.js +164 -0
  47. package/dist/js/node/libs/hook-api/route.js +43 -0
  48. package/dist/js/node/libs/hook-api/template.js +97 -0
  49. package/dist/js/node/libs/loadConfig.js +91 -0
  50. package/dist/js/node/libs/logger.js +133 -0
  51. package/dist/js/node/libs/metrics.js +34 -0
  52. package/dist/js/node/libs/proxy.js +114 -0
  53. package/dist/js/node/libs/render/cache/__tests__/cache.fun.test.js +115 -0
  54. package/dist/js/node/libs/render/cache/__tests__/cache.test.js +245 -0
  55. package/dist/js/node/libs/render/cache/__tests__/cacheable.js +70 -0
  56. package/dist/js/node/libs/render/cache/__tests__/error-configuration.js +60 -0
  57. package/dist/js/node/libs/render/cache/__tests__/matched-cache.js +114 -0
  58. package/dist/js/node/libs/render/cache/index.js +134 -0
  59. package/dist/js/node/libs/render/cache/page-caches/index.js +55 -0
  60. package/dist/js/node/libs/render/cache/page-caches/lru.js +58 -0
  61. package/dist/js/node/libs/render/cache/spr.js +270 -0
  62. package/dist/js/node/libs/render/cache/type.js +15 -0
  63. package/dist/js/node/libs/render/cache/util.js +138 -0
  64. package/dist/js/node/libs/render/index.js +115 -0
  65. package/dist/js/node/libs/render/measure.js +90 -0
  66. package/dist/js/node/libs/render/reader.js +140 -0
  67. package/dist/js/node/libs/render/ssr.js +123 -0
  68. package/dist/js/node/libs/render/static.js +89 -0
  69. package/dist/js/node/libs/render/type.js +32 -0
  70. package/dist/js/node/libs/route/index.js +78 -0
  71. package/dist/js/node/libs/route/matcher.js +106 -0
  72. package/dist/js/node/libs/route/route.js +39 -0
  73. package/dist/js/node/libs/serve-file.js +97 -0
  74. package/dist/js/node/server/index.js +219 -0
  75. package/dist/js/node/server/modern-server-split.js +97 -0
  76. package/dist/js/node/server/modern-server.js +559 -0
  77. package/dist/js/node/type.js +15 -0
  78. package/dist/js/node/utils.js +166 -0
  79. package/dist/js/node/worker-server.js +113 -0
  80. package/dist/js/treeshaking/constants.js +29 -0
  81. package/dist/js/treeshaking/index.js +13 -0
  82. package/dist/js/treeshaking/libs/context/context.js +274 -0
  83. package/dist/js/treeshaking/libs/context/index.js +5 -0
  84. package/dist/js/treeshaking/libs/hook-api/index.js +281 -0
  85. package/dist/js/treeshaking/libs/hook-api/route.js +68 -0
  86. package/dist/js/treeshaking/libs/hook-api/template.js +127 -0
  87. package/dist/js/treeshaking/libs/loadConfig.js +82 -0
  88. package/dist/js/treeshaking/libs/logger.js +205 -0
  89. package/dist/js/treeshaking/libs/metrics.js +6 -0
  90. package/dist/js/treeshaking/libs/proxy.js +244 -0
  91. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.fun.test.js +291 -0
  92. package/dist/js/treeshaking/libs/render/cache/__tests__/cache.test.js +781 -0
  93. package/dist/js/treeshaking/libs/render/cache/__tests__/cacheable.js +67 -0
  94. package/dist/js/treeshaking/libs/render/cache/__tests__/error-configuration.js +45 -0
  95. package/dist/js/treeshaking/libs/render/cache/__tests__/matched-cache.js +147 -0
  96. package/dist/js/treeshaking/libs/render/cache/index.js +346 -0
  97. package/dist/js/treeshaking/libs/render/cache/page-caches/index.js +154 -0
  98. package/dist/js/treeshaking/libs/render/cache/page-caches/lru.js +84 -0
  99. package/dist/js/treeshaking/libs/render/cache/spr.js +492 -0
  100. package/dist/js/treeshaking/libs/render/cache/type.js +1 -0
  101. package/dist/js/treeshaking/libs/render/cache/util.js +280 -0
  102. package/dist/js/treeshaking/libs/render/index.js +234 -0
  103. package/dist/js/treeshaking/libs/render/measure.js +146 -0
  104. package/dist/js/treeshaking/libs/render/reader.js +339 -0
  105. package/dist/js/treeshaking/libs/render/ssr.js +223 -0
  106. package/dist/js/treeshaking/libs/render/static.js +216 -0
  107. package/dist/js/treeshaking/libs/render/type.js +7 -0
  108. package/dist/js/treeshaking/libs/route/index.js +130 -0
  109. package/dist/js/treeshaking/libs/route/matcher.js +143 -0
  110. package/dist/js/treeshaking/libs/route/route.js +40 -0
  111. package/dist/js/treeshaking/libs/serve-file.js +184 -0
  112. package/dist/js/treeshaking/server/index.js +505 -0
  113. package/dist/js/treeshaking/server/modern-server-split.js +360 -0
  114. package/dist/js/treeshaking/server/modern-server.js +1089 -0
  115. package/dist/js/treeshaking/type.js +1 -0
  116. package/dist/js/treeshaking/utils.js +147 -0
  117. package/dist/js/treeshaking/worker-server.js +233 -0
  118. package/dist/types/libs/context/context.d.ts +1 -1
  119. package/dist/types/utils.d.ts +1 -1
  120. package/package.json +9 -9
@@ -0,0 +1,107 @@
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";
23
+ const Byte = 1;
24
+ const KB = 1024 * Byte;
25
+ const MB = 1024 * KB;
26
+ const getContentLength = (cache) => cache.content.length;
27
+ const createCacheItem = (filepath, mtime) => __async(void 0, null, function* () {
28
+ const content = yield fs.readFile(filepath);
29
+ return {
30
+ content,
31
+ mtime
32
+ };
33
+ });
34
+ class LruReader {
35
+ constructor() {
36
+ this.cache = new LRU({
37
+ max: 256 * MB,
38
+ length: getContentLength,
39
+ maxAge: 5 * 60 * 5e3
40
+ });
41
+ }
42
+ init() {
43
+ }
44
+ close() {
45
+ }
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
+ });
66
+ }
67
+ update() {
68
+ const { cache } = this;
69
+ const files = cache.keys();
70
+ for (const filepath of files) {
71
+ if (!fs.existsSync(filepath)) {
72
+ cache.del(filepath);
73
+ }
74
+ try {
75
+ const item = cache.get(filepath);
76
+ const stat = fs.statSync(filepath);
77
+ const { mtime } = stat;
78
+ if (item.mtime < mtime) {
79
+ cache.del(filepath);
80
+ }
81
+ } catch (e) {
82
+ cache.del(filepath);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ const reader = new LruReader();
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 = () => {
93
+ reader.update();
94
+ };
95
+ const init = () => {
96
+ reader.init();
97
+ };
98
+ const close = () => {
99
+ reader.close();
100
+ };
101
+ export {
102
+ LruReader,
103
+ close,
104
+ init,
105
+ readFile,
106
+ updateFile
107
+ };
@@ -0,0 +1,100 @@
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";
30
+ import cache from "./cache";
31
+ import { createLogger, createMetrics } from "./measure";
32
+ const render = (ctx, renderOptions, runner) => __async(void 0, null, function* () {
33
+ var _a;
34
+ const { urlPath, bundle, distDir, template, entryName, staticGenerate } = renderOptions;
35
+ const bundleJS = path.join(distDir, bundle);
36
+ const loadableUri = path.join(distDir, LOADABLE_STATS_FILE);
37
+ const loadableStats = fs.existsSync(loadableUri) ? require(loadableUri) : "";
38
+ const routesManifestUri = path.join(distDir, ROUTE_MINIFEST_FILE);
39
+ const routeManifest = fs.existsSync(routesManifestUri) ? require(routesManifestUri) : void 0;
40
+ const context = {
41
+ request: {
42
+ baseUrl: urlPath,
43
+ params: ctx.params,
44
+ pathname: ctx.path,
45
+ host: ctx.host,
46
+ query: ctx.query,
47
+ url: ctx.href,
48
+ cookieMap: cookie.parse(ctx.headers.cookie || ""),
49
+ headers: ctx.headers
50
+ },
51
+ response: {
52
+ setHeader: (key, value) => {
53
+ return ctx.res.setHeader(key, value);
54
+ },
55
+ status: (code) => {
56
+ ctx.res.statusCode = code;
57
+ },
58
+ locals: ((_a = ctx.res) == null ? void 0 : _a.locals) || {}
59
+ },
60
+ redirection: {},
61
+ template,
62
+ loadableStats,
63
+ routeManifest,
64
+ entryName,
65
+ staticGenerate,
66
+ logger: void 0,
67
+ metrics: void 0,
68
+ req: ctx.req,
69
+ res: ctx.res
70
+ };
71
+ context.logger = createLogger(context, ctx.logger);
72
+ context.metrics = createMetrics(context, ctx.metrics);
73
+ runner.extendSSRContext(context);
74
+ const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
75
+ const content = yield cache(serverRender, ctx)(context);
76
+ const { url, status = 302 } = context.redirection;
77
+ if (url) {
78
+ return {
79
+ content: url,
80
+ contentType: "",
81
+ statusCode: status,
82
+ redirect: true
83
+ };
84
+ }
85
+ if (typeof content === "string") {
86
+ return {
87
+ content,
88
+ contentType: mime.contentType("html")
89
+ };
90
+ } else {
91
+ return {
92
+ content: "",
93
+ contentStream: content,
94
+ contentType: mime.contentType("html")
95
+ };
96
+ }
97
+ });
98
+ export {
99
+ render
100
+ };
@@ -0,0 +1,60 @@
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";
23
+ import { readFile } from "./reader";
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");
41
+ }
42
+ }
43
+ if (!content) {
44
+ return null;
45
+ }
46
+ return {
47
+ content,
48
+ contentType: contentType || ""
49
+ };
50
+ });
51
+ }
52
+ const trimLeft = (str, prefix) => {
53
+ if (str.startsWith(prefix)) {
54
+ return str.substring(prefix.length);
55
+ }
56
+ return str;
57
+ };
58
+ export {
59
+ handleDirectory
60
+ };
@@ -0,0 +1,9 @@
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
+ };
@@ -0,0 +1,54 @@
1
+ import { RouteMatcher } from "./matcher";
2
+ class RouteMatchManager {
3
+ constructor() {
4
+ this.specs = [];
5
+ this.matchers = [];
6
+ }
7
+ filter(pathname) {
8
+ return this.matchers.reduce((matches, matcher) => {
9
+ if (matcher.matchUrlPath(pathname)) {
10
+ matches.push(matcher);
11
+ }
12
+ return matches;
13
+ }, []);
14
+ }
15
+ best(pathname, matches) {
16
+ let best;
17
+ let matchedLen = 0;
18
+ for (const match of matches) {
19
+ const len = match.matchLength(pathname);
20
+ if (len === null) {
21
+ continue;
22
+ }
23
+ if (len > matchedLen) {
24
+ best = match;
25
+ matchedLen = len;
26
+ }
27
+ }
28
+ return best;
29
+ }
30
+ reset(specs) {
31
+ this.specs = specs;
32
+ const matchers = specs.reduce((ms, spec) => {
33
+ ms.push(new RouteMatcher(spec));
34
+ return ms;
35
+ }, []);
36
+ this.matchers = matchers;
37
+ }
38
+ match(pathname) {
39
+ const matches = this.filter(pathname);
40
+ const best = this.best(pathname, matches);
41
+ return best;
42
+ }
43
+ matchEntry(entryname) {
44
+ return this.matchers.find((matcher) => matcher.matchEntry(entryname));
45
+ }
46
+ getBundles() {
47
+ const bundles = this.specs.filter((route) => route.isSSR).map((route) => route.bundle);
48
+ return bundles;
49
+ }
50
+ }
51
+ export {
52
+ RouteMatchManager,
53
+ RouteMatcher
54
+ };
@@ -0,0 +1,87 @@
1
+ import {
2
+ match,
3
+ pathToRegexp,
4
+ compile
5
+ } from "path-to-regexp";
6
+ import { ModernRoute } from "./route";
7
+ const removeTailSlash = (s) => s.replace(/\/+$/, "");
8
+ const removeHtmlSuffix = (url) => {
9
+ if (url.endsWith(".html")) {
10
+ return url.slice(0, -5);
11
+ }
12
+ return url;
13
+ };
14
+ const toPath = (reg, params) => {
15
+ const fn = compile(reg, { encode: encodeURIComponent });
16
+ return fn(params);
17
+ };
18
+ const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
19
+ class RouteMatcher {
20
+ constructor(spec) {
21
+ this.urlPath = "";
22
+ this.spec = spec;
23
+ this.setupUrlPath();
24
+ }
25
+ generate(url) {
26
+ const route = new ModernRoute(this.spec);
27
+ if (this.urlPath) {
28
+ const params = this.parseURLParams(url);
29
+ route.urlPath = toPath(route.urlPath, params);
30
+ route.params = params;
31
+ }
32
+ return route;
33
+ }
34
+ parseURLParams(pathname) {
35
+ if (!this.urlMatcher) {
36
+ return {};
37
+ } else {
38
+ const matchResult = this.urlMatcher(pathname);
39
+ return matchResult.params;
40
+ }
41
+ }
42
+ matchLength(pathname) {
43
+ var _a;
44
+ if (!this.urlReg) {
45
+ return this.urlPath.length;
46
+ } else {
47
+ const result = this.urlReg.exec(pathname);
48
+ return ((_a = result == null ? void 0 : result[0]) == null ? void 0 : _a.length) || null;
49
+ }
50
+ }
51
+ matchUrlPath(requestUrl) {
52
+ let urlWithoutSlash = requestUrl.endsWith("/") && requestUrl !== "/" ? requestUrl.slice(0, -1) : requestUrl;
53
+ urlWithoutSlash = removeHtmlSuffix(urlWithoutSlash);
54
+ if (this.urlMatcher) {
55
+ return Boolean(this.urlMatcher(urlWithoutSlash));
56
+ } else {
57
+ const urlPath = removeHtmlSuffix(this.urlPath);
58
+ if (urlWithoutSlash.startsWith(urlPath)) {
59
+ if (urlPath !== "/" && urlWithoutSlash.length > urlPath.length && !urlWithoutSlash.startsWith(`${urlPath}/`)) {
60
+ return false;
61
+ }
62
+ return true;
63
+ }
64
+ return false;
65
+ }
66
+ }
67
+ matchEntry(entryName) {
68
+ return this.spec.entryName === entryName;
69
+ }
70
+ setupUrlPath() {
71
+ const { urlPath } = this.spec;
72
+ this.urlPath = urlPath === "/" ? urlPath : removeTailSlash(urlPath);
73
+ const useReg = regCharsDetector.test(urlPath);
74
+ if (useReg) {
75
+ this.urlMatcher = match(urlPath, {
76
+ end: false,
77
+ decode: decodeURIComponent
78
+ });
79
+ this.urlReg = pathToRegexp(urlPath, [], {
80
+ end: false
81
+ });
82
+ }
83
+ }
84
+ }
85
+ export {
86
+ RouteMatcher
87
+ };
@@ -0,0 +1,16 @@
1
+ class ModernRoute {
2
+ constructor(routeSpec) {
3
+ this.params = {};
4
+ this.entryName = routeSpec.entryName || "";
5
+ this.urlPath = routeSpec.urlPath;
6
+ this.entryPath = routeSpec.entryPath || "";
7
+ this.isSSR = routeSpec.isSSR || false;
8
+ this.isSPA = routeSpec.isSPA || false;
9
+ this.isApi = routeSpec.isApi || false;
10
+ this.bundle = routeSpec.bundle || "";
11
+ this.responseHeaders = routeSpec.responseHeaders;
12
+ }
13
+ }
14
+ export {
15
+ ModernRoute
16
+ };
@@ -0,0 +1,67 @@
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 serve from "serve-static";
22
+ import { isString, isRegExp } from "@modern-js/utils";
23
+ import { useLocalPrefix } from "../utils";
24
+ const removedPrefix = (req, prefix) => {
25
+ if (useLocalPrefix(prefix)) {
26
+ req.url = req.url.slice(prefix.length);
27
+ return () => {
28
+ req.url = prefix + req.url;
29
+ };
30
+ } else {
31
+ return () => {
32
+ };
33
+ }
34
+ };
35
+ const faviconFallbackHandler = (context, next) => {
36
+ if (context.url === "/favicon.ico") {
37
+ context.res.statusCode = 204;
38
+ context.res.end();
39
+ } else {
40
+ next();
41
+ }
42
+ };
43
+ const createStaticFileHandler = (rules, output = {}) => (context, next) => __async(void 0, null, function* () {
44
+ const { url: requestUrl, req, res } = context;
45
+ const { assetPrefix = "/" } = output;
46
+ const hitRule = rules.find((item) => {
47
+ if (isString(item.path) && requestUrl.startsWith(item.path)) {
48
+ return true;
49
+ } else if (isRegExp(item.path) && item.path.test(requestUrl)) {
50
+ return true;
51
+ }
52
+ return false;
53
+ });
54
+ if (hitRule) {
55
+ const resume = removedPrefix(req, assetPrefix);
56
+ serve(hitRule.target)(req, res, () => {
57
+ resume();
58
+ next();
59
+ });
60
+ } else {
61
+ return next();
62
+ }
63
+ });
64
+ export {
65
+ createStaticFileHandler,
66
+ faviconFallbackHandler
67
+ };
@@ -0,0 +1,208 @@
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));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+ import path from "path";
41
+ import fs from "fs";
42
+ import {
43
+ Logger,
44
+ SHARED_DIR,
45
+ OUTPUT_CONFIG_FILE,
46
+ dotenv,
47
+ dotenvExpand,
48
+ INTERNAL_SERVER_PLUGINS
49
+ } from "@modern-js/utils";
50
+ import {
51
+ serverManager,
52
+ AppContext,
53
+ ConfigContext,
54
+ loadPlugins
55
+ } from "@modern-js/server-core";
56
+ import { metrics as defaultMetrics } from "../libs/metrics";
57
+ import {
58
+ loadConfig,
59
+ getServerConfigPath,
60
+ requireConfig
61
+ } from "../libs/loadConfig";
62
+ import { debug } from "../utils";
63
+ import { createProdServer } from "./modern-server-split";
64
+ class Server {
65
+ constructor(options) {
66
+ this.serverImpl = createProdServer;
67
+ options.logger = options.logger || new Logger({
68
+ level: "warn"
69
+ });
70
+ options.metrics = options.metrics || defaultMetrics;
71
+ this.options = options;
72
+ this.serverConfig = {};
73
+ }
74
+ init() {
75
+ return __async(this, null, function* () {
76
+ const { options } = this;
77
+ this.loadServerEnv(options);
78
+ this.initServerConfig(options);
79
+ yield this.injectContext(this.runner, options);
80
+ this.runner = yield this.createHookRunner();
81
+ yield this.initConfig(this.runner, options);
82
+ yield this.injectContext(this.runner, options);
83
+ this.server = this.serverImpl(options);
84
+ yield this.runPrepareHook(this.runner);
85
+ this.app = yield this.server.createHTTPServer(this.getRequestHandler());
86
+ yield this.server.onInit(this.runner, this.app);
87
+ return this;
88
+ });
89
+ }
90
+ runConfigHook(runner, serverConfig) {
91
+ const newServerConfig = runner.config(serverConfig || {});
92
+ return newServerConfig;
93
+ }
94
+ runPrepareHook(runner) {
95
+ return __async(this, null, function* () {
96
+ runner.prepare();
97
+ });
98
+ }
99
+ initServerConfig(options) {
100
+ const { pwd, serverConfigFile } = options;
101
+ const distDirectory = path.join(pwd, options.config.output.path || "dist");
102
+ const serverConfigPath = getServerConfigPath(
103
+ distDirectory,
104
+ serverConfigFile
105
+ );
106
+ const serverConfig = requireConfig(serverConfigPath);
107
+ this.serverConfig = serverConfig;
108
+ }
109
+ initConfig(runner, options) {
110
+ return __async(this, null, function* () {
111
+ const { pwd, config } = options;
112
+ const { serverConfig } = this;
113
+ const finalServerConfig = this.runConfigHook(runner, serverConfig);
114
+ const resolvedConfigPath = path.join(
115
+ pwd,
116
+ config.output.path || "dist",
117
+ OUTPUT_CONFIG_FILE
118
+ );
119
+ options.config = loadConfig({
120
+ cliConfig: config,
121
+ serverConfig: finalServerConfig,
122
+ resolvedConfigPath
123
+ });
124
+ });
125
+ }
126
+ close() {
127
+ return __async(this, null, function* () {
128
+ this.app.close();
129
+ });
130
+ }
131
+ listen(options, listener) {
132
+ const callback = () => {
133
+ listener == null ? void 0 : listener();
134
+ };
135
+ if (typeof options === "object") {
136
+ this.app.listen(options, callback);
137
+ } else {
138
+ this.app.listen(process.env.PORT || options || 8080, callback);
139
+ }
140
+ }
141
+ getRequestHandler() {
142
+ return (req, res, next) => {
143
+ const requestHandler = this.server.getRequestHandler();
144
+ return requestHandler(req, res, next);
145
+ };
146
+ }
147
+ render(req, res, url) {
148
+ return __async(this, null, function* () {
149
+ return this.server.render(req, res, url);
150
+ });
151
+ }
152
+ createHookRunner() {
153
+ return __async(this, null, function* () {
154
+ serverManager.clear();
155
+ const { options } = this;
156
+ const {
157
+ internalPlugins = INTERNAL_SERVER_PLUGINS,
158
+ pwd,
159
+ plugins = []
160
+ } = options;
161
+ const serverPlugins = this.serverConfig.plugins || [];
162
+ const loadedPlugins = loadPlugins(pwd, [...serverPlugins, ...plugins], {
163
+ internalPlugins
164
+ });
165
+ debug("plugins", loadedPlugins);
166
+ loadedPlugins.forEach((p) => {
167
+ serverManager.usePlugin(p);
168
+ });
169
+ const hooksRunner = yield serverManager.init();
170
+ return hooksRunner;
171
+ });
172
+ }
173
+ injectContext(runner, options) {
174
+ return __async(this, null, function* () {
175
+ const appContext = this.initAppContext();
176
+ const { config, pwd } = options;
177
+ ConfigContext.set(config);
178
+ AppContext.set(__spreadProps(__spreadValues({}, appContext), {
179
+ distDirectory: path.join(pwd, config.output.path || "dist")
180
+ }));
181
+ });
182
+ }
183
+ initAppContext() {
184
+ const { options } = this;
185
+ const { pwd: appDirectory, plugins = [], config } = options;
186
+ const serverPlugins = plugins.map((p) => ({
187
+ server: p
188
+ }));
189
+ return {
190
+ appDirectory,
191
+ distDirectory: path.join(appDirectory, config.output.path || "dist"),
192
+ sharedDirectory: path.resolve(appDirectory, SHARED_DIR),
193
+ plugins: serverPlugins
194
+ };
195
+ }
196
+ loadServerEnv(options) {
197
+ const { pwd: appDirectory } = options;
198
+ const serverEnv = process.env.MODERN_ENV;
199
+ const serverEnvPath = path.resolve(appDirectory, `.env.${serverEnv}`);
200
+ if (serverEnv && fs.existsSync(serverEnvPath) && !fs.statSync(serverEnvPath).isDirectory()) {
201
+ const envConfig = dotenv.config({ path: serverEnvPath });
202
+ dotenvExpand(envConfig);
203
+ }
204
+ }
205
+ }
206
+ export {
207
+ Server
208
+ };