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/index.ts
CHANGED
|
@@ -31,26 +31,26 @@ import type {
|
|
|
31
31
|
Middleware,
|
|
32
32
|
MiddlewareChain,
|
|
33
33
|
EndpointRequest,
|
|
34
|
-
|
|
35
|
-
RouteConfig,
|
|
36
|
-
RouteDefinition,
|
|
37
|
-
SocketCommands,
|
|
38
|
-
SocketOptions,
|
|
39
|
-
AppOptions,
|
|
34
|
+
Handler,
|
|
40
35
|
Server,
|
|
41
36
|
} from './utils'
|
|
42
37
|
|
|
38
|
+
import type { SocketCommands } from './utils'
|
|
39
|
+
|
|
43
40
|
import type {
|
|
41
|
+
SocketOptions,
|
|
44
42
|
SocketRoute,
|
|
45
43
|
SocketState,
|
|
46
44
|
} from './socket'
|
|
47
45
|
|
|
48
46
|
export * from './errors'
|
|
47
|
+
export { ServerCloseSignals } from './socket'
|
|
49
48
|
|
|
50
49
|
export {
|
|
51
50
|
StatusCode,
|
|
52
51
|
HttpMethod,
|
|
53
|
-
|
|
52
|
+
SessionType,
|
|
53
|
+
SessionFilter,
|
|
54
54
|
} from './utils'
|
|
55
55
|
|
|
56
56
|
export {
|
|
@@ -59,27 +59,31 @@ export {
|
|
|
59
59
|
validateSchemas,
|
|
60
60
|
} from './middleware'
|
|
61
61
|
|
|
62
|
-
export type {
|
|
62
|
+
export type {
|
|
63
|
+
ActiveSession,
|
|
64
|
+
ActiveSessions,
|
|
65
|
+
InactiveSession,
|
|
66
|
+
SocketConnection,
|
|
67
|
+
SocketOptions,
|
|
68
|
+
} from './socket'
|
|
63
69
|
|
|
64
70
|
export type {
|
|
65
|
-
AppOptions,
|
|
66
71
|
AsyncHandlerResult,
|
|
67
72
|
BaseRequest,
|
|
68
73
|
CloseSignal,
|
|
69
74
|
EndpointRequest,
|
|
75
|
+
FilterFn,
|
|
70
76
|
FormattedError,
|
|
71
77
|
Handler,
|
|
72
|
-
|
|
78
|
+
HandlerResult,
|
|
73
79
|
Middleware,
|
|
74
80
|
MiddlewareChain,
|
|
75
81
|
NextFn,
|
|
76
|
-
HandlerResult,
|
|
77
82
|
Request,
|
|
78
|
-
RouteConfig,
|
|
79
|
-
RouteDefinition,
|
|
80
83
|
Server,
|
|
81
|
-
|
|
82
|
-
|
|
84
|
+
SessionEntry,
|
|
85
|
+
SocketCommands,
|
|
86
|
+
SocketData,
|
|
83
87
|
WebSocketRequest,
|
|
84
88
|
} from './utils'
|
|
85
89
|
|
|
@@ -89,38 +93,64 @@ export type {
|
|
|
89
93
|
ValidationSchemas,
|
|
90
94
|
} from './middleware'
|
|
91
95
|
|
|
96
|
+
export type RouteDefinition<T = void> = {
|
|
97
|
+
method: HttpMethod
|
|
98
|
+
path: string
|
|
99
|
+
chain: Handler<T> | MiddlewareChain<T>
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type MetaEntry<T = void> = {
|
|
103
|
+
path: string
|
|
104
|
+
middleware: Middleware<T>[]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export type RouteConfig<T = void> = {
|
|
108
|
+
routes: RouteDefinition<T>[]
|
|
109
|
+
meta?: MetaEntry<T>[]
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export type AppOptions<T = void> = {
|
|
113
|
+
hostname?: string
|
|
114
|
+
mountPath?: string
|
|
115
|
+
middleware?: Middleware<T>[]
|
|
116
|
+
ws?: boolean | SocketOptions
|
|
117
|
+
onClose?: () => Promise<void> | void
|
|
118
|
+
}
|
|
119
|
+
|
|
92
120
|
type OutputRoutes = Record<string, string[]>
|
|
93
|
-
type ServerRoutes = Record<string, Record<string, EndpointHandler>>
|
|
94
121
|
|
|
95
|
-
type
|
|
122
|
+
type ServerRoutes<T = void> =
|
|
123
|
+
Record<string, Record<string, EndpointHandler<T>>>
|
|
124
|
+
|
|
125
|
+
type EndpointHandler<T = void> = (
|
|
96
126
|
bunReq: BunRequest,
|
|
97
|
-
server: Server
|
|
127
|
+
server: Server<T>,
|
|
98
128
|
) => AsyncHandlerResult
|
|
99
129
|
|
|
100
|
-
type ChainRoute = {
|
|
130
|
+
type ChainRoute<T = void> = {
|
|
101
131
|
method: HttpMethod
|
|
102
132
|
path: string
|
|
103
|
-
chain: MiddlewareChain
|
|
133
|
+
chain: MiddlewareChain<T>
|
|
104
134
|
}
|
|
105
135
|
|
|
106
|
-
type ModuleRoute = {
|
|
136
|
+
type ModuleRoute<T = void> = {
|
|
107
137
|
method: HttpMethod
|
|
108
138
|
path: string
|
|
109
|
-
handler: EndpointHandler
|
|
139
|
+
handler: EndpointHandler<T>
|
|
110
140
|
}
|
|
111
141
|
|
|
112
|
-
type AppRoutes = {
|
|
113
|
-
server: ServerRoutes
|
|
142
|
+
type AppRoutes<T = void> = {
|
|
143
|
+
server: ServerRoutes<T>
|
|
114
144
|
output: OutputRoutes
|
|
115
|
-
socket: SocketRoute[]
|
|
145
|
+
socket: SocketRoute<T>[]
|
|
116
146
|
}
|
|
117
147
|
|
|
118
148
|
type CloseFn = (force?: boolean) => Promise<void>
|
|
119
149
|
|
|
120
|
-
export type App = {
|
|
121
|
-
server: Server
|
|
150
|
+
export type App<T = void> = {
|
|
151
|
+
server: Server<T>
|
|
122
152
|
routes: OutputRoutes
|
|
123
|
-
ws: SocketCommands
|
|
153
|
+
ws: SocketCommands<T>
|
|
124
154
|
close: CloseFn
|
|
125
155
|
}
|
|
126
156
|
|
|
@@ -128,7 +158,7 @@ function methodNotAllowedHandler (_req: unknown): never {
|
|
|
128
158
|
throw new MethodNotAllowedError()
|
|
129
159
|
}
|
|
130
160
|
|
|
131
|
-
function defaultMethodMap (): Record<string, EndpointHandler
|
|
161
|
+
function defaultMethodMap<T> (): Record<string, EndpointHandler<T>> {
|
|
132
162
|
return {
|
|
133
163
|
HEAD: methodNotAllowedHandler,
|
|
134
164
|
GET: methodNotAllowedHandler,
|
|
@@ -153,11 +183,11 @@ function resolveSocketOptions (
|
|
|
153
183
|
return ws
|
|
154
184
|
}
|
|
155
185
|
|
|
156
|
-
function buildEndpointRequest (
|
|
186
|
+
function buildEndpointRequest<T> (
|
|
157
187
|
bunReq: BunRequest,
|
|
158
|
-
server: Server
|
|
159
|
-
ws: SocketCommands
|
|
160
|
-
): EndpointRequest {
|
|
188
|
+
server: Server<T>,
|
|
189
|
+
ws: SocketCommands<T>,
|
|
190
|
+
): EndpointRequest<T> {
|
|
161
191
|
const url = new URL(bunReq.url)
|
|
162
192
|
const qs = url.search.replace('?', '')
|
|
163
193
|
|
|
@@ -184,7 +214,7 @@ function buildEndpointRequest (
|
|
|
184
214
|
}
|
|
185
215
|
}
|
|
186
216
|
|
|
187
|
-
function normalizeChain (route: RouteDefinition): ChainRoute {
|
|
217
|
+
function normalizeChain<T> (route: RouteDefinition<T>): ChainRoute<T> {
|
|
188
218
|
const chain = Array.isArray(route.chain)
|
|
189
219
|
? route.chain
|
|
190
220
|
: [route.chain]
|
|
@@ -196,13 +226,13 @@ function normalizeChain (route: RouteDefinition): ChainRoute {
|
|
|
196
226
|
}
|
|
197
227
|
}
|
|
198
228
|
|
|
199
|
-
function buildMergedRoutes (
|
|
200
|
-
routePaths: ChainRoute[],
|
|
201
|
-
middleware: Middleware[],
|
|
202
|
-
meta: MetaEntry[],
|
|
203
|
-
state: SocketState
|
|
229
|
+
function buildMergedRoutes<T> (
|
|
230
|
+
routePaths: ChainRoute<T>[],
|
|
231
|
+
middleware: Middleware<T>[],
|
|
232
|
+
meta: MetaEntry<T>[],
|
|
233
|
+
state: SocketState<T>,
|
|
204
234
|
mountPath: string,
|
|
205
|
-
): ChainRoute[] {
|
|
235
|
+
): ChainRoute<T>[] {
|
|
206
236
|
const socketRoutes = buildSocketHandlers(state)
|
|
207
237
|
|
|
208
238
|
for (const socketRoute of socketRoutes) {
|
|
@@ -238,19 +268,21 @@ function buildMergedRoutes (
|
|
|
238
268
|
return routePaths
|
|
239
269
|
}
|
|
240
270
|
|
|
241
|
-
function buildSocketRoutes (
|
|
271
|
+
function buildSocketRoutes<T> (
|
|
272
|
+
mergedRoutes: ChainRoute<T>[],
|
|
273
|
+
): SocketRoute<T>[] {
|
|
242
274
|
return mergedRoutes.map(route => ({
|
|
243
275
|
...route,
|
|
244
276
|
segments: toSegments(route.path),
|
|
245
277
|
}))
|
|
246
278
|
}
|
|
247
279
|
|
|
248
|
-
function buildModuleRoutes (
|
|
249
|
-
socketRoutes: SocketRoute[],
|
|
250
|
-
ws: SocketCommands
|
|
251
|
-
): ModuleRoute[] {
|
|
280
|
+
function buildModuleRoutes<T> (
|
|
281
|
+
socketRoutes: SocketRoute<T>[],
|
|
282
|
+
ws: SocketCommands<T>,
|
|
283
|
+
): ModuleRoute<T>[] {
|
|
252
284
|
return socketRoutes.map(route => {
|
|
253
|
-
const handler: EndpointHandler = async (bunReq, server) => {
|
|
285
|
+
const handler: EndpointHandler<T> = async (bunReq, server) => {
|
|
254
286
|
const req = buildEndpointRequest(bunReq, server, ws)
|
|
255
287
|
|
|
256
288
|
return executeMiddlewareChain(req, route.chain)
|
|
@@ -264,11 +296,13 @@ function buildModuleRoutes (
|
|
|
264
296
|
})
|
|
265
297
|
}
|
|
266
298
|
|
|
267
|
-
function buildServerRoutes (
|
|
268
|
-
|
|
299
|
+
function buildServerRoutes<T> (
|
|
300
|
+
moduleRoutes: ModuleRoute<T>[],
|
|
301
|
+
): ServerRoutes<T> {
|
|
302
|
+
return moduleRoutes.reduce<ServerRoutes<T>>(
|
|
269
303
|
(accum, curr) => {
|
|
270
304
|
if (!accum[curr.path]) {
|
|
271
|
-
accum[curr.path] = defaultMethodMap()
|
|
305
|
+
accum[curr.path] = defaultMethodMap<T>()
|
|
272
306
|
}
|
|
273
307
|
|
|
274
308
|
accum[curr.path][curr.method] = curr.handler
|
|
@@ -277,7 +311,7 @@ function buildServerRoutes (moduleRoutes: ModuleRoute[]): ServerRoutes {
|
|
|
277
311
|
}, {})
|
|
278
312
|
}
|
|
279
313
|
|
|
280
|
-
function buildOutputRoutes (moduleRoutes: ModuleRoute[]): OutputRoutes {
|
|
314
|
+
function buildOutputRoutes<T> (moduleRoutes: ModuleRoute<T>[]): OutputRoutes {
|
|
281
315
|
return moduleRoutes.reduce<OutputRoutes>((accum, curr) => {
|
|
282
316
|
accum[curr.path] = accum[curr.path] || []
|
|
283
317
|
accum[curr.path].push(curr.method)
|
|
@@ -286,12 +320,12 @@ function buildOutputRoutes (moduleRoutes: ModuleRoute[]): OutputRoutes {
|
|
|
286
320
|
}, {})
|
|
287
321
|
}
|
|
288
322
|
|
|
289
|
-
function buildRoutes (
|
|
290
|
-
config: RouteConfig
|
|
291
|
-
state: SocketState | null,
|
|
292
|
-
ws: SocketCommands
|
|
293
|
-
opts: AppOptions
|
|
294
|
-
): AppRoutes {
|
|
323
|
+
function buildRoutes<T> (
|
|
324
|
+
config: RouteConfig<T>,
|
|
325
|
+
state: SocketState<T> | null,
|
|
326
|
+
ws: SocketCommands<T>,
|
|
327
|
+
opts: AppOptions<T>,
|
|
328
|
+
): AppRoutes<T> {
|
|
295
329
|
const mountPath = opts.mountPath || ''
|
|
296
330
|
const middleware = opts.middleware || []
|
|
297
331
|
const meta = config.meta || []
|
|
@@ -330,13 +364,13 @@ function buildRoutes (
|
|
|
330
364
|
}
|
|
331
365
|
}
|
|
332
366
|
|
|
333
|
-
function buildServer (
|
|
367
|
+
function buildServer<T> (
|
|
334
368
|
port: number,
|
|
335
|
-
routes: AppRoutes
|
|
336
|
-
state: SocketState | null,
|
|
337
|
-
ws: SocketCommands
|
|
338
|
-
opts: AppOptions
|
|
339
|
-
): Server {
|
|
369
|
+
routes: AppRoutes<T>,
|
|
370
|
+
state: SocketState<T> | null,
|
|
371
|
+
ws: SocketCommands<T>,
|
|
372
|
+
opts: AppOptions<T>,
|
|
373
|
+
): Server<T> {
|
|
340
374
|
const hostname = opts.hostname || '0.0.0.0'
|
|
341
375
|
|
|
342
376
|
const websocket = state
|
|
@@ -360,10 +394,10 @@ function buildServer (
|
|
|
360
394
|
|
|
361
395
|
return Response.json(httpError.output, { status })
|
|
362
396
|
},
|
|
363
|
-
}) as Server
|
|
397
|
+
}) as Server<T>
|
|
364
398
|
}
|
|
365
399
|
|
|
366
|
-
function processIO (server: Server
|
|
400
|
+
function processIO<T> (server: Server<T>, opts: AppOptions<T>): CloseFn {
|
|
367
401
|
const onClose = opts.onClose || (() => {})
|
|
368
402
|
|
|
369
403
|
console.info(`Running on port: ${server.port}`)
|
|
@@ -400,20 +434,20 @@ function processIO (server: Server, opts: AppOptions): CloseFn {
|
|
|
400
434
|
}
|
|
401
435
|
}
|
|
402
436
|
|
|
403
|
-
export function createApp (
|
|
437
|
+
export function createApp<T = void> (
|
|
404
438
|
port: number,
|
|
405
|
-
config: RouteConfig
|
|
406
|
-
opts: AppOptions = {},
|
|
407
|
-
): App {
|
|
439
|
+
config: RouteConfig<T>,
|
|
440
|
+
opts: AppOptions<T> = {},
|
|
441
|
+
): App<T> {
|
|
408
442
|
const socketOpts = resolveSocketOptions(opts.ws)
|
|
409
443
|
|
|
410
444
|
const state = socketOpts
|
|
411
|
-
? buildSocketState(socketOpts)
|
|
445
|
+
? buildSocketState<T>(socketOpts)
|
|
412
446
|
: null
|
|
413
447
|
|
|
414
448
|
const ws = state
|
|
415
|
-
? buildSocketCommands(state)
|
|
416
|
-
: buildDisabledSocketCommands()
|
|
449
|
+
? buildSocketCommands<T>(state)
|
|
450
|
+
: buildDisabledSocketCommands<T>()
|
|
417
451
|
|
|
418
452
|
const routes = buildRoutes(config, state, ws, opts)
|
|
419
453
|
const server = buildServer(port, routes, state, ws, opts)
|
package/src/core/middleware.ts
CHANGED
|
@@ -39,7 +39,7 @@ export type SchemaKey = keyof ValidationSchemas
|
|
|
39
39
|
let _schemasCompiled = false
|
|
40
40
|
let _customFormats: Record<string, Format> | null = null
|
|
41
41
|
|
|
42
|
-
async function parseBody (req: Request): Promise<unknown> {
|
|
42
|
+
async function parseBody<T> (req: Request<T>): Promise<unknown> {
|
|
43
43
|
try {
|
|
44
44
|
const result = await req.json()
|
|
45
45
|
|
|
@@ -117,9 +117,9 @@ function compileSchemas (
|
|
|
117
117
|
})
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
export function parseJsonBody (): Middleware {
|
|
120
|
+
export function parseJsonBody<T = void> (): Middleware<T> {
|
|
121
121
|
return async (
|
|
122
|
-
req: Request
|
|
122
|
+
req: Request<T>,
|
|
123
123
|
res: unknown,
|
|
124
124
|
next: NextFn,
|
|
125
125
|
): AsyncHandlerResult => {
|
|
@@ -158,8 +158,8 @@ export function resetValidationFormatsState (): void {
|
|
|
158
158
|
_schemasCompiled = false
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
function buildValidationSource (
|
|
162
|
-
req: Request
|
|
161
|
+
function buildValidationSource<T> (
|
|
162
|
+
req: Request<T>,
|
|
163
163
|
res: unknown,
|
|
164
164
|
): Record<SchemaKey, unknown> {
|
|
165
165
|
return {
|
|
@@ -170,13 +170,15 @@ function buildValidationSource (
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
export function validateSchemas
|
|
173
|
+
export function validateSchemas<T = void> (
|
|
174
|
+
schemas: ValidationSchemas,
|
|
175
|
+
): Middleware<T> {
|
|
174
176
|
const entries = compileSchemas(schemas)
|
|
175
177
|
|
|
176
178
|
_schemasCompiled = true
|
|
177
179
|
|
|
178
180
|
return (
|
|
179
|
-
req: Request
|
|
181
|
+
req: Request<T>,
|
|
180
182
|
res: unknown,
|
|
181
183
|
next: NextFn,
|
|
182
184
|
): HandlerResult => {
|