UNPKG

3.65 kBJavaScriptView Raw
1var AuthSocket, HuntingWebSocket, config, getSocketType, guid, json, storage,
2 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
3
4json = require('./util').json;
5
6storage = window.localStorage;
7
8guid = require('./guid');
9
10config = require('./config');
11
12HuntingWebSocket = require('./hunting-websocket');
13
14getSocketType = function(urls) {
15 var protocol;
16 protocol = /(\w+)/.exec(urls[0])[1];
17 if (protocol.indexOf('http') === 0) {
18 console.log("Using http!");
19 return 'http';
20 }
21 console.log("Using websocket!");
22 return 'ws';
23};
24
25AuthSocket = (function() {
26 function AuthSocket(opts) {
27 this.authenticateSocket = __bind(this.authenticateSocket, this);
28 this.send = __bind(this.send, this);
29 this.destinationUrls = opts.urls;
30 this.authSocket = new HuntingWebSocket(opts.authServers || config.authServers);
31 this.socketType = getSocketType(opts.urls);
32 this.events = {};
33 this.appMapping = opts.appName;
34 this.callbacks = {};
35 this.authSocket.onclose = (function(_this) {
36 return function(evt) {
37 return _this.onclose(evt);
38 };
39 })(this);
40 this.authSocket.onreconnect = (function(_this) {
41 return function(evt) {
42 return _this.authenticateSocket();
43 };
44 })(this);
45 this.authSocket.onopen = (function(_this) {
46 return function(evt) {
47 _this.authenticateSocket();
48 return _this.onopen(evt);
49 };
50 })(this);
51 this.authSocket.onerror = (function(_this) {
52 return function(err) {
53 console.log("Error connecting to auth server");
54 return _this.onerror(err);
55 };
56 })(this);
57 this.authSocket.onmessage = (function(_this) {
58 return function(message) {
59 var msg;
60 try {
61 msg = JSON.parse(message.data);
62 if ((msg != null ? msg.destinationError : void 0) && _this.events.error) {
63 console.log("Error connecting to destination socket");
64 return _this.events.error(msg.destinationError);
65 } else {
66 if (msg.httpCorrelationId && _this.callbacks[msg.httpCorrelationId]) {
67 _this.callbacks[msg.httpCorrelationId](msg.message || msg);
68 return delete _this.callbacks[msg.httpCorrelationId];
69 } else {
70 return _this.onmessage(message);
71 }
72 }
73 } catch (_error) {
74 return _this.onmessage(message);
75 }
76 };
77 })(this);
78 }
79
80 AuthSocket.prototype.send = function(message) {
81 var request;
82 request = {
83 destinationUrls: this.destinationUrls,
84 appMapping: this.appMapping
85 };
86 if (message.callback) {
87 request.httpCorrelationId = guid();
88 this.callbacks[request.httpCorrelationId] = message.callback;
89 }
90 request[this.socketType] = message;
91 return this.authSocket.send(json(request));
92 };
93
94 AuthSocket.prototype.close = function() {
95 return this.authSocket.close();
96 };
97
98 AuthSocket.prototype.authenticateSocket = function() {
99 var request, _ref;
100 request = {
101 checkAuth: {
102 appMapping: this.appMapping,
103 token: (_ref = JSON.parse(storage.getItem("aa-token-" + this.appMapping))) != null ? _ref.token : void 0,
104 destinationUrls: this.destinationUrls,
105 openSocket: this.socketType === "ws"
106 }
107 };
108 return this.authSocket.send(json(request));
109 };
110
111 AuthSocket.prototype.onopen = function(event) {};
112
113 AuthSocket.prototype.onclose = function(event) {};
114
115 AuthSocket.prototype.onmessage = function(event) {};
116
117 AuthSocket.prototype.onerror = function(event) {};
118
119 return AuthSocket;
120
121})();
122
123module.exports = AuthSocket;