@percy/client 1.16.0 → 1.18.0

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/client.js CHANGED
@@ -2,7 +2,7 @@ import fs from 'fs';
2
2
  import PercyEnv from '@percy/env';
3
3
  import { git } from '@percy/env/utils';
4
4
  import logger from '@percy/logger';
5
- import { pool, request, sha256hash, base64encode, getPackageJSON } from './utils.js';
5
+ import { pool, request, sha256hash, base64encode, getPackageJSON, waitForTimeout } from './utils.js';
6
6
 
7
7
  // Default client API URL can be set with an env var for API development
8
8
  const {
@@ -112,13 +112,15 @@ export class PercyClient {
112
112
  // created at a time per instance so snapshots and build finalization can be
113
113
  // done more seemlessly without manually tracking build ids
114
114
  async createBuild({
115
- resources = []
115
+ resources = [],
116
+ projectType
116
117
  } = {}) {
117
118
  this.log.debug('Creating a new build...');
118
119
  return this.post('builds', {
119
120
  data: {
120
121
  type: 'builds',
121
122
  attributes: {
123
+ type: projectType,
122
124
  branch: this.env.git.branch,
123
125
  'target-branch': this.env.target.branch,
124
126
  'target-commit-sha': this.env.target.commit,
@@ -404,11 +406,15 @@ export class PercyClient {
404
406
  index = 0,
405
407
  total = 1,
406
408
  filepath,
407
- content
409
+ content,
410
+ sha
408
411
  } = {}) {
409
412
  validateId('comparison', comparisonId);
410
413
  this.log.debug(`Uploading comparison tile: ${index + 1}/${total} (${comparisonId})...`);
411
414
  if (filepath) content = await fs.promises.readFile(filepath);
415
+ if (sha) {
416
+ return await this.verify(comparisonId, sha);
417
+ }
412
418
  return this.post(`comparisons/${comparisonId}/tiles`, {
413
419
  data: {
414
420
  type: 'tiles',
@@ -419,6 +425,41 @@ export class PercyClient {
419
425
  }
420
426
  });
421
427
  }
428
+
429
+ // Convenience method for verifying if tile is present
430
+ async verify(comparisonId, sha) {
431
+ let retries = 10;
432
+ let success = null;
433
+ do {
434
+ await waitForTimeout(500);
435
+ success = await this.verifyComparisonTile(comparisonId, sha);
436
+ retries -= 1;
437
+ } while (retries > 0 && !success);
438
+ if (!success) {
439
+ this.log.error('Uploading comparison tile failed');
440
+ return false;
441
+ }
442
+ return true;
443
+ }
444
+ async verifyComparisonTile(comparisonId, sha) {
445
+ validateId('comparison', comparisonId);
446
+ this.log.debug(`Verifying comparison tile with sha: ${sha}`);
447
+ try {
448
+ return await this.get(`comparisons/${comparisonId}/tiles/verify`, {
449
+ data: {
450
+ type: 'tiles',
451
+ attributes: {
452
+ sha: sha
453
+ }
454
+ }
455
+ });
456
+ } catch (error) {
457
+ if (error.message.includes('409')) {
458
+ return false;
459
+ }
460
+ throw error;
461
+ }
462
+ }
422
463
  async uploadComparisonTiles(comparisonId, tiles) {
423
464
  validateId('comparison', comparisonId);
424
465
  this.log.debug(`Uploading comparison tiles for ${comparisonId}...`);
package/dist/utils.js CHANGED
@@ -13,6 +13,9 @@ export function sha256hash(content) {
13
13
  export function base64encode(content) {
14
14
  return Buffer.from(content).toString('base64');
15
15
  }
16
+ export function waitForTimeout() {
17
+ return new Promise(resolve => setTimeout(resolve, ...arguments));
18
+ }
16
19
 
17
20
  // Returns the package.json content at the package path.
18
21
  export function getPackageJSON(rel) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/client",
3
- "version": "1.16.0",
3
+ "version": "1.18.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -31,8 +31,8 @@
31
31
  "test:coverage": "yarn test --coverage"
32
32
  },
33
33
  "dependencies": {
34
- "@percy/env": "1.16.0",
35
- "@percy/logger": "1.16.0"
34
+ "@percy/env": "1.18.0",
35
+ "@percy/logger": "1.18.0"
36
36
  },
37
- "gitHead": "147d3a6f249771252ee6cf03c9af44e4e21e1c55"
37
+ "gitHead": "8aeb759653765fea11e8ebeb96c53a4c4ea616b2"
38
38
  }