Skip to content

Commit cffc0b4

Browse files
committed
Fix connection timeout issues
1 parent 7c3919d commit cffc0b4

7 files changed

+1889
-300
lines changed

.prettierrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"printWidth": 100,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"bracketSpacing": true,
6+
"semi": true,
7+
"arrowParens": always
8+
}

lib/cache.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var Cache = function(clientId, options) {
2929
path: options.path ? String(options.path) : '',
3030
storage: options.storage || DEFAULT_STORAGE,
3131
writePerms: options.writePerms || DEFAULT_WRITE_PERMS,
32-
indexLimit: options.indexLimit || DEFAULT_INDEX_LIMIT
32+
indexLimit: options.indexLimit || DEFAULT_INDEX_LIMIT,
3333
};
3434

3535
if (this.params.storage !== 'memory') {
@@ -83,9 +83,14 @@ Cache.prototype.get = function(url, data) {
8383
*/
8484
Cache.prototype.getKey = function(url, data) {
8585
data = data || null;
86-
var saneUrl = String(url).trim().replace(/^\/|\/$/g, '');
87-
var keyData = JSON.stringify([ saneUrl, data ]);
88-
return crypto.createHash('md5').update(keyData).digest('hex');
86+
var saneUrl = String(url)
87+
.trim()
88+
.replace(/^\/|\/$/g, '');
89+
var keyData = JSON.stringify([saneUrl, data]);
90+
return crypto
91+
.createHash('md5')
92+
.update(keyData)
93+
.digest('hex');
8994
};
9095

9196
/**
@@ -94,11 +99,13 @@ Cache.prototype.getKey = function(url, data) {
9499
* @return string
95100
*/
96101
Cache.prototype.getPath = function(url, data) {
97-
return this.params.path.replace(/\/$/, '') +
102+
return (
103+
this.params.path.replace(/\/$/, '') +
98104
'/client.' +
99105
this.params.clientId +
100106
'.' +
101-
Array.prototype.slice.call(arguments).join('.');
107+
Array.prototype.slice.call(arguments).join('.')
108+
);
102109
};
103110

104111
/**
@@ -308,7 +315,10 @@ Cache.prototype.clearIndexes = function(invalid) {
308315
this.clearCache(cachePath);
309316
delete this.indexes[collection][key];
310317
}
311-
} else if (invalid[collection] && this.indexes[collection][invalid[collection]] !== undefined) {
318+
} else if (
319+
invalid[collection] &&
320+
this.indexes[collection][invalid[collection]] !== undefined
321+
) {
312322
// Clear a single index element by key
313323
var key = invalid[collection];
314324
var cachePath = this.getPath(key, 'result');
@@ -368,7 +378,7 @@ Cache.prototype.clearCache = function(cachePath) {
368378
* @return array
369379
*/
370380
Cache.prototype.resultCollections = function(result) {
371-
var collections = result.$collection !== undefined ? [ result.$collection ] : [];
381+
var collections = result.$collection !== undefined ? [result.$collection] : [];
372382
// Combine $collection and $expanded headers
373383
if (result.$expanded !== undefined) {
374384
for (var i = 0; i < result.$expanded.length; i++) {
@@ -381,6 +391,5 @@ Cache.prototype.resultCollections = function(result) {
381391
return collections;
382392
};
383393

384-
385394
// Exports
386395
exports.Cache = Cache;

lib/cache.test.js

+33-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('Cache', function() {
1111
path: '',
1212
storage: 'memory',
1313
writePerms: '0644',
14-
indexLimit: 1000
14+
indexLimit: 1000,
1515
});
1616
});
1717

@@ -20,21 +20,21 @@ describe('Cache', function() {
2020
path: '/',
2121
storage: 'memory',
2222
writePerms: '1234',
23-
indexLimit: 1
23+
indexLimit: 1,
2424
});
2525
assert.deepEqual(cache.params, {
2626
clientId: 'test',
2727
path: '/',
2828
storage: 'memory',
2929
writePerms: '1234',
30-
indexLimit: 1
30+
indexLimit: 1,
3131
});
3232
});
3333

3434
it('throws an error on invalid storage', function() {
3535
try {
3636
var cache = new Cache('test', {
37-
storage: 'fail'
37+
storage: 'fail',
3838
});
3939
assert.fail('oops');
4040
} catch (err) {
@@ -56,16 +56,20 @@ describe('Cache', function() {
5656
});
5757

5858
it('returns response object when cache index found', function() {
59-
cache.put('/', {}, {
60-
$data: 'foo',
61-
$collection: 'bar',
62-
$cached: { 'bar': 1 }
63-
});
59+
cache.put(
60+
'/',
61+
{},
62+
{
63+
$data: 'foo',
64+
$collection: 'bar',
65+
$cached: { bar: 1 },
66+
},
67+
);
6468
var result = cache.get('/', {});
6569
assert.deepEqual(result, {
6670
$data: 'foo',
6771
$collection: 'bar',
68-
$cached: true
72+
$cached: true,
6973
});
7074
});
7175
});
@@ -120,7 +124,7 @@ describe('Cache', function() {
120124
var versions = cache.getVersions();
121125
assert.ok(cache.versions === versions);
122126
assert.deepEqual(versions, {
123-
test: 1
127+
test: 1,
124128
});
125129
});
126130
});
@@ -139,7 +143,7 @@ describe('Cache', function() {
139143
var indexes = cache.getIndex();
140144
assert.ok(cache.indexes === indexes);
141145
assert.deepEqual(indexes, {
142-
test: { '12345': 100 }
146+
test: { '12345': 100 },
143147
});
144148
});
145149
});
@@ -152,22 +156,22 @@ describe('Cache', function() {
152156
response = {
153157
$data: 'foo',
154158
$collection: 'bar',
155-
$cached: { 'bar': 1 }
159+
$cached: { bar: 1 },
156160
};
157161
});
158162

