feat(medusa,utils): add server level configurable http compression (#3785)
* feat(medusa,utils): add server level configurable http compression * chore: remove unwanted middleware * chore: add a log for running compression * chore: change package position * chore: reposition options * chore: change equality
This commit is contained in:
6
.changeset/loud-wombats-smoke.md
Normal file
6
.changeset/loud-wombats-smoke.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
"@medusajs/types": patch
|
||||
---
|
||||
|
||||
feat(medusa,utils): add server level configurable http compression
|
||||
@@ -5,6 +5,7 @@ const DB_NAME = process.env.DB_TEMP_NAME
|
||||
|
||||
const redisUrl = process.env.REDIS_URL || "redis://localhost:6379"
|
||||
const cacheTTL = process.env.CACHE_TTL || 15
|
||||
const enableResponseCompression = process.env.ENABLE_RESPONSE_COMPRESSION || true
|
||||
|
||||
module.exports = {
|
||||
plugins: [],
|
||||
@@ -14,6 +15,9 @@ module.exports = {
|
||||
database_type: "postgres",
|
||||
jwt_secret: "test",
|
||||
cookie_secret: "test",
|
||||
http_compression: {
|
||||
enabled: enableResponseCompression
|
||||
}
|
||||
},
|
||||
modules: {
|
||||
cacheService: {
|
||||
|
||||
@@ -55,6 +55,7 @@
|
||||
"chokidar": "^3.4.2",
|
||||
"class-transformer": "^0.5.1",
|
||||
"class-validator": "^0.13.2",
|
||||
"compression": "^1.7.4",
|
||||
"connect-redis": "^5.0.0",
|
||||
"cookie-parser": "^1.4.6",
|
||||
"core-js": "^3.6.5",
|
||||
|
||||
@@ -1,11 +1,23 @@
|
||||
import { Router } from "express"
|
||||
import errorHandler from "./middlewares/error-handler"
|
||||
import compression from "compression"
|
||||
import admin from "./routes/admin"
|
||||
import store from "./routes/store"
|
||||
import { shouldCompressResponse, compressionOptions } from "../utils/api"
|
||||
|
||||
// guaranteed to get dependencies
|
||||
export default (container, config) => {
|
||||
const app = Router()
|
||||
const httpCompressionOptions = compressionOptions(config)
|
||||
|
||||
if (httpCompressionOptions.enabled) {
|
||||
app.use(
|
||||
compression({
|
||||
filter: shouldCompressResponse,
|
||||
...httpCompressionOptions,
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
admin(app, container, config)
|
||||
store(app, container, config)
|
||||
|
||||
39
packages/medusa/src/utils/api/http-compression.ts
Normal file
39
packages/medusa/src/utils/api/http-compression.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { Request, Response, NextFunction } from "express"
|
||||
import compression from "compression"
|
||||
import { Logger } from "@medusajs/types"
|
||||
import {
|
||||
ProjectConfigOptions,
|
||||
HttpCompressionOptions,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export function shouldCompressResponse(req: Request, res: Response) {
|
||||
const logger: Logger = req.scope.resolve("logger")
|
||||
const { projectConfig } = req.scope.resolve("configModule")
|
||||
const { enabled } = compressionOptions(projectConfig)
|
||||
|
||||
if (!enabled) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (req.headers["x-no-compression"]) {
|
||||
// don't compress responses with this request header
|
||||
return false
|
||||
}
|
||||
|
||||
// fallback to standard filter function
|
||||
return compression.filter(req, res)
|
||||
}
|
||||
|
||||
export function compressionOptions(
|
||||
config: ProjectConfigOptions
|
||||
): HttpCompressionOptions {
|
||||
const responseCompressionOptions = config.http_compression ?? {}
|
||||
|
||||
responseCompressionOptions.enabled = responseCompressionOptions.enabled ?? false
|
||||
responseCompressionOptions.level = responseCompressionOptions.level ?? 6
|
||||
responseCompressionOptions.memLevel = responseCompressionOptions.memLevel ?? 8
|
||||
responseCompressionOptions.threshold =
|
||||
responseCompressionOptions.threshold ?? 1024
|
||||
|
||||
return responseCompressionOptions
|
||||
}
|
||||
1
packages/medusa/src/utils/api/index.ts
Normal file
1
packages/medusa/src/utils/api/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./http-compression"
|
||||
@@ -13,27 +13,38 @@ type SessionOptions = {
|
||||
ttl?: number
|
||||
}
|
||||
|
||||
export type ConfigModule = {
|
||||
projectConfig: {
|
||||
redis_url?: string
|
||||
export type HttpCompressionOptions = {
|
||||
enabled?: boolean
|
||||
level?: number
|
||||
memLevel?: number
|
||||
threshold?: number | string
|
||||
}
|
||||
|
||||
session_options?: SessionOptions
|
||||
export type ProjectConfigOptions = {
|
||||
redis_url?: string
|
||||
|
||||
jwt_secret?: string
|
||||
cookie_secret?: string
|
||||
session_options?: SessionOptions
|
||||
|
||||
database_url?: string
|
||||
database_type: string
|
||||
database_database?: string
|
||||
database_schema?: string
|
||||
database_logging: LoggerOptions
|
||||
jwt_secret?: string
|
||||
cookie_secret?: string
|
||||
|
||||
database_extra?: Record<string, unknown> & {
|
||||
ssl: { rejectUnauthorized: false }
|
||||
}
|
||||
store_cors?: string
|
||||
admin_cors?: string
|
||||
database_url?: string
|
||||
database_type: string
|
||||
database_database?: string
|
||||
database_schema?: string
|
||||
database_logging: LoggerOptions
|
||||
|
||||
http_compression?: HttpCompressionOptions
|
||||
|
||||
database_extra?: Record<string, unknown> & {
|
||||
ssl: { rejectUnauthorized: false }
|
||||
}
|
||||
store_cors?: string
|
||||
admin_cors?: string
|
||||
}
|
||||
|
||||
export type ConfigModule = {
|
||||
projectConfig: ProjectConfigOptions
|
||||
featureFlags: Record<string, boolean | string>
|
||||
modules?: Record<
|
||||
string,
|
||||
|
||||
Reference in New Issue
Block a user