Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit f80100e

Browse files
author
Jan Krems
committed
fix: Handle big ws frames correctly
Fixes #10
1 parent 00b77fb commit f80100e

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

lib/internal/inspect-protocol.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function decodeFrameHybi17(data) {
150150
default:
151151
// Nothing. We already have the right size.
152152
}
153-
if ((dataAvailable - payloadOffset) < 0) return notComplete;
153+
if ((dataAvailable - payloadOffset - payloadLength) < 0) return notComplete;
154154

155155
const payloadEnd = payloadOffset + payloadLength;
156156
return {
@@ -175,17 +175,29 @@ class Client extends EventEmitter {
175175
this._unprocessed = Buffer.concat([this._unprocessed, chunk]);
176176

177177
while (this._unprocessed.length > 2) {
178-
const { closed, payload, rest } = decodeFrameHybi17(this._unprocessed);
178+
const { closed, payload: payloadBuffer, rest } = decodeFrameHybi17(this._unprocessed);
179179
this._unprocessed = rest;
180180

181181
if (closed) {
182182
this.reset();
183183
return;
184184
}
185-
if (payload === null) break;
185+
if (payloadBuffer === null) break;
186+
187+
const payloadStr = payloadBuffer.toString();
188+
debuglog('< %s', payloadStr);
189+
if (payloadStr[0] !== '{' || payloadStr[payloadStr.length - 1] !== '}') {
190+
throw new Error(`Payload does not look like JSON: ${payloadStr}`);
191+
}
192+
let payload;
193+
try {
194+
payload = JSON.parse(payloadStr);
195+
} catch (parseError) {
196+
parseError.string = payloadStr;
197+
throw parseError;
198+
}
186199

187-
debuglog('< %s', payload);
188-
const { id, method, params, result, error } = JSON.parse(payload.toString());
200+
const { id, method, params, result, error } = payload;
189201
if (id) {
190202
const handler = this._pending[id];
191203
if (handler) {
@@ -196,7 +208,7 @@ class Client extends EventEmitter {
196208
this.emit('debugEvent', method, params);
197209
this.emit(method, params);
198210
} else {
199-
throw new Error(`Unsupported response: ${payload.toString()}`);
211+
throw new Error(`Unsupported response: ${payloadStr}`);
200212
}
201213
}
202214
}

test/cli/profile.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const { test } = require('tap');
3+
4+
const startCLI = require('./start-cli');
5+
6+
test('profiles', (t) => {
7+
const cli = startCLI(['examples/empty.js']);
8+
9+
function onFatal(error) {
10+
cli.quit();
11+
throw error;
12+
}
13+
14+
return cli.waitFor(/break/)
15+
.then(() => cli.waitForPrompt())
16+
.then(() => cli.command('exec console.profile()'))
17+
.then(() => {
18+
t.match(cli.output, 'undefined');
19+
})
20+
.then(() => cli.command('exec console.profileEnd()'))
21+
.then(() => {
22+
t.match(cli.output, 'undefined');
23+
})
24+
.then(() => cli.quit())
25+
.then(null, onFatal);
26+
});

0 commit comments

Comments
 (0)