( body: NodeJS.ReadableStream, cap: number, )
| 331 | } |
| 332 | |
| 333 | async function readBodyCapped( |
| 334 | body: NodeJS.ReadableStream, |
| 335 | cap: number, |
| 336 | ): Promise<{ buffer: Buffer, sizeBytes: number, truncated: boolean }> { |
| 337 | const chunks: Buffer[] = [] |
| 338 | let received = 0 |
| 339 | let truncated = false |
| 340 | |
| 341 | for await (const chunk of body as AsyncIterable<Buffer>) { |
| 342 | const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk) |
| 343 | if (received + buf.length > cap) { |
| 344 | const remaining = cap - received |
| 345 | if (remaining > 0) { |
| 346 | chunks.push(buf.subarray(0, remaining)) |
| 347 | received += remaining |
| 348 | } |
| 349 | truncated = true |
| 350 | break |
| 351 | } |
| 352 | chunks.push(buf) |
| 353 | received += buf.length |
| 354 | } |
| 355 | |
| 356 | if (truncated) { |
| 357 | const maybeDestroyable = body as unknown as { destroy?: () => void } |
| 358 | if (typeof maybeDestroyable.destroy === 'function') { |
| 359 | maybeDestroyable.destroy() |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | return { |
| 364 | buffer: Buffer.concat(chunks), |
| 365 | sizeBytes: received, |
| 366 | truncated, |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | function resolveEnvironmentVariables( |
| 371 | environmentId: number | null, |
no test coverage detected