0.8.63 • Published 2 months ago

@mcf/server-boilerplate-middleware v0.8.63

Weekly downloads
246
License
MIT
Repository
-
Last release
2 months ago

npm version

Boilerplate Middleware

Boilerplate middleware for Node projects in MyCareersFuture.

Scope

  • express compatible server
  • cookie handling
  • json body data handling ("application/json")
  • form body data handling ("application/x-www-form-urlencoded")
  • basic http header security
  • content security policy
  • cross-origin-resource-sharing support
  • application metrics
  • server request logging
  • distributed tracing
  • endpoint: /healthz
  • endpoint protection for /healthz
  • endpoint: /readyz
  • endpoint protection for /readyz
  • endpoint: /metrics
  • endpoint protection for /metrics

Installation

Install it via npm or yarn:

npm i @mcf/server-boilerplate-middleware
# or
yarn add @mcf/server-boilerplate-middleware

Usage

import {createServer} from '@mcf/server-boilerplate-middleware';
const server = createServer();
// ...

API

The returned server is an Express server with the following additional APIs.

Options

Options are passed into the constructor function to create the server

import {createServer} from '@mcf/server-boilerplate-middleware';
const {server} = createServer({
  ...options,
});

enableCookieParser : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableCookieParser: true})

enableCompression : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableCompression: true})

enableCSP : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableCSP: true})

enableCORS : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableCORS: true})

enableHttpHeadersSecurity : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableHttpHeadersSecurity: true})

enableMetrics : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableMetrics: true})

enableSerializer : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableSerializer: true})

enableServerLogging : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableServerLogging: true})

enableXray : Boolean

TypeDefaultExample
BooleantrueserverBoilerplate({enableXray: true})

compressionOptions : Object

This configuration is only relevant if the enableCompression parameter was not set to false

KeyTypeNotesDefaults To
chunkSizeNumbersize in bytes of chunk16384
levelNumber0-9 - see https://www.npmjs.com/package/compression for more information9
thresholdNumberminimum size in bytes before compression kicks in102400

Defaults to:

const conpressionOptions = {
  chunkSize: 16 * 1024, // 16kb
  level: 9,
  threshold: 300 * 1024, // 300kb
}

cspOptions : Object

This option is only relevant if the enableCSP flag is not set to false.

KeyTypeNotesDefaults To
childSrcArray<String>populates the child-src value of the CSP header['\'none\'']
connectSrcArray<String>populates the connect-src value of the CSP header['\'none\'']
defaultSrcArray<String>populates the default-src value of the CSP header['\'none\'']
fontSrcArray<String>populates the font-src value of the CSP header['\'none\'']
imgSrcArray<String>populates the img-src value of the CSP header['\'none\'']
scriptSrcArray<String>populates the script-src value of the CSP header['\'none\'']
styleSrcArray<String>populates the style-src value of the CSP header['\'none\'']
frameAncestorsArray<String>populates the frame-ancestors value of the CSP header['\'none\'']
reportUriStringpopulates the report-uri value of the CSP header'/csp-report'

Defaults to:

const cspOptions = {
  childSrc: ['\'none\''],
  connectSrc: ['\'none\''],
  defaultSrc: ['\'none\''],
  fontSrc: ['\'none\''],
  imgSrc: ['\'none\''],
  scriptSrc: ['\'none\''],
  styleSrc: ['\'none\''],
  frameAncestors: ['\'none\''],
  reportUri: '/csp-report',
}

The above configuration produces the following CSP:

"child-src 'none'; connect-src 'none'; default-src 'none'; font-src 'none'; img-src 'none'; script-src 'none'; style-src 'none'; frame-ancestors 'none'; report-uri /csp-report"

corsOptions : Object

This configuration is only relevant if the enableCORS parameter was not set to false

KeyTypeNotesDefaults To
allowedHeadersArray<String>provides the Access-Control-Allow-Headers header value[]
allowedMethodsArray<String>provides the Access-Control-Allow-Methods header value['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS']
allowedOriginsArray<String>provides the Access-Control-Allow-Origins header value[]
credentialsBooleanprovides the Access-Control-Allow-Credentials header valuetrue
preflightContinueBooleandecides whether to pass the request on or respond with 204false

Defaults to:

const corsOptions = {
  allowedHeaders: [],
  allowedMethods: ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS'],
  allowedOrigins: [],
  credentials: true,
  preflightContinue: false,
}

metricsOptions : Object

This configuration is only relevant if the enableMetrics parameter was not set to false

