diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..86336375 --- /dev/null +++ b/.eslintignore @@ -0,0 +1,6 @@ +node_modules/ +dist/ +doc/ +examples/static/watson-speech.js +examples/static/bower_components/ +examples/node_modules/ diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 00000000..395da692 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,16 @@ +module.exports = { + "env": { + "browser": true, + "node": true // not actually, but we're writing code as if it were and letting browserify handle it + }, + "extends": "eslint:recommended", + "globals": { + // false meaning this code doesn't define it + Promise: false, + DataView: false, + ArrayBuffer: false + }, + "rules": { + + } +}; diff --git a/dist/watson-speech.js b/dist/watson-speech.js index f5a481c1..c5a761bc 100644 --- a/dist/watson-speech.js +++ b/dist/watson-speech.js @@ -1,4 +1,4 @@ -(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.WatsonSpeech = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o*/ var Stream; -(function (){try{ +(function (){ try{ Stream = require('st' + 'ream'); }catch(_){}finally{ if (!Stream) Stream = require('events').EventEmitter; -}}()) +} }()) /**/ var Buffer = require('buffer').Buffer; @@ -3520,12 +3520,12 @@ var internalUtil = { /**/ var Stream; -(function (){try{ +(function (){ try{ Stream = require('st' + 'ream'); }catch(_){}finally{ if (!Stream) Stream = require('events').EventEmitter; -}}()) +} }()) /**/ var Buffer = require('buffer').Buffer; @@ -3641,14 +3641,14 @@ WritableState.prototype.getBuffer = function writableStateGetBuffer() { return out; }; -(function (){try { +(function (){ try { Object.defineProperty(WritableState.prototype, 'buffer', { get: internalUtil.deprecate(function() { return this.getBuffer(); }, '_writableState.buffer is deprecated. Use _writableState.getBuffer ' + 'instead.') }); -}catch(_){}}()); +}catch(_){} }()); var Duplex; @@ -4976,7 +4976,7 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) { function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) { var name, str, desc; - desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] }; + desc = Object.getOwnPropertyDescriptor(value, key) || {value: value[key]}; if (desc.get) { if (desc.set) { str = ctx.stylize('[Getter/Setter]', 'special'); @@ -5325,22 +5325,22 @@ clone.clonePrototype = function clonePrototype(parent) { function __objToStr(o) { return Object.prototype.toString.call(o); -}; +} clone.__objToStr = __objToStr; function __isDate(o) { return typeof o === 'object' && __objToStr(o) === '[object Date]'; -}; +} clone.__isDate = __isDate; function __isArray(o) { return typeof o === 'object' && __objToStr(o) === '[object Array]'; -}; +} clone.__isArray = __isArray; function __isRegExp(o) { return typeof o === 'object' && __objToStr(o) === '[object RegExp]'; -}; +} clone.__isRegExp = __isRegExp; function __getRegExpFlags(re) { @@ -5349,7 +5349,7 @@ function __getRegExpFlags(re) { if (re.ignoreCase) flags += 'i'; if (re.multiline) flags += 'm'; return flags; -}; +} clone.__getRegExpFlags = __getRegExpFlags; return clone; @@ -5728,7 +5728,7 @@ var has = Object.prototype.hasOwnProperty; var toStr = Object.prototype.toString; var slice = Array.prototype.slice; var isArgs = require('./isArguments'); -var hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'); +var hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'); var hasProtoEnumBug = function () {}.propertyIsEnumerable('prototype'); var dontEnums = [ 'toString', @@ -5900,7 +5900,7 @@ var assignHasPendingExceptions = function () { } // Firefox 37 still has "pending exception" logic in its Object.assign implementation, // which is 72% slower than our shim, and Firefox 40's native implementation. - var thrower = Object.preventExtensions({ 1: 2 }); + var thrower = Object.preventExtensions({1: 2}); try { Object.assign(thrower, 'xy'); } catch (e) { diff --git a/examples/.eslintrc.js b/examples/.eslintrc.js new file mode 100644 index 00000000..291c45be --- /dev/null +++ b/examples/.eslintrc.js @@ -0,0 +1,10 @@ +module.exports = { + extends: '../.eslintrc.js', + "env": { + "browser": true, + "node": true + }, + rules: { + 'no-console': 0 + } +}; diff --git a/examples/stt-token.js b/examples/stt-token.js index 6e55c1c1..1bfab710 100644 --- a/examples/stt-token.js +++ b/examples/stt-token.js @@ -17,7 +17,7 @@ var sttConfig = extend({ }, vcapServices.getCredentials('speech_to_text')); // quick hack to make development easier -try { extend(sttConfig, require('../test/resources/stt-auth.json')) } catch (ex) {console.log(ex)} +try { extend(sttConfig, require('../test/resources/stt-auth.json')) } catch (ex) { console.log(ex) } var sttAuthService = watson.authorization(sttConfig); diff --git a/examples/tts-token.js b/examples/tts-token.js index e53f7a83..87e2f11e 100644 --- a/examples/tts-token.js +++ b/examples/tts-token.js @@ -16,9 +16,6 @@ var ttsConfig = extend({ password: process.env.TTS_PASSWORD || '' }, vcapServices.getCredentials('text_to_speech')); -// quick hack to make development easier -try { extend(ttsConfig, require('../test/resources/tts-auth.json')) } catch (ex) {} - var ttsAuthService = watson.authorization(ttsConfig); router.get('/token', function(req, res) { diff --git a/test/resources/karma.conf.js b/karma.conf.js similarity index 93% rename from test/resources/karma.conf.js rename to karma.conf.js index 21d5b452..d6ea67c9 100644 --- a/test/resources/karma.conf.js +++ b/karma.conf.js @@ -15,7 +15,7 @@ module.exports = function(config) { // list of files / patterns to load in the browser files: [ - '../*-spec.js' + 'test/*-spec.js' ], @@ -27,7 +27,7 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - '../*-spec.js': [ 'browserify' ] + 'test/*-spec.js': ['browserify'] }, browserify: { @@ -35,7 +35,7 @@ module.exports = function(config) { // 'brfs' makes fs.read* work // 'browserify-shim' wraps non-browserify modules // 'envify' makes process.env work - transform: [ 'envify' ] + transform: ['envify'] }, @@ -96,7 +96,7 @@ module.exports = function(config) { port: 9877, // this function takes express app object and allows you to modify it // to your liking. For more see http://expressjs.com/4x/api.html - appVisitor: (process.env.TEST_MODE === 'integration') ? require('./integration_test_server.js') : require('./offline_test_server.js') + appVisitor: (process.env.TEST_MODE === 'integration') ? require('./test/resources/integration_test_server.js') : require('./test/resources/offline_test_server.js') }, browserDisconnectTimeout: 15000, diff --git a/package.json b/package.json index f5fa6a70..7d297c40 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,9 @@ "description": "IBM Watson Speech to Text and Text to Speech SDK for web browsers.", "main": "index.js", "scripts": { - "watch-test": "karma start test/resources/karma.conf.js", - "lint": "eslint --config test/resources/.eslintrc.js *.js speech-to-text/*.js text-to-speech/*.js util/*.js", - "test": "karma start test/resources/karma.conf.js --single-run", - "test-integration": "TEST_MODE=integration karma start test/resources/karma.conf.js --single-run", + "watch-test": "karma start", + "test": "eslint . && karma start --single-run", + "test-integration": "TEST_MODE=integration karma start --single-run", "browserify": "browserify index.js --standalone WatsonSpeech --outfile dist/watson-speech.js --transform envify", "minify": "uglifyjs --compress --mangle --screw-ie8 dist/watson-speech.js --output dist/watson-speech.min.js --preamble \"// IBM Watson Speech JavaScript SDK\n// $npm_package_version\n// Generated at `date`\n// Copyright IBM ($npm_package_license)\n// $npm_package_homepage\"", "watchify": "watchify index.js --standalone WatsonSpeech --outfile dist/watson-speech.js --debug --verbose", @@ -20,8 +19,7 @@ "browserify": "^13.0.0", "concat-stream": "^1.5.1", "envify": "^3.4.0", - "eslint": "^1.10.3", - "eslint-config-google": "^0.3.0", + "eslint": "^2.4.0", "expect.js": "^0.3.1", "jquery": "^2.2.1", "jsdoc": "^3.4.0", @@ -40,7 +38,9 @@ "dependencies": { "clone": "^1.0.2", "defaults": "^1.0.3", + "eslint": "^2.4.0", "get-user-media-promise": "^1.0.0", + "karma-eslint": "^2.1.0", "microphone-stream": "^3.0.2", "object.assign": "^4.0.3", "object.pick": "^1.1.1", diff --git a/speech-to-text/recognize-microphone.js b/speech-to-text/recognize-microphone.js index 425af431..6fc9ddb5 100644 --- a/speech-to-text/recognize-microphone.js +++ b/speech-to-text/recognize-microphone.js @@ -79,8 +79,20 @@ module.exports = function recognizeMicrophone(options) { }); } + // set up the output first so that we have a place to emit errors + // if there's trouble with the input stream + var stream = recognizeStream; + if (options.format !== false) { + stream = stream.pipe(new FormatStream(options)); + stream.stop = recognizeStream.stop.bind(recognizeStream); + } + + if (options.outputElement) { + stream.pipe(new WritableElementStream(options)) + } + getMicStream.catch(function(err) { - console.log(err); + stream.emit('error', err); }); getMicStream.then(function(micStream) { @@ -110,16 +122,6 @@ module.exports = function recognizeMicrophone(options) { }).catch(recognizeStream.emit.bind(recognizeStream, 'error')); - var stream = recognizeStream; - if (options.format !== false) { - stream = stream.pipe(new FormatStream(options)); - stream.stop = recognizeStream.stop.bind(recognizeStream); - } - - if (options.outputElement) { - stream.pipe(new WritableElementStream(options)) - } - return stream; }; diff --git a/speech-to-text/recognize-stream.js b/speech-to-text/recognize-stream.js index a7b2b4c2..c2ff1cc9 100644 --- a/speech-to-text/recognize-stream.js +++ b/speech-to-text/recognize-stream.js @@ -142,6 +142,8 @@ function RecognizeStream(options) { }); // todo: is there a better way to put a stream in flowing mode? }); if (!options.silent) { + // todo: move this to the node.js wrapper + // eslint-disable-next-line no-console console.log(new Error('Watson Speech to Text RecognizeStream: the ' + event + ' event is deprecated and will be removed from a future release. ' + 'Please set {objectMode: true} and listen for the data event instead. ' + 'Pass {silent: true} to disable this message.')); @@ -338,7 +340,7 @@ RecognizeStream.prototype.sendData = function sendData(data) { return this.socket.send(data); }; -RecognizeStream.prototype._read = function (size) { +RecognizeStream.prototype._read = function (/*size*/) { // there's no easy way to control reads from the underlying library // so, the best we can do here is a no-op }; diff --git a/speech-to-text/timing-stream.js b/speech-to-text/timing-stream.js index 05aaf1d7..67dd6173 100644 --- a/speech-to-text/timing-stream.js +++ b/speech-to-text/timing-stream.js @@ -51,7 +51,7 @@ TimingStream.prototype._write = function(result, encoding, next) { next(); }; -TimingStream.prototype._read = function(size) { +TimingStream.prototype._read = function(/*size*/) { // ignore - we'll emit results once the time has come }; diff --git a/test/.eslintrc.js b/test/.eslintrc.js new file mode 100644 index 00000000..8c990661 --- /dev/null +++ b/test/.eslintrc.js @@ -0,0 +1,11 @@ +module.exports = { + extends: '../.eslintrc.js', + "env": { + "browser": true, + "node": true, + "mocha": true + }, + rules: { + 'no-console': 0 + } +}; diff --git a/test/end-to-end-spec.js b/test/end-to-end-spec.js index 2967ed0f..3b249b4f 100644 --- a/test/end-to-end-spec.js +++ b/test/end-to-end-spec.js @@ -4,7 +4,6 @@ var assert = require('assert'); var SpeechToText = require('../speech-to-text'); -var expect = require('expect.js'); var concat = require('concat-stream'); if (typeof fetch == "undefined") { diff --git a/test/format-stream-spec.js b/test/format-stream-spec.js index 3744b7d4..499cf56c 100644 --- a/test/format-stream-spec.js +++ b/test/format-stream-spec.js @@ -20,18 +20,18 @@ describe('FormatStream', function() { it('should format objects', function(done) { var stream = new FormatStream({objectMode: true}); - var source = { alternatives: - [ { + var source = {alternatives: + [{ confidence: 0.881, transcript: 'foo bar ', - final: true } ], - result_index: 0 }; - var expected = { alternatives: - [ { + final: true}], + result_index: 0}; + var expected = {alternatives: + [{ confidence: 0.881, transcript: 'Foo bar. ', - final: true } ], - result_index: 0 }; + final: true}], + result_index: 0}; stream.on('data', function(actual) { assert(actual, expected); done(); diff --git a/test/resources/.eslintrc.js b/test/resources/.eslintrc.js deleted file mode 100644 index 86d8c3f1..00000000 --- a/test/resources/.eslintrc.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = { - "extends": "google", - "env": { - "browser": true, - "node": true - }, - "rules": { - "max-len": 1, - "no-inline-comments": 0, - "no-multiple-empty-lines": 1, - "array-bracket-spacing": 1, - "comma-spacing": 1 - } -}; diff --git a/test/writable-element-stream-spec.js b/test/writable-element-stream-spec.js index aabf9037..68624efb 100644 --- a/test/writable-element-stream-spec.js +++ b/test/writable-element-stream-spec.js @@ -8,7 +8,7 @@ var $ = require('jquery'); describe('WritableElementStream', function() { - it ('should accept strings/buffers and write out contents when in string mode', function() { + it('should accept strings/buffers and write out contents when in string mode', function() { var el = document.createElement('div'); var s = new WritableElementStream({outputElement: el}); s.write('abc'); @@ -16,7 +16,7 @@ describe('WritableElementStream', function() { assert.equal(el.textContent, 'abc123'); }); - it ('should accept objects and write out contents when in object mode', function() { + it('should accept objects and write out contents when in object mode', function() { var el = document.createElement('div'); var s = new WritableElementStream({outputElement: el, objectMode: true}); s.write({final: true, alternatives: [{ @@ -28,7 +28,7 @@ describe('WritableElementStream', function() { assert.equal(el.textContent, 'abc123'); }); - it ('should write interim results', function() { + it('should write interim results', function() { var el = document.createElement('div'); var s = new WritableElementStream({outputElement: el, objectMode: true}); s.write({final: true, alternatives: [{ @@ -40,7 +40,7 @@ describe('WritableElementStream', function() { assert.equal(el.textContent, 'abc123'); }); - it ('should overwrite interim results', function() { + it('should overwrite interim results', function() { var el = document.createElement('div'); var s = new WritableElementStream({outputElement: el, objectMode: true}); s.write({final: true, alternatives: [{ @@ -56,7 +56,7 @@ describe('WritableElementStream', function() { }); ['