From 589f52e9b175103e2f6ffe84d16f175b62613c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Thu, 30 Jun 2016 16:32:51 +0200 Subject: [PATCH] feat: add close method to Couch object, rename _init to open --- .gitignore | 1 + src/index.js | 46 +++++++++++++++++++++++++------------------ test/basic.js | 2 +- test/data/anyuser.js | 4 ++-- test/data/data.js | 4 ++-- test/data/noRights.js | 4 ++-- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index b17871e1..f572d121 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ node_modules test/dummy.js lib +config.json diff --git a/src/index.js b/src/index.js index 57c7fc1a..5f68f1ed 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,10 @@ const getConfig = require('./config/config').getConfig; const globalRightTypes = ['read', 'write', 'create', 'createGroup']; const isEmail = require('./util/isEmail'); +process.on('unhandledRejection', function (err) { + debug.error('unhandled rejection: ' + err.stack); +}); + const basicRights = { $type: 'db', _id: constants.RIGHTS_DOC_ID, @@ -77,16 +81,20 @@ class Couch { this._currentAuth = null; this._authRenewal = null; this._authRenewalInterval = config.authRenewal; - this._init(); + this.open(); } - async _init() { + async open() { if (this._initPromise) { return this._initPromise; } return this._initPromise = this.getInitPromise(); } + close() { + clearInterval(this._authRenewal); + } + async getInitPromise() { debug(`initialize db ${this._databaseName}`); await this._authenticate(); @@ -164,7 +172,7 @@ class Couch { if (!(data instanceof Object)) { throw new CouchError('user data should be an object', 'bad argument'); } - await this._init(); + await this.open(); try { const userDoc = await this.getUser(user); data = simpleMerge(data, userDoc); @@ -180,14 +188,14 @@ class Couch { } async getUser(user) { - await this._init(); + await this.open(); return await getUser(this._db, user); } async createEntry(id, user, options) { options = options || {}; debug(`createEntry (id: ${id}, user: ${user}, kind: ${options.kind})`); - await this._init(); + await this.open(); const result = await nanoPromise.queryView(this._db, 'entryByOwnerAndId', {key: [user, id], reduce: false, include_docs: true}); if (result.length === 0) { const hasRight = await checkRightAnyGroup(this._db, user, 'create'); @@ -230,7 +238,7 @@ class Couch { async queryEntriesByRight(user, view, right, options) { debug(`queryEntriesByRights (${user}, ${view}, ${right})`); - await this._init(); + await this.open(); options = options || {}; if (!this._viewsWithOwner.has(view)) { throw new CouchError(`${view} is not a view with owner`, 'unauthorized'); @@ -288,7 +296,7 @@ class Couch { options.skip = 0; var limit = options.limit || 1; var cumRows = []; - await this._init(); + await this.open(); while (cumRows.length < limit) { let rows = await nanoPromise.queryView(this._db, view, options); // No more results @@ -318,7 +326,7 @@ class Couch { const limit = options.limit; const skip = options.skip; - await this._init(); + await this.open(); // First we get a list of owners for each document const owners = await nanoPromise.queryView(this._db, 'ownersById', { @@ -340,7 +348,7 @@ class Couch { async getEntryByIdAndRights(id, user, rights, options) { debug(`getEntryByIdAndRights (${id}, ${user}, ${rights})`); - await this._init(); + await this.open(); const owners = await getOwnersById(this._db, id); if (owners.length === 0) { @@ -364,7 +372,7 @@ class Couch { async getEntryByUuidAndRights(uuid, user, rights, options) { debug(`getEntryByUuidAndRights (${uuid}, ${user}, ${rights})`); - await this._init(); + await this.open(); const doc = await nanoPromise.getDocument(this._db, uuid); if (!doc) { @@ -401,7 +409,7 @@ class Couch { async _doUpdateOnEntry(id, user, update, updateBody) { // Call update handler - await this._init(); + await this.open(); const doc = await this.getEntryById(id, user); const hasRight = isOwner(doc.$owners, user); if (!hasRight) { @@ -411,7 +419,7 @@ class Couch { } async _doUpdateOnEntryByUuid(uuid, user, update, updateBody) { - await this._init(); + await this.open(); const doc = await this.getEntryByUuid(uuid, user); const hasRight = isOwner(doc.$owners, user); if (!hasRight) { @@ -615,7 +623,7 @@ class Couch { async deleteGroup(groupName, user) { debug(`deleteGroup (${groupName}, ${user})`); - await this._init(); + await this.open(); const doc = await getGroup(this._db, groupName); if (!doc) { @@ -635,7 +643,7 @@ class Couch { debug(`createGroup (${groupName}, ${user})`); if (!Array.isArray(rights)) rights = ['read']; - await this._init(); + await this.open(); const hasRight = await checkRightAnyGroup(this._db, user, 'createGroup'); if (!hasRight) throw new CouchError(`user ${user} does not have createGroup right`); @@ -654,7 +662,7 @@ class Couch { async getGroup(groupName, user) { debug(`getGroup (${groupName}, ${user})`); - await this._init(); + await this.open(); const doc = await getGroup(this._db, groupName); if (!doc) { debug.trace('group does not exist'); @@ -674,7 +682,7 @@ class Couch { */ async getGroupsByRight(user, right) { debug.trace(`getGroupsByRight (${user}, ${right})`); - await this._init(); + await this.open(); // Search in default groups const defaultGroups = await getDefaultGroups(this._db, user, true); // Search inside groups @@ -686,7 +694,7 @@ class Couch { async insertEntry(entry, user, options) { debug(`insertEntry (id: ${entry._id}, user: ${user}, options: ${options})`); - await this._init(); + await this.open(); options = options || {}; options.groups = options.groups || []; @@ -747,12 +755,12 @@ class Couch { log(message, level) { debug(`log (${message}, ${level})`); - return this._init().then(() => log.log(this._db, this._logLevel, message, level)); + return this.open().then(() => log.log(this._db, this._logLevel, message, level)); } getLogs(epoch) { debug(`getLogs (${epoch}`); - return this._init().then(() => log.getLogs(this._db, epoch)); + return this.open().then(() => log.getLogs(this._db, epoch)); } } diff --git a/test/basic.js b/test/basic.js index c859044f..b6820673 100644 --- a/test/basic.js +++ b/test/basic.js @@ -12,7 +12,7 @@ describe('basic initialization tests', function () { couch = new Couch({database: 'test2'}); }); it('should init', function () { - return couch._init(); + return couch.open(); }); it('should throw if no database given', function () { diff --git a/test/data/anyuser.js b/test/data/anyuser.js index 120c6def..91eeb62a 100644 --- a/test/data/anyuser.js +++ b/test/data/anyuser.js @@ -25,7 +25,7 @@ function populate(db) { module.exports = function () { global.couch = new Couch({database: 'test'}); - return global.couch._init() + return global.couch.open() .then(() => destroy(global.couch._nano, global.couch._databaseName)) .then(() => { global.couch = new Couch({ @@ -34,7 +34,7 @@ module.exports = function () { read: ['anyuser'] } }); - return global.couch._init(); + return global.couch.open(); }) .then(() => populate(global.couch._db)); }; diff --git a/test/data/data.js b/test/data/data.js index 89425ca6..a5dd06dc 100644 --- a/test/data/data.js +++ b/test/data/data.js @@ -93,7 +93,7 @@ function populate(db) { module.exports = function () { global.couch = new Couch({database: 'test'}); - return global.couch._init() + return global.couch.open() .then(() => destroy(global.couch._nano, global.couch._databaseName)) .then(() => { global.couch = new Couch({ @@ -105,7 +105,7 @@ module.exports = function () { addAttachment: ['anonymous'] } }); - return global.couch._init(); + return global.couch.open(); }) .then(() => populate(global.couch._db)); }; diff --git a/test/data/noRights.js b/test/data/noRights.js index 3222d0d8..46321851 100644 --- a/test/data/noRights.js +++ b/test/data/noRights.js @@ -122,7 +122,7 @@ function populate(db) { module.exports = function () { global.couch = new Couch({database: 'test'}); - return global.couch._init() + return global.couch.open() .then(() => resetDatabase(global.couch._nano, global.couch._databaseName, global.couch._couchOptions.username)) .then(() => populate(global.couch._db)) .then(() => { @@ -130,6 +130,6 @@ module.exports = function () { database: 'test', rights: {} }); - return global.couch._init(); + return global.couch.open(); }); };