Skip to content

Instantly share code, notes, and snippets.

@chimmelb
Created January 31, 2023 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chimmelb/1a2ece4aa77b6b029eedcd5f3b26a4ae to your computer and use it in GitHub Desktop.
Save chimmelb/1a2ece4aa77b6b029eedcd5f3b26a4ae to your computer and use it in GitHub Desktop.
'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