| 44 | * Computes a hash for a give string and returns hash as a hex value. |
| 45 | */ |
| 46 | export async function computeHash(data: string, algorithm: 'SHA-512' | 'SHA-256' | 'SHA-1'): Promise<string> { |
| 47 | // Save some CPU as this is called in a number of places. |
| 48 | // This will not get too large, will only grow by number of files per workspace, even if user has |
| 49 | // 1000s of files, this will not grow that large to cause any memory issues. |
| 50 | // Files get hashed a lot in a number of places within the extension (.interactive is the IW window Uri). |
| 51 | // Even things that include file paths like kernel id, which isn't a file path, but contains python executable path. |
| 52 | const isCandidateForCaching = data.includes('/') || data.includes('\\') || data.endsWith('.interactive'); |
| 53 | if (isCandidateForCaching && computedHashes[data]) { |
| 54 | return computedHashes[data]; |
| 55 | } |
| 56 | |
| 57 | const hash = await computeHashInternal(data, algorithm); |
| 58 | |
| 59 | if (isCandidateForCaching && !stopStoringHashes) { |
| 60 | // Just a simple fail safe, why 10_000, simple why not 10_000 |
| 61 | // All we want to ensure is that we don't store too many hashes. |
| 62 | // The only way we can get there is if user never closes VS Code and our code |
| 63 | // ends up hashing Uris of cells, then again user would have to have 1000s of cells in notebooks to hit this case. |
| 64 | if (Object.keys(computedHashes).length > 10_000) { |
| 65 | stopStoringHashes = true; |
| 66 | } |
| 67 | computedHashes[data] = hash; |
| 68 | } |
| 69 | return hash; |
| 70 | } |
| 71 | |
| 72 | async function computeHashInternal(data: string, algorithm: 'SHA-512' | 'SHA-256' | 'SHA-1'): Promise<string> { |
| 73 | // Ensure crypto provider is initialized |