@@ -40,8 +40,8 @@ var Session = module.exports = (function () {
40
40
index : '/indexes/%s?query=%s'
41
41
} ;
42
42
43
- Session . prototype . _request = function ( method , query , callback , jsonHandler , writeable , headers , responseHandler , endHandler ) {
44
- var session = this ;
43
+ Session . prototype . _request = function ( method , query , callback , handlers , writeable , headers ) {
44
+ var session = this , handlers = handlers || { } ;
45
45
46
46
var options = {
47
47
host : session . _connection . host ,
@@ -57,41 +57,40 @@ var Session = module.exports = (function () {
57
57
var request = http . request ( options ) ;
58
58
59
59
request . on ( 'response' , function ( response ) {
60
- var error ;
61
- var args = [ undefined ] ;
62
- var data = "" ;
60
+ var args = [ undefined ] , data = '' , error ;
61
+
62
+ response . on ( 'data' , function ( chunk ) {
63
+ try {
64
+ data += chunk . toString ( ) ;
65
+ } catch ( e ) {
66
+ error = new Error ( 'Processing Error: ' + e . message ) ;
67
+ }
68
+ } ) ;
63
69
64
70
if ( response . statusCode === 409 ) {
65
71
error = new Error ( 'Concurrency conflict, no changes were performed. The specified etag did not match the current document etag.' ) ;
66
72
error . statusCode = 409 ;
67
- } else if ( jsonHandler !== undefined ) {
68
- response . on ( 'data' , function ( chunk ) {
69
- try {
70
- data += chunk . toString ( ) ;
71
- } catch ( e ) {
72
- error = new Error ( 'Processing Error: ' + e . message ) ;
73
- }
74
- } ) ;
75
- } else if ( responseHandler ) {
76
- args = args . concat ( responseHandler ( response . statusCode ) ) ;
73
+ } else if ( typeof handlers . response === 'function' ) {
74
+ args = args . concat ( handlers . response ( response . statusCode ) ) ;
77
75
} else if ( response . statusCode === 400 ) {
78
76
error = new Error ( 'Query Failed: The request url was badly formed. Ensure no parameters contain illegal characters' ) ;
79
77
error . statusCode = 400 ;
80
78
}
81
79
82
80
response . on ( 'end' , function ( ) {
83
- try {
84
- var json = JSON . parse ( data ) ;
85
-
86
- if ( response . statusCode > 299 || json . Error ) {
87
- var message = json . Error . match ( / : ( .* ) \r \n / ) [ 1 ] ;
88
- error = new Error ( message || 'An error occured.' ) ;
89
- error . statusCode = response . statusCode ;
90
- } else if ( jsonHandler !== null ) {
91
- args = args . concat ( jsonHandler ( json , response . headers ) ) ;
81
+ if ( data . length > 0 ) {
82
+ try {
83
+ var json = JSON . parse ( data ) ;
84
+
85
+ if ( response . statusCode > 299 || json . Error ) {
86
+ error = new Error ( json . Error . match ( / : ( .* ) \r \n / ) [ 1 ] || 'An error occured.' ) ;
87
+ error . statusCode = response . statusCode ;
88
+ } else if ( typeof handlers . json === 'function' ) {
89
+ args = args . concat ( handlers . json ( json , response . headers ) ) ;
90
+ }
91
+ } catch ( e ) {
92
+ error = new Error ( 'Parse Error: ' + e . message ) ;
92
93
}
93
- } catch ( e ) {
94
- error = new Error ( 'Parse Error: ' + e . message ) ;
95
94
}
96
95
97
96
if ( error ) {
@@ -105,8 +104,8 @@ var Session = module.exports = (function () {
105
104
callback ( error ) ;
106
105
}
107
106
} else {
108
- if ( endHandler ) {
109
- endHandler ( ) ;
107
+ if ( typeof handlers . end === 'function' ) {
108
+ handlers . end ( ) ;
110
109
}
111
110
112
111
if ( callback ) {
@@ -138,7 +137,7 @@ var Session = module.exports = (function () {
138
137
return ;
139
138
}
140
139
141
- var session = this ;
140
+ var session = this , handlers = { } ;
142
141
143
142
if ( typeof includes === 'function' && callback === undefined ) {
144
143
callback = includes ;
@@ -194,60 +193,65 @@ var Session = module.exports = (function () {
194
193
if ( length === 0 ) {
195
194
callback ( undefined , document , new Queryable ( cachedIncludes ) ) ;
196
195
} else if ( length === 1 ) {
197
- session . _request ( 'GET' , util . format ( masks . document , uncachedIncludes [ 0 ] ) , callback , function ( json , headers ) {
198
- _addMeta ( json , headers , uncachedIncludes [ 0 ] ) ;
199
- _addToCache ( json ) ;
200
- cachedIncludes . push ( json ) ;
201
- return [ document , new Queryable ( cachedIncludes ) ] ;
202
- } ) ;
203
- } else if ( length > 1 ) {
204
- session . _request ( 'POST' , masks . queries , callback , function ( json ) {
205
- json . Results . forEach ( _addToCache ) ;
206
- cachedIncludes . concat ( json . Results ) ;
207
- return [ document , new Queryable ( cachedIncludes ) ] ;
208
- } , uncachedIncludes ) ;
196
+ handlers . json = function ( json , headers ) {
197
+ _addMeta ( json , headers , uncachedIncludes [ 0 ] ) ;
198
+ _addToCache ( json ) ;
199
+ cachedIncludes . push ( json ) ;
200
+ return [ document , new Queryable ( cachedIncludes ) ] ;
201
+ } ;
202
+
203
+ session . _request ( 'GET' , util . format ( masks . document , uncachedIncludes [ 0 ] ) , callback , handlers ) ;
204
+ } else if ( length > 1 ) {
205
+ handlers . json = function ( json ) {
206
+ json . Results . forEach ( _addToCache ) ;
207
+ cachedIncludes . concat ( json . Results ) ;
208
+ return [ document , new Queryable ( cachedIncludes ) ] ;
209
+ } ;
210
+
211
+ session . _request ( 'POST' , masks . queries , callback , handlers , uncachedIncludes ) ;
209
212
}
210
213
} else {
211
214
includes = ( typeof includes === 'string' ? [ includes ] : includes ) ;
212
-
213
- var query = masks . queries + ( Array . isArray ( includes ) ? '?' + qs . stringify ( { include : includes } , '&' , '=' ) : '' ) ;
214
-
215
- session . _request ( 'POST' , query , callback , function ( json ) {
216
- json . Results . forEach ( _addToCache ) ;
217
- json . Includes . forEach ( _addToCache ) ;
218
- return [ json . Results [ 0 ] , new Queryable ( json . Includes ) ] ;
219
- } , [ keys ] ) ;
215
+ handlers . json = function ( json ) {
216
+ json . Results . forEach ( _addToCache ) ;
217
+ json . Includes . forEach ( _addToCache ) ;
218
+ return [ json . Results [ 0 ] , new Queryable ( json . Includes ) ] ;
219
+ } ;
220
+
221
+ session . _request ( 'POST' , masks . queries + ( Array . isArray ( includes ) ? '?' + qs . stringify ( { include : includes } , '&' , '=' ) : '' ) , callback , handlers , [ keys ] ) ;
220
222
}
221
223
222
224
} else {
223
225
// no includes
224
226
if ( cached ) {
225
227
callback ( undefined , JSON . parse ( cached ) ) ;
226
228
} else {
227
- session . _request ( 'GET' , util . format ( masks . document , keys ) , callback , function ( json , headers ) {
228
- _addMeta ( json , headers , keys ) ;
229
- _addToCache ( json ) ;
230
- return [ json ] ;
231
- } ) ;
229
+ handlers . json = function ( json , headers ) {
230
+ _addMeta ( json , headers , keys ) ;
231
+ _addToCache ( json ) ;
232
+ return [ json ] ;
233
+ } ;
234
+
235
+ session . _request ( 'GET' , util . format ( masks . document , keys ) , callback , handlers ) ;
232
236
}
233
237
}
234
238
} else if ( Array . isArray ( keys ) ) {
235
239
// it's an array of keys
236
240
// batches - we're not checking the cache here - whats the likelihood of every document being cached?
237
241
includes = ( typeof includes === 'string' ? [ includes ] : includes ) ;
238
-
239
- var query = masks . queries + ( Array . isArray ( includes ) ? '?' + qs . stringify ( { include : includes } , '&' , '=' ) : '' ) ;
240
-
241
- session . _request ( 'POST' , query , callback , function ( json ) {
242
- json . Results . forEach ( _addToCache ) ;
243
- json . Includes . forEach ( _addToCache ) ;
244
- return [ new Queryable ( json . Results ) , new Queryable ( json . Includes ) ] ;
245
- } , keys ) ;
242
+ handlers . json = function ( json ) {
243
+ json . Results . forEach ( _addToCache ) ;
244
+ json . Includes . forEach ( _addToCache ) ;
245
+ return [ new Queryable ( json . Results ) , new Queryable ( json . Includes ) ] ;
246
+ } ;
247
+
248
+ session . _request ( 'POST' , masks . queries + ( Array . isArray ( includes ) ? '?' + qs . stringify ( { include : includes } , '&' , '=' ) : '' ) , callback , handlers , keys ) ;
246
249
}
247
250
} ;
248
251
249
252
Session . prototype . _buildSpecifications = function ( specifications ) {
250
253
query = '' ;
254
+
251
255
if ( typeof specifications === 'object' ) {
252
256
if ( Array . isArray ( specifications . projections ) ) {
253
257
query += '&' + qs . stringify ( { fetch : specifications . projections } , '&' , '=' ) ;
@@ -269,6 +273,7 @@ var Session = module.exports = (function () {
269
273
query += '&pageSize=' + specifications . pageSize ;
270
274
}
271
275
}
276
+
272
277
return query ;
273
278
}
274
279
@@ -291,8 +296,9 @@ var Session = module.exports = (function () {
291
296
292
297
query += this . _buildSpecifications ( specifications ) ;
293
298
294
- this . _request ( 'GET' , query , callback , function ( json ) {
295
- return [ new Queryable ( json . Results ) , new Statistics ( json ) ] ;
299
+ this . _request ( 'GET' , query , callback , { json : function ( json ) {
300
+ return [ new Queryable ( json . Results ) , new Statistics ( json ) ] ;
301
+ }
296
302
} ) ;
297
303
} ;
298
304
@@ -310,8 +316,9 @@ var Session = module.exports = (function () {
310
316
311
317
query += this . _buildSpecifications ( specifications ) ;
312
318
313
- this . _request ( 'GET' , query , callback , function ( json ) {
314
- return [ new Queryable ( json . Results ) , new Statistics ( json ) ] ;
319
+ this . _request ( 'GET' , query , callback , { json : function ( json ) {
320
+ return [ new Queryable ( json . Results ) , new Statistics ( json ) ] ;
321
+ }
315
322
} ) ;
316
323
} ;
317
324
@@ -352,13 +359,13 @@ var Session = module.exports = (function () {
352
359
return ;
353
360
}
354
361
355
- var session = this ;
356
- var cache = session . _cache ;
357
- var changes = session . _changes ;
358
- var store = session . _store ;
359
- var batch = [ ] ;
360
-
361
- var key , document ;
362
+ var session = this ,
363
+ cache = session . _cache ,
364
+ changes = session . _changes ,
365
+ store = session . _store ,
366
+ batch = [ ] ,
367
+ handlers = { } ,
368
+ key , document ;
362
369
363
370
for ( key in changes ) {
364
371
if ( changes . hasOwnProperty ( key ) && cache . hasOwnProperty ( key ) ) {
@@ -395,25 +402,29 @@ var Session = module.exports = (function () {
395
402
return ;
396
403
}
397
404
398
- session . _request ( 'POST' , masks . bulk , callback , function ( json ) {
405
+ handlers . json = function ( json ) {
399
406
var keys = [ ] ;
400
407
401
408
json . forEach ( function ( data ) {
402
409
keys . push ( data . Key ) ;
403
410
} ) ;
404
411
405
412
return [ keys ] ;
406
- } , batch , undefined , undefined , function ( ) {
413
+ } ;
414
+
415
+ handlers . end = function ( ) {
407
416
session . _store = { } ;
408
- } ) ;
417
+ } ;
418
+
419
+ session . _request ( 'POST' , masks . bulk , callback , handlers , batch ) ;
409
420
} ;
410
421
411
422
Session . prototype . delete = function ( document , etag , callback ) {
412
423
if ( ! document ) {
413
424
return ;
414
425
}
415
426
416
- var session = this ;
427
+ var session = this , handlers = { } ;
417
428
418
429
if ( typeof etag === 'function' && callback === undefined ) {
419
430
callback = etag ;
@@ -433,16 +444,18 @@ var Session = module.exports = (function () {
433
444
434
445
var id = typeof document === 'string' ? document : document [ '@metadata' ] [ '@id' ] ;
435
446
var headers = etag && session . options . useOptimisticConcurrency ? { 'If-Match' : etag } : undefined ;
436
-
437
- session . _request ( 'DELETE' , util . format ( masks . document , id ) , callback , null , undefined , headers , undefined , function ( ) {
447
+
448
+ handlers . end = function ( ) {
438
449
if ( session . _cache . hasOwnProperty [ id ] ) {
439
450
delete session . _cache [ id ] ;
440
451
}
441
452
442
453
if ( session . _changes . hasOwnProperty [ id ] ) {
443
454
delete session . _changes [ id ] ;
444
455
}
445
- } ) ;
456
+ } ;
457
+
458
+ session . _request ( 'DELETE' , util . format ( masks . document , id ) , callback , handlers , undefined , headers ) ;
446
459
} else if ( Array . isArray ( document ) ) {
447
460
var batch = [ ] ;
448
461
@@ -456,9 +469,11 @@ var Session = module.exports = (function () {
456
469
}
457
470
} ) ;
458
471
459
- session . _request ( 'POST' , masks . bulk , callback , null , batch , headers , function ( statusCode ) {
472
+ handlers . response = function ( statusCode ) {
460
473
return [ statusCode === 204 ] ;
461
- } , function ( ) {
474
+ } ;
475
+
476
+ handlers . end = function ( ) {
462
477
batch . forEach ( function ( patch ) {
463
478
if ( session . _cache . hasOwnProperty [ id ] ) {
464
479
delete session . _cache [ id ] ;
@@ -468,7 +483,9 @@ var Session = module.exports = (function () {
468
483
delete session . _changes [ id ] ;
469
484
}
470
485
} ) ;
471
- } ) ;
486
+ } ;
487
+
488
+ session . _request ( 'POST' , masks . bulk , callback , handlers , batch , headers ) ;
472
489
}
473
490
} ;
474
491
@@ -486,7 +503,7 @@ var Session = module.exports = (function () {
486
503
var query = util . format ( masks . document , key ) ;
487
504
var headers = etag ? { 'If-Match' : etag } : undefined ;
488
505
489
- this . _request ( 'PATCH' , query , callback , null , patches , headers ) ;
506
+ this . _request ( 'PATCH' , query , callback , undefined , patches , headers ) ;
490
507
}
491
508
} ;
492
509
@@ -496,10 +513,13 @@ var Session = module.exports = (function () {
496
513
}
497
514
498
515
var query = util . format ( masks . document , key ) ;
516
+ var handlers = {
517
+ response : function ( statusCode ) {
518
+ return [ statusCode === 200 ] ;
519
+ }
520
+ } ;
499
521
500
- this . _request ( 'HEAD' , query , callback , undefined , undefined , undefined , function ( statusCode ) {
501
- return [ statusCode === 200 ] ;
502
- } ) ;
522
+ this . _request ( 'HEAD' , query , callback , handlers , undefined , undefined ) ;
503
523
} ;
504
524
505
525
return Session ;
0 commit comments