Skip to content

Commit

Permalink
fix: some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 27, 2021
1 parent 201343c commit caf9ec3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ const methods = {
throw new TypeError('locked')
}

// NOTE: stream.isDisturbed hasn't landed on Node 16.x yet.
// Compat.
stream[kBodyUsed] = true

for await (const chunk of stream) {
Expand Down
6 changes: 5 additions & 1 deletion lib/fetch/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ class File extends Blob {
}

get [Symbol.toStringTag] () {
return 'File'
if (!(this instanceof File)) {
throw new TypeError('Illegal invocation')
}

return this.constructor.name
}
}

Expand Down
35 changes: 32 additions & 3 deletions lib/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 2) {
throw new TypeError(
`Failed to execute 'append' on 'FormData': 2 arguments required, but only ${args.length} present.`
)
}

if (args.length === 3 && !isBlobLike(args[1])) {
throw new TypeError(
"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'"
)
}

const name = toUSVString(args[0])
const filename = args.length === 3 ? toUSVString(args[2]) : undefined

Expand All @@ -48,11 +51,13 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 1) {
throw new TypeError(
`Failed to execute 'delete' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}

const name = toUSVString(args[0])

// The delete(name) method steps are to remove all entries whose name
Expand All @@ -71,11 +76,13 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 1) {
throw new TypeError(
`Failed to execute 'get' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}

const name = toUSVString(args[0])

// 1. If there is no entry whose name is name in this’s entry list,
Expand All @@ -94,11 +101,13 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 1) {
throw new TypeError(
`Failed to execute 'getAll' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}

const name = toUSVString(args[0])

// 1. If there is no entry whose name is name in this’s entry list,
Expand All @@ -114,11 +123,13 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 1) {
throw new TypeError(
`Failed to execute 'has' on 'FormData': 1 arguments required, but only ${args.length} present.`
)
}

const name = toUSVString(args[0])

// The has(name) method steps are to return true if there is an entry
Expand All @@ -130,11 +141,13 @@ class FormData {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

if (args.length < 2) {
throw new TypeError(
`Failed to execute 'set' on 'FormData': 2 arguments required, but only ${args.length} present.`
)
}

if (args.length === 3 && !isBlobLike(args[1])) {
throw new TypeError(
"Failed to execute 'set' on 'FormData': parameter 2 is not of type 'Blob'"
Expand Down Expand Up @@ -169,22 +182,38 @@ class FormData {
}

get [Symbol.toStringTag] () {
return 'FormData'
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
}

return this.constructor.name
}

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

for (const pair of this) {
yield pair
}
}

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

for (const [key] of this) {
yield key
}
}

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

for (const [, value] of this) {
yield value
}
Expand Down Expand Up @@ -214,7 +243,7 @@ function makeEntry (name, value, filename) {

// 3. If value is a Blob object and not a File object, then set value to a new File
// object, representing the same bytes, whose name attribute value is "blob".
if (isBlobLike(value) && !(isFileLike(value))) {
if (isBlobLike(value) && !isFileLike(value)) {
value = value instanceof Blob
? new File([value], 'blob')
: new FileLike(value, 'blob')
Expand All @@ -227,7 +256,7 @@ function makeEntry (name, value, filename) {
// Do we just override the name attribute? Since it says "if value is (now)"
// does that mean that this lives inside the previous condition? In which case
// creating one more File instance doesn't make much sense....
if (isFileLike(value) && filename) {
if (isFileLike(value) && filename != null) {
value = value instanceof File
? new File([value], filename)
: new FileLike(value, filename)
Expand Down
4 changes: 2 additions & 2 deletions test/fetch/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const { File, FileLike } = require('../../lib/fetch/file')

test('Symbol.toStringTag', (t) => {
t.plan(2)
t.equal(File.prototype[Symbol.toStringTag], 'File')
t.equal(FileLike.prototype[Symbol.toStringTag], 'File')
t.equal(new File()[Symbol.toStringTag], 'File')
t.equal(new FileLike()[Symbol.toStringTag], 'File')
})

0 comments on commit caf9ec3

Please sign in to comment.