Skip to content
This repository has been archived by the owner on Jul 31, 2020. It is now read-only.

Fix/circular #67

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"mocha": "^3.0.2",
"node-fetch": "^1.6.3",
"nodemon": "^1.11.0",
"rimraf": "^2.6.0",
"rimraf": "^2.6.1",
"sinon": "^2.2.0",
"sinon-chai": "^2.8.0",
"ts-node": "^2.0.0",
Expand All @@ -70,6 +70,7 @@
"ws": "^1.1.1"
},
"dependencies": {
"deepmerge": "^1.3.2"
"deepmerge": "^1.3.2",
"json-stringify-safe": "^5.0.1"
}
}
15 changes: 15 additions & 0 deletions src/wire/Socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,21 @@ describe('socket', () => {
});
});

it('handles circular references', () => {
const params: any = {
foo: 'bar',
};
params.bar = params;
ws.on('message', payload => {
assertAndReplyTo(payload);
});

return socket.execute('hello', params)
.then(res => {
expect(res).to.equal('hi');
});
});

it('emits a method sent to it', done => {
ws.send(JSON.stringify(METHOD));
socket.on('method', (method: Method<any>) => {
Expand Down
5 changes: 4 additions & 1 deletion src/wire/Socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { resolveOn } from '../util';
import { Method, Packet, PacketState, Reply } from './packets';
import { ExponentialReconnectionPolicy, IReconnectionPolicy } from './reconnection';

import stringify = require('json-stringify-safe'); //tslint:disable-line

/**
* Close codes that are deemed to be recoverable by the reconnection policy
*/
Expand Down Expand Up @@ -339,7 +341,8 @@ export class InteractiveSocket extends EventEmitter {
}

private sendRaw(packet: any) {
const data = JSON.stringify(packet);
// Replace circular references with nothing.
const data = stringify(packet, null, 0, () => {/** */});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only recursive data structure that is specified on the protocol is the memory warning structure the server sends. Sending any recursive packet is going to be invalid. I think that throwing an error is the right thing to do here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some people are literally doing something like:

control.text='blah'
contro.progress = 1.0

client.updateControls({sceneID:'blah',controls:[control]);

Which leads to the circular as control has a ref to scene.

Should we instead add a toJSON to each tree area that strips out circulars?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make sense!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright doing that. woot

const payload = data;

this.emit('send', payload);
Expand Down
10 changes: 10 additions & 0 deletions types/json-stringify-safe.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module 'json-stringify-safe' {
function stringify(
obj: object,
replacer?: (number | string)[] | null,
space?: string | number,
decycler: (key:string, value: any) => any
): string

export = stringify;
}