filer/dist/filer.js

7531 lines
202 KiB
JavaScript
Raw Normal View History

2014-05-28 20:00:10 +00:00
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Filer=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
(function (process){
/*global setImmediate: false, setTimeout: false, console: false */
/**
* https://raw.github.com/caolan/async/master/lib/async.js Feb 18, 2014
* Used under MIT - https://github.com/caolan/async/blob/master/LICENSE
*/
(function () {
var async = {};
// global on the server, window in the browser
var root, previous_async;
root = this;
if (root != null) {
previous_async = root.async;
}
async.noConflict = function () {
root.async = previous_async;
return async;
};
function only_once(fn) {
var called = false;
return function() {
if (called) throw new Error("Callback was already called.");
called = true;
fn.apply(root, arguments);
}
}
//// cross-browser compatiblity functions ////
var _each = function (arr, iterator) {
if (arr.forEach) {
return arr.forEach(iterator);
}
for (var i = 0; i < arr.length; i += 1) {
iterator(arr[i], i, arr);
}
};
var _map = function (arr, iterator) {
if (arr.map) {
return arr.map(iterator);
}
var results = [];
_each(arr, function (x, i, a) {
results.push(iterator(x, i, a));
});
return results;
};
var _reduce = function (arr, iterator, memo) {
if (arr.reduce) {
return arr.reduce(iterator, memo);
}
_each(arr, function (x, i, a) {
memo = iterator(memo, x, i, a);
});
return memo;
};
var _keys = function (obj) {
if (Object.keys) {
return Object.keys(obj);
}
var keys = [];
for (var k in obj) {
if (obj.hasOwnProperty(k)) {
keys.push(k);
}
}
return keys;
};
//// exported async module functions ////
//// nextTick implementation with browser-compatible fallback ////
if (typeof process === 'undefined' || !(process.nextTick)) {
if (typeof setImmediate === 'function') {
async.nextTick = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
async.setImmediate = async.nextTick;
}
else {
async.nextTick = function (fn) {
setTimeout(fn, 0);
};
async.setImmediate = async.nextTick;
}
}
else {
async.nextTick = process.nextTick;
if (typeof setImmediate !== 'undefined') {
async.setImmediate = function (fn) {
// not a direct alias for IE10 compatibility
setImmediate(fn);
};
}
else {
async.setImmediate = async.nextTick;
}
}
async.each = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
var completed = 0;
_each(arr, function (x) {
iterator(x, only_once(function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed >= arr.length) {
callback(null);
}
}
}));
});
};
async.forEach = async.each;
async.eachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
var completed = 0;
var iterate = function () {
iterator(arr[completed], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
if (completed >= arr.length) {
callback(null);
}
else {
iterate();
}
}
});
};
iterate();
};
async.forEachSeries = async.eachSeries;
async.eachLimit = function (arr, limit, iterator, callback) {
var fn = _eachLimit(limit);
fn.apply(null, [arr, iterator, callback]);
};
async.forEachLimit = async.eachLimit;
var _eachLimit = function (limit) {
return function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length || limit <= 0) {
return callback();
}
var completed = 0;
var started = 0;
var running = 0;
(function replenish () {
if (completed >= arr.length) {
return callback();
}
while (running < limit && started < arr.length) {
started += 1;
running += 1;
iterator(arr[started - 1], function (err) {
if (err) {
callback(err);
callback = function () {};
}
else {
completed += 1;
running -= 1;
if (completed >= arr.length) {
callback();
}
else {
replenish();
}
}
});
}
})();
};
};
var doParallel = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.each].concat(args));
};
};
var doParallelLimit = function(limit, fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [_eachLimit(limit)].concat(args));
};
};
var doSeries = function (fn) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [async.eachSeries].concat(args));
};
};
var _asyncMap = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (err, v) {
results[x.index] = v;
callback(err);
});
}, function (err) {
callback(err, results);
});
};
async.map = doParallel(_asyncMap);
async.mapSeries = doSeries(_asyncMap);
async.mapLimit = function (arr, limit, iterator, callback) {
return _mapLimit(limit)(arr, iterator, callback);
};
var _mapLimit = function(limit) {
return doParallelLimit(limit, _asyncMap);
};
// reduce only has a series version, as doing reduce in parallel won't
// work in many situations.
async.reduce = function (arr, memo, iterator, callback) {
async.eachSeries(arr, function (x, callback) {
iterator(memo, x, function (err, v) {
memo = v;
callback(err);
});
}, function (err) {
callback(err, memo);
});
};
// inject alias
async.inject = async.reduce;
// foldl alias
async.foldl = async.reduce;
async.reduceRight = function (arr, memo, iterator, callback) {
var reversed = _map(arr, function (x) {
return x;
}).reverse();
async.reduce(reversed, memo, iterator, callback);
};
// foldr alias
async.foldr = async.reduceRight;
var _filter = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.filter = doParallel(_filter);
async.filterSeries = doSeries(_filter);
// select alias
async.select = async.filter;
async.selectSeries = async.filterSeries;
var _reject = function (eachfn, arr, iterator, callback) {
var results = [];
arr = _map(arr, function (x, i) {
return {index: i, value: x};
});
eachfn(arr, function (x, callback) {
iterator(x.value, function (v) {
if (!v) {
results.push(x);
}
callback();
});
}, function (err) {
callback(_map(results.sort(function (a, b) {
return a.index - b.index;
}), function (x) {
return x.value;
}));
});
};
async.reject = doParallel(_reject);
async.rejectSeries = doSeries(_reject);
var _detect = function (eachfn, arr, iterator, main_callback) {
eachfn(arr, function (x, callback) {
iterator(x, function (result) {
if (result) {
main_callback(x);
main_callback = function () {};
}
else {
callback();
}
});
}, function (err) {
main_callback();
});
};
async.detect = doParallel(_detect);
async.detectSeries = doSeries(_detect);
async.some = function (arr, iterator, main_callback) {
async.each(arr, function (x, callback) {
iterator(x, function (v) {
if (v) {
main_callback(true);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(false);
});
};
// any alias
async.any = async.some;
async.every = function (arr, iterator, main_callback) {
async.each(arr, function (x, callback) {
iterator(x, function (v) {
if (!v) {
main_callback(false);
main_callback = function () {};
}
callback();
});
}, function (err) {
main_callback(true);
});
};
// all alias
async.all = async.every;
async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {
iterator(x, function (err, criteria) {
if (err) {
callback(err);
}
else {
callback(null, {value: x, criteria: criteria});
}
});
}, function (err, results) {
if (err) {
return callback(err);
}
else {
var fn = function (left, right) {
var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
};
callback(null, _map(results.sort(fn), function (x) {
return x.value;
}));
}
});
};
async.auto = function (tasks, callback) {
callback = callback || function () {};
var keys = _keys(tasks);
if (!keys.length) {
return callback(null);
}
var results = {};
var listeners = [];
var addListener = function (fn) {
listeners.unshift(fn);
};
var removeListener = function (fn) {
for (var i = 0; i < listeners.length; i += 1) {
if (listeners[i] === fn) {
listeners.splice(i, 1);
return;
}
}
};
var taskComplete = function () {
_each(listeners.slice(0), function (fn) {
fn();
});
};
addListener(function () {
if (_keys(results).length === keys.length) {
callback(null, results);
callback = function () {};
}
});
_each(keys, function (k) {
var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
var taskCallback = function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
if (err) {
var safeResults = {};
_each(_keys(results), function(rkey) {
safeResults[rkey] = results[rkey];
});
safeResults[k] = args;
callback(err, safeResults);
// stop subsequent errors hitting callback multiple times
callback = function () {};
}
else {
results[k] = args;
async.setImmediate(taskComplete);
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
var ready = function () {
return _reduce(requires, function (a, x) {
return (a && results.hasOwnProperty(x));
}, true) && !results.hasOwnProperty(k);
};
if (ready()) {
task[task.length - 1](taskCallback, results);
}
else {
var listener = function () {
if (ready()) {
removeListener(listener);
task[task.length - 1](taskCallback, results);
}
};
addListener(listener);
}
});
};
async.waterfall = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor !== Array) {
var err = new Error('First argument to waterfall must be an array of functions');
return callback(err);
}
if (!tasks.length) {
return callback();
}
var wrapIterator = function (iterator) {
return function (err) {
if (err) {
callback.apply(null, arguments);
callback = function () {};
}
else {
var args = Array.prototype.slice.call(arguments, 1);
var next = iterator.next();
if (next) {
args.push(wrapIterator(next));
}
else {
args.push(callback);
}
async.setImmediate(function () {
iterator.apply(null, args);
});
}
};
};
wrapIterator(async.iterator(tasks))();
};
var _parallel = function(eachfn, tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
eachfn.map(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args);
});
}
}, callback);
}
else {
var results = {};
eachfn.each(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.parallel = function (tasks, callback) {
_parallel({ map: async.map, each: async.each }, tasks, callback);
};
async.parallelLimit = function(tasks, limit, callback) {
_parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback);
};
async.series = function (tasks, callback) {
callback = callback || function () {};
if (tasks.constructor === Array) {
async.mapSeries(tasks, function (fn, callback) {
if (fn) {
fn(function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
callback.call(null, err, args);
});
}
}, callback);
}
else {
var results = {};
async.eachSeries(_keys(tasks), function (k, callback) {
tasks[k](function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
callback(err);
});
}, function (err) {
callback(err, results);
});
}
};
async.iterator = function (tasks) {
var makeCallback = function (index) {
var fn = function () {
if (tasks.length) {
tasks[index].apply(null, arguments);
}
return fn.next();
};
fn.next = function () {
return (index < tasks.length - 1) ? makeCallback(index + 1): null;
};
return fn;
};
return makeCallback(0);
};
async.apply = function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
return function () {
return fn.apply(
null, args.concat(Array.prototype.slice.call(arguments))
);
};
};
var _concat = function (eachfn, arr, fn, callback) {
var r = [];
eachfn(arr, function (x, cb) {
fn(x, function (err, y) {
r = r.concat(y || []);
cb(err);
});
}, function (err) {
callback(err, r);
});
};
async.concat = doParallel(_concat);
async.concatSeries = doSeries(_concat);
async.whilst = function (test, iterator, callback) {
if (test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.whilst(test, iterator, callback);
});
}
else {
callback();
}
};
async.doWhilst = function (iterator, test, callback) {
iterator(function (err) {
if (err) {
return callback(err);
}
if (test()) {
async.doWhilst(iterator, test, callback);
}
else {
callback();
}
});
};
async.until = function (test, iterator, callback) {
if (!test()) {
iterator(function (err) {
if (err) {
return callback(err);
}
async.until(test, iterator, callback);
});
}
else {
callback();
}
};
async.doUntil = function (iterator, test, callback) {
iterator(function (err) {
if (err) {
return callback(err);
}
if (!test()) {
async.doUntil(iterator, test, callback);
}
else {
callback();
}
});
};
async.queue = function (worker, concurrency) {
if (concurrency === undefined) {
concurrency = 1;
}
function _insert(q, data, pos, callback) {
if(data.constructor !== Array) {
data = [data];
}
_each(data, function(task) {
var item = {
data: task,
callback: typeof callback === 'function' ? callback : null
};
if (pos) {
q.tasks.unshift(item);
} else {
q.tasks.push(item);
}
if (q.saturated && q.tasks.length === concurrency) {
q.saturated();
}
async.setImmediate(q.process);
});
}
var workers = 0;
var q = {
tasks: [],
concurrency: concurrency,
saturated: null,
empty: null,
drain: null,
push: function (data, callback) {
_insert(q, data, false, callback);
},
unshift: function (data, callback) {
_insert(q, data, true, callback);
},
process: function () {
if (workers < q.concurrency && q.tasks.length) {
var task = q.tasks.shift();
if (q.empty && q.tasks.length === 0) {
q.empty();
}
workers += 1;
var next = function () {
workers -= 1;
if (task.callback) {
task.callback.apply(task, arguments);
}
if (q.drain && q.tasks.length + workers === 0) {
q.drain();
}
q.process();
};
var cb = only_once(next);
worker(task.data, cb);
}
},
length: function () {
return q.tasks.length;
},
running: function () {
return workers;
}
};
return q;
};
async.cargo = function (worker, payload) {
var working = false,
tasks = [];
var cargo = {
tasks: tasks,
payload: payload,
saturated: null,
empty: null,
drain: null,
push: function (data, callback) {
if(data.constructor !== Array) {
data = [data];
}
_each(data, function(task) {
tasks.push({
data: task,
callback: typeof callback === 'function' ? callback : null
});
if (cargo.saturated && tasks.length === payload) {
cargo.saturated();
}
});
async.setImmediate(cargo.process);
},
process: function process() {
if (working) return;
if (tasks.length === 0) {
if(cargo.drain) cargo.drain();
return;
}
var ts = typeof payload === 'number'
? tasks.splice(0, payload)
: tasks.splice(0);
var ds = _map(ts, function (task) {
return task.data;
});
if(cargo.empty) cargo.empty();
working = true;
worker(ds, function () {
working = false;
var args = arguments;
_each(ts, function (data) {
if (data.callback) {
data.callback.apply(null, args);
}
});
process();
});
},
length: function () {
return tasks.length;
},
running: function () {
return working;
}
};
return cargo;
};
var _console_fn = function (name) {
return function (fn) {
var args = Array.prototype.slice.call(arguments, 1);
fn.apply(null, args.concat([function (err) {
var args = Array.prototype.slice.call(arguments, 1);
if (typeof console !== 'undefined') {
if (err) {
if (console.error) {
console.error(err);
}
}
else if (console[name]) {
_each(args, function (x) {
console[name](x);
});
}
}
}]));
};
};
async.log = _console_fn('log');
async.dir = _console_fn('dir');
/*async.info = _console_fn('info');
async.warn = _console_fn('warn');
async.error = _console_fn('error');*/
async.memoize = function (fn, hasher) {
var memo = {};
var queues = {};
hasher = hasher || function (x) {
return x;
};
var memoized = function () {
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
var key = hasher.apply(null, args);
if (key in memo) {
callback.apply(null, memo[key]);
}
else if (key in queues) {
queues[key].push(callback);
}
else {
queues[key] = [callback];
fn.apply(null, args.concat([function () {
memo[key] = arguments;
var q = queues[key];
delete queues[key];
for (var i = 0, l = q.length; i < l; i++) {
q[i].apply(null, arguments);
}
}]));
}
};
memoized.memo = memo;
memoized.unmemoized = fn;
return memoized;
};
async.unmemoize = function (fn) {
return function () {
return (fn.unmemoized || fn).apply(null, arguments);
};
};
async.times = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.map(counter, iterator, callback);
};
async.timesSeries = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.mapSeries(counter, iterator, callback);
};
async.compose = function (/* functions... */) {
var fns = Array.prototype.reverse.call(arguments);
return function () {
var that = this;
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
async.reduce(fns, args, function (newargs, fn, cb) {
fn.apply(that, newargs.concat([function () {
var err = arguments[0];
var nextargs = Array.prototype.slice.call(arguments, 1);
cb(err, nextargs);
}]))
},
function (err, results) {
callback.apply(that, [err].concat(results));
});
};
};
var _applyEach = function (eachfn, fns /*args...*/) {
var go = function () {
var that = this;
var args = Array.prototype.slice.call(arguments);
var callback = args.pop();
return eachfn(fns, function (fn, cb) {
fn.apply(that, args.concat([cb]));
},
callback);
};
if (arguments.length > 2) {
var args = Array.prototype.slice.call(arguments, 2);
return go.apply(this, args);
}
else {
return go;
}
};
async.applyEach = doParallel(_applyEach);
async.applyEachSeries = doSeries(_applyEach);
async.forever = function (fn, callback) {
function next(err) {
if (err) {
if (callback) {
return callback(err);
}
throw err;
}
fn(next);
}
next();
};
// AMD / RequireJS
if (typeof define !== 'undefined' && define.amd) {
define([], function () {
return async;
});
}
// Node.js
else if (typeof module !== 'undefined' && module.exports) {
module.exports = async;
}
// included directly via <script> tag
else {
root.async = async;
}
}());
2014-07-14 18:33:22 +00:00
}).call(this,_dereq_("JkpR2F"))
2014-08-17 05:56:03 +00:00
},{"JkpR2F":10}],2:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
// Based on https://github.com/diy/intercom.js/blob/master/lib/events.js
// Copyright 2012 DIY Co Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
function removeItem(item, array) {
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] === item) {
array.splice(i, 1);
}
}
return array;
}
var EventEmitter = function() {};
EventEmitter.createInterface = function(space) {
var methods = {};
methods.on = function(name, fn) {
if (typeof this[space] === 'undefined') {
this[space] = {};
}
if (!this[space].hasOwnProperty(name)) {
this[space][name] = [];
}
this[space][name].push(fn);
};
methods.off = function(name, fn) {
if (typeof this[space] === 'undefined') return;
if (this[space].hasOwnProperty(name)) {
removeItem(fn, this[space][name]);
}
};
methods.trigger = function(name) {
if (typeof this[space] !== 'undefined' && this[space].hasOwnProperty(name)) {
var args = Array.prototype.slice.call(arguments, 1);
for (var i = 0; i < this[space][name].length; i++) {
this[space][name][i].apply(this[space][name][i], args);
}
}
};
methods.removeAllListeners = function(name) {
if (typeof this[space] === 'undefined') return;
var self = this;
self[space][name].forEach(function(fn) {
self.off(name, fn);
});
};
return methods;
};
var pvt = EventEmitter.createInterface('_handlers');
EventEmitter.prototype._on = pvt.on;
EventEmitter.prototype._off = pvt.off;
EventEmitter.prototype._trigger = pvt.trigger;
var pub = EventEmitter.createInterface('handlers');
EventEmitter.prototype.on = function() {
pub.on.apply(this, arguments);
Array.prototype.unshift.call(arguments, 'on');
this._trigger.apply(this, arguments);
};
EventEmitter.prototype.off = pub.off;
EventEmitter.prototype.trigger = pub.trigger;
EventEmitter.prototype.removeAllListeners = pub.removeAllListeners;
module.exports = EventEmitter;
2014-06-09 14:40:50 +00:00
},{}],3:[function(_dereq_,module,exports){
2014-07-15 17:22:25 +00:00
(function (global){
2014-05-27 18:44:30 +00:00
// Based on https://github.com/diy/intercom.js/blob/master/lib/intercom.js
// Copyright 2012 DIY Co Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0
2014-05-28 20:00:10 +00:00
var EventEmitter = _dereq_('./eventemitter.js');
var guid = _dereq_('../src/shared.js').guid;
2014-05-27 18:44:30 +00:00
function throttle(delay, fn) {
var last = 0;
return function() {
var now = Date.now();
if (now - last > delay) {
last = now;
fn.apply(this, arguments);
}
};
}
function extend(a, b) {
if (typeof a === 'undefined' || !a) { a = {}; }
if (typeof b === 'object') {
for (var key in b) {
if (b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
}
return a;
}
var localStorage = (function(window) {
if (typeof window === 'undefined' ||
typeof window.localStorage === 'undefined') {
return {
getItem : function() {},
setItem : function() {},
removeItem : function() {}
};
}
return window.localStorage;
2014-07-15 17:22:25 +00:00
}(global));
2014-05-27 18:44:30 +00:00
function Intercom() {
var self = this;
var now = Date.now();
this.origin = guid();
this.lastMessage = now;
this.receivedIDs = {};
this.previousValues = {};
var storageHandler = function() {
self._onStorageEvent.apply(self, arguments);
};
// If we're in node.js, skip event registration
2014-07-15 17:22:25 +00:00
if (typeof document === 'undefined') {
2014-05-27 18:44:30 +00:00
return;
}
if (document.attachEvent) {
document.attachEvent('onstorage', storageHandler);
} else {
2014-07-15 17:22:25 +00:00
global.addEventListener('storage', storageHandler, false);
2014-05-27 18:44:30 +00:00
}
}
Intercom.prototype._transaction = function(fn) {
var TIMEOUT = 1000;
var WAIT = 20;
var self = this;
var executed = false;
var listening = false;
var waitTimer = null;
function lock() {
if (executed) {
return;
}
var now = Date.now();
var activeLock = localStorage.getItem(INDEX_LOCK)|0;
if (activeLock && now - activeLock < TIMEOUT) {
if (!listening) {
self._on('storage', lock);
listening = true;
}
waitTimer = setTimeout(lock, WAIT);
return;
}
executed = true;
localStorage.setItem(INDEX_LOCK, now);
fn();
unlock();
}
function unlock() {
if (listening) {
self._off('storage', lock);
}
if (waitTimer) {
clearTimeout(waitTimer);
}
localStorage.removeItem(INDEX_LOCK);
}
lock();
};
Intercom.prototype._cleanup_emit = throttle(100, function() {
var self = this;
self._transaction(function() {
var now = Date.now();
var threshold = now - THRESHOLD_TTL_EMIT;
var changed = 0;
var messages;
try {
messages = JSON.parse(localStorage.getItem(INDEX_EMIT) || '[]');
} catch(e) {
messages = [];
}
for (var i = messages.length - 1; i >= 0; i--) {
if (messages[i].timestamp < threshold) {
messages.splice(i, 1);
changed++;
}
}
if (changed > 0) {
localStorage.setItem(INDEX_EMIT, JSON.stringify(messages));
}
});
});
Intercom.prototype._cleanup_once = throttle(100, function() {
var self = this;
self._transaction(function() {
var timestamp, ttl, key;
var table;
var now = Date.now();
var changed = 0;
try {
table = JSON.parse(localStorage.getItem(INDEX_ONCE) || '{}');
} catch(e) {
table = {};
}
for (key in table) {
if (self._once_expired(key, table)) {
delete table[key];
changed++;
}
}
if (changed > 0) {
localStorage.setItem(INDEX_ONCE, JSON.stringify(table));
}
});
});
Intercom.prototype._once_expired = function(key, table) {
if (!table) {
return true;
}
if (!table.hasOwnProperty(key)) {
return true;
}
if (typeof table[key] !== 'object') {
return true;
}
var ttl = table[key].ttl || THRESHOLD_TTL_ONCE;
var now = Date.now();
var timestamp = table[key].timestamp;
return timestamp < now - ttl;
};
Intercom.prototype._localStorageChanged = function(event, field) {
if (event && event.key) {
return event.key === field;
}
var currentValue = localStorage.getItem(field);
if (currentValue === this.previousValues[field]) {
return false;
}
this.previousValues[field] = currentValue;
return true;
};
Intercom.prototype._onStorageEvent = function(event) {
2014-07-15 17:22:25 +00:00
event = event || global.event;
2014-05-27 18:44:30 +00:00
var self = this;
if (this._localStorageChanged(event, INDEX_EMIT)) {
this._transaction(function() {
var now = Date.now();
var data = localStorage.getItem(INDEX_EMIT);
var messages;
try {
messages = JSON.parse(data || '[]');
} catch(e) {
messages = [];
}
for (var i = 0; i < messages.length; i++) {
if (messages[i].origin === self.origin) continue;
if (messages[i].timestamp < self.lastMessage) continue;
if (messages[i].id) {
if (self.receivedIDs.hasOwnProperty(messages[i].id)) continue;
self.receivedIDs[messages[i].id] = true;
}
self.trigger(messages[i].name, messages[i].payload);
}
self.lastMessage = now;
});
}
this._trigger('storage', event);
};
Intercom.prototype._emit = function(name, message, id) {
id = (typeof id === 'string' || typeof id === 'number') ? String(id) : null;
if (id && id.length) {
if (this.receivedIDs.hasOwnProperty(id)) return;
this.receivedIDs[id] = true;
}
var packet = {
id : id,
name : name,
origin : this.origin,
timestamp : Date.now(),
payload : message
};
var self = this;
this._transaction(function() {
var data = localStorage.getItem(INDEX_EMIT) || '[]';
var delimiter = (data === '[]') ? '' : ',';
data = [data.substring(0, data.length - 1), delimiter, JSON.stringify(packet), ']'].join('');
localStorage.setItem(INDEX_EMIT, data);
self.trigger(name, message);
setTimeout(function() {
self._cleanup_emit();
}, 50);
});
};
Intercom.prototype.emit = function(name, message) {
this._emit.apply(this, arguments);
this._trigger('emit', name, message);
};
Intercom.prototype.once = function(key, fn, ttl) {
if (!Intercom.supported) {
return;
}
var self = this;
this._transaction(function() {
var data;
try {
data = JSON.parse(localStorage.getItem(INDEX_ONCE) || '{}');
} catch(e) {
data = {};
}
if (!self._once_expired(key, data)) {
return;
}
data[key] = {};
data[key].timestamp = Date.now();
if (typeof ttl === 'number') {
data[key].ttl = ttl * 1000;
}
localStorage.setItem(INDEX_ONCE, JSON.stringify(data));
fn();
setTimeout(function() {
self._cleanup_once();
}, 50);
});
};
extend(Intercom.prototype, EventEmitter.prototype);
Intercom.supported = (typeof localStorage !== 'undefined');
var INDEX_EMIT = 'intercom';
var INDEX_ONCE = 'intercom_once';
var INDEX_LOCK = 'intercom_lock';
2014-06-09 14:40:50 +00:00
var THRESHOLD_TTL_EMIT = 50000;
var THRESHOLD_TTL_ONCE = 1000 * 3600;
Intercom.destroy = function() {
localStorage.removeItem(INDEX_LOCK);
localStorage.removeItem(INDEX_EMIT);
localStorage.removeItem(INDEX_ONCE);
};
Intercom.getInstance = (function() {
var intercom;
return function() {
if (!intercom) {
intercom = new Intercom();
}
return intercom;
};
})();
module.exports = Intercom;
2014-07-15 17:22:25 +00:00
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
2014-09-02 19:48:54 +00:00
},{"../src/shared.js":27,"./eventemitter.js":2}],4:[function(_dereq_,module,exports){
2014-06-09 14:40:50 +00:00
// Cherry-picked bits of underscore.js, lodash.js
/**
* Lo-Dash 2.4.0 <http://lodash.com/>
* Copyright 2012-2013 The Dojo Foundation <http://dojofoundation.org/>
* Based on Underscore.js 1.5.2 <http://underscorejs.org/LICENSE>
* Copyright 2009-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
* Available under MIT license <http://lodash.com/license>
*/
var ArrayProto = Array.prototype;
var nativeForEach = ArrayProto.forEach;
var nativeIndexOf = ArrayProto.indexOf;
var nativeSome = ArrayProto.some;
var ObjProto = Object.prototype;
var hasOwnProperty = ObjProto.hasOwnProperty;
var nativeKeys = Object.keys;
var breaker = {};
function has(obj, key) {
return hasOwnProperty.call(obj, key);
}
var keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
for (var key in obj) if (has(obj, key)) keys.push(key);
return keys;
};
function size(obj) {
if (obj == null) return 0;
return (obj.length === +obj.length) ? obj.length : keys(obj).length;
}
function identity(value) {
return value;
}
function each(obj, iterator, context) {
var i, length;
if (obj == null) return;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
for (i = 0, length = obj.length; i < length; i++) {
if (iterator.call(context, obj[i], i, obj) === breaker) return;
}
} else {
var keys = keys(obj);
for (i = 0, length = keys.length; i < length; i++) {
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
};
function any(obj, iterator, context) {
iterator || (iterator = identity);
var result = false;
if (obj == null) return result;
if (nativeSome && obj.some === nativeSome) return obj.some(iterator, context);
each(obj, function(value, index, list) {
if (result || (result = iterator.call(context, value, index, list))) return breaker;
});
return !!result;
};
function contains(obj, target) {
if (obj == null) return false;
if (nativeIndexOf && obj.indexOf === nativeIndexOf) return obj.indexOf(target) != -1;
return any(obj, function(value) {
return value === target;
});
};
function Wrapped(value) {
this.value = value;
}
Wrapped.prototype.has = function(key) {
return has(this.value, key);
};
Wrapped.prototype.contains = function(target) {
return contains(this.value, target);
};
Wrapped.prototype.size = function() {
return size(this.value);
};
function nodash(value) {
// don't wrap if already wrapped, even if wrapped by a different `lodash` constructor
return (value && typeof value == 'object' && !Array.isArray(value) && hasOwnProperty.call(value, '__wrapped__'))
? value
: new Wrapped(value);
}
module.exports = nodash;
},{}],5:[function(_dereq_,module,exports){
2014-08-17 05:56:03 +00:00
/*
* base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer
*
* Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license.
*/
(function(chars){
"use strict";
exports.encode = function(arraybuffer) {
var bytes = new Uint8Array(arraybuffer),
i, len = bytes.length, base64 = "";
for (i = 0; i < len; i+=3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
base64 += chars[bytes[i + 2] & 63];
}
if ((len % 3) === 2) {
base64 = base64.substring(0, base64.length - 1) + "=";
} else if (len % 3 === 1) {
base64 = base64.substring(0, base64.length - 2) + "==";
}
return base64;
};
exports.decode = function(base64) {
var bufferLength = base64.length * 0.75,
len = base64.length, i, p = 0,
encoded1, encoded2, encoded3, encoded4;
if (base64[base64.length - 1] === "=") {
bufferLength--;
if (base64[base64.length - 2] === "=") {
bufferLength--;
}
}
var arraybuffer = new ArrayBuffer(bufferLength),
bytes = new Uint8Array(arraybuffer);
for (i = 0; i < len; i+=4) {
encoded1 = chars.indexOf(base64[i]);
encoded2 = chars.indexOf(base64[i+1]);
encoded3 = chars.indexOf(base64[i+2]);
encoded4 = chars.indexOf(base64[i+3]);
bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
}
return arraybuffer;
};
})("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
},{}],6:[function(_dereq_,module,exports){
2014-06-09 14:40:50 +00:00
(function (Buffer){
// Browser Request
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var XHR = XMLHttpRequest
if (!XHR) throw new Error('missing XMLHttpRequest')
request.log = {
'trace': noop, 'debug': noop, 'info': noop, 'warn': noop, 'error': noop
}
var DEFAULT_TIMEOUT = 3 * 60 * 1000 // 3 minutes
//
// request
//
function request(options, callback) {
// The entry-point to the API: prep the options object and pass the real work to run_xhr.
if(typeof callback !== 'function')
throw new Error('Bad callback given: ' + callback)
if(!options)
throw new Error('No options given')
var options_onResponse = options.onResponse; // Save this for later.
if(typeof options === 'string')
options = {'uri':options};
else
options = JSON.parse(JSON.stringify(options)); // Use a duplicate for mutating.
options.onResponse = options_onResponse // And put it back.
if (options.verbose) request.log = getLogger();
if(options.url) {
options.uri = options.url;
delete options.url;
}
if(!options.uri && options.uri !== "")
throw new Error("options.uri is a required argument");
if(typeof options.uri != "string")
throw new Error("options.uri must be a string");
var unsupported_options = ['proxy', '_redirectsFollowed', 'maxRedirects', 'followRedirect']
for (var i = 0; i < unsupported_options.length; i++)
if(options[ unsupported_options[i] ])
throw new Error("options." + unsupported_options[i] + " is not supported")
options.callback = callback
options.method = options.method || 'GET';
options.headers = options.headers || {};
options.body = options.body || null
options.timeout = options.timeout || request.DEFAULT_TIMEOUT
if(options.headers.host)
throw new Error("Options.headers.host is not supported");
if(options.json) {
options.headers.accept = options.headers.accept || 'application/json'
if(options.method !== 'GET')
options.headers['content-type'] = 'application/json'
if(typeof options.json !== 'boolean')
options.body = JSON.stringify(options.json)
else if(typeof options.body !== 'string')
options.body = JSON.stringify(options.body)
}
//BEGIN QS Hack
var serialize = function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
if(options.qs){
var qs = (typeof options.qs == 'string')? options.qs : serialize(options.qs);
if(options.uri.indexOf('?') !== -1){ //no get params
options.uri = options.uri+'&'+qs;
}else{ //existing get params
options.uri = options.uri+'?'+qs;
}
}
//END QS Hack
//BEGIN FORM Hack
var multipart = function(obj) {
//todo: support file type (useful?)
var result = {};
result.boundry = '-------------------------------'+Math.floor(Math.random()*1000000000);
var lines = [];
for(var p in obj){
if (obj.hasOwnProperty(p)) {
lines.push(
'--'+result.boundry+"\n"+
'Content-Disposition: form-data; name="'+p+'"'+"\n"+
"\n"+
obj[p]+"\n"
);
}
}
lines.push( '--'+result.boundry+'--' );
result.body = lines.join('');
result.length = result.body.length;
result.type = 'multipart/form-data; boundary='+result.boundry;
return result;
}
if(options.form){
if(typeof options.form == 'string') throw('form name unsupported');
if(options.method === 'POST'){
var encoding = (options.encoding || 'application/x-www-form-urlencoded').toLowerCase();
options.headers['content-type'] = encoding;
switch(encoding){
case 'application/x-www-form-urlencoded':
options.body = serialize(options.form).replace(/%20/g, "+");
break;
case 'multipart/form-data':
var multi = multipart(options.form);
//options.headers['content-length'] = multi.length;
options.body = multi.body;
options.headers['content-type'] = multi.type;
break;
default : throw new Error('unsupported encoding:'+encoding);
}
}
}
//END FORM Hack
// If onResponse is boolean true, call back immediately when the response is known,
// not when the full request is complete.
options.onResponse = options.onResponse || noop
if(options.onResponse === true) {
options.onResponse = callback
options.callback = noop
}
// XXX Browsers do not like this.
//if(options.body)
// options.headers['content-length'] = options.body.length;
// HTTP basic authentication
if(!options.headers.authorization && options.auth)
options.headers.authorization = 'Basic ' + b64_enc(options.auth.username + ':' + options.auth.password);
return run_xhr(options)
}
var req_seq = 0
function run_xhr(options) {
var xhr = new XHR
, timed_out = false
, is_cors = is_crossDomain(options.uri)
, supports_cors = ('withCredentials' in xhr)
req_seq += 1
xhr.seq_id = req_seq
xhr.id = req_seq + ': ' + options.method + ' ' + options.uri
xhr._id = xhr.id // I know I will type "_id" from habit all the time.
if(is_cors && !supports_cors) {
var cors_err = new Error('Browser does not support cross-origin request: ' + options.uri)
cors_err.cors = 'unsupported'
return options.callback(cors_err, xhr)
}
xhr.timeoutTimer = setTimeout(too_late, options.timeout)
function too_late() {
timed_out = true
var er = new Error('ETIMEDOUT')
er.code = 'ETIMEDOUT'
er.duration = options.timeout
request.log.error('Timeout', { 'id':xhr._id, 'milliseconds':options.timeout })
return options.callback(er, xhr)
}
// Some states can be skipped over, so remember what is still incomplete.
var did = {'response':false, 'loading':false, 'end':false}
xhr.onreadystatechange = on_state_change
xhr.open(options.method, options.uri, true) // asynchronous
// Deal with requests for raw buffer response
if(options.encoding === null) {
xhr.responseType = 'arraybuffer';
}
if(is_cors)
xhr.withCredentials = !! options.withCredentials
xhr.send(options.body)
return xhr
function on_state_change(event) {
if(timed_out)
return request.log.debug('Ignoring timed out state change', {'state':xhr.readyState, 'id':xhr.id})
request.log.debug('State change', {'state':xhr.readyState, 'id':xhr.id, 'timed_out':timed_out})
if(xhr.readyState === XHR.OPENED) {
request.log.debug('Request started', {'id':xhr.id})
for (var key in options.headers)
xhr.setRequestHeader(key, options.headers[key])
}
else if(xhr.readyState === XHR.HEADERS_RECEIVED)
on_response()
else if(xhr.readyState === XHR.LOADING) {
on_response()
on_loading()
}
else if(xhr.readyState === XHR.DONE) {
on_response()
on_loading()
on_end()
}
}
function on_response() {
if(did.response)
return
did.response = true
request.log.debug('Got response', {'id':xhr.id, 'status':xhr.status})
clearTimeout(xhr.timeoutTimer)
xhr.statusCode = xhr.status // Node request compatibility
// Detect failed CORS requests.
if(is_cors && xhr.statusCode == 0) {
var cors_err = new Error('CORS request rejected: ' + options.uri)
cors_err.cors = 'rejected'
// Do not process this request further.
did.loading = true
did.end = true
return options.callback(cors_err, xhr)
}
options.onResponse(null, xhr)
}
function on_loading() {
if(did.loading)
return
did.loading = true
request.log.debug('Response body loading', {'id':xhr.id})
// TODO: Maybe simulate "data" events by watching xhr.responseText
}
function on_end() {
if(did.end)
return
did.end = true
request.log.debug('Request done', {'id':xhr.id})
if(options.encoding === null) {
xhr.body = new Buffer(new Uint8Array(xhr.response));
} else {
xhr.body = xhr.responseText
if(options.json) {
try { xhr.body = JSON.parse(xhr.responseText) }
catch (er) { return options.callback(er, xhr) }
}
}
options.callback(null, xhr, xhr.body)
}
} // request
request.withCredentials = false;
request.DEFAULT_TIMEOUT = DEFAULT_TIMEOUT;
//
// defaults
//
request.defaults = function(options, requester) {
var def = function (method) {
var d = function (params, callback) {
if(typeof params === 'string')
params = {'uri': params};
else {
params = JSON.parse(JSON.stringify(params));
}
for (var i in options) {
if (params[i] === undefined) params[i] = options[i]
}
return method(params, callback)
}
return d
}
var de = def(request)
de.get = def(request.get)
de.post = def(request.post)
de.put = def(request.put)
de.head = def(request.head)
return de
}
//
// HTTP method shortcuts
//
var shortcuts = [ 'get', 'put', 'post', 'head' ];
shortcuts.forEach(function(shortcut) {
var method = shortcut.toUpperCase();
var func = shortcut.toLowerCase();
request[func] = function(opts) {
if(typeof opts === 'string')
opts = {'method':method, 'uri':opts};
else {
opts = JSON.parse(JSON.stringify(opts));
opts.method = method;
}
var args = [opts].concat(Array.prototype.slice.apply(arguments, [1]));
return request.apply(this, args);
}
})
//
// CouchDB shortcut
//
request.couch = function(options, callback) {
if(typeof options === 'string')
options = {'uri':options}
// Just use the request API to do JSON.
options.json = true
if(options.body)
options.json = options.body
delete options.body
callback = callback || noop
var xhr = request(options, couch_handler)
return xhr
function couch_handler(er, resp, body) {
if(er)
return callback(er, resp, body)
if((resp.statusCode < 200 || resp.statusCode > 299) && body.error) {
// The body is a Couch JSON object indicating the error.
er = new Error('CouchDB error: ' + (body.error.reason || body.error.error))
for (var key in body)
er[key] = body[key]
return callback(er, resp, body);
}
return callback(er, resp, body);
}
}
//
// Utility
//
function noop() {}
function getLogger() {
var logger = {}
, levels = ['trace', 'debug', 'info', 'warn', 'error']
, level, i
for(i = 0; i < levels.length; i++) {
level = levels[i]
logger[level] = noop
if(typeof console !== 'undefined' && console && console[level])
logger[level] = formatted(console, level)
}
return logger
}
function formatted(obj, method) {
return formatted_logger
function formatted_logger(str, context) {
if(typeof context === 'object')
str += ' ' + JSON.stringify(context)
return obj[method].call(obj, str)
}
}
// Return whether a URL is a cross-domain request.
function is_crossDomain(url) {
var rurl = /^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+))?)?/
// jQuery #8138, IE may throw an exception when accessing
// a field from window.location if document.domain has been set
var ajaxLocation
try { ajaxLocation = location.href }
catch (e) {
// Use the href attribute of an A element since IE will modify it given document.location
ajaxLocation = document.createElement( "a" );
ajaxLocation.href = "";
ajaxLocation = ajaxLocation.href;
}
var ajaxLocParts = rurl.exec(ajaxLocation.toLowerCase()) || []
, parts = rurl.exec(url.toLowerCase() )
var result = !!(
parts &&
( parts[1] != ajaxLocParts[1]
|| parts[2] != ajaxLocParts[2]
|| (parts[3] || (parts[1] === "http:" ? 80 : 443)) != (ajaxLocParts[3] || (ajaxLocParts[1] === "http:" ? 80 : 443))
)
)
//console.debug('is_crossDomain('+url+') -> ' + result)
return result
}
// MIT License from http://phpjs.org/functions/base64_encode:358
function b64_enc (data) {
// Encodes string using MIME base64 algorithm
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, enc="", tmp_arr = [];
if (!data) {
return data;
}
// assume utf8 data
// data = this.utf8_encode(data+'');
do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);
bits = o1<<16 | o2<<8 | o3;
h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);
enc = tmp_arr.join('');
switch (data.length % 3) {
case 1:
enc = enc.slice(0, -2) + '==';
break;
case 2:
enc = enc.slice(0, -1) + '=';
break;
}
return enc;
}
module.exports = request;
}).call(this,_dereq_("buffer").Buffer)
2014-08-17 05:56:03 +00:00
},{"buffer":7}],7:[function(_dereq_,module,exports){
2014-06-10 18:53:04 +00:00
/*!
* The buffer module from node.js, for the browser.
*
* @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
* @license MIT
*/
var base64 = _dereq_('base64-js')
var ieee754 = _dereq_('ieee754')
exports.Buffer = Buffer
exports.SlowBuffer = Buffer
exports.INSPECT_MAX_BYTES = 50
Buffer.poolSize = 8192
/**
* If `Buffer._useTypedArrays`:
* === true Use Uint8Array implementation (fastest)
* === false Use Object implementation (compatible down to IE6)
*/
Buffer._useTypedArrays = (function () {
// Detect if browser supports Typed Arrays. Supported browsers are IE 10+, Firefox 4+,
// Chrome 7+, Safari 5.1+, Opera 11.6+, iOS 4.2+. If the browser does not support adding
// properties to `Uint8Array` instances, then that's the same as no `Uint8Array` support
// because we need to be able to add all the node Buffer API methods. This is an issue
// in Firefox 4-29. Now fixed: https://bugzilla.mozilla.org/show_bug.cgi?id=695438
try {
var buf = new ArrayBuffer(0)
var arr = new Uint8Array(buf)
arr.foo = function () { return 42 }
return 42 === arr.foo() &&
typeof arr.subarray === 'function' // Chrome 9-10 lack `subarray`
} catch (e) {
return false
}
})()
/**
* Class: Buffer
* =============
*
* The Buffer constructor returns instances of `Uint8Array` that are augmented
* with function properties for all the node `Buffer` API functions. We use
* `Uint8Array` so that square bracket notation works as expected -- it returns
* a single octet.
*
* By augmenting the instances, we can avoid modifying the `Uint8Array`
* prototype.
*/
function Buffer (subject, encoding, noZero) {
if (!(this instanceof Buffer))
return new Buffer(subject, encoding, noZero)
var type = typeof subject
if (encoding === 'base64' && type === 'string') {
2014-07-14 18:33:22 +00:00
subject = base64clean(subject)
2014-06-10 18:53:04 +00:00
}
// Find the length
var length
if (type === 'number')
length = coerce(subject)
else if (type === 'string')
length = Buffer.byteLength(subject, encoding)
else if (type === 'object')
length = coerce(subject.length) // assume that object is array-like
else
throw new Error('First argument needs to be a number, array or string.')
var buf
if (Buffer._useTypedArrays) {
// Preferred: Return an augmented `Uint8Array` instance for best performance
buf = Buffer._augment(new Uint8Array(length))
} else {
// Fallback: Return THIS instance of Buffer (created by `new`)
buf = this
buf.length = length
buf._isBuffer = true
}
var i
if (Buffer._useTypedArrays && typeof subject.byteLength === 'number') {
// Speed optimization -- use set if we're copying from a typed array
buf._set(subject)
} else if (isArrayish(subject)) {
// Treat array-ish objects as a byte array
2014-07-14 18:33:22 +00:00
if (Buffer.isBuffer(subject)) {
for (i = 0; i < length; i++)
2014-06-10 18:53:04 +00:00
buf[i] = subject.readUInt8(i)
2014-07-14 18:33:22 +00:00
} else {
for (i = 0; i < length; i++)
buf[i] = ((subject[i] % 256) + 256) % 256
2014-06-10 18:53:04 +00:00
}
} else if (type === 'string') {
buf.write(subject, 0, encoding)
} else if (type === 'number' && !Buffer._useTypedArrays && !noZero) {
for (i = 0; i < length; i++) {
buf[i] = 0
}
}
return buf
}
// STATIC METHODS
// ==============
Buffer.isEncoding = function (encoding) {
switch (String(encoding).toLowerCase()) {
case 'hex':
case 'utf8':
case 'utf-8':
case 'ascii':
case 'binary':
case 'base64':
case 'raw':
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
return true
default:
return false
}
}
Buffer.isBuffer = function (b) {
return !!(b !== null && b !== undefined && b._isBuffer)
}
Buffer.byteLength = function (str, encoding) {
var ret
str = str.toString()
switch (encoding || 'utf8') {
case 'hex':
ret = str.length / 2
break
case 'utf8':
case 'utf-8':
ret = utf8ToBytes(str).length
break
case 'ascii':
case 'binary':
case 'raw':
ret = str.length
break
case 'base64':
ret = base64ToBytes(str).length
break
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = str.length * 2
break
default:
throw new Error('Unknown encoding')
}
return ret
}
Buffer.concat = function (list, totalLength) {
assert(isArray(list), 'Usage: Buffer.concat(list[, length])')
if (list.length === 0) {
return new Buffer(0)
} else if (list.length === 1) {
return list[0]
}
var i
if (totalLength === undefined) {
totalLength = 0
for (i = 0; i < list.length; i++) {
totalLength += list[i].length
}
}
var buf = new Buffer(totalLength)
var pos = 0
for (i = 0; i < list.length; i++) {
var item = list[i]
item.copy(buf, pos)
pos += item.length
}
return buf
}
Buffer.compare = function (a, b) {
assert(Buffer.isBuffer(a) && Buffer.isBuffer(b), 'Arguments must be Buffers')
var x = a.length
var y = b.length
for (var i = 0, len = Math.min(x, y); i < len && a[i] === b[i]; i++) {}
if (i !== len) {
x = a[i]
y = b[i]
}
if (x < y) {
return -1
}
if (y < x) {
return 1
}
return 0
}
// BUFFER INSTANCE METHODS
// =======================
function hexWrite (buf, string, offset, length) {
offset = Number(offset) || 0
var remaining = buf.length - offset
if (!length) {
length = remaining
} else {
length = Number(length)
if (length > remaining) {
length = remaining
}
}
// must be an even number of digits
var strLen = string.length
assert(strLen % 2 === 0, 'Invalid hex string')
if (length > strLen / 2) {
length = strLen / 2
}
for (var i = 0; i < length; i++) {
var byte = parseInt(string.substr(i * 2, 2), 16)
assert(!isNaN(byte), 'Invalid hex string')
buf[offset + i] = byte
}
return i
}
function utf8Write (buf, string, offset, length) {
var charsWritten = blitBuffer(utf8ToBytes(string), buf, offset, length)
return charsWritten
}
function asciiWrite (buf, string, offset, length) {
var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length)
return charsWritten
}
function binaryWrite (buf, string, offset, length) {
return asciiWrite(buf, string, offset, length)
}
function base64Write (buf, string, offset, length) {
var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length)
return charsWritten
}
function utf16leWrite (buf, string, offset, length) {
var charsWritten = blitBuffer(utf16leToBytes(string), buf, offset, length)
return charsWritten
}
Buffer.prototype.write = function (string, offset, length, encoding) {
// Support both (string, offset, length, encoding)
// and the legacy (string, encoding, offset, length)
if (isFinite(offset)) {
if (!isFinite(length)) {
encoding = length
length = undefined
}
} else { // legacy
var swap = encoding
encoding = offset
offset = length
length = swap
}
offset = Number(offset) || 0
var remaining = this.length - offset
if (!length) {
length = remaining
} else {
length = Number(length)
if (length > remaining) {
length = remaining
}
}
encoding = String(encoding || 'utf8').toLowerCase()
var ret
switch (encoding) {
case 'hex':
ret = hexWrite(this, string, offset, length)
break
case 'utf8':
case 'utf-8':
ret = utf8Write(this, string, offset, length)
break
case 'ascii':
ret = asciiWrite(this, string, offset, length)
break
case 'binary':
ret = binaryWrite(this, string, offset, length)
break
case 'base64':
ret = base64Write(this, string, offset, length)
break
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = utf16leWrite(this, string, offset, length)
break
default:
throw new Error('Unknown encoding')
}
return ret
}
Buffer.prototype.toString = function (encoding, start, end) {
var self = this
encoding = String(encoding || 'utf8').toLowerCase()
start = Number(start) || 0
end = (end === undefined) ? self.length : Number(end)
// Fastpath empty strings
if (end === start)
return ''
var ret
switch (encoding) {
case 'hex':
ret = hexSlice(self, start, end)
break
case 'utf8':
case 'utf-8':
ret = utf8Slice(self, start, end)
break
case 'ascii':
ret = asciiSlice(self, start, end)
break
case 'binary':
ret = binarySlice(self, start, end)
break
case 'base64':
ret = base64Slice(self, start, end)
break
case 'ucs2':
case 'ucs-2':
case 'utf16le':
case 'utf-16le':
ret = utf16leSlice(self, start, end)
break
default:
throw new Error('Unknown encoding')
}
return ret
}
Buffer.prototype.toJSON = function () {
return {
type: 'Buffer',
data: Array.prototype.slice.call(this._arr || this, 0)
}
}
Buffer.prototype.equals = function (b) {
assert(Buffer.isBuffer(b), 'Argument must be a Buffer')
return Buffer.compare(this, b) === 0
}
Buffer.prototype.compare = function (b) {
assert(Buffer.isBuffer(b), 'Argument must be a Buffer')
return Buffer.compare(this, b)
}
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
Buffer.prototype.copy = function (target, target_start, start, end) {
var source = this
if (!start) start = 0
if (!end && end !== 0) end = this.length
if (!target_start) target_start = 0
// Copy 0 bytes; we're done
if (end === start) return
if (target.length === 0 || source.length === 0) return
// Fatal error conditions
assert(end >= start, 'sourceEnd < sourceStart')
assert(target_start >= 0 && target_start < target.length,
'targetStart out of bounds')
assert(start >= 0 && start < source.length, 'sourceStart out of bounds')
assert(end >= 0 && end <= source.length, 'sourceEnd out of bounds')
// Are we oob?
if (end > this.length)
end = this.length
if (target.length - target_start < end - start)
end = target.length - target_start + start
var len = end - start
if (len < 100 || !Buffer._useTypedArrays) {
for (var i = 0; i < len; i++) {
target[i + target_start] = this[i + start]
}
} else {
target._set(this.subarray(start, start + len), target_start)
}
}
function base64Slice (buf, start, end) {
if (start === 0 && end === buf.length) {
return base64.fromByteArray(buf)
} else {
return base64.fromByteArray(buf.slice(start, end))
}
}
function utf8Slice (buf, start, end) {
var res = ''
var tmp = ''
end = Math.min(buf.length, end)
for (var i = start; i < end; i++) {
if (buf[i] <= 0x7F) {
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
tmp = ''
} else {
tmp += '%' + buf[i].toString(16)
}
}
return res + decodeUtf8Char(tmp)
}
function asciiSlice (buf, start, end) {
var ret = ''
end = Math.min(buf.length, end)
for (var i = start; i < end; i++) {
ret += String.fromCharCode(buf[i])
}
return ret
}
function binarySlice (buf, start, end) {
return asciiSlice(buf, start, end)
}
function hexSlice (buf, start, end) {
var len = buf.length
if (!start || start < 0) start = 0
if (!end || end < 0 || end > len) end = len
var out = ''
for (var i = start; i < end; i++) {
out += toHex(buf[i])
}
return out
}
function utf16leSlice (buf, start, end) {
var bytes = buf.slice(start, end)
var res = ''
for (var i = 0; i < bytes.length; i += 2) {
res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
}
return res
}
Buffer.prototype.slice = function (start, end) {
var len = this.length
start = clamp(start, len, 0)
end = clamp(end, len, len)
if (Buffer._useTypedArrays) {
return Buffer._augment(this.subarray(start, end))
} else {
var sliceLen = end - start
var newBuf = new Buffer(sliceLen, undefined, true)
for (var i = 0; i < sliceLen; i++) {
newBuf[i] = this[i + start]
}
return newBuf
}
}
// `get` will be removed in Node 0.13+
Buffer.prototype.get = function (offset) {
console.log('.get() is deprecated. Access using array indexes instead.')
return this.readUInt8(offset)
}
// `set` will be removed in Node 0.13+
Buffer.prototype.set = function (v, offset) {
console.log('.set() is deprecated. Access using array indexes instead.')
return this.writeUInt8(v, offset)
}
Buffer.prototype.readUInt8 = function (offset, noAssert) {
if (!noAssert) {
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset < this.length, 'Trying to read beyond buffer length')
}
if (offset >= this.length)
return
return this[offset]
}
function readUInt16 (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 1 < buf.length, 'Trying to read beyond buffer length')
}
var len = buf.length
if (offset >= len)
return
var val
if (littleEndian) {
val = buf[offset]
if (offset + 1 < len)
val |= buf[offset + 1] << 8
} else {
val = buf[offset] << 8
if (offset + 1 < len)
val |= buf[offset + 1]
}
return val
}
Buffer.prototype.readUInt16LE = function (offset, noAssert) {
return readUInt16(this, offset, true, noAssert)
}
Buffer.prototype.readUInt16BE = function (offset, noAssert) {
return readUInt16(this, offset, false, noAssert)
}
function readUInt32 (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 3 < buf.length, 'Trying to read beyond buffer length')
}
var len = buf.length
if (offset >= len)
return
var val
if (littleEndian) {
if (offset + 2 < len)
val = buf[offset + 2] << 16
if (offset + 1 < len)
val |= buf[offset + 1] << 8
val |= buf[offset]
if (offset + 3 < len)
val = val + (buf[offset + 3] << 24 >>> 0)
} else {
if (offset + 1 < len)
val = buf[offset + 1] << 16
if (offset + 2 < len)
val |= buf[offset + 2] << 8
if (offset + 3 < len)
val |= buf[offset + 3]
val = val + (buf[offset] << 24 >>> 0)
}
return val
}
Buffer.prototype.readUInt32LE = function (offset, noAssert) {
return readUInt32(this, offset, true, noAssert)
}
Buffer.prototype.readUInt32BE = function (offset, noAssert) {
return readUInt32(this, offset, false, noAssert)
}
Buffer.prototype.readInt8 = function (offset, noAssert) {
if (!noAssert) {
assert(offset !== undefined && offset !== null,
'missing offset')
assert(offset < this.length, 'Trying to read beyond buffer length')
}
if (offset >= this.length)
return
var neg = this[offset] & 0x80
if (neg)
return (0xff - this[offset] + 1) * -1
else
return this[offset]
}
function readInt16 (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 1 < buf.length, 'Trying to read beyond buffer length')
}
var len = buf.length
if (offset >= len)
return
var val = readUInt16(buf, offset, littleEndian, true)
var neg = val & 0x8000
if (neg)
return (0xffff - val + 1) * -1
else
return val
}
Buffer.prototype.readInt16LE = function (offset, noAssert) {
return readInt16(this, offset, true, noAssert)
}
Buffer.prototype.readInt16BE = function (offset, noAssert) {
return readInt16(this, offset, false, noAssert)
}
function readInt32 (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 3 < buf.length, 'Trying to read beyond buffer length')
}
var len = buf.length
if (offset >= len)
return
var val = readUInt32(buf, offset, littleEndian, true)
var neg = val & 0x80000000
if (neg)
return (0xffffffff - val + 1) * -1
else
return val
}
Buffer.prototype.readInt32LE = function (offset, noAssert) {
return readInt32(this, offset, true, noAssert)
}
Buffer.prototype.readInt32BE = function (offset, noAssert) {
return readInt32(this, offset, false, noAssert)
}
function readFloat (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset + 3 < buf.length, 'Trying to read beyond buffer length')
}
return ieee754.read(buf, offset, littleEndian, 23, 4)
}
Buffer.prototype.readFloatLE = function (offset, noAssert) {
return readFloat(this, offset, true, noAssert)
}
Buffer.prototype.readFloatBE = function (offset, noAssert) {
return readFloat(this, offset, false, noAssert)
}
function readDouble (buf, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset + 7 < buf.length, 'Trying to read beyond buffer length')
}
return ieee754.read(buf, offset, littleEndian, 52, 8)
}
Buffer.prototype.readDoubleLE = function (offset, noAssert) {
return readDouble(this, offset, true, noAssert)
}
Buffer.prototype.readDoubleBE = function (offset, noAssert) {
return readDouble(this, offset, false, noAssert)
}
Buffer.prototype.writeUInt8 = function (value, offset, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset < this.length, 'trying to write beyond buffer length')
verifuint(value, 0xff)
}
if (offset >= this.length) return
this[offset] = value
return offset + 1
}
function writeUInt16 (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 1 < buf.length, 'trying to write beyond buffer length')
verifuint(value, 0xffff)
}
var len = buf.length
if (offset >= len)
return
for (var i = 0, j = Math.min(len - offset, 2); i < j; i++) {
buf[offset + i] =
(value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
(littleEndian ? i : 1 - i) * 8
}
return offset + 2
}
Buffer.prototype.writeUInt16LE = function (value, offset, noAssert) {
return writeUInt16(this, value, offset, true, noAssert)
}
Buffer.prototype.writeUInt16BE = function (value, offset, noAssert) {
return writeUInt16(this, value, offset, false, noAssert)
}
function writeUInt32 (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 3 < buf.length, 'trying to write beyond buffer length')
verifuint(value, 0xffffffff)
}
var len = buf.length
if (offset >= len)
return
for (var i = 0, j = Math.min(len - offset, 4); i < j; i++) {
buf[offset + i] =
(value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
}
return offset + 4
}
Buffer.prototype.writeUInt32LE = function (value, offset, noAssert) {
return writeUInt32(this, value, offset, true, noAssert)
}
Buffer.prototype.writeUInt32BE = function (value, offset, noAssert) {
return writeUInt32(this, value, offset, false, noAssert)
}
Buffer.prototype.writeInt8 = function (value, offset, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset < this.length, 'Trying to write beyond buffer length')
verifsint(value, 0x7f, -0x80)
}
if (offset >= this.length)
return
if (value >= 0)
this.writeUInt8(value, offset, noAssert)
else
this.writeUInt8(0xff + value + 1, offset, noAssert)
return offset + 1
}
function writeInt16 (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 1 < buf.length, 'Trying to write beyond buffer length')
verifsint(value, 0x7fff, -0x8000)
}
var len = buf.length
if (offset >= len)
return
if (value >= 0)
writeUInt16(buf, value, offset, littleEndian, noAssert)
else
writeUInt16(buf, 0xffff + value + 1, offset, littleEndian, noAssert)
return offset + 2
}
Buffer.prototype.writeInt16LE = function (value, offset, noAssert) {
return writeInt16(this, value, offset, true, noAssert)
}
Buffer.prototype.writeInt16BE = function (value, offset, noAssert) {
return writeInt16(this, value, offset, false, noAssert)
}
function writeInt32 (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 3 < buf.length, 'Trying to write beyond buffer length')
verifsint(value, 0x7fffffff, -0x80000000)
}
var len = buf.length
if (offset >= len)
return
if (value >= 0)
writeUInt32(buf, value, offset, littleEndian, noAssert)
else
writeUInt32(buf, 0xffffffff + value + 1, offset, littleEndian, noAssert)
return offset + 4
}
Buffer.prototype.writeInt32LE = function (value, offset, noAssert) {
return writeInt32(this, value, offset, true, noAssert)
}
Buffer.prototype.writeInt32BE = function (value, offset, noAssert) {
return writeInt32(this, value, offset, false, noAssert)
}
function writeFloat (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 3 < buf.length, 'Trying to write beyond buffer length')
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38)
}
var len = buf.length
if (offset >= len)
return
ieee754.write(buf, value, offset, littleEndian, 23, 4)
return offset + 4
}
Buffer.prototype.writeFloatLE = function (value, offset, noAssert) {
return writeFloat(this, value, offset, true, noAssert)
}
Buffer.prototype.writeFloatBE = function (value, offset, noAssert) {
return writeFloat(this, value, offset, false, noAssert)
}
function writeDouble (buf, value, offset, littleEndian, noAssert) {
if (!noAssert) {
assert(value !== undefined && value !== null, 'missing value')
assert(typeof littleEndian === 'boolean', 'missing or invalid endian')
assert(offset !== undefined && offset !== null, 'missing offset')
assert(offset + 7 < buf.length,
'Trying to write beyond buffer length')
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308)
}
var len = buf.length
if (offset >= len)
return
ieee754.write(buf, value, offset, littleEndian, 52, 8)
return offset + 8
}
Buffer.prototype.writeDoubleLE = function (value, offset, noAssert) {
return writeDouble(this, value, offset, true, noAssert)
}
Buffer.prototype.writeDoubleBE = function (value, offset, noAssert) {
return writeDouble(this, value, offset, false, noAssert)
}
// fill(value, start=0, end=buffer.length)
Buffer.prototype.fill = function (value, start, end) {
if (!value) value = 0
if (!start) start = 0
if (!end) end = this.length
assert(end >= start, 'end < start')
// Fill 0 bytes; we're done
if (end === start) return
if (this.length === 0) return
assert(start >= 0 && start < this.length, 'start out of bounds')
assert(end >= 0 && end <= this.length, 'end out of bounds')
var i
if (typeof value === 'number') {
for (i = start; i < end; i++) {
this[i] = value
}
} else {
var bytes = utf8ToBytes(value.toString())
var len = bytes.length
for (i = start; i < end; i++) {
this[i] = bytes[i % len]
}
}
return this
}
Buffer.prototype.inspect = function () {
var out = []
var len = this.length
for (var i = 0; i < len; i++) {
out[i] = toHex(this[i])
if (i === exports.INSPECT_MAX_BYTES) {
out[i + 1] = '...'
break
}
}
return '<Buffer ' + out.join(' ') + '>'
}
/**
* Creates a new `ArrayBuffer` with the *copied* memory of the buffer instance.
* Added in Node 0.12. Only available in browsers that support ArrayBuffer.
*/
Buffer.prototype.toArrayBuffer = function () {
if (typeof Uint8Array !== 'undefined') {
if (Buffer._useTypedArrays) {
return (new Buffer(this)).buffer
} else {
var buf = new Uint8Array(this.length)
for (var i = 0, len = buf.length; i < len; i += 1) {
buf[i] = this[i]
}
return buf.buffer
}
} else {
throw new Error('Buffer.toArrayBuffer not supported in this browser')
}
}
// HELPER FUNCTIONS
// ================
var BP = Buffer.prototype
/**
* Augment a Uint8Array *instance* (not the Uint8Array class!) with Buffer methods
*/
Buffer._augment = function (arr) {
arr._isBuffer = true
// save reference to original Uint8Array get/set methods before overwriting
arr._get = arr.get
arr._set = arr.set
// deprecated, will be removed in node 0.13+
arr.get = BP.get
arr.set = BP.set
arr.write = BP.write
arr.toString = BP.toString
arr.toLocaleString = BP.toString
arr.toJSON = BP.toJSON
arr.equals = BP.equals
arr.compare = BP.compare
arr.copy = BP.copy
arr.slice = BP.slice
arr.readUInt8 = BP.readUInt8
arr.readUInt16LE = BP.readUInt16LE
arr.readUInt16BE = BP.readUInt16BE
arr.readUInt32LE = BP.readUInt32LE
arr.readUInt32BE = BP.readUInt32BE
arr.readInt8 = BP.readInt8
arr.readInt16LE = BP.readInt16LE
arr.readInt16BE = BP.readInt16BE
arr.readInt32LE = BP.readInt32LE
arr.readInt32BE = BP.readInt32BE
arr.readFloatLE = BP.readFloatLE
arr.readFloatBE = BP.readFloatBE
arr.readDoubleLE = BP.readDoubleLE
arr.readDoubleBE = BP.readDoubleBE
arr.writeUInt8 = BP.writeUInt8
arr.writeUInt16LE = BP.writeUInt16LE
arr.writeUInt16BE = BP.writeUInt16BE
arr.writeUInt32LE = BP.writeUInt32LE
arr.writeUInt32BE = BP.writeUInt32BE
arr.writeInt8 = BP.writeInt8
arr.writeInt16LE = BP.writeInt16LE
arr.writeInt16BE = BP.writeInt16BE
arr.writeInt32LE = BP.writeInt32LE
arr.writeInt32BE = BP.writeInt32BE
arr.writeFloatLE = BP.writeFloatLE
arr.writeFloatBE = BP.writeFloatBE
arr.writeDoubleLE = BP.writeDoubleLE
arr.writeDoubleBE = BP.writeDoubleBE
arr.fill = BP.fill
arr.inspect = BP.inspect
arr.toArrayBuffer = BP.toArrayBuffer
return arr
}
2014-07-14 18:33:22 +00:00
var INVALID_BASE64_RE = /[^+\/0-9A-z]/g
function base64clean (str) {
// Node strips out invalid characters like \n and \t from the string, base64-js does not
str = stringtrim(str).replace(INVALID_BASE64_RE, '')
// Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
while (str.length % 4 !== 0) {
str = str + '='
}
return str
}
2014-06-10 18:53:04 +00:00
function stringtrim (str) {
if (str.trim) return str.trim()
return str.replace(/^\s+|\s+$/g, '')
}
// slice(start, end)
function clamp (index, len, defaultValue) {
if (typeof index !== 'number') return defaultValue
index = ~~index; // Coerce to integer.
if (index >= len) return len
if (index >= 0) return index
index += len
if (index >= 0) return index
return 0
}
function coerce (length) {
// Coerce length to a number (possibly NaN), round up
// in case it's fractional (e.g. 123.456) then do a
// double negate to coerce a NaN to 0. Easy, right?
length = ~~Math.ceil(+length)
return length < 0 ? 0 : length
}
function isArray (subject) {
return (Array.isArray || function (subject) {
return Object.prototype.toString.call(subject) === '[object Array]'
})(subject)
}
function isArrayish (subject) {
return isArray(subject) || Buffer.isBuffer(subject) ||
subject && typeof subject === 'object' &&
typeof subject.length === 'number'
}
function toHex (n) {
if (n < 16) return '0' + n.toString(16)
return n.toString(16)
}
function utf8ToBytes (str) {
var byteArray = []
for (var i = 0; i < str.length; i++) {
var b = str.charCodeAt(i)
if (b <= 0x7F) {
byteArray.push(b)
} else {
var start = i
if (b >= 0xD800 && b <= 0xDFFF) i++
var h = encodeURIComponent(str.slice(start, i+1)).substr(1).split('%')
for (var j = 0; j < h.length; j++) {
byteArray.push(parseInt(h[j], 16))
}
}
}
return byteArray
}
function asciiToBytes (str) {
var byteArray = []
for (var i = 0; i < str.length; i++) {
// Node's code seems to be doing this and not & 0x7F..
byteArray.push(str.charCodeAt(i) & 0xFF)
}
return byteArray
}
function utf16leToBytes (str) {
var c, hi, lo
var byteArray = []
for (var i = 0; i < str.length; i++) {
c = str.charCodeAt(i)
hi = c >> 8
lo = c % 256
byteArray.push(lo)
byteArray.push(hi)
}
return byteArray
}
function base64ToBytes (str) {
return base64.toByteArray(str)
}
function blitBuffer (src, dst, offset, length) {
for (var i = 0; i < length; i++) {
if ((i + offset >= dst.length) || (i >= src.length))
break
dst[i + offset] = src[i]
}
return i
}
function decodeUtf8Char (str) {
try {
return decodeURIComponent(str)
} catch (err) {
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
}
}
/*
* We have to make sure that the value is a valid integer. This means that it
* is non-negative. It has no fractional component and that it does not
* exceed the maximum allowed value.
*/
function verifuint (value, max) {
assert(typeof value === 'number', 'cannot write a non-number as a number')
assert(value >= 0, 'specified a negative value for writing an unsigned value')
assert(value <= max, 'value is larger than maximum value for type')
assert(Math.floor(value) === value, 'value has a fractional component')
}
function verifsint (value, max, min) {
assert(typeof value === 'number', 'cannot write a non-number as a number')
assert(value <= max, 'value larger than maximum allowed value')
assert(value >= min, 'value smaller than minimum allowed value')
assert(Math.floor(value) === value, 'value has a fractional component')
}
function verifIEEE754 (value, max, min) {
assert(typeof value === 'number', 'cannot write a non-number as a number')
assert(value <= max, 'value larger than maximum allowed value')
assert(value >= min, 'value smaller than minimum allowed value')
}
function assert (test, message) {
if (!test) throw new Error(message || 'Failed assertion')
}
2014-08-17 05:56:03 +00:00
},{"base64-js":8,"ieee754":9}],8:[function(_dereq_,module,exports){
2014-06-10 18:53:04 +00:00
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
;(function (exports) {
'use strict';
var Arr = (typeof Uint8Array !== 'undefined')
? Uint8Array
: Array
var PLUS = '+'.charCodeAt(0)
var SLASH = '/'.charCodeAt(0)
var NUMBER = '0'.charCodeAt(0)
var LOWER = 'a'.charCodeAt(0)
var UPPER = 'A'.charCodeAt(0)
function decode (elt) {
var code = elt.charCodeAt(0)
if (code === PLUS)
return 62 // '+'
if (code === SLASH)
return 63 // '/'
if (code < NUMBER)
return -1 //no match
if (code < NUMBER + 10)
return code - NUMBER + 26 + 26
if (code < UPPER + 26)
return code - UPPER
if (code < LOWER + 26)
return code - LOWER + 26
}
function b64ToByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
if (b64.length % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
}
// the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
var len = b64.length
placeHolders = '=' === b64.charAt(len - 2) ? 2 : '=' === b64.charAt(len - 1) ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(b64.length * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? b64.length - 4 : b64.length
var L = 0
function push (v) {
arr[L++] = v
}
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
push((tmp & 0xFF0000) >> 16)
push((tmp & 0xFF00) >> 8)
push(tmp & 0xFF)
}
if (placeHolders === 2) {
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
push(tmp & 0xFF)
} else if (placeHolders === 1) {
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
push((tmp >> 8) & 0xFF)
push(tmp & 0xFF)
}
return arr
}
function uint8ToBase64 (uint8) {
var i,
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
output = "",
temp, length
function encode (num) {
return lookup.charAt(num)
}
function tripletToBase64 (num) {
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
}
// go through the array every three bytes, we'll deal with trailing stuff later
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output += tripletToBase64(temp)
}
// pad the end with zeros, but make sure to not forget the extra bytes
switch (extraBytes) {
case 1:
temp = uint8[uint8.length - 1]
output += encode(temp >> 2)
output += encode((temp << 4) & 0x3F)
output += '=='
break
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
output += encode(temp >> 10)
output += encode((temp >> 4) & 0x3F)
output += encode((temp << 2) & 0x3F)
output += '='
break
}
return output
}
2014-07-14 18:33:22 +00:00
exports.toByteArray = b64ToByteArray
exports.fromByteArray = uint8ToBase64
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports))
2014-06-10 18:53:04 +00:00
2014-08-17 05:56:03 +00:00
},{}],9:[function(_dereq_,module,exports){
2014-06-10 18:53:04 +00:00
exports.read = function(buffer, offset, isLE, mLen, nBytes) {
var e, m,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
nBits = -7,
i = isLE ? (nBytes - 1) : 0,
d = isLE ? -1 : 1,
s = buffer[offset + i];
i += d;
e = s & ((1 << (-nBits)) - 1);
s >>= (-nBits);
nBits += eLen;
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
m = e & ((1 << (-nBits)) - 1);
e >>= (-nBits);
nBits += mLen;
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
if (e === 0) {
e = 1 - eBias;
} else if (e === eMax) {
return m ? NaN : ((s ? -1 : 1) * Infinity);
} else {
m = m + Math.pow(2, mLen);
e = e - eBias;
}
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
};
exports.write = function(buffer, value, offset, isLE, mLen, nBytes) {
var e, m, c,
eLen = nBytes * 8 - mLen - 1,
eMax = (1 << eLen) - 1,
eBias = eMax >> 1,
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
i = isLE ? 0 : (nBytes - 1),
d = isLE ? 1 : -1,
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
value = Math.abs(value);
if (isNaN(value) || value === Infinity) {
m = isNaN(value) ? 1 : 0;
e = eMax;
} else {
e = Math.floor(Math.log(value) / Math.LN2);
if (value * (c = Math.pow(2, -e)) < 1) {
e--;
c *= 2;
}
if (e + eBias >= 1) {
value += rt / c;
} else {
value += rt * Math.pow(2, 1 - eBias);
}
if (value * c >= 2) {
e++;
c /= 2;
}
if (e + eBias >= eMax) {
m = 0;
e = eMax;
} else if (e + eBias >= 1) {
m = (value * c - 1) * Math.pow(2, mLen);
e = e + eBias;
} else {
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
e = 0;
}
}
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
e = (e << mLen) | m;
eLen += mLen;
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
buffer[offset + i - d] |= s * 128;
};
2014-08-17 05:56:03 +00:00
},{}],10:[function(_dereq_,module,exports){
2014-06-09 14:40:50 +00:00
// shim for using process in browser
var process = module.exports = {};
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
}
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
}
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
function noop() {}
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
process.binding = function (name) {
throw new Error('process.binding is not supported');
}
// TODO(shtylman)
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
2014-08-17 05:56:03 +00:00
},{}],11:[function(_dereq_,module,exports){
(function (Buffer){
function FilerBuffer (subject, encoding, nonZero) {
// Automatically turn ArrayBuffer into Uint8Array so that underlying
// Buffer code doesn't just throw away and ignore ArrayBuffer data.
if (subject instanceof ArrayBuffer) {
subject = new Uint8Array(subject);
}
return new Buffer(subject, encoding, nonZero);
};
// Inherit prototype from Buffer
FilerBuffer.prototype = Object.create(Buffer.prototype);
FilerBuffer.prototype.constructor = FilerBuffer;
// Also copy static methods onto FilerBuffer ctor
Object.keys(Buffer).forEach(function (p) {
if (Buffer.hasOwnProperty(p)) {
FilerBuffer[p] = Buffer[p];
}
});
module.exports = FilerBuffer;
}).call(this,_dereq_("buffer").Buffer)
2014-09-02 19:48:54 +00:00
},{"buffer":7}],12:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
var O_READ = 'READ';
var O_WRITE = 'WRITE';
var O_CREATE = 'CREATE';
var O_EXCLUSIVE = 'EXCLUSIVE';
var O_TRUNCATE = 'TRUNCATE';
var O_APPEND = 'APPEND';
var XATTR_CREATE = 'CREATE';
var XATTR_REPLACE = 'REPLACE';
module.exports = {
FILE_SYSTEM_NAME: 'local',
FILE_STORE_NAME: 'files',
IDB_RO: 'readonly',
IDB_RW: 'readwrite',
WSQL_VERSION: "1",
WSQL_SIZE: 5 * 1024 * 1024,
WSQL_DESC: "FileSystem Storage",
MODE_FILE: 'FILE',
MODE_DIRECTORY: 'DIRECTORY',
MODE_SYMBOLIC_LINK: 'SYMLINK',
MODE_META: 'META',
SYMLOOP_MAX: 10,
BINARY_MIME_TYPE: 'application/octet-stream',
JSON_MIME_TYPE: 'application/json',
ROOT_DIRECTORY_NAME: '/', // basename(normalize(path))
// FS Mount Flags
FS_FORMAT: 'FORMAT',
FS_NOCTIME: 'NOCTIME',
FS_NOMTIME: 'NOMTIME',
2014-07-14 20:01:52 +00:00
FS_NODUPEIDCHECK: 'FS_NODUPEIDCHECK',
2014-05-27 18:44:30 +00:00
// FS File Open Flags
O_READ: O_READ,
O_WRITE: O_WRITE,
O_CREATE: O_CREATE,
O_EXCLUSIVE: O_EXCLUSIVE,
O_TRUNCATE: O_TRUNCATE,
O_APPEND: O_APPEND,
O_FLAGS: {
'r': [O_READ],
'r+': [O_READ, O_WRITE],
'w': [O_WRITE, O_CREATE, O_TRUNCATE],
'w+': [O_WRITE, O_READ, O_CREATE, O_TRUNCATE],
'wx': [O_WRITE, O_CREATE, O_EXCLUSIVE, O_TRUNCATE],
'wx+': [O_WRITE, O_READ, O_CREATE, O_EXCLUSIVE, O_TRUNCATE],
'a': [O_WRITE, O_CREATE, O_APPEND],
'a+': [O_WRITE, O_READ, O_CREATE, O_APPEND],
'ax': [O_WRITE, O_CREATE, O_EXCLUSIVE, O_APPEND],
'ax+': [O_WRITE, O_READ, O_CREATE, O_EXCLUSIVE, O_APPEND]
},
XATTR_CREATE: XATTR_CREATE,
XATTR_REPLACE: XATTR_REPLACE,
FS_READY: 'READY',
FS_PENDING: 'PENDING',
FS_ERROR: 'ERROR',
SUPER_NODE_ID: '00000000-0000-0000-0000-000000000000',
// Reserved File Descriptors for streams
STDIN: 0,
STDOUT: 1,
STDERR: 2,
FIRST_DESCRIPTOR: 3,
ENVIRONMENT: {
TMP: '/tmp',
PATH: ''
}
};
2014-09-02 19:48:54 +00:00
},{}],13:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var MODE_FILE = _dereq_('./constants.js').MODE_FILE;
2014-05-27 18:44:30 +00:00
module.exports = function DirectoryEntry(id, type) {
this.id = id;
this.type = type || MODE_FILE;
};
2014-09-02 19:48:54 +00:00
},{"./constants.js":12}],14:[function(_dereq_,module,exports){
2014-06-09 14:40:50 +00:00
(function (Buffer){
// Adapt encodings to work with Buffer or Uint8Array, they expect the latter
function decode(buf) {
return buf.toString('utf8');
}
function encode(string) {
return new Buffer(string, 'utf8');
}
module.exports = {
encode: encode,
decode: decode
};
}).call(this,_dereq_("buffer").Buffer)
2014-09-02 19:48:54 +00:00
},{"buffer":7}],15:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
var errors = {};
[
/**
2014-08-20 18:48:40 +00:00
* node.js errors - we only use some of these, add as needed.
2014-05-27 18:44:30 +00:00
*/
2014-08-20 18:48:40 +00:00
//'-1:UNKNOWN:unknown error',
//'0:OK:success',
//'1:EOF:end of file',
//'2:EADDRINFO:getaddrinfo error',
//'3:EACCES:permission denied',
//'4:EAGAIN:resource temporarily unavailable',
//'5:EADDRINUSE:address already in use',
//'6:EADDRNOTAVAIL:address not available',
//'7:EAFNOSUPPORT:address family not supported',
//'8:EALREADY:connection already in progress',
2014-05-27 18:44:30 +00:00
'9:EBADF:bad file descriptor',
'10:EBUSY:resource busy or locked',
2014-08-20 18:48:40 +00:00
//'11:ECONNABORTED:software caused connection abort',
//'12:ECONNREFUSED:connection refused',
//'13:ECONNRESET:connection reset by peer',
//'14:EDESTADDRREQ:destination address required',
//'15:EFAULT:bad address in system call argument',
//'16:EHOSTUNREACH:host is unreachable',
//'17:EINTR:interrupted system call',
2014-05-27 18:44:30 +00:00
'18:EINVAL:invalid argument',
2014-08-20 18:48:40 +00:00
//'19:EISCONN:socket is already connected',
//'20:EMFILE:too many open files',
//'21:EMSGSIZE:message too long',
//'22:ENETDOWN:network is down',
//'23:ENETUNREACH:network is unreachable',
//'24:ENFILE:file table overflow',
//'25:ENOBUFS:no buffer space available',
//'26:ENOMEM:not enough memory',
2014-05-27 18:44:30 +00:00
'27:ENOTDIR:not a directory',
'28:EISDIR:illegal operation on a directory',
2014-08-20 18:48:40 +00:00
//'29:ENONET:machine is not on the network',
2014-05-27 18:44:30 +00:00
// errno 30 skipped, as per https://github.com/rvagg/node-errno/blob/master/errno.js
2014-08-20 18:48:40 +00:00
//'31:ENOTCONN:socket is not connected',
//'32:ENOTSOCK:socket operation on non-socket',
//'33:ENOTSUP:operation not supported on socket',
2014-05-27 18:44:30 +00:00
'34:ENOENT:no such file or directory',
2014-08-20 18:48:40 +00:00
//'35:ENOSYS:function not implemented',
//'36:EPIPE:broken pipe',
//'37:EPROTO:protocol error',
//'38:EPROTONOSUPPORT:protocol not supported',
//'39:EPROTOTYPE:protocol wrong type for socket',
//'40:ETIMEDOUT:connection timed out',
//'41:ECHARSET:invalid Unicode character',
//'42:EAIFAMNOSUPPORT:address family for hostname not supported',
2014-05-27 18:44:30 +00:00
// errno 43 skipped, as per https://github.com/rvagg/node-errno/blob/master/errno.js
2014-08-20 18:48:40 +00:00
//'44:EAISERVICE:servname not supported for ai_socktype',
//'45:EAISOCKTYPE:ai_socktype not supported',
//'46:ESHUTDOWN:cannot send after transport endpoint shutdown',
2014-05-27 18:44:30 +00:00
'47:EEXIST:file already exists',
2014-08-20 18:48:40 +00:00
//'48:ESRCH:no such process',
//'49:ENAMETOOLONG:name too long',
//'50:EPERM:operation not permitted',
2014-05-27 18:44:30 +00:00
'51:ELOOP:too many symbolic links encountered',
2014-08-20 18:48:40 +00:00
//'52:EXDEV:cross-device link not permitted',
2014-05-27 18:44:30 +00:00
'53:ENOTEMPTY:directory not empty',
2014-08-20 18:48:40 +00:00
//'54:ENOSPC:no space left on device',
2014-05-27 18:44:30 +00:00
'55:EIO:i/o error',
2014-08-20 18:48:40 +00:00
//'56:EROFS:read-only file system',
//'57:ENODEV:no such device',
//'58:ESPIPE:invalid seek',
//'59:ECANCELED:operation canceled',
2014-05-27 18:44:30 +00:00
/**
* Filer specific errors
*/
'1000:ENOTMOUNTED:not mounted',
'1001:EFILESYSTEMERROR:missing super node, use \'FORMAT\' flag to format filesystem.',
'1002:ENOATTR:attribute does not exist'
2014-08-20 18:48:40 +00:00
2014-05-27 18:44:30 +00:00
].forEach(function(e) {
e = e.split(':');
2014-08-20 18:48:40 +00:00
var errno = +e[0];
var errName = e[1];
var defaultMessage = e[2];
function FilerError(msg, path) {
Error.call(this);
2014-05-27 18:44:30 +00:00
2014-08-20 18:48:40 +00:00
this.name = errName;
this.code = errName;
2014-07-29 00:02:40 +00:00
this.errno = errno;
2014-08-20 18:48:40 +00:00
this.message = msg || defaultMessage;
if(path) {
this.path = path;
}
this.stack = (new Error(this.message)).stack;
2014-05-27 18:44:30 +00:00
}
2014-07-29 00:02:40 +00:00
FilerError.prototype = Object.create(Error.prototype);
FilerError.prototype.constructor = FilerError;
2014-08-20 18:48:40 +00:00
FilerError.prototype.toString = function() {
var pathInfo = this.path ? (', \'' + this.path + '\'') : '';
return this.name + ': ' + this.message + pathInfo;
};
2014-05-27 18:44:30 +00:00
// We expose the error as both Errors.EINVAL and Errors[18]
2014-08-20 18:48:40 +00:00
errors[errName] = errors[errno] = FilerError;
2014-05-27 18:44:30 +00:00
});
module.exports = errors;
2014-09-02 19:48:54 +00:00
},{}],16:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var _ = _dereq_('../../lib/nodash.js');
2014-05-27 18:44:30 +00:00
2014-05-28 20:00:10 +00:00
var Path = _dereq_('../path.js');
2014-05-27 18:44:30 +00:00
var normalize = Path.normalize;
var dirname = Path.dirname;
var basename = Path.basename;
var isAbsolutePath = Path.isAbsolute;
var isNullPath = Path.isNull;
2014-05-28 20:00:10 +00:00
var Constants = _dereq_('../constants.js');
2014-05-27 18:44:30 +00:00
var MODE_FILE = Constants.MODE_FILE;
var MODE_DIRECTORY = Constants.MODE_DIRECTORY;
var MODE_SYMBOLIC_LINK = Constants.MODE_SYMBOLIC_LINK;
var MODE_META = Constants.MODE_META;
var ROOT_DIRECTORY_NAME = Constants.ROOT_DIRECTORY_NAME;
var SUPER_NODE_ID = Constants.SUPER_NODE_ID;
var SYMLOOP_MAX = Constants.SYMLOOP_MAX;
var O_READ = Constants.O_READ;
var O_WRITE = Constants.O_WRITE;
var O_CREATE = Constants.O_CREATE;
var O_EXCLUSIVE = Constants.O_EXCLUSIVE;
var O_TRUNCATE = Constants.O_TRUNCATE;
var O_APPEND = Constants.O_APPEND;
var O_FLAGS = Constants.O_FLAGS;
var XATTR_CREATE = Constants.XATTR_CREATE;
var XATTR_REPLACE = Constants.XATTR_REPLACE;
var FS_NOMTIME = Constants.FS_NOMTIME;
var FS_NOCTIME = Constants.FS_NOCTIME;
2014-06-09 14:40:50 +00:00
var Encoding = _dereq_('../encoding.js');
2014-05-28 20:00:10 +00:00
var Errors = _dereq_('../errors.js');
var DirectoryEntry = _dereq_('../directory-entry.js');
var OpenFileDescription = _dereq_('../open-file-description.js');
var SuperNode = _dereq_('../super-node.js');
var Node = _dereq_('../node.js');
var Stats = _dereq_('../stats.js');
2014-08-17 05:56:03 +00:00
var Buffer = _dereq_('../buffer.js');
2014-05-27 18:44:30 +00:00
/**
* Many functions below use this callback pattern. If it's not
* re-defined, we use this to generate a callback. NOTE: this
* can be use for callbacks of both forms without problem (i.e.,
* since result will be undefined if not returned):
* - callback(error)
* - callback(error, result)
*/
function standard_check_result_cb(callback) {
return function(error, result) {
if(error) {
callback(error);
} else {
callback(null, result);
}
};
}
/**
* Update node times. Only passed times are modified (undefined times are ignored)
* and filesystem flags are examined in order to override update logic.
*/
function update_node_times(context, path, node, times, callback) {
// Honour mount flags for how we update times
var flags = context.flags;
if(_(flags).contains(FS_NOCTIME)) {
delete times.ctime;
}
if(_(flags).contains(FS_NOMTIME)) {
delete times.mtime;
}
// Only do the update if required (i.e., times are still present)
var update = false;
if(times.ctime) {
node.ctime = times.ctime;
// We don't do atime tracking for perf reasons, but do mirror ctime
node.atime = times.ctime;
update = true;
}
if(times.atime) {
// The only time we explicitly pass atime is when utimes(), futimes() is called.
// Override ctime mirror here if so
node.atime = times.atime;
update = true;
}
if(times.mtime) {
node.mtime = times.mtime;
update = true;
}
function complete(error) {
// Queue this change so we can send watch events.
// Unlike node.js, we send the full path vs. basename/dirname only.
context.changes.push({ event: 'change', path: path });
callback(error);
}
if(update) {
2014-08-17 05:56:03 +00:00
context.putObject(node.id, node, complete);
2014-05-27 18:44:30 +00:00
} else {
complete();
}
}
/**
* make_node()
*/
// in: file or directory path
// out: new node representing file/directory
function make_node(context, path, mode, callback) {
if(mode !== MODE_DIRECTORY && mode !== MODE_FILE) {
2014-08-20 18:48:40 +00:00
return callback(new Errors.EINVAL('mode must be a directory or file', path));
2014-05-27 18:44:30 +00:00
}
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var parentNode;
var parentNodeData;
var node;
// Check if the parent node exists
function create_node_in_parent(error, parentDirectoryNode) {
if(error) {
callback(error);
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR('a component of the path prefix is not a directory', path));
2014-05-27 18:44:30 +00:00
} else {
parentNode = parentDirectoryNode;
find_node(context, path, check_if_node_exists);
}
}
// Check if the node to be created already exists
function check_if_node_exists(error, result) {
if(!error && result) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST('path name already exists', path));
2014-05-27 18:44:30 +00:00
} else if(error && !(error instanceof Errors.ENOENT)) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
context.getObject(parentNode.data, create_node);
2014-05-27 18:44:30 +00:00
}
}
// Create the new node
function create_node(error, result) {
if(error) {
callback(error);
} else {
parentNodeData = result;
2014-07-14 20:01:52 +00:00
Node.create({guid: context.guid, mode: mode}, function(error, result) {
if(error) {
callback(error);
return;
}
node = result;
node.nlinks += 1;
2014-08-17 05:56:03 +00:00
context.putObject(node.id, node, update_parent_node_data);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
}
// Update parent node time
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, parentPath, node, { mtime: now, ctime: now }, callback);
}
}
// Update the parent nodes data
function update_parent_node_data(error) {
if(error) {
callback(error);
} else {
parentNodeData[name] = new DirectoryEntry(node.id, mode);
2014-08-17 05:56:03 +00:00
context.putObject(parentNode.data, parentNodeData, update_time);
2014-05-27 18:44:30 +00:00
}
}
// Find the parent node
find_node(context, parentPath, create_node_in_parent);
}
/**
* find_node
*/
// in: file or directory path
// out: node structure, or error
function find_node(context, path, callback) {
path = normalize(path);
if(!path) {
return callback(new Errors.ENOENT('path is an empty string'));
}
var name = basename(path);
var parentPath = dirname(path);
var followedCount = 0;
function read_root_directory_node(error, superNode) {
if(error) {
callback(error);
} else if(!superNode || superNode.mode !== MODE_META || !superNode.rnode) {
callback(new Errors.EFILESYSTEMERROR());
} else {
2014-08-17 05:56:03 +00:00
context.getObject(superNode.rnode, check_root_directory_node);
2014-05-27 18:44:30 +00:00
}
}
function check_root_directory_node(error, rootDirectoryNode) {
if(error) {
callback(error);
} else if(!rootDirectoryNode) {
callback(new Errors.ENOENT());
} else {
callback(null, rootDirectoryNode);
}
}
// in: parent directory node
// out: parent directory data
function read_parent_directory_data(error, parentDirectoryNode) {
if(error) {
callback(error);
} else if(parentDirectoryNode.mode !== MODE_DIRECTORY || !parentDirectoryNode.data) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR('a component of the path prefix is not a directory', path));
2014-05-27 18:44:30 +00:00
} else {
2014-08-17 05:56:03 +00:00
context.getObject(parentDirectoryNode.data, get_node_from_parent_directory_data);
2014-05-27 18:44:30 +00:00
}
}
// in: parent directory data
// out: searched node
function get_node_from_parent_directory_data(error, parentDirectoryData) {
if(error) {
callback(error);
} else {
if(!_(parentDirectoryData).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT(null, path));
2014-05-27 18:44:30 +00:00
} else {
var nodeId = parentDirectoryData[name].id;
2014-08-17 05:56:03 +00:00
context.getObject(nodeId, is_symbolic_link);
2014-05-27 18:44:30 +00:00
}
}
}
function is_symbolic_link(error, node) {
if(error) {
callback(error);
} else {
if(node.mode == MODE_SYMBOLIC_LINK) {
followedCount++;
if(followedCount > SYMLOOP_MAX){
2014-08-20 18:48:40 +00:00
callback(new Errors.ELOOP(null, path));
2014-05-27 18:44:30 +00:00
} else {
follow_symbolic_link(node.data);
}
} else {
callback(null, node);
}
}
}
function follow_symbolic_link(data) {
data = normalize(data);
parentPath = dirname(data);
name = basename(data);
if(ROOT_DIRECTORY_NAME == name) {
2014-08-17 05:56:03 +00:00
context.getObject(SUPER_NODE_ID, read_root_directory_node);
2014-05-27 18:44:30 +00:00
} else {
find_node(context, parentPath, read_parent_directory_data);
}
}
if(ROOT_DIRECTORY_NAME == name) {
2014-08-17 05:56:03 +00:00
context.getObject(SUPER_NODE_ID, read_root_directory_node);
2014-05-27 18:44:30 +00:00
} else {
find_node(context, parentPath, read_parent_directory_data);
}
}
/**
* set extended attribute (refactor)
*/
function set_extended_attribute (context, path_or_fd, name, value, flag, callback) {
var path;
function set_xattr (error, node) {
var xattr = (node ? node.xattrs[name] : null);
function update_time(error) {
if(error) {
callback(error);
} else {
update_node_times(context, path, node, { ctime: Date.now() }, callback);
}
}
if (error) {
callback(error);
}
else if (flag === XATTR_CREATE && node.xattrs.hasOwnProperty(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST('attribute already exists', path_or_fd));
2014-05-27 18:44:30 +00:00
}
else if (flag === XATTR_REPLACE && !node.xattrs.hasOwnProperty(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOATTR(null, path_or_fd));
2014-05-27 18:44:30 +00:00
}
else {
node.xattrs[name] = value;
2014-08-17 05:56:03 +00:00
context.putObject(node.id, node, update_time);
2014-05-27 18:44:30 +00:00
}
}
if (typeof path_or_fd == 'string') {
path = path_or_fd;
find_node(context, path_or_fd, set_xattr);
}
else if (typeof path_or_fd == 'object' && typeof path_or_fd.id == 'string') {
path = path_or_fd.path;
2014-08-17 05:56:03 +00:00
context.getObject(path_or_fd.id, set_xattr);
2014-05-27 18:44:30 +00:00
}
else {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('path or file descriptor of wrong type', path_or_fd));
2014-05-27 18:44:30 +00:00
}
}
/**
2014-07-21 19:28:26 +00:00
* ensure_root_directory. Creates a root node if necessary.
*
* Note: this should only be invoked when formatting a new file system.
* Multiple invocations of this by separate instances will still result
* in only a single super node.
2014-05-27 18:44:30 +00:00
*/
2014-07-21 19:28:26 +00:00
function ensure_root_directory(context, callback) {
2014-05-27 18:44:30 +00:00
var superNode;
var directoryNode;
var directoryData;
2014-07-21 19:28:26 +00:00
function ensure_super_node(error, existingNode) {
2014-05-27 18:44:30 +00:00
if(!error && existingNode) {
2014-07-21 19:28:26 +00:00
// Another instance has beat us and already created the super node.
callback();
2014-05-27 18:44:30 +00:00
} else if(error && !(error instanceof Errors.ENOENT)) {
callback(error);
} else {
2014-07-14 20:01:52 +00:00
SuperNode.create({guid: context.guid}, function(error, result) {
if(error) {
callback(error);
return;
}
superNode = result;
2014-08-17 05:56:03 +00:00
context.putObject(superNode.id, superNode, write_directory_node);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
}
function write_directory_node(error) {
if(error) {
callback(error);
} else {
2014-07-14 20:01:52 +00:00
Node.create({guid: context.guid, id: superNode.rnode, mode: MODE_DIRECTORY}, function(error, result) {
if(error) {
callback(error);
return;
}
directoryNode = result;
directoryNode.nlinks += 1;
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.id, directoryNode, write_directory_data);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
}
function write_directory_data(error) {
if(error) {
callback(error);
} else {
directoryData = {};
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.data, directoryData, callback);
2014-05-27 18:44:30 +00:00
}
}
2014-08-17 05:56:03 +00:00
context.getObject(SUPER_NODE_ID, ensure_super_node);
2014-05-27 18:44:30 +00:00
}
/**
* make_directory
*/
function make_directory(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
var parentDirectoryNode;
var parentDirectoryData;
function check_if_directory_exists(error, result) {
if(!error && result) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST(null, path));
2014-05-27 18:44:30 +00:00
} else if(error && !(error instanceof Errors.ENOENT)) {
callback(error);
} else {
find_node(context, parentPath, read_parent_directory_data);
}
}
function read_parent_directory_data(error, result) {
if(error) {
callback(error);
} else {
parentDirectoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(parentDirectoryNode.data, write_directory_node);
2014-05-27 18:44:30 +00:00
}
}
function write_directory_node(error, result) {
if(error) {
callback(error);
} else {
parentDirectoryData = result;
2014-07-14 20:01:52 +00:00
Node.create({guid: context.guid, mode: MODE_DIRECTORY}, function(error, result) {
if(error) {
callback(error);
return;
}
directoryNode = result;
directoryNode.nlinks += 1;
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.id, directoryNode, write_directory_data);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
}
function write_directory_data(error) {
if(error) {
callback(error);
} else {
directoryData = {};
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.data, directoryData, update_parent_directory_data);
2014-05-27 18:44:30 +00:00
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, parentPath, parentDirectoryNode, { mtime: now, ctime: now }, callback);
}
}
function update_parent_directory_data(error) {
if(error) {
callback(error);
} else {
parentDirectoryData[name] = new DirectoryEntry(directoryNode.id, MODE_DIRECTORY);
2014-08-17 05:56:03 +00:00
context.putObject(parentDirectoryNode.data, parentDirectoryData, update_time);
2014-05-27 18:44:30 +00:00
}
}
find_node(context, path, check_if_directory_exists);
}
/**
* remove_directory
*/
function remove_directory(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
var parentDirectoryNode;
var parentDirectoryData;
function read_parent_directory_data(error, result) {
if(error) {
callback(error);
} else {
parentDirectoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(parentDirectoryNode.data, check_if_node_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_node_exists(error, result) {
if(error) {
callback(error);
} else if(ROOT_DIRECTORY_NAME == name) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EBUSY(null, path));
2014-05-27 18:44:30 +00:00
} else if(!_(result).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT(null, path));
2014-05-27 18:44:30 +00:00
} else {
parentDirectoryData = result;
directoryNode = parentDirectoryData[name].id;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode, check_if_node_is_directory);
2014-05-27 18:44:30 +00:00
}
}
function check_if_node_is_directory(error, result) {
if(error) {
callback(error);
} else if(result.mode != MODE_DIRECTORY) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR(null, path));
2014-05-27 18:44:30 +00:00
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_directory_is_empty);
2014-05-27 18:44:30 +00:00
}
}
function check_if_directory_is_empty(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(_(directoryData).size() > 0) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTEMPTY(null, path));
2014-05-27 18:44:30 +00:00
} else {
remove_directory_entry_from_parent_directory_node();
}
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, parentPath, parentDirectoryNode, { mtime: now, ctime: now }, remove_directory_node);
}
}
function remove_directory_entry_from_parent_directory_node() {
delete parentDirectoryData[name];
2014-08-17 05:56:03 +00:00
context.putObject(parentDirectoryNode.data, parentDirectoryData, update_time);
2014-05-27 18:44:30 +00:00
}
function remove_directory_node(error) {
if(error) {
callback(error);
} else {
context.delete(directoryNode.id, remove_directory_data);
}
}
function remove_directory_data(error) {
if(error) {
callback(error);
} else {
context.delete(directoryNode.data, callback);
}
}
find_node(context, parentPath, read_parent_directory_data);
}
function open_file(context, path, flags, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
var directoryEntry;
var fileNode;
var fileData;
var followedCount = 0;
if(ROOT_DIRECTORY_NAME == name) {
if(_(flags).contains(O_WRITE)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EISDIR('the named file is a directory and O_WRITE is set', path));
2014-05-27 18:44:30 +00:00
} else {
find_node(context, path, set_file_node);
}
} else {
find_node(context, parentPath, read_directory_data);
}
function read_directory_data(error, result) {
if(error) {
callback(error);
2014-07-16 21:14:11 +00:00
} else if(result.mode !== MODE_DIRECTORY) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT(null, path));
2014-05-27 18:44:30 +00:00
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_file_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(_(directoryData).has(name)) {
if(_(flags).contains(O_EXCLUSIVE)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('O_CREATE and O_EXCLUSIVE are set, and the named file exists', path));
2014-05-27 18:44:30 +00:00
} else {
directoryEntry = directoryData[name];
if(directoryEntry.type == MODE_DIRECTORY && _(flags).contains(O_WRITE)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EISDIR('the named file is a directory and O_WRITE is set', path));
2014-05-27 18:44:30 +00:00
} else {
2014-08-17 05:56:03 +00:00
context.getObject(directoryEntry.id, check_if_symbolic_link);
2014-05-27 18:44:30 +00:00
}
}
} else {
if(!_(flags).contains(O_CREATE)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('O_CREATE is not set and the named file does not exist', path));
2014-05-27 18:44:30 +00:00
} else {
write_file_node();
}
}
}
}
function check_if_symbolic_link(error, result) {
if(error) {
callback(error);
} else {
var node = result;
if(node.mode == MODE_SYMBOLIC_LINK) {
followedCount++;
if(followedCount > SYMLOOP_MAX){
2014-08-20 18:48:40 +00:00
callback(new Errors.ELOOP(null, path));
2014-05-27 18:44:30 +00:00
} else {
follow_symbolic_link(node.data);
}
} else {
set_file_node(undefined, node);
}
}
}
function follow_symbolic_link(data) {
data = normalize(data);
parentPath = dirname(data);
name = basename(data);
if(ROOT_DIRECTORY_NAME == name) {
if(_(flags).contains(O_WRITE)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EISDIR('the named file is a directory and O_WRITE is set', path));
2014-05-27 18:44:30 +00:00
} else {
find_node(context, path, set_file_node);
}
}
find_node(context, parentPath, read_directory_data);
}
function set_file_node(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
callback(null, fileNode);
}
}
function write_file_node() {
2014-07-14 20:01:52 +00:00
Node.create({guid: context.guid, mode: MODE_FILE}, function(error, result) {
if(error) {
callback(error);
return;
}
fileNode = result;
fileNode.nlinks += 1;
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, write_file_data);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
function write_file_data(error) {
if(error) {
callback(error);
} else {
2014-06-09 14:40:50 +00:00
fileData = new Buffer(0);
fileData.fill(0);
2014-08-17 05:56:03 +00:00
context.putBuffer(fileNode.data, fileData, update_directory_data);
2014-05-27 18:44:30 +00:00
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, parentPath, directoryNode, { mtime: now, ctime: now }, handle_update_result);
}
}
function update_directory_data(error) {
if(error) {
callback(error);
} else {
directoryData[name] = new DirectoryEntry(fileNode.id, MODE_FILE);
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.data, directoryData, update_time);
2014-05-27 18:44:30 +00:00
}
}
function handle_update_result(error) {
if(error) {
callback(error);
} else {
callback(null, fileNode);
}
}
}
function replace_data(context, ofd, buffer, offset, length, callback) {
var fileNode;
function return_nbytes(error) {
if(error) {
callback(error);
} else {
callback(null, length);
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, ofd.path, fileNode, { mtime: now, ctime: now }, return_nbytes);
}
}
function update_file_node(error) {
if(error) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_time);
2014-05-27 18:44:30 +00:00
}
}
function write_file_data(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
2014-06-09 14:40:50 +00:00
var newData = new Buffer(length);
newData.fill(0);
buffer.copy(newData, 0, offset, offset + length);
2014-05-27 18:44:30 +00:00
ofd.position = length;
fileNode.size = length;
fileNode.version += 1;
2014-08-17 05:56:03 +00:00
context.putBuffer(fileNode.data, newData, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, write_file_data);
2014-05-27 18:44:30 +00:00
}
function write_data(context, ofd, buffer, offset, length, position, callback) {
var fileNode;
var fileData;
function return_nbytes(error) {
if(error) {
callback(error);
} else {
callback(null, length);
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, ofd.path, fileNode, { mtime: now, ctime: now }, return_nbytes);
}
}
function update_file_node(error) {
if(error) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_time);
2014-05-27 18:44:30 +00:00
}
}
function update_file_data(error, result) {
if(error) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
fileData = result;
2014-06-25 17:54:32 +00:00
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
2014-05-27 18:44:30 +00:00
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
var newSize = Math.max(fileData.length, _position + length);
2014-06-09 14:40:50 +00:00
var newData = new Buffer(newSize);
newData.fill(0);
2014-05-27 18:44:30 +00:00
if(fileData) {
2014-06-09 14:40:50 +00:00
fileData.copy(newData);
}
buffer.copy(newData, _position, offset, offset + length);
2014-05-27 18:44:30 +00:00
if(undefined === position) {
ofd.position += length;
}
fileNode.size = newSize;
fileNode.version += 1;
2014-08-17 05:56:03 +00:00
context.putBuffer(fileNode.data, newData, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
function read_file_data(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
2014-08-17 05:56:03 +00:00
context.getBuffer(fileNode.data, update_file_data);
2014-05-27 18:44:30 +00:00
}
}
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, read_file_data);
2014-05-27 18:44:30 +00:00
}
function read_data(context, ofd, buffer, offset, length, position, callback) {
var fileNode;
var fileData;
function handle_file_data(error, result) {
if(error) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
fileData = result;
2014-06-25 17:54:32 +00:00
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
2014-05-27 18:44:30 +00:00
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
length = (_position + length > buffer.length) ? length - _position : length;
2014-06-09 14:40:50 +00:00
fileData.copy(buffer, offset, _position, _position + length);
2014-05-27 18:44:30 +00:00
if(undefined === position) {
ofd.position += length;
}
callback(null, length);
}
}
function read_file_data(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
2014-08-17 05:56:03 +00:00
context.getBuffer(fileNode.data, handle_file_data);
2014-05-27 18:44:30 +00:00
}
}
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, read_file_data);
2014-05-27 18:44:30 +00:00
}
function stat_file(context, path, callback) {
path = normalize(path);
var name = basename(path);
find_node(context, path, standard_check_result_cb(callback));
}
function fstat_file(context, ofd, callback) {
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, standard_check_result_cb(callback));
2014-05-27 18:44:30 +00:00
}
function lstat_file(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
if(ROOT_DIRECTORY_NAME == name) {
find_node(context, path, standard_check_result_cb(callback));
} else {
find_node(context, parentPath, read_directory_data);
}
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_file_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(!_(directoryData).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('a component of the path does not name an existing file', path));
2014-05-27 18:44:30 +00:00
} else {
2014-08-17 05:56:03 +00:00
context.getObject(directoryData[name].id, standard_check_result_cb(callback));
2014-05-27 18:44:30 +00:00
}
}
}
}
function link_node(context, oldpath, newpath, callback) {
oldpath = normalize(oldpath);
var oldname = basename(oldpath);
var oldParentPath = dirname(oldpath);
newpath = normalize(newpath);
var newname = basename(newpath);
var newParentPath = dirname(newpath);
var oldDirectoryNode;
var oldDirectoryData;
var newDirectoryNode;
var newDirectoryData;
var fileNode;
function update_time(error) {
if(error) {
callback(error);
} else {
update_node_times(context, newpath, fileNode, { ctime: Date.now() }, callback);
}
}
function update_file_node(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
fileNode.nlinks += 1;
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_time);
2014-05-27 18:44:30 +00:00
}
}
function read_directory_entry(error, result) {
if(error) {
callback(error);
} else {
2014-08-17 05:56:03 +00:00
context.getObject(newDirectoryData[newname].id, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
function check_if_new_file_exists(error, result) {
if(error) {
callback(error);
} else {
newDirectoryData = result;
if(_(newDirectoryData).has(newname)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST('newpath resolves to an existing file', newname));
2014-05-27 18:44:30 +00:00
} else {
newDirectoryData[newname] = oldDirectoryData[oldname];
2014-08-17 05:56:03 +00:00
context.putObject(newDirectoryNode.data, newDirectoryData, read_directory_entry);
2014-05-27 18:44:30 +00:00
}
}
}
function read_new_directory_data(error, result) {
if(error) {
callback(error);
} else {
newDirectoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(newDirectoryNode.data, check_if_new_file_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_old_file_exists(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryData = result;
if(!_(oldDirectoryData).has(oldname)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('a component of either path prefix does not exist', oldname));
2014-05-27 18:44:30 +00:00
} else {
find_node(context, newParentPath, read_new_directory_data);
}
}
}
function read_old_directory_data(error, result) {
if(error) {
callback(error);
} else {
oldDirectoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(oldDirectoryNode.data, check_if_old_file_exists);
2014-05-27 18:44:30 +00:00
}
}
find_node(context, oldParentPath, read_old_directory_data);
}
function unlink_node(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
var fileNode;
function update_directory_data(error) {
if(error) {
callback(error);
} else {
delete directoryData[name];
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.data, directoryData, function(error) {
2014-05-27 18:44:30 +00:00
var now = Date.now();
update_node_times(context, parentPath, directoryNode, { mtime: now, ctime: now }, callback);
});
}
}
function delete_file_data(error) {
if(error) {
callback(error);
} else {
context.delete(fileNode.data, update_directory_data);
}
}
function update_file_node(error, result) {
if(error) {
callback(error);
} else {
fileNode = result;
fileNode.nlinks -= 1;
if(fileNode.nlinks < 1) {
context.delete(fileNode.id, delete_file_data);
} else {
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, function(error) {
2014-05-27 18:44:30 +00:00
update_node_times(context, path, fileNode, { ctime: Date.now() }, update_directory_data);
});
}
}
}
function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(!_(directoryData).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('a component of the path does not name an existing file', name));
2014-05-27 18:44:30 +00:00
} else {
2014-08-17 05:56:03 +00:00
context.getObject(directoryData[name].id, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
}
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_file_exists);
2014-05-27 18:44:30 +00:00
}
}
find_node(context, parentPath, read_directory_data);
}
function read_directory(context, path, callback) {
path = normalize(path);
var name = basename(path);
var directoryNode;
var directoryData;
function handle_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
var files = Object.keys(directoryData);
callback(null, files);
}
}
function read_directory_data(error, result) {
if(error) {
callback(error);
2014-08-20 19:57:09 +00:00
} else if(result.mode !== MODE_DIRECTORY) {
callback(new Errors.ENOTDIR(null, path));
2014-05-27 18:44:30 +00:00
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, handle_directory_data);
2014-05-27 18:44:30 +00:00
}
}
find_node(context, path, read_directory_data);
}
function make_symbolic_link(context, srcpath, dstpath, callback) {
dstpath = normalize(dstpath);
var name = basename(dstpath);
var parentPath = dirname(dstpath);
var directoryNode;
var directoryData;
var fileNode;
if(ROOT_DIRECTORY_NAME == name) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST(null, name));
2014-05-27 18:44:30 +00:00
} else {
find_node(context, parentPath, read_directory_data);
}
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_file_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(_(directoryData).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EEXIST(null, name));
2014-05-27 18:44:30 +00:00
} else {
write_file_node();
}
}
}
function write_file_node() {
2014-07-14 20:01:52 +00:00
Node.create({guid: context.guid, mode: MODE_SYMBOLIC_LINK}, function(error, result) {
if(error) {
callback(error);
return;
}
fileNode = result;
fileNode.nlinks += 1;
fileNode.size = srcpath.length;
fileNode.data = srcpath;
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_directory_data);
2014-07-14 20:01:52 +00:00
});
2014-05-27 18:44:30 +00:00
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, parentPath, directoryNode, { mtime: now, ctime: now }, callback);
}
}
function update_directory_data(error) {
if(error) {
callback(error);
} else {
directoryData[name] = new DirectoryEntry(fileNode.id, MODE_SYMBOLIC_LINK);
2014-08-17 05:56:03 +00:00
context.putObject(directoryNode.data, directoryData, update_time);
2014-05-27 18:44:30 +00:00
}
}
}
function read_link(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);
var directoryNode;
var directoryData;
find_node(context, parentPath, read_directory_data);
function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
2014-08-17 05:56:03 +00:00
context.getObject(directoryNode.data, check_if_file_exists);
2014-05-27 18:44:30 +00:00
}
}
function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(!_(directoryData).has(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOENT('a component of the path does not name an existing file', name));
2014-05-27 18:44:30 +00:00
} else {
2014-08-17 05:56:03 +00:00
context.getObject(directoryData[name].id, check_if_symbolic);
2014-05-27 18:44:30 +00:00
}
}
}
function check_if_symbolic(error, result) {
if(error) {
callback(error);
} else {
if(result.mode != MODE_SYMBOLIC_LINK) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('path not a symbolic link', path));
2014-05-27 18:44:30 +00:00
} else {
callback(null, result.data);
}
}
}
}
function truncate_file(context, path, length, callback) {
path = normalize(path);
var fileNode;
function read_file_data (error, node) {
if (error) {
callback(error);
} else if(node.mode == MODE_DIRECTORY ) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EISDIR(null, path));
2014-05-27 18:44:30 +00:00
} else{
fileNode = node;
2014-08-17 05:56:03 +00:00
context.getBuffer(fileNode.data, truncate_file_data);
2014-05-27 18:44:30 +00:00
}
}
function truncate_file_data(error, fileData) {
if (error) {
callback(error);
} else {
2014-06-25 17:54:32 +00:00
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
2014-06-09 14:40:50 +00:00
var data = new Buffer(length);
data.fill(0);
2014-05-27 18:44:30 +00:00
if(fileData) {
2014-06-09 14:40:50 +00:00
fileData.copy(data);
2014-05-27 18:44:30 +00:00
}
2014-08-17 05:56:03 +00:00
context.putBuffer(fileNode.data, data, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, path, fileNode, { mtime: now, ctime: now }, callback);
}
}
function update_file_node (error) {
if(error) {
callback(error);
} else {
fileNode.size = length;
fileNode.version += 1;
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_time);
2014-05-27 18:44:30 +00:00
}
}
if(length < 0) {
callback(new Errors.EINVAL('length cannot be negative'));
} else {
find_node(context, path, read_file_data);
}
}
function ftruncate_file(context, ofd, length, callback) {
var fileNode;
function read_file_data (error, node) {
if (error) {
callback(error);
} else if(node.mode == MODE_DIRECTORY ) {
callback(new Errors.EISDIR());
} else{
fileNode = node;
2014-08-17 05:56:03 +00:00
context.getBuffer(fileNode.data, truncate_file_data);
2014-05-27 18:44:30 +00:00
}
}
function truncate_file_data(error, fileData) {
if (error) {
callback(error);
} else {
2014-06-09 14:40:50 +00:00
var data;
2014-06-25 17:54:32 +00:00
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
2014-05-27 18:44:30 +00:00
if(fileData) {
2014-06-09 14:40:50 +00:00
data = fileData.slice(0, length);
} else {
data = new Buffer(length);
data.fill(0);
2014-05-27 18:44:30 +00:00
}
2014-08-17 05:56:03 +00:00
context.putBuffer(fileNode.data, data, update_file_node);
2014-05-27 18:44:30 +00:00
}
}
function update_time(error) {
if(error) {
callback(error);
} else {
var now = Date.now();
update_node_times(context, ofd.path, fileNode, { mtime: now, ctime: now }, callback);
}
}
function update_file_node (error) {
if(error) {
callback(error);
} else {
fileNode.size = length;
fileNode.version += 1;
2014-08-17 05:56:03 +00:00
context.putObject(fileNode.id, fileNode, update_time);
2014-05-27 18:44:30 +00:00
}
}
if(length < 0) {
callback(new Errors.EINVAL('length cannot be negative'));
} else {
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, read_file_data);
2014-05-27 18:44:30 +00:00
}
}
function utimes_file(context, path, atime, mtime, callback) {
path = normalize(path);
function update_times(error, node) {
if (error) {
callback(error);
} else {
update_node_times(context, path, node, { atime: atime, ctime: mtime, mtime: mtime }, callback);
}
}
if (typeof atime != 'number' || typeof mtime != 'number') {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('atime and mtime must be number', path));
2014-05-27 18:44:30 +00:00
}
else if (atime < 0 || mtime < 0) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('atime and mtime must be positive integers', path));
2014-05-27 18:44:30 +00:00
}
else {
find_node(context, path, update_times);
}
}
function futimes_file(context, ofd, atime, mtime, callback) {
function update_times (error, node) {
if (error) {
callback(error);
} else {
update_node_times(context, ofd.path, node, { atime: atime, ctime: mtime, mtime: mtime }, callback);
}
}
if (typeof atime != 'number' || typeof mtime != 'number') {
callback(new Errors.EINVAL('atime and mtime must be a number'));
}
else if (atime < 0 || mtime < 0) {
callback(new Errors.EINVAL('atime and mtime must be positive integers'));
}
else {
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, update_times);
2014-05-27 18:44:30 +00:00
}
}
function setxattr_file(context, path, name, value, flag, callback) {
path = normalize(path);
if (typeof name != 'string') {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name must be a string', path));
2014-05-27 18:44:30 +00:00
}
else if (!name) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name cannot be an empty string', path));
2014-05-27 18:44:30 +00:00
}
else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE', path));
2014-05-27 18:44:30 +00:00
}
else {
set_extended_attribute(context, path, name, value, flag, callback);
}
}
function fsetxattr_file (context, ofd, name, value, flag, callback) {
if (typeof name != 'string') {
callback(new Errors.EINVAL('attribute name must be a string'));
}
else if (!name) {
callback(new Errors.EINVAL('attribute name cannot be an empty string'));
}
else if (flag !== null &&
flag !== XATTR_CREATE && flag !== XATTR_REPLACE) {
callback(new Errors.EINVAL('invalid flag, must be null, XATTR_CREATE or XATTR_REPLACE'));
}
else {
set_extended_attribute(context, ofd, name, value, flag, callback);
}
}
function getxattr_file (context, path, name, callback) {
path = normalize(path);
function get_xattr(error, node) {
var xattr = (node ? node.xattrs[name] : null);
if (error) {
callback (error);
}
else if (!node.xattrs.hasOwnProperty(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOATTR(null, path));
2014-05-27 18:44:30 +00:00
}
else {
callback(null, node.xattrs[name]);
}
}
if (typeof name != 'string') {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name must be a string', path));
2014-05-27 18:44:30 +00:00
}
else if (!name) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name cannot be an empty string', path));
2014-05-27 18:44:30 +00:00
}
else {
find_node(context, path, get_xattr);
}
}
function fgetxattr_file (context, ofd, name, callback) {
function get_xattr (error, node) {
var xattr = (node ? node.xattrs[name] : null);
if (error) {
callback(error);
}
else if (!node.xattrs.hasOwnProperty(name)) {
callback(new Errors.ENOATTR());
}
else {
callback(null, node.xattrs[name]);
}
}
if (typeof name != 'string') {
callback(new Errors.EINVAL());
}
else if (!name) {
callback(new Errors.EINVAL('attribute name cannot be an empty string'));
}
else {
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, get_xattr);
2014-05-27 18:44:30 +00:00
}
}
function removexattr_file (context, path, name, callback) {
path = normalize(path);
function remove_xattr (error, node) {
var xattr = (node ? node.xattrs : null);
function update_time(error) {
if(error) {
callback(error);
} else {
update_node_times(context, path, node, { ctime: Date.now() }, callback);
}
}
if (error) {
callback(error);
}
else if (!xattr.hasOwnProperty(name)) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOATTR(null, path));
2014-05-27 18:44:30 +00:00
}
else {
delete node.xattrs[name];
2014-08-17 05:56:03 +00:00
context.putObject(node.id, node, update_time);
2014-05-27 18:44:30 +00:00
}
}
if (typeof name != 'string') {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name must be a string', path));
2014-05-27 18:44:30 +00:00
}
else if (!name) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('attribute name cannot be an empty string', path));
2014-05-27 18:44:30 +00:00
}
else {
find_node(context, path, remove_xattr);
}
}
function fremovexattr_file (context, ofd, name, callback) {
function remove_xattr (error, node) {
function update_time(error) {
if(error) {
callback(error);
} else {
update_node_times(context, ofd.path, node, { ctime: Date.now() }, callback);
}
}
if (error) {
callback(error);
}
else if (!node.xattrs.hasOwnProperty(name)) {
callback(new Errors.ENOATTR());
}
else {
delete node.xattrs[name];
2014-08-17 05:56:03 +00:00
context.putObject(node.id, node, update_time);
2014-05-27 18:44:30 +00:00
}
}
if (typeof name != 'string') {
callback(new Errors.EINVAL('attribute name must be a string'));
}
else if (!name) {
callback(new Errors.EINVAL('attribute name cannot be an empty string'));
}
else {
2014-08-17 05:56:03 +00:00
context.getObject(ofd.id, remove_xattr);
2014-05-27 18:44:30 +00:00
}
}
function validate_flags(flags) {
if(!_(O_FLAGS).has(flags)) {
return null;
}
return O_FLAGS[flags];
}
function validate_file_options(options, enc, fileMode){
if(!options) {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === "function") {
options = { encoding: enc, flag: fileMode };
} else if(typeof options === "string") {
options = { encoding: options, flag: fileMode };
}
return options;
}
function pathCheck(path, callback) {
var err;
2014-08-22 16:35:13 +00:00
if(!path) {
err = new Errors.EINVAL('Path must be a string', path);
} else if(isNullPath(path)) {
2014-08-20 18:48:40 +00:00
err = new Errors.EINVAL('Path must be a string without null bytes.', path);
2014-05-27 18:44:30 +00:00
} else if(!isAbsolutePath(path)) {
2014-08-22 16:35:13 +00:00
err = new Errors.EINVAL('Path must be absolute.', path);
2014-05-27 18:44:30 +00:00
}
if(err) {
callback(err);
return false;
}
return true;
}
function open(fs, context, path, flags, mode, callback) {
// NOTE: we support the same signature as node with a `mode` arg,
// but ignore it.
callback = arguments[arguments.length - 1];
if(!pathCheck(path, callback)) return;
function check_result(error, fileNode) {
if(error) {
callback(error);
} else {
var position;
if(_(flags).contains(O_APPEND)) {
position = fileNode.size;
} else {
position = 0;
}
var openFileDescription = new OpenFileDescription(path, fileNode.id, flags, position);
var fd = fs.allocDescriptor(openFileDescription);
callback(null, fd);
}
}
flags = validate_flags(flags);
if(!flags) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('flags is not valid'), path);
2014-05-27 18:44:30 +00:00
}
open_file(context, path, flags, check_result);
}
function close(fs, context, fd, callback) {
if(!_(fs.openFiles).has(fd)) {
callback(new Errors.EBADF());
} else {
fs.releaseDescriptor(fd);
callback(null);
}
}
function mknod(fs, context, path, mode, callback) {
if(!pathCheck(path, callback)) return;
make_node(context, path, mode, callback);
}
function mkdir(fs, context, path, mode, callback) {
// NOTE: we support passing a mode arg, but we ignore it internally for now.
callback = arguments[arguments.length - 1];
if(!pathCheck(path, callback)) return;
make_directory(context, path, standard_check_result_cb(callback));
}
function rmdir(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
remove_directory(context, path, standard_check_result_cb(callback));
}
function stat(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
} else {
var stats = new Stats(result, fs.name);
callback(null, stats);
}
}
stat_file(context, path, check_result);
}
function fstat(fs, context, fd, callback) {
function check_result(error, result) {
if(error) {
callback(error);
} else {
var stats = new Stats(result, fs.name);
callback(null, stats);
}
}
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else {
fstat_file(context, ofd, check_result);
}
}
function link(fs, context, oldpath, newpath, callback) {
if(!pathCheck(oldpath, callback)) return;
if(!pathCheck(newpath, callback)) return;
link_node(context, oldpath, newpath, standard_check_result_cb(callback));
}
function unlink(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
unlink_node(context, path, standard_check_result_cb(callback));
}
function read(fs, context, fd, buffer, offset, length, position, callback) {
// Follow how node.js does this
function wrapped_cb(err, bytesRead) {
// Retain a reference to buffer so that it can't be GC'ed too soon.
callback(err, bytesRead || 0, buffer);
}
offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length;
callback = arguments[arguments.length - 1];
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else if(!_(ofd.flags).contains(O_READ)) {
callback(new Errors.EBADF('descriptor does not permit reading'));
} else {
read_data(context, ofd, buffer, offset, length, position, standard_check_result_cb(wrapped_cb));
}
}
function readFile(fs, context, path, options, callback) {
callback = arguments[arguments.length - 1];
options = validate_file_options(options, null, 'r');
if(!pathCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'r');
if(!flags) {
2014-08-20 18:48:40 +00:00
return callback(new Errors.EINVAL('flags is not valid', path));
2014-05-27 18:44:30 +00:00
}
open_file(context, path, flags, function(err, fileNode) {
if(err) {
return callback(err);
}
var ofd = new OpenFileDescription(path, fileNode.id, flags, 0);
var fd = fs.allocDescriptor(ofd);
2014-08-18 15:31:56 +00:00
function cleanup() {
fs.releaseDescriptor(fd);
}
fstat_file(context, ofd, function(err, fstatResult) {
if(err) {
cleanup();
return callback(err);
2014-05-27 18:44:30 +00:00
}
var stats = new Stats(fstatResult, fs.name);
2014-08-18 15:31:56 +00:00
if(stats.isDirectory()) {
cleanup();
2014-08-20 18:48:40 +00:00
return callback(new Errors.EISDIR('illegal operation on directory', path));
2014-08-18 15:31:56 +00:00
}
2014-05-27 18:44:30 +00:00
var size = stats.size;
2014-06-09 14:40:50 +00:00
var buffer = new Buffer(size);
buffer.fill(0);
2014-05-27 18:44:30 +00:00
2014-08-18 15:31:56 +00:00
read_data(context, ofd, buffer, 0, size, 0, function(err, nbytes) {
cleanup();
if(err) {
return callback(err);
2014-05-27 18:44:30 +00:00
}
var data;
if(options.encoding === 'utf8') {
2014-06-09 14:40:50 +00:00
data = Encoding.decode(buffer);
2014-05-27 18:44:30 +00:00
} else {
data = buffer;
}
callback(null, data);
});
});
});
}
function write(fs, context, fd, buffer, offset, length, position, callback) {
callback = arguments[arguments.length - 1];
offset = (undefined === offset) ? 0 : offset;
length = (undefined === length) ? buffer.length - offset : length;
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
} else if(buffer.length - offset < length) {
callback(new Errors.EIO('intput buffer is too small'));
} else {
write_data(context, ofd, buffer, offset, length, position, standard_check_result_cb(callback));
}
}
function writeFile(fs, context, path, data, options, callback) {
callback = arguments[arguments.length - 1];
options = validate_file_options(options, 'utf8', 'w');
if(!pathCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'w');
if(!flags) {
2014-08-20 18:48:40 +00:00
return callback(new Errors.EINVAL('flags is not valid', path));
2014-05-27 18:44:30 +00:00
}
data = data || '';
if(typeof data === "number") {
data = '' + data;
}
if(typeof data === "string" && options.encoding === 'utf8') {
2014-06-09 14:40:50 +00:00
data = Encoding.encode(data);
2014-05-27 18:44:30 +00:00
}
open_file(context, path, flags, function(err, fileNode) {
if(err) {
return callback(err);
}
var ofd = new OpenFileDescription(path, fileNode.id, flags, 0);
var fd = fs.allocDescriptor(ofd);
2014-08-18 15:31:56 +00:00
replace_data(context, ofd, data, 0, data.length, function(err, nbytes) {
2014-05-27 18:44:30 +00:00
fs.releaseDescriptor(fd);
2014-08-18 15:31:56 +00:00
if(err) {
return callback(err);
}
2014-05-27 18:44:30 +00:00
callback(null);
});
});
}
function appendFile(fs, context, path, data, options, callback) {
callback = arguments[arguments.length - 1];
options = validate_file_options(options, 'utf8', 'a');
if(!pathCheck(path, callback)) return;
var flags = validate_flags(options.flag || 'a');
if(!flags) {
2014-08-20 18:48:40 +00:00
return callback(new Errors.EINVAL('flags is not valid', path));
2014-05-27 18:44:30 +00:00
}
data = data || '';
if(typeof data === "number") {
data = '' + data;
}
if(typeof data === "string" && options.encoding === 'utf8') {
2014-06-09 14:40:50 +00:00
data = Encoding.encode(data);
2014-05-27 18:44:30 +00:00
}
open_file(context, path, flags, function(err, fileNode) {
if(err) {
return callback(err);
}
var ofd = new OpenFileDescription(path, fileNode.id, flags, fileNode.size);
var fd = fs.allocDescriptor(ofd);
2014-08-18 15:31:56 +00:00
write_data(context, ofd, data, 0, data.length, ofd.position, function(err, nbytes) {
2014-05-27 18:44:30 +00:00
fs.releaseDescriptor(fd);
2014-08-18 15:31:56 +00:00
if(err) {
return callback(err);
}
2014-05-27 18:44:30 +00:00
callback(null);
});
});
}
function exists(fs, context, path, callback) {
function cb(err, stats) {
callback(err ? false : true);
}
stat(fs, context, path, cb);
}
function getxattr(fs, context, path, name, callback) {
if (!pathCheck(path, callback)) return;
getxattr_file(context, path, name, standard_check_result_cb(callback));
}
function fgetxattr(fs, context, fd, name, callback) {
var ofd = fs.openFiles[fd];
if (!ofd) {
callback(new Errors.EBADF());
}
else {
fgetxattr_file(context, ofd, name, standard_check_result_cb(callback));
}
}
function setxattr(fs, context, path, name, value, flag, callback) {
if(typeof flag === 'function') {
callback = flag;
flag = null;
}
if (!pathCheck(path, callback)) return;
setxattr_file(context, path, name, value, flag, standard_check_result_cb(callback));
}
function fsetxattr(fs, context, fd, name, value, flag, callback) {
if(typeof flag === 'function') {
callback = flag;
flag = null;
}
var ofd = fs.openFiles[fd];
if (!ofd) {
callback(new Errors.EBADF());
}
else if (!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
}
else {
fsetxattr_file(context, ofd, name, value, flag, standard_check_result_cb(callback));
}
}
function removexattr(fs, context, path, name, callback) {
if (!pathCheck(path, callback)) return;
removexattr_file(context, path, name, standard_check_result_cb(callback));
}
function fremovexattr(fs, context, fd, name, callback) {
var ofd = fs.openFiles[fd];
if (!ofd) {
callback(new Errors.EBADF());
}
else if (!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
}
else {
fremovexattr_file(context, ofd, name, standard_check_result_cb(callback));
}
}
function lseek(fs, context, fd, offset, whence, callback) {
function update_descriptor_position(error, stats) {
if(error) {
callback(error);
} else {
if(stats.size + offset < 0) {
callback(new Errors.EINVAL('resulting file offset would be negative'));
} else {
ofd.position = stats.size + offset;
callback(null, ofd.position);
}
}
}
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
}
if('SET' === whence) {
if(offset < 0) {
callback(new Errors.EINVAL('resulting file offset would be negative'));
} else {
ofd.position = offset;
callback(null, ofd.position);
}
} else if('CUR' === whence) {
if(ofd.position + offset < 0) {
callback(new Errors.EINVAL('resulting file offset would be negative'));
} else {
ofd.position += offset;
callback(null, ofd.position);
}
} else if('END' === whence) {
fstat_file(context, ofd, update_descriptor_position);
} else {
callback(new Errors.EINVAL('whence argument is not a proper value'));
}
}
function readdir(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
read_directory(context, path, standard_check_result_cb(callback));
}
function utimes(fs, context, path, atime, mtime, callback) {
if(!pathCheck(path, callback)) return;
var currentTime = Date.now();
atime = (atime) ? atime : currentTime;
mtime = (mtime) ? mtime : currentTime;
utimes_file(context, path, atime, mtime, standard_check_result_cb(callback));
}
function futimes(fs, context, fd, atime, mtime, callback) {
var currentTime = Date.now();
atime = (atime) ? atime : currentTime;
mtime = (mtime) ? mtime : currentTime;
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
} else {
futimes_file(context, ofd, atime, mtime, standard_check_result_cb(callback));
}
}
function rename(fs, context, oldpath, newpath, callback) {
if(!pathCheck(oldpath, callback)) return;
if(!pathCheck(newpath, callback)) return;
function unlink_old_node(error) {
if(error) {
callback(error);
} else {
unlink_node(context, oldpath, standard_check_result_cb(callback));
}
}
link_node(context, oldpath, newpath, unlink_old_node);
}
function symlink(fs, context, srcpath, dstpath, type, callback) {
// NOTE: we support passing the `type` arg, but ignore it.
callback = arguments[arguments.length - 1];
if(!pathCheck(srcpath, callback)) return;
if(!pathCheck(dstpath, callback)) return;
make_symbolic_link(context, srcpath, dstpath, standard_check_result_cb(callback));
}
function readlink(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
read_link(context, path, standard_check_result_cb(callback));
}
function lstat(fs, context, path, callback) {
if(!pathCheck(path, callback)) return;
function check_result(error, result) {
if(error) {
callback(error);
} else {
var stats = new Stats(result, fs.name);
callback(null, stats);
}
}
lstat_file(context, path, check_result);
}
function truncate(fs, context, path, length, callback) {
// NOTE: length is optional
callback = arguments[arguments.length - 1];
length = length || 0;
if(!pathCheck(path, callback)) return;
truncate_file(context, path, length, standard_check_result_cb(callback));
}
function ftruncate(fs, context, fd, length, callback) {
// NOTE: length is optional
callback = arguments[arguments.length - 1];
length = length || 0;
var ofd = fs.openFiles[fd];
if(!ofd) {
callback(new Errors.EBADF());
} else if(!_(ofd.flags).contains(O_WRITE)) {
callback(new Errors.EBADF('descriptor does not permit writing'));
} else {
ftruncate_file(context, ofd, length, standard_check_result_cb(callback));
}
}
module.exports = {
2014-07-21 19:28:26 +00:00
ensureRootDirectory: ensure_root_directory,
2014-05-27 18:44:30 +00:00
open: open,
close: close,
mknod: mknod,
mkdir: mkdir,
rmdir: rmdir,
unlink: unlink,
stat: stat,
fstat: fstat,
link: link,
read: read,
readFile: readFile,
write: write,
writeFile: writeFile,
appendFile: appendFile,
exists: exists,
getxattr: getxattr,
fgetxattr: fgetxattr,
setxattr: setxattr,
fsetxattr: fsetxattr,
removexattr: removexattr,
fremovexattr: fremovexattr,
lseek: lseek,
readdir: readdir,
utimes: utimes,
futimes: futimes,
rename: rename,
symlink: symlink,
readlink: readlink,
lstat: lstat,
truncate: truncate,
ftruncate: ftruncate
};
2014-09-02 19:48:54 +00:00
},{"../../lib/nodash.js":4,"../buffer.js":11,"../constants.js":12,"../directory-entry.js":13,"../encoding.js":14,"../errors.js":15,"../node.js":20,"../open-file-description.js":21,"../path.js":22,"../stats.js":31,"../super-node.js":32}],17:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var _ = _dereq_('../../lib/nodash.js');
2014-05-27 18:44:30 +00:00
2014-05-28 20:00:10 +00:00
var isNullPath = _dereq_('../path.js').isNull;
var nop = _dereq_('../shared.js').nop;
2014-05-27 18:44:30 +00:00
2014-05-28 20:00:10 +00:00
var Constants = _dereq_('../constants.js');
2014-05-27 18:44:30 +00:00
var FILE_SYSTEM_NAME = Constants.FILE_SYSTEM_NAME;
var FS_FORMAT = Constants.FS_FORMAT;
var FS_READY = Constants.FS_READY;
var FS_PENDING = Constants.FS_PENDING;
var FS_ERROR = Constants.FS_ERROR;
2014-07-14 20:01:52 +00:00
var FS_NODUPEIDCHECK = Constants.FS_NODUPEIDCHECK;
2014-05-27 18:44:30 +00:00
2014-05-28 20:00:10 +00:00
var providers = _dereq_('../providers/index.js');
2014-05-27 18:44:30 +00:00
2014-05-28 20:00:10 +00:00
var Shell = _dereq_('../shell/shell.js');
var Intercom = _dereq_('../../lib/intercom.js');
var FSWatcher = _dereq_('../fs-watcher.js');
var Errors = _dereq_('../errors.js');
2014-07-14 20:01:52 +00:00
var defaultGuidFn = _dereq_('../shared.js').guid;
2014-05-27 18:44:30 +00:00
var STDIN = Constants.STDIN;
var STDOUT = Constants.STDOUT;
var STDERR = Constants.STDERR;
var FIRST_DESCRIPTOR = Constants.FIRST_DESCRIPTOR;
2014-07-14 20:01:52 +00:00
// The core fs operations live on impl
2014-05-28 20:00:10 +00:00
var impl = _dereq_('./implementation.js');
2014-05-27 18:44:30 +00:00
// node.js supports a calling pattern that leaves off a callback.
function maybeCallback(callback) {
if(typeof callback === "function") {
return callback;
}
return function(err) {
if(err) {
throw err;
}
};
}
/**
* FileSystem
*
* A FileSystem takes an `options` object, which can specify a number of,
* options. All options are optional, and include:
*
* name: the name of the file system, defaults to "local"
*
* flags: one or more flags to use when creating/opening the file system.
* For example: "FORMAT" will cause the file system to be formatted.
* No explicit flags are set by default.
*
* provider: an explicit storage provider to use for the file
* system's database context provider. A number of context
* providers are included (see /src/providers), and users
* can write one of their own and pass it in to be used.
* By default an IndexedDB provider is used.
*
2014-07-14 20:01:52 +00:00
* guid: a function for generating unique IDs for nodes in the filesystem.
* Use this to override the built-in UUID generation. (Used mainly for tests).
*
2014-05-27 18:44:30 +00:00
* callback: a callback function to be executed when the file system becomes
* ready for use. Depending on the context provider used, this might
* be right away, or could take some time. The callback should expect
* an `error` argument, which will be null if everything worked. Also
* users should check the file system's `readyState` and `error`
* properties to make sure it is usable.
*/
function FileSystem(options, callback) {
options = options || {};
2014-07-14 20:01:52 +00:00
callback = callback || nop;
2014-05-27 18:44:30 +00:00
var flags = options.flags;
2014-07-14 20:01:52 +00:00
var guid = options.guid ? options.guid : defaultGuidFn;
2014-05-27 18:44:30 +00:00
var provider = options.provider || new providers.Default(options.name || FILE_SYSTEM_NAME);
// If we're given a provider, match its name unless we get an explicit name
var name = options.name || provider.name;
var forceFormatting = _(flags).contains(FS_FORMAT);
var fs = this;
fs.readyState = FS_PENDING;
fs.name = name;
fs.error = null;
fs.stdin = STDIN;
fs.stdout = STDOUT;
fs.stderr = STDERR;
// Safely expose the list of open files and file
// descriptor management functions
var openFiles = {};
var nextDescriptor = FIRST_DESCRIPTOR;
Object.defineProperty(this, "openFiles", {
get: function() { return openFiles; }
});
this.allocDescriptor = function(openFileDescription) {
var fd = nextDescriptor ++;
openFiles[fd] = openFileDescription;
return fd;
};
this.releaseDescriptor = function(fd) {
delete openFiles[fd];
};
// Safely expose the operation queue
var queue = [];
this.queueOrRun = function(operation) {
var error;
if(FS_READY == fs.readyState) {
operation.call(fs);
} else if(FS_ERROR == fs.readyState) {
error = new Errors.EFILESYSTEMERROR('unknown error');
} else {
queue.push(operation);
}
return error;
};
function runQueued() {
queue.forEach(function(operation) {
operation.call(this);
}.bind(fs));
queue = null;
}
// We support the optional `options` arg from node, but ignore it
this.watch = function(filename, options, listener) {
if(isNullPath(filename)) {
throw new Error('Path must be a string without null bytes.');
}
if(typeof options === 'function') {
listener = options;
options = {};
}
options = options || {};
listener = listener || nop;
var watcher = new FSWatcher();
watcher.start(filename, false, options.recursive);
watcher.on('change', listener);
return watcher;
};
2014-07-14 20:01:52 +00:00
// Deal with various approaches to node ID creation
function wrappedGuidFn(context) {
return function(callback) {
// Skip the duplicate ID check if asked to
if(_(flags).contains(FS_NODUPEIDCHECK)) {
callback(null, guid());
return;
}
// Otherwise (default) make sure this id is unused first
function guidWithCheck(callback) {
var id = guid();
2014-08-17 05:56:03 +00:00
context.getObject(id, function(err, value) {
2014-07-14 20:01:52 +00:00
if(err) {
callback(err);
return;
}
// If this id is unused, use it, otherwise find another
if(!value) {
callback(null, id);
} else {
guidWithCheck(callback);
}
});
}
guidWithCheck(callback);
};
}
2014-05-27 18:44:30 +00:00
// Let other instances (in this or other windows) know about
// any changes to this fs instance.
function broadcastChanges(changes) {
if(!changes.length) {
return;
}
var intercom = Intercom.getInstance();
changes.forEach(function(change) {
intercom.emit(change.event, change.path);
});
}
// Open file system storage provider
2014-08-22 16:35:13 +00:00
provider.open(function(err) {
2014-05-27 18:44:30 +00:00
function complete(error) {
function wrappedContext(methodName) {
var context = provider[methodName]();
context.flags = flags;
context.changes = [];
2014-07-14 20:01:52 +00:00
context.guid = wrappedGuidFn(context);
2014-05-27 18:44:30 +00:00
// When the context is finished, let the fs deal with any change events
context.close = function() {
var changes = context.changes;
broadcastChanges(changes);
changes.length = 0;
};
return context;
}
// Wrap the provider so we can extend the context with fs flags and
// an array of changes (e.g., watch event 'change' and 'rename' events
// for paths updated during the lifetime of the context). From this
// point forward we won't call open again, so it's safe to drop it.
fs.provider = {
openReadWriteContext: function() {
return wrappedContext('getReadWriteContext');
},
openReadOnlyContext: function() {
return wrappedContext('getReadOnlyContext');
}
};
if(error) {
fs.readyState = FS_ERROR;
} else {
fs.readyState = FS_READY;
}
2014-08-18 18:40:44 +00:00
runQueued();
2014-05-27 18:44:30 +00:00
callback(error, fs);
}
if(err) {
return complete(err);
}
var context = provider.getReadWriteContext();
2014-07-14 20:01:52 +00:00
context.guid = wrappedGuidFn(context);
2014-08-22 16:35:13 +00:00
// Mount the filesystem, formatting if necessary
if(forceFormatting) {
// Wipe the storage provider, then write root block
context.clear(function(err) {
if(err) {
return complete(err);
}
impl.ensureRootDirectory(context, complete);
});
} else {
// Use existing (or create new) root and mount
2014-07-21 19:28:26 +00:00
impl.ensureRootDirectory(context, complete);
2014-08-22 16:35:13 +00:00
}
2014-05-27 18:44:30 +00:00
});
}
// Expose storage providers on FileSystem constructor
FileSystem.providers = providers;
/**
* Public API for FileSystem
*/
[
'open',
'close',
'mknod',
'mkdir',
'rmdir',
'stat',
'fstat',
'link',
'unlink',
'read',
'readFile',
'write',
'writeFile',
'appendFile',
'exists',
'lseek',
'readdir',
'rename',
'readlink',
'symlink',
'lstat',
'truncate',
'ftruncate',
'utimes',
'futimes',
'setxattr',
'getxattr',
'fsetxattr',
'fgetxattr',
'removexattr',
'fremovexattr'
].forEach(function(methodName) {
FileSystem.prototype[methodName] = function() {
var fs = this;
var args = Array.prototype.slice.call(arguments, 0);
var lastArgIndex = args.length - 1;
// We may or may not get a callback, and since node.js supports
// fire-and-forget style fs operations, we have to dance a bit here.
var missingCallback = typeof args[lastArgIndex] !== 'function';
var callback = maybeCallback(args[lastArgIndex]);
var error = fs.queueOrRun(function() {
var context = fs.provider.openReadWriteContext();
2014-08-18 18:40:44 +00:00
// Fail early if the filesystem is in an error state (e.g.,
// provider failed to open.
if(FS_ERROR === fs.readyState) {
var err = new Errors.EFILESYSTEMERROR('filesystem unavailable, operation canceled');
return callback.call(fs, err);
}
2014-05-27 18:44:30 +00:00
// Wrap the callback so we can explicitly close the context
function complete() {
context.close();
callback.apply(fs, arguments);
}
// Either add or replace the callback with our wrapper complete()
if(missingCallback) {
args.push(complete);
} else {
args[lastArgIndex] = complete;
}
// Forward this call to the impl's version, using the following
// call signature, with complete() as the callback/last-arg now:
// fn(fs, context, arg0, arg1, ... , complete);
var fnArgs = [fs, context].concat(args);
impl[methodName].apply(null, fnArgs);
});
if(error) {
callback(error);
}
};
});
FileSystem.prototype.Shell = function(options) {
return new Shell(this, options);
};
module.exports = FileSystem;
2014-09-02 19:48:54 +00:00
},{"../../lib/intercom.js":3,"../../lib/nodash.js":4,"../constants.js":12,"../errors.js":15,"../fs-watcher.js":18,"../path.js":22,"../providers/index.js":23,"../shared.js":27,"../shell/shell.js":30,"./implementation.js":16}],18:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var EventEmitter = _dereq_('../lib/eventemitter.js');
2014-07-14 20:01:52 +00:00
var Path = _dereq_('./path.js');
2014-05-28 20:00:10 +00:00
var Intercom = _dereq_('../lib/intercom.js');
2014-05-27 18:44:30 +00:00
/**
* FSWatcher based on node.js' FSWatcher
* see https://github.com/joyent/node/blob/master/lib/fs.js
*/
function FSWatcher() {
EventEmitter.call(this);
var self = this;
var recursive = false;
2014-07-15 13:26:58 +00:00
var recursivePathPrefix;
2014-05-27 18:44:30 +00:00
var filename;
function onchange(path) {
2014-07-15 13:26:58 +00:00
// Watch for exact filename, or parent path when recursive is true.
if(filename === path || (recursive && path.indexOf(recursivePathPrefix) === 0)) {
2014-05-27 18:44:30 +00:00
self.trigger('change', 'change', path);
}
}
// We support, but ignore the second arg, which node.js uses.
self.start = function(filename_, persistent_, recursive_) {
// Bail if we've already started (and therefore have a filename);
if(filename) {
return;
}
2014-07-14 20:01:52 +00:00
if(Path.isNull(filename_)) {
2014-05-27 18:44:30 +00:00
throw new Error('Path must be a string without null bytes.');
}
2014-07-14 20:01:52 +00:00
2014-05-27 18:44:30 +00:00
// TODO: get realpath for symlinks on filename...
2014-07-14 20:01:52 +00:00
// Filer's Path.normalize strips trailing slashes, which we use here.
// See https://github.com/js-platform/filer/issues/105
filename = Path.normalize(filename_);
2014-05-27 18:44:30 +00:00
// Whether to watch beneath this path or not
recursive = recursive_ === true;
2014-07-15 13:26:58 +00:00
// If recursive, construct a path prefix portion for comparisons later
// (i.e., '/path' becomes '/path/' so we can search within a filename for the
// prefix). We also take care to allow for '/' on its own.
if(recursive) {
recursivePathPrefix = filename === '/' ? '/' : filename + '/';
}
2014-05-27 18:44:30 +00:00
var intercom = Intercom.getInstance();
intercom.on('change', onchange);
};
self.close = function() {
var intercom = Intercom.getInstance();
intercom.off('change', onchange);
self.removeAllListeners('change');
};
}
FSWatcher.prototype = new EventEmitter();
FSWatcher.prototype.constructor = FSWatcher;
module.exports = FSWatcher;
2014-09-02 19:48:54 +00:00
},{"../lib/eventemitter.js":2,"../lib/intercom.js":3,"./path.js":22}],19:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
module.exports = {
2014-05-28 20:00:10 +00:00
FileSystem: _dereq_('./filesystem/interface.js'),
2014-08-17 05:56:03 +00:00
Buffer: _dereq_('./buffer.js'),
2014-05-28 20:00:10 +00:00
Path: _dereq_('./path.js'),
Errors: _dereq_('./errors.js')
2014-05-27 18:44:30 +00:00
};
2014-09-02 19:48:54 +00:00
},{"./buffer.js":11,"./errors.js":15,"./filesystem/interface.js":17,"./path.js":22}],20:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var MODE_FILE = _dereq_('./constants.js').MODE_FILE;
2014-05-27 18:44:30 +00:00
2014-07-14 20:01:52 +00:00
function Node(options) {
2014-05-27 18:44:30 +00:00
var now = Date.now();
2014-07-14 20:01:52 +00:00
this.id = options.id;
this.mode = options.mode || MODE_FILE; // node type (file, directory, etc)
this.size = options.size || 0; // size (bytes for files, entries for directories)
this.atime = options.atime || now; // access time (will mirror ctime after creation)
this.ctime = options.ctime || now; // creation/change time
this.mtime = options.mtime || now; // modified time
this.flags = options.flags || []; // file flags
this.xattrs = options.xattrs || {}; // extended attributes
this.nlinks = options.nlinks || 0; // links count
this.version = options.version || 0; // node version
2014-05-27 18:44:30 +00:00
this.blksize = undefined; // block size
this.nblocks = 1; // blocks count
2014-07-14 20:01:52 +00:00
this.data = options.data; // id for data object
}
// Make sure the options object has an id on property,
// either from caller or one we generate using supplied guid fn.
function ensureID(options, prop, callback) {
if(options[prop]) {
callback(null);
} else {
options.guid(function(err, id) {
options[prop] = id;
callback(err);
});
}
}
Node.create = function(options, callback) {
// We expect both options.id and options.data to be provided/generated.
ensureID(options, 'id', function(err) {
if(err) {
callback(err);
return;
}
ensureID(options, 'data', function(err) {
if(err) {
callback(err);
return;
}
callback(null, new Node(options));
});
});
2014-05-27 18:44:30 +00:00
};
2014-07-14 20:01:52 +00:00
module.exports = Node;
2014-09-02 19:48:54 +00:00
},{"./constants.js":12}],21:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
module.exports = function OpenFileDescription(path, id, flags, position) {
this.path = path;
this.id = id;
this.flags = flags;
this.position = position;
};
2014-09-02 19:48:54 +00:00
},{}],22:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// Based on https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
var splitPath = function(filename) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
};
// path.resolve([from ...], to)
function resolve() {
var resolvedPath = '',
resolvedAbsolute = false;
for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
// XXXidbfs: we don't have process.cwd() so we use '/' as a fallback
var path = (i >= 0) ? arguments[i] : '/';
// Skip empty and invalid entries
if (typeof path !== 'string' || !path) {
continue;
}
resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
return !!p;
}), !resolvedAbsolute).join('/');
return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
}
// path.normalize(path)
function normalize(path) {
var isAbsolute = path.charAt(0) === '/',
trailingSlash = path.substr(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
/*
if (path && trailingSlash) {
path += '/';
}
*/
return (isAbsolute ? '/' : '') + path;
}
function join() {
var paths = Array.prototype.slice.call(arguments, 0);
return normalize(paths.filter(function(p, index) {
return p && typeof p === 'string';
}).join('/'));
}
// path.relative(from, to)
function relative(from, to) {
from = exports.resolve(from).substr(1);
to = exports.resolve(to).substr(1);
function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}
var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}
if (start > end) return [];
return arr.slice(start, end - start + 1);
}
var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
for (var i = 0; i < length; i++) {
if (fromParts[i] !== toParts[i]) {
samePartsLength = i;
break;
}
}
var outputParts = [];
for (var i = samePartsLength; i < fromParts.length; i++) {
outputParts.push('..');
}
outputParts = outputParts.concat(toParts.slice(samePartsLength));
return outputParts.join('/');
}
function dirname(path) {
var result = splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
}
function basename(path, ext) {
var f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
// XXXidbfs: node.js just does `return f`
return f === "" ? "/" : f;
}
function extname(path) {
return splitPath(path)[3];
}
function isAbsolute(path) {
if(path.charAt(0) === '/') {
return true;
}
return false;
}
function isNull(path) {
if (('' + path).indexOf('\u0000') !== -1) {
return true;
}
return false;
}
// XXXidbfs: we don't support path.exists() or path.existsSync(), which
// are deprecated, and need a FileSystem instance to work. Use fs.stat().
module.exports = {
normalize: normalize,
resolve: resolve,
join: join,
relative: relative,
sep: '/',
delimiter: ':',
dirname: dirname,
basename: basename,
extname: extname,
isAbsolute: isAbsolute,
isNull: isNull
};
2014-09-02 19:48:54 +00:00
},{}],23:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var IndexedDB = _dereq_('./indexeddb.js');
var WebSQL = _dereq_('./websql.js');
var Memory = _dereq_('./memory.js');
2014-05-27 18:44:30 +00:00
module.exports = {
IndexedDB: IndexedDB,
WebSQL: WebSQL,
Memory: Memory,
/**
* Convenience Provider references
*/
// The default provider to use when none is specified
Default: IndexedDB,
// The Fallback provider does automatic fallback checks
Fallback: (function() {
if(IndexedDB.isSupported()) {
return IndexedDB;
}
if(WebSQL.isSupported()) {
return WebSQL;
}
function NotSupported() {
throw "[Filer Error] Your browser doesn't support IndexedDB or WebSQL.";
}
NotSupported.isSupported = function() {
return false;
};
return NotSupported;
}())
};
2014-09-02 19:48:54 +00:00
},{"./indexeddb.js":24,"./memory.js":25,"./websql.js":26}],24:[function(_dereq_,module,exports){
2014-06-23 18:31:58 +00:00
(function (global){
var FILE_SYSTEM_NAME = _dereq_('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = _dereq_('../constants.js').FILE_STORE_NAME;
var IDB_RW = _dereq_('../constants.js').IDB_RW;
var IDB_RO = _dereq_('../constants.js').IDB_RO;
var Errors = _dereq_('../errors.js');
2014-08-17 05:56:03 +00:00
var FilerBuffer = _dereq_('../buffer.js');
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
var indexedDB = global.indexedDB ||
global.mozIndexedDB ||
global.webkitIndexedDB ||
global.msIndexedDB;
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
function IndexedDBContext(db, mode) {
var transaction = db.transaction(FILE_STORE_NAME, mode);
this.objectStore = transaction.objectStore(FILE_STORE_NAME);
}
2014-08-17 05:56:03 +00:00
2014-06-23 18:31:58 +00:00
IndexedDBContext.prototype.clear = function(callback) {
try {
var request = this.objectStore.clear();
request.onsuccess = function(event) {
callback();
};
request.onerror = function(error) {
callback(error);
};
} catch(e) {
callback(e);
2014-05-27 18:44:30 +00:00
}
2014-06-23 18:31:58 +00:00
};
2014-08-17 05:56:03 +00:00
function _get(objectStore, key, callback) {
2014-06-23 18:31:58 +00:00
try {
2014-08-17 05:56:03 +00:00
var request = objectStore.get(key);
2014-06-23 18:31:58 +00:00
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(null, result);
};
request.onerror = function onerror(error) {
callback(error);
};
} catch(e) {
callback(e);
}
2014-08-17 05:56:03 +00:00
}
IndexedDBContext.prototype.getObject = function(key, callback) {
_get(this.objectStore, key, callback);
2014-06-23 18:31:58 +00:00
};
2014-08-17 05:56:03 +00:00
IndexedDBContext.prototype.getBuffer = function(key, callback) {
_get(this.objectStore, key, function(err, arrayBuffer) {
if(err) {
return callback(err);
}
callback(null, new FilerBuffer(arrayBuffer));
});
};
function _put(objectStore, key, value, callback) {
2014-06-23 18:31:58 +00:00
try {
2014-08-17 05:56:03 +00:00
var request = objectStore.put(value, key);
2014-06-23 18:31:58 +00:00
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(null, result);
};
request.onerror = function onerror(error) {
callback(error);
};
} catch(e) {
callback(e);
}
2014-08-17 05:56:03 +00:00
}
IndexedDBContext.prototype.putObject = function(key, value, callback) {
_put(this.objectStore, key, value, callback);
};
IndexedDBContext.prototype.putBuffer = function(key, uint8BackedBuffer, callback) {
_put(this.objectStore, key, uint8BackedBuffer.buffer, callback);
2014-06-23 18:31:58 +00:00
};
2014-08-17 05:56:03 +00:00
2014-06-23 18:31:58 +00:00
IndexedDBContext.prototype.delete = function(key, callback) {
try {
var request = this.objectStore.delete(key);
request.onsuccess = function onsuccess(event) {
var result = event.target.result;
callback(null, result);
};
request.onerror = function(error) {
callback(error);
};
} catch(e) {
callback(e);
}
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
function IndexedDB(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = null;
}
IndexedDB.isSupported = function() {
return !!indexedDB;
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
IndexedDB.prototype.open = function(callback) {
var that = this;
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
// Bail if we already have a db open
2014-08-22 16:35:13 +00:00
if(that.db) {
return callback();
2014-06-23 18:31:58 +00:00
}
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
// NOTE: we're not using versioned databases.
var openRequest = indexedDB.open(that.name);
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
// If the db doesn't exist, we'll create it
openRequest.onupgradeneeded = function onupgradeneeded(event) {
var db = event.target.result;
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
if(db.objectStoreNames.contains(FILE_STORE_NAME)) {
db.deleteObjectStore(FILE_STORE_NAME);
}
db.createObjectStore(FILE_STORE_NAME);
2014-05-27 18:44:30 +00:00
};
2014-06-23 18:31:58 +00:00
openRequest.onsuccess = function onsuccess(event) {
that.db = event.target.result;
2014-08-22 16:35:13 +00:00
callback();
2014-05-27 18:44:30 +00:00
};
2014-06-23 18:31:58 +00:00
openRequest.onerror = function onerror(error) {
callback(new Errors.EINVAL('IndexedDB cannot be accessed. If private browsing is enabled, disable it.'));
2014-05-27 18:44:30 +00:00
};
2014-06-23 18:31:58 +00:00
};
IndexedDB.prototype.getReadOnlyContext = function() {
// Due to timing issues in Chrome with readwrite vs. readonly indexeddb transactions
// always use readwrite so we can make sure pending commits finish before callbacks.
// See https://github.com/js-platform/filer/issues/128
return new IndexedDBContext(this.db, IDB_RW);
};
IndexedDB.prototype.getReadWriteContext = function() {
return new IndexedDBContext(this.db, IDB_RW);
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
module.exports = IndexedDB;
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
2014-09-02 19:48:54 +00:00
},{"../buffer.js":11,"../constants.js":12,"../errors.js":15}],25:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var FILE_SYSTEM_NAME = _dereq_('../constants.js').FILE_SYSTEM_NAME;
2014-07-21 19:28:26 +00:00
// NOTE: prefer setImmediate to nextTick for proper recursion yielding.
// see https://github.com/js-platform/filer/pull/24
var asyncCallback = _dereq_('../../lib/async.js').setImmediate;
2014-05-27 18:44:30 +00:00
/**
* Make shared in-memory DBs possible when using the same name.
*/
var createDB = (function() {
var pool = {};
return function getOrCreate(name) {
2014-08-22 16:35:13 +00:00
if(!pool.hasOwnProperty(name)) {
2014-05-27 18:44:30 +00:00
pool[name] = {};
}
2014-08-22 16:35:13 +00:00
return pool[name];
2014-05-27 18:44:30 +00:00
};
}());
function MemoryContext(db, readOnly) {
this.readOnly = readOnly;
this.objectStore = db;
}
2014-08-17 05:56:03 +00:00
2014-05-27 18:44:30 +00:00
MemoryContext.prototype.clear = function(callback) {
if(this.readOnly) {
asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
}
var objectStore = this.objectStore;
Object.keys(objectStore).forEach(function(key){
delete objectStore[key];
});
asyncCallback(callback);
};
2014-08-17 05:56:03 +00:00
// Memory context doesn't care about differences between Object and Buffer
MemoryContext.prototype.getObject =
MemoryContext.prototype.getBuffer =
function(key, callback) {
2014-05-27 18:44:30 +00:00
var that = this;
asyncCallback(function() {
callback(null, that.objectStore[key]);
});
};
2014-08-17 05:56:03 +00:00
MemoryContext.prototype.putObject =
MemoryContext.prototype.putBuffer =
function(key, value, callback) {
2014-05-27 18:44:30 +00:00
if(this.readOnly) {
asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
}
this.objectStore[key] = value;
asyncCallback(callback);
};
2014-08-17 05:56:03 +00:00
2014-05-27 18:44:30 +00:00
MemoryContext.prototype.delete = function(key, callback) {
if(this.readOnly) {
asyncCallback(function() {
callback("[MemoryContext] Error: write operation on read only context");
});
return;
}
delete this.objectStore[key];
asyncCallback(callback);
};
function Memory(name) {
this.name = name || FILE_SYSTEM_NAME;
}
Memory.isSupported = function() {
return true;
};
Memory.prototype.open = function(callback) {
2014-08-22 16:35:13 +00:00
this.db = createDB(this.name);
asyncCallback(callback);
2014-05-27 18:44:30 +00:00
};
Memory.prototype.getReadOnlyContext = function() {
return new MemoryContext(this.db, true);
};
Memory.prototype.getReadWriteContext = function() {
return new MemoryContext(this.db, false);
};
module.exports = Memory;
2014-09-02 19:48:54 +00:00
},{"../../lib/async.js":1,"../constants.js":12}],26:[function(_dereq_,module,exports){
2014-06-23 18:31:58 +00:00
(function (global){
var FILE_SYSTEM_NAME = _dereq_('../constants.js').FILE_SYSTEM_NAME;
var FILE_STORE_NAME = _dereq_('../constants.js').FILE_STORE_NAME;
var WSQL_VERSION = _dereq_('../constants.js').WSQL_VERSION;
var WSQL_SIZE = _dereq_('../constants.js').WSQL_SIZE;
var WSQL_DESC = _dereq_('../constants.js').WSQL_DESC;
var Errors = _dereq_('../errors.js');
2014-08-17 05:56:03 +00:00
var FilerBuffer = _dereq_('../buffer.js');
var base64ArrayBuffer = _dereq_('base64-arraybuffer');
2014-06-23 18:31:58 +00:00
function WebSQLContext(db, isReadOnly) {
var that = this;
this.getTransaction = function(callback) {
if(that.transaction) {
callback(that.transaction);
return;
2014-05-27 18:44:30 +00:00
}
2014-06-23 18:31:58 +00:00
// Either do readTransaction() (read-only) or transaction() (read/write)
db[isReadOnly ? 'readTransaction' : 'transaction'](function(transaction) {
that.transaction = transaction;
callback(transaction);
2014-05-27 18:44:30 +00:00
});
};
2014-06-23 18:31:58 +00:00
}
2014-08-17 05:56:03 +00:00
2014-06-23 18:31:58 +00:00
WebSQLContext.prototype.clear = function(callback) {
function onError(transaction, error) {
callback(error);
}
function onSuccess(transaction, result) {
callback(null);
}
this.getTransaction(function(transaction) {
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + ";",
[], onSuccess, onError);
});
};
2014-08-17 05:56:03 +00:00
function _get(getTransaction, key, callback) {
2014-06-23 18:31:58 +00:00
function onSuccess(transaction, result) {
// If the key isn't found, return null
var value = result.rows.length === 0 ? null : result.rows.item(0).data;
2014-08-17 05:56:03 +00:00
callback(null, value);
2014-06-23 18:31:58 +00:00
}
function onError(transaction, error) {
callback(error);
}
2014-08-17 05:56:03 +00:00
getTransaction(function(transaction) {
transaction.executeSql("SELECT data FROM " + FILE_STORE_NAME + " WHERE id = ? LIMIT 1;",
2014-06-23 18:31:58 +00:00
[key], onSuccess, onError);
});
2014-08-17 05:56:03 +00:00
}
WebSQLContext.prototype.getObject = function(key, callback) {
_get(this.getTransaction, key, function(err, result) {
if(err) {
return callback(err);
}
try {
if(result) {
result = JSON.parse(result);
}
} catch(e) {
return callback(e);
}
callback(null, result);
});
2014-06-23 18:31:58 +00:00
};
2014-08-17 05:56:03 +00:00
WebSQLContext.prototype.getBuffer = function(key, callback) {
_get(this.getTransaction, key, function(err, result) {
if(err) {
return callback(err);
}
// Deal with zero-length ArrayBuffers, which will be encoded as ''
if(result || result === '') {
var arrayBuffer = base64ArrayBuffer.decode(result);
result = new FilerBuffer(arrayBuffer);
}
callback(null, result);
});
};
function _put(getTransaction, key, value, callback) {
2014-06-23 18:31:58 +00:00
function onSuccess(transaction, result) {
callback(null);
}
function onError(transaction, error) {
callback(error);
}
2014-08-17 05:56:03 +00:00
getTransaction(function(transaction) {
2014-06-23 18:31:58 +00:00
transaction.executeSql("INSERT OR REPLACE INTO " + FILE_STORE_NAME + " (id, data) VALUES (?, ?);",
[key, value], onSuccess, onError);
});
2014-08-17 05:56:03 +00:00
}
WebSQLContext.prototype.putObject = function(key, value, callback) {
var json = JSON.stringify(value);
_put(this.getTransaction, key, json, callback);
};
WebSQLContext.prototype.putBuffer = function(key, uint8BackedBuffer, callback) {
var base64 = base64ArrayBuffer.encode(uint8BackedBuffer.buffer);
_put(this.getTransaction, key, base64, callback);
2014-06-23 18:31:58 +00:00
};
2014-08-17 05:56:03 +00:00
2014-06-23 18:31:58 +00:00
WebSQLContext.prototype.delete = function(key, callback) {
function onSuccess(transaction, result) {
callback(null);
}
function onError(transaction, error) {
callback(error);
}
this.getTransaction(function(transaction) {
transaction.executeSql("DELETE FROM " + FILE_STORE_NAME + " WHERE id = ?;",
[key], onSuccess, onError);
});
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
function WebSQL(name) {
this.name = name || FILE_SYSTEM_NAME;
this.db = null;
}
WebSQL.isSupported = function() {
return !!global.openDatabase;
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
WebSQL.prototype.open = function(callback) {
var that = this;
// Bail if we already have a db open
if(that.db) {
2014-08-22 16:35:13 +00:00
return callback();
2014-05-27 18:44:30 +00:00
}
2014-06-23 18:31:58 +00:00
var db = global.openDatabase(that.name, WSQL_VERSION, WSQL_DESC, WSQL_SIZE);
if(!db) {
callback("[WebSQL] Unable to open database.");
return;
}
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
function onError(transaction, error) {
if (error.code === 5) {
callback(new Errors.EINVAL('WebSQL cannot be accessed. If private browsing is enabled, disable it.'));
2014-05-27 18:44:30 +00:00
}
2014-06-23 18:31:58 +00:00
callback(error);
}
function onSuccess(transaction, result) {
that.db = db;
2014-08-22 16:35:13 +00:00
callback();
2014-06-23 18:31:58 +00:00
}
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
// Create the table and index we'll need to store the fs data.
db.transaction(function(transaction) {
function createIndex(transaction) {
transaction.executeSql("CREATE INDEX IF NOT EXISTS idx_" + FILE_STORE_NAME + "_id" +
" on " + FILE_STORE_NAME + " (id);",
[], onSuccess, onError);
2014-05-27 18:44:30 +00:00
}
2014-06-23 18:31:58 +00:00
transaction.executeSql("CREATE TABLE IF NOT EXISTS " + FILE_STORE_NAME + " (id unique, data TEXT);",
[], createIndex, onError);
});
};
WebSQL.prototype.getReadOnlyContext = function() {
return new WebSQLContext(this.db, true);
};
WebSQL.prototype.getReadWriteContext = function() {
return new WebSQLContext(this.db, false);
};
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
module.exports = WebSQL;
2014-05-27 18:44:30 +00:00
2014-06-23 18:31:58 +00:00
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
2014-09-02 19:48:54 +00:00
},{"../buffer.js":11,"../constants.js":12,"../errors.js":15,"base64-arraybuffer":5}],27:[function(_dereq_,module,exports){
2014-05-27 18:44:30 +00:00
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
}).toUpperCase();
}
function nop() {}
/**
* Convert a Uint8Array to a regular array
*/
function u8toArray(u8) {
var array = [];
var len = u8.length;
for(var i = 0; i < len; i++) {
array[i] = u8[i];
}
return array;
}
module.exports = {
guid: guid,
u8toArray: u8toArray,
nop: nop
};
2014-09-02 19:48:54 +00:00
},{}],28:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var defaults = _dereq_('../constants.js').ENVIRONMENT;
2014-05-27 18:44:30 +00:00
module.exports = function Environment(env) {
env = env || {};
env.TMP = env.TMP || defaults.TMP;
env.PATH = env.PATH || defaults.PATH;
this.get = function(name) {
return env[name];
};
this.set = function(name, value) {
env[name] = value;
};
};
2014-09-02 19:48:54 +00:00
},{"../constants.js":12}],29:[function(_dereq_,module,exports){
2014-06-09 14:40:50 +00:00
var request = _dereq_('request');
module.exports.download = function(uri, callback) {
request({
url: uri,
method: 'GET',
encoding: null
}, function(err, msg, body) {
var statusCode;
var error;
msg = msg || null;
statusCode = msg && msg.statusCode;
error = statusCode !== 200 ? { message: err || 'Not found!', code: statusCode } : null;
if (error) {
callback(error, null);
return;
}
callback(null, body);
});
};
2014-09-02 19:48:54 +00:00
},{"request":6}],30:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var Path = _dereq_('../path.js');
var Errors = _dereq_('../errors.js');
var Environment = _dereq_('./environment.js');
var async = _dereq_('../../lib/async.js');
2014-06-09 14:40:50 +00:00
var Network = _dereq_('./network.js');
var Encoding = _dereq_('../encoding.js');
2014-05-27 18:44:30 +00:00
function Shell(fs, options) {
options = options || {};
var env = new Environment(options.env);
var cwd = '/';
/**
* The bound FileSystem (cannot be changed)
*/
Object.defineProperty(this, 'fs', {
get: function() { return fs; },
enumerable: true
});
/**
* The shell's environment (e.g., for things like
* path, tmp, and other env vars). Use env.get()
* and env.set() to work with variables.
*/
Object.defineProperty(this, 'env', {
get: function() { return env; },
enumerable: true
});
/**
* Change the current working directory. We
* include `cd` on the `this` vs. proto so that
* we can access cwd without exposing it externally.
*/
this.cd = function(path, callback) {
2014-07-29 00:02:40 +00:00
path = Path.resolve(cwd, path);
2014-05-27 18:44:30 +00:00
// Make sure the path actually exists, and is a dir
fs.stat(path, function(err, stats) {
if(err) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR(null, path));
2014-05-27 18:44:30 +00:00
return;
}
if(stats.type === 'DIRECTORY') {
cwd = path;
callback();
} else {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR(null, path));
2014-05-27 18:44:30 +00:00
}
});
};
/**
* Get the current working directory (changed with `cd()`)
*/
this.pwd = function() {
return cwd;
};
}
/**
* Execute the .js command located at `path`. Such commands
* should assume the existence of 3 arguments, which will be
* defined at runtime:
*
* * fs - the current shell's bound filesystem object
* * args - a list of arguments for the command, or an empty list if none
* * callback - a callback function(error, result) to call when done.
*
* The .js command's contents should be the body of a function
* that looks like this:
*
* function(fs, args, callback) {
* // .js code here
* }
*/
Shell.prototype.exec = function(path, args, callback) {
/* jshint evil:true */
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
if(typeof args === 'function') {
callback = args;
args = [];
}
args = args || [];
callback = callback || function(){};
2014-07-29 00:02:40 +00:00
path = Path.resolve(sh.pwd(), path);
2014-05-27 18:44:30 +00:00
fs.readFile(path, "utf8", function(error, data) {
if(error) {
callback(error);
return;
}
try {
var cmd = new Function('fs', 'args', 'callback', data);
cmd(fs, args, callback);
} catch(e) {
callback(e);
}
});
};
/**
* Create a file if it does not exist, or update access and
* modified times if it does. Valid options include:
*
* * updateOnly - whether to create the file if missing (defaults to false)
* * date - use the provided Date value instead of current date/time
*/
Shell.prototype.touch = function(path, options, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
2014-07-29 00:02:40 +00:00
path = Path.resolve(sh.pwd(), path);
2014-05-27 18:44:30 +00:00
function createFile(path) {
fs.writeFile(path, '', callback);
}
function updateTimes(path) {
var now = Date.now();
var atime = options.date || now;
var mtime = options.date || now;
fs.utimes(path, atime, mtime, callback);
}
fs.stat(path, function(error, stats) {
if(error) {
if(options.updateOnly === true) {
callback();
} else {
createFile(path);
}
} else {
updateTimes(path);
}
});
};
/**
* Concatenate multiple files into a single String, with each
* file separated by a newline. The `files` argument should
* be a String (path to single file) or an Array of Strings
* (multiple file paths).
*/
Shell.prototype.cat = function(files, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
var all = '';
callback = callback || function(){};
if(!files) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('Missing files argument'));
2014-05-27 18:44:30 +00:00
return;
}
files = typeof files === 'string' ? [ files ] : files;
function append(item, callback) {
2014-07-29 00:02:40 +00:00
var filename = Path.resolve(sh.pwd(), item);
2014-05-27 18:44:30 +00:00
fs.readFile(filename, 'utf8', function(error, data) {
if(error) {
callback(error);
return;
}
all += data + '\n';
callback();
});
}
async.eachSeries(files, append, function(error) {
if(error) {
callback(error);
} else {
callback(null, all.replace(/\n$/, ''));
}
});
};
/**
* Get the listing of a directory, returning an array of
* file entries in the following form:
*
* {
* path: <String> the basename of the directory entry
* links: <Number> the number of links to the entry
* size: <Number> the size in bytes of the entry
* modified: <Number> the last modified date/time
* type: <String> the type of the entry
* contents: <Array> an optional array of child entries
* }
*
* By default ls() gives a shallow listing. If you want
* to follow directories as they are encountered, use
* the `recursive=true` option.
*/
Shell.prototype.ls = function(dir, options, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!dir) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('Missing dir argument'));
2014-05-27 18:44:30 +00:00
return;
}
function list(path, callback) {
2014-07-29 00:02:40 +00:00
var pathname = Path.resolve(sh.pwd(), path);
2014-05-27 18:44:30 +00:00
var result = [];
fs.readdir(pathname, function(error, entries) {
if(error) {
callback(error);
return;
}
function getDirEntry(name, callback) {
name = Path.join(pathname, name);
fs.stat(name, function(error, stats) {
if(error) {
callback(error);
return;
}
var entry = {
path: Path.basename(name),
links: stats.nlinks,
size: stats.size,
modified: stats.mtime,
type: stats.type
};
if(options.recursive && stats.type === 'DIRECTORY') {
list(Path.join(pathname, entry.path), function(error, items) {
if(error) {
callback(error);
return;
}
entry.contents = items;
result.push(entry);
callback();
});
} else {
result.push(entry);
callback();
}
});
}
2014-07-21 19:28:26 +00:00
async.eachSeries(entries, getDirEntry, function(error) {
2014-05-27 18:44:30 +00:00
callback(error, result);
});
});
}
list(dir, callback);
};
/**
* Removes the file or directory at `path`. If `path` is a file
* it will be removed. If `path` is a directory, it will be
* removed if it is empty, otherwise the callback will receive
* an error. In order to remove non-empty directories, use the
* `recursive=true` option.
*/
Shell.prototype.rm = function(path, options, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!path) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('Missing path argument'));
2014-05-27 18:44:30 +00:00
return;
}
function remove(pathname, callback) {
2014-07-29 00:02:40 +00:00
pathname = Path.resolve(sh.pwd(), pathname);
2014-05-27 18:44:30 +00:00
fs.stat(pathname, function(error, stats) {
if(error) {
callback(error);
return;
}
// If this is a file, delete it and we're done
if(stats.type === 'FILE') {
fs.unlink(pathname, callback);
return;
}
// If it's a dir, check if it's empty
fs.readdir(pathname, function(error, entries) {
if(error) {
callback(error);
return;
}
// If dir is empty, delete it and we're done
if(entries.length === 0) {
fs.rmdir(pathname, callback);
return;
}
// If not, see if we're allowed to delete recursively
if(!options.recursive) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTEMPTY(null, pathname));
2014-05-27 18:44:30 +00:00
return;
}
// Remove each dir entry recursively, then delete the dir.
entries = entries.map(function(filename) {
// Root dir entries absolutely
return Path.join(pathname, filename);
});
2014-07-21 19:28:26 +00:00
async.eachSeries(entries, remove, function(error) {
2014-05-27 18:44:30 +00:00
if(error) {
callback(error);
return;
}
fs.rmdir(pathname, callback);
});
});
});
}
remove(path, callback);
};
/**
* Gets the path to the temporary directory, creating it if not
* present. The directory used is the one specified in
* env.TMP. The callback receives (error, tempDirName).
*/
Shell.prototype.tempDir = function(callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
var tmp = sh.env.get('TMP');
2014-05-27 18:44:30 +00:00
callback = callback || function(){};
// Try and create it, and it will either work or fail
// but either way it's now there.
fs.mkdir(tmp, function(err) {
callback(null, tmp);
});
};
/**
* Recursively creates the directory at `path`. If the parent
* of `path` does not exist, it will be created.
* Based off EnsureDir by Sam X. Xu
* https://www.npmjs.org/package/ensureDir
* MIT License
*/
Shell.prototype.mkdirp = function(path, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
callback = callback || function(){};
if(!path) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('Missing path argument'));
2014-05-27 18:44:30 +00:00
return;
}
else if (path === '/') {
callback();
return;
}
function _mkdirp(path, callback) {
fs.stat(path, function (err, stat) {
if(stat) {
if(stat.isDirectory()) {
callback();
return;
}
else if (stat.isFile()) {
2014-08-20 18:48:40 +00:00
callback(new Errors.ENOTDIR(null, path));
2014-05-27 18:44:30 +00:00
return;
}
}
else if (err && err.code !== 'ENOENT') {
callback(err);
return;
}
else {
var parent = Path.dirname(path);
if(parent === '/') {
fs.mkdir(path, function (err) {
if (err && err.code != 'EEXIST') {
callback(err);
return;
}
callback();
return;
});
}
else {
_mkdirp(parent, function (err) {
if (err) return callback(err);
fs.mkdir(path, function (err) {
if (err && err.code != 'EEXIST') {
callback(err);
return;
}
callback();
return;
});
});
}
}
});
}
_mkdirp(path, callback);
};
/**
* Downloads the file at `url` and saves it to the filesystem.
* The file is saved to a file named with the current date/time
* unless the `options.filename` is present, in which case that
* filename is used instead. The callback receives (error, path).
*/
Shell.prototype.wget = function(url, options, callback) {
2014-07-29 00:02:40 +00:00
var sh = this;
var fs = sh.fs;
2014-05-27 18:44:30 +00:00
if(typeof options === 'function') {
callback = options;
options = {};
}
options = options || {};
callback = callback || function(){};
if(!url) {
2014-08-20 18:48:40 +00:00
callback(new Errors.EINVAL('Missing url argument'));
2014-05-27 18:44:30 +00:00
return;
}
// Grab whatever is after the last / (assuming there is one). Like the real
// wget, we leave query string or hash portions in tact. This assumes a
// properly encoded URL.
// i.e. instead of "/foo?bar/" we would expect "/foo?bar%2F"
var path = options.filename || url.split('/').pop();
2014-07-29 00:02:40 +00:00
path = Path.resolve(sh.pwd(), path);
2014-05-27 18:44:30 +00:00
function onerror() {
callback(new Error('unable to get resource'));
}
Network.download(url, function(err, data) {
if (err || !data) {
return onerror();
}
fs.writeFile(path, data, function(err) {
if(err) {
callback(err);
} else {
callback(null, path);
}
});
});
};
module.exports = Shell;
2014-09-02 19:48:54 +00:00
},{"../../lib/async.js":1,"../encoding.js":14,"../errors.js":15,"../path.js":22,"./environment.js":28,"./network.js":29}],31:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var Constants = _dereq_('./constants.js');
2014-05-27 18:44:30 +00:00
function Stats(fileNode, devName) {
this.node = fileNode.id;
this.dev = devName;
this.size = fileNode.size;
this.nlinks = fileNode.nlinks;
this.atime = fileNode.atime;
this.mtime = fileNode.mtime;
this.ctime = fileNode.ctime;
this.type = fileNode.mode;
}
Stats.prototype.isFile = function() {
return this.type === Constants.MODE_FILE;
};
Stats.prototype.isDirectory = function() {
return this.type === Constants.MODE_DIRECTORY;
};
Stats.prototype.isSymbolicLink = function() {
return this.type === Constants.MODE_SYMBOLIC_LINK;
};
// These will always be false in Filer.
Stats.prototype.isSocket =
Stats.prototype.isFIFO =
Stats.prototype.isCharacterDevice =
Stats.prototype.isBlockDevice =
function() {
return false;
};
module.exports = Stats;
2014-09-02 19:48:54 +00:00
},{"./constants.js":12}],32:[function(_dereq_,module,exports){
2014-05-28 20:00:10 +00:00
var Constants = _dereq_('./constants.js');
2014-05-27 18:44:30 +00:00
2014-07-14 20:01:52 +00:00
function SuperNode(options) {
2014-05-27 18:44:30 +00:00
var now = Date.now();
this.id = Constants.SUPER_NODE_ID;
this.mode = Constants.MODE_META;
2014-07-14 20:01:52 +00:00
this.atime = options.atime || now;
this.ctime = options.ctime || now;
this.mtime = options.mtime || now;
// root node id (randomly generated)
this.rnode = options.rnode;
}
SuperNode.create = function(options, callback) {
options.guid(function(err, rnode) {
if(err) {
callback(err);
return;
}
options.rnode = options.rnode || rnode;
callback(null, new SuperNode(options));
});
2014-05-27 18:44:30 +00:00
};
2014-07-14 20:01:52 +00:00
module.exports = SuperNode;
2014-09-02 19:48:54 +00:00
},{"./constants.js":12}]},{},[19])
(19)
2014-05-28 20:00:10 +00:00
});