A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/node-fetch/node-fetch/commit/29909d75c62d51e0d1c23758e526dba74bfd463d below:

handle bom in text and json (#1739) · node-fetch/node-fetch@29909d7 · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+20

-5

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+20

-5

lines changed Original file line number Diff line number Diff line change

@@ -39,7 +39,7 @@

39 39

"dependencies": {

40 40

"whatwg-url": "^5.0.0"

41 41

},

42 -

"peerDependencies": {

42 +

"peerDependencies": {

43 43

"encoding": "^0.1.0"

44 44

},

45 45

"peerDependenciesMeta": {

Original file line number Diff line number Diff line change

@@ -114,9 +114,9 @@ Body.prototype = {

114 114

* @return Promise

115 115

*/

116 116

json() {

117 -

return consumeBody.call(this).then((buffer) => {

118 -

try {

119 -

return JSON.parse(buffer.toString());

117 +

return this.text().then((text) => {

118 +

try{

119 +

return JSON.parse(text);

120 120

} catch (err) {

121 121

return Body.Promise.reject(new FetchError(`invalid json response body at ${this.url} reason: ${err.message}`, 'invalid-json'));

122 122

}

@@ -129,7 +129,7 @@ Body.prototype = {

129 129

* @return Promise

130 130

*/

131 131

text() {

132 -

return consumeBody.call(this).then(buffer => buffer.toString());

132 +

return consumeBody.call(this).then(buffer => new TextDecoder().decode(buffer));

133 133

},

134 134 135 135

/**

Original file line number Diff line number Diff line change

@@ -2479,6 +2479,21 @@ describe('Response', function () {

2479 2479

expect(res.headers.get('a')).to.equal('1');

2480 2480

});

2481 2481 2482 +

it('should decode responses containing BOM to json', async () => {

2483 +

const json = await new Response('\uFEFF{"a":1}').json();

2484 +

expect(json.a).to.equal(1);

2485 +

});

2486 + 2487 +

it('should decode responses containing BOM to text', async () => {

2488 +

const text = await new Response('\uFEFF{"a":1}').text();

2489 +

expect(text).to.equal('{"a":1}');

2490 +

});

2491 + 2492 +

it('should keep BOM when getting raw bytes', async () => {

2493 +

const ab = await new Response('\uFEFF{"a":1}').arrayBuffer();

2494 +

expect(ab.byteLength).to.equal(10);

2495 +

});

2496 + 2482 2497

it('should support text() method', function() {

2483 2498

const res = new Response('a=1');

2484 2499

return res.text().then(result => {

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4