galbe 0.7.0 → 0.8.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/docs/context.md +5 -1
- package/package.json +1 -1
- package/src/routes.ts +3 -0
- package/src/server.ts +5 -3
- package/src/types.ts +5 -2
package/docs/context.md
CHANGED
|
@@ -11,7 +11,7 @@ A context has the following properties:
|
|
|
11
11
|
|
|
12
12
|
**request**
|
|
13
13
|
|
|
14
|
-
An instance of the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object created by
|
|
14
|
+
An instance of the [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) object created by the server.
|
|
15
15
|
|
|
16
16
|
**headers**
|
|
17
17
|
|
|
@@ -112,3 +112,7 @@ galbe.get(
|
|
|
112
112
|
$ curl http://localhost:3000/example
|
|
113
113
|
bar
|
|
114
114
|
```
|
|
115
|
+
|
|
116
|
+
**remoteAddress**
|
|
117
|
+
|
|
118
|
+
An instance of the [SocketAdress](https://github.com/oven-sh/bun/blob/fe62a614046948ebba260bed87db96287e67921f/packages/bun-types/bun.d.ts#L2600-L2613) representing the remote address of the client.
|
package/package.json
CHANGED
package/src/routes.ts
CHANGED
package/src/server.ts
CHANGED
|
@@ -28,7 +28,7 @@ export default async (galbe: Galbe, port?: number, hostname?: string) => {
|
|
|
28
28
|
galbe.config.basePath = `/${galbe?.config?.basePath}`
|
|
29
29
|
let pluginsCb = setupPluginCallbacks(galbe)
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const server = Bun.serve({
|
|
32
32
|
port: port || galbe.config?.port || 3000,
|
|
33
33
|
hostname: hostname || galbe.config?.hostname || 'localhost',
|
|
34
34
|
tls: galbe.config?.tls,
|
|
@@ -37,6 +37,7 @@ export default async (galbe: Galbe, port?: number, hostname?: string) => {
|
|
|
37
37
|
if (!METHODS.includes(req.method)) return new Response('', { status: 501 })
|
|
38
38
|
const context = {
|
|
39
39
|
request: req,
|
|
40
|
+
remoteAddress: server.requestIP(req),
|
|
40
41
|
set: { headers: {} },
|
|
41
42
|
state: {}
|
|
42
43
|
} as MakeOptional<Context, 'headers' | 'params' | 'query' | 'body'>
|
|
@@ -128,12 +129,12 @@ export default async (galbe: Galbe, port?: number, hostname?: string) => {
|
|
|
128
129
|
if (nextCalled) console.error('Hook already called - ignored')
|
|
129
130
|
else {
|
|
130
131
|
nextCalled = true
|
|
131
|
-
await callChain[idx + 1].call()
|
|
132
|
+
return await callChain[idx + 1].call()
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
let r = await hook(context as Context, next)
|
|
135
136
|
if (r) return r
|
|
136
|
-
if (!nextCalled && !handlerCalled) await next()
|
|
137
|
+
if (!nextCalled && !handlerCalled) return await next()
|
|
137
138
|
}
|
|
138
139
|
}))
|
|
139
140
|
callChain.push({
|
|
@@ -199,4 +200,5 @@ export default async (galbe: Galbe, port?: number, hostname?: string) => {
|
|
|
199
200
|
})
|
|
200
201
|
}
|
|
201
202
|
})
|
|
203
|
+
return server
|
|
202
204
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ServeOptions, TLSOptions, TLSServeOptions } from 'bun'
|
|
1
|
+
import type { ServeOptions, SocketAddress, TLSOptions, TLSServeOptions } from 'bun'
|
|
2
2
|
import type {
|
|
3
3
|
STAny,
|
|
4
4
|
STArray,
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
STJson,
|
|
9
9
|
STLiteral,
|
|
10
10
|
STMultipartForm,
|
|
11
|
+
STNull,
|
|
11
12
|
STNumber,
|
|
12
13
|
STObject,
|
|
13
14
|
STOptional,
|
|
@@ -48,6 +49,7 @@ export type STResponseValue =
|
|
|
48
49
|
| STUnion
|
|
49
50
|
| STStream
|
|
50
51
|
| STAny
|
|
52
|
+
| STNull
|
|
51
53
|
export type STResponse = Record<number, STResponseValue>
|
|
52
54
|
|
|
53
55
|
export type MaybeArray<T> = T | T[]
|
|
@@ -162,6 +164,7 @@ export type Context<
|
|
|
162
164
|
query: Static<STObject<Exclude<S['query'], undefined>>>
|
|
163
165
|
body: M extends 'get' | 'options' | 'head' ? null : StaticBody<Exclude<S['body'], undefined>>
|
|
164
166
|
request: Request
|
|
167
|
+
remoteAddress: SocketAddress | null
|
|
165
168
|
route?: Route
|
|
166
169
|
state: Record<string, any>
|
|
167
170
|
set: {
|
|
@@ -171,7 +174,7 @@ export type Context<
|
|
|
171
174
|
status?: number
|
|
172
175
|
}
|
|
173
176
|
}
|
|
174
|
-
export type Next = () => void | Promise<
|
|
177
|
+
export type Next = () => void | Promise<any>
|
|
175
178
|
export type Hook<M extends Method = Method, Path extends string = string, S extends RequestSchema = RequestSchema> = (
|
|
176
179
|
ctx: Context<M, Path, S>,
|
|
177
180
|
next: Next
|