Fixed travis

- Tests against Node 4, 8, 10, 12 and newest

Modernized:
- Use arrow functions
- Use string templates in examples and some code
- Use single quote for strings
This commit is contained in:
Fabian Arndt
2019-10-14 01:46:30 +02:00
parent 93e6b59710
commit 8f2b55a7aa
12 changed files with 159 additions and 166 deletions

View File

@ -5,7 +5,7 @@ function UnauthorizedError (code, error) {
this.data = {
message: this.message,
code: code,
type: "UnauthorizedError"
type: 'UnauthorizedError'
};
}

View File

@ -6,7 +6,7 @@ function noQsMethod (options) {
const defaults = { required: true };
options = xtend(defaults, options);
return function (socket) {
return (socket) => {
const server = this.server || socket.server;
if (!server.$emit) {
@ -19,18 +19,18 @@ function noQsMethod (options) {
let auth_timeout = null;
if (options.required) {
auth_timeout = setTimeout(function () {
auth_timeout = setTimeout(() => {
socket.disconnect('unauthorized');
}, options.timeout || 5000);
}
socket.on('authenticate', function (data) {
socket.on('authenticate', (data) => {
if (options.required) {
clearTimeout(auth_timeout);
}
// error handler
const onError = function (err, code) {
const onError = (err, code) => {
if (err) {
code = code || 'unknown';
const error = new UnauthorizedError(code, {
@ -46,12 +46,13 @@ function noQsMethod (options) {
options.callback = Math.abs(options.callback);
}
}
callback_timeout = setTimeout(function () {
callback_timeout = setTimeout(() => {
socket.disconnect('unauthorized');
}, (options.callback === false ? 0 : options.callback));
}
socket.emit('unauthorized', error, function() {
socket.emit('unauthorized', error, () => {
if (typeof options.callback === 'number') {
clearTimeout(callback_timeout);
}
@ -63,20 +64,20 @@ function noQsMethod (options) {
const token = options.cookie ? socket.request.cookies[options.cookie] : (data ? data.token : undefined);
if (!token || typeof token !== "string") {
if (!token || typeof token !== 'string') {
return onError({ message: 'invalid token datatype' }, 'invalid_token');
}
// Store encoded JWT
socket[options.encodedPropertyName] = token;
const onJwtVerificationReady = function (err, decoded) {
const onJwtVerificationReady = (err, decoded) => {
if (err) {
return onError(err, 'invalid_token');
}
// success handler
const onSuccess = function () {
const onSuccess = () => {
socket[options.decodedPropertyName] = decoded;
socket.emit('authenticated');
if (server.$emit) {
@ -99,7 +100,7 @@ function noQsMethod (options) {
}
};
const onSecretReady = function (err, secret) {
const onSecretReady = (err, secret) => {
if (err || !secret) {
return onError(err, 'invalid_secret');
}
@ -116,7 +117,7 @@ function authorize (options, onConnection) {
options = xtend({ decodedPropertyName: 'decoded_token', encodedPropertyName: 'encoded_token' }, options);
if (typeof options.secret !== 'string' && typeof options.secret !== 'function') {
throw new Error('Provided secret "' + options.secret + '" is invalid, must be of type string or function.');
throw new Error(`Provided secret ${options.secret} is invalid, must be of type string or function.`);
}
if (!options.handshake) {
@ -124,14 +125,14 @@ function authorize (options, onConnection) {
}
const defaults = {
success: function (socket, accept) {
success: (socket, accept) => {
if (socket.request) {
accept();
} else {
accept(null, true);
}
},
fail: function (error, socket, accept) {
fail: (error, socket, accept) => {
if (socket.request) {
accept(error);
} else {
@ -142,7 +143,7 @@ function authorize (options, onConnection) {
const auth = xtend(defaults, options);
return function (socket, accept) {
return (socket, accept) => {
let token, error;
const handshake = socket.handshake;
const req = socket.request || socket;
@ -193,7 +194,7 @@ function authorize (options, onConnection) {
// Store encoded JWT
socket[options.encodedPropertyName] = token;
const onJwtVerificationReady = function (err, decoded) {
const onJwtVerificationReady = (err, decoded) => {
if (err) {
error = new UnauthorizedError(err.code || 'invalid_token', err);
return auth.fail(error, socket, accept);
@ -204,7 +205,7 @@ function authorize (options, onConnection) {
return auth.success(socket, accept);
};
const onSecretReady = function (err, secret) {
const onSecretReady = (err, secret) => {
if (err) {
error = new UnauthorizedError(err.code || 'invalid_secret', err);
return auth.fail(error, socket, accept);