Skip to content

Commit 1daae67

Browse files
authored
Fix import style to workaround node < 10 and webpack issues. (node-fetch#544)
* fix import rule for stream PassThrough * avoid named export for compatibility below node 10 * compress flag should not overwrite accept encoding header * doc update * 2.2.1
1 parent 8cc909f commit 1daae67

File tree

9 files changed

+60
-26
lines changed

9 files changed

+60
-26
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ Changelog
55

66
# 2.x release
77

8+
## v2.2.1
9+
10+
- Fix: `compress` flag shouldn't overwrite existing `Accept-Encoding` header.
11+
- Fix: multiple `import` rules, where `PassThrough` etc. doesn't have a named export when using node <10 and `--exerimental-modules` flag.
12+
- Other: Better README.
13+
814
## v2.2.0
915

1016
- Enhance: Support all `ArrayBuffer` view types

LIMITS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,7 @@ Known differences
2626

2727
- If you are using `res.clone()` and writing an isomorphic app, note that stream on Node.js have a smaller internal buffer size (16Kb, aka `highWaterMark`) from client-side browsers (>1Mb, not consistent across browsers).
2828

29+
- Because node.js stream doesn't expose a [*disturbed*](https://fetch.spec.whatwg.org/#concept-readablestream-disturbed) property like Stream spec, using a consumed stream for `new Response(body)` will not set `bodyUsed` flag correctly.
30+
2931
[readable-stream]: https://nodejs.org/api/stream.html#stream_readable_streams
3032
[ERROR-HANDLING.md]: https://github.com/bitinn/node-fetch/blob/master/ERROR-HANDLING.md

README.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
183183
});
184184
```
185185

186-
[TODO]: # (Somewhere i think we also should mention arrayBuffer also if you want to be cross-fetch compatible.)
187-
188186
#### Buffer
189187
If you prefer to cache binary data in full, use buffer(). (NOTE: buffer() is a `node-fetch` only API)
190188

@@ -265,8 +263,6 @@ Perform an HTTP(S) fetch.
265263

266264
`url` should be an absolute url, such as `https://example.com/`. A path-relative URL (`/file/under/root`) or protocol-relative URL (`//can-be-http-or-https.com/`) will result in a rejected promise.
267265

268-
[TODO]: # (It might be a good idea to reformat the options section into a table layout, like the headers section, instead of current code block.)
269-
270266
<a id="fetch-options"></a>
271267
### Options
272268

