@modern-js/server 1.3.1 → 1.3.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 (52) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/package.json +9 -9
  3. package/src/constants.ts +0 -26
  4. package/src/dev-tools/babel/register.ts +0 -37
  5. package/src/dev-tools/dev-server-plugin.ts +0 -48
  6. package/src/dev-tools/https/global.d.ts +0 -3
  7. package/src/dev-tools/https/index.ts +0 -12
  8. package/src/dev-tools/launch-editor/index.ts +0 -29
  9. package/src/dev-tools/mock/getMockData.ts +0 -109
  10. package/src/dev-tools/mock/index.ts +0 -63
  11. package/src/dev-tools/socket-server.ts +0 -192
  12. package/src/dev-tools/watcher/dependency-tree.ts +0 -94
  13. package/src/dev-tools/watcher/index.ts +0 -79
  14. package/src/dev-tools/watcher/stats-cache.ts +0 -53
  15. package/src/index.ts +0 -16
  16. package/src/libs/context/context.ts +0 -176
  17. package/src/libs/context/index.ts +0 -7
  18. package/src/libs/hook-api/route.ts +0 -38
  19. package/src/libs/hook-api/template.ts +0 -53
  20. package/src/libs/metrics.ts +0 -15
  21. package/src/libs/proxy.ts +0 -85
  22. package/src/libs/render/cache/__tests__/cache.fun.test.ts +0 -94
  23. package/src/libs/render/cache/__tests__/cache.test.ts +0 -240
  24. package/src/libs/render/cache/__tests__/cacheable.ts +0 -44
  25. package/src/libs/render/cache/__tests__/error-configuration.ts +0 -34
  26. package/src/libs/render/cache/__tests__/matched-cache.ts +0 -88
  27. package/src/libs/render/cache/index.ts +0 -75
  28. package/src/libs/render/cache/page-caches/index.ts +0 -11
  29. package/src/libs/render/cache/page-caches/lru.ts +0 -38
  30. package/src/libs/render/cache/spr.ts +0 -301
  31. package/src/libs/render/cache/type.ts +0 -59
  32. package/src/libs/render/cache/util.ts +0 -97
  33. package/src/libs/render/index.ts +0 -78
  34. package/src/libs/render/modern/browser-list.ts +0 -7
  35. package/src/libs/render/modern/index.ts +0 -41
  36. package/src/libs/render/modern/module.d.ts +0 -4
  37. package/src/libs/render/reader.ts +0 -119
  38. package/src/libs/render/ssr.ts +0 -62
  39. package/src/libs/render/static.ts +0 -52
  40. package/src/libs/render/type.ts +0 -38
  41. package/src/libs/route/index.ts +0 -77
  42. package/src/libs/route/matcher.ts +0 -93
  43. package/src/libs/route/route.ts +0 -32
  44. package/src/libs/serve-file.ts +0 -34
  45. package/src/server/dev-server/dev-server-split.ts +0 -41
  46. package/src/server/dev-server/dev-server.ts +0 -300
  47. package/src/server/dev-server/index.ts +0 -2
  48. package/src/server/index.ts +0 -163
  49. package/src/server/modern-server-split.ts +0 -97
  50. package/src/server/modern-server.ts +0 -636
  51. package/src/type.ts +0 -88
  52. package/src/utils.ts +0 -79
