@adaas/a-server 0.0.23 → 0.0.24
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/dist/browser/index.d.mts +30 -0
- package/dist/browser/index.mjs +68 -2
- package/dist/browser/index.mjs.map +1 -1
- package/dist/node/{A-Response.entity-BVYAc6-8.d.mts → A-Response.entity-6qhiV7BE.d.mts} +30 -0
- package/dist/node/{A-Response.entity-CRc-t-vr.d.ts → A-Response.entity-bjh6bofZ.d.ts} +30 -0
- package/dist/node/controllers/A-CommandController/A-CommandController.component.d.mts +1 -1
- package/dist/node/controllers/A-CommandController/A-CommandController.component.d.ts +1 -1
- package/dist/node/controllers/A-EntityController/A-EntityController.component.d.mts +1 -1
- package/dist/node/controllers/A-EntityController/A-EntityController.component.d.ts +1 -1
- package/dist/node/controllers/A-ListingController/A-ListingController.component.d.mts +1 -1
- package/dist/node/controllers/A-ListingController/A-ListingController.component.d.ts +1 -1
- package/dist/node/controllers/A-ServerHealthMonitor/A-ServerHealthMonitor.component.d.mts +1 -1
- package/dist/node/controllers/A-ServerHealthMonitor/A-ServerHealthMonitor.component.d.ts +1 -1
- package/dist/node/index.d.mts +1 -1
- package/dist/node/index.d.ts +1 -1
- package/dist/node/lib/A-Response/A-Response.entity.d.mts +1 -1
- package/dist/node/lib/A-Response/A-Response.entity.d.ts +1 -1
- package/dist/node/lib/A-Response/A-Response.entity.js +65 -1
- package/dist/node/lib/A-Response/A-Response.entity.js.map +1 -1
- package/dist/node/lib/A-Response/A-Response.entity.mjs +65 -1
- package/dist/node/lib/A-Response/A-Response.entity.mjs.map +1 -1
- package/dist/node/lib/A-Response/A-Response.types.d.mts +1 -1
- package/dist/node/lib/A-Response/A-Response.types.d.ts +1 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.d.mts +1 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.d.ts +1 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.js +3 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.js.map +1 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.mjs +3 -1
- package/dist/node/lib/A-Server/A-HttpServer.container.mjs.map +1 -1
- package/dist/node/lib/A-ServerController/A-ServerController.component.d.mts +1 -1
- package/dist/node/lib/A-ServerController/A-ServerController.component.d.ts +1 -1
- package/dist/node/lib/A-ServerLogger/A-ServerLogger.component.d.mts +1 -1
- package/dist/node/lib/A-ServerLogger/A-ServerLogger.component.d.ts +1 -1
- package/dist/node/lib/A-ServerProxy/A-ServerProxy.component.d.mts +1 -1
- package/dist/node/lib/A-ServerProxy/A-ServerProxy.component.d.ts +1 -1
- package/dist/node/lib/A-ServerRouter/A-ServerRouter.component.d.mts +1 -1
- package/dist/node/lib/A-ServerRouter/A-ServerRouter.component.d.ts +1 -1
- package/dist/node/lib/A-ServerStatic/A-ServerStatic.component.d.mts +1 -1
- package/dist/node/lib/A-ServerStatic/A-ServerStatic.component.d.ts +1 -1
- package/dist/node/middlewares/A-ServerCORS/A_ServerCORS.component.d.mts +1 -1
- package/dist/node/middlewares/A-ServerCORS/A_ServerCORS.component.d.ts +1 -1
- package/package.json +1 -1
- package/src/lib/A-Response/A-Response.entity.ts +63 -1
- package/src/lib/A-Server/A-HttpServer.container.ts +6 -1
|
@@ -89,6 +89,10 @@ export class A_Response<_ResponseType = any> extends A_Entity<
|
|
|
89
89
|
* Event listeners map for A_Response events
|
|
90
90
|
*/
|
|
91
91
|
private _listeners: Map<A_ResponseFeatureNames, Set<A_Response_Listener>> = new Map();
|
|
92
|
+
/**
|
|
93
|
+
* Whether this response is operating as a persistent SSE stream
|
|
94
|
+
*/
|
|
95
|
+
private _isStreaming: boolean = false;
|
|
92
96
|
|
|
93
97
|
|
|
94
98
|
constructor(params: A_Response_Constructor, options?: A_Response_Options) {
|
|
@@ -158,6 +162,14 @@ export class A_Response<_ResponseType = any> extends A_Entity<
|
|
|
158
162
|
get size(): number {
|
|
159
163
|
return this.original.getHeader('Content-Length') as number || 0;
|
|
160
164
|
}
|
|
165
|
+
/**
|
|
166
|
+
* Whether this response is in SSE streaming mode.
|
|
167
|
+
* When true the server container will NOT auto-send and destroy() will
|
|
168
|
+
* leave the underlying socket open.
|
|
169
|
+
*/
|
|
170
|
+
get isStreaming(): boolean {
|
|
171
|
+
return this._isStreaming;
|
|
172
|
+
}
|
|
161
173
|
|
|
162
174
|
// ======================================================================================
|
|
163
175
|
// --------------------------------------------------------------------------
|
|
@@ -188,7 +200,7 @@ export class A_Response<_ResponseType = any> extends A_Entity<
|
|
|
188
200
|
*/
|
|
189
201
|
async destroy(): Promise<any> {
|
|
190
202
|
|
|
191
|
-
if (!this.original.destroyed) {
|
|
203
|
+
if (!this.original.destroyed && !this._isStreaming) {
|
|
192
204
|
this.original.end();
|
|
193
205
|
this._listeners.clear();
|
|
194
206
|
this.original.removeAllListeners();
|
|
@@ -312,6 +324,56 @@ export class A_Response<_ResponseType = any> extends A_Entity<
|
|
|
312
324
|
}));
|
|
313
325
|
}
|
|
314
326
|
}
|
|
327
|
+
// ======================================================================================
|
|
328
|
+
// --------------------------------------------------------------------------
|
|
329
|
+
// A-Response SSE (Server-Sent Events) Methods
|
|
330
|
+
// --------------------------------------------------------------------------
|
|
331
|
+
// ======================================================================================
|
|
332
|
+
/**
|
|
333
|
+
* Upgrade this response to a persistent SSE stream.
|
|
334
|
+
* Sends the required headers and writes the initial `:ok` comment to flush
|
|
335
|
+
* the connection. After calling this the response will NOT be auto-closed
|
|
336
|
+
* by the server container or by destroy().
|
|
337
|
+
*/
|
|
338
|
+
public sseOpen(): void {
|
|
339
|
+
if (this.headersSent || this._isStreaming) return;
|
|
340
|
+
this._isStreaming = true;
|
|
341
|
+
this.original.writeHead(200, {
|
|
342
|
+
'Content-Type': 'text/event-stream',
|
|
343
|
+
'Cache-Control': 'no-cache, no-transform',
|
|
344
|
+
'Connection': 'keep-alive',
|
|
345
|
+
'X-Accel-Buffering': 'no',
|
|
346
|
+
});
|
|
347
|
+
this.original.write(':ok\n\n');
|
|
348
|
+
// Reset streaming flag when the client disconnects
|
|
349
|
+
this.original.once('close', () => {
|
|
350
|
+
this._isStreaming = false;
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Write a named SSE event onto the open stream.
|
|
355
|
+
* Format: `event: <name>\ndata: <JSON>\n\n`
|
|
356
|
+
*
|
|
357
|
+
* Compatible with browser EventSource `addEventListener(name, handler)`.
|
|
358
|
+
*
|
|
359
|
+
* @returns false when the channel is no longer writable
|
|
360
|
+
*/
|
|
361
|
+
public sseWrite(event: string, data?: any): boolean {
|
|
362
|
+
if (!this._isStreaming || this.original.destroyed) return false;
|
|
363
|
+
try {
|
|
364
|
+
return this.original.write(`event: ${event}\ndata: ${JSON.stringify(data ?? {})}\n\n`);
|
|
365
|
+
} catch {
|
|
366
|
+
return false;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Close the SSE stream gracefully.
|
|
371
|
+
*/
|
|
372
|
+
public sseClose(): void {
|
|
373
|
+
if (!this._isStreaming || this.original.destroyed) return;
|
|
374
|
+
this._isStreaming = false;
|
|
375
|
+
this.original.end();
|
|
376
|
+
}
|
|
315
377
|
/**
|
|
316
378
|
* Write head with status and headers
|
|
317
379
|
*/
|
|
@@ -197,7 +197,12 @@ export class A_HttpServer extends A_Service {
|
|
|
197
197
|
|
|
198
198
|
|
|
199
199
|
req.clearTimeout();
|
|
200
|
-
|
|
200
|
+
|
|
201
|
+
// For SSE streams the controller calls response.sseOpen() which keeps
|
|
202
|
+
// the socket alive; skip the auto-send so we don't close it here.
|
|
203
|
+
if (!res.isStreaming) {
|
|
204
|
+
await res.status(200).send();
|
|
205
|
+
}
|
|
201
206
|
|
|
202
207
|
resolve();
|
|
203
208
|
} catch (error) {
|