Skip to content

Commit ddabe91

Browse files
committed
remove anti-pattern dispatcher hooks (#2723)
1 parent eb884c5 commit ddabe91

21 files changed

+12
-375
lines changed

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/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/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/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/client.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ class Parser {
728728
if (!request) {
729729
return -1
730730
}
731-
request.onResponseStarted()
732731
}
733732

734733
onHeaderField (buf) {
@@ -1626,16 +1625,13 @@ function write (client, request) {
16261625
assert(contentLength === null, 'no body must not have content length')
16271626
socket.write(`${header}\r\n`, 'latin1')
16281627
}
1629-
request.onRequestSent()
16301628
} else if (util.isBuffer(body)) {
16311629
assert(contentLength === body.byteLength, 'buffer body must have content length')
16321630

16331631
socket.cork()
16341632
socket.write(`${header}content-length: ${contentLength}\r\n\r\n`, 'latin1')
16351633
socket.write(body)
16361634
socket.uncork()
1637-
request.onBodySent(body)
1638-
request.onRequestSent()
16391635
if (!expectsPayload) {
16401636
socket[kReset] = true
16411637
}
@@ -1809,7 +1805,6 @@ function writeH2 (client, session, request) {
18091805

18101806
stream.once('response', headers => {
18111807
const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers
1812-
request.onResponseStarted()
18131808

18141809
if (request.onHeaders(Number(statusCode), realHeaders, stream.resume.bind(stream), '') === false) {
18151810
stream.pause()
@@ -1890,15 +1885,13 @@ function writeH2 (client, session, request) {
18901885
function writeBodyH2 () {
18911886
/* istanbul ignore else: assertion */
18921887
if (!body) {
1893-
request.onRequestSent()
1888+
// Do nothing...
18941889
} else if (util.isBuffer(body)) {
18951890
assert(contentLength === body.byteLength, 'buffer body must have content length')
18961891
stream.cork()
18971892
stream.write(body)
18981893
stream.uncork()
18991894
stream.end()
1900-
request.onBodySent(body)
1901-
request.onRequestSent()
19021895
} else if (util.isBlobLike(body)) {
19031896
if (typeof body.stream === 'function') {
19041897
writeIterable({
@@ -1963,22 +1956,14 @@ function writeStream ({ h2stream, body, client, request, socket, contentLength,
19631956
if (err) {
19641957
util.destroy(body, err)
19651958
util.destroy(h2stream, err)
1966-
} else {
1967-
request.onRequestSent()
19681959
}
19691960
}
19701961
)
19711962

1972-
pipe.on('data', onPipeData)
19731963
pipe.once('end', () => {
1974-
pipe.removeListener('data', onPipeData)
19751964
util.destroy(pipe)
19761965
})
19771966

1978-
function onPipeData (chunk) {
1979-
request.onBodySent(chunk)
1980-
}
1981-
19821967
return
19831968
}
19841969

@@ -2104,9 +2089,6 @@ async function writeBlob ({ h2stream, body, client, request, socket, contentLeng
21042089
socket.uncork()
21052090
}
21062091

2107-
request.onBodySent(buffer)
2108-
request.onRequestSent()
2109-
21102092
if (!expectsPayload) {
21112093
socket[kReset] = true
21122094
}
@@ -2152,15 +2134,13 @@ async function writeIterable ({ h2stream, body, client, request, socket, content
21522134
}
21532135

21542136
const res = h2stream.write(chunk)
2155-
request.onBodySent(chunk)
21562137
if (!res) {
21572138
await waitForDrain()
21582139
}
21592140
}
21602141
} catch (err) {
21612142
h2stream.destroy(err)
21622143
} finally {
2163-
request.onRequestSent()
21642144
h2stream.end()
21652145
h2stream
21662146
.off('close', onDrain)
@@ -2211,7 +2191,7 @@ class AsyncWriter {
22112191
}
22122192

22132193
write (chunk) {
2214-
const { socket, request, contentLength, client, bytesWritten, expectsPayload, header } = this
2194+
const { socket, contentLength, client, bytesWritten, expectsPayload, header } = this
22152195

22162196
if (socket[kError]) {
22172197
throw socket[kError]
@@ -2259,8 +2239,6 @@ class AsyncWriter {
22592239

22602240
socket.uncork()
22612241

2262-
request.onBodySent(chunk)
2263-
22642242
if (!ret) {
22652243
if (socket[kParser].timeout && socket[kParser].timeoutType === TIMEOUT_HEADERS) {
22662244
// istanbul ignore else: only for jest
@@ -2274,8 +2252,7 @@ class AsyncWriter {
22742252
}
22752253

22762254
end () {
2277-
const { socket, contentLength, client, bytesWritten, expectsPayload, header, request } = this
2278-
request.onRequestSent()
2255+
const { socket, contentLength, client, bytesWritten, expectsPayload, header } = this
22792256

22802257
socket[kWriting] = false
22812258

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
@@ -323,10 +323,6 @@ function validateHandler (handler, method, upgrade) {
323323
throw new InvalidArgumentError('invalid onError method')
324324
}
325325

326-
if (typeof handler.onBodySent !== 'function' && handler.onBodySent !== undefined) {
327-
throw new InvalidArgumentError('invalid onBodySent method')
328-
}
329-
330326
if (upgrade || method === 'CONNECT') {
331327
if (typeof handler.onUpgrade !== 'function') {
332328
throw new InvalidArgumentError('invalid onUpgrade method')

lib/fetch/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -2114,15 +2114,13 @@ async function httpNetworkFetch (
21142114
timingInfo.finalNetworkRequestStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
21152115
},
21162116

2117-
onResponseStarted () {
2117+
onHeaders (status, rawHeaders, resume, statusText) {
21182118
// Set timingInfo’s final network-response start time to the coarsened shared current
21192119
// time given fetchParams’s cross-origin isolated capability, immediately after the
21202120
// user agent’s HTTP parser receives the first byte of the response (e.g., frame header
21212121
// bytes for HTTP/2 or response status line for HTTP/1.x).
21222122
timingInfo.finalNetworkResponseStartTime = coarsenedSharedCurrentTime(fetchParams.crossOriginIsolatedCapability)
2123-
},
21242123

2125-
onHeaders (status, rawHeaders, resume, statusText) {
21262124
if (status < 200) {
21272125
return
21282126
}

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
@@ -74,12 +74,6 @@ class RetryHandler {
7474
})
7575
}
7676

77-
onRequestSent () {
78-
if (this.handler.onRequestSent) {
79-
this.handler.onRequestSent()
80-
}
81-
}
82-
8377
onUpgrade (statusCode, headers, socket) {
8478
if (this.handler.onUpgrade) {
8579
this.handler.onUpgrade(statusCode, headers, socket)
@@ -94,10 +88,6 @@ class RetryHandler {
9488
}
9589
}
9690

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

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)