forked from shaka-project/trace-anything
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace-anything.js
1147 lines (1037 loc) · 39.6 KB
/
trace-anything.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileoverview Shim and trace calls to absolutely anything.
*
* Requires support for ES classes, ES Map, rest parameters, the spread
* operator, and template strings. This should work in all modern browsers
* (all browsers whose names do not rhyme with Splinter-Wet Deplorer).
*/
/**
* Shim and trace calls to absolutely anything.
*/
class TraceAnything {
/**
* Trace all instances of a certain class. If the constructor is usable
* directly, you must overwrite the original constructor with the return
* value. Otherwise, you can ignore the return value and TraceAnything will
* recognize and trace instances of this class that are returned from other
* traced methods.
*
* @param {function} ctor A constructor whose instances you want to trace.
* @param {TraceAnything.Options} options
* @return {function} A replacement constructor whose instances will have
* their properties, methods, and/or events traced. options.inPlace has no
* effect here.
*/
static traceClass(ctor, options) {
options = Object.assign({}, TraceAnything.defaultOptions, options);
TraceAnything._shimmedClasses.set(ctor, options);
return function(...args) {
const className = ctor.name;
const log = {
timestamp: Date.now(),
type: TraceAnything.LogTypes.Constructor,
className,
args,
};
try {
const original = new ctor(...args);
const traced = TraceAnything.traceObject(original, options);
log.instance = traced;
log.instanceId = TraceAnything._getId(traced, className, options);
log.result = original;
log.duration = Date.now() - log.timestamp;
options.logger(log);
return traced;
} catch (error) {
log.threw = error;
log.duration = Date.now() - log.timestamp;
options.logger(log);
throw error;
}
};
}
/**
* Trace all properties, methods, and/or events (depending on options) of a
* single object.
*
* @param {!Object} object The object you would like to trace.
* @param {TraceAnything.Options} options
* @return {!Object} A replacement object whose properties, methods, and/or
* events will be traced. If options.inPlace is true, "object" will be
* modified in-place and returned.
*/
static traceObject(object, options) {
options = Object.assign({}, TraceAnything.defaultOptions, options);
if (object.__TraceAnything__) {
// We're already tracing this!
return object;
}
const ctor = Object.getPrototypeOf(object).constructor;
const className = ctor.name;
const traced = options.inPlace ? object : {};
// A list of all property names.
const allProperties = [];
// Shim all enumerable members.
for (const k in object) {
if (!options.skipProperties.includes(k)) {
allProperties.push(k);
}
}
// Shim any "extra" properties, such as non-enumerable ones we wouldn't
// find in the loop above, or non-standard properties which the caller
// expects to be tacked on later.
for (const k of options.extraProperties) {
allProperties.push(k);
}
for (const k of allProperties) {
// TODO: This "on" discovery mechanism can cover up non-event properties
// that start with "on", such as Navigator.onLine.
if (k.startsWith('on') && options.events) {
// If we shim event listeners separately, ignore this event listener
// property at this stage.
continue;
}
TraceAnything._shimMember(traced, object, k, className, options);
}
traced.__TraceAnythingEvents__ = new Set();
if (options.events) {
// Shim any "on" event listener properties.
for (const k of allProperties.filter((k) => k.startsWith('on'))) {
TraceAnything._shimEventListenerProperty(
traced, object, k, className, options);
}
// If there's an addEventListener method, we will use that to discover
// events the app is listening for that may not have a corresponding "on"
// property.
if (object.addEventListener) {
TraceAnything._shimEventListenersDynamically(
traced, object, className, options);
}
}
// Add explicit event listeners for these events. This allows tracing of
// non-discoverable events which have no equivalent "on" property and may
// not be used by the application. This also allows the user to request
// certain explicit events without tracing all events.
for (const eventName in options.extraEvents) {
// Since we may be shimming addEventListener, add this event to the set
// before setting the listener.
traced.__TraceAnythingEvents__.add(eventName);
const listener = TraceAnything._shimEventListener(
object, () => {}, className, eventName, options);
traced.addEventListener(eventName, listener);
}
// Make the traced type an instance of the original type, so instanceof
// checks will still pass in the application.
Object.setPrototypeOf(traced, ctor.prototype);
console.assert(traced instanceof ctor);
// Make sure we can tell later what is shimmed already.
traced.__TraceAnything__ = true;
// We ignore the return value, but this has the side-effect of setting the
// generated ID on "traced". This call ensures that when we log this
// object as a return value, its ID will show up in the logs and can be
// correlated to later calls where it is the instance on which methods or
// events are logged. Also, this is done after all the shims are
// installed, so that we don't shim the auto-generated ID property.
TraceAnything._getId(traced, className, options);
return traced;
}
/**
* Trace a single member (method or property) of a single object.
*
* @param {!Object} object The object you would like to trace.
* @param {string} name The name of the member you would like to trace.
* @param {TraceAnything.Options} options
* @return {?} A replacement member which will be traced. If options.inPlace
* is true, "object" will be modified in-place to replace the original
* member.
*/
static traceMember(object, name, options) {
options = Object.assign({}, TraceAnything.defaultOptions, options);
const ctor = Object.getPrototypeOf(object).constructor;
const traced = options.inPlace ? object : {};
TraceAnything._shimMember(traced, object, name, ctor.name, options);
return traced[name];
}
/**
* Trace a single member (method or property) of a class's prototype.
*
* @param {function} ctor A constructor whose instances you want to trace.
* @param {string} name The name of the member you would like to trace.
* @param {TraceAnything.Options} options
* @return {?} A replacement member which will be traced. If options.inPlace
* is true, "ctor.prototype" will be modified in-place to replace the
* original member.
*/
static tracePrototype(ctor, name, options) {
options = Object.assign({}, TraceAnything.defaultOptions, options);
const traced = options.inPlace ? ctor.prototype : {};
TraceAnything._shimMember(traced, ctor.prototype, name, ctor.name, options);
return traced[name];
}
/**
* Trace all instances of a certain element name in the document. Existing
* elements will be traced immediately, and the document will be monitored for
* new elements at runtime.
*
* @param {string} name The name of the tag of the elements you want to trace.
* @param {TraceAnything.Options} options
*/
static traceElement(name, options) {
TraceAnything._traceExistingElements(name, options);
TraceAnything._tracedElementNames.set(name.toLowerCase(), options);
TraceAnything._setupNewElementObserver();
}
/**
* Scan the document for elements we should be tracing, explicitly, right now.
* Useful in testing if you don't want to wait for the mutation observer to
* fire.
*/
static scanDocumentForNewElements() {
for (const [name, options] of TraceAnything._tracedElementNames) {
TraceAnything._traceExistingElements(name, options);
}
}
/**
* Trace existing elements in the document.
*
* @param {string} name The name of the tag of the elements to trace.
* @param {TraceAnything.Options} options
* @private
*/
static _traceExistingElements(name, options) {
for (const element of document.querySelectorAll(name)) {
TraceAnything.traceObject(element, options);
}
}
/**
* Set up an observer to monitor the document for new elements. If this is
* run too early in the lifecycle of the page, the effect will be delayed
* until the page's content is fully loaded.
*/
static _setupNewElementObserver() {
if (!document.body) {
// The document isn't ready yet. Try again when it is.
document.addEventListener('DOMContentLoaded', () => {
TraceAnything.scanDocumentForNewElements();
TraceAnything._setupNewElementObserver();
});
return;
}
// If this is called multiple times before DOMContentLoaded, we could end up
// with multiple deferred calls occurring later. Check if we already have
// an observer, and do nothing if we have one.
if (TraceAnything._newElementObserver) {
return;
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
if (!node.tagName) {
continue;
}
const name = node.tagName.toLowerCase();
const options = TraceAnything._tracedElementNames.get(name);
if (options) {
TraceAnything.traceObject(node, options);
}
}
}
});
observer.observe(document.documentElement, { childList: true });
TraceAnything._newElementObserver = observer;
}
/**
* Shim one member for tracing.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimMember(traced, object, k, className, options) {
// If we are not supposed to trace a member, we still shim it, or else
// reading/writing it won't trigger any native getters/setters on the
// underlying object. This "silent" shim won't trace the values, but will
// just delegate to the underlying object.
if (typeof object[k] == 'function') {
if (options.methods) {
TraceAnything._shimMethod(traced, object, k, className, options);
} else {
TraceAnything._shimPropertySilent(traced, object, k);
}
} else if (options.properties && options.treatPromisePropertiesAsEvents &&
object[k] && object[k].then) {
TraceAnything._shimPromiseProperty(traced, object, k, className, options);
} else {
if (options.properties) {
TraceAnything._shimProperty(traced, object, k, className, options);
} else {
TraceAnything._shimPropertySilent(traced, object, k);
}
}
}
/**
* Shim one method for tracing.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimMethod(traced, object, k, className, options) {
const originalMethod = object[k];
// Set a shim method that logs the arguments and any return values or
// thrown errors. If the return value is of a type that we are tracing,
// we also shim the return value.
traced[k] = function(...args) {
const log = {
timestamp: Date.now(),
type: TraceAnything.LogTypes.Method,
instance: this,
instanceId: TraceAnything._getId(this, className, options),
className,
methodName: k,
args,
};
try {
const returnValue = originalMethod.apply(this, args);
log.result = returnValue;
log.duration = Date.now() - log.timestamp;
if (returnValue == null) {
// If this is null, it's not a Promise. Return the value right away.
options.logger(log);
return returnValue;
}
// We can't shim the types coming out of async methods without waiting
// on the results. This check covers both Promises and more general
// "thenables", of which Promises are one implementation.
if (returnValue.then) {
if (options.logAsyncResultsImmediately) {
options.logger(log);
}
const promiseShim = new Promise((resolve, reject) => {
returnValue.then((asyncValue) => {
asyncValue = TraceAnything._shimReturnValue(asyncValue, options);
if (!options.logAsyncResultsImmediately) {
log.result = asyncValue;
log.duration = Date.now() - log.timestamp;
options.logger(log);
}
resolve(asyncValue);
}, (error) => {
if (!options.logAsyncResultsImmediately) {
delete log.result;
log.threw = error;
log.duration = Date.now() - log.timestamp;
options.logger(log);
}
reject(error);
});
});
return promiseShim;
} else {
options.logger(log);
return TraceAnything._shimReturnValue(returnValue, options);
}
} catch (error) {
delete log.result;
log.threw = error;
log.duration = Date.now() - log.timestamp;
options.logger(log);
throw error;
}
};
// Make sure we can tell later what is shimmed already.
traced[k].__TraceAnything__ = true;
}
/**
* Shim a return value.
*
* @param {?} returnValue
* @param {TraceAnything.Options} options
* @return {?} A traced version of the return value if it is of a type that we
* are configured to trace.
* @private
*/
static _shimReturnValue(returnValue, options) {
if (returnValue == null) {
// This is null or undefined.
return returnValue;
}
if (returnValue.__TraceAnything__) {
// This is a value we're already tracing. So return it now.
return returnValue;
}
const returnType = Object.getPrototypeOf(returnValue).constructor;
const returnTypeOptions = TraceAnything._shimmedClasses.get(returnType);
if (returnTypeOptions) {
// The returned value is of a type we are tracing, but we aren't
// tracing this value yet. This could happen if the value were
// constructed natively inside the browser. To trace it, we return a
// wrapper to trace the return value.
return TraceAnything.traceObject(returnValue, returnTypeOptions);
}
for (const k of options.exploreResultFields) {
if (k in returnValue) {
returnValue[k] = TraceAnything._shimReturnValue(returnValue[k], options);
}
}
// This is a value we aren't tracing and shouldn't be tracing.
return returnValue;
}
/**
* Shim one property (not a method) for tracing.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimProperty(traced, object, k, className, options) {
const originalDescriptor = TraceAnything._getDescriptor(object, k);
console.assert(originalDescriptor != null);
if (options.inPlace && !originalDescriptor.configurable) {
options.logger({
timestamp: Date.now(),
duration: 0,
type: TraceAnything.LogTypes.Warning,
message: `Unable to trace ${k} on ${className} in-place!`,
});
return;
}
const newDescriptor = {
configurable: true,
enumerable: originalDescriptor.enumerable,
};
if ('value' in originalDescriptor) {
// The original had no getter/setter for us to delegate to. So our
// getter and setter will store the value in this local variable. Our
// setter will log the new value.
let propertyValue = originalDescriptor.value;
// Since there was no original getter, the value will only change through
// the setter. So we don't log anything from this getter.
newDescriptor.get = () => propertyValue;
if (originalDescriptor.writable) {
newDescriptor.set = (value) => {
options.logger({
timestamp: Date.now(),
duration: 0,
type: TraceAnything.LogTypes.Setter,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
memberName: k,
value,
});
propertyValue = value;
};
}
} else {
// Define a property whose getter and setter delegate to the object's
// original getter and setter. Our setter will log the new value.
if (originalDescriptor.get) {
newDescriptor.get = function() {
const log = {
timestamp: Date.now(),
type: TraceAnything.LogTypes.Getter,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
memberName: k,
};
try {
const value = originalDescriptor.get.call(this);
log.result = value;
log.duration = Date.now() - log.timestamp;
options.logger(log);
return value;
} catch (error) {
log.threw = error;
log.duration = Date.now() - log.timestamp;
options.logger(log);
throw error;
}
};
}
if (originalDescriptor.set) {
newDescriptor.set = function(value) {
const log = {
timestamp: Date.now(),
type: TraceAnything.LogTypes.Setter,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
memberName: k,
};
try {
originalDescriptor.set.call(this, value);
log.value = value;
log.duration = Date.now() - log.timestamp;
options.logger(log);
} catch (error) {
log.threw = error;
log.duration = Date.now() - log.timestamp;
options.logger(log);
throw error;
}
};
}
}
Object.defineProperty(traced, k, newDescriptor);
}
/**
* Shim one property that contains a Promise or Promise getter. When the
* Promise is resolved, a pseudo-event will be logged.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimPromiseProperty(traced, object, k, className, options) {
const promise = object[k];
promise.then((result) => {
const log = {
timestamp: Date.now(),
duration: 0,
type: TraceAnything.LogTypes.Event,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
eventName: `${k} Promise resolved`,
event: {
result,
},
};
options.logger(log);
}, (error) => {
const log = {
timestamp: Date.now(),
duration: 0,
type: TraceAnything.LogTypes.Event,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
eventName: `${k} Promise rejected`,
event: {
threw: error,
},
};
options.logger(log);
});
}
/**
* If we are not supposed to trace a member, we still shim it, or else
* reading/writing it won't trigger any native getters/setters on the
* underlying object. This "silent" shim won't trace the values, but will
* just delegate to the underlying object.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @private
*/
static _shimPropertySilent(traced, object, k) {
if (options.inPlace) {
// If we're shimming the object in-place, we don't need a "silent" shim.
// That is only needed for constructing a new traced object to take the
// place of the original.
return;
}
const originalDescriptor = TraceAnything._getDescriptor(object, k);
console.assert(originalDescriptor != null);
// Copy the original descriptor, but make it configurable in case another
// part of the code wants to reconfigure the property.
const newDescriptor = Object.assign({}, originalDescriptor, {
configurable: true,
});
Object.defineProperty(traced, k, newDescriptor);
}
/**
* Shim an event listener property for tracing.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimEventListenerProperty(traced, object, k, className, options) {
console.assert(k.startsWith('on'));
const eventName = k.replace(/^on/, '');
if (options.skipEvents.includes(eventName)) {
return;
}
traced.__TraceAnythingEvents__.add(eventName);
const originalDescriptor = TraceAnything._getDescriptor(object, k);
console.assert(originalDescriptor != null);
// An event listener property like "onfoo" should almost certainly have a
// getter and setter for the native code to put the new values into action.
console.assert(originalDescriptor.get && originalDescriptor.set);
// Save the old value to be shimmed later. Note that for an in-place shim,
// this will be overwritten by our getter/setter later, so we must save it
// now.
const oldListener = object[k];
// Shim any future listeners set through the traced object.
Object.defineProperty(traced, k, {
configurable: true,
enumerable: true,
get: function() {
return originalDescriptor.get.call(this);
},
set: function(listener) {
// Because we listen to all events, we don't want to remove the
// listener completely. Instead, turn it into a no-op so the shim can
// still trace the event.
if (!listener) {
listener = () => {};
}
const shim = TraceAnything._shimEventListener(
object, listener, className, eventName, options);
originalDescriptor.set.call(this, shim);
},
});
// Set the old listener again (which may be null or undefined) to shim it
// right away.
traced[k] = oldListener;
}
/**
* Add our own event listeners dynamically when the app adds listeners for
* events we don't know about yet.
*
* @param {!Object} traced The traced object.
* @param {!Object} object The original object.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @private
*/
static _shimEventListenersDynamically(traced, object, className, options) {
const originalMethod = object.addEventListener;
// Set a shim method that tracks any newly discovered events and adds
// listeners for them.
traced.addEventListener = function(eventName, ...args) {
if (!traced.__TraceAnythingEvents__.has(eventName) &&
!options.skipEvents.includes(eventName)) {
traced.__TraceAnythingEvents__.add(eventName);
const listener = TraceAnything._shimEventListener(
traced, () => {}, className, eventName, options);
originalMethod.call(this, eventName, listener);
}
return originalMethod.call(this, eventName, ...args);
}
}
/**
* Shim an event listener for tracing.
*
* @param {!Object} traced The traced object.
* @param {function} listener The event listener.
* @param {string} k The member name.
* @param {string} className The class name.
* @param {string} eventName The event name.
* @param {TraceAnything.Options} options
* @return {function} A shim for the event listener which logs events.
* @private
*/
static _shimEventListener(traced, listener, className, eventName, options) {
// If this event corresponds to a change in a specific property, try to
// find it now. Then we can log the specific value it has in the listener.
let correspondingPropertyName = null;
if (eventName in options.eventProperties) {
correspondingPropertyName = options.eventProperties[eventName];
} else {
const canonicalEventName = eventName.toLowerCase();
const lowerCasePropertyName = canonicalEventName.replace(/change$/, '');
for (const k in traced) {
if (k.toLowerCase() == lowerCasePropertyName) {
correspondingPropertyName = k;
break;
}
}
}
// Return a shim listener which logs the event.
return function(event) {
const log = {
timestamp: Date.now(),
duration: 0,
type: TraceAnything.LogTypes.Event,
instance: traced,
instanceId: TraceAnything._getId(traced, className, options),
className,
eventName,
event,
};
// The corresponding property may be an array of multiple properties
// which should be logged with this event. If so, create an Object
// mapping names to values.
if (Array.isArray(correspondingPropertyName)) {
log.value = {};
for (const name of correspondingPropertyName) {
log.value[name] = TraceAnything._extractProperty(traced, name);
}
} else if (correspondingPropertyName) {
log.value = TraceAnything._extractProperty(
traced, correspondingPropertyName);
}
options.logger(log);
// This supports the EventListener interface, in which "listener" could be
// an object with a "handleEvent" field.
if (listener.handleEvent) {
return listener.handleEvent.call(this, event);
} else {
return listener.call(this, event);
}
};
}
/**
* Extract a single property value from an object by name.
*
* @param {!Object} object The object from which to extract the value.
* @param {string} name The name of the property. If the property is a
* method, the method will be called to get the value.
* @return {?} The extracted value, which could be anything.
* @private
*/
static _extractProperty(object, name) {
const value = object[name];
// If the property is a method, call it now.
if (value instanceof Function) {
return value.call(object);
} else {
return value;
}
}
/**
* Find a property descriptor for a particular property of an object. This
* allows access to getters and setters.
*
* @param {!Object} object The object for which we want to find a property
* descriptor.
* @param {string} k The name of the property.
* @return {Object} The property descriptor, or null if one cannot be found.
* @private
*/
static _getDescriptor(object, k) {
while (object) {
const descriptor = Object.getOwnPropertyDescriptor(object, k);
if (descriptor) {
return descriptor;
}
// Walk the prototype chain and keep looking.
object = Object.getPrototypeOf(object);
}
return null;
}
/**
* @param {!Object} traced The traced object.
* @param {string} className The class name.
* @param {TraceAnything.Options} options
* @return {string} The existing or newly-auto-generated ID for the object.
* @private
*/
static _getId(traced, className, options) {
if (options.idProperty && options.idProperty in traced) {
return traced[options.idProperty];
}
if (traced.__TraceAnythingId__ == null) {
const id = TraceAnything._nextGeneratedId.get(className) || 1;
TraceAnything._nextGeneratedId.set(className, id + 1);
traced.__TraceAnythingId__ = className + '_' + id;
}
return traced.__TraceAnythingId__;
}
}
/**
* Log type constants sent through the logger.
*
* @enum {string}
*/
TraceAnything.LogTypes = {
Constructor: 'Constructor',
Method: 'Method',
Getter: 'Getter',
Setter: 'Setter',
Event: 'Event',
Warning: 'Warning',
};
/**
* @typedef {{
* timestamp: Number,
* duration: Number,
* type: TraceAnything.LogTypes,
* instance: (!Object|undefined),
* instanceId: (string|undefined),
* message: (string|undefined),
* className: (string|undefined),
* methodName: (string|undefined),
* memberName: (string|undefined),
* eventName: (string|undefined),
* args: (!Array<?>|undefined),
* threw: (?|undefined),
* result: (?|undefined),
* value: (?|undefined)
* }}
* @property {Number} timestamp
* A timestamp of when the call was made, in milliseconds since 1970, UTC.
* Suitable for use in the Date constructor in JavaScript.
* @property {Number} duration
* The duration of this call in milliseconds. This is particularly useful
* information for async methods. The value may be 0 for some types, such as
* events or warnings.
* @property {TraceAnything.LogTypes} type
* The type of log.
* @property {(!Object|undefined)} instance
* The instance on which this method call / getter / setter / event occurred.
* @property {(string|undefined)} instanceId
* The instance ID. May be auto-generated for some types.
* See TraceAnything.Options.idProperty.
* @property {(string|undefined)} message
* A message for Warning-type logs.
* @property {(string|undefined)} className
* A class name for non-Warning-type logs.
* @property {(string|undefined)} methodName
* A method name for Method-type logs.
* @property {(string|undefined)} memberName
* A member name for Getter- and Setter-type logs.
* @property {(string|undefined)} eventName
* An event name for Event-type logs.
* @property {(!Array<?>|undefined)} args
* An arguments array for Constructor- and Method-type logs.
* @property {(?|undefined)} threw
* What was thrown if the constructor/method/getter/setter threw.
* @property {(?|undefined)} result
* What was returned if the constructor/method/getter did not throw.
* @property {(?|undefined)} value
* The value that was set in a setter, or the object property associated with
* an event by its name. (For example, object.error for an error event, or
* object.keyStatuses for a keystatuseschange event.)
*/
TraceAnything.Log;
/**
* The default logger for TraceAnything. Everything will be logged to the
* JavaScript console, and in a rich format where objects can be interrogated
* further.
*
* @param {TraceAnything.Log} log
*/
TraceAnything.defaultLogger = (log) => {
// NOTE: We are not combining everything into a single string in the default
// logger, because the JS console is actually capable of printing complex
// values like objects and arrays.
let logPrefix = `TraceAnything: (ID ${log.instanceId}) `;
if (log.type == TraceAnything.LogTypes.Warning) {
console.warn(logPrefix + log.message);
return;
}
if (log.type == TraceAnything.LogTypes.Constructor) {
logPrefix += `new ${log.className}`;
} else if (log.type == TraceAnything.LogTypes.Method) {
logPrefix += `${log.className}.${log.methodName}`;
} else if (log.type == TraceAnything.LogTypes.Getter ||
log.type == TraceAnything.LogTypes.Setter) {
logPrefix += `${log.className}.${log.memberName}`;
} else if (log.type == TraceAnything.LogTypes.Event) {
logPrefix += `${log.className} ${log.eventName} event`;
}
if (log.type == TraceAnything.LogTypes.Constructor ||
log.type == TraceAnything.LogTypes.Method) {
// For console logging, put a comma between the arguments.
// NOTE: Since we want to print potentially complex objects in the args
// array, we don't use join, which results in a single string.
const argsWithCommas = log.args.reduce((r, a) => r.concat(a, ','), []);
// Remove a trailing comma from the end of the array.
argsWithCommas.pop();
if (log.threw) {
console.error(`${logPrefix}(`, ...argsWithCommas, ') threw', log.threw);
} else {
console.debug(`${logPrefix}(`, ...argsWithCommas, ') =>', log.result);
}
} else if (log.type == TraceAnything.LogTypes.Getter) {
if (log.threw) {
console.error(`${logPrefix} threw`, log.threw);
} else {
console.debug(`${logPrefix} =>`, log.result);
}
} else if (log.type == TraceAnything.LogTypes.Setter) {
if (log.threw) {
console.error(`${logPrefix} =`, log.value, 'threw', log.threw);
} else {
console.debug(`${logPrefix} =`, log.value);
}
} else if (log.type == TraceAnything.LogTypes.Event) {
if ('value' in log) {
console.debug(logPrefix, log.event, '=>', log.value);
} else {
console.debug(logPrefix, log.event);
}
}
};
/**
* @typedef {{
* inPlace: boolean,
* methods: boolean,
* properties: boolean,
* treatPromisePropertiesAsEvents: boolean,
* extraProperties: !Array<string>,