Spaces:
Sleeping
Sleeping
Download src/modules/shopify/domain/valueObjects/PayloadHash.ts from fouadkhalid123/asd: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/shopify/domain/valueObjects/PayloadHash.ts
- Command line
-
hf download hf://spaces/fouadkhalid123/asd/src/modules/shopify/domain/valueObjects/PayloadHash.ts
-
curl -L -o PayloadHash.ts https://huggingface.co/spaces/fouadkhalid123/asd/resolve/main/src/modules/shopify/domain/valueObjects/PayloadHash.ts
1.06 kB
| import { createHash } from "crypto"; | |
| export class PayloadHash { | |
| private constructor(public readonly value: string) { } | |
| static compute(payload: unknown): PayloadHash { | |
| const normalized = this.normalize(payload); | |
| const json = JSON.stringify(normalized); | |
| const hash = createHash("sha256").update(json).digest("hex"); | |
| return new PayloadHash(hash); | |
| } | |
| equals(other: PayloadHash): boolean { | |
| return this.value === other.value; | |
| } | |
| private static normalize(obj: unknown): unknown { | |
| if (obj === null || typeof obj !== "object") { | |
| return obj; | |
| } | |
| if (Array.isArray(obj)) { | |
| return obj.map((item) => this.normalize(item)); | |
| } | |
| const sortedObj: any = {}; | |
| const keys = Object.keys(obj as object).sort(); | |
| for (const key of keys) { | |
| sortedObj[key] = this.normalize((obj as any)[key]); | |
| } | |
| return sortedObj; | |
| } | |
| static fromString(hash: string): PayloadHash { | |
| return new PayloadHash(hash); | |
| } | |
| } | |