Skip to content
This repository has been archived by the owner on Apr 6, 2021. It is now read-only.

Client + new functions #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 102 additions & 18 deletions nodefs.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@

const PATH = require("path");
const Stream = require("stream").Stream;

module.exports = function(vfs, base) {

var resolvePath = base
? function(path) {
if (path.substring(0, base.length) === base) {
return path;
}
return PATH.join(base, path);
}
: function(path) { return path; };
( // Module boilerplate to support browser globals, node.js and AMD.
(typeof module !== "undefined" && function (m) { module.exports = m(require('stream'), require('events'), require('smith')); }) ||
(typeof define === "function" && function (m) { define(["smith", "lib/vfs-socket/stream-amd"], m); }) ||
(function (m) { window.consumer = m(window.stream, window.events, window.smith); })
)(function (stream, events, smith) {

const PATH = {
join : function(){
return Array.prototype.join.apply(arguments, ["/"]).replace(/\/\/+/, "/");
}
};

if (typeof module == "undefined")
var Stream = require("lib/vfs-socket/stream-amd").Stream;
else
var Stream = require("stream").Stream;

var exports = function(vfs, base, baseProc) {

var resolvePath = function(path, basePath) {
if (!basePath)
basePath = base;

if (!basePath) return path;

if (path.substring(0, basePath.length) === basePath) {
return path;
}
return PATH.join(basePath, path);
}

function readFile(path, encoding, callback) {
if (!callback) {
Expand Down Expand Up @@ -79,7 +95,7 @@ module.exports = function(vfs, base) {
var files = [];

stream.on("data", function(stat) {
files.push(stat.name);
files.push(stat);
});

var called;
Expand All @@ -99,7 +115,7 @@ module.exports = function(vfs, base) {

function exists(path, callback) {
vfs.stat(resolvePath(path), {}, function(err, stat) {
return callback(stat && !stat.err);
return callback(stat && !stat.err ? true : false, stat);
});
}

Expand All @@ -111,25 +127,85 @@ module.exports = function(vfs, base) {
vfs.rename(resolvePath(to), {from: resolvePath(from)}, callback);
}

function mkdirHandler(callback){
return function(err){
if (err && err.message.indexOf("exists") > -1)
callback({"code": "EEXIST", "message": err.message});
else
callback();
}
}

function mkdirP(path, mode, callback) {
if (!callback) {
callback = mode;
mode = null;
}
vfs.execFile("mkdir", {args: ["-p", resolvePath(path)]}, callback);
vfs.execFile("mkdir", {args: ["-p", resolvePath(path, baseProc)]},
mkdirHandler(callback));
}

function mkdir(path, callback) {
vfs.execFile("mkdir", {args: [resolvePath(path)]}, callback);
vfs.execFile("mkdir", {args: [resolvePath(path, baseProc)]},
mkdirHandler(callback));
}

function rmfile(path, callback) {
vfs.rmfile(resolvePath(path), {}, callback || function(){}); // shouldn't vfs handle callback == null?
}

function rmdir(path, options, callback) {
if (typeof options == "function") {
callback = options;
options = {};
}
vfs.rmdir(resolvePath(path), options, callback || function(){});
}

function copy(path, to, options, callback){
if (typeof options == "function") {
callback = options;
options = {};
}

vfs.copy(resolvePath(path), {
to : resolvePath(to),
overwrite : (options.overwrite !== undefined
? options.overwrite
: true),
recursive : options.recursive
}, callback);
}

function symlink(path, target, callback){
vfs.symlink(resolvePath(path), {target: resolvePath(target)}, callback);
}

function watch(path, callback) {
vfs.watch(resolvePath(path), {}, function (err, meta) {
if (err) return callback(err);

var watcher = meta.watcher;
watcher.on("change", function (event, filename) {
callback(null, event, filename);
});
watcher.on("error", function(err){
callback(err || true);
})

if (callback[path]) callback[path]();
callback[path] = function(){
watcher.removeAllListeners();
watcher.close();
};

callback(null, "init", path);
});
}

function unwatch(path, callback) {
if (callback[path]) callback[path]();
}

return {
readFile: readFile,
Expand All @@ -143,6 +219,14 @@ module.exports = function(vfs, base) {
unlink: rmfile,
rmfile: rmfile,
rmdir: rmdir,
copy: copy,
symlink: symlink,
watch: watch,
unwatch: unwatch,
vfs: vfs
}
}

return exports;

});