{"id":17149539,"url":"https://github.com/mohd-akram/os-lock","last_synced_at":"2025-07-26T00:32:44.067Z","repository":{"id":57316413,"uuid":"145450135","full_name":"mohd-akram/os-lock","owner":"mohd-akram","description":"Cross-platform file locking for Node.js","archived":false,"fork":false,"pushed_at":"2024-01-29T16:37:54.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-15T17:01:44.894Z","etag":null,"topics":["cross-platform","file","lock","locking","node","nodejs","npm","os"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/os-lock","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mohd-akram.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-20T17:34:58.000Z","updated_at":"2025-01-01T16:28:03.000Z","dependencies_parsed_at":"2022-08-25T22:03:39.589Z","dependency_job_id":null,"html_url":"https://github.com/mohd-akram/os-lock","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/mohd-akram/os-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohd-akram%2Fos-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohd-akram%2Fos-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohd-akram%2Fos-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohd-akram%2Fos-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohd-akram","download_url":"https://codeload.github.com/mohd-akram/os-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohd-akram%2Fos-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267093810,"owners_count":24034950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","file","lock","locking","node","nodejs","npm","os"],"created_at":"2024-10-14T21:33:23.216Z","updated_at":"2025-07-26T00:32:43.766Z","avatar_url":"https://github.com/mohd-akram.png","language":"C","readme":"# os-lock\n\nCross-platform file locking. Uses `fcntl` on UNIX and\n`LockFileEx`/`UnlockFileEx` on Windows.\n\n## Install\n\n    npm install os-lock\n\n## Usage\n\n### Synopsis\n\n```typescript\nasync function lock(fd, options);\nasync function lock(\n  fd,\n  start = 0,\n  length = 0,\n  options = { exclusive: false, immediate: false }\n);\nasync function unlock(fd, start = 0, length = 0);\n```\n\n### Arguments\n\n- `fd` - File descriptor.\n- `start` - Starting offset for lock.\n- `length` - Number of bytes to lock. If 0, lock till end of file.\n- `exclusive` - Acquire an exclusive lock (i.e. write lock).\n- `immediate` - Throw if a conflicting lock is present instead of waiting.\n\nAn error thrown due to `immediate` will have its `code` property set to\n`EACCES`, `EAGAIN` or `EBUSY`.\n\n### Example\n\n```javascript\nimport cluster from \"cluster\";\nimport fs from \"fs/promises\";\n\nimport { lock, unlock } from \"os-lock\";\n\nconst filename = \"file\";\n\nasync function primary() {\n  const file = await fs.open(filename, \"wx\");\n  // Get an exclusive lock\n  await lock(file.fd, { exclusive: true });\n  // Fork a new process which will attempt to get a lock for itself\n  // (see the worker() function below)\n  const worker = cluster.fork();\n  // Pretend to do work and unlock the file after 3 seconds\n  setTimeout(async () =\u003e {\n    await unlock(file.fd);\n    await file.close();\n  }, 3000);\n  // Delete the file after the worker is done with it\n  worker.on(\"disconnect\", async () =\u003e await fs.unlink(filename));\n}\n\nasync function worker() {\n  const file = await fs.open(filename, \"r+\");\n  console.time(\"got lock after\");\n  try {\n    // Try (and fail) to get a lock immediately\n    await lock(file.fd, { immediate: true });\n  } catch (e) {\n    if (!/^(EACCES|EAGAIN|EBUSY)$/.test(e.code)) throw e;\n    console.warn(\"failed to get lock immediately\");\n  }\n  // Try again, this time with patience\n  await lock(file.fd);\n  console.timeEnd(\"got lock after\");\n  await unlock(file.fd);\n  cluster.worker.disconnect();\n}\n\nif (cluster.isPrimary) primary();\nelse worker();\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohd-akram%2Fos-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohd-akram%2Fos-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohd-akram%2Fos-lock/lists"}