This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.js
204 lines (184 loc) · 4.96 KB
/
store.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
var assert = require("assert");
var debug = require("debug")("socket.io-store");
var redisClient;
var prefix;
/*
* Validate an options object
*/
var validateOptions = function (options) {
if (options && options.prefix) {
assert(typeof options.prefix === "string", "Invalid prefix: prefix must be a string");
}
};
/**
* Redis-backed store matching the socket.io pre-v1.x api
*
* @constructor
*
* @param {String} socketId The socket session id
* @param {Object} [options]
* @param {Object} [options.redisClient] Redis client
* @param {String} [options.prefix] Prefix to use for namespacing the keys
* saved to redis
*
* @property {Object} redisClient Redis client
* @property {String} _prefix Prefix to use for namespacing the keys
* saved to redis
*/
function SocketStore (socketId, options) {
if (!(this instanceof SocketStore)) {
return new SocketStore(socketId, options);
}
options || (options = {});
validateOptions(options);
if (!socketId) {
throw new Error("Missing required socket id");
}
this.socketId = socketId;
if (options.redisClient) {
this.redisClient = options.redisClient;
}
else if (redisClient) {
this.redisClient = redisClient;
}
// At this point, we cannot wait for the redis client any longer
assert(this.redisClient, "Missing required redis client");
if (options.prefix) {
this._prefix = options.prefix;
}
else if (prefix) {
this._prefix = prefix;
}
else {
this._prefix = "__socket.io-store";
}
debug("Socket store created for socket %s with prefix %s", this.socketId, this._prefix);
}
/*
* SocketStore#set and SocketStore#del can be used without a callback,
* so we use this to handle any possible errors
*/
var defaultCallback = function (err) {
if (err) {
debug(err);
}
};
/**
* @private
* @memberOf SocketStore
* @param {...*} Variable number of parts from which to construct a key
* @returns {String} The key
*/
SocketStore.prototype._createKey = function () {
var parts = [].slice.call(arguments);
parts.unshift(this._prefix, this.socketId);
return parts.join(":");
};
/**
* Get the value of a key
*
* @memberOf SocketStore
* @param {String} key The key to get
* @param {SocketStore~getCallback} cb The callback
*/
SocketStore.prototype.get = function (key, cb) {
this.redisClient.get(this._createKey(key), cb);
};
/**
* @callback SocketStore~getCallback
* @param {Error|null} err An error or `null`
* @param {String} value The value
*/
/**
* Set a value for a key
*
* @memberOf SocketStore
* @param {String} key The key to set the value for
* @param {String} value The value to set
* @param {SocketStore~setCallback} [cb] The callback
*/
SocketStore.prototype.set = function (key, value, cb) {
cb || (cb = defaultCallback);
this.redisClient.set([this._createKey(key), value], cb);
};
/**
* @callback SocketStore~setCallback
* @param {Error|null} err An error or `null`
* @param {Number} set The number of keys set (0 or 1)
*/
/**
* Check whether a key exists
*
* @memberOf SocketStore
* @param {String} key The key to check
* @param {SocketStore~hasCallback} cb The callback
*/
SocketStore.prototype.has = function (key, cb) {
this.redisClient.exists(this._createKey(key), cb);
};
/**
* @callback SocketStore~hasCallback
* @param {Error|null} err An error or `null`
* @param {Number} has 0 if key does not exist, 1 if key exists
*/
/**
* Delete a key
*
* @memberOf SocketStore
* @param {String} key The key to delete
* @param {SocketStore~deleteCallback} cb The callback
*/
SocketStore.prototype.del = function (key, cb) {
cb || (cb = defaultCallback);
this.redisClient.del(this._createKey(key), cb);
};
/**
* @callback SocketStore~deleteCallback
* @param {Error|null} err An error or `null`
* @param {Number} deleted The number of keys deleted
*/
/**
* Initialize the SocketStore options
*
* @static
* @param {Object} [options]
* @param {Object} [options.redisClient] Redis client
* @param {String} [options.prefix] Prefix to use for namespacing the keys
* saved to redis
*/
SocketStore.initialize = function (options) {
if (!options) {
return;
}
validateOptions(options);
if (options.redisClient) {
redisClient = options.redisClient;
}
if (options.prefix) {
prefix = options.prefix;
}
};
var implementedMethods = ["get", "set", "has", "del"];
/**
* @typedef {Function} SocketStore~middleware
* @param {Object} socket A socket.io socket
* @param {Function} next
*/
/**
* Return socket.io <a href="http://socket.io/docs/server-api/#namespace#use(fn:function):namespace">middleware</a>
*
* @static
* @param {Object} [options]
* @returns {SocketStore~middleware} fn socket.io middleware
*/
SocketStore.middleware = function (options) {
validateOptions(options);
return function (socket, next) {
var store = new SocketStore(socket.id, options);
implementedMethods.forEach(function (method) {
socket[method] = store[method].bind(store);
});
next();
};
};
module.exports = SocketStore;