-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- implements #44 - simple map with automatic key expiration - tests
- Loading branch information
Showing
6 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict' | ||
|
||
const { prefs } = require('sdk/simple-prefs') | ||
const { setInterval, clearInterval } = require('sdk/timers') | ||
|
||
const cache = Object.create(null) | ||
const ttl = 30 * 60 * 1000 | ||
|
||
let size = 0 | ||
let hits = 0 | ||
let misses = 0 | ||
|
||
exports.put = function (key, value, expire) { | ||
if (!cache[key]) { size++ } | ||
cache[key] = { value: value, expire: (expire || Date.now() + ttl) } | ||
// console.log('cache.put: ' + key) | ||
return value | ||
} | ||
exports.get = function (key) { | ||
const data = cache[key] | ||
if (typeof data !== 'undefined') { | ||
hits++ | ||
// console.log('cache.hit: ' + key) | ||
return data.value | ||
} else { | ||
misses++ | ||
// console.log('cache.miss: ' + key) | ||
return undefined | ||
} | ||
} | ||
exports.dropExpired = function () { | ||
// console.log('cache.size=' + size) | ||
// console.log('cache.hits=' + hits) | ||
// console.log('cache.misses=' + misses) | ||
// console.log('cache.dropExpired..') | ||
for (let key in cache) { | ||
const data = cache[key] | ||
if (data.expire && data.expire < Date.now()) { | ||
// console.log('cache.drop: ' + key) | ||
delete cache[key] | ||
cache.size-- | ||
} | ||
} | ||
} | ||
exports.ttl = ttl | ||
|
||
// execute on startup and then on every preference change | ||
let cleanup = null | ||
require('sdk/simple-prefs').on('dns', (function f () { | ||
clearInterval(cleanup) | ||
if (prefs.dns) { | ||
cleanup = setInterval(exports.dropExpired, exports.ttl) | ||
// blacklist hosts which may confuse gateway | ||
exports.put('127.0.0.1', false, null) | ||
exports.put(prefs.customGatewayHost, false, null) | ||
} | ||
return f | ||
})()) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
require('../lib/redirects.js') | ||
|
||
const gw = require('../lib/gateways.js') | ||
const api = require('../lib/api.js') | ||
const tabs = require('sdk/tabs') | ||
const { prefs } = require('sdk/simple-prefs') | ||
const dnsCache = require('../lib/dns-cache.js') | ||
|
||
exports['test a redirect triggered by dnslink'] = function (assert, done) { | ||
const fqdnWithDnslink = 'ipfs.git.sexy' | ||
const sitePath = '/some/path/to-resource#with-hash' | ||
const openURL = 'http://' + fqdnWithDnslink + sitePath | ||
const expectedURL = gw.customUri.spec + 'ipns/' + fqdnWithDnslink + sitePath | ||
|
||
dnsCache.put(fqdnWithDnslink, true) // mock: /api/v0/dns/ returned IPFS Path | ||
assert.equal(api.isDnslinkPresent(fqdnWithDnslink), true, 'fqdnWithDnslink should return true') | ||
|
||
const dnsPref = prefs.dns | ||
prefs.dns = true | ||
gw.redirectEnabled = true | ||
|
||
tabs.open({ | ||
url: openURL, | ||
onReady: function onReady (tab) { | ||
assert.equal(tab.url, expectedURL, 'expected redirect') | ||
prefs.dns = dnsPref | ||
tab.close(done) | ||
} | ||
}) | ||
} | ||
|
||
exports['test DNS Cache expiration'] = function (assert) { | ||
const expired = Date.now() - 1000 | ||
const key = 'foo.test' | ||
const value = true | ||
dnsCache.put(key, value, expired) | ||
assert.equal(dnsCache.get(key), value, 'dnsCache should be a map') | ||
dnsCache.dropExpired() | ||
assert.ok(typeof dnsCache.get(key) === 'undefined', 'dnsCache.dropExpired should drop regular key') | ||
assert.equal(dnsCache.get('127.0.0.1'), false, 'special key should not be dropped') | ||
assert.equal(dnsCache.get(prefs.customGatewayHost), false, 'special key should not be dropped') | ||
} | ||
|
||
require('sdk/test').run(exports) |