Skip to content

Commit

Permalink
Fix parsing due to large HTTP chunks
Browse files Browse the repository at this point in the history
Summary: The speech details caused the last payload to be too large to fit into one HTTP chunk.

Reviewed By: jayyteee

Differential Revision: D36902180

fbshipit-source-id: 8f1a745eea88ea0bdf98d69f399797617021a7b7
  • Loading branch information
patapizza authored and facebook-github-bot committed Jun 3, 2022
1 parent 9fff4f4 commit 03fa671
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## v6.2.2

- Fixes parsing for large HTTP chunks.

## v6.2.1

- Emits `partialTranscription` and `fullTranscription` events.
Expand Down
26 changes: 17 additions & 9 deletions lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ class Wit extends EventEmitter {
while (null !== (chunk = bodyStream.read())) {
contents += chunk.toString();
}
for (const {error, intents, text} of splitHttpChunks(
contents,
).map(x => JSON.parse(x))) {
for (const {error, intents, text} of parseResponse(contents)) {
if (!(error || intents)) {
logger.debug('[speech] partialTranscription:', text);
this.emit('partialTranscription', text);
Expand All @@ -119,10 +117,7 @@ class Wit extends EventEmitter {

return req
.then(response => Promise.all([response.text(), response.status]))
.then(([contents, status]) => [
JSON.parse(splitHttpChunks(contents).pop()),
status,
])
.then(([contents, status]) => [parseResponse(contents).pop(), status])
.catch(e => e)
.then(makeWitResponseHandler(logger, 'speech'));
}
Expand Down Expand Up @@ -177,12 +172,25 @@ const encodeURIParams = params =>
.map(([key, value]) => key + '=' + encodeURIComponent(value))
.join('&');

const splitHttpChunks = response =>
response
const parseResponse = response => {
const chunks = response
.split('\r\n')
.map(x => x.trim())
.filter(x => x.length > 0);

let prev = '';
let jsons = [];
for (const chunk of chunks) {
try {
prev += chunk;
jsons.push(JSON.parse(prev));
prev = '';
} catch (_e) {}
}

return jsons;
};

const validate = opts => {
if (!opts.accessToken) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-wit",
"version": "6.2.1",
"version": "6.2.2",
"description": "Wit.ai Node.js SDK",
"keywords": [
"wit",
Expand Down

0 comments on commit 03fa671

Please sign in to comment.