@navios/core 0.3.0 → 0.5.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 +96 -3
- package/docs/README.md +310 -3
- package/docs/adapters.md +308 -0
- package/docs/application-setup.md +524 -0
- package/docs/attributes.md +689 -0
- package/docs/controllers.md +373 -0
- package/docs/endpoints.md +444 -0
- package/docs/exceptions.md +316 -0
- package/docs/guards.md +550 -0
- package/docs/modules.md +251 -0
- package/docs/quick-start.md +295 -0
- package/docs/services.md +428 -0
- package/docs/testing.md +704 -0
- package/lib/_tsup-dts-rollup.d.mts +313 -280
- package/lib/_tsup-dts-rollup.d.ts +313 -280
- package/lib/index.d.mts +47 -26
- package/lib/index.d.ts +47 -26
- package/lib/index.js +633 -1068
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +632 -1061
- package/lib/index.mjs.map +1 -1
- package/package.json +11 -12
- package/project.json +17 -4
- package/src/__tests__/config.service.spec.mts +11 -9
- package/src/__tests__/controller.spec.mts +1 -2
- package/src/attribute.factory.mts +1 -1
- package/src/config/config.provider.mts +2 -2
- package/src/config/config.service.mts +4 -4
- package/src/decorators/controller.decorator.mts +1 -1
- package/src/decorators/endpoint.decorator.mts +9 -10
- package/src/decorators/header.decorator.mts +1 -1
- package/src/decorators/multipart.decorator.mts +5 -5
- package/src/decorators/stream.decorator.mts +5 -6
- package/src/factories/endpoint-adapter.factory.mts +21 -0
- package/src/factories/http-adapter.factory.mts +20 -0
- package/src/factories/index.mts +6 -0
- package/src/factories/multipart-adapter.factory.mts +21 -0
- package/src/factories/reply.factory.mts +21 -0
- package/src/factories/request.factory.mts +21 -0
- package/src/factories/stream-adapter.factory.mts +20 -0
- package/src/index.mts +1 -1
- package/src/interfaces/abstract-execution-context.inteface.mts +13 -0
- package/src/interfaces/abstract-http-adapter.interface.mts +20 -0
- package/src/interfaces/abstract-http-cors-options.interface.mts +59 -0
- package/src/interfaces/abstract-http-handler-adapter.interface.mts +13 -0
- package/src/interfaces/abstract-http-listen-options.interface.mts +4 -0
- package/src/interfaces/can-activate.mts +4 -2
- package/src/interfaces/http-header.mts +18 -0
- package/src/interfaces/index.mts +6 -0
- package/src/logger/console-logger.service.mts +28 -44
- package/src/logger/index.mts +1 -2
- package/src/logger/logger.service.mts +9 -128
- package/src/logger/logger.tokens.mts +21 -0
- package/src/metadata/handler.metadata.mts +7 -5
- package/src/navios.application.mts +65 -172
- package/src/navios.environment.mts +30 -0
- package/src/navios.factory.mts +53 -12
- package/src/services/guard-runner.service.mts +19 -9
- package/src/services/index.mts +0 -2
- package/src/services/module-loader.service.mts +4 -3
- package/src/tokens/endpoint-adapter.token.mts +8 -0
- package/src/tokens/execution-context.token.mts +2 -2
- package/src/tokens/http-adapter.token.mts +8 -0
- package/src/tokens/index.mts +4 -1
- package/src/tokens/multipart-adapter.token.mts +8 -0
- package/src/tokens/reply.token.mts +1 -5
- package/src/tokens/request.token.mts +1 -7
- package/src/tokens/stream-adapter.token.mts +8 -0
- package/tsconfig.json +6 -1
- package/tsconfig.lib.json +8 -0
- package/tsconfig.spec.json +12 -0
- package/tsup.config.mts +1 -0
- package/docs/recipes/prisma.md +0 -60
- package/examples/simple-test/api/index.mts +0 -64
- package/examples/simple-test/config/config.service.mts +0 -14
- package/examples/simple-test/config/configuration.mts +0 -7
- package/examples/simple-test/index.mts +0 -16
- package/examples/simple-test/src/acl/acl-modern.guard.mts +0 -15
- package/examples/simple-test/src/acl/acl.guard.mts +0 -14
- package/examples/simple-test/src/acl/app.guard.mts +0 -27
- package/examples/simple-test/src/acl/one-more.guard.mts +0 -15
- package/examples/simple-test/src/acl/public.attribute.mts +0 -21
- package/examples/simple-test/src/app.module.mts +0 -9
- package/examples/simple-test/src/user/user.controller.mts +0 -72
- package/examples/simple-test/src/user/user.module.mts +0 -14
- package/examples/simple-test/src/user/user.service.mts +0 -14
- package/src/adapters/endpoint-adapter.service.mts +0 -72
- package/src/adapters/handler-adapter.interface.mts +0 -21
- package/src/adapters/index.mts +0 -4
- package/src/adapters/multipart-adapter.service.mts +0 -131
- package/src/adapters/stream-adapter.service.mts +0 -91
- package/src/logger/logger.factory.mts +0 -36
- package/src/logger/pino-wrapper.mts +0 -64
- package/src/services/controller-adapter.service.mts +0 -124
- package/src/services/execution-context.mts +0 -54
- package/src/tokens/application.token.mts +0 -9
package/docs/adapters.md
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# Navios HTTP Adapters
|
|
2
|
+
|
|
3
|
+
This document provides detailed information about Navios HTTP adapters, their implementation, and usage patterns.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Navios uses an adapter pattern to provide HTTP server functionality. Adapters are responsible for:
|
|
8
|
+
|
|
9
|
+
- Binding HTTP requests and responses to Navios's internal format
|
|
10
|
+
- Providing server lifecycle management (start, stop, graceful shutdown)
|
|
11
|
+
- Handling server-specific features (middleware, plugins, etc.)
|
|
12
|
+
- Abstracting runtime-specific implementations
|
|
13
|
+
|
|
14
|
+
## Adapter Architecture
|
|
15
|
+
|
|
16
|
+
Each adapter implements the following core interfaces:
|
|
17
|
+
|
|
18
|
+
### HttpAdapterToken
|
|
19
|
+
|
|
20
|
+
Provides the main HTTP server functionality:
|
|
21
|
+
|
|
22
|
+
- Server initialization and configuration
|
|
23
|
+
- Request/response handling
|
|
24
|
+
- Server lifecycle management
|
|
25
|
+
|
|
26
|
+
### EndpointAdapterToken
|
|
27
|
+
|
|
28
|
+
Handles endpoint-specific functionality:
|
|
29
|
+
|
|
30
|
+
- Route registration
|
|
31
|
+
- Request parameter extraction
|
|
32
|
+
- Response formatting
|
|
33
|
+
|
|
34
|
+
### StreamAdapterToken
|
|
35
|
+
|
|
36
|
+
Manages streaming responses:
|
|
37
|
+
|
|
38
|
+
- File streaming
|
|
39
|
+
- Server-sent events
|
|
40
|
+
- Large response handling
|
|
41
|
+
|
|
42
|
+
### MultipartAdapterToken
|
|
43
|
+
|
|
44
|
+
Handles multipart requests:
|
|
45
|
+
|
|
46
|
+
- File uploads
|
|
47
|
+
- Form data processing
|
|
48
|
+
- Binary data handling
|
|
49
|
+
|
|
50
|
+
## Available Adapters
|
|
51
|
+
|
|
52
|
+
### @navios/adapter-fastify
|
|
53
|
+
|
|
54
|
+
Built on [Fastify](https://www.fastify.io/), providing a robust HTTP server with excellent performance and a rich ecosystem.
|
|
55
|
+
|
|
56
|
+
#### Installation
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm install @navios/adapter-fastify fastify
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
#### Basic Usage
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { defineFastifyEnvironment } from '@navios/adapter-fastify'
|
|
66
|
+
import { NaviosFactory } from '@navios/core'
|
|
67
|
+
|
|
68
|
+
const app = await NaviosFactory.create(AppModule, {
|
|
69
|
+
adapter: defineFastifyEnvironment(),
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### Features
|
|
74
|
+
|
|
75
|
+
- **High Performance**: Fastify is one of the fastest Node.js web frameworks
|
|
76
|
+
- **Schema-based Validation**: Built-in JSON Schema validation
|
|
77
|
+
- **Rich Ecosystem**: Extensive plugin system
|
|
78
|
+
- **TypeScript Support**: Full TypeScript integration
|
|
79
|
+
- **Production Ready**: Battle-tested in production environments
|
|
80
|
+
|
|
81
|
+
#### Fastify-specific Features
|
|
82
|
+
|
|
83
|
+
##### Hooks Integration
|
|
84
|
+
|
|
85
|
+
The adapter integrates with Fastify's hook system:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
// Access to underlying Fastify instance (if needed)
|
|
89
|
+
const fastifyInstance = app.getServer()
|
|
90
|
+
|
|
91
|
+
// Add Fastify hooks
|
|
92
|
+
fastifyInstance.addHook('preHandler', async (request, reply) => {
|
|
93
|
+
// Custom pre-handler logic
|
|
94
|
+
})
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
##### Plugin System
|
|
98
|
+
|
|
99
|
+
You can register Fastify plugins:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
import fastifyStatic from '@fastify/static'
|
|
103
|
+
|
|
104
|
+
const fastifyInstance = app.getServer()
|
|
105
|
+
await fastifyInstance.register(fastifyStatic, {
|
|
106
|
+
root: path.join(__dirname, 'public'),
|
|
107
|
+
prefix: '/public/',
|
|
108
|
+
})
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
#### Configuration Options
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
const app = await NaviosFactory.create(AppModule, {
|
|
115
|
+
adapter: defineFastifyEnvironment({
|
|
116
|
+
// Fastify server options
|
|
117
|
+
trustProxy: true,
|
|
118
|
+
}),
|
|
119
|
+
})
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### @navios/adapter-bun
|
|
123
|
+
|
|
124
|
+
Built for [Bun](https://bun.sh/) runtime, providing optimal performance and native integration.
|
|
125
|
+
|
|
126
|
+
#### Installation
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npm install @navios/adapter-bun
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Basic Usage
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
import { defineBunEnvironment } from '@navios/adapter-bun'
|
|
136
|
+
import { NaviosFactory } from '@navios/core'
|
|
137
|
+
|
|
138
|
+
const app = await NaviosFactory.create(AppModule, {
|
|
139
|
+
adapter: defineBunEnvironment(),
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
#### Features
|
|
144
|
+
|
|
145
|
+
- **Native Performance**: Built on Bun's native HTTP server
|
|
146
|
+
- **Fast Startup**: Optimized for quick application startup
|
|
147
|
+
- **Memory Efficient**: Lower memory footprint
|
|
148
|
+
- **Modern Runtime**: Takes advantage of Bun's modern JavaScript features
|
|
149
|
+
|
|
150
|
+
#### Bun-specific Features
|
|
151
|
+
|
|
152
|
+
#### Configuration Options
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const app = await NaviosFactory.create(AppModule, {
|
|
156
|
+
adapter: defineBunEnvironment({
|
|
157
|
+
// Bun server options
|
|
158
|
+
development: process.env.NODE_ENV === 'development',
|
|
159
|
+
}),
|
|
160
|
+
})
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Adapter Comparison
|
|
164
|
+
|
|
165
|
+
| Feature | Fastify Adapter | Bun Adapter |
|
|
166
|
+
| ------------------ | ---------------------- | ----------- |
|
|
167
|
+
| **Runtime** | Node.js | Bun |
|
|
168
|
+
| **Performance** | High | Very High |
|
|
169
|
+
| **Memory Usage** | Moderate | Low |
|
|
170
|
+
| **Startup Time** | Fast | Very Fast |
|
|
171
|
+
| **Ecosystem** | Rich (Fastify plugins) | Growing |
|
|
172
|
+
| **WebSocket** | Via plugins | Native |
|
|
173
|
+
| **File Serving** | Via plugins | Native |
|
|
174
|
+
| **Production Use** | Mature | Emerging |
|
|
175
|
+
| **Community** | Large | Growing |
|
|
176
|
+
|
|
177
|
+
## Creating Custom Adapters
|
|
178
|
+
|
|
179
|
+
To create a custom adapter, implement the required tokens:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import {
|
|
183
|
+
EndpointAdapterToken,
|
|
184
|
+
HttpAdapterToken,
|
|
185
|
+
MultipartAdapterToken,
|
|
186
|
+
StreamAdapterToken,
|
|
187
|
+
} from '@navios/core'
|
|
188
|
+
|
|
189
|
+
export function defineCustomEnvironment() {
|
|
190
|
+
const httpTokens = new Map([
|
|
191
|
+
[HttpAdapterToken, CustomHttpAdapter],
|
|
192
|
+
[EndpointAdapterToken, CustomEndpointAdapter],
|
|
193
|
+
[StreamAdapterToken, CustomStreamAdapter],
|
|
194
|
+
[MultipartAdapterToken, CustomMultipartAdapter],
|
|
195
|
+
])
|
|
196
|
+
|
|
197
|
+
return { httpTokens }
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### HttpAdapter Implementation
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
import { AbstractHttpAdapter } from '@navios/core'
|
|
205
|
+
import { Injectable } from '@navios/di'
|
|
206
|
+
|
|
207
|
+
@Injectable()
|
|
208
|
+
export class CustomHttpAdapter extends AbstractHttpAdapter {
|
|
209
|
+
async init() {
|
|
210
|
+
// Initialize your HTTP server
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async listen(options: any) {
|
|
214
|
+
// Start listening for requests
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async close() {
|
|
218
|
+
// Graceful shutdown
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Implement other required methods...
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Best Practices
|
|
226
|
+
|
|
227
|
+
### Adapter Selection
|
|
228
|
+
|
|
229
|
+
1. **Choose based on runtime**: Use Fastify for Node.js, Bun for Bun runtime
|
|
230
|
+
2. **Consider ecosystem needs**: Fastify has more plugins available
|
|
231
|
+
3. **Performance requirements**: Bun offers better raw performance
|
|
232
|
+
4. **Team expertise**: Consider your team's familiarity with the underlying technology
|
|
233
|
+
|
|
234
|
+
### Configuration
|
|
235
|
+
|
|
236
|
+
1. **Environment-specific settings**: Configure adapters differently for dev/prod
|
|
237
|
+
2. **Security**: Enable appropriate security features for production
|
|
238
|
+
3. **Monitoring**: Set up proper logging and metrics collection
|
|
239
|
+
4. **Resource limits**: Configure appropriate timeouts and limits
|
|
240
|
+
|
|
241
|
+
### Error Handling
|
|
242
|
+
|
|
243
|
+
1. **Consistent error responses**: Use Navios exceptions for consistent API responses
|
|
244
|
+
2. **Logging**: Ensure proper error logging across adapters
|
|
245
|
+
3. **Graceful degradation**: Handle adapter-specific errors appropriately
|
|
246
|
+
|
|
247
|
+
### Performance Optimization
|
|
248
|
+
|
|
249
|
+
1. **Connection pooling**: Configure appropriate connection limits
|
|
250
|
+
2. **Caching**: Use adapter-specific caching when available
|
|
251
|
+
3. **Compression**: Enable response compression for better performance
|
|
252
|
+
4. **Static files**: Use efficient static file serving strategies
|
|
253
|
+
|
|
254
|
+
## Troubleshooting
|
|
255
|
+
|
|
256
|
+
### Common Issues
|
|
257
|
+
|
|
258
|
+
#### Fastify Adapter
|
|
259
|
+
|
|
260
|
+
- **Plugin conflicts**: Ensure Fastify plugins are compatible
|
|
261
|
+
- **Schema validation**: Check JSON schema definitions for validation errors
|
|
262
|
+
- **Memory leaks**: Monitor for unclosed connections or resources
|
|
263
|
+
|
|
264
|
+
#### Bun Adapter
|
|
265
|
+
|
|
266
|
+
- **Runtime compatibility**: Ensure code is compatible with Bun runtime
|
|
267
|
+
- **Module resolution**: Check import/export statements work with Bun
|
|
268
|
+
- **Native features**: Verify Bun-specific features are used correctly
|
|
269
|
+
|
|
270
|
+
### Debugging
|
|
271
|
+
|
|
272
|
+
1. **Enable logging**: Use appropriate log levels for debugging
|
|
273
|
+
2. **Check adapter health**: Monitor adapter-specific metrics
|
|
274
|
+
3. **Validate configuration**: Ensure adapter configuration is correct
|
|
275
|
+
4. **Test isolation**: Test adapters in isolation when troubleshooting
|
|
276
|
+
|
|
277
|
+
## Migration Guide
|
|
278
|
+
|
|
279
|
+
### From Express to Navios
|
|
280
|
+
|
|
281
|
+
If migrating from Express, the Fastify adapter provides the most familiar experience:
|
|
282
|
+
|
|
283
|
+
```ts
|
|
284
|
+
// Express-style (before)
|
|
285
|
+
app.get('/users/:id', (req, res) => {
|
|
286
|
+
res.json({ id: req.params.id })
|
|
287
|
+
})
|
|
288
|
+
|
|
289
|
+
// Navios with Fastify adapter (after)
|
|
290
|
+
@Controller()
|
|
291
|
+
export class UserController {
|
|
292
|
+
@Endpoint(getUserEndpoint)
|
|
293
|
+
async getUser(request: EndpointParams<typeof getUserEndpoint>) {
|
|
294
|
+
return { id: request.id }
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Between Adapters
|
|
300
|
+
|
|
301
|
+
Switching adapters is straightforward:
|
|
302
|
+
|
|
303
|
+
1. Update dependencies
|
|
304
|
+
2. Change the adapter import and function call
|
|
305
|
+
3. Update any adapter-specific configurations
|
|
306
|
+
4. Test thoroughly in your specific environment
|
|
307
|
+
|
|
308
|
+
The rest of your application code remains unchanged due to Navios's adapter abstraction.
|