mirror of
https://github.com/strapi/strapi.git
synced 2025-12-05 03:21:22 +00:00
Split security middleware, enable database hook by default, fix startup issue
This commit is contained in:
parent
cc01924787
commit
53ceb38fd7
@ -51,16 +51,16 @@ module.exports = function(strapi) {
|
||||
// For each connection in the config register a new Knex connection.
|
||||
_.forEach(connections, (connection, name) => {
|
||||
// Apply defaults
|
||||
_.defaults(connection.settings, strapi.hooks.redis.defaults);
|
||||
_.defaults(connection.settings, strapi.hook.redis.defaults);
|
||||
|
||||
try {
|
||||
const redis = new Redis(_.defaultsDeep({
|
||||
port: _.get(connection.settings, 'port'),
|
||||
host: _.get(connection.settings, 'host')
|
||||
options:
|
||||
host: _.get(connection.settings, 'host'),
|
||||
options: {
|
||||
db: _.get(connection.options, 'database') || 0
|
||||
}
|
||||
}, strapi.config.hook.redis.settings);
|
||||
}, strapi.config.hook.settings.redis));
|
||||
|
||||
redis.on('error', err => {
|
||||
strapi.log.error(err);
|
||||
|
||||
16
packages/strapi-views/.editorconfig
Executable file
16
packages/strapi-views/.editorconfig
Executable file
@ -0,0 +1,16 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[{package.json,*.yml}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
94
packages/strapi-views/.gitignore
vendored
Executable file
94
packages/strapi-views/.gitignore
vendored
Executable file
@ -0,0 +1,94 @@
|
||||
############################
|
||||
# OS X
|
||||
############################
|
||||
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
Icon
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
._*
|
||||
|
||||
|
||||
############################
|
||||
# Linux
|
||||
############################
|
||||
|
||||
*~
|
||||
|
||||
|
||||
############################
|
||||
# Windows
|
||||
############################
|
||||
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
Desktop.ini
|
||||
$RECYCLE.BIN/
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
|
||||
############################
|
||||
# Packages
|
||||
############################
|
||||
|
||||
*.7z
|
||||
*.csv
|
||||
*.dat
|
||||
*.dmg
|
||||
*.gz
|
||||
*.iso
|
||||
*.jar
|
||||
*.rar
|
||||
*.tar
|
||||
*.zip
|
||||
*.com
|
||||
*.class
|
||||
*.dll
|
||||
*.exe
|
||||
*.o
|
||||
*.seed
|
||||
*.so
|
||||
*.swo
|
||||
*.swp
|
||||
*.swn
|
||||
*.swm
|
||||
*.out
|
||||
*.pid
|
||||
|
||||
|
||||
############################
|
||||
# Logs and databases
|
||||
############################
|
||||
|
||||
.tmp
|
||||
*.log
|
||||
*.sql
|
||||
*.sqlite
|
||||
|
||||
|
||||
############################
|
||||
# Misc.
|
||||
############################
|
||||
|
||||
*#
|
||||
.idea
|
||||
nbproject
|
||||
|
||||
|
||||
############################
|
||||
# Node.js
|
||||
############################
|
||||
|
||||
lib-cov
|
||||
lcov.info
|
||||
pids
|
||||
logs
|
||||
results
|
||||
build
|
||||
node_modules
|
||||
.node_history
|
||||
101
packages/strapi-views/.npmignore
Executable file
101
packages/strapi-views/.npmignore
Executable file
@ -0,0 +1,101 @@
|
||||
############################
|
||||
# OS X
|
||||
############################
|
||||
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
Icon
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
._*
|
||||
|
||||
|
||||
############################
|
||||
# Linux
|
||||
############################
|
||||
|
||||
*~
|
||||
|
||||
|
||||
############################
|
||||
# Windows
|
||||
############################
|
||||
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
Desktop.ini
|
||||
$RECYCLE.BIN/
|
||||
*.cab
|
||||
*.msi
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
|
||||
############################
|
||||
# Packages
|
||||
############################
|
||||
|
||||
*.7z
|
||||
*.csv
|
||||
*.dat
|
||||
*.dmg
|
||||
*.gz
|
||||
*.iso
|
||||
*.jar
|
||||
*.rar
|
||||
*.tar
|
||||
*.zip
|
||||
*.com
|
||||
*.class
|
||||
*.dll
|
||||
*.exe
|
||||
*.o
|
||||
*.seed
|
||||
*.so
|
||||
*.swo
|
||||
*.swp
|
||||
*.swn
|
||||
*.swm
|
||||
*.out
|
||||
*.pid
|
||||
|
||||
|
||||
############################
|
||||
# Logs and databases
|
||||
############################
|
||||
|
||||
.tmp
|
||||
*.log
|
||||
*.sql
|
||||
*.sqlite
|
||||
|
||||
|
||||
############################
|
||||
# Misc.
|
||||
############################
|
||||
|
||||
*#
|
||||
.idea
|
||||
nbproject
|
||||
|
||||
|
||||
############################
|
||||
# Node.js
|
||||
############################
|
||||
|
||||
lib-cov
|
||||
lcov.info
|
||||
pids
|
||||
logs
|
||||
results
|
||||
build
|
||||
node_modules
|
||||
.node_history
|
||||
|
||||
|
||||
############################
|
||||
# Tests
|
||||
############################
|
||||
|
||||
test
|
||||
7
packages/strapi-views/LICENSE.md
Executable file
7
packages/strapi-views/LICENSE.md
Executable file
@ -0,0 +1,7 @@
|
||||
Copyright (c) 2015-2017 Strapi Solutions.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
23
packages/strapi-views/README.md
Executable file
23
packages/strapi-views/README.md
Executable file
@ -0,0 +1,23 @@
|
||||
# strapi-redis
|
||||
|
||||
[](https://www.npmjs.org/package/strapi-redis)
|
||||
[](https://www.npmjs.org/package/strapi-redis)
|
||||
[](https://david-dm.org/strapi/strapi-redis)
|
||||
[](https://travis-ci.org/strapi/strapi-redis)
|
||||
[](http://slack.strapi.io)
|
||||
|
||||
This built-in hook allows you to use [Redis](https://redis.io/) as a databases connection. Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker.
|
||||
|
||||
## Motivation
|
||||
|
||||
We developed this hook to use Redis as cache database for our Strapi apps.
|
||||
|
||||
## Resources
|
||||
|
||||
- [MIT License](LICENSE.md)
|
||||
|
||||
## Links
|
||||
|
||||
- [Strapi website](http://strapi.io/)
|
||||
- [Strapi community on Slack](http://slack.strapi.io)
|
||||
- [Strapi news on Twitter](https://twitter.com/strapijs)
|
||||
@ -22,7 +22,9 @@ module.exports = strapi => {
|
||||
*/
|
||||
|
||||
defaults: {
|
||||
views: false
|
||||
views: {
|
||||
enabled: false
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
26
packages/strapi-views/package-lock.json
generated
Normal file
26
packages/strapi-views/package-lock.json
generated
Normal file
@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "strapi-views",
|
||||
"version": "3.0.0-alpha.4.8",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"bluebird": {
|
||||
"version": "3.5.0",
|
||||
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.0.tgz",
|
||||
"integrity": "sha1-eRQg1/VR7qKJdFOop3ZT+WYG1nw="
|
||||
},
|
||||
"consolidate": {
|
||||
"version": "0.14.5",
|
||||
"resolved": "https://registry.npmjs.org/consolidate/-/consolidate-0.14.5.tgz",
|
||||
"integrity": "sha1-WiUEe8dvcwcmZ8jLUsmJiI9JTGM=",
|
||||
"requires": {
|
||||
"bluebird": "3.5.0"
|
||||
}
|
||||
},
|
||||
"lodash": {
|
||||
"version": "4.17.4",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz",
|
||||
"integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4="
|
||||
}
|
||||
}
|
||||
}
|
||||
48
packages/strapi-views/package.json
Normal file
48
packages/strapi-views/package.json
Normal file
@ -0,0 +1,48 @@
|
||||
{
|
||||
"name": "strapi-views",
|
||||
"version": "3.0.0-alpha.4.8",
|
||||
"description": "Views hook to enable server-side rendering for the Strapi framework",
|
||||
"homepage": "http://strapi.io",
|
||||
"keywords": [
|
||||
"redis",
|
||||
"hook",
|
||||
"memory",
|
||||
"cache",
|
||||
"strapi"
|
||||
],
|
||||
"directories": {
|
||||
"lib": "./lib"
|
||||
},
|
||||
"main": "./lib",
|
||||
"dependencies": {
|
||||
"consolidate": "^0.14.5",
|
||||
"lodash": "^4.17.4"
|
||||
},
|
||||
"strapi": {
|
||||
"isHook": true
|
||||
},
|
||||
"author": {
|
||||
"email": "hi@strapi.io",
|
||||
"name": "Strapi team",
|
||||
"url": "http://strapi.io"
|
||||
},
|
||||
"maintainers": [
|
||||
{
|
||||
"name": "Strapi team",
|
||||
"email": "hi@strapi.io",
|
||||
"url": "http://strapi.io"
|
||||
}
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/strapi/strapi.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/strapi/strapi/issues"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 8.0.0",
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
@ -40,7 +40,7 @@ module.exports = function() {
|
||||
'server.json'
|
||||
));
|
||||
|
||||
if (process.env.NODE_ENV === 'development' && server.reload === true) {
|
||||
if (process.env.NODE_ENV === 'development' && server.autoReload === true) {
|
||||
const options = _.assign(
|
||||
{},
|
||||
{
|
||||
@ -94,7 +94,7 @@ module.exports = function() {
|
||||
return require('strapi');
|
||||
}
|
||||
}();
|
||||
|
||||
|
||||
strapi.start();
|
||||
|
||||
} catch (e) {
|
||||
|
||||
@ -143,6 +143,8 @@ class Strapi extends EventEmitter {
|
||||
hooks.call(this)
|
||||
]);
|
||||
|
||||
console.log(this.middlewares.responses);
|
||||
|
||||
// Populate AST with configurations.
|
||||
await appConfigurations.call(this);
|
||||
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
const path = require('path');
|
||||
const glob = require('glob');
|
||||
const utils = require('../utils');
|
||||
const { difference, merge, setWith, get, upperFirst, isString, isEmpty, isObject, orderBy, isBoolean, pullAll, defaults } = require('lodash');
|
||||
const { difference, merge, setWith, get, set, upperFirst, isString, isEmpty, isObject, orderBy, isBoolean, pullAll, defaults } = require('lodash');
|
||||
|
||||
module.exports.nested = function() {
|
||||
return Promise.all([
|
||||
@ -71,7 +71,14 @@ module.exports.app = async function() {
|
||||
|
||||
// Flatten middlewares configurations.
|
||||
const flattenMiddlewaresConfig = middlewareCategories.reduce((acc, index) => {
|
||||
const current = this.config.currentEnvironment[index];
|
||||
const current = merge(this.config.currentEnvironment[index], {
|
||||
public: defaults(this.config.public, {
|
||||
enabled: true
|
||||
}),
|
||||
favicon: defaults(this.config.favicon, {
|
||||
enabled: true
|
||||
})
|
||||
});
|
||||
|
||||
if (isObject(current)) {
|
||||
acc = merge(acc, current);
|
||||
@ -100,6 +107,14 @@ module.exports.app = async function() {
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
// Enable hooks and dependencies related to the connections.
|
||||
for (let name in this.config.connections) {
|
||||
const connection = this.config.connections[name];
|
||||
const connector = connection.connector.replace('strapi-', '');
|
||||
|
||||
enableHookNestedDependencies.call(this, connector, flattenHooksConfig);
|
||||
}
|
||||
|
||||
// Preset config in alphabetical order.
|
||||
this.config.middleware.settings = Object.keys(this.middlewares).reduce((acc, current) => {
|
||||
// Try to find the settings in the current environment, then in the main configurations.
|
||||
@ -116,7 +131,7 @@ module.exports.app = async function() {
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
this.config.hook.settings = Object.keys(this.hooks).reduce((acc, current) => {
|
||||
this.config.hook.settings = Object.keys(this.hook).reduce((acc, current) => {
|
||||
// Try to find the settings in the current environment, then in the main configurations.
|
||||
const currentSettings = flattenHooksConfig[current] || this.config[current];
|
||||
|
||||
@ -183,3 +198,19 @@ module.exports.app = async function() {
|
||||
: `${isEmpty(ssl) || ssl.disabled === true ? 'http' : 'https'}://${this
|
||||
.config.host}:${this.config.port}`;
|
||||
};
|
||||
|
||||
const enableHookNestedDependencies = function (name, flattenHooksConfig) {
|
||||
// Couldn't find configurations for this hook.
|
||||
if (isEmpty(get(flattenHooksConfig, name, true))) {
|
||||
flattenHooksConfig[name] = {
|
||||
enabled: true
|
||||
};
|
||||
|
||||
// Enabled dependencies.
|
||||
if (get(this.hook, `${name}.dependencies`, []).length > 0) {
|
||||
this.hook[name].dependencies.forEach(dependency => {
|
||||
enableHookNestedDependencies.call(this, dependency.replace('strapi-', ''), flattenHooksConfig);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ const { parallel } = require('async');
|
||||
const { setWith, last, upperFirst, lowerFirst, get } = require('lodash');
|
||||
|
||||
module.exports = function() {
|
||||
this.hooks = {};
|
||||
this.hook = {};
|
||||
|
||||
return Promise.all([
|
||||
new Promise((resolve, reject) => {
|
||||
@ -61,14 +61,14 @@ const mountHooks = function (files, cwd) {
|
||||
try {
|
||||
const pkg = JSON.parse(content);
|
||||
|
||||
this.hooks[name] = {
|
||||
this.hook[name] = {
|
||||
loaded: false,
|
||||
identity: name,
|
||||
dependencies: get(pkg, 'strapi.dependencies') || []
|
||||
};
|
||||
|
||||
// Lazy loading.
|
||||
Object.defineProperty(this.hooks[name], 'load', {
|
||||
Object.defineProperty(this.hook[name], 'load', {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
get: () => require(path.resolve(this.config.appPath, cwd, p))
|
||||
|
||||
@ -61,7 +61,7 @@ module.exports = function() {
|
||||
const cwd = path.resolve(__dirname, '..', 'middlewares');
|
||||
|
||||
glob(
|
||||
'./*',
|
||||
'./!(index.js|responses)',
|
||||
{
|
||||
cwd
|
||||
},
|
||||
|
||||
@ -25,7 +25,7 @@ module.exports = function() {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
this.hooks[hook].loaded = true;
|
||||
this.hook[hook].loaded = true;
|
||||
this.emit('hook:' + hook + ':loaded');
|
||||
|
||||
resolve();
|
||||
@ -36,7 +36,7 @@ module.exports = function() {
|
||||
};
|
||||
|
||||
return Promise.all(
|
||||
Object.keys(this.hooks).map(
|
||||
Object.keys(this.hook).map(
|
||||
hook =>
|
||||
new Promise((resolve, reject) => {
|
||||
// Don't load disabled hook.
|
||||
@ -44,8 +44,8 @@ module.exports = function() {
|
||||
return resolve();
|
||||
}
|
||||
|
||||
const module = this.hooks[hook].load;
|
||||
let dependencies = this.hooks[hook].dependencies || [];
|
||||
const module = this.hook[hook].load;
|
||||
let dependencies = this.hook[hook].dependencies || [];
|
||||
|
||||
// Apply default configurations to middleware.
|
||||
if (isUndefined(get(this.config.hook, `settings.${hook}`))) {
|
||||
@ -57,9 +57,9 @@ module.exports = function() {
|
||||
}
|
||||
|
||||
// Take care of hooks internals dependencies.
|
||||
if (dependencies.length > 0 || includes(this.config.hook.loadOrder, hook)) {
|
||||
const position = indexOf(this.config.hook.loadOrder, hook);
|
||||
const previousDependencies = dropRight(this.config.hook.loadOrder, this.config.hook.loadOrder.length - (position + 1));
|
||||
if (dependencies.length > 0 || includes(get(this.config.hook, 'load.order', []), hook)) {
|
||||
const position = indexOf(get(this.config.hook, 'load.order', []), hook);
|
||||
const previousDependencies = dropRight(get(this.config.hook, 'load.order', []), get(this.config.hook, 'load.order', []).length - (position + 1));
|
||||
|
||||
// Remove current hook.
|
||||
previousDependencies.splice(position, 1);
|
||||
@ -77,6 +77,12 @@ module.exports = function() {
|
||||
});
|
||||
|
||||
dependencies.forEach(dependency => {
|
||||
// Some hooks are already loaded, we won't receive
|
||||
// any events of them, so we have to bypass the emitter.
|
||||
if (this.hook[dependency.replace('strapi-', '')].loaded === true) {
|
||||
return queue();
|
||||
}
|
||||
|
||||
this.once('hook:' + dependency.replace('strapi-', '') + ':loaded', () => {
|
||||
queue();
|
||||
})
|
||||
|
||||
@ -6,9 +6,10 @@
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
const cron = require('node-schedule');
|
||||
|
||||
/**
|
||||
* CORS hook
|
||||
* CRON hook
|
||||
*/
|
||||
|
||||
module.exports = strapi => {
|
||||
@ -18,15 +19,7 @@ module.exports = strapi => {
|
||||
*/
|
||||
|
||||
defaults: {
|
||||
cors: {
|
||||
origin: true,
|
||||
expose: ['WWW-Authenticate', 'Server-Authorization'],
|
||||
maxAge: 31536000,
|
||||
credentials: true,
|
||||
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
|
||||
headers: ['Content-Type', 'Authorization'],
|
||||
keepHeadersOnError: false
|
||||
}
|
||||
cron: {}
|
||||
},
|
||||
|
||||
/**
|
||||
@ -35,20 +28,12 @@ module.exports = strapi => {
|
||||
|
||||
initialize: function(cb) {
|
||||
if (
|
||||
_.isPlainObject(strapi.config.middleware.settings.cors) &&
|
||||
!_.isEmpty(strapi.config.middleware.settings.cors)
|
||||
_.isPlainObject(strapi.config.middleware.settings.cron) &&
|
||||
!_.isEmpty(strapi.config.middleware.settings.cron)
|
||||
) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.kcors({
|
||||
origin: strapi.config.middleware.settings.cors.origin,
|
||||
exposeHeaders: strapi.config.middleware.settings.cors.expose,
|
||||
maxAge: strapi.config.middleware.settings.cors.maxAge,
|
||||
credentials: strapi.config.middleware.settings.cors.credentials,
|
||||
allowMethods: strapi.config.middleware.settings.cors.methods,
|
||||
allowHeaders: strapi.config.middleware.settings.cors.headers,
|
||||
keepHeadersOnError: strapi.config.middleware.settings.cors.keepHeadersOnError
|
||||
})
|
||||
);
|
||||
_.forEach(_.keys(strapi.config.middleware.settings.cron), task => {
|
||||
cron.scheduleJob(task, strapi.config.middleware.settings.cron[task]);
|
||||
});
|
||||
}
|
||||
|
||||
cb();
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
const glob = require('glob');
|
||||
const path = require('path');
|
||||
const { parallel } = require('async');
|
||||
const { after, includes, indexOf, dropRight, uniq, defaultsDeep, get, set, isEmpty, isUndefined } = require('lodash');
|
||||
const { after, includes, indexOf, drop, dropRight, uniq, defaultsDeep, get, set, isEmpty, isUndefined, union } = require('lodash');
|
||||
|
||||
module.exports = function() {
|
||||
// Method to initialize middlewares and emit an event.
|
||||
@ -42,7 +42,12 @@ module.exports = function() {
|
||||
}
|
||||
|
||||
const module = this.middlewares[middleware].load;
|
||||
const middlewaresOrder = get(this.config.middleware, 'loadOrder', []).filter(middleware => this.config.middleware.settings[middleware].enabled !== false);
|
||||
|
||||
// Retrieve middlewares configurations order
|
||||
const middlewares = Object.keys(this.middlewares).filter(middleware => this.config.middleware.settings[middleware].enabled !== false);
|
||||
const middlewaresBefore = get(this.config.middleware, 'load.before', []).filter(middleware => this.config.middleware.settings[middleware].enabled !== false);
|
||||
const middlewaresOrder = get(this.config.middleware, 'load.order', []).filter(middleware => this.config.middleware.settings[middleware].enabled !== false);
|
||||
const middlewaresAfter = get(this.config.middleware, 'load.after', []).filter(middleware => this.config.middleware.settings[middleware].enabled !== false);
|
||||
|
||||
|
||||
// Apply default configurations to middleware.
|
||||
@ -54,37 +59,61 @@ module.exports = function() {
|
||||
defaultsDeep(this.config.middleware.settings[middleware], module.defaults[middleware] || module.defaults);
|
||||
}
|
||||
|
||||
if (includes(middlewaresOrder, middleware)) {
|
||||
const position = indexOf(middlewaresOrder, middleware);
|
||||
const previousDependencies = dropRight(middlewaresOrder, middlewaresOrder.length - (position + 1));
|
||||
// Initialize array.
|
||||
let previousDependencies = [];
|
||||
|
||||
// Add BEFORE middlewares to load and remove the current one
|
||||
// to avoid that it waits itself.
|
||||
if (includes(middlewaresBefore, middleware)) {
|
||||
const position = indexOf(middlewaresBefore, middleware);
|
||||
|
||||
|
||||
// Remove current middleware.
|
||||
previousDependencies.splice(position, 1);
|
||||
|
||||
if (previousDependencies.length === 0) {
|
||||
initialize(module, middleware)(resolve, reject);
|
||||
} else {
|
||||
// Wait until the dependencies have been loaded.
|
||||
const queue = after(previousDependencies.length, () => {
|
||||
initialize(module, middleware)(resolve, reject);
|
||||
});
|
||||
|
||||
previousDependencies.forEach(dependency => {
|
||||
// Some hooks are already loaded, we won't receive
|
||||
// any events of them, so we have to bypass the emitter.
|
||||
if (this.middlewares[dependency].loaded === true) {
|
||||
return queue();
|
||||
}
|
||||
|
||||
this.once('middleware:' + dependency + ':loaded', () => {
|
||||
queue();
|
||||
})
|
||||
});
|
||||
}
|
||||
previousDependencies = previousDependencies.concat(dropRight(middlewaresBefore, middlewaresBefore.length - position));
|
||||
} else {
|
||||
previousDependencies = previousDependencies.concat(middlewaresBefore.filter(x => x !== middleware));
|
||||
|
||||
// Add ORDER dependencies to load and remove the current one
|
||||
// to avoid that it waits itself.
|
||||
if (includes(middlewaresOrder, middleware)) {
|
||||
const position = indexOf(middlewaresOrder, middleware);
|
||||
|
||||
previousDependencies = previousDependencies.concat(dropRight(middlewaresOrder, middlewaresOrder.length - position));
|
||||
} else {
|
||||
// Add AFTER middlewares to load and remove the current one
|
||||
// to avoid that it waits itself.
|
||||
if (includes(middlewaresAfter, middleware)) {
|
||||
const position = indexOf(middlewaresAfter, middleware);
|
||||
const toLoadAfter = drop(middlewaresAfter, position);
|
||||
|
||||
// Wait for every middlewares.
|
||||
previousDependencies = previousDependencies.concat(middlewares);
|
||||
// Exclude middlewares which need to be loaded after this one.
|
||||
previousDependencies = previousDependencies.filter(x => !includes(toLoadAfter, x));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove duplicates.
|
||||
previousDependencies = uniq(previousDependencies);
|
||||
|
||||
if (previousDependencies.length === 0) {
|
||||
initialize(module, middleware)(resolve, reject);
|
||||
} else {
|
||||
// Wait until the dependencies have been loaded.
|
||||
const queue = after(previousDependencies.length, () => {
|
||||
initialize(module, middleware)(resolve, reject);
|
||||
});
|
||||
|
||||
previousDependencies.forEach(dependency => {
|
||||
// Some hooks are already loaded, we won't receive
|
||||
// any events of them, so we have to bypass the emitter.
|
||||
if (this.middlewares[dependency].loaded === true) {
|
||||
return queue();
|
||||
}
|
||||
|
||||
this.once('middleware:' + dependency + ':loaded', () => {
|
||||
queue();
|
||||
})
|
||||
});
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
@ -1,110 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Public node modules.
|
||||
const _ = require('lodash');
|
||||
|
||||
/**
|
||||
* Lusca hook
|
||||
*/
|
||||
|
||||
module.exports = strapi => {
|
||||
return {
|
||||
/**
|
||||
* Default options
|
||||
*/
|
||||
|
||||
defaults: {
|
||||
csrf: false,
|
||||
csp: false,
|
||||
p3p: false,
|
||||
hsts: {
|
||||
maxAge: 31536000,
|
||||
includeSubDomains: true
|
||||
},
|
||||
xframe: 'SAMEORIGIN',
|
||||
xssProtection: false
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize the hook
|
||||
*/
|
||||
|
||||
initialize: function(cb) {
|
||||
if (
|
||||
_.isPlainObject(strapi.config.middleware.settings.csrf) &&
|
||||
!_.isEmpty(strapi.config.middleware.settings.csrf)
|
||||
) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.csrf({
|
||||
key: strapi.config.middleware.settings.csrf.key,
|
||||
secret: strapi.config.middleware.settings.csrf.secret
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (_.isPlainObject(strapi.config.middleware.settings.csp) && !_.isEmpty(strapi.config.middleware.settings.csp)) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.csp(strapi.config.middleware.settings.csp)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (_.isString(strapi.config.middleware.settings.xframe)) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.xframe({
|
||||
value: strapi.config.middleware.settings.xframe
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (_.isString(strapi.config.middleware.settings.p3p)) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.p3p({
|
||||
value: strapi.config.middleware.settings.p3p
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
_.isPlainObject(strapi.config.middleware.settings.hsts) &&
|
||||
!_.isEmpty(strapi.config.middleware.settings.hsts)
|
||||
) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.hsts({
|
||||
maxAge: strapi.config.middleware.settings.hsts.maxAge,
|
||||
includeSubDomains: strapi.config.middleware.settings.hsts.includeSubDomains
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
_.isPlainObject(strapi.config.middleware.settings.xssProtection) &&
|
||||
!_.isEmpty(strapi.config.middleware.settings.xssProtection)
|
||||
) {
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.lusca.xssProtection({
|
||||
enabled: strapi.config.middleware.settings.xssProtection.enabled,
|
||||
mode: strapi.config.middleware.settings.xssProtection.mode
|
||||
})
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
cb();
|
||||
}
|
||||
};
|
||||
};
|
||||
@ -1,77 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
|
||||
// Node.js core.
|
||||
const path = require('path');
|
||||
|
||||
// Public modules
|
||||
const _ = require('lodash');
|
||||
const Koa = require('koa');
|
||||
|
||||
/**
|
||||
* Public assets hook
|
||||
*/
|
||||
|
||||
module.exports = strapi => {
|
||||
return {
|
||||
/**
|
||||
* Default options
|
||||
*/
|
||||
|
||||
defaults: {
|
||||
static: true
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize the hook
|
||||
*/
|
||||
|
||||
initialize: function(cb) {
|
||||
if (strapi.config.middleware.settings.static === true) {
|
||||
const isIndexRoute = _.isEmpty(strapi.config.routes)
|
||||
? false
|
||||
: strapi.config.routes.find(route => route.path === '/');
|
||||
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.betterStatic(
|
||||
path.resolve(strapi.config.appPath, strapi.config.paths.static),
|
||||
{
|
||||
index: isIndexRoute ? false : 'index.html',
|
||||
maxage: 60000
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Mount static to a specific path (pattern: `/plugins/xXx`)
|
||||
_.forEach(strapi.plugins, (value, plugin) => {
|
||||
// Create koa sub-app
|
||||
const app = new Koa();
|
||||
|
||||
app.use(
|
||||
strapi.koaMiddlewares.convert(
|
||||
strapi.koaMiddlewares.betterStatic(
|
||||
path.resolve(
|
||||
strapi.config.appPath,
|
||||
'plugins',
|
||||
plugin,
|
||||
strapi.config.paths.static
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
strapi.app.use(
|
||||
strapi.koaMiddlewares.mount(path.join('/plugins', plugin), app)
|
||||
);
|
||||
});
|
||||
|
||||
cb();
|
||||
}
|
||||
};
|
||||
};
|
||||
@ -21,12 +21,20 @@ const strapi = require('../lib/');
|
||||
assert(typeof strapi.middlewares === 'object');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.parser` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.parser.load.initialize === 'function');
|
||||
it('`strapi.middlewares.cors` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.cors.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.kcors` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.cors.load.initialize === 'function');
|
||||
it('`strapi.middlewares.cron` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.cron.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.csp` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.csp.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.csrf` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.csrf.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.favicon` should be a function', () => {
|
||||
@ -37,28 +45,40 @@ const strapi = require('../lib/');
|
||||
assert(typeof strapi.middlewares.gzip.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.language` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.language.load.initialize === 'function');
|
||||
it('`strapi.middlewares.hsts` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.hsts.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.ip` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.ip.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.language` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.language.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.logger` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.logger.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.lusca` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.lusca.load.initialize === 'function');
|
||||
it('`strapi.middlewares.p3p` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.p3p.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.parser` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.parser.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.proxy` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.proxy.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.responses` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.responses.load.initialize === 'function');
|
||||
it('`strapi.middlewares.public` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.public.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.responses` should not be a function', () => {
|
||||
assert(typeof strapi.middlewares.responses === "undefined");
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.responseTime` should be a function', () => {
|
||||
@ -69,19 +89,19 @@ const strapi = require('../lib/');
|
||||
assert(typeof strapi.middlewares.router.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.genericSession` should be a function', () => {
|
||||
it('`strapi.middlewares.session` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.session.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.sslify` should be a function', () => {
|
||||
it('`strapi.middlewares.ssl` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.ssl.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.betterStatic` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.static.load.initialize === 'function');
|
||||
it('`strapi.middlewares.xframe` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.xframe.load.initialize === 'function');
|
||||
});
|
||||
|
||||
it('`strapi.middlewares.views` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.views.load.initialize === 'function');
|
||||
it('`strapi.middlewares.xss` should be a function', () => {
|
||||
assert(typeof strapi.middlewares.xss.load.initialize === 'function');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user