Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alexa-ability

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alexa-ability - npm Package Compare versions

Comparing version 0.3.1 to 0.3.2

lib/defaultHandlers.js

160

lib/Ability.js
'use strict';
var _regenerator = require('babel-runtime/regenerator');
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 _regenerator2 = _interopRequireDefault(_regenerator);
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
Object.defineProperty(exports, "__esModule", {

@@ -24,5 +12,5 @@ value: true

var _co = require('co');
var _bluebird = require('bluebird');
var _co2 = _interopRequireDefault(_co);
var _bluebird2 = _interopRequireDefault(_bluebird);

@@ -43,4 +31,14 @@ var _debug = require('debug');

var _defaultHandlers = require('./defaultHandlers');
var _standardEvents = require('./standardEvents');
var e = _interopRequireWildcard(_standardEvents);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var uLog = (0, _debug2.default)('alexa-ability:ability:use');

@@ -53,100 +51,10 @@ var oLog = (0, _debug2.default)('alexa-ability:ability:on');

var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
(0, _classCallCheck3.default)(this, Ability);
this.handle = _co2.default.wrap(_regenerator2.default.mark(function handle(event) {
var req, type, handler, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, fn;
return _regenerator2.default.wrap(function handle$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
req = new _Request.Request(event);
type = (0, _getEventName.getEventName)(event);
handler = this._handlers[type];
_classCallCheck(this, Ability);
if (!handler) hLog('ao handler found for event: "' + type + '".');else hLog('handling event: ' + type);
hLog('executing ' + this._middleware.length + ' middleware.');
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 8;
_iterator = (0, _getIterator3.default)(this._middleware);
case 10:
if (_iteratorNormalCompletion = (_step = _iterator.next()).done) {
_context.next = 18;
break;
}
fn = _step.value;
hLog('executing middleware function: ' + (fn.name || '<unnamed function>'));
_context.next = 15;
return (0, _resolve.resolve)(fn, req);
case 15:
_iteratorNormalCompletion = true;
_context.next = 10;
break;
case 18:
_context.next = 24;
break;
case 20:
_context.prev = 20;
_context.t0 = _context['catch'](8);
_didIteratorError = true;
_iteratorError = _context.t0;
case 24:
_context.prev = 24;
_context.prev = 25;
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
case 27:
_context.prev = 27;
if (!_didIteratorError) {
_context.next = 30;
break;
}
throw _iteratorError;
case 30:
return _context.finish(27);
case 31:
return _context.finish(24);
case 32:
if (!handler) {
_context.next = 36;
break;
}
hLog('executing handler for event: ' + type);
_context.next = 36;
return (0, _resolve.resolve)(handler, req);
case 36:
return _context.abrupt('return', req);
case 37:
case 'end':
return _context.stop();
}
}
}, handle, this, [[8, 20, 24, 32], [25,, 27, 31]]);
}));
this._middleware = [];
this._handlers = {};
this._handlers = _extends({}, _defaultHandlers.handlers);
}
(0, _createClass3.default)(Ability, [{
_createClass(Ability, [{
key: 'use',

@@ -165,3 +73,3 @@ value: function use(fn) {

(0, _assert2.default)(typeof handler === 'function', 'Expected function for event handler');
if (this._handlers[event]) oLog('averwrote handler for event: ' + event);else oLog('added handler for event: ' + event);
if (this._handlers[event]) oLog('overwrote handler for event: ' + event);else oLog('added handler for event: ' + event);

@@ -171,4 +79,36 @@ this._handlers[event] = handler;

}
}, {
key: 'handle',
value: function handle(event) {
var type = (0, _getEventName.getEventName)(event);
var req = new _Request.Request(event);
// get possible handlers
var errHandler = this._handlers[e.error];
var defHandler = this._handlers[e.unhandledEvent];
var handler = this._handlers[type] || defHandler;
// log
if (this._handlers[type]) hLog('handling event: ' + type);else hLog('no handler found for event: "' + type + '".');
// 1. run middleware
// 2. run handler
// 3. if error, try to catch
// 4. if success, resolve req
return _bluebird2.default.resolve(this._middleware).tap(function (fns) {
return hLog('executing ' + fns.length + ' middleware.');
}).each(function (fn) {
hLog('executing middleware function: ' + (fn.name || '<unnamed function>'));
return (0, _resolve.resolve)(fn, req);
}).tap(function () {
return hLog('executing handler for event: ' + type);
}).then(function () {
return (0, _resolve.resolve)(handler, req);
}).catch(function (err) {
return (0, _resolve.resolve)(errHandler, err, req);
}).return(req);
}
}]);
return Ability;
}();

@@ -16,2 +16,8 @@ 'use strict';

var _standardEvents = require('./standardEvents');
var e = _interopRequireWildcard(_standardEvents);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -27,3 +33,3 @@

case "LaunchRequest":
return "launch";
return e.launch;

@@ -34,7 +40,7 @@ case "IntentRequest":

case "SessionEndedRequest":
return "end";
return e.end;
default:
return "unhandledIntent";
return e.unknownEvent;
}
}

