-
-
Notifications
You must be signed in to change notification settings - Fork 664
feat: dump interceptor #3118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: dump interceptor #3118
Changes from 8 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
6d975ea
feat: add dump interceptor
metcoder95 5024ae1
fix(types): interceptor definitions
metcoder95 befe478
Merge branch 'main' into feat/dump_interceptor
metcoder95 da70cbd
docs: update maxSize description
metcoder95 62a4cde
Merge branch 'main' into feat/dump_interceptor
metcoder95 d82b51b
refactor: leftover
metcoder95 33b7126
docs: adjust
metcoder95 e3190f1
feat: abort on dumped
metcoder95 2fd173d
fix: return on header
metcoder95 85af461
refactor: apply suggestions
metcoder95 f5cc824
feat: extend from decorator handler
metcoder95 675c261
Merge branch 'main' into feat/dump_interceptor
metcoder95 c43c80c
fix: missing handler
metcoder95 8548c8e
feat: add dumpOnAbort
metcoder95 84c0d8b
Merge branch 'main' into feat/dump_interceptor
metcoder95 13ba7e0
Merge branch 'main' into feat/dump_interceptor
metcoder95 bf75a5b
test: adjust test
metcoder95 8607411
fix: bad consumer
metcoder95 428a953
Merge branch 'main' into feat/dump_interceptor
metcoder95 8fae866
refactor: tweaks
metcoder95 4492ab7
Merge branch 'main' into feat/dump_interceptor
metcoder95 cc42e2d
test: simplify
metcoder95 c60ee90
test: disable on windows
metcoder95 38b24fb
Merge branch 'main' into feat/dump_interceptor
metcoder95 fb4ccd8
refactoor Apply suggestions from code review
metcoder95 0d67095
chore: dump on abort by default
metcoder95 e60add7
fix: typo
metcoder95 3a68023
fix: test
metcoder95 3e3295f
refactor: Apply suggestions from code review
metcoder95 2265a19
fix: lint
metcoder95 ae685cf
fix: refactor
metcoder95 85ab3d6
Merge branch 'main' into feat/dump_interceptor
metcoder95 fb5af55
fix: cleanup leftovers
metcoder95 ab67b1b
Merge branch 'main' into feat/dump_interceptor
metcoder95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,170 @@ | ||||||||||||||||||
'use strict' | ||||||||||||||||||
|
||||||||||||||||||
const util = require('../core/util') | ||||||||||||||||||
const { InvalidArgumentError, RequestAbortedError } = require('../core/errors') | ||||||||||||||||||
|
||||||||||||||||||
class DumpHandler { | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
#maxSize = 1024 * 1024 | ||||||||||||||||||
#abort = null | ||||||||||||||||||
#abortOnDumped = true | ||||||||||||||||||
#waitForTrailers = false | ||||||||||||||||||
#hasTrailers = false | ||||||||||||||||||
#dumped = false | ||||||||||||||||||
#aborted = false | ||||||||||||||||||
#completed = false | ||||||||||||||||||
#size = 0 | ||||||||||||||||||
#reason = null | ||||||||||||||||||
#handler = null | ||||||||||||||||||
|
||||||||||||||||||
constructor ({ maxSize, abortOnDumped, waitForTrailers }, handler) { | ||||||||||||||||||
if (maxSize != null && (!Number.isFinite(maxSize) || maxSize < 1)) { | ||||||||||||||||||
throw new InvalidArgumentError('maxSize must be a number greater than 0') | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (abortOnDumped != null && typeof abortOnDumped !== 'boolean') { | ||||||||||||||||||
throw new InvalidArgumentError('abortOnDumped must be a boolean') | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (waitForTrailers != null && typeof waitForTrailers !== 'boolean') { | ||||||||||||||||||
throw new InvalidArgumentError('waitForTrailers must be a boolean') | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
this.#maxSize = maxSize ?? this.#maxSize | ||||||||||||||||||
this.#abortOnDumped = abortOnDumped ?? this.#abortOnDumped | ||||||||||||||||||
this.#waitForTrailers = waitForTrailers ?? this.#waitForTrailers | ||||||||||||||||||
this.#handler = handler | ||||||||||||||||||
|
||||||||||||||||||
// Handle possible onConnect duplication | ||||||||||||||||||
this.#handler.onConnect(reason => { | ||||||||||||||||||
this.#aborted = true | ||||||||||||||||||
if (this.#abort != null) { | ||||||||||||||||||
this.#abort(reason) | ||||||||||||||||||
} else { | ||||||||||||||||||
this.#reason = reason | ||||||||||||||||||
} | ||||||||||||||||||
}) | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onConnect (...args) { | ||||||||||||||||||
const [abort] = args | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
if (this.#aborted) { | ||||||||||||||||||
abort(this.#reason) | ||||||||||||||||||
return | ||||||||||||||||||
} | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This can't happen? |
||||||||||||||||||
|
||||||||||||||||||
this.#abort = abort | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onResponseStarted () { | ||||||||||||||||||
this.#handler.onResponseStarted?.() | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onBodySent () { | ||||||||||||||||||
this.#handler.onBodySent?.() | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onUpgrade (statusCode, headers, socket) { | ||||||||||||||||||
this.#handler.onUpgrade?.(statusCode, headers, socket) | ||||||||||||||||||
} | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
// TODO: will require adjustment after new hooks are out | ||||||||||||||||||
onHeaders (statusCode, rawHeaders, resume, statusMessage) { | ||||||||||||||||||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
const headers = util.parseHeaders(rawHeaders) | ||||||||||||||||||
const contentLength = headers['content-length'] | ||||||||||||||||||
|
||||||||||||||||||
if (contentLength != null && contentLength > this.#maxSize) { | ||||||||||||||||||
this.#reason = new RequestAbortedError( | ||||||||||||||||||
`Response size (${contentLength}) larger than maxSize (${ | ||||||||||||||||||
this.#maxSize | ||||||||||||||||||
})` | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
this.#abort(this.#reason) | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
return | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (this.#waitForTrailers) { | ||||||||||||||||||
this.#hasTrailers = headers.trailer != null | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
this.#handler.onHeaders(statusCode, rawHeaders, resume, statusMessage) | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onError (err) { | ||||||||||||||||||
if ( | ||||||||||||||||||
!(err instanceof RequestAbortedError) && | ||||||||||||||||||
(!this.#dumped || this.#aborted) | ||||||||||||||||||
) { | ||||||||||||||||||
this.#handler.onError(err) | ||||||||||||||||||
return | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
if (!this.#completed) { | ||||||||||||||||||
this.#handler.onComplete([]) | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onData (chunk) { | ||||||||||||||||||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
this.#size = this.#size + chunk.length | ||||||||||||||||||
|
||||||||||||||||||
if (this.#size >= this.#maxSize) { | ||||||||||||||||||
this.#dumped = true | ||||||||||||||||||
|
||||||||||||||||||
if (this.#abortOnDumped && (!this.#waitForTrailers || !this.#hasTrailers)) { | ||||||||||||||||||
console.log('dumped') | ||||||||||||||||||
this.#reason = new RequestAbortedError( | ||||||||||||||||||
`Response dumped (${this.#size}) for max size (${this.#maxSize})` | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
this.#abort(this.#reason) | ||||||||||||||||||
return false | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
return true | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
onComplete (trailers) { | ||||||||||||||||||
this.#completed = true | ||||||||||||||||||
this.#handler.onComplete(trailers) | ||||||||||||||||||
|
||||||||||||||||||
if (this.#dumped && this.#abortOnDumped) { | ||||||||||||||||||
this.#reason = new RequestAbortedError( | ||||||||||||||||||
`Response dumped (${this.#size}) for max size (${this.#maxSize})` | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
this.#abort(this.#reason) | ||||||||||||||||||
metcoder95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
function createDumpInterceptor ( | ||||||||||||||||||
{ | ||||||||||||||||||
maxSize: defaultMaxSize, | ||||||||||||||||||
abortOnDumped: defaultAbortOnDumped, | ||||||||||||||||||
waitForTrailers: defaultWaitForTrailers | ||||||||||||||||||
} = { | ||||||||||||||||||
maxSize: 1024 * 1024, | ||||||||||||||||||
abortOnDumped: true, | ||||||||||||||||||
waitForTrailers: false | ||||||||||||||||||
} | ||||||||||||||||||
) { | ||||||||||||||||||
return dispatch => { | ||||||||||||||||||
return function Intercept (opts, handler) { | ||||||||||||||||||
const { | ||||||||||||||||||
dumpMaxSize = defaultMaxSize, | ||||||||||||||||||
abortOnDumped = defaultAbortOnDumped, | ||||||||||||||||||
waitForTrailers = defaultWaitForTrailers | ||||||||||||||||||
} = opts | ||||||||||||||||||
|
||||||||||||||||||
const dumpHandler = new DumpHandler( | ||||||||||||||||||
{ maxSize: dumpMaxSize, abortOnDumped, waitForTrailers }, | ||||||||||||||||||
handler | ||||||||||||||||||
) | ||||||||||||||||||
|
||||||||||||||||||
return dispatch(opts, dumpHandler) | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
module.exports = createDumpInterceptor |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.