| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7660eb5 commit d194241
27 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + # CacheStorage | ||
| 2 | + | ||
| 3 | + Undici exposes a W3C spec-compliant implementation of [CacheStorage](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) and [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache). | ||
| 4 | + | ||
| 5 | + ## Opening a Cache | ||
| 6 | + | ||
| 7 | + Undici exports a top-level CacheStorage instance. You can open a new Cache, or duplicate a Cache with an existing name, by using `CacheStorage.prototype.open`. If you open a Cache with the same name as an already-existing Cache, its list of cached Responses will be shared between both instances. | ||
| 8 | + | ||
| 9 | + ```mjs | ||
| 10 | + import { caches } from 'undici' | ||
| 11 | + | ||
| 12 | + const cache_1 = await caches.open('v1') | ||
| 13 | + const cache_2 = await caches.open('v1') | ||
| 14 | + | ||
| 15 | + // Although .open() creates a new instance, | ||
| 16 | + assert(cache_1 !== cache_2) | ||
| 17 | + // The same Response is matched in both. | ||
| 18 | + assert.deepStrictEqual(await cache_1.match('/req'), await cache_2.match('/req')) | ||
| 19 | + ``` | ||
| 20 | + | ||
| 21 | + ## Deleting a Cache | ||
| 22 | + | ||
| 23 | + If a Cache is deleted, the cached Responses/Requests can still be used. | ||
| 24 | + | ||
| 25 | + ```mjs | ||
| 26 | + const response = await cache_1.match('/req') | ||
| 27 | + await caches.delete('v1') | ||
| 28 | + | ||
| 29 | + await response.text() // the Response's body | ||
| 30 | + ``` | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,19 +7,25 @@ You can find all the error objects inside the `errors` key. | |||
| 7 | 7 | import { errors } from 'undici' | |
| 8 | 8 | ``` | |
| 9 | 9 | ||
| 10 | - | Error | Error Codes | Description | | ||
| 11 | - | ------------------------------------ | ------------------------------------- | -------------------------------------------------- | | ||
| 12 | - | `InvalidArgumentError` | `UND_ERR_INVALID_ARG` | passed an invalid argument. | | ||
| 13 | - | `InvalidReturnValueError` | `UND_ERR_INVALID_RETURN_VALUE` | returned an invalid value. | | ||
| 14 | - | `RequestAbortedError` | `UND_ERR_ABORTED` | the request has been aborted by the user | | ||
| 15 | - | `ClientDestroyedError` | `UND_ERR_DESTROYED` | trying to use a destroyed client. | | ||
| 16 | - | `ClientClosedError` | `UND_ERR_CLOSED` | trying to use a closed client. | | ||
| 17 | - | `SocketError` | `UND_ERR_SOCKET` | there is an error with the socket. | | ||
| 18 | - | `NotSupportedError` | `UND_ERR_NOT_SUPPORTED` | encountered unsupported functionality. | | ||
| 19 | - | `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` | request body does not match content-length header | | ||
| 20 | - | `ResponseContentLengthMismatchError` | `UND_ERR_RES_CONTENT_LENGTH_MISMATCH` | response body does not match content-length header | | ||
| 21 | - | `InformationalError` | `UND_ERR_INFO` | expected error with reason | | ||
| 22 | - | `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed | | ||
| 10 | + | Error | Error Codes | Description | | ||
| 11 | + | ------------------------------------ | ------------------------------------- | ------------------------------------------------------------------------- | | ||
| 12 | + | `UndiciError` | `UND_ERR` | all errors below are extended from `UndiciError`. | | ||
| 13 | + | `ConnectTimeoutError` | `UND_ERR_CONNECT_TIMEOUT` | socket is destroyed due to connect timeout. | | ||
| 14 | + | `HeadersTimeoutError` | `UND_ERR_HEADERS_TIMEOUT` | socket is destroyed due to headers timeout. | | ||
| 15 | + | `HeadersOverflowError` | `UND_ERR_HEADERS_OVERFLOW` | socket is destroyed due to headers' max size being exceeded. | | ||
| 16 | + | `BodyTimeoutError` | `UND_ERR_BODY_TIMEOUT` | socket is destroyed due to body timeout. | | ||
| 17 | + | `ResponseStatusCodeError` | `UND_ERR_RESPONSE_STATUS_CODE` | an error is thrown when `throwOnError` is `true` for status codes >= 400. | | ||
| 18 | + | `InvalidArgumentError` | `UND_ERR_INVALID_ARG` | passed an invalid argument. | | ||
| 19 | + | `InvalidReturnValueError` | `UND_ERR_INVALID_RETURN_VALUE` | returned an invalid value. | | ||
| 20 | + | `RequestAbortedError` | `UND_ERR_ABORTED` | the request has been aborted by the user | | ||
| 21 | + | `ClientDestroyedError` | `UND_ERR_DESTROYED` | trying to use a destroyed client. | | ||
| 22 | + | `ClientClosedError` | `UND_ERR_CLOSED` | trying to use a closed client. | | ||
| 23 | + | `SocketError` | `UND_ERR_SOCKET` | there is an error with the socket. | | ||
| 24 | + | `NotSupportedError` | `UND_ERR_NOT_SUPPORTED` | encountered unsupported functionality. | | ||
| 25 | + | `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` | request body does not match content-length header | | ||
| 26 | + | `ResponseContentLengthMismatchError` | `UND_ERR_RES_CONTENT_LENGTH_MISMATCH` | response body does not match content-length header | | ||
| 27 | + | `InformationalError` | `UND_ERR_INFO` | expected error with reason | | ||
| 28 | + | `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed | | ||
| 23 | 29 | ||
| 24 | 30 | ### `SocketError` | |
| 25 | 31 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,8 @@ Documentation and examples can be found on [MDN](https://developer.mozilla.org/e | |||
| 8 | 8 | ||
| 9 | 9 | This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/File) | |
| 10 | 10 | ||
| 11 | + In Node versions v18.13.0 and above and v19.2.0 and above, undici will default to using Node's [File](https://nodejs.org/api/buffer.html#class-file) class. In versions where it's not available, it will default to the undici one. | ||
| 12 | + | ||
| 11 | 13 | ## FormData | |
| 12 | 14 | ||
| 13 | 15 | This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FormData) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,17 +1,40 @@ | |||
| 1 | 1 | # Class: WebSocket | |
| 2 | 2 | ||
| 3 | - > ⚠️ Warning: the WebSocket API is experimental and has known bugs. | ||
| 3 | + > ⚠️ Warning: the WebSocket API is experimental. | ||
| 4 | 4 | ||
| 5 | 5 | Extends: [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) | |
| 6 | 6 | ||
| 7 | - The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the [WebSocket spec](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket). | ||
| 7 | + The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the [WebSocket spec](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455). | ||
| 8 | 8 | ||
| 9 | 9 | ## `new WebSocket(url[, protocol])` | |
| 10 | 10 | ||
| 11 | 11 | Arguments: | |
| 12 | 12 | ||
| 13 | 13 | * **url** `URL | string` - The url's protocol *must* be `ws` or `wss`. | |
| 14 | - * **protocol** `string | string[]` (optional) - Subprotocol(s) to request the server use. | ||
| 14 | + * **protocol** `string | string[] | WebSocketInit` (optional) - Subprotocol(s) to request the server use, or a [`Dispatcher`](./Dispatcher.md). | ||
| 15 | + | ||
| 16 | + ### Example: | ||
| 17 | + | ||
| 18 | + This example will not work in browsers or other platforms that don't allow passing an object. | ||
| 19 | + | ||
| 20 | + ```mjs | ||
| 21 | + import { WebSocket, ProxyAgent } from 'undici' | ||
| 22 | + | ||
| 23 | + const proxyAgent = new ProxyAgent('my.proxy.server') | ||
| 24 | + | ||
| 25 | + const ws = new WebSocket('wss://echo.websocket.events', { | ||
| 26 | + dispatcher: proxyAgent, | ||
| 27 | + protocols: ['echo', 'chat'] | ||
| 28 | + }) | ||
| 29 | + ``` | ||
| 30 | + | ||
| 31 | + If you do not need a custom Dispatcher, it's recommended to use the following pattern: | ||
| 32 | + | ||
| 33 | + ```mjs | ||
| 34 | + import { WebSocket } from 'undici' | ||
| 35 | + | ||
| 36 | + const ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat']) | ||
| 37 | + ``` | ||
| 15 | 38 | ||
| 16 | 39 | ## Read More | |
| 17 | 40 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ export * from './types/formdata' | |||
| 24 | 24 | export * from './types/diagnostics-channel' | |
| 25 | 25 | export * from './types/websocket' | |
| 26 | 26 | export * from './types/content-type' | |
| 27 | + export * from './types/cache' | ||
| 27 | 28 | export { Interceptable } from './types/mock-interceptor' | |
| 28 | 29 | ||
| 29 | 30 | export { Dispatcher, BalancedPool, Pool, Client, buildConnector, errors, Agent, request, stream, pipeline, connect, upgrade, setGlobalDispatcher, getGlobalDispatcher, setGlobalOrigin, getGlobalOrigin, MockClient, MockPool, MockAgent, mockErrors, ProxyAgent, RedirectHandler, DecoratorHandler } | |
@@ -52,4 +53,5 @@ declare namespace Undici { | |||
| 52 | 53 | var MockAgent: typeof import('./types/mock-agent').default; | |
| 53 | 54 | var mockErrors: typeof import('./types/mock-errors').default; | |
| 54 | 55 | var fetch: typeof import('./types/fetch').fetch; | |
| 56 | + var caches: typeof import('./types/cache').caches; | ||
| 55 | 57 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -121,6 +121,13 @@ if (util.nodeMajor > 16 || (util.nodeMajor === 16 && util.nodeMinor >= 8)) { | |||
| 121 | 121 | ||
| 122 | 122 | module.exports.setGlobalOrigin = setGlobalOrigin | |
| 123 | 123 | module.exports.getGlobalOrigin = getGlobalOrigin | |
| 124 | + | ||
| 125 | + const { CacheStorage } = require('./lib/cache/cachestorage') | ||
| 126 | + const { kConstruct } = require('./lib/cache/symbols') | ||
| 127 | + | ||
| 128 | + // Cache & CacheStorage are tightly coupled with fetch. Even if it may run | ||
| 129 | + // in an older version of Node, it doesn't have any use without fetch. | ||
| 130 | + module.exports.caches = new CacheStorage(kConstruct) | ||
| 124 | 131 | } | |
| 125 | 132 | ||
| 126 | 133 | if (util.nodeMajor >= 16) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments