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 decoding #1743

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
6 changes: 3 additions & 3 deletions lib/fetch/dataURL.js
@@ -1,6 +1,6 @@
const assert = require('assert')
const { atob } = require('buffer')
const { isValidHTTPToken } = require('./util')
const { isValidHTTPToken, isomorphicDecode } = require('./util')

const encoder = new TextEncoder()

Expand Down Expand Up @@ -54,15 +54,15 @@ function dataURLProcessor (dataURL) {
const encodedBody = input.slice(mimeTypeLength + 1)

// 10. Let body be the percent-decoding of encodedBody.
/** @type {Uint8Array|string} */
let body = stringPercentDecode(encodedBody)

// 11. If mimeType ends with U+003B (;), followed by
// zero or more U+0020 SPACE, followed by an ASCII
// case-insensitive match for "base64", then:
if (/;(\u0020){0,}base64$/i.test(mimeType)) {
// 1. Let stringBody be the isomorphic decode of body.
const stringBody = decodeURIComponent(new TextDecoder('utf-8').decode(body))
const stringBody = isomorphicDecode(body)

// 2. Set body to the forgiving-base64 decode of
// stringBody.
body = forgivingBase64(stringBody)
Expand Down
20 changes: 19 additions & 1 deletion lib/fetch/util.js
Expand Up @@ -856,6 +856,23 @@ function isReadableStreamLike (stream) {
)
}

/**
* @see https://infra.spec.whatwg.org/#isomorphic-decode
* @param {number[]|Uint8Array} input
*/
function isomorphicDecode (input) {
// 1. To isomorphic decode a byte sequence input, return a string whose code point
// length is equal to input’s length and whose code points have the same values
// as the values of input’s bytes, in the same order.
let output = ''

for (let i = 0; i < input.length; i++) {
output += String.fromCharCode(input[i])
}

return output
}

/**
* @param {ReadableStreamController<Uint8Array>} controller
*/
Expand Down Expand Up @@ -929,5 +946,6 @@ module.exports = {
bytesMatch,
isReadableStreamLike,
readableStreamClose,
isomorphicEncode
isomorphicEncode,
isomorphicDecode
}