adula 2.1.0 → 2.3.0
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 +149 -18
- package/build/agent/AGENTS.template.md +4 -4
- package/build/agent/capabilities.md +22 -12
- package/build/agent/skills/adula-frontend-design/SKILL.md +6 -4
- package/build/agent/skills/idea-review/SKILL.md +2 -2
- package/build/commands/adula_assistant_prune.d.ts +10 -0
- package/build/commands/adula_assistant_prune.js +12 -0
- package/build/commands/adula_outbox.d.ts +10 -0
- package/build/commands/adula_outbox.js +11 -0
- package/build/commands/adula_restore_reconcile.d.ts +10 -0
- package/build/commands/adula_restore_reconcile.js +34 -0
- package/build/commands/adula_runtime_health.d.ts +10 -0
- package/build/commands/adula_runtime_health.js +27 -0
- package/build/commands/adula_uploads_prune.d.ts +10 -0
- package/build/commands/adula_uploads_prune.js +16 -0
- package/build/commands/app.d.ts +10 -0
- package/build/commands/app.js +64 -0
- package/build/commands/backup_create.d.ts +11 -0
- package/build/commands/backup_create.js +99 -0
- package/build/commands/backup_open.d.ts +16 -0
- package/build/commands/backup_open.js +48 -0
- package/build/commands/backup_restore_files.d.ts +11 -0
- package/build/commands/backup_restore_files.js +44 -0
- package/build/commands/backup_restore_test.d.ts +12 -0
- package/build/commands/backup_restore_test.js +188 -0
- package/build/commands/backup_seal.d.ts +12 -0
- package/build/commands/backup_seal.js +36 -0
- package/build/commands/backup_services.d.ts +33 -0
- package/build/commands/backup_services.js +6 -0
- package/build/commands/backup_verify.d.ts +9 -0
- package/build/commands/backup_verify.js +59 -0
- package/build/commands/backup_verify_snapshot.d.ts +10 -0
- package/build/commands/backup_verify_snapshot.js +37 -0
- package/build/commands/client.d.ts +16 -0
- package/build/commands/client.js +71 -0
- package/build/commands/doctor.js +21 -1
- package/build/commands/install.d.ts +8 -0
- package/build/commands/install.js +69 -45
- package/build/commands/main.d.ts +3 -2
- package/build/commands/main.js +32 -0
- package/build/commands/module_add.js +4 -20
- package/build/commands/resource.d.ts +2 -0
- package/build/commands/resource.js +12 -3
- package/build/commands/resource_migration.d.ts +15 -0
- package/build/commands/resource_migration.js +81 -0
- package/build/commands/resource_snapshot.d.ts +2 -0
- package/build/commands/resource_snapshot.js +21 -4
- package/build/commands/sla_evaluate.d.ts +14 -0
- package/build/commands/sla_evaluate.js +16 -0
- package/build/commands/ui.js +4 -2
- package/build/commands/ui_css.d.ts +9 -0
- package/build/commands/ui_css.js +19 -0
- package/build/commands/upgrade.d.ts +1 -1
- package/build/commands/upgrade.js +1 -1
- package/build/configure.js +1 -1
- package/build/database/migrations/1770000000020_two_factor.d.ts +5 -0
- package/build/database/migrations/1770000000020_two_factor.js +10 -0
- package/build/database/migrations/1770000000041_import_modes.d.ts +5 -0
- package/build/database/migrations/1770000000041_import_modes.js +10 -0
- package/build/database/migrations/1770000000042_sync_jobs.d.ts +5 -0
- package/build/database/migrations/1770000000042_sync_jobs.js +10 -0
- package/build/database/migrations/1770000000043_notification_channels.d.ts +5 -0
- package/build/database/migrations/1770000000043_notification_channels.js +10 -0
- package/build/database/migrations/1770000000044_channel_confirmations.d.ts +5 -0
- package/build/database/migrations/1770000000044_channel_confirmations.js +10 -0
- package/build/database/migrations/1770000000045_import_leases.d.ts +5 -0
- package/build/database/migrations/1770000000045_import_leases.js +10 -0
- package/build/database/migrations/1770000000046_workflow_delegation_sla.d.ts +5 -0
- package/build/database/migrations/1770000000046_workflow_delegation_sla.js +10 -0
- package/build/database/migrations/1770000000049_print_templates.d.ts +5 -0
- package/build/database/migrations/1770000000049_print_templates.js +10 -0
- package/build/database/migrations/1770000000051_comment_notifications.d.ts +5 -0
- package/build/database/migrations/1770000000051_comment_notifications.js +10 -0
- package/build/database/migrations/1770000000054_cloud_backups.d.ts +5 -0
- package/build/database/migrations/1770000000054_cloud_backups.js +10 -0
- package/build/database/migrations/1770000000055_ai_assistant.d.ts +5 -0
- package/build/database/migrations/1770000000055_ai_assistant.js +10 -0
- package/build/database/migrations/1770000000056_appearance.d.ts +5 -0
- package/build/database/migrations/1770000000056_appearance.js +10 -0
- package/build/index.d.ts +87 -11
- package/build/index.js +50 -7
- package/build/providers/adula_provider.d.ts +2 -0
- package/build/providers/adula_provider.js +7 -0
- package/build/src/admin/contracts.js +1 -1
- package/build/src/admin/controller.d.ts +14 -1
- package/build/src/admin/controller.js +91 -13
- package/build/src/admin/errors.d.ts +32 -0
- package/build/src/admin/errors.js +63 -1
- package/build/src/admin/presentation.d.ts +21 -2
- package/build/src/admin/resource/editor.d.ts +60 -0
- package/build/src/admin/resource/editor.js +345 -0
- package/build/src/admin/resource/export.d.ts +54 -0
- package/build/src/admin/resource/export.js +262 -0
- package/build/src/admin/resource/lifecycle.d.ts +23 -0
- package/build/src/admin/resource/lifecycle.js +91 -0
- package/build/src/admin/resource/query.d.ts +111 -0
- package/build/src/admin/resource/query.js +595 -0
- package/build/src/admin/resource/read.d.ts +109 -0
- package/build/src/admin/resource/read.js +388 -0
- package/build/src/admin/resource/shared.d.ts +93 -0
- package/build/src/admin/resource/shared.js +265 -0
- package/build/src/admin/resource/trash.d.ts +10 -0
- package/build/src/admin/resource/trash.js +56 -0
- package/build/src/admin/resource/write.d.ts +57 -0
- package/build/src/admin/resource/write.js +603 -0
- package/build/src/admin/resource_service.d.ts +80 -193
- package/build/src/admin/resource_service.js +81 -1343
- package/build/src/admin/resource_tools.d.ts +111 -0
- package/build/src/admin/resource_tools.js +139 -0
- package/build/src/ai/assistant.d.ts +100 -0
- package/build/src/ai/assistant.js +487 -0
- package/build/src/ai/client.d.ts +49 -0
- package/build/src/ai/client.js +85 -0
- package/build/src/ai/prompt.d.ts +23 -0
- package/build/src/ai/prompt.js +83 -0
- package/build/src/ai/settings.d.ts +56 -0
- package/build/src/ai/settings.js +214 -0
- package/build/src/ai/usage.d.ts +16 -0
- package/build/src/ai/usage.js +33 -0
- package/build/src/appearance/appearance.d.ts +134 -0
- package/build/src/appearance/appearance.js +461 -0
- package/build/src/appearance/colors.d.ts +43 -0
- package/build/src/appearance/colors.js +99 -0
- package/build/src/appearance/logo.d.ts +32 -0
- package/build/src/appearance/logo.js +112 -0
- package/build/src/attachments/attachment_service.d.ts +46 -2
- package/build/src/attachments/attachment_service.js +96 -4
- package/build/src/auth/actor_store.d.ts +5 -1
- package/build/src/auth/actor_store.js +8 -4
- package/build/src/auth/conditions.d.ts +5 -0
- package/build/src/auth/conditions.js +26 -3
- package/build/src/auth/sql.js +17 -5
- package/build/src/auth/two_factor.d.ts +96 -0
- package/build/src/auth/two_factor.js +259 -0
- package/build/src/backups/archive.d.ts +52 -0
- package/build/src/backups/archive.js +362 -0
- package/build/src/backups/cloud_backups.d.ts +133 -0
- package/build/src/backups/cloud_backups.js +650 -0
- package/build/src/backups/doctor.d.ts +7 -0
- package/build/src/backups/doctor.js +55 -0
- package/build/src/backups/providers.d.ts +81 -0
- package/build/src/backups/providers.js +470 -0
- package/build/src/collaboration/assignments.d.ts +59 -2
- package/build/src/collaboration/assignments.js +196 -74
- package/build/src/collaboration/record_collaboration.d.ts +21 -4
- package/build/src/collaboration/record_collaboration.js +126 -51
- package/build/src/collaboration/shared.d.ts +24 -0
- package/build/src/collaboration/shared.js +34 -0
- package/build/src/commands/app_scaffold.d.ts +72 -0
- package/build/src/commands/app_scaffold.js +285 -0
- package/build/src/commands/capabilities.js +35 -9
- package/build/src/commands/doctor.d.ts +24 -2
- package/build/src/commands/doctor.js +81 -6
- package/build/src/commands/gap_report.d.ts +1 -1
- package/build/src/commands/gap_report.js +2 -2
- package/build/src/commands/generator.d.ts +37 -1
- package/build/src/commands/generator.js +61 -18
- package/build/src/commands/snapshot.d.ts +24 -13
- package/build/src/commands/snapshot.js +54 -14
- package/build/src/commands/source_markers.d.ts +10 -0
- package/build/src/commands/source_markers.js +29 -0
- package/build/src/core/activity.d.ts +2 -0
- package/build/src/core/activity.js +7 -1
- package/build/src/core/administrators.d.ts +38 -0
- package/build/src/core/administrators.js +145 -0
- package/build/src/core/avatar_image.d.ts +33 -0
- package/build/src/core/avatar_image.js +278 -0
- package/build/src/core/html_sanitizer.d.ts +20 -0
- package/build/src/core/html_sanitizer.js +261 -0
- package/build/src/core/like.d.ts +5 -0
- package/build/src/core/like.js +5 -0
- package/build/src/core/lookups.d.ts +80 -0
- package/build/src/core/lookups.js +319 -0
- package/build/src/core/mail_settings.d.ts +83 -0
- package/build/src/core/mail_settings.js +274 -0
- package/build/src/core/mail_test.d.ts +16 -0
- package/build/src/core/mail_test.js +81 -6
- package/build/src/core/message_templates.d.ts +15 -2
- package/build/src/core/message_templates.js +81 -11
- package/build/src/core/module_seed.js +2 -1
- package/build/src/core/notification_channels.d.ts +235 -0
- package/build/src/core/notification_channels.js +660 -0
- package/build/src/core/roles.js +26 -9
- package/build/src/core/saved_views.d.ts +8 -1
- package/build/src/core/saved_views.js +91 -26
- package/build/src/core/sequences.d.ts +32 -0
- package/build/src/core/sequences.js +97 -0
- package/build/src/core/settings.d.ts +5 -0
- package/build/src/core/settings.js +31 -3
- package/build/src/core/template_language.d.ts +61 -0
- package/build/src/core/template_language.js +141 -0
- package/build/src/core/ui_preferences.d.ts +10 -0
- package/build/src/core/ui_preferences.js +15 -1
- package/build/src/core/user_avatars.d.ts +79 -0
- package/build/src/core/user_avatars.js +186 -0
- package/build/src/core/user_invitations.d.ts +5 -1
- package/build/src/core/user_invitations.js +7 -1
- package/build/src/core/user_preferences.d.ts +36 -0
- package/build/src/core/user_preferences.js +125 -0
- package/build/src/core/users.d.ts +2 -0
- package/build/src/core/users.js +5 -3
- package/build/src/database/calendar_days.d.ts +19 -0
- package/build/src/database/calendar_days.js +36 -0
- package/build/src/database/schema.d.ts +201 -2
- package/build/src/database/schema.js +713 -59
- package/build/src/database/schema_diff.d.ts +70 -0
- package/build/src/database/schema_diff.js +190 -0
- package/build/src/events/record_mutation.d.ts +2 -1
- package/build/src/events/record_mutation.js +1 -0
- package/build/src/integrations/api_client.d.ts +37 -0
- package/build/src/integrations/api_client.js +567 -0
- package/build/src/integrations/import_adapters.d.ts +37 -0
- package/build/src/integrations/import_adapters.js +35 -0
- package/build/src/integrations/imports.d.ts +130 -16
- package/build/src/integrations/imports.js +434 -73
- package/build/src/integrations/openapi.d.ts +3 -1
- package/build/src/integrations/openapi.js +465 -31
- package/build/src/integrations/outbound.d.ts +94 -0
- package/build/src/integrations/outbound.js +276 -0
- package/build/src/integrations/print.d.ts +22 -1
- package/build/src/integrations/print.js +68 -22
- package/build/src/integrations/print_templates.d.ts +216 -0
- package/build/src/integrations/print_templates.js +1017 -0
- package/build/src/integrations/sync_jobs.d.ts +139 -0
- package/build/src/integrations/sync_jobs.js +416 -0
- package/build/src/integrations/webhooks.d.ts +12 -12
- package/build/src/integrations/webhooks.js +34 -43
- package/build/src/integrations/xlsx.d.ts +41 -0
- package/build/src/integrations/xlsx.js +519 -0
- package/build/src/languages/ar.d.ts +11 -0
- package/build/src/languages/ar.js +358 -0
- package/build/src/languages/ar_ui.d.ts +461 -0
- package/build/src/languages/ar_ui.js +464 -0
- package/build/src/languages/catalog.d.ts +66 -0
- package/build/src/languages/catalog.js +152 -0
- package/build/src/languages/en.d.ts +7 -0
- package/build/src/languages/en.js +352 -0
- package/build/src/languages/en_ui.d.ts +3 -0
- package/build/src/languages/en_ui.js +458 -0
- package/build/src/languages/validation.d.ts +26 -0
- package/build/src/languages/validation.js +60 -0
- package/build/src/lists/bulk_actions.d.ts +89 -0
- package/build/src/lists/bulk_actions.js +268 -0
- package/build/src/lists/dashboards.d.ts +138 -0
- package/build/src/lists/dashboards.js +290 -0
- package/build/src/mcp/tools.js +9 -104
- package/build/src/resource/define_resource.d.ts +18 -1
- package/build/src/resource/define_resource.js +212 -18
- package/build/src/resource/definition.d.ts +44 -0
- package/build/src/resource/definition.js +171 -0
- package/build/src/resource/registry.js +15 -4
- package/build/src/resource/types.d.ts +313 -16
- package/build/src/resource/values.d.ts +56 -1
- package/build/src/resource/values.js +237 -5
- package/build/src/services/settings.d.ts +2 -0
- package/build/src/services/settings.js +5 -1
- package/build/src/upgrade/legacy.js +2 -2
- package/build/src/workflows/calendar.d.ts +9 -0
- package/build/src/workflows/calendar.js +44 -0
- package/build/src/workflows/define_workflow.d.ts +48 -3
- package/build/src/workflows/define_workflow.js +57 -13
- package/build/src/workflows/delegations.d.ts +107 -0
- package/build/src/workflows/delegations.js +326 -0
- package/build/src/workflows/engine.d.ts +80 -4
- package/build/src/workflows/engine.js +466 -86
- package/build/src/workflows/sla.d.ts +15 -0
- package/build/src/workflows/sla.js +8 -0
- package/build/stubs/resource_contract.txt +12 -1
- package/package.json +9 -2
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { crc32 } from 'node:zlib';
|
|
2
|
+
import { AdulaError } from '../admin/errors.js';
|
|
3
|
+
/**
|
|
4
|
+
* Profile pictures (ADR 050) are untrusted bytes. The browser crops and re-encodes every
|
|
5
|
+
* picture to at most 512 px, so the server accepts only what that produces: PNG or still
|
|
6
|
+
* WebP within these bounds. Nothing here decodes pixel data; the container is checked and
|
|
7
|
+
* rewritten so that only the chunks a browser needs to draw the picture remain.
|
|
8
|
+
*/
|
|
9
|
+
export const AVATAR_LIMITS = Object.freeze({ maxBytes: 1024 * 1024, minSide: 32, maxSide: 512 });
|
|
10
|
+
const TYPE_MESSAGE = 'الصورة الشخصية يجب أن تكون بصيغة PNG أو WebP';
|
|
11
|
+
const CORRUPT_MESSAGE = 'ملف الصورة تالف أو غير مكتمل';
|
|
12
|
+
const SIZE_MESSAGE = 'حجم الصورة الشخصية يتجاوز ١ ميغابايت';
|
|
13
|
+
const DIMENSIONS_MESSAGE = 'أبعاد الصورة الشخصية يجب أن تكون بين ٣٢ و٥١٢ بكسل';
|
|
14
|
+
const ANIMATED_MESSAGE = 'الصورة الشخصية يجب أن تكون صورة ثابتة';
|
|
15
|
+
const wrongType = () => new AdulaError(422, 'E_AVATAR_TYPE', TYPE_MESSAGE);
|
|
16
|
+
const corrupt = () => new AdulaError(422, 'E_AVATAR_CORRUPT', CORRUPT_MESSAGE);
|
|
17
|
+
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
18
|
+
/** Chunks a still PNG needs; text, EXIF, ICC, time and animation chunks are dropped. */
|
|
19
|
+
const PNG_KEEP = new Set(['IHDR', 'PLTE', 'tRNS', 'IDAT', 'IEND']);
|
|
20
|
+
/** Bit depths allowed for each PNG colour type (PNG specification, table 11.1). */
|
|
21
|
+
const PNG_DEPTHS = {
|
|
22
|
+
0: [1, 2, 4, 8, 16],
|
|
23
|
+
2: [8, 16],
|
|
24
|
+
3: [1, 2, 4, 8],
|
|
25
|
+
4: [8, 16],
|
|
26
|
+
6: [8, 16],
|
|
27
|
+
};
|
|
28
|
+
function chunk(type, data) {
|
|
29
|
+
const head = Buffer.alloc(8);
|
|
30
|
+
head.writeUInt32BE(data.length, 0);
|
|
31
|
+
head.write(type, 4, 'latin1');
|
|
32
|
+
const crc = Buffer.alloc(4);
|
|
33
|
+
crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])) >>> 0, 0);
|
|
34
|
+
return Buffer.concat([head, data, crc]);
|
|
35
|
+
}
|
|
36
|
+
function png(bytes) {
|
|
37
|
+
let offset = PNG_SIGNATURE.length;
|
|
38
|
+
const kept = [PNG_SIGNATURE];
|
|
39
|
+
let width = 0;
|
|
40
|
+
let height = 0;
|
|
41
|
+
let colorType = -1;
|
|
42
|
+
let sawPalette = false;
|
|
43
|
+
let idat = 'none';
|
|
44
|
+
let first = true;
|
|
45
|
+
for (;;) {
|
|
46
|
+
if (offset + 12 > bytes.length)
|
|
47
|
+
throw corrupt();
|
|
48
|
+
const length = bytes.readUInt32BE(offset);
|
|
49
|
+
const type = bytes.toString('latin1', offset + 4, offset + 8);
|
|
50
|
+
if (length > 0x7fffffff || offset + 12 + length > bytes.length)
|
|
51
|
+
throw corrupt();
|
|
52
|
+
if (!/^[A-Za-z]{4}$/.test(type))
|
|
53
|
+
throw corrupt();
|
|
54
|
+
const data = bytes.subarray(offset + 8, offset + 8 + length);
|
|
55
|
+
const stored = bytes.readUInt32BE(offset + 8 + length);
|
|
56
|
+
if (crc32(bytes.subarray(offset + 4, offset + 8 + length)) >>> 0 !== stored)
|
|
57
|
+
throw corrupt();
|
|
58
|
+
offset += 12 + length;
|
|
59
|
+
if (first) {
|
|
60
|
+
if (type !== 'IHDR' || length !== 13)
|
|
61
|
+
throw corrupt();
|
|
62
|
+
first = false;
|
|
63
|
+
width = data.readUInt32BE(0);
|
|
64
|
+
height = data.readUInt32BE(4);
|
|
65
|
+
const depth = data[8];
|
|
66
|
+
colorType = data[9];
|
|
67
|
+
if (!PNG_DEPTHS[colorType]?.includes(depth) ||
|
|
68
|
+
data[10] !== 0 ||
|
|
69
|
+
data[11] !== 0 ||
|
|
70
|
+
(data[12] !== 0 && data[12] !== 1))
|
|
71
|
+
throw corrupt();
|
|
72
|
+
}
|
|
73
|
+
else if (type === 'IHDR')
|
|
74
|
+
throw corrupt();
|
|
75
|
+
// Animated PNG frames: a profile picture is a still image.
|
|
76
|
+
if (type === 'acTL' || type === 'fcTL' || type === 'fdAT')
|
|
77
|
+
throw new AdulaError(422, 'E_AVATAR_TYPE', ANIMATED_MESSAGE);
|
|
78
|
+
// An unknown critical chunk (upper-case first letter) cannot be skipped safely.
|
|
79
|
+
if (!PNG_KEEP.has(type) && type[0] === type[0].toUpperCase())
|
|
80
|
+
throw corrupt();
|
|
81
|
+
if (type === 'PLTE') {
|
|
82
|
+
if (idat !== 'none' || sawPalette || length % 3 !== 0 || length === 0)
|
|
83
|
+
throw corrupt();
|
|
84
|
+
sawPalette = true;
|
|
85
|
+
}
|
|
86
|
+
if (type === 'IDAT') {
|
|
87
|
+
if (idat === 'closed')
|
|
88
|
+
throw corrupt();
|
|
89
|
+
idat = 'open';
|
|
90
|
+
}
|
|
91
|
+
else if (idat === 'open')
|
|
92
|
+
idat = 'closed';
|
|
93
|
+
if (PNG_KEEP.has(type))
|
|
94
|
+
kept.push(chunk(type, Buffer.from(data)));
|
|
95
|
+
// Bytes after IEND (a polyglot tail) are not kept.
|
|
96
|
+
if (type === 'IEND')
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
if (idat === 'none' || (colorType === 3 && !sawPalette))
|
|
100
|
+
throw corrupt();
|
|
101
|
+
return { width, height, bytes: Buffer.concat(kept) };
|
|
102
|
+
}
|
|
103
|
+
/** WebP flags (VP8X): ICC profile, EXIF and XMP metadata are cleared with their chunks. */
|
|
104
|
+
const VP8X_ANIMATION = 0x02;
|
|
105
|
+
const VP8X_METADATA = 0x04 | 0x08 | 0x20;
|
|
106
|
+
function webpFrame(type, data) {
|
|
107
|
+
if (type === 'VP8 ') {
|
|
108
|
+
if (data.length < 10 || data[3] !== 0x9d || data[4] !== 0x01 || data[5] !== 0x2a)
|
|
109
|
+
throw corrupt();
|
|
110
|
+
return { width: data.readUInt16LE(6) & 0x3fff, height: data.readUInt16LE(8) & 0x3fff };
|
|
111
|
+
}
|
|
112
|
+
if (data.length < 5 || data[0] !== 0x2f)
|
|
113
|
+
throw corrupt();
|
|
114
|
+
const bits = data.readUInt32LE(1);
|
|
115
|
+
if (bits >>> 29 !== 0)
|
|
116
|
+
throw corrupt();
|
|
117
|
+
return { width: (bits & 0x3fff) + 1, height: ((bits >>> 14) & 0x3fff) + 1 };
|
|
118
|
+
}
|
|
119
|
+
function webp(bytes) {
|
|
120
|
+
if (bytes.length < 20)
|
|
121
|
+
throw corrupt();
|
|
122
|
+
const riff = bytes.readUInt32LE(4);
|
|
123
|
+
if (riff < 12 || riff % 2 !== 0 || riff + 8 > bytes.length)
|
|
124
|
+
throw corrupt();
|
|
125
|
+
const end = riff + 8;
|
|
126
|
+
let offset = 12;
|
|
127
|
+
const chunks = [];
|
|
128
|
+
while (offset < end) {
|
|
129
|
+
if (offset + 8 > end)
|
|
130
|
+
throw corrupt();
|
|
131
|
+
const type = bytes.toString('latin1', offset, offset + 4);
|
|
132
|
+
const length = bytes.readUInt32LE(offset + 4);
|
|
133
|
+
const padded = length + (length % 2);
|
|
134
|
+
if (offset + 8 + padded > end)
|
|
135
|
+
throw corrupt();
|
|
136
|
+
chunks.push({ type, data: bytes.subarray(offset + 8, offset + 8 + length) });
|
|
137
|
+
offset += 8 + padded;
|
|
138
|
+
}
|
|
139
|
+
if (!chunks.length)
|
|
140
|
+
throw corrupt();
|
|
141
|
+
if (chunks.some((entry) => entry.type === 'ANIM' || entry.type === 'ANMF'))
|
|
142
|
+
throw new AdulaError(422, 'E_AVATAR_TYPE', ANIMATED_MESSAGE);
|
|
143
|
+
const [head] = chunks;
|
|
144
|
+
let out;
|
|
145
|
+
let width;
|
|
146
|
+
let height;
|
|
147
|
+
if (head.type === 'VP8 ' || head.type === 'VP8L') {
|
|
148
|
+
if (chunks.length !== 1)
|
|
149
|
+
throw corrupt();
|
|
150
|
+
({ width, height } = webpFrame(head.type, head.data));
|
|
151
|
+
out = [head];
|
|
152
|
+
}
|
|
153
|
+
else if (head.type === 'VP8X') {
|
|
154
|
+
if (head.data.length !== 10)
|
|
155
|
+
throw corrupt();
|
|
156
|
+
const flags = head.data[0];
|
|
157
|
+
if (flags & VP8X_ANIMATION)
|
|
158
|
+
throw new AdulaError(422, 'E_AVATAR_TYPE', ANIMATED_MESSAGE);
|
|
159
|
+
width = head.data.readUIntLE(4, 3) + 1;
|
|
160
|
+
height = head.data.readUIntLE(7, 3) + 1;
|
|
161
|
+
const rest = chunks.slice(1);
|
|
162
|
+
const frames = rest.filter((entry) => entry.type === 'VP8 ' || entry.type === 'VP8L');
|
|
163
|
+
if (frames.length !== 1)
|
|
164
|
+
throw corrupt();
|
|
165
|
+
const frame = webpFrame(frames[0].type, frames[0].data);
|
|
166
|
+
if (frame.width !== width || frame.height !== height)
|
|
167
|
+
throw corrupt();
|
|
168
|
+
const alpha = rest.filter((entry) => entry.type === 'ALPH');
|
|
169
|
+
if (alpha.length > 1 || (alpha.length && frames[0].type !== 'VP8 '))
|
|
170
|
+
throw corrupt();
|
|
171
|
+
const header = Buffer.from(head.data);
|
|
172
|
+
header[0] = flags & ~VP8X_METADATA;
|
|
173
|
+
// EXIF, XMP, ICCP and unknown chunks are dropped; ALPH must precede the frame.
|
|
174
|
+
out = [{ type: 'VP8X', data: header }, ...alpha, frames[0]];
|
|
175
|
+
}
|
|
176
|
+
else
|
|
177
|
+
throw corrupt();
|
|
178
|
+
const parts = [];
|
|
179
|
+
for (const entry of out) {
|
|
180
|
+
const head8 = Buffer.alloc(8);
|
|
181
|
+
head8.write(entry.type, 0, 'latin1');
|
|
182
|
+
head8.writeUInt32LE(entry.data.length, 4);
|
|
183
|
+
parts.push(head8, entry.data);
|
|
184
|
+
if (entry.data.length % 2)
|
|
185
|
+
parts.push(Buffer.alloc(1));
|
|
186
|
+
}
|
|
187
|
+
const body = Buffer.concat(parts);
|
|
188
|
+
const header = Buffer.alloc(12);
|
|
189
|
+
header.write('RIFF', 0, 'latin1');
|
|
190
|
+
header.writeUInt32LE(body.length + 4, 4);
|
|
191
|
+
header.write('WEBP', 8, 'latin1');
|
|
192
|
+
return { width, height, bytes: Buffer.concat([header, body]) };
|
|
193
|
+
}
|
|
194
|
+
function detect(bytes) {
|
|
195
|
+
if (bytes.length >= 8 && bytes.subarray(0, 8).equals(PNG_SIGNATURE))
|
|
196
|
+
return 'png';
|
|
197
|
+
if (bytes.length >= 12 &&
|
|
198
|
+
bytes.toString('latin1', 0, 4) === 'RIFF' &&
|
|
199
|
+
bytes.toString('latin1', 8, 12) === 'WEBP')
|
|
200
|
+
return 'webp';
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
function parse(type, bytes) {
|
|
204
|
+
try {
|
|
205
|
+
return type === 'png' ? png(bytes) : webp(bytes);
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
if (error instanceof AdulaError)
|
|
209
|
+
throw error;
|
|
210
|
+
// A read past a declared length means the file is truncated or forged.
|
|
211
|
+
throw corrupt();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Checks an uploaded profile picture by its first bytes (never its name or declared type),
|
|
216
|
+
* bounds its size and dimensions from the header, and returns a copy without metadata or
|
|
217
|
+
* trailing bytes. Anything else, including SVG, GIF, JPEG, HTML and animations, is refused.
|
|
218
|
+
*/
|
|
219
|
+
export function sanitizeAvatarImage(input) {
|
|
220
|
+
const bytes = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
|
|
221
|
+
if (bytes.length > AVATAR_LIMITS.maxBytes)
|
|
222
|
+
throw new AdulaError(422, 'E_AVATAR_SIZE', SIZE_MESSAGE);
|
|
223
|
+
const type = detect(bytes);
|
|
224
|
+
if (!type)
|
|
225
|
+
throw wrongType();
|
|
226
|
+
const parsed = parse(type, bytes);
|
|
227
|
+
const inBounds = (side) => side >= AVATAR_LIMITS.minSide && side <= AVATAR_LIMITS.maxSide;
|
|
228
|
+
if (!inBounds(parsed.width) || !inBounds(parsed.height))
|
|
229
|
+
throw new AdulaError(422, 'E_AVATAR_DIMENSIONS', DIMENSIONS_MESSAGE);
|
|
230
|
+
// The rewritten file must read back as the same picture.
|
|
231
|
+
const again = parse(type, parsed.bytes);
|
|
232
|
+
if (again.width !== parsed.width || again.height !== parsed.height)
|
|
233
|
+
throw corrupt();
|
|
234
|
+
return {
|
|
235
|
+
type,
|
|
236
|
+
mimeType: type === 'png' ? 'image/png' : 'image/webp',
|
|
237
|
+
extname: type,
|
|
238
|
+
width: parsed.width,
|
|
239
|
+
height: parsed.height,
|
|
240
|
+
bytes: again.bytes,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Prepares an uploaded profile picture for storage (ADR 050). `sanitizeAvatarImage` first
|
|
245
|
+
* checks the bytes without decoding them (type, size, header, dimensions, animation), so
|
|
246
|
+
* nothing unexpected reaches the decoder. libvips then decodes the single still frame
|
|
247
|
+
* within a pixel budget and re-encodes it, which drops every metadata chunk and any payload
|
|
248
|
+
* hidden inside the compressed pixel data. The result is checked again before it is stored.
|
|
249
|
+
*/
|
|
250
|
+
export async function prepareAvatarImage(input) {
|
|
251
|
+
const checked = sanitizeAvatarImage(input);
|
|
252
|
+
const { default: sharp } = await import('sharp');
|
|
253
|
+
const options = {
|
|
254
|
+
limitInputPixels: AVATAR_LIMITS.maxSide * AVATAR_LIMITS.maxSide,
|
|
255
|
+
animated: false,
|
|
256
|
+
failOn: 'error',
|
|
257
|
+
};
|
|
258
|
+
let encoded;
|
|
259
|
+
try {
|
|
260
|
+
const image = sharp(checked.bytes, options);
|
|
261
|
+
const metadata = await image.metadata();
|
|
262
|
+
if ((metadata.pages ?? 1) !== 1)
|
|
263
|
+
throw new AdulaError(422, 'E_AVATAR_TYPE', ANIMATED_MESSAGE);
|
|
264
|
+
// Metadata is kept only when asked for (withMetadata), so the output carries none.
|
|
265
|
+
encoded =
|
|
266
|
+
checked.type === 'png'
|
|
267
|
+
? await image.png({ compressionLevel: 9, adaptiveFiltering: true }).toBuffer()
|
|
268
|
+
: await image.webp({ quality: 90, effort: 4 }).toBuffer();
|
|
269
|
+
if (encoded.length > AVATAR_LIMITS.maxBytes && checked.type === 'png')
|
|
270
|
+
encoded = await sharp(checked.bytes, options).webp({ quality: 85, effort: 4 }).toBuffer();
|
|
271
|
+
}
|
|
272
|
+
catch (error) {
|
|
273
|
+
if (error instanceof AdulaError)
|
|
274
|
+
throw error;
|
|
275
|
+
throw corrupt();
|
|
276
|
+
}
|
|
277
|
+
return sanitizeAvatarImage(encoded);
|
|
278
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-house allowlist sanitizer for administrator-written print markup (ADR 048). It is
|
|
3
|
+
* not a general HTML parser: it keeps a fixed set of structural tags and four attributes
|
|
4
|
+
* and drops everything else, so the output can only lay out escaped text.
|
|
5
|
+
*
|
|
6
|
+
* - Allowed tags: ALLOWED_TAGS. Any other tag is removed; the contents of DROPPED_WITH_CONTENT
|
|
7
|
+
* (script, style, iframe, svg, form, …) are removed with it.
|
|
8
|
+
* - Allowed attributes: class (word characters and dashes), colspan/rowspan (1–99),
|
|
9
|
+
* dir (rtl/ltr/auto) and style restricted by sanitizeStyle. Every other attribute,
|
|
10
|
+
* including on* handlers, href and src, is removed.
|
|
11
|
+
* - An attribute value with `&`, `\`, `{{` or `}}` is removed: entities and CSS escapes
|
|
12
|
+
* could hide a URL, and template values may not flow into attributes.
|
|
13
|
+
* - Comments, doctype and processing instructions are removed; a stray `<` becomes `<`.
|
|
14
|
+
* - Closing tags are balanced against the open allowed tags; unclosed ones are closed.
|
|
15
|
+
*/
|
|
16
|
+
export declare const ALLOWED_TAGS: Set<string>;
|
|
17
|
+
/** A style attribute reduced to allowlisted properties with plain values; '' when none survive. */
|
|
18
|
+
export declare function sanitizeStyle(style: string): string;
|
|
19
|
+
/** Sanitizes administrator print markup; see the module comment for the rules. */
|
|
20
|
+
export declare function sanitizePrintHtml(input: string): string;
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-house allowlist sanitizer for administrator-written print markup (ADR 048). It is
|
|
3
|
+
* not a general HTML parser: it keeps a fixed set of structural tags and four attributes
|
|
4
|
+
* and drops everything else, so the output can only lay out escaped text.
|
|
5
|
+
*
|
|
6
|
+
* - Allowed tags: ALLOWED_TAGS. Any other tag is removed; the contents of DROPPED_WITH_CONTENT
|
|
7
|
+
* (script, style, iframe, svg, form, …) are removed with it.
|
|
8
|
+
* - Allowed attributes: class (word characters and dashes), colspan/rowspan (1–99),
|
|
9
|
+
* dir (rtl/ltr/auto) and style restricted by sanitizeStyle. Every other attribute,
|
|
10
|
+
* including on* handlers, href and src, is removed.
|
|
11
|
+
* - An attribute value with `&`, `\`, `{{` or `}}` is removed: entities and CSS escapes
|
|
12
|
+
* could hide a URL, and template values may not flow into attributes.
|
|
13
|
+
* - Comments, doctype and processing instructions are removed; a stray `<` becomes `<`.
|
|
14
|
+
* - Closing tags are balanced against the open allowed tags; unclosed ones are closed.
|
|
15
|
+
*/
|
|
16
|
+
export const ALLOWED_TAGS = new Set([
|
|
17
|
+
'div',
|
|
18
|
+
'section',
|
|
19
|
+
'header',
|
|
20
|
+
'footer',
|
|
21
|
+
'p',
|
|
22
|
+
'span',
|
|
23
|
+
'strong',
|
|
24
|
+
'em',
|
|
25
|
+
'b',
|
|
26
|
+
'i',
|
|
27
|
+
'u',
|
|
28
|
+
'br',
|
|
29
|
+
'hr',
|
|
30
|
+
'h1',
|
|
31
|
+
'h2',
|
|
32
|
+
'h3',
|
|
33
|
+
'h4',
|
|
34
|
+
'table',
|
|
35
|
+
'thead',
|
|
36
|
+
'tbody',
|
|
37
|
+
'tfoot',
|
|
38
|
+
'tr',
|
|
39
|
+
'th',
|
|
40
|
+
'td',
|
|
41
|
+
'ul',
|
|
42
|
+
'ol',
|
|
43
|
+
'li',
|
|
44
|
+
'small',
|
|
45
|
+
]);
|
|
46
|
+
const VOID = new Set(['br', 'hr']);
|
|
47
|
+
const DROPPED_WITH_CONTENT = new Set([
|
|
48
|
+
'script',
|
|
49
|
+
'style',
|
|
50
|
+
'iframe',
|
|
51
|
+
'frame',
|
|
52
|
+
'frameset',
|
|
53
|
+
'object',
|
|
54
|
+
'embed',
|
|
55
|
+
'applet',
|
|
56
|
+
'svg',
|
|
57
|
+
'math',
|
|
58
|
+
'template',
|
|
59
|
+
'noscript',
|
|
60
|
+
'noembed',
|
|
61
|
+
'noframes',
|
|
62
|
+
'textarea',
|
|
63
|
+
'title',
|
|
64
|
+
'select',
|
|
65
|
+
'form',
|
|
66
|
+
'button',
|
|
67
|
+
'xmp',
|
|
68
|
+
'plaintext',
|
|
69
|
+
'video',
|
|
70
|
+
'audio',
|
|
71
|
+
'canvas',
|
|
72
|
+
'picture',
|
|
73
|
+
'head',
|
|
74
|
+
]);
|
|
75
|
+
/** CSS properties a style attribute may set. Values are checked by CSS_VALUE. */
|
|
76
|
+
const CSS_PROPERTIES = new Set([
|
|
77
|
+
'color',
|
|
78
|
+
'background-color',
|
|
79
|
+
'font-size',
|
|
80
|
+
'font-weight',
|
|
81
|
+
'font-style',
|
|
82
|
+
'text-align',
|
|
83
|
+
'text-decoration',
|
|
84
|
+
'vertical-align',
|
|
85
|
+
'line-height',
|
|
86
|
+
'letter-spacing',
|
|
87
|
+
'white-space',
|
|
88
|
+
'direction',
|
|
89
|
+
'width',
|
|
90
|
+
'min-width',
|
|
91
|
+
'max-width',
|
|
92
|
+
'height',
|
|
93
|
+
'min-height',
|
|
94
|
+
'border',
|
|
95
|
+
'border-top',
|
|
96
|
+
'border-bottom',
|
|
97
|
+
'border-left',
|
|
98
|
+
'border-right',
|
|
99
|
+
'border-color',
|
|
100
|
+
'border-style',
|
|
101
|
+
'border-width',
|
|
102
|
+
'border-collapse',
|
|
103
|
+
'border-radius',
|
|
104
|
+
'margin',
|
|
105
|
+
'margin-top',
|
|
106
|
+
'margin-bottom',
|
|
107
|
+
'margin-left',
|
|
108
|
+
'margin-right',
|
|
109
|
+
'margin-inline-start',
|
|
110
|
+
'margin-inline-end',
|
|
111
|
+
'padding',
|
|
112
|
+
'padding-top',
|
|
113
|
+
'padding-bottom',
|
|
114
|
+
'padding-left',
|
|
115
|
+
'padding-right',
|
|
116
|
+
'padding-inline-start',
|
|
117
|
+
'padding-inline-end',
|
|
118
|
+
'page-break-before',
|
|
119
|
+
'page-break-after',
|
|
120
|
+
'page-break-inside',
|
|
121
|
+
'break-before',
|
|
122
|
+
'break-after',
|
|
123
|
+
'break-inside',
|
|
124
|
+
]);
|
|
125
|
+
/** Plain lengths, keywords, hex colours and rgb()/hsl() colours; no other function. */
|
|
126
|
+
const CSS_VALUE = /^[#%.,\w\s-]*$/;
|
|
127
|
+
const CSS_COLOR_FUNCTION = /\b(?:rgba?|hsla?)\(\s*[\d.%,\s]+\)/gi;
|
|
128
|
+
const CSS_FORBIDDEN = /url|expression|import|javascript|behavio|binding|image|attr|var|@|\/\/|:/i;
|
|
129
|
+
/** Longest style attribute kept; longer ones are dropped whole. */
|
|
130
|
+
const STYLE_LIMIT = 500;
|
|
131
|
+
/** A style attribute reduced to allowlisted properties with plain values; '' when none survive. */
|
|
132
|
+
export function sanitizeStyle(style) {
|
|
133
|
+
if (style.length > STYLE_LIMIT)
|
|
134
|
+
return '';
|
|
135
|
+
if (/[\\&<>"'`{}]|\/\*/.test(style))
|
|
136
|
+
return '';
|
|
137
|
+
const declarations = [];
|
|
138
|
+
for (const part of style.split(';')) {
|
|
139
|
+
const colon = part.indexOf(':');
|
|
140
|
+
if (colon === -1)
|
|
141
|
+
continue;
|
|
142
|
+
const property = part.slice(0, colon).trim().toLowerCase();
|
|
143
|
+
const value = part
|
|
144
|
+
.slice(colon + 1)
|
|
145
|
+
.trim()
|
|
146
|
+
.replace(/\s+/g, ' ');
|
|
147
|
+
if (!CSS_PROPERTIES.has(property) || !value || value.length > 200)
|
|
148
|
+
continue;
|
|
149
|
+
if (CSS_FORBIDDEN.test(value))
|
|
150
|
+
continue;
|
|
151
|
+
if (!CSS_VALUE.test(value.replace(CSS_COLOR_FUNCTION, 'color')))
|
|
152
|
+
continue;
|
|
153
|
+
declarations.push(`${property}: ${value}`);
|
|
154
|
+
}
|
|
155
|
+
return declarations.join('; ');
|
|
156
|
+
}
|
|
157
|
+
function attributeValue(name, value) {
|
|
158
|
+
if (/[&\\]|\{\{|\}\}/.test(value))
|
|
159
|
+
return null;
|
|
160
|
+
switch (name) {
|
|
161
|
+
case 'class':
|
|
162
|
+
return /^[\w\s-]{0,200}$/.test(value) ? value.trim().replace(/\s+/g, ' ') : null;
|
|
163
|
+
case 'colspan':
|
|
164
|
+
case 'rowspan':
|
|
165
|
+
return /^[1-9]\d?$/.test(value.trim()) ? value.trim() : null;
|
|
166
|
+
case 'dir':
|
|
167
|
+
return ['rtl', 'ltr', 'auto'].includes(value.trim().toLowerCase())
|
|
168
|
+
? value.trim().toLowerCase()
|
|
169
|
+
: null;
|
|
170
|
+
case 'style': {
|
|
171
|
+
const style = sanitizeStyle(value);
|
|
172
|
+
return style || null;
|
|
173
|
+
}
|
|
174
|
+
default:
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
const ATTRIBUTE = /([^\s"'>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
179
|
+
function attributes(source) {
|
|
180
|
+
const kept = [];
|
|
181
|
+
const seen = new Set();
|
|
182
|
+
for (const match of source.matchAll(ATTRIBUTE)) {
|
|
183
|
+
const name = match[1].toLowerCase();
|
|
184
|
+
if (seen.has(name))
|
|
185
|
+
continue;
|
|
186
|
+
seen.add(name);
|
|
187
|
+
const value = attributeValue(name, match[2] ?? match[3] ?? match[4] ?? '');
|
|
188
|
+
if (value !== null)
|
|
189
|
+
kept.push(` ${name}="${value.replaceAll('"', '"')}"`);
|
|
190
|
+
}
|
|
191
|
+
return kept.join('');
|
|
192
|
+
}
|
|
193
|
+
/** Text between tags: kept as written, except that a `>` is escaped. */
|
|
194
|
+
const text = (value) => value.replaceAll('>', '>');
|
|
195
|
+
/**
|
|
196
|
+
* A tag from `<` to its `>`. Attribute text cannot contain `<` or `>`, so a scan never runs
|
|
197
|
+
* past the next angle bracket and the sanitizer stays linear in the input length.
|
|
198
|
+
*/
|
|
199
|
+
const TAG = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)((?:[^<>"']|"[^"<>]*"|'[^'<>]*')*)>/y;
|
|
200
|
+
/** Deeper nesting is flattened: tags beyond it are dropped, their text kept. */
|
|
201
|
+
const MAX_DEPTH = 100;
|
|
202
|
+
/** Sanitizes administrator print markup; see the module comment for the rules. */
|
|
203
|
+
export function sanitizePrintHtml(input) {
|
|
204
|
+
let output = '';
|
|
205
|
+
const open = [];
|
|
206
|
+
let index = 0;
|
|
207
|
+
while (index < input.length) {
|
|
208
|
+
const lt = input.indexOf('<', index);
|
|
209
|
+
if (lt === -1) {
|
|
210
|
+
output += text(input.slice(index));
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
output += text(input.slice(index, lt));
|
|
214
|
+
if (input.startsWith('<!--', lt)) {
|
|
215
|
+
const end = input.indexOf('-->', lt + 4);
|
|
216
|
+
index = end === -1 ? input.length : end + 3;
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
if (input[lt + 1] === '!' || input[lt + 1] === '?') {
|
|
220
|
+
const end = input.indexOf('>', lt);
|
|
221
|
+
index = end === -1 ? input.length : end + 1;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
TAG.lastIndex = lt;
|
|
225
|
+
const tag = TAG.exec(input);
|
|
226
|
+
if (!tag) {
|
|
227
|
+
output += '<';
|
|
228
|
+
index = lt + 1;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
index = lt + tag[0].length;
|
|
232
|
+
const closing = tag[1] === '/';
|
|
233
|
+
const name = tag[2].toLowerCase();
|
|
234
|
+
if (!closing && DROPPED_WITH_CONTENT.has(name)) {
|
|
235
|
+
// Skip to the matching close tag (or the end): its contents never reach the page.
|
|
236
|
+
const close = new RegExp(`</${name}\\s*>`, 'gi');
|
|
237
|
+
close.lastIndex = index;
|
|
238
|
+
const end = close.exec(input);
|
|
239
|
+
index = end ? end.index + end[0].length : input.length;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (!ALLOWED_TAGS.has(name))
|
|
243
|
+
continue;
|
|
244
|
+
if (closing) {
|
|
245
|
+
const position = open.lastIndexOf(name);
|
|
246
|
+
if (position === -1)
|
|
247
|
+
continue;
|
|
248
|
+
while (open.length > position)
|
|
249
|
+
output += `</${open.pop()}>`;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (!VOID.has(name) && open.length >= MAX_DEPTH)
|
|
253
|
+
continue;
|
|
254
|
+
output += `<${name}${attributes(tag[3])}>`;
|
|
255
|
+
if (!VOID.has(name))
|
|
256
|
+
open.push(name);
|
|
257
|
+
}
|
|
258
|
+
while (open.length)
|
|
259
|
+
output += `</${open.pop()}>`;
|
|
260
|
+
return output;
|
|
261
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Knex } from 'knex';
|
|
2
|
+
import type { ResourceRegistry } from '../resource/registry.js';
|
|
3
|
+
/** A resource field that stores values of a lookup group. */
|
|
4
|
+
export type LookupFieldUse = {
|
|
5
|
+
resource: string;
|
|
6
|
+
field: string;
|
|
7
|
+
label: string;
|
|
8
|
+
};
|
|
9
|
+
export type LookupGroupSummary = {
|
|
10
|
+
group: string;
|
|
11
|
+
total: number;
|
|
12
|
+
active: number;
|
|
13
|
+
/** Registered resource fields that store this group's keys. */
|
|
14
|
+
fields: LookupFieldUse[];
|
|
15
|
+
/** Declared by a registered module, which seeds it on installation. */
|
|
16
|
+
declared: boolean;
|
|
17
|
+
};
|
|
18
|
+
export type LookupValue = {
|
|
19
|
+
id: number;
|
|
20
|
+
group: string;
|
|
21
|
+
key: string;
|
|
22
|
+
labelAr: string;
|
|
23
|
+
labelEn: string;
|
|
24
|
+
sort: number;
|
|
25
|
+
active: boolean;
|
|
26
|
+
/** Stored records (including deleted ones) holding this key; null without a registry. */
|
|
27
|
+
usage: number | null;
|
|
28
|
+
/** Declared by a registered module; such values are never deleted. */
|
|
29
|
+
declared: boolean;
|
|
30
|
+
};
|
|
31
|
+
export type LookupInput = {
|
|
32
|
+
group: unknown;
|
|
33
|
+
key: unknown;
|
|
34
|
+
labelAr: unknown;
|
|
35
|
+
labelEn?: unknown;
|
|
36
|
+
sort?: unknown;
|
|
37
|
+
};
|
|
38
|
+
export type LookupUpdate = {
|
|
39
|
+
labelAr?: unknown;
|
|
40
|
+
labelEn?: unknown;
|
|
41
|
+
sort?: unknown;
|
|
42
|
+
key?: unknown;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Administration of lookup values (lookups table). Values are never renamed or removed while
|
|
46
|
+
* records hold them: the key is immutable, a value stored by any record cannot be disabled
|
|
47
|
+
* (the record validator requires an active value on every save, so a disabled stored value
|
|
48
|
+
* would block editing those records), and only an unused value that no registered module
|
|
49
|
+
* declares may be deleted (a declared value would be seeded again on the next installation).
|
|
50
|
+
* Usage is counted from the registered resources' lookup fields; without a registry it is
|
|
51
|
+
* unknown and disabling or deleting is refused.
|
|
52
|
+
*/
|
|
53
|
+
export declare class LookupsAdmin {
|
|
54
|
+
private db;
|
|
55
|
+
private registry?;
|
|
56
|
+
constructor(db: Knex, registry?: ResourceRegistry | undefined);
|
|
57
|
+
/** Lookup fields of the registered resources, by group. */
|
|
58
|
+
fields(name?: string): (LookupFieldUse & {
|
|
59
|
+
table: string;
|
|
60
|
+
column: string;
|
|
61
|
+
group: string;
|
|
62
|
+
})[];
|
|
63
|
+
private declared;
|
|
64
|
+
groups(): Promise<LookupGroupSummary[]>;
|
|
65
|
+
values(name: unknown): Promise<LookupValue[]>;
|
|
66
|
+
create(actorId: number, input: LookupInput): Promise<LookupValue>;
|
|
67
|
+
update(actorId: number, id: unknown, input: LookupUpdate): Promise<LookupValue>;
|
|
68
|
+
/** Sets the order of a whole group: `ids` lists every value of the group once, first to last. */
|
|
69
|
+
reorder(actorId: number, name: unknown, ids: unknown): Promise<LookupValue[]>;
|
|
70
|
+
disable(actorId: number, id: unknown): Promise<LookupValue>;
|
|
71
|
+
enable(actorId: number, id: unknown): Promise<LookupValue>;
|
|
72
|
+
delete(actorId: number, id: unknown): Promise<void>;
|
|
73
|
+
private setActive;
|
|
74
|
+
private refuseInUse;
|
|
75
|
+
private lock;
|
|
76
|
+
private withUsage;
|
|
77
|
+
/** Records holding each key, over every registered field of the group; null without a registry. */
|
|
78
|
+
private usageByKey;
|
|
79
|
+
private toValue;
|
|
80
|
+
}
|