diff --git a/packages/strapi-redis/lib/index.js b/packages/strapi-redis/lib/index.js index 8b0697c95f..f7565315e0 100755 --- a/packages/strapi-redis/lib/index.js +++ b/packages/strapi-redis/lib/index.js @@ -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); diff --git a/packages/strapi-views/.editorconfig b/packages/strapi-views/.editorconfig new file mode 100755 index 0000000000..473e45184b --- /dev/null +++ b/packages/strapi-views/.editorconfig @@ -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 diff --git a/packages/strapi-views/.gitignore b/packages/strapi-views/.gitignore new file mode 100755 index 0000000000..664d4fb312 --- /dev/null +++ b/packages/strapi-views/.gitignore @@ -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 diff --git a/packages/strapi-views/.npmignore b/packages/strapi-views/.npmignore new file mode 100755 index 0000000000..1ccdfb71b3 --- /dev/null +++ b/packages/strapi-views/.npmignore @@ -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 diff --git a/packages/strapi-views/LICENSE.md b/packages/strapi-views/LICENSE.md new file mode 100755 index 0000000000..e27c5b5d59 --- /dev/null +++ b/packages/strapi-views/LICENSE.md @@ -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. diff --git a/packages/strapi-views/README.md b/packages/strapi-views/README.md new file mode 100755 index 0000000000..c2c11b2228 --- /dev/null +++ b/packages/strapi-views/README.md @@ -0,0 +1,23 @@ +# strapi-redis + +[![npm version](https://img.shields.io/npm/v/strapi-redis.svg)](https://www.npmjs.org/package/strapi-redis) +[![npm downloads](https://img.shields.io/npm/dm/strapi-redis.svg)](https://www.npmjs.org/package/strapi-redis) +[![npm dependencies](https://david-dm.org/strapi/strapi-redis.svg)](https://david-dm.org/strapi/strapi-redis) +[![Build status](https://travis-ci.org/strapi/strapi-redis.svg?branch=master)](https://travis-ci.org/strapi/strapi-redis) +[![Slack status](http://strapi-slack.herokuapp.com/badge.svg)](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) diff --git a/packages/strapi/lib/middlewares/views/index.js b/packages/strapi-views/lib/index.js similarity index 97% rename from packages/strapi/lib/middlewares/views/index.js rename to packages/strapi-views/lib/index.js index 9ab0063b1e..499c00f05e 100644 --- a/packages/strapi/lib/middlewares/views/index.js +++ b/packages/strapi-views/lib/index.js @@ -22,7 +22,9 @@ module.exports = strapi => { */ defaults: { - views: false + views: { + enabled: false + } }, /** diff --git a/packages/strapi-views/package-lock.json b/packages/strapi-views/package-lock.json new file mode 100644 index 0000000000..13795b340c --- /dev/null +++ b/packages/strapi-views/package-lock.json @@ -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=" + } + } +} diff --git a/packages/strapi-views/package.json b/packages/strapi-views/package.json new file mode 100644 index 0000000000..60c1ced14b --- /dev/null +++ b/packages/strapi-views/package.json @@ -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" +} diff --git a/packages/strapi/bin/strapi-start.js b/packages/strapi/bin/strapi-start.js index ea2e278b96..05018899ed 100644 --- a/packages/strapi/bin/strapi-start.js +++ b/packages/strapi/bin/strapi-start.js @@ -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) { diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index e35bd0c3e5..1c94ca6748 100644 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -143,6 +143,8 @@ class Strapi extends EventEmitter { hooks.call(this) ]); + console.log(this.middlewares.responses); + // Populate AST with configurations. await appConfigurations.call(this); diff --git a/packages/strapi/lib/core/configurations.js b/packages/strapi/lib/core/configurations.js index 33f83db708..e25d49aa31 100644 --- a/packages/strapi/lib/core/configurations.js +++ b/packages/strapi/lib/core/configurations.js @@ -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); + }); + } + } +} diff --git a/packages/strapi/lib/core/hooks.js b/packages/strapi/lib/core/hooks.js index a7d522084e..8eea34e1e4 100644 --- a/packages/strapi/lib/core/hooks.js +++ b/packages/strapi/lib/core/hooks.js @@ -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)) diff --git a/packages/strapi/lib/core/middlewares.js b/packages/strapi/lib/core/middlewares.js index 1ea336f114..e5396981b8 100644 --- a/packages/strapi/lib/core/middlewares.js +++ b/packages/strapi/lib/core/middlewares.js @@ -61,7 +61,7 @@ module.exports = function() { const cwd = path.resolve(__dirname, '..', 'middlewares'); glob( - './*', + './!(index.js|responses)', { cwd }, diff --git a/packages/strapi/lib/hooks/index.js b/packages/strapi/lib/hooks/index.js index 0dd3747692..3df9b27b08 100644 --- a/packages/strapi/lib/hooks/index.js +++ b/packages/strapi/lib/hooks/index.js @@ -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(); }) diff --git a/packages/strapi/lib/middlewares/cors/index.js b/packages/strapi/lib/middlewares/cors/index.js index a958be587c..18dbb79c4f 100644 --- a/packages/strapi/lib/middlewares/cors/index.js +++ b/packages/strapi/lib/middlewares/cors/index.js @@ -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(); diff --git a/packages/strapi/lib/middlewares/index.js b/packages/strapi/lib/middlewares/index.js index 88db11d835..38e6db5ca0 100644 --- a/packages/strapi/lib/middlewares/index.js +++ b/packages/strapi/lib/middlewares/index.js @@ -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(); + }) + }); } }) ) diff --git a/packages/strapi/lib/middlewares/lusca/index.js b/packages/strapi/lib/middlewares/lusca/index.js deleted file mode 100644 index 703ed1c95e..0000000000 --- a/packages/strapi/lib/middlewares/lusca/index.js +++ /dev/null @@ -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(); - } - }; -}; diff --git a/packages/strapi/lib/middlewares/static/index.js b/packages/strapi/lib/middlewares/static/index.js deleted file mode 100644 index 8fa62186f6..0000000000 --- a/packages/strapi/lib/middlewares/static/index.js +++ /dev/null @@ -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(); - } - }; -}; diff --git a/packages/strapi/test/middlewares.js b/packages/strapi/test/middlewares.js index 2578111458..a0e1f417c1 100644 --- a/packages/strapi/test/middlewares.js +++ b/packages/strapi/test/middlewares.js @@ -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'); }); });