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 1 commit
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
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