Skip to content

Commit b860c03

Browse files
authored
feat(interceptors): migrate decorator handler to new hooks (#3905)
1 parent fb3138d commit b860c03

File tree

5 files changed

+452
-162
lines changed

5 files changed

+452
-162
lines changed

lib/handler/decorator-handler.js

+23-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const assert = require('node:assert')
4+
const WrapHandler = require('./wrap-handler')
45

56
/**
67
* @deprecated
@@ -9,63 +10,58 @@ module.exports = class DecoratorHandler {
910
#handler
1011
#onCompleteCalled = false
1112
#onErrorCalled = false
13+
#onResponseStartCalled = false
1214

1315
constructor (handler) {
1416
if (typeof handler !== 'object' || handler === null) {
1517
throw new TypeError('handler must be an object')
1618
}
17-
this.#handler = handler
19+
this.#handler = WrapHandler.wrap(handler)
1820
}
1921

20-
onConnect (...args) {
21-
return this.#handler.onConnect?.(...args)
22+
onRequestStart (...args) {
23+
this.#handler.onRequestStart?.(...args)
2224
}
2325

24-
onError (...args) {
25-
this.#onErrorCalled = true
26-
return this.#handler.onError?.(...args)
27-
}
28-
29-
onUpgrade (...args) {
26+
onRequestUpgrade (...args) {
3027
assert(!this.#onCompleteCalled)
3128
assert(!this.#onErrorCalled)
3229

33-
return this.#handler.onUpgrade?.(...args)
30+
return this.#handler.onRequestUpgrade?.(...args)
3431
}
3532

36-
onResponseStarted (...args) {
33+
onResponseStart (...args) {
3734
assert(!this.#onCompleteCalled)
3835
assert(!this.#onErrorCalled)
36+
assert(!this.#onResponseStartCalled)
3937

40-
return this.#handler.onResponseStarted?.(...args)
41-
}
42-
43-
onHeaders (...args) {
44-
assert(!this.#onCompleteCalled)
45-
assert(!this.#onErrorCalled)
38+
this.#onResponseStartCalled = true
4639

47-
return this.#handler.onHeaders?.(...args)
40+
return this.#handler.onResponseStart?.(...args)
4841
}
4942

50-
onData (...args) {
43+
onResponseData (...args) {
5144
assert(!this.#onCompleteCalled)
5245
assert(!this.#onErrorCalled)
5346

54-
return this.#handler.onData?.(...args)
47+
return this.#handler.onResponseData?.(...args)
5548
}
5649

57-
onComplete (...args) {
50+
onResponseEnd (...args) {
5851
assert(!this.#onCompleteCalled)
5952
assert(!this.#onErrorCalled)
6053

6154
this.#onCompleteCalled = true
62-
return this.#handler.onComplete?.(...args)
55+
return this.#handler.onResponseEnd?.(...args)
6356
}
6457

65-
onBodySent (...args) {
66-
assert(!this.#onCompleteCalled)
67-
assert(!this.#onErrorCalled)
68-
69-
return this.#handler.onBodySent?.(...args)
58+
onResponseError (...args) {
59+
this.#onErrorCalled = true
60+
return this.#handler.onResponseError?.(...args)
7061
}
62+
63+
/**
64+
* @deprecated
65+
*/
66+
onBodySent () {}
7167
}

lib/interceptor/dns.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
const { isIP } = require('node:net')
33
const { lookup } = require('node:dns')
44
const DecoratorHandler = require('../handler/decorator-handler')
5-
const WrapHandler = require('../handler/wrap-handler')
65
const { InvalidArgumentError, InformationalError } = require('../core/errors')
76
const maxInt = Math.pow(2, 31) - 1
87

