@@ -32,10 +32,63 @@ function Backend(options) {
32
32
// The number of open agents for monitoring and testing memory leaks
33
33
this . agentsCount = 0 ;
34
34
this . remoteAgentsCount = 0 ;
35
+
36
+ // The below shims are for backwards compatibility. These options will be
37
+ // removed in a future major version
38
+ if ( ! options . disableDocAction ) {
39
+ this . _shimDocAction ( ) ;
40
+ }
41
+ if ( ! options . disableSpaceDelimitedActions ) {
42
+ this . _shimAfterSubmit ( ) ;
43
+ }
35
44
}
36
45
module . exports = Backend ;
37
46
emitter . mixin ( Backend ) ;
38
47
48
+ Backend . prototype . MIDDLEWARE_ACTIONS = {
49
+ // An operation was successfully submitted to the database.
50
+ afterSubmit : 'afterSubmit' ,
51
+ // DEPRECATED: Synonym for 'afterSubmit'
52
+ 'after submit' : 'after submit' ,
53
+ // An operation is about to be applied to a snapshot before being committed to the database
54
+ apply : 'apply' ,
55
+ // An operation was applied to a snapshot; The operation and new snapshot are about to be written to the database.
56
+ commit : 'commit' ,
57
+ // A new client connected to the server.
58
+ connect : 'connect' ,
59
+ // DEPRECATED: A snapshot was loaded from the database.
60
+ doc : 'doc' ,
61
+ // An operation was loaded from the database
62
+ op : 'op' ,
63
+ // A query is about to be sent to the database
64
+ query : 'query' ,
65
+ // Received a message from a client
66
+ receive : 'receive' ,
67
+ // Snapshot(s) were received from the database and are about to be returned to a client
68
+ readSnapshots : 'readSnapshots' ,
69
+ // An operation is about to be submitted to the database
70
+ submit : 'submit'
71
+ } ;
72
+
73
+ Backend . prototype . _shimDocAction = function ( ) {
74
+ var backend = this ;
75
+ this . use ( this . MIDDLEWARE_ACTIONS . readSnapshots , function ( request , callback ) {
76
+ async . each ( request . snapshots , function ( snapshot , eachCb ) {
77
+ var docRequest = { collection : request . collection , id : snapshot . id , snapshot : snapshot } ;
78
+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . doc , request . agent , docRequest , eachCb ) ;
79
+ } , callback ) ;
80
+ } ) ;
81
+ } ;
82
+
83
+ // Shim for backwards compatibility with deprecated middleware action name.
84
+ // The action 'after submit' is now 'afterSubmit'.
85
+ Backend . prototype . _shimAfterSubmit = function ( ) {
86
+ var backend = this ;
87
+ this . use ( backend . MIDDLEWARE_ACTIONS . afterSubmit , function ( request , callback ) {
88
+ backend . trigger ( backend . MIDDLEWARE_ACTIONS [ 'after submit' ] , request . agent , request , callback ) ;
89
+ } ) ;
90
+ } ;
91
+
39
92
Backend . prototype . close = function ( callback ) {
40
93
var wait = 3 ;
41
94
var backend = this ;
@@ -82,7 +135,7 @@ Backend.prototype.connect = function(connection, req) {
82
135
*/
83
136
Backend . prototype . listen = function ( stream , req ) {
84
137
var agent = new Agent ( this , stream ) ;
85
- this . trigger ( ' connect' , agent , { stream : stream , req : req } , function ( err ) {
138
+ this . trigger ( this . MIDDLEWARE_ACTIONS . connect , agent , { stream : stream , req : req } , function ( err ) {
86
139
if ( err ) return agent . close ( err ) ;
87
140
agent . _open ( ) ;
88
141
} ) ;
@@ -155,11 +208,11 @@ Backend.prototype.submit = function(agent, index, id, op, options, callback) {
155
208
if ( err ) return callback ( err ) ;
156
209
var request = new SubmitRequest ( this , agent , index , id , op , options ) ;
157
210
var backend = this ;
158
- backend . trigger ( ' submit' , agent , request , function ( err ) {
211
+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . submit , agent , request , function ( err ) {
159
212
if ( err ) return callback ( err ) ;
160
213
request . submit ( function ( err ) {
161
214
if ( err ) return callback ( err ) ;
162
- backend . trigger ( 'after submit' , agent , request , function ( err ) {
215
+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . afterSubmit , agent , request , function ( err ) {
163
216
if ( err ) return callback ( err ) ;
164
217
backend . _sanitizeOps ( agent , request . projection , request . collection , id , request . ops , function ( err ) {
165
218
if ( err ) return callback ( err ) ;
@@ -179,7 +232,7 @@ Backend.prototype._sanitizeOp = function(agent, projection, collection, id, op,
179
232
return callback ( err ) ;
180
233
}
181
234
}
182
- this . trigger ( 'op' , agent , { collection : collection , id : id , op : op } , callback ) ;
235
+ this . trigger ( this . MIDDLEWARE_ACTIONS . op , agent , { collection : collection , id : id , op : op } , callback ) ;
183
236
} ;
184
237
Backend . prototype . _sanitizeOps = function ( agent , projection , collection , id , ops , callback ) {
185
238
var backend = this ;
@@ -194,33 +247,31 @@ Backend.prototype._sanitizeOpsBulk = function(agent, projection, collection, ops
194
247
} , callback ) ;
195
248
} ;
196
249
197
- Backend . prototype . _sanitizeSnapshot = function ( agent , projection , collection , id , snapshot , callback ) {
250
+ Backend . prototype . _sanitizeSnapshots = function ( agent , projection , collection , snapshots , callback ) {
198
251
if ( projection ) {
199
252
try {
200
- projections . projectSnapshot ( projection . fields , snapshot ) ;
253
+ projections . projectSnapshots ( projection . fields , snapshots ) ;
201
254
} catch ( err ) {
202
255
return callback ( err ) ;
203
256
}
204
257
}
205
- this . trigger ( 'doc' , agent , { collection : collection , id : id , snapshot : snapshot } , callback ) ;
206
- } ;
207
- Backend . prototype . _sanitizeSnapshots = function ( agent , projection , collection , snapshots , callback ) {
208
- var backend = this ;
209
- async . each ( snapshots , function ( snapshot , eachCb ) {
210
- backend . _sanitizeSnapshot ( agent , projection , collection , snapshot . id , snapshot , eachCb ) ;
211
- } , callback ) ;
212
- } ;
213
- Backend . prototype . _sanitizeSnapshotBulk = function ( agent , projection , collection , snapshotMap , callback ) {
214
- var backend = this ;
215
- async . forEachOf ( snapshotMap , function ( snapshot , id , eachCb ) {
216
- backend . _sanitizeSnapshot ( agent , projection , collection , id , snapshot , eachCb ) ;
217
- } , callback ) ;
258
+ var request = { collection : collection , snapshots : snapshots } ;
259
+ this . trigger ( this . MIDDLEWARE_ACTIONS . readSnapshots , agent , request , callback ) ;
218
260
} ;
219
261
220
262
Backend . prototype . _getSnapshotProjection = function ( db , projection ) {
221
263
return ( db . projectsSnapshots ) ? null : projection ;
222
264
} ;
223
265
266
+ Backend . prototype . _getSnapshotsFromMap = function ( ids , snapshotMap ) {
267
+ var snapshots = new Array ( ids . length ) ;
268
+ for ( var i = 0 ; i < ids . length ; i ++ ) {
269
+ var id = ids [ i ] ;
270
+ snapshots [ i ] = snapshotMap [ id ] ;
271
+ }
272
+ return snapshots ;
273
+ } ;
274
+
224
275
// Non inclusive - gets ops from [from, to). Ie, all relevant ops. If to is
225
276
// not defined (null or undefined) then it returns all ops.
226
277
Backend . prototype . getOps = function ( agent , index , id , from , to , callback ) {
@@ -283,7 +334,8 @@ Backend.prototype.fetch = function(agent, index, id, callback) {
283
334
backend . db . getSnapshot ( collection , id , fields , null , function ( err , snapshot ) {
284
335
if ( err ) return callback ( err ) ;
285
336
var snapshotProjection = backend . _getSnapshotProjection ( backend . db , projection ) ;
286
- backend . _sanitizeSnapshot ( agent , snapshotProjection , collection , id , snapshot , function ( err ) {
337
+ var snapshots = [ snapshot ] ;
338
+ backend . _sanitizeSnapshots ( agent , snapshotProjection , collection , snapshots , function ( err ) {
287
339
if ( err ) return callback ( err ) ;
288
340
backend . emit ( 'timing' , 'fetch' , Date . now ( ) - start , request ) ;
289
341
callback ( null , snapshot ) ;
@@ -306,7 +358,8 @@ Backend.prototype.fetchBulk = function(agent, index, ids, callback) {
306
358
backend . db . getSnapshotBulk ( collection , ids , fields , null , function ( err , snapshotMap ) {
307
359
if ( err ) return callback ( err ) ;
308
360
var snapshotProjection = backend . _getSnapshotProjection ( backend . db , projection ) ;
309
- backend . _sanitizeSnapshotBulk ( agent , snapshotProjection , collection , snapshotMap , function ( err ) {
361
+ var snapshots = backend . _getSnapshotsFromMap ( ids , snapshotMap ) ;
362
+ backend . _sanitizeSnapshots ( agent , snapshotProjection , collection , snapshots , function ( err ) {
310
363
if ( err ) return callback ( err ) ;
311
364
backend . emit ( 'timing' , 'fetchBulk' , Date . now ( ) - start , request ) ;
312
365
callback ( null , snapshotMap ) ;
@@ -479,7 +532,7 @@ Backend.prototype._triggerQuery = function(agent, index, query, options, callbac
479
532
snapshotProjection : null ,
480
533
} ;
481
534
var backend = this ;
482
- backend . trigger ( ' query' , agent , request , function ( err ) {
535
+ backend . trigger ( backend . MIDDLEWARE_ACTIONS . query , agent , request , function ( err ) {
483
536
if ( err ) return callback ( err ) ;
484
537
// Set the DB reference for the request after the middleware trigger so
485
538
// that the db option can be changed in middleware
0 commit comments