@modern-js/prod-server 1.18.0 → 1.18.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @modern-js/prod-server
2
2
 
3
+ ## 1.18.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 3586707: fix: the compaire-version throw error make the supportModern error
8
+ fix: compaire-version 的抛错导致 supportModern 失败
9
+ - f6a3aa1: feat: support etag in dev server by default
10
+ feat: 开发环境默认支持 etag 功能
11
+ - 9f7bfa6: fix: fix the problem that the response header cannot be redirected when setting in ssr
12
+ fix: 修复在 SSR 中设置响应头却无法重定向的问题
13
+ - Updated dependencies [9fcfbd4]
14
+ - Updated dependencies [6c2c745]
15
+ - @modern-js/utils@1.18.1
16
+ - @modern-js/server-core@1.18.1
17
+
3
18
  ## 1.18.0
4
19
 
5
20
  ### Patch Changes
@@ -2,6 +2,9 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
2
2
 
3
3
  import { URL } from 'url';
4
4
  import qs from 'querystring';
5
+ import { Buffer } from 'buffer';
6
+ import createEtag from 'etag';
7
+ import fresh from 'fresh';
5
8
  import { headersWithoutCookie } from "../../utils";
6
9
  export class ModernServerContext {
7
10
  /**
@@ -23,7 +26,7 @@ export class ModernServerContext {
23
26
  return this.req.metrics;
24
27
  }
25
28
 
26
- constructor(req, res) {
29
+ constructor(req, res, options) {
27
30
  _defineProperty(this, "req", void 0);
28
31
 
29
32
  _defineProperty(this, "res", void 0);
@@ -32,8 +35,11 @@ export class ModernServerContext {
32
35
 
33
36
  _defineProperty(this, "serverData", void 0);
34
37
 
38
+ _defineProperty(this, "options", {});
39
+
35
40
  this.req = req;
36
41
  this.res = res;
42
+ this.options = options || {};
37
43
  this.serverData = {};
38
44
  this.bind();
39
45
  }
@@ -47,6 +53,37 @@ export class ModernServerContext {
47
53
  req.get = key => this.getReqHeader(key);
48
54
 
49
55
  res.set = (key, value) => this.res.setHeader(key, value);
56
+
57
+ res.send = body => {
58
+ this.send(body);
59
+ };
60
+ } // compat express res.send, only support etag now
61
+
62
+
63
+ send(body) {
64
+ try {
65
+ const generateETag = !this.res.getHeader('ETag') && this.options.etag;
66
+
67
+ if (body !== undefined && generateETag) {
68
+ const encoding = typeof body === 'string' ? 'utf-8' : undefined;
69
+ const buf = !Buffer.isBuffer(body) ? Buffer.from(body, encoding) : body;
70
+ const etag = createEtag(buf, {
71
+ weak: true
72
+ });
73
+
74
+ if (etag) {
75
+ this.res.setHeader('ETag', etag);
76
+ }
77
+ }
78
+
79
+ if (this.fresh) {
80
+ this.status = 304;
81
+ }
82
+ } catch (e) {
83
+ this.logger.error(e.message);
84
+ }
85
+
86
+ this.res.end(body);
50
87
  }
51
88
 
52
89
  setParams(params) {
@@ -72,6 +109,27 @@ export class ModernServerContext {
72
109
  return req.headers[field] || '';
73
110
  }
74
111
  }
112
+
113
+ get fresh() {
114
+ const {
115
+ status,
116
+ res,
117
+ method
118
+ } = this; // GET or HEAD for weak freshness validation only
119
+
120
+ if ('GET' !== method && 'HEAD' !== method) {
121
+ return false;
122
+ }
123
+
124
+ if (status >= 200 && status < 300 || 304 === status) {
125
+ return fresh(this.headers, {
126
+ etag: res.getHeader('ETag'),
127
+ 'last-modified': res.getHeader('Last-Modified')
128
+ });
129
+ }
130
+
131
+ return false;
132
+ }
75
133
  /* request property */
76
134
 
77
135
 
@@ -1,3 +1,3 @@
1
1
  import { ModernServerContext } from "./context";
2
- export const createContext = (req, res) => new ModernServerContext(req, res);
2
+ export const createContext = (req, res, options) => new ModernServerContext(req, res, options);
3
3
  export { ModernServerContext };
@@ -38,7 +38,11 @@ export const supportModern = context => {
38
38
  return false;
39
39
  }
40
40
 
41
- const result = compareVersions(browserVersion, version);
42
- return result >= 0;
41
+ try {
42
+ const result = compareVersions(browserVersion, version);
43
+ return result >= 0;
44
+ } catch (err) {
45
+ return false;
46
+ }
43
47
  };
