From cf9d89179dc2373f32c5e4256f6f1fc008231e4b Mon Sep 17 00:00:00 2001 From: Spoonsk Date: Fri, 1 Nov 2024 14:56:58 -0700 Subject: [PATCH] fix: adding warning when pushing monitors from yaml files (#977) * package.json - added dependency 'utf-8-validate' * monitor.ts - Added validation and warnings - Validation/warning prior to UTF-8 decoding of files - warning when a 'browser' type monitor is ignored - removing unused variable (causing linter error) * Update src/push/monitor.ts changing warning text Co-authored-by: Vignesh Shanmugam * src/push/monitor.ts - using node standard libarary to test utf-8 encoding - updated types@node to be more in line with current version * Update src/push/monitor.ts - updating warning message. Co-authored-by: Vignesh Shanmugam * fix node type and message --------- Co-authored-by: Vignesh Shanmugam --- package-lock.json | 19 ++++++++++++++----- package.json | 2 +- src/push/monitor.ts | 12 +++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2da7f08c..ff2d7c3b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,7 +43,7 @@ "@types/babel__code-frame": "^7.0.3", "@types/jest": "^28.1.8", "@types/micromatch": "^4.0.9", - "@types/node": "^16.11.59", + "@types/node": "^18.19.63", "@types/semver": "^7", "@types/stack-utils": "^2.0.1", "@typescript-eslint/eslint-plugin": "^5.38.0", @@ -4320,10 +4320,13 @@ "dev": true }, "node_modules/@types/node": { - "version": "16.11.59", - "resolved": "https://registry.npmjs.org/@types/node/-/node-16.11.59.tgz", - "integrity": "sha512-6u+36Dj3aDzhfBVUf/mfmc92OEdzQ2kx2jcXGdigfl70E/neV21ZHE6UCz4MDzTRcVqGAM27fk+DLXvyDsn3Jw==", - "dev": true + "version": "18.19.63", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.19.63.tgz", + "integrity": "sha512-hcUB7THvrGmaEcPcvUZCZtQ2Z3C+UR/aOcraBLCvTsFMh916Gc1kCCYcfcMuB76HM2pSerxl1PoP3KnmHzd9Lw==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/@types/normalize-package-data": { "version": "2.4.1", @@ -16027,6 +16030,12 @@ "node": ">=14.0" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/unique-string": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/unique-string/-/unique-string-3.0.0.tgz", diff --git a/package.json b/package.json index 48e295ac..d65a0c03 100644 --- a/package.json +++ b/package.json @@ -81,7 +81,7 @@ "@types/babel__code-frame": "^7.0.3", "@types/jest": "^28.1.8", "@types/micromatch": "^4.0.9", - "@types/node": "^16.11.59", + "@types/node": "^18.19.63", "@types/semver": "^7", "@types/stack-utils": "^2.0.1", "@typescript-eslint/eslint-plugin": "^5.38.0", diff --git a/src/push/monitor.ts b/src/push/monitor.ts index 217a87d6..85f39d83 100644 --- a/src/push/monitor.ts +++ b/src/push/monitor.ts @@ -28,6 +28,7 @@ import { extname, join } from 'path'; import { LineCounter, parseDocument, Document, YAMLSeq, YAMLMap } from 'yaml'; import { bold, red } from 'kleur/colors'; import { Bundler } from './bundler'; +import NodeBuffer from 'node:buffer'; import { SYNTHETICS_PATH, totalist, indent, warn } from '../helpers'; import { LocationsMap } from '../locations/public-locations'; import { @@ -184,7 +185,13 @@ export async function createLightweightMonitors( let warnOnce = false; const monitors: Monitor[] = []; for (const file of lwFiles.values()) { - const content = await readFile(file, 'utf-8'); + // First check encoding and warn if any files are not the correct encoding. + const bufferContent = await readFile(file); + const isUtf8 = NodeBuffer.isUtf8(bufferContent); + if (!isUtf8) { + warn(`${file} is not UTF-8 encoded. Monitors might be skipped.`); + } + const content = bufferContent.toString('utf-8'); const lineCounter = new LineCounter(); const parsedDoc = parseDocument(content, { lineCounter, @@ -218,6 +225,9 @@ export async function createLightweightMonitors( const monitor = mergedConfig[i]; // Skip browser monitors from the YML files if (monitor['type'] === 'browser') { + warn( + `Browser monitors from ${file} are skipped.` + ); continue; } const { line, col } = lineCounter.linePos(offsets[i]);