This repository was archived by the owner on Aug 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (68 loc) · 2.37 KB
/
index.js
File metadata and controls
88 lines (68 loc) · 2.37 KB
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
const { socket: commons } = require('@feathersjs/transport-commons');
const makeDebug = require('debug');
const Proto = require('uberproto');
const Primus = require('primus');
const http = require('http');
const Emitter = require('primus-emitter');
const debug = makeDebug('@feathersjs/primus');
const socketKey = Symbol('@feathersjs/primus/socket');
function configurePrimus (config, configurer) {
return function () {
const app = this;
const getParams = spark => spark.request.feathers;
if (!app.version || app.version < '3.0.0') {
throw new Error('@feathersjs/primus is not compatible with this version of Feathers. Use the latest at @feathersjs/feathers.');
}
const done = new Promise(resolve => {
Proto.mixin({
listen (...args) {
if (typeof this._super === 'function') {
// If `listen` already exists
// usually the case when the app has been expressified
return this._super(...args);
}
const server = http.createServer();
this.setup(server);
return server.listen(...args);
},
setup (server) {
debug('Setting up Primus');
if (!this.primus) {
const primus = this.primus = new Primus(server, config);
primus.plugin('emitter', Emitter);
primus.use('feathers', function (req, res, next) {
req.feathers = { provider: 'primus' };
next();
}, 0);
primus.on('connection', spark =>
Object.defineProperty(getParams(spark), socketKey, {
value: spark
})
);
primus.on('disconnection', spark => {
const { channels } = app;
if (channels.length) {
app.channel(app.channels).leave(getParams(spark));
}
});
}
if (typeof configurer === 'function') {
debug('Calling Primus configuration function');
configurer.call(this, this.primus);
}
resolve(this.primus);
return this._super.apply(this, arguments);
}
}, app);
});
app.configure(commons({
done,
socketKey,
getParams,
emit: 'send'
}));
};
}
module.exports = configurePrimus;
module.exports.SOCKET_KEY = socketKey;
module.exports.default = configurePrimus;