@nuelink/nuelink-cli 1.1.0 → 1.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.
@@ -0,0 +1,186 @@
1
+ import path from "node:path";
2
+
3
+ const VALID_AUTOMATION_TYPES = ["FEED"];
4
+ const VALID_AUTOMATION_SUB_TYPES = ["RSS"];
5
+ const VALID_IMPORT_AS_TYPES = ["IMAGE", "VIDEO", "CAROUSEL", "TEXT"];
6
+ const SUPPORTED_MEDIA_TYPES = new Map([
7
+ [".jpg", "image/jpeg"],
8
+ [".jpeg", "image/jpeg"],
9
+ [".png", "image/png"],
10
+ [".gif", "image/gif"],
11
+ [".webp", "image/webp"],
12
+ [".mp4", "video/mp4"],
13
+ [".mov", "video/quicktime"],
14
+ ]);
15
+ const DEFAULT_MAX_MEDIA_SIZE_BYTES = Number(process.env.NUELINK_MAX_MEDIA_SIZE_BYTES || 100 * 1024 * 1024);
16
+ const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s\d{2}:\d{2}$/;
17
+ const SQL_DATETIME_PATTERN = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
18
+
19
+ function splitCsv(value) {
20
+ if (!value) {
21
+ return [];
22
+ }
23
+
24
+ return value
25
+ .split(",")
26
+ .map((item) => item.trim())
27
+ .filter(Boolean);
28
+ }
29
+
30
+ function normalizeEnum(value, label, allowedValues) {
31
+ const normalized = String(value ?? "").trim().toUpperCase();
32
+
33
+ if (!allowedValues.includes(normalized)) {
34
+ throw new Error(`Invalid ${label} \"${value}\". Expected one of: ${allowedValues.join(", ")}.`);
35
+ }
36
+
37
+ return normalized;
38
+ }
39
+
40
+ function parsePositiveInteger(value, label) {
41
+ const normalized = String(value ?? "").trim();
42
+
43
+ if (!/^\d+$/.test(normalized)) {
44
+ throw new Error(`Invalid ${label}. Expected a positive integer.`);
45
+ }
46
+
47
+ const parsed = Number(normalized);
48
+
49
+ if (!Number.isSafeInteger(parsed) || parsed <= 0) {
50
+ throw new Error(`Invalid ${label}. Expected a positive integer.`);
51
+ }
52
+
53
+ return parsed;
54
+ }
55
+
56
+ function parseOptionalPositiveInteger(value, label) {
57
+ if (value === undefined || value === null || value === "") {
58
+ return undefined;
59
+ }
60
+
61
+ return parsePositiveInteger(value, label);
62
+ }
63
+
64
+ function parseBooleanStrict(value, label) {
65
+ if (value === undefined || value === null || value === "") {
66
+ return undefined;
67
+ }
68
+
69
+ const normalized = String(value).trim().toLowerCase();
70
+
71
+ if (["true", "1", "yes", "y"].includes(normalized)) {
72
+ return true;
73
+ }
74
+
75
+ if (["false", "0", "no", "n"].includes(normalized)) {
76
+ return false;
77
+ }
78
+
79
+ throw new Error(`Invalid ${label}. Expected true or false.`);
80
+ }
81
+
82
+ function validateHttpUrl(value, label) {
83
+ const normalized = String(value ?? "").trim();
84
+
85
+ let parsedUrl;
86
+ try {
87
+ parsedUrl = new URL(normalized);
88
+ } catch {
89
+ throw new Error(`Invalid ${label}. Expected an absolute http or https URL.`);
90
+ }
91
+
92
+ if (!["http:", "https:"].includes(parsedUrl.protocol)) {
93
+ throw new Error(`Invalid ${label}. Expected an absolute http or https URL.`);
94
+ }
95
+
96
+ return parsedUrl.toString();
97
+ }
98
+
99
+ function validateQueueSlots(queueSlots) {
100
+ queueSlots.forEach((slot) => {
101
+ if (!QUEUE_SLOT_PATTERN.test(slot)) {
102
+ throw new Error(`Invalid queue slot \"${slot}\". Expected values like Mon 10:10.`);
103
+ }
104
+ });
105
+
106
+ return queueSlots;
107
+ }
108
+
109
+ function validatePublishAt(value) {
110
+ if (value === undefined || value === null || value === "" || value === "null") {
111
+ return null;
112
+ }
113
+
114
+ const normalized = String(value).trim();
115
+
116
+ if (SQL_DATETIME_PATTERN.test(normalized)) {
117
+ return normalized;
118
+ }
119
+
120
+ if (Number.isNaN(Date.parse(normalized))) {
121
+ throw new Error("Invalid publish-at. Expected YYYY-MM-DD HH:mm:ss or an ISO 8601 datetime.");
122
+ }
123
+
124
+ return normalized;
125
+ }
126
+
127
+ function validatePollOptions(options, correctOptionIndex) {
128
+ if (options.length === 1) {
129
+ throw new Error("Polls require at least two options.");
130
+ }
131
+
132
+ if (correctOptionIndex !== undefined && (correctOptionIndex < 0 || correctOptionIndex >= options.length)) {
133
+ throw new Error("poll-correct-option-index must point to an existing poll option.");
134
+ }
135
+ }
136
+
137
+ function parseJsonString(value, label) {
138
+ try {
139
+ return JSON.parse(value);
140
+ } catch {
141
+ throw new Error(`Invalid ${label}. Expected valid JSON.`);
142
+ }
143
+ }
144
+
145
+ function validateMediaFile(filePath, fileStat) {
146
+ if (!fileStat.isFile()) {
147
+ throw new Error("Invalid file. Expected a regular file path.");
148
+ }
149
+
150
+ if (fileStat.size <= 0) {
151
+ throw new Error("Invalid file. File must not be empty.");
152
+ }
153
+
154
+ if (fileStat.size > DEFAULT_MAX_MEDIA_SIZE_BYTES) {
155
+ throw new Error(`Invalid file. File exceeds ${DEFAULT_MAX_MEDIA_SIZE_BYTES} bytes.`);
156
+ }
157
+
158
+ const extension = path.extname(filePath).toLowerCase();
159
+ const mediaType = SUPPORTED_MEDIA_TYPES.get(extension);
160
+
161
+ if (!mediaType) {
162
+ throw new Error(`Unsupported file type \"${extension || "(none)"}\".`);
163
+ }
164
+
165
+ return {
166
+ mediaType,
167
+ size: fileStat.size,
168
+ };
169
+ }
170
+
171
+ export {
172
+ VALID_AUTOMATION_SUB_TYPES,
173
+ VALID_AUTOMATION_TYPES,
174
+ VALID_IMPORT_AS_TYPES,
175
+ parseBooleanStrict,
176
+ parseJsonString,
177
+ parseOptionalPositiveInteger,
178
+ parsePositiveInteger,
179
+ splitCsv,
180
+ validateHttpUrl,
181
+ validateMediaFile,
182
+ validatePollOptions,
183
+ validatePublishAt,
184
+ validateQueueSlots,
185
+ normalizeEnum,
186
+ };