| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,101 @@ | |||
| 1 | + # Cookie Handling | ||
| 2 | + | ||
| 3 | + ## `Cookie` interface | ||
| 4 | + | ||
| 5 | + * **name** `string` | ||
| 6 | + * **value** `string` | ||
| 7 | + * **expires** `Date|number` (optional) | ||
| 8 | + * **maxAge** `number` (optional) | ||
| 9 | + * **domain** `string` (optional) | ||
| 10 | + * **path** `string` (optional) | ||
| 11 | + * **secure** `boolean` (optional) | ||
| 12 | + * **httpOnly** `boolean` (optional) | ||
| 13 | + * **sameSite** `'String'|'Lax'|'None'` (optional) | ||
| 14 | + * **unparsed** `string[]` (optional) Left over attributes that weren't parsed. | ||
| 15 | + | ||
| 16 | + ## `deleteCookie(headers, name[, attributes])` | ||
| 17 | + | ||
| 18 | + Sets the expiry time of the cookie to the unix epoch, causing browsers to delete it when received. | ||
| 19 | + | ||
| 20 | + ```js | ||
| 21 | + import { deleteCookie, Headers } from 'undici' | ||
| 22 | + | ||
| 23 | + const headers = new Headers() | ||
| 24 | + deleteCookie(headers, 'name') | ||
| 25 | + | ||
| 26 | + console.log(headers.get('set-cookie')) // name=; Expires=Thu, 01 Jan 1970 00:00:00 GMT | ||
| 27 | + ``` | ||
| 28 | + | ||
| 29 | + Arguments: | ||
| 30 | + | ||
| 31 | + * **headers** `Headers` | ||
| 32 | + * **name** `string` | ||
| 33 | + * **attributes** `{ path?: string, domain?: string }` (optional) | ||
| 34 | + | ||
| 35 | + Returns: `void` | ||
| 36 | + | ||
| 37 | + ## `getCookies(headers)` | ||
| 38 | + | ||
| 39 | + Parses the `Cookie` header and returns a list of attributes and values. | ||
| 40 | + | ||
| 41 | + ```js | ||
| 42 | + import { getCookies, Headers } from 'undici' | ||
| 43 | + | ||
| 44 | + const headers = new Headers({ | ||
| 45 | + cookie: 'get=cookies; and=attributes' | ||
| 46 | + }) | ||
| 47 | + | ||
| 48 | + console.log(getCookies(headers)) // { get: 'cookies', and: 'attributes' } | ||
| 49 | + ``` | ||
| 50 | + | ||
| 51 | + Arguments: | ||
| 52 | + | ||
| 53 | + * **headers** `Headers` | ||
| 54 | + | ||
| 55 | + Returns: `Record<string, string>` | ||
| 56 | + | ||
| 57 | + ## `getSetCookies(headers)` | ||
| 58 | + | ||
| 59 | + Parses all `Set-Cookie` headers. | ||
| 60 | + | ||
| 61 | + ```js | ||
| 62 | + import { getSetCookies, Headers } from 'undici' | ||
| 63 | + | ||
| 64 | + const headers = new Headers({ 'set-cookie': 'undici=getSetCookies; Secure' }) | ||
| 65 | + | ||
| 66 | + console.log(getSetCookies(headers)) | ||
| 67 | + // [ | ||
| 68 | + // { | ||
| 69 | + // name: 'undici', | ||
| 70 | + // value: 'getSetCookies', | ||
| 71 | + // secure: true | ||
| 72 | + // } | ||
| 73 | + // ] | ||
| 74 | + | ||
| 75 | + ``` | ||
| 76 | + | ||
| 77 | + Arguments: | ||
| 78 | + | ||
| 79 | + * **headers** `Headers` | ||
| 80 | + | ||
| 81 | + Returns: `Cookie[]` | ||
| 82 | + | ||
| 83 | + ## `setCookie(headers, cookie)` | ||
| 84 | + | ||
| 85 | + Appends a cookie to the `Set-Cookie` header. | ||
| 86 | + | ||
| 87 | + ```js | ||
| 88 | + import { setCookie, Headers } from 'undici' | ||
| 89 | + | ||
| 90 | + const headers = new Headers() | ||
| 91 | + setCookie(headers, { name: 'undici', value: 'setCookie' }) | ||
| 92 | + | ||
| 93 | + console.log(headers.get('Set-Cookie')) // undici=setCookie | ||
| 94 | + ``` | ||
| 95 | + | ||
| 96 | + Arguments: | ||
| 97 | + | ||
| 98 | + * **headers** `Headers` | ||
| 99 | + * **cookie** `Cookie` | ||
| 100 | + | ||
| 101 | + Returns: `void` | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -135,3 +135,70 @@ diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, soc | |||
| 135 | 135 | // connector is a function that creates the socket | |
| 136 | 136 | console.log(`Connect failed with ${error.message}`) | |
| 137 | 137 | }) | |
| 138 | + ``` | ||
| 139 | + | ||
| 140 | + ## `undici:websocket:open` | ||
| 141 | + | ||
| 142 | + This message is published after the client has successfully connected to a server. | ||
| 143 | + | ||
| 144 | + ```js | ||
| 145 | + import diagnosticsChannel from 'diagnostics_channel' | ||
| 146 | + | ||
| 147 | + diagnosticsChannel.channel('undici:websocket:open').subscribe(({ address, protocol, extensions }) => { | ||
| 148 | + console.log(address) // address, family, and port | ||
| 149 | + console.log(protocol) // negotiated subprotocols | ||
| 150 | + console.log(extensions) // negotiated extensions | ||
| 151 | + }) | ||
| 152 | + ``` | ||
| 153 | + | ||
| 154 | + ## `undici:websocket:close` | ||
| 155 | + | ||
| 156 | + This message is published after the connection has closed. | ||
| 157 | + | ||
| 158 | + ```js | ||
| 159 | + import diagnosticsChannel from 'diagnostics_channel' | ||
| 160 | + | ||
| 161 | + diagnosticsChannel.channel('undici:websocket:close').subscribe(({ websocket, code, reason }) => { | ||
| 162 | + console.log(websocket) // the WebSocket object | ||
| 163 | + console.log(code) // the closing status code | ||
| 164 | + console.log(reason) // the closing reason | ||
| 165 | + }) | ||
| 166 | + ``` | ||
| 167 | + | ||
| 168 | + ## `undici:websocket:socket_error` | ||
| 169 | + | ||
| 170 | + This message is published if the socket experiences an error. | ||
| 171 | + | ||
| 172 | + ```js | ||
| 173 | + import diagnosticsChannel from 'diagnostics_channel' | ||
| 174 | + | ||
| 175 | + diagnosticsChannel.channel('undici:websocket:socket_error').subscribe((error) => { | ||
| 176 | + console.log(error) | ||
| 177 | + }) | ||
| 178 | + ``` | ||
| 179 | + | ||
| 180 | + ## `undici:websocket:ping` | ||
| 181 | + | ||
| 182 | + This message is published after the client receives a ping frame, if the connection is not closing. | ||
| 183 | + | ||
| 184 | + ```js | ||
| 185 | + import diagnosticsChannel from 'diagnostics_channel' | ||
| 186 | + | ||
| 187 | + diagnosticsChannel.channel('undici:websocket:ping').subscribe(({ payload }) => { | ||
| 188 | + // a Buffer or undefined, containing the optional application data of the frame | ||
| 189 | + console.log(payload) | ||
| 190 | + }) | ||
| 191 | + ``` | ||
| 192 | + | ||
| 193 | + ## `undici:websocket:pong` | ||
| 194 | + | ||
| 195 | + This message is published after the client receives a pong frame. | ||
| 196 | + | ||
| 197 | + ```js | ||
| 198 | + import diagnosticsChannel from 'diagnostics_channel' | ||
| 199 | + | ||
| 200 | + diagnosticsChannel.channel('undici:websocket:pong').subscribe(({ payload }) => { | ||
| 201 | + // a Buffer or undefined, containing the optional application data of the frame | ||
| 202 | + console.log(payload) | ||
| 203 | + }) | ||
| 204 | + ``` | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -192,6 +192,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo | |||
| 192 | 192 | * **origin** `string | URL` | |
| 193 | 193 | * **path** `string` | |
| 194 | 194 | * **method** `string` | |
| 195 | + * **reset** `boolean` (optional) - Default: `false` - If `false`, the request will attempt to create a long-living connection by sending the `connection: keep-alive` header,otherwise will attempt to close it immediately after response by sending `connection: close` within the request and closing the socket afterwards. | ||
| 195 | 196 | * **body** `string | Buffer | Uint8Array | stream.Readable | Iterable | AsyncIterable | null` (optional) - Default: `null` | |
| 196 | 197 | * **headers** `UndiciHeaders | string[]` (optional) - Default: `null`. | |
| 197 | 198 | * **query** `Record<string, any> | null` (optional) - Default: `null` - Query string params to be embedded in the request URL. Note that both keys and values of query are encoded using `encodeURIComponent`. If for some reason you need to send them unencoded, embed query params into path directly instead. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,25 @@ | |||
| 1 | + # Fetch | ||
| 2 | + | ||
| 3 | + Undici exposes a fetch() method starts the process of fetching a resource from the network. | ||
| 4 | + | ||
| 5 | + Documentation and examples can be found on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/fetch). | ||
| 6 | + | ||
| 7 | + ## File | ||
| 8 | + | ||
| 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 | + | ||
| 11 | + ## FormData | ||
| 12 | + | ||
| 13 | + This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FormData) | ||
| 14 | + | ||
| 15 | + ## Response | ||
| 16 | + | ||
| 17 | + This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response) | ||
| 18 | + | ||
| 19 | + ## Request | ||
| 20 | + | ||
| 21 | + This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request) | ||
| 22 | + | ||
| 23 | + ## Header | ||
| 24 | + | ||
| 25 | + This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Headers) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + # Class: WebSocket | ||
| 2 | + | ||
| 3 | + > ⚠️ Warning: the WebSocket API is experimental and has known bugs. | ||
| 4 | + | ||
| 5 | + Extends: [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) | ||
| 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). | ||
| 8 | + | ||
| 9 | + ## `new WebSocket(url[, protocol])` | ||
| 10 | + | ||
| 11 | + Arguments: | ||
| 12 | + | ||
| 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. | ||
| 15 | + | ||
| 16 | + ## Read More | ||
| 17 | + | ||
| 18 | + - [MDN - WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) | ||
| 19 | + - [The WebSocket Specification](https://www.rfc-editor.org/rfc/rfc6455) | ||
| 20 | + - [The WHATWG WebSocket Specification](https://websockets.spec.whatwg.org/) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,11 +16,13 @@ import mockErrors from'./types/mock-errors' | |||
| 16 | 16 | import ProxyAgent from'./types/proxy-agent' | |
| 17 | 17 | import { request, pipeline, stream, connect, upgrade } from './types/api' | |
| 18 | 18 | ||
| 19 | + export * from './types/cookies' | ||
| 19 | 20 | export * from './types/fetch' | |
| 20 | 21 | export * from './types/file' | |
| 21 | 22 | export * from './types/filereader' | |
| 22 | 23 | export * from './types/formdata' | |
| 23 | 24 | export * from './types/diagnostics-channel' | |
| 25 | + export * from './types/websocket' | ||
| 24 | 26 | export { Interceptable } from './types/mock-interceptor' | |
| 25 | 27 | ||
| 26 | 28 | export { Dispatcher, BalancedPool, Pool, Client, buildConnector, errors, Agent, request, stream, pipeline, connect, upgrade, setGlobalDispatcher, getGlobalDispatcher, setGlobalOrigin, getGlobalOrigin, MockClient, MockPool, MockAgent, mockErrors, ProxyAgent, RedirectHandler, DecoratorHandler } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,14 @@ const nodeVersion = process.versions.node.split('.') | |||
| 24 | 24 | const nodeMajor = Number(nodeVersion[0]) | |
| 25 | 25 | const nodeMinor = Number(nodeVersion[1]) | |
| 26 | 26 | ||
| 27 | + let hasCrypto | ||
| 28 | + try { | ||
| 29 | + require('crypto') | ||
| 30 | + hasCrypto = true | ||
| 31 | + } catch { | ||
| 32 | + hasCrypto = false | ||
| 33 | + } | ||
| 34 | + | ||
| 27 | 35 | Object.assign(Dispatcher.prototype, api) | |
| 28 | 36 | ||
| 29 | 37 | module.exports.Dispatcher = Dispatcher | |
@@ -119,6 +127,21 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) { | |||
| 119 | 127 | module.exports.getGlobalOrigin = getGlobalOrigin | |
| 120 | 128 | } | |
| 121 | 129 | ||
| 130 | + if (nodeMajor >= 16) { | ||
| 131 | + const { deleteCookie, getCookies, getSetCookies, setCookie } = require('./lib/cookies') | ||
| 132 | + | ||
| 133 | + module.exports.deleteCookie = deleteCookie | ||
| 134 | + module.exports.getCookies = getCookies | ||
| 135 | + module.exports.getSetCookies = getSetCookies | ||
| 136 | + module.exports.setCookie = setCookie | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + if (nodeMajor >= 18 && hasCrypto) { | ||
| 140 | + const { WebSocket } = require('./lib/websocket/websocket') | ||
| 141 | + | ||
| 142 | + module.exports.WebSocket = WebSocket | ||
| 143 | + } | ||
| 144 | + | ||
| 122 | 145 | module.exports.request = makeDispatcher(api.request) | |
| 123 | 146 | module.exports.stream = makeDispatcher(api.stream) | |
| 124 | 147 | module.exports.pipeline = makeDispatcher(api.pipeline) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments