From 5f3e5c279705ab4b6cc05d081435d5c33cae54e2 Mon Sep 17 00:00:00 2001 From: Sergi Romeu Date: Fri, 8 Aug 2025 08:44:39 +0200 Subject: [PATCH] [Synthtrace] Remove `fnv-plus` dependency (#230722) (cherry picked from commit 2a59ab5073882c91582e914fe37d06e22e96dc9e) # Conflicts: # package.json # yarn.lock --- package.json | 2 - renovate.json | 19 ----- .../src/lib/utils/hash.test.ts | 25 +++++++ .../src/lib/utils/hash.ts | 70 ++++++++++++++++++- yarn.lock | 10 --- 5 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.test.ts diff --git a/package.json b/package.json index 31226ac45e557..66dded7748bee 100644 --- a/package.json +++ b/package.json @@ -1135,7 +1135,6 @@ "fastest-levenshtein": "^1.0.16", "fflate": "^0.6.9", "file-saver": "^1.3.8", - "fnv-plus": "^1.3.1", "formik": "^2.4.6", "fp-ts": "^2.3.1", "fuse.js": "^7.0.0", @@ -1598,7 +1597,6 @@ "@types/fetch-mock": "^7.3.1", "@types/file-saver": "^2.0.0", "@types/flot": "^0.0.31", - "@types/fnv-plus": "^1.3.2", "@types/geojson": "^7946.0.10", "@types/getos": "^3.0.4", "@types/gulp": "^4.0.6", diff --git a/renovate.json b/renovate.json index 6052a6dfe27b0..bbbfbb00938ec 100644 --- a/renovate.json +++ b/renovate.json @@ -3974,25 +3974,6 @@ "minimumReleaseAge": "7 days", "enabled": true }, - { - "groupName": "fnv-plus", - "matchDepNames": [ - "fnv-plus", - "@types/fnv-plus" - ], - "reviewers": [ - "team:obs-ux-infra_services-team" - ], - "matchBaseBranches": [ - "main" - ], - "labels": [ - "release_note:skip", - "backport:all-open" - ], - "minimumReleaseAge": "7 days", - "enabled": true - }, { "groupName": "hdr-histogram-js", "matchDepNames": [ diff --git a/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.test.ts b/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.test.ts new file mode 100644 index 0000000000000..ea9394471e3e6 --- /dev/null +++ b/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.test.ts @@ -0,0 +1,25 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the "Elastic License + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side + * Public License v 1"; you may not use this file except in compliance with, at + * your election, the "Elastic License 2.0", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +import { fnv1a32 } from './hash'; + +describe('fnv-plus', function () { + const hash1 = 'hello world'; + const hash2 = 'the quick brown fox jumps over the lazy dog'; + + describe('(32)', function () { + it('should generate a 32-bit hash if specified', function () { + const h1 = fnv1a32(hash1); + const h2 = fnv1a32(hash2); + + expect(h1).toEqual(3582672807); + expect(h2).toEqual(4016652272); + }); + }); +}); diff --git a/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.ts b/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.ts index bdb96e2ee1ac5..e957af4d47ee1 100644 --- a/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.ts +++ b/src/platform/packages/shared/kbn-apm-synthtrace-client/src/lib/utils/hash.ts @@ -7,7 +7,6 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { fast1a32 } from 'fnv-plus'; import { Fields } from '../entity'; export function hashKeysOf(source: T, keys: Array) { @@ -19,6 +18,73 @@ export function hashKeysOf(source: T, keys: Array) { return hashed; } +// this hashing function is the same as fnv-plus +export function fnv1a32(str: string): number { + /* eslint-disable no-bitwise */ + let i; + const l = str.length - 3; + let t0 = 0; + let v0 = 0x9dc5; + let t1 = 0; + let v1 = 0x811c; + + for (i = 0; i < l; ) { + v0 ^= str.charCodeAt(i++); + t0 = v0 * 403; + t1 = v1 * 403; + + t1 += v0 << 8; + + v1 = (t1 + (t0 >>> 16)) & 65535; + + v0 = t0 & 65535; + + v0 ^= str.charCodeAt(i++); + t0 = v0 * 403; + t1 = v1 * 403; + + t1 += v0 << 8; + + v1 = (t1 + (t0 >>> 16)) & 65535; + + v0 = t0 & 65535; + + v0 ^= str.charCodeAt(i++); + t0 = v0 * 403; + t1 = v1 * 403; + + t1 += v0 << 8; + + v1 = (t1 + (t0 >>> 16)) & 65535; + + v0 = t0 & 65535; + + v0 ^= str.charCodeAt(i++); + t0 = v0 * 403; + t1 = v1 * 403; + + t1 += v0 << 8; + + v1 = (t1 + (t0 >>> 16)) & 65535; + + v0 = t0 & 65535; + } + + while (i < l + 3) { + v0 ^= str.charCodeAt(i++); + t0 = v0 * 403; + t1 = v1 * 403; + + t1 += v0 << 8; + + v1 = (t1 + (t0 >>> 16)) & 65535; + + v0 = t0 & 65535; + } + + return ((v1 << 16) >>> 0) + v0; +} + export function appendHash(hash: string, value: string) { - return fast1a32(hash + ',' + value).toString(); + return fnv1a32(hash + ',' + value).toString(); } diff --git a/yarn.lock b/yarn.lock index 01ba30ef817b1..b69c2f6e9a4ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12320,11 +12320,6 @@ dependencies: "@types/jquery" "*" -"@types/fnv-plus@^1.3.2": - version "1.3.2" - resolved "https://registry.yarnpkg.com/@types/fnv-plus/-/fnv-plus-1.3.2.tgz#bd591c1031ae48a18c99eaa60f659288aea545c0" - integrity sha512-Bgr5yn2dph2q8HZKDS002Pob6vaRTRfhqN9E+TOhjKsJvnfZXULPR3ihH8dL5ZjgxbNhqhTn9hijpbAMPtKZzw== - "@types/geojson@*", "@types/geojson@^7946.0.10", "@types/geojson@^7946.0.7": version "7946.0.10" resolved "https://registry.yarnpkg.com/@types/geojson/-/geojson-7946.0.10.tgz#6dfbf5ea17142f7f9a043809f1cd4c448cb68249" @@ -20330,11 +20325,6 @@ fn.name@1.x.x: resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc" integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw== -fnv-plus@^1.3.1: - version "1.3.1" - resolved "https://registry.yarnpkg.com/fnv-plus/-/fnv-plus-1.3.1.tgz#c34cb4572565434acb08ba257e4044ce2b006d67" - integrity sha512-Gz1EvfOneuFfk4yG458dJ3TLJ7gV19q3OM/vVvvHf7eT02Hm1DleB4edsia6ahbKgAYxO9gvyQ1ioWZR+a00Yw== - focus-lock@^0.11.6: version "0.11.6" resolved "https://registry.yarnpkg.com/focus-lock/-/focus-lock-0.11.6.tgz#e8821e21d218f03e100f7dc27b733f9c4f61e683"