From 1fb873294accc1688496888ed5ae3c7e7d5ae2d6 Mon Sep 17 00:00:00 2001 From: vivaxy Date: Fri, 12 Oct 2018 20:41:24 +0800 Subject: [PATCH] feat(gzip): :sparkles:Support gzip compression --- lib/application.js | 71 ++++++++++++++++++++++++---------------------- lib/server.js | 4 +++ middleware/gzip.js | 4 +-- package.json | 1 + yarn.lock | 29 +++++++++++++++---- 5 files changed, 68 insertions(+), 41 deletions(-) diff --git a/lib/application.js b/lib/application.js index aff0f74..8a8c674 100644 --- a/lib/application.js +++ b/lib/application.js @@ -12,7 +12,7 @@ const config = require('./config.js'); const startServer = require('./server.js'); const startWatcher = require('./watcher.js'); const packageJson = require('../package.json'); -const configKey = require('../constant/config.js'); +const configKeys = require('../constant/config.js'); const formatWatch = (value) => { if (isNaN(Number(value))) { @@ -32,83 +32,86 @@ const formatDirectory = (value) => { }; const prepare = () => { - if (config.get(configKey.IS_DEBUG)) { - config.set(configKey.LOG_LEVEL, 0); + if (config.get(configKeys.IS_DEBUG)) { + config.set(configKeys.LOG_LEVEL, 0); } - config.set(configKey.DIRECTORY, formatDirectory('.')); - config.set(configKey.SERVE_HERE_VERSION, packageJson.version); + config.set(configKeys.DIRECTORY, formatDirectory('.')); + config.set(configKeys.SERVE_HERE_VERSION, packageJson.version); commander - .version(config.get(configKey.SERVE_HERE_VERSION), '-v, --version') + .version(config.get(configKeys.SERVE_HERE_VERSION), '-v, --version') .option( - `-p, --${configKey.PORT} [port]`, + `-p, --${configKeys.PORT} [port]`, 'specify port', - config.get(configKey.PORT), + config.get(configKeys.PORT), ) .option( - `-S, --${configKey.SSL}`, + `-S, --${configKeys.SSL}`, 'switch protocol to https', ) .option( - `-G, --${configKey.GZIP}`, - 'switch protocol to https', + `-G, --${configKeys.GZIP}`, + 'open gzip', ) .option( - `-d, --${configKey.DIRECTORY} [directory]`, + `-d, --${configKeys.DIRECTORY} [directory]`, 'specify root directory', formatDirectory, - config.get(configKey.DIRECTORY), + config.get(configKeys.DIRECTORY), ) .option( - `-w, --${configKey.WATCH} [interval]`, + `-w, --${configKeys.WATCH} [interval]`, 'will watch files; once changed, reload pages', formatWatch, ) .option( - `-s, --${configKey.SILENT}`, + `-s, --${configKeys.SILENT}`, 'will not open browser', ) .option( - `-l, --${configKey.LOG_LEVEL} [level]`, + `-l, --${configKeys.LOG_LEVEL} [level]`, 'output log in some level, lower means more details', - config.get(configKey.LOG_LEVEL), + config.get(configKeys.LOG_LEVEL), ) .parse(process.argv); - log.setLevel(Number(commander[configKey.LOG_LEVEL])); + log.setLevel(Number(commander[configKeys.LOG_LEVEL])); - if (commander[configKey.PORT]) { - config.set(configKey.PORT, commander[configKey.PORT]); + if (commander[configKeys.PORT]) { + config.set(configKeys.PORT, commander[configKeys.PORT]); } - if (commander[configKey.SSL]) { - config.set(configKey.SSL, commander[configKey.SSL]); + if (commander[configKeys.DIRECTORY]) { + config.set(configKeys.DIRECTORY, commander[configKeys.DIRECTORY]); } - if (commander[configKey.DIRECTORY]) { - config.set(configKey.DIRECTORY, commander[configKey.DIRECTORY]); + if (commander[configKeys.SSL]) { + config.set(configKeys.SSL, commander[configKeys.SSL]); + } + if (commander[configKeys.GZIP]) { + config.set(configKeys.GZIP, commander[configKeys.GZIP]); } - if (commander[configKey.WATCH] !== undefined) { - if (commander[configKey.WATCH] === true) { - config.set(configKey.WATCH, 0); + if (commander[configKeys.WATCH] !== undefined) { + if (commander[configKeys.WATCH] === true) { + config.set(configKeys.WATCH, 0); } else { - config.set(configKey.WATCH, commander[configKey.WATCH]); + config.set(configKeys.WATCH, commander[configKeys.WATCH]); } } - if (commander[configKey.SILENT]) { - config.set(configKey.SILENT, commander[configKey.SILENT]); + if (commander[configKeys.SILENT]) { + config.set(configKeys.SILENT, commander[configKeys.SILENT]); } - if (commander[configKey.LOG_LEVEL]) { - config.set(configKey.LOG_LEVEL, commander[configKey.LOG_LEVEL]); + if (commander[configKeys.LOG_LEVEL]) { + config.set(configKeys.LOG_LEVEL, commander[configKeys.LOG_LEVEL]); } }; module.exports = () => { prepare(); - if (!config.get(configKey.IS_DEBUG)) { + if (!config.get(configKeys.IS_DEBUG)) { process.on('uncaughtException', (e) => { // throw this to preserve default behaviour // console this instead of throw error to keep the original error trace @@ -118,7 +121,7 @@ module.exports = () => { } startServer((nativeServer) => { - if (config.get(configKey.WATCH) !== false) { + if (config.get(configKeys.WATCH) !== false) { startWatcher(nativeServer); } }); diff --git a/lib/server.js b/lib/server.js index 6abdf1b..af919e4 100644 --- a/lib/server.js +++ b/lib/server.js @@ -73,6 +73,7 @@ module.exports = (callback) => { // - log request // - load route // - exec route + // - gzip // - live reload // - read file // - handle error @@ -94,6 +95,9 @@ module.exports = (callback) => { }); function start() { + if (config.get(configKeys.GZIP)) { + middlewareList.push(require('../middleware/gzip.js')); + } if (watch !== false) { middlewareList.push(require('../middleware/live-reload.js')); } diff --git a/middleware/gzip.js b/middleware/gzip.js index ef5954d..65351d5 100644 --- a/middleware/gzip.js +++ b/middleware/gzip.js @@ -3,6 +3,6 @@ * @author vivaxy */ -module.exports = async(ctx, next) => { +const compress = require('koa-compress'); -}; +module.exports = compress(); diff --git a/package.json b/package.json index 4f77308..bffed49 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "debounce": "^1.0.0", "ip": "^1.1.0", "koa": "^2.5.3", + "koa-compress": "^3.0.0", "koa-router": "^7.4.0", "log-util": "^1.1.1", "mime": "^1.3.4", diff --git a/yarn.lock b/yarn.lock index 11a546a..9259a4f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -312,6 +312,10 @@ builtin-modules@^1.0.0: version "1.1.1" resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" +bytes@^3.0.0: + version "3.0.0" + resolved "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" + cache-base@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2" @@ -533,6 +537,12 @@ component-inherit@0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/component-inherit/-/component-inherit-0.0.3.tgz#645fc4adf58b72b649d5cae65135619db26ff143" +compressible@^2.0.0: + version "2.0.15" + resolved "https://registry.npmjs.org/compressible/-/compressible-2.0.15.tgz#857a9ab0a7e5a07d8d837ed43fe2defff64fe212" + dependencies: + mime-db ">= 1.36.0 < 2" + concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" @@ -1849,6 +1859,15 @@ koa-compose@^4.1.0: version "4.1.0" resolved "https://registry.npmjs.org/koa-compose/-/koa-compose-4.1.0.tgz#507306b9371901db41121c812e923d0d67d3e877" +koa-compress@^3.0.0: + version "3.0.0" + resolved "https://registry.npmjs.org/koa-compress/-/koa-compress-3.0.0.tgz#3194059c215cbc24e59bbc84c2c7453a4c88564f" + dependencies: + bytes "^3.0.0" + compressible "^2.0.0" + koa-is-json "^1.0.0" + statuses "^1.0.0" + koa-convert@^1.2.0: version "1.2.0" resolved "https://registry.npmjs.org/koa-convert/-/koa-convert-1.2.0.tgz#da40875df49de0539098d1700b50820cebcd21d0" @@ -2144,14 +2163,14 @@ micromatch@^3.1.8: snapdragon "^0.8.1" to-regex "^3.0.2" +"mime-db@>= 1.36.0 < 2", mime-db@~1.36.0: + version "1.36.0" + resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.36.0.tgz#5020478db3c7fe93aad7bbcc4dcf869c43363397" + mime-db@~1.27.0: version "1.27.0" resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.27.0.tgz#820f572296bbd20ec25ed55e5b5de869e5436eb1" -mime-db@~1.36.0: - version "1.36.0" - resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.36.0.tgz#5020478db3c7fe93aad7bbcc4dcf869c43363397" - mime-types@^2.1.12, mime-types@~2.1.11, mime-types@~2.1.7: version "2.1.15" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.15.tgz#a4ebf5064094569237b8cf70046776d09fc92aed" @@ -3155,7 +3174,7 @@ static-extend@^0.1.1: define-property "^0.2.5" object-copy "^0.1.0" -"statuses@>= 1.5.0 < 2", statuses@^1.5.0: +"statuses@>= 1.5.0 < 2", statuses@^1.0.0, statuses@^1.5.0: version "1.5.0" resolved "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"