raiton 6.1.0-beta.4 → 6.3.0-beta.10
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/CHANGELOG.md +24 -0
- package/build/bin/index.mjs +25 -1176
- package/docs/contributors/README.md +8 -1
- package/package.json +2 -2
- package/scripts/postinstall.ts +42 -0
- package/scripts/update-version.ts +532 -123
- package/source/core/application.ts +5 -10
- package/source/core/injection/injection.ts +1 -1
- package/source/core/router/handler.ts +13 -10
- package/source/core/router/matcher.ts +16 -4
- package/source/framework/decorators/guard.decorator.ts +2 -5
- package/source/framework/plugins/security/body-limit.ts +2 -2
- package/source/framework/plugins/security/method-guard.ts +2 -2
- package/source/framework/plugins/security/rate-limit.ts +2 -2
- package/source/framework/responses/helpers.ts +3 -1
- package/source/framework/runtime/bun/server.ts +11 -10
|
@@ -2,7 +2,7 @@ import {Security, bodyParserPlugin} from "../framework/plugins";
|
|
|
2
2
|
import {PluginScope} from './plugins/scope'
|
|
3
3
|
import {RequestContext} from './context'
|
|
4
4
|
import {ApplicationConfigInterface, ApplicationInterface} from "../types/application";
|
|
5
|
-
import {HttpException, HttpMethod, ThrowableResponse} from "../framework";
|
|
5
|
+
import {HttpException, HttpMethod, ThrowableResponse, RaitonResponses, HttpStatus} from "../framework";
|
|
6
6
|
import {RouteHandlerCallable} from "../types";
|
|
7
7
|
import {Logger} from "@protorians/logger";
|
|
8
8
|
import {RaitonConfig} from "./config";
|
|
@@ -141,7 +141,7 @@ export class Application implements ApplicationInterface {
|
|
|
141
141
|
Logger.warn(`Request out of application pathname: ${pathname} (expected prefix: ${this.config.pathname})`)
|
|
142
142
|
}
|
|
143
143
|
reply.status(404)
|
|
144
|
-
return reply.send({error: false
|
|
144
|
+
return reply.send(RaitonResponses('Not Found', null, HttpStatus.NOT_FOUND, {error: false}))
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
|
|
@@ -155,7 +155,7 @@ export class Application implements ApplicationInterface {
|
|
|
155
155
|
Logger.warn(`Route not found: ${req.method} ${pathname}`)
|
|
156
156
|
}
|
|
157
157
|
reply.status(404)
|
|
158
|
-
return reply.send({error: false
|
|
158
|
+
return reply.send(RaitonResponses('Not Found', null, HttpStatus.NOT_FOUND, {error: false}))
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
try {
|
|
@@ -177,7 +177,7 @@ export class Application implements ApplicationInterface {
|
|
|
177
177
|
console.error(e)
|
|
178
178
|
}
|
|
179
179
|
context.reply.status(500)
|
|
180
|
-
context.reply.send(
|
|
180
|
+
context.reply.send(RaitonResponses('Internal Server Error', null, HttpStatus.INTERNAL_SERVER_ERROR, {error: true}))
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
@@ -191,12 +191,7 @@ export class Application implements ApplicationInterface {
|
|
|
191
191
|
ctx.reply.status(err.statusCode ?? 500)
|
|
192
192
|
return ctx.reply.send(err.render())
|
|
193
193
|
}
|
|
194
|
-
ctx.reply.send({
|
|
195
|
-
statusCode: 500,
|
|
196
|
-
error: true,
|
|
197
|
-
message: err.message ?? err,
|
|
198
|
-
data: null
|
|
199
|
-
})
|
|
194
|
+
ctx.reply.send(RaitonResponses(err.message ?? err, null, HttpStatus.INTERNAL_SERVER_ERROR, {error: true}))
|
|
200
195
|
}
|
|
201
196
|
}
|
|
202
197
|
}
|
|
@@ -4,6 +4,7 @@ import {Raiton} from "..";
|
|
|
4
4
|
import {HttpException} from "../../framework/exceptions";
|
|
5
5
|
import {ThrowableResponse} from "../../framework/responses/http-throwable";
|
|
6
6
|
import {collectRouteArguments, validateDtoArguments, validateResponse, runMiddlewares} from "./handler.utils";
|
|
7
|
+
import {RaitonResponses, HttpStatus} from "../../framework";
|
|
7
8
|
|
|
8
9
|
export function createHandler(
|
|
9
10
|
instance: any,
|
|
@@ -33,16 +34,18 @@ export function createHandler(
|
|
|
33
34
|
return err.render()
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
return
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
37
|
+
return RaitonResponses(
|
|
38
|
+
err.message ?? err,
|
|
39
|
+
null,
|
|
40
|
+
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
41
|
+
{
|
|
42
|
+
error: true,
|
|
43
|
+
errorStack: isDevelopment
|
|
44
|
+
? (typeof err.stack === 'string' ? err.stack.split('\n')
|
|
45
|
+
: [String(err.stack || err.toString() || err.message || err.name || 'Unknown error')])
|
|
46
|
+
.map((l: any) => typeof l === 'string' ? l.trim() : String(l)) : undefined
|
|
47
|
+
}
|
|
48
|
+
);
|
|
46
49
|
}
|
|
47
50
|
};
|
|
48
51
|
|
|
@@ -42,10 +42,22 @@ export class RouteMatcher {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
match(method: string, url: string): Route | null {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
let best: Route | null = null
|
|
46
|
+
let bestParamCount = Number.MAX_SAFE_INTEGER
|
|
47
|
+
|
|
48
|
+
for (const route of this._routes.values()) {
|
|
49
|
+
if (!route.match(method, url)) continue
|
|
50
|
+
|
|
51
|
+
const paramCount = route.params.length
|
|
52
|
+
if (paramCount < bestParamCount) {
|
|
53
|
+
bestParamCount = paramCount
|
|
54
|
+
best = route
|
|
55
|
+
|
|
56
|
+
if (paramCount === 0) break
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return best ? best.extractParams(url) : null
|
|
49
61
|
}
|
|
50
62
|
|
|
51
63
|
clear(): this {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {GuardOptions} from "../../types";
|
|
2
2
|
import {Middleware} from "./middleware.decorator";
|
|
3
3
|
import {RaitonGuards} from "../../core/guards";
|
|
4
|
-
import {HttpStatus} from "
|
|
4
|
+
import {HttpStatus, RaitonResponses} from "..";
|
|
5
5
|
|
|
6
6
|
export function createGuardDecoration({name, handler}: GuardOptions) {
|
|
7
7
|
return Middleware(async ({next, context}) => {
|
|
@@ -15,9 +15,6 @@ export function createGuardDecoration({name, handler}: GuardOptions) {
|
|
|
15
15
|
if (response) return next()
|
|
16
16
|
|
|
17
17
|
context.reply.status(HttpStatus.FORBIDDEN)
|
|
18
|
-
return
|
|
19
|
-
error: true,
|
|
20
|
-
message: 'Forbidden'
|
|
21
|
-
});
|
|
18
|
+
return context.reply.send(RaitonResponses('Forbidden', null, HttpStatus.FORBIDDEN, {error: true}));
|
|
22
19
|
});
|
|
23
20
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {definePlugin} from "../../../core/plugins";
|
|
2
2
|
import {MiddlewareParametersInterface} from "../../../types";
|
|
3
|
+
import {RaitonResponses, HttpStatus} from "../..";
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
export const secureBodyLimit = (maxBytes = 1_000_000) =>
|
|
@@ -10,8 +11,7 @@ export const secureBodyLimit = (maxBytes = 1_000_000) =>
|
|
|
10
11
|
)
|
|
11
12
|
|
|
12
13
|
if (len > maxBytes) {
|
|
13
|
-
context.
|
|
14
|
-
return context.send({ error: 'Payload too large' })
|
|
14
|
+
return context.send(RaitonResponses('Payload too large', null, HttpStatus.PAYLOAD_TOO_LARGE, {error: true}))
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
await next()
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {definePlugin} from "../../../core/plugins";
|
|
2
2
|
import {ContextInterface, MiddlewareParametersInterface, MiddlewareNextCallable} from "../../../types";
|
|
3
|
+
import {RaitonResponses, HttpStatus} from "../..";
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
export const secureMethodGuard = (allowed: string[]) =>
|
|
6
7
|
definePlugin((scope) => {
|
|
7
8
|
scope.use(async ({context, next}: MiddlewareParametersInterface) => {
|
|
8
9
|
if (!allowed.includes(context.req.method)) {
|
|
9
|
-
context.
|
|
10
|
-
return context.send({ error: 'Method not allowed' })
|
|
10
|
+
return context.send(RaitonResponses('Method not allowed', null, HttpStatus.METHOD_NOT_ALLOWED, {error: true}))
|
|
11
11
|
}
|
|
12
12
|
await next()
|
|
13
13
|
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {definePlugin} from "../../../core/plugins";
|
|
2
2
|
import {ContextInterface, MiddlewareParametersInterface, MiddlewareNextCallable} from "../../../types";
|
|
3
|
+
import {RaitonResponses, HttpStatus} from "../..";
|
|
3
4
|
|
|
4
5
|
|
|
5
6
|
export interface RateLimitOptions {
|
|
@@ -33,8 +34,7 @@ export const secureRateLimit = (
|
|
|
33
34
|
hits.set(ip, entry)
|
|
34
35
|
|
|
35
36
|
if (entry.count > max) {
|
|
36
|
-
context.
|
|
37
|
-
return context.send({error: 'Too many requests'})
|
|
37
|
+
return context.send(RaitonResponses('Too many requests', null, HttpStatus.TOO_MANY_REQUESTS, {error: true}))
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
await next()
|
|
@@ -12,7 +12,9 @@ export function RaitonResponses(
|
|
|
12
12
|
) {
|
|
13
13
|
|
|
14
14
|
if (metadata) {
|
|
15
|
-
metadata.error
|
|
15
|
+
if (metadata.error === undefined) {
|
|
16
|
+
metadata.error = typeof data?.error === 'boolean' ? data?.error : false
|
|
17
|
+
}
|
|
16
18
|
metadata.errorStack = (metadata.errorStack instanceof Error)
|
|
17
19
|
? metadata.errorStack
|
|
18
20
|
: (Raiton.thread?.builder?.options?.serve
|
|
@@ -6,6 +6,7 @@ import {Logger} from "@protorians/logger";
|
|
|
6
6
|
import {getRealIp} from "../../utilities";
|
|
7
7
|
import {findSocketForPath} from "../../../core/socket";
|
|
8
8
|
import {Injection} from "../../../core/injection";
|
|
9
|
+
import {RaitonResponses, HttpStatus} from "../..";
|
|
9
10
|
|
|
10
11
|
export const bunRuntime: RuntimeAdapterInterface = {
|
|
11
12
|
createServer(handler, options?: RuntimeServerOptionsInterface) {
|
|
@@ -50,11 +51,11 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
50
51
|
: entry.metadata.events.find((item: any) => item.type === 'message')
|
|
51
52
|
|
|
52
53
|
if (!event) {
|
|
53
|
-
ws.send(JSON.stringify(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
ws.send(JSON.stringify(RaitonResponses(
|
|
55
|
+
`Socket event "${eventName ?? 'message'}" not found`,
|
|
56
|
+
null,
|
|
57
|
+
HttpStatus.NOT_FOUND
|
|
58
|
+
)))
|
|
58
59
|
return
|
|
59
60
|
}
|
|
60
61
|
|
|
@@ -64,11 +65,11 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
64
65
|
}
|
|
65
66
|
} catch (e: any) {
|
|
66
67
|
Logger.error('Socket message failed', e.message ?? e)
|
|
67
|
-
ws.send(JSON.stringify(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
68
|
+
ws.send(JSON.stringify(RaitonResponses(
|
|
69
|
+
'Internal server error',
|
|
70
|
+
null,
|
|
71
|
+
HttpStatus.INTERNAL_SERVER_ERROR
|
|
72
|
+
)))
|
|
72
73
|
}
|
|
73
74
|
},
|
|
74
75
|
async close(ws: any) {
|