Download src/utils/protected-file-write.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 3.55 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/protected-file-write.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/utils/protected-file-write.js
-
curl -L -o protected-file-write.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/utils/protected-file-write.js
3.55 kB
| /** ๊ณต์ ์ฐ์ถ๋ฌผ์ ๊ฒฝ๋ก๋ฅผ ๊ฒ์ฆํ๊ณ ๊ธฐ์กด ํ์ผ ๋ณด์กด ๋๋ ์์ ๊ต์ฒด๋ฅผ ์ํํ๋ค. */ | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const { randomUUID } = require('crypto'); | |
| function safeSegment(value) { | |
| if (typeof value !== 'string' || Buffer.byteLength(value) > 200 || !/^[\p{L}\p{N}_-][\p{L}\p{N}_.-]{0,159}$/u.test(value) || value.endsWith('.') || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/i.test(value)) throw new Error('Unsafe file name'); | |
| return value; | |
| } | |
| function directory(target) { | |
| let absolute = path.resolve(target); | |
| // macOS ๊ธฐ๋ณธ ์์คํ ๋ณ์นญ์ ์ ๊ท ์์น๋ก ์ฎ๊ธด ๋ค ๊ฒ์ฌํ๋ค. ์ฌ์ฉ์ ํ์ symlink๋ ํ์ฉํ์ง ์๋๋ค. | |
| if (process.platform === 'darwin') for (const alias of ['/var', '/tmp', '/etc']) { | |
| if (absolute === alias || absolute.startsWith(alias + '/')) { | |
| if (fs.realpathSync(alias) === '/private' + alias) absolute = '/private' + absolute; | |
| } | |
| } | |
| const root = path.parse(absolute).root; | |
| let current = root; | |
| for (const part of absolute.slice(root.length).split(path.sep).filter(Boolean)) { | |
| current = path.join(current, part); | |
| try { fs.mkdirSync(current); } catch (error) { if (error.code !== 'EEXIST') throw error; } | |
| const stat = fs.lstatSync(current); | |
| if (stat.isSymbolicLink() || !stat.isDirectory()) throw new Error('Unsafe directory: symlink or non-directory'); | |
| } | |
| return absolute; | |
| } | |
| function readRegular(file) { | |
| file = path.join(directory(path.dirname(file)), path.basename(file)); | |
| const stat = fs.lstatSync(file); | |
| if (!stat.isFile() || stat.isSymbolicLink()) throw new Error('Unsafe destination: expected a regular file'); | |
| const fd = fs.openSync(file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW || 0)); | |
| try { if (!fs.fstatSync(fd).isFile()) throw new Error('Unsafe destination'); return fs.readFileSync(fd); } | |
| finally { fs.closeSync(fd); } | |
| } | |
| function writeProtected(root, parts, content, { replace = false, mode = 0o600, allowHidden = false } = {}) { | |
| if (!Array.isArray(parts) || !parts.length) throw new Error('Destination components are required'); | |
| parts.forEach((part, index) => safeSegment(allowHidden && index === parts.length - 1 && typeof part === 'string' && part.startsWith('.') ? part.slice(1) : part)); | |
| const parent = directory(path.join(root, ...parts.slice(0, -1))); | |
| const file = path.join(parent, parts.at(-1)); const data = Buffer.from(content); | |
| try { | |
| const current = readRegular(file); | |
| if (current.equals(data)) return { file, created: false }; | |
| if (!replace) throw new Error('Existing file differs; preserving local content'); | |
| } catch (error) { if (error.code !== 'ENOENT') throw error; } | |
| const temporary = path.join(parent, '.bada-' + randomUUID() + '.tmp'); | |
| try { | |
| fs.writeFileSync(temporary, data, { flag: 'wx', mode }); | |
| fs.chmodSync(temporary, mode); | |
| directory(parent); | |
| if (replace) { | |
| try { readRegular(file); } catch (error) { if (error.code !== 'ENOENT') throw error; } | |
| fs.renameSync(temporary, file); | |
| } else { | |
| // hard link ์์ฑ์ ๋ชฉ์ ์ง๊ฐ ์ด๋ฏธ ์๊ธด ๊ฒฝ์ฐ EEXIST๋ก ์คํจํ๋ฉฐ ๊ธฐ์กด ํ์ผ์ ๊ต์ฒดํ์ง ์๋๋ค. | |
| try { fs.linkSync(temporary, file); } | |
| catch (error) { if (error.code !== 'EEXIST' || !readRegular(file).equals(data)) throw error; } | |
| } | |
| return { file, created: true }; | |
| } finally { try { fs.unlinkSync(temporary); } catch (error) { if (error.code !== 'ENOENT') throw error; } } | |
| } | |
| module.exports = { safeSegment, directory, readRegular, writeProtected }; | |