Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Apr 27, 2023
1 parent 7541964 commit 2a5103b
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 139 deletions.
2 changes: 1 addition & 1 deletion packages/degenerator/package.json
Expand Up @@ -19,7 +19,7 @@
"url": "git://github.com/TooTallNate/node-degenerator.git"
},
"engines": {
"node": ">= 6"
"node": ">= 14"
},
"license": "MIT",
"dependencies": {
Expand Down
8 changes: 1 addition & 7 deletions packages/https-proxy-agent/src/index.ts
Expand Up @@ -39,7 +39,7 @@ export class HttpsProxyAgent extends Agent {
super();
this.proxy = typeof proxy === 'string' ? new URL(proxy) : proxy;
this.proxyHeaders = opts?.headers ?? {};
debug('creating new HttpsProxyAgent instance: %o', opts);
debug('creating new HttpsProxyAgent instance: %o', this.proxy.href);

const host = this.proxy.hostname || this.proxy.host;
const port = this.proxy.port
Expand All @@ -52,12 +52,6 @@ export class HttpsProxyAgent extends Agent {
host,
port,
};

// ALPN is supported by Node.js >= v5.
// attempt to negotiate http/1.1 for proxy servers that support http/2
if (this.secureProxy && !('ALPNProtocols' in this.connectOpts)) {
this.connectOpts.ALPNProtocols = ['http 1.1'];
}
}

/**
Expand Down
40 changes: 20 additions & 20 deletions packages/https-proxy-agent/test/test.js
Expand Up @@ -169,7 +169,7 @@ describe('HttpsProxyAgent', () => {
});
req.once('error', done);
});
it.only('should work over an HTTPS proxy', (done) => {
it('should work over an HTTPS proxy', (done) => {
server.once('request', (req, res) => {
res.end(JSON.stringify(req.headers));
});
Expand Down Expand Up @@ -328,26 +328,26 @@ describe('HttpsProxyAgent', () => {
});

let proxy = `https://localhost:${sslProxyPort}`;
proxy = url.parse(proxy);
proxy.rejectUnauthorized = false;
let agent = new HttpsProxyAgent(proxy);

let opts = url.parse(`https://localhost:${sslServerPort}`);
opts.agent = agent;
opts.rejectUnauthorized = false;

https.get(opts, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (b) => {
data += b;
});
res.on('end', () => {
data = JSON.parse(data);
assert.equal(`localhost:${sslServerPort}`, data.host);
done();
});
let agent = new HttpsProxyAgent(proxy, {
rejectUnauthorized: false,
});

https.get(
`https://localhost:${sslServerPort}`,
{ agent, rejectUnauthorized: false },
(res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (b) => {
data += b;
});
res.on('end', () => {
data = JSON.parse(data);
assert.equal(`localhost:${sslServerPort}`, data.host);
done();
});
}
);
});

it('should not send a port number for the default port', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/pac-proxy-agent/package.json
Expand Up @@ -45,7 +45,7 @@
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/node": "^12.20.55",
"@types/node": "^14.18.43",
"mocha": "^6.2.3",
"proxy": "^1.0.2",
"socksv5": "0.0.6",
Expand Down
13 changes: 8 additions & 5 deletions packages/pac-proxy-agent/src/index.ts
Expand Up @@ -45,19 +45,22 @@ export const protocols = Object.keys(gProtocols);
* - "pac+https", "https" - refers to an HTTPS endpoint
*/
export class PacProxyAgent extends Agent {
uri: string;
uri: URL;
opts: PacProxyAgentOptions;
cache?: Readable;
resolver?: FindProxyForURL;
resolverHash: string;
resolverPromise?: Promise<FindProxyForURL>;

constructor(uri: string, opts?: PacProxyAgentOptions) {
constructor(uri: string | URL, opts?: PacProxyAgentOptions) {
super();
debug('Creating PacProxyAgent with URI %o and options %o', uri, opts);

// Strip the "pac+" prefix
this.uri = uri.replace(/^pac\+/i, '');
const uriStr = typeof uri === 'string' ? uri : uri.href;
this.uri = new URL(uriStr.replace(/^pac\+/i, ''));

debug('Creating PacProxyAgent with URI %o', this.uri.href);

this.opts = { port: 0, ...opts };
this.cache = undefined;
this.resolver = undefined;
Expand All @@ -66,7 +69,7 @@ export class PacProxyAgent extends Agent {

// For `PacResolver`
if (!this.opts.filename) {
this.opts.filename = uri;
this.opts.filename = this.uri.href;
}
}

Expand Down
16 changes: 4 additions & 12 deletions packages/pac-proxy-agent/test/test.js
Expand Up @@ -149,25 +149,19 @@ describe('PacProxyAgent', function () {

describe('constructor', function () {
it('should throw an Error if no "proxy" argument is given', function () {
assert.throws(function () {
assert.throws(() => {
new PacProxyAgent();
});
});
it('should accept a "string" proxy argument', function () {
let agent = new PacProxyAgent('pac+ftp://example.com/proxy.pac');
assert.equal('ftp://example.com/proxy.pac', agent.uri);
assert.equal('ftp://example.com/proxy.pac', agent.uri.href);
});
it('should accept a `URL` instance proxy argument', function () {
let agent = new PacProxyAgent(
new URL('pac+ftp://example.com/proxy.pac')
);
assert.equal('ftp://example.com/proxy.pac', agent.uri);
});
it('should accept a `uri` on the options object', function () {
let agent = new PacProxyAgent({
uri: 'pac+ftp://example.com/proxy.pac',
});
assert.equal('ftp://example.com/proxy.pac', agent.uri);
assert.equal('ftp://example.com/proxy.pac', agent.uri.href);
});
});

Expand Down Expand Up @@ -213,9 +207,7 @@ describe('PacProxyAgent', function () {
let uri = `data:,${encodeURIComponent(
FindProxyForURL.toString().replace('PORT', proxyHttpsPort)
)}`;
let proxy = url.parse(uri);
proxy.rejectUnauthorized = false;
let agent = new PacProxyAgent(proxy);
let agent = new PacProxyAgent(uri, { rejectUnauthorized: false });

let opts = url.parse(`http://localhost:${httpPort}/test`);
opts.agent = agent;
Expand Down
4 changes: 2 additions & 2 deletions packages/pac-resolver/package.json
Expand Up @@ -15,7 +15,7 @@
"devDependencies": {
"@types/ip": "^1.1.0",
"@types/netmask": "^1.0.30",
"@types/node": "^17.0.19",
"@types/node": "^14.18.43",
"mocha": "^9.2.1",
"tsconfig": "workspace:*",
"typescript": "^5.0.4"
Expand All @@ -31,7 +31,7 @@
"url": "git://github.com/TooTallNate/node-pac-resolver.git"
},
"engines": {
"node": ">= 8"
"node": ">= 14"
},
"keywords": [
"pac",
Expand Down
10 changes: 5 additions & 5 deletions packages/socks-proxy-agent/package.json
Expand Up @@ -112,12 +112,12 @@
"socks": "^2.7.1"
},
"devDependencies": {
"@types/debug": "latest",
"@types/node": "latest",
"cacheable-lookup": "latest",
"dns2": "latest",
"@types/debug": "^4.1.7",
"@types/node": "^14.18.43",
"cacheable-lookup": "^6.1.0",
"dns2": "^2.1.0",
"mocha": "^9.2.2",
"raw-body": "latest",
"raw-body": "^2.5.2",
"socksv5": "github:TooTallNate/socksv5#fix/dstSock-close-event",
"tsconfig": "workspace:*",
"typescript": "^5.0.4"
Expand Down
11 changes: 4 additions & 7 deletions packages/socks-proxy-agent/test/test.js
Expand Up @@ -24,7 +24,7 @@ describe('SocksProxyAgent', function () {

before(function (done) {
// setup SOCKS proxy server
socksServer = socks.createServer(function (info, accept, deny) {
socksServer = socks.createServer(function (_info, accept) {
accept();
});
socksServer.listen(0, '127.0.0.1', function () {
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('SocksProxyAgent', function () {
headers: { foo: 'bar' },
};

const req = http.get(opts, function () {});
const req = http.get(opts);

req.once('error', (err) => {
assert.equal(err.message, 'socket hang up');
Expand Down Expand Up @@ -245,18 +245,15 @@ describe('SocksProxyAgent', function () {
);
opts.agent = agent;

opts.lookup = (hostname, opts, callback) => {
opts.lookup = (hostname, _opts, callback) => {
if (hostname === 'non-existent-domain.test')
callback(null, '127.0.0.1');
else callback(new Error('Bad domain'));
};

let req = http.get(opts, function (res) {
assert.equal(404, res.statusCode);
getRawBody(res, 'utf8', function (err, buf) {
if (err) return done(err);
done();
});
done();
});
req.once('error', done);
});
Expand Down

0 comments on commit 2a5103b

Please sign in to comment.