@@ -223,39 +222,17 @@ class DNSDispatchHandler extends DecoratorHandler {
223222
#state = null
224223
#opts = null
225224
#dispatch = null
226-
#handler = null
227225
#origin = null
228226
#controller = null
229227

230228
constructor (state, { origin, handler, dispatch }, opts) {
231229
super(handler)
232230
this.#origin = origin
233-
this.#handler = WrapHandler.wrap(handler)
234231
this.#opts = { ...opts }
235232
this.#state = state
236233
this.#dispatch = dispatch
237234
}
238235

239-
onRequestStart (controller, context) {
240-
this.#handler.onRequestStart?.(controller, context)
241-
}
242-
243-
onRequestUpgrade (controller, statusCode, headers, socket) {
244-
this.handler.onRequestUpgrade?.(controller, statusCode, headers, socket)
245-
}
246-
247-
onResponseStart (controller, statusCode, headers, statusMessage) {
248-
this.#handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
249-
}
250-
251-
onResponseData (controller, data) {
252-
this.#handler.onResponseData?.(controller, data)
253-
}
254-
255-
onResponseEnd (controller, trailers) {
256-
this.#handler.onResponseEnd?.(controller, trailers)
257-
}
258-
259236
onResponseError (controller, err) {
260237
switch (err.code) {
261238
case 'ETIMEDOUT':
@@ -264,7 +241,7 @@ class DNSDispatchHandler extends DecoratorHandler {
264241
// We delete the record and retry
265242
this.#state.runLookup(this.#origin, this.#opts, (err, newOrigin) => {
266243
if (err) {
267-
this.#handler.onResponseError(controller, err)
244+
super.onResponseError(controller, err)
268245
return
269246
}
270247

@@ -280,14 +257,14 @@ class DNSDispatchHandler extends DecoratorHandler {
280257
}
281258

282259
// if dual-stack disabled, we error out
283-
this.#handler.onResponseError(controller, err)
260+
super.onResponseError(controller, err)
284261
break
285262
}
286263
case 'ENOTFOUND':
287264
this.#state.deleteRecord(this.#origin)
288265
// eslint-disable-next-line no-fallthrough
289266
default:
290-
this.#handler.onResponseError(controller, err)
267+
super.onResponseError(controller, err)
291268
break
292269
}
293270
}

lib/interceptor/dump.js

+29-41
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
'use strict'
22

3-
const util = require('../core/util')
43
const { InvalidArgumentError, RequestAbortedError } = require('../core/errors')
54
const DecoratorHandler = require('../handler/decorator-handler')
65

76
class DumpHandler extends DecoratorHandler {
87
#maxSize = 1024 * 1024
9-
#abort = null
108
#dumped = false
11-
#aborted = false
129
#size = 0
13-
#reason = null
14-
#handler = null
10+
#controller = null
11+
aborted = false
12+
reason = false
1513

16-
constructor ({ maxSize }, handler) {
14+
constructor ({ maxSize, signal }, handler) {
1715
if (maxSize != null && (!Number.isFinite(maxSize) || maxSize < 1)) {
1816
throw new InvalidArgumentError('maxSize must be a number greater than 0')
1917
}
2018

2119
super(handler)
2220

2321
this.#maxSize = maxSize ?? this.#maxSize
24-
this.#handler = handler
22+
// this.#handler = handler
2523
}
2624

27-
onConnect (abort) {
28-
this.#abort = abort
29-
30-
this.#handler.onConnect(this.#customAbort.bind(this))
25+
#abort (reason) {
26+
this.aborted = true
27+
this.reason = reason
3128
}
3229

33-
#customAbort (reason) {
34-
this.#aborted = true
35-
this.#reason = reason
30+
onRequestStart (controller, context) {
31+
controller.abort = this.#abort.bind(this)
32+
this.#controller = controller
33+
34+
return super.onRequestStart(controller, context)
3635
}
3736

38-
// TODO: will require adjustment after new hooks are out
39-
onHeaders (statusCode, rawHeaders, resume, statusMessage) {
40-
const headers = util.parseHeaders(rawHeaders)
37+
onResponseStart (controller, statusCode, headers, statusMessage) {
4138
const contentLength = headers['content-length']
4239

4340
if (contentLength != null && contentLength > this.#maxSize) {
@@ -48,55 +45,50 @@ class DumpHandler extends DecoratorHandler {
4845
)
4946
}
5047

51-
if (this.#aborted) {
48+
if (this.aborted === true) {
5249
return true
5350
}
5451

55-
return this.#handler.onHeaders(
56-
statusCode,
57-
rawHeaders,
58-
resume,
59-
statusMessage
60-
)
52+
return super.onResponseStart(controller, statusCode, headers, statusMessage)
6153
}
6254

63-
onError (err) {
55+
onResponseError (controller, err) {
6456
if (this.#dumped) {
6557
return
6658
}
6759

68-
err = this.#reason ?? err
60+
err = this.#controller.reason ?? err
6961

70-
this.#handler.onError(err)
62+
super.onResponseError(controller, err)
7163
}
7264

73-
onData (chunk) {
65+
onResponseData (controller, chunk) {
7466
this.#size = this.#size + chunk.length
7567

7668
if (this.#size >= this.#maxSize) {
7769
this.#dumped = true
7870

79-
if (this.#aborted) {
80-
this.#handler.onError(this.#reason)
71+
if (this.aborted === true) {
72+
super.onResponseError(controller, this.reason)
8173
} else {
82-
this.#handler.onComplete([])
74+
super.onResponseEnd(controller, {})
8375
}
8476
}
8577

8678
return true
8779
}
8880

89-
onComplete (trailers) {
81+
onResponseEnd (controller, trailers) {
9082
if (this.#dumped) {
9183
return
9284
}
9385

94-
if (this.#aborted) {
95-
this.#handler.onError(this.reason)
86+
if (this.#controller.aborted === true) {
87+
super.onResponseError(controller, this.reason)
9688
return
9789
}
9890

99-
this.#handler.onComplete(trailers)
91+
super.onResponseEnd(controller, trailers)
10092
}
10193
}
10294

@@ -107,13 +99,9 @@ function createDumpInterceptor (
10799
) {
108100
return dispatch => {
109101
return function Intercept (opts, handler) {
110-
const { dumpMaxSize = defaultMaxSize } =
111-
opts
102+
const { dumpMaxSize = defaultMaxSize } = opts
112103

113-
const dumpHandler = new DumpHandler(
114-
{ maxSize: dumpMaxSize },
115-
handler
116-
)
104+
const dumpHandler = new DumpHandler({ maxSize: dumpMaxSize, signal: opts.signal }, handler)
117105

118106
return dispatch(opts, dumpHandler)
119107
}

0 commit comments

Comments
 (0)