Skip to content

Commit 904999e

Browse files
authored
Merge pull request #15 from statelyai/davidkpiano/use-safe-stringify
Use `safe-stable-stringify`
2 parents 36a7ebc + b20b7b7 commit 904999e

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

.changeset/bright-llamas-fetch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@statelyai/inspect": patch
3+
---
4+
5+
Use `safe-stable-stringify` everywhere applicable

src/browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function createBrowserInspector(
5252
const resolvedOptions = {
5353
url: 'https://stately.ai/inspect',
5454
filter: () => true,
55-
serialize: (event) => JSON.parse(safeStringify(event)),
55+
serialize: (inspectionEvent) => JSON.parse(safeStringify(inspectionEvent)),
5656
autoStart: true,
5757
iframe: null,
5858
...options,

src/createInspector.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export function createInspector<TAdapter extends Adapter>(
6969
typeof actorRef === 'string' ? actorRef : actorRef.sessionId;
7070
const definitionObject = (actorRef as any)?.logic?.config;
7171
const definition = definitionObject
72-
? JSON.stringify(definitionObject)
72+
? safeStringify(definitionObject)
7373
: undefined;
7474
const rootId =
7575
info?.rootId ?? typeof actorRef === 'string'
@@ -163,14 +163,14 @@ export function convertXStateEvent(
163163
}
164164
const definitionString =
165165
typeof definitionObject === 'object'
166-
? JSON.stringify(definitionObject, (key, value) => {
166+
? safeStringify(definitionObject, (_key, value) => {
167167
if (typeof value === 'function') {
168168
return { type: value.name };
169169
}
170170

171171
return value;
172172
})
173-
: JSON.stringify({
173+
: safeStringify({
174174
id: name,
175175
});
176176

src/webSocket.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export class WebSocketAdapter implements Adapter {
1717
constructor(options?: WebSocketInspectorOptions) {
1818
this.options = {
1919
filter: () => true,
20-
serialize: (event) => JSON.parse(safeStringify(event)),
20+
serialize: (inspectionEvent) =>
21+
JSON.parse(safeStringify(inspectionEvent)),
2122
autoStart: true,
2223
url: 'ws://localhost:8080',
2324
...options,
@@ -30,8 +31,9 @@ export class WebSocketAdapter implements Adapter {
3031
this.ws.onopen = () => {
3132
console.log('websocket open');
3233
this.status = 'open';
33-
this.deferredEvents.forEach((event) => {
34-
this.ws.send(JSON.stringify(event));
34+
this.deferredEvents.forEach((inspectionEvent) => {
35+
const serializedEvent = this.options.serialize(inspectionEvent);
36+
this.ws.send(safeStringify(serializedEvent));
3537
});
3638
};
3739

@@ -61,11 +63,11 @@ export class WebSocketAdapter implements Adapter {
6163
this.ws.close();
6264
this.status = 'closed';
6365
}
64-
public send(event: StatelyInspectionEvent) {
66+
public send(inspectionEvent: StatelyInspectionEvent) {
6567
if (this.status === 'open') {
66-
this.ws.send(JSON.stringify(event));
68+
this.ws.send(safeStringify(inspectionEvent));
6769
} else {
68-
this.deferredEvents.push(event);
70+
this.deferredEvents.push(inspectionEvent);
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)