Skip to content

Commit c6e0050

Browse files
committed
Undo use strict so as to allow arguments.callee to determine origin paths
1 parent 890442b commit c6e0050

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/*

lib/XMLHttpRequest.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*jslint node:true, vars:true, todo:true, stupid:true, regexp:true*/
1+
/*jslint node:true, vars:true, todo:true, stupid:true, regexp:true, sloppy: true*/
22
/**
33
* Wrapper for built-in http.js to emulate the browser XMLHttpRequest object.
44
*
@@ -12,12 +12,14 @@
1212
* @license MIT
1313
*/
1414

15+
require('array.prototype.find');
16+
require('string.prototype.includes');
17+
1518
var Url = require("url")
1619
, spawn = require("child_process").spawn
1720
, fs = require('fs');
1821

1922
exports.XMLHttpRequest = function() {
20-
'use strict';
2123
/**
2224
* Private variables
2325
*/
@@ -293,6 +295,17 @@ exports.XMLHttpRequest = function() {
293295
var ssl = false, local = false;
294296
var url = Url.parse(settings.url);
295297
var host;
298+
299+
function getStack () {
300+
var orig = Error.prepareStackTrace;
301+
Error.prepareStackTrace = function(_, stack){ return stack; };
302+
var err = new Error();
303+
Error.captureStackTrace(err, arguments.callee);
304+
var stack = err.stack;
305+
Error.prepareStackTrace = orig;
306+
return stack;
307+
}
308+
296309
// Determine the server
297310
switch (url.protocol) {
298311
case 'https:':
@@ -310,7 +323,23 @@ exports.XMLHttpRequest = function() {
310323
case undefined:
311324
case null:
312325
case '':
313-
host = "localhost";
326+
var stack = getStack();
327+
var path = require('path');
328+
var basePath = path.dirname(stack.reverse().find(function (item) {
329+
var filename = item.getFileName();
330+
var idx = filename.search(/[\/\\]node_modules[\/\\]/);
331+
if (idx === -1) { // Should be a user file, as a node executable like nodeunit ought to have node_modules in the path
332+
return true;
333+
}
334+
// Should be a user file because its last "node_modules" contains this XMLHttpRequest file (i.e., XMLHttpRequest is a dependency of some kind)
335+
if (__dirname.includes(filename.slice(0, idx))) {
336+
return true;
337+
}
338+
return false;
339+
}).getFileName());
340+
var pathName = path.resolve(basePath, settings.url);
341+
url = {pathname: pathName};
342+
local = true;
314343
break;
315344

316345
default:

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
, "engines": {
2020
"node": ">=0.4.0"
2121
}
22+
, "dependencies": {
23+
"array.prototype.find": "1.x"
24+
, "string.prototype.includes": "1.x"
25+
}
2226
, "directories": {
2327
"lib": "./lib"
2428
, "example": "./example"

0 commit comments

Comments
 (0)