-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.js
More file actions
40 lines (39 loc) · 1.21 KB
/
Copy pathutils.js
File metadata and controls
40 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @param {import("http").IncomingMessage} req
* @param {number} [timeout]
* @returns {Promise<Buffer>}
*/
function requestBody (req, timeout = 10000) {
if (!req.headers['content-length']) return Promise.resolve(Buffer.allocUnsafe(0))
const sizeToMeet = Number(req.headers['content-length'])
return new Promise((resolve, reject) => {
/** @type {NodeJS.Timeout | null} */
let timer = null
let totalSize = 0
/** @type {Array<Buffer>} */
const chunks = []
/** @param {Buffer} chunk */
function onData (chunk) {
totalSize += chunk.byteLength
if (totalSize > sizeToMeet) {
req.removeListener('data', onData)
req.removeListener('end', onEnd)
return reject(new Error('BYTE_SIZE_DOES_NOT_MATCH_LENGTH'))
}
chunks.push(chunk)
}
function onEnd () {
if (timer) clearTimeout(timer)
req.removeListener('data', onData)
resolve(Buffer.concat(chunks))
}
req.on('data', onData)
req.once('end', onEnd)
timer = setTimeout(() => {
req.removeListener('data', onData)
req.removeListener('end', onEnd)
reject(new Error('TIMEOUT_WAITING_FOR_BODY_REACHED'))
}, timeout)
})
}
module.exports = { requestBody }