arckode-framework 1.2.2 → 1.3.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.
@@ -200,8 +200,18 @@ export class ${p.name}Service {
200
200
  private readonly cache: CacheAdapter,
201
201
  ) {}
202
202
 
203
+ // ACUMULA handlers — nunca pisa el anterior.
204
+ // Si dos conectores registran el mismo evento, ambos corren en cadena (secuencial).
205
+ // Para ejecución paralela independiente → usar EventBus en composition-root.ts.
203
206
  setSockets(s: Partial<${p.name}Sockets>): void {
204
- this.sockets = { ...this.sockets, ...s }
207
+ const next = s as Record<string, any>
208
+ const cur = this.sockets as Record<string, any>
209
+ for (const key of Object.keys(next)) {
210
+ const h = next[key]
211
+ if (!h) continue
212
+ const prev = cur[key]
213
+ cur[key] = prev ? async (...a: any[]) => { await prev(...a); await h(...a) } : h
214
+ }
205
215
  }
206
216
 
207
217
  async list(query?: ${p.name}Query): Promise<${p.name}Paginated> {
@@ -1065,6 +1065,14 @@ export class OrmTransactor implements Transactor {
1065
1065
  // 6. ROUTER + HTTP — Enrutamiento con middlewares y error handler
1066
1066
  // ═══════════════════════════════════════════════════════════════
1067
1067
 
1068
+ export interface UploadedFile {
1069
+ fieldName: string
1070
+ originalName: string
1071
+ buffer: Buffer
1072
+ mimeType: string
1073
+ size: number
1074
+ }
1075
+
1068
1076
  export interface HttpRequest {
1069
1077
  id: string
1070
1078
  method: string
@@ -1073,6 +1081,7 @@ export interface HttpRequest {
1073
1081
  query: Record<string, string>
1074
1082
  headers: Record<string, string>
1075
1083
  body: unknown
1084
+ files?: Record<string, UploadedFile>
1076
1085
  user?: { id: string; role: string }
1077
1086
  }
1078
1087
 
@@ -1318,6 +1327,17 @@ function buildEnvelope(status: number, body: unknown): string {
1318
1327
  return JSON.stringify({ success: true, data: body, meta: null, error: null } satisfies ApiResponse)
1319
1328
  }
1320
1329
 
1330
+ function indexOfBuffer(haystack: Buffer, needle: Buffer, offset = 0): number {
1331
+ const limit = haystack.length - needle.length
1332
+ outer: for (let i = offset; i <= limit; i++) {
1333
+ for (let j = 0; j < needle.length; j++) {
1334
+ if (haystack[i + j] !== needle[j]) continue outer
1335
+ }
1336
+ return i
1337
+ }
1338
+ return -1
1339
+ }
1340
+
1321
1341
  export class NodeServer implements ServerAdapter {
1322
1342
  private server?: ReturnType<typeof createNodeServer>
1323
1343
  private maxBodyBytes: number
@@ -1341,7 +1361,7 @@ export class NodeServer implements ServerAdapter {
1341
1361
 
1342
1362
  await requestStorage.run({ requestId, startTime: Date.now() }, async () => {
1343
1363
  try {
1344
- const body = await this.readBody(nodeReq, this.maxBodyBytes)
1364
+ const { body, files } = await this.readBody(nodeReq, this.maxBodyBytes)
1345
1365
 
1346
1366
  const url = new URL(nodeReq.url ?? '/', `http://${nodeReq.headers.host ?? 'localhost'}`)
1347
1367
  const query: Record<string, string> = {}
@@ -1355,6 +1375,7 @@ export class NodeServer implements ServerAdapter {
1355
1375
  query,
1356
1376
  headers: nodeReq.headers as Record<string, string>,
1357
1377
  body,
1378
+ ...(files ? { files } : {}),
1358
1379
  }
1359
1380
 
1360
1381
  const res = await handler(req)
@@ -1441,7 +1462,10 @@ export class NodeServer implements ServerAdapter {
1441
1462
  return typeof addr === 'object' && addr ? addr.port : this.port
1442
1463
  }
1443
1464
 
1444
- private readBody(req: IncomingMessage, maxBytes = 10 * 1024 * 1024): Promise<unknown> {
1465
+ private readBody(
1466
+ req: IncomingMessage,
1467
+ maxBytes = 10 * 1024 * 1024,
1468
+ ): Promise<{ body: unknown; files?: Record<string, UploadedFile> }> {
1445
1469
  return new Promise((resolve, reject) => {
1446
1470
  const chunks: Buffer[] = []
1447
1471
  let total = 0
@@ -1457,14 +1481,90 @@ export class NodeServer implements ServerAdapter {
1457
1481
  })
1458
1482
 
1459
1483
  req.on('end', () => {
1460
- const raw = Buffer.concat(chunks).toString()
1461
- if (!raw) return resolve(null)
1462
- try { resolve(JSON.parse(raw)) } catch { resolve(raw) }
1484
+ const rawBuffer = Buffer.concat(chunks)
1485
+ if (!rawBuffer.length) return resolve({ body: null })
1486
+
1487
+ const contentType = (req.headers['content-type'] ?? '').toLowerCase()
1488
+ const boundaryMatch = contentType.match(/multipart\/form-data;\s*boundary=(.+)/)
1489
+
1490
+ if (boundaryMatch) {
1491
+ const boundary = (boundaryMatch[1] ?? '').trim()
1492
+ const { fields, files } = this.parseMultipart(rawBuffer, boundary)
1493
+ return resolve({ body: fields, files })
1494
+ }
1495
+
1496
+ const raw = rawBuffer.toString()
1497
+ try { resolve({ body: JSON.parse(raw) }) } catch { resolve({ body: raw }) }
1463
1498
  })
1464
1499
 
1465
1500
  req.on('error', reject)
1466
1501
  })
1467
1502
  }
1503
+
1504
+ private parseMultipart(
1505
+ buffer: Buffer,
1506
+ boundary: string,
1507
+ ): { fields: Record<string, string>; files: Record<string, UploadedFile> } {
1508
+ const fields: Record<string, string> = {}
1509
+ const files: Record<string, UploadedFile> = {}
1510
+
1511
+ const firstDelim = Buffer.from(`--${boundary}`)
1512
+ const innerDelim = Buffer.from(`\r\n--${boundary}`)
1513
+ const doubleCRLF = Buffer.from('\r\n\r\n')
1514
+
1515
+ let pos = indexOfBuffer(buffer, firstDelim, 0)
1516
+ if (pos === -1) return { fields, files }
1517
+ pos += firstDelim.length
1518
+
1519
+ while (pos < buffer.length) {
1520
+ // Skip \r\n after boundary line, or detect closing --
1521
+ if (buffer[pos] === 0x2d && buffer[pos + 1] === 0x2d) break
1522
+ if (buffer[pos] === 0x0d && buffer[pos + 1] === 0x0a) pos += 2
1523
+ else break
1524
+
1525
+ const headerEnd = indexOfBuffer(buffer, doubleCRLF, pos)
1526
+ if (headerEnd === -1) break
1527
+
1528
+ const headerStr = buffer.subarray(pos, headerEnd).toString()
1529
+ pos = headerEnd + 4
1530
+
1531
+ const nextBound = indexOfBuffer(buffer, innerDelim, pos)
1532
+ if (nextBound === -1) break
1533
+
1534
+ const partBody = buffer.subarray(pos, nextBound)
1535
+ pos = nextBound + innerDelim.length
1536
+
1537
+ const headers: Record<string, string> = {}
1538
+ for (const line of headerStr.split('\r\n')) {
1539
+ const colon = line.indexOf(':')
1540
+ if (colon === -1) continue
1541
+ headers[line.slice(0, colon).toLowerCase().trim()] = line.slice(colon + 1).trim()
1542
+ }
1543
+
1544
+ const disp = headers['content-disposition'] ?? ''
1545
+ const nameMatch = /name="([^"]*)"/.exec(disp)
1546
+ const fileMatch = /filename="([^"]*)"/.exec(disp)
1547
+ if (!nameMatch) continue
1548
+
1549
+ const fieldName = nameMatch[1] ?? ''
1550
+
1551
+ if (fileMatch) {
1552
+ files[fieldName] = {
1553
+ fieldName,
1554
+ originalName: fileMatch[1] ?? '',
1555
+ buffer: partBody,
1556
+ mimeType: headers['content-type'] ?? 'application/octet-stream',
1557
+ size: partBody.length,
1558
+ }
1559
+ } else {
1560
+ fields[fieldName] = partBody.toString()
1561
+ }
1562
+
1563
+ if (buffer[pos] === 0x2d && buffer[pos + 1] === 0x2d) break
1564
+ }
1565
+
1566
+ return { fields, files }
1567
+ }
1468
1568
  }
1469
1569
 
1470
1570
  // ═══════════════════════════════════════════════════════════════
@@ -2038,4 +2138,7 @@ export default {
2038
2138
 
2039
2139
  // Response envelope
2040
2140
  // ApiResponse interface is exported as named export above
2141
+
2142
+ // File uploads (multipart/form-data)
2143
+ // UploadedFile interface is exported as named export above
2041
2144
  }
@@ -1,13 +1,8 @@
1
1
  // modules/storage/index.ts — Módulo de almacenamiento
2
2
  // Sube y sirve archivos via adapter (local disk, S3, etc.)
3
3
 
4
- export interface FileUpload {
5
- fieldName: string
6
- originalName: string
7
- buffer: Buffer
8
- mimeType: string
9
- size: number
10
- }
4
+ import type { UploadedFile as FileUpload } from '../../kernel/framework'
5
+ export type { UploadedFile as FileUpload } from '../../kernel/framework'
11
6
 
12
7
  export interface StoredFile {
13
8
  url: string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arckode-framework",
3
- "version": "1.2.2",
3
+ "version": "1.3.0",
4
4
  "description": "AI-first TypeScript/Bun framework. Modular, SOLID, zero magic. The AI reads the composition root and knows everything.",
5
5
  "type": "module",
6
6
  "main": "./kernel/framework.ts",
@@ -66,9 +66,17 @@ import type { PedidosSockets } from './sockets'
66
66
  export class PedidosService {
67
67
  private sockets: PedidosSockets = {}
68
68
 
69
- // MERGEno replace. Si dos conectores inyectan sockets, el segundo no pisa al primero.
69
+ // ACUMULAsi dos conectores registran el mismo evento, ambos corren en cadena.
70
+ // NUNCA usar { ...this.sockets, ...s } — pisa silenciosamente el handler anterior.
70
71
  setSockets(s: Partial<PedidosSockets>): void {
71
- this.sockets = { ...this.sockets, ...s }
72
+ const next = s as Record<string, any>
73
+ const cur = this.sockets as Record<string, any>
74
+ for (const key of Object.keys(next)) {
75
+ const h = next[key]
76
+ if (!h) continue
77
+ const prev = cur[key]
78
+ cur[key] = prev ? async (...a: any[]) => { await prev(...a); await h(...a) } : h
79
+ }
72
80
  }
73
81
 
74
82
  async confirmar(id: string): Promise<PedidoDTO> {
@@ -149,7 +157,54 @@ export function conectarPedidoCompleto(ctx: ConnectorContext): void {
149
157
 
150
158
  ---
151
159
 
152
- ## 7. EVENTBUS (alternativa para eventos broadcast)
160
+ ## 7. SOCKETS DENTRO VS FUERA DE TRANSACCIÓN
161
+
162
+ Este es el error más silencioso del patrón sockets. **Hay dos tipos de socket calls:**
163
+
164
+ ### Dentro de `transactor.run()` — SOLO lógica DB crítica
165
+
166
+ ```ts
167
+ // citas/service.ts
168
+ await this.transactor.run(async () => {
169
+ cita = await this.repo.create({ ... })
170
+ await this.sockets.onCitaCreada?.(cita) // ← SOLO ops DB que deben revertirse si fallan
171
+ })
172
+ ```
173
+
174
+ Reglas:
175
+ - El handler DEBE poder fallar y revertir la transacción completa
176
+ - NO hacer llamadas HTTP (WhatsApp, email) — si el HTTP falla, la transacción falla pero el booking ya ocurrió en la DB
177
+ - Solo poner lógica que tenga sentido dentro de una transacción (ej: reservar un slot)
178
+
179
+ ### Post-commit — notificaciones y side effects
180
+
181
+ ```ts
182
+ // Después del transactor — la cita ya existe, no se puede revertir
183
+ await this.sockets.onCitaCreadaCommitted?.(cita) // WhatsApp, email, CRM, etc.
184
+ ```
185
+
186
+ Reglas:
187
+ - Si el handler falla, NO revierte la transacción (ya committeó)
188
+ - Ideal para notificaciones, actualizaciones de otros sistemas
189
+ - Puede hacer HTTP, enviar mensajes, actualizar servicios externos
190
+
191
+ ### Convención de nombres
192
+
193
+ ```ts
194
+ // sockets.ts — separar explícitamente por contexto de ejecución
195
+ export interface MiModuloSockets {
196
+ // Corre DENTRO de transactor.run() — para lógica DB crítica
197
+ onItemCreado?: (item: ItemDTO) => Promise<void>
198
+ // Corre DESPUÉS del commit — para notificaciones y side effects
199
+ onItemCreadoCommitted?: (item: ItemDTO) => Promise<void>
200
+ }
201
+ ```
202
+
203
+ **Regla crítica:** si un socket dispara una llamada HTTP (WhatsApp, email, push notification), DEBE ser un evento `*Committed` post-transacción.
204
+
205
+ ---
206
+
207
+ ## 8. EVENTBUS (alternativa para eventos broadcast)
153
208
 
154
209
  Cuando muchos módulos necesitan reaccionar al mismo evento sin conocerse entre sí:
155
210
 
@@ -253,7 +308,8 @@ test('confirmar pedido funciona sin sockets registrados', async () => {
253
308
 
254
309
  - [ ] Solo importa tipos (no implementaciones) desde `index.ts` de módulos
255
310
  - [ ] Sin `if`, `for`, ni lógica de negocio — solo llamadas de delegación
256
- - [ ] El service emisor implementa `setSockets()` con MERGE (`{...this.sockets, ...s}`) — no replace
311
+ - [ ] `setSockets()` usa el patrón ACUMULATIVO (for loop + chaining) NUNCA `{ ...this.sockets, ...s }`
312
+ - [ ] Sockets con HTTP/WhatsApp/email usan eventos `*Committed` — nunca dentro de `transactor.run()`
257
313
  - [ ] `setSockets()` usa `?.` al llamar los hooks (son opcionales)
258
314
  - [ ] Registrado en composition-root DESPUÉS de los módulos
259
315
  - [ ] El módulo funciona correctamente sin el conector (sockets son opcionales)