@rmdes/indiekit-endpoint-syndicate 1.0.0-beta.39 → 1.0.0-beta.40
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/lib/controllers/syndicate.js +19 -11
- package/lib/utils.js +54 -49
- package/package.json +5 -5
|
@@ -31,7 +31,7 @@ const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
31
31
|
* multi-container, replace with an atomic per-post claim (findOneAndUpdate
|
|
32
32
|
* setting a claim timestamp, with a stale-claim timeout).
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
const batch = { isRunning: false };
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Syndicate a single post and update it via Micropub
|
|
@@ -54,17 +54,24 @@ const syndicatePost = async ({
|
|
|
54
54
|
// Skip check in force mode (manual re-syndication from UI/backlog script).
|
|
55
55
|
// Photo posts skip OG check — Eleventy deliberately skips OG generation
|
|
56
56
|
// for posts with photos (the photo itself serves as og:image in HTML).
|
|
57
|
-
const meUrl =
|
|
58
|
-
|
|
57
|
+
const meUrl =
|
|
58
|
+
typeof publication.me === "string"
|
|
59
|
+
? publication.me
|
|
60
|
+
: publication.me?.href || publication.me?.toString?.() || "";
|
|
61
|
+
const hasPhotos =
|
|
62
|
+
Array.isArray(postData.properties?.photo) &&
|
|
63
|
+
postData.properties.photo.length > 0;
|
|
59
64
|
if (!force && meUrl && postData.properties?.url) {
|
|
60
|
-
console.log(
|
|
65
|
+
console.log(
|
|
66
|
+
`[syndication] Readiness gate: checking ${postData.properties.url} (me=${meUrl}, hasPhotos=${hasPhotos})`,
|
|
67
|
+
);
|
|
61
68
|
const readiness = hasPhotos
|
|
62
69
|
? await isPostReady(postData.properties.url, meUrl, { skipOg: true })
|
|
63
70
|
: await isPostReady(postData.properties.url, meUrl);
|
|
64
71
|
if (!readiness.ready) {
|
|
65
72
|
console.log(
|
|
66
73
|
`[syndication] Skipping ${postData.properties.url} — not yet built ` +
|
|
67
|
-
|
|
74
|
+
`(post: ${readiness.postStatus}, og: ${readiness.ogStatus})`,
|
|
68
75
|
);
|
|
69
76
|
return { skipped: true, url: postData.properties.url, readiness };
|
|
70
77
|
}
|
|
@@ -99,7 +106,9 @@ const syndicatePost = async ({
|
|
|
99
106
|
throw await IndiekitError.fromFetch(micropubResponse);
|
|
100
107
|
}
|
|
101
108
|
|
|
102
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
@type {object}
|
|
111
|
+
*/
|
|
103
112
|
const body = await micropubResponse.json();
|
|
104
113
|
|
|
105
114
|
return {
|
|
@@ -171,7 +180,7 @@ export const syndicateController = {
|
|
|
171
180
|
}
|
|
172
181
|
|
|
173
182
|
// --- Batch mode (no source_url — process ALL pending posts) ---
|
|
174
|
-
if (
|
|
183
|
+
if (batch.isRunning) {
|
|
175
184
|
console.log("[syndication] Batch already in progress, skipping");
|
|
176
185
|
return response.json({
|
|
177
186
|
success: "OK",
|
|
@@ -179,7 +188,7 @@ export const syndicateController = {
|
|
|
179
188
|
});
|
|
180
189
|
}
|
|
181
190
|
|
|
182
|
-
|
|
191
|
+
batch.isRunning = true;
|
|
183
192
|
|
|
184
193
|
try {
|
|
185
194
|
const allPostData = await getAllPostData(postsCollection);
|
|
@@ -250,8 +259,7 @@ export const syndicateController = {
|
|
|
250
259
|
const failed = results.filter((result) => !result.success).length;
|
|
251
260
|
const skipped = results.filter((result) => result.skipped).length;
|
|
252
261
|
const succeeded = results.length - failed - skipped;
|
|
253
|
-
const description =
|
|
254
|
-
`Processed ${allPostData.length} post(s): ${succeeded} succeeded, ${failed} failed, ${skipped} not yet built`;
|
|
262
|
+
const description = `Processed ${allPostData.length} post(s): ${succeeded} succeeded, ${failed} failed, ${skipped} not yet built`;
|
|
255
263
|
|
|
256
264
|
console.log(`[syndication] ${description}`);
|
|
257
265
|
|
|
@@ -261,7 +269,7 @@ export const syndicateController = {
|
|
|
261
269
|
results,
|
|
262
270
|
});
|
|
263
271
|
} finally {
|
|
264
|
-
|
|
272
|
+
batch.isRunning = false;
|
|
265
273
|
}
|
|
266
274
|
} catch (error) {
|
|
267
275
|
next(error);
|
package/lib/utils.js
CHANGED
|
@@ -5,36 +5,31 @@
|
|
|
5
5
|
* @returns {Promise<object>} Post data for given URL else most recent pending post
|
|
6
6
|
*/
|
|
7
7
|
export const getPostData = async (postsCollection, url) => {
|
|
8
|
-
let postData = {};
|
|
9
|
-
|
|
10
8
|
if (url) {
|
|
11
|
-
|
|
12
|
-
postData = await postsCollection.findOne({
|
|
9
|
+
return postsCollection.findOne({
|
|
13
10
|
"properties.url": url,
|
|
14
11
|
});
|
|
15
|
-
} else {
|
|
16
|
-
// Get most recent published post awaiting syndication
|
|
17
|
-
const items = await postsCollection
|
|
18
|
-
.find({
|
|
19
|
-
"properties.mp-syndicate-to": {
|
|
20
|
-
$exists: true,
|
|
21
|
-
},
|
|
22
|
-
// BUG FIX: Removed "properties.syndication": { $exists: false }
|
|
23
|
-
// That filter skipped partially syndicated posts (e.g., posted to
|
|
24
|
-
// Mastodon but not yet to Bluesky). syndicateToTargets() already
|
|
25
|
-
// calls hasSyndicationUrl() to skip completed targets.
|
|
26
|
-
"properties.post-status": {
|
|
27
|
-
$ne: "draft",
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
// eslint-disable-next-line unicorn/no-array-sort
|
|
31
|
-
.sort({ "properties.published": -1 })
|
|
32
|
-
.limit(1)
|
|
33
|
-
.toArray();
|
|
34
|
-
postData = items[0];
|
|
35
12
|
}
|
|
36
13
|
|
|
37
|
-
|
|
14
|
+
// A post is awaiting syndication while `mp-syndicate-to` remains: it is
|
|
15
|
+
// deleted once every target has returned a URL, and replaced with the
|
|
16
|
+
// targets that failed otherwise. Posts already syndicated to some of their
|
|
17
|
+
// targets are therefore still awaiting the rest, and `syndicateToTargets`
|
|
18
|
+
// skips the ones already done.
|
|
19
|
+
const items = await postsCollection
|
|
20
|
+
.find({
|
|
21
|
+
"properties.mp-syndicate-to": {
|
|
22
|
+
$exists: true,
|
|
23
|
+
},
|
|
24
|
+
"properties.post-status": {
|
|
25
|
+
$ne: "draft",
|
|
26
|
+
},
|
|
27
|
+
})
|
|
28
|
+
.sort({ "properties.published": -1 })
|
|
29
|
+
.limit(1)
|
|
30
|
+
.toArray();
|
|
31
|
+
|
|
32
|
+
return items[0];
|
|
38
33
|
};
|
|
39
34
|
|
|
40
35
|
/**
|
|
@@ -53,7 +48,7 @@ export const getAllPostData = async (postsCollection) => {
|
|
|
53
48
|
$ne: "draft",
|
|
54
49
|
},
|
|
55
50
|
})
|
|
56
|
-
|
|
51
|
+
|
|
57
52
|
.sort({ "properties.published": -1 })
|
|
58
53
|
.toArray();
|
|
59
54
|
|
|
@@ -136,7 +131,9 @@ export const syndicateToTargets = async (
|
|
|
136
131
|
syndicateTo = syndicationTargets
|
|
137
132
|
.filter((t) => {
|
|
138
133
|
try {
|
|
139
|
-
return
|
|
134
|
+
return (
|
|
135
|
+
t?.info?.uid && existingOrigins.has(new URL(t.info.uid).origin)
|
|
136
|
+
);
|
|
140
137
|
} catch {
|
|
141
138
|
return false;
|
|
142
139
|
}
|
|
@@ -228,7 +225,6 @@ export const syndicateToTargets = async (
|
|
|
228
225
|
*
|
|
229
226
|
* Date-based: /type/YYYY/MM/DD/slug → YYYY-MM-DD-slug
|
|
230
227
|
* Non-date: /about/ → about
|
|
231
|
-
*
|
|
232
228
|
* @param {string} postUrl - Full post URL
|
|
233
229
|
* @returns {string|null} OG slug or null
|
|
234
230
|
*/
|
|
@@ -240,22 +236,46 @@ export function urlToOgSlug(postUrl) {
|
|
|
240
236
|
// Date-based URL: /type/yyyy/MM/dd/slug → 5 segments
|
|
241
237
|
if (segments.length === 5) {
|
|
242
238
|
const [, year, month, day, slug] = segments;
|
|
243
|
-
if (
|
|
239
|
+
if (
|
|
240
|
+
/^\d{4}$/.test(year) &&
|
|
241
|
+
/^\d{2}$/.test(month) &&
|
|
242
|
+
/^\d{2}$/.test(day)
|
|
243
|
+
) {
|
|
244
244
|
return `${year}-${month}-${day}-${slug}`;
|
|
245
245
|
}
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
// Fallback: last segment
|
|
249
|
-
return segments
|
|
249
|
+
return segments.at(-1);
|
|
250
250
|
} catch {
|
|
251
|
-
return
|
|
251
|
+
return;
|
|
252
252
|
}
|
|
253
253
|
}
|
|
254
254
|
|
|
255
|
+
/**
|
|
256
|
+
* HEAD a URL and report its status, 0 on network error or timeout
|
|
257
|
+
* @param {string} url - URL to check
|
|
258
|
+
* @returns {Promise<number>} HTTP status
|
|
259
|
+
*/
|
|
260
|
+
const headCheck = async (url) => {
|
|
261
|
+
try {
|
|
262
|
+
const controller = new AbortController();
|
|
263
|
+
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
264
|
+
const response = await fetch(url, {
|
|
265
|
+
method: "HEAD",
|
|
266
|
+
redirect: "follow",
|
|
267
|
+
signal: controller.signal,
|
|
268
|
+
});
|
|
269
|
+
clearTimeout(timeoutId);
|
|
270
|
+
return response.status;
|
|
271
|
+
} catch {
|
|
272
|
+
return 0;
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
255
276
|
/**
|
|
256
277
|
* Check if a post and its OG image are live on the public site.
|
|
257
278
|
* Uses HTTP HEAD requests with a 5-second timeout.
|
|
258
|
-
*
|
|
259
279
|
* @param {string} postUrl - Full public URL of the post
|
|
260
280
|
* @param {string} me - Publication URL (e.g., "https://rmendes.net")
|
|
261
281
|
* @param {object} [options] - Options
|
|
@@ -267,23 +287,8 @@ export async function isPostReady(postUrl, me, options = {}) {
|
|
|
267
287
|
const { skipOg = false } = options;
|
|
268
288
|
const ogSlug = urlToOgSlug(postUrl);
|
|
269
289
|
const meNorm = me?.replace(/\/$/, "");
|
|
270
|
-
const ogUrl =
|
|
271
|
-
|
|
272
|
-
const headCheck = async (url) => {
|
|
273
|
-
try {
|
|
274
|
-
const controller = new AbortController();
|
|
275
|
-
const timeoutId = setTimeout(() => controller.abort(), 5000);
|
|
276
|
-
const response = await fetch(url, {
|
|
277
|
-
method: "HEAD",
|
|
278
|
-
redirect: "follow",
|
|
279
|
-
signal: controller.signal,
|
|
280
|
-
});
|
|
281
|
-
clearTimeout(timeoutId);
|
|
282
|
-
return response.status;
|
|
283
|
-
} catch {
|
|
284
|
-
return 0;
|
|
285
|
-
}
|
|
286
|
-
};
|
|
290
|
+
const ogUrl =
|
|
291
|
+
!skipOg && ogSlug && meNorm ? `${meNorm}/og/${ogSlug}.png` : undefined;
|
|
287
292
|
|
|
288
293
|
const [postStatus, ogStatus] = await Promise.all([
|
|
289
294
|
headCheck(postUrl),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rmdes/indiekit-endpoint-syndicate",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.40",
|
|
4
4
|
"description": "Syndication endpoint for Indiekit. Fork of @indiekit/endpoint-syndicate with batch syndication support and bug fixes.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"indiekit",
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
},
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"engines": {
|
|
18
|
-
"node": ">=
|
|
18
|
+
"node": ">=24.17"
|
|
19
19
|
},
|
|
20
20
|
"type": "module",
|
|
21
21
|
"main": "index.js",
|
|
@@ -28,9 +28,9 @@
|
|
|
28
28
|
"url": "https://github.com/rmdes/indiekit-endpoint-syndicate.git"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@indiekit/error": "^1.0.0-beta.
|
|
32
|
-
"express": "^5.
|
|
33
|
-
"jsonwebtoken": "^9.0.
|
|
31
|
+
"@indiekit/error": "^1.0.0-beta.28",
|
|
32
|
+
"express": "^5.2.1",
|
|
33
|
+
"jsonwebtoken": "^9.0.3"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
36
|
"@indiekit/indiekit": ">=1.0.0-beta.25"
|