Skip to content

Commit

Permalink
Merge pull request #15 from statelyai/davidkpiano/use-safe-stringify
Browse files Browse the repository at this point in the history
Use `safe-stable-stringify`
  • Loading branch information
davidkpiano authored Jan 27, 2024
2 parents 36a7ebc + b20b7b7 commit 904999e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-llamas-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@statelyai/inspect": patch
---

Use `safe-stable-stringify` everywhere applicable
2 changes: 1 addition & 1 deletion src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function createBrowserInspector(
const resolvedOptions = {
url: 'https://stately.ai/inspect',
filter: () => true,
serialize: (event) => JSON.parse(safeStringify(event)),
serialize: (inspectionEvent) => JSON.parse(safeStringify(inspectionEvent)),
autoStart: true,
iframe: null,
...options,
Expand Down
6 changes: 3 additions & 3 deletions src/createInspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function createInspector<TAdapter extends Adapter>(
typeof actorRef === 'string' ? actorRef : actorRef.sessionId;
const definitionObject = (actorRef as any)?.logic?.config;
const definition = definitionObject
? JSON.stringify(definitionObject)
? safeStringify(definitionObject)
: undefined;
const rootId =
info?.rootId ?? typeof actorRef === 'string'
Expand Down Expand Up @@ -163,14 +163,14 @@ export function convertXStateEvent(
}
const definitionString =
typeof definitionObject === 'object'
? JSON.stringify(definitionObject, (key, value) => {
? safeStringify(definitionObject, (_key, value) => {
if (typeof value === 'function') {
return { type: value.name };
}

return value;
})
: JSON.stringify({
: safeStringify({
id: name,
});

Expand Down
14 changes: 8 additions & 6 deletions src/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export class WebSocketAdapter implements Adapter {
constructor(options?: WebSocketInspectorOptions) {
this.options = {
filter: () => true,
serialize: (event) => JSON.parse(safeStringify(event)),
serialize: (inspectionEvent) =>
JSON.parse(safeStringify(inspectionEvent)),
autoStart: true,
url: 'ws://localhost:8080',
...options,
Expand All @@ -30,8 +31,9 @@ export class WebSocketAdapter implements Adapter {
this.ws.onopen = () => {
console.log('websocket open');
this.status = 'open';
this.deferredEvents.forEach((event) => {
this.ws.send(JSON.stringify(event));
this.deferredEvents.forEach((inspectionEvent) => {
const serializedEvent = this.options.serialize(inspectionEvent);
this.ws.send(safeStringify(serializedEvent));
});
};

Expand Down Expand Up @@ -61,11 +63,11 @@ export class WebSocketAdapter implements Adapter {
this.ws.close();
this.status = 'closed';
}
public send(event: StatelyInspectionEvent) {
public send(inspectionEvent: StatelyInspectionEvent) {
if (this.status === 'open') {
this.ws.send(JSON.stringify(event));
this.ws.send(safeStringify(inspectionEvent));
} else {
this.deferredEvents.push(event);
this.deferredEvents.push(inspectionEvent);
}
}
}
Expand Down

0 comments on commit 904999e

Please sign in to comment.