{"version":3,"file":"atomic-json.d.ts","sourceRoot":"","sources":["../../../src/shared/atomic-json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAQ9B,KAAK,YAAY,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,WAAW,GAAG,eAAe,GAAG,YAAY,GAAG,QAAQ,CAAC,CAAC;AAE7F,KAAK,uBAAuB,GAAG;IAC9B,EAAE,CAAC,EAAE,YAAY,CAAC;IAClB,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAClC,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CACjC,CAAC;AAiBF,wBAAgB,sBAAsB,CACrC,OAAO,GAAE,uBAA4B,GACnC,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAkC7C;AAED,eAAO,MAAM,eAAe,6CAA2B,CAAC;AACxD,eAAO,MAAM,sBAAsB,6CAA0C,CAAC","sourcesContent":["import * as fs from \"node:fs\";\nimport * as path from \"node:path\";\nimport {\n\tDEFAULT_FILE_SYSTEM_RETRY_DELAYS_MS,\n\trunFileSystemOperationWithRetry,\n\twaitForFileSystemRetry,\n} from \"./file-system-retry.ts\";\n\ntype AtomicJsonFs = Pick<typeof fs, \"mkdirSync\" | \"writeFileSync\" | \"renameSync\" | \"rmSync\">;\n\ntype AtomicJsonWriterOptions = {\n\tfs?: AtomicJsonFs;\n\tnow?: () => number;\n\tpid?: number;\n\trandom?: () => number;\n\tmode?: number;\n\tretryRenameErrors?: boolean;\n\tretryDirectoryErrors?: boolean;\n\tretryDelaysMs?: readonly number[];\n\twait?: (delayMs: number) => void;\n};\n\nfunction renameWithRetry(\n\tfsImpl: AtomicJsonFs,\n\tsourcePath: string,\n\ttargetPath: string,\n\tretryDelaysMs: readonly number[],\n\twait: (delayMs: number) => void,\n): void {\n\trunFileSystemOperationWithRetry(\n\t\t() => {\n\t\t\tfsImpl.renameSync(sourcePath, targetPath);\n\t\t},\n\t\t{ retryDelaysMs, wait },\n\t);\n}\n\nexport function createAtomicJsonWriter(\n\toptions: AtomicJsonWriterOptions = {},\n): (filePath: string, payload: object) => void {\n\tconst fsImpl = options.fs ?? fs;\n\tconst now = options.now ?? Date.now;\n\tconst pid = options.pid ?? process.pid;\n\tconst random = options.random ?? Math.random;\n\tconst mode = options.mode;\n\tconst retryRenameErrors = options.retryRenameErrors ?? process.platform === \"win32\";\n\tconst retryDirectoryErrors = options.retryDirectoryErrors ?? retryRenameErrors;\n\tconst retryDelaysMs = options.retryDelaysMs ?? DEFAULT_FILE_SYSTEM_RETRY_DELAYS_MS;\n\tconst renameRetryDelaysMs = retryRenameErrors ? retryDelaysMs : [];\n\tconst directoryRetryDelaysMs = retryDirectoryErrors ? retryDelaysMs : [];\n\tconst wait = options.wait ?? waitForFileSystemRetry;\n\treturn (filePath: string, payload: object): void => {\n\t\trunFileSystemOperationWithRetry(\n\t\t\t() => {\n\t\t\t\tfsImpl.mkdirSync(path.dirname(filePath), { recursive: true });\n\t\t\t},\n\t\t\t{ retryDelaysMs: directoryRetryDelaysMs, wait },\n\t\t);\n\t\tconst tempPath = path.join(\n\t\t\tpath.dirname(filePath),\n\t\t\t`.${path.basename(filePath)}.${pid}.${now()}.${random().toString(36).slice(2)}.tmp`,\n\t\t);\n\t\ttry {\n\t\t\tfsImpl.writeFileSync(\n\t\t\t\ttempPath,\n\t\t\t\tJSON.stringify(payload, null, 2),\n\t\t\t\tmode === undefined ? \"utf-8\" : { encoding: \"utf-8\", mode },\n\t\t\t);\n\t\t\trenameWithRetry(fsImpl, tempPath, filePath, renameRetryDelaysMs, wait);\n\t\t} finally {\n\t\t\tfsImpl.rmSync(tempPath, { force: true });\n\t\t}\n\t};\n}\n\nexport const writeAtomicJson = createAtomicJsonWriter();\nexport const writePrivateAtomicJson = createAtomicJsonWriter({ mode: 0o600 });\n"]}