From c79ba8b72779e88630fb69295aede11c35daeace Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Fri, 31 May 2024 22:56:55 -0400 Subject: [PATCH] fix browser DNS resolution; closes #251 --- hyperglass/ui/components/looking-glass-form.tsx | 4 +++- hyperglass/ui/util/common.test.ts | 13 +++++-------- hyperglass/ui/util/common.ts | 5 ++++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/hyperglass/ui/components/looking-glass-form.tsx b/hyperglass/ui/components/looking-glass-form.tsx index fc879be..664f8a8 100644 --- a/hyperglass/ui/components/looking-glass-form.tsx +++ b/hyperglass/ui/components/looking-glass-form.tsx @@ -72,7 +72,9 @@ export const LookingGlassForm = (): JSX.Element => { const isFqdnQuery = useCallback( (target: string | string[], fieldType: Directive['fieldType'] | null): boolean => - typeof target === 'string' && fieldType === 'text' && isFQDN(target), + (typeof target === 'string' || Array.isArray(target)) && + fieldType === 'text' && + isFQDN(target), [], ); diff --git a/hyperglass/ui/util/common.test.ts b/hyperglass/ui/util/common.test.ts index fc041cd..2ca4e12 100644 --- a/hyperglass/ui/util/common.test.ts +++ b/hyperglass/ui/util/common.test.ts @@ -1,5 +1,5 @@ -import { expect, describe, it, test } from 'vitest'; -import { all, chunkArray, entries, dedupObjectArray, andJoin, isFQDN } from './common'; +import { describe, expect, it, test } from 'vitest'; +import { all, andJoin, chunkArray, dedupObjectArray, entries, isFQDN } from './common'; test('all - all items are truthy', () => { // biome-ignore lint/suspicious/noSelfCompare: because this is a test, duh @@ -79,12 +79,6 @@ describe('andJoin - join array of strings to sentence structure', () => { }); describe('isFQDN - determine if a string is an FQDN pattern', () => { - it('is null and should be false', () => { - expect(isFQDN(null)).toBe(false); - }); - it('is undefined and should be false', () => { - expect(isFQDN(undefined)).toBe(false); - }); it("isn't an FQDN and should be false", () => { expect(isFQDN('example')).toBe(false); }); @@ -100,4 +94,7 @@ describe('isFQDN - determine if a string is an FQDN pattern', () => { it('is a longer FQDN and should be true', () => { expect(isFQDN('one.two.three.four.five.example.com')).toBe(true); }); + it('is an array of FQDNs and should be true', () => { + expect(isFQDN(['www.example.com'])).toBe(true); + }); }); diff --git a/hyperglass/ui/util/common.ts b/hyperglass/ui/util/common.ts index fc8d45a..0bbec3b 100644 --- a/hyperglass/ui/util/common.ts +++ b/hyperglass/ui/util/common.ts @@ -130,7 +130,7 @@ export function andJoin(values: string[], options?: AndJoinOptions): string { * * @param value Input value. */ -export function isFQDN(value: unknown): value is string { +export function isFQDN(value: string | string[]): value is string { /** * Don't set the global flag on this. * @see https://stackoverflow.com/questions/24084926/javascript-regexp-cant-use-twice @@ -142,5 +142,8 @@ export function isFQDN(value: unknown): value is string { const pattern = new RegExp( /^(?!:\/\/)([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-][a-zA-Z0-9-]+\.[a-zA-Z-]{2,6}?$/im, ); + if (Array.isArray(value)) { + return isFQDN(value[0]); + } return typeof value === 'string' && pattern.test(value); }