This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
angular-sails-io.js
411 lines (309 loc) · 11.5 KB
/
angular-sails-io.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
'use strict';
/**
* @ngdoc module
* @name sails.io
* @file angularSails.io.js
*
* @description
*
* This file allows you to send and receive socket.io messages to & from Sails
* by simulating a REST client interface on top of socket.io.
*
* It models its API after the $http pattern from Angular and returns $q promises.
*
* So if you're switching from using AJAX to sockets, instead of:
* `$http.post( url, [data]).then( successHandler, errorHandler )`
*
* You would use:
* `socket.post( url, [data]).then( successHandler, errorHandler )`
*
* It also supports $http-style success / error callbacks:
* ` socket.get( url, [params] )
* .success(function(results){})
* .error(function(results){}) `
*
*
* For more information, visit:
* http://github.com/balderdashy/angularSails
*/
angular.module('angularSails.io', [])
/**
* @ngdoc constant
* @name sails.io.$$sailsConnectionMetaData
*
* @description
*
* //TODO
*
*/
.constant('$$sailsConnectionMetaData', {
version: '__sails_io_sdk_version',
platform: '__sails_io_sdk_platform',
language: '__sails_io_sdk_language'
})
/**
* @ngdoc constant
* @name sails.io.$SAILS_SDK_PARAMS
*
* @description
*
* //TODO
*
*/
.constant('$$sailsSDKParams', {
version: '0.11.0', // TODO: pull this automatically from package.json during build.
platform: typeof module === 'undefined' ? 'browser' : 'node',
language: 'javascript',
flavor: 'angular'
})
/**
* @ngdoc factory
* @name sails.io.$SAILS_SDK_INFO
*
* @description
*
* //TODO
*
*/
.factory('$$sailsSDKInfo', ['$$sailsConnectionMetaData', '$$sailsSDKParams', function ($$sailsConnectionMetaData, $$sailsSDKParams) {
function _getVersionString() {
var versionString = $$sailsConnectionMetaData.version + '=' +
$$sailsSDKParams.version + '&' + $$sailsConnectionMetaData.platform + '=' +
$$sailsSDKParams.platform + '&' + $$sailsConnectionMetaData.language + '=' +
$$sailsSDKParams.language;
return versionString;
}
return {
getVersionString: _getVersionString
};
}])
.factory('SocketIo', ['$window', function ($window) {
if (!$window.io) {
throw new Error('Socket IO Not Found!');
}
return $window.io;
}])
/**
* @ngdoc provider
* @name sails.io.$sailsSocket
*
* @description
*
* This provider returns a service that allows you to create a connection to a Sails server via socket.io.
*
*
*
* Code from :
*
* - angular-socket-io by Brian Ford : https://github.com/btford/angular-socket-io
*
* (c) 2014 Brian Ford http://briantford.com
* License: MIT
*
* sails.io.js by Balderdashy : https://github.com/balderdashy/sails
*/
.provider('$sailsSocket', function () {
var _socketDefaults = {
autoConnect: true,
environment: 'development',
baseUrl: 'http://localhost:1337'
};
var SailsSocketFactory = function ($q, $timeout, SocketIo, $$sailsSDKInfo) {
var versionString = $$sailsSDKInfo.getVersionString();
var tick = function (socket, callback) {
return callback ? function () {
var args = arguments;
$timeout(function () {
callback.apply(socket, args);
}, 0);
} : angular.noop;
};
/**
* TmpSocket
*
* A mock Socket used for binding events before the real thing
* has been instantiated (since we need to use io.connect() to
* instantiate the real thing, which would kick off the connection
* process w/ the server, and we don't necessarily have the valid
* configuration to know WHICH SERVER to talk to yet.)
*
* @api private
* @constructor
*/
function TmpSocket() {
var boundEvents = {};
this.socket = {connected: false};
this.on = function (evName, fn) {
boundEvents[evName] = fn;
return this;
};
this.become = function (actualSocket) {
for (var eventName in boundEvents) {
actualSocket.on(eventName, boundEvents[eventName]);
// boundEvents[eventName] = angular.noop;
}
return actualSocket;
};
}
/**
* @ngDoc function
* @name angularSails.io.SailsResponse
*
* @description
*
* Transforms a raw sails response into a $http-like responseObject
*
* @param requestContext
* @param responseContext
* @constructor
*/
function SailsResponse(requestContext, responseContext) {
if(angular.isString(responseContext)){
responseContext = angular.fromJson(responseContext);
}
this.data = responseContext.body || {};
this.headers = responseContext.headers || {};
this.status = responseContext.statusCode || 200;
this.config = requestContext;
}
function _sendRequest(socket, requestCtx) {
// Since callback is embedded in requestCtx,
// retrieve it and delete the key before continuing.
var response = requestCtx.response;
delete requestCtx.response;
// Name of socket request listener on the server
// ( === the request method, e.g. 'get', 'post', 'put', etc. )
var sailsEndpoint = requestCtx.method;
socket.emit(sailsEndpoint, requestCtx, tick(socket, function serverResponded(responseCtx) {
console.log(responseCtx)
var serverResponse = new SailsResponse(requestCtx, responseCtx);
if (serverResponse.status >= 400) {
response.reject(serverResponse);
}
else {
response.resolve(serverResponse);
}
}));
}
/**
* @ngDoc function
* @name angularSails.io.SailsSocket
*
* @param options
* @constructor
*/
var SailsSocket = function (options) {
var self = this;
self._requestQueue = []
self._socketOptions = options || {}
self._socket = new TmpSocket();
};
SailsSocket.prototype.connect = function (url, opts) {
var connection = $q.defer();
var self = this;
opts = opts || {};
// If explicit connection url is specified, use it
url = url || self._socketOptions.baseUrl || undefined;
// Mix the current SDK version into the query string in
// the connection request to the server:
if (typeof opts.query !== 'string') opts.query = versionString;
else opts.query += '&' + versionString;
var socket = SocketIo.connect(url, opts);
self._socket = self._socket.become(socket);
self._socket.on('connect', function () {
angular.forEach(self._requestQueue, function (queuedRequest,idx) {
_sendRequest(self._socket, queuedRequest);
})
})
self._socket.once('connect', connection.resolve);
self._socket.on('connecting', connection.notify);
self._socket.once('connect_failed', connection.reject);
return connection.promise;
}
SailsSocket.prototype.isConnected = function () {
return this._socket.socket.connected;
}
SailsSocket.prototype.get = function (url, data) {
return this._request({
method: 'get',
url: url,
data: data
});
};
SailsSocket.prototype.post = function (url, data) {
return this._request({
method: 'post',
url: url,
data: data
});
};
SailsSocket.prototype.post = function (url, data) {
return this._request({
method: 'post',
url: url,
data: data
});
};
SailsSocket.prototype['delete'] = function (url, data) {
return this._request({
method: 'delete',
url: url,
data: data
});
};
SailsSocket.prototype._request = function (options) {
var response = $q.defer()
var usage = 'Usage:\n socket.' +
(options.method || 'request') +
'( destinationURL, [dataToSend] )';
options = options || {};
options.data = options.data || {};
options.headers = options.headers || {};
// Remove trailing slashes and spaces to make packets smaller.
options.url = options.url.replace(/^(.+)\/*\s*$/, '$1');
if (typeof options.url !== 'string') {
throw new Error('Invalid or missing URL!\n' + usage);
}
var self = this;
// Build a simulated request object.
var request = {
method: options.method,
data: options.data,
url: options.url,
headers: options.headers,
response: response
};
response.promise.success = function (fn) {
response.promise.then(function (response) {
console.log(response)
fn(response.data, response.statusCode, response.headers, request);
});
return response.promise;
};
response.promise.error = function (fn) {
response.promise.then(null, function (response) {
fn(response.data, response.statusCode, response.headers, request);
});
return response.promise;
};
if (self._socket && self.isConnected()) {
_sendRequest(self._socket, request);
}
else {
self._requestQueue.push(request);
}
return response.promise;
};
SailsSocket.prototype.on = function (eventName, callback) {
var self = this;
self._socket.on(eventName, tick(self._socket, callback));
}
return function (options) {
var sailSocket = new SailsSocket(options);
return sailSocket;
}
};
return {
'$get': ['$q', '$timeout', 'SocketIo', '$$sailsSDKInfo', SailsSocketFactory ]
}
});