{"version":3,"file":"bunLockfile.cjs","names":["fs","crypto","path"],"sources":["../src/bunLockfile.ts"],"sourcesContent":["import crypto from 'node:crypto';\nimport fs from 'node:fs';\nimport path from 'node:path';\n\n/**\n * Anchored to `\", ` so only a registry entry's `resolved` slot is cleared. A direct tarball\n * dependency carries its URL in the workspace descriptor and tuple identifier instead, and those\n * URLs must remain intact.\n */\nconst guardResolvedUrlPattern = /(\", )\"https:\\/\\/npm\\.flatt\\.tech\\/[^\"]*\"/g;\n\n/**\n * Remove Takumi Guard proxy URLs from the nearest enclosing bun.lock.\n *\n * Bun writes the workspace root's lockfile even when an install runs from a child package. In a\n * Git checkout the search climbs to that root; without a checkout boundary it inspects only the\n * starting directory so it cannot rewrite an unrelated ancestor's lockfile. The proxy is\n * install-time policy rather than repository state: an empty resolved slot still makes Bun use the\n * configured default registry.\n *\n * @returns The normalized lockfile path, or undefined when no change was needed.\n */\nexport function normalizeBunLockfile(startDirPath: string): string | undefined {\n  const lockfilePath = findBunLockfilePath(startDirPath);\n  if (!lockfilePath) return;\n\n  const content = fs.readFileSync(lockfilePath, 'utf8');\n  const normalizedContent = content.replaceAll(guardResolvedUrlPattern, '$1\"\"');\n  if (normalizedContent === content) return;\n\n  // A sibling temp file makes the replacement an atomic same-directory rename, so an interrupted\n  // write cannot leave a truncated bun.lock behind.\n  const temporaryPath = `${lockfilePath}.normalizing.${process.pid}.${crypto.randomUUID()}`;\n  try {\n    // stat's mode includes file-type bits, which chmod is not specified to accept.\n    const mode = fs.statSync(lockfilePath).mode & 0o777;\n    fs.writeFileSync(temporaryPath, normalizedContent, { mode });\n    // writeFileSync's mode is masked by the process umask; chmod restores the exact original bits.\n    fs.chmodSync(temporaryPath, mode);\n    fs.renameSync(temporaryPath, lockfilePath);\n  } finally {\n    fs.rmSync(temporaryPath, { force: true });\n  }\n  return lockfilePath;\n}\n\nfunction findBunLockfilePath(startDirPath: string): string | undefined {\n  const resolvedStartDirPath = path.resolve(startDirPath);\n  const repositoryRootDirPath = findRepositoryRootDirPath(resolvedStartDirPath);\n  if (!repositoryRootDirPath) {\n    const lockfilePath = path.join(resolvedStartDirPath, 'bun.lock');\n    return fs.existsSync(lockfilePath) ? lockfilePath : undefined;\n  }\n\n  let dirPath = resolvedStartDirPath;\n  while (true) {\n    const lockfilePath = path.join(dirPath, 'bun.lock');\n    if (fs.existsSync(lockfilePath)) return lockfilePath;\n    if (dirPath === repositoryRootDirPath) return;\n\n    dirPath = path.dirname(dirPath);\n  }\n}\n\nfunction findRepositoryRootDirPath(startDirPath: string): string | undefined {\n  let dirPath = startDirPath;\n  while (true) {\n    if (fs.existsSync(path.join(dirPath, '.git'))) return dirPath;\n\n    const parentDirPath = path.dirname(dirPath);\n    if (parentDirPath === dirPath) return;\n    dirPath = parentDirPath;\n  }\n}\n"],"mappings":"qMASA,MAAM,EAA0B,4CAahC,SAAgB,EAAqB,EAA0C,CAC7E,IAAM,EAAe,EAAoB,CAAY,EACrD,GAAI,CAAC,EAAc,OAEnB,IAAM,EAAUA,EAAAA,QAAG,aAAa,EAAc,MAAM,EAC9C,EAAoB,EAAQ,WAAW,EAAyB,MAAM,EAC5E,GAAI,IAAsB,EAAS,OAInC,IAAM,EAAgB,GAAG,EAAa,eAAe,QAAQ,IAAI,GAAGC,EAAAA,QAAO,WAAW,IACtF,GAAI,CAEF,IAAM,EAAOD,EAAAA,QAAG,SAAS,CAAY,CAAC,CAAC,KAAO,IAC9C,EAAA,QAAG,cAAc,EAAe,EAAmB,CAAE,MAAK,CAAC,EAE3D,EAAA,QAAG,UAAU,EAAe,CAAI,EAChC,EAAA,QAAG,WAAW,EAAe,CAAY,CAC3C,QAAU,CACR,EAAA,QAAG,OAAO,EAAe,CAAE,MAAO,EAAK,CAAC,CAC1C,CACA,OAAO,CACT,CAEA,SAAS,EAAoB,EAA0C,CACrE,IAAM,EAAuBE,EAAAA,QAAK,QAAQ,CAAY,EAChD,EAAwB,EAA0B,CAAoB,EAC5E,GAAI,CAAC,EAAuB,CAC1B,I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