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

feat: implement FormData Iterator #1473

Merged
merged 3 commits into from Jun 12, 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
71 changes: 54 additions & 17 deletions lib/fetch/formdata.js
@@ -1,6 +1,6 @@
'use strict'

const { isBlobLike, isFileLike, toUSVString } = require('./util')
const { isBlobLike, isFileLike, toUSVString, makeIterator } = require('./util')
const { kState } = require('./symbols')
const { File, FileLike } = require('./file')
const { Blob } = require('buffer')
Expand Down Expand Up @@ -187,45 +187,68 @@ class FormData {
return this.constructor.name
}

* entries () {
entries () {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

for (const pair of this) {
yield pair
}
return makeIterator(
makeIterable(this[kState], 'entries'),
'FormData'
)
}

* keys () {
keys () {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

for (const [key] of this) {
yield key
return makeIterator(
makeIterable(this[kState], 'keys'),
'FormData'
)
}

values () {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

return makeIterator(
makeIterable(this[kState], 'values'),
'FormData'
)
}

* values () {
/**
* @param {(value: string, key: string, self: FormData) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

for (const [, value] of this) {
yield value
if (arguments.length < 1) {
throw new TypeError(
`Failed to execute 'forEach' on 'FormData': 1 argument required, but only ${arguments.length} present.`
)
}
}

* [Symbol.iterator] () {
// The value pairs to iterate over are this’s entry list’s entries with
// the key being the name and the value being the value.
for (const { name, value } of this[kState]) {
yield [name, value]
if (typeof callbackFn !== 'function') {
throw new TypeError(
"Failed to execute 'forEach' on 'FormData': parameter 1 is not of type 'Function'."
)
}

for (const [key, value] of this) {
callbackFn.apply(thisArg, [value, key, this])
}
}
}

FormData.prototype[Symbol.iterator] = FormData.prototype.entries

function makeEntry (name, value, filename) {
// To create an entry for name, value, and optionally a filename, run these
// steps:
Expand Down Expand Up @@ -267,4 +290,18 @@ function makeEntry (name, value, filename) {
return entry
}

function * makeIterable (entries, type) {
// The value pairs to iterate over are this’s entry list’s entries
// with the key being the name and the value being the value.
for (const { name, value } of entries) {
if (type === 'entries') {
yield [name, value]
} else if (type === 'values') {
yield value
} else {
yield name
}
}
}

module.exports = { FormData }
34 changes: 4 additions & 30 deletions lib/fetch/headers.js
Expand Up @@ -6,6 +6,7 @@ const { validateHeaderName, validateHeaderValue } = require('http')
const { kHeadersList } = require('../core/symbols')
const { kGuard } = require('./symbols')
const { kEnumerableProperty } = require('../core/util')
const { makeIterator } = require('./util')

const kHeadersMap = Symbol('headers map')
const kHeadersSortedMap = Symbol('headers map sorted')
Expand Down Expand Up @@ -73,33 +74,6 @@ function fill (headers, object) {
}
}

// https://tc39.es/ecma262/#sec-%25iteratorprototype%25-object
const esIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))

// https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object
function makeHeadersIterator (iterator) {
const i = {
next () {
if (Object.getPrototypeOf(this) !== i) {
throw new TypeError(
'\'next\' called on an object that does not implement interface Headers Iterator.'
)
}

return iterator.next()
},
// The class string of an iterator prototype object for a given interface is the
// result of concatenating the identifier of the interface and the string " Iterator".
[Symbol.toStringTag]: 'Headers Iterator'
}

// The [[Prototype]] internal slot of an iterator prototype object must be %IteratorPrototype%.
Object.setPrototypeOf(i, esIteratorPrototype)
// esIteratorPrototype needs to be the prototype of i
// which is the prototype of an empty object. Yes, it's confusing.
return Object.setPrototypeOf({}, i)
}

class HeadersList {
constructor (init) {
if (init instanceof HeadersList) {
Expand Down Expand Up @@ -306,23 +280,23 @@ class Headers {
throw new TypeError('Illegal invocation')
}

return makeHeadersIterator(this[kHeadersSortedMap].keys())
return makeIterator(this[kHeadersSortedMap].keys(), 'Headers')
}

values () {
if (!(this instanceof Headers)) {
throw new TypeError('Illegal invocation')
}

return makeHeadersIterator(this[kHeadersSortedMap].values())
return makeIterator(this[kHeadersSortedMap].values(), 'Headers')
}

entries () {
if (!(this instanceof Headers)) {
throw new TypeError('Illegal invocation')
}

return makeHeadersIterator(this[kHeadersSortedMap].entries())
return makeIterator(this[kHeadersSortedMap].entries(), 'Headers')
}

/**
Expand Down
30 changes: 29 additions & 1 deletion lib/fetch/util.js
Expand Up @@ -361,6 +361,33 @@ function serializeJavascriptValueToJSONString (value) {
return result
}

// https://tc39.es/ecma262/#sec-%25iteratorprototype%25-object
const esIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()))

// https://webidl.spec.whatwg.org/#dfn-iterator-prototype-object
function makeIterator (iterator, name) {
const i = {
next () {
if (Object.getPrototypeOf(this) !== i) {
throw new TypeError(
`'next' called on an object that does not implement interface ${name} Iterator.`
)
}

return iterator.next()
},
// The class string of an iterator prototype object for a given interface is the
// result of concatenating the identifier of the interface and the string " Iterator".
[Symbol.toStringTag]: `${name} Iterator`
}

// The [[Prototype]] internal slot of an iterator prototype object must be %IteratorPrototype%.
Object.setPrototypeOf(i, esIteratorPrototype)
// esIteratorPrototype needs to be the prototype of i
// which is the prototype of an empty object. Yes, it's confusing.
return Object.setPrototypeOf({}, i)
}

module.exports = {
isAborted,
isCancelled,
Expand Down Expand Up @@ -390,5 +417,6 @@ module.exports = {
isValidReasonPhrase,
sameOrigin,
normalizeMethod,
serializeJavascriptValueToJSONString
serializeJavascriptValueToJSONString,
makeIterator
}
59 changes: 59 additions & 0 deletions test/fetch/formdata.js
Expand Up @@ -213,6 +213,65 @@ test('formData.values', (t) => {
})
})

test('formData forEach', (t) => {
t.test('invalid arguments', (t) => {
t.throws(() => {
FormData.prototype.forEach.call({})
}, TypeError('Illegal invocation'))

t.throws(() => {
const fd = new FormData()

fd.forEach({})
}, TypeError)

t.end()
})

t.test('with a callback', (t) => {
const fd = new FormData()

fd.set('a', 'b')
fd.set('c', 'd')

let i = 0
fd.forEach((value, key, self) => {
if (i++ === 0) {
t.equal(value, 'b')
t.equal(key, 'a')
} else {
t.equal(value, 'd')
t.equal(key, 'c')
}

t.equal(fd, self)
})

t.end()
})

t.test('with a thisArg', (t) => {
const fd = new FormData()
fd.set('b', 'a')

fd.forEach(function (value, key, self) {
t.equal(this, globalThis)
t.equal(fd, self)
t.equal(key, 'b')
t.equal(value, 'a')
})

const thisArg = Symbol('thisArg')
fd.forEach(function () {
t.equal(this, thisArg)
}, thisArg)

t.end()
})

t.end()
})

test('formData toStringTag', (t) => {
const form = new FormData()
t.equal(form[Symbol.toStringTag], 'FormData')
Expand Down
71 changes: 0 additions & 71 deletions test/fetch/headers-iterator.js

This file was deleted.