Files
NodeBB/test/mocks/newXhr.js

51 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-02-18 02:23:47 -07:00
'use strict';
// see https://gist.github.com/jfromaniello/4087861#gistcomment-1447029
// XMLHttpRequest to override.
2016-10-26 12:06:53 +03:00
2016-10-16 22:52:39 +03:00
var npm2Path = '../../node_modules/socket.io-client/node_modules/engine.io-client/node_modules/xmlhttprequest-ssl';
var npm3Path = '../../node_modules/xmlhttprequest-ssl';
var filePath;
var winston = require('winston');
// Make initial call to require so module is cached.
2016-10-16 17:36:24 +03:00
try {
2016-10-16 22:52:39 +03:00
require(npm2Path);
filePath = require.resolve(npm2Path);
2016-10-16 22:19:30 +03:00
} catch (err) {
2016-10-16 22:52:39 +03:00
if (err) {
winston.info('Couldn\'t find ' + npm2Path);
}
2016-10-16 22:19:30 +03:00
try {
2016-10-16 22:52:39 +03:00
require(npm3Path);
filePath = require.resolve(npm3Path);
2016-10-16 22:19:30 +03:00
} catch (err) {
2016-10-16 22:52:39 +03:00
if (err) {
winston.info('Couldn\'t find ' + npm3Path);
}
2016-10-16 22:19:30 +03:00
}
2016-10-16 17:51:35 +03:00
}
2016-10-16 22:52:39 +03:00
winston.info('xmlhttprequest-ssl path: ' + filePath);
// Get cached version.
2016-10-16 22:52:39 +03:00
var cachedXhr = require.cache[filePath];
var stdXhr = cachedXhr.exports;
// Callbacks exposes an object that callback functions can be added to.
var callbacks = {};
var newXhr = function () {
stdXhr.apply(this, arguments);
2016-10-16 17:51:35 +03:00
for (var method in callbacks) {
if (typeof callbacks[method] === 'function') {
callbacks[method].apply(this, arguments);
}
}
2016-10-16 22:52:39 +03:00
};
newXhr.XMLHttpRequest = newXhr;
cachedXhr.exports = newXhr;
module.exports = newXhr;
2017-02-18 02:30:48 -07:00
module.exports.callbacks = callbacks;