Implement v2 API for stateful sessions and add engagement metrics support

This commit is contained in:
ImBenji
2026-01-02 00:45:14 +00:00
parent 48fce1564a
commit 48e6c7e3c1
7 changed files with 1325 additions and 160 deletions

266
README.md
View File

@@ -6,12 +6,15 @@ Generate Twitter-style quote images programmatically. Perfect for creating socia
## Features ## Features
- 🎨 Twitter-authentic styling - Twitter-authentic styling
- 🖼️ Support for images and text-only tweets - Support for images and text-only tweets
- 📸 High-resolution output (3240x3240) - Verified badge (blue checkmark)
- ⚡ Built-in caching (24-hour TTL) - Engagement metrics (likes, retweets, replies, views)
- 🔒 Base64 image support - High-resolution output (3240x3240)
- 🐳 Docker-ready - Built-in caching (24-hour TTL)
- Base64 image support
- Docker-ready
- v2 API with stateful sessions for incremental updates
## API Endpoints ## API Endpoints
@@ -51,6 +54,8 @@ curl "https://quotes.imbenji.net/generate?displayName=John%20Doe&username=@johnd
| `text` | string | No | Tweet text content | | `text` | string | No | Tweet text content |
| `imageUrl` | string | No | Tweet image URL, base64 data URI, or `null` | | `imageUrl` | string | No | Tweet image URL, base64 data URI, or `null` |
| `timestamp` | integer | No | Unix epoch timestamp in seconds | | `timestamp` | integer | No | Unix epoch timestamp in seconds |
| `verified` | boolean | No | Show verified badge (blue checkmark) next to name |
| `engagement` | object | No | Engagement metrics (likes, retweets, replies, views) |
## Response ## Response
@@ -145,6 +150,251 @@ curl -G "https://quotes.imbenji.net/generate" \
--output quote.png --output quote.png
``` ```
### Verified badge
```javascript
fetch('https://quotes.imbenji.net/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
displayName: 'Elon Musk',
username: '@elonmusk',
text: 'Just bought Twitter!',
verified: true,
timestamp: 1735574400
})
});
```
### Engagement metrics
```javascript
fetch('https://quotes.imbenji.net/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
displayName: 'Popular User',
username: '@viral',
text: 'This tweet went viral!',
verified: true,
engagement: {
replies: 1234,
retweets: 5678,
likes: 98765,
views: 1500000
},
timestamp: 1735574400
})
});
```
**Note:** All four engagement fields (replies, retweets, likes, views) must be provided for the engagement bar to appear. Numbers are automatically formatted (e.g., 1234 → 1.2K, 1500000 → 1.5M).
---
## v2 API (Stateful Sessions)
The v2 API lets you build quote images incrementally. Instead of sending everything in one request, you create a session and update fields as needed. This is more efficient when making multiple edits since you dont need to resend large images every time.
### How it works
1. Create a session with `POST /v2/quote`
2. Update fields with `PATCH /v2/quote/:id` (only send whats changed)
3. Render the image with `GET /v2/quote/:id/image`
Sessions expire after 24 hours of inactivity.
### POST /v2/quote
Create a new session.
**Request:**
```bash
curl -X POST https://quotes.imbenji.net/v2/quote \
-H "Content-Type: application/json" \
-d '{
"displayName": "John Doe",
"username": "@johndoe",
"text": "Hello world"
}'
```
**Response (201):**
```json
{
"id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
"displayName": "John Doe",
"username": "@johndoe",
"text": "Hello world",
"avatarUrl": null,
"imageUrl": null,
"timestamp": null,
"verified": false,
"engagement": null,
"createdAt": 1735600000,
"updatedAt": 1735600000
}
```
### GET /v2/quote/:id
Get current session state.
```bash
curl https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890
```
### PATCH /v2/quote/:id
Update specific fields. Only send the fields you want to change.
```bash
curl -X PATCH https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890 \
-H "Content-Type: application/json" \
-d '{
"text": "Updated text!",
"avatarUrl": "data:image/png;base64,..."
}'
```
**Engagement updates:**
```bash
# Set all engagement metrics
curl -X PATCH https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890 \
-H "Content-Type: application/json" \
-d '{
"engagement": {
"replies": 100,
"retweets": 250,
"likes": 5000,
"views": 50000
}
}'
# Partial update (update only likes, keep other fields)
curl -X PATCH https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890 \
-H "Content-Type: application/json" \
-d '{
"engagement": {
"likes": 10000
}
}'
# Clear engagement entirely (removes engagement bar)
curl -X PATCH https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890 \
-H "Content-Type: application/json" \
-d '{
"engagement": null
}'
```
**Verified badge:**
```bash
# Add verified badge
curl -X PATCH https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890 \
-H "Content-Type: application/json" \
-d '{
"verified": true
}'
```
### GET /v2/quote/:id/image
Render the current session state as a PNG image.
```bash
curl https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890/image --output quote.png
```
**Response headers:**
- `Content-Type: image/png`
- `X-Session-Id: <session-id>`
- `X-Cache: HIT` or `MISS`
### DELETE /v2/quote/:id
Delete a session.
```bash
curl -X DELETE https://quotes.imbenji.net/v2/quote/a1b2c3d4-e5f6-7890-abcd-ef1234567890
```
Returns `204 No Content` on success.
### v2 Example Workflow
```javascript
// 1. Create session with initial data
const res = await fetch('https://quotes.imbenji.net/v2/quote', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
displayName: 'Jane Doe',
username: '@janedoe',
text: 'Working on something cool'
})
});
const session = await res.json();
const sessionId = session.id;
// 2. Later, add an avatar (only sends the avatar, not everything again)
await fetch(`https://quotes.imbenji.net/v2/quote/${sessionId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
avatarUrl: 'data:image/png;base64,iVBORw0KGgo...'
})
});
// 3. Update the text
await fetch(`https://quotes.imbenji.net/v2/quote/${sessionId}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text: 'Finished building something cool!'
})
});
// 4. Generate the final image
const imgRes = await fetch(`https://quotes.imbenji.net/v2/quote/${sessionId}/image`);
const blob = await imgRes.blob();
```
### v2 Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `displayName` | string | Display name |
| `username` | string | Twitter handle with @ |
| `avatarUrl` | string | Avatar image URL or base64 |
| `text` | string | Tweet text content |
| `imageUrl` | string | Tweet image URL or base64 |
| `timestamp` | integer | Unix epoch in seconds |
| `verified` | boolean | Show verified badge (blue checkmark) |
| `engagement` | object/null | Engagement metrics (see below) |
**Engagement object:**
```json
{
"engagement": {
"replies": 89,
"retweets": 567,
"likes": 1234,
"views": 50000
}
}
```
**Engagement behavior:**
- All four fields must be provided for the engagement bar to appear
- Partial updates: Only update specific fields, others remain unchanged
- Set to `null` to clear all engagement and hide the engagement bar
- Numbers are auto-formatted (1234 → 1.2K, 1000000 → 1M)
---
## Caching ## Caching
The API automatically caches generated images for 24 hours from the last request. Identical requests will be served from cache instantly. The API automatically caches generated images for 24 hours from the last request. Identical requests will be served from cache instantly.
@@ -217,7 +467,9 @@ Response:
- **Format:** PNG - **Format:** PNG
- **Max tweet width:** 450px (auto-scaled to fit) - **Max tweet width:** 450px (auto-scaled to fit)
- **Cache TTL:** 24 hours from last access - **Cache TTL:** 24 hours from last access
- **Session TTL:** 24 hours from last update (v2 API)
- **Cleanup interval:** Every hour - **Cleanup interval:** Every hour
- **Database:** SQLite (for v2 sessions)
## Limitations ## Limitations
@@ -232,4 +484,4 @@ For issues or questions, please contact the maintainer.
--- ---
**Built with:** Node.js, Express, node-html-to-image, Puppeteer **Built with:** Node.js, Express, Puppeteer, SQLite

204
api.js
View File

@@ -3,6 +3,8 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const crypto = require('crypto'); const crypto = require('crypto');
const { initPool, renderHtml, POOL_SIZE } = require('./browserPool'); const { initPool, renderHtml, POOL_SIZE } = require('./browserPool');
const v2Routes = require('./v2Routes');
const { cleanupExpiredSessions } = require('./db');
const app = express(); const app = express();
const PORT = 3000; const PORT = 3000;
@@ -26,7 +28,7 @@ app.use((req, res, next) => {
const timestamp = new Date().toISOString(); const timestamp = new Date().toISOString();
console.log(`\n[${timestamp}] ${req.method} ${req.url}`); console.log(`\n[${timestamp}] ${req.method} ${req.url}`);
if (req.method === 'POST' && req.body) { if ((req.method === 'POST' || req.method === 'PATCH') && req.body) {
const logBody = { ...req.body }; const logBody = { ...req.body };
// Truncate long base64 strings for readability // Truncate long base64 strings for readability
@@ -57,6 +59,9 @@ app.use((req, res, next) => {
next(); next();
}); });
// mount v2 api
app.use('/v2', v2Routes);
function normalizeConfig(config) { function normalizeConfig(config) {
// Remove null, undefined, and empty string values // Remove null, undefined, and empty string values
const normalized = {}; const normalized = {};
@@ -172,8 +177,34 @@ function formatTimestamp(epoch) {
return `${hour12}:${minutes} ${ampm} · ${month} ${day}, ${year}`; return `${hour12}:${minutes} ${ampm} · ${month} ${day}, ${year}`;
} }
const TEMPLATE_PATH = path.join(__dirname, 'template.html');
const templateHtml = fs.readFileSync(TEMPLATE_PATH, 'utf8');
function buildEngagementHtml(engagement) {
if (!engagement) return '';
return `
<div class="engagement-bar" role="group">
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M1.751 10c0-4.42 3.584-8 8.005-8h4.366c4.49 0 8.129 3.64 8.129 8.13 0 2.96-1.607 5.68-4.196 7.11l-8.054 4.46v-3.69h-.067c-4.49.1-8.183-3.51-8.183-8.01zm8.005-6c-3.317 0-6.005 2.69-6.005 6 0 3.37 2.77 6.08 6.138 6.01l.351-.01h1.761v2.3l5.087-2.81c1.951-1.08 3.163-3.13 3.163-5.36 0-3.39-2.744-6.13-6.129-6.13H9.756z"></path></g></svg>
<span class="engagement-count">${engagement.replies}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M4.5 3.88l4.432 4.14-1.364 1.46L5.5 7.55V16c0 1.1.896 2 2 2H13v2H7.5c-2.209 0-4-1.79-4-4V7.55L1.432 9.48.068 8.02 4.5 3.88zM16.5 6H11V4h5.5c2.209 0 4 1.79 4 4v8.45l2.068-1.93 1.364 1.46-4.432 4.14-4.432-4.14 1.364-1.46 2.068 1.93V8c0-1.1-.896-2-2-2z"></path></g></svg>
<span class="engagement-count">${engagement.retweets}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M16.697 5.5c-1.222-.06-2.679.51-3.89 2.16l-.805 1.09-.806-1.09C9.984 6.01 8.526 5.44 7.304 5.5c-1.243.07-2.349.78-2.91 1.91-.552 1.12-.633 2.78.479 4.82 1.074 1.97 3.257 4.27 7.129 6.61 3.87-2.34 6.052-4.64 7.126-6.61 1.111-2.04 1.03-3.7.477-4.82-.561-1.13-1.666-1.84-2.908-1.91zm4.187 7.69c-1.351 2.48-4.001 5.12-8.379 7.67l-.503.3-.504-.3c-4.379-2.55-7.029-5.19-8.382-7.67-1.36-2.5-1.41-4.86-.514-6.67.887-1.79 2.647-2.91 4.601-3.01 1.651-.09 3.368.56 4.798 2.01 1.429-1.45 3.146-2.1 4.796-2.01 1.954.1 3.714 1.22 4.601 3.01.896 1.81.846 4.17-.514 6.67z"></path></g></svg>
<span class="engagement-count">${engagement.likes}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M8.75 21V3h2v18h-2zM18 21V8.5h2V21h-2zM4 21l.004-10h2L6 21H4zm9.248 0v-7h2v7h-2z"></path></g></svg>
<span class="engagement-count">${engagement.views}</span>
</div>
</div>
`;
}
async function generateQuoteBuffer(config) { async function generateQuoteBuffer(config) {
// Build HTML directly with values injected
const avatarHtml = config.avatarUrl const avatarHtml = config.avatarUrl
? `<img src="${config.avatarUrl}" style="width:100%;height:100%;object-fit:cover;" />` ? `<img src="${config.avatarUrl}" style="width:100%;height:100%;object-fit:cover;" />`
: `<div style="width:100%;height:100%;background:rgb(51,54,57);"></div>`; : `<div style="width:100%;height:100%;background:rgb(51,54,57);"></div>`;
@@ -182,155 +213,21 @@ async function generateQuoteBuffer(config) {
? `<div class="tweet-image-container" style="margin-bottom:12px;border-radius:16px;overflow:hidden;border:1px solid rgb(47,51,54);"><img src="${config.imageUrl}" style="width:100%;display:block;" /></div>` ? `<div class="tweet-image-container" style="margin-bottom:12px;border-radius:16px;overflow:hidden;border:1px solid rgb(47,51,54);"><img src="${config.imageUrl}" style="width:100%;display:block;" /></div>`
: ''; : '';
// Only show engagement bar if all fields provided const engagementHtml = buildEngagementHtml(config.engagement);
const engagementHtml = config.engagement ? `
<div class="engagement-bar">
<div class="engagement-item">
<span class="engagement-count">${config.engagement.replies}</span>
<span class="engagement-label">Replies</span>
</div>
<div class="engagement-item">
<span class="engagement-count">${config.engagement.retweets}</span>
<span class="engagement-label">Reposts</span>
</div>
<div class="engagement-item">
<span class="engagement-count">${config.engagement.likes}</span>
<span class="engagement-label">Likes</span>
</div>
<div class="engagement-item">
<span class="engagement-count">${config.engagement.views}</span>
<span class="engagement-label">Views</span>
</div>
</div>
` : '';
const html = `<!DOCTYPE html> const verifiedBadge = config.verified ? '<svg viewBox="0 0 22 22" class="verified-badge"><g><path d="M20.396 11c-.018-.646-.215-1.275-.57-1.816-.354-.54-.852-.972-1.438-1.246.223-.607.27-1.264.14-1.897-.131-.634-.437-1.218-.882-1.687-.47-.445-1.053-.75-1.687-.882-.633-.13-1.29-.083-1.897.14-.273-.587-.704-1.086-1.245-1.44S11.647 1.62 11 1.604c-.646.017-1.273.213-1.813.568s-.969.854-1.24 1.44c-.608-.223-1.267-.272-1.902-.14-.635.13-1.22.436-1.69.882-.445.47-.749 1.055-.878 1.688-.13.633-.08 1.29.144 1.896-.587.274-1.087.705-1.443 1.245-.356.54-.555 1.17-.574 1.817.02.647.218 1.276.574 1.817.356.54.856.972 1.443 1.245-.224.606-.274 1.263-.144 1.896.13.634.433 1.218.877 1.688.47.443 1.054.747 1.687.878.633.132 1.29.084 1.897-.136.274.586.705 1.084 1.246 1.439.54.354 1.17.551 1.816.569.647-.016 1.276-.213 1.817-.567s.972-.854 1.245-1.44c.604.239 1.266.296 1.903.164.636-.132 1.22-.447 1.68-.907.46-.46.776-1.044.908-1.681s.075-1.299-.165-1.903c.586-.274 1.084-.705 1.439-1.246.354-.54.551-1.17.569-1.816zM9.662 14.85l-3.429-3.428 1.293-1.302 2.072 2.072 4.4-4.794 1.347 1.246z"></path></g></svg>' : '';
<html>
<head>
<meta charset="UTF-8">
<style>
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
.square-container {
width: calc(100vmin - 20px);
height: calc(100vmin - 20px);
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
overflow: hidden;
}
.tweet-container {
width: 450px;
background-color: #000;
padding: 12px 16px;
box-sizing: border-box;
}
.tweet-header {
display: flex;
align-items: flex-start;
margin-bottom: 12px;
}
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
overflow: hidden;
margin-right: 12px;
background: rgb(51, 54, 57);
}
.user-info {
display: flex;
flex-direction: column;
}
.display-name {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
font-weight: 700;
color: rgb(231, 233, 234);
line-height: 20px;
}
.username {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(113, 118, 123);
line-height: 20px;
}
.tweet-text {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(231, 233, 234);
line-height: 20px;
margin-bottom: 12px;
white-space: pre-wrap;
}
.tweet-time {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(113, 118, 123);
}
.engagement-bar {
display: flex;
gap: 24px;
padding: 12px 0;
margin-top: 12px;
border-top: 1px solid rgb(47, 51, 54);
}
.engagement-item {
display: flex;
gap: 4px;
}
.engagement-count {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 14px;
font-weight: 700;
color: rgb(231, 233, 234);
}
.engagement-label {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 14px;
color: rgb(113, 118, 123);
}
</style>
</head>
<body>
<div class="square-container">
<div class="tweet-container">
<div class="tweet-header">
<div class="avatar">${avatarHtml}</div>
<div class="user-info">
<span class="display-name">${config.displayName}</span>
<span class="username">${config.username}</span>
</div>
</div>
<div class="tweet-text">${config.text}</div>
${imageHtml}
<div class="tweet-time">${config.timestamp}</div>
${engagementHtml}
</div>
</div>
<script>
function fitToSquare() {
const square = document.querySelector('.square-container');
const tweet = document.querySelector('.tweet-container');
const scaleX = square.offsetWidth / tweet.offsetWidth;
const scaleY = square.offsetHeight / tweet.offsetHeight;
const scale = Math.min(scaleX, scaleY) * 0.95;
tweet.style.transform = 'scale(' + scale + ')';
}
window.onload = fitToSquare;
</script>
</body>
</html>`;
const image = await renderHtml(html, 1000); const html = templateHtml
return image; .replace('{{avatarHtml}}', avatarHtml)
.replace('{{displayName}}', config.displayName)
.replace('{{verifiedBadge}}', verifiedBadge)
.replace('{{username}}', config.username)
.replace('{{text}}', config.text)
.replace('{{imageHtml}}', imageHtml)
.replace('{{timestamp}}', config.timestamp)
.replace('{{engagementHtml}}', engagementHtml);
return await renderHtml(html, 1000);
} }
// GET endpoint - use query parameters // GET endpoint - use query parameters
@@ -345,7 +242,8 @@ app.get('/generate', async (req, res) => {
text: req.query.text || "No text provided", text: req.query.text || "No text provided",
imageUrl: fixDataUri(req.query.imageUrl) || null, imageUrl: fixDataUri(req.query.imageUrl) || null,
timestamp: timestamp, timestamp: timestamp,
engagement: null engagement: null,
verified: req.query.verified === 'true' || req.query.verified === '1'
}; };
// Check cache first // Check cache first
@@ -433,7 +331,8 @@ app.post('/generate', async (req, res) => {
text: req.body.text || "No text provided", text: req.body.text || "No text provided",
imageUrl: fixDataUri(req.body.imageUrl) || null, imageUrl: fixDataUri(req.body.imageUrl) || null,
timestamp: timestamp, timestamp: timestamp,
engagement: engagement engagement: engagement,
verified: req.body.verified === true || req.body.verified === 'true' || req.body.verified === '1'
}; };
// Check cache first // Check cache first
@@ -463,10 +362,12 @@ app.get('/health', (req, res) => {
// Clear all cache on startup // Clear all cache on startup
clearCache(); clearCache();
cleanupExpiredSessions();
// Run cleanup every hour // Run cleanup every hour
setInterval(() => { setInterval(() => {
cleanupOldCache(); cleanupOldCache();
cleanupExpiredSessions();
}, 60 * 60 * 1000); }, 60 * 60 * 1000);
// Initialize browser pool then start server // Initialize browser pool then start server
@@ -476,6 +377,7 @@ initPool().then(() => {
console.log(`Browser pool size: ${POOL_SIZE} (set BROWSER_POOL_SIZE env var to change)`); console.log(`Browser pool size: ${POOL_SIZE} (set BROWSER_POOL_SIZE env var to change)`);
console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test&timestamp=1735574400`); console.log(`GET: http://localhost:${PORT}/generate?text=Hello&displayName=Test&username=@test&timestamp=1735574400`);
console.log(`POST: http://localhost:${PORT}/generate`); console.log(`POST: http://localhost:${PORT}/generate`);
console.log(`v2 API: POST/GET/PATCH/DELETE http://localhost:${PORT}/v2/quote`);
console.log(`Cache cleared on startup, cleanup runs every hour`); console.log(`Cache cleared on startup, cleanup runs every hour`);
}); });
}).catch(err => { }).catch(err => {

225
db.js Normal file
View File

@@ -0,0 +1,225 @@
const Database = require('better-sqlite3');
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const DATA_DIR = path.join(__dirname, 'data');
const DB_PATH = path.join(DATA_DIR, 'sessions.db');
// make sure data dir exists
if (!fs.existsSync(DATA_DIR)) {
fs.mkdirSync(DATA_DIR);
}
const db = new Database(DB_PATH);
// setup tables
db.exec(`
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
display_name TEXT DEFAULT 'Anonymous',
username TEXT DEFAULT '@anonymous',
text TEXT DEFAULT 'No text provided',
avatar_url TEXT,
image_url TEXT,
timestamp INTEGER,
verified INTEGER DEFAULT 0,
engagement_likes INTEGER,
engagement_retweets INTEGER,
engagement_replies INTEGER,
engagement_views INTEGER,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_sessions_updated_at ON sessions(updated_at);
`);
// migration: add verified column if it doesn't exist
try {
db.exec(`ALTER TABLE sessions ADD COLUMN verified INTEGER DEFAULT 0`);
} catch (err) {
// column already exists, ignore
}
function generateId() {
return crypto.randomUUID();
}
function nowEpoch() {
return Math.floor(Date.now() / 1000);
}
// convert db row to api resposne format
function rowToSession(row) {
if (!row) return null;
let engagement = null;
if (row.engagement_likes !== null &&
row.engagement_retweets !== null &&
row.engagement_replies !== null &&
row.engagement_views !== null) {
engagement = {
likes: row.engagement_likes,
retweets: row.engagement_retweets,
replies: row.engagement_replies,
views: row.engagement_views
};
}
return {
id: row.id,
displayName: row.display_name,
username: row.username,
text: row.text,
avatarUrl: row.avatar_url,
imageUrl: row.image_url,
timestamp: row.timestamp,
verified: Boolean(row.verified),
engagement: engagement,
createdAt: row.created_at,
updatedAt: row.updated_at
};
}
function createSession(data = {}) {
const id = generateId();
const now = nowEpoch();
const stmt = db.prepare(`
INSERT INTO sessions (
id, display_name, username, text, avatar_url, image_url, timestamp, verified,
engagement_likes, engagement_retweets, engagement_replies, engagement_views,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`);
stmt.run(
id,
data.displayName || 'Anonymous',
data.username || '@anonymous',
data.text || 'No text provided',
data.avatarUrl || null,
data.imageUrl || null,
data.timestamp || null,
data.verified ? 1 : 0,
data.engagement?.likes ?? null,
data.engagement?.retweets ?? null,
data.engagement?.replies ?? null,
data.engagement?.views ?? null,
now,
now
);
return getSession(id);
}
function getSession(id) {
const stmt = db.prepare('SELECT * FROM sessions WHERE id = ?');
const row = stmt.get(id);
return rowToSession(row);
}
function updateSession(id, data) {
const session = getSession(id);
if (!session) return null;
const updates = [];
const values = [];
if (data.displayName !== undefined) {
updates.push('display_name = ?');
values.push(data.displayName);
}
if (data.username !== undefined) {
updates.push('username = ?');
values.push(data.username);
}
if (data.text !== undefined) {
updates.push('text = ?');
values.push(data.text);
}
if (data.avatarUrl !== undefined) {
updates.push('avatar_url = ?');
values.push(data.avatarUrl);
}
if (data.imageUrl !== undefined) {
updates.push('image_url = ?');
values.push(data.imageUrl);
}
if (data.timestamp !== undefined) {
updates.push('timestamp = ?');
values.push(data.timestamp);
}
// engagement updates (atomic clear or partial updates)
if (data.engagement !== undefined) {
if (data.engagement === null) {
// explicitly clear all engagement fields
updates.push('engagement_likes = ?');
updates.push('engagement_retweets = ?');
updates.push('engagement_replies = ?');
updates.push('engagement_views = ?');
values.push(null, null, null, null);
} else {
// partial engagement field updates
if (data.engagement.likes !== undefined) {
updates.push('engagement_likes = ?');
values.push(data.engagement.likes);
}
if (data.engagement.retweets !== undefined) {
updates.push('engagement_retweets = ?');
values.push(data.engagement.retweets);
}
if (data.engagement.replies !== undefined) {
updates.push('engagement_replies = ?');
values.push(data.engagement.replies);
}
if (data.engagement.views !== undefined) {
updates.push('engagement_views = ?');
values.push(data.engagement.views);
}
}
}
if (updates.length === 0) {
return session;
}
updates.push('updated_at = ?');
values.push(nowEpoch());
values.push(id);
const stmt = db.prepare(`UPDATE sessions SET ${updates.join(', ')} WHERE id = ?`);
stmt.run(...values);
return getSession(id);
}
function deleteSession(id) {
const stmt = db.prepare('DELETE FROM sessions WHERE id = ?');
const result = stmt.run(id);
return result.changes > 0;
}
function cleanupExpiredSessions() {
const ONE_DAY = 24 * 60 * 60;
const cutoff = nowEpoch() - ONE_DAY;
const stmt = db.prepare('DELETE FROM sessions WHERE updated_at < ?');
const result = stmt.run(cutoff);
if (result.changes > 0) {
console.log(`Cleaned up ${result.changes} expired session(s)`);
}
}
module.exports = {
createSession,
getSession,
updateSession,
deleteSession,
cleanupExpiredSessions
};

314
package-lock.json generated
View File

@@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"better-sqlite3": "^11.0.0",
"express": "^4.18.2", "express": "^4.18.2",
"puppeteer": "^23.0.0" "puppeteer": "^23.0.0"
} }
@@ -288,6 +289,37 @@
"node": ">=10.0.0" "node": ">=10.0.0"
} }
}, },
"node_modules/better-sqlite3": {
"version": "11.10.0",
"resolved": "https://registry.npmjs.org/better-sqlite3/-/better-sqlite3-11.10.0.tgz",
"integrity": "sha512-EwhOpyXiOEL/lKzHz9AW1msWFNzGc/z+LzeB3/jnFJpxu+th2yqvzsSWas1v9jgs9+xiXJcD5A8CJxAG2TaghQ==",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"bindings": "^1.5.0",
"prebuild-install": "^7.1.1"
}
},
"node_modules/bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"license": "MIT",
"dependencies": {
"file-uri-to-path": "1.0.0"
}
},
"node_modules/bl": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
"integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==",
"license": "MIT",
"dependencies": {
"buffer": "^5.5.0",
"inherits": "^2.0.4",
"readable-stream": "^3.4.0"
}
},
"node_modules/body-parser": { "node_modules/body-parser": {
"version": "1.20.4", "version": "1.20.4",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.4.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.4.tgz",
@@ -407,6 +439,12 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/chownr": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/chownr/-/chownr-1.1.4.tgz",
"integrity": "sha512-jJ0bqzaylmJtVnNgzTeSOs8DPavpbYgEr/b0YL8/2GO3xJEhInFmhKMUnEJQjZumK7KXGFhUy89PrsJWlakBVg==",
"license": "ISC"
},
"node_modules/chromium-bidi": { "node_modules/chromium-bidi": {
"version": "0.6.5", "version": "0.6.5",
"resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.6.5.tgz", "resolved": "https://registry.npmjs.org/chromium-bidi/-/chromium-bidi-0.6.5.tgz",
@@ -541,6 +579,30 @@
} }
} }
}, },
"node_modules/decompress-response": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-6.0.0.tgz",
"integrity": "sha512-aW35yZM6Bb/4oJlZncMH2LCoZtJXTRxES17vE3hoRiowU2kWHaJKFkSBDnDR+cm9J+9QhXmREyIfv0pji9ejCQ==",
"license": "MIT",
"dependencies": {
"mimic-response": "^3.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/deep-extend": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/deep-extend/-/deep-extend-0.6.0.tgz",
"integrity": "sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==",
"license": "MIT",
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/degenerator": { "node_modules/degenerator": {
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/degenerator/-/degenerator-5.0.1.tgz", "resolved": "https://registry.npmjs.org/degenerator/-/degenerator-5.0.1.tgz",
@@ -574,6 +636,15 @@
"npm": "1.2.8000 || >= 1.4.16" "npm": "1.2.8000 || >= 1.4.16"
} }
}, },
"node_modules/detect-libc": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz",
"integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==",
"license": "Apache-2.0",
"engines": {
"node": ">=8"
}
},
"node_modules/devtools-protocol": { "node_modules/devtools-protocol": {
"version": "0.0.1330662", "version": "0.0.1330662",
"resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1330662.tgz", "resolved": "https://registry.npmjs.org/devtools-protocol/-/devtools-protocol-0.0.1330662.tgz",
@@ -758,6 +829,15 @@
"bare-events": "^2.7.0" "bare-events": "^2.7.0"
} }
}, },
"node_modules/expand-template": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/expand-template/-/expand-template-2.0.3.tgz",
"integrity": "sha512-XYfuKMvj4O35f/pOXLObndIRvyQ+/+6AhODh+OKWj9S9498pHHn/IMszH+gt0fBCRWMNfk1ZSp5x3AifmnI2vg==",
"license": "(MIT OR WTFPL)",
"engines": {
"node": ">=6"
}
},
"node_modules/express": { "node_modules/express": {
"version": "4.22.1", "version": "4.22.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.22.1.tgz", "resolved": "https://registry.npmjs.org/express/-/express-4.22.1.tgz",
@@ -854,6 +934,12 @@
"pend": "~1.2.0" "pend": "~1.2.0"
} }
}, },
"node_modules/file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"license": "MIT"
},
"node_modules/finalhandler": { "node_modules/finalhandler": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz", "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz",
@@ -905,6 +991,12 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/fs-constants": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz",
"integrity": "sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==",
"license": "MIT"
},
"node_modules/function-bind": { "node_modules/function-bind": {
"version": "1.1.2", "version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
@@ -989,6 +1081,12 @@
"node": ">= 14" "node": ">= 14"
} }
}, },
"node_modules/github-from-package": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/github-from-package/-/github-from-package-0.0.0.tgz",
"integrity": "sha512-SyHy3T1v2NUXn29OsWdxmK6RwHD+vkj3v8en8AOBZ1wBQ/hCAQ5bAQTD02kW4W9tUp/3Qh6J8r9EvntiyCmOOw==",
"license": "MIT"
},
"node_modules/gopd": { "node_modules/gopd": {
"version": "1.2.0", "version": "1.2.0",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
@@ -1125,6 +1223,12 @@
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC" "license": "ISC"
}, },
"node_modules/ini": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz",
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==",
"license": "ISC"
},
"node_modules/ip-address": { "node_modules/ip-address": {
"version": "10.1.0", "version": "10.1.0",
"resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.1.0.tgz", "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.1.0.tgz",
@@ -1266,18 +1370,51 @@
"node": ">= 0.6" "node": ">= 0.6"
} }
}, },
"node_modules/mimic-response": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-3.1.0.tgz",
"integrity": "sha512-z0yWI+4FDrrweS8Zmt4Ej5HdJmky15+L2e6Wgn3+iK5fWzb6T3fhNFq2+MeTRb064c6Wr4N/wv0DzQTjNzHNGQ==",
"license": "MIT",
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/mitt": { "node_modules/mitt": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.1.tgz", "resolved": "https://registry.npmjs.org/mitt/-/mitt-3.0.1.tgz",
"integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==", "integrity": "sha512-vKivATfr97l2/QBCYAkXYDbrIWPM2IIKEl7YPhjCvKlG3kE2gm+uBo6nEXK3M5/Ffh/FLpKExzOQ3JJoJGFKBw==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/mkdirp-classic": {
"version": "0.5.3",
"resolved": "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz",
"integrity": "sha512-gKLcREMhtuZRwRAfqP3RFW+TK4JqApVBtOIftVgjuABpAtpxhPGaDcfvbhNvD0B8iD1oUr/txX35NjcaY6Ns/A==",
"license": "MIT"
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.3", "version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/napi-build-utils": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/napi-build-utils/-/napi-build-utils-2.0.0.tgz",
"integrity": "sha512-GEbrYkbfF7MoNaoh2iGG84Mnf/WZfB0GdGEsM8wz7Expx/LlWf5U8t9nvJKXSp3qr5IsEbK04cBGhol/KwOsWA==",
"license": "MIT"
},
"node_modules/negotiator": { "node_modules/negotiator": {
"version": "0.6.3", "version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
@@ -1296,6 +1433,18 @@
"node": ">= 0.4.0" "node": ">= 0.4.0"
} }
}, },
"node_modules/node-abi": {
"version": "3.85.0",
"resolved": "https://registry.npmjs.org/node-abi/-/node-abi-3.85.0.tgz",
"integrity": "sha512-zsFhmbkAzwhTft6nd3VxcG0cvJsT70rL+BIGHWVq5fi6MwGrHwzqKaxXE+Hl2GmnGItnDKPPkO5/LQqjVkIdFg==",
"license": "MIT",
"dependencies": {
"semver": "^7.3.5"
},
"engines": {
"node": ">=10"
}
},
"node_modules/object-inspect": { "node_modules/object-inspect": {
"version": "1.13.4", "version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
@@ -1418,6 +1567,60 @@
"integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==",
"license": "ISC" "license": "ISC"
}, },
"node_modules/prebuild-install": {
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/prebuild-install/-/prebuild-install-7.1.3.tgz",
"integrity": "sha512-8Mf2cbV7x1cXPUILADGI3wuhfqWvtiLA1iclTDbFRZkgRQS0NqsPZphna9V+HyTEadheuPmjaJMsbzKQFOzLug==",
"license": "MIT",
"dependencies": {
"detect-libc": "^2.0.0",
"expand-template": "^2.0.3",
"github-from-package": "0.0.0",
"minimist": "^1.2.3",
"mkdirp-classic": "^0.5.3",
"napi-build-utils": "^2.0.0",
"node-abi": "^3.3.0",
"pump": "^3.0.0",
"rc": "^1.2.7",
"simple-get": "^4.0.0",
"tar-fs": "^2.0.0",
"tunnel-agent": "^0.6.0"
},
"bin": {
"prebuild-install": "bin.js"
},
"engines": {
"node": ">=10"
}
},
"node_modules/prebuild-install/node_modules/tar-fs": {
"version": "2.1.4",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-2.1.4.tgz",
"integrity": "sha512-mDAjwmZdh7LTT6pNleZ05Yt65HC3E+NiQzl672vQG38jIrehtJk/J3mNwIg+vShQPcLF/LV7CMnDW6vjj6sfYQ==",
"license": "MIT",
"dependencies": {
"chownr": "^1.1.1",
"mkdirp-classic": "^0.5.2",
"pump": "^3.0.0",
"tar-stream": "^2.1.4"
}
},
"node_modules/prebuild-install/node_modules/tar-stream": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/tar-stream/-/tar-stream-2.2.0.tgz",
"integrity": "sha512-ujeqbceABgwMZxEJnk2HDY2DlnUZ+9oEcb1KzTVfYHio0UE6dG71n60d8D2I4qNvleWrrXpmjpt7vZeF1LnMZQ==",
"license": "MIT",
"dependencies": {
"bl": "^4.0.3",
"end-of-stream": "^1.4.1",
"fs-constants": "^1.0.0",
"inherits": "^2.0.3",
"readable-stream": "^3.1.1"
},
"engines": {
"node": ">=6"
}
},
"node_modules/progress": { "node_modules/progress": {
"version": "2.0.3", "version": "2.0.3",
"resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz", "resolved": "https://registry.npmjs.org/progress/-/progress-2.0.3.tgz",
@@ -1482,7 +1685,6 @@
"deprecated": "< 24.15.0 is no longer supported", "deprecated": "< 24.15.0 is no longer supported",
"hasInstallScript": true, "hasInstallScript": true,
"license": "Apache-2.0", "license": "Apache-2.0",
"peer": true,
"dependencies": { "dependencies": {
"@puppeteer/browsers": "2.3.1", "@puppeteer/browsers": "2.3.1",
"chromium-bidi": "0.6.5", "chromium-bidi": "0.6.5",
@@ -1554,6 +1756,35 @@
"node": ">= 0.8" "node": ">= 0.8"
} }
}, },
"node_modules/rc": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/rc/-/rc-1.2.8.tgz",
"integrity": "sha512-y3bGgqKj3QBdxLbLkomlohkvsA8gdAiUQlSBJnBhfn+BPxg4bc62d8TcBW15wavDfgexCgccckhcZvywyQYPOw==",
"license": "(BSD-2-Clause OR MIT OR Apache-2.0)",
"dependencies": {
"deep-extend": "^0.6.0",
"ini": "~1.3.0",
"minimist": "^1.2.0",
"strip-json-comments": "~2.0.1"
},
"bin": {
"rc": "cli.js"
}
},
"node_modules/readable-stream": {
"version": "3.6.2",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz",
"integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==",
"license": "MIT",
"dependencies": {
"inherits": "^2.0.3",
"string_decoder": "^1.1.1",
"util-deprecate": "^1.0.1"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/require-directory": { "node_modules/require-directory": {
"version": "2.1.1", "version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
@@ -1742,6 +1973,51 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/simple-concat": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/simple-concat/-/simple-concat-1.0.1.tgz",
"integrity": "sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/simple-get": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/simple-get/-/simple-get-4.0.1.tgz",
"integrity": "sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT",
"dependencies": {
"decompress-response": "^6.0.0",
"once": "^1.3.1",
"simple-concat": "^1.0.0"
}
},
"node_modules/smart-buffer": { "node_modules/smart-buffer": {
"version": "4.2.0", "version": "4.2.0",
"resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz", "resolved": "https://registry.npmjs.org/smart-buffer/-/smart-buffer-4.2.0.tgz",
@@ -1810,6 +2086,15 @@
"text-decoder": "^1.1.0" "text-decoder": "^1.1.0"
} }
}, },
"node_modules/string_decoder": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz",
"integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==",
"license": "MIT",
"dependencies": {
"safe-buffer": "~5.2.0"
}
},
"node_modules/string-width": { "node_modules/string-width": {
"version": "4.2.3", "version": "4.2.3",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz",
@@ -1836,6 +2121,15 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/strip-json-comments": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz",
"integrity": "sha512-4gB8na07fecVVkOI6Rs4e7T6NOTki5EmL7TUduTs6bu3EdnSycntVJ4re8kgZA+wx9IueI2Y11bfbgwtzuE0KQ==",
"license": "MIT",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/tar-fs": { "node_modules/tar-fs": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.1.1.tgz", "resolved": "https://registry.npmjs.org/tar-fs/-/tar-fs-3.1.1.tgz",
@@ -1891,6 +2185,18 @@
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"license": "0BSD" "license": "0BSD"
}, },
"node_modules/tunnel-agent": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/tunnel-agent/-/tunnel-agent-0.6.0.tgz",
"integrity": "sha512-McnNiV1l8RYeY8tBgEpuodCC1mLUdbSN+CYBL7kJsJNInOP8UjDDEwdk6Mw60vdLLrr5NHKZhMAOSrR2NZuQ+w==",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
},
"engines": {
"node": "*"
}
},
"node_modules/type-is": { "node_modules/type-is": {
"version": "1.6.18", "version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
@@ -1942,6 +2248,12 @@
"integrity": "sha512-H/A06tKD7sS1O1X2SshBVeA5FLycRpjqiBeqGKmBwBDBy28EnRjORxTNe269KSSr5un5qyWi1iL61wLxpd+ZOg==", "integrity": "sha512-H/A06tKD7sS1O1X2SshBVeA5FLycRpjqiBeqGKmBwBDBy28EnRjORxTNe269KSSr5un5qyWi1iL61wLxpd+ZOg==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==",
"license": "MIT"
},
"node_modules/utils-merge": { "node_modules/utils-merge": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",

View File

@@ -14,6 +14,7 @@
"type": "commonjs", "type": "commonjs",
"dependencies": { "dependencies": {
"puppeteer": "^23.0.0", "puppeteer": "^23.0.0",
"express": "^4.18.2" "express": "^4.18.2",
"better-sqlite3": "^11.0.0"
} }
} }

