3 * Copyright 2016 RIFT.IO Inc
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 //Login needs to be refactored. Too many cross dependencies
19 var AuthActions
= require('../widgets/login/loginAuthActions.js');
20 var $ = require('jquery');
21 var rw
= require('utils/rw.js');
22 var API_SERVER
= rw
.getSearchParams(window
.location
).api_server
;
24 var SockJS
= require('sockjs-client');
28 Utils
.DescriptorModelMeta
= null;
30 var INACTIVITY_TIMEOUT
= 600000;
32 Utils
.getInactivityTimeout = function() {
33 return new Promise(function(resolve
, reject
) {
35 url
: '/inactivity-timeout',
37 success: function(data
) {
40 error: function(error
) {
41 console
.log("There was an error getting the inactivity-timeout: ", error
);
44 }).fail(function(xhr
) {
45 console
.log('There was an xhr error getting the inactivity-timeout', xhr
);
51 Utils
.isMultiplexerLoaded = function() {
52 if (window
.multiplexer
) {
58 Utils
.setupMultiplexClient = function() {
59 var sockjs_url
= '/multiplex';
61 var sockjs
= new SockJS(sockjs_url
);
63 var loadChecker = function() {
65 window
.multiplexer
= new WebSocketMultiplex(sockjs
);
66 console
.log('WebSocketMultiplex loaded');
68 // caught an error, retry in someTime
69 console
.log('WebSocketMultiplex not loaded yet. will try again in 1 second:', e
);
70 setTimeout(function() {
78 Utils
.checkAndResolveSocketRequest = function(data
, resolve
, reject
) {
79 const checker
= () => {
80 if (!Utils
.isMultiplexerLoaded()) {
92 Utils
.bootstrapApplication = function() {
94 return new Promise(function(resolve
, reject
) {
95 Promise
.all([self
.getInactivityTimeout()]).then(function(results
) {
96 INACTIVITY_TIMEOUT
= results
[0]['inactivity-timeout'];
99 console
.log("Error bootstrapping application ", error
);
105 Utils
.getDescriptorModelMeta = function() {
106 return new Promise(function(resolve
, reject
) {
107 if (!Utils
.DescriptorModelMeta
) {
109 url
: '/descriptor-model-meta?api_server=' + API_SERVER
,
111 beforeSend
: Utils
.addAuthorizationStub
,
112 success: function(data
) {
113 Utils
.DescriptorModelMeta
= data
;
114 Utils
.DescriptorModelMetaLoaded
= true;
117 error: function(error
) {
118 console
.log("There was an error getting the schema: ", error
);
121 }).fail(function(xhr
) {
122 console
.log("There was an error getting the schema: ", xhr
);
123 Utils
.checkAuthentication(xhr
.status
);
126 resolve(Utils
.DescriptorModelMeta
);
131 Utils
.addAuthorizationStub = function(xhr
) {
132 var Auth
= window
.sessionStorage
.getItem("auth");
133 xhr
.setRequestHeader('Authorization', 'Basic ' + Auth
);
136 Utils
.getByteDataWithUnitPrefix = function(number
, precision
) {
137 var toPrecision
= precision
|| 3;
138 if (number
< Math
.pow(10, 3)) {
139 return [number
, 'B'];
140 } else if (number
< Math
.pow(10, 6)) {
141 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KB'];
142 } else if (number
< Math
.pow(10, 9)) {
143 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MB'];
144 } else if (number
< Math
.pow(10, 12)) {
145 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GB'];
146 } else if (number
< Math
.pow(10, 15)) {
147 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TB'];
148 } else if (number
< Math
.pow(10, 18)) {
149 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PB'];
150 } else if (number
< Math
.pow(10, 21)) {
151 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EB'];
152 } else if (number
< Math
.pow(10, 24)) {
153 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZB'];
154 } else if (number
< Math
.pow(10, 27)) {
155 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZB'];
157 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YB'];
161 Utils
.getPacketDataWithUnitPrefix = function(number
, precision
) {
162 var toPrecision
= precision
|| 3;
163 if (number
< Math
.pow(10, 3)) {
164 return [number
, 'P'];
165 } else if (number
< Math
.pow(10, 6)) {
166 return [(number
/ Math
.pow(10, 3)).toPrecision(toPrecision
), 'KP'];
167 } else if (number
< Math
.pow(10, 9)) {
168 return [(number
/ Math
.pow(10, 6)).toPrecision(toPrecision
), 'MP'];
169 } else if (number
< Math
.pow(10, 12)) {
170 return [(number
/ Math
.pow(10, 9)).toPrecision(toPrecision
), 'GP'];
171 } else if (number
< Math
.pow(10, 15)) {
172 return [(number
/ Math
.pow(10, 12)).toPrecision(toPrecision
), 'TP'];
173 } else if (number
< Math
.pow(10, 18)) {
174 return [(number
/ Math
.pow(10, 15)).toPrecision(toPrecision
), 'PP'];
175 } else if (number
< Math
.pow(10, 21)) {
176 return [(number
/ Math
.pow(10, 18)).toPrecision(toPrecision
), 'EP'];
177 } else if (number
< Math
.pow(10, 24)) {
178 return [(number
/ Math
.pow(10, 21)).toPrecision(toPrecision
), 'ZP'];
179 } else if (number
< Math
.pow(10, 27)) {
180 return [(number
/ Math
.pow(10, 24)).toPrecision(toPrecision
), 'ZP'];
182 return [(number
/ Math
.pow(10, 27)).toPrecision(toPrecision
), 'YP'];
185 Utils
.loginHash
= "#/login";
186 Utils
.setAuthentication = function(username
, password
, cb
) {
188 var AuthBase64
= btoa(username
+ ":" + password
);
189 window
.sessionStorage
.setItem("auth", AuthBase64
);
190 self
.detectInactivity();
195 Utils
.clearAuthentication = function(callback
) {
197 window
.sessionStorage
.removeItem("auth");
198 AuthActions
.notAuthenticated();
199 window
.sessionStorage
.setItem("locationRefHash", window
.location
.hash
);
203 window
.location
.hash
= Utils
.loginHash
;
206 Utils
.isNotAuthenticated = function(windowLocation
, callback
) {
208 self
.detectInactivity();
209 if (!window
.sessionStorage
.getItem("auth")) {
210 Utils
.clearAuthentication();
213 Utils
.isDetecting
= false;
214 Utils
.detectInactivity = function(callback
, duration
) {
216 if (!self
.isDetecting
) {
217 var cb = function() {
218 self
.clearAuthentication();
224 var timeout
= duration
|| INACTIVITY_TIMEOUT
;
225 var setInactive = function() {
226 isInactive
= setTimeout(cb
, timeout
);
228 var reset = function() {
229 clearTimeout(isInactive
);
233 window
.addEventListener('mousemove', reset
);
234 window
.addEventListener("keypress", reset
);
235 self
.isDetecting
= true;
238 Utils
.checkAuthentication = function(statusCode
, cb
) {
240 if (statusCode
== 401) {
244 window
.sessionStorage
.removeItem("auth")
245 self
.isNotAuthenticated(window
.location
)
251 Utils
.isAuthenticationCached = function() {
253 if (window
.sessionStorage
.getItem("auth")) {
259 Utils
.getHostNameFromURL = function(url
) {
260 var match
= url
.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)([^?#]*)(\?[^#]*|)(#.*|)$/);
261 return match
&& match
[3];
264 Utils
.webSocketProtocol = function() {
268 if (window
.location
.protocol
== 'http:') {
271 this.wsProto
= 'wss:'
277 Utils
.arrayIntersperse
= (arr
, sep
) => {
278 if (arr
.length
=== 0) {
282 return arr
.slice(1).reduce((xs
, x
, i
) => {
283 return xs
.concat([sep
, x
]);
287 module
.exports
= Utils
;