@dim24/servin 0.2.1 → 1.0.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 +757 -78
- package/package.json +1 -1
- package/src/server.js +36 -9
- package/src/tree.js +576 -16
- package/src/websocket.js +332 -6
package/package.json
CHANGED
package/src/server.js
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
import {
|
|
23
23
|
WebSocketBroker,
|
|
24
24
|
createLinearSocket,
|
|
25
|
+
createDeclarativeWsRuntime,
|
|
25
26
|
performWsHandshake
|
|
26
27
|
} from './websocket.js';
|
|
27
28
|
import {
|
|
@@ -105,12 +106,24 @@ export function serve(config = {}) {
|
|
|
105
106
|
const virtualClientCode = generateVirtualClientModule(routes);
|
|
106
107
|
|
|
107
108
|
const broker = new WebSocketBroker();
|
|
109
|
+
const declarativeRuntimes = new Map();
|
|
108
110
|
const sessionManager = config.session ? new SessionManager(config.session, broker) : null;
|
|
109
111
|
const lagMonitor = createLagMonitor(config.lagThresholdMs ?? 60);
|
|
110
112
|
const rateBuckets = new Map();
|
|
111
113
|
const activeDefers = new Set();
|
|
112
114
|
const recordedTests = new Map();
|
|
113
115
|
|
|
116
|
+
function getDeclarativeRuntime(route) {
|
|
117
|
+
const cfg = route.meta?.wsConfig;
|
|
118
|
+
if (!cfg || typeof cfg !== 'object' || (!cfg.open && !cfg.actions && !cfg.close && !cfg.state)) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
if (!declarativeRuntimes.has(route.nodeId)) {
|
|
122
|
+
declarativeRuntimes.set(route.nodeId, createDeclarativeWsRuntime(broker, cfg));
|
|
123
|
+
}
|
|
124
|
+
return declarativeRuntimes.get(route.nodeId);
|
|
125
|
+
}
|
|
126
|
+
|
|
114
127
|
const deferTracker = {
|
|
115
128
|
track(promise) {
|
|
116
129
|
activeDefers.add(promise);
|
|
@@ -444,7 +457,6 @@ export function serve(config = {}) {
|
|
|
444
457
|
await guardFn();
|
|
445
458
|
}
|
|
446
459
|
|
|
447
|
-
// Handle declarative RedirectDescriptor
|
|
448
460
|
if (route.redirectDescriptor) {
|
|
449
461
|
const paramValues = route.paramNames.map((name) => params[name]);
|
|
450
462
|
const queryObj = Object.fromEntries(url.searchParams.entries());
|
|
@@ -462,7 +474,6 @@ export function serve(config = {}) {
|
|
|
462
474
|
});
|
|
463
475
|
}
|
|
464
476
|
|
|
465
|
-
// Handle StaticRouteDescriptor (file or folder)
|
|
466
477
|
if (route.staticDescriptor) {
|
|
467
478
|
const staticRes = await buildStaticHttpResponse(
|
|
468
479
|
route.staticDescriptor,
|
|
@@ -548,7 +559,6 @@ export function serve(config = {}) {
|
|
|
548
559
|
? [...paramValues, payload, queryObj]
|
|
549
560
|
: [...paramValues, queryObj];
|
|
550
561
|
|
|
551
|
-
// SSE Async Generator Streaming
|
|
552
562
|
if (route.isAsyncGen) {
|
|
553
563
|
if (!ctx.isSimulated) await ctx.sessionHandle?.commit(ctx.outgoingHeaders);
|
|
554
564
|
ctx.outgoingHeaders.set('Content-Type', 'text/event-stream; charset=utf-8');
|
|
@@ -580,7 +590,6 @@ export function serve(config = {}) {
|
|
|
580
590
|
return new Response(stream, { status: ctx.statusCode, headers: ctx.outgoingHeaders });
|
|
581
591
|
}
|
|
582
592
|
|
|
583
|
-
// Idempotency Check for Mutations
|
|
584
593
|
const idemHeader = webReq.headers.get('idempotency-key');
|
|
585
594
|
const idemKey = route.nodeIdempotency && idemHeader ? `${route.nodeId}:${idemHeader}` : null;
|
|
586
595
|
if (idemKey && !ctx.isSimulated) {
|
|
@@ -594,7 +603,6 @@ export function serve(config = {}) {
|
|
|
594
603
|
}
|
|
595
604
|
}
|
|
596
605
|
|
|
597
|
-
// Self-Invalidating Cache Lookup for GET
|
|
598
606
|
const cacheKey = route.nodeCache && method === 'GET'
|
|
599
607
|
? `${route.nodeId}:${JSON.stringify(params)}:${url.search}`
|
|
600
608
|
: null;
|
|
@@ -648,7 +656,6 @@ export function serve(config = {}) {
|
|
|
648
656
|
rawResult = await executeHandler();
|
|
649
657
|
}
|
|
650
658
|
|
|
651
|
-
// Invalidate node cache and ancestor caches on successful mutations
|
|
652
659
|
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !ctx.isSimulated) {
|
|
653
660
|
if (route.nodeCache) {
|
|
654
661
|
const prefix = `${route.nodeId}:${JSON.stringify(params)}:`;
|
|
@@ -930,16 +937,36 @@ export function serve(config = {}) {
|
|
|
930
937
|
});
|
|
931
938
|
|
|
932
939
|
const sid = ctx.sessionHandle?.sid || null;
|
|
940
|
+
const wsCfg = match.route.meta.wsConfig || {};
|
|
933
941
|
const linearSocket = createLinearSocket(
|
|
934
942
|
tcpSocket,
|
|
935
943
|
head,
|
|
936
944
|
broker,
|
|
937
|
-
|
|
945
|
+
wsCfg,
|
|
938
946
|
sid
|
|
939
947
|
);
|
|
940
948
|
|
|
941
|
-
const
|
|
942
|
-
|
|
949
|
+
const connData = {};
|
|
950
|
+
for (const [k, v] of url.searchParams.entries()) {
|
|
951
|
+
if (k !== '__lastSeq') connData[k] = v;
|
|
952
|
+
}
|
|
953
|
+
const lastSeq = Number(url.searchParams.get('__lastSeq') || 0);
|
|
954
|
+
const reqInfo = {
|
|
955
|
+
raw: nodeReq,
|
|
956
|
+
url,
|
|
957
|
+
data: connData,
|
|
958
|
+
query: url.searchParams,
|
|
959
|
+
params: match.params,
|
|
960
|
+
lastSeq
|
|
961
|
+
};
|
|
962
|
+
|
|
963
|
+
const declarativeRunner = getDeclarativeRuntime(match.route);
|
|
964
|
+
if (declarativeRunner) {
|
|
965
|
+
await declarativeRunner(linearSocket, reqInfo);
|
|
966
|
+
} else {
|
|
967
|
+
const paramValues = match.route.paramNames.map((n) => match.params[n]);
|
|
968
|
+
await match.route.handler(...paramValues, linearSocket);
|
|
969
|
+
}
|
|
943
970
|
linearSocket.close(1000, 'Session completed');
|
|
944
971
|
} catch (err) {
|
|
945
972
|
if (err instanceof ServinAbortError) {
|