@oneuptime/common 9.2.17 → 9.2.20

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.
Files changed (40) hide show
  1. package/Server/API/AlertAPI.ts +139 -0
  2. package/Server/API/IncidentAPI.ts +132 -0
  3. package/Server/API/ScheduledMaintenanceAPI.ts +164 -0
  4. package/Server/Services/AIService.ts +0 -1
  5. package/Server/Services/IncidentService.ts +0 -1
  6. package/Server/Utils/AI/AlertAIContextBuilder.ts +264 -0
  7. package/Server/Utils/AI/IncidentAIContextBuilder.ts +212 -0
  8. package/Server/Utils/AI/ScheduledMaintenanceAIContextBuilder.ts +345 -0
  9. package/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.ts +78 -1
  10. package/Server/Utils/Workspace/Slack/Slack.ts +80 -1
  11. package/Tests/Types/Domain.test.ts +24 -3
  12. package/Types/Domain.ts +21 -24
  13. package/UI/Components/AI/GenerateFromAIModal.tsx +157 -20
  14. package/build/dist/Server/API/AlertAPI.js +94 -0
  15. package/build/dist/Server/API/AlertAPI.js.map +1 -0
  16. package/build/dist/Server/API/IncidentAPI.js +88 -1
  17. package/build/dist/Server/API/IncidentAPI.js.map +1 -1
  18. package/build/dist/Server/API/ScheduledMaintenanceAPI.js +103 -0
  19. package/build/dist/Server/API/ScheduledMaintenanceAPI.js.map +1 -0
  20. package/build/dist/Server/Services/AIService.js +0 -1
  21. package/build/dist/Server/Services/AIService.js.map +1 -1
  22. package/build/dist/Server/Services/IncidentService.js +0 -1
  23. package/build/dist/Server/Services/IncidentService.js.map +1 -1
  24. package/build/dist/Server/Utils/AI/AlertAIContextBuilder.js +238 -0
  25. package/build/dist/Server/Utils/AI/AlertAIContextBuilder.js.map +1 -0
  26. package/build/dist/Server/Utils/AI/IncidentAIContextBuilder.js +189 -0
  27. package/build/dist/Server/Utils/AI/IncidentAIContextBuilder.js.map +1 -1
  28. package/build/dist/Server/Utils/AI/ScheduledMaintenanceAIContextBuilder.js +311 -0
  29. package/build/dist/Server/Utils/AI/ScheduledMaintenanceAIContextBuilder.js.map +1 -0
  30. package/build/dist/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.js +59 -2
  31. package/build/dist/Server/Utils/Workspace/MicrosoftTeams/MicrosoftTeams.js.map +1 -1
  32. package/build/dist/Server/Utils/Workspace/Slack/Slack.js +61 -1
  33. package/build/dist/Server/Utils/Workspace/Slack/Slack.js.map +1 -1
  34. package/build/dist/Tests/Types/Domain.test.js +19 -3
  35. package/build/dist/Tests/Types/Domain.test.js.map +1 -1
  36. package/build/dist/Types/Domain.js +18 -16
  37. package/build/dist/Types/Domain.js.map +1 -1
  38. package/build/dist/UI/Components/AI/GenerateFromAIModal.js +116 -3
  39. package/build/dist/UI/Components/AI/GenerateFromAIModal.js.map +1 -1
  40. package/package.json +1 -1
