-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathlifecycle.js
153 lines (117 loc) · 3.66 KB
/
lifecycle.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
/**
* Module dependencies
*/
var Sails = require('sails/lib/app');
var SHSockets = require('sails-hook-sockets');
var _ = require('lodash');
// Use a weird port to avoid tests failing if we
// forget to shut down another Sails app
var TEST_SERVER_PORT = 1577;
/**
* @type {Object}
*/
module.exports = {
setup: function (opts, cb) {
// Invalidate socket.io-client in require cache
_.each(_.keys(require.cache), function (modulePath) {
if (modulePath.match(/socket.io-client/)){
delete require.cache[modulePath];
}
});
// Require socket.io-client and sails.io.js fresh
var io = require('socket.io-client');
var sailsIO = require('../../sails.io.js');
if (_.isFunction(opts)) {
cb = opts;
opts = {};
}
// New up an instance of Sails
// and lift it.
var app = Sails();
app.lift({
log: { level: 'error' },
globals: {
sails: true,
_: false,
async: false,
models: false
},
port: TEST_SERVER_PORT,
sockets: {
transports: opts.transports,
path: opts.path,
beforeConnect: opts.beforeConnect || false
},
hooks: {
grunt: false,
sockets: SHSockets
},
routes: _.extend({
'/sails.io.js': function(req, res) {
res.header('Content-type', 'application/javascript');
require('fs').createReadStream(require('path').resolve(__dirname, '..', '..', 'dist', 'sails.io.js')).pipe(res);
}
}, opts.routes || {})
},function (err) {
if (err) { return cb(err); }
// console.log('lifted');
// Instantiate socket client.
io = sailsIO(io);
// Set some options.
io.sails.url = opts.url || 'http://localhost:'+TEST_SERVER_PORT;
// Disable the sails.io.js client's logger
io.sails.environment = opts.environment || 'production';
// Don't automatically reconnect after being disconnected
io.sails.reconnection = false;
if (typeof (opts.multiplex) != 'undefined') {
io.sails.multiplex = opts.multiplex;
}
if (typeof (opts.transports) != 'undefined') {
io.sails.transports = opts.transports;
}
if (typeof (opts.autoConnect) != 'undefined') {
io.sails.autoConnect = opts.autoConnect;
}
if (typeof (opts.useCORSRouteToGetCookie) != 'undefined') {
io.sails.useCORSRouteToGetCookie = opts.useCORSRouteToGetCookie;
}
if (typeof (opts.query) != 'undefined') {
io.sails.query = opts.query;
}
if (typeof (opts.headers) != 'undefined') {
io.sails.headers = opts.headers;
}
if (typeof (opts.initialConnectionHeaders) != 'undefined') {
io.sails.initialConnectionHeaders = opts.initialConnectionHeaders;
}
if (typeof (opts.path) != 'undefined') {
io.sails.path = opts.path;
}
if (typeof (opts.reconnection) != 'undefined') {
io.sails.reconnection = opts.reconnection;
}
// Globalize sails app as `server`
global.server = app;
// Globalize sails.io client as `io`
global.io = io;
return cb(err);
});
},
teardown: function (done) {
if (global.io && io.socket && io.socket.isConnected()) {
// Disconnect socket
io.socket.disconnect();
}
setTimeout(function ensureDisconnect () {
// Tear down sails server
// console.log('lowering...');
global.server.lower(function (){
// console.log('lowered');
// Delete globals (just in case-- shouldn't matter)
delete global.server;
delete global.io;
return setTimeout(done, 100);
});
}, 0);
}
};