Skip to content

Commit 3f6aa44

Browse files
author
Matt Daly
committed
Updated readme. Simplified internal _request function, now uses a handlers object parameter rather than individual parameters
1 parent b2439c6 commit 3f6aa44

File tree

1 file changed

+107
-87
lines changed

1 file changed

+107
-87
lines changed

lib/session.js

+107-87
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var Session = module.exports = (function () {
4040
index: '/indexes/%s?query=%s'
4141
};
4242

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 || { };
4545

4646
var options = {
4747
host: session._connection.host,
@@ -57,41 +57,40 @@ var Session = module.exports = (function () {
5757
var request = http.request(options);
5858

5959
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+
});
6369

6470
if (response.statusCode === 409) {
6571
error = new Error('Concurrency conflict, no changes were performed. The specified etag did not match the current document etag.');
6672
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));
7775
} else if (response.statusCode === 400) {
7876
error = new Error('Query Failed: The request url was badly formed. Ensure no parameters contain illegal characters');
7977
error.statusCode = 400;
8078
}
8179

8280
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);
9293
}
93-
} catch (e) {
94-
error = new Error('Parse Error: ' + e.message);
9594
}
9695

9796
if (error) {
@@ -105,8 +104,8 @@ var Session = module.exports = (function () {
105104
callback(error);
106105
}
107106
} else {
108-
if (endHandler) {
109-
endHandler();
107+
if (typeof handlers.end === 'function') {
108+
handlers.end();
110109
}
111110

112111
if (callback) {
@@ -138,7 +137,7 @@ var Session = module.exports = (function () {
138137
return;
139138
}
140139

141-
var session = this;
140+
var session = this, handlers = { };
142141

143142
if (typeof includes === 'function' && callback === undefined) {
144143
callback = includes;
@@ -194,60 +193,65 @@ var Session = module.exports = (function () {
194193
if (length === 0) {
195194
callback(undefined, document, new Queryable(cachedIncludes));
196195
} 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);
209212
}
210213
} else {
211214
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 ]);
220222
}
221223

222224
} else {
223225
// no includes
224226
if (cached) {
225227
callback(undefined, JSON.parse(cached));
226228
} 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);
232236
}
233237
}
234238
} else if (Array.isArray(keys)) {
235239
// it's an array of keys
236240
// batches - we're not checking the cache here - whats the likelihood of every document being cached?
237241
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);
246249
}
247250
};
248251

249252
Session.prototype._buildSpecifications = function (specifications) {
250253
query = '';
254+
251255
if (typeof specifications === 'object') {
252256
if (Array.isArray(specifications.projections)) {
253257
query += '&' + qs.stringify({ fetch: specifications.projections }, '&', '=');
@@ -269,6 +273,7 @@ var Session = module.exports = (function () {
269273
query += '&pageSize=' + specifications.pageSize;
270274
}
271275
}
276+
272277
return query;
273278
}
274279

@@ -291,8 +296,9 @@ var Session = module.exports = (function () {
291296

292297
query += this._buildSpecifications(specifications);
293298

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+
}
296302
});
297303
};
298304

@@ -310,8 +316,9 @@ var Session = module.exports = (function () {
310316

311317
query += this._buildSpecifications(specifications);
312318

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+
}
315322
});
316323
};
317324

@@ -352,13 +359,13 @@ var Session = module.exports = (function () {
352359
return;
353360
}
354361

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;
362369

363370
for (key in changes) {
364371
if (changes.hasOwnProperty(key) && cache.hasOwnProperty(key)) {
@@ -395,25 +402,29 @@ var Session = module.exports = (function () {
395402
return;
396403
}
397404

398-
session._request('POST', masks.bulk, callback, function (json) {
405+
handlers.json = function (json) {
399406
var keys = [];
400407

401408
json.forEach(function (data) {
402409
keys.push(data.Key);
403410
});
404411

405412
return [ keys ];
406-
}, batch, undefined, undefined, function () {
413+
};
414+
415+
handlers.end = function () {
407416
session._store = { };
408-
});
417+
};
418+
419+
session._request('POST', masks.bulk, callback, handlers, batch);
409420
};
410421

411422
Session.prototype.delete = function (document, etag, callback) {
412423
if (!document) {
413424
return;
414425
}
415426

416-
var session = this;
427+
var session = this, handlers = { };
417428

418429
if (typeof etag === 'function' && callback === undefined) {
419430
callback = etag;
@@ -433,16 +444,18 @@ var Session = module.exports = (function () {
433444

434445
var id = typeof document === 'string' ? document : document['@metadata']['@id'];
435446
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 () {
438449
if (session._cache.hasOwnProperty[id]) {
439450
delete session._cache[id];
440451
}
441452

442453
if (session._changes.hasOwnProperty[id]) {
443454
delete session._changes[id];
444455
}
445-
});
456+
};
457+
458+
session._request('DELETE', util.format(masks.document, id), callback, handlers, undefined, headers);
446459
} else if (Array.isArray(document)) {
447460
var batch = [];
448461

@@ -456,9 +469,11 @@ var Session = module.exports = (function () {
456469
}
457470
});
458471

459-
session._request('POST', masks.bulk, callback, null, batch, headers, function (statusCode) {
472+
handlers.response = function (statusCode) {
460473
return [ statusCode === 204 ];
461-
}, function () {
474+
};
475+
476+
handlers.end = function () {
462477
batch.forEach(function (patch) {
463478
if (session._cache.hasOwnProperty[id]) {
464479
delete session._cache[id];
@@ -468,7 +483,9 @@ var Session = module.exports = (function () {
468483
delete session._changes[id];
469484
}
470485
});
471-
});
486+
};
487+
488+
session._request('POST', masks.bulk, callback, handlers, batch, headers);
472489
}
473490
};
474491

@@ -486,7 +503,7 @@ var Session = module.exports = (function () {
486503
var query = util.format(masks.document, key);
487504
var headers = etag ? { 'If-Match': etag } : undefined;
488505

489-
this._request('PATCH', query, callback, null, patches, headers);
506+
this._request('PATCH', query, callback, undefined, patches, headers);
490507
}
491508
};
492509

@@ -496,10 +513,13 @@ var Session = module.exports = (function () {
496513
}
497514

498515
var query = util.format(masks.document, key);
516+
var handlers = {
517+
response: function (statusCode) {
518+
return [ statusCode === 200 ];
519+
}
520+
};
499521

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);
503523
};
504524

505525
return Session;

0 commit comments

Comments
 (0)