{"_id":"locko","_rev":"4570107","name":"locko","description":"A simple in-process locking mechanism for critical sections of code.","dist-tags":{"latest":"1.1.0"},"maintainers":[{"name":"rschmidt","email":""}],"time":{"modified":"2026-04-10T15:25:12.000Z","created":"2019-06-30T08:24:14.739Z","1.1.0":"2023-12-23T19:23:33.549Z","1.0.0":"2023-04-07T22:58:58.734Z","0.0.3":"2019-06-30T08:34:40.146Z","0.0.2":"2019-06-30T08:29:32.113Z","0.0.1":"2019-06-30T08:28:23.964Z","0.0.0":"2019-06-30T08:24:14.739Z"},"users":{},"author":{"name":"mistval"},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"versions":{"1.1.0":{"name":"locko","version":"1.1.0","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint .","lintfix":"eslint --fix ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^8.37.0","eslint-config-airbnb":"^19.0.4","eslint-config-airbnb-base":"^15.0.0","eslint-plugin-import":"^2.27.5","mocha":"^10.2.0"},"_id":"locko@1.1.0","gitHead":"323cba21a3979fc9236ff661c6d21c95a77520cb","types":"./index.d.ts","_nodeVersion":"20.9.0","_npmVersion":"10.1.0","dist":{"shasum":"43930d87a0d25f20c8d2974c3c4613e50a2978a9","size":2761,"noattachment":false,"key":"/locko/-/locko-1.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-1.1.0.tgz"},"_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"directories":{},"maintainers":[{"name":"rschmidt","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_1.1.0_1703359413368_0.4995645411870573"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-12-23T19:23:33.549Z","publish_time":1703359413549,"_source_registry_name":"default","_cnpm_publish_time":1703359413549},"1.0.0":{"name":"locko","version":"1.0.0","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint .","lintfix":"eslint --fix ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^8.37.0","eslint-config-airbnb":"^19.0.4","eslint-config-airbnb-base":"^15.0.0","eslint-plugin-import":"^2.27.5","mocha":"^10.2.0"},"gitHead":"d3242e2098241100e692dac362564b5eb15c8d58","_id":"locko@1.0.0","_nodeVersion":"18.15.0","_npmVersion":"9.5.0","dist":{"shasum":"384bdb3a14fad5b1c22cdcb228b76165ac58c591","size":2649,"noattachment":false,"key":"/locko/-/locko-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-1.0.0.tgz"},"_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"directories":{},"maintainers":[{"name":"rschmidt","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_1.0.0_1680908338610_0.29341840937950825"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2023-04-07T22:58:58.734Z","publish_time":1680908338734,"_cnpm_publish_time":1680908338734},"0.0.3":{"name":"locko","version":"0.0.3","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^5.3.0","eslint-config-airbnb-base":"*","eslint-plugin-import":"^2.14.0"},"gitHead":"4fbbec81f7f216388eccfff8177ec00e322e2836","_id":"locko@0.0.3","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"dist":{"shasum":"872ebc44f81d200099a9f71f9adb5065ef339c23","size":2356,"noattachment":false,"key":"/locko/-/locko-0.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-0.0.3.tgz"},"maintainers":[{"name":"rschmidt","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_0.0.3_1561883680007_0.955232842485152"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-24T03:42:56.502Z","publish_time":1561883680146,"_cnpm_publish_time":1561883680146},"0.0.2":{"name":"locko","version":"0.0.2","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^5.3.0","eslint-config-airbnb-base":"*","eslint-plugin-import":"^2.14.0"},"gitHead":"dd7d8fcb9443e8cb8e1441c962f853f24ba11bad","_id":"locko@0.0.2","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"dist":{"shasum":"46e9a0323baf337c72306047966a21781c28fbf8","size":2193,"noattachment":false,"key":"/locko/-/locko-0.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-0.0.2.tgz"},"maintainers":[{"name":"rschmidt","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_0.0.2_1561883371948_0.03709485229026099"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-24T03:42:55.629Z","publish_time":1561883372113,"_cnpm_publish_time":1561883372113},"0.0.1":{"name":"locko","version":"0.0.1","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^5.3.0","eslint-config-airbnb-base":"*","eslint-plugin-import":"^2.14.0"},"gitHead":"fe06f3141590f986eaccafbf2e231381e5317397","_id":"locko@0.0.1","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"dist":{"shasum":"77bf9c206b065184b89d217dd0a447cc58d7d49f","size":1526,"noattachment":false,"key":"/locko/-/locko-0.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-0.0.1.tgz"},"maintainers":[{"name":"rschmidt","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_0.0.1_1561883303842_0.15072576793609005"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-24T03:42:54.774Z","publish_time":1561883303964,"_cnpm_publish_time":1561883303964},"0.0.0":{"name":"locko","version":"0.0.0","description":"A simple in-process locking mechanism for critical sections of code.","main":"index.js","scripts":{"test":"mocha ./tests.js","lint":"eslint ."},"repository":{"type":"git","url":"git+https://github.com/mistval/locko.git"},"keywords":["lock","locking","mutex","transaction","atomic","critical","code"],"author":{"name":"mistval"},"license":"MIT","bugs":{"url":"https://github.com/mistval/locko/issues"},"homepage":"https://github.com/mistval/locko#readme","devDependencies":{"eslint":"^5.3.0","eslint-config-airbnb-base":"*","eslint-plugin-import":"^2.14.0"},"_id":"locko@0.0.0","_npmVersion":"6.4.1","_nodeVersion":"10.14.2","_npmUser":{"name":"rschmidt","email":"randallschmidt1@gmail.com"},"dist":{"shasum":"f620ee84fa65e2ffb1a5afaee8d12b6f11cb41af","size":1522,"noattachment":false,"key":"/locko/-/locko-0.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/locko/download/locko-0.0.0.tgz"},"maintainers":[{"name":"rschmidt","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/locko_0.0.0_1561883054590_0.044243640116475635"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2021-12-24T03:42:53.905Z","publish_time":1561883054739,"_cnpm_publish_time":1561883054739}},"readme":"# Locko\n\nLocko is a small package for implementing exclusive locking on critical sections of code.\n\n## When to use\n\nUse locko when you need to ensure that only one \"thread\" can be inside of a section of code at once. Consider the following code.\n\n```js\nconst util = require('util');\nconst sleep = util.promisify(setTimeout);\n\nasync function print() {\n  console.log('First');\n  await sleep(100);\n  console.log('Second');\n  await sleep(100);\n  console.log('Third');\n}\n\nprint();\nprint();\nprint();\n```\n\nWithout locko, this will print:\n\n```\nFirst\nFirst\nFirst\nSecond\nSecond\nSecond\nThird\nThird\nThird\n```\n\nNow add locko:\n\n```js\nconst util = require('util');\nconst sleep = util.promisify(setTimeout);\n\nasync function print() {\n  await locko.lock('print');\n\n  console.log('First');\n  await sleep(100);\n  console.log('Second');\n  await sleep(100);\n  console.log('Third');\n\n  locko.unlock('print');\n}\n\nprint();\nprint();\nprint();\n```\n\nThis will now print:\n\n```\nFirst\nSecond\nThird\nFirst\nSecond\nThird\nFirst\nSecond\nThird\n```\n\n## A real world example\n\nIt's often the case that you need to both read and write to a file, but without any kind of synchronization strategy, it's possible to read from a file while it's being written, or write to a file while it's being read, or have more than one writer writing at once. This is very likely to cause failures in an application.\n\nUsing locko you can ensure that no more than one operation is being executed on a file at any given time.\n\n```js\nasync function readFile(filePath) {\n  await locko.lock(filePath);\n\n  return new Promise((fulfill, reject) => {\n    fs.readFile(filePath, (err, content) => {\n      locko.unlock(filePath);\n\n      if (err) {\n        reject(err);\n      } else {\n        fulfill(content);\n      }\n    }); \n  }); \n}\n\nasync function writeFile(filePath, content) {\n  await locko.lock(filePath);\n\n  return new Promise((fulfill, reject) => {\n    fs.writeFile(filePath, content, (err) => {\n      locko.unlock(filePath);\n\n      if (err) {\n        reject(err);\n      } else {\n        fulfill();\n      }\n    });\n  });\n}\n```\n\n## Best practices\n\nIf you never unlock the lock, it will remain locked forever. Therefore you should usually use a try-finally to ensure that the lock gets released. For example:\n\n```js\nasync function print() {\n  try {\n    await locko.lock('print');\n\n    console.log('First');\n    await wait(100);\n    console.log('Second');\n    await wait(100);\n    console.log('Third');\n  } finally {\n    locko.unlock('print');\n  }\n}\n```\n\nAlternatively, you can use the `doWithLock()` wrapper function that handles unlocking safely for you:\n\n```js\nasync function print() {\n  await locko.doWithLock('print', async () => {\n    console.log('First');\n    await wait(100);\n    console.log('Second');\n    await wait(100);\n    console.log('Third');\n  });\n}\n```\n","_attachments":{},"homepage":"https://github.com/mistval/locko#readme","bugs":{"url":"https://github.com/mistval/locko/issues"},"license":"MIT"}