@segment/analytics.js-integration-segmentio
Advanced tools
Comparing version 3.6.1 to 3.6.2
@@ -0,1 +1,7 @@ | ||
3.6.2 / 2018-17-04 | ||
================== | ||
* Add timeout for requests that will be retried. | ||
3.6.1 / 2018-15-04 | ||
@@ -6,3 +12,2 @@ ================== | ||
3.6.0 / 2017-11-01 | ||
@@ -9,0 +14,0 @@ ================== |
/* eslint-env node */ | ||
/* eslint-disable no-restricted-globals */ | ||
'use strict'; | ||
@@ -12,2 +13,37 @@ | ||
middleware: ['server'], | ||
plugins: [ | ||
'karma-*', | ||
{ | ||
'middleware:server': [ | ||
'factory', | ||
function() { | ||
return function(request, response, next) { | ||
if (request.url === '/base/data' && request.method === 'POST') { | ||
var body = ''; | ||
request.on('data', function(data) { | ||
body += data; | ||
}); | ||
request.on('end', function() { | ||
try { | ||
var data = JSON.parse(body); | ||
response.writeHead(data.length === 3 ? 200 : 400); | ||
return response.end(String(data.length === 3)); | ||
} catch (err) { | ||
response.writeHead(500); | ||
return response.end(); | ||
} | ||
}); | ||
} else { | ||
next(); | ||
} | ||
}; | ||
} | ||
] | ||
} | ||
], | ||
frameworks: ['browserify', 'mocha'], | ||
@@ -14,0 +50,0 @@ |
@@ -78,3 +78,48 @@ 'use strict'; | ||
/** | ||
* Send the given `obj` and `headers` to `url` with the specified `timeout` and | ||
* `fn(err, req)`. Exported for testing. | ||
* | ||
* @param {String} url | ||
* @param {Object} obj | ||
* @param {Object} headers | ||
* @param {long} timeout | ||
* @param {Function} fn | ||
* @api private | ||
*/ | ||
exports.sendJsonWithTimeout = function(url, obj, headers, timeout, fn) { | ||
// only proceed with our new code path when cors is supported. this is | ||
// unlikely to happen in production, but we're being safe to preserve backward | ||
// compatibility. | ||
if (send.type !== 'xhr') { | ||
send(url, obj, headers, fn); | ||
return; | ||
} | ||
var req = new XMLHttpRequest(); | ||
req.onerror = fn; | ||
req.onreadystatechange = done; | ||
req.open('POST', url, true); | ||
req.timeout = timeout; | ||
req.ontimeout = fn; | ||
// TODO: Remove this eslint disable | ||
// eslint-disable-next-line guard-for-in | ||
for (var k in headers) { | ||
req.setRequestHeader(k, headers[k]); | ||
} | ||
req.send(json.stringify(obj)); | ||
function done() { | ||
if (req.readyState === 4) { | ||
return fn(null, req); | ||
} | ||
} | ||
}; | ||
/** | ||
* Initialize. | ||
@@ -95,4 +140,5 @@ * | ||
item.msg.sentAt = new Date(); | ||
// send | ||
send(item.url, item.msg, item.headers, function(err, res) { | ||
// send with 10s timeout | ||
Segment.sendJsonWithTimeout(item.url, item.msg, item.headers, 10 * 1000, function(err, res) { | ||
self.debug('sent %O, received %O', item.msg, [err, res]); | ||
@@ -99,0 +145,0 @@ if (err) return done(err); |
{ | ||
"name": "@segment/analytics.js-integration-segmentio", | ||
"description": "The Segmentio analytics.js integration.", | ||
"version": "3.6.1", | ||
"version": "3.6.2", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "analytics.js", |
@@ -15,2 +15,3 @@ 'use strict'; | ||
var sinon = require('sinon'); | ||
var send = require('@segment/send-json'); | ||
@@ -329,3 +330,3 @@ // FIXME(ndhoule): clear-env's AJAX request clearing interferes with PhantomJS 2 | ||
}); | ||
}); | ||
}); | ||
@@ -495,8 +496,14 @@ describe('unbundling', function() { | ||
describe('#enqueue', function() { | ||
var xhr; | ||
beforeEach(function() { | ||
analytics.spy(segment, 'session'); | ||
xhr = sinon.useFakeXMLHttpRequest(); | ||
}); | ||
afterEach(function() { | ||
if (xhr.restore) xhr.restore(); | ||
}); | ||
it('should use https: protocol when http:', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -514,3 +521,2 @@ xhr.onCreate = spy; | ||
it('should use https: protocol when https:', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -528,3 +534,2 @@ xhr.onCreate = spy; | ||
it('should use https: protocol when https:', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -542,3 +547,2 @@ xhr.onCreate = spy; | ||
it('should use https: protocol when chrome-extension:', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -556,3 +560,2 @@ xhr.onCreate = spy; | ||
it('should enqueue to `api.segment.io/v1` by default', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -572,3 +575,2 @@ xhr.onCreate = spy; | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -586,3 +588,2 @@ xhr.onCreate = spy; | ||
it('should enqueue a normalized payload', sinon.test(function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -951,3 +952,6 @@ xhr.onCreate = spy; | ||
describe('localStorage queueing', function() { | ||
var xhr; | ||
beforeEach(function(done) { | ||
xhr = sinon.useFakeXMLHttpRequest(); | ||
if (window.localStorage) { | ||
@@ -963,2 +967,3 @@ window.localStorage.clear(); | ||
segment._lsqueue.stop(); | ||
xhr.restore(); | ||
}); | ||
@@ -973,3 +978,2 @@ | ||
it('should send requests', function() { | ||
var xhr = sinon.useFakeXMLHttpRequest(); | ||
var spy = sinon.spy(); | ||
@@ -986,2 +990,32 @@ xhr.onCreate = spy; | ||
}); | ||
describe('sendJsonWithTimeout', function() { | ||
var protocol = location.protocol; | ||
var hostname = location.hostname; | ||
var port = location.port; | ||
var endpoint = '/base/data'; | ||
var url = protocol + '//' + hostname + ':' + port + endpoint; | ||
var headers = { 'Content-Type': 'application/json' }; | ||
it('should work', function(done) { | ||
if (send.type !== 'xhr') return done(); | ||
Segment.sendJsonWithTimeout(url, [1, 2, 3], headers, 10 * 1000, function(err, req) { | ||
if (err) return done(new Error(err.message)); | ||
var res = JSON.parse(req.responseText); | ||
assert(res === true); | ||
done(); | ||
}); | ||
}); | ||
it('should timeout', function(done) { | ||
if (send.type !== 'xhr') return done(); | ||
Segment.sendJsonWithTimeout(url, [1, 2, 3], headers, 1, function(err) { | ||
assert(err.type === 'timeout'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
64533
1516