Skip to content

Commit

Permalink
Merge pull request #2 from aegenet/feat-simplify-fetch-w-timeout
Browse files Browse the repository at this point in the history
Feat simplify `fetch-w-timeout`
  • Loading branch information
aegenet authored Oct 9, 2023
2 parents b070655 + cdcb71d commit 9728174
Show file tree
Hide file tree
Showing 7 changed files with 533 additions and 529 deletions.
2 changes: 1 addition & 1 deletion .build/build-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const tasks = {
version = process.env.GITHUB_REF_NAME;
} else if (process.env.GITHUB_REF_NAME) {
// workflow github
version = `999.${new Date().getTime()}.0`;
version = `0.${new Date().getTime()}.0-dev`;
}

if (version) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@aegenet/ya-vigil-reporter",
"description": "Yet Another Vigil Reporter",
"version": "999.0.0",
"version": "0.0.0-dev",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"exports": {
Expand Down Expand Up @@ -37,20 +37,20 @@
},
"devDependencies": {
"@fastify/pre-commit": "^2.0.2",
"@types/mocha": "^10.0.0",
"@types/mocha": "^10.0.2",
"@types/node": "^20.4.5",
"@typescript-eslint/eslint-plugin": "^6.2.0",
"@typescript-eslint/parser": "^6.2.0",
"eslint": "^8.45.0",
"eslint-config-prettier": "^8.9.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"fastify": "^4.20.0",
"json": "^11.0.0",
"mocha": "^10.1.0",
"nyc": "^15.1.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.1",
"rollup-plugin-dts": "^5.0.0",
"rollup-plugin-dts": "^6.1.0",
"ts-mocha": "^10.0.0",
"typescript": "^5.1.6",
"vite": "^4.4.7"
Expand Down
26 changes: 7 additions & 19 deletions src/utils/ya-fetch-w-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@ export type YaFetchWTimeoutOptions = {

/** Fetch with Timeout */
export async function yaFetchWTimeout(input: RequestInfo | URL, init: RequestInit, options: YaFetchWTimeoutOptions): Promise<Response> {
let timeoutToken;
try {
const controller = new AbortController();
timeoutToken = setTimeout(() => controller.abort(), options.timeout);
const resp = await fetch(input, {
...init,
signal: controller.signal,
});
clearTimeout(timeoutToken);
timeoutToken = undefined;
if (resp && !resp.ok) {
throw new Error(options.formatErrorMessage ? await options.formatErrorMessage(resp) : await _defaultFormatErrorMessage(resp));
}
return resp;
} catch (error) {
if (timeoutToken) {
clearTimeout(timeoutToken);
}
throw error;
const resp = await fetch(input, {
...init,
signal: AbortSignal.timeout(options.timeout),
});
if (resp && !resp.ok) {
throw new Error(options.formatErrorMessage ? await options.formatErrorMessage(resp) : await _defaultFormatErrorMessage(resp));
}
return resp;
}

async function _defaultFormatErrorMessage(resp: Response): Promise<string> {
Expand Down
6 changes: 3 additions & 3 deletions src/ya-vigil-reporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe('ya-vigil-reporter', () => {
await vigilReporter.report();
throw new Error('Must fail!');
} catch (error) {
assert.ok((error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.ok((error as Error).message === 'The operation was aborted due to timeout' || (error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.strictEqual(errors.length, 1);
}

Expand Down Expand Up @@ -424,7 +424,7 @@ describe('ya-vigil-reporter', () => {
});
throw new Error('Must fail!');
} catch (error) {
assert.ok((error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.ok((error as Error).message === 'The operation was aborted due to timeout' || (error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.strictEqual(errors.length, 1);
assert.strictEqual(infos.length, 1);
}
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('ya-vigil-reporter', () => {
await vigilReporter.report();
throw new Error('Must fail!');
} catch (error) {
assert.ok((error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.ok((error as Error).message === 'The operation was aborted due to timeout' || (error as Error).message === 'This operation was aborted' || (error as Error).message === 'The operation was aborted.');
assert.strictEqual(errors.length, 1);
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/ya-vigil-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setInterval, clearInterval } from 'node:timers';
import { assertVigilReporterOptions, YaVigilReportResult, type YaVigilReporterOptions } from './models/ya-vigil-reporter-options';
import { YaWorkload, type YaWorkloadResult } from './utils/ya-workload';
import type { YaVigilReportBody } from './models/ya-vigil-report-body';
Expand All @@ -19,7 +20,7 @@ export class YaVigilReporter implements IYaVigilReporter {
private readonly _intervalMs: number;
private readonly _timeoutMs: number;
private _currentCpuUsage?: YaWorkloadResult;
private _cron?: NodeJS.Timer;
private _cron?: NodeJS.Timeout;

constructor(private readonly _options: YaVigilReporterOptions) {
// legacy
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"experimentalDecorators": true,
"moduleResolution": "node",
"skipLibCheck": true,
"target": "ES2020",
"target": "ES2022",
"esModuleInterop": true,
"resolveJsonModule": true,
"strict": true,
Expand Down
Loading

0 comments on commit 9728174

Please sign in to comment.