Skip to content

Commit

Permalink
Added support for compression (#487)
Browse files Browse the repository at this point in the history
* Added support for compression

* Added flag for disabling compression
  • Loading branch information
leo authored Nov 12, 2018
1 parent b456009 commit 2a7f8d2
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 57 deletions.
14 changes: 13 additions & 1 deletion bin/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ const {write: copy} = require('clipboardy');
const handler = require('serve-handler');
const schema = require('@zeit/schemas/deployment/config-static');
const boxen = require('boxen');
const compression = require('compression');

// Utilities
const pkg = require('../package');

const readFile = promisify(fs.readFile);
const lookup = promisify(dns.lookup);
const compressionHandler = promisify(compression());

const warning = (message) => chalk`{yellow WARNING:} ${message}`;
const info = (message) => chalk`{magenta INFO:} ${message}`;
Expand Down Expand Up @@ -153,9 +155,17 @@ const registerShutdown = (fn) => {
};

const startEndpoint = (endpoint, config, args) => {
const server = http.createServer((request, response) => handler(request, response, config));
const {isTTY} = process.stdout;
const clipboard = args['--no-clipboard'] !== true;
const compress = args['--no-compression'] !== true;

const server = http.createServer(async (request, response) => {
if (compress) {
await compressionHandler(request, response);
}

return handler(request, response, config);
});

server.on('error', (err) => {
console.error(error(`Failed to serve: ${err.stack}`));
Expand Down Expand Up @@ -310,13 +320,15 @@ const loadConfig = async (cwd, entry, args) => {
'--debug': Boolean,
'--config': String,
'--no-clipboard': Boolean,
'--no-compression': Boolean,
'-h': '--help',
'-v': '--version',
'-l': '--listen',
'-s': '--single',
'-d': '--debug',
'-c': '--config',
'-n': '--no-clipboard',
'-u': '--no-compression',
// This is deprecated and only for backwards-compatibility.
'-p': '--listen'
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"boxen": "1.3.0",
"chalk": "2.4.1",
"clipboardy": "1.2.3",
"compression": "1.7.3",
"serve-handler": "5.0.3",
"update-check": "1.5.2"
}
Expand Down
Loading

0 comments on commit 2a7f8d2

Please sign in to comment.