@modern-js/prod-server 2.33.0 → 2.34.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.
@@ -3,7 +3,7 @@ import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
3
3
  import path from "path";
4
4
  import { cutNameByHyphen, mime } from "@modern-js/utils";
5
5
  import { ERROR_DIGEST } from "../../constants";
6
- import { flushServerHeader, shouldFlushServerHeader } from "../preload";
6
+ import { shouldFlushServerHeader } from "../preload/shouldFlushServerHeader";
7
7
  import { handleDirectory } from "./static";
8
8
  import { readFile } from "./reader";
9
9
  import * as ssr from "./ssr";
@@ -12,7 +12,7 @@ export var createRenderHandler = function(param) {
12
12
  var distDir = param.distDir, staticGenerate = param.staticGenerate, conf = param.conf, forceCSR = param.forceCSR, nonce = param.nonce, ssrRender = param.ssrRender, _param_metaName = param.metaName, metaName = _param_metaName === void 0 ? "modern-js" : _param_metaName;
13
13
  return function() {
14
14
  var _render = _async_to_generator(function(param2) {
15
- var ctx, route, runner, entryPath, urlPath, entry, result, templatePath, content, useCSR, userAgent, disablePreload, ssrRenderOptions, result1, err;
15
+ var ctx, route, runner, entryPath, urlPath, entry, result, templatePath, content, useCSR, userAgent, disablePreload, flushServerHeader, ssrRenderOptions, result1, err;
16
16
  return _ts_generator(this, function(_state) {
17
17
  switch (_state.label) {
18
18
  case 0:
@@ -58,29 +58,40 @@ export var createRenderHandler = function(param) {
58
58
  if (!(route.isSSR && !useCSR))
59
59
  return [
60
60
  3,
61
- 7
61
+ 9
62
62
  ];
63
63
  _state.label = 4;
64
64
  case 4:
65
65
  _state.trys.push([
66
66
  4,
67
- 6,
67
+ 8,
68
68
  ,
69
- 7
69
+ 9
70
70
  ]);
71
71
  userAgent = ctx.getReqHeader("User-Agent");
72
72
  disablePreload = Boolean(ctx.headers["x-".concat(cutNameByHyphen(metaName), "-disable-preload")]);
73
- if (shouldFlushServerHeader(conf.server, userAgent, disablePreload)) {
74
- flushServerHeader({
75
- serverConf: conf.server,
76
- ctx: ctx,
77
- distDir: distDir,
78
- template: content.toString(),
79
- headers: {
80
- "Content-Type": mime.contentType(path.extname(templatePath))
81
- }
82
- });
83
- }
73
+ if (!shouldFlushServerHeader(conf.server, userAgent, disablePreload))
74
+ return [
75
+ 3,
76
+ 6
77
+ ];
78
+ return [
79
+ 4,
80
+ import("../preload")
81
+ ];
82
+ case 5:
83
+ flushServerHeader = _state.sent().flushServerHeader;
84
+ flushServerHeader({
85
+ serverConf: conf.server,
86
+ ctx: ctx,
87
+ distDir: distDir,
88
+ template: content.toString(),
89
+ headers: {
90
+ "Content-Type": mime.contentType(path.extname(templatePath))
91
+ }
92
+ });
93
+ _state.label = 6;
94
+ case 6:
84
95
  ssrRenderOptions = {
85
96
  distDir: distDir,
86
97
  entryName: route.entryName,
@@ -102,21 +113,21 @@ export var createRenderHandler = function(param) {
102
113
  nonce: nonce
103
114
  }, runner)
104
115
  ];
105
- case 5:
116
+ case 7:
106
117
  result1 = _state.sent();
107
118
  return [
108
119
  2,
109
120
  result1
110
121
  ];
111
- case 6:
122
+ case 8:
112
123
  err = _state.sent();
113
124
  ctx.error(ERROR_DIGEST.ERENDER, err.stack || err.message);
114
125
  ctx.res.set("x-modern-ssr-fallback", "1");
115
126
  return [
116
127
  3,
117
- 7
128
+ 9
118
129
  ];
119
- case 7:
130
+ case 9:
120
131
  return [
121
132
  2,
122
133
  {
@@ -1,28 +1,5 @@
1
1
  import { parseLinks } from "./parseLinks";
2
2
  import { transformLinks2String } from "./transformLinks2String";
3
- export function transformToRegExp(input) {
4
- if (typeof input === "string") {
5
- return new RegExp(input);
6
- }
7
- return input;
8
- }
9
- export function shouldFlushServerHeader(serverConf, userAgent, disablePreload) {
10
- const { ssr: ssrConf } = serverConf || {};
11
- if (disablePreload) {
12
- return false;
13
- }
14
- if (typeof ssrConf === "object" && ssrConf.preload) {
15
- if (typeof ssrConf.preload === "object") {
16
- const { userAgentFilter } = ssrConf.preload;
17
- if (userAgentFilter && userAgent) {
18
- return !transformToRegExp(userAgentFilter).test(userAgent);
19
- }
20
- return true;
21
- }
22
- return true;
23
- }
24
- return false;
25
- }
26
3
  export async function flushServerHeader({ serverConf, ctx, distDir, template, headers }) {
27
4
  const { ssr: ssrConf } = serverConf || {};
28
5
  if (typeof ssrConf !== "object") {
@@ -0,0 +1,23 @@
1
+ export function transformToRegExp(input) {
2
+ if (typeof input === "string") {
3
+ return new RegExp(input);
4
+ }
5
+ return input;
6
+ }
7
+ export function shouldFlushServerHeader(serverConf, userAgent, disablePreload) {
8
+ const { ssr: ssrConf } = serverConf || {};
9
+ if (disablePreload) {
10
+ return false;
11
+ }
12
+ if (typeof ssrConf === "object" && ssrConf.preload) {
13
+ if (typeof ssrConf.preload === "object") {
14
+ const { userAgentFilter } = ssrConf.preload;
15
+ if (userAgentFilter && userAgent) {
16
+ return !transformToRegExp(userAgentFilter).test(userAgent);
17
+ }
18
+ return true;
19
+ }
20
+ return true;
21
+ }
22
+ return false;
23
+ }
@@ -1,113 +1,124 @@
1
- import { transformToRegExp } from "./flushServerHeader";
1
+ import { transformToRegExp } from "./shouldFlushServerHeader";
2
2
  export function transformLinks2String(links, preload) {
3
3
  if (typeof preload === "boolean") {
4
- return dedup(links).map(({ uri, as }) => as ? `<${uri}>; rel=preload; as=${as}` : `<${uri}>; rel=preload`).join(", ");
4
+ return transformLinkToString(dedup(links));
5
5
  }
6
6
  const { include, exclude, attributes } = preload;
7
- const resolveLinks = addAttributes(dedup(removeExclude(addInclude(links, include), exclude)), attributes);
8
- return resolveLinks.join(", ");
9
- function addInclude(links2, include2) {
10
- var _include;
11
- const images = [
12
- "gif",
13
- "jpg",
14
- "jpeg",
15
- "png",
16
- "webp",
17
- "bmp",
18
- "tiff",
19
- "anpg",
20
- "ico"
21
- ];
22
- const videos = [
23
- "mp4",
24
- "webm",
25
- "ogm",
26
- "ogv",
27
- "ogg"
28
- ];
29
- const fonts = [
30
- "woff",
31
- "woff2",
32
- "eot",
33
- "ttf",
34
- "otf"
35
- ];
36
- const includes = ((_include = include2) === null || _include === void 0 ? void 0 : _include.map((item) => {
37
- if (typeof item === "string") {
38
- const type = (() => {
39
- if (item.endsWith(".js")) {
40
- return "script";
41
- }
42
- if (item.endsWith(".css")) {
43
- return "style";
44
- }
45
- if (images.some((image) => item.endsWith(`.${image}`))) {
46
- return "image";
47
- }
48
- if (videos.some((video) => item.endsWith(`.${video}`))) {
49
- return "video";
50
- }
51
- if (fonts.some((font) => item.endsWith(`.${font}`))) {
52
- return "font";
53
- }
54
- })();
55
- return {
56
- uri: item,
57
- as: type
58
- };
59
- }
7
+ const resolveLinks = transformLinkToString(addAttributes(dedup(removeExclude(addInclude(links, include), exclude)), attributes));
8
+ return resolveLinks;
9
+ }
10
+ function addInclude(links, include) {
11
+ var _include;
12
+ const images = [
13
+ "gif",
14
+ "jpg",
15
+ "jpeg",
16
+ "png",
17
+ "webp",
18
+ "bmp",
19
+ "tiff",
20
+ "anpg",
21
+ "ico"
22
+ ];
23
+ const videos = [
24
+ "mp4",
25
+ "webm",
26
+ "ogm",
27
+ "ogv",
28
+ "ogg"
29
+ ];
30
+ const fonts = [
31
+ "woff",
32
+ "woff2",
33
+ "eot",
34
+ "ttf",
35
+ "otf"
36
+ ];
37
+ const includes = ((_include = include) === null || _include === void 0 ? void 0 : _include.map((item) => {
38
+ if (typeof item === "string") {
39
+ const type = (() => {
40
+ if (item.endsWith(".js")) {
41
+ return "script";
42
+ }
43
+ if (item.endsWith(".css")) {
44
+ return "style";
45
+ }
46
+ if (images.some((image) => item.endsWith(`.${image}`))) {
47
+ return "image";
48
+ }
49
+ if (videos.some((video) => item.endsWith(`.${video}`))) {
50
+ return "video";
51
+ }
52
+ if (fonts.some((font) => item.endsWith(`.${font}`))) {
53
+ return "font";
54
+ }
55
+ })();
60
56
  return {
61
- uri: item.url,
62
- as: item.type
57
+ uri: item,
58
+ as: type
63
59
  };
64
- })) || [];
65
- return links2.concat(includes);
66
- }
67
- function removeExclude(links2, exclude2) {
68
- return exclude2 ? links2.filter(({ uri }) => !transformToRegExp(exclude2).test(uri)) : links2;
69
- }
70
- function addAttributes(links2, attributes2) {
71
- const parseAttributes = (_attributes) => {
72
- return Object.entries(_attributes || {}).reduce((results, [key, value]) => {
73
- if (typeof value === "boolean") {
74
- value && results.push(`; ${key}`);
75
- return results;
76
- }
77
- results.push(`; ${key}=${value}`);
78
- return results;
79
- }, []).join("");
60
+ }
61
+ return {
62
+ uri: item.url,
63
+ as: item.as,
64
+ rel: item.rel
80
65
  };
81
- return links2.map(({ uri, as }) => {
82
- if (as) {
83
- const attributesStr = (() => {
84
- const { style, script, image, font } = attributes2 || {};
85
- switch (as) {
86
- case "script":
87
- return parseAttributes(script);
88
- case "style":
89
- return parseAttributes(style);
90
- case "image":
91
- return parseAttributes(image);
92
- case "font":
93
- return parseAttributes(font);
94
- default:
95
- return "";
96
- }
97
- })();
98
- return `<${uri}>; rel=preload; as=${as}${attributesStr}`;
99
- }
100
- return `<${uri}>; rel=preload`;
101
- });
102
- }
103
- function dedup(links2) {
104
- const set = /* @__PURE__ */ new Set();
105
- return links2.filter(({ uri }) => {
106
- if (set.has(uri)) {
107
- return false;
66
+ })) || [];
67
+ return links.concat(includes);
68
+ }
69
+ function removeExclude(links, exclude) {
70
+ return exclude ? links.filter(({ uri }) => !transformToRegExp(exclude).test(uri)) : links;
71
+ }
72
+ function addAttributes(links, attributes) {
73
+ const parseAttributes = (_attributes) => {
74
+ return Object.entries(_attributes || {}).reduce((results, [key, value]) => {
75
+ if (typeof value === "boolean") {
76
+ value && results.push(`; ${key}`);
77
+ return results;
108
78
  }
109
- set.add(uri);
110
- return true;
111
- });
112
- }
79
+ results.push(`; ${key}=${value}`);
80
+ return results;
81
+ }, []).join("");
82
+ };
83
+ return links.map((link) => {
84
+ const { as } = link;
85
+ if (as) {
86
+ const attributesStr = (() => {
87
+ const { style, script, image, font } = attributes || {};
88
+ switch (as) {
89
+ case "script":
90
+ return parseAttributes(script);
91
+ case "style":
92
+ return parseAttributes(style);
93
+ case "image":
94
+ return parseAttributes(image);
95
+ case "font":
96
+ return parseAttributes(font);
97
+ default:
98
+ return "";
99
+ }
100
+ })();
101
+ return {
102
+ ...link,
103
+ rest: attributesStr
104
+ };
105
+ }
106
+ return link;
107
+ });
108
+ }
109
+ function dedup(links) {
110
+ const set = /* @__PURE__ */ new Set();
111
+ return links.filter(({ uri }) => {
112
+ if (set.has(uri)) {
113
+ return false;
114
+ }
115
+ set.add(uri);
116
+ return true;
117
+ });
118
+ }
119
+ function transformLinkToString(links) {
120
+ return links.map(({ uri, as, rel: originalRel, rest }) => {
121
+ const rel = originalRel || "preload";
122
+ return as ? `<${uri}>; rel=${rel}; as=${as}${rest || ""}` : `<${uri}>; rel=${rel}${rest || ""}`;
123
+ }).join(", ");
113
124
  }
@@ -1,7 +1,7 @@
1
1
  import path from "path";
2
2
  import { cutNameByHyphen, mime } from "@modern-js/utils";
3
3
  import { ERROR_DIGEST } from "../../constants";
4
- import { flushServerHeader, shouldFlushServerHeader } from "../preload";
4
+ import { shouldFlushServerHeader } from "../preload/shouldFlushServerHeader";
5
5
  import { handleDirectory } from "./static";
6
6
  import { readFile } from "./reader";
7
7
  import * as ssr from "./ssr";
@@ -27,6 +27,7 @@ export const createRenderHandler = ({ distDir, staticGenerate, conf, forceCSR, n
27
27
  const userAgent = ctx.getReqHeader("User-Agent");
28
28
  const disablePreload = Boolean(ctx.headers[`x-${cutNameByHyphen(metaName)}-disable-preload`]);
29
29
  if (shouldFlushServerHeader(conf.server, userAgent, disablePreload)) {
30
+ const { flushServerHeader } = await import("../preload");
30
31
  flushServerHeader({
31
32
  serverConf: conf.server,
32
33
  ctx,
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  /// <reference types="node" />
4
4
  /// <reference types="node/http" />
5
- /// <reference types=".dts-temp/C8PtmgKJWh8V5V1ezLBNG/src/type" />
5
+ /// <reference types=".dts-temp/ASqVe7K5k9dk3xex6qada/src/type" />
6
6
  import { IncomingMessage, ServerResponse } from 'http';
7
7
  import qs from 'querystring';
8
8
  import type { ModernServerContext as ModernServerContextInterface, Reporter as ModernServerReporter, ServerTiming as ModernServerTiming } from '@modern-js/types';
@@ -2,8 +2,6 @@
2
2
  import { OutgoingHttpHeaders } from 'http';
3
3
  import { ServerOptions } from '@modern-js/server-core';
4
4
  import { ModernServerContext } from '@modern-js/types';
5
- export declare function transformToRegExp(input: string | RegExp): RegExp;
6
- export declare function shouldFlushServerHeader(serverConf: ServerOptions['server'], userAgent?: string, disablePreload?: boolean): boolean;
7
5
  export interface FlushServerHeaderOptions {
8
6
  ctx: ModernServerContext;
9
7
  distDir: string;
@@ -1,6 +1,9 @@
1
1
  export interface Link {
2
2
  uri: string;
3
+ rel?: string;
3
4
  as?: 'script' | 'style' | 'image' | 'video' | 'font' | string;
5
+ /** rest str(like attributes) that need add to link */
6
+ rest?: string;
4
7
  }
5
8
  export interface ParseLinksOptions {
6
9
  distDir: string;
@@ -0,0 +1,3 @@
1
+ import type { ServerOptions } from '@modern-js/server-core';
2
+ export declare function transformToRegExp(input: string | RegExp): RegExp;
3
+ export declare function shouldFlushServerHeader(serverConf: ServerOptions['server'], userAgent?: string, disablePreload?: boolean): boolean;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node/http" />
3
- /// <reference types=".dts-temp/C8PtmgKJWh8V5V1ezLBNG/src/type" />
3
+ /// <reference types=".dts-temp/ASqVe7K5k9dk3xex6qada/src/type" />
4
4
  import { IncomingMessage } from 'http';
5
5
  import type { OutputNormalizedConfig, HtmlNormalizedConfig } from '@modern-js/server-core';
6
6
  export declare const debug: any;
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.33.0",
18
+ "version": "2.34.0",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -74,9 +74,9 @@
74
74
  "node-html-parser": "^6.1.5",
75
75
  "path-to-regexp": "^6.2.0",
76
76
  "serve-static": "^1.14.1",
77
- "@modern-js/plugin": "2.33.0",
78
- "@modern-js/server-core": "2.33.0",
79
- "@modern-js/utils": "2.33.0"
77
+ "@modern-js/plugin": "2.34.0",
78
+ "@modern-js/server-core": "2.34.0",
79
+ "@modern-js/utils": "2.34.0"
80
80
  },
81
81
  "devDependencies": {
82
82
  "@types/cookie": "^0.4.1",
@@ -95,10 +95,10 @@
95
95
  "portfinder": "^1.0.28",
96
96
  "typescript": "^5",
97
97
  "ws": "^8.13.0",
98
- "@modern-js/server-core": "2.33.0",
99
- "@modern-js/types": "2.33.0",
100
- "@scripts/build": "2.33.0",
101
- "@scripts/jest-config": "2.33.0"
98
+ "@modern-js/server-core": "2.34.0",
99
+ "@modern-js/types": "2.34.0",
100
+ "@scripts/build": "2.34.0",
101
+ "@scripts/jest-config": "2.34.0"
102
102
  },
103
103
  "sideEffects": false,
104
104
  "publishConfig": {