{"version":3,"file":"canonical-CVbmCnGM.mjs","names":[],"sources":["../src/canonical.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       ⚡ Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\n/**\n * Converts a JavaScript value to a canonical JSON string representation. This function is used for signing JSON objects in a consistent way, ensuring that the same input will always produce the same output string. The canonicalization process includes:\n * - Sorting object keys in lexicographical order.\n * - Removing whitespace and line breaks.\n * - Representing primitive values (null, boolean, number, string) in their standard JSON format.\n * - Recursively applying these rules to nested objects and arrays.\n *\n * This function is designed to produce a deterministic string representation of a JSON value, which is essential for cryptographic signing and verification processes where the exact byte representation of the data must be consistent across different environments and implementations.\n *\n * @param obj - The JavaScript value to convert to a canonical JSON string.\n * @returns A canonical JSON string representation of the input value.\n */\nexport function canonicalJson(obj: unknown): string {\n  if (obj === null || obj === undefined) {\n    return \"null\";\n  }\n\n  if (typeof obj === \"boolean\" || typeof obj === \"number\") {\n    return JSON.stringify(obj);\n  }\n\n  if (typeof obj === \"string\") {\n    return JSON.stringify(obj);\n  }\n\n  if (Array.isArray(obj)) {\n    const items = obj.map(item => canonicalJson(item));\n\n    return `[${items.join(\",\")}]`;\n  }\n\n  if (typeof obj === \"object\") {\n    const keys = Object.keys(obj).sort();\n    const pairs = keys.map(key => {\n      const value = canonicalJson((obj as Record<string, unknown>)[key]);\n\n      return `${JSON.stringify(key)}:${value}`;\n    });\n\n    return `{${pairs.join(\",\")}}`;\n  }\n\n  return \"null\";\n}\n"],"mappings":";;;;;;;;;;;;;AA8BA,SAAgB,cAAc,KAAsB;AAClD,KAAI,QAAQ,QAAQ,QAAQ,OAC1B,QAAO;AAGT,KAAI,OAAO,QAAQ,aAAa,OAAO,QAAQ,SAC7C,QAAO,KAAK,UAAU,IAAI;AAG5B,KAAI,OAAO,QAAQ,SACjB,QAAO,KAAK,UAAU,IAAI;AAG5B,KAAI,MAAM,QAAQ,IAAI,CAGpB,QAAO,IAFO,IAAI,KAAI,SAAQ,cAAc,KAAK,CAAC,CAEjC,KAAK,IAAI,CAAC;AAG7B,KAAI,OAAO,QAAQ,SAQjB,QAAO,IAPM,OAAO,KAAK,IAAI,CAAC,MAAM,CACjB,KAAI,QAAO;EAC5B,MAAM,QAAQ,cAAe,IAAgC,KAAK;AAElE,SAAO,GAAG,KAAK,UAAU,IAAI,CAAC,GAAG;GACjC,CAEe,KAAK,IAAI,CAAC;AAG7B,QAAO"}