Created
January 31, 2023 18:45
-
-
Save chimmelb/1a2ece4aa77b6b029eedcd5f3b26a4ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
import { Initializer, api, action, log } from 'actionhero' | |
let compression = require('compression') | |
export class CompressMiddleware extends Initializer { | |
constructor() { | |
super() | |
this.name = 'CompressMiddleware' | |
this.loadPriority = 1000 | |
this.startPriority = 1000 | |
this.stopPriority = 1000 | |
} | |
async initialize() { | |
let compress = compression({}) | |
let compressMiddleware: action.ActionMiddleware = { | |
global: false, | |
name: 'compress', | |
priority: 1010, | |
preProcessor: async function (data) { | |
if (data.connection.type === 'web') { | |
data.toRender = false | |
data.connection.rawConnection.res.setHeader('Content-Type', 'application/json') | |
compress(data.connection.rawConnection.req, data.connection.rawConnection.res, () => {}) | |
} | |
}, | |
postProcessor: async function (data) { | |
if (data.connection.type === 'web') { | |
data.connection.rawConnection.res.write(JSON.stringify(data.response)) | |
data.connection.rawConnection.res.end() | |
} | |
}, | |
} | |
action.addMiddleware(compressMiddleware) | |
} | |
async start() {} | |
async stop() {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment