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

feat(bent): add http request timeout option for nodejs #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
148 changes: 77 additions & 71 deletions src/nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,86 +93,92 @@ const decodings = res => {
}
}

const mkrequest = (statusCodes, method, encoding, headers, baseurl) => (_url, body = null, _headers = {}) => {
_url = baseurl + (_url || '')
const parsed = new URL(_url)
let h
if (parsed.protocol === 'https:') {
h = https
} else if (parsed.protocol === 'http:') {
h = http
} else {
throw new Error(`Unknown protocol, ${parsed.protocol}`)
}
const request = {
path: parsed.pathname + parsed.search,
port: parsed.port,
method: method,
headers: { ...(headers || {}), ..._headers },
hostname: parsed.hostname
}
if (parsed.username || parsed.password) {
request.auth = [parsed.username, parsed.password].join(':')
}
const c = caseless(request.headers)
if (encoding === 'json') {
if (!c.get('accept')) {
c.set('accept', 'application/json')
const mkrequest = (statusCodes, method, encoding, headers, baseurl) => {
const inner = (_url, body = null, _headers = {}) => {
_url = baseurl + (_url || '')
const parsed = new URL(_url)
let h
if (parsed.protocol === 'https:') {
h = https
} else if (parsed.protocol === 'http:') {
h = http
} else {
throw new Error(`Unknown protocol, ${parsed.protocol}`)
}
}
if (!c.has('accept-encoding')) {
c.set('accept-encoding', acceptEncoding)
}
return new Promise((resolve, reject) => {
const req = h.request(request, async res => {
res = getResponse(res)
res.on('error', reject)
decodings(res)
res.status = res.statusCode
if (!statusCodes.has(res.statusCode)) {
return reject(new StatusError(res))
const request = {
path: parsed.pathname + parsed.search,
port: parsed.port,
method: method,
headers: { ...(headers || {}), ..._headers },
hostname: parsed.hostname
}
if (parsed.username || parsed.password) {
request.auth = [parsed.username, parsed.password].join(':')
}
const c = caseless(request.headers)
if (encoding === 'json') {
if (!c.get('accept')) {
c.set('accept', 'application/json')
}
}
if (!c.has('accept-encoding')) {
c.set('accept-encoding', acceptEncoding)
}
return new Promise((resolve, reject) => {
if (inner.timeout && typeof inner.timeout === 'number' && inner.timeout > 0) {
Object.assign(request, { timeout: inner.timeout })
}
const req = h.request(request, async res => {
res = getResponse(res)
res.on('error', reject)
decodings(res)
res.status = res.statusCode
if (!statusCodes.has(res.statusCode)) {
return reject(new StatusError(res))
}

if (!encoding) return resolve(res)
else {
if (!encoding) return resolve(res)
else {
/* istanbul ignore else */
if (encoding === 'buffer') {
resolve(res.arrayBuffer())
} else if (encoding === 'json') {
resolve(res.json())
} else if (encoding === 'string') {
resolve(res.text())
if (encoding === 'buffer') {
resolve(res.arrayBuffer())
} else if (encoding === 'json') {
resolve(res.json())
} else if (encoding === 'string') {
resolve(res.text())
}
}
}
})
req.on('error', reject)
if (body) {
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
body = bytes.native(body)
}
if (Buffer.isBuffer(body)) {
})
req.on('error', reject)
if (body) {
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) {
body = bytes.native(body)
}
if (Buffer.isBuffer(body)) {
// noop
} else if (typeof body === 'string') {
body = Buffer.from(body)
} else if (isStream(body)) {
body.pipe(req)
body = null
} else if (typeof body === 'object') {
if (!c.has('content-type')) {
req.setHeader('content-type', 'application/json')
} else if (typeof body === 'string') {
body = Buffer.from(body)
} else if (isStream(body)) {
body.pipe(req)
body = null
} else if (typeof body === 'object') {
if (!c.has('content-type')) {
req.setHeader('content-type', 'application/json')
}
body = Buffer.from(JSON.stringify(body))
} else {
reject(new Error('Unknown body type.'))
}
if (body) {
req.setHeader('content-length', body.length)
req.end(body)
}
body = Buffer.from(JSON.stringify(body))
} else {
reject(new Error('Unknown body type.'))
req.end()
}
if (body) {
req.setHeader('content-length', body.length)
req.end(body)
}
} else {
req.end()
}
})
})
}
return inner
}

module.exports = bent(mkrequest)
1 change: 1 addition & 0 deletions test/test-basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ if (process.browser) {
})
})
const request = bent('POST')
request.timeout = 1000 * 20
const response = request('http://localhost:9999', { ok: true }, { 'content-type': 'application/jose+json' })
const info = await response
same(info.statusCode, 200)
Expand Down