Skip to content

Commit

Permalink
Add "prettier" config
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 2, 2023
1 parent 941efab commit cd3d088
Show file tree
Hide file tree
Showing 20 changed files with 130 additions and 122 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
packages/*/dist
packages/degenerator/test/*
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"lint": "turbo run lint",
"test": "turbo run test",
"test-e2e": "turbo run test-e2e",
"format": "prettier --write \"**/*.{ts,tsx,md}\""
"format": "prettier --write \"**/*.{ts,js}\""
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.8.0",
"eslint-config-turbo": "^1.9.3",
"prettier": "^2.5.1",
"prettier": "^2.8.8",
"turbo": "latest"
}
}
32 changes: 16 additions & 16 deletions packages/agent-base/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import * as http from "http";
import * as https from "https";
import type { Readable } from "stream";
import * as http from 'http';
import * as https from 'https';
import type { Readable } from 'stream';

export async function toBuffer(stream: Readable): Promise<Buffer> {
let length = 0;
const chunks: Buffer[] = [];
for await (const chunk of stream) {
length += chunk.length;
chunks.push(chunk);
}
return Buffer.concat(chunks, length);
let length = 0;
const chunks: Buffer[] = [];
for await (const chunk of stream) {
length += chunk.length;
chunks.push(chunk);
}
return Buffer.concat(chunks, length);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function json(stream: Readable): Promise<any> {
const buf = await toBuffer(stream);
return JSON.parse(buf.toString('utf8'));
const buf = await toBuffer(stream);
return JSON.parse(buf.toString('utf8'));
}

export function req(
url: string | URL,
opts: https.RequestOptions = {}
): Promise<http.IncomingMessage> {
return new Promise((resolve, reject) => {
const href = typeof url === 'string' ? url : url.href;
(href.startsWith("https:") ? https : http)
const href = typeof url === 'string' ? url : url.href;
(href.startsWith('https:') ? https : http)
.request(url, opts, resolve)
.once("error", reject)
.once('error', reject)
.end();
});
}
}
4 changes: 2 additions & 2 deletions packages/data-uri-to-buffer/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
preset: 'ts-jest',
testEnvironment: 'node',
};
4 changes: 2 additions & 2 deletions packages/degenerator/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('@ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
preset: 'ts-jest',
testEnvironment: 'node',
};
4 changes: 2 additions & 2 deletions packages/get-uri/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
preset: 'ts-jest',
testEnvironment: 'node',
};
4 changes: 2 additions & 2 deletions packages/get-uri/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const protocols = {
export type Protocols = typeof protocols;

export type ProtocolsOptions = {
[P in keyof Protocols]: NonNullable<Parameters<Protocols[P]>[1]>
}
[P in keyof Protocols]: NonNullable<Parameters<Protocols[P]>[1]>;
};

export type ProtocolOpts<T> = {
[P in keyof ProtocolsOptions]: Protocol<T> extends P
Expand Down
7 changes: 5 additions & 2 deletions packages/http-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Protocol<T> = T extends `${infer Protocol}:${infer _}` ? Protocol : never;
type ConnectOptsMap = {
http: Omit<net.TcpNetConnectOpts, 'host' | 'port'>;
https: Omit<tls.ConnectionOptions, 'host' | 'port'>;
}
};