KeyTypeNotesDefaults To
livenessCheckEndpointStringdefines the liveness check endpoint for ignoring in metrics'/healthz'
metricsEndpointStringdefines the metrics endpoint for ignoring in metrics'/metrics'
probeIntervalInMillisecondsNumberdefines interval between metrics scrape3000
readinessCheckEndpointStringdefines the readiness check endpoint for ignoring in metrics'/readyz'
pushgatewayUrlStringdefines the pushgateway URL - when this is not null, the pushgateway is considered activatednull
pushgatewayJobNameStringdefines the job name of the job being pushed to the pushgateway - use this to define the application instance when running in a clusterprocess.env.USER || 'unknown'
pushgatewayTimeoutNumberdefines the timeout of the pushgateway if enabled10000

Defaults to:

const metricsOptions = {
  livenessCheckEndpoint: '/healthz',
  metricsEndpoint: '/metrics',
  probeIntervalInMilliseconds: 3000,
  readinessCheckEndpoint: '/readyz',
  pushgatewayUrl: null,
  pushgatewayJobName: process.env.USER || 'unknown',
  pushgatewayTimeout: 10000,
}

loggingOptions : Object

This configuration is only relevant if the enableServerLogging parameter was not set to false

KeyTypeNotesDefaults To
additionalTokenizersArray of TokenizersAdditional tokenizers with the schema {id: string, fn: (req: Request, res: Response) => any}[]
loggerIApplicationLoggerUsed by the server to create a child loggerundefined
logStreamStringSpecifies a stream to use instead of the default console. For example, use this to link Morgan up with Winstonnull
hostnameTypeStringIf set to "os", the os.hostname() will be used. For all other values, process.env[hostnameType] is used."os"

Defaults to:

const loggingOptions = {
  additionalTokenizers: [],
  logger: createLogger(),
  logStream: null,
  hostnameType: 'os',
}

Development

Installing Dependencies

Run the following from the root of the repository to initialise the dependencies since Lerna manages the dependencies for us across the multiple packages:

npx lerna bootstrap

Running Tests

Note: seems like the tests will fail if run locally on a Macbook. This is because prom-client emits metrics which are platform specific and the tests care catered to Linux. So please spin up linux container by doing docker-compose up and running the tests from within the node container.

To run the tests during development, use at the root directory:

npx lerna run --scope @mcf/server-boilerplate-middleware test:watch

To run the tests on the built package, use:

npx lerna run --scope @mcf/server-boilerplate-middleware test

To run a test server using the boilerplate server, use:

npx lerna run --scope @mcf/server-boilerplate-middleware start

Building

npx lerna run --scope @mcf/server-boilerplate-middleware build

Integration Example

Run the following to setup an example environment:

Open a new terminal and run the following to create server a on port 11111:

SVC_ID=a PORT=11111 npm start;

Open another terminal and run the following to create server b on port 22222:

RSVC_ID=a SVC_ID=b PORT=22222 PROXY_PORT=11111 npm start;

Verify that your local Zipkin instance works and then run the following in yet another terminal to demonstrate tracing:

curl "http://localhost:22222/proxy";

ChangeLog

0.8.62

  • set frameguard action (i.e. x-frame-options) to deny
  • set frameAncestors csp directive to none

0.8.61

  • set referrer policy to strict-origin-when-cross-origin explicitly

0.8.60

  • update 'strict-transport-security : max-age to the recommended period of 2 years

0.8.x

  • update 'strict-transport-security : max-age=31536000; includeSubDomains; preload' as per CSA requirements

0.8.5

  • removed zipkin
  • added aws xray tracing

0.8.2-4

  • added keepalive and header timeout configuration

0.8.1

  • changed configuration signature for tracing

0.7.x

0.7.0

  • added distributed tracing capabilities
  • server instance now exports the following methods:
    • .getTracer()
    • .getContext()
    • .getRequest()

0.6.x

0.6.4

  • added :logStream property in loggingOptions options for providing Morgan with a custom logger to use

0.6.0

  • added Morgan server request logging

0.5.x

0.5.3

  • changed the preflightContinue option to be false by default

