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

fetch: implement isomorphic encoding #1741

Merged
merged 1 commit into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/fetch/index.js
Expand Up @@ -36,7 +36,8 @@ const {
isAborted,
isErrorLike,
fullyReadBody,
readableStreamClose
readableStreamClose,
isomorphicEncode
} = require('./util')
const { kState, kHeaders, kGuard, kRealm } = require('./symbols')
const assert = require('assert')
Expand Down Expand Up @@ -832,7 +833,7 @@ async function schemeFetch (fetchParams) {
const body = bodyWithType[0]

// 5. Let length be body’s length, serialized and isomorphic encoded.
const length = `${body.length}`
const length = isomorphicEncode(`${body.length}`)

// 6. Let type be bodyWithType’s type if it is non-null; otherwise the empty byte sequence.
const type = bodyWithType[1] ?? ''
Expand Down Expand Up @@ -1303,8 +1304,7 @@ async function httpNetworkOrCacheFetch (
// 7. If contentLength is non-null, then set contentLengthHeaderValue to
// contentLength, serialized and isomorphic encoded.
if (contentLength != null) {
// TODO: isomorphic encoded
contentLengthHeaderValue = String(contentLength)
contentLengthHeaderValue = isomorphicEncode(`${contentLength}`)
}

// 8. If contentLengthHeaderValue is non-null, then append
Expand All @@ -1327,8 +1327,7 @@ async function httpNetworkOrCacheFetch (
// `Referer`/httpRequest’s referrer, serialized and isomorphic encoded,
// to httpRequest’s header list.
if (httpRequest.referrer instanceof URL) {
// TODO: isomorphic encoded
httpRequest.headersList.append('referer', httpRequest.referrer.href)
httpRequest.headersList.append('referer', isomorphicEncode(httpRequest.referrer.href))
}

// 12. Append a request `Origin` header for httpRequest.
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/response.js
Expand Up @@ -10,7 +10,8 @@ const {
isAborted,
isBlobLike,
serializeJavascriptValueToJSONString,
isErrorLike
isErrorLike,
isomorphicEncode
} = require('./util')
const {
redirectStatus,
Expand Down Expand Up @@ -124,8 +125,7 @@ class Response {
responseObject[kState].status = status

// 6. Let value be parsedURL, serialized and isomorphic encoded.
// TODO: isomorphic encoded?
const value = parsedURL.toString()
const value = isomorphicEncode(URLSerializer(parsedURL))

// 7. Append `Location`/value to responseObject’s response’s header list.
responseObject[kState].headersList.append('location', value)
Expand Down
19 changes: 18 additions & 1 deletion lib/fetch/util.js
Expand Up @@ -870,6 +870,22 @@ function readableStreamClose (controller) {
}
}

/**
* @see https://infra.spec.whatwg.org/#isomorphic-encode
* @param {string} input
*/
function isomorphicEncode (input) {
// 1. Assert: input contains no code points greater than U+00FF.
for (let i = 0; i < input.length; i++) {
assert(input.charCodeAt(i) <= 0xFF)
}

// 2. Return a byte sequence whose length is equal to input’s code
// point length and whose bytes have the same values as the
// values of input’s code points, in the same order
return input
}

/**
* Fetch supports node >= 16.8.0, but Object.hasOwn was added in v16.9.0.
*/
Expand Down Expand Up @@ -912,5 +928,6 @@ module.exports = {
fullyReadBody,
bytesMatch,
isReadableStreamLike,
readableStreamClose
readableStreamClose,
isomorphicEncode
}