@percy/core 1.32.9 → 1.32.10-beta.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
@@ -8,6 +8,7 @@ import WebdriverUtils from '@percy/webdriver-utils';
8
8
  import { handleSyncJob } from './snapshot.js';
9
9
  import { getMaestroHierarchyDrift } from './maestro-hierarchy.js';
10
10
  import { handleComparisonUpload } from './comparison-upload.js';
11
+ import { handlePdfSnapshot } from './pdf-snapshot.js';
11
12
  import { handleMaestroScreenshot } from './maestro-screenshot.js';
12
13
  // Previously, we used `createRequire(import.meta.url).resolve` to resolve the path to the module.
13
14
  // This approach relied on `createRequire`, which is Node.js-specific and less compatible with modern ESM (ECMAScript Module) standards.
@@ -95,6 +96,9 @@ function assertNotCrossOrigin(req) {
95
96
  }
96
97
  }
97
98
 
99
+ // Log levels an SDK may forward through POST /percy/log.
100
+ const SDK_LOG_LEVELS = new Set(['debug', 'info', 'warn', 'error']);
101
+
98
102
  // Create a Percy CLI API server instance
99
103
  export function createPercyServer(percy, port) {
100
104
  let pkg = getPackageJSON(import.meta.url);
@@ -295,7 +299,7 @@ export function createPercyServer(percy, port) {
295
299
  return res.json(200, response);
296
300
  })
297
301
  // post a comparison via multipart file upload
298
- .route('post', '/percy/comparison/upload', /* istanbul ignore next */(req, res) => handleComparisonUpload(req, res, percy))
302
+ .route('post', '/percy/comparison/upload', /* istanbul ignore next */(req, res) => handleComparisonUpload(req, res, percy)).route('post', '/percy/pdf/snapshot', (req, res) => handlePdfSnapshot(req, res, percy))
299
303
  // post a comparison by reading a Maestro screenshot from disk
300
304
  .route('post', '/percy/maestro-screenshot', (req, res) => handleMaestroScreenshot(req, res, percy))
301
305
  // flushes one or more snapshots from the internal queue
@@ -343,7 +347,29 @@ export function createPercyServer(percy, port) {
343
347
  const level = req.body.level;
344
348
  const message = req.body.message;
345
349
  const meta = req.body.meta || {};
346
- log[level](message, meta);
350
+
351
+ // `level` is attacker-controlled input on an unauthenticated endpoint, so
352
+ // it must never be used as a dynamic property key on the logger object
353
+ // (CWE-1321 / PER-8606, PER-8625). Only the four log levels are dispatched;
354
+ // anything else (`__proto__`, `constructor`, `loglevel`, ...) is rejected.
355
+ if (!SDK_LOG_LEVELS.has(level)) {
356
+ return res.json(400, {
357
+ error: 'Invalid log level'
358
+ });
359
+ }
360
+ switch (level) {
361
+ case 'debug':
362
+ log.debug(message, meta);
363
+ break;
364
+ case 'info':
365
+ log.info(message, meta);
366
+ break;
367
+ case 'warn':
368
+ log.warn(message, meta);
369
+ break;
370
+ default:
371
+ log.error(message, meta);
372
+ }
347
373
  res.json(200, {
348
374
  success: true
349
375
  });
package/dist/config.js CHANGED
@@ -1420,8 +1420,59 @@ export const comparisonSchema = {
1420
1420
  }
1421
1421
  };
1422
1422
 
1423
+ // Shape shared by `pages` and `excludePages`, defined once so the two cannot
1424
+ // drift apart. The string pattern constrains only the character set: the
1425
+ // grammar and every semantic rule (ranges, ordering, bounds, emptiness) belong
1426
+ // to parseSelection() in @percy/cli-pdf, which is what actually reads the value
1427
+ // and raises a precise error. Spelling the grammar out here as well meant the
1428
+ // two disagreed — the old pattern rejected "1,,3" and "2,", which the parser
1429
+ // accepts by skipping empty parts.
1430
+ const pageSelection = {
1431
+ oneOf: [{
1432
+ type: 'integer',
1433
+ minimum: 1
1434
+ }, {
1435
+ type: 'array',
1436
+ items: {
1437
+ type: 'integer',
1438
+ minimum: 1
1439
+ }
1440
+ }, {
1441
+ type: 'string',
1442
+ pattern: '^[\\d\\s,-]*\\d[\\d\\s,-]*$'
1443
+ }]
1444
+ };
1445
+
1423
1446
  // Grouped schemas for easier registration
1424
- export const schemas = [configSchema, snapshotSchema, comparisonSchema];
1447
+ export const pdfSnapshotSchema = {
1448
+ $id: '/pdf-snapshot',
1449
+ type: 'object',
1450
+ $ref: '/snapshot#/$defs/common',
1451
+ required: ['name'],
1452
+ unevaluatedProperties: false,
1453
+ properties: {
1454
+ name: {
1455
+ type: 'string',
1456
+ description: 'Base snapshot name; each page becomes "<name> | Page N"'
1457
+ },
1458
+ pages: {
1459
+ description: 'Pages to snapshot: 3, [1,2,5], "1-5", "1,3,8" or "2-" (to the end)',
1460
+ ...pageSelection
1461
+ },
1462
+ excludePages: {
1463
+ description: 'Pages to omit, applied after `pages`. Same forms as `pages`.',
1464
+ ...pageSelection
1465
+ },
1466
+ scale: {
1467
+ type: 'number',
1468
+ exclusiveMinimum: 0,
1469
+ maximum: 5,
1470
+ default: 2,
1471
+ description: 'Rasterization scale. Reduced automatically if a page would exceed 2000px.'
1472
+ }
1473
+ }
1474
+ };
1475
+ export const schemas = [configSchema, snapshotSchema, comparisonSchema, pdfSnapshotSchema];
1425
1476
 
