From 3db9913191e00eaf4e93dbdb0f8cacf438e313b6 Mon Sep 17 00:00:00 2001 From: JT Turner Date: Sun, 24 Jan 2016 11:30:32 -0800 Subject: [PATCH] deps: bluebird@3.1.2 closes #6361 - Updated bluebird dependency to latest 3.1.2. - Updated update check to handle promises not resolving to arrays. - Reviewed all other promise code and it looks good. - Updated code using settle to use the new reflect function. --- core/server/data/import/data-importer.js | 10 ++-- core/server/data/import/index.js | 7 +-- core/server/data/import/utils.js | 16 +++--- core/server/update-check.js | 64 ++++++++++++------------ core/test/unit/mail_spec.js | 12 ++--- package.json | 2 +- 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/core/server/data/import/data-importer.js b/core/server/data/import/data-importer.js index 0655706b29..7f3399c80d 100644 --- a/core/server/data/import/data-importer.js +++ b/core/server/data/import/data-importer.js @@ -52,11 +52,13 @@ DataImporter.prototype.doUserImport = function (t, tableData, owner, users, erro } // Import users, deduplicating with already present users - userOps = utils.importUsers(tableData.users, users, t); + userOps = utils.importUsers(tableData.users, users, t).map(function (userImport) { + return userImport.reflect(); + }); - return Promise.settle(userOps).then(function (descriptors) { + return Promise.all(userOps).then(function (descriptors) { descriptors.forEach(function (d) { - if (d.isRejected()) { + if (!d.isFulfilled()) { errors = errors.concat(d.reason()); } else { imported.push(d.value().toJSON(internal)); @@ -133,7 +135,7 @@ DataImporter.prototype.doImport = function (data) { } }).then(function () { importResults.forEach(function (p) { - if (p.isRejected()) { + if (!p.isFulfilled()) { errors = errors.concat(p.reason()); } }); diff --git a/core/server/data/import/index.js b/core/server/data/import/index.js index cbbc343a53..21b8f3d619 100644 --- a/core/server/data/import/index.js +++ b/core/server/data/import/index.js @@ -170,15 +170,16 @@ validate = function validate(data) { _.each(_.keys(data.data), function (tableName) { _.each(data.data[tableName], function (importValues) { - validateOps.push(validation.validateSchema(tableName, importValues)); + validateOps.push(validation. + validateSchema(tableName, importValues).reflect()); }); }); - return Promise.settle(validateOps).then(function (descriptors) { + return Promise.all(validateOps).then(function (descriptors) { var errorList = []; _.each(descriptors, function (d) { - if (d.isRejected()) { + if (!d.isFulfilled()) { errorList = errorList.concat(d.reason()); } }); diff --git a/core/server/data/import/utils.js b/core/server/data/import/utils.js index 9ada4c1922..1debc7edfe 100644 --- a/core/server/data/import/utils.js +++ b/core/server/data/import/utils.js @@ -204,10 +204,10 @@ utils = { } return _tag; - })); + }).reflect()); }); - return Promise.settle(ops); + return Promise.all(ops); }, importPosts: function importPosts(tableData, transaction) { @@ -232,11 +232,11 @@ utils = { ops.push(models.Post.add(post, _.extend({}, internal, {transacting: transaction, importing: true})) .catch(function (error) { return Promise.reject({raw: error, model: 'post', data: post}); - }) + }).reflect() ); }); - return Promise.settle(ops); + return Promise.all(ops); }, importUsers: function importUsers(tableData, existingUsers, transaction) { @@ -292,9 +292,9 @@ utils = { if (!(error instanceof errors.NotFoundError)) { return Promise.reject({raw: error, model: 'setting', data: tableData}); } - })); + }).reflect()); - return Promise.settle(ops); + return Promise.all(ops); }, /** For later **/ @@ -317,10 +317,10 @@ utils = { } return _app; - })); + }).reflect()); }); - return Promise.settle(ops); + return Promise.all(ops); } }; diff --git a/core/server/update-check.js b/core/server/update-check.js index 649e18090d..11e25a15f8 100644 --- a/core/server/update-check.js +++ b/core/server/update-check.js @@ -53,39 +53,41 @@ function updateCheckError(error) { function updateCheckData() { var data = {}, - ops = [], mailConfig = config.mail; - ops.push(api.settings.read(_.extend({key: 'dbHash'}, internal)).catch(errors.rejectError)); - ops.push(api.settings.read(_.extend({key: 'activeTheme'}, internal)).catch(errors.rejectError)); - ops.push(api.settings.read(_.extend({key: 'activeApps'}, internal)) - .then(function (response) { - var apps = response.settings[0]; - try { - apps = JSON.parse(apps.value); - } catch (e) { - return errors.rejectError(e); - } - - return _.reduce(apps, function (memo, item) { return memo === '' ? memo + item : memo + ', ' + item; }, ''); - }).catch(errors.rejectError)); - ops.push(api.posts.browse().catch(errors.rejectError)); - ops.push(api.users.browse(internal).catch(errors.rejectError)); - ops.push(Promise.promisify(exec)('npm -v').catch(errors.rejectError)); - data.ghost_version = currentVersion; data.node_version = process.versions.node; data.env = process.env.NODE_ENV; data.database_type = config.database.client; - data.email_transport = mailConfig && (mailConfig.options && mailConfig.options.service ? mailConfig.options.service : mailConfig.transport); + data.email_transport = mailConfig && + (mailConfig.options && mailConfig.options.service ? + mailConfig.options.service : + mailConfig.transport); - return Promise.settle(ops).then(function (descriptors) { - var hash = descriptors[0].value().settings[0], - theme = descriptors[1].value().settings[0], - apps = descriptors[2].value(), - posts = descriptors[3].value(), - users = descriptors[4].value(), - npm = descriptors[5].value(), + return Promise.props({ + hash: api.settings.read(_.extend({key: 'dbHash'}, internal)).catch(errors.rejectError).reflect(), + theme: api.settings.read(_.extend({key: 'activeTheme'}, internal)).catch(errors.rejectError).reflect(), + apps: api.settings.read(_.extend({key: 'activeApps'}, internal)) + .then(function (response) { + var apps = response.settings[0]; + try { + apps = JSON.parse(apps.value); + } catch (e) { + return errors.rejectError(e); + } + + return _.reduce(apps, function (memo, item) { return memo === '' ? memo + item : memo + ', ' + item; }, ''); + }).catch(errors.rejectError).reflect(), + posts: api.posts.browse().catch(errors.rejectError).reflect(), + users: api.users.browse(internal).catch(errors.rejectError).reflect(), + npm: Promise.promisify(exec)('npm -v').catch(errors.rejectError).reflect() + }).then(function (descriptors) { + var hash = descriptors.hash.value().settings[0], + theme = descriptors.theme.value().settings[0], + apps = descriptors.apps.value(), + posts = descriptors.posts.value(), + users = descriptors.users.value(), + npm = descriptors.npm.value(), blogUrl = url.parse(config.url), blogId = blogUrl.hostname + blogUrl.pathname.replace(/\//, '') + hash.value; @@ -95,7 +97,7 @@ function updateCheckData() { data.post_count = posts && posts.meta && posts.meta.pagination ? posts.meta.pagination.total : 0; data.user_count = users && users.users && users.users.length ? users.users.length : 0; data.blog_created_at = users && users.users && users.users[0] && users.users[0].created_at ? moment(users.users[0].created_at).unix() : ''; - data.npm_version = _.isArray(npm) && npm[0] ? npm[0].toString().replace(/\n/, '') : ''; + data.npm_version = npm.trim(); return data; }).catch(updateCheckError); @@ -161,16 +163,16 @@ function updateCheckResponse(response) { api.settings.edit( {settings: [{key: 'nextUpdateCheck', value: response.next_check}]}, internal - ).catch(errors.rejectError), + ).catch(errors.rejectError).reflect(), api.settings.edit( {settings: [{key: 'displayUpdateNotification', value: response.version}]}, internal - ).catch(errors.rejectError) + ).catch(errors.rejectError).reflect() ); - return Promise.settle(ops).then(function then(descriptors) { + return Promise.map(ops).then(function then(descriptors) { descriptors.forEach(function forEach(d) { - if (d.isRejected()) { + if (!d.isFulfilled()) { errors.rejectError(d.reason()); } }); diff --git a/core/test/unit/mail_spec.js b/core/test/unit/mail_spec.js index 65fd096192..28e8ebb62b 100644 --- a/core/test/unit/mail_spec.js +++ b/core/test/unit/mail_spec.js @@ -104,14 +104,14 @@ describe('Mail', function () { it('should fail to send messages when given insufficient data', function (done) { mailer = new GhostMail(); - Promise.settle([ - mailer.send(), - mailer.send({}), - mailer.send({subject: '123'}), - mailer.send({subject: '', html: '123'}) + Promise.all([ + mailer.send().reflect(), + mailer.send({}).reflect(), + mailer.send({subject: '123'}).reflect(), + mailer.send({subject: '', html: '123'}).reflect() ]).then(function (descriptors) { descriptors.forEach(function (d) { - d.isRejected().should.be.true(); + d.isFulfilled().should.be.false(); d.reason().should.be.an.instanceOf(Error); d.reason().message.should.eql('Error: Incomplete message data.'); }); diff --git a/package.json b/package.json index 7a8d47e177..cb7d932d00 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ }, "dependencies": { "bcryptjs": "2.3.0", - "bluebird": "2.10.2", + "bluebird": "3.1.2", "body-parser": "1.14.2", "bookshelf": "0.9.1", "busboy": "0.2.12",