mirror of
https://github.com/abrendan/MicDropMessages.git
synced 2025-08-25 14:02:03 +02:00
Initial commit
This commit is contained in:
92
node_modules/walk/lib/node-type-emitter.js
generated
vendored
Normal file
92
node_modules/walk/lib/node-type-emitter.js
generated
vendored
Normal file
@@ -0,0 +1,92 @@
|
||||
/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true*/
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
// "FIFO" isn't easy to convert to camelCase and back reliably
|
||||
var isFnodeTypes = [
|
||||
"isFile", "isDirectory", "isSymbolicLink", "isBlockDevice", "isCharacterDevice", "isFIFO", "isSocket"
|
||||
],
|
||||
fnodeTypes = [
|
||||
"file", "directory", "symbolicLink", "blockDevice", "characterDevice", "FIFO", "socket"
|
||||
],
|
||||
fnodeTypesPlural = [
|
||||
"files", "directories", "symbolicLinks", "blockDevices", "characterDevices", "FIFOs", "sockets"
|
||||
];
|
||||
|
||||
|
||||
//
|
||||
function createNodeGroups() {
|
||||
var nodeGroups = {};
|
||||
fnodeTypesPlural.concat("nodes", "errors").forEach(function (fnodeTypePlural) {
|
||||
nodeGroups[fnodeTypePlural] = [];
|
||||
});
|
||||
return nodeGroups;
|
||||
}
|
||||
|
||||
|
||||
// Determine each file node's type
|
||||
//
|
||||
function sortFnodesByType(stat, fnodes) {
|
||||
var i, isType;
|
||||
|
||||
for (i = 0; i < isFnodeTypes.length; i += 1) {
|
||||
isType = isFnodeTypes[i];
|
||||
if (stat[isType]()) {
|
||||
stat.type = fnodeTypes[i];
|
||||
fnodes[fnodeTypesPlural[i]].push(stat);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get the current number of listeners (which may change)
|
||||
// Emit events to each listener
|
||||
// Wait for all listeners to `next()` before continueing
|
||||
// (in theory this may avoid disk thrashing)
|
||||
function emitSingleEvents(emitter, path, stats, next, self) {
|
||||
var num = 1 + emitter.listeners(stats.type).length + emitter.listeners("node").length;
|
||||
|
||||
function nextWhenReady(flag) {
|
||||
if (flag) {
|
||||
stats.flag = flag;
|
||||
}
|
||||
num -= 1;
|
||||
if (0 === num) { next.call(self); }
|
||||
}
|
||||
|
||||
emitter.emit(stats.type, path, stats, nextWhenReady);
|
||||
emitter.emit("node", path, stats, nextWhenReady);
|
||||
nextWhenReady();
|
||||
}
|
||||
|
||||
|
||||
// Since the risk for disk thrashing among anything
|
||||
// other than files is relatively low, all types are
|
||||
// emitted at once, but all must complete before advancing
|
||||
function emitPluralEvents(emitter, path, nodes, next, self) {
|
||||
var num = 1;
|
||||
|
||||
function nextWhenReady() {
|
||||
num -= 1;
|
||||
if (0 === num) { next.call(self); }
|
||||
}
|
||||
|
||||
fnodeTypesPlural.concat(["nodes", "errors"]).forEach(function (fnodeType) {
|
||||
if (0 === nodes[fnodeType].length) { return; }
|
||||
num += emitter.listeners(fnodeType).length;
|
||||
emitter.emit(fnodeType, path, nodes[fnodeType], nextWhenReady);
|
||||
});
|
||||
nextWhenReady();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
emitNodeType: emitSingleEvents,
|
||||
emitNodeTypeGroups: emitPluralEvents,
|
||||
isFnodeTypes: isFnodeTypes,
|
||||
fnodeTypes: fnodeTypes,
|
||||
fnodeTypesPlural: fnodeTypesPlural,
|
||||
sortFnodesByType: sortFnodesByType,
|
||||
createNodeGroups: createNodeGroups
|
||||
};
|
||||
}());
|
93
node_modules/walk/lib/walk-async-only.js
generated
vendored
Normal file
93
node_modules/walk/lib/walk-async-only.js
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
(function () {
|
||||
"use strict"
|
||||
|
||||
// Array.prototype.forEachAsync(next, item, i, collection)
|
||||
require('futures/forEachAsync');
|
||||
|
||||
function noop() {}
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
TypeEmitter = require('./node-type-emitter');
|
||||
|
||||
// 2010-11-25 jorge@jorgechamorro.com
|
||||
function create(pathname, cb) {
|
||||
var emitter = new EventEmitter(),
|
||||
q = [],
|
||||
queue = [q],
|
||||
curpath;
|
||||
|
||||
function walk() {
|
||||
fs.readdir(curpath, function(err, files) {
|
||||
if (err) {
|
||||
emitter.emit('directoryError', curpath, { error: err }, noop);
|
||||
//emitter.emit('error', curpath, { error: err });
|
||||
}
|
||||
// XXX bug was here. next() was omitted
|
||||
if (!files || 0 == files.length) {
|
||||
return next();
|
||||
}
|
||||
|
||||
var fnodeGroups = TypeEmitter.createNodeGroups();
|
||||
|
||||
// TODO could allow user to selectively stat
|
||||
// and don't stat if there are no stat listeners
|
||||
emitter.emit('names', curpath, files, noop);
|
||||
files.forEachAsync(function (cont, file) {
|
||||
emitter.emit('name', curpath, file, noop);
|
||||
fs.lstat(curpath + path.sep + file, function (err, stat) {
|
||||
stat = stat || {};
|
||||
stat.name = file;
|
||||
if (err) {
|
||||
stat.error = err;
|
||||
//emitter.emit('error', curpath, stat);
|
||||
emitter.emit('nodeError', curpath, stat, noop);
|
||||
fnodeGroups.errors.push(stat);
|
||||
cont();
|
||||
} else {
|
||||
TypeEmitter.sortFnodesByType(stat, fnodeGroups);
|
||||
TypeEmitter.emitNodeType(emitter, curpath, stat, cont);
|
||||
}
|
||||
});
|
||||
}).then(function () {
|
||||
if (fnodeGroups.errors.length) {
|
||||
emitter.emit('errors', curpath, fnodeGroups.errors, noop);
|
||||
}
|
||||
TypeEmitter.emitNodeTypeGroups(emitter, curpath, fnodeGroups, function () {
|
||||
var dirs = [];
|
||||
fnodeGroups.directories.forEach(function (stat) {
|
||||
dirs.push(stat.name);
|
||||
});
|
||||
dirs.forEach(fullPath);
|
||||
queue.push(q = dirs);
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function next() {
|
||||
if (q.length) {
|
||||
curpath = q.pop();
|
||||
return walk();
|
||||
}
|
||||
if (queue.length -= 1) {
|
||||
q = queue[queue.length-1];
|
||||
return next();
|
||||
}
|
||||
emitter.emit('end');
|
||||
}
|
||||
|
||||
function fullPath(v,i,o) {
|
||||
o[i]= [curpath, path.sep, v].join('');
|
||||
}
|
||||
|
||||
curpath = pathname;
|
||||
walk();
|
||||
|
||||
return emitter;
|
||||
}
|
||||
|
||||
module.exports = create;
|
||||
}());
|
302
node_modules/walk/lib/walk.js
generated
vendored
Normal file
302
node_modules/walk/lib/walk.js
generated
vendored
Normal file
@@ -0,0 +1,302 @@
|
||||
// Adapted from work by jorge@jorgechamorro.com on 2010-11-25
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
function noop() {}
|
||||
|
||||
var fs = require('fs')
|
||||
, forEachAsync = require('foreachasync').forEachAsync
|
||||
, EventEmitter = require('events').EventEmitter
|
||||
, TypeEmitter = require('./node-type-emitter')
|
||||
, util = require('util')
|
||||
, path = require('path')
|
||||
;
|
||||
|
||||
function appendToDirs(stat) {
|
||||
/*jshint validthis:true*/
|
||||
if(stat.flag && stat.flag === NO_DESCEND) { return; }
|
||||
this.push(stat.name);
|
||||
}
|
||||
|
||||
function wFilesHandlerWrapper(items) {
|
||||
/*jshint validthis:true*/
|
||||
this._wFilesHandler(noop, items);
|
||||
}
|
||||
|
||||
function Walker(pathname, options, sync) {
|
||||
EventEmitter.call(this);
|
||||
|
||||
var me = this
|
||||
;
|
||||
|
||||
options = options || {};
|
||||
me._wStat = options.followLinks && 'stat' || 'lstat';
|
||||
me._wStatSync = me._wStat + 'Sync';
|
||||
me._wsync = sync;
|
||||
me._wq = [];
|
||||
me._wqueue = [me._wq];
|
||||
me._wcurpath = undefined;
|
||||
me._wfilters = options.filters || [];
|
||||
me._wfirstrun = true;
|
||||
me._wcurpath = pathname;
|
||||
|
||||
if (me._wsync) {
|
||||
//console.log('_walkSync');
|
||||
me._wWalk = me._wWalkSync;
|
||||
} else {
|
||||
//console.log('_walkASync');
|
||||
me._wWalk = me._wWalkAsync;
|
||||
}
|
||||
|
||||
options.listeners = options.listeners || {};
|
||||
Object.keys(options.listeners).forEach(function (event) {
|
||||
var callbacks = options.listeners[event]
|
||||
;
|
||||
|
||||
if ('function' === typeof callbacks) {
|
||||
callbacks = [callbacks];
|
||||
}
|
||||
|
||||
callbacks.forEach(function (callback) {
|
||||
me.on(event, callback);
|
||||
});
|
||||
});
|
||||
|
||||
me._wWalk();
|
||||
}
|
||||
|
||||
// Inherits must come before prototype additions
|
||||
util.inherits(Walker, EventEmitter);
|
||||
|
||||
Walker.prototype._wLstatHandler = function (err, stat) {
|
||||
var me = this
|
||||
;
|
||||
|
||||
stat = stat || {};
|
||||
stat.name = me._wcurfile;
|
||||
|
||||
if (err) {
|
||||
stat.error = err;
|
||||
//me.emit('error', curpath, stat);
|
||||
// TODO v3.0 (don't noop the next if there are listeners)
|
||||
me.emit('nodeError', me._wcurpath, stat, noop);
|
||||
me._wfnodegroups.errors.push(stat);
|
||||
me._wCurFileCallback();
|
||||
} else {
|
||||
TypeEmitter.sortFnodesByType(stat, me._wfnodegroups);
|
||||
// NOTE: wCurFileCallback doesn't need thisness, so this is okay
|
||||
TypeEmitter.emitNodeType(me, me._wcurpath, stat, me._wCurFileCallback, me);
|
||||
}
|
||||
};
|
||||
Walker.prototype._wFilesHandler = function (cont, file) {
|
||||
var statPath
|
||||
, me = this
|
||||
;
|
||||
|
||||
|
||||
me._wcurfile = file;
|
||||
me._wCurFileCallback = cont;
|
||||
me.emit('name', me._wcurpath, file, noop);
|
||||
|
||||
statPath = me._wcurpath + path.sep + file;
|
||||
|
||||
if (!me._wsync) {
|
||||
// TODO how to remove this anony?
|
||||
fs[me._wStat](statPath, function (err, stat) {
|
||||
me._wLstatHandler(err, stat);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
me._wLstatHandler(null, fs[me._wStatSync](statPath));
|
||||
} catch(e) {
|
||||
me._wLstatHandler(e);
|
||||
}
|
||||
};
|
||||
Walker.prototype._wOnEmitDone = function () {
|
||||
var me = this
|
||||
, dirs = []
|
||||
;
|
||||
|
||||
me._wfnodegroups.directories.forEach(appendToDirs, dirs);
|
||||
dirs.forEach(me._wJoinPath, me);
|
||||
me._wqueue.push(me._wq = dirs);
|
||||
me._wNext();
|
||||
};
|
||||
Walker.prototype._wPostFilesHandler = function () {
|
||||
var me = this
|
||||
;
|
||||
|
||||
if (me._wfnodegroups.errors.length) {
|
||||
// TODO v3.0 (don't noop the next)
|
||||
// .errors is an array of stats with { name: name, error: error }
|
||||
me.emit('errors', me._wcurpath, me._wfnodegroups.errors, noop);
|
||||
}
|
||||
// XXX emitNodeTypes still needs refactor
|
||||
TypeEmitter.emitNodeTypeGroups(me, me._wcurpath, me._wfnodegroups, me._wOnEmitDone, me);
|
||||
};
|
||||
Walker.prototype._wReadFiles = function () {
|
||||
var me = this
|
||||
;
|
||||
|
||||
if (!me._wcurfiles || 0 === me._wcurfiles.length) {
|
||||
return me._wNext();
|
||||
}
|
||||
|
||||
// TODO could allow user to selectively stat
|
||||
// and don't stat if there are no stat listeners
|
||||
me.emit('names', me._wcurpath, me._wcurfiles, noop);
|
||||
|
||||
if (me._wsync) {
|
||||
me._wcurfiles.forEach(wFilesHandlerWrapper, me);
|
||||
me._wPostFilesHandler();
|
||||
} else {
|
||||
forEachAsync(me._wcurfiles, me._wFilesHandler, me).then(me._wPostFilesHandler);
|
||||
}
|
||||
};
|
||||
Walker.prototype._wReaddirHandler = function (err, files) {
|
||||
var fnodeGroups = TypeEmitter.createNodeGroups()
|
||||
, me = this
|
||||
, parent
|
||||
, child
|
||||
;
|
||||
|
||||
me._wfnodegroups = fnodeGroups;
|
||||
me._wcurfiles = files;
|
||||
|
||||
// no error, great
|
||||
if (!err) {
|
||||
me._wReadFiles();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO path.sep
|
||||
me._wcurpath = me._wcurpath.replace(/\/$/, '');
|
||||
|
||||
// error? not first run? => directory error
|
||||
if (!me._wfirstrun) {
|
||||
// TODO v3.0 (don't noop the next if there are listeners)
|
||||
me.emit('directoryError', me._wcurpath, { error: err }, noop);
|
||||
// TODO v3.0
|
||||
//me.emit('directoryError', me._wcurpath.replace(/^(.*)\/.*$/, '$1'), { name: me._wcurpath.replace(/^.*\/(.*)/, '$1'), error: err }, noop);
|
||||
me._wReadFiles();
|
||||
return;
|
||||
}
|
||||
|
||||
// error? first run? => maybe a file, maybe a true error
|
||||
me._wfirstrun = false;
|
||||
|
||||
// readdir failed (might be a file), try a stat on the parent
|
||||
parent = me._wcurpath.replace(/^(.*)\/.*$/, '$1');
|
||||
fs[me._wStat](parent, function (e, stat) {
|
||||
|
||||
if (stat) {
|
||||
// success
|
||||
// now try stat on this as a child of the parent directory
|
||||
child = me._wcurpath.replace(/^.*\/(.*)$/, '$1');
|
||||
me._wcurfiles = [child];
|
||||
me._wcurpath = parent;
|
||||
} else {
|
||||
// TODO v3.0
|
||||
//me.emit('directoryError', me._wcurpath.replace(/^(.*)\/.*$/, '$1'), { name: me._wcurpath.replace(/^.*\/(.*)/, '$1'), error: err }, noop);
|
||||
// TODO v3.0 (don't noop the next)
|
||||
// the original readdir error, not the parent stat error
|
||||
me.emit('nodeError', me._wcurpath, { error: err }, noop);
|
||||
}
|
||||
|
||||
me._wReadFiles();
|
||||
});
|
||||
};
|
||||
Walker.prototype._wFilter = function () {
|
||||
var me = this
|
||||
, exclude
|
||||
;
|
||||
|
||||
// Stop directories that contain filter keywords
|
||||
// from continuing through the walk process
|
||||
exclude = me._wfilters.some(function (filter) {
|
||||
if (me._wcurpath.match(filter)) {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
return exclude;
|
||||
};
|
||||
Walker.prototype._wWalkSync = function () {
|
||||
//console.log('walkSync');
|
||||
var err
|
||||
, files
|
||||
, me = this
|
||||
;
|
||||
|
||||
try {
|
||||
files = fs.readdirSync(me._wcurpath);
|
||||
} catch(e) {
|
||||
err = e;
|
||||
}
|
||||
|
||||
me._wReaddirHandler(err, files);
|
||||
};
|
||||
Walker.prototype._wWalkAsync = function () {
|
||||
//console.log('walkAsync');
|
||||
var me = this
|
||||
;
|
||||
|
||||
// TODO how to remove this anony?
|
||||
fs.readdir(me._wcurpath, function (err, files) {
|
||||
me._wReaddirHandler(err, files);
|
||||
});
|
||||
};
|
||||
Walker.prototype._wNext = function () {
|
||||
var me = this
|
||||
;
|
||||
|
||||
if (me._paused) {
|
||||
return;
|
||||
}
|
||||
if (me._wq.length) {
|
||||
me._wcurpath = me._wq.pop();
|
||||
while (me._wq.length && me._wFilter()) {
|
||||
me._wcurpath = me._wq.pop();
|
||||
}
|
||||
if (me._wcurpath && !me._wFilter()) {
|
||||
me._wWalk();
|
||||
} else {
|
||||
me._wNext();
|
||||
}
|
||||
return;
|
||||
}
|
||||
me._wqueue.length -= 1;
|
||||
if (me._wqueue.length) {
|
||||
me._wq = me._wqueue[me._wqueue.length - 1];
|
||||
return me._wNext();
|
||||
}
|
||||
|
||||
// To not break compatibility
|
||||
//process.nextTick(function () {
|
||||
me.emit('end');
|
||||
//});
|
||||
};
|
||||
Walker.prototype._wJoinPath = function (v, i, o) {
|
||||
var me = this
|
||||
;
|
||||
|
||||
o[i] = [me._wcurpath, path.sep, v].join('');
|
||||
};
|
||||
Walker.prototype.pause = function () {
|
||||
this._paused = true;
|
||||
};
|
||||
Walker.prototype.resume = function () {
|
||||
this._paused = false;
|
||||
this._wNext();
|
||||
};
|
||||
|
||||
exports.walk = function (path, opts) {
|
||||
return new Walker(path, opts, false);
|
||||
};
|
||||
|
||||
exports.walkSync = function (path, opts) {
|
||||
return new Walker(path, opts, true);
|
||||
};
|
||||
}());
|
Reference in New Issue
Block a user