| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b941992 commit 4d27b32
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -123,7 +123,7 @@ This section documents our most commonly used API methods. Additional APIs are d | |||
| 123 | 123 | Arguments: | |
| 124 | 124 | ||
| 125 | 125 | * **url** `string | URL | UrlObject` | |
| 126 | - * **options** [`RequestOptions`](./docs/api/Dispatcher.md#parameter-requestoptions) | ||
| 126 | + * **options** [`RequestOptions`](./docs/docs/api/Dispatcher.md#parameter-requestoptions) | ||
| 127 | 127 | * **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher) | |
| 128 | 128 | * **method** `String` - Default: `PUT` if `options.body`, otherwise `GET` | |
| 129 | 129 | * **maxRedirections** `Integer` - Default: `0` | |
@@ -132,14 +132,14 @@ Returns a promise with the result of the `Dispatcher.request` method. | |||
| 132 | 132 | ||
| 133 | 133 | Calls `options.dispatcher.request(options)`. | |
| 134 | 134 | ||
| 135 | - See [Dispatcher.request](./docs/api/Dispatcher.md#dispatcherrequestoptions-callback) for more details, and [request examples](./examples/README.md) for examples. | ||
| 135 | + See [Dispatcher.request](./docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback) for more details, and [request examples](./examples/README.md) for examples. | ||
| 136 | 136 | ||
| 137 | 137 | ### `undici.stream([url, options, ]factory): Promise` | |
| 138 | 138 | ||
| 139 | 139 | Arguments: | |
| 140 | 140 | ||
| 141 | 141 | * **url** `string | URL | UrlObject` | |
| 142 | - * **options** [`StreamOptions`](./docs/api/Dispatcher.md#parameter-streamoptions) | ||
| 142 | + * **options** [`StreamOptions`](./docs/docs/api/Dispatcher.md#parameter-streamoptions) | ||
| 143 | 143 | * **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher) | |
| 144 | 144 | * **method** `String` - Default: `PUT` if `options.body`, otherwise `GET` | |
| 145 | 145 | * **maxRedirections** `Integer` - Default: `0` | |
@@ -149,14 +149,14 @@ Returns a promise with the result of the `Dispatcher.stream` method. | |||
| 149 | 149 | ||
| 150 | 150 | Calls `options.dispatcher.stream(options, factory)`. | |
| 151 | 151 | ||
| 152 | - See [Dispatcher.stream](./docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback) for more details. | ||
| 152 | + See [Dispatcher.stream](./docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback) for more details. | ||
| 153 | 153 | ||
| 154 | 154 | ### `undici.pipeline([url, options, ]handler): Duplex` | |
| 155 | 155 | ||
| 156 | 156 | Arguments: | |
| 157 | 157 | ||
| 158 | 158 | * **url** `string | URL | UrlObject` | |
| 159 | - * **options** [`PipelineOptions`](./docs/api/Dispatcher.md#parameter-pipelineoptions) | ||
| 159 | + * **options** [`PipelineOptions`](./docs/docs/api/Dispatcher.md#parameter-pipelineoptions) | ||
| 160 | 160 | * **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher) | |
| 161 | 161 | * **method** `String` - Default: `PUT` if `options.body`, otherwise `GET` | |
| 162 | 162 | * **maxRedirections** `Integer` - Default: `0` | |
@@ -166,7 +166,7 @@ Returns: `stream.Duplex` | |||
| 166 | 166 | ||
| 167 | 167 | Calls `options.dispatch.pipeline(options, handler)`. | |
| 168 | 168 | ||
| 169 | - See [Dispatcher.pipeline](./docs/api/Dispatcher.md#dispatcherpipelineoptions-handler) for more details. | ||
| 169 | + See [Dispatcher.pipeline](./docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler) for more details. | ||
| 170 | 170 | ||
| 171 | 171 | ### `undici.connect([url, options]): Promise` | |
| 172 | 172 | ||
@@ -175,7 +175,7 @@ Starts two-way communications with the requested resource using [HTTP CONNECT](h | |||
| 175 | 175 | Arguments: | |
| 176 | 176 | ||
| 177 | 177 | * **url** `string | URL | UrlObject` | |
| 178 | - * **options** [`ConnectOptions`](./docs/api/Dispatcher.md#parameter-connectoptions) | ||
| 178 | + * **options** [`ConnectOptions`](./docs/docs/api/Dispatcher.md#parameter-connectoptions) | ||
| 179 | 179 | * **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher) | |
| 180 | 180 | * **maxRedirections** `Integer` - Default: `0` | |
| 181 | 181 | * **callback** `(err: Error | null, data: ConnectData | null) => void` (optional) | |
@@ -184,7 +184,7 @@ Returns a promise with the result of the `Dispatcher.connect` method. | |||
| 184 | 184 | ||
| 185 | 185 | Calls `options.dispatch.connect(options)`. | |
| 186 | 186 | ||
| 187 | - See [Dispatcher.connect](./docs/api/Dispatcher.md#dispatcherconnectoptions-callback) for more details. | ||
| 187 | + See [Dispatcher.connect](./docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback) for more details. | ||
| 188 | 188 | ||
| 189 | 189 | ### `undici.fetch(input[, init]): Promise` | |
| 190 | 190 | ||
@@ -335,7 +335,7 @@ Upgrade to a different protocol. See [MDN - HTTP - Protocol upgrade mechanism](h | |||
| 335 | 335 | Arguments: | |
| 336 | 336 | ||
| 337 | 337 | * **url** `string | URL | UrlObject` | |
| 338 | - * **options** [`UpgradeOptions`](./docs/api/Dispatcher.md#parameter-upgradeoptions) | ||
| 338 | + * **options** [`UpgradeOptions`](./docs/docs/api/Dispatcher.md#parameter-upgradeoptions) | ||
| 339 | 339 | * **dispatcher** `Dispatcher` - Default: [getGlobalDispatcher](#undicigetglobaldispatcher) | |
| 340 | 340 | * **maxRedirections** `Integer` - Default: `0` | |
| 341 | 341 | * **callback** `(error: Error | null, data: UpgradeData) => void` (optional) | |
@@ -344,7 +344,7 @@ Returns a promise with the result of the `Dispatcher.upgrade` method. | |||
| 344 | 344 | ||
| 345 | 345 | Calls `options.dispatcher.upgrade(options)`. | |
| 346 | 346 | ||
| 347 | - See [Dispatcher.upgrade](./docs/api/Dispatcher.md#dispatcherupgradeoptions-callback) for more details. | ||
| 347 | + See [Dispatcher.upgrade](./docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback) for more details. | ||
| 348 | 348 | ||
| 349 | 349 | ### `undici.setGlobalDispatcher(dispatcher)` | |
| 350 | 350 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -626,10 +626,6 @@ Reflect.deleteProperty(Headers, 'setHeadersGuard') | |||
| 626 | 626 | Reflect.deleteProperty(Headers, 'getHeadersList') | |
| 627 | 627 | Reflect.deleteProperty(Headers, 'setHeadersList') | |
| 628 | 628 | ||
| 629 | - Object.defineProperty(Headers.prototype, util.inspect.custom, { | ||
| 630 | - enumerable: false | ||
| 631 | - }) | ||
| 632 | - | ||
| 633 | 629 | iteratorMixin('Headers', Headers, kHeadersSortedMap, 0, 1) | |
| 634 | 630 | ||
| 635 | 631 | Object.defineProperties(Headers.prototype, { | |
@@ -642,6 +638,17 @@ Object.defineProperties(Headers.prototype, { | |||
| 642 | 638 | [Symbol.toStringTag]: { | |
| 643 | 639 | value: 'Headers', | |
| 644 | 640 | configurable: true | |
| 641 | + }, | ||
| 642 | + [util.inspect.custom]: { | ||
| 643 | + enumerable: false | ||
| 644 | + }, | ||
| 645 | + // Compatibility for global headers | ||
| 646 | + [Symbol('headers list')]: { | ||
| 647 | + configurable: false, | ||
| 648 | + enumerable: false, | ||
| 649 | + get: function () { | ||
| 650 | + return getHeadersList(this) | ||
| 651 | + } | ||
| 645 | 652 | } | |
| 646 | 653 | }) | |
| 647 | 654 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -240,8 +240,8 @@ class ByteParser extends Writable { | |||
| 240 | 240 | ||
| 241 | 241 | this.#loop = true | |
| 242 | 242 | this.#state = parserStates.INFO | |
| 243 | - this.run(callback) | ||
| 244 | 243 | this.#fragments.length = 0 | |
| 244 | + this.run(callback) | ||
| 245 | 245 | }) | |
| 246 | 246 | ||
| 247 | 247 | this.#loop = false | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,15 +2,30 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { WebsocketFrameSend } = require('./frame') | |
| 4 | 4 | const { opcodes, sendHints } = require('./constants') | |
| 5 | + const FixedQueue = require('../../dispatcher/fixed-queue') | ||
| 5 | 6 | ||
| 6 | - /** @type {Uint8Array} */ | ||
| 7 | + /** @type {typeof Uint8Array} */ | ||
| 7 | 8 | const FastBuffer = Buffer[Symbol.species] | |
| 8 | 9 | ||
| 10 | + /** | ||
| 11 | + * @typedef {object} SendQueueNode | ||
| 12 | + * @property {Promise<void> | null} promise | ||
| 13 | + * @property {((...args: any[]) => any)} callback | ||
| 14 | + * @property {Buffer | null} frame | ||
| 15 | + */ | ||
| 16 | + | ||
| 9 | 17 | class SendQueue { | |
| 10 | - #queued = new Set() | ||
| 11 | - #size = 0 | ||
| 18 | + /** | ||
| 19 | + * @type {FixedQueue} | ||
| 20 | + */ | ||
| 21 | + #queue = new FixedQueue() | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * @type {boolean} | ||
| 25 | + */ | ||
| 26 | + #running = false | ||
| 12 | 27 | ||
| 13 | - /** @type {import('net').Socket} */ | ||
| 28 | + /** @type {import('node:net').Socket} */ | ||
| 14 | 29 | #socket | |
| 15 | 30 | ||
| 16 | 31 | constructor (socket) { | |
@@ -19,66 +34,70 @@ class SendQueue { | |||
| 19 | 34 | ||
| 20 | 35 | add (item, cb, hint) { | |
| 21 | 36 | if (hint !== sendHints.blob) { | |
| 22 | - const data = clone(item, hint) | ||
| 23 | - | ||
| 24 | - if (this.#size === 0) { | ||
| 25 | - this.#dispatch(data, cb, hint) | ||
| 37 | + const frame = createFrame(item, hint) | ||
| 38 | + if (!this.#running) { | ||
| 39 | + // fast-path | ||
| 40 | + this.#socket.write(frame, cb) | ||
| 26 | 41 | } else { | |
| 27 | - this.#queued.add([data, cb, true, hint]) | ||
| 28 | - this.#size++ | ||
| 29 | - | ||
| 30 | - this.#run() | ||
| 42 | + /** @type {SendQueueNode} */ | ||
| 43 | + const node = { | ||
| 44 | + promise: null, | ||
| 45 | + callback: cb, | ||
| 46 | + frame | ||
| 47 | + } | ||
| 48 | + this.#queue.push(node) | ||
| 31 | 49 | } | |
| 32 | - | ||
| 33 | 50 | return | |
| 34 | 51 | } | |
| 35 | 52 | ||
| 36 | - const promise = item.arrayBuffer() | ||
| 37 | - const queue = [null, cb, false, hint] | ||
| 38 | - promise.then((ab) => { | ||
| 39 | - queue[0] = clone(ab, hint) | ||
| 40 | - queue[2] = true | ||
| 53 | + /** @type {SendQueueNode} */ | ||
| 54 | + const node = { | ||
| 55 | + promise: item.arrayBuffer().then((ab) => { | ||
| 56 | + node.promise = null | ||
| 57 | + node.frame = createFrame(ab, hint) | ||
| 58 | + }), | ||
| 59 | + callback: cb, | ||
| 60 | + frame: null | ||
| 61 | + } | ||
| 41 | 62 | ||
| 42 | - this.#run() | ||
| 43 | - }) | ||
| 63 | + this.#queue.push(node) | ||
| 44 | 64 | ||
| 45 | - this.#queued.add(queue) | ||
| 46 | - this.#size++ | ||
| 65 | + if (!this.#running) { | ||
| 66 | + this.#run() | ||
| 67 | + } | ||
| 47 | 68 | } | |
| 48 | 69 | ||
| 49 | - #run () { | ||
| 50 | - for (const queued of this.#queued) { | ||
| 51 | - const [data, cb, done, hint] = queued | ||
| 52 | - | ||
| 53 | - if (!done) return | ||
| 54 | - | ||
| 55 | - this.#queued.delete(queued) | ||
| 56 | - this.#size-- | ||
| 57 | - | ||
| 58 | - this.#dispatch(data, cb, hint) | ||
| 70 | + async #run () { | ||
| 71 | + this.#running = true | ||
| 72 | + const queue = this.#queue | ||
| 73 | + while (!queue.isEmpty()) { | ||
| 74 | + const node = queue.shift() | ||
| 75 | + // wait pending promise | ||
| 76 | + if (node.promise !== null) { | ||
| 77 | + await node.promise | ||
| 78 | + } | ||
| 79 | + // write | ||
| 80 | + this.#socket.write(node.frame, node.callback) | ||
| 81 | + // cleanup | ||
| 82 | + node.callback = node.frame = null | ||
| 59 | 83 | } | |
| 84 | + this.#running = false | ||
| 60 | 85 | } | |
| 86 | + } | ||
| 61 | 87 | ||
| 62 | - #dispatch (data, cb, hint) { | ||
| 63 | - const frame = new WebsocketFrameSend() | ||
| 64 | - const opcode = hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY | ||
| 65 | - | ||
| 66 | - frame.frameData = data | ||
| 67 | - const buffer = frame.createFrame(opcode) | ||
| 68 | - | ||
| 69 | - this.#socket.write(buffer, cb) | ||
| 70 | - } | ||
| 88 | + function createFrame (data, hint) { | ||
| 89 | + return new WebsocketFrameSend(toBuffer(data, hint)).createFrame(hint === sendHints.string ? opcodes.TEXT : opcodes.BINARY) | ||
| 71 | 90 | } | |
| 72 | 91 | ||
| 73 | - function clone (data, hint) { | ||
| 92 | + function toBuffer (data, hint) { | ||
| 74 | 93 | switch (hint) { | |
| 75 | 94 | case sendHints.string: | |
| 76 | 95 | return Buffer.from(data) | |
| 77 | 96 | case sendHints.arrayBuffer: | |
| 78 | 97 | case sendHints.blob: | |
| 79 | 98 | return new FastBuffer(data) | |
| 80 | 99 | case sendHints.typedArray: | |
| 81 | - return Buffer.copyBytesFrom(data) | ||
| 100 | + return new FastBuffer(data.buffer, data.byteOffset, data.byteLength) | ||
| 82 | 101 | } | |
| 83 | 102 | } | |
| 84 | 103 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments