Skip to content

Commit

Permalink
fix: timeout parameter being respected
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Aug 8, 2023
1 parent f04c595 commit 3dcd3fe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
40 changes: 13 additions & 27 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,21 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node: [16.x, 18.x, 20.x]
experimental: [false]
include:
- os: ubuntu-latest
node: 14
experimental: true
npm_version: "@8.3.1"
- os: windows-latest
node: 14
experimental: true
npm_version: "@8.3.1"
- os: macos-latest
node: 14
experimental: true
npm_version: "@8.3.1"
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v3

- name: Set up Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
- name: Set up Node.js ${{ matrix.node }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install typescript
run: npm install -g typescript npm${{ matrix.npm_version }} && npm install
- name: Install typescript
run: npm install -g typescript npm${{ matrix.npm_version }} && npm install

- name: Lint, Compile, Test
run: npm run compile && npm run test -- --coverage --verbose --maxWorkers=2
- name: Lint, Compile, Test
run: npm run compile && npm run test -- --coverage --verbose --maxWorkers=2

- name: Run codecov
uses: codecov/codecov-action@v3
with:
directory: ./coverage
- name: Run codecov
uses: codecov/codecov-action@v3
with:
directory: ./coverage
21 changes: 12 additions & 9 deletions packages/vetch/__tests__/vetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ describe('option configuration', () => {
});

test('should default to application/json', async () => {
nock(url)
.matchHeader('accept', 'application/json')
.get('/')
.reply(200, {});
nock(url).matchHeader('accept', 'application/json').get('/').reply(200, {});
const res = await request({ url });
expect(res.data).toEqual({});
});
Expand All @@ -122,19 +119,25 @@ describe('option configuration', () => {
test('should allow for our custom user agent', async () => {
const options = {
reqheaders: {
'user-agent': (val) => {
return /^@vonage\/server-sdk\/[\d].[\d].[\d].* node\/.*$/.test(
val,
);
'user-agent': (val: string) => {
return /^@vonage\/server-sdk\/[\d].[\d].[\d].* node\/.*$/.test(val);
},
},
};

nock(url, options).get('/').reply(200);
const inst = new Vetch();
const res = await inst.request({ url });
await inst.request({ url });
expect(nock.isDone()).toBeTruthy();
});

test('should timeout', async () => {
nock(url).get('/').delayConnection(5).reply(200);
const inst = new Vetch({ timeout: 1 });
await expect(inst.request({ url })).rejects.toThrow(
'The user aborted a request',
);
});
});

describe('data handling', () => {
Expand Down
26 changes: 19 additions & 7 deletions packages/vetch/lib/vetch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import fetch, { Response as fetchResponse } from 'node-fetch';
import fetch, { Response as fetchResponse, options } from 'node-fetch';
import { stringify } from 'querystring';
import merge from 'lodash.merge';
import http from 'http';
import https from 'https';
import URL from 'url';
import { VetchError } from './types/vetchError';
import { Headers } from './interfaces/headers';
import { VetchResponse } from './interfaces/vetchResponse';
Expand All @@ -26,9 +25,22 @@ export class Vetch {
private async _defaultAdapter<T>(
opts: VetchOptions,
): Promise<VetchResponse<T>> {
const res = await fetch(opts.url, opts);
const data = await this.getResponseData(opts, res);
return this.createResponse(opts, res, data);
const { timeout } = opts;
let timeoutId = null;
const fetchConfig: options = opts;
if (timeout) {
const controller = new AbortController();
timeoutId = setTimeout(() => controller.abort(), timeout);
fetchConfig.signal = controller.signal;
}

try {
const res = await fetch(opts.url, fetchConfig);
const data = await this.getResponseData(opts, res);
return this.createResponse(opts, res, data);
} finally {
clearTimeout(timeoutId);
}
}

async request<T>(opts: VetchOptions = {}): Promise<VetchResponse<T>> {
Expand Down Expand Up @@ -116,10 +128,10 @@ export class Vetch {

// Allow a custom timeout to be used
const httpAgent = new http.Agent({
timeout: this.defaults.timeout,
timeout: opts.timeout,
});
const httpsAgent = new https.Agent({
timeout: this.defaults.timeout,
timeout: opts.timeout,
});
opts.agent = (parsedUrl: URL): https.Agent | http.Agent => {
if (parsedUrl.protocol === 'http:') {
Expand Down

0 comments on commit 3dcd3fe

Please sign in to comment.