Skip to content

Commit

Permalink
perf: optimize getResolveErrorBodyCallback (#2921)
Browse files Browse the repository at this point in the history
* perf: optimize getResolveErrorBodyCallback

* fix: test

---------

Co-authored-by: uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
tsctx and Uzlopak authored Mar 6, 2024
1 parent aa86e89 commit 7d20bed
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const Readable = require('./readable')
const { Readable } = require('./readable')
const {
InvalidArgumentError,
RequestAbortedError
Expand Down
4 changes: 3 additions & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const kContentType = Symbol('kContentType')

const noop = () => {}

module.exports = class BodyReadable extends Readable {
class BodyReadable extends Readable {
constructor ({
resume,
abort,
Expand Down Expand Up @@ -348,3 +348,5 @@ function consumeFinish (consume, err) {
consume.length = 0
consume.body = null
}

module.exports = { Readable: BodyReadable, chunksDecode }
14 changes: 8 additions & 6 deletions lib/api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ const assert = require('node:assert')
const {
ResponseStatusCodeError
} = require('../core/errors')
const { toUSVString } = require('../core/util')

const { chunksDecode } = require('./readable')
const CHUNK_LIMIT = 128 * 1024

async function getResolveErrorBodyCallback ({ callback, body, contentType, statusCode, statusMessage, headers }) {
assert(body)

let chunks = []
let limit = 0
let length = 0

for await (const chunk of body) {
chunks.push(chunk)
limit += chunk.length
if (limit > 128 * 1024) {
length += chunk.length
if (length > CHUNK_LIMIT) {
chunks = null
break
}
Expand All @@ -26,13 +28,13 @@ async function getResolveErrorBodyCallback ({ callback, body, contentType, statu

try {
if (contentType.startsWith('application/json')) {
const payload = JSON.parse(toUSVString(Buffer.concat(chunks)))
const payload = JSON.parse(chunksDecode(chunks, length))
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
return
}

if (contentType.startsWith('text/')) {
const payload = toUSVString(Buffer.concat(chunks))
const payload = chunksDecode(chunks, length)
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
return
}
Expand Down
2 changes: 1 addition & 1 deletion test/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { tspl } = require('@matteo.collina/tspl')
const { test, describe } = require('node:test')
const Readable = require('../lib/api/readable')
const { Readable } = require('../lib/api/readable')

describe('Readable', () => {
test('avoid body reordering', async function (t) {
Expand Down

0 comments on commit 7d20bed

Please sign in to comment.