@@ -6,3 +6,3 @@ 'use strict';

});
exports.intent = exports.Ability = undefined;
exports.events = exports.Ability = undefined;

@@ -18,8 +18,8 @@ var _Ability = require('./Ability');

var _amazonIntents = require('./amazon-intents');
var _standardEvents = require('./standardEvents');
var _intent = _interopRequireWildcard(_amazonIntents);
var _events = _interopRequireWildcard(_standardEvents);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
exports.intent = _intent;
exports.events = _events;
'use strict';
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
Object.defineProperty(exports, "__esModule", {

@@ -26,5 +20,7 @@ value: true

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Request = exports.Request = function () {
function Request(event) {
(0, _classCallCheck3.default)(this, Request);
_classCallCheck(this, Request);

@@ -38,3 +34,3 @@ this._event = event;

(0, _createClass3.default)(Request, [{
_createClass(Request, [{
key: 'say',

@@ -92,3 +88,4 @@ value: function say(text) {

}]);
return Request;
}();

@@ -1,7 +0,3 @@

"use strict";
'use strict';
var _promise = require("babel-runtime/core-js/promise");
var _promise2 = _interopRequireDefault(_promise);
Object.defineProperty(exports, "__esModule", {

@@ -12,2 +8,6 @@ value: true

var _bluebird = require('bluebird');
var _bluebird2 = _interopRequireDefault(_bluebird);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -20,5 +20,7 @@

if (fn.length <= args.length) return _promise2.default.resolve(fn.apply(undefined, args));
if (fn.length <= args.length) return _bluebird2.default.try(function () {
return fn.apply(undefined, args);
});
return new _promise2.default(function (res, rej) {
return new _bluebird2.default(function (res, rej) {
fn.apply(undefined, args.concat([function (err, val) {

@@ -25,0 +27,0 @@ return err ? rej(err) : res(val);

{
"name": "alexa-ability",
"version": "0.3.1",
"version": "0.3.2",
"description": "",

@@ -21,3 +21,3 @@ "main": "lib/index.js",

"dependencies": {
"co": "^4.6.0",
"bluebird": "^3.1.5",
"debug": "^2.2.0",

@@ -29,10 +29,7 @@ "lodash": "^4.0.1"

"babel-cli": "^6.4.5",
"babel-plugin-transform-runtime": "^6.4.3",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-1": "^6.3.13",
"babel-register": "^6.4.3",
"babel-runtime": "^6.3.19",
"chai": "^3.4.1",
"chai-as-promised": "^5.2.0",
"es6-promise": "^3.0.2",
"mocha": "^2.3.4",

@@ -39,0 +36,0 @@ "sinon": "^1.17.2",

@@ -0,6 +1,8 @@

import noop from 'lodash/noop';
import { Ability } from '../src/Ability';
import { Request } from '../src/Request';
import noop from 'lodash/noop';
import * as e from '../src/standardEvents';
const launchRequest = require('./fixtures/launch-request');
const unknownRequest = require('./fixtures/unknown-request');

@@ -56,3 +58,34 @@ describe('Ability', function() {

});
it('should attempt the error handler when middleware fails', function() {
const err = new Error();
const spy = sinon.spy();
app.use(function(){ throw err });
app.on(e.error, spy);
return app.handle(launchRequest)
.then(() => expect(spy).to.have.been.called);
});
it('should attempt the error handler when the handler fails', function() {
const err = new Error();
const spy = sinon.spy();
app.on(e.launch, function(){ throw err });
app.on(e.error, spy);
return app.handle(launchRequest)
.then(() => expect(spy).to.have.been.called);
});
it('should use the "unknownEvent" handler for unknown events', function() {
const spy = sinon.spy();
app.on(e.unknownEvent, spy);
return app.handle(unknownRequest)
.then(() => expect(spy).to.have.been.called);
});
});
});
import { getEventName } from '../src/getEventName';
import * as e from '../src/standardEvents';

@@ -12,3 +13,3 @@ const launchRequest = require('./fixtures/launch-request');

const type = getEventName(launchRequest);
expect(type).to.equal("launch");
expect(type).to.equal(e.launch);
});

@@ -23,9 +24,9 @@

const type = getEventName(sessionEndedRequest);
expect(type).to.equal("end");
expect(type).to.equal(e.end);
});
it('should return "unhandledIntent" for an unknown event', function() {
it('should return "unknownEvent" for an unknown event', function() {
const type = getEventName(unknownRequest);
expect(type).to.equal("unhandledIntent");
expect(type).to.equal(e.unknownEvent);
});
});

@@ -26,3 +26,3 @@ import { resolve } from '../src/resolve';

expect(result).to.be.instanceOf(Promise);
return result.should.be.rejectedWith(err);
return result.should.be.rejected;
})

@@ -29,0 +29,0 @@

@@ -0,1 +1,2 @@

import Promise from 'bluebird';
import chai from 'chai';

@@ -5,7 +6,4 @@ import sinon from 'sinon';

import sinonChai from "sinon-chai";
import { Promise } from 'es6-promise';
if (!global.Promise) { // shim Promise
global.Promise = Promise;
}
global.Promise = Promise;

@@ -12,0 +10,0 @@ chai.should();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc