Browse files

vfs-local: Modify extend interface to pass in vfs instance and allow …

…async registration
  • Loading branch information...
1 parent 3a90332 commit 6b04c9579684c3f6cb66fb5b0c6f661c0610c170 @creationix creationix committed Jul 10, 2012
Showing with 25 additions and 12 deletions.
  1. +9 −6 example/extension.js
  2. +15 −5 local/localfs.js
  3. +1 −1 local/package.json
View
15 example/extension.js
@@ -1,7 +1,10 @@
-exports.add = function (a, b, callback) {
- callback(null, a + b);
-};
-
-exports.multiply = function (a, b, callback) {
- callback(null, a * b);
+module.exports = function (vfs, register) {
+ register(null, {
+ add: function (a, b, callback) {
+ callback(null, a + b);
+ },
+ multiply: function (a, b, callback) {
+ callback(null, a * b);
+ }
+ });
};
View
20 local/localfs.js
@@ -126,7 +126,7 @@ module.exports = function setup(fsOptions) {
var apis = {};
var handlers = {};
- return {
+ var vfs = {
// Process Management
spawn: spawn,
exec: exec,
@@ -159,6 +159,8 @@ module.exports = function setup(fsOptions) {
emit: emit
};
+ return vfs;
+
function on(name, handler, callback) {
if (!handlers[name]) handlers[name] = [];
handlers[name].push(handler);
@@ -414,25 +416,33 @@ module.exports = function setup(fsOptions) {
// The user can pass in a path to a file to require
if (options.file) {
- functions = require(options.file);
+ require(options.file)(vfs, onEvaluate);
}
// User can pass in code as a pre-buffered string
else if (options.code) {
- functions = evaluate(options.code);
+ evaluate(options.code)(vfs, onEvaluate);
}
// Or we'll give them a writable stream to pipe it to.
else {
var stream = meta.stream = new MemStream();
stream.on("done", function (code) {
- functions = evaluate(code);
- api.emit("ready");
+ evaluate(code)(vfs, onEvaluate);
});
}
return callback(null, meta);
+ function onEvaluate(err, exports) {
+ if (err) {
+ api.emit("error", err);
+ return;
+ }
+ functions = exports;
+ api.emit("ready");
+ }
+
}
function killtree(child, signal){
View
2 local/package.json
@@ -6,7 +6,7 @@
],
"name": "vfs-local",
"description": "A vfs implementation that works on the local filesystem, but with optional security constraints",
- "version": "0.1.7",
+ "version": "0.1.8",
"repository": {
"type": "git",
"url": "git://github.com/c9/vfs.git"

0 comments on commit 6b04c95

Please sign in to comment.