@likec4/log 1.26.1 → 1.29.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/dist/index.cjs DELETED
@@ -1,1485 +0,0 @@
1
- 'use strict';
2
-
3
- const logtape = require('@logtape/logtape');
4
-
5
- const normalizeDescriptors=(error)=>{
6
- CORE_ERROR_PROPS.forEach((propName)=>{
7
- normalizeDescriptor$1(error,propName);
8
- });
9
- };
10
-
11
- const CORE_ERROR_PROPS=["name","message","stack","cause","errors"];
12
-
13
- const normalizeDescriptor$1=(error,propName)=>{
14
- const descriptor=getDescriptor(error,propName);
15
-
16
- if(descriptor===undefined){
17
- return
18
- }
19
-
20
- if(isReadonlyGetter(descriptor)){
21
- setErrorProperty$1(error,propName,error[propName]);
22
- return
23
- }
24
-
25
- if(isInvalidDescriptor(descriptor)){
26
- setErrorDescriptor(error,propName,descriptor);
27
- }
28
- };
29
-
30
-
31
- const getDescriptor=(value,propName)=>{
32
- const descriptor=Object.getOwnPropertyDescriptor(value,propName);
33
-
34
- if(descriptor!==undefined){
35
- return descriptor
36
- }
37
-
38
- const prototype=Object.getPrototypeOf(value);
39
- return prototype===null?undefined:getDescriptor(prototype,propName)
40
- };
41
-
42
-
43
- const isReadonlyGetter=({get,set})=>
44
- get!==undefined&&set===undefined;
45
-
46
- const isInvalidDescriptor=({enumerable,writable})=>
47
- enumerable||!writable;
48
-
49
-
50
- const setErrorProperty$1=(error,propName,value)=>{
51
- setErrorDescriptor(error,propName,{value});
52
- };
53
-
54
-
55
- const setErrorDescriptor=(error,propName,descriptor)=>{
56
-
57
- Object.defineProperty(error,propName,{
58
- ...descriptor,
59
- ...("get"in descriptor||"set"in descriptor?{}:{writable:true}),
60
- enumerable:false,
61
- configurable:true
62
- });
63
- };
64
-
65
- const normalizeAggregate=(error,recurse)=>{
66
- if(Array.isArray(error.errors)){
67
- const aggregateErrors=error.errors.
68
- filter(isDefined).
69
- map(recurse).
70
- filter(Boolean);
71
- setErrorProperty$1(error,"errors",aggregateErrors);
72
- }else if(isAggregateError(error)){
73
- setErrorProperty$1(error,"errors",[]);
74
- }else if(error.errors!==undefined){
75
- deleteAggregateErrors(error);
76
- }
77
- };
78
-
79
- const isDefined=(error)=>error!==undefined;
80
-
81
- const isAggregateError=(error)=>
82
- "AggregateError"in globalThis&&(
83
- error.name==="AggregateError"||error instanceof AggregateError);
84
-
85
- const deleteAggregateErrors=(error)=>{
86
-
87
- delete error.errors;
88
-
89
- if(error.errors!==undefined){
90
- setErrorProperty$1(error,"errors",[]);
91
- }
92
- };
93
-
94
- const normalizeCause=(error,recurse)=>{
95
- if(!("cause"in error)){
96
- return
97
- }
98
-
99
- const cause=error.cause===undefined?error.cause:recurse(error.cause);
100
-
101
- if(cause===undefined){
102
-
103
- delete error.cause;
104
- }else {
105
- setErrorProperty$1(error,"cause",cause);
106
- }
107
- };
108
-
109
- const isErrorInstance$1=(value)=>
110
- isInstanceOfError$1(value)||hasErrorTag$1(value);
111
-
112
-
113
-
114
- const isInstanceOfError$1=(value)=>{
115
- try{
116
- return value instanceof Error
117
- }catch{
118
- return false
119
- }
120
- };
121
-
122
- const hasErrorTag$1=(value)=>{
123
- try{
124
- return ERROR_TAGS$1.has(Object.prototype.toString.call(value))
125
- }catch{
126
- return false
127
- }
128
- };
129
-
130
- const ERROR_TAGS$1=new Set([
131
-
132
- "[object Error]",
133
-
134
- "[object DOMException]",
135
-
136
- "[object DOMError]",
137
-
138
- "[object Exception]"]
139
- );
140
-
141
- function isPlainObject(value) {
142
- if (typeof value !== 'object' || value === null) {
143
- return false;
144
- }
145
-
146
- const prototype = Object.getPrototypeOf(value);
147
- return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
148
- }
149
-
150
- const isNonModifiableError=(error)=>
151
- !Object.isExtensible(error)||
152
- CORE_ERROR_PROPS.some(
153
- (propName)=>
154
- isNonConfigurableProp(error,propName)||isThrowingProp(error,propName)
155
- );
156
-
157
-
158
-
159
- const isNonConfigurableProp=(error,propName)=>{
160
- const descriptor=Object.getOwnPropertyDescriptor(error,propName);
161
- return descriptor!==undefined&&!descriptor.configurable
162
- };
163
-
164
-
165
- const isThrowingProp=(error,propName)=>{
166
- try{
167
-
168
- error[propName];
169
- return false
170
- }catch{
171
- return true
172
- }
173
- };
174
-
175
- const setStack=(error)=>{
176
- const stack=getStack$3(error.message,error.name);
177
- setErrorProperty$1(error,"stack",stack);
178
- };
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
- const getStack$3=(message="",name="Error")=>{
187
- const StackError=getErrorClass(name);
188
- const{stack}=new StackError(message);
189
- return typeof stack==="string"&&stack!==""?
190
- stack:
191
- `${name}: ${message}`
192
- };
193
-
194
-
195
-
196
-
197
-
198
- const getErrorClass=(name)=>{
199
- const descriptor={
200
- value:name,
201
- enumerable:false,
202
- writable:true,
203
- configurable:true
204
- };
205
-
206
- const StackError=Object.defineProperty(
207
- class extends Error{},
208
- "name",
209
- descriptor
210
- );
211
-
212
- Object.defineProperty(StackError.prototype,"name",descriptor);
213
- return StackError
214
- };
215
-
216
- const copyObject=(object)=>{
217
- const objectCopy={};
218
-
219
-
220
- for(const propName of getPropsToCopy(object)){
221
-
222
- try{
223
- const value=object[propName];
224
- const{
225
- enumerable,
226
- configurable,
227
- writable=true
228
- }=getDescriptor(object,propName);
229
-
230
- Object.defineProperty(objectCopy,propName,{
231
- value,
232
- enumerable,
233
- configurable,
234
- writable
235
- });
236
- }catch{}
237
- }
238
-
239
- return objectCopy
240
- };
241
-
242
-
243
-
244
-
245
-
246
- const getPropsToCopy=(object)=>{
247
- const propNames=getOwnKeys(object);
248
-
249
-
250
- for(const propName of CORE_ERROR_PROPS){
251
-
252
- if(isInheritedProp(object,propName)){
253
-
254
- propNames.push(propName);
255
- }
256
- }
257
-
258
- return propNames
259
- };
260
-
261
-
262
- const getOwnKeys=(object)=>{
263
- try{
264
- return Reflect.ownKeys(object)
265
- }catch{
266
- return []
267
- }
268
- };
269
-
270
-
271
- const isInheritedProp=(object,propName)=>{
272
- try{
273
- return propName in object&&!Object.hasOwn(object,propName)
274
- }catch{
275
- return false
276
- }
277
- };
278
-
279
- const objectifyError=(object)=>{
280
- const{name,message,stack,cause,errors,...objectA}=copyObject(object);
281
- const messageA=getMessage$1(message,objectA);
282
- const error=newError(name,messageA);
283
-
284
- if(message===messageA){
285
- assignObjectProps(error,objectA);
286
- }
287
-
288
- Object.entries({name,stack,cause,errors}).forEach(
289
- ([propName,propValue])=>{
290
- setNewErrorProperty(error,propName,propValue);
291
- }
292
- );
293
-
294
- if(stack===undefined){
295
- setStack(error);
296
- }
297
-
298
- return error
299
- };
300
-
301
-
302
- const getMessage$1=(message,object)=>
303
- typeof message==="string"&&message!==""?
304
- message:
305
- truncateMessage(safeJsonStringify(object));
306
-
307
- const safeJsonStringify=(object)=>{
308
- try{
309
- return JSON.stringify(object)
310
- }catch{
311
- return safeStringify$1(object)
312
- }
313
- };
314
-
315
- const safeStringify$1=(object)=>{
316
- try{
317
- return String(object)
318
- }catch{
319
- return "Invalid error"
320
- }
321
- };
322
-
323
- const truncateMessage=(message)=>
324
- message.length<MESSAGE_MAX_SIZE?
325
- message:
326
- `${message.slice(0,MESSAGE_MAX_SIZE)}...`;
327
-
328
- const MESSAGE_MAX_SIZE=1e3;
329
-
330
- const newError=(name,message)=>{
331
- if(name==="AggregateError"&&"AggregateError"in globalThis){
332
- return new AggregateError([],message)
333
- }
334
-
335
- if(name in NATIVE_ERRORS){
336
- return new NATIVE_ERRORS[name](message)
337
- }
338
-
339
- return new Error(message)
340
- };
341
-
342
- const NATIVE_ERRORS={
343
- Error,
344
- ReferenceError,
345
- TypeError,
346
- SyntaxError,
347
- RangeError,
348
- URIError,
349
- EvalError
350
- };
351
-
352
-
353
- const assignObjectProps=(error,object)=>{
354
-
355
- for(const propName in object){
356
-
357
- if(!(propName in error)){
358
- error[propName]=object[propName];
359
- }
360
- }
361
- };
362
-
363
- const setNewErrorProperty=(error,propName,propValue)=>{
364
- if(propValue!==undefined){
365
- setErrorProperty$1(error,propName,propValue);
366
- }
367
- };
368
-
369
- const stringifyError=(value)=>{
370
- try{
371
- const error=new Error(String(value));
372
- setStack(error);
373
- return error
374
- }catch(error_){
375
- return error_
376
- }
377
- };
378
-
379
- const{toString:objectToString}=Object.prototype;
380
-
381
-
382
- const createError=(value)=>{
383
- if(isErrorPlainObj(value)){
384
- return objectifyError(value)
385
- }
386
-
387
- if(!isErrorInstance$1(value)){
388
- return stringifyError(value)
389
- }
390
-
391
- if(isInvalidError(value)){
392
- return objectifyError(value)
393
- }
394
-
395
- return value
396
- };
397
-
398
-
399
- const isErrorPlainObj=(value)=>{
400
- try{
401
- return isPlainObject(value)
402
- }catch{
403
- return false
404
- }
405
- };
406
-
407
- const isInvalidError=(value)=>
408
- isProxy(value)||isNonModifiableError(value)||hasInvalidConstructor(value);
409
-
410
-
411
-
412
-
413
- const isProxy=(value)=>{
414
- try{
415
- return objectToString.call(value)==="[object Object]"
416
- }catch{
417
- return true
418
- }
419
- };
420
-
421
-
422
-
423
- const hasInvalidConstructor=(error)=>
424
- typeof error.constructor!=="function"||
425
- typeof error.constructor.name!=="string"||
426
- error.constructor.name===""||
427
- error.constructor.prototype!==Object.getPrototypeOf(error);
428
-
429
- const normalizeException=(error,{shallow=false}={})=>
430
- recurseException(error,[],shallow);
431
-
432
- const recurseException=(error,parents,shallow)=>{
433
- if(parents.includes(error)){
434
- return
435
- }
436
-
437
- const recurse=shallow?
438
- identity:
439
- (innerError)=>recurseException(innerError,[...parents,error],shallow);
440
-
441
- const errorA=createError(error);
442
- normalizeProps(errorA,recurse);
443
- return errorA
444
- };
445
-
446
- const identity=(error)=>error;
447
-
448
- const normalizeProps=(error,recurse)=>{
449
- normalizeName(error);
450
- normalizeMessage(error);
451
- normalizeStack(error);
452
- normalizeCause(error,recurse);
453
- normalizeAggregate(error,recurse);
454
- normalizeDescriptors(error);
455
- };
456
-
457
-
458
-
459
-
460
-
461
-
462
- const normalizeName=(error)=>{
463
- if(isDefinedString$1(error.name)){
464
- return
465
- }
466
-
467
- const prototypeName=Object.getPrototypeOf(error).name;
468
- const name=isDefinedString$1(prototypeName)?
469
- prototypeName:
470
- error.constructor.name;
471
- setErrorProperty$1(error,"name",name);
472
- };
473
-
474
-
475
- const normalizeMessage=(error)=>{
476
- if(!isDefinedString$1(error.message)){
477
- setErrorProperty$1(error,"message","");
478
- }
479
- };
480
-
481
-
482
- const normalizeStack=(error)=>{
483
- if(!isDefinedString$1(error.stack)){
484
- setStack(error);
485
- }
486
- };
487
-
488
- const isDefinedString$1=(value)=>typeof value==="string"&&value!=="";
489
-
490
- const normalizeArgs$1=(error,ErrorClass,currentName=error.name)=>{
491
- validateErrorClass(ErrorClass);
492
-
493
- if(typeof currentName!=="string"){
494
- throw new TypeError(`currentName must be a string: ${currentName}`)
495
- }
496
-
497
- return currentName
498
- };
499
-
500
- const validateErrorClass=(ErrorClass)=>{
501
- if(!isClass(ErrorClass)){
502
- throw new TypeError(`ErrorClass must be a class: ${ErrorClass}`)
503
- }
504
-
505
- if(!isErrorClass(ErrorClass.prototype)){
506
- throw new TypeError(`ErrorClass must inherit from Error: ${ErrorClass}`)
507
- }
508
-
509
- if(!hasConstructor(ErrorClass)){
510
- throw new TypeError(
511
- `ErrorClass must be have a valid constructor: ${ErrorClass}`
512
- )
513
- }
514
- };
515
-
516
- const isClass=(ErrorClass)=>
517
- typeof ErrorClass==="function"&&
518
- typeof ErrorClass.prototype==="object"&&
519
- ErrorClass.prototype!==null;
520
-
521
-
522
- const isErrorClass=(prototype)=>
523
- prototype!==null&&(
524
- prototype.name==="Error"||isErrorClass(Object.getPrototypeOf(prototype)));
525
-
526
- const hasConstructor=(ErrorClass)=>
527
- typeof ErrorClass.prototype.constructor==="function";
528
-
529
- const setNonEnumProp$1=(error,propName,value)=>{
530
-
531
- Object.defineProperty(error,propName,{
532
- value,
533
- enumerable:false,
534
- writable:true,
535
- configurable:true
536
- });
537
- };
538
-
539
- const updatePrototype=(error,ErrorClass)=>{
540
- if(Object.getPrototypeOf(error)===ErrorClass.prototype){
541
- return
542
- }
543
-
544
- setPrototype(error,ErrorClass);
545
- deleteOwnProperty(error,"constructor");
546
- fixName(error,ErrorClass);
547
- };
548
-
549
-
550
-
551
-
552
-
553
- const setPrototype=(error,ErrorClass)=>{
554
-
555
- Object.setPrototypeOf(error,ErrorClass.prototype);
556
- };
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
- const fixName=(error,ErrorClass)=>{
568
- deleteOwnProperty(error,"name");
569
-
570
- const prototypeName=getClassName(ErrorClass.prototype);
571
-
572
- if(error.name!==prototypeName){
573
- setNonEnumProp$1(error,"name",prototypeName);
574
- }
575
- };
576
-
577
- const getClassName=(prototype)=>
578
- getPrototypeName(prototype)??
579
- getConstructorName(prototype)??
580
- getClassName(Object.getPrototypeOf(prototype));
581
-
582
- const getPrototypeName=(prototype)=>
583
- Object.hasOwn(prototype,"name")&&isDefinedString(prototype.name)?
584
- prototype.name:
585
- undefined;
586
-
587
- const getConstructorName=(prototype)=>
588
- typeof prototype.constructor==="function"&&
589
- isDefinedString(prototype.constructor.name)?
590
- prototype.constructor.name:
591
- undefined;
592
-
593
- const isDefinedString=(value)=>typeof value==="string"&&value!=="";
594
-
595
-
596
-
597
- const deleteOwnProperty=(error,propName)=>{
598
- if(Object.hasOwn(error,propName)){
599
-
600
- delete error[propName];
601
- }
602
- };
603
-
604
- const updateStack$1=(error,currentName)=>{
605
- if(!shouldUpdateStack(error,currentName)){
606
- return
607
- }
608
-
609
- const stack=getStack$2(error,currentName);
610
- setNonEnumProp$1(error,"stack",stack);
611
- };
612
-
613
- const shouldUpdateStack=(error,currentName)=>
614
- currentName!==error.name&&
615
- currentName!==""&&
616
- error.stack.includes(currentName)&&
617
- stackIncludesName();
618
-
619
-
620
- const stackIncludesName=()=>{
621
-
622
- class StackError extends Error{}
623
- const descriptor={
624
- value:EXAMPLE_NAME,
625
- enumerable:false,
626
- writable:true,
627
- configurable:true
628
- };
629
-
630
- Object.defineProperty(StackError,"name",descriptor);
631
-
632
- Object.defineProperty(StackError.prototype,"name",descriptor);
633
- const{stack}=new StackError("");
634
- return typeof stack==="string"&&stack.includes(EXAMPLE_NAME)
635
- };
636
-
637
- const EXAMPLE_NAME="SetErrorClassError";
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
- const getStack$2=({name,stack},currentName)=>{
647
- if(stack.startsWith(`${currentName}: `)){
648
- return stack.replace(currentName,name)
649
- }
650
-
651
- const replacers=getReplacers$1(currentName,name);
652
- const[fromA,to]=replacers.find(([from])=>stack.includes(from));
653
- return stack.replace(fromA,to)
654
- };
655
-
656
-
657
-
658
- const getReplacers$1=(currentName,newName)=>[
659
- [`\n${currentName}: `,`\n${newName}: `],
660
- [`${currentName}: `,`${newName}: `],
661
- [`${currentName} `,`${newName} `],
662
- [currentName,newName]];
663
-
664
- const setErrorClass=(error,ErrorClass,currentName)=>{
665
- const errorA=normalizeException(error);
666
- const currentNameA=normalizeArgs$1(errorA,ErrorClass,currentName);
667
- updatePrototype(errorA,ErrorClass);
668
- updateStack$1(errorA,currentNameA);
669
- return errorA
670
- };
671
-
672
- const mergeDescriptors=(newDescriptor,currentDescriptor)=>
673
- currentDescriptor.configurable===false?
674
- mergeNonConfig(newDescriptor,currentDescriptor):
675
- mergeConfig(newDescriptor,currentDescriptor);
676
-
677
-
678
-
679
-
680
- const mergeNonConfig=(newDescriptor,currentDescriptor)=>({
681
- ...currentDescriptor,
682
- ...getNonConfigWritable(newDescriptor,currentDescriptor),
683
- ...getNonConfigValue(newDescriptor,currentDescriptor)
684
- });
685
-
686
- const getNonConfigWritable=(newDescriptor,currentDescriptor)=>
687
- currentDescriptor.writable===true&&newDescriptor.writable===false?
688
- {writable:false}:
689
- {};
690
-
691
- const getNonConfigValue=(newDescriptor,currentDescriptor)=>
692
- newDescriptor.hasValue&&
693
- "value"in currentDescriptor&&
694
- currentDescriptor.writable===true?
695
- {value:newDescriptor.value}:
696
- {};
697
-
698
- const mergeConfig=(newDescriptor,currentDescriptor)=>{
699
- const enumerable=mergeDescriptor(
700
- newDescriptor.enumerable,
701
- currentDescriptor.enumerable,
702
- true
703
- );
704
- const writable=mergeDescriptor(
705
- newDescriptor.writable,
706
- currentDescriptor.writable,
707
- true
708
- );
709
- const configurable=mergeDescriptor(
710
- newDescriptor.configurable,
711
- currentDescriptor.configurable,
712
- true
713
- );
714
- const valueProps=mergeValue(newDescriptor,currentDescriptor,writable);
715
- return {...valueProps,enumerable,configurable}
716
- };
717
-
718
- const mergeValue=(newDescriptor,currentDescriptor,writable)=>{
719
- if(newDescriptor.hasValue){
720
- return {value:newDescriptor.value,writable}
721
- }
722
-
723
- if(!hasGetSet(newDescriptor)&&!hasGetSet(currentDescriptor)){
724
- return {value:currentDescriptor.value,writable}
725
- }
726
-
727
- return {
728
- get:mergeDescriptor(newDescriptor.get,currentDescriptor.get),
729
- set:mergeDescriptor(newDescriptor.set,currentDescriptor.set)
730
- }
731
- };
732
-
733
- const hasGetSet=({get,set})=>get!==undefined||set!==undefined;
734
-
735
- const mergeDescriptor=(newValue,currentValue,defaultValue)=>
736
- newValue??currentValue??defaultValue;
737
-
738
- const normalizeInput=(input,key,newDescriptor)=>{
739
- if(!isAnyObj(input)){
740
- throw new TypeError(`Argument must be an object: ${input}`)
741
- }
742
-
743
- if(!isValidKey(key)){
744
- throw new TypeError(
745
- `Property key must be a string, a symbol or an integer: ${key}`
746
- )
747
- }
748
-
749
- return normalizeDescriptor(newDescriptor)
750
- };
751
-
752
- const isAnyObj=(value)=>typeof value==="object"&&value!==null;
753
-
754
- const isValidKey=(key)=>{
755
- const type=typeof key;
756
- return type==="string"||type==="symbol"||type==="number"
757
- };
758
-
759
- const normalizeDescriptor=(newDescriptor)=>{
760
- if(!isPlainObject(newDescriptor)){
761
- throw new TypeError(`Descriptor must be a plain object: ${newDescriptor}`)
762
- }
763
-
764
- const{
765
- enumerable,
766
- writable,
767
- configurable,
768
- value,
769
- get,
770
- set,
771
- ...unknownProps
772
- }=newDescriptor;
773
- const hasValue=("value"in newDescriptor);
774
- validateDescriptor({
775
- enumerable,
776
- writable,
777
- configurable,
778
- get,
779
- set,
780
- unknownProps,
781
- hasValue
782
- });
783
- return {enumerable,writable,configurable,value,get,set,hasValue}
784
- };
785
-
786
- const validateDescriptor=({
787
- enumerable,
788
- writable,
789
- configurable,
790
- get,
791
- set,
792
- unknownProps,
793
- hasValue
794
- })=>{
795
- validateGetSet(hasValue,get,"get");
796
- validateGetSet(hasValue,set,"set");
797
- validateBoolean(enumerable,"enumerable");
798
- validateBoolean(writable,"writable");
799
- validateBoolean(configurable,"configurable");
800
- validateUnknownProps(unknownProps);
801
- };
802
-
803
- const validateGetSet=(hasValue,getSet,propName)=>{
804
- validateFunction(getSet,propName);
805
-
806
- if(hasValue&&getSet!==undefined){
807
- throw new TypeError(
808
- `Descriptor property "value" and "${propName}" must not both be defined: ${getSet}`
809
- )
810
- }
811
- };
812
-
813
- const validateFunction=(propValue,propName)=>{
814
- if(propValue!==undefined&&typeof propValue!=="function"){
815
- throw new TypeError(
816
- `Descriptor property "${propName}" must be a function: ${propValue}`
817
- )
818
- }
819
- };
820
-
821
- const validateBoolean=(propValue,propName)=>{
822
- if(propValue!==undefined&&typeof propValue!=="boolean"){
823
- throw new TypeError(
824
- `Descriptor property "${propName}" must be a boolean: ${propValue}`
825
- )
826
- }
827
- };
828
-
829
- const validateUnknownProps=(unknownProps)=>{
830
- const[unknownProp]=Object.keys(unknownProps);
831
-
832
- if(unknownProp!==undefined){
833
- throw new TypeError(
834
- `Unknown descriptor property "${unknownProp}": ${unknownProps[unknownProp]}`
835
- )
836
- }
837
- };
838
-
839
- const redefineProperty=(input,key,newDescriptor)=>{
840
- const newDescriptorA=normalizeInput(input,key,newDescriptor);
841
- const currentDescriptor=getCurrentDescriptor(input,key);
842
- const finalDescriptor=mergeDescriptors(newDescriptorA,currentDescriptor);
843
- setProperty(input,key,finalDescriptor);
844
- return input
845
- };
846
-
847
-
848
- const getCurrentDescriptor=(input,key)=>{
849
- const descriptor=Object.getOwnPropertyDescriptor(input,key);
850
-
851
- if(descriptor!==undefined){
852
- return descriptor
853
- }
854
-
855
- const prototype=Object.getPrototypeOf(input);
856
- return prototype===null?{}:getCurrentDescriptor(prototype,key)
857
- };
858
-
859
-
860
- const setProperty=(input,key,finalDescriptor)=>{
861
- try{
862
-
863
- Object.defineProperty(input,key,finalDescriptor);
864
- }catch{}
865
- };
866
-
867
- const assignProp=(error,propName,propValue)=>{
868
- if(propValue!==undefined){
869
- return setProp(error,propName,propValue)
870
- }
871
-
872
- try{
873
-
874
- delete error[propName];
875
- }catch{}
876
-
877
- if(error[propName]!==undefined){
878
- return setProp(error,propName)
879
- }
880
- };
881
-
882
- const setProp=(error,propName,propValue)=>{
883
- const nonEnum=getNonEnum(propName);
884
- redefineProperty(error,propName,{value:propValue,...nonEnum});
885
- };
886
-
887
-
888
- const getNonEnum=(propName)=>
889
- typeof propName==="string"&&propName.startsWith("_")?
890
- {enumerable:false}:
891
- {};
892
-
893
- const isErrorInstance=(value)=>
894
- isInstanceOfError(value)||hasErrorTag(value);
895
-
896
-
897
-
898
- const isInstanceOfError=(value)=>{
899
- try{
900
- return value instanceof Error
901
- }catch{
902
- return false
903
- }
904
- };
905
-
906
- const hasErrorTag=(value)=>{
907
- try{
908
- return ERROR_TAGS.has(Object.prototype.toString.call(value))
909
- }catch{
910
- return false
911
- }
912
- };
913
-
914
- const ERROR_TAGS=new Set([
915
-
916
- "[object Error]",
917
-
918
- "[object DOMException]",
919
-
920
- "[object DOMError]",
921
-
922
- "[object Exception]"]
923
- );
924
-
925
- const normalizeOptions=(error,props,opts={})=>{
926
- validateErrorOrObject(error,"First argument");
927
- validateErrorOrObject(props,"Second argument");
928
-
929
- if(!isPlainObject(opts)){
930
- throw new TypeError(`Options must be a plain object: ${opts}`)
931
- }
932
-
933
- const{soft=false}=opts;
934
-
935
- if(typeof soft!=="boolean"){
936
- throw new TypeError(`Option "soft" must be a boolean: ${soft}`)
937
- }
938
-
939
- return {soft}
940
- };
941
-
942
- const validateErrorOrObject=(value,prefix)=>{
943
- if(value===undefined){
944
- throw new TypeError(`${prefix} is required.`)
945
- }
946
-
947
- if(!isErrorOrObject(value)){
948
- throw new TypeError(
949
- `${prefix} must be a plain object or an error: ${value}`
950
- )
951
- }
952
- };
953
-
954
- const isErrorOrObject=(value)=>isPlainObject(value)||isErrorInstance(value);
955
-
956
- const shouldSkipProp=({error,props,propName,soft})=>
957
- isIgnoredPropName(propName)||
958
- !isEnum.call(props,propName)||
959
- soft&&error[propName]!==undefined;
960
-
961
- const isIgnoredPropName=(propName)=>
962
- propName in CHECK_ERROR||IGNORED_PROPS.has(propName);
963
-
964
-
965
-
966
-
967
- const CHECK_ERROR=new Error("check");
968
-
969
-
970
-
971
- const IGNORED_PROPS=new Set(["prototype","errors","cause"]);
972
-
973
- const{propertyIsEnumerable:isEnum}=Object.prototype;
974
-
975
- const setErrorProps=(error,props,opts)=>{
976
- const{soft}=normalizeOptions(error,props,opts);
977
-
978
-
979
- for(const propName of Reflect.ownKeys(props)){
980
- setErrorProp({error,props,propName,soft});
981
- }
982
-
983
- return error
984
- };
985
-
986
-
987
-
988
-
989
-
990
- const setErrorProp=({error,props,propName,soft})=>{
991
- if(!shouldSkipProp({error,props,propName,soft})){
992
- assignProp(error,propName,props[propName]);
993
- }
994
- };
995
-
996
- const setErrorProperty=(error,propName,value)=>{
997
-
998
- Object.defineProperty(error,propName,{
999
- value,
1000
- writable:true,
1001
- enumerable:false,
1002
- configurable:true
1003
- });
1004
- };
1005
-
1006
- const mergeAggregateCauses=(parent,recurse)=>{
1007
- if(parent.errors===undefined){
1008
- return
1009
- }
1010
-
1011
- const errors=parent.errors.
1012
- map((error)=>recurse(error).error).
1013
- filter(Boolean);
1014
- setErrorProperty(parent,"errors",errors);
1015
- };
1016
-
1017
- const mergeAggregateErrors=({target,source,parent,child})=>{
1018
- if(!hasErrors(target)){
1019
- mergeSourceErrors(target,source);
1020
- return
1021
- }
1022
-
1023
- if(hasErrors(source)){
1024
- setErrorProperty(target,"errors",[...child.errors,...parent.errors]);
1025
- }
1026
- };
1027
-
1028
- const mergeSourceErrors=(target,source)=>{
1029
- if(source.errors!==undefined){
1030
- setErrorProperty(target,"errors",source.errors);
1031
- }
1032
- };
1033
-
1034
- const hasErrors=(targetOrSource)=>
1035
- targetOrSource.errors!==undefined&&targetOrSource.errors.length!==0;
1036
-
1037
- const normalizeArgs=(
1038
- error,
1039
- newMessage,
1040
- currentMessage=error.message)=>
1041
- {
1042
- if(typeof newMessage!=="string"){
1043
- throw new TypeError(`newMessage must be a string: ${newMessage}`)
1044
- }
1045
-
1046
- if(typeof currentMessage!=="string"){
1047
- throw new TypeError(`currentMessage must be a string: ${currentMessage}`)
1048
- }
1049
-
1050
- return currentMessage
1051
- };
1052
-
1053
- const getStack$1=({name,stack},newMessage,currentMessage)=>
1054
- currentMessage!==""&&stack.includes(currentMessage)?
1055
- replaceMessage({name,stack,newMessage,currentMessage}):
1056
- insertMessage(name,stack,newMessage);
1057
-
1058
-
1059
-
1060
- const replaceMessage=({name,stack,newMessage,currentMessage})=>{
1061
- const replacers=getReplacers(name,newMessage,currentMessage);
1062
- const[fromA,to]=replacers.find(([from])=>stack.includes(from));
1063
- return stack.replace(fromA,to)
1064
- };
1065
-
1066
-
1067
-
1068
- const getReplacers=(name,newMessage,currentMessage)=>[
1069
- [`${name}: ${currentMessage}`,`${name}: ${newMessage}`],
1070
- [`: ${currentMessage}`,`: ${newMessage}`],
1071
- [`\n${currentMessage}`,`\n${newMessage}`],
1072
- [` ${currentMessage}`,` ${newMessage}`],
1073
- [currentMessage,newMessage]];
1074
-
1075
-
1076
- const insertMessage=(name,stack,newMessage)=>{
1077
- const nameAndColon=`${name}: `;
1078
- const newMessageA=newMessage.trimEnd();
1079
-
1080
- if(stack===name||stack.startsWith(`${name}\n`)){
1081
- return stack.replace(name,`${nameAndColon}${newMessageA}`)
1082
- }
1083
-
1084
- return stack.startsWith(nameAndColon)?
1085
- stack.replace(nameAndColon,`${nameAndColon}${newMessageA}\n`):
1086
- `${nameAndColon}${newMessageA}\n${stack}`
1087
- };
1088
-
1089
- const setErrorMessage=(error,newMessage,currentMessage)=>{
1090
- const errorA=normalizeException(error);
1091
- const currentMessageA=normalizeArgs(errorA,newMessage,currentMessage);
1092
- setNonEnumProp(errorA,"message",newMessage);
1093
- updateStack(errorA,newMessage,currentMessageA);
1094
- return errorA
1095
- };
1096
-
1097
-
1098
-
1099
- const updateStack=(error,newMessage,currentMessage)=>{
1100
- if(newMessage===currentMessage||!stackIncludesMessage()){
1101
- return
1102
- }
1103
-
1104
- const stack=getStack$1(error,newMessage,currentMessage);
1105
- setNonEnumProp(error,"stack",stack);
1106
- };
1107
-
1108
-
1109
- const stackIncludesMessage=()=>{
1110
- const{stack}=new Error(EXAMPLE_MESSAGE);
1111
- return typeof stack==="string"&&stack.includes(EXAMPLE_MESSAGE)
1112
- };
1113
-
1114
- const EXAMPLE_MESSAGE="set-error-message test message";
1115
-
1116
- const setNonEnumProp=(error,propName,value)=>{
1117
-
1118
- Object.defineProperty(error,propName,{
1119
- value,
1120
- enumerable:false,
1121
- writable:true,
1122
- configurable:true
1123
- });
1124
- };
1125
-
1126
- const wrapErrorMessage=(error,newMessage,oldMessage)=>{
1127
- if(typeof newMessage!=="string"){
1128
- throw new TypeError(
1129
- `Second argument must be a message string: ${newMessage}`
1130
- )
1131
- }
1132
-
1133
- const errorA=normalizeException(error);
1134
- const message=getMessage(newMessage,errorA.message);
1135
- return setErrorMessage(errorA,message,oldMessage)
1136
- };
1137
-
1138
-
1139
-
1140
-
1141
-
1142
-
1143
-
1144
-
1145
-
1146
- const getMessage=(rawNewMessage,rawCurrentMessage)=>{
1147
- const newMessage=rawNewMessage.trim();
1148
- const currentMessage=rawCurrentMessage.trim();
1149
-
1150
- if(newMessage===""){
1151
- return currentMessage
1152
- }
1153
-
1154
- if(currentMessage===""){
1155
- return newMessage
1156
- }
1157
-
1158
- return concatMessages(newMessage,currentMessage,rawNewMessage)
1159
- };
1160
-
1161
- const concatMessages=(newMessage,currentMessage,rawNewMessage)=>{
1162
- if(!newMessage.endsWith(PREPEND_CHAR)){
1163
- return `${currentMessage}\n${newMessage}`
1164
- }
1165
-
1166
- return rawNewMessage.endsWith(PREPEND_NEWLINE_CHAR)?
1167
- `${newMessage}\n${currentMessage}`:
1168
- `${newMessage} ${currentMessage}`
1169
- };
1170
-
1171
- const PREPEND_CHAR=":";
1172
- const PREPEND_NEWLINE_CHAR="\n";
1173
-
1174
- const mergeMessage=({parent,child,target,stackError})=>{
1175
- const parentMessage=parent.message;
1176
-
1177
- target.message=child.message;
1178
- return wrapErrorMessage(target,parentMessage,stackError.message)
1179
- };
1180
-
1181
- const hasStack=(error,stack)=>getStack(error)===stack;
1182
-
1183
-
1184
-
1185
-
1186
- const getStack=(error)=>
1187
- typeof error==="object"&&error!==null?error.stack:undefined;
1188
-
1189
-
1190
-
1191
-
1192
-
1193
-
1194
-
1195
-
1196
-
1197
-
1198
- const mergeStack=({wrap,target,source,childHasStack})=>{
1199
- if(wrap===childHasStack){
1200
- return target
1201
- }
1202
-
1203
- setErrorProperty(target,"stack",source.stack);
1204
- return source
1205
- };
1206
-
1207
- const getWrap=(parent)=>{
1208
- const{wrap,name}=parent;
1209
-
1210
- if(typeof wrap!=="boolean"){
1211
- return name==="Error"
1212
- }
1213
-
1214
- if(Object.hasOwn(parent,"wrap")){
1215
-
1216
- delete parent.wrap;
1217
- }
1218
-
1219
- return wrap
1220
- };
1221
-
1222
- const mergeErrorCause=(error)=>mergeError(error,[]).error;
1223
-
1224
-
1225
-
1226
-
1227
-
1228
-
1229
- const mergeError=(error,parents)=>{
1230
- if(parents.includes(error)){
1231
- return {}
1232
- }
1233
-
1234
- const recurse=(innerError)=>mergeError(innerError,[...parents,error]);
1235
- const stack=getStack(error);
1236
- const errorA=normalizeException(error,{shallow:true});
1237
- const parentHasStack=hasStack(errorA,stack);
1238
-
1239
- mergeAggregateCauses(errorA,recurse);
1240
- const{parent:errorB,childHasStack}=mergeCause(errorA,recurse);
1241
- const errorHasStack=parentHasStack||childHasStack;
1242
- return {error:errorB,errorHasStack}
1243
- };
1244
-
1245
-
1246
-
1247
- const mergeCause=(parent,recurse)=>{
1248
- const wrap=getWrap(parent);
1249
-
1250
- if(parent.cause===undefined){
1251
- return {parent,childHasStack:false}
1252
- }
1253
-
1254
- const{error:child,errorHasStack:childHasStack}=recurse(parent.cause);
1255
-
1256
- delete parent.cause;
1257
- const parentA=mergeChild({parent,child,childHasStack,wrap});
1258
- return {parent:parentA,childHasStack}
1259
- };
1260
-
1261
- const mergeChild=({parent,child,childHasStack,wrap})=>{
1262
- if(child===undefined){
1263
- return parent
1264
- }
1265
-
1266
- const[target,source]=wrap?[child,parent]:[parent,child];
1267
- const stackError=mergeStack({wrap,target,source,childHasStack});
1268
- const targetA=setErrorClass(target,target.constructor,stackError.name);
1269
- const targetB=mergeMessage({parent,child,target:targetA,stackError});
1270
- mergeAggregateErrors({target:targetB,source,parent,child});
1271
- const targetC=setErrorProps(targetB,source,{soft:!wrap});
1272
- return targetC
1273
- };
1274
-
1275
- function safeStringifyReplacer(seen) {
1276
- return function (key, value) {
1277
- // Handle objects with a custom `.toJSON()` method.
1278
- if (typeof value?.toJSON === 'function') {
1279
- value = value.toJSON();
1280
- }
1281
-
1282
- if (!(value !== null && typeof value === 'object')) {
1283
- return value;
1284
- }
1285
-
1286
- if (seen.has(value)) {
1287
- return '[Circular]';
1288
- }
1289
-
1290
- seen.add(value);
1291
-
1292
- const newValue = Array.isArray(value) ? [] : {};
1293
-
1294
- for (const [key2, value2] of Object.entries(value)) {
1295
- newValue[key2] = safeStringifyReplacer(seen)(key2, value2);
1296
- }
1297
-
1298
- seen.delete(value);
1299
-
1300
- return newValue;
1301
- };
1302
- }
1303
-
1304
- function safeStringify(object, {indentation} = {}) {
1305
- const seen = new WeakSet();
1306
- return JSON.stringify(object, safeStringifyReplacer(seen), indentation);
1307
- }
1308
-
1309
- const parseStack = (stack) => {
1310
- const lines = stack.split("\n").map((l) => {
1311
- let replaced = l.trim().replace("file://", "");
1312
- return replaced;
1313
- });
1314
- return lines;
1315
- };
1316
- function ident(value, identation = 2) {
1317
- value = Array.isArray(value) ? value : value.split("\n");
1318
- return value.map((l) => `${" ".repeat(identation)}${l}`).join("\n");
1319
- }
1320
- function loggable(error) {
1321
- if (typeof error === "string") {
1322
- return error;
1323
- }
1324
- if (error instanceof Error) {
1325
- const mergedErr = mergeErrorCause(error);
1326
- if (mergedErr.stack) {
1327
- const stack = parseStack(mergedErr.stack);
1328
- return mergedErr.message + "\n" + ident(stack.slice(1));
1329
- }
1330
- return mergedErr.message;
1331
- }
1332
- return safeStringify(error);
1333
- }
1334
-
1335
- function gerErrorFromLogRecord(record) {
1336
- const errors = Object.values(record.properties).filter((v) => v instanceof Error).map((err) => {
1337
- const mergedErr = mergeErrorCause(err);
1338
- if (mergedErr.stack) {
1339
- mergedErr.stack = parseStack(mergedErr.stack).join("\n");
1340
- }
1341
- return mergedErr;
1342
- });
1343
- if (errors.length === 0) {
1344
- return null;
1345
- }
1346
- return errors.length === 1 ? errors[0] : new AggregateError(errors);
1347
- }
1348
- function errorFromLogRecord(record) {
1349
- const error = gerErrorFromLogRecord(record);
1350
- if (error && typeof record.rawMessage === "string") {
1351
- return wrapErrorMessage(error, record.rawMessage + "\n");
1352
- }
1353
- return error;
1354
- }
1355
- function appendErrorToMessage(values, color) {
1356
- const error = gerErrorFromLogRecord(values.record);
1357
- if (error) {
1358
- let errorMessge = error.message;
1359
- if (error.stack) {
1360
- errorMessge = errorMessge + "\n" + ident(error.stack.split("\n").slice(1));
1361
- }
1362
- if (color) {
1363
- errorMessge = `${ansiColors.red}${errorMessge}${RESET}`;
1364
- }
1365
- return {
1366
- ...values,
1367
- message: values.message + "\n" + ident(errorMessge)
1368
- };
1369
- }
1370
- return values;
1371
- }
1372
- const levelAbbreviations = {
1373
- "debug": "DEBUG",
1374
- "info": "INFO ",
1375
- "warning": "WARN ",
1376
- "error": "ERROR",
1377
- "fatal": "FATAL"
1378
- };
1379
- function getMessageOnlyFormatter() {
1380
- return getTextFormatter({
1381
- format: ({ message }) => {
1382
- return message;
1383
- }
1384
- });
1385
- }
1386
- const level = (l) => levelAbbreviations[l];
1387
- function getTextFormatter(options) {
1388
- const _format = options?.format ?? (({ timestamp, level: level2, category, message }) => {
1389
- return `${timestamp} ${level2} ${category} ${message}`;
1390
- });
1391
- return logtape.getTextFormatter({
1392
- timestamp: "time",
1393
- level,
1394
- category: ".",
1395
- ...options,
1396
- format: (values) => {
1397
- return _format(appendErrorToMessage(values));
1398
- }
1399
- });
1400
- }
1401
- const RESET = "\x1B[0m";
1402
- const ansiColors = {
1403
- // black: "\x1b[30m",
1404
- red: "\x1B[31m"
1405
- // green: "\x1b[32m",
1406
- // yellow: "\x1b[33m",
1407
- // blue: "\x1b[34m",
1408
- // magenta: "\x1b[35m",
1409
- // cyan: "\x1b[36m",
1410
- // white: "\x1b[37m",
1411
- };
1412
- function getAnsiColorFormatter(options) {
1413
- const _format = options?.format ?? (({ timestamp, level: level2, category, message }) => {
1414
- return `${timestamp} ${level2} ${category} ${message}`;
1415
- });
1416
- return logtape.getAnsiColorFormatter({
1417
- timestamp: "time",
1418
- level,
1419
- categoryStyle: "bold",
1420
- categoryColor: "cyan",
1421
- category: ".",
1422
- ...options,
1423
- format: (values) => {
1424
- return _format(appendErrorToMessage(values, true));
1425
- }
1426
- });
1427
- }
1428
- function getConsoleSink(options) {
1429
- return logtape.getConsoleSink({
1430
- formatter: getAnsiColorFormatter(),
1431
- ...options
1432
- });
1433
- }
1434
-
1435
- const logger = logtape.getLogger("likec4");
1436
- function createLogger(subcategory) {
1437
- return logger.getChild(subcategory);
1438
- }
1439
- let configureWasCalled = false;
1440
- async function configureLogger(config) {
1441
- try {
1442
- configureWasCalled = true;
1443
- const sinks = config?.sinks ?? {};
1444
- await logtape.configure({
1445
- ...config,
1446
- sinks: {
1447
- ...sinks,
1448
- // @ts-expect-error console is not a valid sink id
1449
- console: sinks["console"] ?? getConsoleSink()
1450
- },
1451
- loggers: [
1452
- { category: ["logtape", "meta"], sinks: ["console"], lowestLevel: "warning" },
1453
- ...config?.loggers ?? [
1454
- {
1455
- category: "likec4",
1456
- sinks: ["console"],
1457
- lowestLevel: "debug"
1458
- }
1459
- ]
1460
- ]
1461
- });
1462
- } catch (e) {
1463
- console.error(e);
1464
- }
1465
- }
1466
- function ensureLoggerIsConfigured() {
1467
- if (!configureWasCalled) {
1468
- configureLogger();
1469
- console.warn("logger automatically configured with default settings");
1470
- }
1471
- }
1472
-
1473
- exports.withFilter = logtape.withFilter;
1474
- exports.configureLogger = configureLogger;
1475
- exports.consola = logger;
1476
- exports.createLogger = createLogger;
1477
- exports.ensureLoggerIsConfigured = ensureLoggerIsConfigured;
1478
- exports.errorFromLogRecord = errorFromLogRecord;
1479
- exports.getAnsiColorFormatter = getAnsiColorFormatter;
1480
- exports.getConsoleSink = getConsoleSink;
1481
- exports.getMessageOnlyFormatter = getMessageOnlyFormatter;
1482
- exports.getTextFormatter = getTextFormatter;
1483
- exports.loggable = loggable;
1484
- exports.logger = logger;
1485
- exports.rootLogger = logger;