-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matteo Collina <[email protected]> PR-URL: nodejs-private/node-private#577 Reviewed-By: Rafael Gonzaga <[email protected]>
- Loading branch information
Showing
104 changed files
with
5,210 additions
and
20,964 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Class: RetryHandler | ||
|
||
Extends: `undici.DispatcherHandlers` | ||
|
||
A handler class that implements the retry logic for a request. | ||
|
||
## `new RetryHandler(dispatchOptions, retryHandlers, [retryOptions])` | ||
|
||
Arguments: | ||
|
||
- **options** `Dispatch.DispatchOptions & RetryOptions` (required) - It is an intersection of `Dispatcher.DispatchOptions` and `RetryOptions`. | ||
- **retryHandlers** `RetryHandlers` (required) - Object containing the `dispatch` to be used on every retry, and `handler` for handling the `dispatch` lifecycle. | ||
|
||
Returns: `retryHandler` | ||
|
||
### Parameter: `Dispatch.DispatchOptions & RetryOptions` | ||
|
||
Extends: [`Dispatch.DispatchOptions`](Dispatcher.md#parameter-dispatchoptions). | ||
|
||
#### `RetryOptions` | ||
|
||
- **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => void` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed. | ||
- **maxRetries** `number` (optional) - Maximum number of retries. Default: `5` | ||
- **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds) | ||
- **minTimeout** `number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second) | ||
- **timeoutFactor** `number` (optional) - Factor to multiply the timeout by for each retry attempt. Default: `2` | ||
- **retryAfter** `boolean` (optional) - It enables automatic retry after the `Retry-After` header is received. Default: `true` | ||
- | ||
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'PUT', 'HEAD', 'OPTIONS', 'DELETE']` | ||
- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]` | ||
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', | ||
|
||
**`RetryContext`** | ||
|
||
- `state`: `RetryState` - Current retry state. It can be mutated. | ||
- `opts`: `Dispatch.DispatchOptions & RetryOptions` - Options passed to the retry handler. | ||
|
||
### Parameter `RetryHandlers` | ||
|
||
- **dispatch** `(options: Dispatch.DispatchOptions, handlers: Dispatch.DispatchHandlers) => Promise<Dispatch.DispatchResponse>` (required) - Dispatch function to be called after every retry. | ||
- **handler** Extends [`Dispatch.DispatchHandlers`](Dispatcher.md#dispatcherdispatchoptions-handler) (required) - Handler function to be called after the request is successful or the retries are exhausted. | ||
|
||
Examples: | ||
|
||
```js | ||
const client = new Client(`http://localhost:${server.address().port}`); | ||
const chunks = []; | ||
const handler = new RetryHandler( | ||
{ | ||
...dispatchOptions, | ||
retryOptions: { | ||
// custom retry function | ||
retry: function (err, state, callback) { | ||
counter++; | ||
|
||
if (err.code && err.code === "UND_ERR_DESTROYED") { | ||
callback(err); | ||
return; | ||
} | ||
|
||
if (err.statusCode === 206) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
setTimeout(() => callback(null), 1000); | ||
}, | ||
}, | ||
}, | ||
{ | ||
dispatch: (...args) => { | ||
return client.dispatch(...args); | ||
}, | ||
handler: { | ||
onConnect() {}, | ||
onBodySent() {}, | ||
onHeaders(status, _rawHeaders, resume, _statusMessage) { | ||
// do something with headers | ||
}, | ||
onData(chunk) { | ||
chunks.push(chunk); | ||
return true; | ||
}, | ||
onComplete() {}, | ||
onError() { | ||
// handle error properly | ||
}, | ||
}, | ||
} | ||
); | ||
``` | ||
|
||
#### Example - Basic RetryHandler with defaults | ||
|
||
```js | ||
const client = new Client(`http://localhost:${server.address().port}`); | ||
const handler = new RetryHandler(dispatchOptions, { | ||
dispatch: client.dispatch.bind(client), | ||
handler: { | ||
onConnect() {}, | ||
onBodySent() {}, | ||
onHeaders(status, _rawHeaders, resume, _statusMessage) {}, | ||
onData(chunk) {}, | ||
onComplete() {}, | ||
onError(err) {}, | ||
}, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,3 @@ | ||
import Dispatcher from'./types/dispatcher' | ||
import { setGlobalDispatcher, getGlobalDispatcher } from './types/global-dispatcher' | ||
import { setGlobalOrigin, getGlobalOrigin } from './types/global-origin' | ||
import Pool from'./types/pool' | ||
import { RedirectHandler, DecoratorHandler } from './types/handlers' | ||
|
||
import BalancedPool from './types/balanced-pool' | ||
import Client from'./types/client' | ||
import buildConnector from'./types/connector' | ||
import errors from'./types/errors' | ||
import Agent from'./types/agent' | ||
import MockClient from'./types/mock-client' | ||
import MockPool from'./types/mock-pool' | ||
import MockAgent from'./types/mock-agent' | ||
import mockErrors from'./types/mock-errors' | ||
import ProxyAgent from'./types/proxy-agent' | ||
import { request, pipeline, stream, connect, upgrade } from './types/api' | ||
|
||
export * from './types/cookies' | ||
export * from './types/fetch' | ||
export * from './types/file' | ||
export * from './types/filereader' | ||
export * from './types/formdata' | ||
export * from './types/diagnostics-channel' | ||
export * from './types/websocket' | ||
export * from './types/content-type' | ||
export * from './types/cache' | ||
export { Interceptable } from './types/mock-interceptor' | ||
|
||
export { Dispatcher, BalancedPool, Pool, Client, buildConnector, errors, Agent, request, stream, pipeline, connect, upgrade, setGlobalDispatcher, getGlobalDispatcher, setGlobalOrigin, getGlobalOrigin, MockClient, MockPool, MockAgent, mockErrors, ProxyAgent, RedirectHandler, DecoratorHandler } | ||
export * from './types/index' | ||
import Undici from './types/index' | ||
export default Undici | ||
|
||
declare namespace Undici { | ||
var Dispatcher: typeof import('./types/dispatcher').default | ||
var Pool: typeof import('./types/pool').default; | ||
var RedirectHandler: typeof import ('./types/handlers').RedirectHandler | ||
var DecoratorHandler: typeof import ('./types/handlers').DecoratorHandler | ||
var createRedirectInterceptor: typeof import ('./types/interceptors').createRedirectInterceptor | ||
var BalancedPool: typeof import('./types/balanced-pool').default; | ||
var Client: typeof import('./types/client').default; | ||
var buildConnector: typeof import('./types/connector').default; | ||
var errors: typeof import('./types/errors').default; | ||
var Agent: typeof import('./types/agent').default; | ||
var setGlobalDispatcher: typeof import('./types/global-dispatcher').setGlobalDispatcher; | ||
var getGlobalDispatcher: typeof import('./types/global-dispatcher').getGlobalDispatcher; | ||
var request: typeof import('./types/api').request; | ||
var stream: typeof import('./types/api').stream; | ||
var pipeline: typeof import('./types/api').pipeline; | ||
var connect: typeof import('./types/api').connect; | ||
var upgrade: typeof import('./types/api').upgrade; | ||
var MockClient: typeof import('./types/mock-client').default; | ||
var MockPool: typeof import('./types/mock-pool').default; | ||
var MockAgent: typeof import('./types/mock-agent').default; | ||
var mockErrors: typeof import('./types/mock-errors').default; | ||
var fetch: typeof import('./types/fetch').fetch; | ||
var caches: typeof import('./types/cache').caches; | ||
} |
Oops, something went wrong.