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: implement fetch per spec #928

Merged
merged 56 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
fb9281e
fix: implement fetch per spec
ronag Aug 5, 2021
866cc90
fixup: strict null check
ronag Aug 11, 2021
217af2b
fixup: extractBody
ronag Aug 11, 2021
0d40f36
fixup: body used
ronag Aug 11, 2021
ca62f2a
fixup: use instanceof Blob
ronag Aug 11, 2021
2a6cdb1
fixup
ronag Aug 11, 2021
3113392
fixup
ronag Aug 11, 2021
ffeba7b
fixup: isBodyReadable
ronag Aug 11, 2021
7c288ea
fixup: localURLSOnly
ronag Aug 11, 2021
c03a784
fixup: timingAllow
ronag Aug 11, 2021
c611493
fixup: timingInfo
ronag Aug 11, 2021
360530b
fixup: disregard any enqueuing
ronag Aug 11, 2021
9c9fa53
fixup: strict cmp
ronag Aug 11, 2021
9172bec
fixup: clone
ronag Aug 11, 2021
362ef33
fixup: don't need to copy URL
ronag Aug 11, 2021
1c42102
fix: Response init
ronag Aug 11, 2021
883a56c
fixup: opaqueredirect
ronag Aug 11, 2021
4724d36
fixup: avoid new Response
ronag Aug 11, 2021
8647470
fixup: abort default err
ronag Aug 11, 2021
db693fc
fixup: url
ronag Aug 11, 2021
6c0d4bd
fixup: don't use body.source when writing request body
ronag Aug 11, 2021
f81577a
fixup
ronag Aug 11, 2021
515d6c1
fixup
ronag Aug 11, 2021
e475f5e
fixup: fixes from review
ronag Aug 12, 2021
866e64b
fixup: check valid reason phrase
ronag Aug 12, 2021
e16955e
fixup: error messages
ronag Aug 12, 2021
c2eeb96
fixup: isBuffer
ronag Aug 12, 2021
301dc8e
fixup: more error msg
ronag Aug 12, 2021
9122f57
fixup: forbidden methods
ronag Aug 12, 2021
74fbc20
fixup: node stream body does not violate spec
ronag Aug 12, 2021
25485a0
fixup: error msgs
ronag Aug 12, 2021
3344db1
fixup
ronag Aug 12, 2021
9f2fd14
fixup
ronag Aug 12, 2021
3bcf1a3
fixup
ronag Aug 12, 2021
b397500
fixup
ronag Aug 12, 2021
52f6f83
fixup: chrome compat
ronag Aug 12, 2021
a8a9b6a
fixup
ronag Aug 12, 2021
4ce55d3
fixup: error msgs
ronag Aug 12, 2021
af5866a
fixup
ronag Aug 12, 2021
9f6d2e0
fixup: timingInfo
ronag Aug 12, 2021
1e87e8d
fixup: comments
ronag Aug 12, 2021
7bd95d5
fixup: prettier + standard --fix
ronag Aug 12, 2021
f71abfe
fixup: bad port + missing cond
ronag Aug 12, 2021
41eae28
fixup: docs
ronag Aug 12, 2021
2cf7293
fixup: move fetch to new folder
ronag Aug 12, 2021
83adb4e
fixup: Dispatcher.fetch doesn't exist
ronag Aug 12, 2021
387919b
fixup: link MDN docs
ronag Aug 12, 2021
efddfc2
fixup
ronag Aug 12, 2021
c3822cf
fixup: node fetch in CI
ronag Aug 12, 2021
abe0c68
fixup: terminate on onError
ronag Aug 12, 2021
32aef9f
fixup
ronag Aug 12, 2021
6f005a6
fixup: transmit request body algorithm
ronag Aug 12, 2021
a901f6c
fixup
ronag Aug 12, 2021
b6cb85d
fixup
ronag Aug 12, 2021
53b3ab1
fixup
ronag Aug 12, 2021
260cf8f
fixup
ronag Aug 12, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,33 @@ jobs:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install

- name: Unit test (no coverage)
- name: Test Tap
run: |
npm test
npm run test:tap

- name: Unit test (coverage)
- name: Test Jest
run: |
npm run coverage:ci
npm run test:jest

- name: Test node-fetch
run: |
npm run test:node-fetch

- name: Test types
- name: Test Types
run: |
npm run test:typescript