136
template.html Normal file
View File

@@ -0,0 +1,136 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
.square-container {
width: calc(100vmin - 20px);
height: calc(100vmin - 20px);
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
overflow: hidden;
}
.tweet-container {
width: 450px;
background-color: #000;
padding: 12px 16px;
box-sizing: border-box;
}
.tweet-header {
display: flex;
align-items: flex-start;
margin-bottom: 12px;
}
.avatar {
width: 40px;
height: 40px;
border-radius: 50%;
overflow: hidden;
margin-right: 12px;
background: rgb(51, 54, 57);
}
.user-info {
display: flex;
flex-direction: column;
}
.display-name {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
font-weight: 700;
color: rgb(231, 233, 234);
line-height: 20px;
display: flex;
align-items: center;
gap: 4px;
}
.verified-badge {
width: 18px;
height: 18px;
fill: rgb(29, 155, 240);
}
.username {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(113, 118, 123);
line-height: 20px;
}
.tweet-text {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(231, 233, 234);
line-height: 20px;
margin-bottom: 12px;
white-space: pre-wrap;
}
.tweet-time {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 15px;
color: rgb(113, 118, 123);
}
.tweet-source {
color: rgb(29, 155, 240);
}
.engagement-bar {
display: flex;
justify-content: space-between;
padding: 12px 0;
margin-top: 12px;
border-top: 1px solid rgb(47, 51, 54);
}
.engagement-item {
display: flex;
align-items: center;
gap: 4px;
color: rgb(113, 118, 123);
}
.engagement-icon {
width: 18.75px;
height: 18.75px;
fill: rgb(113, 118, 123);
}
.engagement-count {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: 13px;
color: rgb(113, 118, 123);
}
</style>
</head>
<body>
<div class="square-container">
<div class="tweet-container">
<div class="tweet-header">
<div class="avatar">{{avatarHtml}}</div>
<div class="user-info">
<span class="display-name">{{displayName}}{{verifiedBadge}}</span>
<span class="username">{{username}}</span>
</div>
</div>
<div class="tweet-text">{{text}}</div>
{{imageHtml}}
<div class="tweet-time">{{timestamp}} · <span class="tweet-source">via quotes.imbenji.net</span></div>
{{engagementHtml}}
</div>
</div>
<script>
function fitToSquare() {
const square = document.querySelector('.square-container');
const tweet = document.querySelector('.tweet-container');
const scaleX = square.offsetWidth / tweet.offsetWidth;
const scaleY = square.offsetHeight / tweet.offsetHeight;
const scale = Math.min(scaleX, scaleY) * 0.95;
tweet.style.transform = 'scale(' + scale + ')';
}
window.onload = fitToSquare;
</script>
</body>
</html>

