From b5724b2ca0e12b76b3bd5993e2923483c24aea83 Mon Sep 17 00:00:00 2001 From: Tau Date: Fri, 15 Mar 2019 16:36:06 -0400 Subject: [PATCH] chunithm.ts: Clean up response compression --- src/chunithm.ts | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/src/chunithm.ts b/src/chunithm.ts index 9460fc6..8d2bb57 100644 --- a/src/chunithm.ts +++ b/src/chunithm.ts @@ -1,32 +1,16 @@ +import compression = require("compression"); import express = require("express"); -import * as zlib from "zlib"; const app = express(); -// Standard inbound JSON deserialization +// Thankfully we can use standard middleware for JSON I/O. We have to use a +// zlib middleware as well because compression is non-optional: the client will +// attempt to inflate whatever response we send to it whether there's a +// Transfer-Encoding header or not. +app.use(compression()); app.use(express.json()); -// Custom outbound JSON serialization that forces compression. Client tries to -// inflate the response whether you have a Transfer-Encoding header or not -.- - -app.use(function(req, resp, next) { - resp.json = function(obj) { - const str = JSON.stringify(obj); - const buf = Buffer.from(str); - const comp = zlib.deflateSync(buf); - - resp.set("Content-Type", "application/json"); - resp.set("Content-Length", comp.length.toString()); - resp.set("Transfer-Encoding", "deflate"); - resp.send(comp); - - return resp; - }; - - next(); -}); - // Trace requests and responses app.use(function(req, resp, next) {