This repository was archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocumentdb-session.js
449 lines (352 loc) · 13.2 KB
/
documentdb-session.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
const documentdb = require('documentdb');
/**
* The default configuration options for `documentdb-session`
* @const
* @type {Object} defaults
*/
const defaults = {
collection: 'sessions',
database: 'sessionstore',
discriminator: { type: 'session' },
host: process.env.DOCUMENTDB_URL,
key: process.env.DOCUMENTDB_KEY,
ttl: null,
proxyUrl: null,
};
/**
* The default callback for use with store methods, in case one isn't provided
* @function
* @param {Object} err An error object, if one was thrown
*/
const defaultCallback = err => {
throw new Error(err);
};
const DocumentDBSession = expressSession => {
if (!expressSession) {
throw new Error('documentdb-session must be called by passing it the express-session object.');
}
/* A class representing a DocumentDBStore */
class DocumentDBStore extends expressSession.Store {
/**
* Create a new DocumentDBStore
* @type {Object}
* @param {Object} [config] The configuration object
*/
constructor(config = {}) {
super();
// copy configuration options to the Store
Object.assign(this, defaults, config);
// if the `host` property is missing from the config, throw an error
if (!this.host) {
throw new Error('The `host` config variable is required. Please include it in the `host` property of the config object, or in the `DOCUMENTDB_URL` environment variable.');
}
// if the `key` property is missing from the config, throw an error
if (!this.key) {
throw new Error('The `key` config variable is required. Please include it in the `key` property of the config object, or in the `DOCUMENTDB_KEY` environment variable.');
}
this.databaseLink = `dbs/${this.database}`;
this.collectionLink = `${this.databaseLink}/colls/${this.collection}`;
this.filterOn = Object.keys(this.discriminator)[0]; // the key to filter sessions on
this.filterValue = this.discriminator[this.filterOn]; // the value to filter sessions on
this.initialized = false;
// a hash of the stored procedures used by this package
this.sprocs = {
clear: {
id: 'clear',
serverScript: require('./sprocs/clear'),
},
length: {
id: 'length',
serverScript: require('./sprocs/length'),
},
};
// create a new DocumentDB client and assign it to Store.client
let connectionOptions = new documentdb.DocumentBase.ConnectionPolicy();
connectionOptions.ProxyUrl = this.proxyUrl
this.client = new documentdb.DocumentClient(this.host, { masterKey: this.key }, connectionOptions);
}
/**
* Retrieve all the sessions in the database.
* @method
* @function
* @type {Function}
* @param {Function} [cb=defaultCallback] The callback function to run
*/
all(cb = defaultCallback) {
// The DocumentDB query to run
const querySpec = {
query: 'SELECT * FROM d WHERE d[@attr] = @value',
parameters: [
{
name: '@attr',
value: this.filterOn,
},
{
name: '@value',
value: this.filterValue,
},
],
};
// Runs the .queryDocuments() method from the documentdb SDK
const queryDocuments = () => {
this.client.queryDocuments(this.collectionLink, querySpec).toArray((err, sessions) => {
if (err) return cb(new Error(`Error querying documents: ${err.body}`));
cb(null, sessions);
});
};
if (this.initialized) {
// if the database has been initialized, query documents
queryDocuments();
} else {
// if the database hasn't been initialized, initialize it, then query documents
this.initialize(err => {
if (err) return cb(err);
queryDocuments();
});
}
}
/**
* Deletes all the sessions from the database
* @function
* @method
* @type {Function}
* @param {Function} [cb=defaultCallback] The callback function to run
*/
clear(cb = defaultCallback) {
// Run the stored procedure for `.clear()`
const executeStoredProcedure = () => {
const sprocLink = `${this.collectionLink}/sprocs/clear`;
this.client.executeStoredProcedure(sprocLink, (err, res) => {
if (err) return cb(new Error(`Error executing the stored procedure for '.clear()': ${err.body}`));
// if a continuation is returned, execute the stored procedure again
if (res.continuation) {
this.clear(cb);
} else {
cb();
}
});
};
if (this.initialized) {
// if the database has been initialized, run the stored procedure
executeStoredProcedure();
} else {
// if the database hasn't been initialized, initialize it, then run the stored procedure
this.initialize(err => {
if (err) return cb(err);
executeStoredProcedure();
});
}
}
/**
* Creates the sessions collection
* @function
* @method
* @return {Promise} Returns a Promise that resolves once the collection is created
*/
createCollection() {
return new Promise((resolve, reject) => {
const collectionId = this.collection || defaults.collection;
const body = {
id: collectionId,
defaultTtl: -1,
};
this.client.createCollection(this.databaseLink, body, err => {
if (err && err.code != 409) {
reject(new Error(`Error creating the "${this.collection}" collection: ${err.body}`));
} else {
resolve();
}
});
});
}
/**
* Creates the session store database. If the database already exists, the function simply resolves.
* @function
* @method
* @return {Promise} Returns a Promise that resolves when the database is created
*/
createDatabase() {
return new Promise((resolve, reject) => {
const databaseId = this.database || defaults.database;
this.client.createDatabase({ id: databaseId }, err => {
if (err && err.code != 409) {
reject(new Error(`Error creating the "${this.database}" database: ${err.body}`));
} else {
resolve();
}
});
});
}
/**
* Deletes the session with the given session ID
* @function
* @method
* @type {Function}
* @param {String} sid The ID of the session to delete
* @param {Function} [cb=defaultCallback] The callback function to run
*/
destroy(sid, cb = defaultCallback) {
const docLink = `${this.collectionLink}/docs/${sid}`;
// Runs the .deleteDocument() method of the DocumentDB SDK
const deleteDocument = () => this.client.deleteDocument(docLink, err => {
if (err) return cb(new Error(`Error deleting document: ${err.body}`));
cb();
});
if (this.initialized) {
// if the database is initialized, delete the session
deleteDocument();
} else {
// if the database hasn't been initialized, initialize it, then delete the session
this.initialize(err => {
if (err) return cb(err);
deleteDocument();
});
}
}
/**
* Retrieves the session with the given session ID
* @method
* @function
* @param {String} sid The ID of the session to retrieve
* @param {Function} [cb=defaultCallback] The callback function to run
*/
get(sid, cb = defaultCallback) {
const docLink = `${this.collectionLink}/docs/${sid}`;
const readDocument = () => this.client.readDocument(docLink, (err, doc) => {
if (err && err.code == 404) return cb(null, null);
else if (err) return cb(new Error(`Error reading document: ${err.body}`));
cb(null, doc);
});
if (this.initialized) {
// if the database is initialized, get the session
readDocument();
} else {
// if the database hasn't been initialized, initialize it, then get the session
this.initialize(err => {
if (err) return cb(err);
readDocument();
});
}
}
/**
* Initializes the database by attempting to create both the database and the collection, and then uploading the stored procedures. If either the database or collection already exists, it moves to the next step in the initialization.
* @function
* @method
* @type {Function}
* @param {Function} [cb = defaultCallback] The callback function to run
*/
initialize(cb = defaultCallback) {
this.createDatabase()
.then(this.createCollection.bind(this))
.then(this.uploadSprocs.bind(this))
.then(() => {
this.initialized = true;
cb();
})
.catch(err => cb(err));
}
/**
* Counts the number of sessions in the database
* @method
* @function
* @type {Function}
* @param {Function} [cb = defaultCallback] The callback function to run
* @return {Function} cb
*/
length(cb = defaultCallback) {
// Execute the stored procedure for `.length()`
const executeStoredProcedure = continuationToken => {
const params = [this.filterOn, this.filterValue, continuationToken];
const sprocLink = `${this.collectionLink}/sprocs/length`;
let documentsFound = 0;
this.client.executeStoredProcedure(sprocLink, params, (err, res) => {
if (err) return cb(new Error(`Error executing the stored procedure for '.length()': ${err.body}`));
// add the retrieved results to the running total
documentsFound += res.documentsFound;
// if a continuation token was returned, run the stored procedure again to get more results
if (res.continuation) {
executeStoredProcedure(res.continuation);
}
cb(null, documentsFound);
});
};
// if the database is initialized, run the stored procedure
if (this.initialized) return executeStoredProcedure();
// if the database isn't initialized, initialize it, then run the stored procedure
this.initialize(err => {
if (err) cb(err);
executeStoredProcedure();
});
}
/**
* Upserts the provided session to the database
* @method
* @function
* @param {String} sid The ID of the session to update
* @param {Object} session The session object to upload
* @param {Function} [cb=defaultCallback] The callback function to run
*/
set(sid, session, cb = defaultCallback) {
// if the session ID parameter and the session ID property don't match, throw an error
if (sid === session.id) {
// create a new session object, to avoid altering the original parameter
const doc = Object.assign({}, session);
doc.id = sid;
doc[this.filterOn] = this.filterValue;
doc.lastActive = Date.now();
if (this.ttl) doc.ttl = this.ttl;
// only add a `ttl` property if one is set in the documentdb-session config
// an invalid `ttl` property on a document with throw an error in documentdb
const upsertDocument = () => this.client.upsertDocument(this.collectionLink, doc, err => {
if (err) {
return cb(new Error(`Error upserting session data to database: ${err.body}`));
}
cb();
});
if (this.initialized) {
// if the database is initialized, upsert the session data
upsertDocument();
} else {
// if the database hasn't been initialized, initialize it, then upsert the session data
this.initialize(err => {
if (err) return cb(err);
upsertDocument();
});
}
} else {
cb(new Error('The value for the `sid` parameter is not equal to the value of `session.id`.'));
}
}
/**
* Updates the timestamp of the provided session (by calling the .set() method)
* @function
* @method
* @type {Function}
* @param {String} sid The ID of the session to update
* @param {Object} session The session object
* @param {Function} [cb = defaultCallback] The callback to run
*/
touch(sid, session, cb = defaultCallback) {
this.set(sid, session, cb);
}
/**
* Upserts the stored procedures to the collection
* @method
* @function
* @return {Promise} Returns a Promise that resolves when the stored procedures are uploaded
*/
uploadSprocs() {
const uploadSproc = sproc => new Promise((resolve, reject) => {
this.client.upsertStoredProcedure(this.collectionLink, this.sprocs[sproc.id], err => {
if (err) reject(new Error(`Error upserting stored procedure: ${err.body}`));
resolve();
});
});
const sprocs = Object.keys(this.sprocs).map(key => this.sprocs[key]);
const promises = sprocs.map(uploadSproc);
return Promise.all(promises);
}
}
return DocumentDBStore;
};
module.exports = DocumentDBSession;