@@ -1,62 +0,0 @@
1
- import path from 'path';
2
- import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils';
3
- import mime from 'mime-types';
4
- import { ModernServerContext } from '../context';
5
- import { RenderResult, ServerHookRunner } from '../../type';
6
- import cache from './cache';
7
- import { SSRServerContext } from './type';
8
-
9
- export const render = async (
10
- ctx: ModernServerContext,
11
- renderOptions: {
12
- distDir: string;
13
- bundle: string;
14
- template: string;
15
- entryName: string;
16
- staticGenerate: boolean;
17
- },
18
- runner: ServerHookRunner,
19
- ): Promise<RenderResult> => {
20
- const { bundle, distDir, template, entryName, staticGenerate } =
21
- renderOptions;
22
- const bundleJS = path.join(distDir, bundle);
23
-
24
- const context: SSRServerContext = {
25
- request: {
26
- params: ctx.params,
27
- pathname: ctx.path,
28
- query: ctx.query as Record<string, string>,
29
- headers: ctx.headers,
30
- cookie: ctx.headers.cookie,
31
- },
32
- redirection: {},
33
- template,
34
- entryName,
35
- distDir,
36
- staticGenerate,
37
- logger: ctx.logger,
38
- metrics: ctx.metrics,
39
- };
40
-
41
- runner.extendSSRContext(context);
42
-
43
- const serverRender = require(bundleJS)[SERVER_RENDER_FUNCTION_NAME];
44
-
45
- const html = await cache(serverRender, ctx)(context);
46
-
47
- const { url, status = 302 } = context.redirection;
48
-
49
- if (url) {
50
- return {
51
- content: url,
52
- contentType: '',
53
- statusCode: status,
54
- redirect: true,
55
- };
56
- }
57
-
58
- return {
59
- content: html,
60
- contentType: mime.contentType('html') as string,
61
- };
62
- };
@@ -1,52 +0,0 @@
1
- import path from 'path';
2
- import mime from 'mime-types';
3
- import { RenderResult } from '../../type';
4
- import { ModernServerContext } from '../context';
5
- import { readFile } from './reader';
6
-
7
- export async function handleDirectory(
8
- ctx: ModernServerContext,
9
- entryPath: string,
10
- urlPath: string,
11
- ): Promise<RenderResult | null> {
12
- const { path: pathname } = ctx;
13
- const filepath = path.join(entryPath, trimLeft(pathname, urlPath));
14
-
15
- // If can match accurately, always return the one that matches accurately
16
- let content = await readFile(filepath);
17
- let contentType = mime.contentType(path.extname(filepath) || '');
18
-
19
- // automatic addressing
20
- if (!content) {
21
- if (pathname.endsWith('/')) {
22
- content = await readFile(`${filepath}index.html`);
23
- } else if (!pathname.includes('.')) {
24
- content = await readFile(`${filepath}.html`);
25
- if (!content) {
26
- content = await readFile(`${filepath}/index.html`);
27
- }
28
- }
29
-
30
- // set content-type as html
31
- if (content) {
32
- contentType = mime.contentType('html');
33
- }
34
- }
35
-
36
- if (!content) {
37
- return null;
38
- }
39
-
40
- return {
41
- content,
42
- contentType: contentType || '',
43
- };
44
- }
45
-
46
- const trimLeft = (str: string, prefix: string): string => {
47
- if (str.startsWith(prefix)) {
48
- return str.substring(prefix.length);
49
- }
50
-
51
- return str;
52
- };
@@ -1,38 +0,0 @@
1
- import { BaseSSRServerContext } from '@modern-js/types/server';
2
-
3
- type MetaKeyMap = {
4
- header?: string[];
5
- query?: string[];
6
- };
7
- type MetaKeyMatch = {
8
- header?: MatchMap;
9
- query?: MatchMap;
10
- };
11
- type MatchMap = Record<string, Record<string, string>>;
12
-
13
- export type CacheConfig = {
14
- interval: number;
15
- staleLimit: number | boolean;
16
- level: number;
17
- includes?: MetaKeyMap | null;
18
- excludes?: MetaKeyMap | null;
19
- matches?: MetaKeyMatch | null;
20
- };
21
-
22
- export enum RenderLevel {
23
- CLIENT_RENDER,
24
- SERVER_PREFETCH,
25
- SERVER_RENDER,
26
- }
27
-
28
- export type SSRServerContext = BaseSSRServerContext & {
29
- cacheConfig?: CacheConfig;
30
- staticGenerate?: boolean;
31
- };
32
-
33
- export type ModernSSRReactComponent = React.ComponentType<any> & {
34
- init: (context: SSRServerContext) => Promise<void>;
35
- prefetch: (context: SSRServerContext) => Promise<Record<string, any>>;
36
- };
37
-
38
- export type RenderFunction = (context: SSRServerContext) => Promise<string>;
@@ -1,77 +0,0 @@
1
- import { RouteMatcher } from './matcher';
2
- import { ModernRoute, ModernRouteInterface } from './route';
3
-
4
- export class RouteMatchManager {
5
- public matchers: RouteMatcher[];
6
-
7
- private specs: ModernRouteInterface[] = [];
8
-
9
- constructor() {
10
- this.matchers = [];
11
- }
12
-
13
- // get all routes matches pathname
14
- private filter(pathname: string) {
15
- return this.matchers.reduce((matches, matcher) => {
16
- // 非网关来的,或网关匹配上之后,内部再进行一次自己的匹配
17
- if (matcher.matchUrlPath(pathname)) {
18
- matches.push(matcher);
19
- }
20
- return matches;
21
- }, [] as RouteMatcher[]);
22
- }
23
-
24
- // get best match from a set of matches
25
- private best(pathname: string, matches: RouteMatcher[]) {
26
- let best: RouteMatcher | undefined;
27
- let matchedLen = 0;
28
-
29
- for (const match of matches) {
30
- const len = match.matchLength(pathname);
31
-
32
- if (len === null) {
33
- continue;
34
- }
35
-
36
- if (len > matchedLen) {
37
- best = match;
38
- matchedLen = len;
39
- }
40
- }
41
-
42
- return best;
43
- }
44
-
45
- // reset routes matcher
46
- public reset(specs: ModernRouteInterface[]) {
47
- this.specs = specs;
48
- const matchers = specs.reduce((ms, spec) => {
49
- ms.push(new RouteMatcher(spec));
50
- return ms;
51
- }, [] as RouteMatcher[]);
52
-
53
- this.matchers = matchers;
54
- }
55
-
56
- // get best match from all matcher in manager
57
- public match(pathname: string) {
58
- const matches = this.filter(pathname);
59
- const best = this.best(pathname, matches);
60
- return best;
61
- }
62
-
63
- public matchEntry(entryname: string) {
64
- return this.matchers.find(matcher => matcher.matchEntry(entryname));
65
- }
66
-
67
- public getBundles() {
68
- const bundles = this.specs
69
- .filter(route => route.isSSR)
70
- .map(route => route.bundle);
71
-
72
- return bundles;
73
- }
74
- }
75
-
76
- export type { ModernRouteInterface, ModernRoute };
77
- export { RouteMatcher };
@@ -1,93 +0,0 @@
1
- import { removeTailSlash } from '@modern-js/utils';
2
- import {
3
- MatchFunction,
4
- MatchResult,
5
- match,
6
- pathToRegexp,
7
- } from 'path-to-regexp';
8
- import { ModernRoute, ModernRouteInterface } from './route';
9
-
10
- // eslint-disable-next-line no-useless-escape
11
- const regCharsDetector = /[^a-zA-Z\-_0-9\/\.]/;
12
- export class RouteMatcher {
13
- public spec: ModernRouteInterface;
14
-
15
- public urlPath: string = '';
16
-
17
- public urlMatcher?: MatchFunction;
18
-
19
- public urlReg?: RegExp;
20
-
21
- constructor(spec: ModernRouteInterface) {
22
- this.spec = spec;
23
- this.setupUrlPath();
24
- }
25
-
26
- // generate modern route object
27
- public generate() {
28
- return new ModernRoute(this.spec);
29
- }
30
-
31
- public parseURLParams(pathname: string) {
32
- if (!this.urlMatcher) {
33
- return {};
34
- } else {
35
- const matchResult = this.urlMatcher(pathname);
36
-
37
- return (matchResult as MatchResult<Record<string, string>>).params;
38
- }
39
- }
40
-
41
- // get match url length
42
- public matchLength(pathname: string): number | null {
43
- if (!this.urlReg) {
44
- return this.urlPath.length;
45
- } else {
46
- const result = this.urlReg.exec(pathname);
47
- return result?.[0]?.length || null;
48
- }
49
- }
50
-
51
- // if match url path
52
- public matchUrlPath(requestUrl: string): boolean {
53
- const urlWithoutSlash =
54
- requestUrl.endsWith('/') && requestUrl !== '/'
55
- ? requestUrl.slice(0, -1)
56
- : requestUrl;
57
-
58
- if (this.urlMatcher) {
59
- return Boolean(this.urlMatcher(urlWithoutSlash));
60
- } else {
61
- if (urlWithoutSlash.startsWith(this.urlPath)) {
62
- // avoid /abcd match /a
63
- if (
64
- this.urlPath !== '/' &&
65
- urlWithoutSlash.length > this.urlPath.length &&
66
- !urlWithoutSlash.startsWith(`${this.urlPath}/`)
67
- ) {
68
- return false;
69
- }
70
-
71
- return true;
72
- }
73
-
74
- return false;
75
- }
76
- }
77
-
78
- public matchEntry(entryName: string): boolean {
79
- return this.spec.entryName === entryName;
80
- }
81
-
82
- // compiler urlPath to regexp if necessary
83
- private setupUrlPath() {
84
- const { urlPath } = this.spec;
85
- this.urlPath = urlPath === '/' ? urlPath : removeTailSlash(urlPath);
86
-
87
- const useReg = regCharsDetector.test(urlPath);
88
- if (useReg) {
89
- this.urlMatcher = match(urlPath, { decode: decodeURIComponent });
90
- this.urlReg = pathToRegexp(urlPath);
91
- }
92
- }
93
- }
@@ -1,32 +0,0 @@
1
- import { ServerRoute as ModernRouteInterface } from '@modern-js/types';
2
-
3
- export type { ModernRouteInterface };
4
-
5
- export class ModernRoute implements ModernRouteInterface {
6
- public entryName: string;
7
-
8
- public urlPath: string;
9
-
10
- public entryPath: string;
11
-
12
- public bundle: string;
13
-
14
- public isApi: boolean;
15
-
16
- public isSSR: boolean;
17
-
18
- public isSPA: boolean;
19
-
20
- public enableModernMode?: boolean;
21
-
22
- constructor(routeSpec: ModernRouteInterface) {
23
- this.entryName = routeSpec.entryName || '';
24
- this.urlPath = routeSpec.urlPath;
25
- this.entryPath = routeSpec.entryPath || '';
26
- this.isSSR = routeSpec.isSSR || false;
27
- this.isSPA = routeSpec.isSPA || false;
28
- this.isApi = routeSpec.isApi || false;
29
- this.bundle = routeSpec.bundle || '';
30
- this.enableModernMode = routeSpec.enableModernMode ?? false;
31
- }
32
- }
@@ -1,34 +0,0 @@
1
- // Todo 看看是不是能 fork 一份,即使命中也返回
2
- import serve from 'serve-static';
3
- import { isString, isRegExp } from '@modern-js/utils';
4
- import { NextFunction } from '../type';
5
- import { ModernServerContext } from './context';
6
-
7
- type Rule = {
8
- path: string | RegExp;
9
- target: string;
10
- };
11
-
12
- export const createStaticFileHandler =
13
- (rules: Rule[]) =>
14
- // eslint-disable-next-line consistent-return
15
- async (context: ModernServerContext, next: NextFunction) => {
16
- const { url: requestUrl, req, res } = context;
17
-
18
- const hitRule = rules.find(item => {
19
- if (isString(item.path) && requestUrl.startsWith(item.path)) {
20
- return true;
21
- } else if (isRegExp(item.path) && item.path.test(requestUrl)) {
22
- return true;
23
- }
24
- return false;
25
- });
26
-
27
- if (hitRule) {
28
- serve(hitRule.target)(req, res, () => {
29
- next();
30
- });
31
- } else {
32
- return next();
33
- }
34
- };
@@ -1,41 +0,0 @@
1
- import type { APIServerStartInput } from '@modern-js/server-plugin';
2
- import { mergeExtension } from '../../utils';
3
- import { ModernRouteInterface } from '../../libs/route';
4
- import { ApiServerMode } from '../../constants';
5
- import { ModernDevServer } from './dev-server';
6
-
7
- export class ModernSSRDevServer extends ModernDevServer {
8
- protected prepareAPIHandler(
9
- _m: ApiServerMode,
10
- _: APIServerStartInput['config'],
11
- ) {
12
- return null as any;
13
- }
14
-
15
- protected async prepareWebHandler(
16
- extension: ReturnType<typeof mergeExtension>,
17
- ) {
18
- return super.prepareWebHandler(extension);
19
- }
20
-
21
- protected filterRoutes(routes: ModernRouteInterface[]) {
22
- return routes.filter(route => route.entryName);
23
- }
24
- }
25
-
26
- export class ModernAPIDevServer extends ModernDevServer {
27
- protected async prepareAPIHandler(
28
- mode: ApiServerMode,
29
- extension: APIServerStartInput['config'],
30
- ) {
31
- return super.prepareAPIHandler(mode, extension);
32
- }
33
-
34
- protected filterRoutes(routes: ModernRouteInterface[]) {
35
- return routes.filter(route => route.isApi);
36
- }
37
-
38
- protected async preServerInit() {
39
- // noop
40
- }
41
- }