From 29909d75c62d51e0d1c23758e526dba74bfd463d Mon Sep 17 00:00:00 2001 From: Alexis Clarembeau Date: Mon, 8 May 2023 18:19:42 +0200 Subject: [PATCH] fix: handle bom in text and json (#1739) * fix: handle bom in text and json * add unit tests --- package.json | 2 +- src/body.js | 8 ++++---- test/test.js | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 66f59adc6..92f958869 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "dependencies": { "whatwg-url": "^5.0.0" }, - "peerDependencies": { + "peerDependencies": { "encoding": "^0.1.0" }, "peerDependenciesMeta": { diff --git a/src/body.js b/src/body.js index a9d2e7973..273044efe 100644 --- a/src/body.js +++ b/src/body.js @@ -114,9 +114,9 @@ Body.prototype = { * @return Promise */ json() { - return consumeBody.call(this).then((buffer) => { - try { - return JSON.parse(buffer.toString()); + return this.text().then((text) => { + try{ + return JSON.parse(text); } catch (err) { return Body.Promise.reject(new FetchError(`invalid json response body at ${this.url} reason: ${err.message}`, 'invalid-json')); } @@ -129,7 +129,7 @@ Body.prototype = { * @return Promise */ text() { - return consumeBody.call(this).then(buffer => buffer.toString()); + return consumeBody.call(this).then(buffer => new TextDecoder().decode(buffer)); }, /** diff --git a/test/test.js b/test/test.js index 21cf24055..0939e0f1f 100644 --- a/test/test.js +++ b/test/test.js @@ -2479,6 +2479,21 @@ 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 => {