Skip to content

Commit

Permalink
perf: convert object to params (#3302)
Browse files Browse the repository at this point in the history
* Refactored passing object to function params

* Refactor write function argument order

* Updated argument order in writeBuffer function
  • Loading branch information
DarkGL authored Jun 6, 2024
1 parent 7cc2413 commit 250c863
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 50 deletions.
20 changes: 10 additions & 10 deletions lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,27 +978,27 @@ function writeH1 (client, request) {

/* istanbul ignore else: assertion */
if (!body || bodyLength === 0) {
writeBuffer({ abort, body: null, client, request, socket, contentLength, header, expectsPayload })
writeBuffer(abort, null, client, request, socket, contentLength, header, expectsPayload)
} else if (util.isBuffer(body)) {
writeBuffer({ abort, body, client, request, socket, contentLength, header, expectsPayload })
writeBuffer(abort, body, client, request, socket, contentLength, header, expectsPayload)
} else if (util.isBlobLike(body)) {
if (typeof body.stream === 'function') {
writeIterable({ abort, body: body.stream(), client, request, socket, contentLength, header, expectsPayload })
writeIterable(abort, body.stream(), client, request, socket, contentLength, header, expectsPayload)
} else {
writeBlob({ abort, body, client, request, socket, contentLength, header, expectsPayload })
writeBlob(abort, body, client, request, socket, contentLength, header, expectsPayload)
}
} else if (util.isStream(body)) {
writeStream({ abort, body, client, request, socket, contentLength, header, expectsPayload })
writeStream(abort, body, client, request, socket, contentLength, header, expectsPayload)
} else if (util.isIterable(body)) {
writeIterable({ abort, body, client, request, socket, contentLength, header, expectsPayload })
writeIterable(abort, body, client, request, socket, contentLength, header, expectsPayload)
} else {
assert(false)
}

return true
}

function writeStream ({ abort, body, client, request, socket, contentLength, header, expectsPayload }) {
function writeStream (abort, body, client, request, socket, contentLength, header, expectsPayload) {
assert(contentLength !== 0 || client[kRunning] === 0, 'stream body cannot be pipelined')

let finished = false
Expand Down Expand Up @@ -1101,7 +1101,7 @@ function writeStream ({ abort, body, client, request, socket, contentLength, hea
}
}

function writeBuffer ({ abort, body, client, request, socket, contentLength, header, expectsPayload }) {
function writeBuffer (abort, body, client, request, socket, contentLength, header, expectsPayload) {
try {
if (!body) {
if (contentLength === 0) {
Expand Down Expand Up @@ -1131,7 +1131,7 @@ function writeBuffer ({ abort, body, client, request, socket, contentLength, hea
}
}

async function writeBlob ({ abort, body, client, request, socket, contentLength, header, expectsPayload }) {
async function writeBlob (abort, body, client, request, socket, contentLength, header, expectsPayload) {
assert(contentLength === body.size, 'blob body must have content length')

try {
Expand Down Expand Up @@ -1159,7 +1159,7 @@ async function writeBlob ({ abort, body, client, request, socket, contentLength,
}
}

async function writeIterable ({ abort, body, client, request, socket, contentLength, header, expectsPayload }) {
async function writeIterable (abort, body, client, request, socket, contentLength, header, expectsPayload) {
assert(contentLength !== 0 || client[kRunning] === 0, 'iterator body cannot be pipelined')

let callback = null
Expand Down
78 changes: 38 additions & 40 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,82 +477,80 @@ function writeH2 (client, request) {
function writeBodyH2 () {
/* istanbul ignore else: assertion */
if (!body || contentLength === 0) {
writeBuffer({
writeBuffer(
abort,
stream,
null,
client,
request,
client[kSocket],
contentLength,
expectsPayload,
h2stream: stream,
body: null,
socket: client[kSocket]
})
expectsPayload
)
} else if (util.isBuffer(body)) {
writeBuffer({
writeBuffer(
abort,
stream,
body,
client,
request,
client[kSocket],
contentLength,
body,
expectsPayload,
h2stream: stream,
socket: client[kSocket]
})
expectsPayload
)
} else if (util.isBlobLike(body)) {
if (typeof body.stream === 'function') {
writeIterable({
writeIterable(
abort,
stream,
body.stream(),
client,
request,
client[kSocket],
contentLength,
expectsPayload,
h2stream: stream,
body: body.stream(),
socket: client[kSocket]
})
expectsPayload
)
} else {
writeBlob({
writeBlob(
abort,
stream,
body,
client,
request,
client[kSocket],
contentLength,
expectsPayload,
h2stream: stream,
socket: client[kSocket]
})
expectsPayload
)
}
} else if (util.isStream(body)) {
writeStream({
writeStream(
abort,
client[kSocket],
expectsPayload,
stream,
body,
client,
request,
contentLength,
expectsPayload,
socket: client[kSocket],
h2stream: stream,
header: ''
})
contentLength
)
} else if (util.isIterable(body)) {
writeIterable({
writeIterable(
abort,
stream,
body,
client,
request,
client[kSocket],
contentLength,
expectsPayload,
header: '',
h2stream: stream,
socket: client[kSocket]
})
expectsPayload
)
} else {
assert(false)
}
}
}

function writeBuffer ({ abort, h2stream, body, client, request, socket, contentLength, expectsPayload }) {
function writeBuffer (abort, h2stream, body, client, request, socket, contentLength, expectsPayload) {
try {
if (body != null && util.isBuffer(body)) {
assert(contentLength === body.byteLength, 'buffer body must have content length')
Expand All @@ -575,7 +573,7 @@ function writeBuffer ({ abort, h2stream, body, client, request, socket, contentL
}
}

function writeStream ({ abort, socket, expectsPayload, h2stream, body, client, request, contentLength }) {
function writeStream (abort, socket, expectsPayload, h2stream, body, client, request, contentLength) {
assert(contentLength !== 0 || client[kRunning] === 0, 'stream body cannot be pipelined')

// For HTTP/2, is enough to pipe the stream
Expand Down Expand Up @@ -606,7 +604,7 @@ function writeStream ({ abort, socket, expectsPayload, h2stream, body, client, r
}
}

async function writeBlob ({ abort, h2stream, body, client, request, socket, contentLength, expectsPayload }) {
async function writeBlob (abort, h2stream, body, client, request, socket, contentLength, expectsPayload) {
assert(contentLength === body.size, 'blob body must have content length')

try {
Expand Down Expand Up @@ -634,7 +632,7 @@ async function writeBlob ({ abort, h2stream, body, client, request, socket, cont
}
}

async function writeIterable ({ abort, h2stream, body, client, request, socket, contentLength, expectsPayload }) {
async function writeIterable (abort, h2stream, body, client, request, socket, contentLength, expectsPayload) {
assert(contentLength !== 0 || client[kRunning] === 0, 'iterator body cannot be pipelined')

let callback = null
Expand Down

0 comments on commit 250c863

Please sign in to comment.