Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

hash.ts

157 lines3.8 KB

1import { MurmurHashStream, randomHash } from "./murmur.js"
2import type { Hasher } from "./murmur.js"
3​
4/*
5 * Implementation of structural hashing based on the Composites polyfill implementation:
6 * https://github.com/tc39/proposal-composites
7 */
8​
9const TRUE = randomHash()
10const FALSE = randomHash()
11const NULL = randomHash()
12const UNDEFINED = randomHash()
13const KEY = randomHash()
14const FUNCTIONS = randomHash()
15const DATE_MARKER = randomHash()
16const OBJECT_MARKER = randomHash()
17const ARRAY_MARKER = randomHash()
18const MAP_MARKER = randomHash()
19const SET_MARKER = randomHash()
20​
21const hashCache = new WeakMap<object, number>()
22​
23export function hash(input: any): number {
24 const hasher = new MurmurHashStream()
25 updateHasher(hasher, input)
26 return hasher.digest()
28​
29function hashObject(input: object): number {
30 const cachedHash = hashCache.get(input)
31 if (cachedHash !== undefined) {
32 return cachedHash
33 }
34​
35 let valueHash: number | undefined
36 if (input instanceof Date) {
37 valueHash = hashDate(input)
38 } else {
39 let plainObjectInput = input
40 let marker = OBJECT_MARKER
41​
42 if (input instanceof Array) {
43 marker = ARRAY_MARKER
44 }
45​
46 if (input instanceof Map) {
47 marker = MAP_MARKER
48 plainObjectInput = [...input.entries()]
49 }
50​
51 if (input instanceof Set) {
52 marker = SET_MARKER
53 plainObjectInput = [...input.entries()]
54 }
55​
56 if (
57 (typeof Buffer !== `undefined` && input instanceof Buffer) ||
58 input instanceof Uint8Array ||
59 input instanceof File
60 ) {
61 // Deeply hashing these objects would be too costly
62 // but we also don't want to ignore them
63 // so we track them by reference and cache them in a weak map
64 return cachedReferenceHash(input)
65 }
66​
67 valueHash = hashPlainObject(plainObjectInput, marker)
68 }
69​
70 hashCache.set(input, valueHash)
71 return valueHash
73​
74function hashDate(input: Date): number {
75 const hasher = new MurmurHashStream()
76 hasher.update(DATE_MARKER)
77 hasher.update(input.getTime())
78 return hasher.digest()
80​
81function hashPlainObject(input: object, marker: number): number {
82 const hasher = new MurmurHashStream()
83​
84 // Mark the type of the input
85 hasher.update(marker)
86 const keys = Object.keys(input)
87 keys.sort(keySort)
88 for (const key of keys) {
89 hasher.update(KEY)
90 hasher.update(key)
91 updateHasher(hasher, input[key as keyof typeof input])
92 }
93​
94 return hasher.digest()
96​
97function updateHasher(hasher: Hasher, input: unknown): void {
98 if (input === null) {
99 hasher.update(NULL)
100 return
101 }
102 switch (typeof input) {
103 case `undefined`:
104 hasher.update(UNDEFINED)
105 return
106 case `boolean`:
107 hasher.update(input ? TRUE : FALSE)
108 return
109 case `number`:
110 // Normalize NaNs and -0
111 hasher.update(isNaN(input) ? NaN : input === 0 ? 0 : input)
112 return
113 case `bigint`:
114 case `string`:
115 case `symbol`:
116 hasher.update(input)
117 return
118 case `object`:
119 hasher.update(getCachedHash(input))
120 return
121 case `function`:
122 // Functions are assigned a globally unique ID
123 // and that ID is cached in the weak map
124 hasher.update(cachedReferenceHash(input))
125 return
126 default:
127 console.warn(
128 `Ignored input during hashing because it is of type ${typeof input} which is not supported`
129 )
130 }
132​
133function getCachedHash(input: object): number {
134 let valueHash = hashCache.get(input)
135 if (valueHash === undefined) {
136 valueHash = hashObject(input)
137 }
138 return valueHash
140​
141let nextRefId = 1
142function cachedReferenceHash(fn: object): number {
143 let valueHash = hashCache.get(fn)
144 if (valueHash === undefined) {
145 valueHash = nextRefId ^ FUNCTIONS
146 nextRefId++
147 hashCache.set(fn, valueHash)
148 }
149 return valueHash
151​
152/**
153 * Strings sorted lexicographically.
154 */
155function keySort(a: string, b: string): number {
156 return a.localeCompare(b)