@emeryld/rrroutes-server 2.5.0 → 2.5.2

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.cjs CHANGED
@@ -69,7 +69,8 @@ var normalizeOptions = (options) => {
69
69
  stripPrototypePollutionKeys: options.stripPrototypePollutionKeys ?? true,
70
70
  blockedKeys: new Set(options.blockedKeys ?? defaultBlockedKeys),
71
71
  maxDepth: options.maxDepth ?? defaultMaxDepth,
72
- customSanitizer: options.customSanitizer
72
+ customSanitizer: options.customSanitizer,
73
+ profiler: options.profiler ?? false
73
74
  };
74
75
  };
75
76
  var applyCustomSanitizer = (value, options, context) => {
@@ -138,42 +139,92 @@ var sanitizeValue = (value, options, depth, seen, req, target, path) => {
138
139
  seen.delete(value);
139
140
  return applyCustomSanitizer(objectTarget, options, context);
140
141
  };
142
+ var findPropertyDescriptor = (source, key) => {
143
+ let cursor = source;
144
+ while (cursor) {
145
+ const descriptor = Object.getOwnPropertyDescriptor(cursor, key);
146
+ if (descriptor) return descriptor;
147
+ cursor = Object.getPrototypeOf(cursor);
148
+ }
149
+ return void 0;
150
+ };
151
+ var setRequestQuery = (req, value) => {
152
+ const queryDescriptor = findPropertyDescriptor(req, "query");
153
+ if (!queryDescriptor || queryDescriptor.writable || queryDescriptor.set) {
154
+ ;
155
+ req.query = value;
156
+ return;
157
+ }
158
+ Object.defineProperty(req, "query", {
159
+ configurable: true,
160
+ enumerable: true,
161
+ writable: true,
162
+ value
163
+ });
164
+ };
165
+ var profileTargetSanitization = (options, req, target, sanitize) => {
166
+ if (!options.profiler) {
167
+ sanitize();
168
+ return;
169
+ }
170
+ const startedAt = process.hrtime.bigint();
171
+ try {
172
+ sanitize();
173
+ } finally {
174
+ const durationMs = Number(process.hrtime.bigint() - startedAt) / 1e6;
175
+ const method = req.method ?? "UNKNOWN";
176
+ const url = req.originalUrl ?? req.url ?? "";
177
+ console.info(
178
+ `[RequestSanitizationProfiler] ${method} ${url} target=${target} durationMs=${durationMs.toFixed(3)}`
179
+ );
180
+ }
181
+ };
141
182
  var createRequestSanitizationMiddleware = (options = {}) => {
142
183
  const normalized = normalizeOptions(options);
143
184
  return (req, _res, next) => {
144
185
  try {
145
186
  if (normalized.targets.has("params") && req.params) {
146
- req.params = sanitizeValue(
147
- req.params,
148
- normalized,
149
- 0,
150
- /* @__PURE__ */ new WeakSet(),
151
- req,
152
- "params",
153
- []
154
- );
187
+ profileTargetSanitization(normalized, req, "params", () => {
188
+ req.params = sanitizeValue(
189
+ req.params,
190
+ normalized,
191
+ 0,
192
+ /* @__PURE__ */ new WeakSet(),
193
+ req,
194
+ "params",
195
+ []
196
+ );
197
+ });
155
198
  }
156
- if (normalized.targets.has("query") && req.query) {
157
- req.query = sanitizeValue(
158
- req.query,
159
- normalized,
160
- 0,
161
- /* @__PURE__ */ new WeakSet(),
162
- req,
163
- "query",
164
- []
165
- );
199
+ if (normalized.targets.has("query")) {
200
+ const query = req.query;
201
+ if (query) {
202
+ profileTargetSanitization(normalized, req, "query", () => {
203
+ const sanitizedQuery = sanitizeValue(
204
+ query,
205
+ normalized,
206
+ 0,
207
+ /* @__PURE__ */ new WeakSet(),
208
+ req,
209
+ "query",
210
+ []
211
+ );
212
+ setRequestQuery(req, sanitizedQuery);
213
+ });
214
+ }
166
215
  }
167
216
  if (normalized.targets.has("body") && req.body !== void 0) {
168
- req.body = sanitizeValue(
169
- req.body,
170
- normalized,
171
- 0,
172
- /* @__PURE__ */ new WeakSet(),
173
- req,
174
- "body",
175
- []
176
- );
217
+ profileTargetSanitization(normalized, req, "body", () => {
218
+ req.body = sanitizeValue(
219
+ req.body,
220
+ normalized,
221
+ 0,
222
+ /* @__PURE__ */ new WeakSet(),
223
+ req,
224
+ "body",
225
+ []
226
+ );
227
+ });
177
228
  }
178
229
  next();
179
230
  } catch (err) {