Comparing version 1.6.2 to 1.6.3
242
index.js
'use strict'; | ||
var Fireproof = require('./lib/core'); | ||
require('./lib/stats')(Fireproof); | ||
require('./lib/snapshot')(Fireproof); | ||
require('./lib/query')(Fireproof); | ||
require('./lib/read')(Fireproof); | ||
require('./lib/write')(Fireproof); | ||
require('./lib/on-disconnect')(Fireproof); | ||
require('./lib/auth')(Fireproof); | ||
require('./lib/user')(Fireproof); | ||
/** | ||
* Fireproofs an existing Firebase reference, giving it magic promise powers. | ||
* @name Fireproof | ||
* @constructor | ||
* @global | ||
* @param {Firebase} firebaseRef A Firebase reference object. | ||
* @property then A promise shortcut for .once('value'), | ||
* except for references created by .push(), where it resolves on success | ||
* and rejects on failure of the property object. | ||
* @example | ||
* var fp = new Fireproof(new Firebase('https://test.firebaseio.com/something')); | ||
* fp.then(function(snap) { console.log(snap.val()); }); | ||
*/ | ||
function Fireproof(firebaseRef, promise) { | ||
this._ref = firebaseRef; | ||
if (promise && promise.then) { | ||
this.then = promise.then.bind(promise); | ||
} else { | ||
this.then = function(ok, fail) { | ||
return this.once('value', function() {}) | ||
.then(ok || null, fail || null); | ||
}; | ||
} | ||
} | ||
var Q; | ||
Fireproof._checkQ = function() { | ||
if (Q === undefined) { | ||
throw new Error('You must supply a Defer-style promise library to Fireproof!'); | ||
} | ||
return Q; | ||
}; | ||
Fireproof._nextTick = function(fn) { | ||
if (process && process.nextTick) { | ||
process.nextTick(fn); | ||
} else { | ||
setTimeout(fn, 0); | ||
} | ||
}; | ||
Fireproof._handleError = function(onComplete) { | ||
var deferred = Fireproof._checkQ().defer(); | ||
var rv = function(err, val) { | ||
if (onComplete && typeof onComplete === 'function') { | ||
Fireproof._nextTick(function() { | ||
onComplete(err); | ||
}); | ||
} | ||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
deferred.resolve(val); | ||
} | ||
}; | ||
rv.promise = deferred.promise; | ||
return rv; | ||
}; | ||
/** | ||
* Tell Fireproof to use a given promise library from now on. | ||
* @method Fireproof.bless | ||
* @param {Q} Q a Q-style promise constructor with at least defer(). | ||
* @throws if you don't provide a valid Deferred-style promise library. | ||
*/ | ||
Fireproof.bless = function(newQ) { | ||
if (newQ === undefined || newQ === null || typeof(newQ.defer) !== 'function') { | ||
throw new Error('You tried to give Fireproof an invalid Q library!'); | ||
} | ||
var deferred = newQ.defer(); | ||
if (deferred === undefined || deferred === null || | ||
deferred.promise === undefined || deferred.promise === null || | ||
typeof(deferred.reject) !== 'function' || | ||
typeof(deferred.resolve) !== 'function' || | ||
typeof(deferred.promise.then) !== 'function') { | ||
throw new Error('You tried to give Fireproof an invalid Q library!'); | ||
} | ||
Q = newQ; | ||
}; | ||
/* FIXME(goldibex): Find out the reason for this demonry. | ||
* For reasons completely incomprehensible to me, some type of race condition | ||
* is possible if multiple Fireproof references attempt authentication at the | ||
* same time, the result of which is one or more of the promises will never | ||
* resolve. | ||
* Accordingly, it is necessary that we wrap authentication actions in a | ||
* global lock. This is accomplished using setInterval. No, I don't like it | ||
* any more than you do. | ||
*/ | ||
var authing = false; | ||
/** | ||
* Delegates Firebase#auth. | ||
* @method Fireproof#auth | ||
* @param {string} authToken Firebase authentication token. | ||
* @param {function=} onComplete Callback on initial completion. | ||
* @param {function=} onCancel Callback if we ever get disconnected. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.auth = function(authToken, onComplete, onCancel) { | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
var authIntervalId = setInterval(function() { | ||
if (!authing) { | ||
authing = true; | ||
self._ref.auth(authToken, function(err, info) { | ||
authing = false; | ||
clearInterval(authIntervalId); | ||
if (err !== null) { | ||
deferred.reject(err); | ||
} else { | ||
deferred.resolve(info); | ||
} | ||
if (typeof onComplete === 'function') { | ||
Fireproof._nextTick(function() { | ||
onComplete(err, info); | ||
}); | ||
} | ||
}, onCancel); | ||
} | ||
}, 1); | ||
return deferred.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#child, wrapping the child in fireproofing. | ||
* @method Fireproof#child | ||
* @param {string} childPath The subpath to refer to. | ||
* @returns {Fireproof} A reference to the child path. | ||
*/ | ||
Fireproof.prototype.child = function(childPath) { | ||
return new Fireproof(this._ref.child(childPath)); | ||
}; | ||
/** | ||
* Delegates Firebase#parent, wrapping the child in fireproofing. | ||
* @method Fireproof#parent | ||
* @returns {Fireproof} A ref to the parent path, or null if there is none. | ||
*/ | ||
Fireproof.prototype.parent = function() { | ||
if (this._ref.parent() === null) { | ||
return null; | ||
} else { | ||
return new Fireproof(this._ref.parent()); | ||
} | ||
}; | ||
/** | ||
* Delegates Firebase#root, wrapping the root in fireproofing. | ||
* @method Fireproof#root | ||
* @returns {Fireproof} A ref to the root. | ||
*/ | ||
Fireproof.prototype.root = function() { | ||
return new Fireproof(this._ref.root()); | ||
}; | ||
/** | ||
* Hands back the original Firebase reference. | ||
* @method Fireproof#toFirebase | ||
* @returns {Firebase} The proxied Firebase reference. | ||
*/ | ||
Fireproof.prototype.toFirebase = function() { | ||
return this._ref; | ||
}; | ||
/** | ||
* Delegates Firebase#name. | ||
* @method Fireproof#name | ||
* @returns {string} The last component of this reference object's path. | ||
*/ | ||
Fireproof.prototype.name = function() { | ||
return this._ref.name(); | ||
}; | ||
/** | ||
* Delegates Firebase#toString. | ||
* @method Fireproof#toString | ||
* @returns {string} The full URL of this reference object. | ||
*/ | ||
Fireproof.prototype.toString = function() { | ||
return this._ref.toString(); | ||
}; | ||
module.exports = Fireproof; |
240
lib/auth.js
'use strict'; | ||
function findOptions(onComplete, options) { | ||
@@ -14,159 +12,155 @@ | ||
/** | ||
* Delegates Firebase#authWithCustomToken. | ||
* @method Fireproof#authWithCustomToken | ||
* @param {String} authToken | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithCustomToken = function(authToken, onComplete, options) { | ||
module.exports = function(Fireproof) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithCustomToken(authToken, oc, options); | ||
/** | ||
* Delegates Firebase#authWithCustomToken. | ||
* @method Fireproof#authWithCustomToken | ||
* @param {String} authToken | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithCustomToken = function(authToken, onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithCustomToken(authToken, oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#authAnonymously. | ||
* @method Fireproof#authAnonymously | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authAnonymously = function(onComplete, options) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authAnonymously(oc, options); | ||
/** | ||
* Delegates Firebase#authAnonymously. | ||
* @method Fireproof#authAnonymously | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authAnonymously = function(onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authAnonymously(oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#authWithPassword. | ||
* @method Fireproof#authWithPassword | ||
* @param {Object} credentials Should include `email` and `password`. | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithPassword = function(credentials, onComplete, options) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithPassword(credentials, oc, options); | ||
/** | ||
* Delegates Firebase#authWithPassword. | ||
* @method Fireproof#authWithPassword | ||
* @param {Object} credentials Should include `email` and `password`. | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithPassword = function(credentials, onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithPassword(credentials, oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#authWithOAuthPopup. | ||
* @method Fireproof#authWithOAuthPopup | ||
* @param {String} provider | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthPopup = function(provider, onComplete, options) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthPopup(provider, oc, options); | ||
/** | ||
* Delegates Firebase#authWithOAuthPopup. | ||
* @method Fireproof#authWithOAuthPopup | ||
* @param {String} provider | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthPopup = function(provider, onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthPopup(provider, oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#authWithOAuthRedirect. | ||
* @method Fireproof#authWithOAuthRedirect | ||
* @param {String} provider | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthRedirect = function(provider, onComplete, options) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthRedirect(provider, oc, options); | ||
/** | ||
* Delegates Firebase#authWithOAuthRedirect. | ||
* @method Fireproof#authWithOAuthRedirect | ||
* @param {String} provider | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthRedirect = function(provider, onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthRedirect(provider, oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#authWithOAuthPopup. | ||
* @method Fireproof#authWithOAuthPopup | ||
* @param {String} provider | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthToken = function(provider, credentials, onComplete, options) { | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthRedirect(provider, credentials, oc, options); | ||
/** | ||
* Delegates Firebase#authWithOAuthPopup. | ||
* @method Fireproof#authWithOAuthPopup | ||
* @param {String} provider | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @param {Object} [options] | ||
* @returns {Promise} that resolves on auth success and rejects on auth failure. | ||
*/ | ||
Fireproof.prototype.authWithOAuthToken = function(provider, credentials, onComplete, options) { | ||
return oc.promise; | ||
var oc = Fireproof._handleError(onComplete); | ||
options = findOptions(onComplete, options); | ||
this._ref.authWithOAuthRedirect(provider, credentials, oc, options); | ||
}; | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#getAuth. | ||
* @method Fireproof#getAuth | ||
* @returns {Object} user info object, or null otherwise. | ||
*/ | ||
Fireproof.prototype.getAuth = function() { | ||
return this._ref.getAuth(); | ||
}; | ||
/** | ||
* Delegates Firebase#getAuth. | ||
* @method Fireproof#getAuth | ||
* @returns {Object} user info object, or null otherwise. | ||
*/ | ||
Fireproof.prototype.getAuth = function() { | ||
return this._ref.getAuth(); | ||
}; | ||
/** | ||
* Delegates Firebase#onAuth. | ||
* @method Fireproof#onAuth | ||
* @param {Function} onComplete Gets called on auth change. | ||
* @param {Object} [context] | ||
*/ | ||
Fireproof.prototype.onAuth = function(onComplete, context) { | ||
return this._ref.onAuth(onComplete, context); | ||
}; | ||
/** | ||
* Delegates Firebase#onAuth. | ||
* @method Fireproof#onAuth | ||
* @param {Function} onComplete Gets called on auth change. | ||
* @param {Object} [context] | ||
*/ | ||
Fireproof.prototype.onAuth = function(onComplete, context) { | ||
return this._ref.onAuth(onComplete, context); | ||
}; | ||
/** | ||
* Delegates Firebase#offAuth. | ||
* @method Fireproof#offAuth | ||
* @param {Function} onComplete The function previously passed to onAuth. | ||
* @param {Object} [context] | ||
*/ | ||
Fireproof.prototype.offAuth = function(onComplete, context) { | ||
return this._ref.offAuth(onComplete, context); | ||
}; | ||
/** | ||
* Delegates Firebase#offAuth. | ||
* @method Fireproof#offAuth | ||
* @param {Function} onComplete The function previously passed to onAuth. | ||
* @param {Object} [context] | ||
*/ | ||
Fireproof.prototype.offAuth = function(onComplete, context) { | ||
return this._ref.offAuth(onComplete, context); | ||
}; | ||
/** | ||
* Delegates Firebase#unauth. | ||
* @method Fireproof#unauth | ||
*/ | ||
Fireproof.prototype.unauth = function() { | ||
return this._ref.unauth(); | ||
}; | ||
/** | ||
* Delegates Firebase#unauth. | ||
* @method Fireproof#unauth | ||
*/ | ||
Fireproof.prototype.unauth = function() { | ||
return this._ref.unauth(); | ||
}; | ||
}; |
'use strict'; | ||
function OnDisconnect(ref) { | ||
this._od = ref.onDisconnect(); | ||
} | ||
module.exports = function(Fireproof) { | ||
/** | ||
* Delegates onDisconnect()#cancel. | ||
* @method Fireproof#onDisconnect#cancel | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} | ||
*/ | ||
OnDisconnect.prototype.cancel = function(cb) { | ||
function OnDisconnect(ref) { | ||
this._od = ref.onDisconnect(); | ||
} | ||
var handler = Fireproof._handleError(cb); | ||
this._od.cancel(handler); | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates onDisconnect()#cancel. | ||
* @method onDisconnect()#cancel | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
OnDisconnect.prototype.cancel = function(cb) { | ||
var handler = Fireproof._handleError(cb); | ||
/** | ||
* Delegates onDisconnect()#remove. | ||
* @method Fireproof#onDisconnect#remove | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} | ||
*/ | ||
OnDisconnect.prototype.remove = function(cb) { | ||
this._od.cancel(handler); | ||
var handler = Fireproof._handleError(cb); | ||
this._od.remove(handler); | ||
return handler.promise; | ||
return handler.promise; | ||
}; | ||
}; | ||
/** | ||
* Delegates onDisconnect()#set. | ||
* @method Fireproof#onDisconnect#set | ||
* @param {*} value Value to set on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} | ||
*/ | ||
OnDisconnect.prototype.set = function(value, cb) { | ||
/** | ||
* Delegates onDisconnect()#remove. | ||
* @method onDisconnect()#remove | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
OnDisconnect.prototype.remove = function(cb) { | ||
var handler = Fireproof._handleError(cb); | ||
this._od.set(value, handler); | ||
return handler.promise; | ||
var handler = Fireproof._handleError(cb); | ||
}; | ||
this._od.remove(handler); | ||
return handler.promise; | ||
/** | ||
* Delegates onDisconnect()#setWithPriority. | ||
* @method Fireproof#onDisconnect#setWithPriority | ||
* @param {*} value Value to set on the ref on disconnect. | ||
* @param {*} priority Priority to set on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} | ||
*/ | ||
OnDisconnect.prototype.setWithPriority = function(value, priority, cb) { | ||
}; | ||
var handler = Fireproof._handleError(cb); | ||
this._od.setWithPriority(value, priority, handler); | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates onDisconnect()#set. | ||
* @method onDisconnect()#set | ||
* @param {*} value Value to set on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
OnDisconnect.prototype.set = function(value, cb) { | ||
var handler = Fireproof._handleError(cb); | ||
/** | ||
* Delegates onDisconnect()#update. | ||
* @method Fireproof#onDisconnect#update | ||
* @param {*} value Value to update on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} | ||
*/ | ||
OnDisconnect.prototype.update = function(value, cb) { | ||
this._od.set(value, handler); | ||
var handler = Fireproof._handleError(cb); | ||
this._od.update(value, handler); | ||
return handler.promise; | ||
return handler.promise; | ||
}; | ||
}; | ||
/** | ||
* Delegates Fireproof#onDisconnect. | ||
* @method Fireproof#onDisconnect | ||
* @returns {Fireproof.OnDisconnect} | ||
*/ | ||
Fireproof.prototype.onDisconnect = function() { | ||
return new OnDisconnect(this._ref); | ||
}; | ||
/** | ||
* Delegates onDisconnect()#setWithPriority. | ||
* @method onDisconnect()#setWithPriority | ||
* @param {*} value Value to set on the ref on disconnect. | ||
* param {*} priority Priority to set on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
OnDisconnect.prototype.setWithPriority = function(value, priority, cb) { | ||
var handler = Fireproof._handleError(cb); | ||
this._od.setWithPriority(value, priority, handler); | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates onDisconnect()#update. | ||
* @method onDisconnect()#update | ||
* @param {*} value Value to update on the ref on disconnect. | ||
* @param {function=} callback Firebase callback. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
OnDisconnect.prototype.update = function(value, cb) { | ||
var handler = Fireproof._handleError(cb); | ||
this._od.update(value, handler); | ||
return handler.promise; | ||
}; | ||
Fireproof.prototype.onDisconnect = function() { | ||
return new OnDisconnect(this._ref); | ||
}; | ||
}; |
'use strict'; | ||
/** | ||
* Delegates Firebase#limit. | ||
* @method Fireproof#limit | ||
* @param {Number} limit | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.limit = function(limit) { | ||
return new Fireproof(this._ref.limit(limit)); | ||
}; | ||
module.exports = function(Fireproof) { | ||
/** | ||
* Delegates Firebase#limit. | ||
* @method Fireproof#limit | ||
* @param {Number} limit | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.limit = function(limit) { | ||
return new Fireproof(this._ref.limit(limit)); | ||
}; | ||
/** | ||
* Delegates Firebase#startAt. | ||
* @method Fireproof#startAt | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.startAt = function(priority, name) { | ||
return new Fireproof(this._ref.startAt(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#startAt. | ||
* @method Fireproof#startAt | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.startAt = function(priority, name) { | ||
return new Fireproof(this._ref.startAt(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#endAt. | ||
* @method Fireproof#endAt | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.endAt = function(priority, name) { | ||
return new Fireproof(this._ref.endAt(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#endAt. | ||
* @method Fireproof#endAt | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.endAt = function(priority, name) { | ||
return new Fireproof(this._ref.endAt(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#equalTo. | ||
* @method Fireproof#equalTo | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.equalTo = function(priority, name) { | ||
return new Fireproof(this._ref.equalTo(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#equalTo. | ||
* @method Fireproof#equalTo | ||
* @param {object} priority | ||
* @param {string} name | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.equalTo = function(priority, name) { | ||
return new Fireproof(this._ref.equalTo(priority, name)); | ||
}; | ||
/** | ||
* Delegates Firebase#ref. | ||
* @method Fireproof#ref | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.ref = function() { | ||
return new Fireproof(this._ref.ref()); | ||
}; | ||
/** | ||
* Delegates Firebase#ref. | ||
* @method Fireproof#ref | ||
* @returns {Fireproof} | ||
*/ | ||
Fireproof.prototype.ref = function() { | ||
return new Fireproof(this._ref.ref()); | ||
}; |
255
lib/read.js
'use strict'; | ||
/** | ||
* Delegates Firebase#transaction. | ||
* @method Fireproof#transaction | ||
* @param {function} updateFunction | ||
* @param {function} onComplete | ||
* @param {boolean=} applyLocally | ||
* @returns {Promise} an Object with two properties: 'committed' and 'snapshot'. | ||
*/ | ||
Fireproof.prototype.transaction = function(updateFunction, onComplete, applyLocally) { | ||
module.exports = function(Fireproof) { | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
/** | ||
* Delegates Firebase#transaction. | ||
* @method Fireproof#transaction | ||
* @param {function} updateFunction | ||
* @param {function} onComplete | ||
* @param {boolean=} applyLocally | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.transaction = function(updateFunction, onComplete, applyLocally) { | ||
Fireproof.stats._addListener(self.toString()); | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
self._ref.transaction(updateFunction, function(err, committed, snap) { | ||
Fireproof.stats._addListener(self.toString()); | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
self._ref.transaction(updateFunction, function(err, committed, snap) { | ||
Fireproof._nextTick(function() { | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
if (onComplete) { | ||
onComplete(err, committed, snap); | ||
} | ||
Fireproof._nextTick(function() { | ||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
if (onComplete) { | ||
onComplete(err, committed, snap); | ||
} | ||
deferred.resolve({ | ||
committed: committed, | ||
snapshot: snap | ||
}); | ||
if (err) { | ||
deferred.reject(err); | ||
} else { | ||
} | ||
deferred.resolve({ | ||
committed: committed, | ||
snapshot: snap | ||
}); | ||
}); | ||
} | ||
}, applyLocally); | ||
}); | ||
return deferred.promise; | ||
}, applyLocally); | ||
}; | ||
return deferred.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#on. | ||
* @method Fireproof#on | ||
* @param {string} eventType 'value', 'child_added', 'child_changed', 'child_moved', | ||
* or 'child_removed' | ||
* @param {function} callback | ||
* @param {function=} cancelCallback | ||
* @param {object=} context | ||
* @returns {function} Your callback parameter wrapped in fireproofing. Use | ||
* this return value, not your own copy of callback, to call .off(). It also | ||
* functions as a promise that resolves with a {FireproofSnapshot}. | ||
*/ | ||
Fireproof.prototype.on = function(eventType, callback, cancelCallback, context) { | ||
var deferred = Fireproof._checkQ().defer(), | ||
resolved = false, | ||
self = this; | ||
/** | ||
* Delegates Firebase#on. | ||
* @method Fireproof#on | ||
* @param {string} eventType | ||
* @param {function} callback | ||
* @param {function=} cancelCallback | ||
* @param {object=} context | ||
* @returns {function} Your callback parameter wrapped in fireproofing. Use | ||
* this return value, not your own copy of callback, to call .off(). It also | ||
* functions as a promise that resolves on success and rejects on failure. | ||
*/ | ||
Fireproof.prototype.on = function(eventType, callback, cancelCallback, context) { | ||
Fireproof.stats._addListener(self.toString()); | ||
var deferred = Fireproof._checkQ().defer(), | ||
resolved = false, | ||
self = this; | ||
if (typeof callback !== 'function') { | ||
callback = function() {}; | ||
} | ||
Fireproof.stats._addListener(self.toString()); | ||
if (typeof cancelCallback !== 'function') { | ||
cancelCallback = function() {}; | ||
} | ||
if (typeof callback !== 'function') { | ||
callback = function() {}; | ||
} | ||
var callbackHandler = function(snap, prev) { | ||
if (typeof cancelCallback !== 'function') { | ||
cancelCallback = function() {}; | ||
} | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
var callbackHandler = function(snap, prev) { | ||
Fireproof._nextTick(function() { | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
callback(snap, prev); | ||
if (!resolved) { | ||
resolved = true; | ||
deferred.resolve(snap, prev); | ||
} | ||
Fireproof._nextTick(function() { | ||
}); | ||
callback(snap, prev); | ||
if (!resolved) { | ||
resolved = true; | ||
deferred.resolve(snap, prev); | ||
} | ||
}; | ||
}); | ||
callbackHandler.then = deferred.promise.then.bind(deferred.promise); | ||
}; | ||
self._ref.on(eventType, callbackHandler, function(err) { | ||
callbackHandler.then = deferred.promise.then.bind(deferred.promise); | ||
Fireproof.stats._removeListener(self.toString()); | ||
self._ref.on(eventType, callbackHandler, function(err) { | ||
Fireproof._nextTick(function() { | ||
cancelCallback(err); | ||
Fireproof.stats._removeListener(self.toString()); | ||
if (!resolved) { | ||
resolved = true; | ||
deferred.reject(err); | ||
} | ||
Fireproof._nextTick(function() { | ||
cancelCallback(err); | ||
}); | ||
if (!resolved) { | ||
resolved = true; | ||
deferred.reject(err); | ||
} | ||
}, context); | ||
}); | ||
return callbackHandler; | ||
}, context); | ||
}; | ||
return callbackHandler; | ||
}; | ||
/** | ||
* Delegates Firebase#off. | ||
* @method Fireproof#off | ||
* @param {string} eventType | ||
* @param {function=} callback | ||
* @param {object=} context | ||
*/ | ||
Fireproof.prototype.off = function(eventType, callback, context) { | ||
Fireproof.stats._removeListener(this.toString()); | ||
this._ref.off(eventType, callback, context); | ||
/** | ||
* Delegates Firebase#off. | ||
* @method Fireproof#off | ||
* @param {string} eventType | ||
* @param {function=} callback | ||
* @param {object=} context | ||
*/ | ||
Fireproof.prototype.off = function(eventType, callback, context) { | ||
}; | ||
Fireproof.stats._removeListener(this.toString()); | ||
this._ref.off(eventType, callback, context); | ||
}; | ||
/** | ||
* Delegates Firebase#once. | ||
* @method Fireproof#once | ||
* @param {object} eventType 'value', 'child_added', 'child_changed', 'child_moved', | ||
* or 'child_removed' | ||
* @param {function} successCallback | ||
* @param {function=} failureCallback | ||
* @param {object=} context | ||
* @returns {Promise} Resolves with {FireproofSnapshot}. | ||
*/ | ||
Fireproof.prototype.once = function(eventType, successCallback, failureCallback, context) { | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
/** | ||
* Delegates Firebase#once. | ||
* @method Fireproof#once | ||
* @param {object} eventType | ||
* @param {function} successCallback | ||
* @param {function=} failureCallback | ||
* @param {object=} context | ||
* @returns {Promise} Resolves on success and rejects on failure. | ||
*/ | ||
Fireproof.prototype.once = function(eventType, successCallback, failureCallback, context) { | ||
Fireproof.stats._addListener(self.toString()); | ||
var deferred = Fireproof._checkQ().defer(), | ||
self = this; | ||
if (typeof successCallback !== 'function') { | ||
successCallback = function() {}; | ||
} | ||
Fireproof.stats._addListener(self.toString()); | ||
if (typeof failureCallback !== 'function') { | ||
failureCallback = function() {}; | ||
} | ||
if (typeof successCallback !== 'function') { | ||
successCallback = function() {}; | ||
} | ||
self._ref.once(eventType, function(snap) { | ||
if (typeof failureCallback !== 'function') { | ||
failureCallback = function() {}; | ||
} | ||
Fireproof.stats._removeListener(self.toString()); | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
self._ref.once(eventType, function(snap) { | ||
deferred.resolve(snap); | ||
Fireproof._nextTick(function() { | ||
successCallback(snap); | ||
}); | ||
Fireproof.stats._removeListener(self.toString()); | ||
Fireproof.stats._record('read', self.toString()); | ||
snap = new Fireproof.Snapshot(snap); | ||
}, function(err) { | ||
deferred.resolve(snap); | ||
Fireproof._nextTick(function() { | ||
successCallback(snap); | ||
}); | ||
Fireproof.stats._removeListener(self.toString()); | ||
deferred.reject(err); | ||
Fireproof._nextTick(function() { | ||
failureCallback(err); | ||
}); | ||
}, function(err) { | ||
}, context); | ||
Fireproof.stats._removeListener(self.toString()); | ||
deferred.reject(err); | ||
Fireproof._nextTick(function() { | ||
failureCallback(err); | ||
}); | ||
return deferred.promise; | ||
}, context); | ||
}; | ||
return deferred.promise; | ||
}; | ||
}; |
'use strict'; | ||
/** | ||
* A delegate object for Firebase's Snapshot. | ||
* @name FireproofSnapshot | ||
* @constructor | ||
* @global | ||
* @private | ||
* @param {Snapshot} snap The snapshot to delegate to. | ||
*/ | ||
function FireproofSnapshot(snap) { | ||
this._snap = snap; | ||
} | ||
Fireproof.Snapshot = FireproofSnapshot; | ||
module.exports = function(Fireproof) { | ||
/** | ||
* Delegates DataSnapshot#child. | ||
* @method FireproofSnapshot#child | ||
* @param {String} path Path of the child. | ||
* @returns {FireproofSnapshot} The snapshot of the child. | ||
*/ | ||
FireproofSnapshot.prototype.child = function(path) { | ||
return new FireproofSnapshot(this._snap.child(path)); | ||
}; | ||
function FireproofSnapshot(snap) { | ||
this._snap = snap; | ||
} | ||
/** | ||
* Delegates DataSnapshot#forEach. | ||
* @method FireproofSnapshot#forEach | ||
* @param {cb} eachFn The function to call on each child. | ||
* @returns {Boolean} True if a callback returned true and cancelled enumeration. | ||
*/ | ||
FireproofSnapshot.prototype.forEach = function(cb) { | ||
return this._snap.forEach(function(childSnap) { | ||
if (cb(new FireproofSnapshot(childSnap)) === true) { | ||
return true; | ||
} | ||
}); | ||
/** | ||
* Delegates DataSnapshot#child. | ||
* @method DataSnapshot#child | ||
* @param {String} path Path of the child. | ||
* @returns {FireproofSnapshot} The snapshot of the child. | ||
*/ | ||
FireproofSnapshot.prototype.child = function(path) { | ||
return new FireproofSnapshot(this._snap.child(path)); | ||
}; | ||
}; | ||
/** | ||
* Delegates DataSnapshot#forEach. | ||
* @method DataSnapshot#forEach | ||
* @param {cb} eachFn The function to call on each child. | ||
* @returns {Boolean} True if a callback returned true and cancelled enumeration. | ||
*/ | ||
FireproofSnapshot.prototype.forEach = function(cb) { | ||
/** | ||
* Delegates DataSnapshot#hasChild. | ||
* @method FireproofSnapshot#hasChild | ||
* @param {cb} eachFn The function to call on each child. | ||
* @returns {Boolean} True if the snap has the specified child. | ||
*/ | ||
FireproofSnapshot.prototype.hasChild = function(name) { | ||
return this._snap.hasChild(name); | ||
}; | ||
return this._snap.forEach(function(childSnap) { | ||
if (cb(new FireproofSnapshot(childSnap)) === true) { | ||
return true; | ||
} | ||
}); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#hasChildren. | ||
* @method FireproofSnapshot#hasChildren | ||
* @returns {Boolean} True if the snapshot has children. | ||
*/ | ||
FireproofSnapshot.prototype.hasChildren = function() { | ||
return this._snap.hasChildren(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#hasChild. | ||
* @method DataSnapshot#hasChild | ||
* @param {cb} eachFn The function to call on each child. | ||
* @returns {Boolean} True if the snap has the specified child. | ||
*/ | ||
FireproofSnapshot.prototype.hasChild = function(name) { | ||
return this._snap.hasChild(name); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#numChildren. | ||
* @method FireproofSnapshot#numChildren | ||
* @returns {Number} The number of children the snapshot has. | ||
*/ | ||
FireproofSnapshot.prototype.numChildren = function() { | ||
return this._snap.numChildren(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#hasChildren. | ||
* @method DataSnapshot#hasChildren | ||
* @returns {Boolean} True if the snapshot has children. | ||
*/ | ||
FireproofSnapshot.prototype.hasChildren = function() { | ||
return this._snap.hasChildren(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#name. | ||
* @method FireproofSnapshot#name | ||
* @returns {String} The last part of the snapshot's path. | ||
*/ | ||
FireproofSnapshot.prototype.name = function() { | ||
return this._snap.name(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#numChildren. | ||
* @method DataSnapshot#numChildren | ||
* @returns {Number} The number of children the snapshot has. | ||
*/ | ||
FireproofSnapshot.prototype.numChildren = function() { | ||
return this._snap.numChildren(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#val. | ||
* @method FireproofSnapshot#val | ||
* @returns {*} The Javascript deserialization of the snapshot. | ||
*/ | ||
FireproofSnapshot.prototype.val = function() { | ||
return this._snap.val(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#name. | ||
* @method DataSnapshot#name | ||
* @returns {String} The last part of the snapshot's path. | ||
*/ | ||
FireproofSnapshot.prototype.name = function() { | ||
return this._snap.name(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#ref. | ||
* @method FireproofSnapshot#ref | ||
* @returns {Fireproof} The Fireproof object for the snap's location. | ||
*/ | ||
FireproofSnapshot.prototype.ref = function() { | ||
return new Fireproof(this._snap.ref()); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#val. | ||
* @method DataSnapshot#val | ||
* @returns {*} The Javascript deserialization of the snapshot. | ||
*/ | ||
FireproofSnapshot.prototype.val = function() { | ||
return this._snap.val(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#getPriority. | ||
* @method FireproofSnapshot#getPriority | ||
* @returns {*} The snapshot's priority. | ||
*/ | ||
FireproofSnapshot.prototype.getPriority = function() { | ||
return this._snap.getPriority(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#ref. | ||
* @method DataSnapshot#ref | ||
* @returns {Fireproof} The Fireproof object for the snap's location. | ||
*/ | ||
FireproofSnapshot.prototype.ref = function() { | ||
return new Fireproof(this._snap.ref()); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#getPriority. | ||
* @method DataSnapshot#getPriority | ||
* @returns {*} The snapshot's priority. | ||
*/ | ||
FireproofSnapshot.prototype.getPriority = function() { | ||
return this._snap.getPriority(); | ||
}; | ||
/** | ||
* Delegates DataSnapshot#exportVal. | ||
* @method DataSnapshot#exportVal | ||
* @returns {*} The Firebase export object of the snapshot. | ||
*/ | ||
FireproofSnapshot.prototype.exportVal = function() { | ||
return this._snap.exportVal(); | ||
}; | ||
Fireproof.Snapshot = FireproofSnapshot; | ||
/** | ||
* Delegates DataSnapshot#exportVal. | ||
* @method FireproofSnapshot#exportVal | ||
* @returns {*} The Firebase export object of the snapshot. | ||
*/ | ||
FireproofSnapshot.prototype.exportVal = function() { | ||
return this._snap.exportVal(); | ||
}; |
246
lib/stats.js
'use strict'; | ||
/** | ||
* Statistics about Firebase usage. | ||
* @namespace Fireproof.stats | ||
* @property {object} events | ||
* @property {object} listeners | ||
* @static | ||
*/ | ||
Fireproof.stats = { }; | ||
module.exports = function(Fireproof) { | ||
/** | ||
* Resets the count of Firebase operations back to 0. | ||
* @method reset | ||
* @memberof Fireproof.stats | ||
*/ | ||
Fireproof.stats.reset = function() { | ||
Fireproof.stats = { }; | ||
Fireproof.stats.events = { | ||
/** | ||
* Resets the count of Firebase operations back to 0. | ||
* @method Fireproof.stats.reset | ||
* @method Fireproof.stats.reset | ||
*/ | ||
Fireproof.stats.reset = function() { | ||
read: [], | ||
write: [], | ||
update: [] | ||
Fireproof.stats.events = { | ||
read: [], | ||
write: [], | ||
update: [] | ||
}; | ||
}; | ||
}; | ||
/** | ||
* Resets the count of Firebase listeners back to 0. | ||
* @method Fireproof.stats.reset | ||
* @method Fireproof.stats.reset | ||
*/ | ||
Fireproof.stats.resetListeners = function() { | ||
Fireproof.stats.listeners = {}; | ||
}; | ||
/** | ||
* Records a Firebase operation. | ||
* @private | ||
* @method Fireproof.stats._record | ||
* @param {String} kind The kind of event (read, write, or update). | ||
* @param {String} path The Firebase path to the event. | ||
*/ | ||
Fireproof.stats._record = function(name, path) { | ||
Fireproof.stats.events[name].push({ path: path, time: Date.now() }); | ||
}; | ||
/** | ||
* Resets the count of Firebase listeners back to 0. | ||
* @method resetListeners | ||
* @memberof Fireproof.stats | ||
*/ | ||
Fireproof.stats.resetListeners = function() { | ||
Fireproof.stats.listeners = {}; | ||
}; | ||
/** | ||
* Records a Firebase operation. | ||
* @private | ||
* @param {String} kind The kind of event (read, write, or update). | ||
* @param {String} path The Firebase path to the event. | ||
*/ | ||
Fireproof.stats._record = function(name, path) { | ||
Fireproof.stats.events[name].push({ path: path, time: Date.now() }); | ||
}; | ||
/** | ||
* Adds a Firebase listener. | ||
* @private | ||
* @method Fireproof.stats._addListener | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._addListener = function(path) { | ||
if (!Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path] = 1; | ||
} else { | ||
Fireproof.stats.listeners[path]++; | ||
} | ||
/** | ||
* Adds a Firebase listener. | ||
* @private | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._addListener = function(path) { | ||
}; | ||
if (!Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path] = 1; | ||
} else { | ||
Fireproof.stats.listeners[path]++; | ||
} | ||
}; | ||
/** | ||
* Removes a Firebase listener. | ||
* @private | ||
* @method Fireproof.stats._removeListener | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._removeListener = function(path) { | ||
if (Fireproof.stats.listeners[path] === 1) { | ||
delete Fireproof.stats.listeners[path]; | ||
} else if (Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path]--; | ||
} | ||
/** | ||
* Removes a Firebase listener. | ||
* @private | ||
* @param {String} path The Firebase path of the listener. | ||
*/ | ||
Fireproof.stats._removeListener = function(path) { | ||
}; | ||
if (Fireproof.stats.listeners[path] === 1) { | ||
delete Fireproof.stats.listeners[path]; | ||
} else if (Fireproof.stats.listeners[path]) { | ||
Fireproof.stats.listeners[path]--; | ||
} | ||
}; | ||
/** | ||
* Gets data about listeners on Firebase locations. | ||
* @method Fireproof.stats.getListeners | ||
* @returns {Object} Listener counts keyed by Firebase path. | ||
*/ | ||
Fireproof.stats.getListeners = function() { | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
/** | ||
* Gets data about listeners on Firebase locations. | ||
* @method getListeners | ||
* @memberof Fireproof.stats | ||
* @returns {Object} Listener counts keyed by Firebase path. | ||
*/ | ||
Fireproof.stats.getListeners = function() { | ||
acc[k] = Fireproof.stats.listeners[k]; | ||
return acc; | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
}, {}); | ||
acc[k] = Fireproof.stats.listeners[k]; | ||
return acc; | ||
}; | ||
}, {}); | ||
}; | ||
/** | ||
* Gets the total number of listeners on Firebase locations. | ||
* @method Fireproof.stats.getListenerCount | ||
* @returns {Number} The total number of Firebase listeners presently operating. | ||
*/ | ||
Fireproof.stats.getListenerCount = function() { | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
return acc + Fireproof.stats.listeners[k]; | ||
}, 0); | ||
/** | ||
* Gets the total number of listeners on Firebase locations. | ||
* @method getListenerCount | ||
* @memberof Fireproof.stats | ||
* @returns {Number} The total number of Firebase listeners presently operating. | ||
*/ | ||
Fireproof.stats.getListenerCount = function() { | ||
}; | ||
return Object.keys(Fireproof.stats.listeners) | ||
.reduce(function(acc, k) { | ||
return acc + Fireproof.stats.listeners[k]; | ||
}, 0); | ||
/** | ||
* Gets the per-operation, per-path counts of Firebase operations. | ||
* @method Fireproof.stats.getPathCounts | ||
* @returns {Object} An object with three keys: "read", "write", | ||
* and "update". Each key has an object value, of which the keys are Firebase | ||
* paths and the values are counts. | ||
*/ | ||
Fireproof.stats.getPathCounts = function() { | ||
}; | ||
return Object.keys(Fireproof.stats.events) | ||
.reduce(function(result, statName) { | ||
/** | ||
* Gets the per-operation, per-path counts of Firebase operations. | ||
* @method getPathCounts | ||
* @memberof Fireproof.stats | ||
* @returns {Object} An object with three keys: "read", "write", | ||
* and "update". Each key has an object value, of which the keys are Firebase | ||
* paths and the values are counts. | ||
*/ | ||
Fireproof.stats.getPathCounts = function() { | ||
result[statName] = Fireproof.stats.events[statName] | ||
.reduce(function(acc, datum) { | ||
return Object.keys(Fireproof.stats.events) | ||
.reduce(function(result, statName) { | ||
if (acc[datum.path]) { | ||
acc[datum.path]++; | ||
} else { | ||
acc[datum.path] = 1; | ||
} | ||
result[statName] = Fireproof.stats.events[statName] | ||
.reduce(function(acc, datum) { | ||
return acc; | ||
if (acc[datum.path]) { | ||
acc[datum.path]++; | ||
} else { | ||
acc[datum.path] = 1; | ||
} | ||
}, {}); | ||
return acc; | ||
return result; | ||
}, {}); | ||
}; | ||
return result; | ||
/** | ||
* Gets the per-operation counts of Firebase operations. | ||
* @methods Fireproof.stats.getCounts | ||
* @returns {Object} An object with three keys: "read", "write", and | ||
* "update". The values are the counts of operations under those headings. | ||
*/ | ||
Fireproof.stats.getCounts = function() { | ||
}, {}); | ||
return Object.keys(Fireproof.stats.events) | ||
.reduce(function(result, statName) { | ||
}; | ||
result[statName] = Fireproof.stats.events[statName].length; | ||
return result; | ||
/** | ||
* Gets the per-operation counts of Firebase operations. | ||
* @method getCounts | ||
* @memberof Fireproof.stats | ||
* @returns {Object} An object with three keys: "read", "write", and | ||
* "update". The values are the counts of operations under those headings. | ||
*/ | ||
Fireproof.stats.getCounts = function() { | ||
}, {}); | ||
return Object.keys(Fireproof.stats.events) | ||
.reduce(function(result, statName) { | ||
}; | ||
result[statName] = Fireproof.stats.events[statName].length; | ||
return result; | ||
}, {}); | ||
Fireproof.stats.reset(); | ||
Fireproof.stats.resetListeners(); | ||
}; | ||
}; | ||
Fireproof.stats.reset(); | ||
Fireproof.stats.resetListeners(); |
'use strict'; | ||
/** | ||
* Delegates Firebase#createUser. | ||
* @method Fireproof#createUser | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.createUser = function(credentials, onComplete) { | ||
module.exports = function(Fireproof) { | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.createUser(credentials, oc); | ||
return oc.promise; | ||
/** | ||
* Delegates Firebase#createUser. | ||
* @method Fireproof#createUser | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
*/ | ||
Fireproof.prototype.createUser = function(credentials, onComplete) { | ||
}; | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.createUser(credentials, oc); | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#changePassword. | ||
* @method Fireproof#changePassword | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.changePassword = function(credentials, onComplete) { | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.changePassword(credentials, oc); | ||
return oc.promise; | ||
/** | ||
* Delegates Firebase#changePassword. | ||
* @method Fireproof#changePassword | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
*/ | ||
Fireproof.prototype.changePassword = function(credentials, onComplete) { | ||
}; | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.changePassword(credentials, oc); | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#resetPassword. | ||
* @method Fireproof#resetPassword | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.resetPassword = function(credentials, onComplete) { | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.resetPassword(credentials, oc); | ||
return oc.promise; | ||
/** | ||
* Delegates Firebase#changePassword. | ||
* @method Fireproof#changePassword | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
*/ | ||
Fireproof.prototype.resetPassword = function(credentials, onComplete) { | ||
}; | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.resetPassword(credentials, oc); | ||
return oc.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#removeUser. | ||
* @method Fireproof#createUser | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.removeUser = function(credentials, onComplete) { | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.removeUser(credentials, oc); | ||
return oc.promise; | ||
/** | ||
* Delegates Firebase#changePassword. | ||
* @method Fireproof#changePassword | ||
* @param {Object} credentials | ||
* @param {Function} [onComplete] | ||
*/ | ||
Fireproof.prototype.removeUser = function(credentials, onComplete) { | ||
var oc = Fireproof._handleError(onComplete); | ||
this._ref.removeUser(credentials, oc); | ||
return oc.promise; | ||
}; | ||
}; |
183
lib/write.js
'use strict'; | ||
/** | ||
* Delegates Firebase#set. | ||
* @method Fireproof#set | ||
* @param {object} value The value to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
* @example | ||
* ```js | ||
* fireproofRef.set('something') | ||
* .then(function()) { | ||
* console.log('set was successful!'); | ||
* }, function(err) { | ||
* console.error('error while setting:', err); | ||
* }); | ||
* ``` | ||
*/ | ||
Fireproof.prototype.set = function(value, onComplete) { | ||
module.exports = function(Fireproof) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#set. | ||
* @method Fireproof#set | ||
* @param {object} value The value to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
* @example | ||
* fireproofRef.set('something') | ||
* .then(function()) { | ||
* console.log('set was successful!'); | ||
* }, function(err) { | ||
* console.error('error while setting:', err); | ||
* }); | ||
*/ | ||
Fireproof.prototype.set = function(value, onComplete) { | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.set(value, handler); | ||
var handler = Fireproof._handleError(onComplete); | ||
return handler.promise; | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.set(value, handler); | ||
}; | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#update. | ||
* @method Fireproof#update | ||
* @param {object} value An object with keys and values to update. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.update = function(value, onComplete) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#update. | ||
* @method Fireproof#update | ||
* @param {object} value An object with keys and values to update. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.update = function(value, onComplete) { | ||
Fireproof.stats._record('update', this.toString()); | ||
this._ref.update(value, handler); | ||
var handler = Fireproof._handleError(onComplete); | ||
return handler.promise; | ||
Fireproof.stats._record('update', this.toString()); | ||
this._ref.update(value, handler); | ||
}; | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#remove. | ||
* @method Fireproof#remove | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.remove = function(onComplete) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#remove. | ||
* @method Fireproof#remove | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.remove = function(onComplete) { | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.remove(handler); | ||
var handler = Fireproof._handleError(onComplete); | ||
return handler.promise; | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.remove(handler); | ||
}; | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#push. | ||
* @method Fireproof#push | ||
* @param {object} value An object with keys and values to update. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.push = function(value, onComplete) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#push. | ||
* @method Fireproof#push | ||
* @param {object} value An object with keys and values to update. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.push = function(value, onComplete) { | ||
Fireproof.stats._record('write', this.toString()); | ||
var handler = Fireproof._handleError(onComplete); | ||
var rv = new Fireproof( | ||
this._ref.push(value, handler), | ||
handler.promise | ||
); | ||
Fireproof.stats._record('write', this.toString()); | ||
return rv; | ||
var rv = new Fireproof( | ||
this._ref.push(value, handler), | ||
handler.promise | ||
); | ||
}; | ||
return rv; | ||
}; | ||
/** | ||
* Delegates Firebase#setWithPriority. | ||
* @method Fireproof#setWithPriority | ||
* @param {object} value The value to set this path to. | ||
* @param {object} priority The priority to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.setWithPriority = function(value, priority, onComplete) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#setWithPriority. | ||
* @method Fireproof#setWithPriority | ||
* @param {object} value The value to set this path to. | ||
* @param {object} priority The priority to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.setWithPriority = function(value, priority, onComplete) { | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.setWithPriority(value, priority, handler); | ||
var handler = Fireproof._handleError(onComplete); | ||
return handler.promise; | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.setWithPriority(value, priority, handler); | ||
}; | ||
return handler.promise; | ||
}; | ||
/** | ||
* Delegates Firebase#setPriority. | ||
* @method Fireproof#setPriority | ||
* @param {object} priority The priority to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} | ||
*/ | ||
Fireproof.prototype.setPriority = function(priority, onComplete) { | ||
var handler = Fireproof._handleError(onComplete); | ||
/** | ||
* Delegates Firebase#setPriority. | ||
* @method Fireproof#setPriority | ||
* @param {object} priority The priority to set this path to. | ||
* @param {function=} onComplete Callback when the operation is done. | ||
* @returns {Promise} Resolves on success, rejects on failure. | ||
*/ | ||
Fireproof.prototype.setPriority = function(priority, onComplete) { | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.setPriority(priority, handler); | ||
var handler = Fireproof._handleError(onComplete); | ||
return handler.promise; | ||
Fireproof.stats._record('write', this.toString()); | ||
this._ref.setPriority(priority, handler); | ||
}; | ||
return handler.promise; | ||
}; | ||
}; |
{ | ||
"name": "fireproof", | ||
"version": "1.6.2", | ||
"version": "1.6.3", | ||
"description": "Promises for Firebase objects.", | ||
"main": "index.js", | ||
"main": "dist/fireproof.js", | ||
"scripts": { | ||
"test": "mocha --recursive test/setup test/spec", | ||
"test": "./node_modules/.bin/gulp test", | ||
"postpublish": "./script/postpublish.bash" | ||
@@ -17,11 +17,18 @@ }, | ||
"devDependencies": { | ||
"chai": "^1.9.2", | ||
"chai-as-promised": "^4.1.1", | ||
"firebase": "^1.1.2", | ||
"chai": "^1.9.2", | ||
"firebase-admin": "^2.0.1", | ||
"gulp": "^3.8.9", | ||
"gulp-bump": "^0.1.11", | ||
"gulp-concat": "^2.4.1", | ||
"gulp-help": "^1.3.0", | ||
"gulp-jsdoc-to-markdown": "^0.1.5", | ||
"gulp-mocha": "^1.1.1", | ||
"gulp-rename": "^1.2.0", | ||
"gulp-wrap": "^0.5.0", | ||
"kew": "^0.4.0", | ||
"mocha": "^1.21.5", | ||
"firebase-admin": "^2.0.1", | ||
"chai-as-promised": "^4.1.1", | ||
"jsdoc": "^3.3.0-alpha9", | ||
"ink-docstrap": "^0.4.12", | ||
"kew": "^0.4.0" | ||
"rimraf": "^2.2.8" | ||
} | ||
} |
@@ -15,3 +15,3 @@ | ||
See the API documentation [here.](https://casetext.github.io/fireproof) | ||
See the API documentation [here.](https://github.com/casetext/fireproof/blob/master/README.md) | ||
@@ -18,0 +18,0 @@ The bottom line is this: all Firebase methods are reproduced on a Fireproof object. |
'use strict'; | ||
var Fireproof = require('../../../index'), | ||
Q = require('kew'); | ||
Fireproof.bless(Q); | ||
describe('Fireproof', function() { | ||
@@ -11,0 +5,0 @@ |
'use strict'; | ||
var Fireproof = require('../../../index'), | ||
Firebase = require('firebase'), | ||
Q = require('kew'); | ||
Fireproof.bless(Q); | ||
describe('onDisconnect', function() { | ||
@@ -12,0 +5,0 @@ |
'use strict'; | ||
var Q = require('kew'); | ||
var Fireproof = require('../../../index'), | ||
Q = require('kew'); | ||
Fireproof.bless(Q); | ||
var fireproof; | ||
@@ -11,0 +7,0 @@ |
'use strict'; | ||
var Fireproof = require('../../../index'), | ||
Q = require('kew'); | ||
Fireproof.bless(Q); | ||
// on Snapshot, only forEach and ref need to be tested, and just to make sure | ||
@@ -10,0 +5,0 @@ // they hand back the correct kind of delegate object. |
@@ -6,8 +6,5 @@ | ||
var Fireproof = require('../../../index'), | ||
Q = require('kew'), | ||
url = require('url'); | ||
var url = require('url'), | ||
Q = require('kew'); | ||
Fireproof.bless(Q); | ||
describe('Stats', function() { | ||
@@ -14,0 +11,0 @@ |
'use strict'; | ||
var Fireproof = require('../../../index'), | ||
Q = require('kew'); | ||
var Q = require('kew'); | ||
Fireproof.bless(Q); | ||
describe('write operation', function() { | ||
@@ -11,0 +7,0 @@ |
Sorry, the diff of this file is not supported yet
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
102399
28
2398
2
15
1