raiton 6.3.1-beta.11 → 6.4.0-beta.12
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 +11 -0
- package/build/bin/index.mjs +3 -2
- package/docs/user-guide/README.md +15 -12
- package/docs/user-guide/https.md +337 -0
- package/docs/user-guide/security.md +2 -2
- package/package.json +1 -1
- package/source/core/application.ts +24 -3
- package/source/core/thread.ts +19 -3
- package/source/framework/artifacts.ts +1 -0
- package/source/framework/runtime/bun/server.ts +13 -2
- package/source/framework/runtime/node/server.ts +19 -4
- package/source/framework/utilities/https.ts +298 -0
- package/source/framework/utilities/index.ts +2 -1
- package/source/framework/utilities/openapi.utils.ts +31 -13
- package/source/types/application.ts +4 -0
- package/source/types/runtime.ts +8 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
## [6.4.0-beta.12](https://github.com/protorians/raiton/compare/v6.3.1-beta.11...v6.4.0-beta.12) (2026-09-02)
|
|
2
|
+
|
|
3
|
+
### Features
|
|
4
|
+
|
|
5
|
+
* feat: add HTTPS configuration with certificate management, auto-generation, and runtime support ([0e9aacb](https://github.com/protorians/raiton/commit/0e9aacb74a37db61a3b57dcf0a12cf64f8ae61cb))
|
|
6
|
+
|
|
7
|
+
### Bug Fixes
|
|
8
|
+
|
|
9
|
+
* refactor: improve HTTPS certificate generation and refine routing configuration handling ([8206e6f](https://github.com/protorians/raiton/commit/8206e6f2e98208c49d0ecdb55bb06d93d623191f))
|
|
10
|
+
* refactor: include `mcp` in artifact registration list ([7ccc5b9](https://github.com/protorians/raiton/commit/7ccc5b9d8fa3d01ec0ee2d3a86c5f985e5ca752f))
|
|
11
|
+
|
|
1
12
|
## [6.3.1-beta.11](https://github.com/protorians/raiton/compare/v6.3.0-beta.10...v6.3.1-beta.11) (2026-08-30)
|
|
2
13
|
|
|
3
14
|
### Bug Fixes
|
package/build/bin/index.mjs
CHANGED
|
@@ -4819,7 +4819,7 @@ var {
|
|
|
4819
4819
|
Help
|
|
4820
4820
|
} = import__.default;
|
|
4821
4821
|
// package.json
|
|
4822
|
-
var version = "6.
|
|
4822
|
+
var version = "6.4.0-beta.12";
|
|
4823
4823
|
|
|
4824
4824
|
// source/bin/cli.ts
|
|
4825
4825
|
var CLI = new Command;
|
|
@@ -6426,7 +6426,8 @@ class Artifacts {
|
|
|
6426
6426
|
"entity",
|
|
6427
6427
|
"model",
|
|
6428
6428
|
"schema",
|
|
6429
|
-
"socket"
|
|
6429
|
+
"socket",
|
|
6430
|
+
"mcp"
|
|
6430
6431
|
];
|
|
6431
6432
|
static register(type) {
|
|
6432
6433
|
this.types.add(type);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Guide Développeur
|
|
2
2
|
|
|
3
|
-
Ce dossier décrit comment utiliser Raiton pour créer un serveur d
|
|
3
|
+
Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
4
4
|
|
|
5
5
|
## Menu
|
|
6
6
|
|
|
@@ -12,20 +12,21 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d’API.
|
|
|
12
12
|
| [api/README.md](api/README.md) | Déclarer les routes, paramètres et réponses |
|
|
13
13
|
| [decorators/README.md](decorators/README.md) | Utiliser les décorateurs de contrôleur, paramètre, injection et OpenAPI |
|
|
14
14
|
| [runtime.md](runtime.md) | Choisir et configurer le runtime |
|
|
15
|
-
| [env.md](env.md) | Lire les variables d
|
|
15
|
+
| [env.md](env.md) | Lire les variables d'environnement |
|
|
16
16
|
| [encryption.md](encryption.md) | Hasher et vérifier des secrets |
|
|
17
17
|
| [injection.md](injection.md) | Injecter des services et séparer la logique métier |
|
|
18
18
|
| [middleware.md](middleware.md) | Exécuter des règles transverses sur les requêtes |
|
|
19
19
|
| [responses.md](responses.md) | Structurer les réponses et les erreurs |
|
|
20
20
|
| [body-parser.md](body-parser.md) | Lire JSON, formulaires et fichiers |
|
|
21
21
|
| [security.md](security.md) | Sécurité HTTP, CORS, rate-limit, body-limit |
|
|
22
|
-
| [
|
|
22
|
+
| [https.md](https.md) | HTTPS, certificats et environnements |
|
|
23
|
+
| [openapi/README.md](openapi/README.md) | Générer et documenter l'API |
|
|
23
24
|
| [sockets.md](sockets.md) | Déclarer et gérer les sockets |
|
|
24
25
|
| [tutorial.md](tutorial.md) | Construire une mini API complète de bout en bout |
|
|
25
|
-
| [plugins.md](plugins.md) | Vue d
|
|
26
|
+
| [plugins.md](plugins.md) | Vue d'ensemble des plugins intégrés |
|
|
26
27
|
| [types.md](types.md) | Comprendre les types utiles au développeur |
|
|
27
28
|
| [cli.md](cli.md) | Flux de développement, build et start |
|
|
28
|
-
| [best-practices.md](best-practices.md) | Recommandations d
|
|
29
|
+
| [best-practices.md](best-practices.md) | Recommandations d'organisation |
|
|
29
30
|
|
|
30
31
|
## Parcours recommandé
|
|
31
32
|
|
|
@@ -42,12 +43,13 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d’API.
|
|
|
42
43
|
11. lire [responses.md](responses.md)
|
|
43
44
|
12. lire [body-parser.md](body-parser.md)
|
|
44
45
|
13. lire [security.md](security.md)
|
|
45
|
-
14. lire [
|
|
46
|
-
15. lire [
|
|
47
|
-
16. lire [
|
|
48
|
-
17. lire [
|
|
49
|
-
18. lire [
|
|
50
|
-
19. lire [
|
|
46
|
+
14. lire [https.md](https.md)
|
|
47
|
+
15. lire [openapi/README.md](openapi/README.md)
|
|
48
|
+
16. lire [sockets.md](sockets.md)
|
|
49
|
+
17. lire [types.md](types.md)
|
|
50
|
+
18. lire [tutorial.md](tutorial.md)
|
|
51
|
+
19. lire [plugins.md](plugins.md)
|
|
52
|
+
20. lire [cli.md](cli.md)
|
|
51
53
|
|
|
52
54
|
## Ce que couvre ce guide
|
|
53
55
|
|
|
@@ -55,8 +57,9 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d’API.
|
|
|
55
57
|
- la configuration du projet via `raiton.config.ts`
|
|
56
58
|
- une organisation modulaire par domaine
|
|
57
59
|
- les routes, contrôleurs et réponses
|
|
58
|
-
- l
|
|
60
|
+
- l'injection de services
|
|
59
61
|
- les plugins de sécurité et de documentation
|
|
62
|
+
- le protocole HTTPS et la gestion des certificats
|
|
60
63
|
- le cycle local `dev` / `build` / `start`
|
|
61
64
|
|
|
62
65
|
> **Navigation :** [← docs/](../README.md) | [← contributeurs](../contributors/README.md)
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
# HTTPS
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← security](security.md) | [openapi →](openapi/README.md)
|
|
4
|
+
|
|
5
|
+
Le module HTTPS permet d'activer le protocole TLS sur le serveur Raiton.
|
|
6
|
+
Il prend en charge l'auto-génération de certificats (openssl ou mkcert), le cumul de configurations et la gestion des environnements dev/prod.
|
|
7
|
+
|
|
8
|
+
## Exemple rapide
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
import "reflect-metadata"
|
|
12
|
+
import { ThreadInterface } from "raiton/types"
|
|
13
|
+
import { Application } from "raiton/core"
|
|
14
|
+
import { RuntimeType } from "raiton/framework"
|
|
15
|
+
import { defineHttps } from "raiton/framework"
|
|
16
|
+
|
|
17
|
+
export default async (thread: ThreadInterface) => {
|
|
18
|
+
const app = new Application({
|
|
19
|
+
port: 5711,
|
|
20
|
+
prefix: "/api",
|
|
21
|
+
https: defineHttps()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return await thread.setup({
|
|
25
|
+
application: app,
|
|
26
|
+
runtime: RuntimeType.Bun
|
|
27
|
+
}).run()
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`defineHttps()` sans argument active HTTPS en mode `localhost` avec un certificat auto-signé via openssl.
|
|
32
|
+
Les certificats sont générés dans `.raiton/certs/` et réutilisés entre les relances.
|
|
33
|
+
|
|
34
|
+
## Cumuler des configurations
|
|
35
|
+
|
|
36
|
+
Le champ `https` accepte un tableau de configurations qui sont fusionnées de gauche à droite.
|
|
37
|
+
La dernière configuration non-undefined l'emporte.
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { defineHttps, httpsLan } from "raiton/framework"
|
|
41
|
+
|
|
42
|
+
const app = new Application({
|
|
43
|
+
port: 5711,
|
|
44
|
+
https: [
|
|
45
|
+
defineHttps({ port: 5711 }),
|
|
46
|
+
httpsLan(),
|
|
47
|
+
]
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Cela est utile pour superposer des configs :
|
|
52
|
+
|
|
53
|
+
| Cas | Syntaxe |
|
|
54
|
+
|---|---|
|
|
55
|
+
| Config de base + override LAN | `[defineHttps(), httpsLan()]` |
|
|
56
|
+
| Config de base + certificats custom | `[defineHttps(), defineHttps({cert, key})]` |
|
|
57
|
+
| Dev avec mkcert, prod avec openssl | Conditional array |
|
|
58
|
+
|
|
59
|
+
La fonction `mergeHttps()` est aussi disponible si vous voulez fusionner manuellement :
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { defineHttps, httpsLan, mergeHttps } from "raiton/framework"
|
|
63
|
+
|
|
64
|
+
const config = mergeHttps(
|
|
65
|
+
defineHttps(),
|
|
66
|
+
httpsLan(),
|
|
67
|
+
{ enabled: true, environment: 'localhost', generator: 'openssl', autoGenerate: true }
|
|
68
|
+
)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Les 3 environnements
|
|
72
|
+
|
|
73
|
+
| Environnement | Usage | Certificat |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| `localhost` | Développement local | Auto-signé, généré automatiquement |
|
|
76
|
+
| `lan` | Accès depuis un autre appareil du réseau local | Auto-signé avec l'IP LAN comme domaine |
|
|
77
|
+
| `production` | Serveur en ligne | Doit être fourni explicitement |
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { defineHttps, HttpsEnvironment } from "raiton/framework"
|
|
81
|
+
|
|
82
|
+
const dev = defineHttps({ environment: HttpsEnvironment.Localhost })
|
|
83
|
+
const lan = defineHttps({ environment: HttpsEnvironment.LAN })
|
|
84
|
+
const prod = defineHttps({
|
|
85
|
+
environment: HttpsEnvironment.Production,
|
|
86
|
+
certPath: "/etc/ssl/certs/server.pem",
|
|
87
|
+
keyPath: "/etc/ssl/private/server.key",
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Les 3 presets
|
|
92
|
+
|
|
93
|
+
Des raccourcis pour les cas courants :
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
import { httpsLocalhost, httpsLan, httpsProduction } from "raiton/framework"
|
|
97
|
+
|
|
98
|
+
httpsLocalhost()
|
|
99
|
+
httpsLan()
|
|
100
|
+
|
|
101
|
+
httpsProduction({
|
|
102
|
+
cert: fs.readFileSync("./cert.pem", "utf-8"),
|
|
103
|
+
key: fs.readFileSync("./key.pem", "utf-8"),
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Générateur de certificats : openssl ou mkcert
|
|
108
|
+
|
|
109
|
+
Par défaut, Raiton utilise `openssl`. Vous pouvez basculer sur `mkcert` pour des certificats approuvés par le navigateur.
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { defineHttps, HttpsGenerator } from "raiton/framework"
|
|
113
|
+
|
|
114
|
+
// Avec openssl (défaut)
|
|
115
|
+
defineHttps({ generator: HttpsGenerator.OpenSSL })
|
|
116
|
+
|
|
117
|
+
// Avec mkcert (certificats de confiance locale)
|
|
118
|
+
defineHttps({ generator: HttpsGenerator.Mkcert })
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### openssl vs mkcert
|
|
122
|
+
|
|
123
|
+
| | openssl | mkcert |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| Installation | Inclus dans la plupart des OS | `brew install mkcert` / `choco install mkcert` |
|
|
126
|
+
| Certificat de confiance | Non (avertissement navigateur) | Oui (racine locale installée) |
|
|
127
|
+
| Usage | Dev rapide, CI/CD | Dev local, démo clients |
|
|
128
|
+
|
|
129
|
+
### mkcert : certificats de confiance
|
|
130
|
+
|
|
131
|
+
`mkcert` installe une autorité de certification locale. Les certificats générés sont approuvés par Chrome, Firefox, Safari, etc. sans avertissement.
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
# Installation
|
|
135
|
+
brew install mkcert # macOS
|
|
136
|
+
choco install mkcert # Windows
|
|
137
|
+
# ou https://github.com/FiloSottile/mkcert
|
|
138
|
+
|
|
139
|
+
# mkcert installe automatiquement sa CA locale
|
|
140
|
+
mkcert -install
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// Raiton utilise mkcert automatiquement
|
|
145
|
+
const app = new Application({
|
|
146
|
+
port: 5711,
|
|
147
|
+
https: defineHttps({ generator: HttpsGenerator.Mkcert })
|
|
148
|
+
})
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Contrôle de l'auto-génération
|
|
152
|
+
|
|
153
|
+
Le paramètre `autoGenerate` permet de désactiver la génération automatique de certificats.
|
|
154
|
+
Utile si vous voulez que le serveur démarre uniquement avec des certificats fournis.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Ne génère rien, échoue si aucun certificat n'est fourni
|
|
158
|
+
defineHttps({
|
|
159
|
+
autoGenerate: false,
|
|
160
|
+
certPath: "./certs/server.pem",
|
|
161
|
+
keyPath: "./certs/server.key",
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
// Génère via mkcert (échoue si mkcert n'est pas installé)
|
|
165
|
+
defineHttps({
|
|
166
|
+
autoGenerate: true,
|
|
167
|
+
generator: HttpsGenerator.Mkcert,
|
|
168
|
+
})
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
| autoGenerate | generator | Comportement |
|
|
172
|
+
|---|---|---|
|
|
173
|
+
| `true` (défaut) | `openssl` | Génère via openssl si aucun certificat fourni |
|
|
174
|
+
| `true` (défaut) | `mkcert` | Génère via mkcert si aucun certificat fourni |
|
|
175
|
+
| `false` | `*` | Ne génère rien, erreur si pas de certificat |
|
|
176
|
+
| `true` | `none` | Erreur : pas de générateur sélectionné |
|
|
177
|
+
|
|
178
|
+
## Fournir des certificats
|
|
179
|
+
|
|
180
|
+
### Auto-signé (dev/lan)
|
|
181
|
+
|
|
182
|
+
Aucune configuration nécessaire. Raiton génère le certificat et le stocke dans `.raiton/certs/`.
|
|
183
|
+
|
|
184
|
+
### Par contenu (string)
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
defineHttps({
|
|
188
|
+
cert: process.env.TLS_CERT!,
|
|
189
|
+
key: process.env.TLS_KEY!,
|
|
190
|
+
ca: process.env.TLS_CA,
|
|
191
|
+
})
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Par chemin de fichier
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
defineHttps({
|
|
198
|
+
certPath: "./certs/server.pem",
|
|
199
|
+
keyPath: "./certs/server.key",
|
|
200
|
+
caPath: "./certs/ca.pem",
|
|
201
|
+
})
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Pattern `.env` pour switch dev/prod
|
|
205
|
+
|
|
206
|
+
```env
|
|
207
|
+
# .env
|
|
208
|
+
HTTPS_ENABLED=true
|
|
209
|
+
HTTPS_CERT_PATH=./certs/fullchain.pem
|
|
210
|
+
HTTPS_KEY_PATH=./certs/privkey.pem
|
|
211
|
+
HTTPS_GENERATOR=mkcert
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
import { defineHttps, HttpsGenerator } from "raiton/framework"
|
|
216
|
+
|
|
217
|
+
const https = process.env.HTTPS_ENABLED === "true"
|
|
218
|
+
? defineHttps({
|
|
219
|
+
certPath: process.env.HTTPS_CERT_PATH,
|
|
220
|
+
keyPath: process.env.HTTPS_KEY_PATH,
|
|
221
|
+
generator: process.env.HTTPS_GENERATOR === "mkcert"
|
|
222
|
+
? HttpsGenerator.Mkcert
|
|
223
|
+
: HttpsGenerator.OpenSSL,
|
|
224
|
+
})
|
|
225
|
+
: undefined
|
|
226
|
+
|
|
227
|
+
const app = new Application({
|
|
228
|
+
port: 5711,
|
|
229
|
+
https,
|
|
230
|
+
})
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Bootstrap complet
|
|
234
|
+
|
|
235
|
+
```typescript
|
|
236
|
+
import "reflect-metadata"
|
|
237
|
+
import { ThreadInterface } from "raiton/types"
|
|
238
|
+
import { Application } from "raiton/core"
|
|
239
|
+
import { RuntimeType, Security } from "raiton/framework"
|
|
240
|
+
import { defineHttps } from "raiton/framework"
|
|
241
|
+
|
|
242
|
+
export default async (thread: ThreadInterface) => {
|
|
243
|
+
const app = new Application({
|
|
244
|
+
port: 5711,
|
|
245
|
+
hostname: "0.0.0.0",
|
|
246
|
+
prefix: "/api",
|
|
247
|
+
https: [
|
|
248
|
+
defineHttps(),
|
|
249
|
+
{ environment: 'lan', autoGenerate: true },
|
|
250
|
+
],
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
app.register(Security.cors({ origin: "*" }))
|
|
254
|
+
|
|
255
|
+
return await thread.setup({
|
|
256
|
+
application: app,
|
|
257
|
+
runtime: RuntimeType.Bun
|
|
258
|
+
}).run()
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Lors du démarrage, les logs affichent :
|
|
263
|
+
|
|
264
|
+
```
|
|
265
|
+
[info] Local access: https://localhost:5711/api
|
|
266
|
+
[info] LAN access: https://192.168.1.42:5711/api
|
|
267
|
+
[info] HTTPS: enabled (lan)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Compatibilité Bun / Node
|
|
271
|
+
|
|
272
|
+
Le support HTTPS fonctionne avec les deux runtimes principaux :
|
|
273
|
+
|
|
274
|
+
- **Bun** — utilise `Bun.serve({ tls: { cert, key } })`
|
|
275
|
+
- **Node** — utilise `https.createServer({ cert, key })`
|
|
276
|
+
|
|
277
|
+
```typescript
|
|
278
|
+
// Bun
|
|
279
|
+
return await thread.setup({ application: app, runtime: RuntimeType.Bun }).run()
|
|
280
|
+
|
|
281
|
+
// Node
|
|
282
|
+
return await thread.setup({ application: app, runtime: RuntimeType.Node }).run()
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Référence API
|
|
286
|
+
|
|
287
|
+
### `defineHttps(options?)`
|
|
288
|
+
|
|
289
|
+
| Paramètre | Type | Défaut | Description |
|
|
290
|
+
|---|---|---|---|
|
|
291
|
+
| `environment` | `HttpsEnvironment` | `localhost` | `localhost`, `lan`, ou `production` |
|
|
292
|
+
| `generator` | `HttpsGenerator` | `openssl` | `openssl`, `mkcert`, ou `none` |
|
|
293
|
+
| `autoGenerate` | `boolean` | `true` | Générer les certificats automatiquement |
|
|
294
|
+
| `cert` | `string` | — | Contenu PEM du certificat |
|
|
295
|
+
| `key` | `string` | — | Contenu PEM de la clé privée |
|
|
296
|
+
| `ca` | `string` | — | Contenu PEM du certificat CA |
|
|
297
|
+
| `certPath` | `string` | — | Chemin vers le fichier certificat |
|
|
298
|
+
| `keyPath` | `string` | — | Chemin vers le fichier clé |
|
|
299
|
+
| `caPath` | `string` | — | Chemin vers le fichier CA |
|
|
300
|
+
| `certDir` | `string` | `.raiton/certs/` | Dossier de stockage des certificats générés |
|
|
301
|
+
| `port` | `number` | — | Port HTTPS (optionnel) |
|
|
302
|
+
|
|
303
|
+
### `mergeHttps(...configs)`
|
|
304
|
+
|
|
305
|
+
Fusionne plusieurs `HttpsConfigInterface` de gauche à droite. La dernière valeur définie l'emporte.
|
|
306
|
+
|
|
307
|
+
### `resolveHttpsConfig(input)`
|
|
308
|
+
|
|
309
|
+
Résout `HttpsConfigInterface | HttpsConfigInterface[]` en une seule config. Utile pour normaliser le champ `https` de l'application.
|
|
310
|
+
|
|
311
|
+
## Pourquoi utilisé
|
|
312
|
+
|
|
313
|
+
- sécuriser les échanges entre client et serveur
|
|
314
|
+
- respecter les best practices même en développement
|
|
315
|
+
- tester en HTTPS localement avant la mise en production
|
|
316
|
+
- accès LAN pour tester sur appareils physiques
|
|
317
|
+
- mkcert pour des certificats de confiance sans avertissement
|
|
318
|
+
|
|
319
|
+
## Avantages
|
|
320
|
+
|
|
321
|
+
- une seule ligne pour activer en dev (`defineHttps()`)
|
|
322
|
+
- cumul de configurations via tableau
|
|
323
|
+
- choix entre openssl et mkcert
|
|
324
|
+
- auto-génération désactivable
|
|
325
|
+
- compatible Bun et Node
|
|
326
|
+
- granulaire : la configuration peut varier selon l'environnement
|
|
327
|
+
|
|
328
|
+
## Inconvénients
|
|
329
|
+
|
|
330
|
+
- `openssl` ou `mkcert` doit être installé pour la génération automatique
|
|
331
|
+
- en production, les certificats doivent être fournis explicitement
|
|
332
|
+
- les certificats auto-signés (openssl) génèrent un avertissement dans le navigateur
|
|
333
|
+
- mkcert nécessite `mkcert -install` pour installer la CA locale
|
|
334
|
+
|
|
335
|
+
---
|
|
336
|
+
|
|
337
|
+
[← security](security.md) | [openapi →](openapi/README.md)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Sécurité
|
|
2
2
|
|
|
3
|
-
> **Navigation :** [← body-parser](body-parser.md) | [
|
|
3
|
+
> **Navigation :** [← body-parser](body-parser.md) | [https →](https.md)
|
|
4
4
|
|
|
5
5
|
Les plugins de sécurité servent à protéger une API exposée publiquement.
|
|
6
6
|
|
|
@@ -43,4 +43,4 @@ app.register({
|
|
|
43
43
|
|
|
44
44
|
---
|
|
45
45
|
|
|
46
|
-
[← body-parser](body-parser.md) | [
|
|
46
|
+
[← body-parser](body-parser.md) | [https →](https.md)
|
package/package.json
CHANGED
|
@@ -8,9 +8,11 @@ import {Logger} from "@protorians/logger";
|
|
|
8
8
|
import {RaitonConfig} from "./config";
|
|
9
9
|
import {Artifacts} from "../framework/artifacts";
|
|
10
10
|
import {Injection} from "./injection";
|
|
11
|
+
import {HttpsConfigInterface, resolveHttpsConfig} from "../framework/utilities/https";
|
|
11
12
|
|
|
12
13
|
export class Application implements ApplicationInterface {
|
|
13
14
|
private root: PluginScope
|
|
15
|
+
private _resolvedHttps: HttpsConfigInterface | undefined
|
|
14
16
|
|
|
15
17
|
readonly version: string = RaitonConfig.get('version') || '0.0.1'
|
|
16
18
|
|
|
@@ -22,6 +24,7 @@ export class Application implements ApplicationInterface {
|
|
|
22
24
|
readonly config: ApplicationConfigInterface
|
|
23
25
|
) {
|
|
24
26
|
this.root = new PluginScope()
|
|
27
|
+
this._resolvedHttps = resolveHttpsConfig(this.config.https)
|
|
25
28
|
if (this.config.workdir) {
|
|
26
29
|
process.chdir(this.config.workdir)
|
|
27
30
|
}
|
|
@@ -41,8 +44,12 @@ export class Application implements ApplicationInterface {
|
|
|
41
44
|
}
|
|
42
45
|
|
|
43
46
|
public get hostname(): string {
|
|
47
|
+
const protocol = this._resolvedHttps?.enabled
|
|
48
|
+
? 'https'
|
|
49
|
+
: (this.config.protocole || 'http')
|
|
50
|
+
|
|
44
51
|
return `${
|
|
45
|
-
|
|
52
|
+
protocol
|
|
46
53
|
}://${
|
|
47
54
|
this.config.hostname || 'localhost'
|
|
48
55
|
}${
|
|
@@ -52,6 +59,10 @@ export class Application implements ApplicationInterface {
|
|
|
52
59
|
}`
|
|
53
60
|
}
|
|
54
61
|
|
|
62
|
+
public get https(): HttpsConfigInterface | undefined {
|
|
63
|
+
return this._resolvedHttps
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
public setOption<K extends keyof ApplicationConfigInterface>(key: K, value: ApplicationConfigInterface[K]): this {
|
|
56
67
|
this.config[key] = value;
|
|
57
68
|
return this;
|
|
@@ -73,8 +84,7 @@ export class Application implements ApplicationInterface {
|
|
|
73
84
|
}
|
|
74
85
|
|
|
75
86
|
route(method: HttpMethod, path: string, handler: RouteHandlerCallable, version?: string): this {
|
|
76
|
-
const
|
|
77
|
-
const fullPath = `${prefix}${path}`.replace(/\/+/g, '/') || '/'
|
|
87
|
+
const fullPath = path.replace(/\/+/g, '/') || '/'
|
|
78
88
|
this.root.route(method, fullPath, handler, version)
|
|
79
89
|
return this
|
|
80
90
|
}
|
|
@@ -130,6 +140,17 @@ export class Application implements ApplicationInterface {
|
|
|
130
140
|
const url = new URL(req.url, this.hostname)
|
|
131
141
|
let pathname = url.pathname
|
|
132
142
|
|
|
143
|
+
if (this.config.prefix) {
|
|
144
|
+
const prefix = this.config.prefix.endsWith('/') ? this.config.prefix.slice(0, -1) : this.config.prefix
|
|
145
|
+
if (pathname === prefix) {
|
|
146
|
+
pathname = '/'
|
|
147
|
+
} else if (pathname.startsWith(prefix + '/')) {
|
|
148
|
+
pathname = pathname.substring(prefix.length) || '/'
|
|
149
|
+
} else if (this.config.verbose) {
|
|
150
|
+
Logger.warn(`Request out of application prefix: ${pathname} (expected: ${prefix}/*)`)
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
133
154
|
if (this.config.pathname && this.config.pathname !== '/') {
|
|
134
155
|
const appPathname = this.config.pathname.endsWith('/') ? this.config.pathname : `${this.config.pathname}/`
|
|
135
156
|
if (pathname.startsWith(appPathname)) {
|
package/source/core/thread.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
BuilderInterface,
|
|
3
3
|
RuntimeAdapterInterface,
|
|
4
4
|
RuntimeServerInterface,
|
|
5
|
+
RuntimeServerHttpsInterface,
|
|
5
6
|
ThreadInterface,
|
|
6
7
|
ThreadOptionsInterface,
|
|
7
8
|
ThreadSetupOptionsInterface,
|
|
@@ -102,16 +103,31 @@ export class RaitonThread implements ThreadInterface {
|
|
|
102
103
|
const hostname = this.application.config.hostname || '0.0.0.0';
|
|
103
104
|
const displayHostname = (hostname === '0.0.0.0') ? (this.getNetworkIp() || 'localhost') : hostname;
|
|
104
105
|
const prefix = this.application.config.prefix
|
|
106
|
+
const httpsConfig = this.application.https
|
|
107
|
+
|
|
108
|
+
const httpsServerOptions: RuntimeServerHttpsInterface | undefined = httpsConfig?.enabled
|
|
109
|
+
? {
|
|
110
|
+
enabled: true,
|
|
111
|
+
environment: httpsConfig.environment,
|
|
112
|
+
certificate: httpsConfig.certificate!,
|
|
113
|
+
}
|
|
114
|
+
: undefined
|
|
105
115
|
|
|
106
116
|
this.runtimeServer = this.runtime.createServer(
|
|
107
117
|
this.application.handle.bind(this.application),
|
|
108
|
-
{prefix}
|
|
118
|
+
{prefix, https: httpsServerOptions}
|
|
109
119
|
)
|
|
110
120
|
|
|
121
|
+
const protocol = httpsConfig?.enabled ? 'https' : 'http'
|
|
122
|
+
|
|
111
123
|
await this.runtimeServer.listen(port, hostname)
|
|
112
124
|
|
|
113
|
-
Logger.log(LBadge.info('Local access:'),
|
|
114
|
-
Logger.log(LBadge.info('LAN access:'),
|
|
125
|
+
Logger.log(LBadge.info('Local access:'), `${protocol}://localhost:${port}${prefix ?? ''}`,)
|
|
126
|
+
Logger.log(LBadge.info('LAN access:'), `${protocol}://${displayHostname}:${port}${prefix ?? ''}`,)
|
|
127
|
+
|
|
128
|
+
if (httpsConfig?.enabled) {
|
|
129
|
+
Logger.log(LBadge.info('HTTPS:'), `enabled (${httpsConfig.environment ?? 'custom'})`,)
|
|
130
|
+
}
|
|
115
131
|
}
|
|
116
132
|
|
|
117
133
|
return this;
|
|
@@ -88,7 +88,7 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
88
88
|
|
|
89
89
|
return {
|
|
90
90
|
async listen(port, hostname) {
|
|
91
|
-
|
|
91
|
+
const serveOptions: any = {
|
|
92
92
|
port,
|
|
93
93
|
hostname,
|
|
94
94
|
websocket: socketHandlers,
|
|
@@ -170,7 +170,18 @@ export const bunRuntime: RuntimeAdapterInterface = {
|
|
|
170
170
|
{status: statusCode, headers}
|
|
171
171
|
)
|
|
172
172
|
}
|
|
173
|
-
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (options?.https?.enabled && options.https.certificate) {
|
|
176
|
+
serveOptions.tls = {
|
|
177
|
+
cert: options.https.certificate.cert,
|
|
178
|
+
key: options.https.certificate.key,
|
|
179
|
+
ca: options.https.certificate.ca,
|
|
180
|
+
passphrase: options.https.certificate.passphrase,
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
server = Bun.serve(serveOptions)
|
|
174
185
|
},
|
|
175
186
|
async close() {
|
|
176
187
|
server?.stop()
|
|
@@ -1,20 +1,35 @@
|
|
|
1
1
|
import http from 'node:http'
|
|
2
|
+
import https from 'node:https'
|
|
2
3
|
import {
|
|
3
4
|
RuntimeAdapterInterface,
|
|
4
5
|
RuntimeRequestInterface,
|
|
5
|
-
RuntimeReplyInterface
|
|
6
|
+
RuntimeReplyInterface,
|
|
7
|
+
RuntimeServerOptionsInterface
|
|
6
8
|
} from '../../../types'
|
|
7
9
|
import { NodeRequest } from './request'
|
|
8
10
|
import { NodeReply } from './reply'
|
|
9
11
|
|
|
10
12
|
export const nodeRuntime: RuntimeAdapterInterface = {
|
|
11
|
-
createServer(handler) {
|
|
12
|
-
const
|
|
13
|
+
createServer(handler, options?: RuntimeServerOptionsInterface) {
|
|
14
|
+
const requestHandler = async (req: any, res: any) => {
|
|
13
15
|
const runtimeReq: any = new NodeRequest(req)
|
|
14
16
|
const runtimeReply: RuntimeReplyInterface = new NodeReply(res)
|
|
15
17
|
|
|
16
18
|
await handler(runtimeReq, runtimeReply)
|
|
17
|
-
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
let server: http.Server | https.Server
|
|
22
|
+
|
|
23
|
+
if (options?.https?.enabled && options.https.certificate) {
|
|
24
|
+
server = https.createServer({
|
|
25
|
+
cert: options.https.certificate.cert,
|
|
26
|
+
key: options.https.certificate.key,
|
|
27
|
+
ca: options.https.certificate.ca,
|
|
28
|
+
passphrase: options.https.certificate.passphrase,
|
|
29
|
+
}, requestHandler as any)
|
|
30
|
+
} else {
|
|
31
|
+
server = http.createServer(requestHandler as any)
|
|
32
|
+
}
|
|
18
33
|
|
|
19
34
|
return {
|
|
20
35
|
listen(port, hostname) {
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import os from 'node:os'
|
|
3
|
+
import {execSync, execFileSync} from 'node:child_process'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
|
|
6
|
+
export enum HttpsEnvironment {
|
|
7
|
+
Localhost = 'localhost',
|
|
8
|
+
LAN = 'lan',
|
|
9
|
+
Production = 'production',
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export enum HttpsGenerator {
|
|
13
|
+
OpenSSL = 'openssl',
|
|
14
|
+
Mkcert = 'mkcert',
|
|
15
|
+
None = 'none',
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface HttpsCertificateConfig {
|
|
19
|
+
cert: string
|
|
20
|
+
key: string
|
|
21
|
+
ca?: string
|
|
22
|
+
passphrase?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface HttpsConfigInterface {
|
|
26
|
+
enabled: boolean
|
|
27
|
+
environment: HttpsEnvironment
|
|
28
|
+
generator: HttpsGenerator
|
|
29
|
+
autoGenerate: boolean
|
|
30
|
+
port?: number
|
|
31
|
+
certificate?: HttpsCertificateConfig
|
|
32
|
+
certDir?: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface HttpsDefinitionOptions {
|
|
36
|
+
environment?: HttpsEnvironment
|
|
37
|
+
generator?: HttpsGenerator
|
|
38
|
+
autoGenerate?: boolean
|
|
39
|
+
port?: number
|
|
40
|
+
cert?: string
|
|
41
|
+
key?: string
|
|
42
|
+
ca?: string
|
|
43
|
+
passphrase?: string
|
|
44
|
+
certPath?: string
|
|
45
|
+
keyPath?: string
|
|
46
|
+
caPath?: string
|
|
47
|
+
certDir?: string
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ── Helpers ────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
function ensureDirectorySync(dirPath: string): void {
|
|
53
|
+
if (!fs.existsSync(dirPath)) {
|
|
54
|
+
fs.mkdirSync(dirPath, {recursive: true})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readFileSyncSafe(filePath: string): string | undefined {
|
|
59
|
+
try {
|
|
60
|
+
return fs.readFileSync(filePath, 'utf-8')
|
|
61
|
+
} catch {
|
|
62
|
+
return undefined
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function resolveCertFromPaths(
|
|
67
|
+
certPath?: string,
|
|
68
|
+
keyPath?: string,
|
|
69
|
+
caPath?: string,
|
|
70
|
+
): HttpsCertificateConfig | undefined {
|
|
71
|
+
if (!certPath || !keyPath) return undefined
|
|
72
|
+
const cert = readFileSyncSafe(certPath)
|
|
73
|
+
const key = readFileSyncSafe(keyPath)
|
|
74
|
+
const ca = caPath ? readFileSyncSafe(caPath) : undefined
|
|
75
|
+
if (!cert || !key) return undefined
|
|
76
|
+
return {cert, key, ca}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getLanHostname(): string {
|
|
80
|
+
const interfaces = os.networkInterfaces()
|
|
81
|
+
for (const name of Object.keys(interfaces)) {
|
|
82
|
+
for (const iface of interfaces[name] || []) {
|
|
83
|
+
if (iface.family === 'IPv4' && !iface.internal) {
|
|
84
|
+
return iface.address
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return 'localhost'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isCommandAvailable(command: string): boolean {
|
|
92
|
+
try {
|
|
93
|
+
execSync(
|
|
94
|
+
process.platform === 'win32' ? `where ${command}` : `which ${command}`,
|
|
95
|
+
{stdio: 'pipe'}
|
|
96
|
+
)
|
|
97
|
+
return true
|
|
98
|
+
} catch {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ── Certificate generators ────────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
function generateWithOpenSSL(domain: string, certDir: string): HttpsCertificateConfig {
|
|
106
|
+
const keyPath = path.join(certDir, `${domain}.key`)
|
|
107
|
+
const certPath = path.join(certDir, `${domain}.pem`)
|
|
108
|
+
|
|
109
|
+
const existingKey = readFileSyncSafe(keyPath)
|
|
110
|
+
const existingCert = readFileSyncSafe(certPath)
|
|
111
|
+
if (existingKey && existingCert) {
|
|
112
|
+
return {key: existingKey, cert: existingCert}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
ensureDirectorySync(certDir)
|
|
116
|
+
|
|
117
|
+
const sanHosts = new Set<string>([domain])
|
|
118
|
+
sanHosts.add('localhost')
|
|
119
|
+
sanHosts.add('*.localhost')
|
|
120
|
+
|
|
121
|
+
const sans = [
|
|
122
|
+
...[...sanHosts].map(d => `DNS:${d}`),
|
|
123
|
+
'IP:127.0.0.1',
|
|
124
|
+
'IP:::1',
|
|
125
|
+
].join(',')
|
|
126
|
+
|
|
127
|
+
execSync(
|
|
128
|
+
`openssl req -x509 -newkey rsa:2048 -nodes ` +
|
|
129
|
+
`-keyout "${keyPath}" -out "${certPath}" ` +
|
|
130
|
+
`-days 365 -subj "/CN=${domain}/O=Raiton Dev/C=US" ` +
|
|
131
|
+
`-addext "subjectAltName=${sans}"`,
|
|
132
|
+
{stdio: 'pipe'}
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
return {key: fs.readFileSync(keyPath, 'utf-8'), cert: fs.readFileSync(certPath, 'utf-8')}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function generateWithMkcert(domain: string, certDir: string): HttpsCertificateConfig {
|
|
139
|
+
const keyPath = path.join(certDir, `${domain}.key`)
|
|
140
|
+
const certPath = path.join(certDir, `${domain}.pem`)
|
|
141
|
+
|
|
142
|
+
const existingKey = readFileSyncSafe(keyPath)
|
|
143
|
+
const existingCert = readFileSyncSafe(certPath)
|
|
144
|
+
if (existingKey && existingCert) {
|
|
145
|
+
return {key: existingKey, cert: existingCert}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
ensureDirectorySync(certDir)
|
|
149
|
+
|
|
150
|
+
const hosts = new Set<string>([domain])
|
|
151
|
+
hosts.add('localhost')
|
|
152
|
+
hosts.add('127.0.0.1')
|
|
153
|
+
|
|
154
|
+
execSync(
|
|
155
|
+
`mkcert -cert-file "${certPath}" -key-file "${keyPath}" ` +
|
|
156
|
+
`${[...hosts].map(h => `"${h}"`).join(' ')}`,
|
|
157
|
+
{stdio: 'pipe'}
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
return {key: fs.readFileSync(keyPath, 'utf-8'), cert: fs.readFileSync(certPath, 'utf-8')}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function autoGenerateCert(
|
|
164
|
+
domain: string,
|
|
165
|
+
generator: HttpsGenerator,
|
|
166
|
+
certDir: string,
|
|
167
|
+
): HttpsCertificateConfig {
|
|
168
|
+
if (generator === HttpsGenerator.Mkcert) {
|
|
169
|
+
if (!isCommandAvailable('mkcert')) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
'[Raiton HTTPS] mkcert is not installed. ' +
|
|
172
|
+
'Install it (https://github.com/FiloSottile/mkcert) or switch generator to HttpsGenerator.OpenSSL.'
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
return generateWithMkcert(domain, certDir)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (generator === HttpsGenerator.OpenSSL) {
|
|
179
|
+
if (!isCommandAvailable('openssl')) {
|
|
180
|
+
throw new Error(
|
|
181
|
+
'[Raiton HTTPS] openssl is not installed. ' +
|
|
182
|
+
'Install it or switch generator to HttpsGenerator.Mkcert.'
|
|
183
|
+
)
|
|
184
|
+
}
|
|
185
|
+
return generateWithOpenSSL(domain, certDir)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
throw new Error(
|
|
189
|
+
'[Raiton HTTPS] autoGenerate is enabled but generator is set to "none". ' +
|
|
190
|
+
'Set generator to HttpsGenerator.OpenSSL or HttpsGenerator.Mkcert, or provide certificates explicitly.'
|
|
191
|
+
)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ── Core API ──────────────────────────────────────────────────────
|
|
195
|
+
|
|
196
|
+
export function defineHttps(options: HttpsDefinitionOptions = {}): HttpsConfigInterface {
|
|
197
|
+
const environment = options.environment ?? HttpsEnvironment.Localhost
|
|
198
|
+
const generator = options.generator ?? HttpsGenerator.OpenSSL
|
|
199
|
+
const autoGenerate = options.autoGenerate ?? true
|
|
200
|
+
const certDir = options.certDir ?? path.join(process.cwd(), '.raiton', 'certs')
|
|
201
|
+
|
|
202
|
+
let certificate: HttpsCertificateConfig | undefined
|
|
203
|
+
|
|
204
|
+
if (options.cert && options.key) {
|
|
205
|
+
certificate = {
|
|
206
|
+
cert: options.cert,
|
|
207
|
+
key: options.key,
|
|
208
|
+
ca: options.ca,
|
|
209
|
+
passphrase: options.passphrase,
|
|
210
|
+
}
|
|
211
|
+
} else if (options.certPath && options.keyPath) {
|
|
212
|
+
certificate = resolveCertFromPaths(options.certPath, options.keyPath, options.caPath)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!certificate) {
|
|
216
|
+
if (environment === HttpsEnvironment.Production && !autoGenerate) {
|
|
217
|
+
throw new Error(
|
|
218
|
+
'[Raiton HTTPS] Production environment requires explicit certificate configuration. ' +
|
|
219
|
+
'Use cert/key strings, certPath/keyPath, or enable autoGenerate.'
|
|
220
|
+
)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (autoGenerate && generator !== HttpsGenerator.None) {
|
|
224
|
+
const domain = environment === HttpsEnvironment.LAN ? getLanHostname() : 'localhost'
|
|
225
|
+
certificate = autoGenerateCert(domain, generator, certDir)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
enabled: true,
|
|
231
|
+
environment,
|
|
232
|
+
generator,
|
|
233
|
+
autoGenerate,
|
|
234
|
+
port: options.port,
|
|
235
|
+
certificate,
|
|
236
|
+
certDir,
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ── Presets ───────────────────────────────────────────────────────
|
|
241
|
+
|
|
242
|
+
export function httpsLocalhost(options?: Omit<HttpsDefinitionOptions, 'environment'>): HttpsConfigInterface {
|
|
243
|
+
return defineHttps({...options, environment: HttpsEnvironment.Localhost})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export function httpsLan(options?: Omit<HttpsDefinitionOptions, 'environment'>): HttpsConfigInterface {
|
|
247
|
+
return defineHttps({...options, environment: HttpsEnvironment.LAN})
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function httpsProduction(options: Omit<HttpsDefinitionOptions, 'environment'> & {
|
|
251
|
+
cert: string
|
|
252
|
+
key: string
|
|
253
|
+
}): HttpsConfigInterface {
|
|
254
|
+
return defineHttps({...options, environment: HttpsEnvironment.Production})
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ── Merge ─────────────────────────────────────────────────────────
|
|
258
|
+
|
|
259
|
+
export type HttpsConfigInput = HttpsConfigInterface | HttpsConfigInterface[]
|
|
260
|
+
|
|
261
|
+
export function resolveHttpsConfig(input: HttpsConfigInput | undefined): HttpsConfigInterface | undefined {
|
|
262
|
+
if (!input) return undefined
|
|
263
|
+
|
|
264
|
+
const configs = Array.isArray(input) ? input : [input]
|
|
265
|
+
|
|
266
|
+
if (configs.length === 0) return undefined
|
|
267
|
+
if (configs.length === 1) return configs[0]
|
|
268
|
+
|
|
269
|
+
return mergeHttps(...configs)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export function mergeHttps(...configs: HttpsConfigInterface[]): HttpsConfigInterface {
|
|
273
|
+
if (configs.length === 0) {
|
|
274
|
+
throw new Error('[Raiton HTTPS] mergeHttps requires at least one configuration.')
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
if (configs.length === 1) return configs[0]
|
|
278
|
+
|
|
279
|
+
const merged: HttpsConfigInterface = {
|
|
280
|
+
...configs[0],
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
for (let i = 1; i < configs.length; i++) {
|
|
284
|
+
const next = configs[i]
|
|
285
|
+
if (!next) continue
|
|
286
|
+
|
|
287
|
+
merged.enabled = next.enabled
|
|
288
|
+
merged.environment = next.environment
|
|
289
|
+
merged.generator = next.generator
|
|
290
|
+
merged.autoGenerate = next.autoGenerate
|
|
291
|
+
|
|
292
|
+
if (next.port !== undefined) merged.port = next.port
|
|
293
|
+
if (next.certDir !== undefined) merged.certDir = next.certDir
|
|
294
|
+
if (next.certificate) merged.certificate = next.certificate
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return merged
|
|
298
|
+
}
|
|
@@ -9,17 +9,32 @@ import {RaitonThread} from "../../core/thread";
|
|
|
9
9
|
* Generate the OpenAPI specification object
|
|
10
10
|
*/
|
|
11
11
|
export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promise<any> {
|
|
12
|
-
//
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
// Use application config directly (includes HTTPS, port, hostname)
|
|
13
|
+
const appConfig = RaitonThread.current?.application?.config;
|
|
14
|
+
const appHttps = RaitonThread.current?.application?.https;
|
|
15
|
+
|
|
16
|
+
let protocole: string;
|
|
17
|
+
let hostname: string;
|
|
18
|
+
let port: number | undefined;
|
|
19
|
+
let pathname: string;
|
|
20
|
+
let prefix: string;
|
|
21
|
+
|
|
22
|
+
if (appConfig) {
|
|
23
|
+
protocole = appHttps?.enabled ? 'https' : (appConfig.protocole || 'http');
|
|
24
|
+
hostname = appConfig.hostname || 'localhost';
|
|
25
|
+
port = appConfig.port;
|
|
26
|
+
pathname = appConfig.pathname || '/';
|
|
27
|
+
prefix = appConfig.prefix || '';
|
|
28
|
+
} else {
|
|
29
|
+
// Fallback to RaitonConfig
|
|
30
|
+
await RaitonConfig.sync(process.cwd());
|
|
31
|
+
const config: Map<keyof any, any> = RaitonConfig.current as any;
|
|
32
|
+
protocole = (config.get('protocole') as string) ?? 'http';
|
|
33
|
+
hostname = (config.get('hostname') as string) ?? 'localhost';
|
|
34
|
+
port = config.get('port') as number;
|
|
35
|
+
pathname = (config.get('pathname') as string) ?? '/';
|
|
36
|
+
prefix = (config.get('prefix') as string) ?? '';
|
|
37
|
+
}
|
|
23
38
|
const basePath = pathname.endsWith('/') ? pathname : pathname + '/';
|
|
24
39
|
|
|
25
40
|
let baseUrl = `${protocole}://${hostname}`;
|
|
@@ -27,6 +42,10 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
27
42
|
baseUrl += `:${port}`;
|
|
28
43
|
}
|
|
29
44
|
baseUrl += basePath;
|
|
45
|
+
if (prefix) {
|
|
46
|
+
baseUrl += prefix.startsWith('/') ? prefix.slice(1) : prefix;
|
|
47
|
+
if (!baseUrl.endsWith('/')) baseUrl += '/';
|
|
48
|
+
}
|
|
30
49
|
|
|
31
50
|
// Initialize OpenAPI document
|
|
32
51
|
const spec: any = {
|
|
@@ -308,7 +327,6 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
308
327
|
}
|
|
309
328
|
|
|
310
329
|
// Document registered WebSocket sockets as POST operations (one per event)
|
|
311
|
-
const prefix = RaitonThread.current?.application?.config?.prefix ?? '';
|
|
312
330
|
const sockets = getSocketRegistry();
|
|
313
331
|
|
|
314
332
|
for (const [, socketEntry] of sockets) {
|
|
@@ -320,7 +338,7 @@ export async function generateOpenApiSpec(scope: PluginScope, opts: any): Promis
|
|
|
320
338
|
if (event.type !== 'event' && event.type !== 'message') continue;
|
|
321
339
|
if (!event.name) continue;
|
|
322
340
|
|
|
323
|
-
const channelPath = `${
|
|
341
|
+
const channelPath = `${namespace}`.replace(/\/+/g, '/') || '/';
|
|
324
342
|
const eventPath = `${channelPath}/${event.name}`.replace(/\/+/g, '/');
|
|
325
343
|
|
|
326
344
|
if (!spec.paths[eventPath]) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {HttpMethod} from "../framework";
|
|
2
|
+
import {HttpsConfigInput, HttpsConfigInterface} from "../framework/utilities/https";
|
|
2
3
|
|
|
3
4
|
export interface ApplicationConfigInterface {
|
|
4
5
|
workdir?: string;
|
|
@@ -9,6 +10,7 @@ export interface ApplicationConfigInterface {
|
|
|
9
10
|
prefix?: string;
|
|
10
11
|
develop?: boolean;
|
|
11
12
|
verbose?: boolean;
|
|
13
|
+
https?: HttpsConfigInput;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export interface ApplicationInterface {
|
|
@@ -16,6 +18,8 @@ export interface ApplicationInterface {
|
|
|
16
18
|
|
|
17
19
|
get hostname(): string;
|
|
18
20
|
|
|
21
|
+
get https(): HttpsConfigInterface | undefined;
|
|
22
|
+
|
|
19
23
|
setOption<K extends keyof ApplicationConfigInterface>(key: K, value: ApplicationConfigInterface[K]): this;
|
|
20
24
|
|
|
21
25
|
setOptions(options: ApplicationConfigInterface): this;
|
package/source/types/runtime.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {RuntimeType} from "../framework/enums/runtime.enum";
|
|
2
|
+
import {HttpsCertificateConfig, HttpsEnvironment} from "../framework/utilities/https";
|
|
2
3
|
|
|
3
4
|
export interface RuntimeServerInterface {
|
|
4
5
|
listen(port: number, hostname?: string): Promise<void>
|
|
@@ -8,8 +9,15 @@ export interface RuntimeServerInterface {
|
|
|
8
9
|
handle?(request: Request): Promise<Response>
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
export interface RuntimeServerHttpsInterface {
|
|
13
|
+
enabled: boolean
|
|
14
|
+
environment?: HttpsEnvironment
|
|
15
|
+
certificate: HttpsCertificateConfig
|
|
16
|
+
}
|
|
17
|
+
|
|
11
18
|
export interface RuntimeServerOptionsInterface {
|
|
12
19
|
prefix?: string;
|
|
20
|
+
https?: RuntimeServerHttpsInterface;
|
|
13
21
|
}
|
|
14
22
|
|
|
15
23
|
export interface RuntimeRequestInterface {
|