Skip to content

Commit

Permalink
chore: enhance server performance (#4632)
Browse files Browse the repository at this point in the history
* chore: enhance server performance

* fix: the url, querystring maybe change
  • Loading branch information
GiveMe-A-Name authored Sep 15, 2023
1 parent dc93952 commit 074f9a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
28 changes: 23 additions & 5 deletions packages/server/prod-server/src/libs/context/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage, ServerResponse } from 'http';
import { URL } from 'url';
import qs from 'querystring';
import qs, { ParsedUrlQuery } from 'querystring';
import { Buffer } from 'buffer';
import type {
ModernServerContext as ModernServerContextInterface,
Expand Down Expand Up @@ -55,6 +55,10 @@ export class ModernServerContext implements ModernServerContextInterface {

private options: ContextOptions = {};

#urls: Map<string, URL>;

#queries: Map<string, ParsedUrlQuery>;

constructor(
req: IncomingMessage,
res: ServerResponse,
Expand All @@ -64,6 +68,8 @@ export class ModernServerContext implements ModernServerContextInterface {
this.res = res;
this.options = options || {};
this.bind();
this.#urls = new Map();
this.#queries = new Map();
this.serverTiming = new ServerTiming(
this.res,
cutNameByHyphen(options?.metaName || 'modern-js'),
Expand All @@ -72,8 +78,13 @@ export class ModernServerContext implements ModernServerContextInterface {

private get parsedURL() {
try {
// only for parse url, use mock
return new URL(this.req.url!, MOCK_URL_BASE);
let url = this.#urls.get(this.req.url!);
if (!url) {
// only for parse url, use mock
url = new URL(this.req.url!, MOCK_URL_BASE);
this.#urls.set(this.req.url!, url);
}
return url;
} catch (e) {
this.logger.error(
'Parse URL error',
Expand Down Expand Up @@ -180,10 +191,12 @@ export class ModernServerContext implements ModernServerContextInterface {
if (!host) {
host = this.getReqHeader('Host');
}

host = (host as string).split(/\s*,\s*/, 1)[0] || 'undefined';
// the host = '',if we can't cat Host or X-Forwarded-Host header
// but the this.href would assign a invalid value:`http[s]://${pathname}`
// so we need assign host a no-empty value.
return (host as string).split(/\s*,\s*/, 1)[0] || 'undefined';
return host;
}

public get protocol() {
Expand Down Expand Up @@ -229,7 +242,12 @@ export class ModernServerContext implements ModernServerContextInterface {

public get query() {
const str = this.querystring;
return qs.parse(str);
let query = this.#queries.get(str);
if (!query) {
query = qs.parse(str);
this.#queries.set(str, query);
}
return query;
}

/* response property */
Expand Down
44 changes: 20 additions & 24 deletions packages/server/prod-server/src/server/modernServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,23 +535,21 @@ export class ModernServer implements ModernServerInterface {
return;
}

if (route.entryName) {
if (route.entryName && this.runMode === RUN_MODE.FULL) {
const afterMatchContext = createAfterMatchContext(
context,
route.entryName,
);

// only full mode run server hook
if (this.runMode === RUN_MODE.FULL) {
const end = time();
await this.runner.afterMatch(afterMatchContext, { onLast: noop });
const cost = end();
cost &&
reporter.reportTiming(
ServerReportTimings.SERVER_HOOK_AFTER_MATCH,
cost,
);
}
const end = time();

await this.runner.afterMatch(afterMatchContext, { onLast: noop });
const cost = end();
cost &&
reporter.reportTiming(
ServerReportTimings.SERVER_HOOK_AFTER_MATCH,
cost,
);

if (this.isSend(res)) {
return;
Expand Down Expand Up @@ -607,25 +605,23 @@ export class ModernServer implements ModernServerInterface {
return;
}

if (route.entryName) {
if (route.entryName && this.runMode === RUN_MODE.FULL) {
const afterRenderContext = createAfterRenderContext(
context,
response.toString(),
);

// only full mode run server hook
// FIXME: how to run server hook in streaming
if (this.runMode === RUN_MODE.FULL) {
const end = time();
await this.runner.afterRender(afterRenderContext, { onLast: noop });
const cost = end();
// we shouldn't reporter unable run after-render.
cost &&
reporter.reportTiming(
ServerReportTimings.SERVER_HOOK_AFTER_RENDER,
cost,
);
}
const end = time();
await this.runner.afterRender(afterRenderContext, { onLast: noop });
const cost = end();
// we shouldn't reporter unable run after-render.
cost &&
reporter.reportTiming(
ServerReportTimings.SERVER_HOOK_AFTER_RENDER,
cost,
);

if (this.isSend(res)) {
return;
Expand Down

0 comments on commit 074f9a3

Please sign in to comment.