Skip to content

Add stop function to ElysiaAdapter type #1089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 58 additions & 43 deletions src/adapter/bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const BunAdapter: ElysiaAdapter = {
headers: hasHeaderShorthand
? 'c.headers = c.request.headers.toJSON()\n'
: 'c.headers = {}\n' +
'for (const [key, value] of c.request.headers.entries())' +
'c.headers[key] = value\n'
'for (const [key, value] of c.request.headers.entries())' +
'c.headers[key] = value\n'
},
listen(app) {
return (options, callback) => {
Expand All @@ -62,38 +62,38 @@ export const BunAdapter: ElysiaAdapter = {
const serve =
typeof options === 'object'
? ({
development: !isProduction,
reusePort: true,
...(app.config.serve || {}),
...(options || {}),
// @ts-ignore
static: {
...app.router.static.http.static,
...app.config.serve?.static
},
websocket: {
...(app.config.websocket || {}),
...(websocket || {})
},
fetch,
// @ts-expect-error private property
error: app.outerErrorHandler
} as Serve)
development: !isProduction,
reusePort: true,
...(app.config.serve || {}),
...(options || {}),
// @ts-ignore
static: {
...app.router.static.http.static,
...app.config.serve?.static
},
websocket: {
...(app.config.websocket || {}),
...(websocket || {})
},
fetch,
// @ts-expect-error private property
error: app.outerErrorHandler
} as Serve)
: ({
development: !isProduction,
reusePort: true,
...(app.config.serve || {}),
// @ts-ignore
static: app.router.static.http.static,
websocket: {
...(app.config.websocket || {}),
...(websocket || {})
},
port: options,
fetch,
// @ts-expect-error private property
error: app.outerErrorHandler
} as Serve)
development: !isProduction,
reusePort: true,
...(app.config.serve || {}),
// @ts-ignore
static: app.router.static.http.static,
websocket: {
...(app.config.websocket || {}),
...(websocket || {})
},
port: options,
fetch,
// @ts-expect-error private property
error: app.outerErrorHandler
} as Serve)

app.server = Bun?.serve(serve)

Expand All @@ -120,6 +120,21 @@ export const BunAdapter: ElysiaAdapter = {
})
}
},
async stop(app, closeActiveConnections) {
if (!app.server)
throw new Error(
"Elysia isn't running. Call `app.listen` to start the server."
)

if (app.server) {
app.server.stop(closeActiveConnections)
app.server = null

if (app.event.stop?.length)
for (let i = 0; i < app.event.stop.length; i++)
app.event.stop[i].fn(app)
}
},
ws(app, path, options) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { parse, body, response, ...rest } = options
Expand Down Expand Up @@ -195,20 +210,20 @@ export const BunAdapter: ElysiaAdapter = {
]

const handleErrors = !errorHandlers.length
? () => {}
? () => { }
: async (ws: ServerWebSocket<any>, error: unknown) => {
for (const handleError of errorHandlers) {
let response = handleError(
Object.assign(context, { error })
)
if (response instanceof Promise)
response = await response
for (const handleError of errorHandlers) {
let response = handleError(
Object.assign(context, { error })
)
if (response instanceof Promise)
response = await response

await handleResponse(ws, response)
await handleResponse(ws, response)

if (response) break
}
if (response) break
}
}

if (
server?.upgrade<any>(context.request, {
Expand Down
15 changes: 15 additions & 0 deletions src/adapter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ export interface ElysiaAdapter {
options: string | number | Partial<Serve>,
callback?: ListenCallback
) => void
/**
* Stop server from serving
*
* ---
* @example
* ```typescript
* app.stop()
* ```
*
* @example
* ```typescript
* app.stop(true) // Abruptly any requests inflight
* ```
*/
stop(app: AnyElysia, closeActiveConnections?: boolean): Promise<void>
isWebStandard?: boolean
handler: {
/**
Expand Down
15 changes: 15 additions & 0 deletions src/adapter/web-standard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ export const WebStandardAdapter: ElysiaAdapter = {
}
}
},
async stop(app, closeActiveConnections) {
if (!app.server)
throw new Error(
"Elysia isn't running. Call `app.listen` to start the server."
)

if (app.server) {
app.server.stop(closeActiveConnections)
app.server = null

if (app.event.stop?.length)
for (let i = 0; i < app.event.stop.length; i++)
app.event.stop[i].fn(app)
}
},
composeGeneralHandler: {
parameters: 'r',
createContext(app) {
Expand Down
14 changes: 2 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6397,19 +6397,9 @@ export default class Elysia<
* ```
*/
stop = async (closeActiveConnections?: boolean) => {
if (!this.server)
throw new Error(
"Elysia isn't running. Call `app.listen` to start the server."
)

if (this.server) {
this.server.stop(closeActiveConnections)
this.server = null
await this['~adapter'].stop(this, closeActiveConnections)

if (this.event.stop?.length)
for (let i = 0; i < this.event.stop.length; i++)
this.event.stop[i].fn(this)
}
return this
}

/**
Expand Down