fix: add uuid fallback for non-secure contexts (HTTP)

This commit is contained in:
Sam Chau
2026-01-19 22:28:49 +10:30
parent e1c0a66603
commit 8026bc93c9
4 changed files with 23 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
import { writable } from 'svelte/store';
import { uuid } from '$shared/uuid';
export type AlertType = 'success' | 'error' | 'warning' | 'info';
@@ -15,7 +16,7 @@ function createAlertStore() {
return {
subscribe,
add: (type: AlertType, message: string, duration = 5000) => {
const id = crypto.randomUUID();
const id = uuid();
const alert: Alert = { id, type, message, duration };
update((alerts) => [...alerts, alert]);

View File

@@ -3,6 +3,8 @@
* Defines all available filter fields for upgrade filtering
*/
import { uuid } from './uuid.ts';
export interface FilterOperator {
id: string;
label: string;
@@ -294,7 +296,7 @@ export function createEmptyGroup(): FilterGroup {
*/
export function createEmptyFilterConfig(name: string = 'New Filter'): FilterConfig {
return {
id: crypto.randomUUID(),
id: uuid(),
name,
enabled: true,
group: createEmptyGroup(),

16
src/lib/shared/uuid.ts Normal file
View File

@@ -0,0 +1,16 @@
/**
* Generate a UUID v4, with fallback for non-secure contexts (HTTP)
* crypto.randomUUID() requires HTTPS or localhost
*/
export function uuid(): string {
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
}
// Fallback for non-secure contexts
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

View File

@@ -11,6 +11,7 @@
Trash2
} from 'lucide-svelte';
import { createEmptyFilterConfig, type FilterConfig } from '$lib/shared/filters';
import { uuid } from '$shared/uuid';
import { selectors } from '$lib/shared/selectors';
import { createSearchStore } from '$lib/client/stores/search';
import FilterGroupComponent from './FilterGroup.svelte';
@@ -123,7 +124,7 @@
if (filter) {
const duplicate: FilterConfig = {
...structuredClone(filter),
id: crypto.randomUUID(),
id: uuid(),
name: `${filter.name} (Copy)`
};
filters = [...filters, duplicate];