Skip to content

Commit 1fc5ec7

Browse files
fix(overmind): safely serialize arrays with unsafe classes
1 parent c1d85c2 commit 1fc5ec7

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

packages/node_modules/overmind/src/Devtools.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ export type DevtoolsMessage = {
1111
data: any
1212
}
1313

14+
function isClass(value: any) {
15+
return (
16+
typeof value === 'object' &&
17+
!Array.isArray(value) &&
18+
value !== null &&
19+
value.constructor &&
20+
value.constructor.name !== 'Object'
21+
)
22+
}
23+
1424
export class Devtools {
1525
private safeClassNames: Set<string> = new Set()
1626
private unsafeClassNames: Set<string> = new Set()
@@ -90,13 +100,26 @@ export class Devtools {
90100
}
91101
}
92102

93-
if (
94-
typeof value === 'object' &&
95-
value !== null &&
96-
!Array.isArray(value) &&
97-
value.constructor &&
98-
value.constructor.name !== 'Object'
99-
) {
103+
if (Array.isArray(value) && isClass(value[0])) {
104+
if (!safeClassNames.has(value[0].constructor.name) && !unsafeClassNames.has(value[0].constructor.name)) {
105+
try {
106+
JSON.stringify(value[0])
107+
safeClassNames.add(value[0].constructor.name)
108+
} catch {
109+
unsafeClassNames.add(value[0].constructor.name)
110+
}
111+
}
112+
113+
if (safeClassNames.has(value[0].constructor.name)) {
114+
return value.map((item) => ({
115+
__CLASS__: true,
116+
name: value[0].constructor.name,
117+
value: item
118+
}))
119+
} else {
120+
return value.map(() => `[${value.constructor.name || 'NOT SERIALIZABLE'}]`)
121+
}
122+
} else if (isClass(value)) {
100123
if (!safeClassNames.has(value.constructor.name) && !unsafeClassNames.has(value.constructor.name)) {
101124
try {
102125
JSON.stringify(value)

0 commit comments

Comments
 (0)