@modern-js/prod-server 2.9.0 → 2.10.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.
@@ -27,7 +27,6 @@ import {
27
27
  import * as reader from "../libs/render/reader";
28
28
  import { createProxyHandler } from "../libs/proxy";
29
29
  import { createContext } from "../libs/context";
30
- import { templateInjectableStream } from "../libs/hook-api/template";
31
30
  import {
32
31
  AGGRED_DIR,
33
32
  ERROR_DIGEST,
@@ -53,7 +52,7 @@ class ModernServer {
53
52
  }) {
54
53
  this.handlers = [];
55
54
  this.reader = reader;
56
- this.beforeRouteHandler = null;
55
+ this.loaderHandler = null;
57
56
  this.frameWebHandler = null;
58
57
  this.frameAPIHandler = null;
59
58
  this.proxyHandler = null;
@@ -91,7 +90,7 @@ class ModernServer {
91
90
  this.router.reset(usageRoutes);
92
91
  this.warmupSSRBundle();
93
92
  await this.prepareFrameHandler();
94
- await this.prepareBeforeRouteHandler(usageRoutes, distDir);
93
+ await this.prepareLoaderHandler(usageRoutes, distDir);
95
94
  const ssrConfig = (_b = this.conf.server) == null ? void 0 : _b.ssr;
96
95
  const forceCSR = typeof ssrConfig === "object" ? ssrConfig.forceCSR : false;
97
96
  this.routeRenderHandler = createRenderHandler({
@@ -102,24 +101,12 @@ class ModernServer {
102
101
  await this.setupBeforeProdMiddleware();
103
102
  this.addHandler(this.setupStaticMiddleware((_c = this.conf.output) == null ? void 0 : _c.assetPrefix));
104
103
  this.addHandler(faviconFallbackHandler);
105
- this.addBeforeRouteHandler();
106
104
  this.addHandler(this.routeHandler.bind(this));
107
105
  this.compose();
108
106
  }
109
107
  // server ready
110
108
  onRepack(_) {
111
109
  }
112
- addBeforeRouteHandler() {
113
- this.addHandler(async (context, next) => {
114
- if (this.beforeRouteHandler) {
115
- await this.beforeRouteHandler(context);
116
- if (this.isSend(context.res)) {
117
- return;
118
- }
119
- }
120
- return next();
121
- });
122
- }
123
110
  onServerChange({ filepath }) {
124
111
  const { pwd } = this;
125
112
  const { api, server } = AGGRED_DIR;
@@ -146,6 +133,9 @@ class ModernServer {
146
133
  if (!result) {
147
134
  return null;
148
135
  }
136
+ if (result.contentStream) {
137
+ return result.contentStream;
138
+ }
149
139
  return result.content.toString();
150
140
  }
151
141
  async createHTTPServer(handler) {
@@ -174,9 +164,9 @@ class ModernServer {
174
164
  context.error(ERROR_DIGEST.ENOTF, "404 Not Found");
175
165
  this.renderErrorPage(context, 404);
176
166
  }
177
- async prepareBeforeRouteHandler(specs, distDir) {
167
+ async prepareLoaderHandler(specs, distDir) {
178
168
  const { runner } = this;
179
- const handler = await runner.preparebeforeRouteHandler(
169
+ const handler = await runner.prepareLoaderHandler(
180
170
  {
181
171
  serverRoutes: specs,
182
172
  distDir
@@ -185,7 +175,7 @@ class ModernServer {
185
175
  onLast: () => null
186
176
  }
187
177
  );
188
- this.beforeRouteHandler = handler;
178
+ this.loaderHandler = handler;
189
179
  }
190
180
  // gather frame extension and get framework handler
191
181
  async prepareFrameHandler(options) {
@@ -271,11 +261,48 @@ class ModernServer {
271
261
  await this.frameAPIHandler(req, res);
272
262
  }
273
263
  async handleWeb(context, route) {
274
- return this.routeRenderHandler({
264
+ const { res } = context;
265
+ if (this.loaderHandler) {
266
+ await this.loaderHandler(context);
267
+ if (this.isSend(res)) {
268
+ return null;
269
+ }
270
+ }
271
+ context.setParams(route.params);
272
+ context.setServerData("router", {
273
+ baseUrl: route.urlPath,
274
+ params: route.params
275
+ });
276
+ if (route.responseHeaders) {
277
+ Object.keys(route.responseHeaders).forEach((key) => {
278
+ const value = route.responseHeaders[key];
279
+ if (value) {
280
+ context.res.setHeader(key, value);
281
+ }
282
+ });
283
+ }
284
+ const renderResult = await this.routeRenderHandler({
275
285
  ctx: context,
276
286
  route,
277
287
  runner: this.runner
278
288
  });
289
+ if (!renderResult) {
290
+ this.render404(context);
291
+ return null;
292
+ }
293
+ if (renderResult.redirect) {
294
+ this.redirect(
295
+ res,
296
+ renderResult.content,
297
+ renderResult.statusCode
298
+ );
299
+ return null;
300
+ }
301
+ if (this.isSend(res)) {
302
+ return null;
303
+ }
304
+ res.setHeader("content-type", renderResult.contentType);
305
+ return renderResult;
279
306
  }
280
307
  async proxy() {
281
308
  return null;
@@ -308,31 +335,31 @@ class ModernServer {
308
335
  await this.handleAPI(context);
309
336
  return;
310
337
  }
311
- const afterMatchContext = createAfterMatchContext(context, route.entryName);
312
- if (this.runMode === RUN_MODE.FULL) {
313
- await this.runner.afterMatch(afterMatchContext, { onLast: noop });
314
- }
315
- if (this.isSend(res)) {
316
- return;
317
- }
318
- const { current, url, status } = afterMatchContext.router;
319
- if (url) {
320
- this.redirect(res, url, status);
321
- return;
322
- }
323
- if (route.entryName !== current) {
324
- const matched2 = this.router.matchEntry(current);
325
- if (!matched2) {
326
- this.render404(context);
338
+ if (route.entryName) {
339
+ const afterMatchContext = createAfterMatchContext(
340
+ context,
341
+ route.entryName
342
+ );
343
+ if (this.runMode === RUN_MODE.FULL) {
344
+ await this.runner.afterMatch(afterMatchContext, { onLast: noop });
345
+ }
346
+ if (this.isSend(res)) {
327
347
  return;
328
348
  }
329
- route = matched2.generate(context.url);
349
+ const { current, url, status } = afterMatchContext.router;
350
+ if (url) {
351
+ this.redirect(res, url, status);
352
+ return;
353
+ }
354
+ if (route.entryName !== current) {
355
+ const matched2 = this.router.matchEntry(current);
356
+ if (!matched2) {
357
+ this.render404(context);
358
+ return;
359
+ }
360
+ route = matched2.generate(context.url);
361
+ }
330
362
  }
331
- context.setParams(route.params);
332
- context.setServerData("router", {
333
- baseUrl: route.urlPath,
334
- params: route.params
335
- });
336
363
  if (this.frameWebHandler) {
337
364
  res.locals = res.locals || {};
338
365
  const middlewareContext = createMiddlewareContext(context);
@@ -341,47 +368,20 @@ class ModernServer {
341
368
  ...res.locals,
342
369
  ...middlewareContext.response.locals
343
370
  };
344
- }
345
- if (this.isSend(res)) {
346
- return;
347
- }
348
- if (route.responseHeaders) {
349
- Object.keys(route.responseHeaders).forEach((key) => {
350
- const value = route.responseHeaders[key];
351
- if (value) {
352
- context.res.setHeader(key, value);
353
- }
354
- });
371
+ if (this.isSend(res)) {
372
+ return;
373
+ }
355
374
  }
356
375
  const renderResult = await this.handleWeb(context, route);
357
376
  if (!renderResult) {
358
- this.render404(context);
359
- return;
360
- }
361
- if (renderResult.redirect) {
362
- this.redirect(
363
- res,
364
- renderResult.content,
365
- renderResult.statusCode
366
- );
367
377
  return;
368
378
  }
369
- if (this.isSend(res)) {
379
+ const { contentStream: responseStream } = renderResult;
380
+ let { content: response } = renderResult;
381
+ if (route.entryName && responseStream) {
382
+ responseStream.pipe(res);
370
383
  return;
371
384
  }
372
- res.setHeader("content-type", renderResult.contentType);
373
- const { contentStream } = renderResult;
374
- if (contentStream) {
375
- contentStream.pipe(
376
- templateInjectableStream({
377
- prependHead: route.entryName ? `<script>window._SERVER_DATA=${JSON.stringify(
378
- context.serverData
379
- )}</script>` : void 0
380
- })
381
- ).pipe(res);
382
- return;
383
- }
384
- let response = renderResult.content;
385
385
  if (route.entryName) {
386
386
  const afterRenderContext = createAfterRenderContext(
387
387
  context,
@@ -393,11 +393,6 @@ class ModernServer {
393
393
  if (this.isSend(res)) {
394
394
  return;
395
395
  }
396
- afterRenderContext.template.prependHead(
397
- `<script>window._SERVER_DATA=${JSON.stringify(
398
- context.serverData
399
- )}</script>`
400
- );
401
396
  response = afterRenderContext.template.get();
402
397
  }
403
398
  res.end(response);
@@ -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/XV7p0812FeqQvmefgYASU/src/type" />
5
+ /// <reference types=".dts-temp/J6fbpWzvbo_85KxyybF4s/src/type" />
6
6
  import { IncomingMessage, ServerResponse } from 'http';
7
7
  import qs from 'querystring';
8
8
  import type { ModernServerContext as ModernServerContextInterface } from '@modern-js/types';
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ import type { ModernServerContext } from '@modern-js/types';
4
+ export declare const injectSeverData: (content: string, context: ModernServerContext) => string;
5
+ export declare const injectServerDataStream: (content: Readable, context: ModernServerContext) => import("stream").Transform;
@@ -1,5 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
+ /// <reference types="node" />
3
4
  import { IncomingMessage, ServerResponse } from 'http';
4
5
  import type { ListenOptions } from 'net';
5
6
  import { ModernServerOptions, ServerConstructor } from '../type';
@@ -45,7 +46,7 @@ export declare class Server {
45
46
  close(): Promise<void>;
46
47
  listen<T extends number | ListenOptions | undefined>(options: T, listener: any): void;
47
48
  getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
48
- render(req: IncomingMessage, res: ServerResponse, url?: string): Promise<string | null>;
49
+ render(req: IncomingMessage, res: ServerResponse, url?: string): Promise<string | import("stream").Readable | null>;
49
50
  private createHookRunner;
50
51
  private injectContext;
51
52
  private initAppContext;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { IncomingMessage, ServerResponse, Server } from 'http';
3
4
  import { Adapter, WebAdapter, APIServerStartInput, ServerOptions } from '@modern-js/server-core';
4
5
  import type { ModernServerContext, ServerRoute } from '@modern-js/types';
@@ -23,7 +24,7 @@ export declare class ModernServer implements ModernServerInterface {
23
24
  protected reader: typeof reader;
24
25
  protected readonly proxyTarget: ModernServerOptions['proxyTarget'];
25
26
  private routeRenderHandler;
26
- private beforeRouteHandler;
27
+ private loaderHandler;
27
28
  private frameWebHandler;
28
29
  private frameAPIHandler;
29
30
  private proxyHandler;
@@ -41,19 +42,18 @@ export declare class ModernServer implements ModernServerInterface {
41
42
  }: ModernServerOptions);
42
43
  onInit(runner: ServerHookRunner, app: Server): Promise<void>;
43
44
  onRepack(_: BuildOptions): void;
44
- addBeforeRouteHandler(): void;
45
45
  protected onServerChange({
46
46
  filepath
47
47
  }: {
48
48
  filepath: string;
49
49
  }): void;
50
50
  getRequestHandler(): (req: IncomingMessage, res: ServerResponse, next?: () => void) => void | ServerResponse;
51
- render(req: IncomingMessage, res: ServerResponse, url?: string): Promise<string | null>;
51
+ render(req: IncomingMessage, res: ServerResponse, url?: string): Promise<string | import("stream").Readable | null>;
52
52
  createHTTPServer(handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void): Promise<Server<typeof IncomingMessage, typeof ServerResponse>>;
53
53
  protected getRoutes(): ServerRoute[];
54
54
  protected addHandler(handler: ModernServerHandler): void;
55
55
  protected render404(context: ModernServerContext): void;
56
- protected prepareBeforeRouteHandler(specs: ServerRoute[], distDir: string): Promise<void>;
56
+ protected prepareLoaderHandler(specs: ServerRoute[], distDir: string): Promise<void>;
57
57
  protected prepareFrameHandler(options?: {
58
58
  onlyApi: boolean;
59
59
  onlyWeb: boolean;
@@ -61,7 +61,7 @@ export interface ModernServerInterface {
61
61
  onRepack: (options: BuildOptions) => void;
62
62
  getRequestHandler: () => (req: IncomingMessage, res: ServerResponse, next?: () => void) => void;
63
63
  createHTTPServer: (handler: (req: IncomingMessage, res: ServerResponse, next?: () => void) => void) => Promise<Server>;
64
- render: (req: IncomingMessage, res: ServerResponse, url?: string) => Promise<string | null>;
64
+ render: (req: IncomingMessage, res: ServerResponse, url?: string) => Promise<string | Readable | null>;
65
65
  }
66
66
  export type ServerConstructor = (options: ModernServerOptions) => ModernServerInterface;
67
67
  export type ModernServerHandler = (context: ModernServerContext, next: NextFunction) => Promise<void> | void;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node/http" />
3
- /// <reference types=".dts-temp/XV7p0812FeqQvmefgYASU/src/type" />
3
+ /// <reference types=".dts-temp/J6fbpWzvbo_85KxyybF4s/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
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "2.9.0",
14
+ "version": "2.10.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/cjs/index.js",
@@ -23,6 +23,7 @@
23
23
  "import": "./dist/esm-node/index.js",
24
24
  "require": "./dist/cjs/index.js"
25
25
  },
26
+ "types": "./dist/types/index.d.ts",
26
27
  "default": "./dist/esm/index.js"
27
28
  },
28
29
  "./worker": {
@@ -31,6 +32,7 @@
31
32
  "import": "./dist/esm-node/workerServer.js",
32
33
  "require": "./dist/cjs/workerServer.js"
33
34
  },
35
+ "types": "./dist/types/workerServer.d.ts",
34
36
  "default": "./dist/esm/workerServer.js"
35
37
  }
36
38
  },
@@ -54,12 +56,12 @@
54
56
  "merge-deep": "^3.0.3",
55
57
  "path-to-regexp": "^6.2.0",
56
58
  "serve-static": "^1.14.1",
57
- "@modern-js/utils": "2.9.0",
58
- "@modern-js/server-core": "2.9.0"
59
+ "@modern-js/utils": "2.10.0",
60
+ "@modern-js/server-core": "2.10.0"
59
61
  },
60
62
  "devDependencies": {
61
63
  "@types/cookie": "^0.4.1",
62
- "@types/jest": "^27",
64
+ "@types/jest": "^29",
63
65
  "@types/lru-cache": "^5.1.1",
64
66
  "@types/merge-deep": "^3.0.0",
65
67
  "@types/node": "^14",
@@ -67,14 +69,14 @@
67
69
  "@types/fresh": "^0.5.0",
68
70
  "@types/serve-static": "^1.13.10",
69
71
  "axios": "^1.2.1",
70
- "jest": "^27",
72
+ "jest": "^29",
71
73
  "node-mocks-http": "^1.11.0",
72
74
  "portfinder": "^1.0.28",
73
75
  "typescript": "^4",
74
- "@modern-js/types": "2.9.0",
75
- "@modern-js/server-core": "2.9.0",
76
- "@scripts/jest-config": "2.9.0",
77
- "@scripts/build": "2.9.0"
76
+ "@modern-js/types": "2.10.0",
77
+ "@modern-js/server-core": "2.10.0",
78
+ "@scripts/build": "2.10.0",
79
+ "@scripts/jest-config": "2.10.0"
78
80
  },
79
81
  "sideEffects": false,
80
82
  "publishConfig": {