Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: timeout parameter being respected #850

Merged
merged 1 commit into from
Aug 9, 2023
Merged
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
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