export type HttpProxyAgentOptions<T> = {
[P in keyof ConnectOptsMap]: Protocol<T> extends P
Expand Down Expand Up @@ -53,7 +53,10 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
debug('Creating new HttpProxyAgent instance: %o', this.proxy.href);

// Trim off the brackets from IPv6 addresses
const host = (this.proxy.hostname || this.proxy.host).replace(/^\[|\]$/g, '');
const host = (this.proxy.hostname || this.proxy.host).replace(
/^\[|\]$/g,
''
);
const port = this.proxy.port
? parseInt(this.proxy.port, 10)
: this.secureProxy
Expand Down
10 changes: 7 additions & 3 deletions packages/http-proxy-agent/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ describe('HttpProxyAgent', () => {
assert.equal(false, agent.secureProxy);
});
it('should be `true` when "https:" protocol is used', () => {
let agent = new HttpProxyAgent(`https://127.0.0.1:${proxyPort}`);
let agent = new HttpProxyAgent(
`https://127.0.0.1:${proxyPort}`
);
assert.equal(true, agent.secureProxy);
});
});
Expand Down Expand Up @@ -200,7 +202,7 @@ describe('HttpProxyAgent', () => {
});
it('should receive the 407 authorization code on the `http.ClientResponse`', (done) => {
// reject all requests
proxy.authenticate = () => false
proxy.authenticate = () => false;

let proxyUri = `http://127.0.0.1:${proxyPort}`;
let agent = new HttpProxyAgent(proxyUri);
Expand All @@ -222,7 +224,9 @@ describe('HttpProxyAgent', () => {
// set a proxy authentication function for this test
proxy.authenticate = (req) => {
// username:password is "foo:bar"
return req.headers['proxy-authorization'] === 'Basic Zm9vOmJhcg=='
return (
req.headers['proxy-authorization'] === 'Basic Zm9vOmJhcg=='
);
};

// set HTTP "request" event handler for this test
Expand Down
2 changes: 1 addition & 1 deletion packages/https-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class HttpsProxyAgent<Uri extends string> extends Agent {
this.connectOpts = {
// Attempt to negotiate http/1.1 for proxy servers that support http/2
ALPNProtocols: ['http/1.1'],
...(opts ? omit(opts, "headers") : null),
...(opts ? omit(opts, 'headers') : null),
host,
port,
};
Expand Down
84 changes: 42 additions & 42 deletions packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getUri,
protocols as gProtocols,
ProtocolOpts as GetUriOptions,
} from "get-uri";
} from 'get-uri';
import {
createPacResolver,
FindProxyForURL,
Expand All @@ -28,8 +28,8 @@ type Protocols = keyof typeof gProtocols;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type Protocol<T> = T extends `pac+${infer P}:${infer _}`
? P
// eslint-disable-next-line @typescript-eslint/no-unused-vars
: T extends `${infer P}:${infer _}`
: // eslint-disable-next-line @typescript-eslint/no-unused-vars
T extends `${infer P}:${infer _}`
? P
: never;

Expand All @@ -55,11 +55,11 @@ export type PacProxyAgentOptions<T> = PacResolverOptions &
*/
export class PacProxyAgent<Uri extends string> extends Agent {
static readonly protocols: `pac-${Protocols}`[] = [
"pac-data",
"pac-file",
"pac-ftp",
"pac-http",
"pac-https",
'pac-data',
'pac-file',
'pac-ftp',
'pac-http',
'pac-https',
];

uri: URL;
Expand All @@ -73,16 +73,16 @@ export class PacProxyAgent<Uri extends string> extends Agent {
super();

// Strip the "pac+" prefix
const uriStr = typeof uri === "string" ? uri : uri.href;
this.uri = new URL(uriStr.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);
debug('Creating PacProxyAgent with URI %o', this.uri.href);

// @ts-expect-error Not sure why TS is complaining here…
this.opts = { ...opts };
this.cache = undefined;
this.resolver = undefined;
this.resolverHash = "";
this.resolverHash = '';
this.resolverPromise = undefined;

// For `PacResolver`
Expand Down Expand Up @@ -118,17 +118,17 @@ export class PacProxyAgent<Uri extends string> extends Agent {
const code = await this.loadPacFile();

// Create a sha1 hash of the JS code
const hash = crypto.createHash("sha1").update(code).digest("hex");
const hash = crypto.createHash('sha1').update(code).digest('hex');

if (this.resolver && this.resolverHash === hash) {
debug(
"Same sha1 hash for code - contents have not changed, reusing previous proxy resolver"
'Same sha1 hash for code - contents have not changed, reusing previous proxy resolver'
);
return this.resolver;
}

// Cache the resolver
debug("Creating new proxy resolver instance");
debug('Creating new proxy resolver instance');
this.resolver = createPacResolver(code, this.opts);

// Store that sha1 hash for future comparison purposes
Expand All @@ -138,10 +138,10 @@ export class PacProxyAgent<Uri extends string> extends Agent {
} catch (err: unknown) {
if (
this.resolver &&
(err as NodeJS.ErrnoException).code === "ENOTMODIFIED"
(err as NodeJS.ErrnoException).code === 'ENOTMODIFIED'
) {
debug(
"Got ENOTMODIFIED response, reusing previous proxy resolver"
'Got ENOTMODIFIED response, reusing previous proxy resolver'
);
return this.resolver;
}
Expand All @@ -155,16 +155,16 @@ export class PacProxyAgent<Uri extends string> extends Agent {
* @api private
*/
private async loadPacFile(): Promise<string> {
debug("Loading PAC file: %o", this.uri);
debug('Loading PAC file: %o', this.uri);

const rs = await getUri(this.uri, { ...this.opts, cache: this.cache });
debug("Got `Readable` instance for URI");
debug('Got `Readable` instance for URI');
this.cache = rs;

const buf = await toBuffer(rs);
debug("Read %o byte PAC file from URI", buf.length);
debug('Read %o byte PAC file from URI', buf.length);

return buf.toString("utf8");
return buf.toString('utf8');
}

/**
Expand All @@ -184,15 +184,15 @@ export class PacProxyAgent<Uri extends string> extends Agent {
const defaultPort = secureEndpoint ? 443 : 80;
let path = req.path;
let search: string | null = null;
const firstQuestion = path.indexOf("?");
const firstQuestion = path.indexOf('?');
if (firstQuestion !== -1) {
search = path.substring(firstQuestion);
path = path.substring(0, firstQuestion);
}

const urlOpts = {
...opts,
protocol: secureEndpoint ? "https:" : "http:",
protocol: secureEndpoint ? 'https:' : 'http:',
pathname: path,
search,

Expand All @@ -206,47 +206,47 @@ export class PacProxyAgent<Uri extends string> extends Agent {
};
const url = format(urlOpts);

debug("url: %o", url);
debug('url: %o', url);
let result = await resolver(url);

// Default to "DIRECT" if a falsey value was returned (or nothing)
if (!result) {
result = "DIRECT";
result = 'DIRECT';
}

const proxies = String(result)
.trim()
.split(/\s*;\s*/g)
.filter(Boolean);

if (this.opts.fallbackToDirect && !proxies.includes("DIRECT")) {
proxies.push("DIRECT");
if (this.opts.fallbackToDirect && !proxies.includes('DIRECT')) {
proxies.push('DIRECT');
}

for (const proxy of proxies) {
let agent: Agent | null = null;
let socket: net.Socket | null = null;
const [type, target] = proxy.split(/\s+/);
debug("Attempting to use proxy: %o", proxy);
debug('Attempting to use proxy: %o', proxy);

if (type === "DIRECT") {
if (type === 'DIRECT') {
// Direct connection to the destination endpoint
socket = secureEndpoint ? tls.connect(opts) : net.connect(opts);
} else if (type === "SOCKS" || type === "SOCKS5") {
} else if (type === 'SOCKS' || type === 'SOCKS5') {
// Use a SOCKSv5h proxy
agent = new SocksProxyAgent(`socks://${target}`, this.opts);
} else if (type === "SOCKS4") {
} else if (type === 'SOCKS4') {
// Use a SOCKSv4a proxy
agent = new SocksProxyAgent(`socks4a://${target}`, this.opts);
} else if (
type === "PROXY" ||
type === "HTTP" ||
type === "HTTPS"
type === 'PROXY' ||
type === 'HTTP' ||
type === 'HTTPS'
) {
// Use an HTTP or HTTPS proxy
// http://dev.chromium.org/developers/design-documents/secure-web-proxy
const proxyURL = `${
type === "HTTPS" ? "https" : "http"
type === 'HTTPS' ? 'https' : 'http'
}://${target}`;
if (secureEndpoint) {
agent = new HttpsProxyAgent(proxyURL, this.opts);
Expand All @@ -258,24 +258,24 @@ export class PacProxyAgent<Uri extends string> extends Agent {
try {
if (socket) {
// "DIRECT" connection, wait for connection confirmation
await once(socket, "connect");
req.emit("proxy", { proxy, socket });
await once(socket, 'connect');
req.emit('proxy', { proxy, socket });
return socket;
}
if (agent) {
const s = await agent.connect(req, opts);
if (!(s instanceof net.Socket)) {
throw new Error(
"Expected a `net.Socket` to be returned from agent"
'Expected a `net.Socket` to be returned from agent'
);
}
req.emit("proxy", { proxy, socket: s });
req.emit('proxy', { proxy, socket: s });
return s;
}
throw new Error(`Could not determine proxy type for: ${proxy}`);
} catch (err) {
debug("Got error for proxy %o: %o", proxy, err);
req.emit("proxy", { proxy, error: err });
debug('Got error for proxy %o: %o', proxy, err);
req.emit('proxy', { proxy, error: err });
}
}

Expand All @@ -285,4 +285,4 @@ export class PacProxyAgent<Uri extends string> extends Agent {
)}`
);
}
}
}

1 comment on commit cd3d088

@vercel
Copy link

@vercel vercel bot commented on cd3d088 May 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

proxy-agents – ./

proxy-agents-tootallnate.vercel.app
proxy-agents-git-main-tootallnate.vercel.app
proxy-agents.vercel.app

Please sign in to comment.