Skip to content

Commit 6f51bfd

Browse files
committed
Adicionando configurações de GZIP ao http
1 parent e1b5e94 commit 6f51bfd

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ let server = require("http")
1111
server.createServer(8778, null /*router*/, {
1212
"staticFilesPath": /*String (Default: /static)*/,
1313
"maxPostSize": /*Inteiro (Default: 2MB)*/,
14+
"compression": /*Boolean (Default: true)*/,
15+
"compressionMinSize": /*Inteiro em bytes (Default: 1024)*/,
16+
"compressableMimeType": /*Inteiro (Default: "text/html,text/xml,text/css,application/json,application/javascript")*/,
1417
})
1518
```
1619
O servidor HTTP irá subir na porta 8778.
@@ -41,6 +44,9 @@ As propriedades abaixo devem ser configuradas no arquivo *config.json* (distribu
4144
"http": { /*Configuração do http*/
4245
"staticFilesPath": /*String (Default: static)*/,
4346
"maxPostSize": /*Inteiro (Default: 2MB)*/,
47+
"compression": /*Boolean (Default: true)*/,
48+
"compressionMinSize": /*Inteiro em bytes (Default: 1024)*/,
49+
"compressableMimeType": /*Inteiro (Default: "text/html,text/xml,text/css,application/json,application/javascript")*/,
4450
}
4551
}
4652

@@ -51,6 +57,9 @@ Para mais informações sobre o módulo de roteamento, acesse [thrust-bitcodes/r
5157

5258
## What's new
5359

60+
v0.2.2 - Melhoria: Adicionando opções referentes a compressão GZIP das requisições:
61+
* Agora, por padrão o http irá realizar a compressão GZIP dos dados retornados, também é possível alterar os padrões, via options ou config.json, vide parâmetros acima.
62+
5463
v0.2.1 - Melhoria: Adicionando opção maxPostSize para o servidor
5564
* Por padrão o servidor tem um limite de tamanho do post de 2MB, foi criada a opção maxPostSize para que seja possível alterar este valor padrão
5665

brief.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"description": "HTTP server module",
33
"name": "http",
44
"path": "index.js",
5-
"version": "0.2.1",
5+
"version": "0.2.2",
66
"author": "Nery Jr",
77
"contributors": [
88
"Cleverson Puche",

index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@ var router
3535
Caso o router não seja passado, o server criará um default internamente.
3636
* @param {Number} port - porta em que o servidor será levantado
3737
* @param {thrust-bitcodes/router} [httpRouter=undefined] -router customizado com rotas de serviço
38+
* @param {Object} options - Objeto que pode ser passado sobreescrevendo configurações padrão do http
3839
*/
3940
function createServer(port, httpRouter, options) {
40-
var opts = options || {}
41-
var config = getBitcodeConfig('http')
42-
var staticFilesPath = opts.staticFilesPath || config('staticFilesPath') || '/static'
41+
var config = getBitcodeConfig('http');
42+
43+
var compression = config('compression');
44+
45+
var opts = Object.assign({
46+
staticFilesPath: config('staticFilesPath') || '/static',
47+
compression: typeof compression != undefined ? config('compression') : true,
48+
compressionMinSize: config('compressionMinSize') || 1024,
49+
compressableMimeType: config('compressableMimeType') || "text/html,text/xml,text/css,application/json,application/javascript"
50+
}, options);
4351

4452
var tomcat = new Tomcat()
4553
var ctx = tomcat.addContext("/", new File(rootPath).getAbsolutePath())
@@ -55,7 +63,7 @@ function createServer(port, httpRouter, options) {
5563
ctx.setAllowCasualMultipartParsing(true)
5664
ctx.addServletMappingDecoded("/*", "thrust")
5765

58-
staticFilesPath = '/' + staticFilesPath.replace(/^\/|\/\*$|\//g, '') + '/*'
66+
var staticFilesPath = '/' + opts.staticFilesPath.replace(/^\/|\/\*$|\//g, '') + '/*'
5967

6068
Tomcat.addServlet(ctx, "static", org.apache.catalina.servlets.DefaultServlet.class.getCanonicalName());
6169
ctx.addServletMappingDecoded(staticFilesPath, "static");
@@ -67,12 +75,14 @@ function createServer(port, httpRouter, options) {
6775

6876
var connector = tomcat.getConnector();
6977

70-
var maxPostSize = opts.maxPostSize || config('maxPostSize');
71-
72-
if (maxPostSize) {
73-
connector.setMaxPostSize(maxPostSize);
78+
if (opts.maxPostSize) {
79+
connector.setMaxPostSize(opts.maxPostSize);
7480
}
7581

82+
connector.setProperty("compression", opts.compression ? "on" : "off");
83+
connector.setProperty("compressionMinSize", String(opts.compressionMinSize));
84+
connector.setProperty("compressableMimeType", opts.compressableMimeType);
85+
7686
tomcat.start()
7787
print("Running on port " + port + "...")
7888
tomcat.getServer().await()

0 commit comments

Comments
 (0)