337
v2Routes.js Normal file
View File

@@ -0,0 +1,337 @@
const express = require('express');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const router = express.Router();
const { createSession, getSession, updateSession, deleteSession } = require('./db');
const { renderHtml } = require('./browserPool');
const CACHE_DIR = path.join(__dirname, 'cache');
// caching functions
function normalizeConfig(config) {
const normalized = {};
for (const [key, value] of Object.entries(config)) {
if (value !== null && value !== undefined && value !== '') {
normalized[key] = value;
}
}
return normalized;
}
function hashConfig(config) {
const normalized = normalizeConfig(config);
const configString = JSON.stringify(normalized);
return crypto.createHash('sha256').update(configString).digest('hex');
}
function getCachedImage(config) {
const hash = hashConfig(config);
const cachePath = path.join(CACHE_DIR, `${hash}.png`);
if (fs.existsSync(cachePath)) {
const now = new Date();
fs.utimesSync(cachePath, now, now);
return fs.readFileSync(cachePath);
}
return null;
}
function cacheImage(config, imageBuffer) {
const hash = hashConfig(config);
const cachePath = path.join(CACHE_DIR, `${hash}.png`);
fs.writeFileSync(cachePath, imageBuffer);
}
function deleteCachedImage(config) {
const hash = hashConfig(config);
const cachePath = path.join(CACHE_DIR, `${hash}.png`);
if (fs.existsSync(cachePath)) {
fs.unlinkSync(cachePath);
}
}
// build config from session for cache operations
function buildConfigFromSession(session) {
const timestamp = session.timestamp
? formatTimestamp(session.timestamp)
: formatTimestamp(Math.floor(Date.now() / 1000));
let engagement = null;
if (session.engagement) {
engagement = {
likes: formatCount(session.engagement.likes),
retweets: formatCount(session.engagement.retweets),
replies: formatCount(session.engagement.replies),
views: formatCount(session.engagement.views)
};
}
return {
displayName: session.displayName,
username: session.username,
avatarUrl: session.avatarUrl,
text: session.text,
imageUrl: session.imageUrl,
timestamp: timestamp,
engagement: engagement,
verified: session.verified
};
}
// helper functions (copied from api.js)
function formatCount(num) {
if (num === null || num === undefined) return '0';
num = parseInt(num);
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'B';
}
if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
}
if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
}
return num.toString();
}
function formatTimestamp(epoch) {
const date = new Date(epoch * 1000);
const hours = date.getHours();
const minutes = date.getMinutes().toString().padStart(2, '0');
const ampm = hours >= 12 ? 'PM' : 'AM';
const hour12 = hours % 12 || 12;
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const month = months[date.getMonth()];
const day = date.getDate();
const year = date.getFullYear();
return `${hour12}:${minutes} ${ampm} · ${month} ${day}, ${year}`;
}
function detectImageType(base64String) {
const base64Data = base64String.includes(',') ? base64String.split(',')[1] : base64String;
const buffer = Buffer.from(base64Data, 'base64');
if (buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {
return 'image/jpeg';
} else if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4E && buffer[3] === 0x47) {
return 'image/png';
} else if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46) {
return 'image/gif';
} else if (buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46) {
return 'image/webp';
}
return 'image/png';
}
function fixDataUri(dataUri) {
if (!dataUri || !dataUri.startsWith('data:')) return dataUri;
const parts = dataUri.split(',');
if (parts.length !== 2) return dataUri;
const base64Data = parts[1];
try {
const correctType = detectImageType(base64Data);
return `data:${correctType};base64,${base64Data}`;
} catch (error) {
console.error('Invalid base64 data:', error.message);
return null;
}
}
const TEMPLATE_PATH = path.join(__dirname, 'template.html');
const templateHtml = fs.readFileSync(TEMPLATE_PATH, 'utf8');
function buildEngagementHtml(engagement) {
if (!engagement) return '';
return `
<div class="engagement-bar" role="group">
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M1.751 10c0-4.42 3.584-8 8.005-8h4.366c4.49 0 8.129 3.64 8.129 8.13 0 2.96-1.607 5.68-4.196 7.11l-8.054 4.46v-3.69h-.067c-4.49.1-8.183-3.51-8.183-8.01zm8.005-6c-3.317 0-6.005 2.69-6.005 6 0 3.37 2.77 6.08 6.138 6.01l.351-.01h1.761v2.3l5.087-2.81c1.951-1.08 3.163-3.13 3.163-5.36 0-3.39-2.744-6.13-6.129-6.13H9.756z"></path></g></svg>
<span class="engagement-count">${engagement.replies}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M4.5 3.88l4.432 4.14-1.364 1.46L5.5 7.55V16c0 1.1.896 2 2 2H13v2H7.5c-2.209 0-4-1.79-4-4V7.55L1.432 9.48.068 8.02 4.5 3.88zM16.5 6H11V4h5.5c2.209 0 4 1.79 4 4v8.45l2.068-1.93 1.364 1.46-4.432 4.14-4.432-4.14 1.364-1.46 2.068 1.93V8c0-1.1-.896-2-2-2z"></path></g></svg>
<span class="engagement-count">${engagement.retweets}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M16.697 5.5c-1.222-.06-2.679.51-3.89 2.16l-.805 1.09-.806-1.09C9.984 6.01 8.526 5.44 7.304 5.5c-1.243.07-2.349.78-2.91 1.91-.552 1.12-.633 2.78.479 4.82 1.074 1.97 3.257 4.27 7.129 6.61 3.87-2.34 6.052-4.64 7.126-6.61 1.111-2.04 1.03-3.7.477-4.82-.561-1.13-1.666-1.84-2.908-1.91zm4.187 7.69c-1.351 2.48-4.001 5.12-8.379 7.67l-.503.3-.504-.3c-4.379-2.55-7.029-5.19-8.382-7.67-1.36-2.5-1.41-4.86-.514-6.67.887-1.79 2.647-2.91 4.601-3.01 1.651-.09 3.368.56 4.798 2.01 1.429-1.45 3.146-2.1 4.796-2.01 1.954.1 3.714 1.22 4.601 3.01.896 1.81.846 4.17-.514 6.67z"></path></g></svg>
<span class="engagement-count">${engagement.likes}</span>
</div>
<div class="engagement-item">
<svg viewBox="0 0 24 24" class="engagement-icon"><g><path d="M8.75 21V3h2v18h-2zM18 21V8.5h2V21h-2zM4 21l.004-10h2L6 21H4zm9.248 0v-7h2v7h-2z"></path></g></svg>
<span class="engagement-count">${engagement.views}</span>
</div>
</div>
`;
}
async function generateQuoteBuffer(config) {
const avatarHtml = config.avatarUrl
? `<img src="${config.avatarUrl}" style="width:100%;height:100%;object-fit:cover;" />`
: `<div style="width:100%;height:100%;background:rgb(51,54,57);"></div>`;
const imageHtml = config.imageUrl
? `<div class="tweet-image-container" style="margin-bottom:12px;border-radius:16px;overflow:hidden;border:1px solid rgb(47,51,54);"><img src="${config.imageUrl}" style="width:100%;display:block;" /></div>`
: '';
const engagementHtml = buildEngagementHtml(config.engagement);
const verifiedBadge = config.verified ? '<svg viewBox="0 0 22 22" class="verified-badge"><g><path d="M20.396 11c-.018-.646-.215-1.275-.57-1.816-.354-.54-.852-.972-1.438-1.246.223-.607.27-1.264.14-1.897-.131-.634-.437-1.218-.882-1.687-.47-.445-1.053-.75-1.687-.882-.633-.13-1.29-.083-1.897.14-.273-.587-.704-1.086-1.245-1.44S11.647 1.62 11 1.604c-.646.017-1.273.213-1.813.568s-.969.854-1.24 1.44c-.608-.223-1.267-.272-1.902-.14-.635.13-1.22.436-1.69.882-.445.47-.749 1.055-.878 1.688-.13.633-.08 1.29.144 1.896-.587.274-1.087.705-1.443 1.245-.356.54-.555 1.17-.574 1.817.02.647.218 1.276.574 1.817.356.54.856.972 1.443 1.245-.224.606-.274 1.263-.144 1.896.13.634.433 1.218.877 1.688.47.443 1.054.747 1.687.878.633.132 1.29.084 1.897-.136.274.586.705 1.084 1.246 1.439.54.354 1.17.551 1.816.569.647-.016 1.276-.213 1.817-.567s.972-.854 1.245-1.44c.604.239 1.266.296 1.903.164.636-.132 1.22-.447 1.68-.907.46-.46.776-1.044.908-1.681s.075-1.299-.165-1.903c.586-.274 1.084-.705 1.439-1.246.354-.54.551-1.17.569-1.816zM9.662 14.85l-3.429-3.428 1.293-1.302 2.072 2.072 4.4-4.794 1.347 1.246z"></path></g></svg>' : '';
const html = templateHtml
.replace('{{avatarHtml}}', avatarHtml)
.replace('{{displayName}}', config.displayName)
.replace('{{verifiedBadge}}', verifiedBadge)
.replace('{{username}}', config.username)
.replace('{{text}}', config.text)
.replace('{{imageHtml}}', imageHtml)
.replace('{{timestamp}}', config.timestamp)
.replace('{{engagementHtml}}', engagementHtml);
return await renderHtml(html, 1000);
}
// POST /v2/quote - create new session
router.post('/quote', (req, res) => {
try {
const data = {
displayName: req.body.displayName,
username: req.body.username?.trim(),
text: req.body.text,
avatarUrl: fixDataUri(req.body.avatarUrl),
imageUrl: fixDataUri(req.body.imageUrl),
timestamp: req.body.timestamp,
engagement: req.body.engagement,
verified: req.body.verified
};
const session = createSession(data);
res.status(201).json(session);
} catch (error) {
console.error('Failed to create session:', error);
res.status(500).json({ error: 'Failed to create session' });
}
});
// GET /v2/quote/:id - get session state
router.get('/quote/:id', (req, res) => {
const session = getSession(req.params.id);
if (!session) {
return res.status(404).json({ error: 'Session not found or expired' });
}
res.json(session);
});
// PATCH /v2/quote/:id - update session
router.patch('/quote/:id', (req, res) => {
try {
// get current session to invalidate its cached image
const currentSession = getSession(req.params.id);
if (!currentSession) {
return res.status(404).json({ error: 'Session not found or expired' });
}
// clear cached image for old state
const oldConfig = buildConfigFromSession(currentSession);
deleteCachedImage(oldConfig);
const data = {};
if (req.body.displayName !== undefined) data.displayName = req.body.displayName;
if (req.body.username !== undefined) data.username = req.body.username?.trim();
if (req.body.text !== undefined) data.text = req.body.text;
if (req.body.avatarUrl !== undefined) data.avatarUrl = fixDataUri(req.body.avatarUrl);
if (req.body.imageUrl !== undefined) data.imageUrl = fixDataUri(req.body.imageUrl);
if (req.body.timestamp !== undefined) data.timestamp = req.body.timestamp;
if (req.body.engagement !== undefined) data.engagement = req.body.engagement;
if (req.body.verified !== undefined) data.verified = req.body.verified;
const session = updateSession(req.params.id, data);
if (!session) {
return res.status(404).json({ error: 'Session not found or expired' });
}
res.json(session);
} catch (error) {
console.error('Failed to update session:', error);
res.status(500).json({ error: 'Failed to update session' });
}
});
// GET /v2/quote/:id/image - render the image
router.get('/quote/:id/image', async (req, res) => {
try {
const session = getSession(req.params.id);
if (!session) {
return res.status(404).json({ error: 'Session not found or expired' });
}
const timestamp = session.timestamp
? formatTimestamp(session.timestamp)
: formatTimestamp(Math.floor(Date.now() / 1000));
let engagement = null;
if (session.engagement) {
engagement = {
likes: formatCount(session.engagement.likes),
retweets: formatCount(session.engagement.retweets),
replies: formatCount(session.engagement.replies),
views: formatCount(session.engagement.views)
};
}
const config = {
displayName: session.displayName,
username: session.username,
avatarUrl: session.avatarUrl,
text: session.text,
imageUrl: session.imageUrl,
timestamp: timestamp,
engagement: engagement,
verified: session.verified
};
// check cache first
let image = getCachedImage(config);
let fromCache = true;
if (!image) {
image = await generateQuoteBuffer(config);
cacheImage(config, image);
fromCache = false;
}
res.setHeader('Content-Type', 'image/png');
res.setHeader('X-Cache', fromCache ? 'HIT' : 'MISS');
res.setHeader('X-Session-Id', session.id);
res.send(image);
} catch (error) {
console.error('Failed to generate image:', error);
res.status(500).json({ error: 'Failed to generate image' });
}
});
// DELETE /v2/quote/:id - delete session
router.delete('/quote/:id', (req, res) => {
const deleted = deleteSession(req.params.id);
if (!deleted) {
return res.status(404).json({ error: 'Session not found or expired' });
}
res.status(204).send();
});
module.exports = router;