@@ -0,0 +1,345 @@
1
+ import ObjectID from "../../../Types/ObjectID";
2
+ import ScheduledMaintenance from "../../../Models/DatabaseModels/ScheduledMaintenance";
3
+ import ScheduledMaintenanceStateTimeline from "../../../Models/DatabaseModels/ScheduledMaintenanceStateTimeline";
4
+ import ScheduledMaintenanceInternalNote from "../../../Models/DatabaseModels/ScheduledMaintenanceInternalNote";
5
+ import ScheduledMaintenancePublicNote from "../../../Models/DatabaseModels/ScheduledMaintenancePublicNote";
6
+ import ScheduledMaintenanceService from "../../Services/ScheduledMaintenanceService";
7
+ import ScheduledMaintenanceStateTimelineService from "../../Services/ScheduledMaintenanceStateTimelineService";
8
+ import ScheduledMaintenanceInternalNoteService from "../../Services/ScheduledMaintenanceInternalNoteService";
9
+ import ScheduledMaintenancePublicNoteService from "../../Services/ScheduledMaintenancePublicNoteService";
10
+ import CaptureSpan from "../Telemetry/CaptureSpan";
11
+ import OneUptimeDate from "../../../Types/Date";
12
+ import SortOrder from "../../../Types/BaseDatabase/SortOrder";
13
+ import { LLMMessage } from "../LLM/LLMService";
14
+
15
+ export interface ScheduledMaintenanceContextData {
16
+ scheduledMaintenance: ScheduledMaintenance;
17
+ stateTimeline: Array<ScheduledMaintenanceStateTimeline>;
18
+ internalNotes: Array<ScheduledMaintenanceInternalNote>;
19
+ publicNotes: Array<ScheduledMaintenancePublicNote>;
20
+ }
21
+
22
+ export interface AIGenerationContext {
23
+ contextText: string;
24
+ systemPrompt: string;
25
+ messages: Array<LLMMessage>;
26
+ }
27
+
28
+ export default class ScheduledMaintenanceAIContextBuilder {
29
+ @CaptureSpan()
30
+ public static async buildScheduledMaintenanceContext(data: {
31
+ scheduledMaintenanceId: ObjectID;
32
+ }): Promise<ScheduledMaintenanceContextData> {
33
+ const scheduledMaintenance: ScheduledMaintenance | null =
34
+ await ScheduledMaintenanceService.findOneById({
35
+ id: data.scheduledMaintenanceId,
36
+ select: {
37
+ _id: true,
38
+ title: true,
39
+ description: true,
40
+ createdAt: true,
41
+ startsAt: true,
42
+ endsAt: true,
43
+ customFields: true,
44
+ projectId: true,
45
+ currentScheduledMaintenanceState: {
46
+ name: true,
47
+ color: true,
48
+ },
49
+ monitors: {
50
+ name: true,
51
+ },
52
+ labels: {
53
+ name: true,
54
+ color: true,
55
+ },
56
+ },
57
+ props: {
58
+ isRoot: true,
59
+ },
60
+ });
61
+
62
+ if (!scheduledMaintenance) {
63
+ throw new Error("Scheduled Maintenance not found");
64
+ }
65
+
66
+ // Fetch state timeline
67
+ const stateTimeline: Array<ScheduledMaintenanceStateTimeline> =
68
+ await ScheduledMaintenanceStateTimelineService.findBy({
69
+ query: {
70
+ scheduledMaintenanceId: data.scheduledMaintenanceId,
71
+ },
72
+ select: {
73
+ _id: true,
74
+ createdAt: true,
75
+ startsAt: true,
76
+ endsAt: true,
77
+ scheduledMaintenanceState: {
78
+ name: true,
79
+ color: true,
80
+ },
81
+ createdByUser: {
82
+ name: true,
83
+ email: true,
84
+ },
85
+ },
86
+ sort: {
87
+ startsAt: SortOrder.Ascending,
88
+ },
89
+ limit: 100,
90
+ skip: 0,
91
+ props: {
92
+ isRoot: true,
93
+ },
94
+ });
95
+
96
+ // Fetch internal notes
97
+ const internalNotes: Array<ScheduledMaintenanceInternalNote> =
98
+ await ScheduledMaintenanceInternalNoteService.findBy({
99
+ query: {
100
+ scheduledMaintenanceId: data.scheduledMaintenanceId,
101
+ },
102
+ select: {
103
+ _id: true,
104
+ note: true,
105
+ createdAt: true,
106
+ createdByUser: {
107
+ name: true,
108
+ email: true,
109
+ },
110
+ },
111
+ sort: {
112
+ createdAt: SortOrder.Ascending,
113
+ },
114
+ limit: 100,
115
+ skip: 0,
116
+ props: {
117
+ isRoot: true,
118
+ },
119
+ });
120
+
121
+ // Fetch public notes
122
+ const publicNotes: Array<ScheduledMaintenancePublicNote> =
123
+ await ScheduledMaintenancePublicNoteService.findBy({
124
+ query: {
125
+ scheduledMaintenanceId: data.scheduledMaintenanceId,
126
+ },
127
+ select: {
128
+ _id: true,
129
+ note: true,
130
+ createdAt: true,
131
+ postedAt: true,
132
+ createdByUser: {
133
+ name: true,
134
+ email: true,
135
+ },
136
+ },
137
+ sort: {
138
+ createdAt: SortOrder.Ascending,
139
+ },
140
+ limit: 100,
141
+ skip: 0,
142
+ props: {
143
+ isRoot: true,
144
+ },
145
+ });
146
+
147
+ return {
148
+ scheduledMaintenance,
149
+ stateTimeline,
150
+ internalNotes,
151
+ publicNotes,
152
+ };
153
+ }
154
+
155
+ @CaptureSpan()
156
+ public static formatScheduledMaintenanceContextForNote(
157
+ contextData: ScheduledMaintenanceContextData,
158
+ noteType: "public" | "internal",
159
+ template?: string,
160
+ ): AIGenerationContext {
161
+ const { scheduledMaintenance, stateTimeline, internalNotes, publicNotes } =
162
+ contextData;
163
+
164
+ let contextText: string = "";
165
+
166
+ // Basic scheduled maintenance information
167
+ contextText += "# Scheduled Maintenance Information\n\n";
168
+ contextText += `**Title:** ${scheduledMaintenance.title || "N/A"}\n\n`;
169
+ contextText += `**Description:** ${scheduledMaintenance.description || "N/A"}\n\n`;
170
+ contextText += `**Current State:** ${scheduledMaintenance.currentScheduledMaintenanceState?.name || "N/A"}\n\n`;
171
+ contextText += `**Scheduled Start:** ${scheduledMaintenance.startsAt ? OneUptimeDate.getDateAsFormattedString(scheduledMaintenance.startsAt) : "N/A"}\n\n`;
172
+ contextText += `**Scheduled End:** ${scheduledMaintenance.endsAt ? OneUptimeDate.getDateAsFormattedString(scheduledMaintenance.endsAt) : "N/A"}\n\n`;
173
+ contextText += `**Created At:** ${scheduledMaintenance.createdAt ? OneUptimeDate.getDateAsFormattedString(scheduledMaintenance.createdAt) : "N/A"}\n\n`;
174
+
175
+ // Affected monitors
176
+ if (
177
+ scheduledMaintenance.monitors &&
178
+ scheduledMaintenance.monitors.length > 0
179
+ ) {
180
+ contextText += "**Affected Monitors:** ";
181
+ contextText += scheduledMaintenance.monitors
182
+ .map((m: { name?: string }) => {
183
+ return m.name;
184
+ })
185
+ .join(", ");
186
+ contextText += "\n\n";
187
+ }
188
+
189
+ // Labels
190
+ if (scheduledMaintenance.labels && scheduledMaintenance.labels.length > 0) {
191
+ contextText += "**Labels:** ";
192
+ contextText += scheduledMaintenance.labels
193
+ .map((l: { name?: string }) => {
194
+ return l.name;
195
+ })
196
+ .join(", ");
197
+ contextText += "\n\n";
198
+ }
199
+
200
+ // State timeline
201
+ if (stateTimeline.length > 0) {
202
+ contextText += "# State Timeline\n\n";
203
+ for (const timeline of stateTimeline) {
204
+ const startTime: string = timeline.startsAt
205
+ ? OneUptimeDate.getDateAsFormattedString(timeline.startsAt)
206
+ : "N/A";
207
+ const stateName: string =
208
+ timeline.scheduledMaintenanceState?.name?.toString() || "Unknown";
209
+ const createdBy: string =
210
+ timeline.createdByUser?.name?.toString() ||
211
+ timeline.createdByUser?.email?.toString() ||
212
+ "System";
213
+
214
+ contextText += `- **${startTime}**: State changed to **${stateName}** by ${createdBy}\n`;
215
+ }
216
+ contextText += "\n";
217
+ }
218
+
219
+ // Include internal notes for context (for both note types)
220
+ if (internalNotes.length > 0) {
221
+ contextText += "# Internal Notes (Private)\n\n";
222
+ for (const note of internalNotes) {
223
+ const noteTime: string = note.createdAt
224
+ ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
225
+ : "N/A";
226
+ const createdBy: string =
227
+ note.createdByUser?.name?.toString() ||
228
+ note.createdByUser?.email?.toString() ||
229
+ "Unknown";
230
+
231
+ contextText += `**[${noteTime}] ${createdBy}:**\n`;
232
+ contextText += `${note.note || "N/A"}\n\n`;
233
+ }
234
+ }
235
+
236
+ // Public notes
237
+ if (publicNotes.length > 0) {
238
+ contextText += "# Public Notes\n\n";
239
+ for (const note of publicNotes) {
240
+ const noteTime: string = note.postedAt
241
+ ? OneUptimeDate.getDateAsFormattedString(note.postedAt)
242
+ : note.createdAt
243
+ ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
244
+ : "N/A";
245
+ const createdBy: string =
246
+ note.createdByUser?.name?.toString() ||
247
+ note.createdByUser?.email?.toString() ||
248
+ "Unknown";
249
+
250
+ contextText += `**[${noteTime}] ${createdBy}:**\n`;
251
+ contextText += `${note.note || "N/A"}\n\n`;
252
+ }
253
+ }
254
+
255
+ // System prompt for note generation
256
+ let systemPrompt: string;
257
+
258
+ if (noteType === "public") {
259
+ if (template) {
260
+ systemPrompt = `You are an expert technical communicator. Your task is to fill in a public scheduled maintenance note template based on the provided maintenance event data.
261
+
262
+ CRITICAL INSTRUCTIONS:
263
+ - You MUST use ONLY the exact template structure provided below
264
+ - Fill in each section of the template with relevant information from the maintenance data
265
+ - Do NOT add any new sections, headers, or content that is not part of the template
266
+ - Do NOT add introductions, conclusions, or any text outside the template structure
267
+ - Write in a professional, clear, and customer-friendly manner
268
+ - Focus on what customers need to know: timing, impact, and what to expect
269
+ - Avoid technical jargon - keep it understandable for non-technical readers
270
+ - Be concise but informative
271
+
272
+ TEMPLATE TO FILL (use this exact structure):
273
+
274
+ ${template}`;
275
+ } else {
276
+ systemPrompt = `You are an expert technical communicator. Your task is to generate a public scheduled maintenance note that will be visible to customers on the status page.
277
+
278
+ The note should:
279
+ 1. Be written in a professional, customer-friendly tone
280
+ 2. Clearly communicate the current status of the maintenance
281
+ 3. Explain what work is being done and any impact on users
282
+ 4. Provide timing information (when it started, expected completion, etc.)
283
+ 5. Set appropriate expectations
284
+ 6. Be concise but informative
285
+
286
+ DO NOT include:
287
+ - Internal technical details that customers don't need
288
+ - Confidential information
289
+ - Excessive jargon
290
+
291
+ Write in markdown format for better readability.`;
292
+ }
293
+ } else if (template) {
294
+ // Internal note with template
295
+ systemPrompt = `You are an expert Site Reliability Engineer (SRE). Your task is to fill in an internal scheduled maintenance note template based on the provided maintenance event data.
296
+
297
+ CRITICAL INSTRUCTIONS:
298
+ - You MUST use ONLY the exact template structure provided below
299
+ - Fill in each section of the template with relevant information from the maintenance data
300
+ - Do NOT add any new sections, headers, or content that is not part of the template
301
+ - Do NOT add introductions, conclusions, or any text outside the template structure
302
+ - Be technical and detailed - this is for the internal team
303
+ - Include relevant technical details, progress updates, and observations
304
+
305
+ TEMPLATE TO FILL (use this exact structure):
306
+
307
+ ${template}`;
308
+ } else {
309
+ // Internal note without template
310
+ systemPrompt = `You are an expert Site Reliability Engineer (SRE). Your task is to generate an internal scheduled maintenance note for the team.
311
+
312
+ The note should:
313
+ 1. Provide technical details about the maintenance progress
314
+ 2. Document observations, findings, or actions taken
315
+ 3. Include any issues encountered or changes to the plan
316
+ 4. Be detailed enough to help team members understand the current status
317
+ 5. Use technical language appropriate for the engineering team
318
+
319
+ Write in markdown format for better readability. Be thorough and technical.`;
320
+ }
321
+
322
+ // Build user message
323
+ const userMessage: string = template
324
+ ? `Fill in the template above using ONLY the following scheduled maintenance data. Output only the filled template, nothing else:\n\n${contextText}`
325
+ : `Based on the following scheduled maintenance data, please generate ${noteType === "public" ? "a customer-facing public" : "an internal technical"} maintenance note:\n\n${contextText}`;
326
+
327
+ // Build messages array
328
+ const messages: Array<LLMMessage> = [
329
+ {
330
+ role: "system",
331
+ content: systemPrompt,
332
+ },
333
+ {
334
+ role: "user",
335
+ content: userMessage,
336
+ },
337
+ ];
338
+
339
+ return {
340
+ contextText,
341
+ systemPrompt,
342
+ messages,
343
+ };
344
+ }
345
+ }
@@ -434,12 +434,89 @@ export default class MicrosoftTeamsUtil extends WorkspaceBase {
434
434
  return { actionType: actionType as MicrosoftTeamsActionType, actionValue };
435
435
  }
