Skip to content

Commit ffa5a7d

Browse files
committed
fix: find bigint paths
1 parent 137d8e6 commit ffa5a7d

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/utils/multicast-listener.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as rxjs from 'rxjs'
22
import * as C from '../constants/constants.js'
3-
import { h64ToString } from '../utils/utils.js'
3+
import { h64ToString, findBigIntPaths } from '../utils/utils.js'
44
import * as timers from '../utils/timers.js'
55

66
class Provider {
@@ -35,7 +35,16 @@ class Provider {
3535
return
3636
}
3737

38-
const body = typeof value !== 'string' ? this.#listener._stringify(value) : value
38+
if (typeof value !== 'string') {
39+
try {
40+
value = this.#listener._stringify(value)
41+
} catch (err) {
42+
const bigIntPaths = /BigInt/.test(err.message) ? findBigIntPaths(value) : undefined
43+
throw Object.assign(new Error(`invalid value: ${value}`), { cause: err, data: { name: this.#name, bigIntPaths }})
44+
}
45+
}
46+
47+
const body = value
3948
const hash = h64ToString(body)
4049
const version = `INF-${hash}`
4150

src/utils/unicast-listener.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as C from '../constants/constants.js'
2-
import { h64ToString } from '../utils/utils.js'
2+
import { h64ToString, findBigIntPaths } from '../utils/utils.js'
33

44
class Observer {
55
#name
@@ -15,15 +15,22 @@ class Observer {
1515

1616
next(value) {
1717
let data
18-
if (value && typeof value === 'string') {
18+
if (value == null) {
19+
// Do nothing...
20+
} else if (typeof value === 'string') {
1921
if (value.charAt(0) !== '{' && value.charAt(0) !== '[') {
2022
throw new Error(`invalid value: ${value}`)
2123
}
2224
data = value
23-
} else if (value && typeof value === 'object') {
24-
data = JSON.stringify(value)
25-
} else if (data != null) {
26-
throw new Error(`invalid value: ${value}`)
25+
} else if (typeof value === 'object') {
26+
try {
27+
data = JSON.stringify(value)
28+
} catch (err) {
29+
const bigIntPaths = /BigInt/.test(err.message) ? findBigIntPaths(value) : undefined
30+
throw Object.assign(new Error(`invalid value: ${value}`), { cause: err, data: { name: this.#name, bigIntPaths }})
31+
}
32+
} else {
33+
throw Object.assign(new Error(`invalid value: ${value}`), { data: { name: this.#name }})
2734
}
2835

2936
const version = data ? `INF-${h64ToString(data)}` : ''

src/utils/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,19 @@ const HASHER = await xxhash()
182182
export function h64ToString(str) {
183183
return HASHER.h64ToString(str)
184184
}
185+
186+
export function findBigIntPaths(obj, path = "") {
187+
const paths = []
188+
189+
if (typeof obj === "bigint") {
190+
return [path]
191+
}
192+
193+
if (typeof obj === "object" && obj !== null) {
194+
for (const key of Object.keys(obj)) {
195+
paths.push(...findBigIntPaths(obj[key], path ? `${path}.${key}` : key))
196+
}
197+
}
198+
199+
return paths
200+
}

0 commit comments

Comments
 (0)