@atolis-hq/wake 0.3.63 → 0.3.64
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const defaultMaximumGitHubResponseBytes = 8 * 1024 * 1024;
|
|
2
|
+
export class GitHubResponseTooLargeError extends Error {
|
|
3
|
+
maximumBytes;
|
|
4
|
+
observedBytes;
|
|
5
|
+
constructor(maximumBytes, observedBytes) {
|
|
6
|
+
super(`GitHub response exceeded ${maximumBytes} bytes (observed ${observedBytes})`);
|
|
7
|
+
this.maximumBytes = maximumBytes;
|
|
8
|
+
this.observedBytes = observedBytes;
|
|
9
|
+
this.name = 'GitHubResponseTooLargeError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function createBoundedGitHubFetch(baseFetch = globalThis.fetch, maximumResponseBytes = defaultMaximumGitHubResponseBytes) {
|
|
13
|
+
return async (input, init) => {
|
|
14
|
+
const response = await baseFetch(input, init);
|
|
15
|
+
const declaredBytes = contentLength(response);
|
|
16
|
+
if (declaredBytes !== undefined && declaredBytes > maximumResponseBytes) {
|
|
17
|
+
await response.body?.cancel();
|
|
18
|
+
throw new GitHubResponseTooLargeError(maximumResponseBytes, declaredBytes);
|
|
19
|
+
}
|
|
20
|
+
return new Proxy(response, {
|
|
21
|
+
get(target, property) {
|
|
22
|
+
if (property === 'text')
|
|
23
|
+
return async () => decode(await readBoundedBody(target, maximumResponseBytes));
|
|
24
|
+
if (property === 'arrayBuffer')
|
|
25
|
+
return async () => (await readBoundedBody(target, maximumResponseBytes)).buffer;
|
|
26
|
+
if (property === 'json')
|
|
27
|
+
return async () => JSON.parse(decode(await readBoundedBody(target, maximumResponseBytes)));
|
|
28
|
+
const value = Reflect.get(target, property, target);
|
|
29
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function contentLength(response) {
|
|
35
|
+
const value = response.headers.get('content-length');
|
|
36
|
+
if (value === null)
|
|
37
|
+
return undefined;
|
|
38
|
+
const parsed = Number(value);
|
|
39
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
40
|
+
}
|
|
41
|
+
async function readBoundedBody(response, maximumBytes) {
|
|
42
|
+
if (response.body === null)
|
|
43
|
+
return new Uint8Array();
|
|
44
|
+
const reader = response.body.getReader();
|
|
45
|
+
const chunks = [];
|
|
46
|
+
let observedBytes = 0;
|
|
47
|
+
try {
|
|
48
|
+
while (true) {
|
|
49
|
+
const chunk = await reader.read();
|
|
50
|
+
if (chunk.done)
|
|
51
|
+
return concatChunks(chunks, observedBytes);
|
|
52
|
+
observedBytes += chunk.value.byteLength;
|
|
53
|
+
if (observedBytes > maximumBytes) {
|
|
54
|
+
await reader.cancel();
|
|
55
|
+
throw new GitHubResponseTooLargeError(maximumBytes, observedBytes);
|
|
56
|
+
}
|
|
57
|
+
chunks.push(chunk.value);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
reader.releaseLock();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function decode(bytes) {
|
|
65
|
+
return new TextDecoder().decode(bytes);
|
|
66
|
+
}
|
|
67
|
+
function concatChunks(chunks, length) {
|
|
68
|
+
const body = new Uint8Array(length);
|
|
69
|
+
let offset = 0;
|
|
70
|
+
for (const chunk of chunks) {
|
|
71
|
+
body.set(chunk, offset);
|
|
72
|
+
offset += chunk.byteLength;
|
|
73
|
+
}
|
|
74
|
+
return body;
|
|
75
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Octokit } from '@octokit/rest';
|
|
2
2
|
import { MergeMethod, ProviderPermission, PullRequestState } from '../../../activities/index.js';
|
|
3
3
|
import { GitHubOutboundAction } from '../contracts/vocabulary.js';
|
|
4
|
+
import { createBoundedGitHubFetch } from './bounded-fetch.js';
|
|
4
5
|
import { branch, getCombinedStatusForRef, getIssueLabels, getPullRequest, listCheckRunsForRef, listIssueComments, listIssues, listPullRequestFiles, listPullRequests, listReviewComments, listReviews, } from './client-reads.js';
|
|
5
6
|
import { createEtagCache } from './etag-cache.js';
|
|
6
7
|
// Octokit's request-log plugin reports every non-2xx response through this
|
|
@@ -29,6 +30,7 @@ function logGitHubRequestFailure(message) {
|
|
|
29
30
|
export function createGitHubClient(token) {
|
|
30
31
|
const octokit = new Octokit({
|
|
31
32
|
auth: token,
|
|
33
|
+
request: { fetch: createBoundedGitHubFetch() },
|
|
32
34
|
log: { debug() { }, info() { }, warn() { }, error: logGitHubRequestFailure },
|
|
33
35
|
});
|
|
34
36
|
const cache = createEtagCache();
|