@akc42/app-utils 3.2.2 → 3.3.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/config-promise.js +3 -3
- package/debug.js +72 -20
- package/package.json +1 -1
package/config-promise.js
CHANGED
|
@@ -20,12 +20,12 @@
|
|
|
20
20
|
|
|
21
21
|
let configPromise;
|
|
22
22
|
|
|
23
|
-
export function
|
|
24
|
-
configPromise = promise;
|
|
23
|
+
export function setConfig(promise) {
|
|
24
|
+
configPromise = promise; //set to undefined to allow config call to request from server again.
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
async function config() {
|
|
28
|
-
if (configPromise === undefined) {
|
|
28
|
+
if (typeof configPromise === 'undefined') {
|
|
29
29
|
let resolved = false;
|
|
30
30
|
let resolver;
|
|
31
31
|
configPromise = new Promise(accept => resolver = accept);
|
package/debug.js
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
/*
|
|
22
22
|
The purpose of this module is to provide a debugable capability which can be
|
|
23
23
|
dynamically switched on and off browser by setting a key in the config
|
|
24
|
-
returned by the server. It will post request to '/api/
|
|
24
|
+
returned by the server. It will post request to '/api/debuglog' url with
|
|
25
25
|
application/json body part containing message, topic and gap, where message is
|
|
26
26
|
the concatenation of the debug parameters separated by space, topic is the
|
|
27
27
|
topic of this debug message and gap is the number of milliseconds since the
|
|
@@ -41,31 +41,85 @@
|
|
|
41
41
|
|
|
42
42
|
debug(..messages) //messages will be concatenated by space
|
|
43
43
|
|
|
44
|
-
the debug function will only log the message if config.debug (see
|
|
45
|
-
config-promise) is set to a string which is a
|
|
44
|
+
the debug function will only log the message on the server if config.debug (see
|
|
45
|
+
config-promise) is set to a string which is a colon separated list of topics
|
|
46
46
|
and that list has the topic for this debug call in it.
|
|
47
47
|
|
|
48
48
|
NOTE: It is normally expected for the server to provide a mechanism to update
|
|
49
|
-
the config before it is returned
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
49
|
+
the config before it is returned, However an alternative approach would be to
|
|
50
|
+
specifically overwrite sessionStorage 'debug' item with a new list of topics when you want
|
|
51
|
+
debug to switch on and off dynamically.
|
|
52
|
+
|
|
53
|
+
regardless of whether the message is logged on the server, it is also added to the performance.mark buffer
|
|
54
|
+
so that it can be sent to the server on a crash.
|
|
55
|
+
|
|
56
|
+
Although Debug is the default export this module also provides the following named exports
|
|
57
|
+
|
|
58
|
+
initialiseDebug - this function is used to manage tracing of debug messages regardless of whether the topic is set
|
|
59
|
+
It also adds an event handler to handle resource buffer full events. NOTE if the buffer fills up priority is given
|
|
60
|
+
|
|
61
|
+
unloadDebug - this function tidies up and reverses the initialiseDebug
|
|
62
|
+
|
|
63
|
+
debugDump - perform a dump to the server (and and a clearing out of the buffered info) of the debug calls made to date
|
|
54
64
|
|
|
55
65
|
*/
|
|
56
66
|
import config from './config-promise.js';
|
|
57
67
|
|
|
68
|
+
const BUFFER_SIZE = 50;
|
|
69
|
+
const KEY_TOPIC = 'key'; //topic name which will get kept from a full resource buffer when we empty it.
|
|
70
|
+
let buffer = []; //buffer of up to 50 topic/message pairs to allow us to hold some if resource buffer becomes full;
|
|
71
|
+
|
|
58
72
|
const topicMap = new Map();
|
|
59
73
|
|
|
74
|
+
let initialised = false;
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
function bufferFull() {
|
|
78
|
+
const entries = performance.getEntriesByType('mark');
|
|
79
|
+
performance.clearMarks();
|
|
80
|
+
const startPoint = entries.length - BUFFER_SIZE;
|
|
81
|
+
buffer.splice(0, buffer.length + startPoint);
|
|
82
|
+
for (let i = 0; i < entries.length; i++) {
|
|
83
|
+
if (entries[i].name === KEY_TOPIC || i >= startPoint) {
|
|
84
|
+
buffer.push({ topic: entries[i].name, message: entries[i].detail, time: Math.round(entries[i].startTime) });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function initialiseDebug() {
|
|
90
|
+
initialised = true;
|
|
91
|
+
buffer = [];
|
|
92
|
+
performance.setResourceTimingBufferSize(150)
|
|
93
|
+
window.addEventListener('resourcetimingbufferfull', bufferFull)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function unloadDebug() {
|
|
97
|
+
window.removeEventListener('resourcetimingbufferfull', bufferFull);
|
|
98
|
+
}
|
|
99
|
+
function debugDump() {
|
|
100
|
+
bufferFull(); //this clears out the marks and gives us a buffer to now send to
|
|
101
|
+
buffer.reverse();
|
|
102
|
+
let message = '';
|
|
103
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
104
|
+
message += `(${buffer[i].topic}) ${buffer[i].message} :gap ${i < buffer.length - 1 ? buffer[i].time - buffer[i + 1].time : 0}ms\n`;
|
|
105
|
+
}
|
|
106
|
+
const blob = new Blob([JSON.stringify({
|
|
107
|
+
message: message
|
|
108
|
+
})], { type: 'application/json' });
|
|
109
|
+
navigator.sendBeacon(`/api/debuglog/clientpath`, blob);
|
|
110
|
+
|
|
111
|
+
buffer = []; //we will start our buffer from scratch again
|
|
112
|
+
|
|
113
|
+
}
|
|
60
114
|
|
|
61
115
|
|
|
62
|
-
function Debug
|
|
116
|
+
function Debug(t) {
|
|
63
117
|
if (typeof t !== 'string' || t.length === 0 || !/^[a-zA-Z]+$/.test(t)) {
|
|
64
118
|
console.error('Debug requires topic which is a non zero length string of only letters', t, 'Received');
|
|
65
119
|
throw new Error('Invalid Debug Topic');
|
|
66
120
|
}
|
|
67
|
-
const tl = t.toLowerCase();
|
|
68
|
-
if (topicMap.has(tl)
|
|
121
|
+
const tl = t.toLowerCase();
|
|
122
|
+
if (topicMap.has(tl)) {
|
|
69
123
|
const topic = topicMap.get(tl);
|
|
70
124
|
return topic.debug;
|
|
71
125
|
}
|
|
@@ -79,9 +133,12 @@ function Debug (t) {
|
|
|
79
133
|
const now = new Date().getTime();
|
|
80
134
|
const gap = now - this.timestamp;
|
|
81
135
|
this.timestamp = now;
|
|
136
|
+
const message = args.reduce((cum, arg) => {
|
|
137
|
+
return `${cum} ${arg}`.trim();
|
|
138
|
+
}, '');
|
|
139
|
+
if (initialised) performance.mark(this.topic, { detail: message }); //save our message locally regardless of if enabled
|
|
82
140
|
if (!this.defined) {
|
|
83
|
-
|
|
84
|
-
await config();
|
|
141
|
+
await config();
|
|
85
142
|
this.defined = true;
|
|
86
143
|
}
|
|
87
144
|
let enabled = false;
|
|
@@ -90,16 +147,11 @@ function Debug (t) {
|
|
|
90
147
|
const topics = debugConf.split(':');
|
|
91
148
|
if (topics.includes(this.topic)) enabled = true;
|
|
92
149
|
}
|
|
93
|
-
|
|
94
150
|
if (enabled) {
|
|
95
|
-
const message = args.reduce((cum, arg) => {
|
|
96
|
-
return `${cum} ${arg}`.trim();
|
|
97
|
-
}, '');
|
|
98
|
-
console.log(`+${gap}ms`, this.topic, message);
|
|
99
151
|
const blob = new Blob([JSON.stringify({
|
|
100
152
|
message: message,
|
|
101
153
|
gap: gap
|
|
102
|
-
})], { type: 'application/json' })
|
|
154
|
+
})], { type: 'application/json' });
|
|
103
155
|
|
|
104
156
|
navigator.sendBeacon(`/api/debuglog/${this.topic}`, blob);
|
|
105
157
|
}
|
|
@@ -110,4 +162,4 @@ function Debug (t) {
|
|
|
110
162
|
return topicHandler.debug
|
|
111
163
|
}
|
|
112
164
|
|
|
113
|
-
export default
|
|
165
|
+
export { Debug as default, initialiseDebug, unloadDebug, debugDump };
|