44
48
  export const getModernEntry = filepath => filepath.replace(/\.html$/, '-es6.html');
@@ -67,7 +67,7 @@ export class Server {
67
67
 
68
68
  this.app = await this.server.createHTTPServer(this.getRequestHandler()); // runner can only be used after server init
69
69
 
70
- await this.server.onInit(this.runner);
70
+ await this.server.onInit(this.runner, this.app);
71
71
  return this;
72
72
  }
73
73
  /**
@@ -124,19 +124,12 @@ export class Server {
124
124
  }
125
125
 
126
126
  async close() {
127
- await this.server.onClose();
128
- await new Promise(resolve => this.app.close(() => {
129
- resolve();
130
- }));
127
+ this.app.close();
131
128
  }
132
129
 
133
130
  listen(options, listener) {
134
131
  const callback = () => {
135
- if (listener) {
136
- listener();
137
- }
138
-
139
- this.server.onListening(this.app);
132
+ listener === null || listener === void 0 ? void 0 : listener();
140
133
  };
141
134
 
142
135
  if (typeof options === 'object') {
@@ -7,7 +7,7 @@ class ModernSSRServer extends ModernServer {
7
7
  }
8
8
 
9
9
  filterRoutes(routes) {
10
- return routes.filter(route => route.isSSR);
10
+ return routes.filter(route => !route.isApi);
11
11
  }
12
12
 
13
13
  async setupBeforeProdMiddleware() {
@@ -20,7 +20,7 @@ import { clone } from '@modern-js/utils/lodash';
20
20
  import { RouteMatchManager } from "../libs/route";
21
21
  import { createRenderHandler } from "../libs/render";
22
22
  import { createStaticFileHandler } from "../libs/serve-file";
23
- import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExtension, noop, debug } from "../utils";
23
+ import { createErrorDocument, createMiddlewareCollecter, getStaticReg, mergeExtension, noop, debug, isRedirect } from "../utils";
24
24
  import * as reader from "../libs/render/reader";
25
25
  import { createProxyHandler } from "../libs/proxy";
26
26
  import { createContext } from "../libs/context";
@@ -103,7 +103,7 @@ export class ModernServer {
103
103
  } // server prepare
104
104
 
105
105
 
106
- async onInit(runner) {
106
+ async onInit(runner, app) {
107
107
  var _conf$bff;
108
108
 
109
109
  this.runner = runner;
@@ -123,7 +123,10 @@ export class ModernServer {
123
123
  } // start file reader
124
124
 
125
125
 
126
- this.reader.init(); // use preset routes priority
126
+ this.reader.init();
127
+ app.on('close', () => {
128
+ this.reader.close();
129
+ }); // use preset routes priority
127
130
 
128
131
  const usageRoutes = this.filterRoutes(this.getRoutes());
129
132
  this.router.reset(usageRoutes); // warmup ssr bundle in production env
@@ -146,19 +149,10 @@ export class ModernServer {
146
149
  this.addHandler(this.routeHandler.bind(this)); // compose middlewares to http handler
147
150
 
148
151
  this.compose();
149
- } // close any thing run in server
150
-
151
-
152
- async onClose() {
153
- this.reader.close();
154
152
  } // server ready
155
153
 
156
154
 
157
155
  onRepack(_) {// empty
158
- } // invoke when http server listen
159
-
160
-
161
- onListening(_) {// empty
162
156
  }
163
157
 
164
158
  onServerChange({
@@ -353,6 +347,10 @@ export class ModernServer {
353
347
  require(filepath);
354
348
  });
355
349
  }
350
+
351
+ createContext(req, res, options = {}) {
352
+ return createContext(req, res, options);
353
+ }
356
354
  /* —————————————————————— private function —————————————————————— */
357
355
  // handler route.json, include api / csr / ssr
358
356
 
@@ -441,6 +439,11 @@ export class ModernServer {
441
439
  return;
442
440
  }
443
441
 
