@percy/sdk-utils 1.0.8 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundle.js +123 -445
- package/dist/index.js +72 -10
- package/dist/logger.js +140 -0
- package/dist/percy-dom.js +22 -8
- package/dist/percy-enabled.js +33 -37
- package/dist/percy-idle.js +18 -4
- package/dist/percy-info.js +9 -1
- package/dist/post-snapshot.js +21 -7
- package/dist/request.js +21 -5
- package/package.json +3 -13
- package/test/client.js +95 -154
- package/test/helpers.js +85 -8
- package/test/server.js +13 -16
package/dist/bundle.js
CHANGED
|
@@ -6,430 +6,6 @@
|
|
|
6
6
|
process.env = process.env || {};
|
|
7
7
|
process.env.__PERCY_BROWSERIFIED__ = true;
|
|
8
8
|
|
|
9
|
-
const {
|
|
10
|
-
assign,
|
|
11
|
-
entries
|
|
12
|
-
} = Object; // matches ansi escape sequences
|
|
13
|
-
|
|
14
|
-
const ANSI_REG = new RegExp('[\\u001B\\u009B][[\\]()#;?]*((?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' + '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g'); // color names by ansi escape code
|
|
15
|
-
|
|
16
|
-
const ANSI_COLORS = {
|
|
17
|
-
'91m': 'red',
|
|
18
|
-
'32m': 'green',
|
|
19
|
-
'93m': 'yellow',
|
|
20
|
-
'34m': 'blue',
|
|
21
|
-
'95m': 'magenta',
|
|
22
|
-
'90m': 'grey'
|
|
23
|
-
}; // colorize each line of a string using an ansi escape sequence
|
|
24
|
-
|
|
25
|
-
const LINE_REG = /^.*$/gm;
|
|
26
|
-
|
|
27
|
-
function colorize(code, str) {
|
|
28
|
-
return str.replace(LINE_REG, line => `\u001b[${code}${line}\u001b[39m`);
|
|
29
|
-
} // map ansi colors to bound colorize functions
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const colors = entries(ANSI_COLORS).reduce((colors, _ref) => {
|
|
33
|
-
let [code, name] = _ref;
|
|
34
|
-
return assign(colors, {
|
|
35
|
-
[name]: colorize.bind(null, code)
|
|
36
|
-
});
|
|
37
|
-
}, {});
|
|
38
|
-
|
|
39
|
-
function _defineProperty(obj, key, value) {
|
|
40
|
-
if (key in obj) {
|
|
41
|
-
Object.defineProperty(obj, key, {
|
|
42
|
-
value: value,
|
|
43
|
-
enumerable: true,
|
|
44
|
-
configurable: true,
|
|
45
|
-
writable: true
|
|
46
|
-
});
|
|
47
|
-
} else {
|
|
48
|
-
obj[key] = value;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return obj;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const URL_REGEXP = /\bhttps?:\/\/[^\s/$.?#].[^\s]*\b/i;
|
|
55
|
-
const LOG_LEVELS = {
|
|
56
|
-
debug: 0,
|
|
57
|
-
info: 1,
|
|
58
|
-
warn: 2,
|
|
59
|
-
error: 3
|
|
60
|
-
}; // A PercyLogger instance retains logs in-memory for quick lookups while also writing log
|
|
61
|
-
// messages to stdout and stderr depending on the log level and debug string.
|
|
62
|
-
|
|
63
|
-
class PercyLogger {
|
|
64
|
-
// default log level
|
|
65
|
-
// namespace regular expressions used to determine which debug logs to write
|
|
66
|
-
// in-memory store for logs and meta info
|
|
67
|
-
// track deprecations to limit noisy logging
|
|
68
|
-
// static vars can be overriden for testing
|
|
69
|
-
// Handles setting env var values and returns a singleton
|
|
70
|
-
constructor() {
|
|
71
|
-
_defineProperty(this, "level", 'info');
|
|
72
|
-
|
|
73
|
-
_defineProperty(this, "namespaces", {
|
|
74
|
-
include: [/^.*?$/],
|
|
75
|
-
exclude: []
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
_defineProperty(this, "messages", new Set());
|
|
79
|
-
|
|
80
|
-
_defineProperty(this, "deprecations", new Set());
|
|
81
|
-
|
|
82
|
-
let {
|
|
83
|
-
instance = this
|
|
84
|
-
} = this.constructor;
|
|
85
|
-
|
|
86
|
-
if (process.env.PERCY_DEBUG) {
|
|
87
|
-
instance.debug(process.env.PERCY_DEBUG);
|
|
88
|
-
} else if (process.env.PERCY_LOGLEVEL) {
|
|
89
|
-
instance.loglevel(process.env.PERCY_LOGLEVEL);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.constructor.instance = instance;
|
|
93
|
-
return instance;
|
|
94
|
-
} // Change log level at any time or return the current log level
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
loglevel(level) {
|
|
98
|
-
if (level) this.level = level;
|
|
99
|
-
return this.level;
|
|
100
|
-
} // Change namespaces by generating an array of namespace regular expressions from a
|
|
101
|
-
// comma separated debug string
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
debug(namespaces) {
|
|
105
|
-
if (this.namespaces.string === namespaces) return;
|
|
106
|
-
this.namespaces.string = namespaces;
|
|
107
|
-
namespaces = namespaces.split(/[\s,]+/).filter(Boolean);
|
|
108
|
-
if (!namespaces.length) return this.namespaces;
|
|
109
|
-
this.loglevel('debug');
|
|
110
|
-
this.namespaces = namespaces.reduce((namespaces, ns) => {
|
|
111
|
-
ns = ns.replace(/:?\*/g, m => m[0] === ':' ? ':?.*?' : '.*?');
|
|
112
|
-
|
|
113
|
-
if (ns[0] === '-') {
|
|
114
|
-
namespaces.exclude.push(new RegExp('^' + ns.substr(1) + '$'));
|
|
115
|
-
} else {
|
|
116
|
-
namespaces.include.push(new RegExp('^' + ns + '$'));
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
return namespaces;
|
|
120
|
-
}, {
|
|
121
|
-
string: namespaces,
|
|
122
|
-
include: [],
|
|
123
|
-
exclude: []
|
|
124
|
-
});
|
|
125
|
-
} // Creates a new log group and returns level specific functions for logging
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
group(name) {
|
|
129
|
-
return Object.keys(LOG_LEVELS).reduce((group, level) => Object.assign(group, {
|
|
130
|
-
[level]: this.log.bind(this, name, level)
|
|
131
|
-
}), {
|
|
132
|
-
deprecated: this.deprecated.bind(this, name),
|
|
133
|
-
shouldLog: this.shouldLog.bind(this, name),
|
|
134
|
-
progress: this.progress.bind(this, name),
|
|
135
|
-
format: this.format.bind(this, name),
|
|
136
|
-
loglevel: this.loglevel.bind(this),
|
|
137
|
-
stdout: this.constructor.stdout,
|
|
138
|
-
stderr: this.constructor.stderr
|
|
139
|
-
});
|
|
140
|
-
} // Query for a set of logs by filtering the in-memory store
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
query(filter) {
|
|
144
|
-
return Array.from(this.messages).filter(filter);
|
|
145
|
-
} // Formats messages before they are logged to stdio
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
format(debug, level, message, elapsed) {
|
|
149
|
-
let label = 'percy';
|
|
150
|
-
let suffix = '';
|
|
151
|
-
|
|
152
|
-
if (arguments.length === 1) {
|
|
153
|
-
// format(message)
|
|
154
|
-
[debug, message] = [null, debug];
|
|
155
|
-
} else if (arguments.length === 2) {
|
|
156
|
-
// format(debug, message)
|
|
157
|
-
[level, message] = [null, level];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (this.level === 'debug') {
|
|
161
|
-
// include debug info in the label
|
|
162
|
-
if (debug) label += `:${debug}`; // include elapsed time since last log
|
|
163
|
-
|
|
164
|
-
if (elapsed != null) {
|
|
165
|
-
suffix = ' ' + colors.grey(`(${elapsed}ms)`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
label = colors.magenta(label);
|
|
170
|
-
|
|
171
|
-
if (level === 'error') {
|
|
172
|
-
// red errors
|
|
173
|
-
message = colors.red(message);
|
|
174
|
-
} else if (level === 'warn') {
|
|
175
|
-
// yellow warnings
|
|
176
|
-
message = colors.yellow(message);
|
|
177
|
-
} else if (level === 'info' || level === 'debug') {
|
|
178
|
-
// blue info and debug URLs
|
|
179
|
-
message = message.replace(URL_REGEXP, colors.blue('$&'));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return `[${label}] ${message}${suffix}`;
|
|
183
|
-
} // Replaces the current line with a log message
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
progress(debug, message, persist) {
|
|
187
|
-
if (!this.shouldLog(debug, 'info')) return;
|
|
188
|
-
let {
|
|
189
|
-
stdout
|
|
190
|
-
} = this.constructor;
|
|
191
|
-
|
|
192
|
-
if (stdout.isTTY || !this._progress) {
|
|
193
|
-
message && (message = this.format(debug, message));
|
|
194
|
-
if (stdout.isTTY) stdout.cursorTo(0);else message && (message = message + '\n');
|
|
195
|
-
if (message) stdout.write(message);
|
|
196
|
-
if (stdout.isTTY) stdout.clearLine(1);
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
this._progress = !!message && {
|
|
200
|
-
message,
|
|
201
|
-
persist
|
|
202
|
-
};
|
|
203
|
-
} // Returns true or false if the level and debug group can write messages to stdio
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
shouldLog(debug, level) {
|
|
207
|
-
return LOG_LEVELS[level] != null && LOG_LEVELS[level] >= LOG_LEVELS[this.level] && !this.namespaces.exclude.some(ns => ns.test(debug)) && this.namespaces.include.some(ns => ns.test(debug));
|
|
208
|
-
} // Ensures that deprecation messages are not logged more than once
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
deprecated(debug, message, meta) {
|
|
212
|
-
if (this.deprecations.has(message)) return;
|
|
213
|
-
this.deprecations.add(message);
|
|
214
|
-
this.log(debug, 'warn', `Warning: ${message}`, meta);
|
|
215
|
-
} // Returns true if a socket is present and ready
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
get isRemote() {
|
|
219
|
-
var _this$socket;
|
|
220
|
-
|
|
221
|
-
return ((_this$socket = this.socket) === null || _this$socket === void 0 ? void 0 : _this$socket.readyState) === 1;
|
|
222
|
-
} // Generic log method accepts a debug group, log level, log message, and optional meta
|
|
223
|
-
// information to store with the message and other info
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
log(debug, level, message) {
|
|
227
|
-
let meta = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
228
|
-
// message might be an error object
|
|
229
|
-
let isError = typeof message !== 'string' && (level === 'error' || level === 'debug');
|
|
230
|
-
let error = isError && message; // if remote, send logs there
|
|
231
|
-
|
|
232
|
-
if (this.isRemote) {
|
|
233
|
-
// serialize error messages
|
|
234
|
-
message = isError && 'stack' in error ? {
|
|
235
|
-
message: error.message,
|
|
236
|
-
stack: error.stack
|
|
237
|
-
} : message;
|
|
238
|
-
return this.socket.send(JSON.stringify({
|
|
239
|
-
log: [debug, level, message, {
|
|
240
|
-
remote: true,
|
|
241
|
-
...meta
|
|
242
|
-
}]
|
|
243
|
-
}));
|
|
244
|
-
} // ensure the message is a string
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
message = isError && message.stack || message.message || message.toString(); // timestamp each log
|
|
248
|
-
|
|
249
|
-
let timestamp = Date.now();
|
|
250
|
-
let entry = {
|
|
251
|
-
debug,
|
|
252
|
-
level,
|
|
253
|
-
message,
|
|
254
|
-
meta,
|
|
255
|
-
timestamp
|
|
256
|
-
};
|
|
257
|
-
this.messages.add(entry); // maybe write the message to stdio
|
|
258
|
-
|
|
259
|
-
if (this.shouldLog(debug, level)) {
|
|
260
|
-
let elapsed = timestamp - (this.lastlog || timestamp);
|
|
261
|
-
if (isError && this.level !== 'debug') message = error.toString();
|
|
262
|
-
this.write(level, this.format(debug, error ? 'error' : level, message, elapsed));
|
|
263
|
-
this.lastlog = timestamp;
|
|
264
|
-
}
|
|
265
|
-
} // Writes a message to stdio based on the loglevel
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
write(level, message) {
|
|
269
|
-
var _this$_progress;
|
|
270
|
-
|
|
271
|
-
let {
|
|
272
|
-
stdout,
|
|
273
|
-
stderr
|
|
274
|
-
} = this.constructor;
|
|
275
|
-
let progress = stdout.isTTY && this._progress;
|
|
276
|
-
|
|
277
|
-
if (progress) {
|
|
278
|
-
stdout.cursorTo(0);
|
|
279
|
-
stdout.clearLine();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
(level === 'info' ? stdout : stderr).write(message + '\n');
|
|
283
|
-
if (!((_this$_progress = this._progress) !== null && _this$_progress !== void 0 && _this$_progress.persist)) delete this._progress;else if (progress) stdout.write(progress.message);
|
|
284
|
-
} // Opens a socket logging connection
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
connect(socket) {
|
|
288
|
-
// send logging environment info
|
|
289
|
-
let PERCY_DEBUG = process.env.PERCY_DEBUG;
|
|
290
|
-
let PERCY_LOGLEVEL = process.env.PERCY_LOGLEVEL || this.loglevel();
|
|
291
|
-
socket.send(JSON.stringify({
|
|
292
|
-
env: {
|
|
293
|
-
PERCY_DEBUG,
|
|
294
|
-
PERCY_LOGLEVEL
|
|
295
|
-
}
|
|
296
|
-
})); // attach remote logging handler
|
|
297
|
-
|
|
298
|
-
socket.onmessage = _ref => {
|
|
299
|
-
let {
|
|
300
|
-
data
|
|
301
|
-
} = _ref;
|
|
302
|
-
let {
|
|
303
|
-
log,
|
|
304
|
-
logAll
|
|
305
|
-
} = JSON.parse(data);
|
|
306
|
-
if (logAll) logAll.forEach(e => this.messages.add(e));
|
|
307
|
-
if (log) this.log(...log);
|
|
308
|
-
}; // return a cleanup function
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return () => {
|
|
312
|
-
socket.onmessage = null;
|
|
313
|
-
};
|
|
314
|
-
} // Connects to a remote logger
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
async remote(createSocket) {
|
|
318
|
-
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
319
|
-
if (this.isRemote) return; // if not already connected, wait until the timeout
|
|
320
|
-
|
|
321
|
-
let err = await new Promise(resolve => {
|
|
322
|
-
let done = event => {
|
|
323
|
-
if (timeoutid == null) return;
|
|
324
|
-
timeoutid = clearTimeout(timeoutid);
|
|
325
|
-
if (this.socket) this.socket.onopen = this.socket.onerror = null;
|
|
326
|
-
resolve((event === null || event === void 0 ? void 0 : event.error) || (event === null || event === void 0 ? void 0 : event.type) === 'error' && 'Error: Socket connection failed');
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
let timeoutid = setTimeout(done, timeout, {
|
|
330
|
-
error: 'Error: Socket connection timed out'
|
|
331
|
-
});
|
|
332
|
-
Promise.resolve().then(async () => {
|
|
333
|
-
this.socket = await createSocket();
|
|
334
|
-
if (this.isRemote) return done();
|
|
335
|
-
this.socket.onopen = this.socket.onerror = done;
|
|
336
|
-
}).catch(error => done({
|
|
337
|
-
error
|
|
338
|
-
}));
|
|
339
|
-
}); // there was an error connecting, will fallback to normal logging
|
|
340
|
-
|
|
341
|
-
if (err) {
|
|
342
|
-
this.log('logger', 'debug', 'Unable to connect to remote logger');
|
|
343
|
-
this.log('logger', 'debug', err);
|
|
344
|
-
return;
|
|
345
|
-
} // send any messages already logged in this environment
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
if (this.messages.size) {
|
|
349
|
-
this.socket.send(JSON.stringify({
|
|
350
|
-
logAll: Array.from(this.messages).map(entry => ({ ...entry,
|
|
351
|
-
meta: {
|
|
352
|
-
remote: true,
|
|
353
|
-
...entry.meta
|
|
354
|
-
}
|
|
355
|
-
}))
|
|
356
|
-
}));
|
|
357
|
-
} // attach an incoming message handler
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
this.socket.onmessage = _ref2 => {
|
|
361
|
-
let {
|
|
362
|
-
data
|
|
363
|
-
} = _ref2;
|
|
364
|
-
let {
|
|
365
|
-
env
|
|
366
|
-
} = JSON.parse(data); // update local environment info
|
|
367
|
-
|
|
368
|
-
if (env) Object.assign(process.env, env);
|
|
369
|
-
}; // return a cleanup function
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
return () => {
|
|
373
|
-
this.socket.onmessage = null;
|
|
374
|
-
this.socket = null;
|
|
375
|
-
};
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
_defineProperty(PercyLogger, "stdout", process.stdout);
|
|
381
|
-
|
|
382
|
-
_defineProperty(PercyLogger, "stderr", process.stderr);
|
|
383
|
-
|
|
384
|
-
class PercyBrowserLogger extends PercyLogger {
|
|
385
|
-
write(level, message) {
|
|
386
|
-
let out = ['warn', 'error'].includes(level) ? level : 'log';
|
|
387
|
-
let colors = [];
|
|
388
|
-
message = message.replace(ANSI_REG, (_, ansi) => {
|
|
389
|
-
colors.push(`color:${ANSI_COLORS[ansi] || 'inherit'}`);
|
|
390
|
-
return '%c';
|
|
391
|
-
});
|
|
392
|
-
console[out](message, ...colors);
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
progress() {
|
|
396
|
-
console.error('The log.progress() method is not supported in browsers');
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
function logger(name) {
|
|
402
|
-
return new PercyBrowserLogger().group(name);
|
|
403
|
-
}
|
|
404
|
-
Object.assign(logger, {
|
|
405
|
-
format: function () {
|
|
406
|
-
return new PercyBrowserLogger().format(...arguments);
|
|
407
|
-
},
|
|
408
|
-
query: function () {
|
|
409
|
-
return new PercyBrowserLogger().query(...arguments);
|
|
410
|
-
},
|
|
411
|
-
connect: function () {
|
|
412
|
-
return new PercyBrowserLogger().connect(...arguments);
|
|
413
|
-
},
|
|
414
|
-
remote: function () {
|
|
415
|
-
return new PercyBrowserLogger().remote(...arguments);
|
|
416
|
-
},
|
|
417
|
-
loglevel: function () {
|
|
418
|
-
return new PercyBrowserLogger().loglevel(...arguments);
|
|
419
|
-
}
|
|
420
|
-
});
|
|
421
|
-
Object.defineProperties(logger, {
|
|
422
|
-
Logger: {
|
|
423
|
-
get: () => PercyBrowserLogger
|
|
424
|
-
},
|
|
425
|
-
stdout: {
|
|
426
|
-
get: () => PercyBrowserLogger.stdout
|
|
427
|
-
},
|
|
428
|
-
stderr: {
|
|
429
|
-
get: () => PercyBrowserLogger.stderr
|
|
430
|
-
}
|
|
431
|
-
});
|
|
432
|
-
|
|
433
9
|
// helper to create a version object from a string
|
|
434
10
|
function toVersion(str) {
|
|
435
11
|
str || (str = '0.0.0');
|
|
@@ -489,6 +65,127 @@
|
|
|
489
65
|
|
|
490
66
|
};
|
|
491
67
|
|
|
68
|
+
const LOG_LEVELS = {
|
|
69
|
+
debug: 0,
|
|
70
|
+
info: 1,
|
|
71
|
+
warn: 2,
|
|
72
|
+
error: 3
|
|
73
|
+
}; // Create a small logger util using the specified namespace
|
|
74
|
+
|
|
75
|
+
function logger(namespace) {
|
|
76
|
+
return Object.keys(LOG_LEVELS).reduce((ns, lvl) => Object.assign(ns, {
|
|
77
|
+
[lvl]: log.bind(null, namespace, lvl)
|
|
78
|
+
}), {});
|
|
79
|
+
} // Set and/or return the local loglevel
|
|
80
|
+
|
|
81
|
+
const loglevel = logger.loglevel = lvl => {
|
|
82
|
+
return loglevel.lvl = lvl || loglevel.lvl || process.env.PERCY_LOGLEVEL || 'info';
|
|
83
|
+
}; // Track and send/write logs for the specified namespace and log level
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
const log = logger.log = (ns, lvl, msg, meta) => {
|
|
87
|
+
let err = typeof msg !== 'string' && (lvl === 'error' || lvl === 'debug');
|
|
88
|
+
meta = {
|
|
89
|
+
remote: true,
|
|
90
|
+
...meta
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
if (remote.socket) {
|
|
94
|
+
// prefer remote logging when available and serialize any errors
|
|
95
|
+
if (err) msg = {
|
|
96
|
+
name: msg.name,
|
|
97
|
+
message: msg.message,
|
|
98
|
+
stack: msg.stack
|
|
99
|
+
};
|
|
100
|
+
return remote.socket.send(JSON.stringify({
|
|
101
|
+
log: [ns, lvl, msg, meta]
|
|
102
|
+
}));
|
|
103
|
+
} else {
|
|
104
|
+
// keep log history when not remote
|
|
105
|
+
let [debug, level, message, timestamp] = [ns, lvl, msg, Date.now()];
|
|
106
|
+
(log.history || (log.history = [])).push({
|
|
107
|
+
debug,
|
|
108
|
+
level,
|
|
109
|
+
message,
|
|
110
|
+
meta,
|
|
111
|
+
timestamp
|
|
112
|
+
});
|
|
113
|
+
} // check if the specific level is within the local loglevel range
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
if (LOG_LEVELS[lvl] != null && LOG_LEVELS[lvl] >= LOG_LEVELS[loglevel()]) {
|
|
117
|
+
var _msg;
|
|
118
|
+
|
|
119
|
+
let debug = loglevel() === 'debug';
|
|
120
|
+
let label = debug ? `percy:${ns}` : 'percy'; // colorize the label when possible for consistency with the CLI logger
|
|
121
|
+
|
|
122
|
+
if (!process.env.__PERCY_BROWSERIFIED__) label = `\u001b[95m${label}\u001b[39m`;
|
|
123
|
+
msg = `[${label}] ${err && debug && ((_msg = msg) === null || _msg === void 0 ? void 0 : _msg.stack) || msg}`;
|
|
124
|
+
|
|
125
|
+
if (process.env.__PERCY_BROWSERIFIED__) {
|
|
126
|
+
// use console[warn|error|log] in browsers
|
|
127
|
+
console[['warn', 'error'].includes(lvl) ? lvl : 'log'](msg);
|
|
128
|
+
} else {
|
|
129
|
+
// use process[stdout|stderr].write in node
|
|
130
|
+
process[lvl === 'info' ? 'stdout' : 'stderr'].write(msg + '\n');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}; // Create a new WebSocket and resolve with it once connected
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
async function createWebSocket(address) {
|
|
137
|
+
let timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
138
|
+
// attempt to import `ws` in node environments
|
|
139
|
+
let WebSocket = process.env.__PERCY_BROWSERIFIED__
|
|
140
|
+
/* eslint-disable-next-line import/no-extraneous-dependencies */
|
|
141
|
+
? window.WebSocket : (await ({})).default;
|
|
142
|
+
let ws = new WebSocket(address.replace(/^http/, 'ws'));
|
|
143
|
+
return new Promise((resolve, reject) => {
|
|
144
|
+
let done = ws.onopen = ws.onerror = e => {
|
|
145
|
+
var _ws$_socket;
|
|
146
|
+
|
|
147
|
+
(_ws$_socket = ws._socket) === null || _ws$_socket === void 0 ? void 0 : _ws$_socket.unref();
|
|
148
|
+
clearTimeout(timeoutid);
|
|
149
|
+
ws.onopen = ws.onerror = null;
|
|
150
|
+
if (!e.error && e.type !== 'error') return resolve(ws);else reject(e.error || 'Error: Socket connection failed');
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
let timeoutid = setTimeout(done, timeout, {
|
|
154
|
+
error: 'Error: Socket connection timed out'
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
} // Connect to a remote logger at the specified address within the timeout
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
const remote = logger.remote = async timeout => {
|
|
161
|
+
try {
|
|
162
|
+
var _remote$socket;
|
|
163
|
+
|
|
164
|
+
// already connected
|
|
165
|
+
if (((_remote$socket = remote.socket) === null || _remote$socket === void 0 ? void 0 : _remote$socket.readyState) === 1) return; // connect to namespaced logging address
|
|
166
|
+
|
|
167
|
+
let address = new URL('/logger', info.address).href; // create and cache a websocket connection
|
|
168
|
+
|
|
169
|
+
let ws = remote.socket = await createWebSocket(address, timeout); // accept loglevel updates
|
|
170
|
+
|
|
171
|
+
/* istanbul ignore next: difficult to test currently */
|
|
172
|
+
|
|
173
|
+
ws.onmessage = e => loglevel(JSON.parse(e.data).loglevel); // cleanup message handler on close
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
ws.onclose = () => remote.socket = ws.onmessage = ws.onclose = null; // send any messages already logged in this environment
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
if (log.history) ws.send(JSON.stringify({
|
|
180
|
+
messages: log.history
|
|
181
|
+
}));
|
|
182
|
+
} catch (err) {
|
|
183
|
+
// there was an error connecting, will fallback to minimal logging
|
|
184
|
+
log('utils', 'debug', 'Unable to connect to remote logger');
|
|
185
|
+
log('utils', 'debug', err);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
492
189
|
async function request(path) {
|
|
493
190
|
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
494
191
|
let response = await request.fetch(`${info.address}${path}`, options); // maybe parse response body as json
|
|
@@ -536,7 +233,7 @@
|
|
|
536
233
|
request.fetch = async function fetch(url, options) {
|
|
537
234
|
let {
|
|
538
235
|
default: http
|
|
539
|
-
} = await
|
|
236
|
+
} = await ({});
|
|
540
237
|
return new Promise((resolve, reject) => {
|
|
541
238
|
http.request(url, options).on('response', response => {
|
|
542
239
|
let body = '';
|
|
@@ -552,25 +249,6 @@
|
|
|
552
249
|
};
|
|
553
250
|
}
|
|
554
251
|
|
|
555
|
-
async function connectRemoteLogger() {
|
|
556
|
-
await logger.remote(async () => {
|
|
557
|
-
let url = info.address.replace('http', 'ws');
|
|
558
|
-
|
|
559
|
-
if (process.env.__PERCY_BROWSERIFIED__) {
|
|
560
|
-
return new window.WebSocket(url);
|
|
561
|
-
} else {
|
|
562
|
-
/* eslint-disable-next-line import/no-extraneous-dependencies */
|
|
563
|
-
let {
|
|
564
|
-
default: WebSocket
|
|
565
|
-
} = await import('ws');
|
|
566
|
-
let ws = new WebSocket(url); // allow node to exit with an active connection
|
|
567
|
-
|
|
568
|
-
return ws.once('open', () => ws._socket.unref());
|
|
569
|
-
}
|
|
570
|
-
});
|
|
571
|
-
} // Check if Percy is enabled using the healthcheck endpoint
|
|
572
|
-
|
|
573
|
-
|
|
574
252
|
async function isPercyEnabled() {
|
|
575
253
|
if (info.enabled == null) {
|
|
576
254
|
let log = logger('utils');
|
|
@@ -596,7 +274,7 @@
|
|
|
596
274
|
}
|
|
597
275
|
|
|
598
276
|
if (info.enabled) {
|
|
599
|
-
await
|
|
277
|
+
await logger.remote();
|
|
600
278
|
}
|
|
601
279
|
}
|
|
602
280
|
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,72 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
Object.defineProperty(exports, "fetchPercyDOM", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _percyDom.default;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "isPercyEnabled", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _percyEnabled.default;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "logger", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return _logger.default;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(exports, "percy", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _percyInfo.default;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "postSnapshot", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _postSnapshot.default;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "request", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _request.default;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, "waitForPercyIdle", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _percyIdle.default;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
var _logger = _interopRequireDefault(require("./logger.js"));
|
|
51
|
+
|
|
52
|
+
var _percyInfo = _interopRequireDefault(require("./percy-info.js"));
|
|
53
|
+
|
|
54
|
+
var _request = _interopRequireDefault(require("./request.js"));
|
|
55
|
+
|
|
56
|
+
var _percyEnabled = _interopRequireDefault(require("./percy-enabled.js"));
|
|
57
|
+
|
|
58
|
+
var _percyIdle = _interopRequireDefault(require("./percy-idle.js"));
|
|
59
|
+
|
|
60
|
+
var _percyDom = _interopRequireDefault(require("./percy-dom.js"));
|
|
61
|
+
|
|
62
|
+
var _postSnapshot = _interopRequireDefault(require("./post-snapshot.js"));
|
|
63
|
+
|
|
64
|
+
var _default = _interopRequireWildcard(require("./index.js"));
|
|
65
|
+
|
|
66
|
+
exports.default = _default;
|
|
67
|
+
|
|
68
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
69
|
+
|
|
70
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
71
|
+
|
|
72
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|