diff --git a/lib/configuration/hooks/studio/index.js b/lib/configuration/hooks/studio/index.js index 1bfd102d08..b9a2e070b4 100644 --- a/lib/configuration/hooks/studio/index.js +++ b/lib/configuration/hooks/studio/index.js @@ -62,192 +62,193 @@ module.exports = function (strapi) { ] }); - process.nextTick(function () { - manager.on('connect_failed', function () { - if (firstConnectionAttempt) { - strapi.log.warn('Connection to the Studio server failed!'); + manager.on('connect_failed', function () { + if (firstConnectionAttempt) { + strapi.log.warn('Connection to the Studio server failed!'); + } + }); + + manager.on('reconnect_failed', function () { + strapi.log.warn('Reconnection to the Studio server failed!'); + }); + + manager.on('reconnect', function () { + strapi.log.info('Connection to the Studio server found, please wait a few seconds...'); + }); + + manager.on('reconnecting', function (number) { + strapi.log.warn('Connection error with the Studio server, new attempt in progress... (' + number + ')'); + }); + + socket.on('connect', function () { + firstConnectionAttempt = false; + + strapi.once('bootstrap:done', function () { + _self.connectWithStudio(socket); + }); + }); + + socket.on('error', function (err) { + strapi.log.warn(err); + }); + + socket.on('disconnect', function () { + strapi.log.info('Disconnected from the Studio server.'); + }); + + socket.on('authorized', function (data) { + const decryptedData = strapi.rsa.decryptPublic(data, 'json'); + + if (decryptedData.status === 'ok') { + if (strapi.config.environment === 'development') { + socket.emit('testEncryption', { + appId: strapi.config.studio.appId, + token: strapi.token, + encrypted: strapi.rsa.encrypt({ + secretKey: strapi.config.studio.secretKey, + data: 'ok' + }) + }, function (err) { + if (err) { + strapi.log.warn(err); + } + + strapi.log.info('Connected with the Studio server.'); + }); + } else { + strapi.log.warn('The use of the Studio is restricted to development environment.'); } - }); + } + }); - manager.on('reconnect_failed', function () { - strapi.log.warn('Reconnection to the Studio server failed!'); - }); + socket.on('todo', function (data, fn) { + if (!data.hasOwnProperty('from') || !data.hasOwnProperty('to')) { + fn(stringify('Some required attributes are missing', null, 2), null); + } else if (data.from === strapi.token) { + if (data.hasOwnProperty('files')) { + const syncPromise = function (file, index) { + const deferred = Promise.defer(); - manager.on('reconnect', function () { - strapi.log.info('Connection to the Studio server found, please wait a few seconds...'); - }); - - manager.on('reconnecting', function (number) { - strapi.log.warn('Connection error with the Studio server, new attempt in progress... (' + number + ')'); - }); - - socket.on('connect', function () { - firstConnectionAttempt = false; - - strapi.once('bootstrap:done', function () { - _self.connectWithStudio(socket); - }); - }); - - socket.on('error', function (err) { - strapi.log.warn(err); - }); - - socket.on('disconnect', function () { - strapi.log.info('Disconnected from the Studio server.'); - }); - - socket.on('authorized', function (data) { - const decryptedData = strapi.rsa.decryptPublic(data, 'json'); - - if (decryptedData.status === 'ok') { - if (strapi.config.environment === 'development') { - socket.emit('testEncryption', { - appId: strapi.config.studio.appId, - token: strapi.token, - encrypted: strapi.rsa.encrypt({ - secretKey: strapi.config.studio.secretKey, - data: 'ok' - }) - }, function (err) { - if (err) { - strapi.log.warn(err); - } - - strapi.log.info('Connected with the Studio server.'); - }); - } else { - strapi.log.warn('The use of the Studio is restricted to development environment.'); - } - } - }); - - socket.on('todo', function (data, fn) { - if (!data.hasOwnProperty('from') || !data.hasOwnProperty('to')) { - fn(stringify('Some required attributes are missing', null, 2), null); - } else if (data.from === strapi.token) { - if (data.hasOwnProperty('files')) { - const syncPromise = function (file, index) { - const deferred = Promise.defer(); - - _self.unzipper(file) - .then(function () { - if (!_.isEmpty(data.files[index + 1])) { - return syncPromise(data.files[index + 1], index + 1); - } else { - deferred.resolve(); - } - }) - .then(function () { - deferred.resolve(); - }) - .catch(function (err) { - deferred.reject(err); - }); - - return deferred.promise; - }; - - syncPromise(_.first(data.files), 0) + _self.unzipper(file) .then(function () { - if (data.hasOwnProperty('action') && _.isFunction(_self[data.action])) { - _self[data.action](data, function (err, obj) { - if (err) { - fn({ - appId: strapi.config.studio.appId, - token: strapi.token, - encrypted: strapi.rsa.encrypt({ - err: stringify(err, null, 2), - data: null - }) - }); + if (!_.isEmpty(data.files[index + 1])) { + return syncPromise(data.files[index + 1], index + 1); + } else { + deferred.resolve(); + } + }) + .then(function () { + deferred.resolve(); + }) + .catch(function (err) { + deferred.reject(err); + }); - return false; - } + return deferred.promise; + }; + syncPromise(_.first(data.files), 0) + .then(function () { + if (data.hasOwnProperty('action') && _.isFunction(_self[data.action])) { + _self[data.action](data, function (err, obj) { + + if (err) { fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ - err: null, - data: stringify(obj, null, 2) + err: stringify(err, null, 2), + data: null }) }); - }); - } else if (!data.hasOwnProperty('action')) { + + return false; + } + fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ err: null, - data: stringify(true, null, 2) + data: stringify(obj, null, 2) }) }); - } else { - fn({ - appId: strapi.config.studio.appId, - token: strapi.token, - encrypted: strapi.rsa.encrypt({ - err: stringify('Unknow action', null, 2), - data: null - }) - }); - } - }) - .catch(function (err) { + }); + } else if (!data.hasOwnProperty('action')) { fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ - err: err, - data: null + err: null, + data: stringify(true, null, 2) }) }); - }); - } else if (!data.hasOwnProperty('action')) { - fn(strapi.rsa.encrypt(stringify('`action` attribute is missing', null, 2)), strapi.rsa.encryptPrivate(null)); - } else if (_.isFunction(_self[data.action])) { - _self[data.action](data, function (err, obj) { - if (err) { + } else { fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ - err: err, + err: stringify('Unknow action', null, 2), data: null }) }); - - return false; } - + }) + .catch(function (err) { fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ - err: null, - data: stringify(obj, null, 2) + err: err, + data: null }) }); }); - } else { + } else if (!data.hasOwnProperty('action')) { + fn(strapi.rsa.encrypt(stringify('`action` attribute is missing', null, 2)), strapi.rsa.encryptPrivate(null)); + } else if (_.isFunction(_self[data.action])) { + _self[data.action](data, function (err, obj) { + if (err) { + fn({ + appId: strapi.config.studio.appId, + token: strapi.token, + encrypted: strapi.rsa.encrypt({ + err: err, + data: null + }) + }); + + return false; + } + + + fn({ appId: strapi.config.studio.appId, token: strapi.token, encrypted: strapi.rsa.encrypt({ - err: stringify('Unknow action', null, 2), - data: null + err: null, + data: stringify(obj, null, 2) }) }); - } + }); } else { - fn(stringify('Bad user token', null, 2), null); + fn({ + appId: strapi.config.studio.appId, + token: strapi.token, + encrypted: strapi.rsa.encrypt({ + err: stringify('Unknow action', null, 2), + data: null + }) + }); } - }); + } else { + fn(stringify('Bad user token', null, 2), null); + } + }); - socket.on('err', function (data) { - strapi.log.warn(data.text); - }); + socket.on('err', function (data) { + strapi.log.warn(data.text); }); cb(); @@ -318,12 +319,23 @@ module.exports = function (strapi) { */ pullServer: function (data, cb) { - const obj = {}; - obj.token = strapi.token; - obj.config = strapi.config; - obj.models = strapi.models; - obj.api = strapi.api; - obj.templates = {}; + const obj = { + token: strapi.token, + config: strapi.config, + models: _.mapValues(_.cloneDeep(strapi.models), function (model) { + model.attributes = _.omit(model.attributes, _.isFunction); + + return model; + }), + api: _.mapValues(_.cloneDeep(strapi.api), function (api) { + return _.mapValues(api.models, function (model) { + model.attributes = _.omit(model.attributes, _.isFunction); + + return model; + }); + }), + templates: {} + }; cb(null, obj); },