(path: string, bytes: Buffer, expectedSha256: string, mode?: number)
| 179 | } |
| 180 | |
| 181 | function ensureFile(path: string, bytes: Buffer, expectedSha256: string, mode?: number): void { |
| 182 | if (readFileSha256(path) === expectedSha256) return; |
| 183 | |
| 184 | mkdirSync(dirname(path), { recursive: true }); |
| 185 | const tempPath = `${path}.${process.pid}.${Date.now()}.tmp`; |
| 186 | writeFileSync(tempPath, bytes, { mode: mode ?? 0o644 }); |
| 187 | |
| 188 | try { |
| 189 | renameSync(tempPath, path); |
| 190 | return; |
| 191 | } catch { |
| 192 | if (readFileSha256(path) === expectedSha256) { |
| 193 | rmSync(tempPath, { force: true }); |
| 194 | return; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | try { |
| 199 | rmSync(path, { force: true }); |
| 200 | renameSync(tempPath, path); |
| 201 | } catch (error) { |
| 202 | rmSync(tempPath, { force: true }); |
| 203 | if (readFileSha256(path) === expectedSha256) return; |
| 204 | throw error; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | function ensureEntryFile(cacheRoot: string): void { |
| 209 | const entryPath = join(cacheRoot, 'node_modules', '.kimi-native-entry.cjs'); |
no test coverage detected