@percy/core 1.31.6-beta.4 → 1.31.6-beta.6

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/network.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { request as makeRequest } from '@percy/client/utils';
2
2
  import logger from '@percy/logger';
3
3
  import mime from 'mime-types';
4
- import { DefaultMap, createResource, hostnameMatches, normalizeURL, waitFor, decodeAndEncodeURLWithLogging } from './utils.js';
4
+ import { DefaultMap, createResource, hostnameMatches, normalizeURL, waitFor, decodeAndEncodeURLWithLogging, handleGoogleFontMimeType } from './utils.js';
5
5
  const MAX_RESOURCE_SIZE = 25 * 1024 ** 2 * 0.63; // 25MB, 0.63 factor for accounting for base64 encoding
6
6
  const ALLOWED_STATUSES = [200, 201, 301, 302, 304, 307, 308];
7
7
  const ALLOWED_RESOURCES = ['Document', 'Stylesheet', 'Image', 'Media', 'Font', 'Other'];
@@ -516,6 +516,7 @@ async function saveResponseResource(network, request, session) {
516
516
  let resource = network.intercept.getResource(url);
517
517
  if (!resource || !resource.root && !resource.provided && disableCache) {
518
518
  try {
519
+ var _mimeType;
519
520
  // Don't rename the below log line as it is used in getting network logs in api
520
521
  log.debug(`Processing resource: ${url}`, meta);
521
522
  let shouldCapture = response && hostnameMatches(allowedHostnames, url);
@@ -546,11 +547,14 @@ async function saveResponseResource(network, request, session) {
546
547
  // ensure the mimetype is correct for text/plain responses
547
548
  response.mimeType === 'text/plain' && detectedMime || response.mimeType;
548
549
 
550
+ // Handle Google Fonts MIME type detection and override
551
+ mimeType = handleGoogleFontMimeType(urlObj, mimeType, body, log, meta);
552
+
549
553
  // if we detect a font mime, we dont want to override it as different browsers may behave
550
554
  // differently for incorrect mimetype in font response, but we want to treat it as a
551
555
  // font anyway as font responses from the browser may not be properly encoded,
552
556
  // so request them directly.
553
- if (mimeType !== null && mimeType !== void 0 && mimeType.includes('font') || detectedMime && detectedMime.includes('font')) {
557
+ if ((_mimeType = mimeType) !== null && _mimeType !== void 0 && _mimeType.includes('font') || detectedMime && detectedMime.includes('font')) {
554
558
  log.debug('- Requesting asset directly', meta);
555
559
  body = await makeDirectRequest(network, request, session);
556
560
  log.debug('- Got direct response', meta);
package/dist/utils.js CHANGED
@@ -26,6 +26,66 @@ export function normalizeURL(url) {
26
26
  return `${protocol}//${host}${pathname}${search}`;
27
27
  }
28
28
 
29
+ // Detects font MIME type from file content by checking magic bytes
30
+ // Returns the detected MIME type or null if not a recognized font format
31
+ export function detectFontMimeType(buffer) {
32
+ try {
33
+ // Ensure buffer has at least 4 bytes
34
+ if (!buffer || buffer.length < 4) {
35
+ return null;
36
+ }
37
+
38
+ // Read the first 4 bytes as the file header
39
+ const header = buffer.slice(0, 4).toString('binary');
40
+
41
+ // Check for WOFF signature: 'wOFF'
42
+ if (header === 'wOFF') {
43
+ return 'font/woff';
44
+ }
45
+
46
+ // Check for WOFF2 signature: 'wOF2'
47
+ if (header === 'wOF2') {
48
+ return 'font/woff2';
49
+ }
50
+
51
+ // Check for TrueType/OpenType signature: 0x00 0x01 0x00 0x00
52
+ if (header.charCodeAt(0) === 0x00 && header.charCodeAt(1) === 0x01 && header.charCodeAt(2) === 0x00 && header.charCodeAt(3) === 0x00) {
53
+ return 'font/ttf';
54
+ }
55
+
56
+ // Check for OpenType signature: 'OTTO'
57
+ if (header === 'OTTO') {
58
+ return 'font/otf';
59
+ }
60
+
61
+ // Not a recognized font format
62
+ return null;
63
+ } catch (error) {
64
+ // Return null on any error during detection
65
+ return null;
66
+ }
67
+ }
68
+
69
+ // Handles Google Fonts MIME type detection and override
70
+ // Google Fonts sometimes returns font files with text/html mime type
71
+ // This function detects the actual font format from the file content
72
+ export function handleGoogleFontMimeType(urlObj, mimeType, body, log, meta) {
73
+ // Check if this is a Google Fonts request with incorrect mime type
74
+ let isGoogleFont = urlObj.hostname === 'fonts.gstatic.com';
75
+ if (isGoogleFont && mimeType === 'text/html') {
76
+ const detectedFontMime = detectFontMimeType(body);
77
+ if (detectedFontMime) {
78
+ mimeType = detectedFontMime;
79
+ log.debug(`- Detected Google Font as ${detectedFontMime} from content, overriding mime type`, meta);
80
+ } else {
81
+ // Fallback to generic font mime type if we can't detect the specific format
82
+ mimeType = 'application/font-woff2';
83
+ log.debug('- Google Font detected but format unclear, treating as font', meta);
84
+ }
85
+ }
86
+ return mimeType;
87
+ }
88
+
29
89
  /* istanbul ignore next: tested, but coverage is stripped */
30
90
  // Returns the body for automateScreenshot in structure
31
91
  export function percyAutomateRequestHandler(req, percy) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@percy/core",
3
- "version": "1.31.6-beta.4",
3
+ "version": "1.31.6-beta.6",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -43,12 +43,12 @@
43
43
  "test:types": "tsd"
44
44
  },
45
45
  "dependencies": {
46
- "@percy/client": "1.31.6-beta.4",
47
- "@percy/config": "1.31.6-beta.4",
48
- "@percy/dom": "1.31.6-beta.4",
49
- "@percy/logger": "1.31.6-beta.4",
50
- "@percy/monitoring": "1.31.6-beta.4",
51
- "@percy/webdriver-utils": "1.31.6-beta.4",
46
+ "@percy/client": "1.31.6-beta.6",
47
+ "@percy/config": "1.31.6-beta.6",
48
+ "@percy/dom": "1.31.6-beta.6",
49
+ "@percy/logger": "1.31.6-beta.6",
50
+ "@percy/monitoring": "1.31.6-beta.6",
51
+ "@percy/webdriver-utils": "1.31.6-beta.6",
52
52
  "content-disposition": "^0.5.4",
53
53
  "cross-spawn": "^7.0.3",
54
54
  "extract-zip": "^2.0.1",
@@ -61,5 +61,5 @@
61
61
  "ws": "^8.17.1",
62
62
  "yaml": "^2.4.1"
63
63
  },
64
- "gitHead": "1b6ad1626f99fc294d4777140e34776d83d1e35e"
64
+ "gitHead": "d324722ebebe9f69066bfc5d726fa69dfcbb2cf8"
65
65
  }