@percy/core 1.8.0 → 1.9.1

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/api.js CHANGED
@@ -12,6 +12,18 @@ export function createPercyServer(percy, port) {
12
12
  port
13
13
  }) // facilitate logger websocket connections
14
14
  .websocket('/(logger)?', ws => {
15
+ var _percy$testing, _percy$testing2;
16
+
17
+ // support sabotaging remote logging connections in testing mode
18
+ if (((_percy$testing = percy.testing) === null || _percy$testing === void 0 ? void 0 : _percy$testing.remoteLogging) === false) return ws.terminate(); // track all remote logging connections in testing mode
19
+
20
+ if (percy.testing) ((_percy$testing2 = percy.testing).remoteLoggers || (_percy$testing2.remoteLoggers = new Set())).add(ws);
21
+ ws.addEventListener('close', () => {
22
+ var _percy$testing3, _percy$testing3$remot;
23
+
24
+ return (_percy$testing3 = percy.testing) === null || _percy$testing3 === void 0 ? void 0 : (_percy$testing3$remot = _percy$testing3.remoteLoggers) === null || _percy$testing3$remot === void 0 ? void 0 : _percy$testing3$remot.delete(ws);
25
+ }); // listen for messages with specific logging payloads
26
+
15
27
  ws.addEventListener('message', ({
16
28
  data
17
29
  }) => {
@@ -23,13 +35,14 @@ export function createPercyServer(percy, port) {
23
35
  for (let m of messages) logger.instance.messages.add(m);
24
36
 
25
37
  if (log) logger.instance.log(...log);
26
- });
38
+ }); // respond with the current loglevel
39
+
27
40
  ws.send(JSON.stringify({
28
41
  loglevel: logger.loglevel()
29
42
  }));
30
43
  }) // general middleware
