Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public enable(options: IInterceptOptions = {}): void {
// switch comparator functions when specified
this.comparatorFn =
options.comparatorFn === undefined ? this.comparatorFn : options.comparatorFn;
if (this.mitm || this.origOnSocket) {
debug('Interceptor already enabled. Do nothing.');
return;
}
const self = this;
this.mitm = Mitm();
this.ignorePorts = options.ignorePorts || [];
this.origOnSocket = ClientRequest.prototype.onSocket;
ClientRequest.prototype.onSocket = _.flowRight(
this.origOnSocket,
// tslint:disable-next-line:only-arrow-functions
function(this: ClientRequestFull, socket: RegisteredSocket): RegisteredSocket {
if (undefined !== socket.__yesno_req_id) {
debug('New socket on client request', socket.__yesno_req_id);
self.clientRequests[socket.__yesno_req_id].clientRequest = this;
this.setHeader(YESNO_INTERNAL_HTTP_HEADER, socket.__yesno_req_id);
}
return socket;
},
);
beforeAll(() => {
Mitm().on('request', req => {
const host = _.get(req, 'headers.host')
const url = _.get(req, 'url')
throw new Error(
`Network requests forbidden in offline mode. Tried to call URL "${host}${url}"`
)
})
})
constructor (proxyHost) {
if (isNil(proxyHost)) {
throw new Error('Please provide a proxy to route the request to.')
}
this.whitelist = [ parseUrl(proxyHost) ]
this.mitm = Mitm()
this.mitm.disable()
this.disabled = true
this.proxyHost = proxyHost
}
constructor (proxyHost) {
if (isNil(proxyHost)) {
throw new Error('Please provide a proxy to route the request to.')
}
this.whitelist = [ parseUrl(proxyHost) ]
this.mitm = Mitm()
this.mitm.disable()
this.disabled = true
this.proxyHost = proxyHost
}