Skip to content

Commit 1ae6d79

Browse files
authored
fix(fetch): remove brand checks from Symbol.toStringTag (nodejs#1436)
1 parent 38ccc5d commit 1ae6d79

File tree

8 files changed

+37
-28
lines changed

8 files changed

+37
-28
lines changed

lib/fetch/file.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ class File extends Blob {
6969
}
7070

7171
get [Symbol.toStringTag] () {
72-
if (!(this instanceof File)) {
73-
throw new TypeError('Illegal invocation')
74-
}
75-
7672
return this.constructor.name
7773
}
7874
}
@@ -190,10 +186,6 @@ class FileLike {
190186
}
191187

192188
get [Symbol.toStringTag] () {
193-
if (!(this instanceof FileLike)) {
194-
throw new TypeError('Illegal invocation')
195-
}
196-
197189
return 'File'
198190
}
199191
}

lib/fetch/formdata.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,6 @@ class FormData {
182182
}
183183

184184
get [Symbol.toStringTag] () {
185-
if (!(this instanceof FormData)) {
186-
throw new TypeError('Illegal invocation')
187-
}
188-
189185
return this.constructor.name
190186
}
191187

@@ -269,4 +265,4 @@ function makeEntry (name, value, filename) {
269265
return entry
270266
}
271267

272-
module.exports = { FormData: globalThis.FormData ?? FormData }
268+
module.exports = { FormData }

lib/fetch/request.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,6 @@ class Request {
516516
}
517517

518518
get [Symbol.toStringTag] () {
519-
if (!(this instanceof Request)) {
520-
throw new TypeError('Illegal invocation')
521-
}
522-
523519
return this.constructor.name
524520
}
525521

lib/fetch/response.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,6 @@ class Response {
178178
}
179179

180180
get [Symbol.toStringTag] () {
181-
if (!(this instanceof Response)) {
182-
throw new TypeError('Illegal invocation')
183-
}
184-
185181
return this.constructor.name
186182
}
187183

test/fetch/file.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ const { test } = require('tap')
44
const { File, FileLike } = require('../../lib/fetch/file')
55

66
test('args validation', (t) => {
7-
t.plan(12)
7+
t.plan(14)
88

99
t.throws(() => {
1010
File.prototype.name.call(null)
1111
}, TypeError)
1212
t.throws(() => {
1313
File.prototype.lastModified.call(null)
1414
}, TypeError)
15-
t.throws(() => {
16-
File.prototype[Symbol.toStringTag].call(null)
15+
t.doesNotThrow(() => {
16+
File.prototype[Symbol.toStringTag].charAt(0)
1717
}, TypeError)
1818

1919
t.throws(() => {
@@ -40,9 +40,12 @@ test('args validation', (t) => {
4040
t.throws(() => {
4141
FileLike.prototype.lastModified.call(null)
4242
}, TypeError)
43-
t.throws(() => {
44-
FileLike.prototype[Symbol.toStringTag].call(null)
43+
t.doesNotThrow(() => {
44+
FileLike.prototype[Symbol.toStringTag].charAt(0)
4545
}, TypeError)
46+
47+
t.equal(File.prototype[Symbol.toStringTag], 'File')
48+
t.equal(FileLike.prototype[Symbol.toStringTag], 'File')
4649
})
4750

4851
test('return value of File.lastModified', (t) => {

test/fetch/formdata.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ test('arg validation', (t) => {
7373
Reflect.apply(FormData.prototype[Symbol.iterator], null)
7474
}, TypeError)
7575

76+
// toStringTag
77+
t.doesNotThrow(() => {
78+
FormData.prototype[Symbol.toStringTag].charAt(0)
79+
})
80+
7681
t.end()
7782
})
7883

@@ -208,5 +213,6 @@ test('formData.values', (t) => {
208213
test('formData toStringTag', (t) => {
209214
const form = new FormData()
210215
t.equal(form[Symbol.toStringTag], 'FormData')
216+
t.equal(FormData.prototype[Symbol.toStringTag], 'FormData')
211217
t.end()
212218
})

test/fetch/request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ test('arg validation', (t) => {
149149
Request.prototype.clone.call(null)
150150
}, TypeError)
151151

152+
t.doesNotThrow(() => {
153+
Request.prototype[Symbol.toStringTag].charAt(0)
154+
})
155+
152156
t.end()
153157
})
154158

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

328332
t.end()
329333
})
334+
335+
test('Symbol.toStringTag', (t) => {
336+
const req = new Request('http://localhost')
337+
338+
t.equal(req[Symbol.toStringTag], 'Request')
339+
t.equal(Request.prototype[Symbol.toStringTag], 'Request')
340+
t.end()
341+
})

test/fetch/response.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ test('arg validation', (t) => {
4545
}, TypeError)
4646
}
4747

48-
t.throws(() => {
49-
Response.prototype[Symbol.toStringTag].call(null)
48+
t.doesNotThrow(() => {
49+
Response.prototype[Symbol.toStringTag].charAt(0)
5050
}, TypeError)
5151

5252
t.throws(() => {
@@ -94,3 +94,11 @@ test('response clone', (t) => {
9494
t.equal(response2.body, null)
9595
t.end()
9696
})
97+
98+
test('Symbol.toStringTag', (t) => {
99+
const resp = new Response()
100+
101+
t.equal(resp[Symbol.toStringTag], 'Response')
102+
t.equal(Response.prototype[Symbol.toStringTag], 'Response')
103+
t.end()
104+
})

0 commit comments

Comments
 (0)