Skip to content

Commit

Permalink
Merge branch 'main' of github.com:coratgerl/wobe
Browse files Browse the repository at this point in the history
  • Loading branch information
coratgerl committed Oct 4, 2024
2 parents 0e25430 + f7e58d0 commit 12ed9ed
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
- uses: oven-sh/setup-bun@v2
with:
bun-version: latest
- run: bun install
Expand Down
2 changes: 1 addition & 1 deletion packages/wobe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"get-port": "7.0.0"
},
"scripts": {
"build": "bun build --minify --outdir dist $(pwd)/src/index.ts --target=bun && bun generate:types",
"build": "bun build --outdir dist $(pwd)/src/index.ts --target=bun && bun generate:types",
"generate:types": "bun tsc --project .",
"lint": "biome lint . --no-errors-on-unmatched --config-path=../../",
"ci": "bun lint $(pwd) && bun run test:bun src && bun test:node src",
Expand Down
15 changes: 0 additions & 15 deletions packages/wobe/src/Wobe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,6 @@ describe('Wobe', () => {
expect(res.status).toBe(200)
})

it('should only create the context once (with the cache)', async () => {
const spyStoreSet = spyOn(WobeStore.prototype, 'set')
const spyStoreGet = spyOn(WobeStore.prototype, 'get')

await fetch(`http://127.0.0.1:${port}/testContextCache`)

expect(spyStoreSet).toHaveBeenCalledTimes(1)
expect(spyStoreGet).toHaveBeenCalledTimes(1)

await fetch(`http://127.0.0.1:${port}/testContextCache`)

expect(spyStoreSet).toHaveBeenCalledTimes(1)
expect(spyStoreGet).toHaveBeenCalledTimes(2)
})

it('should have the correct state if there is afterHandler middleware (with context cache)', async () => {
const res = await fetch(
`http://127.0.0.1:${port}/testAfterHandlerCache`,
Expand Down
17 changes: 1 addition & 16 deletions packages/wobe/src/adapters/bun/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ import type { RuntimeAdapter } from '..'
import { Context } from '../../Context'
import { HttpException } from '../../HttpException'
import type { WobeOptions, WobeWebSocket } from '../../Wobe'
import { WobeResponse } from '../../WobeResponse'
import type { RadixTree } from '../../router'
import { WobeStore } from '../../tools'
import { bunWebSocket } from './websocket'

const _contextStore = new WobeStore<Context>({ interval: 10000 })

export const BunAdapter = (): RuntimeAdapter => ({
createServer: (
port: number,
Expand All @@ -26,18 +22,7 @@ export const BunAdapter = (): RuntimeAdapter => ({
websocket: bunWebSocket(webSocket),
async fetch(req, server) {
try {
const cacheKey = req.url + '$method:' + req.method

let context = _contextStore.get(cacheKey)

if (context) {
context.request = req
context.res = new WobeResponse(req)
} else {
context = new Context(req, router)

_contextStore.set(cacheKey, context)
}
const context = new Context(req, router)

context.getIpAdress = () =>
this.requestIP(req)?.address || ''
Expand Down
17 changes: 1 addition & 16 deletions packages/wobe/src/adapters/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { createServer as createHttpServer } from 'node:http'
import { createServer as createHttpsServer } from 'node:https'
import { HttpException } from '../../HttpException'
import { Context } from '../../Context'
import { WobeStore } from '../../tools'
import type { RuntimeAdapter } from '..'
import type { RadixTree } from '../../router'
import type { WobeOptions } from '../../Wobe'
import { WobeResponse } from '../../WobeResponse'

const transformResponseInstanceToValidResponse = async (response: Response) => {
const headers: Record<string, string> = {}
Expand All @@ -21,8 +19,6 @@ const transformResponseInstanceToValidResponse = async (response: Response) => {
return { headers, body: await response.text() }
}

const _contextStore = new WobeStore<Context>({ interval: 10000 })

export const NodeAdapter = (): RuntimeAdapter => ({
createServer: (port: number, router: RadixTree, options?: WobeOptions) => {
// @ts-expect-error
Expand All @@ -41,10 +37,6 @@ export const NodeAdapter = (): RuntimeAdapter => ({

req.on('end', async () => {
try {
const cacheKey = url + '$method:' + req.method

let context = _contextStore.get(cacheKey)

const request = new Request(url, {
method: req.method,
headers: req.headers as any,
Expand All @@ -54,14 +46,7 @@ export const NodeAdapter = (): RuntimeAdapter => ({
: undefined,
})

if (!context) {
context = new Context(request, router)

_contextStore.set(cacheKey, context)
} else {
context.request = request
context.res = new WobeResponse(request)
}
const context = new Context(request, router)

if (!context.handler) {
options?.onNotFound?.(context.request)
Expand Down

0 comments on commit 12ed9ed

Please sign in to comment.