Skip to content

Commit

Permalink
Add a configurable timeout to the requests (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
GusRuss89 authored Oct 13, 2021
1 parent a3ba337 commit 6f58737
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tall",
"version": "4.0.1",
"version": "4.1.0",
"description": "Promise-based, No-dependency URL unshortner (expander) module for Node.js",
"type": "module",
"main": "./lib/cjs/index.js",
Expand All @@ -23,6 +23,7 @@
"build": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && echo '{\"type\":\"commonjs\"}' > lib/cjs/package.json",
"build:test": "node test/commonjs.cjs && node test/esm.js",
"prepublish": "npm run build",
"prepare": "npm run build",
"test:lint": "eslint .",
"test:unit": "jest --coverage --verbose",
"test": "npm run test:lint && npm run test:unit"
Expand Down
26 changes: 26 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ test('it should fail if a URL without protocol is given', async () => {
await expect(() => tall('example.com')).rejects.toMatchObject({ message: 'Invalid URL: example.com' })
})

test('it should fail if the request times out', async () => {
nock('http://example.com')
.get('/a-link')
.times(1)
.delay(1000)
.reply(200, 'OK')

await expect(() => tall('http://example.com/a-link', { timeout: 1 })).rejects.toMatchObject({ message: 'socket hang up' })
})

test('it should not fail if the request is within the timeout', async () => {
nock('http://example.com')
.get('/a-link')
.times(1)
.delay(1)
.reply(301, 'Moved', { location: 'http://dest.pizza/a-link' })
nock('http://dest.pizza')
.get('/a-link')
.times(1)
.reply(200, 'OK')

const url = await tall('http://example.com/a-link', { timeout: 1000 })

expect(url).toBe('http://dest.pizza/a-link')
})

test('it should return the original url if no redirect', async () => {
nock('https://example.com')
.get('/test')
Expand Down
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ export interface TallHTTPHeaders {
}

export interface TallOptions {
method?: TallAvailableHTTPMethod
maxRedirects?: number
headers?: TallHTTPHeaders
method: TallAvailableHTTPMethod
maxRedirects: number
headers: TallHTTPHeaders
timeout: number
}

const defaultOptions: TallOptions = {
method: 'GET',
maxRedirects: 3,
headers: {}
headers: {},
timeout: 120000
}

export const tall = (url: string, options?: TallOptions): Promise<string> => {
export const tall = (url: string, options?: Partial<TallOptions>): Promise<string> => {
const opt = Object.assign({}, defaultOptions, options)
return new Promise((resolve, reject) => {
try {
Expand All @@ -35,7 +37,7 @@ export const tall = (url: string, options?: TallOptions): Promise<string> => {
const method = opt.method
const request = protocol === 'https:' ? httpsReq : httpReq
const headers = opt.headers
return request(url, { method, headers }, response => {
const req = request(url, { method, headers }, response => {
if (response.headers.location && opt.maxRedirects) {
opt.maxRedirects--
return resolve(
Expand All @@ -48,8 +50,10 @@ export const tall = (url: string, options?: TallOptions): Promise<string> => {

resolve(url)
})
.on('error', reject)
.end()
req.on('error', reject)
req.setTimeout(opt.timeout, () => req.destroy())
req.end()
return req
} catch (err) {
return reject(err)
}
Expand Down

0 comments on commit 6f58737

Please sign in to comment.