From 325043eb53e0abd4965954b2842618e398cb9838 Mon Sep 17 00:00:00 2001 From: Tau Date: Mon, 26 Nov 2018 14:21:03 -0500 Subject: [PATCH] Deal with mandatory deflate on Chuni API --- src/title.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/title.js b/src/title.js index c44c366..1c8ef57 100644 --- a/src/title.js +++ b/src/title.js @@ -1,9 +1,32 @@ const express = require('express') +const zlib = require('zlib') const app = express() +// Standard inbound JSON deserialization + 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) + resp.set('Transfer-Encoding', 'deflate') + resp.send(comp) + } + + next() +}) + +// Trace requests and responses + app.use(function (req, resp, next) { console.log('\n--- Chunithm %s ---\n', req.url) console.log('Request:', req.body)