Skip to content

Commit

Permalink
fix(fetch): set Symbol.toStringTag properly on classes (nodejs#1742)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent cbe5272 commit dfd9d16
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
8 changes: 4 additions & 4 deletions lib/fetch/file.js
Expand Up @@ -98,10 +98,6 @@ class File extends Blob {

return this[kState].type
}

get [Symbol.toStringTag] () {
return this.constructor.name
}
}

class FileLike {
Expand Down Expand Up @@ -222,6 +218,10 @@ class FileLike {
}

Object.defineProperties(File.prototype, {
[Symbol.toStringTag]: {
value: 'File',
configurable: true
},
name: kEnumerableProperty,
lastModified: kEnumerableProperty
})
Expand Down
13 changes: 7 additions & 6 deletions lib/fetch/formdata.js
Expand Up @@ -8,8 +8,6 @@ const { Blob } = require('buffer')

// https://xhr.spec.whatwg.org/#formdata
class FormData {
static name = 'FormData'

constructor (form) {
if (form !== undefined) {
webidl.errors.conversionFailed({
Expand Down Expand Up @@ -196,10 +194,6 @@ class FormData {
}
}

get [Symbol.toStringTag] () {
return this.constructor.name
}

entries () {
if (!(this instanceof FormData)) {
throw new TypeError('Illegal invocation')
Expand Down Expand Up @@ -265,6 +259,13 @@ class FormData {

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
Expand Down
10 changes: 5 additions & 5 deletions lib/fetch/headers.js
Expand Up @@ -171,10 +171,6 @@ class Headers {
}
}

get [Symbol.toStringTag] () {
return this.constructor.name
}

// https://fetch.spec.whatwg.org/#dom-headers-append
append (name, value) {
if (!(this instanceof Headers)) {
Expand Down Expand Up @@ -481,7 +477,11 @@ Object.defineProperties(Headers.prototype, {
values: kEnumerableProperty,
entries: kEnumerableProperty,
forEach: kEnumerableProperty,
[Symbol.iterator]: { enumerable: false }
[Symbol.iterator]: { enumerable: false },
[Symbol.toStringTag]: {
value: 'Headers',
configurable: true
}
})

webidl.converters.HeadersInit = function (V) {
Expand Down
10 changes: 5 additions & 5 deletions lib/fetch/request.js
Expand Up @@ -522,10 +522,6 @@ class Request {
this[kState].body = finalBody
}

get [Symbol.toStringTag] () {
return this.constructor.name
}

// Returns request’s HTTP method, which is "GET" by default.
get method () {
if (!(this instanceof Request)) {
Expand Down Expand Up @@ -866,7 +862,11 @@ Object.defineProperties(Request.prototype, {
attribute: kEnumerableProperty,
referrerPolicy: kEnumerableProperty,
referrer: kEnumerableProperty,
mode: kEnumerableProperty
mode: kEnumerableProperty,
[Symbol.toStringTag]: {
value: 'Request',
configurable: true
}
})

webidl.converters.Request = webidl.interfaceConverter(
Expand Down
10 changes: 5 additions & 5 deletions lib/fetch/response.js
Expand Up @@ -169,10 +169,6 @@ class Response {
initializeResponse(this, init, bodyWithType)
}

get [Symbol.toStringTag] () {
return this.constructor.name
}

// Returns response’s type, e.g., "cors".
get type () {
if (!(this instanceof Response)) {
Expand Down Expand Up @@ -314,7 +310,11 @@ Object.defineProperties(Response.prototype, {
headers: kEnumerableProperty,
clone: kEnumerableProperty,
body: kEnumerableProperty,
bodyUsed: kEnumerableProperty
bodyUsed: kEnumerableProperty,
[Symbol.toStringTag]: {
value: 'Response',
configurable: true
}
})

Object.defineProperties(Response, {
Expand Down
30 changes: 30 additions & 0 deletions test/fetch/general.js
@@ -0,0 +1,30 @@
'use strict'

const { test } = require('tap')
const {
File,
FormData,
Headers,
Request,
Response
} = require('../../index')

test('Symbol.toStringTag descriptor', (t) => {
for (const cls of [
File,
FormData,
Headers,
Request,
Response
]) {
const desc = Object.getOwnPropertyDescriptor(cls.prototype, Symbol.toStringTag)
t.same(desc, {
value: cls.name,
writable: false,
enumerable: false,
configurable: true
})
}

t.end()
})

0 comments on commit dfd9d16

Please sign in to comment.