(filePath, data, options = 'utf8')
| 27 | |
| 28 | // Atomic file write: write to temp file then rename to prevent corruption |
| 29 | const atomicWriteFileSync = (filePath, data, options = 'utf8') => { |
| 30 | const dir = path.dirname(filePath); |
| 31 | if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 32 | const tempPath = path.join(dir, `.${path.basename(filePath)}.${crypto.randomBytes(6).toString('hex')}.tmp`); |
| 33 | try { |
| 34 | fs.writeFileSync(tempPath, data, options); |
| 35 | // Retry rename for Windows file locking issues |
| 36 | let retries = 5; |
| 37 | while (retries > 0) { |
| 38 | try { |
| 39 | fs.renameSync(tempPath, filePath); |
| 40 | break; |
| 41 | } catch (e) { |
| 42 | if (retries === 1) throw e; |
| 43 | retries--; |
| 44 | // Synchronous delay |
| 45 | const start = Date.now(); |
| 46 | while (Date.now() - start < 50) {} |
| 47 | } |
| 48 | } |
| 49 | } catch (err) { |
| 50 | if (fs.existsSync(tempPath)) { |
| 51 | try { fs.unlinkSync(tempPath); } catch (e) {} |
| 52 | } |
| 53 | throw err; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | const app = express(); |
| 58 | const DEFAULT_PORT = 1920; |
no outgoing calls
no test coverage detected