Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/utils/create-stream-promise.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import { Buffer } from 'buffer'
import { IncomingMessage } from 'http'
import type { Readable } from 'stream'

const SEC_TO_MILLISEC = 1e3

// 6 MiB
const DEFAULT_BYTES_LIMIT = 6e6

const createStreamPromise = function (
stream: IncomingMessage,
stream: Readable,
timeoutSeconds: number,
bytesLimit = DEFAULT_BYTES_LIMIT,
): Promise<Buffer> {
return new Promise(function streamPromiseFunc(resolve, reject) {
let data: unknown[] | null = []
let data: Buffer[] | 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
let timeoutId: NodeJS.Timeout | null = 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) {
stream.on('data', function onData(chunk: Buffer) {
if (!Array.isArray(data)) {
// Stream harvesting closed
return
Expand All @@ -41,12 +40,15 @@ const createStreamPromise = function (
stream.on('error', function onError(error) {
data = null
reject(error)
clearTimeout(timeoutId)
if (timeoutId) {
clearTimeout(timeoutId)
}
})
stream.on('end', function onEnd() {
clearTimeout(timeoutId)
if (timeoutId) {
clearTimeout(timeoutId)
}
if (data) {
// @ts-expect-error TS(7005) FIXME: Variable 'data' implicitly has an 'any[]' type.
resolve(Buffer.concat(data))
}
})
Expand Down