Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"flat-cache": "^6.1.1",
"lodash": "^4.17.15",
"ssri": "^12.0.0",
"timeout-signal": "^1.1.0",
"type-is": "^1.6.18"
},
"devDependencies": {
Expand Down
8 changes: 1 addition & 7 deletions packages/node/src/lib/get-project-base-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import crypto from 'crypto';

import findCacheDir from 'find-cache-dir';
import { FlatCache } from 'flat-cache';
import timeoutSignal from 'timeout-signal';

import pkg from '../../package.json';
import config from '../config';
Expand Down Expand Up @@ -35,8 +34,6 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c
lastUpdated === undefined ||
(lastUpdated !== undefined && Math.abs(lastUpdated - Math.round(Date.now() / 1000)) >= 86400)
) {
const signal = timeoutSignal(requestTimeout);

let baseUrl = '';
await fetch(`${config.readmeApiUrl}/v1/`, {
method: 'get',
Expand All @@ -46,7 +43,7 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
signal,
signal: AbortSignal.timeout(requestTimeout),
})
.then(res => {
if (res.status >= 400 && res.status <= 599) {
Expand All @@ -69,9 +66,6 @@ export async function getProjectBaseUrl(readmeApiKey: string, requestTimeout = c
// now yesterday so that in 2 minutes we'll automatically make another attempt.
cache.setKey('baseUrl', null);
cache.setKey('lastUpdated', Math.round(Date.now() / 1000) - 86400 + 120);
})
.finally(() => {
timeoutSignal.clear(signal);
});

cache.save();
Expand Down
31 changes: 12 additions & 19 deletions packages/node/src/lib/metrics-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { Options } from './log';
import type { Har } from 'har-format';
import type { UUID } from 'node:crypto';

import timeoutSignal from 'timeout-signal';

import pkg from '../../package.json';
import config from '../config';

Expand Down Expand Up @@ -90,7 +88,6 @@ function getLogIds(body: OutgoingLogBody | OutgoingLogBody[]): LogId {
}

export function metricsAPICall(readmeAPIKey: string, body: OutgoingLogBody[], options: Options): Promise<LogResponse> {
const signal = timeoutSignal(config.timeout);
const encodedKey = Buffer.from(`${readmeAPIKey}:`).toString('base64');

const makeRequest = () => {
Expand All @@ -112,23 +109,19 @@ export function metricsAPICall(readmeAPIKey: string, body: OutgoingLogBody[], op
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
signal,
})
.then(response => {
if (shouldBackoff(response)) {
// backoff for a few seconds, but not if another callback has already started backing off
if (!backoffExpiresAt) {
backoffExpiresAt = new Date();
backoffExpiresAt.setSeconds(backoffExpiresAt.getSeconds() + BACKOFF_SECONDS);
}
signal: AbortSignal.timeout(config.timeout),
}).then(response => {
if (shouldBackoff(response)) {
// backoff for a few seconds, but not if another callback has already started backing off
if (!backoffExpiresAt) {
backoffExpiresAt = new Date();
backoffExpiresAt.setSeconds(backoffExpiresAt.getSeconds() + BACKOFF_SECONDS);
}
const logLevel = response.ok ? 'info' : 'error';
logger[logLevel]({ message: `Service responded with status ${response.status}: ${response.statusText}.` });
return response;
})
.finally(() => {
timeoutSignal.clear(signal);
});
}
const logLevel = response.ok ? 'info' : 'error';
logger[logLevel]({ message: `Service responded with status ${response.status}: ${response.statusText}.` });
return response;
});
};

if (options.fireAndForget) {
Expand Down