Bringing files up to date with master, fixing the missing contributor code.

This commit is contained in:
Jason Lei 2018-09-17 15:50:13 +08:00
parent 800fb6150d
commit 9861b9ad3f
2 changed files with 47 additions and 73 deletions

View File

@ -7,10 +7,21 @@
*/ */
const fs = require('fs'); const fs = require('fs');
const crypto = require('crypto');
const _ = require('lodash'); const _ = require('lodash');
const toArray = require('stream-to-array'); const toArray = require('stream-to-array');
const uuid = require('uuid/v4'); const uuid = require('uuid/v4');
function niceHash(buffer) {
return crypto
.createHash('sha256')
.update(buffer)
.digest('base64')
.replace(/=/g, '')
.replace(/\//g, '-')
.replace(/\+/, '_');
}
module.exports = { module.exports = {
bufferize: async files => { bufferize: async files => {
if (_.isEmpty(files) === 0) { if (_.isEmpty(files) === 0) {
@ -23,28 +34,22 @@ module.exports = {
// transform all files in buffer // transform all files in buffer
return Promise.all( return Promise.all(
files.map(async stream => { files.map(async stream => {
const parts = stream.path const parts = await toArray(fs.createReadStream(stream.path));
? await toArray(fs.createReadStream(stream.path))
: await toArray(stream.stream);
const buffers = parts.map( const buffers = parts.map(
part => (_.isBuffer(part) ? part : Buffer.from(part)), part => (_.isBuffer(part) ? part : Buffer.from(part)),
); );
if (!stream.path) { const buffer = Buffer.concat(buffers);
stream.name = stream.filename;
stream.type = stream.mimetype;
stream.size = parseInt(stream.encoding.replace('bit', ''));
}
return { return {
name: stream.name, name: stream.name,
sha256: niceHash(buffer),
hash: uuid().replace(/-/g, ''), hash: uuid().replace(/-/g, ''),
ext: ext:
stream.name.split('.').length > 1 stream.name.split('.').length > 1
? `.${_.last(stream.name.split('.'))}` ? `.${_.last(stream.name.split('.'))}`
: '', : '',
buffer: Buffer.concat(buffers), buffer,
mime: stream.type, mime: stream.type,
size: (stream.size / 1000).toFixed(2), size: (stream.size / 1000).toFixed(2),
}; };

View File

@ -111,27 +111,21 @@ class Strapi extends EventEmitter {
// Update source admin. // Update source admin.
await admin.call(this); await admin.call(this);
// Launch server. // Launch server.
this.server.listen(this.config.port, err => { this.server.listen(this.config.port, async (err) => {
if (err) { if (err) {
this.log.debug('⚠️ Server wasn\'t able to start properly.'); this.log.debug(`⚠️ Server wasn't able to start properly.`);
this.log.error(err); this.log.error(err);
return this.stop(); return this.stop();
} }
this.log.info('Time: ' + new Date()); this.log.info('Time: ' + new Date());
this.log.info( this.log.info('Launched in: ' + (Date.now() - this.config.launchedAt) + ' ms');
'Launched in: ' + (Date.now() - this.config.launchedAt) + ' ms',
);
this.log.info('Environment: ' + this.config.environment); this.log.info('Environment: ' + this.config.environment);
this.log.info('Process PID: ' + process.pid); this.log.info('Process PID: ' + process.pid);
this.log.info( this.log.info(`Version: ${this.config.info.strapi} (node v${this.config.info.node})`);
`Version: ${this.config.info.strapi} (node v${
this.config.info.node
})`,
);
this.log.info('To shut down your server, press <CTRL> + C at any time'); this.log.info('To shut down your server, press <CTRL> + C at any time');
console.log(); console.log();
this.log.info(`☄️ Admin panel: ${this.config.url}/admin`); this.log.info(`☄️ Admin panel: ${this.config.admin.url}`);
this.log.info(`⚡️ Server: ${this.config.url}`); this.log.info(`⚡️ Server: ${this.config.url}`);
console.log(); console.log();
@ -141,11 +135,15 @@ class Strapi extends EventEmitter {
if (cb && typeof cb === 'function') { if (cb && typeof cb === 'function') {
cb(); cb();
} }
if (this.config.environment === 'development' && get(this.config.currentEnvironment, 'server.admin.autoOpen', true) !== false) {
await utils.openBrowser.call(this);
}
}); });
} catch (err) { } catch (err) {
this.log.debug('⛔️ Server wasn\'t able to start properly.'); this.log.debug(`⛔️ Server wasn't able to start properly.`);
this.log.error(err); this.log.error(err);
console.error(err); console.log(err);
this.stop(); this.stop();
} }
} }
@ -157,17 +155,15 @@ class Strapi extends EventEmitter {
const key = conn.remoteAddress + ':' + conn.remotePort; const key = conn.remoteAddress + ':' + conn.remotePort;
connections[key] = conn; connections[key] = conn;
conn.on('close', function() { conn.on('close', function () {
delete connections[key]; delete connections[key];
}); });
}); });
this.server.on('error', err => { this.server.on('error', err => {
if (err.code === 'EADDRINUSE') { if (err.code === 'EADDRINUSE') {
this.log.debug('⛔️ Server wasn\'t able to start properly.'); this.log.debug(`⛔️ Server wasn't able to start properly.`);
this.log.error( this.log.error(`The port ${err.port} is already used by another application.`);
`The port ${err.port} is already used by another application.`,
);
this.stop(); this.stop();
return; return;
} }
@ -191,8 +187,7 @@ class Strapi extends EventEmitter {
if ( if (
cluster.isWorker && cluster.isWorker &&
this.config.environment === 'development' && this.config.environment === 'development' &&
get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === true
true
) { ) {
process.send('stop'); process.send('stop');
} }
@ -231,10 +226,7 @@ class Strapi extends EventEmitter {
await store.call(this); await store.call(this);
// Initialize hooks and middlewares. // Initialize hooks and middlewares.
await Promise.all([ await Promise.all([initializeMiddlewares.call(this), initializeHooks.call(this)]);
initializeMiddlewares.call(this),
initializeHooks.call(this),
]);
// Harmonize plugins configuration. // Harmonize plugins configuration.
await plugins.call(this); await plugins.call(this);
@ -245,7 +237,7 @@ class Strapi extends EventEmitter {
shouldReload: 0 shouldReload: 0
}; };
const reload = function() { const reload = function () {
if (state.shouldReload > 0) { if (state.shouldReload > 0) {
// Reset the reloading state // Reset the reloading state
state.shouldReload -= 1; state.shouldReload -= 1;
@ -256,11 +248,7 @@ class Strapi extends EventEmitter {
if ( if (
cluster.isWorker && cluster.isWorker &&
this.config.environment === 'development' && this.config.environment === 'development' &&
get( get(this.config, 'currentEnvironment.server.autoReload.enabled', true) === true
this.config,
'currentEnvironment.server.autoReload.enabled',
true,
) === true
) { ) {
process.send('reload'); process.send('reload');
} }
@ -305,9 +293,7 @@ class Strapi extends EventEmitter {
try { try {
fn(err => { fn(err => {
if (ranBootstrapFn) { if (ranBootstrapFn) {
this.log.error( this.log.error('You called the callback in `strapi.config.boostrap` more than once!');
'You called the callback in `strapi.config.boostrap` more than once!',
);
return reject(); return reject();
} }
@ -319,9 +305,7 @@ class Strapi extends EventEmitter {
}); });
} catch (e) { } catch (e) {
if (ranBootstrapFn) { if (ranBootstrapFn) {
this.log.error( this.log.error('The bootstrap function threw an error after its callback was called.');
'The bootstrap function threw an error after its callback was called.',
);
return reject(e); return reject(e);
} }
@ -334,9 +318,7 @@ class Strapi extends EventEmitter {
}); });
return Promise.all( return Promise.all(
Object.values(this.plugins).map(x => Object.values(this.plugins).map(x => execBootstrap(get(x, 'config.functions.bootstrap'))),
execBootstrap(get(x, 'config.functions.bootstrap')),
),
).then(() => execBootstrap(this.config.functions.bootstrap)); ).then(() => execBootstrap(this.config.functions.bootstrap));
} }
@ -356,16 +338,14 @@ class Strapi extends EventEmitter {
query(entity, plugin) { query(entity, plugin) {
if (!entity) { if (!entity) {
return this.log.error( return this.log.error(
'You can\'t call the query method without passing the model\'s name as a first argument.', `You can't call the query method without passing the model's name as a first argument.`,
); );
} }
const model = entity.toLowerCase(); const model = entity.toLowerCase();
const Model = const Model =
get(strapi.plugins, [plugin, 'models', model]) || get(strapi.plugins, [plugin, 'models', model]) || get(strapi, ['models', model]) || undefined;
get(strapi, ['models', model]) ||
undefined;
if (!Model) { if (!Model) {
return this.log.error(`The model ${model} can't be found.`); return this.log.error(`The model ${model} can't be found.`);
@ -374,9 +354,7 @@ class Strapi extends EventEmitter {
const connector = Model.orm; const connector = Model.orm;
if (!connector) { if (!connector) {
return this.log.error( return this.log.error(`Impossible to determine the use ORM for the model ${model}.`);
`Impossible to determine the use ORM for the model ${model}.`,
);
} }
// Get stack trace. // Get stack trace.
@ -388,9 +366,7 @@ class Strapi extends EventEmitter {
let pluginPath = undefined; let pluginPath = undefined;
if (file.indexOf('strapi-plugin-') !== -1) { if (file.indexOf('strapi-plugin-') !== -1) {
pluginPath = file pluginPath = file.split(path.sep).filter(x => x.indexOf('strapi-plugin-') !== -1)[0];
.split(path.sep)
.filter(x => x.indexOf('strapi-plugin-') !== -1)[0];
} else if (file.indexOf(path.sep + 'plugins' + path.sep) !== -1) { } else if (file.indexOf(path.sep + 'plugins' + path.sep) !== -1) {
const pathTerms = file.split(path.sep); const pathTerms = file.split(path.sep);
const index = pathTerms.indexOf('plugins'); const index = pathTerms.indexOf('plugins');
@ -401,22 +377,15 @@ class Strapi extends EventEmitter {
} }
if (!pluginPath) { if (!pluginPath) {
return this.log.error( return this.log.error('Impossible to find the plugin where `strapi.query` has been called.');
'Impossible to find the plugin where `strapi.query` has been called.',
);
} }
// Get plugin name. // Get plugin name.
const pluginName = pluginPath.replace('strapi-plugin-', '').toLowerCase(); const pluginName = pluginPath.replace('strapi-plugin-', '').toLowerCase();
const queries = get( const queries = get(this.plugins, `${pluginName}.config.queries.${connector}`);
this.plugins,
`${pluginName}.config.queries.${connector}`,
);
if (!queries) { if (!queries) {
return this.log.error( return this.log.error(`There is no query available for the model ${model}.`);
`There is no query available for the model ${model}.`,
);
} }
// Bind queries with the current model to allow the use of `this`. // Bind queries with the current model to allow the use of `this`.
@ -427,7 +396,7 @@ class Strapi extends EventEmitter {
{ {
orm: connector, orm: connector,
primaryKey: Model.primaryKey, primaryKey: Model.primaryKey,
associations: Model.associations, associations: Model.associations
}, },
); );