Skip to content

Commit 0d5f462

Browse files
committed
refactor: optimize internal read function
1 parent ac9f996 commit 0d5f462

File tree

5 files changed

+42
-69
lines changed

5 files changed

+42
-69
lines changed

lib/read.js

+38-65
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,55 @@ module.exports = read
3636
*/
3737

3838
function read (req, res, next, parse, debug, options) {
39-
var length
40-
var opts = options
41-
var stream
42-
4339
// read options
44-
var encoding = opts.encoding !== null
45-
? opts.encoding
46-
: null
47-
var verify = opts.verify
48-
49-
try {
50-
// get the content stream
51-
stream = contentstream(req, debug, opts.inflate)
52-
length = stream.length
53-
stream.length = undefined
54-
} catch (err) {
55-
return next(err)
40+
const charset = options.charset
41+
42+
// get the content stream
43+
const contentEncoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
44+
debug('content-encoding "%s"', contentEncoding)
45+
46+
if (options.inflate === false && contentEncoding !== 'identity') {
47+
return next(createError(415, 'content encoding unsupported', {
48+
encoding: contentEncoding,
49+
type: 'encoding.unsupported'
50+
}))
5651
}
5752

58-
// set raw-body options
59-
opts.length = length
60-
opts.encoding = verify
61-
? null
62-
: encoding
53+
let stream
54+
if (contentEncoding === 'identity') {
55+
// set raw-body expected length
56+
stream = req
57+
options.length = req.headers['content-length']
58+
} else {
59+
try {
60+
stream = createDecompressionStream(contentEncoding, debug)
61+
req.pipe(stream)
62+
} catch (err) {
63+
return next(err)
64+
}
65+
}
6366

6467
// assert charset is supported
65-
if (opts.encoding === null && encoding !== null && !iconv.encodingExists(encoding)) {
66-
return next(createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
67-
charset: encoding.toLowerCase(),
68+
if (options.verify && charset !== null && !iconv.encodingExists(charset)) {
69+
return next(createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
70+
charset: charset.toLowerCase(),
6871
type: 'charset.unsupported'
6972
}))
7073
}
7174

75+
// set raw-body encoding
76+
options.encoding = options.verify ? null : charset
77+
7278
// read body
7379
debug('read body')
74-
getBody(stream, opts, function (error, body) {
80+
getBody(stream, options, function (error, body) {
7581
if (error) {
7682
var _error
7783

7884
if (error.type === 'encoding.unsupported') {
7985
// echo back charset
80-
_error = createError(415, 'unsupported charset "' + encoding.toUpperCase() + '"', {
81-
charset: encoding.toLowerCase(),
86+
_error = createError(415, 'unsupported charset "' + charset.toUpperCase() + '"', {
87+
charset: charset.toLowerCase(),
8288
type: 'charset.unsupported'
8389
})
8490
} else {
@@ -100,10 +106,10 @@ function read (req, res, next, parse, debug, options) {
100106
}
101107

102108
// verify
103-
if (verify) {
109+
if (options.verify) {
104110
try {
105111
debug('verify body')
106-
verify(req, res, body, encoding)
112+
options.verify(req, res, body, charset)
107113
} catch (err) {
108114
next(createError(403, err, {
109115
body: body,
@@ -117,10 +123,10 @@ function read (req, res, next, parse, debug, options) {
117123
var str = body
118124
try {
119125
debug('parse body')
120-
str = typeof body !== 'string' && encoding !== null
121-
? iconv.decode(body, encoding)
126+
str = typeof body !== 'string' && charset !== null
127+
? iconv.decode(body, charset)
122128
: body
123-
req.body = parse(str, encoding)
129+
req.body = parse(str, charset)
124130
} catch (err) {
125131
next(createError(400, err, {
126132
body: str,
@@ -133,39 +139,6 @@ function read (req, res, next, parse, debug, options) {
133139
})
134140
}
135141

136-
/**
137-
* Get the content stream of the request.
138-
*
139-
* @param {object} req
140-
* @param {function} debug
141-
* @param {boolean} [inflate=true]
142-
* @return {object}
143-
* @api private
144-
*/
145-
146-
function contentstream (req, debug, inflate) {
147-
var encoding = (req.headers['content-encoding'] || 'identity').toLowerCase()
148-
var length = req.headers['content-length']
149-
150-
debug('content-encoding "%s"', encoding)
151-
152-
if (inflate === false && encoding !== 'identity') {
153-
throw createError(415, 'content encoding unsupported', {
154-
encoding: encoding,
155-
type: 'encoding.unsupported'
156-
})
157-
}
158-
159-
if (encoding === 'identity') {
160-
req.length = length
161-
return req
162-
}
163-
164-
var stream = createDecompressionStream(encoding, debug)
165-
req.pipe(stream)
166-
return stream
167-
}
168-
169142
/**
170143
* Create a decompression stream for the given encoding.
171144
* @param {string} encoding

lib/types/json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ function json (options) {
123123

124124
// read
125125
read(req, res, next, parse, debug, {
126-
encoding: charset,
126+
charset,
127127
inflate,
128128
limit,
129129
verify

lib/types/raw.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function raw (options) {
6666

6767
// read
6868
read(req, res, next, parse, debug, {
69-
encoding: null,
69+
charset: null,
7070
inflate,
7171
limit,
7272
verify

lib/types/text.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function text (options) {
7171

7272
// read
7373
read(req, res, next, parse, debug, {
74-
encoding: charset,
74+
charset,
7575
inflate,
7676
limit,
7777
verify

lib/types/urlencoded.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function urlencoded (options) {
9191

9292
// read
9393
read(req, res, next, parse, debug, {
94-
encoding: charset,
94+
charset,
9595
inflate,
9696
limit,
9797
verify

0 commit comments

Comments
 (0)