Skip to content

Commit

Permalink
Build 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed May 11, 2014
1 parent d4f98d3 commit 467f946
Show file tree
Hide file tree
Showing 2 changed files with 349 additions and 0 deletions.
348 changes: 348 additions & 0 deletions dist/pouchdb.authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
(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(require,module,exports){
'use strict';

var utils = require('./utils');

function wrapError(callback) {
// provide more helpful error message
return function (err, res) {
if (err) {
if (err.name === 'unknown_error') {
err.message = (err.message || '') +
' Unknown error! Did you remember to enable CORS?';
}
}
return callback(err, res);
};
}

exports.signup = utils.toPromise(function (username, password, opts, callback) {
var db = this;
var PouchDB = db.constructor;
var pouchUtils = PouchDB.utils;
if (typeof callback === 'undefined') {
callback = typeof opts === 'undefined' ? (typeof password === 'undefined' ?
username : password) : opts;
opts = {};
}
if (['http', 'https'].indexOf(db.type()) === -1) {
return callback(new AuthError('this plugin only works for the http/https adapter'));
} else if (!username) {
return callback(new AuthError('you must provide a username'));
} else if (!password) {
return callback(new AuthError('you must provide a password'));
}

var userId = 'org.couchdb.user:' + username;
var user = {
name : username,
password : password,
roles : opts.roles || [],
type : 'user',
_id : userId
};

var reservedWords = ['name', 'password', 'roles', 'type'];
if (opts.metadata) {
for (var key in opts.metadata) {
if (opts.hasOwnProperty(key)) {
if (reservedWords.indexOf(key) !== -1 || key.startsWith('_')) {
return callback(new AuthError('cannot use reserved word in metadata: "' + key + '"'));
}
}
}
user = pouchUtils.extend(true, user, opts.metadata);
}

var url = utils.getUsersUrl(db) + '/' + encodeURIComponent(userId);
var ajaxOpts = pouchUtils.extend(true, {
method : 'PUT',
url : url,
body : user
}, opts.ajax || {});
pouchUtils.ajax(ajaxOpts, wrapError(callback));
});

exports.signUp = exports.signup;

exports.login = utils.toPromise(function (username, password, opts, callback) {
var db = this;
var PouchDB = db.constructor;
var pouchUtils = PouchDB.utils;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
if (['http', 'https'].indexOf(db.type()) === -1) {
return callback(new AuthError('this plugin only works for the http/https adapter'));
}

if (!username) {
return callback(new AuthError('you must provide a username'));
} else if (!password) {
return callback(new AuthError('you must provide a password'));
}

var ajaxOpts = pouchUtils.extend(true, {
method : 'POST',
url : utils.getSessionUrl(db),
body : {name : username, password : password}
}, opts.ajax || {});
pouchUtils.ajax(ajaxOpts, wrapError(callback));
});

exports.logIn = exports.login;

exports.logout = utils.toPromise(function (opts, callback) {
var db = this;
var PouchDB = db.constructor;
var pouchUtils = PouchDB.utils;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
var ajaxOpts = pouchUtils.extend(true, {
method : 'DELETE',
url : utils.getSessionUrl(db)
}, opts.ajax || {});
pouchUtils.ajax(ajaxOpts, wrapError(callback));
});

exports.logOut = exports.logout;

exports.getSession = utils.toPromise(function (opts, callback) {
var db = this;
var PouchDB = db.constructor;
var pouchUtils = PouchDB.utils;
if (typeof callback === 'undefined') {
callback = opts;
opts = {};
}
var url = utils.getSessionUrl(db);

var ajaxOpts = pouchUtils.extend(true, {
method : 'GET',
url : url
}, opts.ajax || {});
pouchUtils.ajax(ajaxOpts, wrapError(callback));
});

exports.getUser = utils.toPromise(function (username, opts, callback) {
var db = this;
var PouchDB = db.constructor;
var pouchUtils = PouchDB.utils;
if (typeof callback === 'undefined') {
callback = typeof opts === 'undefined' ? username : opts;
opts = {};
}
if (!username) {
return callback(new AuthError('you must provide a username'));
}

var url = utils.getUsersUrl(db);
var ajaxOpts = pouchUtils.extend(true, {
method : 'GET',
url : url + '/' + encodeURIComponent('org.couchdb.user:' + username)
}, opts.ajax || {});
pouchUtils.ajax(ajaxOpts, wrapError(callback));
});


function AuthError(message) {
this.status = 400;
this.name = 'authentication_error';
this.message = message;
this.error = true;
try {
Error.captureStackTrace(this, AuthError);
} catch (e) {}
}

utils.inherits(AuthError, Error);

if (typeof window !== 'undefined' && window.PouchDB) {
Object.keys(exports).forEach(function (key) {
window.PouchDB.plugin(exports);
});
}
},{"./utils":5}],2:[function(require,module,exports){

},{}],3:[function(require,module,exports){
// 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.once = noop;
process.off = 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');
};

},{}],4:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}

},{}],5:[function(require,module,exports){
(function (process,global){
'use strict';

var Promise;
if (typeof window !== 'undefined' && window.PouchDB) {
Promise = window.PouchDB.utils.Promise;
} else {
Promise = typeof global.Promise === 'function' ? global.Promise : require('lie');
}

function getBaseUrl(db) {
return db.getUrl().replace(/\/[^\/]+\/?$/, '');
}
exports.getUsersUrl = function (db) {
return getBaseUrl(db) + '/_users';
};
exports.getSessionUrl = function (db) {
return getBaseUrl(db) + '/_session';
};
exports.once = function (fun) {
var called = false;
return exports.getArguments(function (args) {
if (called) {
console.trace();
throw new Error('once called more than once');
} else {
called = true;
fun.apply(this, args);
}
});
};
exports.getArguments = function (fun) {
return function () {
var len = arguments.length;
var args = new Array(len);
var i = -1;
while (++i < len) {
args[i] = arguments[i];
}
return fun.call(this, args);
};
};
exports.toPromise = function (func) {
//create the function we will be returning
return exports.getArguments(function (args) {
var self = this;
var tempCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls it,
// but do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (fulfill, reject) {
try {
var callback = exports.once(function (err, mesg) {
if (err) {
reject(err);
} else {
fulfill(mesg);
}
});
// create a callback for this invocation
// apply the function in the orig context
args.push(callback);
func.apply(self, args);
} catch (e) {
reject(e);
}
});
// if there is a callback, call it back
if (usedCB) {
promise.then(function (result) {
usedCB(null, result);
}, usedCB);
}
promise.cancel = function () {
return this;
};
return promise;
});
};

exports.inherits = require('inherits');
}).call(this,require("/Users/nolan/workspace/pouchdb-authentication/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js"),typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"/Users/nolan/workspace/pouchdb-authentication/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":3,"inherits":4,"lie":2}]},{},[1])
1 change: 1 addition & 0 deletions dist/pouchdb.authentication.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 467f946

Please sign in to comment.