159163
it('sets index, version and result cache', function() {
160164
cache.put('/', {}, response);
161165
assert.deepEqual(cache.getIndex(), {
162-
bar: { '58cd6550e4fe03ea78ee22cf52c759b7': 50 }
166+
bar: { '58cd6550e4fe03ea78ee22cf52c759b7': 50 },
163167
});
164168
assert.deepEqual(cache.getVersions(), {
165-
bar: 1
169+
bar: 1,
166170
});
167171
assert.deepEqual(cache.get('/', {}), {
168172
$data: 'foo',
169173
$collection: 'bar',
170-
$cached: true
174+
$cached: true,
171175
});
172176
});
173177
});
@@ -178,13 +182,13 @@ describe('Cache', function() {
178182
cache.putIndex('bar', '12345', 100);
179183
var indexes = cache.getIndex();
180184
assert.deepEqual(indexes, {
181-
bar: { '12345': 100 }
185+
bar: { '12345': 100 },
182186
});
183187
cache.putIndex('bar2', '123456', 1001);
184188
var indexes2 = cache.getIndex();
185189
assert.deepEqual(indexes2, {
186190
bar: { '12345': 100 },
187-
bar2: { '123456': 1001 }
191+
bar2: { '123456': 1001 },
188192
});
189193
});
190194

@@ -193,7 +197,7 @@ describe('Cache', function() {
193197
cache.putIndex('bar', '12345', 100);
194198
var indexes = cache.getIndex();
195199
assert.deepEqual(indexes, {
196-
bar: { '12345': 100 }
200+
bar: { '12345': 100 },
197201
});
198202
cache.putIndex('bar', '12345', 1001);
199203
var indexes2 = cache.getIndex();
@@ -206,15 +210,19 @@ describe('Cache', function() {
206210
describe('#remove', function() {
207211
it('removes an entry from result cache', function() {
208212
var cache = new Cache('test');
209-
cache.put('/', {}, {
210-
$data: 'foo',
211-
$collection: 'bar',
212-
$cached: { 'bar': 1 }
213-
});
213+
cache.put(
214+
'/',
215+
{},
216+
{
217+
$data: 'foo',
218+
$collection: 'bar',
219+
$cached: { bar: 1 },
220+
},
221+
);
214222
assert.deepEqual(cache.get('/', {}), {
215223
$data: 'foo',
216224
$collection: 'bar',
217-
$cached: true
225+
$cached: true,
218226
});
219227
cache.remove('/', {});
220228
assert.deepEqual(cache.get('/', {}), null);

0 commit comments

Comments
 (0)