mirror of
https://github.com/TiddlyWiki/TiddlyWiki5.git
synced 2026-05-05 07:46:54 +00:00
WebServer: Enable deflate and gzip compression (#3677)
* get-index: Enable deflate and gzip compression * Spaces -> Tabs * listen: Add optional `gzip=yes` parameter (defaults to "no") * get-index: Add comment explaining the usage of `zlib.*Sync` instead of async.
This commit is contained in:
committed by
Jeremy Ruston
parent
02fbae4200
commit
049244e8a8
@@ -12,14 +12,40 @@ GET /
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
var zlib = require('zlib');
|
||||
|
||||
exports.method = "GET";
|
||||
|
||||
exports.path = /^\/$/;
|
||||
|
||||
exports.handler = function(request,response,state) {
|
||||
response.writeHead(200, {"Content-Type": state.server.get("root-serve-type")});
|
||||
var acceptEncoding = request.headers['accept-encoding'];
|
||||
if (!acceptEncoding) { acceptEncoding = ''; }
|
||||
|
||||
var text = state.wiki.renderTiddler(state.server.get("root-render-type"),state.server.get("root-tiddler"));
|
||||
response.end(text,"utf8");
|
||||
|
||||
var responseHeaders = {
|
||||
"Content-Type": state.server.get("root-serve-type")
|
||||
};
|
||||
|
||||
/*
|
||||
If the gzip=yes flag for `listen` is set, check if the user agent permits
|
||||
compression. If so, compress our response. Note that we use the synchronous
|
||||
functions from zlib to stay in the imperative style. The current `Server`
|
||||
doesn't depend on this, and we may just as well use the async versions.
|
||||
*/
|
||||
if(state.server.enableGzip) {
|
||||
if (/\bdeflate\b/.test(acceptEncoding)) {
|
||||
responseHeaders['Content-Encoding'] = 'deflate';
|
||||
text = zlib.deflateSync(text);
|
||||
} else if (/\bgzip\b/.test(acceptEncoding)) {
|
||||
responseHeaders['Content-Encoding'] = 'gzip';
|
||||
text = zlib.gzipSync(text);
|
||||
}
|
||||
}
|
||||
|
||||
response.writeHead(200, responseHeaders);
|
||||
response.end(text);
|
||||
};
|
||||
|
||||
}());
|
||||
|
||||
Reference in New Issue
Block a user