From 54ea8b521cec697aaabf6efa5f4faa1a851c048f Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Fri, 6 Dec 2024 13:41:42 -0700 Subject: [PATCH] Run format check in CI --- .github/workflows/test.yml | 23 ++++++ .prettierignore | 3 + .prettierrc | 2 +- package.json | 3 +- packages/agent-base/tsconfig.json | 2 +- packages/data-uri-to-buffer/src/common.ts | 88 +++++++++++---------- packages/data-uri-to-buffer/src/index.ts | 7 +- packages/data-uri-to-buffer/src/node.ts | 7 +- packages/degenerator/tsconfig.json | 2 +- packages/get-uri/tsconfig.json | 5 +- packages/http-proxy-agent/tsconfig.json | 2 +- packages/https-proxy-agent/test/e2e.test.ts | 3 +- packages/https-proxy-agent/tsconfig.json | 2 +- packages/pac-proxy-agent/test/test.ts | 5 +- packages/pac-proxy-agent/tsconfig.json | 2 +- packages/pac-resolver/src/ip.ts | 52 ++++++------ packages/pac-resolver/tsconfig.json | 2 +- packages/proxy-agent/src/index.ts | 29 +++++-- packages/proxy-agent/tsconfig.json | 2 +- packages/proxy/tsconfig.json | 2 +- packages/socks-proxy-agent/src/index.ts | 3 +- packages/socks-proxy-agent/test/test.ts | 24 +++--- packages/socks-proxy-agent/tsconfig.json | 2 +- packages/tsconfig/base.json | 2 +- pnpm-workspace.yaml | 4 +- 25 files changed, 169 insertions(+), 109 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dd2d621c..bd994e4c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -71,6 +71,29 @@ jobs: - name: Run Lint run: pnpm lint + id: lint + continue-on-error: true + + - name: Run Format + run: pnpm format:check + id: format + continue-on-error: true + + - name: Check Results + run: | + declare -A OUTCOMES=( + [lint]="${{ steps.lint.outcome }}" + [format]="${{ steps.format.outcome }}" + ) + STATUS=0 + for STEP in "${!OUTCOMES[@]}"; do + OUTCOME="${OUTCOMES[$STEP]}" + echo "$STEP: $OUTCOME" + if [ "$OUTCOME" != "success" ]; then + STATUS=1 + fi + done + exit $STATUS e2e: name: E2E diff --git a/.prettierignore b/.prettierignore index d0f47c2a..16179af3 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,5 @@ packages/*/dist packages/degenerator/test/*.js +*.md +*.pac +pnpm-lock.yaml diff --git a/.prettierrc b/.prettierrc index 8db60caa..62532247 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,3 +1,3 @@ { - "singleQuote": true + "singleQuote": true } diff --git a/package.json b/package.json index a864d7ac..e1ce26f9 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "lint": "turbo run lint", "test": "turbo run test", "test-e2e": "turbo run test-e2e", - "format": "prettier --write \"**/*.{ts,js,mjs}\"", + "format": "prettier --write .", + "format:check": "prettier --check .", "ci:version": "changeset version && pnpm install --no-frozen-lockfile", "ci:publish": "pnpm publish -r && changeset tag" }, diff --git a/packages/agent-base/tsconfig.json b/packages/agent-base/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/agent-base/tsconfig.json +++ b/packages/agent-base/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/data-uri-to-buffer/src/common.ts b/packages/data-uri-to-buffer/src/common.ts index d59c83be..d7cebe24 100644 --- a/packages/data-uri-to-buffer/src/common.ts +++ b/packages/data-uri-to-buffer/src/common.ts @@ -15,55 +15,59 @@ export interface IBufferConversions { * * @param {String} uri Data URI to turn into a Buffer instance */ -export const makeDataUriToBuffer = (convert: IBufferConversions) => (uri: string | URL): ParsedDataURI => { - uri = String(uri); +export const makeDataUriToBuffer = + (convert: IBufferConversions) => + (uri: string | URL): ParsedDataURI => { + uri = String(uri); - if (!/^data:/i.test(uri)) { - throw new TypeError( - '`uri` does not appear to be a Data URI (must begin with "data:")' - ); - } + if (!/^data:/i.test(uri)) { + throw new TypeError( + '`uri` does not appear to be a Data URI (must begin with "data:")' + ); + } - // strip newlines - uri = uri.replace(/\r?\n/g, ''); + // strip newlines + uri = uri.replace(/\r?\n/g, ''); - // split the URI up into the "metadata" and the "data" portions - const firstComma = uri.indexOf(','); - if (firstComma === -1 || firstComma <= 4) { - throw new TypeError('malformed data: URI'); - } + // split the URI up into the "metadata" and the "data" portions + const firstComma = uri.indexOf(','); + if (firstComma === -1 || firstComma <= 4) { + throw new TypeError('malformed data: URI'); + } - // remove the "data:" scheme and parse the metadata - const meta = uri.substring(5, firstComma).split(';'); + // remove the "data:" scheme and parse the metadata + const meta = uri.substring(5, firstComma).split(';'); - let charset = ''; - let base64 = false; - const type = meta[0] || 'text/plain'; - let typeFull = type; - for (let i = 1; i < meta.length; i++) { - if (meta[i] === 'base64') { - base64 = true; - } else if (meta[i]) { - typeFull += `;${meta[i]}`; - if (meta[i].indexOf('charset=') === 0) { - charset = meta[i].substring(8); + let charset = ''; + let base64 = false; + const type = meta[0] || 'text/plain'; + let typeFull = type; + for (let i = 1; i < meta.length; i++) { + if (meta[i] === 'base64') { + base64 = true; + } else if (meta[i]) { + typeFull += `;${meta[i]}`; + if (meta[i].indexOf('charset=') === 0) { + charset = meta[i].substring(8); + } } } - } - // defaults to US-ASCII only if type is not provided - if (!meta[0] && !charset.length) { - typeFull += ';charset=US-ASCII'; - charset = 'US-ASCII'; - } + // defaults to US-ASCII only if type is not provided + if (!meta[0] && !charset.length) { + typeFull += ';charset=US-ASCII'; + charset = 'US-ASCII'; + } - // get the encoded data portion and decode URI-encoded chars - const data = unescape(uri.substring(firstComma + 1)); - const buffer = base64 ? convert.base64ToArrayBuffer(data) : convert.stringToBuffer(data); + // get the encoded data portion and decode URI-encoded chars + const data = unescape(uri.substring(firstComma + 1)); + const buffer = base64 + ? convert.base64ToArrayBuffer(data) + : convert.stringToBuffer(data); - return { - type, - typeFull, - charset, - buffer, + return { + type, + typeFull, + charset, + buffer, + }; }; -} diff --git a/packages/data-uri-to-buffer/src/index.ts b/packages/data-uri-to-buffer/src/index.ts index fb2f24a5..628f1e00 100644 --- a/packages/data-uri-to-buffer/src/index.ts +++ b/packages/data-uri-to-buffer/src/index.ts @@ -1,6 +1,6 @@ import { makeDataUriToBuffer } from './common'; -export type { ParsedDataURI } from './common'; +export type { ParsedDataURI } from './common'; function base64ToArrayBuffer(base64: string) { const chars = @@ -55,4 +55,7 @@ function stringToBuffer(str: string): ArrayBuffer { * * @param {String} uri Data URI to turn into a Buffer instance */ -export const dataUriToBuffer = makeDataUriToBuffer({ stringToBuffer, base64ToArrayBuffer }); +export const dataUriToBuffer = makeDataUriToBuffer({ + stringToBuffer, + base64ToArrayBuffer, +}); diff --git a/packages/data-uri-to-buffer/src/node.ts b/packages/data-uri-to-buffer/src/node.ts index 7c320fdc..5b694bdd 100644 --- a/packages/data-uri-to-buffer/src/node.ts +++ b/packages/data-uri-to-buffer/src/node.ts @@ -1,6 +1,6 @@ import { makeDataUriToBuffer } from './common'; -export type { ParsedDataURI } from './common'; +export type { ParsedDataURI } from './common'; function nodeBuffertoArrayBuffer(nodeBuf: Buffer) { if (nodeBuf.byteLength === nodeBuf.buffer.byteLength) { @@ -25,4 +25,7 @@ function stringToBuffer(str: string): ArrayBuffer { * * @param {String} uri Data URI to turn into a Buffer instance */ -export const dataUriToBuffer = makeDataUriToBuffer({ stringToBuffer, base64ToArrayBuffer }); +export const dataUriToBuffer = makeDataUriToBuffer({ + stringToBuffer, + base64ToArrayBuffer, +}); diff --git a/packages/degenerator/tsconfig.json b/packages/degenerator/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/degenerator/tsconfig.json +++ b/packages/degenerator/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/get-uri/tsconfig.json b/packages/get-uri/tsconfig.json index d43bf6a4..b4617fa2 100644 --- a/packages/get-uri/tsconfig.json +++ b/packages/get-uri/tsconfig.json @@ -8,10 +8,7 @@ "outDir": "dist", "sourceMap": true, "declaration": true, - "typeRoots": [ - "./@types", - "./node_modules/@types" - ] + "typeRoots": ["./@types", "./node_modules/@types"] }, "include": ["src/**/*"], "exclude": ["node_modules"] diff --git a/packages/http-proxy-agent/tsconfig.json b/packages/http-proxy-agent/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/http-proxy-agent/tsconfig.json +++ b/packages/http-proxy-agent/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/https-proxy-agent/test/e2e.test.ts b/packages/https-proxy-agent/test/e2e.test.ts index 7e1215e0..6b612c25 100644 --- a/packages/https-proxy-agent/test/e2e.test.ts +++ b/packages/https-proxy-agent/test/e2e.test.ts @@ -9,8 +9,7 @@ interface NordVPNServer { locations: { country: { code: string; - } - + }; }[]; technologies: { identifier: string; diff --git a/packages/https-proxy-agent/tsconfig.json b/packages/https-proxy-agent/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/https-proxy-agent/tsconfig.json +++ b/packages/https-proxy-agent/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/pac-proxy-agent/test/test.ts b/packages/pac-proxy-agent/test/test.ts index 3455fa40..05f29c29 100644 --- a/packages/pac-proxy-agent/test/test.ts +++ b/packages/pac-proxy-agent/test/test.ts @@ -188,7 +188,10 @@ describe('PacProxyAgent', () => { )}`; const agent = new PacProxyAgent(uri); - const res = await req(new URL('/test', httpServerUrl), { agent, headers: { upgrade: 'websocket' } }); + const res = await req(new URL('/test', httpServerUrl), { + agent, + headers: { upgrade: 'websocket' }, + }); const data = await json(res); assert.equal(httpServerUrl.host, data.host); assert(!('via' in data)); // Used CONNECT rather than plain HTTP proxy diff --git a/packages/pac-proxy-agent/tsconfig.json b/packages/pac-proxy-agent/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/pac-proxy-agent/tsconfig.json +++ b/packages/pac-proxy-agent/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/pac-resolver/src/ip.ts b/packages/pac-resolver/src/ip.ts index 68bdce78..5080fb2d 100644 --- a/packages/pac-resolver/src/ip.ts +++ b/packages/pac-resolver/src/ip.ts @@ -7,28 +7,35 @@ export const ip = { // Default to `ipv4` const family = normalizeFamily(); - const all = Object.values(interfaces).map((addrs = []) => { - const addresses = addrs.filter((details) => { - const detailsFamily = normalizeFamily(details.family); - if (detailsFamily !== family || ip.isLoopback(details.address)) { - return false; - } - return true; - - }); - - return addresses.length ? addresses[0].address : undefined; - }).filter(Boolean); - - return !all.length ? ip.loopback(family) : all[0] as string; + const all = Object.values(interfaces) + .map((addrs = []) => { + const addresses = addrs.filter((details) => { + const detailsFamily = normalizeFamily(details.family); + if ( + detailsFamily !== family || + ip.isLoopback(details.address) + ) { + return false; + } + return true; + }); + + return addresses.length ? addresses[0].address : undefined; + }) + .filter(Boolean); + + return !all.length ? ip.loopback(family) : (all[0] as string); }, isLoopback(addr: string): boolean { - return /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/ - .test(addr) - || /^fe80::1$/.test(addr) - || /^::1$/.test(addr) - || /^::$/.test(addr); + return ( + /^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})/.test( + addr + ) || + /^fe80::1$/.test(addr) || + /^::1$/.test(addr) || + /^::$/.test(addr) + ); }, loopback(family: IpFamily): string { @@ -40,8 +47,7 @@ export const ip = { } return family === 'ipv4' ? '127.0.0.1' : 'fe80::1'; - } - + }, }; function normalizeFamily(family?: unknown): IpFamily { @@ -51,7 +57,7 @@ function normalizeFamily(family?: unknown): IpFamily { if (family === 6) { return 'ipv6'; } - return family ? (family as string).toLowerCase() as IpFamily : 'ipv4'; + return family ? ((family as string).toLowerCase() as IpFamily) : 'ipv4'; } -type IpFamily = 'ipv4' | 'ipv6' +type IpFamily = 'ipv4' | 'ipv6'; diff --git a/packages/pac-resolver/tsconfig.json b/packages/pac-resolver/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/pac-resolver/tsconfig.json +++ b/packages/pac-resolver/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/proxy-agent/src/index.ts b/packages/proxy-agent/src/index.ts index b8294d48..126dc4aa 100644 --- a/packages/proxy-agent/src/index.ts +++ b/packages/proxy-agent/src/index.ts @@ -7,18 +7,27 @@ import createDebug from 'debug'; import { getProxyForUrl as envGetProxyForUrl } from 'proxy-from-env'; import type { PacProxyAgent, PacProxyAgentOptions } from 'pac-proxy-agent'; import type { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent'; -import type { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent'; -import type { SocksProxyAgent, SocksProxyAgentOptions } from 'socks-proxy-agent'; +import type { + HttpsProxyAgent, + HttpsProxyAgentOptions, +} from 'https-proxy-agent'; +import type { + SocksProxyAgent, + SocksProxyAgentOptions, +} from 'socks-proxy-agent'; const debug = createDebug('proxy-agent'); type ValidProtocol = | (typeof HttpProxyAgent.protocols)[number] | (typeof HttpsProxyAgent.protocols)[number] - | (typeof SocksProxyAgent.protocols)[number] - | (typeof PacProxyAgent.protocols)[number]; + | (typeof SocksProxyAgent.protocols)[number] + | (typeof PacProxyAgent.protocols)[number]; -type AgentConstructor = new (proxy: string, proxyAgentOptions?: ProxyAgentOptions) => Agent; +type AgentConstructor = new ( + proxy: string, + proxyAgentOptions?: ProxyAgentOptions +) => Agent; type GetProxyForUrlCallback = ( url: string, @@ -41,7 +50,10 @@ const wellKnownAgents = { * Supported proxy types. */ export const proxies: { - [P in ValidProtocol]: [() => Promise, () => Promise]; + [P in ValidProtocol]: [ + () => Promise, + () => Promise + ]; } = { http: [wellKnownAgents.http, wellKnownAgents.https], https: [wellKnownAgents.http, wellKnownAgents.https], @@ -150,8 +162,9 @@ export class ProxyAgent extends Agent { if (!isValidProtocol(proxyProto)) { throw new Error(`Unsupported protocol for proxy URL: ${proxy}`); } - const ctor = - await proxies[proxyProto][secureEndpoint || isWebSocket ? 1 : 0](); + const ctor = await proxies[proxyProto][ + secureEndpoint || isWebSocket ? 1 : 0 + ](); agent = new ctor(proxy, this.connectOpts); this.cache.set(cacheKey, agent); } else { diff --git a/packages/proxy-agent/tsconfig.json b/packages/proxy-agent/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/proxy-agent/tsconfig.json +++ b/packages/proxy-agent/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/proxy/tsconfig.json b/packages/proxy/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/proxy/tsconfig.json +++ b/packages/proxy/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/socks-proxy-agent/src/index.ts b/packages/socks-proxy-agent/src/index.ts index 7a911d20..d107d376 100644 --- a/packages/socks-proxy-agent/src/index.ts +++ b/packages/socks-proxy-agent/src/index.ts @@ -79,8 +79,7 @@ export type SocksProxyAgentOptions = Omit< 'ipaddress' | 'host' | 'port' | 'type' | 'userId' | 'password' > & { socketOptions?: SocksSocketOptions; -} & - http.AgentOptions; +} & http.AgentOptions; export class SocksProxyAgent extends Agent { static protocols = [ diff --git a/packages/socks-proxy-agent/test/test.ts b/packages/socks-proxy-agent/test/test.ts index f93b9444..526e2554 100644 --- a/packages/socks-proxy-agent/test/test.ts +++ b/packages/socks-proxy-agent/test/test.ts @@ -108,7 +108,9 @@ describe('SocksProxyAgent', () => { httpServer.once('request', (req, res) => res.end()); const res = await req(new URL('/foo', httpServerUrl), { - agent: new SocksProxyAgent(socksServerUrl, { socketOptions: { family: 6 } }), + agent: new SocksProxyAgent(socksServerUrl, { + socketOptions: { family: 6 }, + }), }); assert(res); }); @@ -117,13 +119,18 @@ describe('SocksProxyAgent', () => { let err: Error | undefined; try { await req(new URL('/foo', httpServerUrl), { - agent: new SocksProxyAgent(socksServerUrl, { socketOptions: { family: 4 } }), + agent: new SocksProxyAgent(socksServerUrl, { + socketOptions: { family: 4 }, + }), }); } catch (_err) { err = _err as Error; } assert(err); - assert.equal(err.message, `connect ECONNREFUSED 127.0.0.1:${socksServerUrl.port}`); + assert.equal( + err.message, + `connect ECONNREFUSED 127.0.0.1:${socksServerUrl.port}` + ); }); }); @@ -161,9 +168,9 @@ describe('SocksProxyAgent', () => { ) ); - socksServerUrl.username = 'nodejs' - socksServerUrl.password = 'rules!' - console.log(socksServerUrl.href) + socksServerUrl.username = 'nodejs'; + socksServerUrl.password = 'rules!'; + console.log(socksServerUrl.href); httpServer.once('request', function (req, res) { assert.equal('/foo', req.url); @@ -180,7 +187,6 @@ describe('SocksProxyAgent', () => { const body = await json(res); assert.equal('bar', body.foo); - }); it('should emit "error" event if username/password auth fails', async () => { @@ -199,8 +205,8 @@ describe('SocksProxyAgent', () => { ) ); - socksServerUrl.username = 'nodejs' - socksServerUrl.password = 'bad' + socksServerUrl.username = 'nodejs'; + socksServerUrl.password = 'bad'; let err: Error | undefined; try { diff --git a/packages/socks-proxy-agent/tsconfig.json b/packages/socks-proxy-agent/tsconfig.json index 24b6cf7e..c6a3e46a 100644 --- a/packages/socks-proxy-agent/tsconfig.json +++ b/packages/socks-proxy-agent/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "tsconfig/base.json", "compilerOptions": { - "outDir": "dist", + "outDir": "dist" }, "include": ["src"], "exclude": ["node_modules"] diff --git a/packages/tsconfig/base.json b/packages/tsconfig/base.json index b28bb607..1881d1bf 100644 --- a/packages/tsconfig/base.json +++ b/packages/tsconfig/base.json @@ -19,4 +19,4 @@ "lib": ["ESNext"], "strict": true } -} \ No newline at end of file +} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3ff5faaa..e9b0dad6 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,3 @@ packages: - - "apps/*" - - "packages/*" + - 'apps/*' + - 'packages/*'