10000 core: Don't use global buffer (#1422) · node-fetch/node-fetch@1493d04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1493d04

Browse files
authored
core: Don't use global buffer (#1422)
* remove unused file * two test is coveraged by the Uint8Array test * use arrayBuffer to test base64 instead * avoid testing buffer * avoid using Buffer * import buffer module * use one same textEncoder * import stream consumer that can test iterable objects * fix a test * fix test where type should be empty
1 parent eb33090 commit 1493d04

File tree

10 files changed

+47
-108
lines changed

10 files changed

+47
-108
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@
5858
"formdata-node": "^4.2.4",
5959
"mocha": "^9.1.3",
6060
"p-timeout": "^5.0.0",
61+
"stream-consumers": "^1.0.1",
6162
"tsd": "^0.14.0",
6263
"xo": "^0.39.1"
6364
},
6465
"dependencies": {
6566
"data-uri-to-buffer": "^4.0.0",
66-
"formdata-polyfill": "^4.0.10",
67-
"fetch-blob": "^3.1.3"
67+
"fetch-blob": "^3.1.3",
68+
"formdata-polyfill": "^4.0.10"
6869
},
6970
"tsd": {
7071
"cwd": "@types",

src/body.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import Stream, {PassThrough} from 'node:stream';
99
import {types, deprecate, promisify} from 'node:util';
10+
import {Buffer} from 'node:buffer';
1011

1112
import Blob from 'fetch-blob';
1213
import {FormData, formDataToBlob} from 'formdata-polyfill/esm.min.js';

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import http from 'node:http';
1010
import https from 'node:https';
1111
import zlib from 'node:zlib';
1212
import Stream, {PassThrough, pipeline as pump} from 'node:stream';
13+
import {Buffer} from 'node:buffer';
14+
1315
import dataUriToBuffer from 'data-uri-to-buffer';
1416

1517
import {writeToStream, clone} from './body.js';

test/external-encoding.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ const {expect} = chai;
55

66
describe('external encoding', () => {
77
describe('data uri', () => {
8-
it('should accept base64-encoded gif data uri', () => {
9-
return fetch('data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=').then(r => {
10-
expect(r.status).to.equal(200);
11-
expect(r.headers.get('Content-Type')).to.equal('image/gif');
12-
13-
return r.buffer().then(b => {
14-
expect(b).to.be.an.instanceOf(Buffer);
15-
});
16-
});
8+
it('should accept base64-encoded gif data uri', async () => {
9+
const b64 = 'data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';
10+
const res = await fetch(b64);
11+
expect(res.status).to.equal(200);
12+
expect(res.headers.get('Content-Type')).to.equal('image/gif');
13+
const buf = await res.arrayBuffer();
14+
expect(buf.byteLength).to.equal(35);
15+
expect(buf).to.be.an.instanceOf(ArrayBuffer);
1716
});
1817

1918
it('should accept data uri with specified charset', async () => {

test/headers.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ describe('Headers', () => {
178178
res.j = Number.NaN;
179179
res.k = true;
180180
res.l = false;
181-
res.m = Buffer.from('test');
182181

183182
const h1 = new Headers(res);
184183
h1.set('n', [1, 2]);
@@ -198,7 +197,6 @@ describe('Headers', () => {
198197
expect(h1Raw.j).to.include('NaN');
199198
expect(h1Raw.k).to.include('true');
200199
expect(h1Raw.l).to.include('false');
201-
expect(h1Raw.m).to.include('test');
202200
expect(h1Raw.n).to.include('1,2');
203201
expect(h1Raw.n).to.include('3,4');
204202

test/main.js

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {FormData as FormDataNode} from 'formdata-polyfill/esm.min.js';
1616
import delay from 'delay';
1717
import AbortControllerMysticatea from 'abort-controller';
1818
import abortControllerPolyfill from 'abortcontroller-polyfill/dist/abortcontroller.js';
19+
import {text} from 'stream-consumers';
1920

2021
// Test subjects
2122
import Blob from 'fetch-blob';
@@ -36,6 +37,7 @@ import TestServer from './utils/server.js';
3637
import chaiTimeout from './utils/chai-timeout.js';
3738

3839
const AbortControllerPolyfill = abortControllerPolyfill.AbortController;
40+
const encoder = new TextEncoder();
3941

4042
function isNodeLowerThan(version) {
4143
return !~process.version.localeCompare(version, undefined, {numeric: true});
@@ -51,18 +53,6 @@ chai.use(chaiString);
5153
chai.use(chaiTimeout);
5254
const {expect} = chai;
5355

54-
function streamToPromise(stream, dataHandler) {
55-
return new Promise((resolve, reject) => {
56-
stream.on('data', (...args) => {
57-
Promise.resolve()
58-
.then(() => dataHandler(...args))
59-
.catch(reject);
60-
});
61-
stream.on('end', resolve);
62-
stream.on('error', reject);
63-
});
64-
}
65-
6656
describe('node-fetch', () => {
6757
const local = new TestServer();
6858
let base;
@@ -1314,25 +1304,7 @@ describe('node-fetch', () => {
13141304
});
13151305
});
13161306

1317-
it('should allow POST request with buffer body', () => {
1318-
const url = `${base}inspect`;
1319-
const options = {
1320-
method: 'POST',
1321-
body: Buffer.from('a=1', 'utf-8')
1322-
};
1323-
return fetch(url, options).then(res => {
1324-
return res.json();
1325-
}).then(res => {
1326-
expect(res.method).to.equal('POST');
1327-
expect(res.body).to.equal('a=1');
1328-
expect(res.headers['transfer-encoding']).to.be.undefined;
1329-
expect(res.headers['content-type']).to.be.undefined;
1330-
expect(res.headers['content-length']).to.equal('3');
1331-
});
1332-
});
1333-
13341307
it('should allow POST request with ArrayBuffer body', () => {
1335-
const encoder = new TextEncoder();
13361308
const url = `${base}inspect`;
13371309
const options = {
13381310
method: 'POST',
@@ -1351,7 +1323,7 @@ describe('node-fetch', () => {
13511323
const url = `${base}inspect`;
13521324
const options = {
13531325
method: 'POST',
1354-
body: new VMUint8Array(Buffer.from('Hello, world!\n')).buffer
1326+
body: new VMUint8Array(encoder.encode('Hello, world!\n')).buffer
13551327
};
13561328
return fetch(url, options).then(res => res.json()).then(res => {
13571329
expect(res.method).to.equal('POST');
@@ -1363,7 +1335,6 @@ describe('node-fetch', () => {
13631335
});
13641336

13651337
it('should allow POST request with ArrayBufferView (Uint8Array) body', () => {
1366-
const encoder = new TextEncoder();
13671338
const url = `${base}inspect`;
13681339
const options = {
13691340
method: 'POST',
@@ -1379,7 +1350,6 @@ describe('node-fetch', () => {
13791350
});
13801351

13811352
it('should allow POST request with ArrayBufferView (DataView) body', () => {
1382-
const encoder = new TextEncoder();
13831353
const url = `${base}inspect`;
13841354
const options = {
13851355
method: 'POST',
@@ -1398,7 +1368,7 @@ describe('node-fetch', () => {
13981368
const url = `${base}inspect`;
13991369
const options = {
14001370
method: 'POST',
1401-
body: new VMUint8Array(Buffer.from('Hello, world!\n'))
1371+
body: new VMUint8Array(encoder.encode('Hello, world!\n'))
14021372
};
14031373
return fetch(url, options).then(res => res.json()).then(res => {
14041374
expect(res.method).to.equal('POST');
@@ -1410,7 +1380,6 @@ describe('node-fetch', () => {
14101380
});
14111381

14121382
it('should allow POST request with ArrayBufferView (Uint8Array, offset, length) body', () => {
1413-
const encoder = new TextEncoder();
14141383
const url = `${base}inspect`;
14151384
const options = {
14161385
method: 'POST',
@@ -1846,39 +1815,28 @@ describe('node-fetch', () => {
18461815
});
18471816
});
18481817

1849-
it('should allow piping response body as stream', () => {
1818+
it('should allow piping response body as stream', async () => {
18501819
const url = `${base}hello`;
1851-
return fetch(url).then(res => {
1852-
expect(res.body).to.be.an.instanceof(stream.Transform);
1853-
return streamToPromise(res.body, chunk => {
1854-
if (chunk === null) {
1855-
return;
1856-
}
1857-
1858-
expect(chunk.toString()).to.equal('world');
1859-
});
1860-
});
1820+
const res = await fetch(url);
1821+
expect(res.body).to.be.an.instanceof(stream.Transform);
1822+
const body = await text(res.body);
1823+
expect(body).to.equal('world');
18611824
});
18621825

1863-
it('should allow cloning a response, and use both as stream', () => {
1826+
it('should allow cloning a response, and use both as stream', async () => {
18641827
const url = `${base}hello`;
1865-
return fetch(url).then(res => {
1866-
const r1 = res.clone();
1867-
expect(res.body).to.be.an.instanceof(stream.Transform);
1868-
expect(r1.body).to.be.an.instanceof(stream.Transform);
1869-
const dataHandler = chunk => {
1870-
if (chunk === null) {
1871-
return;
1872-
}
1828+
const res = await fetch(url);
1829+
const r1 = res.clone();
1830+
expect(res.body).to.be.an.instanceof(stream.Transform);
1831+
expect(r1.body).to.be.an.instanceof(stream.Transform);
18731832

1874-
expect(chunk.toString()).to.equal('world');
1875-
};
1833+
const [t1, t2] = await Promise.all([
1834+
text(res.body),
1835+
text(r1.body)
1836+
]);
18761837

1877-
return Promise.all([
1878-
streamToPromise(res.body, dataHandler),
1879-
streamToPromise(r1.body, dataHandler)
1880-
]);
1881-
});
1838+
expect(t1).to.equal('world');
1839+
expect(t2).to.equal('world');
18821840
});
18831841

18841842
it('should allow cloning a json response and log it as text response', () => {
@@ -2141,13 +2099,10 @@ describe('node-fetch', () => {
21412099
});
21422100
});
21432101

2144-
it('should support reading blob as stream', () => {
2145-
return new Response('hello')
2146-
.blob()
2147-
.then(blob => streamToPromise(stream.Readable.from(blob.stream()), data => {
2148-
const string = Buffer.from(data).toString();
2149-
expect(string).to.equal('hello');
2150-
}));
2102+
it('should support reading blob as stream', async () => {
2103+
const blob = await new Response('hello').blob();
2104+
const str = await text(blob.stream());
2105+
expect(str).to.equal('hello');
21512106
});
21522107

21532108
it('should support blob round-trip', () => {
@@ -2233,7 +2188,7 @@ describe('node-fetch', () => {
22332188
// Issue #414
22342189
it('should reject if attempt to accumulate body stream throws', () => {
22352190
const res = new Response(stream.Readable.from((async function * () {
2236-
yield Buffer.from('tada');
2191+
yield encoder.encode('tada');
22372192
await new Promise(resolve => {
22382193
setTimeout(resolve, 200);
22392194
});
@@ -2329,7 +2284,7 @@ describe('node-fetch', () => {
23292284
size: 1024
23302285
});
23312286

2332-
const bufferBody = Buffer.from(bodyContent);
2287+
const bufferBody = encoder.encode(bodyContent);
23332288
const bufferRequest = new Request(url, {
23342289
method: 'POST',
23352290
body: bufferBody,

test/referrer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ describe('Request constructor', () => {
127127
expect(() => {
128128
const req = new Request('http://example.com', {referrer: 'foobar'});
129129
expect.fail(req);
130-
}).to.throw(TypeError, 'Invalid URL: foobar');
130+
}).to.throw(TypeError, /Invalid URL/);
131131
});
132132
});
133133

test/request.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,17 @@ describe('Request', () => {
201201
});
202202
});
203203

204-
it('should support blob() method', () => {
204+
it('should support blob() method', async () => {
205205
const url = base;
206206
const request = new Request(url, {
207207
method: 'POST',
208-
body: Buffer.from('a=1')
208+
body: new TextEncoder().encode('a=1')
209209
});
210210
expect(request.url).to.equal(url);
211-
return request.blob().then(result => {
212-
expect(result).to.be.an.instanceOf(Blob);
213-
expect(result.size).to.equal(3);
214-
expect(result.type).to.equal('');
215-
});
211+
const blob = await request.blob();
212+
expect(blob).to.be.an.instanceOf(Blob);
213+
expect(blob.size).to.equal(3);
214+
expect(blob.type).to.equal('');
216215
});
217216

218217
it('should support clone() method', () => {

test/response.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ describe('Response', () => {
154154
});
155155
});
156156

157-
it('should support buffer as body', () => {
158-
const res = new Response(Buffer.from('a=1'));
159-
return res.text().then(result => {
160-
expect(result).to.equal('a=1');
161-
});
162-
});
163-
164157
it('should support ArrayBuffer as body', () => {
165158
const encoder = new TextEncoder();
166159
const res = new Response(encoder.encode('a=1'));

test/utils/read-stream.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0