Skip to content

Commit

Permalink
fix(fetch): remove brand checks from Symbol.toStringTag (nodejs#1436)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 6378696 commit 796b91e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
8 changes: 0 additions & 8 deletions lib/fetch/file.js
Expand Up @@ -69,10 +69,6 @@ class File extends Blob {
}

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

return this.constructor.name
}
}
Expand Down Expand Up @@ -190,10 +186,6 @@ class FileLike {
}

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

return 'File'
}
}
Expand Down
6 changes: 1 addition & 5 deletions lib/fetch/formdata.js
Expand Up @@ -182,10 +182,6 @@ class FormData {
}

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

return this.constructor.name
}

Expand Down Expand Up @@ -269,4 +265,4 @@ function makeEntry (name, value, filename) {
return entry
}

module.exports = { FormData: globalThis.FormData ?? FormData }
module.exports = { FormData }
4 changes: 0 additions & 4 deletions lib/fetch/request.js
Expand Up @@ -516,10 +516,6 @@ class Request {
}

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

return this.constructor.name
}

Expand Down
4 changes: 0 additions & 4 deletions lib/fetch/response.js
Expand Up @@ -178,10 +178,6 @@ class Response {
}

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

return this.constructor.name
}

Expand Down
13 changes: 8 additions & 5 deletions test/fetch/file.js
Expand Up @@ -4,16 +4,16 @@ const { test } = require('tap')
const { File, FileLike } = require('../../lib/fetch/file')

test('args validation', (t) => {
t.plan(12)
t.plan(14)

t.throws(() => {
File.prototype.name.call(null)
}, TypeError)
t.throws(() => {
File.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
File.prototype[Symbol.toStringTag].call(null)
t.doesNotThrow(() => {
File.prototype[Symbol.toStringTag].charAt(0)
}, TypeError)

t.throws(() => {
Expand All @@ -40,9 +40,12 @@ test('args validation', (t) => {
t.throws(() => {
FileLike.prototype.lastModified.call(null)
}, TypeError)
t.throws(() => {
FileLike.prototype[Symbol.toStringTag].call(null)
t.doesNotThrow(() => {
FileLike.prototype[Symbol.toStringTag].charAt(0)
}, TypeError)

t.equal(File.prototype[Symbol.toStringTag], 'File')
t.equal(FileLike.prototype[Symbol.toStringTag], 'File')
})

test('return value of File.lastModified', (t) => {
Expand Down
6 changes: 6 additions & 0 deletions test/fetch/formdata.js
Expand Up @@ -73,6 +73,11 @@ test('arg validation', (t) => {
Reflect.apply(FormData.prototype[Symbol.iterator], null)
}, TypeError)

// toStringTag
t.doesNotThrow(() => {
FormData.prototype[Symbol.toStringTag].charAt(0)
})

t.end()
})

Expand Down Expand Up @@ -208,5 +213,6 @@ test('formData.values', (t) => {
test('formData toStringTag', (t) => {
const form = new FormData()
t.equal(form[Symbol.toStringTag], 'FormData')
t.equal(FormData.prototype[Symbol.toStringTag], 'FormData')
t.end()
})
12 changes: 12 additions & 0 deletions test/fetch/request.js
Expand Up @@ -149,6 +149,10 @@ test('arg validation', (t) => {
Request.prototype.clone.call(null)
}, TypeError)

t.doesNotThrow(() => {
Request.prototype[Symbol.toStringTag].charAt(0)
})

t.end()
})

Expand Down Expand Up @@ -327,3 +331,11 @@ test('Passing headers in init', (t) => {

t.end()
})

test('Symbol.toStringTag', (t) => {
const req = new Request('http://localhost')

t.equal(req[Symbol.toStringTag], 'Request')
t.equal(Request.prototype[Symbol.toStringTag], 'Request')
t.end()
})
12 changes: 10 additions & 2 deletions test/fetch/response.js
Expand Up @@ -45,8 +45,8 @@ test('arg validation', (t) => {
}, TypeError)
}

t.throws(() => {
Response.prototype[Symbol.toStringTag].call(null)
t.doesNotThrow(() => {
Response.prototype[Symbol.toStringTag].charAt(0)
}, TypeError)

t.throws(() => {
Expand Down Expand Up @@ -94,3 +94,11 @@ test('response clone', (t) => {
t.equal(response2.body, null)
t.end()
})

test('Symbol.toStringTag', (t) => {
const resp = new Response()

t.equal(resp[Symbol.toStringTag], 'Response')
t.equal(Response.prototype[Symbol.toStringTag], 'Response')
t.end()
})

0 comments on commit 796b91e

Please sign in to comment.