aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/undici/lib/interceptor/dump.js
diff options
context:
space:
mode:
authorAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
committerAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
commit76cb9c2a39d477a64824a985ade40507e3bbade1 (patch)
tree41e997aa9c6f538d3a136af61dae9424db2005a9 /vanilla/node_modules/undici/lib/interceptor/dump.js
parent819a39a21ac992b1393244a4c283bbb125208c69 (diff)
downloadneko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.gz
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.bz2
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.zip
feat(vanilla): add testing infrastructure and tests (NK-wjnczv)
Diffstat (limited to 'vanilla/node_modules/undici/lib/interceptor/dump.js')
-rw-r--r--vanilla/node_modules/undici/lib/interceptor/dump.js112
1 files changed, 112 insertions, 0 deletions
diff --git a/vanilla/node_modules/undici/lib/interceptor/dump.js b/vanilla/node_modules/undici/lib/interceptor/dump.js
new file mode 100644
index 0000000..4810a09
--- /dev/null
+++ b/vanilla/node_modules/undici/lib/interceptor/dump.js
@@ -0,0 +1,112 @@
+'use strict'
+
+const { InvalidArgumentError, RequestAbortedError } = require('../core/errors')
+const DecoratorHandler = require('../handler/decorator-handler')
+
+class DumpHandler extends DecoratorHandler {
+ #maxSize = 1024 * 1024
+ #dumped = false
+ #size = 0
+ #controller = null
+ aborted = false
+ reason = false
+
+ constructor ({ maxSize, signal }, handler) {
+ if (maxSize != null && (!Number.isFinite(maxSize) || maxSize < 1)) {
+ throw new InvalidArgumentError('maxSize must be a number greater than 0')
+ }
+
+ super(handler)
+
+ this.#maxSize = maxSize ?? this.#maxSize
+ // this.#handler = handler
+ }
+
+ #abort (reason) {
+ this.aborted = true
+ this.reason = reason
+ }
+
+ onRequestStart (controller, context) {
+ controller.abort = this.#abort.bind(this)
+ this.#controller = controller
+
+ return super.onRequestStart(controller, context)
+ }
+
+ onResponseStart (controller, statusCode, headers, statusMessage) {
+ const contentLength = headers['content-length']
+
+ if (contentLength != null && contentLength > this.#maxSize) {
+ throw new RequestAbortedError(
+ `Response size (${contentLength}) larger than maxSize (${
+ this.#maxSize
+ })`
+ )
+ }
+
+ if (this.aborted === true) {
+ return true
+ }
+
+ return super.onResponseStart(controller, statusCode, headers, statusMessage)
+ }
+
+ onResponseError (controller, err) {
+ if (this.#dumped) {
+ return
+ }
+
+ // On network errors before connect, controller will be null
+ err = this.#controller?.reason ?? err
+
+ super.onResponseError(controller, err)
+ }
+
+ onResponseData (controller, chunk) {
+ this.#size = this.#size + chunk.length
+
+ if (this.#size >= this.#maxSize) {
+ this.#dumped = true
+
+ if (this.aborted === true) {
+ super.onResponseError(controller, this.reason)
+ } else {
+ super.onResponseEnd(controller, {})
+ }
+ }
+
+ return true
+ }
+
+ onResponseEnd (controller, trailers) {
+ if (this.#dumped) {
+ return
+ }
+
+ if (this.#controller.aborted === true) {
+ super.onResponseError(controller, this.reason)
+ return
+ }
+
+ super.onResponseEnd(controller, trailers)
+ }
+}
+
+function createDumpInterceptor (
+ { maxSize: defaultMaxSize } = {
+ maxSize: 1024 * 1024
+ }
+) {
+ return dispatch => {
+ return function Intercept (opts, handler) {
+ const { dumpMaxSize = defaultMaxSize } = opts
+
+ const dumpHandler = new DumpHandler({ maxSize: dumpMaxSize, signal: opts.signal }, handler)
+
+ return dispatch(opts, dumpHandler)
+ }
+ }
+}
+
+module.exports = createDumpInterceptor