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

fix(fetch): toUSVString #986

Merged
merged 4 commits into from
Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const util = require('../core/util')
const { ReadableStreamFrom } = require('./util')
const { ReadableStreamFrom, toUSVString } = require('./util')
const { FormData } = require('./formdata')
const { kState } = require('./symbols')
const { Blob } = require('buffer')
Expand Down Expand Up @@ -156,7 +156,7 @@ function extractBody (object, keepalive = false) {
// TODO: byte sequence?
// TODO: scalar value string?
// TODO: else?
source = String(object)
source = toUSVString(object)
contentType = 'text/plain;charset=UTF-8'
}

Expand Down Expand Up @@ -293,7 +293,7 @@ const methods = {

async text () {
const blob = await this.blob()
return await blob.text()
return toUSVString(await blob.text())
},

async json () {
Expand Down
22 changes: 11 additions & 11 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { Blob } = require('buffer')
const { kState } = require('./symbols')
const { File } = require('./file')
const { HTMLFormElement } = require('./util')
const { HTMLFormElement, toUSVString } = require('./util')

class FormData {
constructor (...args) {
Expand All @@ -30,11 +30,11 @@ class FormData {
"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'"
)
}
const name = String(args[0])
const filename = args.length === 3 ? String(args[2]) : undefined
const name = toUSVString(args[0])
const filename = args.length === 3 ? toUSVString(args[2]) : undefined

// 1. Let value be value if given; otherwise blobValue.
const value = args[1] instanceof Blob ? args[1] : String(args[1])
const value = args[1] instanceof Blob ? args[1] : toUSVString(args[1])

// 2. Let entry be the result of creating an entry with
// name, value, and filename if given.
Expand All @@ -53,7 +53,7 @@ class FormData {
`Failed to execute 'delete' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}
const name = String(args[0])
const name = toUSVString(args[0])

// The delete(name) method steps are to remove all entries whose name
// is name from this’s entry list.
Expand All @@ -76,7 +76,7 @@ class FormData {
`Failed to execute 'get' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}
const name = String(args[0])
const name = toUSVString(args[0])

// 1. If there is no entry whose name is name in this’s entry list,
// then return null.
Expand All @@ -99,7 +99,7 @@ class FormData {
`Failed to execute 'getAll' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}
const name = String(args[0])
const name = toUSVString(args[0])

// 1. If there is no entry whose name is name in this’s entry list,
// then return the empty list.
Expand All @@ -119,7 +119,7 @@ class FormData {
`Failed to execute 'has' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}
const name = String(args[0])
const name = toUSVString(args[0])

// The has(name) method steps are to return true if there is an entry
// whose name is name in this’s entry list; otherwise false.
Expand All @@ -140,14 +140,14 @@ class FormData {
"Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'"
)
}
const name = String(args[0])
const filename = args.length === 3 ? String(args[2]) : undefined
const name = toUSVString(args[0])
const filename = args.length === 3 ? toUSVString(args[2]) : undefined

// The set(name, value) and set(name, blobValue, filename) method steps
// are:

// 1. Let value be value if given; otherwise blobValue.
const value = args[1] instanceof Blob ? args[1] : String(args[1])
const value = args[1] instanceof Blob ? args[1] : toUSVString(args[1])

// 2. Let entry be the result of creating an entry with name, value, and
// filename if given.
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
const { extractBody, mixinBody, cloneBody } = require('./body')
const { Headers, fill: fillHeaders, HeadersList } = require('./headers')
const util = require('../core/util')
const { isValidHTTPToken, EnvironmentSettingsObject } = require('./util')
const { isValidHTTPToken, EnvironmentSettingsObject, toUSVString } = require('./util')
const {
forbiddenMethods,
corsSafeListedMethods,
Expand Down Expand Up @@ -46,7 +46,7 @@ class Request {
"Failed to construct 'Request': cannot convert to dictionary."
)
}
const input = args[0] instanceof Request ? args[0] : String(args[0])
const input = args[0] instanceof Request ? args[0] : toUSVString(args[0])
const init = args.length >= 1 ? args[1] ?? {} : {}

// TODO
Expand Down
4 changes: 2 additions & 2 deletions lib/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Headers, HeadersList, fill } = require('./headers')
const { extractBody, cloneBody, mixinBody } = require('./body')
const util = require('../core/util')
const { kEnumerableProperty } = util
const { responseURL, isValidReasonPhrase } = require('./util')
const { responseURL, isValidReasonPhrase, toUSVString } = require('./util')
const {
redirectStatus,
nullBodyStatus,
Expand Down Expand Up @@ -44,7 +44,7 @@ class Response {
}

const status = args.length >= 2 ? args[1] : 302
const url = String(args[0])
const url = toUSVString(args[0])

// 1. Let parsedURL be the result of parsing url with current settings
// object’s API base URL.
Expand Down
18 changes: 18 additions & 0 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,23 @@ function tryUpgradeRequestToAPotentiallyTrustworthyURL (request) {
// TODO
}

function _toUSVString (val) {
// TODO: This is internal to node core...
return val
ronag marked this conversation as resolved.
Show resolved Hide resolved
}

const unpairedSurrogateRe =
/(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])/

// https://github.com/nodejs/node/blob/7ca38f05a023666274569343f128c5aed81599f3/lib/internal/url.js#L144
function toUSVString (val) {
const str = `${val}`
// As of V8 5.5, `str.search()` (and `unpairedSurrogateRe[@@search]()`) are
// slower than `unpairedSurrogateRe.exec()`.
const match = unpairedSurrogateRe.exec(str)
return match ? _toUSVString(str, match.index) : str
}

class ServiceWorkerGlobalScope {} // dummy
class Window {} // dummy
class EnvironmentSettingsObject {} // dummy
Expand All @@ -290,6 +307,7 @@ module.exports = {
ServiceWorkerGlobalScope,
Window,
EnvironmentSettingsObject,
toUSVString,
tryUpgradeRequestToAPotentiallyTrustworthyURL,
coarsenedSharedCurrentTime,
matchRequestIntegrity,
Expand Down