Skip to content

Commit 618c70c

Browse files
authored
deps: remove promise-inflight (#223)
The use of promise-inflight was originally intended to prevent duplicate concurrent git ls-remote calls. However, this scenario is rare, and the performance impact of duplicate calls is minimal. The existing LRU cache already prevents redundant requests over time. Removing promise-inflight simplifies the code and removes a dependency often cited by security linters. This does mean duplicate inflight requests are now possible, but the expected impact is negligible. Should we want to make sure they don't happen, we could inline a solution fairly easily
1 parent 29dfb33 commit 618c70c

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

lib/revs.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
const pinflight = require('promise-inflight')
21
const spawn = require('./spawn.js')
32
const { LRUCache } = require('lru-cache')
3+
const linesToRevs = require('./lines-to-revs.js')
44

55
const revsCache = new LRUCache({
66
max: 100,
77
ttl: 5 * 60 * 1000,
88
})
99

10-
const linesToRevs = require('./lines-to-revs.js')
11-
1210
module.exports = async (repo, opts = {}) => {
1311
if (!opts.noGitRevCache) {
1412
const cached = revsCache.get(repo)
@@ -17,12 +15,8 @@ module.exports = async (repo, opts = {}) => {
1715
}
1816
}
1917

20-
return pinflight(`ls-remote:${repo}`, () =>
21-
spawn(['ls-remote', repo], opts)
22-
.then(({ stdout }) => linesToRevs(stdout.trim().split('\n')))
23-
.then(revs => {
24-
revsCache.set(repo, revs)
25-
return revs
26-
})
27-
)
18+
const { stdout } = await spawn(['ls-remote', repo], opts)
19+
const revs = linesToRevs(stdout.trim().split('\n'))
20+
revsCache.set(repo, revs)
21+
return revs
2822
}

test/revs.js

+25
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,28 @@ t.test('check the revs', async t => {
120120
Object.keys(r.shas).forEach(sha => r.shas[sha].forEach(ref =>
121121
t.equal(r.refs[ref].sha, sha, `shas list is consistent ${ref}`)))
122122
})
123+
124+
t.test('cache prevents repeated git ls-remote calls', async t => {
125+
let callCount = 0
126+
const originalSpawn = require('../lib/spawn.js')
127+
128+
// Mock `spawn` to track calls
129+
require.cache[require.resolve('../lib/spawn.js')].exports = (...args) => {
130+
callCount++
131+
return originalSpawn(...args)
132+
}
133+
134+
// Force reloading revs.js after modifying spawn
135+
delete require.cache[require.resolve('../lib/revs.js')]
136+
const revsModule = require('../lib/revs.js')
137+
138+
await revsModule(repo) // First call should hit `git ls-remote`
139+
await revsModule(repo) // Second call should use cache
140+
141+
t.equal(callCount, 1, 'git ls-remote should be called only once due to caching')
142+
143+
// Restore original spawn.js
144+
t.teardown(() => {
145+
require.cache[require.resolve('../lib/spawn.js')].exports = originalSpawn
146+
})
147+
})

0 commit comments

Comments
 (0)