@emeryld/rrroutes-server 2.2.16 → 2.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.
- package/README.md +104 -65
- package/dist/index.cjs +87 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +87 -21
- package/dist/index.js.map +1 -1
- package/dist/routesV3.server.d.ts +28 -34
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -8,7 +8,10 @@ var serverDebugEventTypes = [
|
|
|
8
8
|
var noopServerEmit = () => {
|
|
9
9
|
};
|
|
10
10
|
function createServerDebugEmitter(option) {
|
|
11
|
-
const disabled = {
|
|
11
|
+
const disabled = {
|
|
12
|
+
emit: noopServerEmit,
|
|
13
|
+
mode: "minimal"
|
|
14
|
+
};
|
|
12
15
|
if (!option) return disabled;
|
|
13
16
|
if (typeof option === "object") {
|
|
14
17
|
const toggles = option;
|
|
@@ -31,8 +34,11 @@ function createServerDebugEmitter(option) {
|
|
|
31
34
|
}
|
|
32
35
|
var keyOf = (leaf) => `${leaf.method.toUpperCase()} ${leaf.path}`;
|
|
33
36
|
var CTX_SYMBOL = Symbol.for("typedLeaves.ctx");
|
|
34
|
-
var AFTER_HANDLER_NEXT_SYMBOL = Symbol.for(
|
|
37
|
+
var AFTER_HANDLER_NEXT_SYMBOL = Symbol.for(
|
|
38
|
+
"typedLeaves.afterHandlerNext"
|
|
39
|
+
);
|
|
35
40
|
function setAfterHandlerNext(res, value) {
|
|
41
|
+
;
|
|
36
42
|
res.locals[AFTER_HANDLER_NEXT_SYMBOL] = value;
|
|
37
43
|
}
|
|
38
44
|
function handlerInvokedNext(res) {
|
|
@@ -75,9 +81,11 @@ function logHandlerDebugWithRoutesLogger(logger, event) {
|
|
|
75
81
|
event
|
|
76
82
|
];
|
|
77
83
|
if (event.stage === "error") {
|
|
84
|
+
;
|
|
78
85
|
(logger.error ?? logger.warn ?? logger.debug ?? logger.info ?? logger.log ?? logger.system)?.call(logger, ...payload);
|
|
79
86
|
return;
|
|
80
87
|
}
|
|
88
|
+
;
|
|
81
89
|
(logger.debug ?? logger.verbose ?? logger.info ?? logger.log ?? logger.system)?.call(logger, ...payload);
|
|
82
90
|
}
|
|
83
91
|
var defaultSend = (res, data) => {
|
|
@@ -99,7 +107,9 @@ function collectRoutesFromStack(appOrRouter) {
|
|
|
99
107
|
const route = layer && layer.route;
|
|
100
108
|
if (!route) continue;
|
|
101
109
|
const paths = Array.isArray(route.path) ? route.path : [route.path];
|
|
102
|
-
const methodEntries = Object.entries(route.methods ?? {}).filter(
|
|
110
|
+
const methodEntries = Object.entries(route.methods ?? {}).filter(
|
|
111
|
+
([, enabled]) => enabled
|
|
112
|
+
);
|
|
103
113
|
for (const path of paths) {
|
|
104
114
|
for (const [method] of methodEntries) {
|
|
105
115
|
result.push(`${method.toUpperCase()} ${path}`);
|
|
@@ -140,11 +150,14 @@ function createRRRoute(router, config) {
|
|
|
140
150
|
let routeDebugEmitter;
|
|
141
151
|
if (defDebug) {
|
|
142
152
|
const { debugName: overrideName, ...rest } = defDebug;
|
|
143
|
-
const hasOverrides = Object.values(rest).some(
|
|
153
|
+
const hasOverrides = Object.values(rest).some(
|
|
154
|
+
(value) => value !== void 0
|
|
155
|
+
);
|
|
144
156
|
if (hasOverrides) {
|
|
145
|
-
routeDebugEmitter = createServerDebugEmitter(
|
|
146
|
-
|
|
147
|
-
|
|
157
|
+
routeDebugEmitter = createServerDebugEmitter({
|
|
158
|
+
...config.debug,
|
|
159
|
+
...rest
|
|
160
|
+
});
|
|
148
161
|
}
|
|
149
162
|
debugName = overrideName ?? debugName;
|
|
150
163
|
}
|
|
@@ -158,7 +171,13 @@ function createRRRoute(router, config) {
|
|
|
158
171
|
const ctxMw = async (req, res, next) => {
|
|
159
172
|
const requestUrl = req.originalUrl ?? path;
|
|
160
173
|
const startedAt = Date.now();
|
|
161
|
-
emit({
|
|
174
|
+
emit({
|
|
175
|
+
type: "buildCtx",
|
|
176
|
+
stage: "start",
|
|
177
|
+
method: methodUpper,
|
|
178
|
+
path,
|
|
179
|
+
url: requestUrl
|
|
180
|
+
});
|
|
162
181
|
try {
|
|
163
182
|
const ctx = await config.buildCtx(req, res);
|
|
164
183
|
res.locals[CTX_SYMBOL] = ctx;
|
|
@@ -185,11 +204,22 @@ function createRRRoute(router, config) {
|
|
|
185
204
|
next(err);
|
|
186
205
|
}
|
|
187
206
|
};
|
|
188
|
-
const before = [
|
|
207
|
+
const before = [
|
|
208
|
+
ctxMw,
|
|
209
|
+
...globalBeforeMws,
|
|
210
|
+
...derived,
|
|
211
|
+
...routeSpecific
|
|
212
|
+
];
|
|
189
213
|
const wrapped = async (req, res, next) => {
|
|
190
214
|
const requestUrl = req.originalUrl.split("?")[0] ?? path;
|
|
191
215
|
const startedAt = Date.now();
|
|
192
|
-
emit({
|
|
216
|
+
emit({
|
|
217
|
+
type: "request",
|
|
218
|
+
stage: "start",
|
|
219
|
+
method: methodUpper,
|
|
220
|
+
path,
|
|
221
|
+
url: requestUrl
|
|
222
|
+
});
|
|
193
223
|
let params;
|
|
194
224
|
let query;
|
|
195
225
|
let body;
|
|
@@ -336,10 +366,13 @@ function createRRRoute(router, config) {
|
|
|
336
366
|
const key = keyOf(leaf);
|
|
337
367
|
knownLeaves.set(key, leaf);
|
|
338
368
|
}
|
|
369
|
+
;
|
|
339
370
|
Object.keys(controllers).forEach((key) => {
|
|
340
371
|
const leaf = registry.byKey[key];
|
|
341
372
|
if (!leaf) {
|
|
342
|
-
logger?.warn?.(
|
|
373
|
+
logger?.warn?.(
|
|
374
|
+
`No leaf found for controller key: ${key}. Not registering route.`
|
|
375
|
+
);
|
|
343
376
|
return;
|
|
344
377
|
}
|
|
345
378
|
const def = controllers[key];
|
|
@@ -364,7 +397,9 @@ function createRRRoute(router, config) {
|
|
|
364
397
|
function warnMissing(registry, warnLogger) {
|
|
365
398
|
const registeredFromStore = new Set(Array.from(registered));
|
|
366
399
|
if (registeredFromStore.size === 0) {
|
|
367
|
-
collectRoutesFromStack(router).forEach(
|
|
400
|
+
collectRoutesFromStack(router).forEach(
|
|
401
|
+
(key) => registeredFromStore.add(key)
|
|
402
|
+
);
|
|
368
403
|
}
|
|
369
404
|
for (const leaf of registry.all) {
|
|
370
405
|
const key = keyOf(leaf);
|
|
@@ -508,7 +543,12 @@ function createBuiltInConnectionHandlers(opts) {
|
|
|
508
543
|
const list = toArray(parsed.data.rooms);
|
|
509
544
|
const join = async (room) => {
|
|
510
545
|
await socket.join(room);
|
|
511
|
-
dbg(null, {
|
|
546
|
+
dbg(null, {
|
|
547
|
+
type: "rooms",
|
|
548
|
+
action: "join",
|
|
549
|
+
rooms: room,
|
|
550
|
+
socketId: socket.id
|
|
551
|
+
});
|
|
512
552
|
};
|
|
513
553
|
const run = async () => {
|
|
514
554
|
await getSysEvent("sys:room_join")({
|
|
@@ -522,7 +562,9 @@ function createBuiltInConnectionHandlers(opts) {
|
|
|
522
562
|
try {
|
|
523
563
|
await run();
|
|
524
564
|
} catch (error) {
|
|
525
|
-
socket.emit(`${roomJoinEvent}:error`, {
|
|
565
|
+
socket.emit(`${roomJoinEvent}:error`, {
|
|
566
|
+
error: normalizeError(error)
|
|
567
|
+
});
|
|
526
568
|
dbg(roomJoinEvent, {
|
|
527
569
|
type: "rooms",
|
|
528
570
|
action: "join",
|
|
@@ -560,7 +602,12 @@ function createBuiltInConnectionHandlers(opts) {
|
|
|
560
602
|
const list = toArray(parsed.data.rooms);
|
|
561
603
|
const leave = async (room) => {
|
|
562
604
|
await socket.leave(room);
|
|
563
|
-
dbg(null, {
|
|
605
|
+
dbg(null, {
|
|
606
|
+
type: "rooms",
|
|
607
|
+
action: "leave",
|
|
608
|
+
rooms: room,
|
|
609
|
+
socketId: socket.id
|
|
610
|
+
});
|
|
564
611
|
};
|
|
565
612
|
const run = async () => {
|
|
566
613
|
await getSysEvent("sys:room_leave")({
|
|
@@ -574,7 +621,9 @@ function createBuiltInConnectionHandlers(opts) {
|
|
|
574
621
|
try {
|
|
575
622
|
await run();
|
|
576
623
|
} catch (error) {
|
|
577
|
-
socket.emit(`${roomLeaveEvent}:error`, {
|
|
624
|
+
socket.emit(`${roomLeaveEvent}:error`, {
|
|
625
|
+
error: normalizeError(error)
|
|
626
|
+
});
|
|
578
627
|
dbg(roomJoinEvent, {
|
|
579
628
|
type: "rooms",
|
|
580
629
|
phase: "handler_error",
|
|
@@ -623,7 +672,12 @@ function createBuiltInConnectionHandlers(opts) {
|
|
|
623
672
|
});
|
|
624
673
|
let pongPayload;
|
|
625
674
|
try {
|
|
626
|
-
pongPayload = await getSysEvent("sys:ping")({
|
|
675
|
+
pongPayload = await getSysEvent("sys:ping")({
|
|
676
|
+
ping: parsedPing.data,
|
|
677
|
+
ctx,
|
|
678
|
+
socket,
|
|
679
|
+
helper
|
|
680
|
+
});
|
|
627
681
|
} catch (error) {
|
|
628
682
|
socket.emit(`${pingEvent}:error`, { error: normalizeError(error) });
|
|
629
683
|
dbg(null, {
|
|
@@ -788,7 +842,9 @@ function createSocketConnections(io, events, opts) {
|
|
|
788
842
|
},
|
|
789
843
|
metadata
|
|
790
844
|
});
|
|
791
|
-
throw new Error(
|
|
845
|
+
throw new Error(
|
|
846
|
+
`Invalid payload for "${String(eventName)}": ${check.error.message}`
|
|
847
|
+
);
|
|
792
848
|
}
|
|
793
849
|
const envelope = {
|
|
794
850
|
eventName,
|
|
@@ -849,7 +905,12 @@ function createSocketConnections(io, events, opts) {
|
|
|
849
905
|
}
|
|
850
906
|
}
|
|
851
907
|
registrations.delete(eventName);
|
|
852
|
-
dbg(eventName, {
|
|
908
|
+
dbg(eventName, {
|
|
909
|
+
type: "register",
|
|
910
|
+
action: "unregister",
|
|
911
|
+
event: eventName,
|
|
912
|
+
msg: "unregistered"
|
|
913
|
+
});
|
|
853
914
|
};
|
|
854
915
|
const {
|
|
855
916
|
builtInConnectionListener,
|
|
@@ -935,7 +996,10 @@ function createSocketConnections(io, events, opts) {
|
|
|
935
996
|
});
|
|
936
997
|
};
|
|
937
998
|
io.on("connection", connectionListener);
|
|
938
|
-
addRegistration(String(eventName), {
|
|
999
|
+
addRegistration(String(eventName), {
|
|
1000
|
+
connectionListener,
|
|
1001
|
+
socketListeners
|
|
1002
|
+
});
|
|
939
1003
|
dbg(String(eventName), {
|
|
940
1004
|
type: "register",
|
|
941
1005
|
action: "register",
|
|
@@ -1081,7 +1145,9 @@ var createConnectionLoggingMiddleware = (options = {}) => {
|
|
|
1081
1145
|
const logger = options.logger ?? defaultLogger;
|
|
1082
1146
|
const includeHeaders = options.includeHeaders ?? false;
|
|
1083
1147
|
const redactKeys = new Set(
|
|
1084
|
-
(options.redactAuthKeys ?? ["authorization", "token"]).map(
|
|
1148
|
+
(options.redactAuthKeys ?? ["authorization", "token"]).map(
|
|
1149
|
+
(key) => key.toLowerCase()
|
|
1150
|
+
)
|
|
1085
1151
|
);
|
|
1086
1152
|
return (socket, next) => {
|
|
1087
1153
|
const context = buildContext(socket, includeHeaders, redactKeys);
|