MCPcopy Create free account
hub / github.com/npm/cli / withLock

Function withLock

workspaces/libnpmexec/lib/with-lock.js:48–67  ·  view source on GitHub ↗

* Acquire an advisory lock for the given path and hold it for the duration of the callback. * * The lock will be released automatically when the callback resolves or rejects. * Concurrent calls to withLock() for the same path will wait until the lock is released.

(lockPath, cb)

Source from the content-addressed store, hash-verified

46 * Concurrent calls to withLock() for the same path will wait until the lock is released.
47 */
48async function withLock (lockPath, cb) {
49 try {
50 const signal = await acquireLock(lockPath)
51 return await new Promise((resolve, reject) => {
52 signal.addEventListener('abort', () => {
53 reject(Object.assign(new Error('Lock compromised'), { code: 'ECOMPROMISED' }))
54 });
55
56 (async () => {
57 try {
58 resolve(await cb(signal))
59 } catch (err) {
60 reject(err)
61 }
62 })()
63 })
64 } finally {
65 releaseLock(lockPath)
66 }
67}
68
69function acquireLock (lockPath) {
70 return promiseRetry(async (retry) => {

Callers 2

execFunction · 0.85
with-lock.jsFile · 0.85

Calls 3

acquireLockFunction · 0.85
resolveFunction · 0.85
releaseLockFunction · 0.85

Tested by

no test coverage detected