Skip to content

Commit 37c2891

Browse files
iarnaisaacs
authored andcommitted
fix(cache): Switch to lru-cache to save ourselves from unlimited memory consumption
PR-URL: #38 Credit: @iarna Close: #38 Reviewed-by: @isaacs BREAKING CHANGE: Drop support for node 0.x
1 parent cbf63b7 commit 37c2891

File tree

3 files changed

+791
-1021
lines changed

3 files changed

+791
-1021
lines changed

index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
var url = require('url')
33
var gitHosts = require('./git-host-info.js')
44
var GitHost = module.exports = require('./git-host.js')
5+
var LRU = require('lru-cache')
6+
var cache = new LRU({max: 1000})
57

68
var protocolToRepresentationMap = {
79
'git+ssh:': 'sshurl',
@@ -22,17 +24,15 @@ var authProtocols = {
2224
'git+http:': true
2325
}
2426

25-
var cache = {}
26-
2727
module.exports.fromUrl = function (giturl, opts) {
2828
if (typeof giturl !== 'string') return
2929
var key = giturl + JSON.stringify(opts || {})
3030

31-
if (!(key in cache)) {
32-
cache[key] = fromUrl(giturl, opts)
31+
if (!cache.has(key)) {
32+
cache.set(key, fromUrl(giturl, opts))
3333
}
3434

35-
return cache[key]
35+
return cache.get(key)
3636
}
3737

3838
function fromUrl (giturl, opts) {

0 commit comments

Comments
 (0)