Skip to content

Commit

Permalink
🎉 feat: exp adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
SaltyAom committed Oct 15, 2024
1 parent d99ed73 commit 9b5cfc9
Show file tree
Hide file tree
Showing 19 changed files with 1,526 additions and 133 deletions.
24 changes: 15 additions & 9 deletions example/a.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { Elysia } from '../src'
import { ElysiaNodeContext, NodeAdapter } from '../src/adapter/node'
import { WebStandardAdapter } from '../src/adapter/web-standard'
import { NodeAdapter } from '../src/adapter/node'

const app = new Elysia({ adapter: WebStandardAdapter })
new Elysia({ adapter: NodeAdapter })
.get('/', ({ cookie: { a, b } }) => {
a.value = 'hi'
b.value = 'hi'

return 'hi'
})
.get('/stream', async function* () {
for (let i = 0; i < 10; i++) {
await new Promise((resolve) => setTimeout(resolve, 100))
yield i
}
})
.post('/', async ({ body, headers }) => {
return {
body,
headers,
env: typeof Bun !== 'undefined' ? 'bun' : 'likely Node'
}
})
// .listen(3000)

Bun.serve({
port: 3000,
fetch: app.fetch
})
.listen(3000)
40 changes: 35 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@
"import": "./dist/error.mjs",
"require": "./dist/cjs/error.js"
},
"./handler": {
"types": "./dist/handler.d.ts",
"import": "./dist/handler.mjs",
"require": "./dist/cjs/handler.js"
},
"./sucrose": {
"types": "./dist/sucrose.d.ts",
"import": "./dist/sucrose.mjs",
Expand Down Expand Up @@ -77,6 +72,41 @@
"types": "./dist/fast-querystring.d.ts",
"import": "./dist/fast-querystring.mjs",
"require": "./dist/cjs/fast-querystring.js"
},
"./adapter": {
"types": "./dist/adapter/index.d.ts",
"import": "./dist/adapter/index.mjs",
"require": "./dist/cjs/adapter/index.js"
},
"./adapter/bun": {
"types": "./dist/adapter/bun/index.d.ts",
"import": "./dist/adapter/bun/index.mjs",
"require": "./dist/cjs/adapter/bun/index.js"
},
"./adapter/bun/handler": {
"types": "./dist/adapter/bun/handler.d.ts",
"import": "./dist/adapter/bun/handler.mjs",
"require": "./dist/cjs/adapter/bun/handler.js"
},
"./adapter/web-standard": {
"types": "./dist/adapter/web-standard/index.d.ts",
"import": "./dist/adapter/web-standard/index.mjs",
"require": "./dist/cjs/adapter/web-standard/index.js"
},
"./adapter/web-standard/handler": {
"types": "./dist/adapter/web-standard/handler.d.ts",
"import": "./dist/adapter/web-standard/handler.mjs",
"require": "./dist/cjs/adapter/web-standard/handler.js"
},
"./adapter/node": {
"types": "./dist/adapter/node/index.d.ts",
"import": "./dist/adapter/node/index.mjs",
"require": "./dist/cjs/adapter/node/index.js"
},
"./adapter/node/handler": {
"types": "./dist/adapter/node/handler.d.ts",
"import": "./dist/adapter/node/handler.mjs",
"require": "./dist/cjs/adapter/node/handler.js"
}
},
"repository": {
Expand Down
40 changes: 40 additions & 0 deletions src/adapter/bun/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Context } from '../../context'
import type { LocalHook } from '../../types'

import {
mapResponse,
mapEarlyResponse,
mapCompactResponse,
createStaticHandler
} from '../web-standard/handler'

export const createNativeStaticHandler = (
handle: unknown,
hooks: LocalHook<any, any, any, any, any, any, any>,
setHeaders: Context['set']['headers'] = {}
): (() => Response) | undefined => {
if (typeof handle === 'function' || handle instanceof Blob) return

const response = mapResponse(handle, {
headers: setHeaders
})

if (
hooks.parse.length === 0 &&
hooks.transform.length === 0 &&
hooks.beforeHandle.length === 0 &&
hooks.afterHandle.length === 0
) {
if (!response.headers.has('content-type'))
response.headers.append('content-type', 'text/plain;charset=utf-8')

return response.clone.bind(response)
}
}

export {
mapResponse,
mapEarlyResponse,
mapCompactResponse,
createStaticHandler
}
13 changes: 8 additions & 5 deletions src/adapter/bun/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import type { Serve } from 'bun'

import { createNativeStaticHandler } from './handler'

import { isProduction } from '../../error'
import { getLoosePath, isNumericString } from '../../utils'
import { hasHeaderShorthand, isNumericString } from '../../utils'
import { websocket } from '../../ws'

import type { ElysiaAdapter } from '../types'

import { WebStandardAdapter } from '../web-standard'

const headersHasToJSON = (new Headers() as Headers).toJSON

export const BunAdapter: ElysiaAdapter = {
...WebStandardAdapter,
handler: {
...WebStandardAdapter.handler,
createNativeStaticHandler
},
composeHandler: {
...WebStandardAdapter.composeHandler,
// @ts-ignore Bun specific
headers: headersHasToJSON
headers: hasHeaderShorthand
? 'c.headers = c.request.headers.toJSON()\n'
: 'c.headers = {}\n' +
'for (const [key, value] of c.request.headers.entries())' +
Expand Down
Loading

0 comments on commit 9b5cfc9

Please sign in to comment.