Skip to content

Commit

Permalink
feat(gzip): ✨Support gzip compression
Browse files Browse the repository at this point in the history
  • Loading branch information
vivaxy committed Oct 12, 2018
1 parent 1b3e054 commit 1fb8732
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 41 deletions.
71 changes: 37 additions & 34 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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
Expand All @@ -118,7 +121,7 @@ module.exports = () => {
}

startServer((nativeServer) => {
if (config.get(configKey.WATCH) !== false) {
if (config.get(configKeys.WATCH) !== false) {
startWatcher(nativeServer);
}
});
Expand Down
4 changes: 4 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = (callback) => {
// - log request
// - load route
// - exec route
// - gzip
// - live reload
// - read file
// - handle error
Expand All @@ -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'));
}
Expand Down
4 changes: 2 additions & 2 deletions middleware/gzip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
* @author vivaxy
*/

module.exports = async(ctx, next) => {
const compress = require('koa-compress');

};
module.exports = compress();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 24 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 1fb8732

Please sign in to comment.