442
+ if (res.getHeader('Location') && isRedirect(res.statusCode)) {
443
+ res.end();
444
+ return;
445
+ }
446
+
444
447
  let response = file.content;
445
448
 
446
449
  if (route.entryName) {
@@ -577,7 +580,7 @@ export class ModernServer {
577
580
  let context;
578
581
 
579
582
  try {
580
- context = createContext(req, res);
583
+ context = this.createContext(req, res);
581
584
  } catch (e) {
582
585
  this.logger.error(e);
583
586
  res.statusCode = 500;
@@ -126,4 +126,7 @@ export const headersWithoutCookie = headers => {
126
126
  }
127
127
 
128
128
  return headers;
129
+ };
130
+ export const isRedirect = code => {
131
+ return [301, 302, 307, 308].includes(code);
129
132
  };
@@ -9,6 +9,12 @@ var _url = require("url");
9
9
 
10
10
  var _querystring = _interopRequireDefault(require("querystring"));
11
11
 
12
+ var _buffer = require("buffer");
13
+
14
+ var _etag = _interopRequireDefault(require("etag"));
15
+
16
+ var _fresh = _interopRequireDefault(require("fresh"));
17
+
12
18
  var _utils = require("../../utils");
13
19
 
14
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -35,7 +41,7 @@ class ModernServerContext {
35
41
  return this.req.metrics;
36
42
  }
37
43
 
38
- constructor(req, res) {
44
+ constructor(req, res, options) {
39
45
  _defineProperty(this, "req", void 0);
40
46
 
41
47
  _defineProperty(this, "res", void 0);
@@ -44,8 +50,11 @@ class ModernServerContext {
44
50
 
45
51
  _defineProperty(this, "serverData", void 0);
46
52
 
53
+ _defineProperty(this, "options", {});
54
+
47
55
  this.req = req;
48
56
  this.res = res;
57
+ this.options = options || {};
49
58
  this.serverData = {};
50
59
  this.bind();
51
60
  }
@@ -59,6 +68,37 @@ class ModernServerContext {
59
68
  req.get = key => this.getReqHeader(key);
60
69
 
61
70
  res.set = (key, value) => this.res.setHeader(key, value);
71
+
72
+ res.send = body => {
73
+ this.send(body);
74
+ };
75
+ } // compat express res.send, only support etag now
76
+
77
+
78
+ send(body) {
79
+ try {
80
+ const generateETag = !this.res.getHeader('ETag') && this.options.etag;
81
+
82
+ if (body !== undefined && generateETag) {
83
+ const encoding = typeof body === 'string' ? 'utf-8' : undefined;
84
+ const buf = !_buffer.Buffer.isBuffer(body) ? _buffer.Buffer.from(body, encoding) : body;
85
+ const etag = (0, _etag.default)(buf, {
86
+ weak: true
87
+ });
88
+
89
+ if (etag) {
90
+ this.res.setHeader('ETag', etag);
91
+ }
92
+ }
93
+
94
+ if (this.fresh) {
95
+ this.status = 304;
96
+ }
97
+ } catch (e) {
98
+ this.logger.error(e.message);
99
+ }
100
+
101
+ this.res.end(body);
62
102
  }
63
103
 
64
104
  setParams(params) {
@@ -84,6 +124,27 @@ class ModernServerContext {
84
124
  return req.headers[field] || '';
85
125
  }
86
126
  }
127
+
128
+ get fresh() {
129
+ const {
130
+ status,
131
+ res,
132
+ method
133
+ } = this; // GET or HEAD for weak freshness validation only
134
+
135
+ if ('GET' !== method && 'HEAD' !== method) {
136
+ return false;
137
+ }
138
+
139
+ if (status >= 200 && status < 300 || 304 === status) {
140
+ return (0, _fresh.default)(this.headers, {
141
+ etag: res.getHeader('ETag'),
142
+ 'last-modified': res.getHeader('Last-Modified')
143
+ });
144
+ }
145
+
146
+ return false;
147
+ }
87
148
  /* request property */
88
149
 
89
150
 
@@ -13,6 +13,6 @@ exports.createContext = void 0;
13
13
 
14
14
  var _context = require("./context");
15
15
 
16
- const createContext = (req, res) => new _context.ModernServerContext(req, res);
16
+ const createContext = (req, res, options) => new _context.ModernServerContext(req, res, options);
17
17
 
18
18
  exports.createContext = createContext;
@@ -49,8 +49,12 @@ const supportModern = context => {
49
49
  return false;
50
50
  }
51
51
 
52
- const result = (0, _compareVersions.default)(browserVersion, version);
53
- return result >= 0;
52
+ try {
53
+ const result = (0, _compareVersions.default)(browserVersion, version);
54
+ return result >= 0;
55
+ } catch (err) {
56
+ return false;
57
+ }
54
58
  };
55
59
 
56
60
  exports.supportModern = supportModern;
@@ -84,7 +84,7 @@ class Server {
84
84
 
85
85
  this.app = await this.server.createHTTPServer(this.getRequestHandler()); // runner can only be used after server init
86
86
 
87
- await this.server.onInit(this.runner);
87
+ await this.server.onInit(this.runner, this.app);
88
88
  return this;
89
89
  }
90
90
  /**
@@ -145,19 +145,12 @@ class Server {
145
145
  }
146
146
 
147
147
  async close() {
148
- await this.server.onClose();
149
- await new Promise(resolve => this.app.close(() => {
150
- resolve();
151
- }));
148
+ this.app.close();
152
149
  }
153
150
 
154
151
  listen(options, listener) {
155
152
  const callback = () => {
156
- if (listener) {
157
- listener();
158
- }
159
-
160
- this.server.onListening(this.app);
153
+ listener === null || listener === void 0 ? void 0 : listener();
161
154
  };
162
155
 
163
156
  if (typeof options === 'object') {
@@ -15,7 +15,7 @@ class ModernSSRServer extends _modernServer.ModernServer {
15
15
  }
16
16
 
17
17
  filterRoutes(routes) {
18
- return routes.filter(route => route.isSSR);
18
+ return routes.filter(route => !route.isApi);
19
19
  }
20
20
 
21
21
  async setupBeforeProdMiddleware() {
@@ -132,7 +132,7 @@ class ModernServer {
132
132
  } // server prepare
133
133
 
134
134
 
135
- async onInit(runner) {
135
+ async onInit(runner, app) {
136
136
  var _conf$bff;
137
137
 
138
138
  this.runner = runner;
@@ -152,7 +152,10 @@ class ModernServer {
152
152
  } // start file reader
153
153
 
154
154
 
155
- this.reader.init(); // use preset routes priority
155
+ this.reader.init();
156
+ app.on('close', () => {
157
+ this.reader.close();
158
+ }); // use preset routes priority
156
159
 
157
160
  const usageRoutes = this.filterRoutes(this.getRoutes());
158
161
  this.router.reset(usageRoutes); // warmup ssr bundle in production env
@@ -175,19 +178,10 @@ class ModernServer {
175
178
  this.addHandler(this.routeHandler.bind(this)); // compose middlewares to http handler
176
179
 
177
180
  this.compose();
178
- } // close any thing run in server
179
-
180
-
181
- async onClose() {
182
- this.reader.close();
183
181
  } // server ready
184
182
 
185
183
 
186
184
  onRepack(_) {// empty
187
- } // invoke when http server listen
188
-
189
-
190
- onListening(_) {// empty
191
185
  }
192
186
 
193
187
  onServerChange({
@@ -390,6 +384,10 @@ class ModernServer {
390
384
  require(filepath);
391
385
  });
392
386
  }
387
+
388
+ createContext(req, res, options = {}) {
389
+ return (0, _context.createContext)(req, res, options);
390
+ }
393
391
  /* —————————————————————— private function —————————————————————— */
394
392
  // handler route.json, include api / csr / ssr
395
393
 
@@ -478,6 +476,11 @@ class ModernServer {
478
476
  return;
479
477
  }
480
478
 
479
+ if (res.getHeader('Location') && (0, _utils2.isRedirect)(res.statusCode)) {
480
+ res.end();
481
+ return;
482
+ }
483
+
481
484
  let response = file.content;
482
485
 
483
486
  if (route.entryName) {
@@ -614,7 +617,7 @@ class ModernServer {
614
617
  let context;
615
618
 
616
619
  try {
617
- context = (0, _context.createContext)(req, res);
620
+ context = this.createContext(req, res);
618
621
  } catch (e) {
619
622
  this.logger.error(e);
620
623
  res.statusCode = 500;
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useLocalPrefix = exports.toPath = exports.prepareFavicons = exports.noop = exports.mergeExtension = exports.headersWithoutCookie = exports.getStaticReg = exports.debug = exports.createMiddlewareCollecter = exports.createErrorDocument = void 0;
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
7
 
8
8
  var _pathToRegexp = require("path-to-regexp");
9
9
 
@@ -163,4 +163,10 @@ const headersWithoutCookie = headers => {
163
163
  return headers;
164
164
  };
165
165
 
166
- exports.headersWithoutCookie = headersWithoutCookie;
166
+ exports.headersWithoutCookie = headersWithoutCookie;
167
+
168
+ const isRedirect = code => {
169
+ return [301, 302, 307, 308].includes(code);
170
+ };
171
+
172
+ exports.isRedirect = isRedirect;
@@ -2,15 +2,16 @@
2
2
  /// <reference types="node" />
3
3
  /// <reference types="node" />
4
4
  /// <reference types="node" />
5
+ /// <reference types="node" />
5
6
  /// <reference types="node/http" />
6
7
  import { IncomingMessage, ServerResponse } from 'http';
7
8
  import { URL } from 'url';
8
9
  import qs from 'querystring';
9
- import type { ModernServerContext as ModernServerContextInterface, Metrics, Logger } from '@modern-js/types/server';
10
+ import type { ModernServerContext as ModernServerContextInterface } from '@modern-js/types';
10
11
  export declare type ContextOptions = {
11
- logger?: Logger;
12
- metrics?: Metrics;
12
+ etag?: boolean;
13
13
  };
14
+ declare type ResponseBody = string | Buffer;
14
15
  export declare class ModernServerContext implements ModernServerContextInterface {
15
16
  /**
16
17
  * http request
@@ -26,14 +27,17 @@ export declare class ModernServerContext implements ModernServerContextInterface
26
27
  */
27
28
 
28
29
  params: Record<string, string>;
29
- get logger(): Logger;
30
- get metrics(): Metrics;
30
+ get logger(): import("@modern-js/types").Logger;
31
+ get metrics(): import("@modern-js/types").Metrics;
31
32
  serverData: Record<string, any>;
32
- constructor(req: IncomingMessage, res: ServerResponse);
33
+ private options;
34
+ constructor(req: IncomingMessage, res: ServerResponse, options?: ContextOptions);
33
35
  private bind;
36
+ send(body: ResponseBody): void;
34
37
  setParams(params: Record<string, string>): void;
35
38
  setServerData(key: string, value: any): void;
36
39
  getReqHeader(key: string): string | string[];
40
+ get fresh(): boolean;
37
41
  get headers(): import("http").IncomingHttpHeaders;
38
42
  get method(): string;
39
43
  get url(): string;
@@ -55,4 +59,5 @@ export declare class ModernServerContext implements ModernServerContextInterface
55
59
 
56
60
  resHasHandled(): boolean;
57
61
  error(dig: string, e?: Error | string): void;
58
- }
62
+ }
63
+ export {};
@@ -1,4 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'http';
2
- import { ModernServerContext } from './context';
3
- export declare const createContext: (req: IncomingMessage, res: ServerResponse) => ModernServerContext;
4
- export { ModernServerContext };
2
+ import { ModernServerContext, ContextOptions } from './context';
3
+ export declare const createContext: (req: IncomingMessage, res: ServerResponse, options?: ContextOptions) => ModernServerContext;
4
+ export { ModernServerContext };
5
+ export type { ContextOptions };
@@ -1,5 +1,4 @@
1
- import { ProxyDetail, NextFunction, BffProxyOptions } from '@modern-js/types';
2
- import { ModernServerContext } from './context';
1
+ import { ProxyDetail, NextFunction, BffProxyOptions, ModernServerContext } from '@modern-js/types';
3
2
  export type { BffProxyOptions };
4
3
  export declare function formatProxyOptions(proxyOptions: BffProxyOptions): ProxyDetail[];
5
4
  export declare const createProxyHandler: (proxyOptions?: BffProxyOptions) => ((ctx: ModernServerContext, next: NextFunction) => Promise<void>)[] | null;
@@ -1,4 +1,4 @@
1
- import { ModernServerContext } from '../../context';
1
+ import type { ModernServerContext } from '@modern-js/types';
2
2
  import { RenderFunction } from '../type';
3
3
 
4
4
  declare const _default: (renderFn: RenderFunction, ctx: ModernServerContext) => RenderFunction;
@@ -1,6 +1,6 @@
1
+ import type { ModernServerContext } from '@modern-js/types';
1
2
  import { RenderResult, ServerHookRunner } from '../../type';
2
3
  import { ModernRoute } from '../route';
3
- import { ModernServerContext } from '../context';
4
4
  export declare const createRenderHandler: ({
5
5
  distDir,
6
6
  staticGenerate
@@ -1,3 +1,3 @@
1
- import { ModernServerContext } from '../../context';
1
+ import { ModernServerContext } from '@modern-js/types';
2
2
  export declare const supportModern: (context: ModernServerContext) => boolean;
3
3
  export declare const getModernEntry: (filepath: string) => string;
@@ -1,4 +1,4 @@
1
- import { ModernServerContext } from '../context';
1
+ import type { ModernServerContext } from '@modern-js/types';
2
2
  import { RenderResult, ServerHookRunner } from '../../type';
3
3
  export declare const render: (ctx: ModernServerContext, renderOptions: {
4
4
  distDir: string;
@@ -1,3 +1,3 @@
1
+ import type { ModernServerContext } from '@modern-js/types';
1
2
  import { RenderResult } from '../../type';
2
- import { ModernServerContext } from '../context';
3
3
  export declare function handleDirectory(ctx: ModernServerContext, entryPath: string, urlPath: string): Promise<RenderResult | null>;
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { BaseSSRServerContext } from '@modern-js/types/server';
2
+ import { BaseSSRServerContext } from '@modern-js/types';
3
3
  declare type MetaKeyMap = {
4
4
  header?: string[];
5
5
  query?: string[];
@@ -1,6 +1,6 @@
1
1
  import { NormalizedConfig } from '@modern-js/core';
2
+ import type { ModernServerContext } from '@modern-js/types';
2
3
  import { NextFunction } from '../type';
3
- import { ModernServerContext } from './context';
4
4
  declare type Rule = {
5
5
  path: string | RegExp;
6
6
  target: string;
@@ -2,12 +2,12 @@
2
2
  import { IncomingMessage, ServerResponse, Server } from 'http';
3
3
  import { Adapter, APIServerStartInput } from '@modern-js/server-core';
4
4
  import type { NormalizedConfig } from '@modern-js/core';
5
- import { ModernServerOptions, NextFunction, ServerHookRunner, Metrics, Logger, ModernServerInterface, HookNames, BuildOptions } from '../type';
5
+ import type { ModernServerContext } from '@modern-js/types';
6
+ import type { ContextOptions } from '../libs/context';
7
+ import { ModernServerOptions, NextFunction, ServerHookRunner, Metrics, Logger, ModernServerInterface, HookNames, BuildOptions, ModernServerHandler } from '../type';
6
8
  import { RouteMatchManager, ModernRouteInterface, ModernRoute } from '../libs/route';
7
9
  import { mergeExtension } from '../utils';
8
10
  import * as reader from '../libs/render/reader';
9
- import { ModernServerContext } from '../libs/context';
10
- declare type ModernServerHandler = (context: ModernServerContext, next: NextFunction) => Promise<void> | void;
11
11
  declare type ModernServerAsyncHandler = (context: ModernServerContext, next: NextFunction) => Promise<void>;
12
12
  export declare class ModernServer implements ModernServerInterface {
13
13
  pwd: string;
@@ -40,10 +40,8 @@ export declare class ModernServer implements ModernServerInterface {
40
40
  runMode,
41
41
  proxyTarget
42
42
  }: ModernServerOptions);
43
- onInit(runner: ServerHookRunner): Promise<void>;
44
- onClose(): Promise<void>;
43
+ onInit(runner: ServerHookRunner, app: Server): Promise<void>;
45
44
  onRepack(_: BuildOptions): void;
46
- onListening(_: Server): void;
47
45
  protected onServerChange({
48
46
  filepath
49
47
  }: {
@@ -70,6 +68,7 @@ export declare class ModernServer implements ModernServerInterface {
70
68
  protected handleWeb(context: ModernServerContext, route: ModernRoute): Promise<import("../type").RenderResult | null>;
71
69
  protected proxy(): Promise<any>;
72
70
  protected warmupSSRBundle(): void;
71
+ protected createContext(req: IncomingMessage, res: ServerResponse, options?: ContextOptions): import("../libs/context").ModernServerContext;
73
72
  private routeHandler;
74
73
  private injectMicroFE;
75
74
  private compose;
@@ -4,8 +4,8 @@ import { IncomingMessage, Server, ServerResponse } from 'http';
4
4
  import { serverManager } from '@modern-js/server-core';
5
5
  import type { ServerPlugin } from '@modern-js/server-core';
6
6
  import type { NormalizedConfig } from '@modern-js/core';
7
- import type { Metrics, Logger, NextFunction } from '@modern-js/types/server';
8
- import { ModernRouteInterface } from './libs/route';
7
+ import type { Metrics, Logger, NextFunction, ModernServerContext } from '@modern-js/types';
8
+ import type { ModernRouteInterface } from './libs/route';
9
9
  declare module 'http' {
10
10
  interface IncomingMessage {
11
11
  logger: Logger;
@@ -54,11 +54,10 @@ export declare type HookNames = 'beforeMatch' | 'afterMatch' | 'beforeRender' |
54
54
  export interface ModernServerInterface {
55
55
  pwd: string;
56
56
  distDir: string;
57
- onInit: (runner: ServerHookRunner) => Promise<void>;
58
- onClose: () => Promise<void>;
57
+ onInit: (runner: ServerHookRunner, app: Server) => Promise<void>;
59
58
  onRepack: (options: BuildOptions) => void;
60
- onListening: (app: Server) => void;
61
59
  getRequestHandler: () => (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
62
60
  createHTTPServer: (handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void) => Promise<Server>;
63
61
  }
64
- export declare type ServerConstructor = (options: ModernServerOptions) => ModernServerInterface;
62
+ export declare type ServerConstructor = (options: ModernServerOptions) => ModernServerInterface;
63
+ export declare type ModernServerHandler = (context: ModernServerContext, next: NextFunction) => Promise<void> | void;
@@ -22,4 +22,5 @@ export declare const toPath: (reg: string, params: Record<string, any>) => strin
22
22
  export declare const useLocalPrefix: (url: string) => boolean;
23
23
  export declare const getStaticReg: (output?: NormalizedConfig['output']) => RegExp;
24
24
  export declare const prepareFavicons: (favicon: string | undefined, faviconByEntries?: Record<string, string | undefined>) => string[];
25
- export declare const headersWithoutCookie: (headers: IncomingMessage['headers']) => import("http").IncomingHttpHeaders;
25
+ export declare const headersWithoutCookie: (headers: IncomingMessage['headers']) => import("http").IncomingHttpHeaders;
26
+ export declare const isRedirect: (code: number) => boolean;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.18.0",
14
+ "version": "1.18.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -28,10 +28,12 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@modern-js/utils": "1.18.0",
31
+ "@modern-js/utils": "1.18.1",
32
32
  "@babel/compat-data": "^7.17.10",
33
- "@modern-js/server-core": "1.18.0",
33
+ "@modern-js/server-core": "1.18.1",
34
34
  "axios": "^0.24.0",
35
+ "etag": "^1.8.1",
36
+ "fresh": "^0.5.2",
35
37
  "compare-versions": "^3.6.0",
36
38
  "cookie": "^0.4.2",
37
39
  "http-proxy-middleware": "^2.0.1",
@@ -43,15 +45,17 @@
43
45
  "ua-parser-js": "^0.7.28"
44
46
  },
45
47
  "devDependencies": {
46
- "@modern-js/types": "1.18.0",
47
- "@modern-js/core": "1.18.0",
48
- "@scripts/jest-config": "1.18.0",
49
- "@scripts/build": "1.18.0",
48
+ "@modern-js/types": "1.18.1",
49
+ "@modern-js/core": "1.18.1",
50
+ "@scripts/jest-config": "1.18.1",
51
+ "@scripts/build": "1.18.1",
50
52
  "@types/cookie": "^0.4.1",
51
53
  "@types/jest": "^27",
52
54
  "@types/lru-cache": "^5.1.1",
53
55
  "@types/merge-deep": "^3.0.0",
54
56
  "@types/node": "^14",
57
+ "@types/etag": "^1.8.1",
58
+ "@types/fresh": "^0.5.0",
55
59
  "@types/serve-static": "^1.13.10",
56
60
  "@types/ua-parser-js": "^0.7.36",
57
61
  "jest": "^27",