Skip to content

Commit

Permalink
WIP: compose
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Feb 23, 2024
1 parent 789e792 commit 245118d
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/api/api-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function connect (opts, callback) {

try {
this
.intercept(redirect(opts))
.compose(redirect(opts))
.dispatch({ ...opts, method: opts?.method || 'CONNECT' }, new ConnectHandler(opts, callback))
} catch (err) {
if (typeof callback !== 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ function pipeline (opts, handler) {
const pipelineHandler = new PipelineHandler(opts, handler)

this
.intercept(redirect(opts))
.compose(redirect(opts))
.dispatch({ ...opts, body: pipelineHandler.req }, pipelineHandler)

return pipelineHandler.ret
Expand Down
2 changes: 1 addition & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function request (opts, callback) {

try {
this
.intercept(redirect(opts))
.compose(redirect(opts))
.dispatch(opts, new RequestHandler(opts, callback))
} catch (err) {
if (typeof callback !== 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function stream (opts, factory, callback) {

try {
this
.intercept(redirect(opts))
.compose(redirect(opts))
.dispatch(opts, new StreamHandler(opts, factory, callback))
} catch (err) {
if (typeof callback !== 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/api/api-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function upgrade (opts, callback) {

try {
this
.intercept(redirect(opts))
.compose(redirect(opts))
.dispatch({ ...opts, method: opts?.method || 'GET', upgrade: opts?.protocol || 'Websocket' }, new UpgradeHandler(opts, callback))
} catch (err) {
if (typeof callback !== 'function') {
Expand Down
2 changes: 1 addition & 1 deletion lib/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Dispatcher extends EventEmitter {
throw new Error('not implemented')
}

intercept (...interceptors) {
compose (...interceptors) {
let dispatcher = this
for (const interceptor of interceptors) {
if (interceptor == null) {
Expand Down
4 changes: 1 addition & 3 deletions lib/mock/mock-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class MockClient extends Client {
* Sets up the base interceptor for mocking replies from undici.
*/
intercept (opts) {
return opts == null || typeof opts === 'function'
? super.intercept(opts)
: new MockInterceptor(opts, this[kDispatches])
return new MockInterceptor(opts, this[kDispatches])
}

async [kClose] () {
Expand Down
4 changes: 1 addition & 3 deletions lib/mock/mock-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ class MockPool extends Pool {
* Sets up the base interceptor for mocking replies from undici.
*/
intercept (opts) {
return opts == null || typeof opts === 'function'
? super.intercept(opts)
: new MockInterceptor(opts, this[kDispatches])
return new MockInterceptor(opts, this[kDispatches])
}

async [kClose] () {
Expand Down
30 changes: 15 additions & 15 deletions test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test('use proxy-agent to connect through proxy', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
const parsedOrigin = new URL(serverUrl)

proxy.on('connect', () => {
Expand Down Expand Up @@ -103,7 +103,7 @@ test('use proxy agent to connect through proxy using Pool', async (t) => {
const clientFactory = (url, options) => {
return new Pool(url, options)
}
const proxyAgent = new Agent().intercept(proxyInterceptor({ auth: Buffer.from('user:pass').toString('base64'), uri: proxyUrl, clientFactory }))
const proxyAgent = new Agent().compose(proxyInterceptor({ auth: Buffer.from('user:pass').toString('base64'), uri: proxyUrl, clientFactory }))
const firstRequest = request(`${serverUrl}`, { dispatcher: proxyAgent })
const secondRequest = await request(`${serverUrl}`, { dispatcher: proxyAgent })
t.strictEqual((await firstRequest).statusCode, 200)
Expand All @@ -120,7 +120,7 @@ test('use proxy-agent to connect through proxy using path with params', async (t

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
const parsedOrigin = new URL(serverUrl)

proxy.on('connect', () => {
Expand Down Expand Up @@ -156,7 +156,7 @@ test('use proxy-agent with auth', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
auth: Buffer.from('user:pass').toString('base64'),
uri: proxyUrl
}))
Expand Down Expand Up @@ -200,7 +200,7 @@ test('use proxy-agent with token', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
token: `Bearer ${Buffer.from('user:pass').toString('base64')}`,
uri: proxyUrl
}))
Expand Down Expand Up @@ -244,7 +244,7 @@ test('use proxy-agent with custom headers', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
uri: proxyUrl,
headers: {
'User-Agent': 'Foobar/1.0.0'
Expand Down Expand Up @@ -277,7 +277,7 @@ test('sending proxy-authorization in request headers should throw', async (t) =>

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))

server.on('request', (req, res) => {
res.end(JSON.stringify({ hello: 'world' }))
Expand Down Expand Up @@ -336,7 +336,7 @@ test('use proxy-agent with setGlobalDispatcher', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
const parsedOrigin = new URL(serverUrl)
setGlobalDispatcher(proxyAgent)

Expand Down Expand Up @@ -378,7 +378,7 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async
const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`

const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
setGlobalDispatcher(proxyAgent)

after(() => setGlobalDispatcher(defaultDispatcher))
Expand Down Expand Up @@ -431,7 +431,7 @@ test('should throw when proxy does not return 200', async (t) => {
fn(null, false)
}

const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
try {
await request(serverUrl, { dispatcher: proxyAgent })
t.fail()
Expand All @@ -458,7 +458,7 @@ test('pass ProxyAgent proxy status code error when using fetch - #2161', async (
fn(null, false)
}

const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))
try {
await fetch(serverUrl, { dispatcher: proxyAgent })
} catch (e) {
Expand All @@ -480,7 +480,7 @@ test('Proxy via HTTP to HTTPS endpoint', async (t) => {

const serverUrl = `https://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
uri: proxyUrl,
requestTls: {
ca: [
Expand Down Expand Up @@ -532,7 +532,7 @@ test('Proxy via HTTPS to HTTPS endpoint', async (t) => {

const serverUrl = `https://localhost:${server.address().port}`
const proxyUrl = `https://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
uri: proxyUrl,
proxyTls: {
ca: [
Expand Down Expand Up @@ -593,7 +593,7 @@ test('Proxy via HTTPS to HTTP endpoint', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `https://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor({
const proxyAgent = new Agent().compose(proxyInterceptor({
uri: proxyUrl,
proxyTls: {
ca: [
Expand Down Expand Up @@ -642,7 +642,7 @@ test('Proxy via HTTP to HTTP endpoint', async (t) => {

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new Agent().intercept(proxyInterceptor(proxyUrl))
const proxyAgent = new Agent().compose(proxyInterceptor(proxyUrl))

server.on('request', function (req, res) {
t.ok(!req.connection.encrypted)
Expand Down
16 changes: 8 additions & 8 deletions test/retry-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ test('Should retry status code', async t => {
await once(server, 'close')
})

client.intercept(retry({
client.compose(retry({
retry: (err, { state, opts }, done) => {
counter++

Expand Down Expand Up @@ -151,7 +151,7 @@ test('Should use retry-after header for retries', async t => {
await once(server, 'close')
})

client.intercept(retry({
client.compose(retry({

})).dispatch(
{
Expand Down Expand Up @@ -228,7 +228,7 @@ test('Should use retry-after header for retries (date)', async t => {
await once(server, 'close')
})

client.intercept(retry({
client.compose(retry({
})).dispatch(
{
method: 'PUT',
Expand Down Expand Up @@ -301,7 +301,7 @@ test('Should retry with defaults', async t => {
await once(server, 'close')
})

client.intercept(retry()).dispatch(
client.compose(retry()).dispatch(
{
method: 'GET',
path: '/',
Expand Down Expand Up @@ -365,7 +365,7 @@ test('Should handle 206 partial content', async t => {
}
}

client.intercept(retry({
client.compose(retry({
retry: function (err, _, done) {
counter++

Expand Down Expand Up @@ -447,7 +447,7 @@ test('Should handle 206 partial content - bad-etag', async t => {
}
}

client.intercept(retry()).dispatch(
client.compose(retry()).dispatch(
{
method: 'GET',
path: '/',
Expand Down Expand Up @@ -513,7 +513,7 @@ test('retrying a request with a body', async t => {
await once(server, 'close')
})

client.intercept(retry({
client.compose(retry({
retry: (err, { state, opts }, done) => {
counter++

Expand Down Expand Up @@ -583,7 +583,7 @@ test('should not error if request is not meant to be retried', async t => {
await once(server, 'close')
})

client.intercept(retry()).dispatch(
client.compose(retry()).dispatch(
{
method: 'GET',
path: '/',
Expand Down

0 comments on commit 245118d

Please sign in to comment.