1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
'use strict'
const { isBlobLike, toUSVString, makeIterator } = require('./util')
const { kState } = require('./symbols')
const { File: UndiciFile, FileLike, isFileLike } = require('./file')
const { webidl } = require('./webidl')
const { Blob, File: NativeFile } = require('buffer')
/** @type {globalThis['File']} */
const File = NativeFile ?? UndiciFile
// https://xhr.spec.whatwg.org/#formdata
class FormData {
constructor (form) {
if (form !== undefined) {
throw webidl.errors.conversionFailed({
prefix: 'FormData constructor',
argument: 'Argument 1',
types: ['undefined']
})
}
this[kState] = []
}
append (name, value, filename = undefined) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 2, { header: 'FormData.append' })
if (arguments.length === 3 && !isBlobLike(value)) {
throw new TypeError(
"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'"
)
}
// 1. Let value be value if given; otherwise blobValue.
name = webidl.converters.USVString(name)
value = isBlobLike(value)
? webidl.converters.Blob(value, { strict: false })
: webidl.converters.USVString(value)
filename = arguments.length === 3
? webidl.converters.USVString(filename)
: undefined
// 2. Let entry be the result of creating an entry with
// name, value, and filename if given.
const entry = makeEntry(name, value, filename)
// 3. Append entry to this’s entry list.
this[kState].push(entry)
}
delete (name) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.delete' })
name = webidl.converters.USVString(name)
// The delete(name) method steps are to remove all entries whose name
// is name from this’s entry list.
this[kState] = this[kState].filter(entry => entry.name !== name)
}
get (name) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.get' })
name = webidl.converters.USVString(name)
// 1. If there is no entry whose name is name in this’s entry list,
// then return null.
const idx = this[kState].findIndex((entry) => entry.name === name)
if (idx === -1) {
return null
}
// 2. Return the value of the first entry whose name is name from
// this’s entry list.
return this[kState][idx].value
}
getAll (name) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.getAll' })
name = webidl.converters.USVString(name)
// 1. If there is no entry whose name is name in this’s entry list,
// then return the empty list.
// 2. Return the values of all entries whose name is name, in order,
// from this’s entry list.
return this[kState]
.filter((entry) => entry.name === name)
.map((entry) => entry.value)
}
has (name) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.has' })
name = webidl.converters.USVString(name)
// 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.
return this[kState].findIndex((entry) => entry.name === name) !== -1
}
set (name, value, filename = undefined) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 2, { header: 'FormData.set' })
if (arguments.length === 3 && !isBlobLike(value)) {
throw new TypeError(
"Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'"
)
}
// The set(name, value) and set(name, blobValue, filename) method steps
// are:
// 1. Let value be value if given; otherwise blobValue.
name = webidl.converters.USVString(name)
value = isBlobLike(value)
? webidl.converters.Blob(value, { strict: false })
: webidl.converters.USVString(value)
filename = arguments.length === 3
? toUSVString(filename)
: undefined
// 2. Let entry be the result of creating an entry with name, value, and
// filename if given.
const entry = makeEntry(name, value, filename)
// 3. If there are entries in this’s entry list whose name is name, then
// replace the first such entry with entry and remove the others.
const idx = this[kState].findIndex((entry) => entry.name === name)
if (idx !== -1) {
this[kState] = [
...this[kState].slice(0, idx),
entry,
...this[kState].slice(idx + 1).filter((entry) => entry.name !== name)
]
} else {
// 4. Otherwise, append entry to this’s entry list.
this[kState].push(entry)
}
}
entries () {
webidl.brandCheck(this, FormData)
return makeIterator(
() => this[kState].map(pair => [pair.name, pair.value]),
'FormData',
'key+value'
)
}
keys () {
webidl.brandCheck(this, FormData)
return makeIterator(
() => this[kState].map(pair => [pair.name, pair.value]),
'FormData',
'key'
)
}
values () {
webidl.brandCheck(this, FormData)
return makeIterator(
() => this[kState].map(pair => [pair.name, pair.value]),
'FormData',
'value'
)
}
/**
* @param {(value: string, key: string, self: FormData) => void} callbackFn
* @param {unknown} thisArg
*/
forEach (callbackFn, thisArg = globalThis) {
webidl.brandCheck(this, FormData)
webidl.argumentLengthCheck(arguments, 1, { header: 'FormData.forEach' })
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
Object.defineProperties(FormData.prototype, {
[Symbol.toStringTag]: {
value: 'FormData',
configurable: true
}
})
/**
* @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#create-an-entry
* @param {string} name
* @param {string|Blob} value
* @param {?string} filename
* @returns
*/
function makeEntry (name, value, filename) {
// 1. Set name to the result of converting name into a scalar value string.
// "To convert a string into a scalar value string, replace any surrogates
// with U+FFFD."
// see: https://nodejs.org/dist/latest-v18.x/docs/api/buffer.html#buftostringencoding-start-end
name = Buffer.from(name).toString('utf8')
// 2. If value is a string, then set value to the result of converting
// value into a scalar value string.
if (typeof value === 'string') {
value = Buffer.from(value).toString('utf8')
} else {
// 3. Otherwise:
// 1. If value is not a File object, then set value to a new File object,
// representing the same bytes, whose name attribute value is "blob"
if (!isFileLike(value)) {
value = value instanceof Blob
? new File([value], 'blob', { type: value.type })
: new FileLike(value, 'blob', { type: value.type })
}
// 2. If filename is given, then set value to a new File object,
// representing the same bytes, whose name attribute is filename.
if (filename !== undefined) {
/** @type {FilePropertyBag} */
const options = {
type: value.type,
lastModified: value.lastModified
}
value = (NativeFile && value instanceof NativeFile) || value instanceof UndiciFile
? new File([value], filename, options)
: new FileLike(value, filename, options)
}
}
// 4. Return an entry whose name is name and whose value is value.
return { name, value }
}
module.exports = { FormData }
|