docs-server
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -0,1 +1,16 @@ | ||
<a name="1.5.0"></a> | ||
# [1.5.0](https://github.com/lbwa/docs-server/compare/v1.4.1...v1.5.0) (2018-08-16) | ||
### Bug Fixes | ||
* **Etag:** correct Etag to string type rather than number type ([5d3a027](https://github.com/lbwa/docs-server/commit/5d3a027)) | ||
### Features | ||
* **Server:** add Etag module ([17e69d2](https://github.com/lbwa/docs-server/commit/17e69d2)) | ||
<a name="1.4.1"></a> | ||
@@ -2,0 +17,0 @@ ## [1.4.1](https://github.com/lbwa/docs-server/compare/v1.4.0...v1.4.1) (2018-08-15) |
@@ -11,8 +11,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const gen = require('../generator'); | ||
const gen = require('../generator/server'); | ||
const { stringify } = require('../utils/index'); | ||
module.exports = (ctx, next) => __awaiter(this, void 0, void 0, function* () { | ||
const path = ctx.path.replace(/^\//, ''); | ||
const contentList = gen.contentList; | ||
if (!contentList[path]) { | ||
const contentStorage = gen.contentStorage; | ||
if (!contentStorage[path]) { | ||
yield next(); | ||
@@ -22,3 +22,3 @@ return; | ||
ctx.status = 200; | ||
ctx.body = stringify(contentList[path]); | ||
ctx.body = stringify(contentStorage[path]); | ||
ctx.set({ | ||
@@ -25,0 +25,0 @@ 'Content-Type': 'application/json; charset=utf-8' |
@@ -11,4 +11,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const staticGenerator = require("./generator/static"); | ||
const Server = require('./server'); | ||
const gen = require('./generator'); | ||
const gen = require('./generator/server'); | ||
const join = require('path').join; | ||
@@ -19,3 +20,3 @@ function resolve(dir) { | ||
class Application { | ||
constructor({ cwd = resolve('/'), dest = resolve('/menu.json'), port = '8800', headers = {}, threshold = 1, extra = [], filter, headerMiddleware } = {}) { | ||
constructor({ cwd = resolve('/'), dest = resolve('/menu.json'), port = '8800', mode = 'server', headers = {}, threshold = 1, extra = [], filter, headerMiddleware, staticNormalize = null } = {}) { | ||
this.options = { | ||
@@ -25,2 +26,3 @@ cwd, | ||
port, | ||
mode, | ||
headers, | ||
@@ -30,16 +32,27 @@ threshold, | ||
filter, | ||
headerMiddleware | ||
headerMiddleware, | ||
staticNormalize | ||
}; | ||
this.activate(); | ||
this.run(); | ||
} | ||
activate() { | ||
run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const options = this.options; | ||
this.genPromise = this.activateGenerator(options.cwd, options.dest, options.filter); | ||
if (options.mode !== 'server') { | ||
this.staticMode(); | ||
return; | ||
} | ||
this.genPromise = this.runGenerator(options.cwd, options.dest, options.filter); | ||
this.gen = yield this.genPromise; | ||
this.server = this.activateServer(options.headers, options.threshold, this.gen.contentList, options.extra, options.dest, options.headerMiddleware); | ||
this.server = this.runServer(options.headers, options.threshold, this.gen.contentStorage, options.extra, options.dest, options.headerMiddleware); | ||
}); | ||
} | ||
activateGenerator(cwd, dest, filter) { | ||
return gen.activate({ | ||
staticMode() { | ||
staticGenerator.run({ | ||
cwd: this.options.cwd, | ||
normalize: this.options.staticNormalize | ||
}); | ||
} | ||
runGenerator(cwd, dest, filter) { | ||
return gen.run({ | ||
cwd, | ||
@@ -50,3 +63,3 @@ dest, | ||
} | ||
activateServer(customHeaders, threshold, contentList, extra, dest, headerMiddleware) { | ||
runServer(customHeaders, threshold, contentStorage, extra, dest, headerMiddleware) { | ||
const port = this.options.port; | ||
@@ -56,3 +69,3 @@ const server = new Server({ | ||
threshold, | ||
contentList, | ||
contentStorage, | ||
extra, | ||
@@ -59,0 +72,0 @@ dest, |
@@ -11,4 +11,4 @@ "use strict"; | ||
const router = new Router(); | ||
function createRoutes(contentList, extra, dest) { | ||
const docsRoutes = Object.keys(contentList); | ||
function createRoutes(contentStorage, extra, dest) { | ||
const docsRoutes = Object.keys(contentStorage); | ||
router.get('/', home); | ||
@@ -24,4 +24,4 @@ for (let route of docsRoutes) { | ||
} | ||
module.exports = function createRouter(contentList, extra, dest) { | ||
createRoutes(contentList, extra, dest) | ||
module.exports = function createRouter(contentStorage, extra, dest) { | ||
createRoutes(contentStorage, extra, dest) | ||
.get('*', createErrorHandle(404)) | ||
@@ -28,0 +28,0 @@ .all('*', createErrorHandle(405)); |
@@ -10,3 +10,3 @@ "use strict"; | ||
class Server extends Koa { | ||
constructor({ customHeaders = {}, threshold = 1, contentList = {}, extra = [], dest = './menu.json', headerMiddleware } = {}) { | ||
constructor({ customHeaders = {}, threshold = 1, contentStorage = {}, extra = [], dest = './menu.json', headerMiddleware } = {}) { | ||
super(); | ||
@@ -17,3 +17,3 @@ this.__etag = etag.createEtag(); | ||
this.setGzip(threshold); | ||
this.setRouter(contentList, extra, dest); | ||
this.setRouter(contentStorage, extra, dest); | ||
} | ||
@@ -33,4 +33,4 @@ setIOMiddleware() { | ||
} | ||
setRouter(contentList, extra, dest) { | ||
const router = createRouter(contentList, extra, dest); | ||
setRouter(contentStorage, extra, dest) { | ||
const router = createRouter(contentStorage, extra, dest); | ||
this.use(router.routes()); | ||
@@ -37,0 +37,0 @@ this.use(router.allowedMethods()); |
{ | ||
"name": "docs-server", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "A server which is used to build one of microservices for docs system", | ||
@@ -19,2 +19,3 @@ "main": "dist/index.js", | ||
"ts": "npx tsc", | ||
"static": "yarn clean && yarn ts && node sample/static", | ||
"server": "node sample/dev", | ||
@@ -30,8 +31,9 @@ "dev": "yarn clean && yarn run ts && yarn run server", | ||
"chalk": "^2.4.1", | ||
"front-matter": "^2.3.0", | ||
"glob": "^7.1.2", | ||
"gray-matter": "^4.0.1", | ||
"koa": "^2.5.2", | ||
"koa-compress": "^3.0.0", | ||
"koa-router": "^7.4.0", | ||
"koa-send": "^5.0.0" | ||
"koa-send": "^5.0.0", | ||
"mark-to-json": "^0.2.0" | ||
}, | ||
@@ -38,0 +40,0 @@ "devDependencies": { |
@@ -9,3 +9,3 @@ # Docs-server [![npm](https://img.shields.io/npm/v/docs-server.svg)](https://www.npmjs.com/package/docs-server) [![CircleCI](https://circleci.com/gh/lbwa/docs-server.svg?style=svg)](https://circleci.com/gh/lbwa/docs-server) [![node](https://img.shields.io/node/v/docs-server.svg)](https://www.npmjs.com/package/docs-server) | ||
I use this project in some platform which user has no write permission. so this project don't support for adding new docs when server is running. This is intentional. You can use this project to your own documentation module If you have same situation. | ||
I use this project in some platform where user has no write permission on runtime mode, so this project don't support for adding new docs when server is running. This is intentional. You can use this project to your own documentation module If you have same situation. | ||
@@ -26,2 +26,4 @@ ## Features | ||
- Support `static` mode, You can only generate `JSON` static files without server building. | ||
## Installing | ||
@@ -39,3 +41,3 @@ | ||
- Easy running | ||
- Building server from easy way | ||
@@ -49,4 +51,6 @@ ```js | ||
- ( Optional ) You can specify your custom configuration. | ||
### Server mode | ||
You can specify your custom configuration. | ||
```js | ||
@@ -159,4 +163,27 @@ const resolve = require('path').resolve | ||
### Static mode | ||
```js | ||
const DocsServer = require('docs-server') | ||
// JSON file path will be generated from original markdown files path | ||
new DocsServer({ | ||
mode: 'static', | ||
// Default behavior is only remove files extension | ||
staticNormalize (origin) { | ||
// do something you like | ||
return /* for example */ origin.replace(/\.md$/, '') | ||
} | ||
}) | ||
``` | ||
In the static mode, `mode` option is ***required***, and `staticNormalize` is ***optional***. `staticNormalize` option is used to specify what you want to change based on markdown file path. | ||
The sample file under static mode is [here] | ||
[here]:./sample/static.js | ||
## CHANGELOG | ||
[CHANGELOG](./CHANGELOG.md) |
@@ -23,8 +23,8 @@ import Koa = require('koa') | ||
export interface initialContent { | ||
contentData: string | ||
export interface contentWrapper { | ||
content: string | ||
origin: string | ||
} | ||
export interface contentList { | ||
export interface contentStorage { | ||
[path: string]: contentItem | ||
@@ -36,3 +36,3 @@ } | ||
dest: string | ||
filter: Function | ||
filter: (origin: string) => string | ||
} | ||
@@ -44,2 +44,3 @@ | ||
port?: number | string | ||
mode?: string | ||
headers?: resHeaders | ||
@@ -50,2 +51,3 @@ threshold?: number | ||
headerMiddleware?: headerMiddleware | ||
staticNormalize? : (path: string) => string | ||
} | ||
@@ -56,3 +58,3 @@ | ||
threshold?: number | ||
contentList?: contentList | ||
contentStorage?: contentStorage | ||
extra?: extraRoute[] | ||
@@ -59,0 +61,0 @@ dest?: string |
import Koa = require('koa') | ||
const gen = require('../generator') | ||
const gen = require('../generator/server') | ||
const { stringify } = require('../utils/index') | ||
@@ -8,5 +8,5 @@ | ||
const path = ctx.path.replace(/^\//, '') | ||
const contentList = gen.contentList // sync with gen.contentList, same object | ||
const contentStorage = gen.contentStorage // sync with gen.contentStorage, same object | ||
if (!contentList[path]) { | ||
if (!contentStorage[path]) { | ||
await next() | ||
@@ -17,3 +17,3 @@ return | ||
ctx.status = 200 | ||
ctx.body = stringify(contentList[path]) | ||
ctx.body = stringify(contentStorage[path]) | ||
ctx.set({ | ||
@@ -20,0 +20,0 @@ 'Content-Type': 'application/json; charset=utf-8' |
@@ -0,7 +1,8 @@ | ||
import http = require('http') | ||
import staticGenerator = require('./generator/static') | ||
import { appOptions, extraRoute, server } from './config/types' | ||
import Gen from './generator' | ||
import http = require('http') | ||
import ServerGenerator from './generator/server' | ||
const Server = require('./server') | ||
const gen = require('./generator') | ||
const gen = require('./generator/server') | ||
const join = require('path').join | ||
@@ -32,4 +33,4 @@ | ||
server: http.Server // http.Server -> to expose `server.close` function | ||
genPromise: Promise<Gen> | ||
gen: Gen | ||
genPromise: Promise<ServerGenerator> | ||
gen: ServerGenerator | ||
@@ -41,2 +42,3 @@ constructor ( | ||
port = '8800', | ||
mode = 'server', | ||
headers = {}, | ||
@@ -46,3 +48,4 @@ threshold = 1, | ||
filter, | ||
headerMiddleware | ||
headerMiddleware, | ||
staticNormalize = null | ||
}: appOptions = {} | ||
@@ -54,2 +57,3 @@ ) { | ||
port, | ||
mode, | ||
headers, | ||
@@ -59,11 +63,17 @@ threshold, | ||
filter, | ||
headerMiddleware | ||
headerMiddleware, | ||
staticNormalize | ||
} | ||
this.activate() | ||
this.run() | ||
} | ||
async activate () { | ||
async run () { | ||
const options = this.options | ||
if (options.mode !== 'server') { | ||
this.staticMode() | ||
return | ||
} | ||
/** | ||
@@ -74,3 +84,3 @@ * 1. this.activeGenerator will be invoked immediately | ||
*/ | ||
this.genPromise = this.activateGenerator( | ||
this.genPromise = this.runGenerator( | ||
options.cwd, | ||
@@ -90,6 +100,6 @@ options.dest, | ||
// Don't be invoked until generator complete mission | ||
this.server = this.activateServer( | ||
this.server = this.runServer( | ||
options.headers, | ||
options.threshold, | ||
this.gen.contentList, | ||
this.gen.contentStorage, | ||
options.extra, | ||
@@ -101,4 +111,11 @@ options.dest, | ||
staticMode () { | ||
staticGenerator.run({ | ||
cwd: this.options.cwd, | ||
normalize: this.options.staticNormalize | ||
}) | ||
} | ||
/** | ||
*activate generator | ||
*run generator | ||
* | ||
@@ -111,4 +128,4 @@ * @param {string} cwd project root path(current working directory) | ||
*/ | ||
activateGenerator (cwd: string, dest: string, filter: Function) { | ||
return gen.activate({ | ||
runGenerator (cwd: string, dest: string, filter: Function) { | ||
return gen.run({ | ||
cwd, | ||
@@ -125,3 +142,3 @@ dest, | ||
* @param {Number} threshold minimum size in bytes to turn on gzip | ||
* @param {Gen['contentList']} contentList content storage | ||
* @param {Gen['contentStorage']} contentStorage content storage | ||
* @param {extraRoute[]} extra extra static resources routes | ||
@@ -133,6 +150,6 @@ * @param {string} dest the output path of menu.json | ||
*/ | ||
activateServer ( | ||
runServer ( | ||
customHeaders: server['customHeaders'], | ||
threshold: server['threshold'], | ||
contentList: Gen['contentList'], | ||
contentStorage: ServerGenerator['contentStorage'], | ||
extra: extraRoute[], | ||
@@ -146,3 +163,3 @@ dest: string, | ||
threshold, | ||
contentList, | ||
contentStorage, | ||
extra, | ||
@@ -149,0 +166,0 @@ dest, |
@@ -0,0 +0,0 @@ import Koa = require('koa') |
@@ -0,0 +0,0 @@ import Koa = require('koa') |
@@ -0,0 +0,0 @@ import Koa = require('koa') |
import Router = require('koa-router') | ||
import { contentList, extraRoute } from '../config/types' | ||
import { contentStorage, extraRoute } from '../config/types' | ||
@@ -16,3 +16,3 @@ const logger = require('../utils/logger') | ||
* | ||
* @param {contentList} contentList content storage | ||
* @param {contentStorage} contentStorage content storage | ||
* @param {extraRoute[]} extra extra static resources routes | ||
@@ -22,7 +22,7 @@ * @returns a koa router instance | ||
function createRoutes ( | ||
contentList: contentList, | ||
contentStorage: contentStorage, | ||
extra: extraRoute[], | ||
dest: string | ||
) { | ||
const docsRoutes = Object.keys(contentList) | ||
const docsRoutes = Object.keys(contentStorage) | ||
@@ -48,3 +48,3 @@ router.get('/', home) | ||
* | ||
* @param {contentList} contentList content storage | ||
* @param {contentStorage} contentStorage content storage | ||
* @param {extraRoute[]} extra extra static resources routes | ||
@@ -54,7 +54,7 @@ * @returns a koa router instance | ||
module.exports = function createRouter ( | ||
contentList: contentList, | ||
contentStorage: contentStorage, | ||
extra: extraRoute[], | ||
dest: string | ||
) { | ||
createRoutes(contentList, extra, dest) | ||
createRoutes(contentStorage, extra, dest) | ||
.get('*', createErrorHandle(404)) | ||
@@ -61,0 +61,0 @@ .all('*', createErrorHandle(405)) |
@@ -0,0 +0,0 @@ import { extraRoute } from '../config/types' |
@@ -22,3 +22,3 @@ import Koa = require('koa') | ||
threshold=1, | ||
contentList = {}, | ||
contentStorage = {}, | ||
extra = [], | ||
@@ -34,3 +34,3 @@ dest = './menu.json', | ||
this.setGzip(threshold) | ||
this.setRouter(contentList, extra, dest) | ||
this.setRouter(contentStorage, extra, dest) | ||
} | ||
@@ -60,7 +60,7 @@ | ||
setRouter ( | ||
contentList: server['contentList'], | ||
contentStorage: server['contentStorage'], | ||
extra: server['extra'], | ||
dest: server['dest'] | ||
) { | ||
const router = createRouter(contentList, extra, dest) | ||
const router = createRouter(contentStorage, extra, dest) | ||
this.use(router.routes()) | ||
@@ -67,0 +67,0 @@ this.use(router.allowedMethods()) |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
60175
40
1398
185
8
3
+ Addedgray-matter@^4.0.1
+ Addedmark-to-json@^0.2.0
+ Addedextend-shallow@2.0.1(transitive)
+ Addedgray-matter@4.0.3(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedkind-of@6.0.3(transitive)
+ Addedmark-to-json@0.2.0(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedsection-matter@1.0.0(transitive)
+ Addedstrip-bom-string@1.0.0(transitive)
- Removedfront-matter@^2.3.0
- Removedfront-matter@2.3.0(transitive)