| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b7d5869 commit 46d9308
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,6 +194,21 @@ Basic usage example: | |||
| 194 | 194 | } | |
| 195 | 195 | ``` | |
| 196 | 196 | ||
| 197 | + You can pass an optional dispatcher to `fetch` as: | ||
| 198 | + | ||
| 199 | + ```js | ||
| 200 | + import { fetch, Agent } from 'undici' | ||
| 201 | + | ||
| 202 | + const res = await fetch('https://example.com', { | ||
| 203 | + // Mocks are also supported | ||
| 204 | + dispatcher: new Agent({ | ||
| 205 | + keepAliveTimeout: 10, | ||
| 206 | + keepAliveMaxTimeout: 10 | ||
| 207 | + }) | ||
| 208 | + }) | ||
| 209 | + const json = await res.json() | ||
| 210 | + console.log(json) | ||
| 211 | + ``` | ||
| 197 | 212 | ||
| 198 | 213 | #### `request.body` | |
| 199 | 214 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,6 @@ import { errors } from 'undici' | |||
| 19 | 19 | | `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` | request body does not match content-length header | | |
| 20 | 20 | | `ResponseContentLengthMismatchError` | `UND_ERR_RES_CONTENT_LENGTH_MISMATCH` | response body does not match content-length header | | |
| 21 | 21 | | `InformationalError` | `UND_ERR_INFO` | expected error with reason | | |
| 22 | - | `TrailerMismatchError` | `UND_ERR_TRAILER_MISMATCH` | trailers did not match specification | | ||
| 23 | 22 | ||
| 24 | 23 | ### `SocketError` | |
| 25 | 24 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,12 +1,11 @@ | |||
| 1 | 1 | 'use strict' | |
| 2 | 2 | ||
| 3 | - const Agent = require('./lib/agent') | ||
| 4 | - | ||
| 5 | - const globalDispatcher = new Agent() | ||
| 6 | - | ||
| 3 | + const { getGlobalDispatcher } = require('./lib/global') | ||
| 7 | 4 | const fetchImpl = require('./lib/fetch') | |
| 5 | + | ||
| 8 | 6 | module.exports.fetch = async function fetch (resource) { | |
| 9 | - return fetchImpl.apply(globalDispatcher, arguments) | ||
| 7 | + const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher() | ||
| 8 | + return fetchImpl.apply(dispatcher, arguments) | ||
| 10 | 9 | } | |
| 11 | 10 | module.exports.FormData = require('./lib/fetch/formdata').FormData | |
| 12 | 11 | module.exports.Headers = require('./lib/fetch/headers').Headers | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,7 @@ const MockAgent = require('./lib/mock/mock-agent') | |||
| 15 | 15 | const MockPool = require('./lib/mock/mock-pool') | |
| 16 | 16 | const mockErrors = require('./lib/mock/mock-errors') | |
| 17 | 17 | const ProxyAgent = require('./lib/proxy-agent') | |
| 18 | + const { getGlobalDispatcher, setGlobalDispatcher } = require('./lib/global') | ||
| 18 | 19 | ||
| 19 | 20 | const nodeVersion = process.versions.node.split('.') | |
| 20 | 21 | const nodeMajor = Number(nodeVersion[0]) | |
@@ -32,19 +33,6 @@ module.exports.ProxyAgent = ProxyAgent | |||
| 32 | 33 | module.exports.buildConnector = buildConnector | |
| 33 | 34 | module.exports.errors = errors | |
| 34 | 35 | ||
| 35 | - let globalDispatcher = new Agent() | ||
| 36 | - | ||
| 37 | - function setGlobalDispatcher (agent) { | ||
| 38 | - if (!agent || typeof agent.dispatch !== 'function') { | ||
| 39 | - throw new InvalidArgumentError('Argument agent must implement Agent') | ||
| 40 | - } | ||
| 41 | - globalDispatcher = agent | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - function getGlobalDispatcher () { | ||
| 45 | - return globalDispatcher | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | 36 | function makeDispatcher (fn) { | |
| 49 | 37 | return (url, opts, handler) => { | |
| 50 | 38 | if (typeof opts === 'function') { | |
@@ -98,7 +86,7 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) { | |||
| 98 | 86 | if (!fetchImpl) { | |
| 99 | 87 | fetchImpl = require('./lib/fetch') | |
| 100 | 88 | } | |
| 101 | - const dispatcher = getGlobalDispatcher() | ||
| 89 | + const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher() | ||
| 102 | 90 | return fetchImpl.apply(dispatcher, arguments) | |
| 103 | 91 | } | |
| 104 | 92 | module.exports.Headers = require('./lib/fetch/headers').Headers | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,7 +11,6 @@ const RedirectHandler = require('./handler/redirect') | |||
| 11 | 11 | const { | |
| 12 | 12 | RequestContentLengthMismatchError, | |
| 13 | 13 | ResponseContentLengthMismatchError, | |
| 14 | - TrailerMismatchError, | ||
| 15 | 14 | InvalidArgumentError, | |
| 16 | 15 | RequestAbortedError, | |
| 17 | 16 | HeadersTimeoutError, | |
@@ -425,7 +424,6 @@ class Parser { | |||
| 425 | 424 | ||
| 426 | 425 | this.bytesRead = 0 | |
| 427 | 426 | ||
| 428 | - this.trailer = '' | ||
| 429 | 427 | this.keepAlive = '' | |
| 430 | 428 | this.contentLength = '' | |
| 431 | 429 | } | |
@@ -615,8 +613,6 @@ class Parser { | |||
| 615 | 613 | const key = this.headers[len - 2] | |
| 616 | 614 | if (key.length === 10 && key.toString().toLowerCase() === 'keep-alive') { | |
| 617 | 615 | this.keepAlive += buf.toString() | |
| 618 | - } else if (key.length === 7 && key.toString().toLowerCase() === 'trailer') { | ||
| 619 | - this.trailer += buf.toString() | ||
| 620 | 616 | } else if (key.length === 14 && key.toString().toLowerCase() === 'content-length') { | |
| 621 | 617 | this.contentLength += buf.toString() | |
| 622 | 618 | } | |
@@ -819,7 +815,7 @@ class Parser { | |||
| 819 | 815 | } | |
| 820 | 816 | ||
| 821 | 817 | onMessageComplete () { | |
| 822 | - const { client, socket, statusCode, upgrade, trailer, headers, contentLength, bytesRead, shouldKeepAlive } = this | ||
| 818 | + const { client, socket, statusCode, upgrade, headers, contentLength, bytesRead, shouldKeepAlive } = this | ||
| 823 | 819 | ||
| 824 | 820 | if (socket.destroyed && (!statusCode || shouldKeepAlive)) { | |
| 825 | 821 | return -1 | |
@@ -838,7 +834,6 @@ class Parser { | |||
| 838 | 834 | this.statusText = '' | |
| 839 | 835 | this.bytesRead = 0 | |
| 840 | 836 | this.contentLength = '' | |
| 841 | - this.trailer = '' | ||
| 842 | 837 | this.keepAlive = '' | |
| 843 | 838 | ||
| 844 | 839 | assert(this.headers.length % 2 === 0) | |
@@ -849,23 +844,6 @@ class Parser { | |||
| 849 | 844 | return | |
| 850 | 845 | } | |
| 851 | 846 | ||
| 852 | - const trailers = trailer ? trailer.split(/,\s*/) : [] | ||
| 853 | - for (let i = 0; i < trailers.length; i++) { | ||
| 854 | - const trailer = trailers[i] | ||
| 855 | - let found = false | ||
| 856 | - for (let n = 0; n < headers.length; n += 2) { | ||
| 857 | - const key = headers[n] | ||
| 858 | - if (key.length === trailer.length && key.toString().toLowerCase() === trailer.toLowerCase()) { | ||
| 859 | - found = true | ||
| 860 | - break | ||
| 861 | - } | ||
| 862 | - } | ||
| 863 | - if (!found) { | ||
| 864 | - util.destroy(socket, new TrailerMismatchError()) | ||
| 865 | - return -1 | ||
| 866 | - } | ||
| 867 | - } | ||
| 868 | - | ||
| 869 | 847 | /* istanbul ignore next: should be handled by llhttp? */ | |
| 870 | 848 | if (request.method !== 'HEAD' && contentLength && bytesRead !== parseInt(contentLength, 10)) { | |
| 871 | 849 | util.destroy(socket, new ResponseContentLengthMismatchError()) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -116,16 +116,6 @@ class ResponseContentLengthMismatchError extends UndiciError { | |||
| 116 | 116 | } | |
| 117 | 117 | } | |
| 118 | 118 | ||
| 119 | - class TrailerMismatchError extends UndiciError { | ||
| 120 | - constructor (message) { | ||
| 121 | - super(message) | ||
| 122 | - Error.captureStackTrace(this, TrailerMismatchError) | ||
| 123 | - this.name = 'TrailerMismatchError' | ||
| 124 | - this.message = message || 'Trailers does not match trailer header' | ||
| 125 | - this.code = 'UND_ERR_TRAILER_MISMATCH' | ||
| 126 | - } | ||
| 127 | - } | ||
| 128 | - | ||
| 129 | 119 | class ClientDestroyedError extends UndiciError { | |
| 130 | 120 | constructor (message) { | |
| 131 | 121 | super(message) | |
@@ -196,7 +186,6 @@ module.exports = { | |||
| 196 | 186 | BodyTimeoutError, | |
| 197 | 187 | RequestContentLengthMismatchError, | |
| 198 | 188 | ConnectTimeoutError, | |
| 199 | - TrailerMismatchError, | ||
| 200 | 189 | InvalidArgumentError, | |
| 201 | 190 | InvalidReturnValueError, | |
| 202 | 191 | RequestAbortedError, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments