@percy/sdk-utils 1.0.0 → 1.0.3
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 +669 -0
- package/dist/index.js +10 -0
- package/dist/percy-dom.js +12 -0
- package/dist/percy-enabled.js +55 -0
- package/dist/percy-idle.js +10 -0
- package/dist/percy-info.js +59 -0
- package/dist/post-snapshot.js +17 -0
- package/dist/request.js +65 -0
- package/package.json +7 -7
- package/test/client.js +278 -0
- package/test/helpers.js +80 -0
- package/test/server.js +208 -0
package/dist/bundle.js
ADDED
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
(function (exports) {
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const process = (typeof globalThis !== "undefined" && globalThis.process) || {};
|
|
6
|
+
process.env = process.env || {};
|
|
7
|
+
process.env.__PERCY_BROWSERIFIED__ = true;
|
|
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
|
+
// helper to create a version object from a string
|
|
434
|
+
function toVersion(str) {
|
|
435
|
+
str || (str = '0.0.0');
|
|
436
|
+
return str.split(/\.|-/).reduce((version, part, i) => {
|
|
437
|
+
let v = parseInt(part, 10);
|
|
438
|
+
version[i] = isNaN(v) ? part : v;
|
|
439
|
+
return version;
|
|
440
|
+
}, {
|
|
441
|
+
get major() {
|
|
442
|
+
return this[0] || 0;
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
get minor() {
|
|
446
|
+
return this[1] || 0;
|
|
447
|
+
},
|
|
448
|
+
|
|
449
|
+
get patch() {
|
|
450
|
+
return this[2] || 0;
|
|
451
|
+
},
|
|
452
|
+
|
|
453
|
+
get prerelease() {
|
|
454
|
+
return this[3];
|
|
455
|
+
},
|
|
456
|
+
|
|
457
|
+
get build() {
|
|
458
|
+
return this[4];
|
|
459
|
+
},
|
|
460
|
+
|
|
461
|
+
toString() {
|
|
462
|
+
return str;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
});
|
|
466
|
+
} // private version cache
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
let version = toVersion(); // contains local percy info
|
|
470
|
+
|
|
471
|
+
const info = {
|
|
472
|
+
// get or set the CLI API address via the environment
|
|
473
|
+
get address() {
|
|
474
|
+
return process.env.PERCY_SERVER_ADDRESS || 'http://localhost:5338';
|
|
475
|
+
},
|
|
476
|
+
|
|
477
|
+
set address(addr) {
|
|
478
|
+
return process.env.PERCY_SERVER_ADDRESS = addr;
|
|
479
|
+
},
|
|
480
|
+
|
|
481
|
+
// version information
|
|
482
|
+
get version() {
|
|
483
|
+
return version;
|
|
484
|
+
},
|
|
485
|
+
|
|
486
|
+
set version(v) {
|
|
487
|
+
return version = toVersion(v);
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
async function request(path) {
|
|
493
|
+
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
494
|
+
let response = await request.fetch(`${info.address}${path}`, options); // maybe parse response body as json
|
|
495
|
+
|
|
496
|
+
if (typeof response.body === 'string' && response.headers['content-type'] === 'application/json') {
|
|
497
|
+
try {
|
|
498
|
+
response.body = JSON.parse(response.body);
|
|
499
|
+
} catch (e) {}
|
|
500
|
+
} // throw an error if status is not ok
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
if (!(response.status >= 200 && response.status < 300)) {
|
|
504
|
+
throw Object.assign(new Error(), {
|
|
505
|
+
message: response.body.error || `${response.status} ${response.statusText}`,
|
|
506
|
+
response
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return response;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
request.post = function post(url, json) {
|
|
514
|
+
return request(url, {
|
|
515
|
+
method: 'POST',
|
|
516
|
+
body: JSON.stringify(json)
|
|
517
|
+
});
|
|
518
|
+
}; // environment specific implementation
|
|
519
|
+
|
|
520
|
+
|
|
521
|
+
if (process.env.__PERCY_BROWSERIFIED__) {
|
|
522
|
+
// use window.fetch in browsers
|
|
523
|
+
const winFetch = window.fetch;
|
|
524
|
+
|
|
525
|
+
request.fetch = async function fetch(url, options) {
|
|
526
|
+
let response = await winFetch(url, options);
|
|
527
|
+
return {
|
|
528
|
+
status: response.status,
|
|
529
|
+
statusText: response.statusText,
|
|
530
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
531
|
+
body: await response.text()
|
|
532
|
+
};
|
|
533
|
+
};
|
|
534
|
+
} else {
|
|
535
|
+
// use http.request in node
|
|
536
|
+
request.fetch = async function fetch(url, options) {
|
|
537
|
+
let {
|
|
538
|
+
default: http
|
|
539
|
+
} = await import('http');
|
|
540
|
+
return new Promise((resolve, reject) => {
|
|
541
|
+
http.request(url, options).on('response', response => {
|
|
542
|
+
let body = '';
|
|
543
|
+
response.on('data', chunk => body += chunk.toString());
|
|
544
|
+
response.on('end', () => resolve({
|
|
545
|
+
status: response.statusCode,
|
|
546
|
+
statusText: response.statusMessage,
|
|
547
|
+
headers: response.headers,
|
|
548
|
+
body
|
|
549
|
+
}));
|
|
550
|
+
}).on('error', reject).end(options.body);
|
|
551
|
+
});
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
|
|
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
|
+
async function isPercyEnabled() {
|
|
575
|
+
if (info.enabled == null) {
|
|
576
|
+
let log = logger('utils');
|
|
577
|
+
let error;
|
|
578
|
+
|
|
579
|
+
try {
|
|
580
|
+
let response = await request('/percy/healthcheck');
|
|
581
|
+
info.version = response.headers['x-percy-core-version'];
|
|
582
|
+
info.config = response.body.config;
|
|
583
|
+
info.enabled = true;
|
|
584
|
+
} catch (e) {
|
|
585
|
+
info.enabled = false;
|
|
586
|
+
error = e;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
if (info.enabled && info.version.major !== 1) {
|
|
590
|
+
log.info('Unsupported Percy CLI version, disabling snapshots');
|
|
591
|
+
log.debug(`Found version: ${info.version}`);
|
|
592
|
+
info.enabled = false;
|
|
593
|
+
} else if (!info.enabled) {
|
|
594
|
+
log.info('Percy is not running, disabling snapshots');
|
|
595
|
+
log.debug(error);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (info.enabled) {
|
|
599
|
+
await connectRemoteLogger();
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
return info.enabled;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
const RETRY_ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT'];
|
|
607
|
+
async function waitForPercyIdle() {
|
|
608
|
+
try {
|
|
609
|
+
return !!(await request('/percy/idle'));
|
|
610
|
+
} catch (e) {
|
|
611
|
+
return RETRY_ERROR_CODES.includes(e.code) && waitForPercyIdle();
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
async function fetchPercyDOM() {
|
|
616
|
+
if (info.domScript == null) {
|
|
617
|
+
let response = await request('/percy/dom.js');
|
|
618
|
+
info.domScript = response.body;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
return info.domScript;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// error message, signal that Percy has been disabled.
|
|
625
|
+
|
|
626
|
+
async function postSnapshot(options, params) {
|
|
627
|
+
let query = params ? `?${new URLSearchParams(params)}` : '';
|
|
628
|
+
await request.post(`/percy/snapshot${query}`, options).catch(err => {
|
|
629
|
+
var _err$response, _err$response$body, _err$response$body$bu;
|
|
630
|
+
|
|
631
|
+
if ((_err$response = err.response) !== null && _err$response !== void 0 && (_err$response$body = _err$response.body) !== null && _err$response$body !== void 0 && (_err$response$body$bu = _err$response$body.build) !== null && _err$response$body$bu !== void 0 && _err$response$body$bu.error) {
|
|
632
|
+
info.enabled = false;
|
|
633
|
+
} else {
|
|
634
|
+
throw err;
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
640
|
+
__proto__: null,
|
|
641
|
+
logger: logger,
|
|
642
|
+
percy: info,
|
|
643
|
+
request: request,
|
|
644
|
+
isPercyEnabled: isPercyEnabled,
|
|
645
|
+
waitForPercyIdle: waitForPercyIdle,
|
|
646
|
+
fetchPercyDOM: fetchPercyDOM,
|
|
647
|
+
postSnapshot: postSnapshot,
|
|
648
|
+
'default': index
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
exports["default"] = index;
|
|
652
|
+
exports.fetchPercyDOM = fetchPercyDOM;
|
|
653
|
+
exports.isPercyEnabled = isPercyEnabled;
|
|
654
|
+
exports.logger = logger;
|
|
655
|
+
exports.percy = info;
|
|
656
|
+
exports.postSnapshot = postSnapshot;
|
|
657
|
+
exports.request = request;
|
|
658
|
+
exports.waitForPercyIdle = waitForPercyIdle;
|
|
659
|
+
|
|
660
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
661
|
+
|
|
662
|
+
})(this.PercySDKUtils = this.PercySDKUtils || {});
|
|
663
|
+
}).call(window);
|
|
664
|
+
|
|
665
|
+
if (typeof define === "function" && define.amd) {
|
|
666
|
+
define([], () => window.PercySDKUtils);
|
|
667
|
+
} else if (typeof module === "object" && module.exports) {
|
|
668
|
+
module.exports = window.PercySDKUtils;
|
|
669
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import logger from '@percy/logger';
|
|
2
|
+
import percy from './percy-info.js';
|
|
3
|
+
import request from './request.js';
|
|
4
|
+
import isPercyEnabled from './percy-enabled.js';
|
|
5
|
+
import waitForPercyIdle from './percy-idle.js';
|
|
6
|
+
import fetchPercyDOM from './percy-dom.js';
|
|
7
|
+
import postSnapshot from './post-snapshot.js';
|
|
8
|
+
export { logger, percy, request, isPercyEnabled, waitForPercyIdle, fetchPercyDOM, postSnapshot }; // export the namespace by default
|
|
9
|
+
|
|
10
|
+
export * as default from './index.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import percy from './percy-info.js';
|
|
2
|
+
import request from './request.js'; // Fetch and cache the @percy/dom script
|
|
3
|
+
|
|
4
|
+
export async function fetchPercyDOM() {
|
|
5
|
+
if (percy.domScript == null) {
|
|
6
|
+
let response = await request('/percy/dom.js');
|
|
7
|
+
percy.domScript = response.body;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return percy.domScript;
|
|
11
|
+
}
|
|
12
|
+
export default fetchPercyDOM;
|