Skip to content

Commit b32091e

Browse files
Introduce basic types
1 parent 1f48665 commit b32091e

28 files changed

+653
-321
lines changed

branch.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import name from './name.ts';
1+
import name from "./name.ts";
2+
import type { CreatePayload, Repo } from "./types.ts";
23

3-
export default function branch(repo, payload) {
4-
return `\n [\`${payload.ref}\`](https://github.com/${repo.name}/tree/${payload.ref})\n in${name(repo.name)}`;
4+
export default function branch(repo: Repo, payload: CreatePayload) {
5+
return `\n [\`${payload.ref}\`](https://github.com/${repo.name}/tree/${
6+
payload.ref
7+
})\n in${name(repo.name)}`;
58
}

commit.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import name from './name.ts';
1+
import name from "./name.ts";
2+
import type { PushPayload, Repo } from "./types.ts";
23

3-
export default function commit(repo, payload) {
4+
export default function commit(repo: Repo, payload: PushPayload) {
45
// TODO: Find out a new way to get the commit name
5-
return `\n [commit](https://github.com/${repo.name}/commit/${payload.head})\n into${name(repo.name)}`;
6+
return `\n [commit](https://github.com/${repo.name}/commit/${
7+
payload.head
8+
})\n into${name(repo.name)}`;
69
}

date.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
let now = new Date();
2-
const dates = {};
2+
const dates: { [key: string]: string } = {};
33
for (let index = 0; index < 7; index++) {
44
const date = now.toISOString().slice(0, 10);
55
if (index === 0) {
6-
dates[date] = 'Today';
7-
}
8-
else if (index === 1) {
9-
dates[date] = 'Yesterday';
10-
}
11-
else {
12-
dates[date] = now.toLocaleDateString('en-US', { weekday: 'long' });
6+
dates[date] = "Today";
7+
} else if (index === 1) {
8+
dates[date] = "Yesterday";
9+
} else {
10+
dates[date] = now.toLocaleDateString("en-US", { weekday: "long" });
1311
}
1412

1513
now = new Date(now.setDate(now.getDate() - 1));
1614
}
1715

18-
export default function date(instant) {
16+
export default function date(instant: Date) {
1917
const date = instant.toISOString().slice(0, 10);
2018
return dates[date] || date;
2119
}

downloadPages.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,67 @@
1-
import fs from 'fs';
2-
import headers from './headers.ts';
3-
import reportRateLimit from './reportRateLimit.ts';
4-
5-
/**
6-
*
7-
* @param {string} url
8-
*/
9-
export default async function downloadPages(url) {
10-
const result = [];
1+
import fs from "fs";
2+
import headers from "./headers.ts";
3+
import reportRateLimit from "./reportRateLimit.ts";
4+
5+
export default async function downloadPages(initialUrl: string) {
6+
const result: any[] = [];
7+
let url: string | undefined = initialUrl;
118
do {
9+
if (!url) {
10+
break;
11+
}
12+
1213
console.group(`Downloading ${url}…`);
13-
const response = await fetch(url, { headers });
14+
const response: any = await fetch(url, { headers });
1415
reportRateLimit(response.headers);
1516

1617
// Note that `Link` is not always there with single-page responses
17-
const link = response.headers.get('link') ?? '';
18+
const link = response.headers.get("link") ?? "";
1819
const regex = /<(?<url>[^>]+)>; rel="(?<rel>first|prev|next|last)"/g;
19-
const links = [...link.matchAll(regex)].reduce(
20-
(links, match) => {
21-
links[match.groups['rel']] = match.groups['url'];
22-
console.log(`Found ${match.groups['rel']} link ${match.groups['url']}`);
23-
return links;
24-
},
25-
{}
26-
);
20+
const links = [...link.matchAll(regex)].reduce((map, match) => {
21+
if (match.groups?.rel && match.groups.url) {
22+
map[match.groups.rel] = match.groups.url;
23+
console.log(`Found ${match.groups.rel} link ${match.groups.url}`);
24+
}
25+
return map;
26+
}, {});
2727

2828
const data = await response.json();
29-
29+
3030
// Save response to a file marked to be uploaded as an artifact for debugging
31-
await fs.promises.writeFile(`${url.match(/\w+/g).join('-')}.${response.status}.artifact.json`, JSON.stringify(data, null, 2));
32-
31+
const fileNameBits = url.match(/\w+/g) ?? [];
32+
await fs.promises.writeFile(
33+
`${fileNameBits.join("-") || "response"}.${
34+
response.status
35+
}.artifact.json`,
36+
JSON.stringify(data, null, 2)
37+
);
38+
3339
// GitHub Search API has a secondary rate limit which can report remaining calls but fail with a 403 still :(
3440
if (response.status !== 200) {
35-
console.log('X-GitHub-Request-Id:', response.headers.get('X-GitHub-Request-Id'));
36-
throw new Error(`Errored (${response.status} ${response.statusText}) mid-way paging while on URL ${url}:\n\n${JSON.stringify(data, null, 2)}}`);
41+
console.log(
42+
"X-GitHub-Request-Id:",
43+
response.headers.get("X-GitHub-Request-Id")
44+
);
45+
throw new Error(
46+
`Errored (${response.status} ${
47+
response.statusText
48+
}) mid-way paging while on URL ${url}:\n\n${JSON.stringify(
49+
data,
50+
null,
51+
2
52+
)}}`
53+
);
3754
}
3855

3956
if (Array.isArray(data)) {
4057
result.push(...data);
41-
}
42-
else {
58+
} else {
4359
result.push(data);
4460
}
4561

4662
url = links.next;
4763
console.groupEnd();
48-
}
49-
while (url);
64+
} while (url);
5065

5166
return result;
5267
}

extract.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,68 @@
11
/* https://github.com/TomasHubelbauer/node-extract-zip */
22

3-
import util from 'util';
4-
import zlib from 'zlib';
3+
import util from "util";
4+
import zlib from "zlib";
55

66
/**
77
* Extracts a single file from a single-file DEFLATE ZIP archive.
8-
* @param {Buffer} buffer The ZIP archive file content buffer
98
*/
10-
export default async function extract(buffer) {
9+
export default async function extract(buffer: Buffer) {
1110
// https://en.wikipedia.org/wiki/ZIP_(file_format)#End_of_central_directory_record_(EOCD)
12-
const eocdIndex = buffer.indexOf(0x504b0506.toString(16) /* BE not LE */, 0, 'hex');
11+
const eocdIndex = buffer.indexOf(
12+
(0x504b0506).toString(16) /* BE not LE */,
13+
0,
14+
"hex"
15+
);
1316
if (eocdIndex === -1) {
14-
throw new Error('0x06054b50 (EOCD) not found!');
17+
throw new Error("0x06054b50 (EOCD) not found!");
1518
}
1619

17-
if (buffer.readUint16LE(eocdIndex + 4) !== 0 || buffer.readUint16LE(eocdIndex + 6) !== 0) {
18-
throw new Error('EOCD disk number or CD disk number is not zero; is multi-disk');
20+
if (
21+
buffer.readUint16LE(eocdIndex + 4) !== 0 ||
22+
buffer.readUint16LE(eocdIndex + 6) !== 0
23+
) {
24+
throw new Error(
25+
"EOCD disk number or CD disk number is not zero; is multi-disk"
26+
);
1927
}
2028

2129
if (buffer.readUint16LE(eocdIndex + 8) !== 1) {
22-
throw new Error('CD count is not one; is multi-file');
30+
throw new Error("CD count is not one; is multi-file");
2331
}
2432

2533
// https://en.wikipedia.org/wiki/ZIP_(file_format)#Central_directory_file_header
2634
const cdIndex = buffer.readUint32LE(eocdIndex + 16);
2735
if (buffer.readUInt32LE(cdIndex) !== 0x02014b50) {
28-
throw new Error('0x02014b50 (CD) not found!');
36+
throw new Error("0x02014b50 (CD) not found!");
2937
}
3038

3139
if (buffer.readUint16LE(cdIndex + 10) !== 8) {
32-
throw new Error('CD compression method is not 8 (DEFLATE); is uncompressed?');
40+
throw new Error(
41+
"CD compression method is not 8 (DEFLATE); is uncompressed?"
42+
);
3343
}
3444

3545
const size = buffer.readUint32LE(cdIndex + 20);
3646

3747
// https://en.wikipedia.org/wiki/ZIP_(file_format)#Local_file_header
3848
const lfhIndex = buffer.readUint32LE(cdIndex + 42);
3949
if (buffer.readUInt32LE(lfhIndex) !== 0x04034b50) {
40-
throw new Error('0x04034b50 (LFH) not found!');
50+
throw new Error("0x04034b50 (LFH) not found!");
4151
}
4252

4353
if (buffer.readUint16LE(lfhIndex + 8) !== 8) {
44-
throw new Error('LFH compression method is not 8 (DEFLATE); is uncompressed?');
54+
throw new Error(
55+
"LFH compression method is not 8 (DEFLATE); is uncompressed?"
56+
);
4557
}
4658

47-
const offset = lfhIndex + 30 + buffer.readUint16LE(lfhIndex + 26) + buffer.readUint16LE(lfhIndex + 28);
59+
const offset =
60+
lfhIndex +
61+
30 +
62+
buffer.readUint16LE(lfhIndex + 26) +
63+
buffer.readUint16LE(lfhIndex + 28);
4864

49-
return util.promisify(zlib.inflateRaw)(buffer.subarray(offset, offset + size));
65+
return util.promisify(zlib.inflateRaw)(
66+
buffer.subarray(offset, offset + size)
67+
);
5068
}

0 commit comments

Comments
 (0)