Skip to content

Commit 6432492

Browse files
committed
better helpers
1 parent 0ec2f70 commit 6432492

4 files changed

Lines changed: 26 additions & 37 deletions

File tree

src/_internalUtils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { spawnSync, SpawnSyncOptionsWithStringEncoding } from 'child_process';
22
import crypto from 'crypto';
33
import fs from 'fs';
4-
import http from 'http';
5-
import https from 'https';
64
import util from 'util';
75
import zlib from 'zlib';
86

@@ -55,13 +53,21 @@ async function fetchOk(input: RequestInfo | URL, init?: RequestInit): Promise<Re
5553
return response;
5654
}
5755

58-
export async function fetchWithRetries(input: RequestInfo | URL, init?: RequestInit, backoff: number[] = HTTP_BACKOFF): Promise<Response> {
59-
return await retryWithBackoff(async () => await fetchOk(input, init), backoff);
56+
export async function getJSON<T>(input: RequestInfo | URL, init?: RequestInit, backoff: number[] = HTTP_BACKOFF): Promise<T> {
57+
return await retryWithBackoff(async () => {
58+
const response = await fetchOk(input, init);
59+
return await response.json() as T;
60+
}, backoff);
6061
}
6162

62-
export async function fetchAndDrainWithRetries(input: RequestInfo | URL, init?: RequestInit, backoff: number[] = HTTP_BACKOFF): Promise<void> {
63+
export async function putBuffer(input: RequestInfo | URL, body: Buffer, headers?: HeadersInit, backoff: number[] = HTTP_BACKOFF): Promise<void> {
6364
await retryWithBackoff(async () => {
64-
const response = await fetchOk(input, init);
65+
const response = await fetchOk(input, {
66+
method: 'PUT',
67+
headers,
68+
body: new Uint8Array(body),
69+
});
70+
// Read response to ensure it completes.
6571
await response.arrayBuffer();
6672
}, backoff);
6773
}

src/fetchTestDurations.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FlakinessReport } from '@flakiness/flakiness-report';
22
import { URL } from 'url';
3-
import { compressTextAsync, fetchAndDrainWithRetries, fetchWithRetries, sha1Text } from './_internalUtils.js';
3+
import { compressTextAsync, getJSON, putBuffer, sha1Text } from './_internalUtils.js';
44
import { GithubOIDC } from './githubOIDC.js';
55

66
type TestDurationsFetcherOptions = {
@@ -104,14 +104,14 @@ class TestDurationsFetcher {
104104
private async _api<OUTPUT>(pathname: string, token: string, body?: any): Promise<OUTPUT> {
105105
const url = new URL(this._options.flakinessEndpoint);
106106
url.pathname = pathname;
107-
return await fetchWithRetries(url, {
107+
return await getJSON<OUTPUT>(url, {
108108
method: 'POST',
109109
headers: {
110110
'Authorization': `Bearer ${token}`,
111111
'Content-Type': 'application/json',
112112
},
113113
body: body ? JSON.stringify(body) : undefined,
114-
}).then(async response => await response.json());
114+
});
115115
}
116116

117117
async fetch(): Promise<FlakinessReport.Report> {
@@ -139,7 +139,7 @@ class TestDurationsFetcher {
139139
createResponse.testDurationsToken,
140140
);
141141

142-
return await fetchWithRetries(submitResponse.downloadUrl, undefined, DOWNLOAD_BACKOFF).then(async response => await response.json() as FlakinessReport.Report);
142+
return await getJSON<FlakinessReport.Report>(submitResponse.downloadUrl, undefined, DOWNLOAD_BACKOFF);
143143
}
144144

145145
private async _uploadReport(data: string, uploadUrl: string) {
@@ -149,10 +149,6 @@ class TestDurationsFetcher {
149149
'Content-Length': Buffer.byteLength(compressed) + '',
150150
'Content-Encoding': 'br',
151151
};
152-
await fetchAndDrainWithRetries(uploadUrl, {
153-
method: 'PUT',
154-
headers,
155-
body: Buffer.from(compressed),
156-
});
152+
await putBuffer(uploadUrl, compressed, headers);
157153
}
158154
}

src/githubOIDC.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { fetchWithRetries } from './_internalUtils.js';
1+
import { getJSON } from './_internalUtils.js';
22

33
/**
44
* Provides GitHub Actions OIDC (OpenID Connect) token exchange.
@@ -58,9 +58,9 @@ export class GithubOIDC {
5858
const url = new URL(this._requestUrl);
5959
url.searchParams.set('audience', flakinessProject);
6060

61-
let response: Response;
61+
let json: { value?: string };
6262
try {
63-
response = await fetchWithRetries(url, {
63+
json = await getJSON<{ value?: string }>(url, {
6464
headers: {
6565
'Authorization': `bearer ${this._requestToken}`,
6666
'Accept': 'application/json; api-version=2.0',
@@ -70,7 +70,6 @@ export class GithubOIDC {
7070
throw new Error(`Failed to request GitHub OIDC token: ${error.message || String(error)}`);
7171
}
7272

73-
const json = await response.json() as { value?: string };
7473
if (!json.value)
7574
throw new Error('GitHub OIDC token response did not contain a token value.');
7675

src/uploadReport.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import assert from 'assert';
33
import fs from 'fs';
44
import { URL } from 'url';
55
import { GithubOIDC } from './githubOIDC.js';
6-
import { compressTextAsync, fetchAndDrainWithRetries, fetchWithRetries, sha1File, sha1Text } from './_internalUtils.js';
6+
import { compressTextAsync, getJSON, putBuffer, sha1File, sha1Text } from './_internalUtils.js';
77

88
type ReportUploaderOptions = {
99
flakinessEndpoint: string;
@@ -308,7 +308,7 @@ class ReportUpload {
308308
const url = new URL(this._options.flakinessEndpoint);
309309
url.pathname = pathname;
310310
try {
311-
const response = await fetchWithRetries(url, {
311+
const result = await getJSON<OUTPUT>(url, {
312312
method: 'POST',
313313
headers: {
314314
'Authorization': `Bearer ${token}`,
@@ -317,7 +317,7 @@ class ReportUpload {
317317
body: body ? JSON.stringify(body) : undefined,
318318
});
319319
return {
320-
result: await response.json() as OUTPUT,
320+
result,
321321
error: undefined,
322322
};
323323
} catch (error: any) {
@@ -370,11 +370,7 @@ class ReportUpload {
370370
'Content-Length': Buffer.byteLength(compressed) + '',
371371
'Content-Encoding': 'br',
372372
};
373-
await fetchAndDrainWithRetries(uploadUrl, {
374-
method: 'PUT',
375-
headers,
376-
body: Buffer.from(compressed),
377-
});
373+
await putBuffer(uploadUrl, compressed, headers);
378374
}
379375

380376
private async _uploadAttachment(attachment: Attachment, uploadUrl: string) {
@@ -387,13 +383,9 @@ class ReportUpload {
387383
// Stream file only if there's attachment path and we should NOT compress it.
388384
if (!compressable && attachment.type === 'file') {
389385
const fileBuffer = await fs.promises.readFile(attachment.path);
390-
await fetchAndDrainWithRetries(uploadUrl, {
391-
method: 'PUT',
392-
headers: {
386+
await putBuffer(uploadUrl, fileBuffer, {
393387
'Content-Type': attachment.contentType,
394388
'Content-Length': fileBuffer.length + '',
395-
},
396-
body: new Uint8Array(fileBuffer),
397389
});
398390
return;
399391
}
@@ -413,10 +405,6 @@ class ReportUpload {
413405
headers['Content-Encoding'] = encoding;
414406
}
415407

416-
await fetchAndDrainWithRetries(uploadUrl, {
417-
method: 'PUT',
418-
headers,
419-
body: new Uint8Array(buffer),
420-
});
408+
await putBuffer(uploadUrl, buffer, headers);
421409
}
422410
}

0 commit comments

Comments
 (0)