436
436
 
437
+ /**
438
+ * Converts markdown tables to HTML tables for Teams MessageCard.
439
+ * Teams MessageCard supports HTML in the text field.
440
+ */
441
+ private static convertMarkdownTablesToHtml(markdown: string): string {
442
+ // Regular expression to match markdown tables
443
+ const tableRegex: RegExp =
444
+ /(?:^|\n)((?:\|[^\n]+\|\n)+(?:\|[-:\s|]+\|\n)(?:\|[^\n]+\|\n?)+)/g;
445
+
446
+ return markdown.replace(
447
+ tableRegex,
448
+ (_match: string, table: string): string => {
449
+ const lines: Array<string> = table.trim().split("\n");
450
+
451
+ if (lines.length < 2) {
452
+ return table;
453
+ }
454
+
455
+ // Parse header row
456
+ const headerLine: string = lines[0] || "";
457
+ const headers: Array<string> = headerLine
458
+ .split("|")
459
+ .map((cell: string) => {
460
+ return cell.trim();
461
+ })
462
+ .filter((cell: string) => {
463
+ return cell.length > 0;
464
+ });
465
+
466
+ // Skip separator line (line with dashes) and get data rows
467
+ const dataRows: Array<string> = lines.slice(2);
468
+
469
+ // Build HTML table
470
+ let html: string =
471
+ '<table style="border-collapse: collapse; width: 100%;">';
472
+
473
+ // Header row
474
+ html += "<tr>";
475
+ for (const header of headers) {
476
+ html += `<th style="border: 1px solid #ddd; padding: 8px; background-color: #f2f2f2; text-align: left;"><strong>${header}</strong></th>`;
477
+ }
478
+ html += "</tr>";
479
+
480
+ // Data rows
481
+ for (const row of dataRows) {
482
+ const cells: Array<string> = row
483
+ .split("|")
484
+ .map((cell: string) => {
485
+ return cell.trim();
486
+ })
487
+ .filter((cell: string) => {
488
+ return cell.length > 0;
489
+ });
490
+
491
+ if (cells.length === 0) {
492
+ continue;
493
+ }
494
+
495
+ html += "<tr>";
496
+ for (const cell of cells) {
497
+ html += `<td style="border: 1px solid #ddd; padding: 8px;">${cell}</td>`;
498
+ }
499
+ html += "</tr>";
500
+ }
501
+
502
+ html += "</table>";
503
+
504
+ return "\n" + html + "\n";
505
+ },
506
+ );
507
+ }
508
+
437
509
  private static buildMessageCardFromMarkdown(markdown: string): JSONObject {
438
510
  /*
439
511
  * Teams MessageCard has limited markdown support. Headings like '##' are not supported
440
512
  * and single newlines can collapse. Convert common patterns to a structured card.
441
513
  */
442
- const lines: Array<string> = markdown
514
+
515
+ // First, convert markdown tables to HTML
516
+ const markdownWithHtmlTables: string =
517
+ this.convertMarkdownTablesToHtml(markdown);
518
+
519
+ const lines: Array<string> = markdownWithHtmlTables
443
520
  .split("\n")
444
521
  .map((l: string) => {
445
522
  return l.trim();
@@ -1892,9 +1892,88 @@ export default class SlackUtil extends WorkspaceBase {
1892
1892
  return apiResult;
1893
1893
  }
1894
1894
 
1895
+ /**
1896
+ * Converts markdown tables to a Slack-friendly format.
1897
+ * Since Slack's mrkdwn doesn't support tables, we convert them to
1898
+ * a row-by-row format with bold headers.
1899
+ */
1900
+ private static convertMarkdownTablesToSlackFormat(markdown: string): string {
1901
+ // Regular expression to match markdown tables
1902
+ const tableRegex: RegExp =
1903
+ /(?:^|\n)((?:\|[^\n]+\|\n)+(?:\|[-:\s|]+\|\n)(?:\|[^\n]+\|\n?)+)/g;
1904
+
1905
+ return markdown.replace(
1906
+ tableRegex,
1907
+ (_match: string, table: string): string => {
1908
+ const lines: Array<string> = table.trim().split("\n");
1909
+
1910
+ if (lines.length < 2) {
1911
+ return table;
1912
+ }
1913
+
1914
+ // Parse header row
1915
+ const headerLine: string = lines[0] || "";
1916
+ const headers: Array<string> = headerLine
1917
+ .split("|")
1918
+ .map((cell: string) => {
1919
+ return cell.trim();
1920
+ })
1921
+ .filter((cell: string) => {
1922
+ return cell.length > 0;
1923
+ });
1924
+
1925
+ /*
1926
+ * Skip separator line (line with dashes)
1927
+ * Find data rows (skip header and separator)
1928
+ */
1929
+ const dataRows: Array<string> = lines.slice(2);
1930
+ const formattedRows: Array<string> = [];
1931
+
1932
+ for (let rowIndex: number = 0; rowIndex < dataRows.length; rowIndex++) {
1933
+ const row: string = dataRows[rowIndex] || "";
1934
+ const cells: Array<string> = row
1935
+ .split("|")
1936
+ .map((cell: string) => {
1937
+ return cell.trim();
1938
+ })
1939
+ .filter((cell: string) => {
1940
+ return cell.length > 0;
1941
+ });
1942
+
1943
+ if (cells.length === 0) {
1944
+ continue;
1945
+ }
1946
+
1947
+ const rowParts: Array<string> = [];
1948
+ for (
1949
+ let cellIndex: number = 0;
1950
+ cellIndex < cells.length;
1951
+ cellIndex++
1952
+ ) {
1953
+ const header: string =
1954
+ headers[cellIndex] || `Column ${cellIndex + 1}`;
1955
+ const value: string = cells[cellIndex] || "";
1956
+ rowParts.push(`*${header}:* ${value}`);
1957
+ }
1958
+
1959
+ if (dataRows.length > 1) {
1960
+ formattedRows.push(`_Row ${rowIndex + 1}_\n${rowParts.join("\n")}`);
1961
+ } else {
1962
+ formattedRows.push(rowParts.join("\n"));
1963
+ }
1964
+ }
1965
+
1966
+ return "\n" + formattedRows.join("\n\n") + "\n";
1967
+ },
1968
+ );
1969
+ }
1970
+
1895
1971
  @CaptureSpan()
1896
1972
  public static convertMarkdownToSlackRichText(markdown: string): string {
1897
- return SlackifyMarkdown(markdown);
1973
+ // First convert tables to Slack-friendly format
1974
+ const markdownWithConvertedTables: string =
1975
+ this.convertMarkdownTablesToSlackFormat(markdown);
1976
+ return SlackifyMarkdown(markdownWithConvertedTables);
1898
1977
  }
1899
1978
 
1900
1979
  @CaptureSpan()
@@ -13,22 +13,43 @@ describe("class Domain", () => {
13
13
  expect(new Domain("example.ac").domain).toBe("example.ac");
14
14
  });
15
15
  test("new Domain() should throw the BadDataException if domain is invalid", () => {
16
+ // No dot in domain
16
17
  expect(() => {
17
18
  return new Domain("example");
18
19
  }).toThrowError(BadDataException);
19
20
  expect(() => {
20
21
  new Domain("example");
21
22
  }).toThrowError(BadDataException);
23
+
24
+ // Invalid characters
22
25
  expect(() => {
23
26
  new Domain("example@com");
24
27
  }).toThrowError(BadDataException);
25
28
 
29
+ // TLD with numbers (invalid - TLD must be letters only)
30
+ expect(() => {
31
+ new Domain("example.c0m");
32
+ }).toThrowError(BadDataException);
33
+
34
+ // Single letter TLD (invalid - TLD must be at least 2 characters)
26
35
  expect(() => {
27
- new Domain("example.invalid");
36
+ new Domain("example.c");
28
37
  }).toThrowError(BadDataException);
38
+
39
+ // Domain starting with hyphen
40
+ expect(() => {
41
+ new Domain("-example.com");
42
+ }).toThrowError(BadDataException);
43
+
44
+ // Domain ending with hyphen before TLD
45
+ expect(() => {
46
+ new Domain("example-.com");
47
+ }).toThrowError(BadDataException);
48
+
49
+ // Mutation to invalid domain
29
50
  expect(() => {
30
- const validDomain: Domain = new Domain("example.valid");
31
- validDomain.domain = "example.invalid";
51
+ const validDomain: Domain = new Domain("example.com");
52
+ validDomain.domain = "invalid";
32
53
  }).toThrowError(BadDataException);
33
54
  });
34
55
  test("Domain.domain should be mutable", () => {
package/Types/Domain.ts CHANGED
@@ -18,33 +18,30 @@ export default class Domain extends DatabaseProperty {
18
18
  }
19
19
 
20
20
  public static isValidDomain(domain: string): boolean {
21
- if (!domain.includes(".")) {
21
+ /*
22
+ * Regex-based domain validation
23
+ * - Each label (part between dots) must be 1-63 characters
24
+ * - Labels can contain alphanumeric characters and hyphens
25
+ * - Labels cannot start or end with a hyphen
26
+ * - TLD must be at least 2 characters and contain only letters
27
+ * - Total length should not exceed 253 characters
28
+ */
29
+
30
+ if (!domain || domain.length > 253) {
22
31
  return false;
23
32
  }
24
33
 
25
- const firstTLDs: Array<string> =
26
- "ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|be|bf|bg|bh|bi|bj|bm|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|cl|cm|cn|co|cr|cu|cv|cw|cx|cz|de|dj|dk|dm|do|dz|ec|ee|eg|es|et|eu|fi|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|im|in|io|iq|ir|is|it|je|jo|jp|kg|ki|km|kn|kp|kr|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|na|nc|ne|nf|ng|nl|no|nr|nu|nz|om|pa|pe|pf|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|yt".split(
27
- "|",
28
- );
29
- const secondTLDs: Array<string> =
30
- "ac|academy|accountant|accountants|actor|adult|aero|ag|agency|ai|airforce|am|amsterdam|apartments|app|archi|army|art|asia|associates|at|attorney|au|auction|auto|autos|baby|band|bar|barcelona|bargains|basketball|bayern|be|beauty|beer|berlin|best|bet|bid|bike|bingo|bio|biz|biz.pl|black|blog|blue|boats|boston|boutique|broker|build|builders|business|buzz|bz|ca|cab|cafe|camera|camp|capital|car|cards|care|careers|cars|casa|cash|casino|catering|cc|center|ceo|ch|charity|chat|cheap|church|city|cl|claims|cleaning|clinic|clothing|cloud|club|cn|co|co.in|co.jp|co.kr|co.nz|co.uk|co.za|coach|codes|coffee|college|com|com.ag|com.au|com.br|com.bz|com.cn|com.co|com.es|com.ky|com.mx|com.pe|com.ph|com.pl|com.ru|com.tw|community|company|computer|condos|construction|consulting|contact|contractors|cooking|cool|country|coupons|courses|credit|creditcard|cricket|cruises|cymru|cz|dance|date|dating|de|deals|degree|delivery|democrat|dental|dentist|design|dev|diamonds|digital|direct|directory|discount|dk|doctor|dog|domains|download|earth|education|email|energy|engineer|engineering|enterprises|equipment|es|estate|eu|events|exchange|expert|exposed|express|fail|faith|family|fan|fans|farm|fashion|film|finance|financial|firm.in|fish|fishing|fit|fitness|flights|florist|fm|football|forsale|foundation|fr|fun|fund|furniture|futbol|fyi|gallery|games|garden|gay|gen.in|gg|gifts|gives|giving|glass|global|gmbh|gold|golf|graphics|gratis|green|gripe|group|gs|guide|guru|hair|haus|health|healthcare|hockey|holdings|holiday|homes|horse|hospital|host|house|idv.tw|immo|immobilien|in|inc|ind.in|industries|info|info.pl|ink|institute|insure|international|investments|io|irish|ist|istanbul|it|jetzt|jewelry|jobs|jp|kaufen|kids|kim|kitchen|kiwi|kr|ky|la|land|lat|law|lawyer|lease|legal|lgbt|life|lighting|limited|limo|live|llc|llp|loan|loans|london|love|ltd|ltda|luxury|maison|makeup|management|market|marketing|mba|me|me.uk|media|melbourne|memorial|men|menu|miami|mobi|moda|moe|money|monster|mortgage|motorcycles|movie|ms|music|mx|nagoya|name|navy|ne.kr|net|net.ag|net.au|net.br|net.bz|net.cn|net.co|net.in|net.ky|net.nz|net.pe|net.ph|net.pl|net.ru|network|news|ninja|nl|no|nom.co|nom.es|nom.pe|nrw|nyc|okinawa|one|onl|online|org|org.ag|org.au|org.cn|org.es|org.in|org.ky|org.nz|org.pe|org.ph|org.pl|org.ru|org.uk|organic|page|paris|partners|parts|party|pe|pet|ph|photography|photos|pictures|pink|pizza|pl|place|plumbing|plus|poker|porn|press|pro|productions|promo|properties|protection|pub|pw|quebec|quest|racing|re.kr|realestate|recipes|red|rehab|reise|reisen|rent|rentals|repair|report|republican|rest|restaurant|review|reviews|rich|rip|rocks|rodeo|rugby|run|ryukyu|sale|salon|sarl|school|schule|science|se|security|services|sex|sg|sh|shiksha|shoes|shop|shopping|show|singles|site|ski|skin|soccer|social|software|solar|solutions|space|storage|store|stream|studio|study|style|supplies|supply|support|surf|surgery|sydney|systems|tax|taxi|team|tech|technology|tel|tennis|theater|theatre|tickets|tienda|tips|tires|today|tokyo|tools|tours|town|toys|trade|trading|training|travel|tube|tv|tw|uk|university|uno|us|vacations|vc|vegas|ventures|vet|viajes|video|villas|vin|vip|vision|vodka|vote|voto|voyage|wales|watch|web|webcam|website|wedding|wiki|win|wine|work|works|world|ws|wtf|xxx|xyz|yachts|yoga|yokohama|zone|移动|dev|com|edu|gov|net|mil|org|nom|sch|sbs|caa|res|off|gob|int|tur|ip6|uri|urn|asn|act|nsw|qld|tas|vic|pro|biz|adm|adv|agr|arq|art|ato|bio|bmd|cim|cng|cnt|ecn|eco|emp|eng|esp|etc|eti|far|fnd|fot|fst|g12|ggf|imb|ind|inf|jor|jus|leg|lel|mat|med|mus|not|ntr|odo|ppg|psc|psi|qsl|rec|slg|srv|teo|tmp|trd|vet|zlg|web|ltd|sld|pol|fin|k12|lib|pri|aip|fie|eun|sci|prd|cci|pvt|mod|idv|rel|sex|gen|nic|abr|bas|cal|cam|emr|fvg|laz|lig|lom|mar|mol|pmn|pug|sar|sic|taa|tos|umb|vao|vda|ven|mie|北海道|和歌山|神奈川|鹿児島|ass|rep|tra|per|ngo|soc|grp|plc|its|air|and|bus|can|ddr|jfk|mad|nrw|nyc|ski|spy|tcm|ulm|usa|war|fhs|vgs|dep|eid|fet|fla|flå|gol|hof|hol|sel|vik|cri|iwi|ing|abo|fam|gok|gon|gop|gos|aid|atm|gsm|sos|elk|waw|est|aca|bar|cpa|jur|law|sec|plo|www|bir|cbg|jar|khv|msk|nov|nsk|ptz|rnd|spb|stv|tom|tsk|udm|vrn|cmw|kms|nkz|snz|pub|fhv|red|ens|nat|rns|rnu|bbs|tel|bel|kep|nhs|dni|fed|isa|nsn|gub|e12|tec|орг|обр|упр|alt|nis|jpn|mex|ath|iki|nid|gda|inc|za|ovh|lol|africa|top|coop".split(
31
- "|",
32
- );
33
-
34
- const parts: Array<string> = domain.split(".");
35
- const lastItem: string = parts[parts.length - 1] as string;
36
- const beforeLastItem: string = parts[parts.length - 2] as string;
37
-
38
- if (firstTLDs.includes(lastItem)) {
39
- if (secondTLDs.includes(beforeLastItem)) {
40
- return true;
41
- }
42
- return true;
43
- } else if (secondTLDs.includes(lastItem)) {
44
- return true;
45
- }
46
-
47
- return false;
34
+ /*
35
+ * Domain validation regex:
36
+ * ^ - start of string
37
+ * (?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+ - one or more labels followed by dot
38
+ * [a-zA-Z]{2,63} - TLD: 2-63 letters only
39
+ * $ - end of string
40
+ */
41
+ const domainRegex: RegExp =
42
+ /^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,63}$/;
43
+
44
+ return domainRegex.test(domain);
48
45
  }
49
46
 
50
47
  public constructor(domain: string) {