|
| 1 | +const HASH_ALGORITHM = "SHA-256"; |
| 2 | +const CODEPUSH_METADATA = ".codepushrelease"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Computes SHA256 hash of the provided data |
| 6 | + */ |
| 7 | +export async function computeHash(input: string | Uint8Array): Promise<string> { |
| 8 | + const data = |
| 9 | + typeof input === "string" ? new TextEncoder().encode(input) : input; |
| 10 | + |
| 11 | + const hashBuffer = await crypto.subtle.digest(HASH_ALGORITHM, data); |
| 12 | + return Array.from(new Uint8Array(hashBuffer)) |
| 13 | + .map((b) => b.toString(16).padStart(2, "0")) |
| 14 | + .join(""); |
| 15 | +} |
| 16 | + |
| 17 | +export class PackageManifest { |
| 18 | + private readonly _map: Map<string, string>; |
| 19 | + |
| 20 | + constructor(map?: Map<string, string>) { |
| 21 | + this._map = new Map(map || new Map()); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns the internal map of file paths to hashes |
| 26 | + */ |
| 27 | + public toMap(): Map<string, string> { |
| 28 | + return new Map(this._map); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Computes the overall package hash from all file hashes |
| 33 | + */ |
| 34 | + public async computeHash(): Promise<string> { |
| 35 | + const entries = Array.from(this._map.entries()) |
| 36 | + .filter( |
| 37 | + ([name]) => |
| 38 | + name !== CODEPUSH_METADATA && !name.endsWith(`/${CODEPUSH_METADATA}`), |
| 39 | + ) |
| 40 | + .map(([name, hash]) => `${name}:${hash}`) |
| 41 | + .sort(); // Sort entries for consistent hashing |
| 42 | + |
| 43 | + return computeHash(JSON.stringify(entries)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Serializes the manifest to JSON |
| 48 | + */ |
| 49 | + public serialize(): string { |
| 50 | + return JSON.stringify(Object.fromEntries(this._map)); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Deserializes a manifest from JSON |
| 55 | + */ |
| 56 | + public static deserialize( |
| 57 | + serializedContents: string, |
| 58 | + ): PackageManifest | undefined { |
| 59 | + try { |
| 60 | + const obj = JSON.parse(serializedContents); |
| 61 | + const map = new Map<string, string>(); |
| 62 | + |
| 63 | + for (const [key, value] of Object.entries(obj)) { |
| 64 | + if (typeof value === "string") { |
| 65 | + map.set(key, value); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return new PackageManifest(map); |
| 70 | + } catch { |
| 71 | + return undefined; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Normalizes a file path to use forward slashes |
| 77 | + */ |
| 78 | + public static normalizePath(filePath: string): string { |
| 79 | + return filePath.replace(/\\/g, "/"); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Checks if a file should be ignored in manifest |
| 84 | + */ |
| 85 | + public static isIgnored(relativeFilePath: string): boolean { |
| 86 | + const MACOSX = "__MACOSX/"; |
| 87 | + const DS_STORE = ".DS_Store"; |
| 88 | + |
| 89 | + return ( |
| 90 | + relativeFilePath.startsWith(MACOSX) || |
| 91 | + relativeFilePath === DS_STORE || |
| 92 | + relativeFilePath.endsWith(`/${DS_STORE}`) |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
0 commit comments