0.5.1

  • added features to accommodate a push gateway model (see pushgatewayUrl, pushgatewayTimeout and pushgatewayJobName for more info)
  • if pushgatewayUrl is defined in the metricsOptions options property, the push gateway metrics flow model is activated, metrics will be pushed every :probeIntervalInMilliseconds milliseconds`

0.5.0

  • added Prometheus metrics (see enableMetrics and metricsOptions properties)

0.4.0

  • added CORS support (see enableCORS and corsOptions)

0.3.x

0.3.1

  • added gzip compression module

0.3.0

  • refactored security module into two submodules: http headers and csp
  • also changed API for content security policy (CSP)
  • added new flag, enableCSP, for server initialisation

0.2.x

0.2.1

  • added connect-src to CSP configuration

0.2.0

  • fixed ci pipeline problems, no changes to functionality, we can now expect stably numbered patch releases

0.1.x

0.1.0

  • added content security policy configuration for:
    • 'default-src'
    • 'child-src'
    • 'font-src'
    • 'img-src'
    • 'script-src'
    • 'style-src'
    • 'report-uri'
  • added basic http security headers
    • hides 'x-powered-by'
    • adds 'x-xss-protection: 1; mode=block'
    • adds 'x-content-type-options : nosniff'
    • adds 'x-dns-prefetch-control : off'
    • adds 'x-download-options : noopen'
    • adds 'x-frame-options : SAMEORIGIN'
    • adds 'strict-transport-security : max-age=15552000; includeSubDomains'
  • added body data parsing suport for Content-Type: application/json
  • added body data parsing suport for Content-Type: application/x-www-form-urlencoded
  • added cookie parsing superpowers

0.0.x

0.0.4

  • fixed behaviour to allow import via require('@mcf/server-boilerplate-middleware') without a .default property

0.0.2

  • initial commit with an Express compatible server
0.8.63

2 months ago

0.8.62

7 months ago

0.8.61

8 months ago

0.8.60

8 months ago

0.8.59

10 months ago

0.8.56

12 months ago

0.8.55

12 months ago

0.8.58

11 months ago

0.8.57

11 months ago

0.8.54

1 year ago

0.8.52

1 year ago

0.8.51

1 year ago

0.8.53

1 year ago

0.8.50

1 year ago

0.8.49

1 year ago

0.8.48

1 year ago

0.8.47

1 year ago

0.8.46

1 year ago

0.8.45

2 years ago

0.8.44

2 years ago

0.8.43

2 years ago

0.8.42

2 years ago

0.8.41

2 years ago

0.8.40

2 years ago

0.8.39

2 years ago

0.8.38

3 years ago

0.8.37

3 years ago

0.8.36

3 years ago

0.8.35

3 years ago

0.8.34

3 years ago

0.8.33

3 years ago

0.8.30

3 years ago

0.8.29

3 years ago

0.8.28

3 years ago

0.8.26

3 years ago

0.8.25

3 years ago

0.8.23

4 years ago

0.8.24

4 years ago

0.8.22

4 years ago

0.8.21

4 years ago

0.8.20

4 years ago

0.8.19

4 years ago

0.8.18

4 years ago

0.8.17

4 years ago

0.8.16

4 years ago

0.8.15

4 years ago

0.8.14

4 years ago

0.8.13

4 years ago

0.8.12

5 years ago

0.8.11

5 years ago

0.8.10

5 years ago

0.8.9

5 years ago

0.8.8

5 years ago

0.8.7

5 years ago

0.8.6

5 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.2

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.6

6 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.73

6 years ago

0.1.72

6 years ago

0.1.71

6 years ago

0.1.70

6 years ago

0.1.69

6 years ago

0.1.68

6 years ago

0.1.67

6 years ago

0.1.66

6 years ago

0.1.65

6 years ago

0.1.64

6 years ago

0.1.63

6 years ago

0.1.62

6 years ago

0.1.61

6 years ago

0.1.60

6 years ago

0.1.59

6 years ago

0.1.58

6 years ago

0.1.57

6 years ago

0.1.56

6 years ago

0.1.55

6 years ago

0.1.54

6 years ago

0.1.53

6 years ago

0.1.52

6 years ago

0.1.51

6 years ago

0.1.50

6 years ago

0.1.49

6 years ago

0.1.48

6 years ago

0.1.47

6 years ago

0.1.46

6 years ago

0.1.45

6 years ago

0.1.44

6 years ago

0.1.43

6 years ago

0.1.42

6 years ago

0.1.41

6 years ago

0.1.40

6 years ago

0.1.39

6 years ago

0.1.38

6 years ago

0.1.37

6 years ago

0.1.36

6 years ago

0.1.35

6 years ago

0.1.34

6 years ago

0.1.33

6 years ago

0.1.32

6 years ago

0.1.31

6 years ago

0.1.30

6 years ago

0.1.29

6 years ago

0.1.28

6 years ago

0.1.27

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago