Skip to content

Commit aeae87c

Browse files
fix: do not catch encoding errors
It does not make sense to catch the errors thrown by JSON.stringify() and convert them to an ERROR packet (which are meant for namespace authentication errors), it should be caught higher in the stack. Related: 92c530d
1 parent 567c0ca commit aeae87c

File tree

2 files changed

+3
-21
lines changed

2 files changed

+3
-21
lines changed

lib/index.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,7 @@ export class Encoder {
8787

8888
// json data
8989
if (null != obj.data) {
90-
const payload = tryStringify(obj.data);
91-
if (payload !== false) {
92-
str += payload;
93-
} else {
94-
return ERROR_PACKET;
95-
}
90+
str += JSON.stringify(obj.data);
9691
}
9792

9893
debug("encoded %j as %s", obj, str);
@@ -117,22 +112,11 @@ export class Encoder {
117112
}
118113
}
119114

120-
const ERROR_PACKET = PacketType.ERROR + '"encode error"';
121-
122-
function tryStringify(str) {
123-
try {
124-
return JSON.stringify(str);
125-
} catch (e) {
126-
return false;
127-
}
128-
}
129-
130115
/**
131116
* A socket.io Decoder instance
132117
*
133118
* @return {Object} decoder
134119
*/
135-
// @ts-ignore
136120
export class Decoder extends Emitter {
137121
private reconstructor: BinaryReconstructor;
138122

test/parser.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('parser', function(){
5959
});
6060
});
6161

62-
it('properly handles circular objects', function() {
62+
it('throws an error when encoding circular objects', function() {
6363
var a = {};
6464
a.b = a;
6565

@@ -72,9 +72,7 @@ describe('parser', function(){
7272

7373
const encoder = new Encoder();
7474

75-
encoder.encode(data, function(encodedPackets) {
76-
expect(encodedPackets[0]).to.be('4"encode error"');
77-
});
75+
expect(() => encoder.encode(data)).to.throwException();
7876
});
7977

8078
it('decodes a bad binary packet', function(){

0 commit comments

Comments
 (0)