webspresso 0.0.51 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/core/orm/index.js +4 -0
- package/core/orm/model.js +2 -0
- package/core/orm/types.js +2 -0
- package/core/orm/utils.js +28 -0
- package/package.json +1 -1
- package/plugins/admin-panel/admin-user-model.js +1 -0
- package/plugins/admin-panel/api.js +12 -8
- package/plugins/admin-panel/components.js +6 -3
- package/plugins/admin-panel/core/api-extensions.js +7 -4
- package/plugins/site-analytics/admin-component.js +30 -1
- package/plugins/site-analytics/api-handlers.js +32 -1
- package/plugins/site-analytics/client-error-handler.js +84 -0
- package/plugins/site-analytics/client-error-tracker.js +69 -0
- package/plugins/site-analytics/index.js +25 -1
- package/plugins/site-analytics/tracking.js +44 -4
package/README.md
CHANGED
|
@@ -1164,9 +1164,13 @@ const User = defineModel({
|
|
|
1164
1164
|
timestamps: true, // Auto-manage created_at/updated_at
|
|
1165
1165
|
tenant: 'tenant_id', // Multi-tenant column (optional)
|
|
1166
1166
|
},
|
|
1167
|
+
|
|
1168
|
+
hidden: ['password_hash', 'api_token'], // Never expose in API/templates (security)
|
|
1167
1169
|
});
|
|
1168
1170
|
```
|
|
1169
1171
|
|
|
1172
|
+
**Hidden columns:** Add column names to `hidden` so they are never exposed in admin API responses, exports, or when passing to templates. Use for sensitive data like `password_hash`, `api_token`, `secret_key`. The admin panel will exclude these from list views and forms automatically.
|
|
1173
|
+
|
|
1170
1174
|
### Auto-Loading Models
|
|
1171
1175
|
|
|
1172
1176
|
Models are automatically loaded from the `models/` directory when you create a database instance:
|
package/core/orm/index.js
CHANGED
|
@@ -13,6 +13,7 @@ const { createMigrationManager } = require('./migrations');
|
|
|
13
13
|
const { createSeeder } = require('./seeder');
|
|
14
14
|
const { createScopeContext } = require('./scopes');
|
|
15
15
|
const { ModelEvents, Hooks, HookCancellationError, createEventContext } = require('./events');
|
|
16
|
+
const { omitHiddenColumns, sanitizeForOutput } = require('./utils');
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Create a database instance
|
|
@@ -272,6 +273,9 @@ module.exports = {
|
|
|
272
273
|
// Column utilities
|
|
273
274
|
extractColumnsFromSchema,
|
|
274
275
|
getColumnMeta,
|
|
276
|
+
// Output sanitization (exclude hidden columns from API/templates)
|
|
277
|
+
omitHiddenColumns,
|
|
278
|
+
sanitizeForOutput,
|
|
275
279
|
// Events/Signals
|
|
276
280
|
ModelEvents,
|
|
277
281
|
Hooks,
|
package/core/orm/model.js
CHANGED
|
@@ -28,6 +28,7 @@ function defineModel(options) {
|
|
|
28
28
|
scopes = {},
|
|
29
29
|
admin = {},
|
|
30
30
|
hooks = {},
|
|
31
|
+
hidden = [],
|
|
31
32
|
} = options;
|
|
32
33
|
|
|
33
34
|
// Validate required fields
|
|
@@ -88,6 +89,7 @@ function defineModel(options) {
|
|
|
88
89
|
customFields: admin.customFields || {},
|
|
89
90
|
queries: admin.queries || {},
|
|
90
91
|
},
|
|
92
|
+
hidden: Array.isArray(hidden) ? hidden : [],
|
|
91
93
|
hooks: {},
|
|
92
94
|
};
|
|
93
95
|
|
package/core/orm/types.js
CHANGED
|
@@ -147,6 +147,7 @@
|
|
|
147
147
|
* @property {RelationsMap} [relations={}] - Relation definitions
|
|
148
148
|
* @property {ScopeOptions} [scopes={}] - Scope options
|
|
149
149
|
* @property {AdminMetadata} [admin] - Admin panel metadata
|
|
150
|
+
* @property {string[]} [hidden=[]] - Column names to never expose in API/templates (e.g. password_hash, api_token)
|
|
150
151
|
*/
|
|
151
152
|
|
|
152
153
|
/**
|
|
@@ -159,6 +160,7 @@
|
|
|
159
160
|
* @property {ScopeOptions} scopes - Scope options
|
|
160
161
|
* @property {Map<string, ColumnMeta>} columns - Parsed column metadata
|
|
161
162
|
* @property {AdminMetadata} [admin] - Admin panel metadata
|
|
163
|
+
* @property {string[]} hidden - Column names never exposed in API/templates
|
|
162
164
|
*/
|
|
163
165
|
|
|
164
166
|
// ============================================================================
|
package/core/orm/utils.js
CHANGED
|
@@ -114,9 +114,37 @@ function deepClone(obj) {
|
|
|
114
114
|
return cloned;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Remove hidden columns from a record for safe API/template output
|
|
119
|
+
* @param {Object} record - Record from database
|
|
120
|
+
* @param {import('./types').ModelDefinition} model - Model definition with hidden columns
|
|
121
|
+
* @returns {Object} Record without hidden columns
|
|
122
|
+
*/
|
|
123
|
+
function omitHiddenColumns(record, model) {
|
|
124
|
+
if (!record) return record;
|
|
125
|
+
if (!model?.hidden?.length) return record;
|
|
126
|
+
return omit(record, model.hidden);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Remove hidden columns from records (array or single) for safe output
|
|
131
|
+
* @param {Object|Object[]} records - Record(s) from database
|
|
132
|
+
* @param {import('./types').ModelDefinition} model - Model definition
|
|
133
|
+
* @returns {Object|Object[]} Sanitized record(s)
|
|
134
|
+
*/
|
|
135
|
+
function sanitizeForOutput(records, model) {
|
|
136
|
+
if (!model?.hidden?.length) return records;
|
|
137
|
+
if (Array.isArray(records)) {
|
|
138
|
+
return records.map((r) => omit(r, model.hidden));
|
|
139
|
+
}
|
|
140
|
+
return omit(records, model.hidden);
|
|
141
|
+
}
|
|
142
|
+
|
|
117
143
|
module.exports = {
|
|
118
144
|
pick,
|
|
119
145
|
omit,
|
|
146
|
+
omitHiddenColumns,
|
|
147
|
+
sanitizeForOutput,
|
|
120
148
|
formatDateForDb,
|
|
121
149
|
generateMigrationTimestamp,
|
|
122
150
|
snakeToCamel,
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
const { getAllModels, getModel } = require('../../core/orm/model');
|
|
8
|
+
const { sanitizeForOutput } = require('../../core/orm/utils');
|
|
8
9
|
const { checkAdminExists, setupAdmin, login, logout, requireAuth } = require('./auth');
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -198,9 +199,11 @@ function createApiHandlers(options) {
|
|
|
198
199
|
return res.status(403).json({ error: 'Model not enabled in admin panel' });
|
|
199
200
|
}
|
|
200
201
|
|
|
201
|
-
// Build column metadata
|
|
202
|
+
// Build column metadata (hidden columns excluded from list/forms for security)
|
|
203
|
+
const hiddenSet = new Set(model.hidden || []);
|
|
202
204
|
const columns = [];
|
|
203
205
|
for (const [name, meta] of model.columns) {
|
|
206
|
+
const isHidden = hiddenSet.has(name);
|
|
204
207
|
columns.push({
|
|
205
208
|
name,
|
|
206
209
|
type: meta.type,
|
|
@@ -214,7 +217,8 @@ function createApiHandlers(options) {
|
|
|
214
217
|
autoIncrement: meta.autoIncrement || false,
|
|
215
218
|
customField: model.admin.customFields?.[name] || null,
|
|
216
219
|
validations: meta.validations || null,
|
|
217
|
-
ui: meta.ui || null,
|
|
220
|
+
ui: meta.ui ? { ...meta.ui, hidden: isHidden || meta.ui.hidden } : (isHidden ? { hidden: true } : null),
|
|
221
|
+
hidden: isHidden, // Excluded from list display and API responses
|
|
218
222
|
});
|
|
219
223
|
}
|
|
220
224
|
|
|
@@ -394,7 +398,7 @@ function createApiHandlers(options) {
|
|
|
394
398
|
const records = await query.list();
|
|
395
399
|
|
|
396
400
|
res.json({
|
|
397
|
-
data: records,
|
|
401
|
+
data: sanitizeForOutput(records, model),
|
|
398
402
|
pagination: {
|
|
399
403
|
page,
|
|
400
404
|
perPage,
|
|
@@ -426,7 +430,7 @@ function createApiHandlers(options) {
|
|
|
426
430
|
return res.status(404).json({ error: 'Record not found' });
|
|
427
431
|
}
|
|
428
432
|
|
|
429
|
-
res.json({ data: record });
|
|
433
|
+
res.json({ data: sanitizeForOutput(record, model) });
|
|
430
434
|
} catch (error) {
|
|
431
435
|
res.status(500).json({ error: error.message });
|
|
432
436
|
}
|
|
@@ -459,7 +463,7 @@ function createApiHandlers(options) {
|
|
|
459
463
|
const repo = db.getRepository(model.name);
|
|
460
464
|
const record = await repo.create(req.body);
|
|
461
465
|
|
|
462
|
-
res.status(201).json({ data: record });
|
|
466
|
+
res.status(201).json({ data: sanitizeForOutput(record, model) });
|
|
463
467
|
} catch (error) {
|
|
464
468
|
res.status(400).json({ error: error.message });
|
|
465
469
|
}
|
|
@@ -496,7 +500,7 @@ function createApiHandlers(options) {
|
|
|
496
500
|
return res.status(404).json({ error: 'Record not found' });
|
|
497
501
|
}
|
|
498
502
|
|
|
499
|
-
res.json({ data: record });
|
|
503
|
+
res.json({ data: sanitizeForOutput(record, model) });
|
|
500
504
|
} catch (error) {
|
|
501
505
|
res.status(400).json({ error: error.message });
|
|
502
506
|
}
|
|
@@ -550,7 +554,7 @@ function createApiHandlers(options) {
|
|
|
550
554
|
return res.status(404).json({ error: 'Record not found in trash' });
|
|
551
555
|
}
|
|
552
556
|
|
|
553
|
-
res.json({ success: true, data: record });
|
|
557
|
+
res.json({ success: true, data: sanitizeForOutput(record, model) });
|
|
554
558
|
} catch (error) {
|
|
555
559
|
res.status(500).json({ error: error.message });
|
|
556
560
|
}
|
|
@@ -579,7 +583,7 @@ function createApiHandlers(options) {
|
|
|
579
583
|
// Get all related records (for dropdown/select)
|
|
580
584
|
const records = await relatedRepo.findAll();
|
|
581
585
|
|
|
582
|
-
res.json({ data: records });
|
|
586
|
+
res.json({ data: sanitizeForOutput(records, relatedModel) });
|
|
583
587
|
} catch (error) {
|
|
584
588
|
res.status(500).json({ error: error.message });
|
|
585
589
|
}
|
|
@@ -1484,12 +1484,15 @@ const BulkFieldUpdateDropdown = {
|
|
|
1484
1484
|
// Get columns to display in table (limit to reasonable number)
|
|
1485
1485
|
function getDisplayColumns(columns) {
|
|
1486
1486
|
if (!columns || columns.length === 0) return [];
|
|
1487
|
-
|
|
1487
|
+
|
|
1488
|
+
// Filter out hidden columns (password_hash, api_token, etc.)
|
|
1489
|
+
const visible = [...columns].filter((col) => !col.hidden);
|
|
1490
|
+
|
|
1488
1491
|
// Prioritize: id, name/title, then others (excluding long text/json fields)
|
|
1489
1492
|
const priority = ['id', 'name', 'title', 'email', 'slug', 'status', 'published', 'created_at'];
|
|
1490
1493
|
const exclude = ['password', 'content', 'body', 'description']; // Usually too long
|
|
1491
|
-
|
|
1492
|
-
const sorted =
|
|
1494
|
+
|
|
1495
|
+
const sorted = visible.sort((a, b) => {
|
|
1493
1496
|
const aIdx = priority.indexOf(a.name);
|
|
1494
1497
|
const bIdx = priority.indexOf(b.name);
|
|
1495
1498
|
if (aIdx !== -1 && bIdx !== -1) return aIdx - bIdx;
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* @module plugins/admin-panel/core/api-extensions
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
const { sanitizeForOutput } = require('../../../core/orm/utils');
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
10
|
* Build query with filters applied
|
|
9
11
|
* @param {Object} repo - Repository instance
|
|
@@ -573,8 +575,9 @@ function createExtensionApiHandlers(options) {
|
|
|
573
575
|
}
|
|
574
576
|
|
|
575
577
|
if (format === 'csv') {
|
|
576
|
-
// CSV export
|
|
577
|
-
const
|
|
578
|
+
// CSV export (exclude hidden columns)
|
|
579
|
+
const hiddenSet = new Set(model.hidden || []);
|
|
580
|
+
const columns = Array.from(model.columns.keys()).filter((c) => !hiddenSet.has(c));
|
|
578
581
|
const header = columns.join(',');
|
|
579
582
|
const rows = records.map(record => {
|
|
580
583
|
return columns.map(col => {
|
|
@@ -595,8 +598,8 @@ function createExtensionApiHandlers(options) {
|
|
|
595
598
|
res.setHeader('Content-Disposition', `attachment; filename="${modelName}_export.csv"`);
|
|
596
599
|
res.json({ data: csvContent, format: 'csv' });
|
|
597
600
|
} else {
|
|
598
|
-
// JSON export
|
|
599
|
-
res.json({ data: records, model: modelName, exportedAt: new Date().toISOString() });
|
|
601
|
+
// JSON export (exclude hidden columns)
|
|
602
|
+
res.json({ data: sanitizeForOutput(records, model), model: modelName, exportedAt: new Date().toISOString() });
|
|
600
603
|
}
|
|
601
604
|
} catch (error) {
|
|
602
605
|
console.error('Export error:', error);
|
|
@@ -218,6 +218,7 @@ var AnalyticsPage = {
|
|
|
218
218
|
analyticsApi('top-pages', days),
|
|
219
219
|
analyticsApi('bot-activity', days),
|
|
220
220
|
analyticsApi('countries', days),
|
|
221
|
+
analyticsApi('client-errors', days),
|
|
221
222
|
analyticsApi('recent', days),
|
|
222
223
|
]).then(function(results) {
|
|
223
224
|
vnode.state.stats = results[0];
|
|
@@ -225,7 +226,8 @@ var AnalyticsPage = {
|
|
|
225
226
|
vnode.state.topPages = results[2];
|
|
226
227
|
vnode.state.botActivity = results[3];
|
|
227
228
|
vnode.state.countries = results[4];
|
|
228
|
-
vnode.state.
|
|
229
|
+
vnode.state.clientErrors = results[5];
|
|
230
|
+
vnode.state.recent = results[6];
|
|
229
231
|
vnode.state.loading = false;
|
|
230
232
|
|
|
231
233
|
if (chartInstance) {
|
|
@@ -384,6 +386,33 @@ var AnalyticsPage = {
|
|
|
384
386
|
]),
|
|
385
387
|
]),
|
|
386
388
|
|
|
389
|
+
// Client Errors
|
|
390
|
+
m('div.bg-white.rounded-lg.shadow.mb-6', [
|
|
391
|
+
m('div.px-5.py-4.border-b.border-gray-100.flex.items-center.justify-between', [
|
|
392
|
+
m('h3.text-sm.font-semibold.text-gray-900.flex.items-center.gap-2', [
|
|
393
|
+
m('span', '⚠️'),
|
|
394
|
+
'Client Errors',
|
|
395
|
+
]),
|
|
396
|
+
m('span.text-xs.text-gray-400', 'Last ' + s.days + ' days'),
|
|
397
|
+
]),
|
|
398
|
+
m('div.p-4.max-h-64.overflow-y-auto', [
|
|
399
|
+
!s.clientErrors || s.clientErrors.length === 0
|
|
400
|
+
? m('p.text-gray-400.text-sm.text-center.py-6', 'No client errors')
|
|
401
|
+
: s.clientErrors.slice(0, 15).map(function(err) {
|
|
402
|
+
return m('div.border-b.border-gray-50.pb-3.mb-3.last:border-0.last:mb-0.last:pb-0', [
|
|
403
|
+
m('div.flex.items-start.gap-2', [
|
|
404
|
+
m('span.text-xs.px-1.5.py-0.5.rounded.bg-red-100.text-red-700', err.error_type || 'error'),
|
|
405
|
+
m('span.text-xs.text-gray-500', relativeTime(err.created_at)),
|
|
406
|
+
]),
|
|
407
|
+
m('p.text-sm.text-gray-800.font-mono.break-all.mt-1', {
|
|
408
|
+
title: err.stack || err.message,
|
|
409
|
+
}, (err.message || '').slice(0, 120) + (err.message && err.message.length > 120 ? '…' : '')),
|
|
410
|
+
err.path && m('p.text-xs.text-gray-500.mt-0.5', err.path),
|
|
411
|
+
]);
|
|
412
|
+
}),
|
|
413
|
+
]),
|
|
414
|
+
]),
|
|
415
|
+
|
|
387
416
|
// Country Stats
|
|
388
417
|
m('div.bg-white.rounded-lg.shadow.mb-6', [
|
|
389
418
|
m('div.px-5.py-4.border-b.border-gray-100.flex.items-center.justify-between', [
|
|
@@ -11,7 +11,11 @@
|
|
|
11
11
|
* @param {string} [options.tableName='analytics_page_views']
|
|
12
12
|
*/
|
|
13
13
|
function createAnalyticsApiHandlers(options) {
|
|
14
|
-
const {
|
|
14
|
+
const {
|
|
15
|
+
knex,
|
|
16
|
+
tableName = 'analytics_page_views',
|
|
17
|
+
errorsTableName = 'analytics_client_errors',
|
|
18
|
+
} = options;
|
|
15
19
|
|
|
16
20
|
function parseDays(req) {
|
|
17
21
|
const days = parseInt(req.query.days) || 30;
|
|
@@ -208,6 +212,32 @@ function createAnalyticsApiHandlers(options) {
|
|
|
208
212
|
}
|
|
209
213
|
}
|
|
210
214
|
|
|
215
|
+
/**
|
|
216
|
+
* GET /client-errors - Recent client-side JS errors
|
|
217
|
+
*/
|
|
218
|
+
async function getClientErrors(req, res) {
|
|
219
|
+
try {
|
|
220
|
+
const days = parseDays(req);
|
|
221
|
+
const since = sinceDate(days);
|
|
222
|
+
const limit = Math.min(parseInt(req.query.limit) || 50, 200);
|
|
223
|
+
|
|
224
|
+
const hasTable = await knex.schema.hasTable(errorsTableName);
|
|
225
|
+
if (!hasTable) {
|
|
226
|
+
return res.json([]);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const rows = await knex(errorsTableName)
|
|
230
|
+
.select('id', 'error_type', 'message', 'stack', 'path', 'created_at')
|
|
231
|
+
.where('created_at', '>=', since)
|
|
232
|
+
.orderBy('created_at', 'desc')
|
|
233
|
+
.limit(limit);
|
|
234
|
+
|
|
235
|
+
res.json(rows);
|
|
236
|
+
} catch (e) {
|
|
237
|
+
res.status(500).json({ error: e.message });
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
211
241
|
/**
|
|
212
242
|
* GET /recent - Recent page views
|
|
213
243
|
*/
|
|
@@ -233,6 +263,7 @@ function createAnalyticsApiHandlers(options) {
|
|
|
233
263
|
getTopPages,
|
|
234
264
|
getBotActivity,
|
|
235
265
|
getCountries,
|
|
266
|
+
getClientErrors,
|
|
236
267
|
getRecent,
|
|
237
268
|
};
|
|
238
269
|
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client Error Report Handler
|
|
3
|
+
* Receives error reports from browser and stores in DB
|
|
4
|
+
* @module plugins/site-analytics/client-error-handler
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const DEFAULT_TABLE = 'analytics_client_errors';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create the client errors table
|
|
11
|
+
*/
|
|
12
|
+
async function ensureErrorsTable(knex, tableName = DEFAULT_TABLE) {
|
|
13
|
+
const exists = await knex.schema.hasTable(tableName);
|
|
14
|
+
if (!exists) {
|
|
15
|
+
await knex.schema.createTable(tableName, (table) => {
|
|
16
|
+
table.bigIncrements('id').primary();
|
|
17
|
+
table.string('error_type', 50).index(); // 'error' | 'unhandledrejection'
|
|
18
|
+
table.string('message', 500).index();
|
|
19
|
+
table.text('stack');
|
|
20
|
+
table.string('path', 500).index();
|
|
21
|
+
table.string('referrer', 500);
|
|
22
|
+
table.text('user_agent');
|
|
23
|
+
table.string('source', 500); // script url
|
|
24
|
+
table.integer('line');
|
|
25
|
+
table.integer('column');
|
|
26
|
+
table.timestamp('created_at').defaultTo(knex.fn.now()).index();
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Create error report handler
|
|
33
|
+
* @param {Object} options
|
|
34
|
+
* @param {Object} options.knex - Knex instance
|
|
35
|
+
* @param {string} [options.tableName='analytics_client_errors']
|
|
36
|
+
*/
|
|
37
|
+
function createErrorReportHandler(options) {
|
|
38
|
+
const { knex, tableName = DEFAULT_TABLE } = options;
|
|
39
|
+
let tableReady = false;
|
|
40
|
+
|
|
41
|
+
return async function errorReportHandler(req, res) {
|
|
42
|
+
if (req.method !== 'POST') {
|
|
43
|
+
return res.status(405).json({ error: 'Method not allowed' });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
if (!tableReady) {
|
|
48
|
+
await ensureErrorsTable(knex, tableName);
|
|
49
|
+
tableReady = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const body = req.body || {};
|
|
53
|
+
const { type, message, stack, path, referrer, userAgent, source, line, column } = body;
|
|
54
|
+
|
|
55
|
+
if (!message && !stack) {
|
|
56
|
+
return res.status(400).json({ error: 'message or stack required' });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await knex(tableName).insert({
|
|
60
|
+
error_type: type || 'error',
|
|
61
|
+
message: String(message || '').slice(0, 500),
|
|
62
|
+
stack: stack ? String(stack).slice(0, 5000) : null,
|
|
63
|
+
path: path ? String(path).slice(0, 500) : null,
|
|
64
|
+
referrer: referrer ? String(referrer).slice(0, 500) : null,
|
|
65
|
+
user_agent: userAgent ? String(userAgent).slice(0, 1000) : null,
|
|
66
|
+
source: source ? String(source).slice(0, 500) : null,
|
|
67
|
+
line: line != null ? parseInt(line) : null,
|
|
68
|
+
column: column != null ? parseInt(column) : null,
|
|
69
|
+
created_at: knex.fn.now(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
res.status(202).json({ ok: true });
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error('[site-analytics] Client error report failed:', e.message);
|
|
75
|
+
res.status(500).json({ error: 'Report failed' });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = {
|
|
81
|
+
createErrorReportHandler,
|
|
82
|
+
ensureErrorsTable,
|
|
83
|
+
DEFAULT_TABLE,
|
|
84
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client Error Tracker
|
|
3
|
+
* Generates inline script to catch JS errors and unhandled rejections, reports to backend
|
|
4
|
+
* @module plugins/site-analytics/client-error-tracker
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Generate client-side error tracking script
|
|
9
|
+
* @param {Object} options
|
|
10
|
+
* @param {string} [options.endpoint='/_analytics/report-error'] - POST endpoint for error reports
|
|
11
|
+
* @returns {string} Inline script content
|
|
12
|
+
*/
|
|
13
|
+
function generateErrorTrackerScript(options = {}) {
|
|
14
|
+
const endpoint = options.endpoint || '/_analytics/report-error';
|
|
15
|
+
|
|
16
|
+
return `
|
|
17
|
+
(function() {
|
|
18
|
+
var endpoint = ${JSON.stringify(endpoint)};
|
|
19
|
+
var reported = {};
|
|
20
|
+
var MAX_STACK = 2000;
|
|
21
|
+
|
|
22
|
+
function report(type, message, stack, extra) {
|
|
23
|
+
var key = type + ':' + (message || '').slice(0, 100);
|
|
24
|
+
if (reported[key]) return;
|
|
25
|
+
reported[key] = true;
|
|
26
|
+
|
|
27
|
+
var payload = {
|
|
28
|
+
type: type,
|
|
29
|
+
message: String(message || 'Unknown error').slice(0, 500),
|
|
30
|
+
stack: stack ? String(stack).slice(0, MAX_STACK) : null,
|
|
31
|
+
path: window.location.pathname || '/',
|
|
32
|
+
referrer: document.referrer || null,
|
|
33
|
+
userAgent: navigator.userAgent || null
|
|
34
|
+
};
|
|
35
|
+
if (extra) {
|
|
36
|
+
for (var k in extra) payload[k] = extra[k];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
fetch(endpoint, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { 'Content-Type': 'application/json' },
|
|
43
|
+
body: JSON.stringify(payload),
|
|
44
|
+
keepalive: true
|
|
45
|
+
}).catch(function(){});
|
|
46
|
+
} catch (e) {}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
window.onerror = function(msg, url, line, col, err) {
|
|
50
|
+
report('error', err ? err.message : msg, err ? err.stack : null, { line: line, column: col, source: url });
|
|
51
|
+
return false;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
window.addEventListener('unhandledrejection', function(e) {
|
|
55
|
+
var msg = e.reason;
|
|
56
|
+
var stack = null;
|
|
57
|
+
if (msg && typeof msg === 'object') {
|
|
58
|
+
stack = msg.stack;
|
|
59
|
+
msg = msg.message || String(msg);
|
|
60
|
+
} else {
|
|
61
|
+
msg = String(msg);
|
|
62
|
+
}
|
|
63
|
+
report('unhandledrejection', msg, stack);
|
|
64
|
+
});
|
|
65
|
+
})();
|
|
66
|
+
`.trim();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = { generateErrorTrackerScript };
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
const { createTrackingMiddleware } = require('./tracking');
|
|
8
8
|
const { createAnalyticsApiHandlers } = require('./api-handlers');
|
|
9
9
|
const { generateAnalyticsComponent } = require('./admin-component');
|
|
10
|
+
const { generateErrorTrackerScript } = require('./client-error-tracker');
|
|
11
|
+
const { createErrorReportHandler } = require('./client-error-handler');
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
14
|
* Site Analytics Plugin Factory
|
|
@@ -15,6 +17,10 @@ const { generateAnalyticsComponent } = require('./admin-component');
|
|
|
15
17
|
* @param {string[]} [options.excludePaths=[]] - Extra paths to exclude from tracking
|
|
16
18
|
* @param {boolean} [options.trackBots=true] - Record bot visits (still counted separately)
|
|
17
19
|
* @param {string} [options.tableName='analytics_page_views'] - DB table name
|
|
20
|
+
* @param {number} [options.batchSize=20] - Flush page view queue when it reaches this size
|
|
21
|
+
* @param {number} [options.flushIntervalMs=3000] - Flush interval for low traffic
|
|
22
|
+
* @param {boolean} [options.trackClientErrors=true] - Capture and report client-side JS errors
|
|
23
|
+
* @param {string} [options.errorsTableName='analytics_client_errors'] - Client errors table
|
|
18
24
|
* @returns {Object} Plugin definition
|
|
19
25
|
*/
|
|
20
26
|
function siteAnalyticsPlugin(options = {}) {
|
|
@@ -23,6 +29,8 @@ function siteAnalyticsPlugin(options = {}) {
|
|
|
23
29
|
excludePaths = [],
|
|
24
30
|
trackBots = true,
|
|
25
31
|
tableName = 'analytics_page_views',
|
|
32
|
+
trackClientErrors = true,
|
|
33
|
+
errorsTableName = 'analytics_client_errors',
|
|
26
34
|
} = options;
|
|
27
35
|
|
|
28
36
|
if (!db) {
|
|
@@ -48,9 +56,20 @@ function siteAnalyticsPlugin(options = {}) {
|
|
|
48
56
|
excludePaths,
|
|
49
57
|
trackBots,
|
|
50
58
|
tableName,
|
|
59
|
+
batchSize: options.batchSize ?? 20,
|
|
60
|
+
flushIntervalMs: options.flushIntervalMs ?? 3000,
|
|
51
61
|
});
|
|
52
62
|
|
|
53
63
|
ctx.app.use(trackingMiddleware);
|
|
64
|
+
|
|
65
|
+
// Client error tracking: public POST endpoint + inject script
|
|
66
|
+
if (trackClientErrors) {
|
|
67
|
+
const errorHandler = createErrorReportHandler({ knex, tableName: errorsTableName });
|
|
68
|
+
ctx.addRoute('post', '/_analytics/report-error', errorHandler);
|
|
69
|
+
|
|
70
|
+
const script = generateErrorTrackerScript({ endpoint: '/_analytics/report-error' });
|
|
71
|
+
ctx.injectBody(`<script>${script}</script>`, { id: 'site-analytics-error-tracker', priority: 5 });
|
|
72
|
+
}
|
|
54
73
|
},
|
|
55
74
|
|
|
56
75
|
onRoutesReady(ctx) {
|
|
@@ -61,7 +80,11 @@ function siteAnalyticsPlugin(options = {}) {
|
|
|
61
80
|
}
|
|
62
81
|
|
|
63
82
|
const knex = db.knex || db;
|
|
64
|
-
const handlers = createAnalyticsApiHandlers({
|
|
83
|
+
const handlers = createAnalyticsApiHandlers({
|
|
84
|
+
knex,
|
|
85
|
+
tableName,
|
|
86
|
+
errorsTableName,
|
|
87
|
+
});
|
|
65
88
|
|
|
66
89
|
adminApi.registerModule({
|
|
67
90
|
id: 'analytics',
|
|
@@ -90,6 +113,7 @@ function siteAnalyticsPlugin(options = {}) {
|
|
|
90
113
|
{ method: 'get', path: '/top-pages', handler: handlers.getTopPages },
|
|
91
114
|
{ method: 'get', path: '/bot-activity', handler: handlers.getBotActivity },
|
|
92
115
|
{ method: 'get', path: '/countries', handler: handlers.getCountries },
|
|
116
|
+
{ method: 'get', path: '/client-errors', handler: handlers.getClientErrors },
|
|
93
117
|
{ method: 'get', path: '/recent', handler: handlers.getRecent },
|
|
94
118
|
],
|
|
95
119
|
},
|
|
@@ -36,6 +36,8 @@ function getClientIp(req) {
|
|
|
36
36
|
* @param {string[]} [options.excludePaths] - Paths to exclude from tracking
|
|
37
37
|
* @param {boolean} [options.trackBots=true] - Whether to record bot visits
|
|
38
38
|
* @param {string} [options.tableName='analytics_page_views'] - DB table name
|
|
39
|
+
* @param {number} [options.batchSize=20] - Flush when queue reaches this size
|
|
40
|
+
* @param {number} [options.flushIntervalMs=3000] - Flush interval for low traffic
|
|
39
41
|
*/
|
|
40
42
|
function createTrackingMiddleware(options) {
|
|
41
43
|
const {
|
|
@@ -43,10 +45,14 @@ function createTrackingMiddleware(options) {
|
|
|
43
45
|
excludePaths = [],
|
|
44
46
|
trackBots = true,
|
|
45
47
|
tableName = 'analytics_page_views',
|
|
48
|
+
batchSize = 20,
|
|
49
|
+
flushIntervalMs = 3000,
|
|
46
50
|
} = options;
|
|
47
51
|
|
|
48
52
|
const allExcludes = [...DEFAULT_EXCLUDE, ...excludePaths];
|
|
49
53
|
let tableReady = false;
|
|
54
|
+
const queue = [];
|
|
55
|
+
let flushScheduled = false;
|
|
50
56
|
|
|
51
57
|
async function ensureTable() {
|
|
52
58
|
if (tableReady) return;
|
|
@@ -101,6 +107,37 @@ function createTrackingMiddleware(options) {
|
|
|
101
107
|
return sessionId;
|
|
102
108
|
}
|
|
103
109
|
|
|
110
|
+
async function flushQueue() {
|
|
111
|
+
if (queue.length === 0) return;
|
|
112
|
+
const batch = queue.splice(0, queue.length);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
await ensureTable();
|
|
116
|
+
for (const row of batch) {
|
|
117
|
+
row.created_at = knex.fn.now();
|
|
118
|
+
}
|
|
119
|
+
await knex(tableName).insert(batch);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.error('[site-analytics] Batch insert failed:', e.message);
|
|
122
|
+
// Re-queue failed items (optional - could drop to avoid loops)
|
|
123
|
+
queue.unshift(...batch);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function scheduleFlush() {
|
|
128
|
+
if (flushScheduled || queue.length === 0) return;
|
|
129
|
+
flushScheduled = true;
|
|
130
|
+
setTimeout(() => {
|
|
131
|
+
flushScheduled = false;
|
|
132
|
+
flushQueue().catch(() => {});
|
|
133
|
+
}, flushIntervalMs);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Periodic flush (catches low-traffic case)
|
|
137
|
+
setInterval(() => {
|
|
138
|
+
if (queue.length > 0) flushQueue().catch(() => {});
|
|
139
|
+
}, flushIntervalMs).unref();
|
|
140
|
+
|
|
104
141
|
return async function trackingMiddleware(req, res, next) {
|
|
105
142
|
next();
|
|
106
143
|
|
|
@@ -111,8 +148,6 @@ function createTrackingMiddleware(options) {
|
|
|
111
148
|
if (allExcludes.some(prefix => path.startsWith(prefix))) return;
|
|
112
149
|
|
|
113
150
|
try {
|
|
114
|
-
await ensureTable();
|
|
115
|
-
|
|
116
151
|
const userAgent = req.headers['user-agent'] || '';
|
|
117
152
|
const ip = getClientIp(req);
|
|
118
153
|
const { isBot, botName } = detectBot(userAgent);
|
|
@@ -125,7 +160,7 @@ function createTrackingMiddleware(options) {
|
|
|
125
160
|
const country = detectCountry(req);
|
|
126
161
|
const referrer = req.headers['referer'] || req.headers['referrer'] || null;
|
|
127
162
|
|
|
128
|
-
|
|
163
|
+
queue.push({
|
|
129
164
|
session_id: sessionId,
|
|
130
165
|
visitor_id: visitorId,
|
|
131
166
|
path,
|
|
@@ -135,8 +170,13 @@ function createTrackingMiddleware(options) {
|
|
|
135
170
|
country,
|
|
136
171
|
is_bot: isBot,
|
|
137
172
|
bot_name: botName,
|
|
138
|
-
created_at: knex.fn.now(),
|
|
139
173
|
});
|
|
174
|
+
|
|
175
|
+
if (queue.length >= batchSize) {
|
|
176
|
+
flushQueue().catch(() => {});
|
|
177
|
+
} else {
|
|
178
|
+
scheduleFlush();
|
|
179
|
+
}
|
|
140
180
|
} catch (e) {
|
|
141
181
|
// Non-blocking: don't let tracking errors affect the app
|
|
142
182
|
}
|