raiton 5.9.7-beta.18 → 6.0.1-beta.2
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 +13 -9
- package/build/bin/index.mjs +7 -18
- package/docs/README.md +6 -129
- package/docs/contributors/README.md +103 -0
- package/docs/{cli → contributors/cli}/README.md +1 -1
- package/docs/{cli → contributors/cli}/hmr.md +20 -23
- package/docs/{core → contributors/core}/README.md +1 -1
- package/docs/{core → contributors/core}/builder-thread.md +4 -7
- package/docs/{core → contributors/core}/controller.md +1 -1
- package/docs/contributors/framework/README.md +67 -0
- package/docs/{framework → contributors/framework}/base-classes.md +30 -2
- package/docs/{framework → contributors/framework}/decorators/socket.md +31 -3
- package/docs/{types → contributors/types}/README.md +1 -1
- package/docs/user-guide/README.md +62 -0
- package/docs/user-guide/api/README.md +21 -0
- package/docs/user-guide/api/example.md +50 -0
- package/docs/user-guide/api/responses.md +58 -0
- package/docs/user-guide/api/routes.md +56 -0
- package/docs/user-guide/architecture.md +83 -0
- package/docs/user-guide/best-practices.md +36 -0
- package/docs/user-guide/body-parser.md +39 -0
- package/docs/user-guide/cli.md +51 -0
- package/docs/user-guide/config.md +47 -0
- package/docs/user-guide/decorators/README.md +33 -0
- package/docs/user-guide/decorators/api-body.md +47 -0
- package/docs/user-guide/decorators/api-operation.md +47 -0
- package/docs/user-guide/decorators/api-param.md +47 -0
- package/docs/user-guide/decorators/api-property.md +46 -0
- package/docs/user-guide/decorators/api-query.md +47 -0
- package/docs/user-guide/decorators/api-response.md +48 -0
- package/docs/user-guide/decorators/api-tags.md +47 -0
- package/docs/user-guide/decorators/controllable.md +46 -0
- package/docs/user-guide/decorators/guard.md +57 -0
- package/docs/user-guide/decorators/injection.md +55 -0
- package/docs/user-guide/decorators/middleware.md +56 -0
- package/docs/user-guide/decorators/parameters.md +57 -0
- package/docs/user-guide/decorators/route-interaction.md +47 -0
- package/docs/user-guide/decorators/routes.md +62 -0
- package/docs/user-guide/decorators/socket.md +59 -0
- package/docs/user-guide/encryption.md +40 -0
- package/docs/user-guide/env.md +39 -0
- package/docs/user-guide/getting-started.md +72 -0
- package/docs/user-guide/injection.md +80 -0
- package/docs/user-guide/middleware.md +98 -0
- package/docs/user-guide/openapi/README.md +70 -0
- package/docs/user-guide/openapi/plugin.md +72 -0
- package/docs/user-guide/plugins.md +69 -0
- package/docs/user-guide/responses.md +99 -0
- package/docs/user-guide/runtime.md +44 -0
- package/docs/user-guide/security.md +46 -0
- package/docs/user-guide/sockets.md +49 -0
- package/docs/user-guide/tutorial.md +175 -0
- package/docs/user-guide/types.md +118 -0
- package/package.json +1 -1
- package/source/commands/build.command.ts +2 -2
- package/source/core/builder.ts +15 -14
- package/source/core/controller/builder.ts +14 -6
- package/source/core/socket/builder.ts +5 -0
- package/source/core/socket/index.ts +1 -0
- package/source/core/socket/metadata.ts +54 -1
- package/source/core/thread.ts +4 -1
- package/source/framework/artifacts.ts +2 -12
- package/source/framework/constants/decorators.constant.ts +1 -0
- package/source/framework/decorators/api-response.decorator.ts +4 -5
- package/source/framework/decorators/api.decorator.ts +36 -20
- package/source/framework/decorators/socket.decorator.ts +2 -1
- package/source/framework/runtime/bun/server.ts +100 -3
- package/source/framework/runtime/index.ts +4 -3
- package/source/framework/utilities/openapi.utils.ts +154 -109
- package/source/types/raiton.ts +2 -2
- package/source/types/runtime.ts +5 -1
- package/docs/framework/README.md +0 -96
- /package/docs/{cli → contributors/cli}/commands.md +0 -0
- /package/docs/{core → contributors/core}/application.md +0 -0
- /package/docs/{core → contributors/core}/config.md +0 -0
- /package/docs/{core → contributors/core}/injection.md +0 -0
- /package/docs/{core → contributors/core}/middleware.md +0 -0
- /package/docs/{core → contributors/core}/plugin-scope.md +0 -0
- /package/docs/{core → contributors/core}/router.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/README.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/controllable.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/injection.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/middleware.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/openapi.md +0 -0
- /package/docs/{framework → contributors/framework}/decorators/parametrable.md +0 -0
- /package/docs/{framework → contributors/framework}/encryption.md +0 -0
- /package/docs/{framework → contributors/framework}/env.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/README.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/body-parser.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/openapi.md +0 -0
- /package/docs/{framework → contributors/framework}/plugins/security.md +0 -0
- /package/docs/{framework → contributors/framework}/responses.md +0 -0
- /package/docs/{framework → contributors/framework}/runtime.md +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Sockets
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← openapi](openapi/README.md) | [tutorial →](tutorial.md)
|
|
4
|
+
|
|
5
|
+
Les sockets servent à créer des échanges temps réel.
|
|
6
|
+
|
|
7
|
+
## Exemple
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { Socket, OnSocketConnect, OnSocketMessage } from "raiton/framework"
|
|
11
|
+
|
|
12
|
+
@Socket("/chat")
|
|
13
|
+
export class ChatSocket {
|
|
14
|
+
@OnSocketConnect()
|
|
15
|
+
connect() {
|
|
16
|
+
return { connected: true }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@OnSocketMessage("message")
|
|
20
|
+
message(payload: any) {
|
|
21
|
+
return { ok: true, payload }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Pourquoi utilisé
|
|
27
|
+
|
|
28
|
+
- notifications temps réel
|
|
29
|
+
- chat
|
|
30
|
+
- synchronisation instantanée
|
|
31
|
+
|
|
32
|
+
## Comment l’utiliser
|
|
33
|
+
|
|
34
|
+
- déclarez une classe socket
|
|
35
|
+
- ajoutez des handlers avec `@OnSocketConnect()` et `@OnSocketMessage()`
|
|
36
|
+
|
|
37
|
+
## Avantages
|
|
38
|
+
|
|
39
|
+
- intégration naturelle avec le runtime Bun
|
|
40
|
+
- API simple à déclarer
|
|
41
|
+
|
|
42
|
+
## Inconvénients
|
|
43
|
+
|
|
44
|
+
- support plus limité que le HTTP
|
|
45
|
+
- dépend du runtime choisi
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
[← openapi](openapi/README.md) | [tutorial →](tutorial.md)
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Tutoriel complet
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← responses](responses.md) | [plugins →](plugins.md)
|
|
4
|
+
|
|
5
|
+
Ce tutoriel assemble les briques du guide dans un flux concret:
|
|
6
|
+
- configuration du projet
|
|
7
|
+
- bootstrap du serveur
|
|
8
|
+
- contrôleur
|
|
9
|
+
- service
|
|
10
|
+
- middleware
|
|
11
|
+
- réponse structurée
|
|
12
|
+
|
|
13
|
+
## 1. Configuration du projet
|
|
14
|
+
|
|
15
|
+
Créez un fichier `raiton.config.ts` à la racine:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
export default {
|
|
19
|
+
rootDir: "./source",
|
|
20
|
+
version: "0.10.0",
|
|
21
|
+
port: 5711,
|
|
22
|
+
pathname: "/"
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 2. Point d’entrée
|
|
27
|
+
|
|
28
|
+
Dans `source/main.ts`:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import "reflect-metadata"
|
|
32
|
+
import { ThreadInterface } from "raiton/types"
|
|
33
|
+
import { Application } from "raiton/core"
|
|
34
|
+
import { RuntimeType } from "raiton/framework"
|
|
35
|
+
import { UsersController } from "./controllers/users.controller"
|
|
36
|
+
|
|
37
|
+
export default async (thread: ThreadInterface) => {
|
|
38
|
+
const app = new Application({
|
|
39
|
+
port: 5711,
|
|
40
|
+
prefix: "/api"
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
app.get("/users", () => ({
|
|
44
|
+
statusCode: 200,
|
|
45
|
+
error: false,
|
|
46
|
+
message: "Users retrieved",
|
|
47
|
+
data: []
|
|
48
|
+
}))
|
|
49
|
+
|
|
50
|
+
return await thread.setup({
|
|
51
|
+
application: app,
|
|
52
|
+
runtime: RuntimeType.Bun
|
|
53
|
+
}).run()
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## 3. Service métier
|
|
58
|
+
|
|
59
|
+
Dans `source/services/users.service.ts`:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { Injectable } from "raiton/framework"
|
|
63
|
+
|
|
64
|
+
@Injectable()
|
|
65
|
+
export class UsersService {
|
|
66
|
+
private readonly users = [
|
|
67
|
+
{ id: "1", name: "John Doe" },
|
|
68
|
+
{ id: "2", name: "Jane Doe" }
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
findAll() {
|
|
72
|
+
return this.users
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
findById(id: string) {
|
|
76
|
+
return this.users.find((user) => user.id === id) ?? null
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## 4. Middleware
|
|
82
|
+
|
|
83
|
+
Dans `source/middleware/auth.middleware.ts`:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
export async function authMiddleware({ context, next }) {
|
|
87
|
+
const token = context.req.headers.get("authorization")
|
|
88
|
+
|
|
89
|
+
if (!token) {
|
|
90
|
+
context.reply.status(401)
|
|
91
|
+
return context.reply.send({
|
|
92
|
+
statusCode: 401,
|
|
93
|
+
error: true,
|
|
94
|
+
message: "Unauthorized",
|
|
95
|
+
data: null
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
context.state.user = { id: "1", role: "admin" }
|
|
100
|
+
await next()
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## 5. Contrôleur
|
|
105
|
+
|
|
106
|
+
Dans `source/controllers/users.controller.ts`:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { Controllable, Get, Inject, Param, Middleware } from "raiton/framework"
|
|
110
|
+
import { UsersService } from "../services/users.service"
|
|
111
|
+
import { authMiddleware } from "../middleware/auth.middleware"
|
|
112
|
+
|
|
113
|
+
@Controllable("/users")
|
|
114
|
+
@Middleware(authMiddleware)
|
|
115
|
+
export class UsersController {
|
|
116
|
+
constructor(
|
|
117
|
+
@Inject(UsersService) private readonly usersService: UsersService
|
|
118
|
+
) {}
|
|
119
|
+
|
|
120
|
+
@Get("/")
|
|
121
|
+
list() {
|
|
122
|
+
return {
|
|
123
|
+
statusCode: 200,
|
|
124
|
+
error: false,
|
|
125
|
+
message: "Users retrieved",
|
|
126
|
+
data: this.usersService.findAll()
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
@Get("/:id")
|
|
131
|
+
getOne(@Param("id") id: string) {
|
|
132
|
+
const user = this.usersService.findById(id)
|
|
133
|
+
|
|
134
|
+
if (!user) {
|
|
135
|
+
return {
|
|
136
|
+
statusCode: 404,
|
|
137
|
+
error: true,
|
|
138
|
+
message: "Utilisateur introuvable",
|
|
139
|
+
data: null
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
statusCode: 200,
|
|
145
|
+
error: false,
|
|
146
|
+
message: "User retrieved",
|
|
147
|
+
data: user
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## 6. Démarrer le projet
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
bun raiton dev
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## 7. Résultat attendu
|
|
160
|
+
|
|
161
|
+
- `GET /api/users` renvoie la liste des utilisateurs
|
|
162
|
+
- `GET /api/users/:id` renvoie un utilisateur ou une erreur 404
|
|
163
|
+
- les routes du contrôleur sont protégées par un middleware d’authentification
|
|
164
|
+
|
|
165
|
+
## Ce que montre ce flux
|
|
166
|
+
|
|
167
|
+
- `source/main.ts` initialise le serveur
|
|
168
|
+
- `UsersService` contient la logique métier
|
|
169
|
+
- `UsersController` expose l’API
|
|
170
|
+
- `authMiddleware` applique une règle transversale
|
|
171
|
+
- les réponses restent standardisées
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
[← responses](responses.md) | [plugins →](plugins.md)
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# Types
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← sockets](sockets.md) | [tutorial →](tutorial.md)
|
|
4
|
+
|
|
5
|
+
Le package `raiton/types` regroupe les interfaces qui servent à typer le bootstrap, les contrôleurs, le contexte de requête, les middlewares et les plugins.
|
|
6
|
+
|
|
7
|
+
Cette page ne liste pas tous les types internes du framework. Elle met en avant ceux que vous utiliserez le plus souvent pour construire un serveur d’API typé.
|
|
8
|
+
|
|
9
|
+
## Pourquoi utilisé
|
|
10
|
+
|
|
11
|
+
- éviter les objets `any` dans le point d’entrée et les couches métier
|
|
12
|
+
- rendre les signatures explicites
|
|
13
|
+
- faciliter l’autocomplétion dans l’éditeur
|
|
14
|
+
- sécuriser les contrats entre `source/main.ts`, les middlewares et les plugins
|
|
15
|
+
|
|
16
|
+
## Types principaux
|
|
17
|
+
|
|
18
|
+
- `ThreadInterface` : orchestration du serveur et démarrage de l’application
|
|
19
|
+
- `ApplicationInterface` : instance applicative configurée avec `port`, `prefix` et les routes
|
|
20
|
+
- `ContextInterface` : contexte de requête disponible dans les handlers et certains middlewares
|
|
21
|
+
- `MiddlewareParametersInterface` : contrat d’entrée d’un middleware fonctionnel
|
|
22
|
+
- `PluginInterface` : forme minimale d’un plugin Raiton
|
|
23
|
+
- `RouteHandlerCallable` : signature d’un handler de route
|
|
24
|
+
|
|
25
|
+
## Exemple d’usage
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Application } from "raiton/core"
|
|
29
|
+
import { RuntimeType } from "raiton/framework"
|
|
30
|
+
import type {
|
|
31
|
+
ThreadInterface,
|
|
32
|
+
ContextInterface,
|
|
33
|
+
MiddlewareParametersInterface,
|
|
34
|
+
PluginInterface
|
|
35
|
+
} from "raiton/types"
|
|
36
|
+
|
|
37
|
+
const loggerPlugin: PluginInterface = {
|
|
38
|
+
name: "logger",
|
|
39
|
+
setup(scope) {
|
|
40
|
+
scope.app.use(async ({ context, next }) => {
|
|
41
|
+
console.log(`${context.req.method} ${context.req.url}`)
|
|
42
|
+
await next()
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export default async (thread: ThreadInterface) => {
|
|
48
|
+
const app = new Application({
|
|
49
|
+
port: 5711,
|
|
50
|
+
prefix: "/api"
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
app.register(loggerPlugin)
|
|
54
|
+
|
|
55
|
+
app.get("/health", (ctx: ContextInterface) => {
|
|
56
|
+
ctx.reply.status(200)
|
|
57
|
+
return {
|
|
58
|
+
statusCode: 200,
|
|
59
|
+
error: false,
|
|
60
|
+
message: "OK",
|
|
61
|
+
data: null
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const authMiddleware = async ({ context, next }: MiddlewareParametersInterface) => {
|
|
66
|
+
const token = context.req.headers.get("authorization")
|
|
67
|
+
|
|
68
|
+
if (!token) {
|
|
69
|
+
context.reply.status(401)
|
|
70
|
+
return context.reply.send({
|
|
71
|
+
statusCode: 401,
|
|
72
|
+
error: true,
|
|
73
|
+
message: "Unauthorized",
|
|
74
|
+
data: null
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
await next()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
app.use(authMiddleware)
|
|
82
|
+
|
|
83
|
+
return await thread.setup({
|
|
84
|
+
application: app,
|
|
85
|
+
runtime: RuntimeType.Bun
|
|
86
|
+
}).run()
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Comment l’utiliser
|
|
91
|
+
|
|
92
|
+
- importez les types avec `import type` quand ils ne servent qu’à la compilation
|
|
93
|
+
- utilisez `ThreadInterface` dans `source/main.ts` pour typer le bootstrap
|
|
94
|
+
- utilisez `ContextInterface` dans les handlers si vous voulez typer explicitement le contexte
|
|
95
|
+
- utilisez `MiddlewareParametersInterface` pour écrire des middlewares fonctionnels
|
|
96
|
+
- utilisez `PluginInterface` pour décrire un plugin réutilisable
|
|
97
|
+
|
|
98
|
+
## Avantages
|
|
99
|
+
|
|
100
|
+
- code plus lisible
|
|
101
|
+
- moins d’ambiguïté sur les objets manipulés
|
|
102
|
+
- meilleure détection d’erreurs au moment du développement
|
|
103
|
+
- intégration naturelle avec TypeScript
|
|
104
|
+
|
|
105
|
+
## Inconvénients
|
|
106
|
+
|
|
107
|
+
- exposition de plusieurs interfaces peut sembler plus verbeuse au début
|
|
108
|
+
- certains types sont internes et ne sont utiles qu’à des cas avancés
|
|
109
|
+
- un typage explicite demande un peu plus de discipline dans les signatures
|
|
110
|
+
|
|
111
|
+
## À retenir
|
|
112
|
+
|
|
113
|
+
Si vous ne devez retenir que quelques types, partez de `ThreadInterface`, `ApplicationInterface`, `ContextInterface`, `MiddlewareParametersInterface` et `PluginInterface`.
|
|
114
|
+
Ils couvrent la plupart des points d’entrée d’une application Raiton.
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
[← sockets](sockets.md) | [tutorial →](tutorial.md)
|
package/package.json
CHANGED
|
@@ -22,11 +22,11 @@ export default class BuildCommand extends RaitonCommand {
|
|
|
22
22
|
|
|
23
23
|
const builder = new RaitonBuilder(this.workdir, {
|
|
24
24
|
serve: options.develop,
|
|
25
|
-
hmr:
|
|
25
|
+
hmr: options.develop
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
await builder.prepare()
|
|
29
29
|
await builder.boot()
|
|
30
30
|
// await builder.start(async () => (options.develop || options.bootstrap) ? await builder.boot() : void (0))
|
|
31
31
|
}
|
|
32
|
-
}
|
|
32
|
+
}
|
package/source/core/builder.ts
CHANGED
|
@@ -5,7 +5,7 @@ import fs, {WatchEventType} from "node:fs";
|
|
|
5
5
|
import type {BuilderConfigInterface, BuilderInterface, ThreadInterface,} from "../types";
|
|
6
6
|
import {RaitonThread} from "./thread";
|
|
7
7
|
import {Raiton} from "./raiton";
|
|
8
|
-
import {isControllerArtifact} from "../framework";
|
|
8
|
+
import {isControllerArtifact, isSocketArtifact} from "../framework";
|
|
9
9
|
import {ControllerBuilder} from "./controller";
|
|
10
10
|
import {watch} from "fs";
|
|
11
11
|
import {LBadge, Logger} from "@protorians/logger";
|
|
@@ -60,17 +60,10 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
60
60
|
type
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
Logger.log(LBadge.info('HMR'), 'activated');
|
|
63
|
+
// Logger.log(LBadge.info('HMR'), 'activated');
|
|
64
64
|
|
|
65
|
-
if (isControllerArtifact(filename))
|
|
66
|
-
Raiton.signals.dispatch('hmr:
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (Artifacts.is(filename))
|
|
70
|
-
Artifacts.reload(
|
|
71
|
-
await import(`${filename}?v=${payload.version || 1}&t=${payload.timestamp || Date.now()}`),
|
|
72
|
-
filename
|
|
73
|
-
)
|
|
65
|
+
if (Artifacts.is(filename) || isControllerArtifact(filename) || isSocketArtifact(filename))
|
|
66
|
+
Raiton.signals.dispatch('hmr:artifact', payload)
|
|
74
67
|
|
|
75
68
|
return payload;
|
|
76
69
|
}
|
|
@@ -120,9 +113,17 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
120
113
|
|
|
121
114
|
if (this.options.hmr && this.options.serve) {
|
|
122
115
|
Raiton.signals.listen(
|
|
123
|
-
'hmr:
|
|
116
|
+
'hmr:artifact',
|
|
124
117
|
async ({filename, version, timestamp}) => {
|
|
125
|
-
await
|
|
118
|
+
const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
|
|
119
|
+
|
|
120
|
+
if (isControllerArtifact(filename) || isSocketArtifact(filename)) {
|
|
121
|
+
await ControllerBuilder.build({filename, version, timestamp})
|
|
122
|
+
} else if (Artifacts.is(filename)) {
|
|
123
|
+
Artifacts.reload(imported, filename)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
Logger.log(LBadge.debug('HMR'), (path.relative(this.workdir, filename)))
|
|
126
127
|
}
|
|
127
128
|
)
|
|
128
129
|
// this.parsing();
|
|
@@ -170,4 +171,4 @@ export class RaitonBuilder implements BuilderInterface {
|
|
|
170
171
|
return app;
|
|
171
172
|
}
|
|
172
173
|
|
|
173
|
-
}
|
|
174
|
+
}
|
|
@@ -2,9 +2,10 @@ import fs from "node:fs";
|
|
|
2
2
|
import {BuilderHMRDeclarationInterface} from "../../types";
|
|
3
3
|
import {LBadge, Logger} from "@protorians/logger";
|
|
4
4
|
import {compileController} from "./compiler";
|
|
5
|
+
import {compileSocket} from "../socket/builder";
|
|
5
6
|
import {RaitonThread} from "../thread";
|
|
6
7
|
import {Injection} from "../injection";
|
|
7
|
-
import {isControllerArtifact} from "../../framework";
|
|
8
|
+
import {isControllerArtifact, isSocketArtifact} from "../../framework";
|
|
8
9
|
import path from "node:path";
|
|
9
10
|
|
|
10
11
|
export class ControllerBuilder {
|
|
@@ -22,19 +23,26 @@ export class ControllerBuilder {
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
static async build<T>({filename, version, timestamp}: BuilderHMRDeclarationInterface): Promise<T | undefined> {
|
|
25
|
-
|
|
26
|
+
const isController = isControllerArtifact(filename);
|
|
27
|
+
const isSocket = isSocketArtifact(filename);
|
|
28
|
+
|
|
29
|
+
if (!isController && !isSocket)
|
|
26
30
|
return undefined;
|
|
27
31
|
|
|
28
32
|
const imported = await import(`${filename}?v=${version || 1}&t=${timestamp || Date.now()}`)
|
|
29
|
-
const
|
|
33
|
+
const artifact = imported.default || imported || undefined;
|
|
30
34
|
|
|
31
|
-
if (!
|
|
35
|
+
if (!artifact) return undefined;
|
|
32
36
|
if (!RaitonThread.current?.application) return undefined;
|
|
33
37
|
|
|
34
|
-
const
|
|
35
|
-
const name = controller.name || (typeof controller === 'function' ? controller.name : undefined);
|
|
38
|
+
const name = artifact.name || (typeof artifact === 'function' ? artifact.name : undefined);
|
|
36
39
|
if (name) Injection.registerArtifactPath(name, filename);
|
|
37
40
|
|
|
41
|
+
if (isSocket) {
|
|
42
|
+
return compileSocket(artifact) as any;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const compilated = compileController(artifact, RaitonThread.current.application);
|
|
38
46
|
return compilated;
|
|
39
47
|
}
|
|
40
48
|
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
import {SocketMetaInterface} from "../../types";
|
|
1
|
+
import {ConstructorType, SocketMetaInterface} from "../../types";
|
|
2
2
|
import {METADATA_KEYS} from "../../framework";
|
|
3
3
|
import "reflect-metadata";
|
|
4
4
|
|
|
5
|
+
export interface SocketRegistryEntryInterface {
|
|
6
|
+
namespace: string;
|
|
7
|
+
construct: ConstructorType;
|
|
8
|
+
metadata: SocketMetaInterface;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const registry = new Map<string, SocketRegistryEntryInterface>();
|
|
12
|
+
|
|
5
13
|
export function getSocketMetadata(target: any): SocketMetaInterface {
|
|
6
14
|
let metadata = Reflect.getMetadata(METADATA_KEYS.SOCKETS, target);
|
|
7
15
|
if (!metadata) {
|
|
@@ -11,3 +19,48 @@ export function getSocketMetadata(target: any): SocketMetaInterface {
|
|
|
11
19
|
|
|
12
20
|
return metadata;
|
|
13
21
|
}
|
|
22
|
+
|
|
23
|
+
export function registerSocket(construct: any): SocketRegistryEntryInterface {
|
|
24
|
+
const metadata = getSocketMetadata(construct.prototype || construct);
|
|
25
|
+
const namespace = metadata.namespace || '/';
|
|
26
|
+
const entry: SocketRegistryEntryInterface = {namespace, construct, metadata};
|
|
27
|
+
registry.set(namespace, entry);
|
|
28
|
+
return entry;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getSocketRegistry(): Map<string, SocketRegistryEntryInterface> {
|
|
32
|
+
return registry;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getSocketForNamespace(namespace: string): SocketRegistryEntryInterface | undefined {
|
|
36
|
+
return registry.get(normalizePath(namespace));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function findSocketForPath(pathname: string, prefix?: string): SocketRegistryEntryInterface | undefined {
|
|
40
|
+
const normalized = normalizePath(pathname);
|
|
41
|
+
|
|
42
|
+
if (registry.has(normalized)) return registry.get(normalized);
|
|
43
|
+
|
|
44
|
+
if (prefix && prefix !== '/') {
|
|
45
|
+
const withoutPrefix = normalized.startsWith(prefix)
|
|
46
|
+
? (normalized.substring(prefix.length) || '/')
|
|
47
|
+
: null;
|
|
48
|
+
if (withoutPrefix && registry.has(withoutPrefix)) return registry.get(withoutPrefix);
|
|
49
|
+
|
|
50
|
+
const withPrefix = `${prefix}${normalized}`;
|
|
51
|
+
if (registry.has(withPrefix)) return registry.get(withPrefix);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function clearSocketRegistry(): void {
|
|
58
|
+
registry.clear();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function normalizePath(pathname: string): string {
|
|
62
|
+
if (pathname.length > 1 && pathname.endsWith('/')) {
|
|
63
|
+
return pathname.slice(0, -1);
|
|
64
|
+
}
|
|
65
|
+
return pathname;
|
|
66
|
+
}
|
package/source/core/thread.ts
CHANGED
|
@@ -103,7 +103,10 @@ export class RaitonThread implements ThreadInterface {
|
|
|
103
103
|
const displayHostname = (hostname === '0.0.0.0') ? (this.getNetworkIp() || 'localhost') : hostname;
|
|
104
104
|
const prefix = this.application.config.prefix
|
|
105
105
|
|
|
106
|
-
this.runtimeServer = this.runtime.createServer(
|
|
106
|
+
this.runtimeServer = this.runtime.createServer(
|
|
107
|
+
this.application.handle.bind(this.application),
|
|
108
|
+
{prefix}
|
|
109
|
+
)
|
|
107
110
|
|
|
108
111
|
await this.runtimeServer.listen(port, hostname)
|
|
109
112
|
|
|
@@ -2,7 +2,7 @@ import {Injection} from "../core/injection";
|
|
|
2
2
|
import {Logger} from "@protorians/logger";
|
|
3
3
|
import type {ConstructorType} from "../types";
|
|
4
4
|
import {Raiton} from "../core/raiton";
|
|
5
|
-
import {isArtifact
|
|
5
|
+
import {isArtifact} from "./utilities";
|
|
6
6
|
|
|
7
7
|
export class Artifacts {
|
|
8
8
|
|
|
@@ -34,6 +34,7 @@ export class Artifacts {
|
|
|
34
34
|
'entity',
|
|
35
35
|
'model',
|
|
36
36
|
'schema',
|
|
37
|
+
'socket',
|
|
37
38
|
]
|
|
38
39
|
|
|
39
40
|
static register(type: string) {
|
|
@@ -68,17 +69,6 @@ export class Artifacts {
|
|
|
68
69
|
if (filename) Injection.registerArtifactPath(name, filename);
|
|
69
70
|
Injection.updateConstruct(name, mod as ConstructorType)
|
|
70
71
|
|
|
71
|
-
const dependents = Injection.getDependents(name);
|
|
72
|
-
for (const dependent of dependents) {
|
|
73
|
-
const dependentPath = Injection.getArtifactPath(dependent);
|
|
74
|
-
if (dependentPath && isControllerArtifact(dependentPath)) {
|
|
75
|
-
Raiton.signals.dispatch('hmr:controller', {
|
|
76
|
-
filename: dependentPath,
|
|
77
|
-
timestamp: Date.now(),
|
|
78
|
-
version: 1
|
|
79
|
-
})
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
72
|
}
|
|
83
73
|
}
|
|
84
74
|
|
|
@@ -15,6 +15,7 @@ export const METADATA_KEYS = {
|
|
|
15
15
|
API_TAGS: Symbol('api:tags'),
|
|
16
16
|
API_OPERATION: Symbol('api:operation'),
|
|
17
17
|
API_SECURITY: Symbol('api:security'),
|
|
18
|
+
API_PROPERTY: Symbol('api:property'),
|
|
18
19
|
// Route interaction metadata key
|
|
19
20
|
ROUTE_INTERACTION: Symbol('route:interaction'),
|
|
20
21
|
}
|
|
@@ -32,11 +32,10 @@ export interface ApiResponseOptions {
|
|
|
32
32
|
*/
|
|
33
33
|
export function ApiResponse(options: ApiResponseOptions) {
|
|
34
34
|
return function (target: any, propertyKey: string | symbol) {
|
|
35
|
-
const responses = Reflect.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
responses[propertyKey].push(options);
|
|
35
|
+
const responses = Reflect.getOwnMetadata(METADATA_KEYS.API_RESPONSES, target) || {};
|
|
36
|
+
const methodResponses = responses[propertyKey] ? [...responses[propertyKey]] : [];
|
|
37
|
+
methodResponses.push(options);
|
|
38
|
+
responses[propertyKey] = methodResponses;
|
|
40
39
|
Reflect.defineMetadata(METADATA_KEYS.API_RESPONSES, responses, target);
|
|
41
40
|
};
|
|
42
41
|
}
|