31
44
  .route((req, res, next) => {
32
- var _percy$testing, _percy$testing3, _percy$testing3$api, _percy$testing4, _percy$testing4$api;
45
+ var _percy$testing4, _percy$testing7, _percy$testing7$api, _percy$testing8, _percy$testing8$api;
33
46
 
34
47
  // treat all request bodies as json
35
48
  if (req.body) try {
@@ -38,35 +51,55 @@ export function createPercyServer(percy, port) {
38
51
 
39
52
  res.setHeader('Access-Control-Expose-Headers', '*, X-Percy-Core-Version'); // skip or change api version header in testing mode
40
53
 
41
- if (((_percy$testing = percy.testing) === null || _percy$testing === void 0 ? void 0 : _percy$testing.version) !== false) {
42
- var _percy$testing2;
54
+ if (((_percy$testing4 = percy.testing) === null || _percy$testing4 === void 0 ? void 0 : _percy$testing4.version) !== false) {
55
+ var _percy$testing5;
43
56
 
44
- res.setHeader('X-Percy-Core-Version', ((_percy$testing2 = percy.testing) === null || _percy$testing2 === void 0 ? void 0 : _percy$testing2.version) ?? pkg.version);
45
- } // support sabotaging requests in testing mode
57
+ res.setHeader('X-Percy-Core-Version', ((_percy$testing5 = percy.testing) === null || _percy$testing5 === void 0 ? void 0 : _percy$testing5.version) ?? pkg.version);
58
+ } // track all api reqeusts in testing mode
46
59
 
47
60
 
48
- if (((_percy$testing3 = percy.testing) === null || _percy$testing3 === void 0 ? void 0 : (_percy$testing3$api = _percy$testing3.api) === null || _percy$testing3$api === void 0 ? void 0 : _percy$testing3$api[req.url.pathname]) === 'error') {
49
- return res.json(500, {
50
- success: false,
51
- error: 'Error: testing'
61
+ if (percy.testing && !req.url.pathname.startsWith('/test/')) {
62
+ var _percy$testing6;
63
+
64
+ ((_percy$testing6 = percy.testing).requests || (_percy$testing6.requests = [])).push({
65
+ url: `${req.url.pathname}${req.url.search}`,
66
+ method: req.method,
67
+ body: req.body
52
68
  });
53
- } else if (((_percy$testing4 = percy.testing) === null || _percy$testing4 === void 0 ? void 0 : (_percy$testing4$api = _percy$testing4.api) === null || _percy$testing4$api === void 0 ? void 0 : _percy$testing4$api[req.url.pathname]) === 'disconnect') {
54
- return req.connection.destroy();
69
+ } // support sabotaging requests in testing mode
70
+
71
+
72
+ if (((_percy$testing7 = percy.testing) === null || _percy$testing7 === void 0 ? void 0 : (_percy$testing7$api = _percy$testing7.api) === null || _percy$testing7$api === void 0 ? void 0 : _percy$testing7$api[req.url.pathname]) === 'error') {
73
+ next = () => {
74
+ var _percy$testing$build;
75
+
76
+ return Promise.reject(new Error(((_percy$testing$build = percy.testing.build) === null || _percy$testing$build === void 0 ? void 0 : _percy$testing$build.error) || 'testing'));
77
+ };
78
+ } else if (((_percy$testing8 = percy.testing) === null || _percy$testing8 === void 0 ? void 0 : (_percy$testing8$api = _percy$testing8.api) === null || _percy$testing8$api === void 0 ? void 0 : _percy$testing8$api[req.url.pathname]) === 'disconnect') {
79
+ next = () => req.connection.destroy();
55
80
  } // return json errors
56
81
 
57
82
 
58
- return next().catch(e => res.json(e.status ?? 500, {
59
- build: percy.build,
60
- error: e.message,
61
- success: false
62
- }));
83
+ return next().catch(e => {
84
+ var _percy$testing9;
85
+
86
+ return res.json(e.status ?? 500, {
87
+ build: ((_percy$testing9 = percy.testing) === null || _percy$testing9 === void 0 ? void 0 : _percy$testing9.build) || percy.build,
88
+ error: e.message,
89
+ success: false
90
+ });
91
+ });
63
92
  }) // healthcheck returns basic information
64
- .route('get', '/percy/healthcheck', (req, res) => res.json(200, {
65
- loglevel: percy.loglevel(),
66
- config: percy.config,
67
- build: percy.build,
68
- success: true
69
- })) // get or set config options
93
+ .route('get', '/percy/healthcheck', (req, res) => {
94
+ var _percy$testing10;
95
+
96
+ return res.json(200, {
97
+ build: ((_percy$testing10 = percy.testing) === null || _percy$testing10 === void 0 ? void 0 : _percy$testing10.build) ?? percy.build,
98
+ loglevel: percy.loglevel(),
99
+ config: percy.config,
100
+ success: true
101
+ });
102
+ }) // get or set config options
70
103
  .route(['get', 'post'], '/percy/config', async (req, res) => res.json(200, {
71
104
  config: req.body ? await percy.setConfig(req.body) : percy.config,
72
105
  success: true
@@ -105,28 +138,46 @@ export function createPercyServer(percy, port) {
105
138
  }
106
139
  }, res) => {
107
140
  body = Buffer.isBuffer(body) ? body.toString() : body;
141
+ let {
142
+ remoteLoggers
143
+ } = percy.testing;
108
144
 
109
145
  if (cmd === 'reset') {
110
146
  // the reset command will reset testing mode and clear any logs
111
- percy.testing = {};
147
+ percy.testing = remoteLoggers ? {
148
+ remoteLoggers
149
+ } : {};
112
150
  logger.instance.messages.clear();
113
151
  } else if (cmd === 'version') {
114
152
  // the version command will update the api version header for testing
115
153
  percy.testing.version = body;
116
154
  } else if (cmd === 'error' || cmd === 'disconnect') {
155
+ var _percy$testing11;
156
+
117
157
  // the error or disconnect commands will cause specific endpoints to fail
118
- percy.testing.api = { ...percy.testing.api,
119
- [body]: cmd
158
+ ((_percy$testing11 = percy.testing).api || (_percy$testing11.api = {}))[body] = cmd;
159
+ } else if (cmd === 'build-failure') {
160
+ // the build-failure command will cause api errors to include a failed build
161
+ percy.testing.build = {
162
+ failed: true,
163
+ error: 'Build failed'
120
164
  };
165
+ } else if (cmd === 'remote-logging') {
166
+ // the remote-logging command will toggle remote logging support
167
+ if (body === false) remoteLoggers === null || remoteLoggers === void 0 ? void 0 : remoteLoggers.forEach(ws => ws.terminate());
168
+ percy.testing.remoteLogging = body;
121
169
  } else {
122
170
  // 404 for unknown commands
123
171
  return res.send(404);
124
172
  }
125
173
 
126
174
  return res.json(200, {
127
- testing: percy.testing
175
+ success: true
128
176
  });
129
- }) // returns an array of raw logs from the logger
177
+ }) // returns an array of raw requests made to the api
178
+ .route('get', '/test/requests', (req, res) => res.json(200, {
179
+ requests: percy.testing.requests
180
+ })) // returns an array of raw logs from the logger
130
181
  .route('get', '/test/logs', (req, res) => res.json(200, {
131
182
  logs: Array.from(logger.instance.messages)
132
183
  })) // serves a very basic html page for testing snapshots
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/core",
3
- "version": "1.8.0",
3
+ "version": "1.9.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,10 +39,10 @@
39
39
  "test:types": "tsd"
40
40
  },
41
41
  "dependencies": {
42
- "@percy/client": "1.8.0",
43
- "@percy/config": "1.8.0",
44
- "@percy/dom": "1.8.0",
45
- "@percy/logger": "1.8.0",
42
+ "@percy/client": "1.9.1",
43
+ "@percy/config": "1.9.1",
44
+ "@percy/dom": "1.9.1",
45
+ "@percy/logger": "1.9.1",
46
46
  "content-disposition": "^0.5.4",
47
47
  "cross-spawn": "^7.0.3",
48
48
  "extract-zip": "^2.0.1",
@@ -53,5 +53,5 @@
53
53
  "rimraf": "^3.0.2",
54
54
  "ws": "^8.0.0"
55
55
  },
56
- "gitHead": "596808e5775a87514d3c7ffc8ac593cdc9289ad7"
56
+ "gitHead": "b2ff5100698488c8bf8c681972f4f2b9a1e2055f"
57
57
  }
package/types/index.d.ts CHANGED
@@ -38,6 +38,8 @@ interface CommonSnapshotOptions {
38
38
  minHeight?: number;
39
39
  percyCSS?: string;
40
40
  enableJavaScript?: boolean;
41
+ devicePixelRatio?: number;
42
+ scope?: string;
41
43
  }
42
44
 
43
45
  export interface SnapshotOptions extends CommonSnapshotOptions {