Skip to content

Commit

Permalink
Run format check in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Dec 6, 2024
1 parent 1699a09 commit 54ea8b5
Show file tree
Hide file tree
Showing 25 changed files with 169 additions and 109 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
packages/*/dist
packages/degenerator/test/*.js
*.md
*.pac
pnpm-lock.yaml
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"singleQuote": true
"singleQuote": true
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/agent-base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
88 changes: 46 additions & 42 deletions packages/data-uri-to-buffer/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
};
}
7 changes: 5 additions & 2 deletions packages/data-uri-to-buffer/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { makeDataUriToBuffer } from './common';

export type { ParsedDataURI } from './common';
export type { ParsedDataURI } from './common';

function base64ToArrayBuffer(base64: string) {
const chars =
Expand Down Expand Up @@ -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,
});
7 changes: 5 additions & 2 deletions packages/data-uri-to-buffer/src/node.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
});
2 changes: 1 addition & 1 deletion packages/degenerator/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
5 changes: 1 addition & 4 deletions packages/get-uri/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion packages/http-proxy-agent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
3 changes: 1 addition & 2 deletions packages/https-proxy-agent/test/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ interface NordVPNServer {
locations: {
country: {
code: string;
}

};
}[];
technologies: {
identifier: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/https-proxy-agent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
5 changes: 4 additions & 1 deletion packages/pac-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/pac-proxy-agent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
52 changes: 29 additions & 23 deletions packages/pac-resolver/src/ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -40,8 +47,7 @@ export const ip = {
}

return family === 'ipv4' ? '127.0.0.1' : 'fe80::1';
}

},
};

function normalizeFamily(family?: unknown): IpFamily {
Expand All @@ -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';
2 changes: 1 addition & 1 deletion packages/pac-resolver/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/base.json",
"compilerOptions": {
"outDir": "dist",
"outDir": "dist"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
Loading

0 comments on commit 54ea8b5

Please sign in to comment.