forked from gitstua/trmnl-plugin-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
586 lines (506 loc) · 21.1 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright (c) 2025 Stu Eggerton. All rights reserved.
// See LICENSE.md for license details.
const express = require('express');
const { Liquid } = require('liquidjs');
const fs = require('fs').promises;
exports.fs = fs;
const fsSync = require('fs'); // Add this for sync operations
const path = require('path');
const fetch = require('node-fetch');
const config = require('./config');
const toml = require('toml');
const downloadAssets = require('./download-assets');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const puppeteer = require('puppeteer');
require('dotenv').config();
const { version } = require('./package.json');
const apiRoutes = require('./routes/api');
const { getAvailablePlugins } = require('./config');
const adminRouter = require('./admin');
const { authMiddleware } = require('./middleware/auth');
const app = express();
// All configuration values are now imported via config.js
let requestCount = 0;
let lastResetTime = Date.now();
// Create required directories synchronously at startup
//if (!fsSync.existsSync(config.CACHE_PATH)) {
// fsSync.mkdirSync(config.CACHE_PATH, { recursive: true });
//}
//if (!fsSync.existsSync(config.FONTS_PATH)) {
// fsSync.mkdirSync(config.FONTS_PATH, { recursive: true });
//}
// Add near the top with other global constants
let globalDeviceData = {};
// Function to initialize the liquid engine synchronously
function setupLiquidEngine() {
try {
// Synchronously check for settings.yml in the plugins directory
fsSync.accessSync(path.join(config.PLUGINS_PATH, 'settings.yml'));
// Single plugin mode
return new Liquid({
root: config.PLUGINS_PATH,
extname: '.liquid',
lookupRoot: config.PLUGINS_PATH
});
} catch {
// Multi-plugin mode
return new Liquid({
root: config.PLUGINS_PATH,
extname: '.liquid',
lookupRoot: config.PLUGINS_PATH
});
}
}
// Initialize engine synchronously
const engine = setupLiquidEngine();
app.engine('html', engine.express());
app.set('view engine', 'html');
// Set the views directory to the root directory
app.set('views', config.PLUGINS_PATH); // Change this to use PLUGINS_PATH
// Serve static files
app.use(express.static('public', {
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
// Serve cached CDN files
app.use('/fonts', express.static(path.join(config.CACHE_PATH, 'fonts'), {
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
app.use('/images', express.static(path.join(config.CACHE_PATH, 'images'), {
setHeaders: (res) => {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
}
}));
const CDN_BASE = 'https://usetrmnl.com';
// Serve static files based on USE_CACHE setting
if (config.USE_CACHE === 'true') {
// Serve from cache
app.use('/css/latest', express.static(path.join(config.CACHE_PATH, 'css/latest')));
app.use('/js/latest', express.static(path.join(config.CACHE_PATH, 'js/latest')));
app.use('/fonts', express.static(path.join(config.CACHE_PATH, 'fonts')));
app.use('/images', express.static(path.join(config.CACHE_PATH, 'images')));
} else {
// Proxy to CDN - maintain exact same paths
app.use(['/css/latest', '/js/latest', '/fonts', '/images'], async (req, res) => {
try {
const response = await fetch(`${CDN_BASE}${req.originalUrl}`);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
const data = await response.buffer();
res.type(response.headers.get('content-type'));
res.send(data);
} catch (error) {
console.error('Proxy error:', error);
res.status(500).send('Error proxying to CDN');
}
});
}
// Add this new endpoint
app.get('/api/plugins', async (req, res) => {
const plugins = await getAvailablePlugins();
res.json(plugins);
});
// Update the root route to handle rendering all plugins
app.get('/', async (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// Simplify rate limit check to track all requests
function checkRateLimit() {
const now = Date.now();
const fiveMinutesMs = 5 * 60 * 1000;
// Reset counter if 5 minutes have passed
if (now - lastResetTime > fiveMinutesMs) {
requestCount = 0;
lastResetTime = now;
}
// Increment and check
requestCount++;
return requestCount <= config.MAX_REQUESTS_PER_5_MIN;
}
// Update the fetchLiveData function
async function fetchLiveData(url, headers) {
let parsedHeaders = {};
try {
// Check global rate limit before making request
if (!checkRateLimit()) {
const error = new Error('Too Many Requests');
error.status = 429;
error.retryAfter = Math.ceil((lastResetTime + 5 * 60 * 1000 - Date.now()) / 1000);
throw error;
}
if (headers && headers !== '{}' && Object.keys(headers).length > 0) {
try {
parsedHeaders = typeof headers === 'string' ? JSON.parse(headers) : headers;
} catch (error) {
console.error('Error parsing headers:', error);
}
}
const response = await fetch(url, Object.keys(parsedHeaders).length > 0 ? { headers: parsedHeaders } : undefined);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
if (!text.trim()) {
return {};
}
return JSON.parse(text);
} catch (error) {
console.error(`Error fetching live data from ${url}:`, error);
if (error.status === 429) {
throw {
status: 429,
error: 'Too Many Requests',
message: 'Rate limit exceeded. Please try again later.',
retryAfter: error.retryAfter
};
}
throw error;
}
}
// Update the preview route to handle both single and multi-plugin modes
app.get(['/preview/:layout', '/preview/:plugin/:layout'], async (req, res) => {
let layout = req.params.layout;
const plugin = req.params.plugin;
const { live } = req.query;
try {
// Get plugin ID and info
const pluginId = plugin || (await config.isPluginDirectory(config.PLUGINS_PATH) ? '.' : null);
if (!pluginId) {
return res.status(400).json({
error: 'Plugin ID required',
message: 'Please select a plugin before loading preview'
});
}
const publicUrlWithTokens = await config.getPluginInfoWithTokens(pluginId);
let data;
// Load data based on strategy
if (publicUrlWithTokens.trmnl.plugin_settings.strategy === 'static' ||
publicUrlWithTokens.trmnl.plugin_settings.strategy === 'webhook') {
data = JSON.parse(await fs.readFile(config.getSamplePath(pluginId), 'utf8'));
} else if (live === 'true') {
try {
const polling_url = publicUrlWithTokens.trmnl.plugin_settings.polling_url;
const headers = publicUrlWithTokens.trmnl.plugin_settings.headers;
// Get data using the polling URL and headers from plugin info
data = await fetchLiveData(polling_url, headers);
if (Array.isArray(data)) {
data = { data };
}
// Save to tmp directory
const pluginPath = config.getPluginPath(pluginId);
const tmpDir = path.join(pluginPath, 'tmp');
await fs.mkdir(tmpDir, { recursive: true });
await fs.writeFile(
path.join(tmpDir, 'data.json'),
JSON.stringify(data, null, 2)
);
} catch (error) {
if (error.error === 'Authentication Required') {
return res.status(400).json(error);
}
throw error;
}
} else {
data = JSON.parse(await fs.readFile(config.getSamplePath(pluginId), 'utf8'));
}
// Merge data with plugin settings
data = {
...data,
trmnl: {
...publicUrlWithTokens.trmnl,
...config.deviceData
}
};
// Render template
const viewPath = pluginId === '.'
? path.join('views', `${layout}.liquid`)
: path.join(pluginId, 'views', `${layout}.liquid`);
const templateContent = await engine.renderFile(viewPath, data);
res.send(`
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://usetrmnl.com/css/latest/plugins.css">
<script src="https://usetrmnl.com/js/latest/plugins.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;350;375;400;450;600;700&display=swap" rel="stylesheet">
</head>
<body class="environment trmnl">
<div class="screen">
<div class="view view--${layout}">
${templateContent}
</div>
</div>
</body>
</html>
`);
} catch (error) {
console.error('Error:', error);
if (error.status === 429) {
res.status(429)
.set('Retry-After', error.retryAfter)
.json({
error: 'Too Many Requests',
message: 'Rate limit exceeded. Please try again later.',
retryAfter: error.retryAfter
});
} else {
res.status(500).json({ error: error.message });
}
}
});
// Update the layout endpoint
app.get(['/api/layout/:layout', '/api/layout/:pluginId/:layout'], async (req, res) => {
try {
let layoutName, pluginId;
if (req.params.pluginId && req.params.layout) {
// Route: /api/layout/:pluginId/:layout
pluginId = req.params.pluginId;
layoutName = req.params.layout;
} else {
// Route: /api/layout/:layout
layoutName = req.params.layout;
pluginId = undefined;
}
layoutName = layoutName.replace('.html', '').replace('-', '_');
// If no plugin specified and in single plugin mode, use '.'
const effectivePluginId = pluginId || (await config.isPluginDirectory(config.PLUGINS_PATH) ? '.' : null);
if (!effectivePluginId) {
return res.status(400).json({
error: 'Plugin ID required',
message: 'Please select a plugin before loading layout'
});
}
const pluginPath = config.getPluginPath(effectivePluginId);
const layoutContent = await fs.readFile(
path.join(pluginPath, 'views', `${layoutName}.liquid`),
'utf8'
);
res.send(layoutContent);
} catch (error) {
console.error('Error reading layout template:', error);
res.status(404).json({ error: 'Layout template not found' });
}
});
// Update the endpoint to serve the raw config.toml content
app.get('/api/plugin-settings/:pluginId', async (req, res) => {
try {
const pluginId = req.params.pluginId;
const pluginPath = config.getPluginPath(pluginId);
const settingsPath = path.join(pluginPath, 'settings.yml');
const settingsContent = await fs.readFile(settingsPath, 'utf8');
res.type('text/yaml').send(settingsContent);
} catch (error) {
console.error('Error reading settings.yml:', error);
res.status(500).send('Error reading settings.yml');
}
});
// Update the debug endpoint to be more secure
app.get('/debug', async (req, res) => {
// Only allow debug endpoint in development
if (config.DEBUG_MODE !== 'true') {
return res.status(403).json({
error: 'Debug endpoint disabled',
message: 'Debug endpoint is only available in development mode with DEBUG_MODE=true'
});
}
try {
const debug = {
environment: {
NODE_ENV: process.env.NODE_ENV,
CACHE_PATH: process.env.CACHE_PATH,
PLUGINS_PATH: process.env.PLUGINS_PATH,
DEBUG_MODE: process.env.DEBUG_MODE,
NODE_ENV: process.env.NODE_ENV,
// Add other relevant env vars but exclude sensitive ones
},
docker: {
isDocker: false,
mounts: []
},
filesystem: {
cache: {},
app: {}
}
};
// Check if running in Docker
try {
await fs.access('/.dockerenv');
debug.docker.isDocker = true;
// Get Docker mounts
const { stdout: dfOutput } = await exec('df -h');
debug.docker.mounts = dfOutput
.split('\n')
.filter(line => line.includes('/data/cache'))
.map(line => {
const parts = line.split(/\s+/);
return {
filesystem: parts[0],
size: parts[1],
used: parts[2],
available: parts[3],
mountpoint: parts[5]
};
});
} catch (e) {
// Not running in Docker
}
// Function to get directory structure with file sizes
async function getDirectoryStructure(dir) {
const structure = {};
try {
const items = await fs.readdir(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stats = await fs.stat(fullPath);
if (stats.isDirectory()) {
structure[item] = await getDirectoryStructure(fullPath);
} else {
structure[item] = {
size: stats.size,
sizeHuman: `${(stats.size / 1024).toFixed(2)} KB`,
modified: stats.mtime
};
}
}
} catch (error) {
structure['error'] = error.message;
}
return structure;
}
// Get file system structure
debug.filesystem.cache = await getDirectoryStructure(config.CACHE_PATH);
debug.filesystem.app = await getDirectoryStructure(process.cwd());
res.json(debug);
} catch (error) {
res.status(500).json({
error: 'Failed to get debug information',
details: error.message
});
}
});
// Add helper function for delay
async function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Add display endpoint with feature flag check
const handleDisplay = async (req, res) => {
if (!config.ENABLE_IMAGE_GENERATION) {
return res.status(403).json({
error: 'Image generation disabled',
message: 'Image generation is disabled in this environment'
});
}
try {
const engine = req.query.engine || 'puppeteer';
const pluginId = req.query.plugin;
const layout = req.query.layout || 'full';
if (!pluginId) {
return res.status(400).send('Plugin ID is required');
}
// Determine plugin path and create tmp directory using the helper function
const pluginPath = config.getPluginPath(pluginId);
const pluginTmpDir = path.join(pluginPath, 'tmp');
const pluginPreviewDir = path.join(pluginPath, 'Preview');
// Create plugin's tmp and preview directory if it doesn't exist
await fs.mkdir(pluginTmpDir, { recursive: true });
await fs.mkdir(pluginPreviewDir, { recursive: true });
const pluginUrl = `http://localhost:${config.PORT}/preview/${pluginId}/${layout}`;
let screenshotBuffer;
try {
const browser = await puppeteer.launch(config.BROWSER_LAUNCH_CONFIG.puppeteer);
const page = await browser.newPage();
await page.goto(pluginUrl, { waitUntil: 'networkidle0' });
await page.setViewport({ width: config.DISPLAY.width, height: config.DISPLAY.height });
// Write out the version number of puppeteer
console.log('Puppeteer version:', require('puppeteer/package.json').version);
// Wait for the configured screenshot delay
await delay(config.DISPLAY.delay);
screenshotBuffer = await page.screenshot();
await browser.close();
} catch (error) {
console.error('Puppeteer error:', error);
if (config.DEBUG_MODE === 'true') {
throw error;
}
throw new Error('Failed to launch Puppeteer browser. See logs for more details.');
}
// Use plugin's tmp directory for temporary files
const tempPng = path.join(pluginPreviewDir, `${layout}.png`);
const tempBmp = path.join(pluginTmpDir, `${layout}.bmp`);
// Save screenshot to temp file
await fs.writeFile(tempPng, screenshotBuffer);
//Output the version number of ImageMagick
console.log('🎨🎨🎨 ImageMagick wrapper version:', require('imagemagick/package.json').version);
//Output the version number of ImageMagick using convert --version
exec(`${config.IMAGE_MAGICK_BIN} --version`, (err, stdout, stderr) => {
console.log('ImageMagick version:', stdout);
});
console.log('🎨🎨🎨 ImageMagick switches:', config.IMAGE_MAGICK_SWICTHES);
// Convert to BMP using ImageMagick - thanks to https://github.com/schrockwell/
await new Promise((resolve, reject) => {
exec(`${config.IMAGE_MAGICK_BIN} ${tempPng} ${config.IMAGE_MAGICK_SWICTHES} ${tempBmp}`, (err) => {
if (err) return reject(err);
resolve();
});
});
//add 2 more output files to the tmp directory
//1 = -dither FloydSteinberg -monochrome -depth 1 -strip -define bmp:format=bmp3
// 2 = -dither FloydSteinberg -monochrome -depth 1 -strip -compress RLE -define bmp:format=bmp3
//examine the BMP file using magick identify -verbose output.bmp
// REQUIRES IMAGE MAGICK 7+
//exec(`magick identify -verbose ${tempBmp}`, (err, stdout, stderr) => {
// console.log('BMP file information:', stdout);
//});
// Read the BMP file
const bmpBuffer = await fs.readFile(tempBmp);
res.setHeader('Content-Type', 'image/bmp');
// Update Content-Disposition to include both plugin ID and layout
const downloadFilename = `${pluginId === '.' ? 'plugin' : pluginId}-${layout}.bmp`;
res.setHeader('Content-Disposition', `attachment; filename=${downloadFilename}`);
res.send(bmpBuffer);
// Leaving the ImageMagick temporary BMP file intact in the tmp directory.
} catch (error) {
console.error('Error generating display:', error);
res.status(500).send('Error generating display: ' + error.message);
}
};
// Mount API routes
app.use('/api', authMiddleware, apiRoutes);
// Mount the admin routes
app.use('/admin', authMiddleware, adminRouter);
// Keep existing display endpoint for BMP generation
app.get('/display', handleDisplay);
async function startServer() {
try {
console.log('🚦 Rate limit set to:', config.MAX_REQUESTS_PER_5_MIN, 'requests per 5 minutes');
// Add plugin mode detection and logging
const isSinglePluginMode = await config.isPluginDirectory(config.PLUGINS_PATH);
if (isSinglePluginMode) {
console.log('🔌 Running in single plugin mode since config.toml file was found in the folder');
console.log(`📂 Plugin folder: ${path.basename(config.PLUGINS_PATH)}`);
} else {
console.log('🔌 Running in multi-plugin mode since no settings.yml file was found in the folder');
console.log(`📂 Plugins directory: ${config.PLUGINS_PATH}`);
}
console.log('💾 Cache directory set to:', config.CACHE_PATH);
await downloadAssets();
app.listen(config.PORT, () => {
console.log(`Test app running at http://localhost:${config.PORT}`);
});
} catch (error) {
console.error('Failed to start server:', error);
process.exit(1);
}
}
startServer();