@nuelink/nuelink-cli 1.4.0 → 1.4.1
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +51 -29
- package/SECURITY.md +70 -0
- package/package.json +30 -13
- package/src/cli.js +37 -13
- package/src/commands.js +166 -125
- package/src/config.js +107 -38
- package/src/errors.js +47 -0
- package/src/http.js +95 -54
- package/src/postPayload.js +177 -171
- package/src/validation.js +53 -14
- package/test/cli.test.mjs +1217 -0
- package/test/mock-fetch.cjs +261 -0
- package/test/mock-fetch.mjs +261 -0
package/src/postPayload.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
|
-
parseBooleanStrict,
|
|
3
|
-
parseJsonString,
|
|
4
|
-
parseOptionalNonNegativeInteger,
|
|
2
|
+
parseBooleanStrict,
|
|
3
|
+
parseJsonString,
|
|
4
|
+
parseOptionalNonNegativeInteger,
|
|
5
5
|
splitCsv,
|
|
6
6
|
validateHttpUrl,
|
|
7
|
-
validatePollOptions,
|
|
8
|
-
|
|
7
|
+
validatePollOptions,
|
|
8
|
+
validateRequiredString,
|
|
9
|
+
validateScheduledAt,
|
|
9
10
|
} from "./validation.js";
|
|
10
11
|
|
|
11
|
-
const VALID_PUBLISH_MODES = ["DRAFT", "QUEUE", "SCHEDULE", "IMMEDIATE"];
|
|
12
|
-
const VALID_POLL_PERIODS = ["1d", "3d", "7d"];
|
|
13
|
-
const VALID_TRIAL_REEL_MODES = ["MANUAL", "SS_PERFORMANCE"];
|
|
14
|
-
|
|
15
|
-
function validateOptionalLength(value, maximum, label) {
|
|
16
|
-
if (value !== undefined && value !== null && String(value).length > maximum) {
|
|
17
|
-
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
12
|
+
const VALID_PUBLISH_MODES = ["DRAFT", "QUEUE", "SCHEDULE", "IMMEDIATE"];
|
|
13
|
+
const VALID_POLL_PERIODS = ["1d", "3d", "7d"];
|
|
14
|
+
const VALID_TRIAL_REEL_MODES = ["MANUAL", "SS_PERFORMANCE"];
|
|
15
|
+
|
|
16
|
+
function validateOptionalLength(value, maximum, label) {
|
|
17
|
+
if (value !== undefined && value !== null && String(value).length > maximum) {
|
|
18
|
+
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
20
21
|
|
|
21
22
|
function parseCsv(value) {
|
|
22
23
|
return splitCsv(value);
|
|
@@ -55,7 +56,7 @@ function mergeMedia(idsCsv, urlsCsv) {
|
|
|
55
56
|
return [...idMedia, ...urlMedia];
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
function normalizePublishMode(value) {
|
|
59
|
+
function normalizePublishMode(value) {
|
|
59
60
|
const normalized = String(value ?? "").trim().toUpperCase();
|
|
60
61
|
|
|
61
62
|
if (!VALID_PUBLISH_MODES.includes(normalized)) {
|
|
@@ -63,69 +64,72 @@ function normalizePublishMode(value) {
|
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
return normalized;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function validateRawPostPayload(payload) {
|
|
69
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
70
|
-
throw new Error("Invalid payload. Expected a JSON object.");
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const publishMode = normalizePublishMode(payload.publishMode);
|
|
74
|
-
const scheduledAt = validateScheduledAt(payload.scheduledAt);
|
|
75
|
-
if (publishMode === "SCHEDULE" && !scheduledAt) {
|
|
76
|
-
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
validateOptionalLength(payload.title, 255, "title");
|
|
80
|
-
validateOptionalLength(payload.caption, 3000, "caption");
|
|
81
|
-
validateOptionalLength(payload.alt, 255, "alt");
|
|
82
|
-
if (payload.link !== undefined) {
|
|
83
|
-
validateHttpUrl(payload.link, "link");
|
|
84
|
-
}
|
|
85
|
-
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function validateRawPostPayload(payload) {
|
|
70
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) {
|
|
71
|
+
throw new Error("Invalid payload. Expected a JSON object.");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const publishMode = normalizePublishMode(payload.publishMode);
|
|
75
|
+
const scheduledAt = validateScheduledAt(payload.scheduledAt);
|
|
76
|
+
if (publishMode === "SCHEDULE" && !scheduledAt) {
|
|
77
|
+
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
validateOptionalLength(payload.title, 255, "title");
|
|
81
|
+
validateOptionalLength(payload.caption, 3000, "caption");
|
|
82
|
+
validateOptionalLength(payload.alt, 255, "alt");
|
|
83
|
+
if (payload.link !== undefined) {
|
|
84
|
+
validateHttpUrl(payload.link, "link");
|
|
85
|
+
}
|
|
86
|
+
|
|
86
87
|
const poll = payload.poll;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
88
|
+
const caption = !poll
|
|
89
|
+
? validateRequiredString(payload.caption, 3000, "caption")
|
|
90
|
+
: payload.caption === undefined
|
|
91
|
+
? undefined
|
|
92
|
+
: String(payload.caption).trim();
|
|
93
|
+
if (poll !== undefined) {
|
|
94
|
+
if (!poll || typeof poll !== "object" || Array.isArray(poll)) {
|
|
95
|
+
throw new Error("Invalid poll. Expected a JSON object.");
|
|
96
|
+
}
|
|
97
|
+
const options = Array.isArray(poll.options) ? poll.options : [];
|
|
98
|
+
validatePollOptions(options, poll.correctOptionIndex);
|
|
99
|
+
if (!poll.question || options.length < 2 || options.length > 4 || !VALID_POLL_PERIODS.includes(poll.period)) {
|
|
100
|
+
throw new Error("poll requires question, 2 to 4 options, and period (1d, 3d, or 7d).");
|
|
101
|
+
}
|
|
102
|
+
validateOptionalLength(poll.question, 280, "poll-question");
|
|
103
|
+
validateOptionalLength(poll.explanation, 512, "poll-explanation");
|
|
104
|
+
options.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (payload.media !== undefined) {
|
|
108
|
+
if (!Array.isArray(payload.media)) {
|
|
109
|
+
throw new Error("Invalid media. Expected an array.");
|
|
110
|
+
}
|
|
111
|
+
payload.media.forEach((item, index) => {
|
|
112
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) {
|
|
113
|
+
throw new Error(`Invalid media item at index ${index}.`);
|
|
114
|
+
}
|
|
115
|
+
const hasId = typeof item.id === "string" && item.id.trim().length > 0;
|
|
116
|
+
const hasUrl = typeof item.url === "string" && item.url.trim().length > 0;
|
|
117
|
+
if (hasId === hasUrl) {
|
|
118
|
+
throw new Error(`Invalid media item at index ${index}. Expected exactly one of id or url.`);
|
|
119
|
+
}
|
|
120
|
+
if (hasUrl) {
|
|
121
|
+
validateHttpUrl(item.url, `media[${index}].url`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
123
126
|
return {
|
|
124
127
|
...payload,
|
|
128
|
+
...(payload.caption !== undefined ? { caption } : {}),
|
|
125
129
|
publishMode,
|
|
126
|
-
...(payload.scheduledAt !== undefined ? { scheduledAt } : {}),
|
|
127
|
-
};
|
|
128
|
-
}
|
|
130
|
+
...(payload.scheduledAt !== undefined ? { scheduledAt } : {}),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
129
133
|
|
|
130
134
|
function buildPostPayload(options) {
|
|
131
135
|
const media = mergeMedia(options.mediaIds, options.mediaUrls);
|
|
@@ -136,38 +140,38 @@ function buildPostPayload(options) {
|
|
|
136
140
|
throw new Error("scheduledAt is required when publishMode is SCHEDULE.");
|
|
137
141
|
}
|
|
138
142
|
|
|
139
|
-
const pollOptions = options.pollOptions ? parseCsv(options.pollOptions) : [];
|
|
140
|
-
const pollCorrectOptionIndex = parseOptionalNonNegativeInteger(options.pollCorrectOptionIndex, "poll-correct-option-index");
|
|
141
|
-
validatePollOptions(pollOptions, pollCorrectOptionIndex);
|
|
142
|
-
validateOptionalLength(options.title, 255, "title");
|
|
143
|
-
validateOptionalLength(options.caption, 3000, "caption");
|
|
144
|
-
validateOptionalLength(options.alt, 255, "alt");
|
|
145
|
-
validateOptionalLength(options.pollQuestion, 280, "poll-question");
|
|
146
|
-
validateOptionalLength(options.pollExplanation, 512, "poll-explanation");
|
|
147
|
-
validateOptionalLength(options.comment, 1000, "comment");
|
|
148
|
-
validateOptionalLength(options.instagramCollab, 1000, "instagram-collab");
|
|
149
|
-
pollOptions.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
150
|
-
|
|
151
|
-
if (options.pollPeriod && !VALID_POLL_PERIODS.includes(options.pollPeriod)) {
|
|
152
|
-
throw new Error(`Invalid poll-period. Expected one of: ${VALID_POLL_PERIODS.join(", ")}.`);
|
|
153
|
-
}
|
|
154
|
-
if (options.instagramTrialReel && !VALID_TRIAL_REEL_MODES.includes(options.instagramTrialReel)) {
|
|
155
|
-
throw new Error(`Invalid instagram-trial-reel. Expected one of: ${VALID_TRIAL_REEL_MODES.join(", ")}.`);
|
|
156
|
-
}
|
|
157
|
-
if (Boolean(options.instagramLocationId) !== Boolean(options.instagramLocationName)) {
|
|
158
|
-
throw new Error("instagram-location-id and instagram-location-name must be provided together.");
|
|
159
|
-
}
|
|
160
|
-
if (Boolean(options.facebookLocationId) !== Boolean(options.facebookLocationName)) {
|
|
161
|
-
throw new Error("facebook-location-id and facebook-location-name must be provided together.");
|
|
162
|
-
}
|
|
143
|
+
const pollOptions = options.pollOptions ? parseCsv(options.pollOptions) : [];
|
|
144
|
+
const pollCorrectOptionIndex = parseOptionalNonNegativeInteger(options.pollCorrectOptionIndex, "poll-correct-option-index");
|
|
145
|
+
validatePollOptions(pollOptions, pollCorrectOptionIndex);
|
|
146
|
+
validateOptionalLength(options.title, 255, "title");
|
|
147
|
+
validateOptionalLength(options.caption, 3000, "caption");
|
|
148
|
+
validateOptionalLength(options.alt, 255, "alt");
|
|
149
|
+
validateOptionalLength(options.pollQuestion, 280, "poll-question");
|
|
150
|
+
validateOptionalLength(options.pollExplanation, 512, "poll-explanation");
|
|
151
|
+
validateOptionalLength(options.comment, 1000, "comment");
|
|
152
|
+
validateOptionalLength(options.instagramCollab, 1000, "instagram-collab");
|
|
153
|
+
pollOptions.forEach((option) => validateOptionalLength(option, 30, "poll-option"));
|
|
154
|
+
|
|
155
|
+
if (options.pollPeriod && !VALID_POLL_PERIODS.includes(options.pollPeriod)) {
|
|
156
|
+
throw new Error(`Invalid poll-period. Expected one of: ${VALID_POLL_PERIODS.join(", ")}.`);
|
|
157
|
+
}
|
|
158
|
+
if (options.instagramTrialReel && !VALID_TRIAL_REEL_MODES.includes(options.instagramTrialReel)) {
|
|
159
|
+
throw new Error(`Invalid instagram-trial-reel. Expected one of: ${VALID_TRIAL_REEL_MODES.join(", ")}.`);
|
|
160
|
+
}
|
|
161
|
+
if (Boolean(options.instagramLocationId) !== Boolean(options.instagramLocationName)) {
|
|
162
|
+
throw new Error("instagram-location-id and instagram-location-name must be provided together.");
|
|
163
|
+
}
|
|
164
|
+
if (Boolean(options.facebookLocationId) !== Boolean(options.facebookLocationName)) {
|
|
165
|
+
throw new Error("facebook-location-id and facebook-location-name must be provided together.");
|
|
166
|
+
}
|
|
163
167
|
|
|
164
168
|
const poll = {
|
|
165
169
|
...(options.pollQuestion ? { question: options.pollQuestion } : {}),
|
|
166
170
|
...(pollOptions.length > 0 ? { options: pollOptions } : {}),
|
|
167
171
|
...(options.pollPeriod ? { period: options.pollPeriod } : {}),
|
|
168
|
-
...(pollCorrectOptionIndex !== undefined
|
|
169
|
-
? { correctOptionIndex: pollCorrectOptionIndex }
|
|
170
|
-
: {}),
|
|
172
|
+
...(pollCorrectOptionIndex !== undefined
|
|
173
|
+
? { correctOptionIndex: pollCorrectOptionIndex }
|
|
174
|
+
: {}),
|
|
171
175
|
...(options.pollExplanation ? { explanation: options.pollExplanation } : {}),
|
|
172
176
|
};
|
|
173
177
|
|
|
@@ -188,7 +192,7 @@ function buildPostPayload(options) {
|
|
|
188
192
|
};
|
|
189
193
|
|
|
190
194
|
const facebook = {
|
|
191
|
-
...(options.facebookLocationId || options.facebookLocationName
|
|
195
|
+
...(options.facebookLocationId || options.facebookLocationName
|
|
192
196
|
? {
|
|
193
197
|
location: {
|
|
194
198
|
...(options.facebookLocationId ? { id: options.facebookLocationId } : {}),
|
|
@@ -199,75 +203,77 @@ function buildPostPayload(options) {
|
|
|
199
203
|
};
|
|
200
204
|
|
|
201
205
|
const tiktok = {
|
|
202
|
-
...(parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") !== undefined
|
|
203
|
-
? { sendToInbox: parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") }
|
|
204
|
-
: {}),
|
|
205
|
-
...(parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") !== undefined
|
|
206
|
-
? { autoAddMusic: parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") }
|
|
207
|
-
: {}),
|
|
206
|
+
...(parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") !== undefined
|
|
207
|
+
? { sendToInbox: parseBooleanStrict(options.tiktokSendToInbox, "tiktok-send-to-inbox") }
|
|
208
|
+
: {}),
|
|
209
|
+
...(parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") !== undefined
|
|
210
|
+
? { autoAddMusic: parseBooleanStrict(options.tiktokAutoAddMusic, "tiktok-auto-add-music") }
|
|
211
|
+
: {}),
|
|
208
212
|
};
|
|
209
213
|
|
|
210
214
|
let youtubePlaylists;
|
|
211
|
-
if (options.youtubePlaylists) {
|
|
212
|
-
youtubePlaylists = parseJsonString(options.youtubePlaylists, "youtube-playlists");
|
|
213
|
-
if (!Array.isArray(youtubePlaylists)) {
|
|
214
|
-
throw new Error("Invalid youtube-playlists. Expected a JSON array.");
|
|
215
|
-
}
|
|
216
|
-
youtubePlaylists = youtubePlaylists.map((playlist) => ({
|
|
217
|
-
...playlist,
|
|
218
|
-
channelId: Number(playlist.channelId),
|
|
219
|
-
}));
|
|
220
|
-
if (youtubePlaylists.some((playlist) =>
|
|
221
|
-
!Number.isSafeInteger(playlist.channelId) ||
|
|
222
|
-
playlist.channelId < 1 ||
|
|
223
|
-
!Array.isArray(playlist.playlistIds)
|
|
224
|
-
)) {
|
|
225
|
-
throw new Error("Invalid youtube-playlists. Each item requires a positive integer channelId and playlistIds array.");
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const youtube = {
|
|
230
|
-
...(options.youtubeTags
|
|
231
|
-
? {
|
|
232
|
-
tags: parseCsv(options.youtubeTags).map((tag) => {
|
|
233
|
-
validateOptionalLength(tag, 255, "youtube-tag");
|
|
234
|
-
return tag;
|
|
235
|
-
}),
|
|
236
|
-
}
|
|
237
|
-
: {}),
|
|
215
|
+
if (options.youtubePlaylists) {
|
|
216
|
+
youtubePlaylists = parseJsonString(options.youtubePlaylists, "youtube-playlists");
|
|
217
|
+
if (!Array.isArray(youtubePlaylists)) {
|
|
218
|
+
throw new Error("Invalid youtube-playlists. Expected a JSON array.");
|
|
219
|
+
}
|
|
220
|
+
youtubePlaylists = youtubePlaylists.map((playlist) => ({
|
|
221
|
+
...playlist,
|
|
222
|
+
channelId: Number(playlist.channelId),
|
|
223
|
+
}));
|
|
224
|
+
if (youtubePlaylists.some((playlist) =>
|
|
225
|
+
!Number.isSafeInteger(playlist.channelId) ||
|
|
226
|
+
playlist.channelId < 1 ||
|
|
227
|
+
!Array.isArray(playlist.playlistIds)
|
|
228
|
+
)) {
|
|
229
|
+
throw new Error("Invalid youtube-playlists. Each item requires a positive integer channelId and playlistIds array.");
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const youtube = {
|
|
234
|
+
...(options.youtubeTags
|
|
235
|
+
? {
|
|
236
|
+
tags: parseCsv(options.youtubeTags).map((tag) => {
|
|
237
|
+
validateOptionalLength(tag, 255, "youtube-tag");
|
|
238
|
+
return tag;
|
|
239
|
+
}),
|
|
240
|
+
}
|
|
241
|
+
: {}),
|
|
238
242
|
...(youtubePlaylists ? { playlists: youtubePlaylists } : {}),
|
|
239
|
-
};
|
|
240
|
-
const googlemybusiness = {
|
|
241
|
-
...(parseBooleanStrict(
|
|
242
|
-
options.googlemybusinessUploadToPhotosSection,
|
|
243
|
-
"googlemybusiness-upload-to-photos-section",
|
|
244
|
-
) !== undefined
|
|
245
|
-
? {
|
|
246
|
-
uploadToPhotosSection: parseBooleanStrict(
|
|
247
|
-
options.googlemybusinessUploadToPhotosSection,
|
|
248
|
-
"googlemybusiness-upload-to-photos-section",
|
|
249
|
-
),
|
|
250
|
-
}
|
|
251
|
-
: {}),
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const hasPoll = Object.keys(poll).length > 0;
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
243
|
+
};
|
|
244
|
+
const googlemybusiness = {
|
|
245
|
+
...(parseBooleanStrict(
|
|
246
|
+
options.googlemybusinessUploadToPhotosSection,
|
|
247
|
+
"googlemybusiness-upload-to-photos-section",
|
|
248
|
+
) !== undefined
|
|
249
|
+
? {
|
|
250
|
+
uploadToPhotosSection: parseBooleanStrict(
|
|
251
|
+
options.googlemybusinessUploadToPhotosSection,
|
|
252
|
+
"googlemybusiness-upload-to-photos-section",
|
|
253
|
+
),
|
|
254
|
+
}
|
|
255
|
+
: {}),
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const hasPoll = Object.keys(poll).length > 0;
|
|
259
|
+
const caption = !hasPoll
|
|
260
|
+
? validateRequiredString(options.caption, 3000, "caption")
|
|
261
|
+
: options.caption === undefined
|
|
262
|
+
? undefined
|
|
263
|
+
: String(options.caption).trim();
|
|
264
|
+
if (hasPoll && (!poll.question || poll.options?.length < 2 || !poll.period)) {
|
|
265
|
+
throw new Error("poll requires question, 2 to 4 options, and period.");
|
|
266
|
+
}
|
|
267
|
+
if (poll.options?.length > 4) {
|
|
268
|
+
throw new Error("Polls allow at most four options.");
|
|
269
|
+
}
|
|
264
270
|
|
|
265
271
|
return {
|
|
266
272
|
...(options.title ? { title: options.title } : {}),
|
|
267
|
-
...(
|
|
273
|
+
...(caption ? { caption } : {}),
|
|
268
274
|
...(options.alt ? { alt: options.alt } : {}),
|
|
269
275
|
...(options.link ? { link: validateHttpUrl(options.link, "link") } : {}),
|
|
270
|
-
publishMode,
|
|
276
|
+
publishMode,
|
|
271
277
|
scheduledAt,
|
|
272
278
|
...(parseBooleanStrict(options.postAsStory, "post-as-story") !== undefined
|
|
273
279
|
? { postAsStory: parseBooleanStrict(options.postAsStory, "post-as-story") }
|
|
@@ -276,22 +282,22 @@ function buildPostPayload(options) {
|
|
|
276
282
|
? {
|
|
277
283
|
comment: {
|
|
278
284
|
comment: options.comment,
|
|
279
|
-
...(parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") !== undefined
|
|
280
|
-
? { delay: parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") }
|
|
285
|
+
...(parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") !== undefined
|
|
286
|
+
? { delay: parseOptionalNonNegativeInteger(options.commentDelay, "comment-delay") }
|
|
281
287
|
: {}),
|
|
282
288
|
},
|
|
283
289
|
}
|
|
284
290
|
: {}),
|
|
285
291
|
...(media.length > 0 ? { media } : {}),
|
|
286
292
|
...(Object.keys(poll).length > 0 ? { poll } : {}),
|
|
287
|
-
...(Object.keys(instagram).length > 0 || Object.keys(facebook).length > 0 || Object.keys(tiktok).length > 0 || Object.keys(youtube).length > 0 || Object.keys(googlemybusiness).length > 0
|
|
293
|
+
...(Object.keys(instagram).length > 0 || Object.keys(facebook).length > 0 || Object.keys(tiktok).length > 0 || Object.keys(youtube).length > 0 || Object.keys(googlemybusiness).length > 0
|
|
288
294
|
? {
|
|
289
295
|
platforms: {
|
|
290
296
|
...(Object.keys(instagram).length > 0 ? { instagram } : {}),
|
|
291
297
|
...(Object.keys(facebook).length > 0 ? { facebook } : {}),
|
|
292
298
|
...(Object.keys(tiktok).length > 0 ? { tiktok } : {}),
|
|
293
|
-
...(Object.keys(youtube).length > 0 ? { youtube } : {}),
|
|
294
|
-
...(Object.keys(googlemybusiness).length > 0 ? { googlemybusiness } : {}),
|
|
299
|
+
...(Object.keys(youtube).length > 0 ? { youtube } : {}),
|
|
300
|
+
...(Object.keys(googlemybusiness).length > 0 ? { googlemybusiness } : {}),
|
|
295
301
|
},
|
|
296
302
|
}
|
|
297
303
|
: {}),
|
|
@@ -303,7 +309,7 @@ export {
|
|
|
303
309
|
mergeMedia,
|
|
304
310
|
parseBoolean,
|
|
305
311
|
parseCsv,
|
|
306
|
-
parseNumber,
|
|
307
|
-
validateRawPostPayload,
|
|
308
|
-
VALID_PUBLISH_MODES,
|
|
309
|
-
};
|
|
312
|
+
parseNumber,
|
|
313
|
+
validateRawPostPayload,
|
|
314
|
+
VALID_PUBLISH_MODES,
|
|
315
|
+
};
|
package/src/validation.js
CHANGED
|
@@ -41,11 +41,11 @@ const SUPPORTED_MEDIA_TYPES = new Map([
|
|
|
41
41
|
[".mp4", "video/mp4"],
|
|
42
42
|
[".mov", "video/quicktime"],
|
|
43
43
|
]);
|
|
44
|
-
const DEFAULT_MAX_MEDIA_SIZE_BYTES =
|
|
45
|
-
const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
|
|
44
|
+
const DEFAULT_MAX_MEDIA_SIZE_BYTES = 100 * 1024 * 1024;
|
|
45
|
+
const QUEUE_SLOT_PATTERN = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (?:[01][0-9]|2[0-3]):[0-5][0-9]$/;
|
|
46
46
|
const SQL_DATETIME_PATTERN = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/;
|
|
47
47
|
|
|
48
|
-
function splitCsv(value) {
|
|
48
|
+
function splitCsv(value) {
|
|
49
49
|
if (!value) {
|
|
50
50
|
return [];
|
|
51
51
|
}
|
|
@@ -54,7 +54,40 @@ function splitCsv(value) {
|
|
|
54
54
|
.split(",")
|
|
55
55
|
.map((item) => item.trim())
|
|
56
56
|
.filter(Boolean);
|
|
57
|
-
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function validateRequiredString(value, maximum, label) {
|
|
60
|
+
const normalized = String(value ?? "").trim();
|
|
61
|
+
|
|
62
|
+
if (!normalized) {
|
|
63
|
+
throw new Error(`${label} must not be blank.`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (normalized.length > maximum) {
|
|
67
|
+
throw new Error(`${label} must not exceed ${maximum} characters.`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return normalized;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function readPositiveIntegerEnvironment(name, fallback) {
|
|
74
|
+
const raw = process.env[name];
|
|
75
|
+
if (raw === undefined || raw === "") {
|
|
76
|
+
return fallback;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const normalized = String(raw).trim();
|
|
80
|
+
if (!/^\d+$/.test(normalized)) {
|
|
81
|
+
throw new Error(`${name} must be a positive integer.`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const parsed = Number(normalized);
|
|
85
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
86
|
+
throw new Error(`${name} must be a positive integer.`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return parsed;
|
|
90
|
+
}
|
|
58
91
|
|
|
59
92
|
function normalizeEnum(value, label, allowedValues) {
|
|
60
93
|
const normalized = String(value ?? "").trim().toUpperCase();
|
|
@@ -151,7 +184,7 @@ function validateHttpUrl(value, label) {
|
|
|
151
184
|
return parsedUrl.toString();
|
|
152
185
|
}
|
|
153
186
|
|
|
154
|
-
function validateQueueSlots(queueSlots) {
|
|
187
|
+
function validateQueueSlots(queueSlots) {
|
|
155
188
|
queueSlots.forEach((slot) => {
|
|
156
189
|
if (!QUEUE_SLOT_PATTERN.test(slot)) {
|
|
157
190
|
throw new Error(`Invalid queue slot \"${slot}\". Expected values like Mon 10:10.`);
|
|
@@ -159,8 +192,8 @@ function validateQueueSlots(queueSlots) {
|
|
|
159
192
|
});
|
|
160
193
|
|
|
161
194
|
return queueSlots;
|
|
162
|
-
}
|
|
163
|
-
|
|
195
|
+
}
|
|
196
|
+
|
|
164
197
|
function validateScheduledAt(value) {
|
|
165
198
|
if (value === undefined || value === null || value === "" || value === "null") {
|
|
166
199
|
return null;
|
|
@@ -209,7 +242,12 @@ function parseJsonString(value, label) {
|
|
|
209
242
|
}
|
|
210
243
|
}
|
|
211
244
|
|
|
212
|
-
function validateMediaFile(filePath, fileStat) {
|
|
245
|
+
function validateMediaFile(filePath, fileStat) {
|
|
246
|
+
const maximumSize = readPositiveIntegerEnvironment(
|
|
247
|
+
"NUELINK_MAX_MEDIA_SIZE_BYTES",
|
|
248
|
+
DEFAULT_MAX_MEDIA_SIZE_BYTES,
|
|
249
|
+
);
|
|
250
|
+
|
|
213
251
|
if (!fileStat.isFile()) {
|
|
214
252
|
throw new Error("Invalid file. Expected a regular file path.");
|
|
215
253
|
}
|
|
@@ -218,8 +256,8 @@ function validateMediaFile(filePath, fileStat) {
|
|
|
218
256
|
throw new Error("Invalid file. File must not be empty.");
|
|
219
257
|
}
|
|
220
258
|
|
|
221
|
-
if (fileStat.size >
|
|
222
|
-
throw new Error(`Invalid file. File exceeds ${
|
|
259
|
+
if (fileStat.size > maximumSize) {
|
|
260
|
+
throw new Error(`Invalid file. File exceeds ${maximumSize} bytes.`);
|
|
223
261
|
}
|
|
224
262
|
|
|
225
263
|
const extension = path.extname(filePath).toLowerCase();
|
|
@@ -248,9 +286,10 @@ export {
|
|
|
248
286
|
parsePositiveInteger,
|
|
249
287
|
splitCsv,
|
|
250
288
|
validateHttpUrl,
|
|
251
|
-
validateMediaFile,
|
|
289
|
+
validateMediaFile,
|
|
252
290
|
validatePollOptions,
|
|
253
291
|
validateScheduledAt,
|
|
254
|
-
validateQueueSlots,
|
|
255
|
-
|
|
256
|
-
|
|
292
|
+
validateQueueSlots,
|
|
293
|
+
validateRequiredString,
|
|
294
|
+
normalizeEnum,
|
|
295
|
+
};
|