@@ -285,23 +281,22 @@ The default values are shown after each option key.
285281
timeout: 0, // req/res timeout in ms, it resets on redirect. 0 to disable (OS limit applies)
286282
compress: true, // support gzip/deflate content encoding. false to disable
287283
size: 0, // maximum response body size in bytes. 0 to disable
288-
agent: null // http(s).Agent instance, allows custom proxy, certificate etc.
284+
agent: null // http(s).Agent instance, allows custom proxy, certificate, dns lookup etc.
289285
}
290286
```
291287

292288
##### Default Headers
293289

294290
If no values are set, the following request headers will be sent automatically:
295291

296-
[TODO]: # ("we always said content-length will be "automatically calculated, if possible" in the default header section, but we never explain what's the condition for it to be calculated, and that chunked transfer-encoding will be used when they are not calculated or supplied." - "Maybe also add Transfer-Encoding: chunked? That header is added by Node.js automatically if the input is a stream, I believe.")
297-
298-
Header | Value
299-
----------------- | --------------------------------------------------------
300-
`Accept-Encoding` | `gzip,deflate` _(when `options.compress === true`)_
301-
`Accept` | `*/*`
302-
`Connection` | `close` _(when no `options.agent` is present)_
303-
`Content-Length` | _(automatically calculated, if possible)_
304-
`User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
292+
Header | Value
293+
------------------- | --------------------------------------------------------
294+
`Accept-Encoding` | `gzip,deflate` _(when `options.compress === true`)_
295+
`Accept` | `*/*`
296+
`Connection` | `close` _(when no `options.agent` is present)_
297+
`Content-Length` | _(automatically calculated, if possible)_
298+
`Transfer-Encoding` | `chunked` _(when `req.body` is a stream)_
299+
`User-Agent` | `node-fetch/1.0 (+https://github.com/bitinn/node-fetch)`
305300

306301
<a id="class-request"></a>
307302
### Class: Request
@@ -451,8 +446,6 @@ Consume the body and return a promise that will resolve to one of these formats.
451446

452447
Consume the body and return a promise that will resolve to a Buffer.
453448

454-
[TODO]: # (textConverted API should mention an optional dependency on encoding, which users need to install by themselves, and this is done purely for backward compatibility with 1.x release.)
455-
456449
#### body.textConverted()
457450

458451
<small>*(node-fetch extension)*</small>
@@ -461,6 +454,8 @@ Consume the body and return a promise that will resolve to a Buffer.
461454

462455
Identical to `body.text()`, except instead of always converting to UTF-8, encoding sniffing will be performed and text converted to UTF-8, if possible.
463456

457+
(This API requires an optional dependency on npm package [encoding](https://www.npmjs.com/package/encoding), which you need to install manually. `webpack` users may see [a warning message](https://github.com/bitinn/node-fetch/issues/412#issuecomment-379007792) due to this optional dependency.)
458+
464459
<a id="class-fetcherror"></a>
465460
### Class: FetchError
466461

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-fetch",
3-
"version": "2.2.0",
3+
"version": "2.2.1",
44
"description": "A light-weight module that brings window.fetch to node.js",
55
"main": "lib/index",
66
"browser": "./browser.js",

src/body.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
* Body interface provides common methods for Request and Response
66
*/
77

8-
import Stream, { PassThrough } from 'stream';
8+
import Stream from 'stream';
9+
910
import Blob, { BUFFER } from './blob.js';
1011
import FetchError from './fetch-error.js';
1112

@@ -14,6 +15,9 @@ try { convert = require('encoding').convert; } catch(e) {}
1415

1516
const INTERNALS = Symbol('Body internals');
1617

18+
// fix an issue where "PassThrough" isn't a named export for node <10
19+
const PassThrough = Stream.PassThrough;
20+
1721
/**
1822
* Body mixin
1923
*

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@
77
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
88
*/
99

10-
import { resolve as resolve_url } from 'url';
10+
import Url from 'url';
1111
import http from 'http';
1212
import https from 'https';
1313
import zlib from 'zlib';
14-
import { PassThrough } from 'stream';
14+
import Stream from 'stream';
1515

1616
import Body, { writeToStream, getTotalBytes } from './body';
1717
import Response from './response';
1818
import Headers, { createHeadersLenient } from './headers';
1919
import Request, { getNodeRequestOptions } from './request';
2020
import FetchError from './fetch-error';
2121

22+
// fix an issue where "PassThrough", "resolve" aren't a named export for node <10
23+
const PassThrough = Stream.PassThrough;
24+
const resolve_url = Url.resolve;
25+
2226
/**
2327
* Fetch function
2428
*

src/request.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@
77
* All spec algorithm step numbers are based on https://fetch.spec.whatwg.org/commit-snapshots/ae716822cb3a61843226cd090eefc6589446c1d2/.
88
*/
99

10-
import { format as format_url, parse as parse_url } from 'url';
10+
import Url from 'url';
11+
1112
import Headers, { exportNodeCompatibleHeaders } from './headers.js';
1213
import Body, { clone, extractContentType, getTotalBytes } from './body';
1314

1415
const INTERNALS = Symbol('Request internals');
1516

17+
// fix an issue where "format", "parse" aren't a named export for node <10
18+
const parse_url = Url.parse;
19+
const format_url = Url.format;
20+
1621
/**
1722
* Check if a value is an instance of Request.
1823
*
@@ -187,9 +192,10 @@ export function getNodeRequestOptions(request) {
187192
}
188193

189194
// HTTP-network-or-cache fetch step 2.15
190-
if (request.compress) {
195+
if (request.compress && !headers.has('Accept-Encoding')) {
191196
headers.set('Accept-Encoding', 'gzip,deflate');
192197
}
198+
193199
if (!headers.has('Connection') && !request.agent) {
194200
headers.set('Connection', 'close');
195201
}

src/response.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
* Response class provides content decoding
66
*/
77

8-
import { STATUS_CODES } from 'http';
8+
import http from 'http';
9+
910
import Headers from './headers.js';
1011
import Body, { clone } from './body';
1112

1213
const INTERNALS = Symbol('Response internals');
1314

15+
// fix an issue where "STATUS_CODES" aren't a named export for node <10
16+
const STATUS_CODES = http.STATUS_CODES;
17+
1418
/**
1519
* Response class
1620
*

test/test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,19 @@ describe('node-fetch', () => {
731731
});
732732
});
733733

734+
it('should not overwrite existing accept-encoding header when auto decompression is true', function() {
735+
const url = `${base}inspect`;
736+
const opts = {
737+
compress: true,
738+
headers: {
739+
'Accept-Encoding': 'gzip'
740+
}
741+
};
742+
return fetch(url, opts).then(res => res.json()).then(res => {
743+
expect(res.headers['accept-encoding']).to.equal('gzip');
744+
});
745+
});
746+
734747
it('should allow custom timeout', function() {
735748
this.timeout(500);
736749
const url = `${base}timeout`;
@@ -782,7 +795,7 @@ describe('node-fetch', () => {
782795

783796
it('should set default User-Agent', function () {
784797
const url = `${base}inspect`;
785-
fetch(url).then(res => res.json()).then(res => {
798+
return fetch(url).then(res => res.json()).then(res => {
786799
expect(res.headers['user-agent']).to.startWith('node-fetch/');
787800
});
788801
});
@@ -794,7 +807,7 @@ describe('node-fetch', () => {
794807
'user-agent': 'faked'
795808
}
796809
};
797-
fetch(url, opts).then(res => res.json()).then(res => {
810+
return fetch(url, opts).then(res => res.json()).then(res => {
798811
expect(res.headers['user-agent']).to.equal('faked');
799812
});
800813
});
@@ -813,7 +826,7 @@ describe('node-fetch', () => {
813826
'accept': 'application/json'
814827
}
815828
};
816-
fetch(url, opts).then(res => res.json()).then(res => {
829+
return fetch(url, opts).then(res => res.json()).then(res => {
817830
expect(res.headers.accept).to.equal('application/json');
818831
});
819832
});

0 commit comments

Comments
 (0)