From 846afe2e6868af8f0e33f8c9d63b0eb0d7deb044 Mon Sep 17 00:00:00 2001 From: Jack Westbrook Date: Tue, 10 Dec 2024 07:38:09 +0100 Subject: [PATCH] chore(cloudwatch): vendor jsurl stringify function --- package.json | 1 - .../plugins/datasource/cloudwatch/aws_url.ts | 61 ++++++++++++++++++- yarn.lock | 8 --- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 13333aa40b9..404b9a14877 100644 --- a/package.json +++ b/package.json @@ -325,7 +325,6 @@ "js-yaml": "^4.1.0", "json-markup": "^1.1.0", "json-source-map": "0.6.1", - "jsurl2": "^2.2.0", "kbar": "0.1.0-beta.45", "leven": "^4.0.0", "lodash": "4.17.21", diff --git a/public/app/plugins/datasource/cloudwatch/aws_url.ts b/public/app/plugins/datasource/cloudwatch/aws_url.ts index 85ad1bfd120..4ed84166bd9 100644 --- a/public/app/plugins/datasource/cloudwatch/aws_url.ts +++ b/public/app/plugins/datasource/cloudwatch/aws_url.ts @@ -1,5 +1,3 @@ -import { stringify } from 'jsurl2'; - export interface AwsUrl { end: string; start: string; @@ -31,3 +29,62 @@ export function encodeUrl(obj: AwsUrl, region: string): string { region )}/cloudwatch/home?region=${region}#logs-insights:queryDetail=${stringify(obj)}`; } + +// Lifted from https://github.com/Sage/jsurl +function stringify(v: T): string { + function encode(s: string): string { + return !/[^\w-.]/.test(s) + ? s + : s.replace(/[^\w-.]/g, (ch: string): string => { + if (ch === '$') { + return '!'; + } + const charCode = ch.charCodeAt(0); + // Thanks to Douglas Crockford for the negative slice trick + return charCode < 0x100 + ? '*' + ('00' + charCode.toString(16)).slice(-2) + : '**' + ('0000' + charCode.toString(16)).slice(-4); + }); + } + + let tmpAry; + + switch (typeof v) { + case 'number': + return isFinite(v) ? '~' + v : '~null'; + case 'boolean': + return '~' + v; + case 'string': + return "~'" + encode(v); + case 'object': + if (!v) { + return '~null'; + } + + tmpAry = []; + + if (Array.isArray(v)) { + for (let i = 0; i < v.length; i++) { + tmpAry[i] = stringify(v[i]) || '~null'; + } + + return '~(' + (tmpAry.join('') || '~') + ')'; + } else { + for (const key in v) { + if (v.hasOwnProperty(key)) { + const val = stringify(v[key]); + + // skip undefined and functions + if (val) { + tmpAry.push(encode(key) + val); + } + } + } + + return '~(' + tmpAry.join('~') + ')'; + } + default: + // function, undefined + return ''; + } +} diff --git a/yarn.lock b/yarn.lock index ca77a754c07..ee34738e30e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -18362,7 +18362,6 @@ __metadata: jsdom-testing-mocks: "npm:^1.13.1" json-markup: "npm:^1.1.0" json-source-map: "npm:0.6.1" - jsurl2: "npm:^2.2.0" kbar: "npm:0.1.0-beta.45" lerna: "npm:8.1.8" leven: "npm:^4.0.0" @@ -21136,13 +21135,6 @@ __metadata: languageName: node linkType: hard -"jsurl2@npm:^2.2.0": - version: 2.2.0 - resolution: "jsurl2@npm:2.2.0" - checksum: 10/eb54376c7af6881b2110eb5b71314f17f7165fb450d82b67cc5f28f66a8999439b140b7533354ed9d522236e08d2bd94730186464c1fc9b502e8977027fbc43c - languageName: node - linkType: hard - "jsx-ast-utils@npm:^2.4.1 || ^3.0.0, jsx-ast-utils@npm:^3.3.5": version: 3.3.5 resolution: "jsx-ast-utils@npm:3.3.5"