Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError } = require('../core/errors')
const util = require('../core/util')
const { ReadableStreamFrom } = require('../fetch/util')
const { ReadableStreamFrom, toUSVString } = require('../fetch/util')

let Blob

Expand Down Expand Up @@ -98,27 +98,27 @@ module.exports = class BodyReadable extends Readable {
}

// https://fetch.spec.whatwg.org/#dom-body-text
text () {
return consume(this, 'text')
async text () {
return toUSVString(await consume(this, 'text'))
}

// https://fetch.spec.whatwg.org/#dom-body-json
json () {
async json () {
return consume(this, 'json')
}

// https://fetch.spec.whatwg.org/#dom-body-blob
blob () {
async blob () {
return consume(this, 'blob')
}

// https://fetch.spec.whatwg.org/#dom-body-arraybuffer
arrayBuffer () {
async arrayBuffer () {
return consume(this, 'arrayBuffer')
}

// https://fetch.spec.whatwg.org/#dom-body-formdata
formData () {
async formData () {
// TODO: Implement.
throw new NotSupportedError()
}
Expand Down
19 changes: 10 additions & 9 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 @@ -75,8 +75,9 @@ function extractBody (object, keepalive = false) {
const prefix = `--${boundary}\r\nContent-Disposition: form-data`

/*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
const escape = str => str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22')
const normalizeLinefeeds = value => value.replace(/\r?\n|\r/g, '\r\n')
const escape = (str) =>
str.replace(/\n/g, '%0A').replace(/\r/g, '%0D').replace(/"/g, '%22')
const normalizeLinefeeds = (value) => value.replace(/\r?\n|\r/g, '\r\n')

// Set action to this step: run the multipart/form-data
// encoding algorithm, with object’s entry list and UTF-8.
Expand All @@ -94,7 +95,8 @@ function extractBody (object, keepalive = false) {
yield enc.encode(
prefix +
`; name="${escape(normalizeLinefeeds(name))}"` +
(value.filename ? `; filename="${escape(value.filename)}"` : '') + '\r\n' +
(value.filename ? `; filename="${escape(value.filename)}"` : '') +
'\r\n' +
`Content-Type: ${
value.type || 'application/octet-stream'
}\r\n\r\n`
Expand Down Expand Up @@ -149,14 +151,13 @@ function extractBody (object, keepalive = false) {
)
}

stream = object instanceof ReadableStream
? object
: ReadableStreamFrom(object)
stream =
object instanceof ReadableStream ? object : ReadableStreamFrom(object)
} else {
// 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 +294,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
8 changes: 6 additions & 2 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
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 +50,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
2 changes: 2 additions & 0 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const { redirectStatus } = require('./constants')
const { performance } = require('perf_hooks')
const nodeUtil = require('util')

let ReadableStream

Expand Down Expand Up @@ -290,6 +291,7 @@ module.exports = {
ServiceWorkerGlobalScope,
Window,
EnvironmentSettingsObject,
toUSVString: nodeUtil.toUSVString || ((val) => `${val}`),
tryUpgradeRequestToAPotentiallyTrustworthyURL,
coarsenedSharedCurrentTime,
matchRequestIntegrity,
Expand Down