elysia 1.4.30 → 1.4.31

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/error.js CHANGED
@@ -22,6 +22,12 @@ __export(error_exports, {
22
22
  InvalidCookieSignature: () => InvalidCookieSignature,
23
23
  InvalidFileType: () => InvalidFileType,
24
24
  MAX_ERRORS: () => MAX_ERRORS,
25
+ MAX_VALIDATION_ERROR_CONTAINER_ENTRIES: () => MAX_VALIDATION_ERROR_CONTAINER_ENTRIES,
26
+ MAX_VALIDATION_ERROR_DEPTH: () => MAX_VALIDATION_ERROR_DEPTH,
27
+ MAX_VALIDATION_ERROR_ENTRIES: () => MAX_VALIDATION_ERROR_ENTRIES,
28
+ MAX_VALIDATION_ERROR_NODES: () => MAX_VALIDATION_ERROR_NODES,
29
+ MAX_VALIDATION_ERROR_OUTPUT_LENGTH: () => MAX_VALIDATION_ERROR_OUTPUT_LENGTH,
30
+ MAX_VALIDATION_ERROR_STRING_LENGTH: () => MAX_VALIDATION_ERROR_STRING_LENGTH,
25
31
  NotFoundError: () => NotFoundError,
26
32
  ParseError: () => ParseError,
27
33
  ValidationError: () => ValidationError,
@@ -106,9 +112,10 @@ const mapValueError = (error) => {
106
112
  summary: isRoot ? "Value should be an email" : `Property '${property}' should be ${format}`
107
113
  };
108
114
  case 54:
115
+ const found = formatValidationSummaryValue(value);
109
116
  return {
110
117
  ...error,
111
- summary: `${message.slice(0, 9).trim()} property '${property}' to be ${message.slice(8).trim()} but found: ${value}`
118
+ summary: `${message.slice(0, 9).trim()} property '${property}' to be ${message.slice(8).trim()} but found: ${found}`
112
119
  };
113
120
  case 62:
114
121
  const union = error.schema.anyOf.map((x) => `'${x?.format ?? x.type}'`).join(", ");
@@ -119,11 +126,217 @@ const mapValueError = (error) => {
119
126
  default:
120
127
  return { summary: message, ...error };
121
128
  }
122
- }, MAX_ERRORS = 64, takeErrors = (iterator, limit = MAX_ERRORS) => {
129
+ }, MAX_ERRORS = 64, MAX_VALIDATION_ERROR_DEPTH = 16, MAX_VALIDATION_ERROR_NODES = 8192, MAX_VALIDATION_ERROR_ENTRIES = 4096, MAX_VALIDATION_ERROR_CONTAINER_ENTRIES = 512, MAX_VALIDATION_ERROR_STRING_LENGTH = 4096, MAX_VALIDATION_ERROR_OUTPUT_LENGTH = 65536, VALIDATION_TRUNCATED = "[Truncated]", VALIDATION_UNSERIALIZABLE = "[Unserializable]", OMIT_VALIDATION_VALUE = Symbol("omitValidationValue"), boundValidationString = (value, state) => {
130
+ const remaining = state ? MAX_VALIDATION_ERROR_OUTPUT_LENGTH - state.strings : MAX_VALIDATION_ERROR_STRING_LENGTH, limit = Math.min(MAX_VALIDATION_ERROR_STRING_LENGTH, remaining);
131
+ return value.length <= limit ? (state && (state.strings += value.length), value) : (state && (state.strings += Math.max(limit, 0)), limit <= 0 ? "" : limit <= VALIDATION_TRUNCATED.length ? VALIDATION_TRUNCATED.slice(0, limit) : value.slice(0, limit - VALIDATION_TRUNCATED.length) + VALIDATION_TRUNCATED);
132
+ }, formatValidationSummaryValue = (value) => {
133
+ if (typeof value == "string") return boundValidationString(value);
134
+ if (value === null || typeof value == "number" || typeof value == "boolean" || typeof value == "bigint" || typeof value > "u")
135
+ return String(value);
136
+ try {
137
+ if (Array.isArray(value)) {
138
+ const length = Object.getOwnPropertyDescriptor(
139
+ value,
140
+ "length"
141
+ )?.value;
142
+ if (typeof length != "number" || length > MAX_VALIDATION_ERROR_CONTAINER_ENTRIES)
143
+ return "[object Object]";
144
+ const output = [];
145
+ let size = 0;
146
+ for (let index = 0; index < length; index++) {
147
+ const descriptor = Object.getOwnPropertyDescriptor(
148
+ value,
149
+ `${index}`
150
+ );
151
+ if (!descriptor || !("value" in descriptor)) {
152
+ output.push("");
153
+ continue;
154
+ }
155
+ const item = descriptor.value;
156
+ if (item == null) output.push("");
157
+ else if (typeof item == "string" || typeof item == "number" || typeof item == "boolean" || typeof item == "bigint") {
158
+ const part = String(item);
159
+ if (size += part.length + 1, size > MAX_VALIDATION_ERROR_STRING_LENGTH)
160
+ return boundValidationString(output.join(","));
161
+ output.push(part);
162
+ } else return "[object Object]";
163
+ }
164
+ return boundValidationString(output.join(","));
165
+ }
166
+ } catch {
167
+ }
168
+ return "[object Object]";
169
+ }, validationOutputFits = (value) => {
170
+ let bytes = 0;
171
+ for (let index = 0; index < value.length; index++) {
172
+ const code = value.charCodeAt(index);
173
+ if (code < 128 ? bytes++ : code < 2048 ? bytes += 2 : code >= 55296 && code <= 56319 && value.charCodeAt(index + 1) >= 56320 && value.charCodeAt(index + 1) <= 57343 ? (bytes += 4, index++) : bytes += 3, bytes > MAX_VALIDATION_ERROR_OUTPUT_LENGTH) return !1;
174
+ }
175
+ return !0;
176
+ }, projectValidationValue = (value, state, depth = 0) => {
177
+ if (state.nodes++ >= MAX_VALIDATION_ERROR_NODES) return VALIDATION_TRUNCATED;
178
+ switch (typeof value) {
179
+ case "string":
180
+ return boundValidationString(value, state);
181
+ case "number":
182
+ return Number.isFinite(value) ? value : null;
183
+ case "boolean":
184
+ return value;
185
+ case "bigint":
186
+ return "[BigInt]";
187
+ case "undefined":
188
+ case "function":
189
+ case "symbol":
190
+ return OMIT_VALIDATION_VALUE;
191
+ }
192
+ if (typeof value != "object" || value === null) return null;
193
+ if (depth >= MAX_VALIDATION_ERROR_DEPTH) return VALIDATION_TRUNCATED;
194
+ if (state.depth.has(value)) return "[Circular]";
195
+ try {
196
+ if (value instanceof Date) {
197
+ const timestamp = Date.prototype.getTime.call(value);
198
+ return Number.isFinite(timestamp) ? boundValidationString(
199
+ Date.prototype.toISOString.call(value),
200
+ state
201
+ ) : null;
202
+ }
203
+ } catch {
204
+ }
205
+ state.depth.add(value);
206
+ try {
207
+ if (Array.isArray(value)) {
208
+ const output2 = [], lengthDescriptor = Object.getOwnPropertyDescriptor(
209
+ value,
210
+ "length"
211
+ ), length = typeof lengthDescriptor?.value == "number" ? lengthDescriptor.value : 0;
212
+ let index = 0;
213
+ for (; index < length && index < MAX_VALIDATION_ERROR_CONTAINER_ENTRIES && state.entries < MAX_VALIDATION_ERROR_ENTRIES && state.nodes < MAX_VALIDATION_ERROR_NODES; index++) {
214
+ state.entries++;
215
+ const descriptor = Object.getOwnPropertyDescriptor(
216
+ value,
217
+ `${index}`
218
+ ), projected = descriptor ? "value" in descriptor ? projectValidationValue(
219
+ descriptor.value,
220
+ state,
221
+ depth + 1
222
+ ) : VALIDATION_UNSERIALIZABLE : OMIT_VALIDATION_VALUE;
223
+ output2.push(
224
+ projected === OMIT_VALIDATION_VALUE ? null : projected
225
+ );
226
+ }
227
+ return index < length && output2.push(VALIDATION_TRUNCATED), state.preservePrototype || Object.setPrototypeOf(output2, null), output2;
228
+ }
229
+ const output = state.preservePrototype ? {} : /* @__PURE__ */ Object.create(null);
230
+ let truncated = !1, entries = 0, inspected = 0;
231
+ for (const key in value) {
232
+ if (inspected++ >= MAX_VALIDATION_ERROR_CONTAINER_ENTRIES) {
233
+ truncated = !0;
234
+ break;
235
+ }
236
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
237
+ if (!descriptor?.enumerable) continue;
238
+ if (entries >= MAX_VALIDATION_ERROR_CONTAINER_ENTRIES || state.entries >= MAX_VALIDATION_ERROR_ENTRIES || state.nodes >= MAX_VALIDATION_ERROR_NODES) {
239
+ truncated = !0;
240
+ break;
241
+ }
242
+ entries++, state.entries++;
243
+ const projected = "value" in descriptor ? projectValidationValue(descriptor.value, state, depth + 1) : VALIDATION_UNSERIALIZABLE;
244
+ projected !== OMIT_VALIDATION_VALUE && Object.defineProperty(output, boundValidationString(key, state), {
245
+ value: projected,
246
+ enumerable: !0,
247
+ configurable: !0,
248
+ writable: !0
249
+ });
250
+ }
251
+ return truncated && Object.defineProperty(output, VALIDATION_TRUNCATED, {
252
+ value: !0,
253
+ enumerable: !0
254
+ }), output;
255
+ } catch {
256
+ return VALIDATION_UNSERIALIZABLE;
257
+ } finally {
258
+ state.depth.delete(value);
259
+ }
260
+ }, validationSerializationFallback = () => {
261
+ const fallback = /* @__PURE__ */ Object.create(null);
262
+ return fallback.type = "validation", fallback.message = "Validation details exceeded serialization limit", fallback;
263
+ }, projectValidationDetails = (value, preservePrototype = !1) => {
264
+ const projected = projectValidationValue(value, {
265
+ depth: /* @__PURE__ */ new WeakSet(),
266
+ nodes: 0,
267
+ entries: 0,
268
+ strings: 0,
269
+ preservePrototype
270
+ }), result = projected === OMIT_VALIDATION_VALUE ? null : projected;
271
+ try {
272
+ const serialized = JSON.stringify(result);
273
+ if (typeof serialized == "string" && validationOutputFits(serialized))
274
+ return result;
275
+ } catch {
276
+ }
277
+ return validationSerializationFallback();
278
+ }, stringifyValidationDetails = (value, space) => {
279
+ const projected = projectValidationDetails(value);
280
+ try {
281
+ const serialized = JSON.stringify(projected, null, space);
282
+ if (validationOutputFits(serialized)) return serialized;
283
+ } catch {
284
+ }
285
+ return JSON.stringify(validationSerializationFallback(), null, space);
286
+ }, stringifyCustomValidationError = (value) => {
287
+ if (value !== null && typeof value == "object")
288
+ return stringifyValidationDetails(value);
289
+ try {
290
+ return boundValidationString(String(value));
291
+ } catch {
292
+ return VALIDATION_UNSERIALIZABLE;
293
+ }
294
+ }, tryMapValueError = (error) => {
295
+ try {
296
+ return mapValueError(error);
297
+ } catch {
298
+ return;
299
+ }
300
+ }, readValidationDataProperty = (value, key) => {
301
+ if (!(value === null || typeof value != "object"))
302
+ try {
303
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
304
+ if (descriptor && "value" in descriptor) return descriptor.value;
305
+ } catch {
306
+ }
307
+ }, formatStandardValidationPath = (value) => {
308
+ try {
309
+ if (!Array.isArray(value)) return "root";
310
+ const length = Object.getOwnPropertyDescriptor(value, "length")?.value;
311
+ if (typeof length != "number") return "root";
312
+ const output = [];
313
+ let size = 0;
314
+ const limit = Math.min(length, MAX_VALIDATION_ERROR_CONTAINER_ENTRIES);
315
+ for (let index = 0; index < limit; index++) {
316
+ const descriptor = Object.getOwnPropertyDescriptor(
317
+ value,
318
+ `${index}`
319
+ );
320
+ if (!descriptor || !("value" in descriptor)) continue;
321
+ const part = descriptor.value;
322
+ if (typeof part == "string" || typeof part == "number" || typeof part == "boolean" || typeof part == "bigint") {
323
+ const formatted = boundValidationString(String(part));
324
+ if (size += formatted.length + 1, size > MAX_VALIDATION_ERROR_STRING_LENGTH) {
325
+ output.push(VALIDATION_TRUNCATED);
326
+ break;
327
+ }
328
+ output.push(formatted);
329
+ } else output.push(VALIDATION_UNSERIALIZABLE);
330
+ }
331
+ return limit < length && output.push(VALIDATION_TRUNCATED), boundValidationString(output.join(".")) || "root";
332
+ } catch {
333
+ return "root";
334
+ }
335
+ }, takeErrors = (iterator, limit = MAX_ERRORS) => {
123
336
  const errors = [];
124
337
  for (const error of iterator) {
125
338
  if (!error) continue;
126
- const mapped = mapValueError(error);
339
+ const mapped = tryMapValueError(error);
127
340
  if (mapped && errors.push(mapped), errors.length >= limit) break;
128
341
  }
129
342
  return errors;
@@ -181,27 +394,39 @@ class ValidationError extends Error {
181
394
  const standard = (
182
395
  // @ts-ignore
183
396
  ("~standard" in validator ? validator : validator.schema)["~standard"]
397
+ ), _errors = errors ?? standard.validate(value).issues;
398
+ error = readValidationDataProperty(_errors, 0);
399
+ const standardErrorPath = readValidationDataProperty(error, "path"), standardErrorMessage = readValidationDataProperty(
400
+ error,
401
+ "message"
402
+ ), standardErrorProblem = readValidationDataProperty(
403
+ error,
404
+ "problem"
184
405
  );
185
- error = (errors ?? standard.validate(value).issues)?.[0], isProduction && !allowUnsafeValidationDetails ? message = JSON.stringify({
406
+ isProduction && !allowUnsafeValidationDetails ? message = stringifyValidationDetails({
186
407
  type: "validation",
187
408
  on: type,
188
409
  found: value
189
- }) : message = JSON.stringify(
410
+ }) : message = stringifyValidationDetails(
190
411
  {
191
412
  type: "validation",
192
413
  on: type,
193
- property: error.path?.[0] || "root",
194
- message: error?.message,
195
- summary: error?.problem,
414
+ property: readValidationDataProperty(standardErrorPath, 0) || "root",
415
+ message: standardErrorMessage,
416
+ summary: standardErrorProblem,
196
417
  expected,
197
418
  found: value,
198
419
  errors
199
420
  },
200
- null,
201
421
  2
202
- ), customError = error?.message;
422
+ ), customError = standardErrorMessage;
203
423
  } else {
204
- value && typeof value == "object" && value instanceof ElysiaCustomStatusResponse && (value = value.response), error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : import_value.Value.Errors(validator, value).First());
424
+ if (value && typeof value == "object")
425
+ try {
426
+ value instanceof ElysiaCustomStatusResponse && (value = value.response);
427
+ } catch {
428
+ }
429
+ error = errors?.First() ?? ("Errors" in validator ? validator.Errors(value).First() : import_value.Value.Errors(validator, value).First());
205
430
  const accessor = error?.path || "root", schema = validator?.schema ?? validator;
206
431
  if (!isProduction && !allowUnsafeValidationDetails)
207
432
  try {
@@ -225,7 +450,7 @@ class ValidationError extends Error {
225
450
  value,
226
451
  property: accessor,
227
452
  message: error?.message,
228
- summary: mapValueError(error)?.summary,
453
+ summary: tryMapValueError(error)?.summary,
229
454
  found: value,
230
455
  expected,
231
456
  errors: "Errors" in validator ? takeErrors(
@@ -240,22 +465,21 @@ class ValidationError extends Error {
240
465
  )
241
466
  },
242
467
  validator
243
- ) : error.schema.error : void 0, customError !== void 0 ? message = typeof customError == "object" ? JSON.stringify(customError) : customError + "" : isProduction && !allowUnsafeValidationDetails ? message = JSON.stringify({
468
+ ) : error.schema.error : void 0, customError !== void 0 ? message = stringifyCustomValidationError(customError) : isProduction && !allowUnsafeValidationDetails ? message = stringifyValidationDetails({
244
469
  type: "validation",
245
470
  on: type,
246
471
  found: value
247
- }) : message = JSON.stringify(
472
+ }) : message = stringifyValidationDetails(
248
473
  {
249
474
  type: "validation",
250
475
  on: type,
251
476
  property: accessor,
252
477
  message: error?.message,
253
- summary: mapValueError(error)?.summary,
478
+ summary: tryMapValueError(error)?.summary,
254
479
  expected,
255
480
  found: value,
256
481
  errors: "Errors" in validator ? takeErrors(validator.Errors(value)) : takeErrors(import_value.Value.Errors(validator, value))
257
482
  },
258
- null,
259
483
  2
260
484
  );
261
485
  }
@@ -283,12 +507,17 @@ class ValidationError extends Error {
283
507
  "~standard" in this.validator.schema ? /* @ts-ignore */ ("~standard" in this.validator ? this.validator : (
284
508
  // @ts-ignore
285
509
  this.validator.schema
286
- ))["~standard"].validate(this.value).issues?.slice(0, MAX_ERRORS).map((issue) => ({
287
- summary: issue.message,
288
- path: issue.path?.join(".") || "root",
289
- message: issue.message,
290
- value: this.value
291
- })) || [] : "Errors" in this.validator ? takeErrors(this.validator.Errors(this.value)) : (
510
+ ))["~standard"].validate(this.value).issues?.slice(0, MAX_ERRORS).map((issue) => {
511
+ const message = readValidationDataProperty(issue, "message");
512
+ return {
513
+ summary: message,
514
+ path: formatStandardValidationPath(
515
+ readValidationDataProperty(issue, "path")
516
+ ),
517
+ message,
518
+ value: this.value
519
+ };
520
+ }) || [] : "Errors" in this.validator ? takeErrors(this.validator.Errors(this.value)) : (
292
521
  // @ts-ignore
293
522
  takeErrors(import_value.Value.Errors(this.validator, this.value))
294
523
  )
@@ -336,22 +565,30 @@ class ValidationError extends Error {
336
565
  */
337
566
  detail(message, allowUnsafeValidatorDetails = this.allowUnsafeValidationDetails) {
338
567
  if (!this.customError) return this.message;
339
- const value = this.value, expected = this.expected, errors = this.all;
340
- return isProduction && !allowUnsafeValidatorDetails ? {
341
- type: "validation",
342
- on: this.type,
343
- found: value,
344
- message
345
- } : {
346
- type: "validation",
347
- on: this.type,
348
- property: this.valueError?.path || "root",
349
- message,
350
- summary: mapValueError(this.valueError)?.summary,
351
- found: value,
352
- expected,
353
- errors
354
- };
568
+ const value = this.value, expected = this.expected;
569
+ let errors = [], property = "root", summary;
570
+ try {
571
+ errors = this.all, property = this.valueError?.path || "root", summary = tryMapValueError(this.valueError)?.summary;
572
+ } catch {
573
+ }
574
+ return projectValidationDetails(
575
+ isProduction && !allowUnsafeValidatorDetails ? {
576
+ type: "validation",
577
+ on: this.type,
578
+ found: value,
579
+ message
580
+ } : {
581
+ type: "validation",
582
+ on: this.type,
583
+ property,
584
+ message,
585
+ summary,
586
+ found: value,
587
+ expected,
588
+ errors
589
+ },
590
+ !0
591
+ );
355
592
  }
356
593
  }
357
594
  // Annotate the CommonJS export names for ESM import in node:
@@ -363,6 +600,12 @@ class ValidationError extends Error {
363
600
  InvalidCookieSignature,
364
601
  InvalidFileType,
365
602
  MAX_ERRORS,
603
+ MAX_VALIDATION_ERROR_CONTAINER_ENTRIES,
604
+ MAX_VALIDATION_ERROR_DEPTH,
605
+ MAX_VALIDATION_ERROR_ENTRIES,
606
+ MAX_VALIDATION_ERROR_NODES,
607
+ MAX_VALIDATION_ERROR_OUTPUT_LENGTH,
608
+ MAX_VALIDATION_ERROR_STRING_LENGTH,
366
609
  NotFoundError,
367
610
  ParseError,
368
611
  ValidationError,