Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve encoding #1768

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
14 changes: 7 additions & 7 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ function extractBody (object, keepalive = false) {
ReadableStream = require('stream/web').ReadableStream
}

const textEncoder = new TextEncoder()

// 1. Let stream be null.
let stream = null

Expand All @@ -41,7 +43,7 @@ function extractBody (object, keepalive = false) {
stream = new ReadableStream({
async pull (controller) {
controller.enqueue(
typeof source === 'string' ? new TextEncoder().encode(source) : source
typeof source === 'string' ? textEncoder.encode(source) : source
)
queueMicrotask(() => readableStreamClose(controller))
},
Expand Down Expand Up @@ -108,17 +110,15 @@ function extractBody (object, keepalive = false) {
// Set action to this step: run the multipart/form-data
// encoding algorithm, with object’s entry list and UTF-8.
action = async function * (object) {
const enc = new TextEncoder()

for (const [name, value] of object) {
if (typeof value === 'string') {
yield enc.encode(
yield textEncoder.encode(
prefix +
`; name="${escape(normalizeLinefeeds(name))}"` +
`\r\n\r\n${normalizeLinefeeds(value)}\r\n`
)
} else {
yield enc.encode(
yield textEncoder.encode(
prefix +
`; name="${escape(normalizeLinefeeds(name))}"` +
(value.name ? `; filename="${escape(value.name)}"` : '') +
Expand All @@ -130,11 +130,11 @@ function extractBody (object, keepalive = false) {

yield * value.stream()

yield enc.encode('\r\n')
yield textEncoder.encode('\r\n')
}
}

yield enc.encode(`--${boundary}--`)
yield textEncoder.encode(`--${boundary}--`)
}

// Set source to object.
Expand Down
9 changes: 6 additions & 3 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ function fill (headers, object) {
// 2. Append (header’s first item, header’s second item) to headers.
headers.append(header[0], header[1])
}
} else if (typeof object === 'object' && object !== null) {
} else if (object !== null && typeof object === 'object') {
// Note: null should throw

// 2. Otherwise, object is a record, then for each key → value in object,
// append (key, value) to headers
for (const [key, value] of Object.entries(object)) {
headers.append(key, value)
const keys = Object.keys(object)
const keysLength = keys.length

for (let i = 0; i < keysLength; i++) {
headers.append(keys[i], object[keys[i]])
}
} else {
throw webidl.errors.conversionFailed({
Expand Down