Skip to content

Commit

Permalink
sync read file & require of json files
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsofapollo committed Aug 20, 2012
1 parent a7d7393 commit 7ec9c31
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 52 deletions.
1 change: 0 additions & 1 deletion lib/modules/env.js
Expand Up @@ -8,5 +8,4 @@ if (typeof(XPCOMUtils) === 'undefined') {
XPCOMUtils.defineLazyServiceGetter(Service, 'env',
'@mozilla.org/process/environment;1',
'nsIEnvironment');

module.exports = Service.env;
16 changes: 16 additions & 0 deletions lib/modules/fs.js
@@ -0,0 +1,16 @@
var fsPath = window.xpcModule.require('path');

exports.readFileSync = function readFileSync(name) {
var xhr = new XMLHttpRequest();
var path = fsPath.resolve(name);

xhr = new XMLHttpRequest();
xhr.open('GET', 'file://' + path, false);

xhr.send(null);
return xhr.responseText;
};

exports.writeFileSync = function writeFileSync() {

};
98 changes: 62 additions & 36 deletions lib/window/xpc-module.js
Expand Up @@ -13,6 +13,7 @@ window.xpcModule = (function() {
var cache = require.cache = {};

var builtIn = {
'fs': _ROOT + '/lib/modules/fs.js',
'process': _ROOT + '/lib/modules/process.js',
'events': _ROOT + '/lib/modules/events.js',
'env': _ROOT + '/lib/modules/env.js',
Expand All @@ -21,8 +22,6 @@ window.xpcModule = (function() {
'mocha-formatting': _ROOT + '/lib/modules/mocha-formatting.js'
};

var fsPath = require('path');

function lazyDefine(obj, prop, fn) {
Object.defineProperty(obj, prop, {
configurable: true,
Expand All @@ -33,53 +32,80 @@ window.xpcModule = (function() {
});
}

function require(path) {
if (path in builtIn) {
return require(builtIn[path]);
}
require.handlers = {
js: function requireJS(path) {

if (path.substr(-3) !== '.js') {
path += '.js';
}
if (path.substr(0, 1) !== '/') {
path = fsPath.resolve(root, path);
}

if (path.substr(0, 1) !== '/') {
path = fsPath.resolve(root, path);
}
if (path in cache) {
return cache[path];
}

// module object
var instance = {
exports: {}
};

var scope = Object.create(window);
scope.module = instance;
scope.exports = instance.exports;

lazyDefine(scope, 'process', function() {
return require('process');
});

if (path in cache) {
return cache[path];
// Work around race condition we can't use
// fsPath while loading it :)
if (fsPath) {
scope.__dirname = fsPath.resolve(path, '../');
scope.__filename = path;
}

try {
mozIJSSubScriptLoader.loadSubScript(
'file://' + path,
scope
);
} catch (e) {
throw new Error(
"Cannot load module at: '" + path + "'\n\n: " +
e.toString()
);
}

return cache[path] = instance.exports;
},

json: function requireJSON(path) {
var path = fsPath.resolve(path);
var contents = require('fs').readFileSync(path);
return JSON.parse(contents);
}
};

// module object
var instance = {
exports: {}
};
var fsPath = require('path');

var scope = Object.create(window);
scope.module = instance;
scope.exports = instance.exports;
function require(path) {

lazyDefine(scope, 'process', function() {
return require('process');
});
if (path in builtIn) {
return require(builtIn[path]);
}

// Work around race condition we can't use
// fsPath while loading it :)
var ext;
if (fsPath) {
scope.__dirname = fsPath.resolve(path, '../');
scope.__filename = path;
ext = fsPath.extname(path).slice(1);
} else {
ext = 'js';
}

try {
mozIJSSubScriptLoader.loadSubScript(
'file://' + path,
scope
);
} catch (e) {
throw new Error("Cannot load module at: '" + path + "'");
if (!ext) {
path += '.js';
ext = 'js';
}

return cache[path] = scope.module.exports;
return require.handlers[ext](path);
}

var module = {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/file.json
@@ -0,0 +1,3 @@
{
"json": true
}
17 changes: 17 additions & 0 deletions test/modules/fs-test.js
@@ -0,0 +1,17 @@
describe('modules/fs', function() {
var fs = window.xpcModule.require('fs');

describe('read', function() {
var fileName = __dirname + '/../fixtures/file.js';
var expected = 'local file';

describe('readFileSync', function() {
it('should be able to read files', function() {
var result = fs.readFileSync(fileName);
expect(result).to.contain('local file');
});
});

});

});
46 changes: 31 additions & 15 deletions test/xpc-module-test.js
Expand Up @@ -93,26 +93,42 @@ describe('window.xpcModule', function() {
describe('#require', function() {
var fsPath = require('path');

beforeEach(function() {
subject = require('./fixtures/test-mod.js');
});

it('should define __dirname', function() {
var root = fsPath.resolve('./fixtures/');
expect(subject.__dirname).to.be(root);
expect(subject.__filename).to.contain('fixtures/test-mod.js');
});
describe('.json', function() {
beforeEach(function() {
subject = require('./fixtures/file.json');
});

it('should not polute global scope', function() {
expect(window.globalContextDirty).not.to.be.ok();
});
it('should load & parse json file', function() {
expect(subject).to.eql({
json: true
});
});

it('should load module', function() {
expect(subject).to.be.ok();
});

it('should be the same object when required twice', function() {
expect(subject).to.be(require('./fixtures/test-mod.js'));
describe('.js', function() {
beforeEach(function() {
subject = require('./fixtures/test-mod.js');
});

it('should define __dirname', function() {
var root = fsPath.resolve('./fixtures/');
expect(subject.__dirname).to.be(root);
expect(subject.__filename).to.contain('fixtures/test-mod.js');
});

it('should not polute global scope', function() {
expect(window.globalContextDirty).not.to.be.ok();
});

it('should load module', function() {
expect(subject).to.be.ok();
});

it('should be the same object when required twice', function() {
expect(subject).to.be(require('./fixtures/test-mod.js'));
});
});

});
Expand Down

0 comments on commit 7ec9c31

Please sign in to comment.