utils.js (1398B)
1 import crypto from 'crypto'; 2 3 export const LOCK_TTL_MS = 2 * 60 * 1000; 4 export const MAX_CONTENT_BYTES = 1024 * 1024; 5 6 export function secureToken() { 7 return crypto.randomBytes(32).toString('hex'); 8 } 9 10 export function newId() { 11 return crypto.randomUUID(); 12 } 13 14 export function parseExpiresIn(expiresIn) { 15 const now = Date.now(); 16 switch (expiresIn) { 17 case '1h': 18 return new Date(now + 60 * 60 * 1000).toISOString(); 19 case '24h': 20 return new Date(now + 24 * 60 * 60 * 1000).toISOString(); 21 case '7d': 22 return new Date(now + 7 * 24 * 60 * 60 * 1000).toISOString(); 23 case '30d': 24 return new Date(now + 30 * 24 * 60 * 60 * 1000).toISOString(); 25 case 'never': 26 return null; 27 default: 28 return undefined; 29 } 30 } 31 32 export function isExpired(expiresAt) { 33 if (expiresAt == null) return false; 34 return Date.parse(expiresAt) <= Date.now(); 35 } 36 37 export function assertMode(mode) { 38 return mode === 'markdown' || mode === 'org'; 39 } 40 41 export function getContentByteLength(content) { 42 return Buffer.byteLength(content ?? '', 'utf8'); 43 } 44 45 export function assertContentSize(content) { 46 return getContentByteLength(content) <= MAX_CONTENT_BYTES; 47 } 48 49 export function lockExpiresAtFromNow() { 50 return new Date(Date.now() + LOCK_TTL_MS).toISOString(); 51 } 52 53 export function sendError(res, status, error, message) { 54 return res.status(status).json({ error, message }); 55 }