Skip to content

Commit

Permalink
refactor(nodejs#2722)!: Drop Interceptors support (nodejs#2754)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored and KhafraDev committed Jun 24, 2024
1 parent 9f8572c commit c2ba195
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 237 deletions.
2 changes: 0 additions & 2 deletions docs/docs/api/Agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Returns: `Agent`
Extends: [`PoolOptions`](Pool.md#parameter-pooloptions)

* **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
* **maxRedirections** `Integer` - Default: `0`. The number of HTTP redirection to follow unless otherwise specified in `DispatchOptions`.
* **interceptors** `{ Agent: DispatchInterceptor[] }` - Default: `[RedirectInterceptor]` - A list of interceptors that are applied to the dispatch method. Additional logic can be applied (such as, but not limited to: 302 status code handling, authentication, cookies, compression and caching). Note that the behavior of interceptors is Experimental and might change at any given time.

## Instance Properties

Expand Down
1 change: 0 additions & 1 deletion docs/docs/api/Pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Extends: [`ClientOptions`](Client.md#parameter-clientoptions)

* **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Client(origin, opts)`
* **connections** `number | null` (optional) - Default: `null` - The number of `Client` instances to create. When set to `null`, the `Pool` instance will create an unlimited amount of `Client` instances.
* **interceptors** `{ Pool: DispatchInterceptor[] } }` - Default: `{ Pool: [] }` - A list of interceptors that are applied to the dispatch method. Additional logic can be applied (such as, but not limited to: 302 status code handling, authentication, cookies, compression and caching).

## Instance Properties

Expand Down
14 changes: 13 additions & 1 deletion lib/api/api-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('node:assert')
const { AsyncResource } = require('node:async_hooks')
const { InvalidArgumentError, SocketError } = require('../core/errors')
const util = require('../core/util')
const RedirectHandler = require('../handler/RedirectHandler')
const { addSignal, removeSignal } = require('./abort-signal')

class ConnectHandler extends AsyncResource {
Expand Down Expand Up @@ -95,7 +96,18 @@ function connect (opts, callback) {

try {
const connectHandler = new ConnectHandler(opts, callback)
this.dispatch({ ...opts, method: 'CONNECT' }, connectHandler)
const connectOptions = { ...opts, method: 'CONNECT' }

if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

if (opts?.maxRedirections > 0) {
RedirectHandler.buildDispatch(this, opts.maxRedirections)(connectOptions, connectHandler)
return
}

this.dispatch(connectOptions, connectHandler)
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
17 changes: 14 additions & 3 deletions lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ const {
Duplex,
PassThrough
} = require('node:stream')
const assert = require('node:assert')
const { AsyncResource } = require('node:async_hooks')
const {
InvalidArgumentError,
InvalidReturnValueError,
RequestAbortedError
} = require('../core/errors')
const util = require('../core/util')
const { AsyncResource } = require('node:async_hooks')
const RedirectHandler = require('../handler/RedirectHandler')
const { addSignal, removeSignal } = require('./abort-signal')
const assert = require('node:assert')

const kResume = Symbol('resume')

Expand Down Expand Up @@ -241,7 +242,17 @@ class PipelineHandler extends AsyncResource {
function pipeline (opts, handler) {
try {
const pipelineHandler = new PipelineHandler(opts, handler)
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)

if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

if (opts?.maxRedirections > 0) {
RedirectHandler.buildDispatch(this, opts.maxRedirections)({ ...opts, body: pipelineHandler.req }, pipelineHandler)
} else {
this.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)
}

return pipelineHandler.ret
} catch (err) {
return new PassThrough().destroy(err)
Expand Down
27 changes: 26 additions & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
'use strict'

<<<<<<< HEAD
const assert = require('node:assert')
const { Readable } = require('./readable')
const { InvalidArgumentError, RequestAbortedError } = require('../core/errors')
=======
const { AsyncResource } = require('node:async_hooks')
const Readable = require('./readable')
const {
InvalidArgumentError,
RequestAbortedError
} = require('../core/errors')
>>>>>>> 6c405130... refactor(#2722)!: Drop Interceptors support (#2754)
const util = require('../core/util')
const RedirectHandler = require('../handler/RedirectHandler')
const { getResolveErrorBodyCallback } = require('./util')
<<<<<<< HEAD
const { AsyncResource } = require('node:async_hooks')
=======
const { addSignal, removeSignal } = require('./abort-signal')
>>>>>>> 6c405130... refactor(#2722)!: Drop Interceptors support (#2754)

class RequestHandler extends AsyncResource {
constructor (opts, callback) {
Expand Down Expand Up @@ -194,7 +208,18 @@ function request (opts, callback) {
}

try {
this.dispatch(opts, new RequestHandler(opts, callback))
const handler = new RequestHandler(opts, callback)

if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

if (opts?.maxRedirections > 0) {
RedirectHandler.buildDispatch(this, opts.maxRedirections)(opts, handler)
return
}

this.dispatch(opts, handler)
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
14 changes: 13 additions & 1 deletion lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const assert = require('node:assert')
const { finished, PassThrough } = require('node:stream')
const { InvalidArgumentError, InvalidReturnValueError } = require('../core/errors')
const util = require('../core/util')
const RedirectHandler = require('../handler/RedirectHandler')
const { getResolveErrorBodyCallback } = require('./util')
const { AsyncResource } = require('node:async_hooks')
const { addSignal, removeSignal } = require('./abort-signal')
Expand Down Expand Up @@ -207,7 +208,18 @@ function stream (opts, factory, callback) {
}

try {
this.dispatch(opts, new StreamHandler(opts, factory, callback))
const handler = new StreamHandler(opts, factory, callback)

if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

if (opts?.maxRedirections > 0) {
RedirectHandler.buildDispatch(this, opts.maxRedirections)(opts, handler)
return
}

this.dispatch(opts, handler)
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
22 changes: 19 additions & 3 deletions lib/api/api-upgrade.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
'use strict'

<<<<<<< HEAD
const { InvalidArgumentError, SocketError } = require('../core/errors')
=======
const assert = require('node:assert')
>>>>>>> 6c405130... refactor(#2722)!: Drop Interceptors support (#2754)
const { AsyncResource } = require('node:async_hooks')
const { InvalidArgumentError, RequestAbortedError, SocketError } = require('../core/errors')
const util = require('../core/util')
const RedirectHandler = require('../handler/RedirectHandler')
const { addSignal, removeSignal } = require('./abort-signal')
const assert = require('node:assert')

class UpgradeHandler extends AsyncResource {
constructor (opts, callback) {
Expand Down Expand Up @@ -91,11 +96,22 @@ function upgrade (opts, callback) {

try {
const upgradeHandler = new UpgradeHandler(opts, callback)
this.dispatch({
const upgradeOpts = {
...opts,
method: opts.method || 'GET',
upgrade: opts.protocol || 'Websocket'
}, upgradeHandler)
}

if (opts?.maxRedirections != null && (!Number.isInteger(opts?.maxRedirections) || opts?.maxRedirections < 0)) {
throw new InvalidArgumentError('maxRedirections must be a positive number')
}

if (opts?.maxRedirections > 0) {
RedirectHandler.buildDispatch(this, opts.maxRedirections)(upgradeOpts, upgradeHandler)
return
}

this.dispatch(upgradeOpts, upgradeHandler)
} catch (err) {
if (typeof callback !== 'function') {
throw err
Expand Down
1 change: 0 additions & 1 deletion lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ module.exports = {
kMaxRequests: Symbol('maxRequestsPerClient'),
kProxy: Symbol('proxy agent options'),
kCounter: Symbol('socket request counter'),
kInterceptors: Symbol('dispatch interceptors'),
kMaxResponseSize: Symbol('max response size'),
kHTTP2Session: Symbol('http2Session'),
kHTTP2SessionState: Symbol('http2Session state'),
Expand Down
189 changes: 0 additions & 189 deletions test/jest/interceptor.test.js

This file was deleted.

Loading

0 comments on commit c2ba195

Please sign in to comment.