'use strict';

const validate = require('./validate'),
    _ = require('lodash'),
    assert = require('assert'),
    schema = {
        properties: {
            event: {
                type: 'string',
                required: true,
                allowEmpty: false
            },
            onEvent: {
                required: true,
                allowEmpty: false,
                conform(onEvent) {
                    return _.isFunction(onEvent);
                },
                messages: {
                    conform: 'onEvent must be a function'
                },
            },
            middleware: {
                conform(middleware) {
                    return _.isFunction(middleware) || _.isArray(middleware);
                },
                messages: {
                    conform: 'middleware must be a function or an array of functions'
                },
                required: false,
                allowEmpty: true
            }
        }
    };

function toArray(arg) {
    return !_.isArray(arg) ? [arg] : arg;
}

class Socket {

    /**
     * @description Creates a new socket instance
     * @param {String} packageName - The name of the LabShare package that created the socket definition
     * @param {Object} options
     * @param {String} options.event - The event name
     * @param {String} options.onEvent - The main event handler
     * @param {Array.<Function>|Function} options.middleware - One or more connect-style middleware functions
     */
    constructor(packageName, options = {}) {
        assert.ok(_.isString(packageName), 'Socket: "packageName" is required');
        validate({object: options, packageName, type: 'socket', schema});

        _.extend(this, options);

        this.middleware = toArray(this.middleware);
    }
}

module.exports = Socket;
@@ -1423,16 +1423,17 @@
 nction%7D 
+%5B
 options.
@@ -1442,16 +1442,17 @@
 ddleware
+%5D
  - One o
@@ -1732,24 +1732,73 @@
  options);%0A%0A
+        this.middleware = this.middleware %7C%7C %5B%5D;%0A
         this
@@ -1870,8 +1870,9 @@
  Socket;
+%0A

Fix middleware: an undefined value was being assigned in the Socket constructor