Skip to content

Commit

Permalink
fix: don't fail on missing express
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 10, 2024
1 parent d1cf74a commit 52b60ad
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 180 deletions.
5 changes: 2 additions & 3 deletions scripts/cannonPost.bench.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@ yarn tsn cannonPost.bench

import http from 'node:http'
import { runScript } from '@naturalcycles/nodejs-lib'
import express = require('express')
import { runCannon } from '../src'

runScript(async () => {
await runCannon(
{
'01-post': async () => {
const app = express()
const app = require('express')()
app.disable('etag')
app.disable('x-powered-by')
app.post('/hello', (req, res) => res.json({ ok: true }))
app.post('/hello', (req: any, res: any) => res.json({ ok: true }))
return http.createServer(app)
},
},
Expand Down
19 changes: 9 additions & 10 deletions src/cannon.profiles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import http from 'node:http'
import express from 'express'
import helmet from 'helmet'
import { HttpServerFactory } from './cannon.model'

export const bareNodeServerFactory: HttpServerFactory = async () => {
Expand All @@ -11,18 +9,19 @@ export const bareNodeServerFactory: HttpServerFactory = async () => {
}

export const bareExpressServerFactory: HttpServerFactory = async () => {
const app = express()
const app = require('express')()
app.disable('etag')
app.disable('x-powered-by')
app.get('/', (req, res) => res.json({ hello: 'world' }))
app.get('/', (req: any, res: any) => res.json({ hello: 'world' }))
return http.createServer(app)
}

/**
* Based on: https://github.com/fastify/benchmarks/blob/master/benchmarks/express-with-middlewares.js
*/
export const expressWithMiddlewaresServerFactory: HttpServerFactory = async () => {
const app = express()
const app = require('express')()
const helmet = require('helmet')
app.disable('etag')
app.disable('x-powered-by')

Expand All @@ -34,7 +33,7 @@ export const expressWithMiddlewaresServerFactory: HttpServerFactory = async () =
app.use(helmet.ieNoOpen())
app.use(helmet.xssFilter())

app.get('/', (req, res) => res.json({ hello: 'world' }))
app.get('/', (req: any, res: any) => res.json({ hello: 'world' }))

return http.createServer(app)
}
Expand All @@ -44,20 +43,20 @@ export const expressWithMiddlewaresServerFactory: HttpServerFactory = async () =
*/
export function expressFunctionFactory(fn: () => any): HttpServerFactory {
return async () => {
const app = express()
const app = require('express')()
app.disable('etag')
app.disable('x-powered-by')
app.get('/', async (req, res) => res.json(await fn()))
app.get('/', async (req: any, res: any) => res.json(await fn()))
return http.createServer(app)
}
}

export function expressSyncFunctionFactory(fn: () => any): HttpServerFactory {
return async () => {
const app = express()
const app = require('express')()
app.disable('etag')
app.disable('x-powered-by')
app.get('/', (req, res) => res.json(fn()))
app.get('/', (req: any, res: any) => res.json(fn()))
return http.createServer(app)
}
}
Loading

0 comments on commit 52b60ad

Please sign in to comment.