| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d568600 commit b79c652
18 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,9 @@ Extends: [`AgentOptions`](Agent.md#parameter-agentoptions) | |||
| 19 | 19 | * **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string. | |
| 20 | 20 | * **token** `string` (optional) - It can be passed by a string of token for authentication. | |
| 21 | 21 | * **auth** `string` (**deprecated**) - Use token. | |
| 22 | - * **clientFactory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)` | ||
| 22 | + * **clientFactory** `(origin: URL, opts: Object) => Dispatcher` (optional) - Default: `(origin, opts) => new Pool(origin, opts)` | ||
| 23 | + * **requestTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the request. See [TLS](https://nodejs.org/api/tls.html#tlsconnectoptions-callback). | ||
| 24 | + * **proxyTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the proxy server. See [TLS](https://nodejs.org/api/tls.html#tlsconnectoptions-callback). | ||
| 23 | 25 | ||
| 24 | 26 | Examples: | |
| 25 | 27 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,4 @@ | |||
| 1 | + const { addAbortListener } = require('../core/util') | ||
| 1 | 2 | const { RequestAbortedError } = require('../core/errors') | |
| 2 | 3 | ||
| 3 | 4 | const kListener = Symbol('kListener') | |
@@ -29,11 +30,7 @@ function addSignal (self, signal) { | |||
| 29 | 30 | abort(self) | |
| 30 | 31 | } | |
| 31 | 32 | ||
| 32 | - if ('addEventListener' in self[kSignal]) { | ||
| 33 | - self[kSignal].addEventListener('abort', self[kListener]) | ||
| 34 | - } else { | ||
| 35 | - self[kSignal].addListener('abort', self[kListener]) | ||
| 36 | - } | ||
| 33 | + addAbortListener(self[kSignal], self[kListener]) | ||
| 37 | 34 | } | |
| 38 | 35 | ||
| 39 | 36 | function removeSignal (self) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -155,12 +155,13 @@ module.exports = class BodyReadable extends Readable { | |||
| 155 | 155 | const abortFn = () => { | |
| 156 | 156 | this.destroy() | |
| 157 | 157 | } | |
| 158 | + let signalListenerCleanup | ||
| 158 | 159 | if (signal) { | |
| 159 | 160 | if (typeof signal !== 'object' || !('aborted' in signal)) { | |
| 160 | 161 | throw new InvalidArgumentError('signal must be an AbortSignal') | |
| 161 | 162 | } | |
| 162 | 163 | util.throwIfAborted(signal) | |
| 163 | - signal.addEventListener('abort', abortFn, { once: true }) | ||
| 164 | + signalListenerCleanup = util.addAbortListener(signal, abortFn) | ||
| 164 | 165 | } | |
| 165 | 166 | try { | |
| 166 | 167 | for await (const chunk of this) { | |
@@ -173,8 +174,10 @@ module.exports = class BodyReadable extends Readable { | |||
| 173 | 174 | } catch { | |
| 174 | 175 | util.throwIfAborted(signal) | |
| 175 | 176 | } finally { | |
| 176 | - if (signal) { | ||
| 177 | - signal.removeEventListener('abort', abortFn) | ||
| 177 | + if (typeof signalListenerCleanup === 'function') { | ||
| 178 | + signalListenerCleanup() | ||
| 179 | + } else if (signalListenerCleanup) { | ||
| 180 | + signalListenerCleanup[Symbol.dispose]() | ||
| 178 | 181 | } | |
| 179 | 182 | } | |
| 180 | 183 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -422,6 +422,24 @@ function throwIfAborted (signal) { | |||
| 422 | 422 | } | |
| 423 | 423 | } | |
| 424 | 424 | ||
| 425 | + let events | ||
| 426 | + function addAbortListener (signal, listener) { | ||
| 427 | + if (typeof Symbol.dispose === 'symbol') { | ||
| 428 | + if (!events) { | ||
| 429 | + events = require('events') | ||
| 430 | + } | ||
| 431 | + if (typeof events.addAbortListener === 'function' && 'aborted' in signal) { | ||
| 432 | + return events.addAbortListener(signal, listener) | ||
| 433 | + } | ||
| 434 | + } | ||
| 435 | + if ('addEventListener' in signal) { | ||
| 436 | + signal.addEventListener('abort', listener, { once: true }) | ||
| 437 | + return () => signal.removeEventListener('abort', listener) | ||
| 438 | + } | ||
| 439 | + signal.addListener('abort', listener) | ||
| 440 | + return () => signal.removeListener('abort', listener) | ||
| 441 | + } | ||
| 442 | + | ||
| 425 | 443 | const hasToWellFormed = !!String.prototype.toWellFormed | |
| 426 | 444 | ||
| 427 | 445 | /** | |
@@ -469,6 +487,7 @@ module.exports = { | |||
| 469 | 487 | isFormDataLike, | |
| 470 | 488 | buildURL, | |
| 471 | 489 | throwIfAborted, | |
| 490 | + addAbortListener, | ||
| 472 | 491 | nodeMajor, | |
| 473 | 492 | nodeMinor, | |
| 474 | 493 | nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -105,7 +105,7 @@ function extractBody (object, keepalive = false) { | |||
| 105 | 105 | // Set source to a copy of the bytes held by object. | |
| 106 | 106 | source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength)) | |
| 107 | 107 | } else if (util.isFormDataLike(object)) { | |
| 108 | - const boundary = `----formdata-undici-${Math.random()}`.replace('.', '').slice(0, 32) | ||
| 108 | + const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}` | ||
| 109 | 109 | const prefix = `--${boundary}\r\nContent-Disposition: form-data` | |
| 110 | 110 | ||
| 111 | 111 | /*! formdata-polyfill. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -56,7 +56,7 @@ const { | |||
| 56 | 56 | const { kHeadersList } = require('../core/symbols') | |
| 57 | 57 | const EE = require('events') | |
| 58 | 58 | const { Readable, pipeline } = require('stream') | |
| 59 | - const { isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util') | ||
| 59 | + const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor } = require('../core/util') | ||
| 60 | 60 | const { dataURLProcessor, serializeAMimeType } = require('./dataURL') | |
| 61 | 61 | const { TransformStream } = require('stream/web') | |
| 62 | 62 | const { getGlobalDispatcher } = require('../global') | |
@@ -174,22 +174,22 @@ async function fetch (input, init = {}) { | |||
| 174 | 174 | let controller = null | |
| 175 | 175 | ||
| 176 | 176 | // 11. Add the following abort steps to requestObject’s signal: | |
| 177 | - requestObject.signal.addEventListener( | ||
| 178 | - 'abort', | ||
| 177 | + addAbortListener( | ||
| 178 | + requestObject.signal, | ||
| 179 | 179 | () => { | |
| 180 | 180 | // 1. Set locallyAborted to true. | |
| 181 | 181 | locallyAborted = true | |
| 182 | 182 | ||
| 183 | - // 2. Abort the fetch() call with p, request, responseObject, | ||
| 183 | + // 2. Assert: controller is non-null. | ||
| 184 | + assert(controller != null) | ||
| 185 | + | ||
| 186 | + // 3. Abort controller with requestObject’s signal’s abort reason. | ||
| 187 | + controller.abort(requestObject.signal.reason) | ||
| 188 | + | ||
| 189 | + // 4. Abort the fetch() call with p, request, responseObject, | ||
| 184 | 190 | // and requestObject’s signal’s abort reason. | |
| 185 | 191 | abortFetch(p, request, responseObject, requestObject.signal.reason) | |
| 186 | - | ||
| 187 | - // 3. If controller is not null, then abort controller. | ||
| 188 | - if (controller != null) { | ||
| 189 | - controller.abort() | ||
| 190 | - } | ||
| 191 | - }, | ||
| 192 | - { once: true } | ||
| 192 | + } | ||
| 193 | 193 | ) | |
| 194 | 194 | ||
| 195 | 195 | // 12. Let handleFetchDone given response response be to finalize and | |
@@ -319,7 +319,7 @@ function finalizeAndReportTiming (response, initiatorType = 'other') { | |||
| 319 | 319 | // https://w3c.github.io/resource-timing/#dfn-mark-resource-timing | |
| 320 | 320 | function markResourceTiming (timingInfo, originalURL, initiatorType, globalThis, cacheState) { | |
| 321 | 321 | if (nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 2)) { | |
| 322 | - performance.markResourceTiming(timingInfo, originalURL, initiatorType, globalThis, cacheState) | ||
| 322 | + performance.markResourceTiming(timingInfo, originalURL.href, initiatorType, globalThis, cacheState) | ||
| 323 | 323 | } | |
| 324 | 324 | } | |
| 325 | 325 | ||
@@ -1986,7 +1986,7 @@ async function httpNetworkFetch ( | |||
| 1986 | 1986 | if (key.toLowerCase() === 'content-encoding') { | |
| 1987 | 1987 | // https://www.rfc-editor.org/rfc/rfc7231#section-3.1.2.1 | |
| 1988 | 1988 | // "All content-coding values are case-insensitive..." | |
| 1989 | - codings = val.toLowerCase().split(',').map((x) => x.trim()) | ||
| 1989 | + codings = val.toLowerCase().split(',').map((x) => x.trim()).reverse() | ||
| 1990 | 1990 | } else if (key.toLowerCase() === 'location') { | |
| 1991 | 1991 | location = val | |
| 1992 | 1992 | } | |
@@ -2007,7 +2007,14 @@ async function httpNetworkFetch ( | |||
| 2007 | 2007 | for (const coding of codings) { | |
| 2008 | 2008 | // https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2 | |
| 2009 | 2009 | if (coding === 'x-gzip' || coding === 'gzip') { | |
| 2010 | - decoders.push(zlib.createGunzip()) | ||
| 2010 | + decoders.push(zlib.createGunzip({ | ||
| 2011 | + // Be less strict when decoding compressed responses, since sometimes | ||
| 2012 | + // servers send slightly invalid responses that are still accepted | ||
| 2013 | + // by common browsers. | ||
| 2014 | + // Always using Z_SYNC_FLUSH is what cURL does. | ||
| 2015 | + flush: zlib.constants.Z_SYNC_FLUSH, | ||
| 2016 | + finishFlush: zlib.constants.Z_SYNC_FLUSH | ||
| 2017 | + })) | ||
| 2011 | 2018 | } else if (coding === 'deflate') { | |
| 2012 | 2019 | decoders.push(zlib.createInflate()) | |
| 2013 | 2020 | } else if (coding === 'br') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -232,14 +232,18 @@ class Request { | |||
| 232 | 232 | } | |
| 233 | 233 | ||
| 234 | 234 | // 3. If one of the following is true | |
| 235 | - // parsedReferrer’s cannot-be-a-base-URL is true, scheme is "about", | ||
| 236 | - // and path contains a single string "client" | ||
| 237 | - // parsedReferrer’s origin is not same origin with origin | ||
| 235 | + // - parsedReferrer’s scheme is "about" and path is the string "client" | ||
| 236 | + // - parsedReferrer’s origin is not same origin with origin | ||
| 238 | 237 | // then set request’s referrer to "client". | |
| 239 | - // TODO | ||
| 240 | - | ||
| 241 | - // 4. Otherwise, set request’s referrer to parsedReferrer. | ||
| 242 | - request.referrer = parsedReferrer | ||
| 238 | + if ( | ||
| 239 | + (parsedReferrer.protocol === 'about:' && parsedReferrer.hostname === 'client') || | ||
| 240 | + (origin && !sameOrigin(parsedReferrer, this[kRealm].settingsObject.baseUrl)) | ||
| 241 | + ) { | ||
| 242 | + request.referrer = 'client' | ||
| 243 | + } else { | ||
| 244 | + // 4. Otherwise, set request’s referrer to parsedReferrer. | ||
| 245 | + request.referrer = parsedReferrer | ||
| 246 | + } | ||
| 243 | 247 | } | |
| 244 | 248 | } | |
| 245 | 249 | ||
@@ -336,6 +340,8 @@ class Request { | |||
| 336 | 340 | ||
| 337 | 341 | // 28. Set this’s signal to a new AbortSignal object with this’s relevant | |
| 338 | 342 | // Realm. | |
| 343 | + // TODO: could this be simplified with AbortSignal.any | ||
| 344 | + // (https://dom.spec.whatwg.org/#dom-abortsignal-any) | ||
| 339 | 345 | const ac = new AbortController() | |
| 340 | 346 | this[kSignal] = ac.signal | |
| 341 | 347 | this[kSignal][kRealm] = this[kRealm] | |
@@ -381,7 +387,7 @@ class Request { | |||
| 381 | 387 | } | |
| 382 | 388 | } catch {} | |
| 383 | 389 | ||
| 384 | - signal.addEventListener('abort', abort, { once: true }) | ||
| 390 | + util.addAbortListener(signal, abort) | ||
| 385 | 391 | requestFinalizer.register(ac, { signal, abort }) | |
| 386 | 392 | } | |
| 387 | 393 | } | |
@@ -729,12 +735,11 @@ class Request { | |||
| 729 | 735 | if (this.signal.aborted) { | |
| 730 | 736 | ac.abort(this.signal.reason) | |
| 731 | 737 | } else { | |
| 732 | - this.signal.addEventListener( | ||
| 733 | - 'abort', | ||
| 738 | + util.addAbortListener( | ||
| 739 | + this.signal, | ||
| 734 | 740 | () => { | |
| 735 | 741 | ac.abort(this.signal.reason) | |
| 736 | - }, | ||
| 737 | - { once: true } | ||
| 742 | + } | ||
| 738 | 743 | ) | |
| 739 | 744 | } | |
| 740 | 745 | clonedRequestObject[kSignal] = ac.signal | |
| Back | FazBrowse Home | New Git URL |
0 commit comments