Small refactoring

This commit is contained in:
Gustaf Räntilä
2017-09-20 13:30:50 +02:00
parent b295c2f10a
commit 2e17393fca
+11 -17
View File
@@ -1,18 +1,12 @@
'use strict';
var coreApi = require( './google-u2f-api' );
module.exports = API;
var _global =
typeof window !== 'undefined'
? window
: typeof global !== 'undefined'
? global
: { };
var hasNativeSupport = _global.u2f && typeof _global.u2f.sign === 'function';
var _u2f = hasNativeSupport ? _global.u2f : coreApi;
var hasNativeSupport =
( typeof window.u2f !== 'undefined' ) &&
( typeof window.u2f.sign === 'function' );
var _u2f = hasNativeSupport ? window.u2f : null;
var chromeApi = hasNativeSupport ? null : require( './google-u2f-api' );
function API( Promise )
{
@@ -32,7 +26,7 @@ var errorMap = {
'-1': 'CANCELED'
};
var backendErrorCodes = hasNativeSupport ? _global.u2f : coreApi.ErrorCodes;
var backendErrorCodes = hasNativeSupport ? _u2f : chromeApi.ErrorCodes;
(
hasNativeSupport
? [
@@ -43,7 +37,7 @@ var backendErrorCodes = hasNativeSupport ? _global.u2f : coreApi.ErrorCodes;
'OTHER_ERROR',
'TIMEOUT',
]
: Object.keys( coreApi.ErrorCodes )
: Object.keys( chromeApi.ErrorCodes )
)
.forEach( function( key ) {
API.ErrorCodes[ key ] = backendErrorCodes[ key ];
@@ -85,7 +79,7 @@ function defer( Promise, fun )
ret.promise.cancel = function( msg, disconnect )
{
if ( disconnect && !hasNativeSupport )
coreApi.disconnect( );
chromeApi.disconnect( );
ret.reject( makeError( msg, { errorCode: -1 } ) );
};
@@ -114,7 +108,7 @@ function isSupported( ignorePreconditions /* = false */ )
return Promise.resolve( false );
}
return defer( Promise, coreApi.isSupported ).promise;
return defer( Promise, chromeApi.isSupported ).promise;
}
function ensureSupport( )
@@ -178,7 +172,7 @@ function register( registerRequests, signRequests /* = null */, timeout )
else
resolve( response );
}
coreApi.register( registerRequests, signRequests, cb, timeout );
chromeApi.register( registerRequests, signRequests, cb, timeout );
}
} ).promise;
}
@@ -221,7 +215,7 @@ function sign( signRequests, timeout )
resolve( response );
}
coreApi.sign( signRequests, cb, timeout );
chromeApi.sign( signRequests, cb, timeout );
}
} ).promise;
}