Deal with mandatory deflate on Chuni API

This commit is contained in:
Tau
2019-11-06 17:28:29 -05:00
parent 7065388c23
commit 325043eb53
+23
View File
@@ -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)