1426
1477
  // Config migrate function
1427
1478
  export function configMigration(config, util) {
package/dist/discovery.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import logger from '@percy/logger';
2
2
  import Queue from './queue.js';
3
3
  import Page from './page.js';
4
- import { normalizeURL, hostnameMatches, createResource, createRootResource, createPercyCSSResource, createLogResource, redactSecrets, yieldAll, snapshotLogName, waitForTimeout, withRetries, waitForSelectorInsideBrowser, isGzipped, maybeScrollToBottom, assertNotMetadataTarget } from './utils.js';
4
+ import { normalizeURL, hostnameMatches, createResource, createRootResource, createPercyCSSResource, yieldAll, snapshotLogName, waitForTimeout, withRetries, waitForSelectorInsideBrowser, isGzipped, maybeScrollToBottom, assertNotMetadataTarget } from './utils.js';
5
5
  import { ByteLRU, entrySize, DiskSpillStore, createSpillDir } from './cache/byte-lru.js';
6
6
  import { sha256hash } from '@percy/client/utils';
7
7
  import Pako from 'pako';
@@ -224,12 +224,6 @@ function processSnapshotResources({
224
224
 
225
225
  // For multi dom root resources are stored as array
226
226
  resources = resources.flat();
227
-
228
- // include associated snapshot logs matched by meta information. Redact
229
- // secrets before egress — this per-snapshot log resource is a parallel
230
- // egress path to sendBuildLogs and must scrub tokens/credentials too (CWE-532).
231
- resources.push(createLogResource(redactSecrets(logger.snapshotLogs(snapshot.meta.snapshot))));
232
- logger.evictSnapshot(snapshot.meta.snapshot);
233
227
  if (process.env.PERCY_GZIP) {
234
228
  const kept = [];
235
229
  for (let index = 0; index < resources.length; index++) {
package/dist/page.js CHANGED
@@ -68,6 +68,43 @@ function serializeDomCapture(_, options) {
68
68
  url: document.URL
69
69
  };
70
70
  }
71
+
72
+ // Builds a real Error from a CDP exception.
73
+ //
74
+ // `exception.description` is the remote stack trace as one string, opening with
75
+ // the remote error's own `Name: message` and followed by its frames. This used
76
+ // to `throw` that bare string, so every caller saw `error.message === undefined`
77
+ // and an in-page failure surfaced as literally "undefined".
78
+ //
79
+ // `name` is deliberately blank. @percy/logger renders a thrown Error as
80
+ // `Error.prototype.toString.call(err)` and only falls back to `stack` at debug
81
+ // level (logger.js `log()`), so keeping the default name would both double the
82
+ // prefix -- "Error: Error: test error" -- and drop the remote frames from what
83
+ // the user sees. With an empty name, toString returns `message` verbatim, so the
84
+ // logged text stays byte-identical to what the thrown string produced, frames
85
+ // and all. That output is pinned by snapshot.test.js "logs execute errors and
86
+ // does not snapshot": those `at execute (<anonymous>:4:17)` lines are how a user
87
+ // debugs their own execute script.
88
+ //
89
+ // The trade is that `message` carries the frames too. Callers that want just the
90
+ // summary line -- an HTTP error body, say -- should take `message.split('\n')[0]`.
91
+ export function remoteError(exceptionDetails) {
92
+ let {
93
+ exception,
94
+ text
95
+ } = exceptionDetails ?? {};
96
+ let description = exception === null || exception === void 0 ? void 0 : exception.description;
97
+ if (!description) {
98
+ // A non-Error was thrown in the page, so CDP reports the thrown value
99
+ // rather than a stack. `text` is CDP's own summary of the exception.
100
+ let value = exception === null || exception === void 0 ? void 0 : exception.value;
101
+ description = value == null ? text || 'Unknown page error' : typeof value === 'object' ? JSON.stringify(value) : String(value);
102
+ }
103
+ let error = new Error(description);
104
+ error.name = '';
105
+ error.stack = description;
106
+ return error;
107
+ }
71
108
  export class Page {
72
109
  static TIMEOUT = undefined;
73
110
  log = logger('core:page');
@@ -214,7 +251,7 @@ export class Page {
214
251
  userGesture: true
215
252
  });
216
253
  if (exceptionDetails) {
217
- throw exceptionDetails.exception.description;
254
+ throw remoteError(exceptionDetails);
218
255
  } else {
219
256
  return result.value;
220
257
  }
@@ -0,0 +1,151 @@
1
+ import fs from 'fs';
2
+ import logger from '@percy/logger';
3
+ import { Server } from './server.js';
4
+ async function createAssetServer(pdfBuffer, assets) {
5
+ // Loopback only. This origin serves the customer's PDF with no auth, and the
6
+ // sole client is the discovery browser running on this machine -- unlike the
7
+ // API server it has no reason to be reachable off-box.
8
+ let server = Server.createServer({
9
+ port: 0,
10
+ host: '127.0.0.1'
11
+ });
12
+ server.serve('/pdfjs', assets.buildDir);
13
+ server.serve('/standard_fonts', assets.standardFontsDir);
14
+ server.serve('/cmaps', assets.cmapsDir);
15
+ server.route('get', '/doc.pdf', (req, res) => res.send(200, 'application/pdf', pdfBuffer));
16
+ server.route('get', '/', (req, res) => res.send(200, 'text/html', '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>'));
17
+ await server.listen();
18
+ return server;
19
+ }
20
+
21
+ // Marks an error the caller can fix by changing the request -- a page selection
22
+ // out of range, too many pages, an unusable scale, a page that rasterizes below
23
+ // Percy's minimum. handlePdfSnapshot answers 400 for these and 500 for
24
+ // everything else, so a browser launch failure or a CDP disconnect is no longer
25
+ // reported to the SDK as if the caller sent a bad request.
26
+ function asInputError(error) {
27
+ return Object.assign(error, {
28
+ status: 400
29
+ });
30
+ }
31
+
32
+ // Page#eval has no timeout of its own -- Page.TIMEOUT only covers navigation,
33
+ // and Runtime.callFunctionOn with awaitPromise waits forever -- so every in-page
34
+ // call is raced against one. The timer is unref'd so a pending race never holds
35
+ // the process open.
36
+ //
37
+ // A losing promise needs no catch of its own: Promise.race attaches handlers to
38
+ // every input, so a late rejection from the timed-out eval is already handled.
39
+ export function withTimeout(promise, ms, description) {
40
+ let timer;
41
+ let timeout = new Promise((resolve, reject) => {
42
+ timer = setTimeout(() => reject(new Error(`Timed out after ${ms}ms ${description}`)), ms);
43
+ timer.unref();
44
+ });
45
+ return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
46
+ }
47
+ export async function rasterizePdf(percy, pdfBuffer, options) {
48
+ let log = logger('core:pdf-rasterize');
49
+ let {
50
+ pdfjsAssets,
51
+ resolvePages,
52
+ fitScale,
53
+ assertRasterDimensions,
54
+ openDocument,
55
+ measurePages,
56
+ renderPage,
57
+ destroyDocument,
58
+ DEFAULT_SCALE,
59
+ MAX_SCALE,
60
+ PAGE_RENDER_TIMEOUT
61
+ } = await import('@percy/cli-pdf');
62
+ let scale = options.scale == null ? DEFAULT_SCALE : Number(options.scale);
63
+ if (!Number.isFinite(scale) || scale <= 0 || scale > MAX_SCALE) {
64
+ throw asInputError(new Error(`Invalid scale ${options.scale}: expected a number between 0 and ${MAX_SCALE}`));
65
+ }
66
+ let assets = pdfjsAssets();
67
+ let server = await createAssetServer(pdfBuffer, assets);
68
+ let origin = server.address();
69
+ let page;
70
+ try {
71
+ await percy.browser.launch();
72
+ page = await percy.browser.page({
73
+ meta: {
74
+ snapshot: {
75
+ name: 'pdf'
76
+ }
77
+ }
78
+ });
79
+ await page.goto(`${origin}/`);
80
+ let pdfjsSource = await fs.promises.readFile(assets.libPath, 'utf-8');
81
+ await withTimeout(/* eslint-disable-next-line no-new-func */
82
+ page.eval(new Function(pdfjsSource)), PAGE_RENDER_TIMEOUT, 'injecting pdf.js');
83
+ let {
84
+ pageCount
85
+ } = await withTimeout(page.eval(openDocument, {
86
+ origin
87
+ }), PAGE_RENDER_TIMEOUT, 'opening the PDF');
88
+ let selected;
89
+ try {
90
+ selected = resolvePages(options, pageCount);
91
+ } catch (error) {
92
+ throw asInputError(error);
93
+ }
94
+ log.debug(`Rendering ${selected.length} of ${pageCount} page(s) at scale ${scale}`);
95
+ let sizes = await withTimeout(page.eval(measurePages, {
96
+ pageNumbers: selected
97
+ }), PAGE_RENDER_TIMEOUT, 'measuring the PDF pages');
98
+ let pages = [];
99
+ for (let {
100
+ pageNumber,
101
+ width,
102
+ height
103
+ } of sizes) {
104
+ let effectiveScale = fitScale(scale, {
105
+ width,
106
+ height
107
+ });
108
+ if (effectiveScale < scale) {
109
+ log.warn(`Page ${pageNumber} is ${Math.round(width)}x${Math.round(height)}pt; ` + `scale reduced from ${scale} to ${effectiveScale.toFixed(3)} to stay within ` + 'Percy\'s 2000px limit');
110
+ }
111
+ let rendered = await withTimeout(page.eval(renderPage, {
112
+ pageNumber,
113
+ scale: effectiveScale
114
+ }), PAGE_RENDER_TIMEOUT, `rendering page ${pageNumber}`);
115
+ try {
116
+ assertRasterDimensions(pageNumber, rendered.width, rendered.height);
117
+ } catch (error) {
118
+ throw asInputError(error);
119
+ }
120
+ let png = Buffer.from(rendered.dataUrl.slice(rendered.dataUrl.indexOf(',') + 1), 'base64');
121
+ log.debug(`Page ${pageNumber}: ${rendered.width}x${rendered.height}px, ${png.length} bytes`);
122
+ pages.push({
123
+ page: pageNumber,
124
+ width: rendered.width,
125
+ height: rendered.height,
126
+ scale: effectiveScale,
127
+ png
128
+ });
129
+ }
130
+ await withTimeout(page.eval(destroyDocument), PAGE_RENDER_TIMEOUT, 'closing the PDF');
131
+ return {
132
+ pageCount,
133
+ scale,
134
+ pages
135
+ };
136
+ } finally {
137
+ // Settle both regardless: a timed-out page is exactly the case where close()
138
+ // is liable to reject, and letting that escape here would leak the asset
139
+ // server -- a listening socket still holding the customer's PDF. Report
140
+ // what failed rather than discarding it: a socket that will not drain is
141
+ // still holding that PDF, and nothing else would record it.
142
+ for (let result of await Promise.allSettled([(_page = page) === null || _page === void 0 ? void 0 : _page.close(), server.close()])) {
143
+ var _page;
144
+ /* istanbul ignore next: both closes resolve in every reachable test path */
145
+ if (result.status === 'rejected') {
146
+ var _result$reason;
147
+ log.debug(`PDF cleanup failed: ${((_result$reason = result.reason) === null || _result$reason === void 0 ? void 0 : _result$reason.message) ?? result.reason}`);
148
+ }
149
+ }
150
+ }
151
+ }
@@ -0,0 +1,256 @@
1
+ import logger from '@percy/logger';
2
+ import PercyConfig from '@percy/config';
3
+ import { ServerError } from './server.js';
4
+ import { handleSyncJob } from './snapshot.js';
5
+ import { rasterizePdf } from './pdf-rasterize.js';
6
+ import { createImageSnapshotResources, getPackageJSON, normalizeOptions } from './utils.js';
7
+ const MAX_PDF_BYTES = 50 * 1024 * 1024;
8
+ // base64 inflates by 4/3 and pads to a multiple of 4.
9
+ const MAX_PDF_BASE64_CHARS = Math.ceil(MAX_PDF_BYTES / 3) * 4;
10
+ const PDF_MAGIC = Buffer.from('%PDF-', 'latin1');
11
+
12
+ // Tagged onto the build's User-Agent so percy-api routes these pages down the
13
+ // extraction path instead of the renderer. Comparison#upload_snapshot? gates on
14
+ // `user_agent&.include?('@percy/cli-upload')` plus a root resource URL under
15
+ // `http://local/`, then recovers the image straight from the wrapper HTML.
16
+ // Measured ~1s per page extracted versus ~9-19s rendered, which is the whole
17
+ // point: the CLI already produced the exact PNG, so re-rendering it in the
18
+ // renderer fleet buys nothing.
19
+ //
20
+ // Because that check is a substring match, naming @percy/cli-pdf alongside it
21
+ // keeps the User-Agent honest about which code actually ran rather than
22
+ // impersonating the upload command.
23
+ export const UPLOAD_CLIENT_INFO = (() => {
24
+ let {
25
+ version
26
+ } = getPackageJSON(import.meta.url);
27
+ return [`@percy/cli-pdf/${version}`, `@percy/cli-upload/${version}`];
28
+ })();
29
+ export function pageSnapshotName(name, pageNumber) {
30
+ return `${name} | Page ${pageNumber}`;
31
+ }
32
+ function pageUrls(name, pageNumber) {
33
+ let base = `http://local/${encodeURIComponent(name)}/page-${pageNumber}`;
34
+ return {
35
+ rootUrl: base,
36
+ imageUrl: `${base}.png`
37
+ };
38
+ }
39
+ export function decodePdf(pdf) {
40
+ if (!pdf || typeof pdf !== 'object') {
41
+ throw new ServerError(400, 'Missing required `pdf` object');
42
+ }
43
+ let {
44
+ content
45
+ } = pdf;
46
+ if (typeof content !== 'string' || !content.length) {
47
+ throw new ServerError(400, 'Missing required `pdf.content` (base64-encoded PDF)');
48
+ }
49
+
50
+ // Reject on the ENCODED length first. Buffer.from() would otherwise allocate
51
+ // the full decode before we ever reach the size check, and the server buffers
52
+ // request bodies with no cap of its own (see IncomingMessage in server.js), so
53
+ // a 1GB body would be buffered, JSON-parsed and decoded before the 413.
54
+ if (content.length > MAX_PDF_BASE64_CHARS) {
55
+ throw new ServerError(413, `PDF exceeds the maximum size of ${MAX_PDF_BYTES / 1024 / 1024}MB`);
56
+ }
57
+ let buffer = Buffer.from(content, 'base64');
58
+ if (buffer.length < PDF_MAGIC.length) {
59
+ throw new ServerError(400, '`pdf.content` is too short to be a PDF');
60
+ }
61
+ if (buffer.length > MAX_PDF_BYTES) {
62
+ throw new ServerError(413, `PDF exceeds the maximum size of ${MAX_PDF_BYTES / 1024 / 1024}MB`);
63
+ }
64
+ if (!buffer.subarray(0, PDF_MAGIC.length).equals(PDF_MAGIC)) {
65
+ throw new ServerError(400, '`pdf.content` does not decode to a PDF (missing %PDF- header)');
66
+ }
67
+ return buffer;
68
+ }
69
+ export async function loadPdfModule(load = () => import('@percy/cli-pdf')) {
70
+ try {
71
+ return await load();
72
+ } catch (error) {
73
+ throw new ServerError(501, ['PDF snapshots require the @percy/cli-pdf package, which is not installed.', 'Install it with: npm install --save-dev @percy/cli-pdf', `(underlying error: ${error.message})`].join(' '));
74
+ }
75
+ }
76
+ function validatePdfSnapshotOptions(options) {
77
+ let log = logger('core:pdf-snapshot');
78
+ let normalized = normalizeOptions(options);
79
+ let {
80
+ clientInfo,
81
+ environmentInfo,
82
+ pdf,
83
+ ...validatable
84
+ } = normalized;
85
+ let errors = PercyConfig.validate(validatable, '/pdf-snapshot');
86
+ if ((errors === null || errors === void 0 ? void 0 : errors.length) > 0) {
87
+ log.warn('Invalid PDF snapshot options:');
88
+ for (let e of errors) log.warn(`- ${e.path}: ${e.message}`);
89
+ }
90
+ return normalized;
91
+ }
92
+ function queuePages(percy, {
93
+ name,
94
+ rendered,
95
+ sync,
96
+ snapshotOptions
97
+ }) {
98
+ return rendered.map(({
99
+ page,
100
+ width,
101
+ height,
102
+ png
103
+ }) => {
104
+ let snapshotName = pageSnapshotName(name, page);
105
+ let {
106
+ rootUrl,
107
+ imageUrl
108
+ } = pageUrls(name, page);
109
+ let options = {
110
+ ...snapshotOptions,
111
+ name: snapshotName,
112
+ widths: snapshotOptions.widths || [width],
113
+ minHeight: snapshotOptions.minHeight || height,
114
+ resources: () => buildPageResources({
115
+ rootUrl,
116
+ imageUrl,
117
+ snapshotName,
118
+ width,
119
+ height,
120
+ png
121
+ })
122
+ };
123
+ if (sync) options.sync = true;
124
+ let promise = null;
125
+ if (sync) {
126
+ promise = new Promise((resolve, reject) => {
127
+ percy.upload(options, {
128
+ resolve,
129
+ reject
130
+ }).catch(reject);
131
+ });
132
+ } else {
133
+ percy.upload(options);
134
+ }
135
+ return {
136
+ page,
137
+ snapshotName,
138
+ promise
139
+ };
140
+ });
141
+ }
142
+ function buildPageResources({
143
+ rootUrl,
144
+ imageUrl,
145
+ snapshotName,
146
+ width,
147
+ height,
148
+ png
149
+ }) {
150
+ return createImageSnapshotResources({
151
+ name: snapshotName,
152
+ rootUrl,
153
+ imageUrl,
154
+ width,
155
+ height,
156
+ content: png,
157
+ mimetype: 'image/png'
158
+ });
159
+ }
160
+ export async function handlePdfSnapshot(req, res, percy) {
161
+ let log = logger('core:pdf-snapshot');
162
+ let body = req.body;
163
+ if (!body || typeof body !== 'object' || Array.isArray(body) || Buffer.isBuffer(body)) {
164
+ throw new ServerError(400, 'Expected a JSON object body');
165
+ }
166
+ let {
167
+ name,
168
+ pdf,
169
+ pages,
170
+ excludePages,
171
+ scale,
172
+ ...rest
173
+ } = validatePdfSnapshotOptions(body);
174
+ if (typeof name !== 'string' || !name.trim()) {
175
+ throw new ServerError(400, 'Missing required `name`');
176
+ }
177
+ let buffer = decodePdf(pdf);
178
+ await loadPdfModule();
179
+ let sync = percy.syncMode(rest);
180
+ let rasterized;
181
+ try {
182
+ rasterized = await rasterizePdf(percy, buffer, {
183
+ pages,
184
+ excludePages,
185
+ scale
186
+ });
187
+ } catch (error) {
188
+ // Only errors the caller can act on are 400s -- rasterizePdf tags those with
189
+ // `status`. A browser launch failure, an OOM, an asset-server bind error or
190
+ // a CDP disconnect are ours, not theirs, and must not tell the SDK that its
191
+ // request was malformed.
192
+ // remoteError puts the remote frames in `message` so the logger prints them
193
+ // (see page.js); the HTTP body wants only the summary line.
194
+ let message = ((error === null || error === void 0 ? void 0 : error.message) ?? String(error)).split('\n')[0];
195
+ log.error(`Failed to rasterize PDF "${name}": ${message}`);
196
+ throw new ServerError((error === null || error === void 0 ? void 0 : error.status) ?? 500, `Could not rasterize PDF: ${message}`);
197
+ }
198
+ let {
199
+ pageCount,
200
+ pages: rendered
201
+ } = rasterized;
202
+ percy.client.addClientInfo(rest.clientInfo);
203
+ percy.client.addClientInfo(UPLOAD_CLIENT_INFO);
204
+ percy.client.addEnvironmentInfo(rest.environmentInfo);
205
+ let {
206
+ clientInfo,
207
+ environmentInfo,
208
+ sync: _sync,
209
+ ...snapshotOptions
210
+ } = rest;
211
+ log.info(`PDF "${name}": snapshotting ${rendered.length} of ${pageCount} page(s)` + (sync ? ' (waiting for comparison results)' : ''));
212
+ let queued = queuePages(percy, {
213
+ name,
214
+ rendered,
215
+ sync,
216
+ snapshotOptions
217
+ });
218
+ if (!sync) {
219
+ return res.json(200, {
220
+ success: true,
221
+ data: {
222
+ 'pdf-name': name,
223
+ 'page-count': pageCount,
224
+ 'pages-snapshotted': queued.length,
225
+ status: 'queued',
226
+ pages: queued.map(({
227
+ page,
228
+ snapshotName
229
+ }) => ({
230
+ page,
231
+ 'snapshot-name': snapshotName
232
+ }))
233
+ }
234
+ });
235
+ }
236
+ let results = await Promise.all(queued.map(async ({
237
+ page,
238
+ snapshotName,
239
+ promise
240
+ }) => ({
241
+ ...(await handleSyncJob(promise, percy, 'snapshot')),
242
+ page,
243
+ 'snapshot-name': snapshotName
244
+ })));
245
+ let failed = results.filter(r => r.error || r.status === 'failure');
246
+ return res.json(200, {
247
+ success: true,
248
+ data: {
249
+ 'pdf-name': name,
250
+ 'page-count': pageCount,
251
+ 'pages-snapshotted': results.length,
252
+ status: failed.length ? 'failure' : 'success',
253
+ pages: results
254
+ }
255
+ });
256
+ }
package/dist/server.js CHANGED
@@ -116,11 +116,13 @@ export class ServerError extends Error {
116
116
  // custom server class handles routing requests and provides alternate methods and properties
117
117
  var _sockets = /*#__PURE__*/new WeakMap();
118
118
  var _defaultPort = /*#__PURE__*/new WeakMap();
119
+ var _host = /*#__PURE__*/new WeakMap();
119
120
  var _routes = /*#__PURE__*/new WeakMap();
120
121
  var _Server_brand = /*#__PURE__*/new WeakSet();
121
122
  export class Server extends http.Server {
122
123
  constructor({
123
- port
124
+ port,
125
+ host
124
126
  } = {}) {
125
127
  super({
126
128
  IncomingMessage,
@@ -130,6 +132,7 @@ export class Server extends http.Server {
130
132
  _classPrivateMethodInitSpec(this, _Server_brand);
131
133
  _classPrivateFieldInitSpec(this, _sockets, new Set());
132
134
  _classPrivateFieldInitSpec(this, _defaultPort, void 0);
135
+ _classPrivateFieldInitSpec(this, _host, void 0);
133
136
  // initial routes include cors and 404 handling
134
137
  _classPrivateFieldInitSpec(this, _routes, [{
135
138
  priority: -1,
@@ -168,6 +171,7 @@ export class Server extends http.Server {
168
171
  handle: req => ServerError.throw(404)
169
172
  }]);
170
173
  _classPrivateFieldSet(_defaultPort, this, port);
174
+ _classPrivateFieldSet(_host, this, host);
171
175
 
172
176
  // handle requests on end
173
177
  this.on('request', (req, res) => {
@@ -181,8 +185,13 @@ export class Server extends http.Server {
181
185
  }
182
186
 
183
187
  // return host bind address - defaults to "::"
188
+ //
189
+ // An explicit `host` wins over the environment: a server that must not be
190
+ // reachable off-box (the PDF asset server, which serves the customer's
191
+ // document unauthenticated) has to stay on loopback even where an operator
192
+ // has widened PERCY_SERVER_HOST for the API server.
184
193
  get host() {
185
- return process.env.PERCY_SERVER_HOST || '::';
194
+ return _classPrivateFieldGet(_host, this) || process.env.PERCY_SERVER_HOST || '::';
186
195
  }
187
196
 
188
197
  // return the listening port or any default port
@@ -438,11 +447,13 @@ export function createServer(options = {}) {
438
447
  let {
439
448
  serve,
440
449
  port,
450
+ host,
441
451
  baseUrl = '/',
442
452
  ...opts
443
453
  } = options;
444
454
  let server = new Server({
445
- port
455
+ port,
456
+ host
446
457
  });
447
458
  return serve ? server.serve(baseUrl, serve, opts) : server;
448
459
  }
package/dist/snapshot.js CHANGED
@@ -1,9 +1,10 @@
1
1
  import logger from '@percy/logger';
2
2
  import PercyConfig from '@percy/config';
3
3
  import micromatch from 'micromatch';
4
+ import Pako from 'pako';
4
5
  import { configSchema } from './config.js';
5
6
  import Queue from './queue.js';
6
- import { request, hostnameMatches, yieldTo, snapshotLogName, decodeAndEncodeURLWithLogging, compareObjectTypes, normalizeOptions } from './utils.js';
7
+ import { request, hostnameMatches, yieldTo, snapshotLogName, decodeAndEncodeURLWithLogging, compareObjectTypes, normalizeOptions, base64encode, redactSecrets } from './utils.js';
7
8
  import { JobData } from './wait-for-job.js';
8
9
 
9
10
  // Throw a better error message for missing or invalid urls
@@ -426,6 +427,31 @@ async function runDoctorOnFailure(percy) {
426
427
  }
427
428
  }
428
429
 
430
+ // Uploads a single web snapshot's CLI log to the logs endpoint, keyed by the
431
+ // snapshot id, so it lands directly in the log bucket instead of travelling as a
432
+ // snapshot resource. Best-effort: a failure here must never fail the snapshot.
433
+ export async function uploadSnapshotLog(percy, buildId, snapshotId, meta = {}) {
434
+ try {
435
+ var _meta$snapshot;
436
+ if (!buildId || !snapshotId) return;
437
+
438
+ // Redact secrets before egress (CWE-532) — this per-snapshot log is a
439
+ // parallel egress path to sendBuildLogs and must scrub tokens/credentials.
440
+ let logs = redactSecrets(logger.snapshotLogs(meta.snapshot));
441
+ if (!logs.length) return;
442
+ let content = base64encode(Pako.gzip(JSON.stringify(logs)));
443
+ await percy.client.sendSnapshotLog(buildId, snapshotId, content, meta);
444
+ percy.log.debug(`Snapshot log sent for ${(_meta$snapshot = meta.snapshot) === null || _meta$snapshot === void 0 ? void 0 : _meta$snapshot.name}`, meta);
445
+ } catch (err) {
446
+ var _meta$snapshot2;
447
+ percy.log.debug(`Could not send the snapshot log for ${(_meta$snapshot2 = meta.snapshot) === null || _meta$snapshot2 === void 0 ? void 0 : _meta$snapshot2.name}`, meta);
448
+ percy.log.debug(err);
449
+ } finally {
450
+ // drop this snapshot's cached logs regardless of upload outcome
451
+ logger.evictSnapshot(meta.snapshot);
452
+ }
453
+ }
454
+
429
455
  // Creates a snapshots queue that manages a Percy build and uploads snapshots.
430
456
  export function createSnapshotsQueue(percy) {
431
457
  let {
@@ -569,6 +595,13 @@ export function createSnapshotsQueue(percy) {
569
595
  let response = yield percy.client[send](build.id, snapshot);
570
596
  if (percy.deferUploads) percy.log.info(`Snapshot uploaded: ${name}`, meta);
571
597
 
598
+ // upload the per-snapshot CLI log directly to the logs endpoint (web
599
+ // snapshots only; automate/app comparisons carry no snapshot log). For
600
+ // sendSnapshot, response.data.id is the snapshot id. Best-effort.
601
+ if (send === 'sendSnapshot') {
602
+ yield uploadSnapshotLog(percy, build.id, response.data.id, meta);
603
+ }
604
+
572
605
  // Pushing to syncQueue, that will check for
573
606
  // snapshot processing status, and will resolve once done
574
607
  if (snapshot.sync) {
package/dist/utils.js CHANGED
@@ -470,6 +470,103 @@ export function createRootResource(url, content, attrs = {}) {
470
470
  });
471
471
  }
472
472
 
473
+ // Escapes HTML-special characters for TEXT context. Snapshot names reach the
474
+ // wrapper's <title>, and a name containing `</title><script>` would otherwise
475
+ // be injected into the DOM Percy's renderer loads.
476
+ function escapeHtml(value) {
477
+ return String(value).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
478
+ }
479
+
480
+ // Escapes for ATTRIBUTE context, and deliberately escapes less than escapeHtml.
481
+ //
482
+ // The `src` value is compared byte-for-byte against the registered resource URL
483
+ // by percy-api's extractor, so any character we rewrite here that the URL itself
484
+ // still carries breaks the match. The value sits inside double quotes, so only
485
+ // `&` and `"` can actually break out of the attribute -- `<`, `>` and `'` are
486
+ // all inert here and must be left alone.
487
+ //
488
+ // `'` is the one that bit us: encodeURIComponent does NOT encode it, so a
489
+ // document named "Jack's Resume" produced src="...Jack&#39;s..." against a
490
+ // resource URL of "...Jack's...". The extractor found no match, percy-api
491
+ // rescued, and every page silently fell back to being rendered.
492
+ //
493
+ // Callers must pass an already percent-encoded URL (see
494
+ // createImageSnapshotResources), which leaves `&` and `"` unreachable in
495
+ // practice -- this escape is the backstop that keeps the DOM well-formed if one
496
+ // ever slips through, not a substitute for encoding.
497
+ function escapeAttr(value) {
498
+ return String(value).replace(/&/g, '&amp;').replace(/"/g, '&quot;');
499
+ }
500
+
501
+ // The canonical wrapper DOM for an image-backed snapshot: one image at its
502
+ // native size, with no margins, padding or font metrics that could shift
503
+ // between renders.
504
+ //
505
+ // THIS SHAPE IS A CONTRACT WITH percy-api. When a build's User-Agent contains
506
+ // `@percy/cli-upload` and the root resource URL starts `http://local/`,
507
+ // Comparison#upload_snapshot? routes the comparison down the extraction path,
508
+ // which recovers the image by matching this HTML against
509
+ //
510
+ // /<img\s+src="([^"]+)"\s+width="(\d+)px"\s+height="(\d+)px"/
511
+ //
512
+ // and skips the renderer entirely -- measured at ~1s per page versus ~9-19s
513
+ // rendered. A mismatch is not an error: extraction raises, percy-api rescues,
514
+ // and the snapshot silently falls back to being rendered. So the attribute
515
+ // order (src, width, height), the `px` suffixes and the integer dimensions all
516
+ // matter, and there must only ever be one definition of them.
517
+ export function buildImageSnapshotHtml({
518
+ name,
519
+ imageUrl,
520
+ width,
521
+ height
522
+ }) {
523
+ return `
524
+ <!doctype html>
525
+ <html lang="en">
526
+ <head>
527
+ <meta charset="utf-8">
528
+ <title>${escapeHtml(name)}</title>
529
+ <style>
530
+ *, *::before, *::after { margin: 0; padding: 0; font-size: 0; }
531
+ html, body { width: 100%; }
532
+ img { max-width: 100%; }
533
+ </style>
534
+ </head>
535
+ <body>
536
+ <img src="${escapeAttr(imageUrl)}" width="${width}px" height="${height}px"/>
537
+ </body>
538
+ </html>
539
+ `;
540
+ }
541
+
542
+ // Builds the root + image resource pair for an image-backed snapshot.
543
+ //
544
+ // `imageUrl` MUST already be percent-encoded (encodeURIComponent over the
545
+ // varying segment). It is neither re-encoded nor text-escaped here, because the
546
+ // resource is registered under this exact string and percy-api's extractor
547
+ // compares the two byte-for-byte:
548
+ // - re-encoding turns %20 into %2520, so the img src matches no registered
549
+ // resource and the snapshot renders blank;
550
+ // - text-escaping rewrites characters encodeURIComponent leaves literal (`'`
551
+ // most notably), so extraction misses and the page is silently re-rendered.
552
+ // Only escapeAttr's `&`/`"` backstop is applied. See buildImageSnapshotHtml.
553
+ export function createImageSnapshotResources({
554
+ name,
555
+ rootUrl,
556
+ imageUrl,
557
+ width,
558
+ height,
559
+ content,
560
+ mimetype
561
+ }) {
562
+ return [createRootResource(rootUrl, buildImageSnapshotHtml({
563
+ name,
564
+ imageUrl,
565
+ width,
566
+ height
567
+ })), createResource(imageUrl, content, mimetype)];
568
+ }
569
+
473
570
  // Creates a Percy CSS resource object.
474
571
  export function createPercyCSSResource(url, css) {
475
572
  let {
@@ -481,14 +578,6 @@ export function createPercyCSSResource(url, css) {
481
578
  });
482
579
  }
483
580
 
484
- // Creates a log resource object.
485
- export function createLogResource(logs) {
486
- let [url, content] = [`/percy.${Date.now()}.log`, JSON.stringify(logs)];
487
- return createResource(url, content, 'text/plain', {
488
- log: true
489
- });
490
- }
491
-
492
581
  // Returns true or false if the provided object is a generator or not
493
582
  export function isGenerator(subject) {
494
583
  return typeof (subject === null || subject === void 0 ? void 0 : subject.next) === 'function' && (typeof subject[Symbol.iterator] === 'function' || typeof subject[Symbol.asyncIterator] === 'function');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/core",
3
- "version": "1.32.9",
3
+ "version": "1.32.10-beta.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "publishConfig": {
11
11
  "access": "public",
12
- "tag": "latest"
12
+ "tag": "beta"
13
13
  },
14
14
  "engines": {
15
15
  "node": ">=14"
@@ -46,12 +46,12 @@
46
46
  "dependencies": {
47
47
  "@grpc/grpc-js": "^1.14.3",
48
48
  "@grpc/proto-loader": "^0.8.0",
49
- "@percy/client": "1.32.9",
50
- "@percy/config": "1.32.9",
51
- "@percy/dom": "1.32.9",
52
- "@percy/logger": "1.32.9",
53
- "@percy/monitoring": "1.32.9",
54
- "@percy/webdriver-utils": "1.32.9",
49
+ "@percy/client": "1.32.10-beta.1",
50
+ "@percy/config": "1.32.10-beta.1",
51
+ "@percy/dom": "1.32.10-beta.1",
52
+ "@percy/logger": "1.32.10-beta.1",
53
+ "@percy/monitoring": "1.32.10-beta.1",
54
+ "@percy/webdriver-utils": "1.32.10-beta.1",
55
55
  "adm-zip": "^0.6.0",
56
56
  "busboy": "^1.6.0",
57
57
  "content-disposition": "^0.5.4",
@@ -67,7 +67,8 @@
67
67
  "yaml": "^2.4.1"
68
68
  },
69
69
  "optionalDependencies": {
70
- "@percy/cli-doctor": "1.32.9"
70
+ "@percy/cli-doctor": "1.32.10-beta.1",
71
+ "@percy/cli-pdf": "1.32.10-beta.1"
71
72
  },
72
- "gitHead": "598d96e0ae2f4fde8cdcd9e1542eff3f8bd7e8ab"
73
+ "gitHead": "b90cd9fe29b14fdb7a1e383609654b227adacf17"
73
74
  }