|
| 1 | +import { PlotlySchema } from './PlotlySchema'; |
| 2 | + |
| 3 | +function addBase64Padding(s: string): string { |
| 4 | + const paddingNeeded = (4 - (s.length % 4)) % 4; |
| 5 | + return s + '='.repeat(paddingNeeded); |
| 6 | +} |
| 7 | +// Function to check if a string is base64-encoded |
| 8 | +function isBase64(s: string): boolean { |
| 9 | + if (typeof s !== 'string') { |
| 10 | + return false; |
| 11 | + } |
| 12 | + |
| 13 | + // Base64 strings must have a length that is a multiple of 4 |
| 14 | + if (s.length % 4 !== 0) { |
| 15 | + s = addBase64Padding(s); |
| 16 | + } |
| 17 | + |
| 18 | + // Use a regular expression to check if the string contains only valid base64 characters |
| 19 | + const base64Regex = /^[A-Za-z0-9+/]+={0,2}$/; |
| 20 | + if (!base64Regex.test(s)) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + decodeBase64FromString(s); |
| 26 | + return true; |
| 27 | + } catch { |
| 28 | + return false; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function decodeBase64FromString(base64String: string): Uint8Array { |
| 33 | + if (typeof window !== 'undefined' && typeof atob === 'function') { |
| 34 | + // For browsers |
| 35 | + const binaryString = atob(base64String); |
| 36 | + const binaryLength = binaryString.length; |
| 37 | + const bytes = new Uint8Array(binaryLength); |
| 38 | + for (let i = 0; i < binaryLength; i++) { |
| 39 | + bytes[i] = binaryString.charCodeAt(i); |
| 40 | + } |
| 41 | + return bytes; |
| 42 | + } |
| 43 | + throw new Error('Base64 decoding is not supported in this environment.'); |
| 44 | +} |
| 45 | + |
| 46 | +// Helper function to decode base64-encoded data based on dtype |
| 47 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 48 | +function decodeBase64(value: string, dtype: string): any { |
| 49 | + // Add padding if necessary |
| 50 | + value = addBase64Padding(value); |
| 51 | + |
| 52 | + try { |
| 53 | + const decodedBytes = decodeBase64FromString(value); |
| 54 | + switch (dtype) { |
| 55 | + case 'f8': |
| 56 | + return Array.from(new Float64Array(decodedBytes.buffer)); |
| 57 | + case 'i8': |
| 58 | + return Array.from(new BigInt64Array(decodedBytes.buffer)); |
| 59 | + case 'u8': |
| 60 | + return Array.from(new BigUint64Array(decodedBytes.buffer)); |
| 61 | + case 'i4': |
| 62 | + return Array.from(new Int32Array(decodedBytes.buffer)); |
| 63 | + case 'i2': |
| 64 | + return Array.from(new Int16Array(decodedBytes.buffer)); |
| 65 | + case 'i1': |
| 66 | + return Array.from(new Int8Array(decodedBytes.buffer)); |
| 67 | + default: |
| 68 | + try { |
| 69 | + return decodedBytes.toString(); |
| 70 | + } catch (error) { |
| 71 | + return decodedBytes; |
| 72 | + } |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + throw new Error(`Failed to decode base64 value: ${value}`); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 80 | +function decodeBdataInDict(d: any): void { |
| 81 | + for (const [key, value] of Object.entries(d)) { |
| 82 | + if (typeof value === 'object' && !Array.isArray(value) && value !== null) { |
| 83 | + decodeBdataInDict(value); // Recursively process nested objects |
| 84 | + } else if (key === 'bdata' && typeof value === 'string' && isBase64(value)) { |
| 85 | + const dtype = d.dtype || 'utf-8'; // Get dtype or default to 'utf-8' |
| 86 | + d[key] = decodeBase64(value, dtype); // Decode the base64-encoded value |
| 87 | + } else if (Array.isArray(value)) { |
| 88 | + for (let i = 0; i < value.length; i++) { |
| 89 | + if (typeof value[i] === 'object' && value[i] !== null) { |
| 90 | + decodeBdataInDict(value[i]); // Recursively process objects in arrays |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Function to process a PlotlySchema object |
| 98 | +export function decodeBase64Fields(plotlySchema: PlotlySchema): PlotlySchema { |
| 99 | + // Create a deep copy of the original data |
| 100 | + const originalData = JSON.parse(JSON.stringify(plotlySchema.data)); |
| 101 | + |
| 102 | + // Decode base64-encoded 'bdata' in the JSON data |
| 103 | + decodeBdataInDict(plotlySchema.data); |
| 104 | + |
| 105 | + // Check if the data has changed |
| 106 | + if (JSON.stringify(plotlySchema.data) !== JSON.stringify(originalData)) { |
| 107 | + let isNan = false; |
| 108 | + |
| 109 | + // Overwrite the 'y', 'x', or 'z' value with the decoded 'bdata' |
| 110 | + for (const item of plotlySchema.data || []) { |
| 111 | + ['y', 'x', 'z'].forEach(key => { |
| 112 | + if ( |
| 113 | + item[key as keyof typeof item] && |
| 114 | + typeof item[key as keyof typeof item] === 'object' && |
| 115 | + 'bdata' in (item[key as keyof typeof item] as Record<string, number[]>) |
| 116 | + ) { |
| 117 | + const bdata = (item[key as keyof typeof item] as { bdata: number[] }).bdata; |
| 118 | + if (Array.isArray(bdata) && bdata.some(x => typeof x === 'number' && isNaN(x))) { |
| 119 | + isNan = true; |
| 120 | + } else { |
| 121 | + (item[key as keyof typeof item] as number[]) = bdata as number[]; |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | + if (isNan) { |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (!isNan) { |
| 131 | + return plotlySchema; // Return the decoded data |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + plotlySchema.data = originalData; // Restore the original data if no changes were made |
| 136 | + return plotlySchema; // Return the original data if no changes were made |
| 137 | +} |
0 commit comments