Skip to content

Commit f3e70ac

Browse files
committed
Add sha256 fallback if web crypto api fails
1 parent dc80037 commit f3e70ac

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"eslint-plugin-unused-imports": "^2.0.0",
4444
"framer-motion": "^8.4.3",
4545
"idb": "^8.0.0",
46+
"js-sha256": "^0.11.1",
4647
"lodash": "^4.17.21",
4748
"monaco-editor": "^0.52.2",
4849
"monaco-yaml": "^5.4.0",

web/src/helpers/metadataProcessor.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { getAllCardsAndModels, getAllCardsLegacy, getDatabaseTablesAndModelsWith
1616
import { fetchDatabaseFields } from '../../../apps/src/metabase/helpers/metabaseAPI';
1717
import { getSelectedDbId } from '../../../apps/src/metabase/helpers/metabaseStateAPI';
1818
import { getModelsWithFields } from '../../../apps/src/metabase/helpers/metabaseModels';
19+
import { sha256 } from 'js-sha256';
1920

2021
export interface MetadataItem {
2122
metadata_type: string;
@@ -58,14 +59,26 @@ export async function processMetadata(metadataItems: MetadataItem[]): Promise<an
5859
/**
5960
* Calculates metadata hash for caching purposes (simplified & faster)
6061
*/
62+
63+
function calculateMetadataHashFallback(content: string) {
64+
return sha256(content); // `sha256` is globally available
65+
}
66+
6167
async function calculateMetadataHash(metadataType: string, metadataValue: any, version: string): Promise<string> {
6268
// Simplified hash calculation - just hash the stringified data
6369
const content = JSON.stringify({ metadataType, version, metadataValue });
64-
const data = new TextEncoder().encode(content);
65-
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
70+
try {
71+
const data = new TextEncoder().encode(content);
72+
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
6673

67-
return Array.from(new Uint8Array(hashBuffer))
68-
.map(b => b.toString(16).padStart(2, '0')).join('');
74+
return Array.from(new Uint8Array(hashBuffer))
75+
.map(b => b.toString(16).padStart(2, '0')).join('');
76+
} catch (error) {
77+
console.warn('Failed to calculate metadata hash using crypto API, falling back:', error);
78+
// Fallback to a simpler hash calculation
79+
return calculateMetadataHashFallback(content);
80+
}
81+
6982
}
7083

7184
// Global map to track ongoing uploads by hash

0 commit comments

Comments
 (0)