-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
150 lines (114 loc) · 3.27 KB
/
index.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
"use strict";
/*!
* Prism Realtime Client
* Copyright(c) 2013 Owen Barnes <[email protected]>
* MIT Licensed
*/
/* global unescape, escape */
/**
* Module dependencies.
*/
var EE = require('events').EventEmitter;
var Service = require('realtime-service-client');
var systemService = require('./lib/system-service');
/**
* Prism Client
*/
function Client(options) {
options = options || {};
this.services = {};
this.api = {};
this.status = new EE();
this.version = '0.0.1';
this.transport = options.transport || null;
this.sessionCookieName = options.sessionCookieName || 'connect.sid';
this._registerSystemService();
}
/**
* Provide details of a service running on the server (saves needing to discover)
*/
Client.prototype.provide = function(params, handler) {
var service = new Service(this, params);
this.services[service.id] = service;
var api = handler(service);
if (api) {
// Hide private services if client support JS 1.8.5
if (Object.defineProperty && service.private) {
Object.defineProperty(this.api, service.name, {value: api, enumerable: false});
} else {
this.api[service.name] = api;
}
return api;
}
return null;
};
/**
* Load all services in from an Array
*/
Client.prototype.load = function(services) {
services.forEach(function(service){
this.provide(service.paramsForClient(), service.clientApi);
}.bind(this));
};
/**
* Discover which services are available and download the client-side code from the server.
*/
Client.prototype.discover = function(cb) {
this.api._system.discover({}, cb);
};
/**
* Process an incoming message string
*/
Client.prototype.process = function(req) {
var msgAry = req.message.split('|');
var serviceId = msgAry.shift();
var service = this.services[serviceId];
if (service) {
service.read(msgAry);
} else {
throw('Unable to process incoming message. Service ID ' + serviceId + ' not found');
}
};
/**
* Attempt to connect. When successful, transmit sessionId
*/
Client.prototype.connect = function(cb) {
this.status.on('open', function(){
this.api._system.connect(cb);
}.bind(this));
this.connection = this.transport(this);
};
/**
* Attempt to get session ID from cookie
*/
Client.prototype._getSessionId = function() {
if (typeof document === 'undefined') return false;
var c_end, c_start;
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(this.sessionCookieName + "=");
if (c_start !== -1) {
c_start = c_start + this.sessionCookieName.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end === -1) c_end = document.cookie.length;
return unescape(document.cookie.substring(c_start, c_end));
}
}
return false;
};
Client.prototype._registerSystemService = function() {
systemService(this);
};
/**
* Set new session ID cookie
*/
Client.prototype._setSessionId = function(value) {
if (typeof document === 'undefined') return false;
var exdays = 1;
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays === null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = this.sessionCookieName + "=" + c_value;
};
module.exports = function(options){
return new Client(options);
};