Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert handle bom in text and json #1741

Merged
merged 1 commit into from May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -39,7 +39,7 @@
"dependencies": {
"whatwg-url": "^5.0.0"
},
"peerDependencies": {
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
Expand Down
8 changes: 4 additions & 4 deletions src/body.js
Expand Up @@ -114,9 +114,9 @@ Body.prototype = {
* @return Promise
*/
json() {
return this.text().then((text) => {
try{
return JSON.parse(text);
return consumeBody.call(this).then((buffer) => {
try {
return JSON.parse(buffer.toString());
} catch (err) {
return Body.Promise.reject(new FetchError(`invalid json response body at ${this.url} reason: ${err.message}`, 'invalid-json'));
}
Expand All @@ -129,7 +129,7 @@ Body.prototype = {
* @return Promise
*/
text() {
return consumeBody.call(this).then(buffer => new TextDecoder().decode(buffer));
return consumeBody.call(this).then(buffer => buffer.toString());
},

/**
Expand Down
15 changes: 0 additions & 15 deletions test/test.js
Expand Up @@ -2479,21 +2479,6 @@ describe('Response', function () {
expect(res.headers.get('a')).to.equal('1');
});

it('should decode responses containing BOM to json', async () => {
const json = await new Response('\uFEFF{"a":1}').json();
expect(json.a).to.equal(1);
});

it('should decode responses containing BOM to text', async () => {
const text = await new Response('\uFEFF{"a":1}').text();
expect(text).to.equal('{"a":1}');
});

it('should keep BOM when getting raw bytes', async () => {
const ab = await new Response('\uFEFF{"a":1}').arrayBuffer();
expect(ab.byteLength).to.equal(10);
});

it('should support text() method', function() {
const res = new Response('a=1');
return res.text().then(result => {
Expand Down