Skip to content

Commit 79eb238

Browse files
committed
remove anti-pattern dispatcher hooks (#2723)
1 parent 3934694 commit 79eb238

21 files changed

+11
-374
lines changed

docs/docs/api/DiagnosticsChannel.md

-11
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@ diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
2727

2828
Note: a request is only loosely completed to a given socket.
2929

30-
31-
## `undici:request:bodySent`
32-
33-
```js
34-
import diagnosticsChannel from 'diagnostics_channel'
35-
36-
diagnosticsChannel.channel('undici:request:bodySent').subscribe(({ request }) => {
37-
// request is the same object undici:request:create
38-
})
39-
```
40-
4130
## `undici:request:headers`
4231

4332
This message is published after the response headers have been received, i.e. the response has been completed.

docs/docs/api/Dispatcher.md

-2
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,9 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo
209209
* **onConnect** `(abort: () => void, context: object) => void` - Invoked before request is dispatched on socket. May be invoked multiple times when a request is retried when the request at the head of the pipeline fails.
210210
* **onError** `(error: Error) => void` - Invoked when an error has occurred. May not throw.
211211
* **onUpgrade** `(statusCode: number, headers: Buffer[], socket: Duplex) => void` (optional) - Invoked when request is upgraded. Required if `DispatchOptions.upgrade` is defined or `DispatchOptions.method === 'CONNECT'`.
212-
* **onResponseStarted** `() => void` (optional) - Invoked when response is received, before headers have been read.
213212
* **onHeaders** `(statusCode: number, headers: Buffer[], resume: () => void, statusText: string) => boolean` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. Not required for `upgrade` requests.
214213
* **onData** `(chunk: Buffer) => boolean` - Invoked when response payload data is received. Not required for `upgrade` requests.
215214
* **onComplete** `(trailers: Buffer[]) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests.
216-
* **onBodySent** `(chunk: string | Buffer | Uint8Array) => void` - Invoked when a body chunk is sent to the server. Not required. For a stream or iterable body this will be invoked for every chunk. For other body types, it will be invoked once after the body is sent.
217215

218216
#### Example 1 - Dispatch GET request
219217

docs/docs/api/RedirectHandler.md

-8
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,3 @@ Called when the request is complete.
8686
Parameters:
8787

8888
- **trailers** `object` - The trailers received.
89-
90-
#### `onBodySent(chunk)`
91-
92-
Called when the request body is sent.
93-
94-
Parameters:
95-
96-
- **chunk** `Buffer` - The chunk of the request body sent.

docs/docs/api/RetryHandler.md

-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ const handler = new RetryHandler(
7373
},
7474
handler: {
7575
onConnect() {},
76-
onBodySent() {},
7776
onHeaders(status, _rawHeaders, resume, _statusMessage) {
7877
// do something with headers
7978
},
@@ -98,7 +97,6 @@ const handler = new RetryHandler(dispatchOptions, {
9897
dispatch: client.dispatch.bind(client),
9998
handler: {
10099
onConnect() {},
101-
onBodySent() {},
102100
onHeaders(status, _rawHeaders, resume, _statusMessage) {},
103101
onData(chunk) {},
104102
onComplete() {},

lib/core/diagnostics.js

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const channels = {
1414
sendHeaders: diagnosticsChannel.channel('undici:client:sendHeaders'),
1515
// Request
1616
create: diagnosticsChannel.channel('undici:request:create'),
17-
bodySent: diagnosticsChannel.channel('undici:request:bodySent'),
1817
headers: diagnosticsChannel.channel('undici:request:headers'),
1918
trailers: diagnosticsChannel.channel('undici:request:trailers'),
2019
error: diagnosticsChannel.channel('undici:request:error'),

lib/core/request.js

-28
Original file line numberDiff line numberDiff line change
@@ -191,30 +191,6 @@ class Request {
191191
}
192192
}
193193

194-
onBodySent (chunk) {
195-
if (this[kHandler].onBodySent) {
196-
try {
197-
return this[kHandler].onBodySent(chunk)
198-
} catch (err) {
199-
this.abort(err)
200-
}
201-
}
202-
}
203-
204-
onRequestSent () {
205-
if (channels.bodySent.hasSubscribers) {
206-
channels.bodySent.publish({ request: this })
207-
}
208-
209-
if (this[kHandler].onRequestSent) {
210-
try {
211-
return this[kHandler].onRequestSent()
212-
} catch (err) {
213-
this.abort(err)
214-
}
215-
}
216-
}
217-
218194
onConnect (abort) {
219195
assert(!this.aborted)
220196
assert(!this.completed)
@@ -227,10 +203,6 @@ class Request {
227203
}
228204
}
229205

230-
onResponseStarted () {
231-
return this[kHandler].onResponseStarted?.()
232-
}
233-
234206
onHeaders (statusCode, headers, resume, statusText) {
235207
assert(!this.aborted)
236208
assert(!this.completed)

lib/core/util.js

-4
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,6 @@ function validateHandler (handler, method, upgrade) {
331331
throw new InvalidArgumentError('invalid onError method')
332332
}
333333

334-
if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) {
335-
throw new InvalidArgumentError('invalid onBodySent method')
336-
}
337-
338334
if (upgrade || method === 'CONNECT') {
339335
if (typeof handler.onUpgrade !== 'function') {
340336
throw new InvalidArgumentError('invalid onUpgrade method')

lib/dispatcher/client.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ class Parser {
743743
if (!request) {
744744
return -1
745745
}
746-
request.onResponseStarted()
747746
}
748747

749748
onHeaderField (buf) {
@@ -1555,16 +1554,13 @@ function writeH1 (client, request) {
15551554
assert(contentLength === null, 'no body must not have content length')
15561555
socket.write(`${header}\r\n`, 'latin1')
15571556
}
1558-
request.onRequestSent()
15591557
} else if (util.isBuffer(body)) {
15601558
assert(contentLength === body.byteLength, 'buffer body must have content length')
15611559

15621560
socket.cork()
15631561
socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
15641562
socket.write(body)
15651563
socket.uncork()
1566-
request.onBodySent(body)
1567-
request.onRequestSent()
15681564
if (!expectsPayload) {
15691565
socket[kReset] = true
15701566
}
@@ -1739,7 +1735,6 @@ function writeH2 (client, request) {
17391735

17401736
stream.once('response', headers => {
17411737
const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers
1742-
request.onResponseStarted()
17431738

17441739
if (request.onHeaders(Number(statusCode), realHeaders, stream.resume.bind(stream), '') === false) {
17451740
stream.pause()
@@ -1820,15 +1815,13 @@ function writeH2 (client, request) {
18201815
function writeBodyH2 () {
18211816
/* istanbul ignore else: assertion */
18221817
if (!body) {
1823-
request.onRequestSent()
1818+
// Do nothing...
18241819
} else if (util.isBuffer(body)) {
18251820
assert(contentLength === body.byteLength, 'buffer body must have content length')
18261821
stream.cork()
18271822
stream.write(body)
18281823
stream.uncork()
18291824
stream.end()
1830-
request.onBodySent(body)
1831-
request.onRequestSent()
18321825
} else if (util.isBlobLike(body)) {
18331826
if (typeof body.stream === 'function') {
18341827
writeIterable({
@@ -1893,22 +1886,14 @@ function writeStream ({ h2stream, body, client, request, socket, contentLength,
18931886
if (err) {
18941887
util.destroy(body, err)
18951888
util.destroy(h2stream, err)
1896-
} else {
1897-
request.onRequestSent()
18981889
}
18991890
}
19001891
)
19011892

1902-
pipe.on('data', onPipeData)
19031893
pipe.once('end', () => {
1904-
pipe.removeListener('data', onPipeData)
19051894
util.destroy(pipe)
19061895
})
19071896

1908-
function onPipeData (chunk) {
1909-
request.onBodySent(chunk)
1910-
}
1911-
19121897
return
19131898
}
19141899

@@ -2034,9 +2019,6 @@ async function writeBlob ({ h2stream, body, client, request, socket, contentLeng
20342019
socket.uncork()
20352020
}
20362021

2037-
request.onBodySent(buffer)
2038-
request.onRequestSent()
2039-
20402022
if (!expectsPayload) {
20412023
socket[kReset] = true
20422024
}
@@ -2082,15 +2064,13 @@ async function writeIterable ({ h2stream, body, client, request, socket, content
20822064
}
20832065

20842066
const res = h2stream.write(chunk)
2085-
request.onBodySent(chunk)
20862067
if (!res) {
20872068
await waitForDrain()
20882069
}
20892070
}
20902071
} catch (err) {
20912072
h2stream.destroy(err)
20922073
} finally {
2093-
request.onRequestSent()
20942074
h2stream.end()
20952075
h2stream
20962076
.off('close', onDrain)
@@ -2141,7 +2121,7 @@ class AsyncWriter {
21412121
}
21422122

21432123
write (chunk) {
2144-
const { socket, request, contentLength, client, bytesWritten, expectsPayload, header } = this
2124+
const { socket, contentLength, client, bytesWritten, expectsPayload, header } = this
21452125

21462126
if (socket[kError]) {
21472127
throw socket[kError]
@@ -2189,8 +2169,6 @@ class AsyncWriter {
21892169

21902170
socket.uncork()
21912171

2192-
request.onBodySent(chunk)
2193-
21942172
if (!ret) {
21952173
if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) {
21962174
// istanbul ignore else: only for jest
@@ -2204,8 +2182,7 @@ class AsyncWriter {
22042182
}
22052183

22062184
end () {
2207-
const { socket, contentLength, client, bytesWritten, expectsPayload, header, request } = this
2208-
request.onRequestSent()
2185+
const { socket, contentLength, client, bytesWritten, expectsPayload, header } = this
22092186

22102187
socket[kWriting] = false
22112188

lib/handler/DecoratorHandler.js

-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,4 @@ module.exports = class DecoratorHandler {
2828
onComplete (...args) {
2929
return this.handler.onComplete(...args)
3030
}
31-
32-
onBodySent (...args) {
33-
return this.handler.onBodySent(...args)
34-
}
3531
}

lib/handler/RedirectHandler.js

-6
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ class RedirectHandler {
173173
this.handler.onComplete(trailers)
174174
}
175175
}
176-
177-
onBodySent (chunk) {
178-
if (this.handler.onBodySent) {
179-
this.handler.onBodySent(chunk)
180-
}
181-
}
182176
}
183177

184178
function parseLocation (statusCode, headers) {

lib/handler/RetryHandler.js

-10
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,6 @@ class RetryHandler {
7575
})
7676
}
7777

78-
onRequestSent () {
79-
if (this.handler.onRequestSent) {
80-
this.handler.onRequestSent()
81-
}
82-
}
83-
8478
onUpgrade (statusCode, headers, socket) {
8579
if (this.handler.onUpgrade) {
8680
this.handler.onUpgrade(statusCode, headers, socket)
@@ -95,10 +89,6 @@ class RetryHandler {
9589
}
9690
}
9791

98-
onBodySent (chunk) {
99-
if (this.handler.onBodySent) return this.handler.onBodySent(chunk)
100-
}
101-
10292
static [kRetryHandlerDefaultRetry] (err, { state, opts }, cb) {
10393
const { statusCode, code, headers } = err
10494
const { method, retryOptions } = opts

lib/web/fetch/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2109,15 +2109,13 @@ async function httpNetworkFetch (
21092109
timingInfo.finalNetworkRequestStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
21102110
},
21112111

2112-
onResponseStarted () {
2112+
onHeaders (status, rawHeaders, resume, statusText) {
21132113
// Set timingInfo’s final network-response start time to the coarsened shared current
21142114
// time given fetchParams’s cross-origin isolated capability, immediately after the
21152115
// user agent’s HTTP parser receives the first byte of the response (e.g., frame header
21162116
// bytes for HTTP/2 or response status line for HTTP/1.x).
21172117
timingInfo.finalNetworkResponseStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
2118-
},
21192118

2120-
onHeaders (status, rawHeaders, resume, statusText) {
21212119
if (status < 200) {
21222120
return
21232121
}

test/http2.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ test('Should handle h2 request with body (string or buffer) - dispatch', async t
819819
stream.end('hello h2!')
820820
})
821821

822-
t = tspl(t, { plan: 7 })
822+
t = tspl(t, { plan: 6 })
823823

824824
server.listen(0, () => {
825825
const client = new Client(`https://localhost:${server.address().port}`, {
@@ -857,9 +857,6 @@ test('Should handle h2 request with body (string or buffer) - dispatch', async t
857857
onData (chunk) {
858858
response.push(chunk)
859859
},
860-
onBodySent (body) {
861-
t.strictEqual(body.toString('utf-8'), expectedBody)
862-
},
863860
onComplete () {
864861
t.strictEqual(Buffer.concat(response).toString('utf-8'), 'hello h2!')
865862
t.strictEqual(

test/jest/interceptor.test.js

-8
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,6 @@ describe('interceptors with NtlmRequestHandler', () => {
143143
return this.handler.onComplete(...args)
144144
}
145145
}
146-
147-
onBodySent (...args) {
148-
if (this.requestCount < 2) {
149-
// Do nothing
150-
} else {
151-
return this.handler.onBodySent(...args)
152-
}
153-
}
154146
}
155147
let server
156148

0 commit comments

Comments
 (0)