mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-11-27 03:57:21 +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:
parent
02fbae4200
commit
049244e8a8
@ -28,6 +28,7 @@ All parameters are optional with safe defaults, and can be specified in any orde
|
||||
* ''tls-cert'' - pathname of TLS certificate file (relative to wiki folder)
|
||||
* ''tls-key'' - pathname of TLS key file (relative to wiki folder)
|
||||
* ''debug-level'' - optional debug level; set to "debug" to view request details (defaults to "none")
|
||||
* ''gzip'' - set to "yes" to enable gzip compression for some http endpoints (defaults to "no")
|
||||
|
||||
For information on opening up your instance to the entire local network, and possible security concerns, see the WebServer tiddler at TiddlyWiki.com.
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
}());
|
||||
|
@ -43,6 +43,8 @@ function Server(options) {
|
||||
$tw.utils.extend({},this.defaultVariables,options.variables);
|
||||
// Initialise CSRF
|
||||
this.csrfDisable = this.get("csrf-disable") === "yes";
|
||||
// Initialize Gzip compression
|
||||
this.enableGzip = this.get("gzip") === "yes";
|
||||
// Initialise authorization
|
||||
var authorizedUserName = (this.get("username") && this.get("password")) ? this.get("username") : "(anon)";
|
||||
this.authorizationPrincipals = {
|
||||
@ -84,7 +86,8 @@ Server.prototype.defaultVariables = {
|
||||
"tiddler-render-template": "$:/core/templates/server/static.tiddler.html",
|
||||
"system-tiddler-render-type": "text/plain",
|
||||
"system-tiddler-render-template": "$:/core/templates/wikified-tiddler",
|
||||
"debug-level": "none"
|
||||
"debug-level": "none",
|
||||
"gzip": "no"
|
||||
};
|
||||
|
||||
Server.prototype.get = function(name) {
|
||||
|
Loading…
Reference in New Issue
Block a user