@emeryld/rrroutes-server 2.4.2 → 2.4.3

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/index.js CHANGED
@@ -64,6 +64,20 @@ var decodeJsonLikeQueryValue = (value) => {
64
64
  return value;
65
65
  };
66
66
  var CTX_SYMBOL = /* @__PURE__ */ Symbol.for("typedLeaves.ctx");
67
+ var REQUEST_PAYLOAD_SYMBOL = /* @__PURE__ */ Symbol.for(
68
+ "typedLeaves.requestPayload"
69
+ );
70
+ function getRouteRequestPayload(res) {
71
+ const payload = res.locals[REQUEST_PAYLOAD_SYMBOL];
72
+ if (payload) {
73
+ return payload;
74
+ }
75
+ throw new Error("Request payload was not initialized before middleware");
76
+ }
77
+ function setRouteRequestPayload(res, payload) {
78
+ ;
79
+ res.locals[REQUEST_PAYLOAD_SYMBOL] = payload;
80
+ }
67
81
  function getCtx(res) {
68
82
  return res.locals[CTX_SYMBOL];
69
83
  }
@@ -81,6 +95,26 @@ function adaptCtxMw(mw) {
81
95
  }
82
96
  };
83
97
  }
98
+ function adaptRouteBeforeMw(mw) {
99
+ return (req, res, next) => {
100
+ try {
101
+ const result = mw({
102
+ req,
103
+ res,
104
+ next,
105
+ ctx: getCtx(res),
106
+ ...getRouteRequestPayload(res)
107
+ });
108
+ if (result && typeof result.then === "function") {
109
+ return result.catch((err) => next(err));
110
+ }
111
+ return result;
112
+ } catch (err) {
113
+ next(err);
114
+ return void 0;
115
+ }
116
+ };
117
+ }
84
118
  function logHandlerDebugWithRoutesLogger(logger, event) {
85
119
  if (!logger || event.type !== "handler") return;
86
120
  const payload = [
@@ -171,7 +205,9 @@ function createRRRoute(router, config) {
171
205
  const emit = (event) => activeEmit(event, debugName);
172
206
  const isVerboseDebug = activeDebugMode === "complete";
173
207
  emit({ type: "register", method: methodUpper, path });
174
- const routeSpecific = (def?.before ?? []).map((mw) => adaptCtxMw(mw));
208
+ const routeSpecific = (def?.before ?? []).map(
209
+ (mw) => adaptRouteBeforeMw(mw)
210
+ );
175
211
  const derived = buildDerived(leaf);
176
212
  const ctxMw = async (req, res, next) => {
177
213
  const requestUrl = req.originalUrl ?? path;
@@ -183,8 +219,66 @@ function createRRRoute(router, config) {
183
219
  path,
184
220
  url: requestUrl
185
221
  });
222
+ let params;
223
+ let query;
224
+ let body;
186
225
  try {
187
- const ctx = await config.buildCtx(req, res);
226
+ params = leaf.cfg.paramsSchema ? lowProfileParse(leaf.cfg.paramsSchema, req.params) : Object.keys(req.params || {}).length ? req.params : void 0;
227
+ const hasQueryKeys = req.query && Object.keys(req.query || {}).length > 0;
228
+ const parsedQueryInput = leaf.cfg.querySchema && hasQueryKeys ? decodeJsonLikeQueryValue(req.query) : req.query;
229
+ if (leaf.cfg.querySchema) {
230
+ try {
231
+ query = lowProfileParse(
232
+ leaf.cfg.querySchema,
233
+ parsedQueryInput
234
+ );
235
+ } catch (err) {
236
+ const parseError = new Error(
237
+ `Query parsing error: ${err.message ?? String(err)}`
238
+ );
239
+ parseError.raw = JSON.stringify(req.query);
240
+ parseError.cause = err;
241
+ throw parseError;
242
+ }
243
+ } else {
244
+ query = hasQueryKeys ? req.query : void 0;
245
+ }
246
+ body = leaf.cfg.bodySchema ? lowProfileParse(leaf.cfg.bodySchema, req.body) : req.body !== void 0 ? req.body : void 0;
247
+ } catch (err) {
248
+ const payloadError = {
249
+ params,
250
+ query,
251
+ body
252
+ };
253
+ emit(
254
+ decorateDebugEvent(
255
+ isVerboseDebug,
256
+ {
257
+ type: "request",
258
+ stage: "error",
259
+ method: methodUpper,
260
+ path,
261
+ url: requestUrl,
262
+ durationMs: Date.now() - startedAt,
263
+ error: err
264
+ },
265
+ isVerboseDebug ? payloadError : void 0
266
+ )
267
+ );
268
+ next(err);
269
+ return;
270
+ }
271
+ const requestPayload = {
272
+ params,
273
+ query,
274
+ body
275
+ };
276
+ setRouteRequestPayload(res, requestPayload);
277
+ try {
278
+ const ctx = await config.buildCtx({
279
+ req,
280
+ res
281
+ });
188
282
  res.locals[CTX_SYMBOL] = ctx;
189
283
  emit({
190
284
  type: "buildCtx",
@@ -224,9 +318,10 @@ function createRRRoute(router, config) {
224
318
  path,
225
319
  url: requestUrl
226
320
  });
227
- let params;
228
- let query;
229
- let body;
321
+ const requestPayload = getRouteRequestPayload(res);
322
+ const params = requestPayload.params;
323
+ const query = requestPayload.query;
324
+ const body = requestPayload.body;
230
325
  let responsePayload;
231
326
  let hasResponsePayload = false;
232
327
  const downstreamNext = next;
@@ -244,26 +339,6 @@ function createRRRoute(router, config) {
244
339
  }
245
340
  };
246
341
  try {
247
- params = leaf.cfg.paramsSchema ? lowProfileParse(leaf.cfg.paramsSchema, req.params) : Object.keys(req.params || {}).length ? req.params : void 0;
248
- try {
249
- const parsedQueryInput = leaf.cfg.querySchema && req.query ? decodeJsonLikeQueryValue(req.query) : req.query;
250
- query = leaf.cfg.querySchema ? lowProfileParse(leaf.cfg.querySchema, parsedQueryInput) : Object.keys(req.query || {}).length ? req.query : void 0;
251
- } catch (e) {
252
- emitWithCtx({
253
- type: "request",
254
- stage: "error",
255
- method: methodUpper,
256
- path,
257
- url: requestUrl,
258
- error: {
259
- ...e,
260
- raw: JSON.stringify(req.query),
261
- message: `Query parsing error: ${e.message}`
262
- }
263
- });
264
- throw e;
265
- }
266
- body = leaf.cfg.bodySchema ? lowProfileParse(leaf.cfg.bodySchema, req.body) : req.body !== void 0 ? req.body : void 0;
267
342
  const handlerStartedAt = Date.now();
268
343
  emitWithCtx(
269
344
  {
@@ -472,8 +547,12 @@ function createBuiltInConnectionHandlers(opts) {
472
547
  const pingEvent = "sys:ping";
473
548
  const pongEvent = "sys:pong";
474
549
  const heartbeatEnabled = heartbeat?.enabled !== false;
475
- const joinPayloadSchema = buildRoomPayloadSchema(config.joinMetaMessage);
476
- const leavePayloadSchema = buildRoomPayloadSchema(config.leaveMetaMessage);
550
+ const joinPayloadSchema = buildRoomPayloadSchema(
551
+ config.joinMetaMessage
552
+ );
553
+ const leavePayloadSchema = buildRoomPayloadSchema(
554
+ config.leaveMetaMessage
555
+ );
477
556
  const pingPayloadSchema = config.pingPayload;
478
557
  const pongPayloadSchema = config.pongPayload;
479
558
  const sysEvents = sys;