|
| 1 | +import { |
| 2 | + CONTENT_HASH_OFFSET, |
| 3 | + CONTENT_HASH_SIZE, |
| 4 | + ENTRY_SIZE, |
| 5 | + HEADER_SIZE, |
| 6 | + PATH_HASH_SIZE, |
| 7 | +} from "../../utils/constants"; |
| 8 | + |
| 9 | +export class AssetsManifest { |
| 10 | + private data: Uint8Array; |
| 11 | + |
| 12 | + constructor(data: ArrayBuffer) { |
| 13 | + this.data = new Uint8Array(data); |
| 14 | + } |
| 15 | + |
| 16 | + async get(pathname: string) { |
| 17 | + const pathHash = await hashPath(pathname); |
| 18 | + const entry = binarySearch(this.data, pathHash); |
| 19 | + return entry ? Uint8ToHexString(entry) : null; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +export const hashPath = async (path: string) => { |
| 24 | + const encoder = new TextEncoder(); |
| 25 | + const data = encoder.encode(path); |
| 26 | + const hashBuffer = await crypto.subtle.digest( |
| 27 | + "SHA-256", |
| 28 | + data.buffer as ArrayBuffer |
| 29 | + ); |
| 30 | + return new Uint8Array(hashBuffer, 0, PATH_HASH_SIZE); |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Search for an entry with the given hash path. |
| 35 | + * |
| 36 | + * @param manifest the manifest bytes |
| 37 | + * @param pathHash the path hash to find in the manifest |
| 38 | + * @returns The content hash when the entry is found and `false` otherwise |
| 39 | + */ |
| 40 | +export const binarySearch = ( |
| 41 | + manifest: Uint8Array, |
| 42 | + pathHash: Uint8Array |
| 43 | +): Uint8Array | false => { |
| 44 | + if (pathHash.byteLength !== PATH_HASH_SIZE) { |
| 45 | + throw new TypeError( |
| 46 | + `Search value should have a length of ${PATH_HASH_SIZE}` |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + const numberOfEntries = (manifest.byteLength - HEADER_SIZE) / ENTRY_SIZE; |
| 51 | + |
| 52 | + if (numberOfEntries === 0) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + let lowIndex = 0; |
| 57 | + let highIndex = numberOfEntries - 1; |
| 58 | + |
| 59 | + while (lowIndex <= highIndex) { |
| 60 | + const middleIndex = (lowIndex + highIndex) >> 1; |
| 61 | + |
| 62 | + const cmp = comparePathHashWithEntry(pathHash, manifest, middleIndex); |
| 63 | + |
| 64 | + if (cmp < 0) { |
| 65 | + highIndex = middleIndex - 1; |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (cmp > 0) { |
| 70 | + lowIndex = middleIndex + 1; |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + return new Uint8Array( |
| 75 | + manifest.buffer, |
| 76 | + HEADER_SIZE + middleIndex * ENTRY_SIZE + CONTENT_HASH_OFFSET, |
| 77 | + CONTENT_HASH_SIZE |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + return false; |
| 82 | +}; |
| 83 | + |
| 84 | +/** |
| 85 | + * Compares a search value with an entry in the manifest |
| 86 | + * |
| 87 | + * @param searchValue a `Uint8Array` of size `PATH_HASH_SIZE` |
| 88 | + * @param manifest the manifest bytes |
| 89 | + * @param entryIndex the index in the manifest of the entry to compare |
| 90 | + */ |
| 91 | +function comparePathHashWithEntry( |
| 92 | + searchValue: Uint8Array, |
| 93 | + manifest: Uint8Array, |
| 94 | + entryIndex: number |
| 95 | +) { |
| 96 | + let entryOffset = HEADER_SIZE + entryIndex * ENTRY_SIZE; |
| 97 | + for (let offset = 0; offset < PATH_HASH_SIZE; offset++, entryOffset++) { |
| 98 | + // We know that both values could not be undefined |
| 99 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 100 | + const s = searchValue[offset]!; |
| 101 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 102 | + const e = manifest[entryOffset]!; |
| 103 | + if (s < e) { |
| 104 | + return -1; |
| 105 | + } |
| 106 | + if (s > e) { |
| 107 | + return 1; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Converts an Uint8Array to an hex string |
| 116 | + * |
| 117 | + * @param array The content hash |
| 118 | + * @returns padded hex string |
| 119 | + */ |
| 120 | +const Uint8ToHexString = (array: Uint8Array) => { |
| 121 | + return [...array].map((b) => b.toString(16).padStart(2, "0")).join(""); |
| 122 | +}; |
0 commit comments