Skip to content

Commit

Permalink
perf: remove redundant operation in FormData (#2726)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsctx authored Feb 9, 2024
1 parent e30f200 commit c4adfdd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
8 changes: 1 addition & 7 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,7 @@ const hasToWellFormed = !!String.prototype.toWellFormed
* @param {string} val
*/
function toUSVString (val) {
if (hasToWellFormed) {
return `${val}`.toWellFormed()
} else if (nodeUtil.toUSVString) {
return nodeUtil.toUSVString(val)
}

return `${val}`
return hasToWellFormed ? `${val}`.toWellFormed() : nodeUtil.toUSVString(val)
}

/**
Expand Down
11 changes: 4 additions & 7 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { isBlobLike, toUSVString, makeIterator } = require('./util')
const { isBlobLike, makeIterator } = require('./util')
const { kState } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
Expand Down Expand Up @@ -133,7 +133,7 @@ class FormData {
? webidl.converters.Blob(value, { strict: false })
: webidl.converters.USVString(value)
filename = arguments.length === 3
? toUSVString(filename)
? webidl.converters.USVString(filename)
: undefined

// 2. Let entry be the result of creating an entry with name, value, and
Expand Down Expand Up @@ -238,15 +238,12 @@ Object.defineProperties(FormData.prototype, {
*/
function makeEntry (name, value, filename) {
// 1. Set name to the result of converting name into a scalar value string.
// "To convert a string into a scalar value string, replace any surrogates
// with U+FFFD."
// see: https://nodejs.org/dist/latest-v20.x/docs/api/buffer.html#buftostringencoding-start-end
name = Buffer.from(name).toString('utf8')
// Note: This operation was done by the webidl converter USVString.

// 2. If value is a string, then set value to the result of converting
// value into a scalar value string.
if (typeof value === 'string') {
value = Buffer.from(value).toString('utf8')
// Note: This operation was done by the webidl converter USVString.
} else {
// 3. Otherwise:

Expand Down

0 comments on commit c4adfdd

Please sign in to comment.