Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] include gasket-express-plugin in monorepo #13

Merged
merged 2 commits into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/gasket-express-plugin/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "godaddy"
}
1 change: 1 addition & 0 deletions packages/gasket-express-plugin/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json binary
69 changes: 69 additions & 0 deletions packages/gasket-express-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Logs (npm-debug.log, yarn-error.log, etc)
logs
*.log

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# OSX Finder cache files
.DS_Store

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# Editor files
.idea
*.iml

### ▲ .gitignore contents above
### ▼ additional ignore files for publishing

test/*
.eslintrc
.gitattributes
7 changes: 7 additions & 0 deletions packages/gasket-express-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CHANGELOG

### 1.0.0

- Renaming express-create to expressCreate
- Separate express plugins from core
- Initial release.
21 changes: 21 additions & 0 deletions packages/gasket-express-plugin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 GoDaddy Operating Company, LLC.

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.
111 changes: 111 additions & 0 deletions packages/gasket-express-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# @gasket/express-plugin

The `express-plugin` adds `express` to your application.

## Installation

```
npm install --save @gasket/express-plugin
```

## Configuration

The express plugin is configured using the `gasket.config.js` file.

- First, add it to the `plugins` section of your `gasket.config.js`:

```js
{
"plugins": [
"add": ["express"]
]
}
```

All the configurations for the plugin are added under `express` in the config:

- `compression`: true by default. Can to set to false if applying compression differently.
- `excludedRoutesRegex`: Regex of the routes to exclude by express.

#### Example configuration

```js
module.exports = {
plugins: {
add: ['express']
},
express: {
compression: false,
excludedRoutesRegex: /^(?!\/_next\/)/
}
}
```

## Hooks

`express` hooks into the following lifecycle in order to work.

#### expressCreate

```js
module.exports = {
name: 'express',
hooks: {
/**
* Creates the express app
* @param {Gasket} gasket The Gasket API
* @return {Promise<Object>} express app
*/
'expressCreate': async function createExpress(gasket, devServer) {
return express();
}
}
};
```

## Lifecycles

#### middleware

Executed when the `express` server has been created, it will apply all returned
functions as middleware.

```js
module.exports = {
hooks: {
/**
* Add Express middleware
*
* @param {Gasket} gasket The Gasket API
* @param {Express} app - Express app instance
* @returns {function|function[]} middleware(s)
*/
middleware: function (gasket, app) {
return require('x-xss-protection')();
}
}
}
```

You may also return an `Array` to inject more than one middleware.

#### express

Executed **after** the `middleware` event for when you need full control over
the `express` instance.

```js
module.exports = {
hooks: {
/**
* Update Express app instance
*
* @param {Gasket} gasket The Gasket API
* @param {Express} express - Express app instance
* @returns {function|function[]} middleware(s)
*/
express: async function (gasket, express) {
}
}
}
```
48 changes: 48 additions & 0 deletions packages/gasket-express-plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const debug = require('diagnostics')('gasket:express');
const express = require('express');
const cookieParser = require('cookie-parser');
const compression = require('compression');

module.exports = {
name: 'express',
hooks: {
/**
* Create the Express instance and setup the lifecycle hooks.
*
* @param {Gasket} gasket Gasket API.
* @param {Object} config application config.
* @returns {Express} The web server.
* @public
*/
expressCreate: async function createExpress(gasket) {
const { config } = gasket;
const excludedRoutesRegex = config.express && config.express.excludedRoutesRegex;
const app = express();

if (excludedRoutesRegex) {
app.use(excludedRoutesRegex, cookieParser());
} else {
app.use(cookieParser());
}

const { compression: compressionConfig = true } = config.express || {};
if (compressionConfig) {
app.use(compression());
}

const middlewares = await gasket.exec('middleware', app);

debug('applied %s middleware layers to express', middlewares.length);
middlewares.forEach(layer => {
if (excludedRoutesRegex) {
app.use(excludedRoutesRegex, layer);
} else {
app.use(layer);
}
});

await gasket.exec('express', app);
return app;
}
}
};
47 changes: 47 additions & 0 deletions packages/gasket-express-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@gasket/express-plugin",
"version": "1.0.0",
"description": "Adds express support to your application",
"author": "GoDaddy Operating Company, LLC",
"homepage": "https://github.com/godaddy/gasket/tree/master/packages/gasket-express-plugin",
"license": "MIT",
"main": "index.js",
"scripts": {
"lint": "eslint index.js test.js",
"lint:fix": "npm run lint -- --fix",
"report": "nyc report --reporter=lcov",
"pretest": "npm run lint",
"test": "nyc --reporter=text --reporter=json-summary npm run test:runner",
"test:runner": "mocha --require ./setup.js test.js"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/godaddy/gasket"
},
"keywords": [
"express",
"gasket",
"plugin"
],
"dependencies": {
"compression": "^1.7.2",
"cookie-parser": "^1.4.3",
"diagnostics": "^2.0.0"
},
"peerDependencies": {
"express": "^4.16.3"
},
"devDependencies": {
"assume": "^2.1.0",
"assume-sinon": "^1.0.0",
"eslint": "^5.13.0",
"eslint-config-godaddy": "^3.0.0",
"eslint-plugin-json": "^1.3.2",
"eslint-plugin-mocha": "^5.2.1",
"express": "^4.16.3",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"proxyquire": "^2.1.0",
"sinon": "^7.2.3"
}
}
4 changes: 4 additions & 0 deletions packages/gasket-express-plugin/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const assumeSinonPlugin = require('assume-sinon');
const assume = require('assume');

assume.use(assumeSinonPlugin);
Loading