- name: Coverage report
- name: Coverage
run: |
npm run coverage:ci

- name: Coverage Report
uses: codecov/codecov-action@v1
44 changes: 4 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,55 +155,19 @@ Calls `options.dispatch.connect(options)`.

See [Dispatcher.connect](docs/api/Dispatcher.md#dispatcherconnect) for more details.

https://fetch.spec.whatwg.org/

### `undici.fetch([url, options]): Promise`
### `undici.fetch(input[, init]): Promise`

Implements [fetch](https://fetch.spec.whatwg.org/).

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
https://fetch.spec.whatwg.org/#fetch-method

Only supported on Node 16+.

This is [experimental](https://nodejs.org/api/documentation.html#documentation_stability_index) and is not yet fully compliant the Fetch Standard. We plan to ship breaking changes to this feature until it is out of experimental.

Arguments:

* **url** `string | URL | object`
* **options** `RequestInit`

Returns: `Promise<Response>`

#### Parameter: `RequestInit`

https://fetch.spec.whatwg.org/#request-class

* **method** `string`
* **headers** `HeadersInit`
* **body** `BodyInit?`
* **referrer** *not supported*
* **referrerPolicy** *not supported*
* **mode** *not supported*
* **credentials** *not supported*
* **cache** *not supported*
* **redirect** `RequestRedirect`
* **integrity** *not supported*
* **keepalive** *not supported*
* **signal** `AbortSignal?`
* **window** `null`

#### Parameter: `Response`

https://fetch.spec.whatwg.org/#response-class

* **type** `ResponseType`
* **url** `string`
* **redirected** `boolean`
* **status** `number`
* **ok** `boolean`
* **statusText** `string`
* **headers** `Headers`

See [Dispatcher.fetch](docs/api/Dispatcher.md#dispatcherfetchoptions-callback) for more details.

### `undici.upgrade([url, options]): Promise`

Upgrade to a different protocol. See [MDN - HTTP - Protocol upgrade mechanism](https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism) for more details.
Expand Down
8 changes: 0 additions & 8 deletions docs/api/Dispatcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,6 @@ client.dispatch({
})
```

### `Dispatcher.fetch(options)`

Implements [fetch](https://fetch.spec.whatwg.org/).

Only supported on Node 16+.

This is [experimental](https://nodejs.org/api/documentation.html#documentation_stability_index) and is not yet fully compliant the Fetch Standard. We plan to ship breaking changes to this feature until it is out of experimental.

### `Dispatcher.pipeline(options, handler)`

For easy use with [stream.pipeline](https://nodejs.org/api/stream.html#stream_stream_pipeline_source_transforms_destination_callback). The `handler` argument should return a `Readable` from which the result will be read. Usually it should just return the `body` argument unless some kind of transformation needs to be performed based on e.g. `headers` or `statusCode`. The `handler` should validate the response and save any required state. If there is an error, it should be thrown. The function returns a `Duplex` which writes to the request and reads from the response.
Expand Down
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const MockAgent = require('./lib/mock/mock-agent')
const MockPool = require('./lib/mock/mock-pool')
const mockErrors = require('./lib/mock/mock-errors')

const nodeMajor = Number(process.versions.node.split('.')[0])

Object.assign(Dispatcher.prototype, api)

module.exports.Dispatcher = Dispatcher
Expand Down Expand Up @@ -84,7 +86,17 @@ function makeDispatcher (fn) {
module.exports.setGlobalDispatcher = setGlobalDispatcher
module.exports.getGlobalDispatcher = getGlobalDispatcher

module.exports.fetch = makeDispatcher(api.fetch)
if (nodeMajor >= 16) {
const fetchImpl = require('./lib/fetch')
module.exports.fetch = async function fetch (resource, init) {
const dispatcher = getGlobalDispatcher()
return fetchImpl.call(dispatcher, resource, init)
}
module.exports.Headers = require('./lib/fetch/headers').Headers
module.exports.Response = require('./lib/fetch/response').Response
module.exports.Request = require('./lib/fetch/request').Request
}

module.exports.request = makeDispatcher(api.request)
module.exports.stream = makeDispatcher(api.stream)
module.exports.pipeline = makeDispatcher(api.pipeline)
Expand Down
66 changes: 0 additions & 66 deletions lib/api/api-fetch/body.js

This file was deleted.

Loading