@appliedblockchain/koa-healthcheck
Advanced tools
Comparing version 1.2.0 to 1.3.0
20
index.js
@@ -11,3 +11,3 @@ 'use strict' | ||
exports.healthcheck = function (provided) { | ||
exports.healthcheck = function (provided, healthToken) { | ||
const options = Object.assign({ | ||
@@ -52,8 +52,16 @@ path: '/health' | ||
const custom = options.custom ? | ||
await generateCustom(options.custom) : | ||
{} | ||
ctx.body = Object.assign({}, healthReport, custom) | ||
if (healthToken && ctx.headers['health-token'] === healthToken) { | ||
const custom = options.custom ? | ||
await generateCustom(options.custom) : | ||
{} | ||
ctx.body = Object.assign({}, healthReport, custom) | ||
} else if (options.custom) { | ||
ctx.body = options.custom | ||
} else { | ||
ctx.body = { | ||
timestamp: Date.now(), | ||
uptime: process.uptime() | ||
} | ||
} | ||
} | ||
} |
{ | ||
"name": "@appliedblockchain/koa-healthcheck", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Koa healthcheck middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -35,1 +35,23 @@ ## Koa Healthcheck Middleware | ||
``` | ||
#### Healthcheck Token: | ||
If healthcheck token is not provided, healthcheck would not show detail information about server. | ||
```javascript | ||
const HEALTH_TOKEN = 'example-token-1234567890' | ||
app.use(healthcheck({ custom: { | ||
foo: 'bar', | ||
bin: () => 'baz' | ||
}}, HEALTH_TOKEN )) | ||
``` | ||
Provides detail information about server: | ||
```shell | ||
curl -X GET -H "health-token:example-token-1234567890" localhost:3000/health | ||
``` | ||
Provides minimal/custom information only: | ||
```shell | ||
curl -X GET localhost:3000/health | ||
``` |
@@ -6,2 +6,9 @@ const request = require('supertest') | ||
const HEALTH_TOKEN = '71ef10b1b40bc496461d176c4d62379f83d4c32246d7e6a4889d166b831d32fa' | ||
const healthTokenHeader = { | ||
headers: { | ||
'health-token': HEALTH_TOKEN | ||
} | ||
} | ||
describe('Healthcheck', () => { | ||
@@ -16,2 +23,11 @@ | ||
expect(propertyNames).toContain('uptime') | ||
}) | ||
test('middleware with default path and health token', async () => { | ||
const middlewareObject = healthcheck(undefined, HEALTH_TOKEN) | ||
const ctx = Object.assign({ path: '/health' }, healthTokenHeader) | ||
await middlewareObject(ctx) | ||
const propertyNames = Object.keys(ctx.body) | ||
expect(propertyNames).toContain('timestamp') | ||
expect(propertyNames).toContain('uptime') | ||
expect(propertyNames).toContain('application') | ||
@@ -23,4 +39,4 @@ expect(propertyNames).toContain('resources') | ||
test('middleware with custom path', async () => { | ||
const middlewareObject = healthcheck({ path: '/custom' }) | ||
const ctx = { path: '/custom' } | ||
const middlewareObject = healthcheck({ path: '/custom' }, HEALTH_TOKEN) | ||
const ctx = Object.assign({ path: '/custom' }, healthTokenHeader) | ||
await middlewareObject(ctx) | ||
@@ -41,4 +57,4 @@ const propertyNames = Object.keys(ctx.body) | ||
} | ||
const middlewareObject = healthcheck({ custom }) | ||
const ctx = { path: '/health' } | ||
const middlewareObject = healthcheck({ custom }, HEALTH_TOKEN) | ||
const ctx = Object.assign({ path: '/health' }, healthTokenHeader) | ||
await middlewareObject(ctx) | ||
@@ -51,3 +67,3 @@ expect(ctx.body.foo).toEqual('bar') | ||
test('middleware next', async () => { | ||
const middlewareObject = healthcheck({ path: '/custom' }) | ||
const middlewareObject = healthcheck({ path: '/custom' }, HEALTH_TOKEN) | ||
const ctx = {} | ||
@@ -61,6 +77,8 @@ const mockNext = jest.fn() | ||
const app = new Koa() | ||
app.use(healthcheck()) | ||
app.use(healthcheck(null, HEALTH_TOKEN)) | ||
const server = app.listen() | ||
const close = makeClose(server) | ||
const res = await request(server).get('/health').expect(200) | ||
const res = await request(server).get('/health').set({ | ||
'health-token': HEALTH_TOKEN | ||
}).expect(200) | ||
const propertyNames = Object.keys(res.body) | ||
@@ -83,6 +101,8 @@ expect(propertyNames).toContain('timestamp') | ||
} | ||
app.use(healthcheck({ custom })) | ||
app.use(healthcheck({ custom }, HEALTH_TOKEN)) | ||
const server = app.listen() | ||
const close = makeClose(server) | ||
const res = await request(server).get('/health').expect(200) | ||
const res = await request(server).get('/health').set({ | ||
'health-token': HEALTH_TOKEN | ||
}).expect(200) | ||
expect(res.body.baz).toEqual(1) | ||
@@ -89,0 +109,0 @@ await close() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
8579
175
57