node-wit
Advanced tools
Comparing version 6.2.0 to 6.2.1
@@ -36,2 +36,9 @@ /** | ||
wit.on('partialTranscription', text => { | ||
console.log(text + '...'); | ||
}); | ||
wit.on('fullTranscription', text => { | ||
console.log(text + ' (final)'); | ||
}); | ||
rl.on('line', line => { | ||
@@ -76,2 +83,3 @@ line = line.trim(); | ||
microphone.start(); | ||
console.log('🎤 Listening...'); | ||
@@ -78,0 +86,0 @@ return; |
@@ -10,8 +10,10 @@ /** | ||
const fetch = require('isomorphic-fetch'); | ||
const Url = require('url'); | ||
const EventEmitter = require('events'); | ||
const HttpsProxyAgent = require('https-proxy-agent'); | ||
const {Readable} = require('stream'); | ||
const Url = require('url'); | ||
class Wit { | ||
class Wit extends EventEmitter { | ||
constructor(opts) { | ||
super(); | ||
this.config = Object.freeze(validate(opts)); | ||
@@ -80,5 +82,6 @@ } | ||
return fetch(fullURL, { | ||
const req = fetch(fullURL, { | ||
body, | ||
method, | ||
proxy, | ||
headers: { | ||
@@ -89,11 +92,38 @@ ...headers, | ||
}, | ||
}) | ||
}); | ||
const _partialResponses = req | ||
.then( | ||
response => | ||
new Promise((resolve, reject) => { | ||
logger.debug('status', response.status); | ||
const bodyStream = response.body; | ||
bodyStream.on('readable', () => { | ||
let chunk; | ||
let contents = ''; | ||
while (null !== (chunk = bodyStream.read())) { | ||
contents += chunk.toString(); | ||
} | ||
for (const {error, intents, text} of splitHttpChunks( | ||
contents, | ||
).map(x => JSON.parse(x))) { | ||
if (!(error || intents)) { | ||
logger.debug('[speech] partialTranscription:', text); | ||
this.emit('partialTranscription', text); | ||
} else if (text) { | ||
logger.debug('[speech] fullTranscription:', text); | ||
this.emit('fullTranscription', text); | ||
} | ||
} | ||
}); | ||
}), | ||
) | ||
.catch(e => logger.error('[speech] could not parse partial response', e)); | ||
return req | ||
.then(response => Promise.all([response.text(), response.status])) | ||
.then(([contents, status]) => { | ||
const chunks = contents | ||
.split('\r\n') | ||
.map(x => x.trim()) | ||
.filter(x => x.length > 0); | ||
return [JSON.parse(chunks[chunks.length - 1]), status]; | ||
}) | ||
.then(([contents, status]) => [ | ||
JSON.parse(splitHttpChunks(contents).pop()), | ||
status, | ||
]) | ||
.catch(e => e) | ||
@@ -153,2 +183,8 @@ .then(makeWitResponseHandler(logger, 'speech')); | ||
const splitHttpChunks = response => | ||
response | ||
.split('\r\n') | ||
.map(x => x.trim()) | ||
.filter(x => x.length > 0); | ||
const validate = opts => { | ||
@@ -155,0 +191,0 @@ if (!opts.accessToken) { |
{ | ||
"name": "node-wit", | ||
"version": "6.2.0", | ||
"version": "6.2.1", | ||
"description": "Wit.ai Node.js SDK", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17626
10
560
15
6