-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04c8acb
commit 8ac252d
Showing
3 changed files
with
43 additions
and
85 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,45 +1,11 @@ | ||
'use strict' | ||
|
||
const { InvalidArgumentError } = require('../core/errors') | ||
const ProxyAgent = require('../dispatcher/proxy-agent') | ||
const Dispatcher = require('../dispatcher/dispatcher') | ||
|
||
class ProxyInterceptor extends Dispatcher { | ||
constructor (dispatcher, opts) { | ||
if (dispatcher == null) { | ||
throw new InvalidArgumentError( | ||
'Dispatcher instance is mandatory for ProxyInterceptor' | ||
) | ||
} | ||
|
||
if (typeof opts === 'string') { | ||
opts = { uri: opts } | ||
} | ||
|
||
if (!opts || (!opts.uri && !(opts instanceof URL))) { | ||
throw new InvalidArgumentError( | ||
'Proxy opts.uri or instance of URL is mandatory' | ||
) | ||
} | ||
|
||
if (opts.auth && opts.token) { | ||
throw new InvalidArgumentError( | ||
'opts.auth cannot be used in combination with opts.token' | ||
) | ||
module.exports = opts => { | ||
const agent = new ProxyAgent(opts) | ||
return () => { | ||
return function proxyInterceptor (opts, handler) { | ||
return agent.dispatch(opts, handler) | ||
} | ||
|
||
super() | ||
this.dispatcher = dispatcher | ||
this.agent = new ProxyAgent(opts) | ||
} | ||
|
||
dispatch (opts, handler) { | ||
return this.agent.dispatch(opts, handler) | ||
} | ||
|
||
close () { | ||
return this.dispatcher.close().then(() => this.agent.close()) | ||
} | ||
} | ||
|
||
module.exports = ProxyInterceptor |
This file contains 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