Skip to content

Commit c0d4f4f

Browse files
committed
refactor: optimize internal read function
1 parent d11899b commit c0d4f4f

File tree

5 files changed

+61
-93
lines changed

5 files changed

+61
-93
lines changed

lib/read.js

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
* @private
1212
*/
1313

14-
var createError = require('http-errors')
15-
var getBody = require('raw-body')
16-
var iconv = require('iconv-lite')
17-
var onFinished = require('on-finished')
18-
var zlib = require('node:zlib')
14+
const createError = require('http-errors')
15+
const getBody = require('raw-body')
16+
const iconv = require('iconv-lite')
17+
const onFinished = require('on-finished')
18+
const zlib = require('node:zlib')
1919

2020
/**
2121
* Module exports.
@@ -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) {
76-
var _error
82+
let _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,
@@ -114,13 +120,13 @@ function read (req, res, next, parse, debug, options) {
114120
}
115121

116122
// parse
117-
var str = body
123+
let 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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ function json (options) {
124124

125125
// read
126126
read(req, res, next, parse, debug, {
127-
encoding: charset,
128-
inflate: inflate,
129-
limit: limit,
130-
verify: verify
127+
charset,
128+
inflate,
129+
limit,
130+
verify
131131
})
132132
}
133133
}

lib/types/raw.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ function raw (options) {
6767

6868
// read
6969
read(req, res, next, parse, debug, {
70-
encoding: null,
71-
inflate: inflate,
72-
limit: limit,
73-
verify: verify
70+
charset: null,
71+
inflate,
72+
limit,
73+
verify
7474
})
7575
}
7676
}

lib/types/text.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ function text (options) {
7272

7373
// read
7474
read(req, res, next, parse, debug, {
75-
encoding: charset,
76-
inflate: inflate,
77-
limit: limit,
78-
verify: verify
75+
charset,
76+
inflate,
77+
limit,
78+
verify
7979
})
8080
}
8181
}

lib/types/urlencoded.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ function urlencoded (options) {
3939
var { inflate, limit, verify, shouldParse } = normalizeOptions(opts, 'application/x-www-form-urlencoded')
4040

4141
var extended = Boolean(opts.extended)
42-
var charsetSentinel = opts.charsetSentinel
43-
var interpretNumericEntities = opts.interpretNumericEntities
4442

4543
var defaultCharset = opts.defaultCharset || 'utf-8'
4644
if (defaultCharset !== 'utf-8' && defaultCharset !== 'iso-8859-1') {
@@ -96,13 +94,10 @@ function urlencoded (options) {
9694

9795
// read
9896
read(req, res, next, parse, debug, {
99-
debug: debug,
100-
encoding: charset,
101-
inflate: inflate,
102-
limit: limit,
103-
verify: verify,
104-
charsetSentinel: charsetSentinel,
105-
interpretNumericEntities: interpretNumericEntities
97+
charset,
98+
inflate,
99+
limit,
100+
verify
106101
})
107102
}
108103
}

0 commit comments

Comments
 (0)