|
| 1 | +/** |
| 2 | + * Vendored from deterministic-object-hash@2.0.2 (MIT) |
| 3 | + * https://github.com/nicholasgasior/deterministic-object-hash |
| 4 | + * |
| 5 | + * Only `deterministicString` is needed - the async `deterministicHash` (which |
| 6 | + * pulls in `node:crypto`) is intentionally excluded so this module stays |
| 7 | + * runtime-agnostic (works in Node, workerd, browsers, etc.). |
| 8 | + */ |
| 9 | + |
| 10 | +const objConstructorString = Function.prototype.toString.call(Object); |
| 11 | + |
| 12 | +function isPlainObject(value: unknown): value is Record<string | symbol, unknown> { |
| 13 | + if ( |
| 14 | + typeof value !== 'object' || |
| 15 | + value === null || |
| 16 | + Object.prototype.toString.call(value) !== '[object Object]' |
| 17 | + ) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + const proto = Object.getPrototypeOf(value); |
| 21 | + if (proto === null) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + if (!Object.prototype.hasOwnProperty.call(proto, 'constructor')) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + return ( |
| 28 | + typeof proto.constructor === 'function' && |
| 29 | + proto.constructor instanceof proto.constructor && |
| 30 | + Function.prototype.toString.call(proto.constructor) === objConstructorString |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +/** Recursively serializes any JS value into a deterministic string. */ |
| 35 | +export function deterministicString(input: unknown): string { |
| 36 | + if (typeof input === 'string') { |
| 37 | + return JSON.stringify(input); |
| 38 | + } else if (typeof input === 'symbol' || typeof input === 'function') { |
| 39 | + return input.toString(); |
| 40 | + } else if (typeof input === 'bigint') { |
| 41 | + return `${input}n`; |
| 42 | + } else if ( |
| 43 | + input === globalThis || |
| 44 | + input === undefined || |
| 45 | + input === null || |
| 46 | + typeof input === 'boolean' || |
| 47 | + typeof input === 'number' || |
| 48 | + typeof input !== 'object' |
| 49 | + ) { |
| 50 | + return `${input}`; |
| 51 | + } else if (input instanceof Date) { |
| 52 | + return `(${input.constructor.name}:${input.getTime()})`; |
| 53 | + } else if ( |
| 54 | + input instanceof RegExp || |
| 55 | + input instanceof Error || |
| 56 | + input instanceof WeakMap || |
| 57 | + input instanceof WeakSet |
| 58 | + ) { |
| 59 | + return `(${input.constructor.name}:${input.toString()})`; |
| 60 | + } else if (input instanceof Set) { |
| 61 | + let ret = `(${input.constructor.name}:[`; |
| 62 | + for (const val of input.values()) { |
| 63 | + ret += `${deterministicString(val)},`; |
| 64 | + } |
| 65 | + ret += '])'; |
| 66 | + return ret; |
| 67 | + } else if ( |
| 68 | + Array.isArray(input) || |
| 69 | + input instanceof Int8Array || |
| 70 | + input instanceof Uint8Array || |
| 71 | + input instanceof Uint8ClampedArray || |
| 72 | + input instanceof Int16Array || |
| 73 | + input instanceof Uint16Array || |
| 74 | + input instanceof Int32Array || |
| 75 | + input instanceof Uint32Array || |
| 76 | + input instanceof Float32Array || |
| 77 | + input instanceof Float64Array || |
| 78 | + input instanceof BigInt64Array || |
| 79 | + input instanceof BigUint64Array |
| 80 | + ) { |
| 81 | + let ret = `(${input.constructor.name}:[`; |
| 82 | + for (const [k, v] of input.entries()) { |
| 83 | + ret += `(${k}:${deterministicString(v)}),`; |
| 84 | + } |
| 85 | + ret += '])'; |
| 86 | + return ret; |
| 87 | + } else if (input instanceof ArrayBuffer || input instanceof SharedArrayBuffer) { |
| 88 | + if (input.byteLength % 8 === 0) { |
| 89 | + return deterministicString(new BigUint64Array(input)); |
| 90 | + } else if (input.byteLength % 4 === 0) { |
| 91 | + return deterministicString(new Uint32Array(input)); |
| 92 | + } else if (input.byteLength % 2 === 0) { |
| 93 | + return deterministicString(new Uint16Array(input)); |
| 94 | + } else { |
| 95 | + let ret = '('; |
| 96 | + for (let i = 0; i < input.byteLength; i++) { |
| 97 | + ret += `${deterministicString(new Uint8Array(input.slice(i, i + 1)))},`; |
| 98 | + } |
| 99 | + ret += ')'; |
| 100 | + return ret; |
| 101 | + } |
| 102 | + } else if (input instanceof Map || isPlainObject(input)) { |
| 103 | + const sortable: [string, string][] = []; |
| 104 | + const entries = input instanceof Map ? input.entries() : Object.entries(input); |
| 105 | + for (const [k, v] of entries) { |
| 106 | + sortable.push([deterministicString(k), deterministicString(v)]); |
| 107 | + } |
| 108 | + if (!(input instanceof Map)) { |
| 109 | + const symbolKeys = Object.getOwnPropertySymbols(input); |
| 110 | + for (let i = 0; i < symbolKeys.length; i++) { |
| 111 | + sortable.push([ |
| 112 | + deterministicString(symbolKeys[i]!), |
| 113 | + deterministicString((input as Record<symbol, unknown>)[symbolKeys[i]!]), |
| 114 | + ]); |
| 115 | + } |
| 116 | + } |
| 117 | + sortable.sort(([a], [b]) => a.localeCompare(b)); |
| 118 | + let ret = `(${input.constructor.name}:[`; |
| 119 | + for (const [k, v] of sortable) { |
| 120 | + ret += `(${k}:${v}),`; |
| 121 | + } |
| 122 | + ret += '])'; |
| 123 | + return ret; |
| 124 | + } |
| 125 | + |
| 126 | + const allEntries: [string, string][] = []; |
| 127 | + for (const k in input) { |
| 128 | + allEntries.push([ |
| 129 | + deterministicString(k), |
| 130 | + deterministicString((input as Record<string, unknown>)[k]), |
| 131 | + ]); |
| 132 | + } |
| 133 | + const symbolKeys = Object.getOwnPropertySymbols(input); |
| 134 | + for (let i = 0; i < symbolKeys.length; i++) { |
| 135 | + allEntries.push([ |
| 136 | + deterministicString(symbolKeys[i]!), |
| 137 | + deterministicString((input as Record<symbol, unknown>)[symbolKeys[i]!]), |
| 138 | + ]); |
| 139 | + } |
| 140 | + allEntries.sort(([a], [b]) => a.localeCompare(b)); |
| 141 | + let ret = `(${input.constructor.name}:[`; |
| 142 | + for (const [k, v] of allEntries) { |
| 143 | + ret += `(${k}:${v}),`; |
| 144 | + } |
| 145 | + ret += '])'; |
| 146 | + return ret; |
| 147 | +} |
0 commit comments