@xeno-js/core 3.0.5 → 4.1.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 +140 -531
- package/dist/index.cjs +4087 -4768
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +348 -1662
- package/dist/index.d.ts +348 -1662
- package/dist/index.js +4080 -4776
- package/dist/index.js.map +1 -1
- package/package.json +12 -58
package/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<img src="logo/logo.png" alt="Xeno.JS
|
|
2
|
+
<img src="logo/logo.png" alt="Xeno.JS logo" width="140" />
|
|
3
3
|
|
|
4
|
-
<h1>Xeno.JS</h1>
|
|
5
|
-
<p><strong>
|
|
6
|
-
<p>
|
|
4
|
+
<h1>Xeno.JS Core</h1>
|
|
5
|
+
<p><strong>Application architecture for TypeScript and Node.js.</strong></p>
|
|
6
|
+
<p>Compose services, request context, CQRS pipelines, and infrastructure through explicit boundaries.</p>
|
|
7
7
|
|
|
8
8
|
<p>
|
|
9
|
-
<a href="https://
|
|
10
|
-
|
|
11
|
-
<a href="https://www.npmjs.com/package/@xeno-js/core">
|
|
12
|
-
<img src="https://img.shields.io/npm/dt/@xeno-js/core?style=flat-square" alt="NPM Total Downloads" />
|
|
9
|
+
<a href="https://github.com/xeno-js/xeno-shared">
|
|
10
|
+
<img src="https://img.shields.io/github/stars/xeno-js/xeno-shared?style=flat-square" alt="GitHub Stars" />
|
|
13
11
|
</a>
|
|
14
|
-
<a href="
|
|
12
|
+
<a href="https://www.npmjs.com/package/@xeno-js/core"><img src="https://img.shields.io/npm/v/@xeno-js/core?style=flat-square" alt="npm version" /></a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/@xeno-js/core"><img src="https://img.shields.io/npm/dt/@xeno-js/core?style=flat-square" alt="npm downloads" /></a>
|
|
14
|
+
<a href="LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue?style=flat-square" alt="MIT license" /></a>
|
|
15
15
|
<a href="https://buymeacoffee.com/xenojs">
|
|
16
16
|
<img src="https://img.shields.io/badge/Buy%20Me%20A%20Coffee-Support-FFdd00?style=flat-square&logo=buy-me-a-coffee&logoColor=black" alt="Buy Me A Coffee" />
|
|
17
17
|
</a>
|
|
@@ -20,582 +20,191 @@
|
|
|
20
20
|
|
|
21
21
|
---
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
`@xeno-js/core` provides the runtime and composition APIs for Xeno.JS. It acts
|
|
24
|
+
as the composition root, managing Dependency Injection (DI), isolated request
|
|
25
|
+
contexts, and Command/Query pipelines, keeping application use cases independent
|
|
26
|
+
of transport and provider-specific implementations.
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
It provides explicit building blocks for applications organized around:
|
|
28
|
-
|
|
29
|
-
- **Dependency Injection** with explicit service registration and lifetimes
|
|
30
|
-
- **Domain-Driven Design (DDD)** and domain/application boundaries
|
|
31
|
-
- **CQRS** with commands, queries, handlers, and composable pipelines
|
|
32
|
-
- **Request context** built around asynchronous execution context
|
|
33
|
-
- **Repositories and data sources** that keep persistence behind application
|
|
34
|
-
boundaries
|
|
35
|
-
- **Infrastructure adapters** for databases, Redis, authentication, logging,
|
|
36
|
-
resilience, and other integrations
|
|
37
|
-
|
|
38
|
-
The goal is simple: **make application architecture explicit in code.**
|
|
39
|
-
|
|
40
|
-
Xeno.JS is not tied to a specific HTTP server. Your application layer can remain
|
|
41
|
-
independent from the transport that delivers a request.
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
## Why Xeno.JS?
|
|
46
|
-
|
|
47
|
-
### 01 — Explicit Architecture
|
|
48
|
-
|
|
49
|
-
**Your dependency graph is code.**
|
|
50
|
-
|
|
51
|
-
Xeno.JS does not require decorators, runtime scanning, or implicit dependency
|
|
52
|
-
discovery. Services are registered explicitly, and their lifetimes are visible
|
|
53
|
-
at the composition root.
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
services.addScoped('USER_REPOSITORY', (container) => {
|
|
57
|
-
return new UserRepository(
|
|
58
|
-
container.resolve('USER_DATA_SOURCE'),
|
|
59
|
-
container.resolve('USER_MAPPER'),
|
|
60
|
-
)
|
|
61
|
-
})
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
This makes the composition of the application easier to inspect, test, and
|
|
65
|
-
reason about.
|
|
66
|
-
|
|
67
|
-
### 02 — Transport Independence
|
|
68
|
-
|
|
69
|
-
Business logic should not belong to your HTTP framework.
|
|
70
|
-
|
|
71
|
-
Xeno.JS keeps application concerns separate from delivery mechanisms, allowing
|
|
72
|
-
the same application architecture to be hosted behind transports such as
|
|
73
|
-
Fastify, Hono, Express, or other adapters.
|
|
74
|
-
|
|
75
|
-
```text
|
|
76
|
-
HTTP / CLI / Worker / Lambda
|
|
77
|
-
|
|
|
78
|
-
v
|
|
79
|
-
Presentation
|
|
80
|
-
|
|
|
81
|
-
v
|
|
82
|
-
Application
|
|
83
|
-
Commands / Queries
|
|
84
|
-
|
|
|
85
|
-
v
|
|
86
|
-
Domain
|
|
87
|
-
|
|
|
88
|
-
v
|
|
89
|
-
Infrastructure
|
|
90
|
-
DB / Redis / APIs
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### 03 — CQRS as an Application Primitive
|
|
94
|
-
|
|
95
|
-
Commands and queries are first-class application concepts.
|
|
96
|
-
|
|
97
|
-
Pipelines can compose cross-cutting behavior around execution, such as:
|
|
98
|
-
|
|
99
|
-
- authorization
|
|
100
|
-
- idempotency
|
|
101
|
-
- concurrency control
|
|
102
|
-
- caching
|
|
103
|
-
- resilience policies
|
|
104
|
-
- request context
|
|
105
|
-
|
|
106
|
-
This keeps cross-cutting concerns out of individual handlers.
|
|
107
|
-
|
|
108
|
-
### 04 — Explicit Lifetimes and Request Boundaries
|
|
109
|
-
|
|
110
|
-
Xeno.JS distinguishes service lifetimes such as singleton, scoped, and transient
|
|
111
|
-
services.
|
|
112
|
-
|
|
113
|
-
Request-scoped dependencies are resolved inside an explicit application scope,
|
|
114
|
-
while request metadata can be carried through asynchronous execution using
|
|
115
|
-
`AsyncLocalStorage`.
|
|
116
|
-
|
|
117
|
-
Database transaction state is scoped to the same application boundary, allowing
|
|
118
|
-
`UnitOfWork` and `DbContext` to operate against the transaction associated with
|
|
119
|
-
the current scope.
|
|
120
|
-
|
|
121
|
-
### 05 — Infrastructure Stays Outside the Domain
|
|
122
|
-
|
|
123
|
-
Database clients, Redis, HTTP clients, authentication providers, loggers, and
|
|
124
|
-
other infrastructure integrations are composed at the edge of the application.
|
|
125
|
-
|
|
126
|
-
Your domain and application code can depend on contracts instead of concrete
|
|
127
|
-
infrastructure.
|
|
128
|
-
|
|
129
|
-
---
|
|
130
|
-
|
|
131
|
-
## Architecture
|
|
132
|
-
|
|
133
|
-
A typical Xeno.JS application can be organized like this:
|
|
134
|
-
|
|
135
|
-
```text
|
|
136
|
-
+------------------------------------------+
|
|
137
|
-
| Presentation |
|
|
138
|
-
| HTTP / CLI / Workers / Lambda |
|
|
139
|
-
+---------------------+--------------------+
|
|
140
|
-
|
|
|
141
|
-
v
|
|
142
|
-
+------------------------------------------+
|
|
143
|
-
| Application |
|
|
144
|
-
| Commands / Queries / Handlers / Pipes |
|
|
145
|
-
+---------------------+--------------------+
|
|
146
|
-
|
|
|
147
|
-
v
|
|
148
|
-
+------------------------------------------+
|
|
149
|
-
| Domain |
|
|
150
|
-
| Entities / Policies / Rules |
|
|
151
|
-
+---------------------+--------------------+
|
|
152
|
-
|
|
|
153
|
-
v
|
|
154
|
-
+------------------------------------------+
|
|
155
|
-
| Infrastructure |
|
|
156
|
-
| DB / Redis / APIs / Auth / Logs |
|
|
157
|
-
+------------------------------------------+
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
Xeno's core is focused on composition and application architecture.
|
|
161
|
-
Infrastructure capabilities can be enabled only when they are needed.
|
|
162
|
-
|
|
163
|
-
---
|
|
164
|
-
|
|
165
|
-
## Core Concepts
|
|
166
|
-
|
|
167
|
-
| Concept | Purpose |
|
|
168
|
-
| ------------------ | --------------------------------------------------------- |
|
|
169
|
-
| `AppBuilder` | Composition root for assembling an application |
|
|
170
|
-
| `ServiceContainer` | Explicit dependency injection and service lifetimes |
|
|
171
|
-
| `CQRS` | Commands, queries, handlers, and mediator-based execution |
|
|
172
|
-
| `Pipelines` | Cross-cutting behavior around application execution |
|
|
173
|
-
| `Request Context` | Request metadata across asynchronous execution |
|
|
174
|
-
| `Repository` | Application-facing persistence abstraction |
|
|
175
|
-
| `DataSource` | Infrastructure-facing data access implementation |
|
|
176
|
-
| `Module` | Explicit registration of related capabilities |
|
|
177
|
-
| `Result` | Typed success/failure flow for application operations |
|
|
178
|
-
|
|
179
|
-
---
|
|
180
|
-
|
|
181
|
-
## Installation
|
|
28
|
+
## Install
|
|
182
29
|
|
|
183
30
|
```bash
|
|
184
31
|
npm install @xeno-js/core
|
|
185
|
-
```
|
|
186
|
-
|
|
187
|
-
Install only the integrations your application uses. Xeno.JS exposes optional
|
|
188
|
-
infrastructure dependencies for capabilities such as databases, Redis, logging,
|
|
189
|
-
resilience, and authentication.
|
|
190
|
-
|
|
191
|
-
For example:
|
|
192
32
|
|
|
193
|
-
```bash
|
|
194
|
-
npm install zod pino cockatiel drizzle-orm
|
|
195
33
|
```
|
|
196
34
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
## A Small Example
|
|
35
|
+
The package requires Node.js 20 or newer. `@xeno-js/shared` is a required peer
|
|
36
|
+
dependency.
|
|
200
37
|
|
|
201
|
-
|
|
38
|
+
---
|
|
202
39
|
|
|
203
|
-
|
|
204
|
-
> it, please install the following packages:
|
|
40
|
+
## 1. Bootstrapping the Application
|
|
205
41
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
42
|
+
**Problem:** Node.js applications often suffer from fragmented initialization
|
|
43
|
+
where loggers, database connections, and DI containers are configured in
|
|
44
|
+
disparate files, making the startup sequence unpredictable.
|
|
209
45
|
|
|
210
|
-
|
|
46
|
+
**Principle:** Composition must be explicit, sequential, and isolated from the
|
|
47
|
+
transport layer.
|
|
211
48
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
IReadDataSource,
|
|
216
|
-
IReadDao,
|
|
217
|
-
IHandler,
|
|
218
|
-
IController,
|
|
219
|
-
} from '@xeno-js/core'
|
|
220
|
-
import type { XenoDbRegistry } from '@xeno-js/postgresql'
|
|
221
|
-
/** We need to import the databse schema: see @link https://www.xeno-js.it/docs/data/db-schema */
|
|
222
|
-
import { DbSchema } from './schema'
|
|
223
|
-
|
|
224
|
-
export interface AppRegistry extends XenoDbRegistry<DbSchema> {
|
|
225
|
-
USER_DATA_SOURCE: IReadDataSource
|
|
226
|
-
USER_REPOSITORY: IReadDao
|
|
227
|
-
FIND_USER_HANDLER: IHandler<FindUserQuery, void>
|
|
228
|
-
FIND_USER_CONTROLLER: IController<FindUserDto, void>
|
|
229
|
-
}
|
|
49
|
+
**Mechanism:** `XenoAppBuilder` exposes a fluent API to queue module
|
|
50
|
+
configurations. It initializes the queued modules in ascending priority order
|
|
51
|
+
when `build()` is called.
|
|
230
52
|
|
|
231
|
-
|
|
232
|
-
import { AppBuilder, TOKENS } from '@xeno-js/core'
|
|
233
|
-
import { withPostgresql } from '@xeno-js/postgresql'
|
|
234
|
-
import { AppRegistry } from './registry'
|
|
235
|
-
import {
|
|
236
|
-
UserDataSource,
|
|
237
|
-
UserRepository,
|
|
238
|
-
FindUserHandler,
|
|
239
|
-
FindUserController,
|
|
240
|
-
} from './user'
|
|
241
|
-
|
|
242
|
-
const app = new AppBuilder<AppRegistry>()
|
|
243
|
-
.addServices((services) => {
|
|
244
|
-
services.addScoped('USER_DATA_SOURCE', (c) => {
|
|
245
|
-
return new UserDataSource(c.resolve(TOKENS.DB_CONTEXT))
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
services.addScoped('USER_REPOSITORY', (container) => {
|
|
249
|
-
return new UserRepository(container.resolve('USER_DATA_SOURCE'))
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
services.addScoped('FIND_USER_HANDLER', (container) => {
|
|
253
|
-
return new FindUserHandler(
|
|
254
|
-
container.resolve('USER_REPOSITORY'),
|
|
255
|
-
container.resolve(TOKENS.USER_CONTEXT_FACTORY),
|
|
256
|
-
)
|
|
257
|
-
})
|
|
258
|
-
|
|
259
|
-
services.addTransient('FIND_USER_CONTROLLER', (c) => {
|
|
260
|
-
return new FindUserController(
|
|
261
|
-
c.resolve(TOKENS.REQUEST_CONTEXT),
|
|
262
|
-
c.resolve(TOKENS.MEDIATOR),
|
|
263
|
-
)
|
|
264
|
-
})
|
|
265
|
-
})
|
|
266
|
-
.addLogger()
|
|
267
|
-
.addPipeline()
|
|
268
|
-
.addDb(
|
|
269
|
-
withPostgresql((opts, config) => {
|
|
270
|
-
opts.connectionString = config.getOrThrow('DATABASE_URL')
|
|
271
|
-
}),
|
|
272
|
-
)
|
|
53
|
+
**Implementation:**
|
|
273
54
|
|
|
274
|
-
|
|
55
|
+
```ts
|
|
56
|
+
import { XenoAppBuilder, withConsoleLogger, LOG_LEVEL } from '@xeno-js/core'
|
|
57
|
+
import type { ApplicationRegistry } from '@xeno-js/shared'
|
|
275
58
|
|
|
276
|
-
|
|
277
|
-
import { TOKENS } from '@xeno-js/core'
|
|
278
|
-
import { container } from './bootstrap'
|
|
59
|
+
import { IPaymentService, PaymentService } from './payments'
|
|
279
60
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const scope = container.createScope()
|
|
283
|
-
try {
|
|
284
|
-
const controller = scope.resolve('FIND_USER_CONTROLLER')
|
|
285
|
-
const response = await controller.handle(
|
|
286
|
-
{ id: args[0] },
|
|
287
|
-
new AbortController().signal,
|
|
288
|
-
)
|
|
289
|
-
return {
|
|
290
|
-
success: response.ok,
|
|
291
|
-
status: response.status,
|
|
292
|
-
data: response.data,
|
|
293
|
-
}
|
|
294
|
-
} catch (err: unknown) {
|
|
295
|
-
logger.error('Error during CLI execution', err)
|
|
296
|
-
return {
|
|
297
|
-
success: false,
|
|
298
|
-
error: { message: 'Internal Server Error' },
|
|
299
|
-
}
|
|
300
|
-
} finally {
|
|
301
|
-
await scope.dispose()
|
|
302
|
-
}
|
|
61
|
+
interface AppRegistry extends ApplicationRegistry {
|
|
62
|
+
paymentService: IPaymentService
|
|
303
63
|
}
|
|
304
64
|
|
|
305
|
-
|
|
306
|
-
.
|
|
307
|
-
.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
> ⚠️ **Implementation note: Example using Fastify** The following snippet uses
|
|
313
|
-
> **Fastify** solely for demonstration purposes to illustrate the transport
|
|
314
|
-
> layer. Thanks to the framework's agnostic architecture, the underlying logic
|
|
315
|
-
> (`container` and `handler`) remains unchanged regardless of the chosen HTTP
|
|
316
|
-
> system (e.g., Express, Koa) or interface (CLI, gRPC). Xeno.js offers fastify
|
|
317
|
-
> adapter, if you want to try it install the following package:
|
|
318
|
-
|
|
319
|
-
```bash
|
|
320
|
-
# Install the package
|
|
321
|
-
npm i @xeno-js/fastify
|
|
322
|
-
# See the source code: https://github.com/xeno-js/xeno-fastify
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
// src/bootstrap.ts
|
|
327
|
-
import { FastifyXenoBuilder } from '@xeno-js/fastify'
|
|
328
|
-
import { TOKENS } from '@xeno-js/core'
|
|
329
|
-
import { withPostgresql } from '@xeno-js/postgresql'
|
|
330
|
-
import { AppRegistry } from './registry'
|
|
331
|
-
import {
|
|
332
|
-
UserDataSource,
|
|
333
|
-
UserRepository,
|
|
334
|
-
FindUserHandler,
|
|
335
|
-
FindUserController,
|
|
336
|
-
} from './user'
|
|
337
|
-
|
|
338
|
-
const app = new FastifyXenoBuilder<AppRegistry>()
|
|
339
|
-
.addServices((services) => {
|
|
340
|
-
services.addScoped('USER_DATA_SOURCE', (c) => {
|
|
341
|
-
return new UserDataSource(c.resolve(TOKENS.DB_CONTEXT))
|
|
342
|
-
})
|
|
343
|
-
|
|
344
|
-
services.addScoped('USER_REPOSITORY', (container) => {
|
|
345
|
-
return new UserRepository(container.resolve('USER_DATA_SOURCE'))
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
services.addScoped('FIND_USER_HANDLER', (container) => {
|
|
349
|
-
return new FindUserHandler(
|
|
350
|
-
container.resolve('USER_REPOSITORY'),
|
|
351
|
-
container.resolve(TOKENS.USER_CONTEXT_FACTORY),
|
|
352
|
-
)
|
|
353
|
-
})
|
|
354
|
-
|
|
355
|
-
services.addTransient('FIND_USER_CONTROLLER', (c) => {
|
|
356
|
-
return new FindUserController(
|
|
357
|
-
c.resolve(TOKENS.REQUEST_CONTEXT),
|
|
358
|
-
c.resolve(TOKENS.MEDIATOR),
|
|
359
|
-
)
|
|
360
|
-
})
|
|
361
|
-
})
|
|
362
|
-
.addLogger()
|
|
363
|
-
.addPipeline()
|
|
364
|
-
.addDb(
|
|
365
|
-
withPostgresql((opts, config) => {
|
|
366
|
-
opts.connectionString = config.getOrThrow('DATABASE_URL')
|
|
65
|
+
const app = await new XenoAppBuilder<AppRegistry>()
|
|
66
|
+
// 1. Configure Plugins
|
|
67
|
+
.addLogger(
|
|
68
|
+
withConsoleLogger((opts) => {
|
|
69
|
+
opts.level = LOG_LEVEL.INFO
|
|
70
|
+
opts.isEnabled = true
|
|
367
71
|
}),
|
|
368
72
|
)
|
|
369
|
-
.
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
const scope = container.createScope()
|
|
376
|
-
try {
|
|
377
|
-
const controller = scope.resolve('FIND_USER_CONTROLLER')
|
|
378
|
-
const response = await controller.handle(
|
|
379
|
-
req.params,
|
|
380
|
-
new AbortController().signal,
|
|
381
|
-
)
|
|
382
|
-
return res.status(response.status).send(response.data)
|
|
383
|
-
} catch (err: unknown) {
|
|
384
|
-
req.log.error(err)
|
|
385
|
-
return res.status(500).send({
|
|
386
|
-
success: false,
|
|
387
|
-
error: { message: 'Internal Server Error' },
|
|
388
|
-
})
|
|
389
|
-
} finally {
|
|
390
|
-
await scope.dispose()
|
|
391
|
-
}
|
|
73
|
+
// 2. Register Dependencies
|
|
74
|
+
.addServices((container) => {
|
|
75
|
+
container.addSingleton(
|
|
76
|
+
'paymentService',
|
|
77
|
+
(c) => new PaymentService(c.resolve(TOKENS.LOGGER)),
|
|
78
|
+
)
|
|
392
79
|
})
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
})
|
|
80
|
+
// 3. Initialize Runtime
|
|
81
|
+
.build()
|
|
396
82
|
```
|
|
397
83
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
### One Application. Multiple Entry Points
|
|
404
|
-
|
|
405
|
-
Build your application once and expose it through different entry points. With
|
|
406
|
-
Xeno.JS, CLI commands and HTTP APIs can share the same application core, keeping
|
|
407
|
-
business logic independent from the transport layer.
|
|
408
|
-
|
|
409
|
-
Adding Fastify does not require rewriting your use cases. Changing how your
|
|
410
|
-
application is exposed should not force changes to what your application does.
|
|
411
|
-
|
|
412
|
-
---
|
|
413
|
-
|
|
414
|
-
## CQRS & Pipelines
|
|
415
|
-
|
|
416
|
-
Cross-cutting behavior can be composed around commands and queries:
|
|
417
|
-
|
|
418
|
-
```typescript
|
|
419
|
-
.addPipeline((config) => {
|
|
420
|
-
config.authorization.policies = {
|
|
421
|
-
FIND_USER_QUERY_HANDLER: {
|
|
422
|
-
roles: ['admin'],
|
|
423
|
-
permissions: ['read'],
|
|
424
|
-
},
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
config.commandBus.idempotency = {
|
|
428
|
-
lockTtlSeconds: 30,
|
|
429
|
-
processedTtlSeconds: 60,
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
config.commandBus.concurrency = {
|
|
433
|
-
delayConfig: {
|
|
434
|
-
baseDelayMs: 100,
|
|
435
|
-
maxJitterMs: 500,
|
|
436
|
-
},
|
|
437
|
-
maxRetries: 3,
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
config.queryBus.isEnabled = true
|
|
441
|
-
})
|
|
442
|
-
```
|
|
84
|
+
**Behavior:** Modules initialize sequentially. Repeated calls to `build()`
|
|
85
|
+
safely return the cached promise of the first successful build. If a module
|
|
86
|
+
fails during bootstrap, the container is safely disposed, and the error is
|
|
87
|
+
rethrown.
|
|
443
88
|
|
|
444
|
-
The
|
|
445
|
-
|
|
89
|
+
**Boundary:** The builder composes the backend runtime. It does not
|
|
90
|
+
automatically start an HTTP server listener; calling `.listen()` is required if
|
|
91
|
+
HTTP modules are attached.
|
|
446
92
|
|
|
447
93
|
---
|
|
448
94
|
|
|
449
|
-
##
|
|
95
|
+
## 2. Dependency Injection and Context Isolation
|
|
450
96
|
|
|
451
|
-
|
|
97
|
+
**Problem:** Sharing state or database transactions across concurrent
|
|
98
|
+
asynchronous operations leads to data leaks and race conditions.
|
|
452
99
|
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
- **Authentication:** Supabase integrations and custom strategies
|
|
456
|
-
- **HTTP clients:** Axios
|
|
457
|
-
- **Resilience:** Cockatiel
|
|
458
|
-
- **Logging:** Console, Pino, Sentry, or custom loggers
|
|
459
|
-
- **Validation:** Zod
|
|
100
|
+
**Principle:** Request-bound dependencies must be explicitly scoped and securely
|
|
101
|
+
isolated.
|
|
460
102
|
|
|
461
|
-
|
|
462
|
-
|
|
103
|
+
**Mechanism:** The `ServiceContainer` provides strict `Singleton`, `Scoped`, and
|
|
104
|
+
`Transient` lifetimes. `AsyncLocalStorage` isolates the execution context. The
|
|
105
|
+
container explicitly prevents _Captive Dependencies_ (injecting a Scoped service
|
|
106
|
+
into a Singleton).
|
|
463
107
|
|
|
464
|
-
|
|
108
|
+
**Implementation:**
|
|
465
109
|
|
|
466
|
-
|
|
110
|
+
```ts
|
|
111
|
+
// 1. Registration
|
|
112
|
+
builder.addServices((container) => {
|
|
113
|
+
container.addScoped('paymentProcessor', (scope) => new PaymentProcessor())
|
|
467
114
|
|
|
468
|
-
|
|
115
|
+
// This will throw a [DI Captive Dependency Error] at runtime to prevent leaks:
|
|
116
|
+
// container.addSingleton('appManager', (scope) => new AppManager(scope.resolve('paymentProcessor')));
|
|
117
|
+
})
|
|
469
118
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
119
|
+
const app = await builder.build()
|
|
120
|
+
|
|
121
|
+
// 2. Execution outside HTTP (e.g., Background Workers, CLI)
|
|
122
|
+
await app.runInScope(
|
|
123
|
+
async (services) => {
|
|
124
|
+
const processor = services.paymentProcessor
|
|
125
|
+
await processor.execute()
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
identity: { userId: 'usr_123', tenantId: 'tenant_abc' },
|
|
129
|
+
},
|
|
130
|
+
)
|
|
473
131
|
```
|
|
474
132
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
## Documentation
|
|
480
|
-
|
|
481
|
-
The documentation hub contains the architecture and integration guides:
|
|
482
|
-
|
|
483
|
-
**[xeno-js.it](https://www.xeno-js.it/docs/introduction)**
|
|
484
|
-
|
|
485
|
-
Recommended starting points:
|
|
486
|
-
|
|
487
|
-
- [Introduction](https://www.xeno-js.it/docs/introduction)
|
|
488
|
-
- [Fundamentals](https://www.xeno-js.it/docs/fundamentals/overview)
|
|
489
|
-
- [Dependency Injection](https://www.xeno-js.it/docs/dependency-injection/service-container)
|
|
490
|
-
- [CQRS](https://www.xeno-js.it/docs/application/cqrs/overview)
|
|
491
|
-
- [Pipelines](https://www.xeno-js.it/docs/application/overview)
|
|
492
|
-
- [CLI](https://www.xeno-js.it/docs/cli/overview)
|
|
493
|
-
|
|
494
|
-
---
|
|
495
|
-
|
|
496
|
-
## Ecosystem
|
|
497
|
-
|
|
498
|
-
Xeno.JS is designed as an ecosystem rather than a single monolithic package:
|
|
499
|
-
|
|
500
|
-
| Package | Role |
|
|
501
|
-
| ----------------- | ----------------------------------------- |
|
|
502
|
-
| `@xeno-js/core` | Application architecture and backend core |
|
|
503
|
-
| `@xeno-js/shared` | Shared contracts and types |
|
|
504
|
-
| `@xeno-js/vue` | Vue integration |
|
|
505
|
-
| `@xeno-js/cli` | Project scaffolding and developer tooling |
|
|
506
|
-
|
|
507
|
-
---
|
|
508
|
-
|
|
509
|
-
## What Xeno.JS Is Not
|
|
510
|
-
|
|
511
|
-
Xeno.JS is not primarily an HTTP framework.
|
|
512
|
-
|
|
513
|
-
If you are looking for a framework centered on routing, controllers, middleware,
|
|
514
|
-
and server lifecycle, there are excellent options already available in the
|
|
515
|
-
Node.js ecosystem.
|
|
516
|
-
|
|
517
|
-
Xeno.JS focuses on the layer above transport:
|
|
518
|
-
|
|
519
|
-
> **How should a TypeScript application be structured so that its business
|
|
520
|
-
> logic, dependencies, and infrastructure boundaries remain explicit as the
|
|
521
|
-
> application grows?**
|
|
522
|
-
|
|
523
|
-
---
|
|
524
|
-
|
|
525
|
-
## Production Considerations
|
|
526
|
-
|
|
527
|
-
Xeno.JS provides architectural primitives, but application correctness still
|
|
528
|
-
depends on how those primitives are composed.
|
|
133
|
+
**Behavior:** `runInScope` generates a synthetic request context containing
|
|
134
|
+
trace IDs and network context. The active scope tracks disposable instances
|
|
135
|
+
(`IDisposable`) and automatically disposes them in a `finally` block when the
|
|
136
|
+
operation completes or fails.
|
|
529
137
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
- request and transaction isolation
|
|
534
|
-
- service lifetime boundaries
|
|
535
|
-
- authorization policies
|
|
536
|
-
- idempotency semantics
|
|
537
|
-
- concurrency behavior
|
|
538
|
-
- cache consistency
|
|
539
|
-
- failure and retry behavior
|
|
540
|
-
- trusted proxy / client IP configuration
|
|
541
|
-
- database transaction boundaries
|
|
542
|
-
|
|
543
|
-
The framework is designed to make these boundaries explicit rather than hide
|
|
544
|
-
them behind conventions.
|
|
138
|
+
**Boundary:** The container throws `[DI Container Error]` if a developer
|
|
139
|
+
attempts to resolve a `Scoped` service directly from the root container outside
|
|
140
|
+
of an active scope.
|
|
545
141
|
|
|
546
142
|
---
|
|
547
143
|
|
|
548
|
-
##
|
|
144
|
+
## 3. CQRS and Cross-Cutting Pipelines
|
|
549
145
|
|
|
550
|
-
|
|
146
|
+
**Problem:** Business use cases become polluted when authentication, validation,
|
|
147
|
+
performance tracking, and idempotency logic are hardcoded directly into the
|
|
148
|
+
handlers.
|
|
551
149
|
|
|
552
|
-
|
|
150
|
+
**Principle:** Separate the execution of a use case from the behaviors that wrap
|
|
151
|
+
it.
|
|
553
152
|
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
git checkout -b feat/your-feature
|
|
153
|
+
**Mechanism:** The `addPipeline()` method configures the CQRS module.
|
|
154
|
+
`PipelinePlugin` extensions decorate the `Mediator` dispatch process using a
|
|
155
|
+
chain of responsibility (Composite Pipeline).
|
|
558
156
|
|
|
559
|
-
|
|
560
|
-
npm run check
|
|
561
|
-
```
|
|
157
|
+
**Implementation:**
|
|
562
158
|
|
|
563
|
-
|
|
159
|
+
```ts
|
|
160
|
+
import {
|
|
161
|
+
withPerformance,
|
|
162
|
+
withConcurrency,
|
|
163
|
+
withIdempotency,
|
|
164
|
+
} from '@xeno-js/core'
|
|
564
165
|
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
166
|
+
// Configure the builder with pipeline behaviors
|
|
167
|
+
const app = await new XenoAppBuilder()
|
|
168
|
+
.addPipeline([
|
|
169
|
+
withPerformance((opts) => {
|
|
170
|
+
opts.thresholdMs = 500 // Logs warnings for slow handlers
|
|
171
|
+
}),
|
|
172
|
+
withConcurrency((opts) => {
|
|
173
|
+
// Manage concurrency
|
|
174
|
+
opts.maxRetries = 3
|
|
175
|
+
opts.delayConfig = {
|
|
176
|
+
baseDelayMs: 100,
|
|
177
|
+
maxJitterMs: 40,
|
|
178
|
+
}
|
|
179
|
+
}),
|
|
180
|
+
withQueryCaching(), // For caching the query request
|
|
181
|
+
withIdempotency(), // Relies on IIdempotencyStore (e.g., Redis plugin)
|
|
182
|
+
])
|
|
183
|
+
.build()
|
|
569
184
|
```
|
|
570
185
|
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
npm run check
|
|
575
|
-
```
|
|
186
|
+
**Behavior:** When a command is dispatched via the Mediator, it sequentially
|
|
187
|
+
passes through the configured behaviors (e.g., checking idempotency, enforcing
|
|
188
|
+
concurrency limits) before reaching the concrete handler.
|
|
576
189
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
| `npm run lint` | ESLint |
|
|
582
|
-
| `npm run format:check` | Prettier validation |
|
|
583
|
-
| `npm run test` | Vitest test suite |
|
|
584
|
-
| `npm run test:coverage` | Test suite with coverage |
|
|
190
|
+
**Boundary:** `addPipeline()` orchestrates the flow. It does not provide the
|
|
191
|
+
concrete infrastructure implementations (e.g., validation schemas or caching
|
|
192
|
+
storage). You must provide the respective adapters (like `@xeno-js/zod` or
|
|
193
|
+
`@xeno-js/redis`).
|
|
585
194
|
|
|
586
195
|
---
|
|
587
196
|
|
|
588
|
-
##
|
|
197
|
+
## Integrations
|
|
589
198
|
|
|
590
|
-
|
|
591
|
-
|
|
199
|
+
`@xeno-js/core` relies on adapter packages to connect infrastructure and
|
|
200
|
+
presentation layers. Register them via the builder extension points:
|
|
592
201
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
202
|
+
- [`@xeno-js/fastify`](https://github.com/xeno-js/fastify) - HTTP Transport
|
|
203
|
+
- [`@xeno-js/postgresql`](https://github.com/xeno-js/postgresql) -
|
|
204
|
+
Drizzle/PostgreSQL Data Access
|
|
205
|
+
- [`@xeno-js/redis`](https://github.com/xeno-js/redis) - Caching and Idempotency
|
|
206
|
+
store
|
|
596
207
|
|
|
597
208
|
## License
|
|
598
209
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
Licensed under the [MIT License](LICENSE).
|
|
210
|
+
Xeno.JS Core is released under the [MIT License](LICENSE).
|