Last active
September 8, 2017 16:37
-
-
Save ndmanvar/7ef3048b0570fc5e7f740b9d830bcb33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var events = require('events'); | |
var util = require('util'); | |
var timeoutReq = require('timed-out'); | |
var http = require('http'); | |
var https = require('https'); | |
function Transport() {} | |
util.inherits(Transport, events.EventEmitter); | |
function HTTPTransport(options) { | |
this.defaultPort = 80; | |
this.transport = http; | |
this.options = options || {}; | |
} | |
util.inherits(HTTPTransport, Transport); | |
HTTPTransport.prototype.send = function (client, message, headers, eventId, cb) { | |
console.log(client.dsn); | |
// HardCoded to only work with proxy on localhost:8080 | |
/* | |
TODO: | |
- https support (i.e. if sending to https endpoint, tls conection needs to happen. See: https://www.vanamco.com/2014/06/24/proxy-requests-in-node-js/) | |
- conditional to detect if http/https endpoint, then options should like ... and tls connection if https | |
- context config for proxy. (i.e. so we can specify proxy when configuring Raven) | |
*/ | |
// location of original transport.js: https://github.com/getsentry/raven-node/blob/master/lib/transports.js | |
var options = { | |
host: 'localhost', | |
port: '8080', | |
path: 'http://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/', | |
headers: headers, | |
method: 'POST', | |
ca: client.ca | |
}; | |
for (var key in this.options) { | |
if (this.options.hasOwnProperty(key)) { | |
options[key] = this.options[key]; | |
} | |
} | |
var req = this.transport.request(options, function (res) { | |
res.setEncoding('utf8'); | |
if (res.statusCode >= 200 && res.statusCode < 300) { | |
client.emit('logged', eventId); | |
cb && cb(null, eventId); | |
} else { | |
var reason = res.headers['x-sentry-error']; | |
var e = new Error('HTTP Error (' + res.statusCode + '): ' + reason); | |
e.response = res; | |
e.statusCode = res.statusCode; | |
e.reason = reason; | |
e.sendMessage = message; | |
e.requestHeaders = headers; | |
e.eventId = eventId; | |
client.emit('error', e); | |
cb && cb(e); | |
} | |
// force the socket to drain | |
var noop = function () {}; | |
res.on('data', noop); | |
res.on('end', noop); | |
}); | |
timeoutReq(req, client.sendTimeout * 1000); | |
var cbFired = false; | |
req.on('error', function (e) { | |
client.emit('error', e); | |
if (!cbFired) { | |
cb && cb(e); | |
cbFired = true; | |
} | |
}); | |
req.end(message); | |
}; | |
function HTTPSTransport(options) { | |
this.defaultPort = 443; | |
this.transport = https; | |
this.options = options || {}; | |
} | |
util.inherits(HTTPSTransport, HTTPTransport); | |
module.exports.http = new HTTPTransport(); | |
module.exports.https = new HTTPSTransport(); | |
module.exports.Transport = Transport; | |
module.exports.HTTPTransport = HTTPTransport; | |
module.exports.HTTPSTransport = HTTPSTransport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment