Skip to content

Commit 79c2a67

Browse files
committed
perf(Server): improve request handle performance
1 parent 5262f2b commit 79c2a67

File tree

3 files changed

+80
-81
lines changed

3 files changed

+80
-81
lines changed

packages/server/js/response.js

+1-46
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const mime = require('mime-types')
66
const { Writable } = require('stream')
77
const parseRange = require('range-parser')
88
const { keepHeaderCase } = require('./constants')
9+
const httpStatusCode = require('./status')
910

1011
const staticPath = path.resolve(process.cwd(), 'static')
1112

@@ -22,52 +23,6 @@ const hContentRange = 'Content-Range'
2223
const hLastModified = 'Last-Modified'
2324
const hCacheControl = 'Cache-Control'
2425

25-
const httpStatusCode = {
26-
// Informational
27-
100: 'Continue',
28-
101: 'Switching Protocols',
29-
// Successful
30-
200: 'OK',
31-
201: 'Created',
32-
202: 'Accepted',
33-
203: 'Non-Authoritative Information',
34-
204: 'No Content',
35-
205: 'Reset Content',
36-
206: 'Partial Content',
37-
// Redirection
38-
300: 'Multiple Choices',
39-
301: 'Moved Permanently',
40-
302: 'Found',
41-
303: 'See Other',
42-
304: 'Not Modified',
43-
305: 'Use Proxy',
44-
307: 'Temporary Redirect',
45-
// Client Error
46-
400: 'Bad Request',
47-
401: 'Unauthorized',
48-
402: 'Payment Required',
49-
403: 'Forbidden',
50-
404: 'Not Found',
51-
405: 'Method Not Allowed',
52-
406: 'Not Acceptable',
53-
408: 'Request Timeout',
54-
409: 'Conflict',
55-
410: 'Gone',
56-
411: 'Length Required',
57-
412: 'Precondition Failed',
58-
413: 'Request Entity Too Large',
59-
414: 'Request-URI Too Long',
60-
415: 'Unsupported Media Type',
61-
416: 'Requested Range Not Satisfiable',
62-
417: 'Expectation Failed',
63-
418: 'I\'m a teapot',
64-
// Server Error
65-
500: 'Internal Server Error',
66-
501: 'Not Implemented',
67-
502: 'Bad Gateway',
68-
503: 'Service Unavailable'
69-
}
70-
7126
class Response extends Writable {
7227
constructor (connection) {
7328
super()

packages/server/js/routes.js

+34-35
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ const WebSocketResponse = require('./websocket')
55
const Connection = require('./connection')
66
const ServerError = require('./errors')
77

8+
async function handler (params, callbacks, response, request) {
9+
const paramsMap = {}
10+
if (params) {
11+
params.forEach((key, index) => {
12+
paramsMap[key] = decodeURIComponent(request.getParameter(index))
13+
})
14+
}
15+
const conn = Connection.create(this, request, response)
16+
const req = Request.create(conn)
17+
const res = Response.create(conn)
18+
try {
19+
conn.processBodyData()
20+
await callbacks(req, res, paramsMap)
21+
} catch (e) {
22+
this.log.error('Server Internal Error', e)
23+
if (!res._writableState.destroyed) {
24+
if (e instanceof ServerError && e.httpCode) {
25+
res.status(e.httpCode).end(e.message)
26+
} else {
27+
res.status(500).end('Server Internal Error')
28+
}
29+
}
30+
}
31+
}
32+
833
class Routes {
934
constructor () {
1035
this._routes = {}
@@ -24,51 +49,25 @@ class Routes {
2449

2550
route (method, path, callbacks) {
2651
if (this.lock) {
27-
throw new ServerError({
28-
code: 'SERVER_ALREADY_STARTED',
29-
message: 'Cannot add route after sterver started.'
30-
})
52+
throw new Error('The server is started, should not add route.')
53+
}
54+
if (typeof path !== 'string' && !path.startsWith('/')) {
55+
throw new TypeError('Route path whould starts with "/".')
3156
}
3257
if (!this._routes[path]) {
3358
this._routes[path] = {}
3459
}
3560
if (this._routes[path][method]) {
36-
throw new ServerError({
37-
code: 'INVALID_DUPLICATE_ROUTER',
38-
message: 'Invalid, duplicated router.'
39-
})
61+
throw new Error(`${method.toUpperCase()} "${path}" is exists.`)
4062
}
4163
if (method === 'ws') {
4264
this._routes[path][method] = callbacks
4365
} else {
44-
let URLParams = path.match(/:\w+/g)
45-
if (URLParams) {
46-
URLParams = URLParams.map(key => key.slice(1))
47-
}
48-
this._routes[path][method] = async (response, request) => {
49-
const params = {}
50-
if (URLParams) {
51-
URLParams.forEach((key, index) => {
52-
params[key] = decodeURIComponent(request.getParameter(index))
53-
})
54-
}
55-
const conn = Connection.create(this, request, response)
56-
const req = Request.create(conn)
57-
const res = Response.create(conn)
58-
try {
59-
conn.processBodyData()
60-
await callbacks(req, res, params)
61-
} catch (e) {
62-
this.log.error('Server Internal Error', e)
63-
if (!res._writableState.destroyed) {
64-
if (e instanceof ServerError && e.httpCode) {
65-
res.status(e.httpCode).end(e.message)
66-
} else {
67-
res.status(500).end('Server Internal Error')
68-
}
69-
}
70-
}
66+
let params = path.match(/:\w+/g)
67+
if (params) {
68+
params = params.map(key => key.slice(1))
7169
}
70+
this._routes[path][method] = handler.bind(this, params, callbacks)
7271
}
7372
}
7473

packages/server/js/status.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = {
2+
// Informational
3+
100: 'Continue',
4+
101: 'Switching Protocols',
5+
// Successful
6+
200: 'OK',
7+
201: 'Created',
8+
202: 'Accepted',
9+
203: 'Non-Authoritative Information',
10+
204: 'No Content',
11+
205: 'Reset Content',
12+
206: 'Partial Content',
13+
// Redirection
14+
300: 'Multiple Choices',
15+
301: 'Moved Permanently',
16+
302: 'Found',
17+
303: 'See Other',
18+
304: 'Not Modified',
19+
305: 'Use Proxy',
20+
307: 'Temporary Redirect',
21+
// Client Error
22+
400: 'Bad Request',
23+
401: 'Unauthorized',
24+
402: 'Payment Required',
25+
403: 'Forbidden',
26+
404: 'Not Found',
27+
405: 'Method Not Allowed',
28+
406: 'Not Acceptable',
29+
408: 'Request Timeout',
30+
409: 'Conflict',
31+
410: 'Gone',
32+
411: 'Length Required',
33+
412: 'Precondition Failed',
34+
413: 'Request Entity Too Large',
35+
414: 'Request-URI Too Long',
36+
415: 'Unsupported Media Type',
37+
416: 'Requested Range Not Satisfiable',
38+
417: 'Expectation Failed',
39+
418: 'I\'m a teapot',
40+
// Server Error
41+
500: 'Internal Server Error',
42+
501: 'Not Implemented',
43+
502: 'Bad Gateway',
44+
503: 'Service Unavailable'
45+
}

0 commit comments

Comments
 (0)