-
Notifications
You must be signed in to change notification settings - Fork 366
/
create-stream-promise.ts
56 lines (49 loc) · 1.54 KB
/
create-stream-promise.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Buffer } from 'buffer'
import { IncomingMessage } from 'http'
const SEC_TO_MILLISEC = 1e3
// 6 MiB
const DEFAULT_BYTES_LIMIT = 6e6
const createStreamPromise = function (
stream: IncomingMessage,
timeoutSeconds: number,
bytesLimit = DEFAULT_BYTES_LIMIT,
): Promise<Buffer> {
return new Promise(function streamPromiseFunc(resolve, reject) {
let data: unknown[] | null = []
let dataLength = 0
// @ts-expect-error TS(7034) FIXME: Variable 'timeoutId' implicitly has type 'any' in ... Remove this comment to see the full error message
let timeoutId: NodeJS.Timeout = null
if (timeoutSeconds != null && Number.isFinite(timeoutSeconds)) {
timeoutId = setTimeout(() => {
data = null
reject(new Error('Request timed out waiting for body'))
}, timeoutSeconds * SEC_TO_MILLISEC)
}
stream.on('data', function onData(chunk) {
if (!Array.isArray(data)) {
// Stream harvesting closed
return
}
dataLength += chunk.length
if (dataLength > bytesLimit) {
data = null
reject(new Error('Stream body too big'))
} else {
data.push(chunk)
}
})
stream.on('error', function onError(error) {
data = null
reject(error)
clearTimeout(timeoutId)
})
stream.on('end', function onEnd() {
clearTimeout(timeoutId)
if (data) {
// @ts-expect-error TS(7005) FIXME: Variable 'data' implicitly has an 'any[]' type.
resolve(Buffer.concat(data))
}
})
})
}
export default createStreamPromise