Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #55 from bugsnag/http-agent
Browse files Browse the repository at this point in the history
Add agent option
  • Loading branch information
bengourley authored Mar 31, 2020
2 parents 4618ca6 + fc7d9aa commit 161f522
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: node_js

node_js:
- "4"
- "6"
- "8"

sudo: false
- "10"
- "12"

script:
- npm test
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import path from 'path';
import { upload } from 'bugsnag-sourcemaps';

upload({
agent, // optional (node http agent)
apiKey: 'YOUR_API_KEY_HERE',
appVersion: '1.2.3', // optional
codeBundleId: '1.0-123', // optional (react-native only)
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const noopLogger = { debug: () => {}, info: () => {}, warn: () => {}, error: ()
* @returns {Promise<string>}
*/
function sendRequest (options) {
return new Promise((resolve, reject) => request(options.endpoint, () => prepareRequest(options), () => resolve(options), reject))
return new Promise((resolve, reject) => request(options.endpoint, () => prepareRequest(options), () => resolve(options), reject, options))
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/prepare-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module.exports = function prepareRequest (options) {
case 'projectRoot':
case 'stripProjectRoot':
case 'addWildcardPrefix':
case 'tempDir': {
case 'tempDir':
case 'agent': {
break
}
case 'overwrite': {
Expand Down
9 changes: 5 additions & 4 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ const MAX_ATTEMPTS = 5
const RETRY_INTERVAL = parseInt(process.env.BUGSNAG_RETRY_INTERVAL) || 1000
const TIMEOUT = parseInt(process.env.BUGSNAG_TIMEOUT) || 30000

module.exports = (endpoint, makePayload, onSuccess, onError) => {
module.exports = (endpoint, makePayload, onSuccess, onError, opts) => {
let attempts = 0
const maybeRetry = (err) => {
attempts++
if (err && err.isRetryable !== false && attempts < MAX_ATTEMPTS) return setTimeout(go, RETRY_INTERVAL)
return onError(err)
}
const go = () => send(endpoint, makePayload(), onSuccess, maybeRetry)
const go = () => send(endpoint, makePayload(), onSuccess, maybeRetry, opts)
go()
}

const send = (endpoint, data, onSuccess, onError) => {
const send = (endpoint, data, onSuccess, onError, opts) => {
onError = once(onError)
const formData = new FormData()
Object.keys(data).forEach(k => formData.append(k, data[k]))
Expand All @@ -32,7 +32,8 @@ const send = (endpoint, data, onSuccess, onError) => {
hostname: parsedUrl.hostname,
path: parsedUrl.path || '/',
headers: formData.getHeaders(),
port: parsedUrl.port || undefined
port: parsedUrl.port || undefined,
agent: opts && opts.agent
}, res => {
res.pipe(concat(body => {
if (res.statusCode === 200) return onSuccess()
Expand Down
8 changes: 4 additions & 4 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ afterEach(() => jest.resetModules())
test('single uploads', () => {
let mockCalled = 0
jest.mock('../lib/request', () => {
return (endpoint, makePayload, onSuccess, onError) => {
return (endpoint, makePayload, onSuccess, onError, opts) => {
mockCalled++
onSuccess()
}
Expand All @@ -28,7 +28,7 @@ test('multiple uploads', () => {
let mockCalled = 0
let mockCalledWith = []
jest.mock('../lib/request', () => {
return (endpoint, makePayload, onSuccess, onError) => {
return (endpoint, makePayload, onSuccess, onError, opts) => {
mockCalled++
mockCalledWith.push(makePayload())
onSuccess()
Expand Down Expand Up @@ -81,7 +81,7 @@ test('multiple uploads (resolving relative source paths inside map)', () => {
let mockCalledWith = []
const mockConcat = concat
jest.mock('../lib/request', () => {
return (endpoint, makePayload, onSuccess, onError) => {
return (endpoint, makePayload, onSuccess, onError, opts) => {
mockCalled++
const payload = makePayload()
payload.sourceMap.pipe(mockConcat(data => {
Expand Down Expand Up @@ -153,7 +153,7 @@ test('webpack paths', () => {
let mockCalledWith = []
const mockConcat = concat
jest.mock('../lib/request', () => {
return (endpoint, makePayload, onSuccess, onError) => {
return (endpoint, makePayload, onSuccess, onError, opts) => {
mockCalled++
const payload = makePayload()
payload.sourceMap.pipe(mockConcat(data => {
Expand Down

0 comments on commit 161f522

Please sign in to comment.