sleepy-serv 0.23.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +53 -8
- package/dist/core/index.d.ts +31 -8
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +2 -2
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/socket.d.ts +44 -17
- package/dist/core/socket.d.ts.map +1 -1
- package/dist/core/utils.d.ts +40 -86
- package/dist/core/utils.d.ts.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/core/index.ts +105 -71
- package/src/core/middleware.ts +9 -7
- package/src/core/socket.ts +172 -71
- package/src/core/utils.ts +51 -103
- package/src/index.ts +4 -4
package/src/core/utils.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { ErrorObject } from 'ajv'
|
|
2
2
|
import type { BunRequest, Server as BunServer } from 'bun'
|
|
3
3
|
|
|
4
|
+
export type TimeoutHandle = ReturnType<typeof setTimeout>
|
|
5
|
+
|
|
4
6
|
export const HttpMethod = {
|
|
5
7
|
Head: 'HEAD',
|
|
6
8
|
Get: 'GET',
|
|
@@ -94,142 +96,88 @@ export type AsyncHandlerResult = Promise<Response>
|
|
|
94
96
|
export type HandlerResult = Response | AsyncHandlerResult
|
|
95
97
|
export type NextFn = (data?: unknown) => HandlerResult
|
|
96
98
|
|
|
97
|
-
export type Middleware = (
|
|
98
|
-
req: Request
|
|
99
|
+
export type Middleware<T = void> = (
|
|
100
|
+
req: Request<T>,
|
|
99
101
|
res: unknown,
|
|
100
102
|
next: NextFn,
|
|
101
103
|
) => HandlerResult
|
|
102
104
|
|
|
103
|
-
export type Handler = (
|
|
104
|
-
req: Request
|
|
105
|
+
export type Handler<T = void> = (
|
|
106
|
+
req: Request<T>,
|
|
105
107
|
res: unknown,
|
|
106
108
|
) => HandlerResult
|
|
107
109
|
|
|
108
|
-
export type MiddlewareChain = (Middleware | Handler)[]
|
|
110
|
+
export type MiddlewareChain<T = void> = (Middleware<T> | Handler<T>)[]
|
|
109
111
|
|
|
110
|
-
export type
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
export type CloseSignal = {
|
|
113
|
+
code: number
|
|
114
|
+
reason: string
|
|
113
115
|
}
|
|
114
116
|
|
|
115
|
-
export type
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
export type SocketData<T = void> = {
|
|
118
|
+
clientId: string
|
|
119
|
+
superseded: boolean
|
|
120
|
+
reaped: boolean
|
|
121
|
+
reaperHandle: TimeoutHandle | null
|
|
122
|
+
data: T
|
|
119
123
|
}
|
|
120
124
|
|
|
121
|
-
export
|
|
122
|
-
|
|
125
|
+
export const SessionType = {
|
|
126
|
+
Active: 'active',
|
|
127
|
+
Inactive: 'inactive',
|
|
128
|
+
} as const
|
|
129
|
+
|
|
130
|
+
export type SessionType = typeof SessionType[keyof typeof SessionType]
|
|
131
|
+
|
|
132
|
+
export const SessionFilter = {
|
|
133
|
+
Active: 'active',
|
|
134
|
+
Inactive: 'inactive',
|
|
135
|
+
All: 'all',
|
|
136
|
+
} as const
|
|
137
|
+
|
|
138
|
+
export type SessionFilter = typeof SessionFilter[keyof typeof SessionFilter]
|
|
123
139
|
|
|
124
|
-
export type SessionEntry = {
|
|
140
|
+
export type SessionEntry<T = void> = {
|
|
125
141
|
clientId: string
|
|
126
|
-
|
|
142
|
+
type: SessionType
|
|
143
|
+
data: T
|
|
127
144
|
}
|
|
128
145
|
|
|
129
|
-
export type FilterFn = (
|
|
130
|
-
|
|
131
|
-
data: unknown,
|
|
146
|
+
export type FilterFn<T = void> = (
|
|
147
|
+
session: SessionEntry<T>,
|
|
132
148
|
index: number,
|
|
133
149
|
) => boolean
|
|
134
150
|
|
|
135
|
-
export type SocketCommands = {
|
|
151
|
+
export type SocketCommands<T = void> = {
|
|
136
152
|
broadcast: (event: string, body: unknown) => void
|
|
137
|
-
send: (event: string, body: unknown, fn: FilterFn) => void
|
|
138
|
-
drop: (signal: CloseSignal, fn: FilterFn) => void
|
|
139
|
-
query: (fn: FilterFn) => SessionEntry[]
|
|
153
|
+
send: (event: string, body: unknown, fn: FilterFn<T>) => void
|
|
154
|
+
drop: (signal: CloseSignal, fn: FilterFn<T>) => void
|
|
155
|
+
query: (fn: FilterFn<T>, filter?: SessionFilter) => SessionEntry<T>[]
|
|
140
156
|
}
|
|
141
157
|
|
|
142
|
-
export type BaseRequest = {
|
|
158
|
+
export type BaseRequest<T = void> = {
|
|
143
159
|
method: HttpMethod
|
|
144
160
|
route: string
|
|
145
161
|
headers: Headers
|
|
146
162
|
params: Record<string, string>
|
|
147
163
|
query: Record<string, unknown>
|
|
148
164
|
json: () => Promise<unknown>
|
|
149
|
-
ws: SocketCommands
|
|
165
|
+
ws: SocketCommands<T>
|
|
150
166
|
}
|
|
151
167
|
|
|
152
|
-
export type EndpointRequest = BaseRequest & {
|
|
168
|
+
export type EndpointRequest<T = void> = BaseRequest<T> & {
|
|
153
169
|
raw: BunRequest
|
|
154
|
-
server: Server
|
|
170
|
+
server: Server<T>
|
|
155
171
|
}
|
|
156
172
|
|
|
157
|
-
export type WebSocketRequest = BaseRequest & {
|
|
173
|
+
export type WebSocketRequest<T = void> = BaseRequest<T> & {
|
|
158
174
|
id: string
|
|
159
175
|
clientId: string
|
|
160
176
|
}
|
|
161
177
|
|
|
162
|
-
export type Request = EndpointRequest | WebSocketRequest
|
|
163
|
-
|
|
164
|
-
export type CloseSignal = {
|
|
165
|
-
code: number
|
|
166
|
-
reason: string
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export const InternalCloseSignal = {
|
|
170
|
-
Ok: {
|
|
171
|
-
code: 1000,
|
|
172
|
-
reason: 'ok',
|
|
173
|
-
},
|
|
174
|
-
Reaped: {
|
|
175
|
-
code: 4998,
|
|
176
|
-
reason: 'reaped',
|
|
177
|
-
},
|
|
178
|
-
Superseded: {
|
|
179
|
-
code: 4999,
|
|
180
|
-
reason: 'superseded',
|
|
181
|
-
},
|
|
182
|
-
} as const
|
|
178
|
+
export type Request<T = void> = EndpointRequest<T> | WebSocketRequest<T>
|
|
183
179
|
|
|
184
|
-
export type
|
|
185
|
-
dropThreshold?: number
|
|
186
|
-
heartbeatInterval?: number
|
|
187
|
-
maxTickets?: number
|
|
188
|
-
reclaimTtl?: number
|
|
189
|
-
ticketTtl?: number
|
|
190
|
-
onOpen?: (clientId: string) => void
|
|
191
|
-
onClose?: (clientId: string, signal: CloseSignal) => void
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
export type SocketData = {
|
|
195
|
-
clientId: string
|
|
196
|
-
superseded: boolean
|
|
197
|
-
reaped: boolean
|
|
198
|
-
reaperHandle: ReturnType<typeof setTimeout> | null
|
|
199
|
-
app: unknown
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export type SocketConnection = {
|
|
203
|
-
data: SocketData
|
|
204
|
-
send: (data: string) => unknown
|
|
205
|
-
close: (code?: number, reason?: string) => void
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export type Server = BunServer<SocketData>
|
|
209
|
-
|
|
210
|
-
export type RouteDefinition = {
|
|
211
|
-
method: HttpMethod
|
|
212
|
-
path: string
|
|
213
|
-
chain: Handler | MiddlewareChain
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
export type MetaEntry = {
|
|
217
|
-
path: string
|
|
218
|
-
middleware: Middleware[]
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
export type RouteConfig = {
|
|
222
|
-
routes: RouteDefinition[]
|
|
223
|
-
meta?: MetaEntry[]
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export type AppOptions = {
|
|
227
|
-
hostname?: string
|
|
228
|
-
mountPath?: string
|
|
229
|
-
middleware?: Middleware[]
|
|
230
|
-
ws?: boolean | SocketOptions
|
|
231
|
-
onClose?: () => Promise<void> | void
|
|
232
|
-
}
|
|
180
|
+
export type Server<T = void> = BunServer<SocketData<T>>
|
|
233
181
|
|
|
234
182
|
export function toSegments (pathString: string): string[] {
|
|
235
183
|
const [pathname] = String(pathString).split('?')
|
|
@@ -259,9 +207,9 @@ export function formatError (
|
|
|
259
207
|
}
|
|
260
208
|
}
|
|
261
209
|
|
|
262
|
-
export async function executeMiddlewareChain (
|
|
263
|
-
req: Request
|
|
264
|
-
chain: MiddlewareChain
|
|
210
|
+
export async function executeMiddlewareChain<T = void> (
|
|
211
|
+
req: Request<T>,
|
|
212
|
+
chain: MiddlewareChain<T>,
|
|
265
213
|
): AsyncHandlerResult {
|
|
266
214
|
if (!chain.length) {
|
|
267
215
|
throw new RangeError('Middleware chain is empty')
|
|
@@ -276,8 +224,8 @@ export async function executeMiddlewareChain (
|
|
|
276
224
|
const next = (data?: unknown) => executeMiddleware(index + 1, data)
|
|
277
225
|
|
|
278
226
|
const result = isLast
|
|
279
|
-
? await (fn as Handler)(req, res)
|
|
280
|
-
: await (fn as Middleware)(req, res, next)
|
|
227
|
+
? await (fn as Handler<T>)(req, res)
|
|
228
|
+
: await (fn as Middleware<T>)(req, res, next)
|
|
281
229
|
|
|
282
230
|
if (result instanceof Response) {
|
|
283
231
|
return result
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export * from './core'
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type { AppOptions, App } from './core'
|
|
4
4
|
|
|
5
|
-
export declare function createApp (
|
|
5
|
+
export declare function createApp<T = void> (
|
|
6
6
|
port: number,
|
|
7
|
-
opts?: AppOptions
|
|
8
|
-
): App
|
|
7
|
+
opts?: AppOptions<T>,
|
|
8
|
+
): App<T>
|