Comparing version 6.1.1 to 6.2.0
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -4,0 +2,0 @@ * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
/** | ||
@@ -9,3 +7,3 @@ * Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved. | ||
DEFAULT_API_VERSION: '20210928', | ||
DEFAULT_WIT_URL: 'https://api.wit.ai' | ||
}; | ||
DEFAULT_WIT_URL: 'https://api.wit.ai', | ||
}; |
@@ -7,23 +7,23 @@ /** | ||
var fs = require('fs'); | ||
var mic = require('mic'); | ||
var readline = require('readline'); | ||
const fs = require('fs'); | ||
const mic = require('mic'); | ||
const readline = require('readline'); | ||
var AUDIO_PATH = '/tmp/output.raw'; | ||
var MIC_TIMEOUT_MS = 3000; | ||
const AUDIO_PATH = '/tmp/output.raw'; | ||
const MIC_TIMEOUT_MS = 3000; | ||
module.exports = function (wit, handleResponse, context) { | ||
var rl = readline.createInterface({ | ||
module.exports = (wit, handleResponse, context) => { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
output: process.stdout, | ||
}); | ||
rl.setPrompt('> '); | ||
var prompt = function prompt() { | ||
const prompt = () => { | ||
rl.prompt(); | ||
rl.write(null, { ctrl: true, name: 'e' }); | ||
rl.write(null, {ctrl: true, name: 'e'}); | ||
}; | ||
prompt(); | ||
var makeResponseHandler = function makeResponseHandler(rsp) { | ||
const makeResponseHandler = rsp => { | ||
if (handleResponse) { | ||
@@ -37,3 +37,3 @@ handleResponse(rsp); | ||
rl.on('line', function (line) { | ||
rl.on('line', line => { | ||
line = line.trim(); | ||
@@ -46,3 +46,3 @@ if (!line) { | ||
if (line === '!speech') { | ||
var microphone = mic({ | ||
const microphone = mic({ | ||
bitwidth: '16', | ||
@@ -53,17 +53,24 @@ channels: '1', | ||
fileType: 'raw', | ||
rate: '16000' | ||
rate: '16000', | ||
}); | ||
var inputAudioStream = microphone.getAudioStream(); | ||
var outputFileStream = fs.WriteStream(AUDIO_PATH); | ||
const inputAudioStream = microphone.getAudioStream(); | ||
const outputFileStream = fs.WriteStream(AUDIO_PATH); | ||
inputAudioStream.pipe(outputFileStream); | ||
inputAudioStream.on('startComplete', function () { | ||
setTimeout(function () { | ||
inputAudioStream.on('startComplete', () => { | ||
setTimeout(() => { | ||
microphone.stop(); | ||
}, MIC_TIMEOUT_MS); | ||
}); | ||
inputAudioStream.on('stopComplete', function () { | ||
var stream = fs.ReadStream(AUDIO_PATH); | ||
wit.speech('audio/raw;encoding=signed-integer;bits=16;rate=16000;endian=little', stream, context).then(makeResponseHandler).catch(console.error); | ||
inputAudioStream.on('stopComplete', () => { | ||
const stream = fs.ReadStream(AUDIO_PATH); | ||
wit | ||
.speech( | ||
'audio/raw;encoding=signed-integer;bits=16;rate=16000;endian=little', | ||
stream, | ||
context, | ||
) | ||
.then(makeResponseHandler) | ||
.catch(console.error); | ||
}); | ||
@@ -79,2 +86,2 @@ | ||
}); | ||
}; | ||
}; |
@@ -7,26 +7,25 @@ /** | ||
var _funcs; | ||
const DEBUG = 'debug'; | ||
const INFO = 'info'; | ||
const WARN = 'warn'; | ||
const ERROR = 'error'; | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
const levels = [DEBUG, INFO, WARN, ERROR]; | ||
const funcs = { | ||
[DEBUG]: console.debug.bind(console, '[wit][debug]'), | ||
[INFO]: console.log.bind(console, '[wit]'), | ||
[WARN]: console.warn.bind(console, '[wit]'), | ||
[ERROR]: console.error.bind(console, '[wit]'), | ||
}; | ||
const noop = () => {} | ||
var DEBUG = 'debug'; | ||
var INFO = 'info'; | ||
var WARN = 'warn'; | ||
var ERROR = 'error'; | ||
var levels = [DEBUG, INFO, WARN, ERROR]; | ||
var funcs = (_funcs = {}, _defineProperty(_funcs, DEBUG, console.debug.bind(console, '[wit][debug]')), _defineProperty(_funcs, INFO, console.log.bind(console, '[wit]')), _defineProperty(_funcs, WARN, console.warn.bind(console, '[wit]')), _defineProperty(_funcs, ERROR, console.error.bind(console, '[wit]')), _funcs); | ||
var noop = function noop() {}; | ||
var Logger = function Logger(lvl) { | ||
var _this = this; | ||
const Logger = function(lvl) { | ||
this.level = lvl || INFO; | ||
levels.forEach(function (x) { | ||
var should = levels.indexOf(x) >= levels.indexOf(lvl); | ||
_this[x] = should ? funcs[x] : noop; | ||
levels.forEach((x) => { | ||
const should = levels.indexOf(x) >= levels.indexOf(lvl); | ||
this[x] = should ? funcs[x] : noop; | ||
}); | ||
}; | ||
module.exports = { Logger: Logger, DEBUG: DEBUG, INFO: INFO, WARN: WARN, ERROR: ERROR }; | ||
module.exports = { Logger, DEBUG, INFO, WARN, ERROR }; |
195
lib/wit.js
@@ -7,34 +7,15 @@ /** | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
const {DEFAULT_API_VERSION, DEFAULT_WIT_URL} = require('./config'); | ||
const log = require('./log'); | ||
const fetch = require('isomorphic-fetch'); | ||
const Url = require('url'); | ||
const HttpsProxyAgent = require('https-proxy-agent'); | ||
const {Readable} = require('stream'); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _require = require('./config'), | ||
DEFAULT_API_VERSION = _require.DEFAULT_API_VERSION, | ||
DEFAULT_WIT_URL = _require.DEFAULT_WIT_URL; | ||
var log = require('./log'); | ||
var fetch = require('isomorphic-fetch'); | ||
var Url = require('url'); | ||
var HttpsProxyAgent = require('https-proxy-agent'); | ||
var _require2 = require('stream'), | ||
Readable = _require2.Readable; | ||
function Wit(opts) { | ||
if (!(this instanceof Wit)) { | ||
return new Wit(opts); | ||
class Wit { | ||
constructor(opts) { | ||
this.config = Object.freeze(validate(opts)); | ||
} | ||
var _config = this.config = Object.freeze(validate(opts)), | ||
accessToken = _config.accessToken, | ||
apiVersion = _config.apiVersion, | ||
headers = _config.headers, | ||
logger = _config.logger, | ||
witURL = _config.witURL, | ||
proxy = _config.proxy; | ||
this.message = function (q, context, n) { | ||
message(q, context, n) { | ||
if (typeof q !== 'string') { | ||
@@ -44,8 +25,10 @@ throw new Error('Please provide a text input (string).'); | ||
var params = { | ||
q: q, | ||
v: apiVersion | ||
const {apiVersion, headers, logger, proxy, witURL} = this.config; | ||
const params = { | ||
q, | ||
v: apiVersion, | ||
}; | ||
if ((typeof context === 'undefined' ? 'undefined' : _typeof(context)) === 'object') { | ||
if (typeof context === 'object') { | ||
params.context = JSON.stringify(context); | ||
@@ -58,16 +41,16 @@ } | ||
var method = 'GET'; | ||
var fullURL = witURL + '/message?' + encodeURIParams(params); | ||
const method = 'GET'; | ||
const fullURL = witURL + '/message?' + encodeURIParams(params); | ||
logger.debug(method, fullURL); | ||
return fetch(fullURL, { | ||
method: method, | ||
headers: headers, | ||
proxy: proxy | ||
}).then(function (response) { | ||
return Promise.all([response.json(), response.status]); | ||
}).then(makeWitResponseHandler(logger, 'message')); | ||
}; | ||
method, | ||
headers, | ||
proxy, | ||
}) | ||
.then(response => Promise.all([response.json(), response.status])) | ||
.then(makeWitResponseHandler(logger, 'message')); | ||
} | ||
this.speech = function (contentType, body, context, n) { | ||
speech(contentType, body, context, n) { | ||
if (typeof contentType !== 'string') { | ||
@@ -81,7 +64,9 @@ throw new Error('Please provide a content-type (string).'); | ||
var params = { | ||
v: apiVersion | ||
const {apiVersion, headers, logger, proxy, witURL} = this.config; | ||
const params = { | ||
v: apiVersion, | ||
}; | ||
if ((typeof context === 'undefined' ? 'undefined' : _typeof(context)) === 'object') { | ||
if (typeof context === 'object') { | ||
params.context = JSON.stringify(context); | ||
@@ -94,68 +79,63 @@ } | ||
var method = 'POST'; | ||
var fullURL = witURL + '/speech?' + encodeURIParams(params); | ||
const method = 'POST'; | ||
const fullURL = witURL + '/speech?' + encodeURIParams(params); | ||
logger.debug(method, fullURL); | ||
return fetch(fullURL, { | ||
body: body, | ||
method: method, | ||
headers: _extends({}, headers, { | ||
body, | ||
method, | ||
headers: { | ||
...headers, | ||
'Content-Type': contentType, | ||
'Transfer-Encoding': 'chunked' | ||
'Transfer-Encoding': 'chunked', | ||
}, | ||
}) | ||
.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(function (response) { | ||
return Promise.all([response.text(), response.status]); | ||
}).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
contents = _ref2[0], | ||
status = _ref2[1]; | ||
.catch(e => e) | ||
.then(makeWitResponseHandler(logger, 'speech')); | ||
} | ||
} | ||
var chunks = contents.split('\r\n').map(function (x) { | ||
return x.trim(); | ||
}).filter(function (x) { | ||
return x.length > 0; | ||
}); | ||
return [JSON.parse(chunks[chunks.length - 1]), status]; | ||
}).catch(function (e) { | ||
return e; | ||
}).then(makeWitResponseHandler(logger, 'speech')); | ||
const makeWitResponseHandler = (logger, endpoint) => rsp => { | ||
const error = e => { | ||
logger.error('[' + endpoint + '] Error: ' + e); | ||
throw e; | ||
}; | ||
} | ||
var makeWitResponseHandler = function makeWitResponseHandler(logger, endpoint) { | ||
return function (rsp) { | ||
var error = function error(e) { | ||
logger.error('[' + endpoint + '] Error: ' + e); | ||
throw e; | ||
}; | ||
if (rsp instanceof Error) { | ||
return error(rsp); | ||
} | ||
if (rsp instanceof Error) { | ||
return error(rsp); | ||
} | ||
const [json, status] = rsp; | ||
var _rsp = _slicedToArray(rsp, 2), | ||
json = _rsp[0], | ||
status = _rsp[1]; | ||
if (json instanceof Error) { | ||
return error(json); | ||
} | ||
if (json instanceof Error) { | ||
return error(json); | ||
} | ||
const err = json.error || (status !== 200 && json.body + ' (' + status + ')'); | ||
var err = json.error || status !== 200 && json.body + ' (' + status + ')'; | ||
if (err) { | ||
return error(err); | ||
} | ||
if (err) { | ||
return error(err); | ||
} | ||
logger.debug('[' + endpoint + '] Response: ' + JSON.stringify(json)); | ||
return json; | ||
}; | ||
logger.debug('[' + endpoint + '] Response: ' + JSON.stringify(json)); | ||
return json; | ||
}; | ||
var getProxyAgent = function getProxyAgent(witURL) { | ||
var url = Url.parse(witURL); | ||
var proxy = url.protocol === 'http:' ? process.env.http_proxy || process.env.HTTP_PROXY : process.env.https_proxy || process.env.HTTPS_PROXY; | ||
var noProxy = process.env.no_proxy || process.env.NO_PROXY; | ||
const getProxyAgent = witURL => { | ||
const url = Url.parse(witURL); | ||
const proxy = | ||
url.protocol === 'http:' | ||
? process.env.http_proxy || process.env.HTTP_PROXY | ||
: process.env.https_proxy || process.env.HTTPS_PROXY; | ||
const noProxy = process.env.no_proxy || process.env.NO_PROXY; | ||
var shouldIgnore = noProxy && noProxy.indexOf(url.hostname) > -1; | ||
const shouldIgnore = noProxy && noProxy.indexOf(url.hostname) > -1; | ||
if (proxy && !shouldIgnore) { | ||
@@ -170,15 +150,12 @@ return new HttpsProxyAgent(proxy); | ||
var encodeURIParams = function encodeURIParams(params) { | ||
return Object.entries(params).map(function (_ref3) { | ||
var _ref4 = _slicedToArray(_ref3, 2), | ||
key = _ref4[0], | ||
value = _ref4[1]; | ||
const encodeURIParams = params => | ||
Object.entries(params) | ||
.map(([key, value]) => key + '=' + encodeURIComponent(value)) | ||
.join('&'); | ||
return key + '=' + encodeURIComponent(value); | ||
}).join('&'); | ||
}; | ||
var validate = function validate(opts) { | ||
const validate = opts => { | ||
if (!opts.accessToken) { | ||
throw new Error('Could not find access token, learn more at https://wit.ai/docs'); | ||
throw new Error( | ||
'Could not find access token, learn more at https://wit.ai/docs', | ||
); | ||
} | ||
@@ -190,3 +167,3 @@ | ||
Authorization: 'Bearer ' + opts.accessToken, | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
}; | ||
@@ -199,2 +176,2 @@ opts.logger = opts.logger || new log.Logger(log.INFO); | ||
module.exports = Wit; | ||
module.exports = Wit; |
{ | ||
"name": "node-wit", | ||
"version": "6.1.1", | ||
"version": "6.2.0", | ||
"description": "Wit.ai Node.js SDK", | ||
@@ -30,8 +30,5 @@ "keywords": [ | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=6.17.1" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-es2015": "^6.16.0", | ||
"babel-preset-stage-0": "^6.16.0", | ||
"chai": "^3.5.0", | ||
@@ -38,0 +35,0 @@ "mocha": "^3.5.3", |
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
3
7911
244
4