@rmdes/indiekit-endpoint-syndicate 1.0.0-beta.45 → 1.0.0-beta.48

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 (2) hide show
  1. package/lib/utils.js +40 -40
  2. package/package.json +3 -3
package/lib/utils.js CHANGED
@@ -1,23 +1,18 @@
1
1
  /**
2
2
  * Get post data for a single post
3
+ * Get every post awaiting syndication
4
+ *
5
+ * A post is awaiting syndication while `mp-syndicate-to` remains: it is
6
+ * deleted once every target has returned a URL, and replaced with the
7
+ * targets that failed otherwise. Posts already syndicated to some of their
8
+ * targets are therefore still awaiting the rest, and `syndicateToTargets`
9
+ * skips the ones already done. Oldest first, so a backlog is syndicated in
10
+ * the order it was published.
3
11
  * @param {object} postsCollection - Posts database collection
4
- * @param {string} url - URL of existing post (optional)
5
- * @returns {Promise<object>} Post data for given URL else oldest post awaiting syndication
12
+ * @returns {Promise<Array<object>>} Post data, oldest first
6
13
  */
7
- export const getPostData = async (postsCollection, url) => {
8
- if (url) {
9
- return postsCollection.findOne({
10
- "properties.url": url,
11
- });
12
- }
13
-
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. Oldest first, so a backlog is syndicated in
19
- // the order it was published.
20
- const items = await postsCollection
14
+ export const getPostsAwaitingSyndication = async (postsCollection) => {
15
+ return postsCollection
21
16
  .find({
22
17
  "properties.mp-syndicate-to": {
23
18
  $exists: true,
@@ -27,35 +22,39 @@ export const getPostData = async (postsCollection, url) => {
27
22
  },
28
23
  })
29
24
  .sort({ "properties.published": 1 })
30
- .limit(1)
31
25
  .toArray();
32
-
33
- return items[0];
34
26
  };
35
27
 
36
28
  /**
37
- * Get ALL posts awaiting syndication (batch mode)
29
+ * Get post data
38
30
  * @param {object} postsCollection - Posts database collection
39
- * @returns {Promise<Array>} Array of post data objects
31
+ * @param {string} url - URL of existing post (optional)
32
+ * @returns {Promise<object>} Post data for given URL else oldest post awaiting syndication
40
33
  */
41
- export const getAllPostData = async (postsCollection) => {
42
- const items = await postsCollection
43
- .find({
44
- "properties.mp-syndicate-to": {
45
- $exists: true,
46
- },
47
- // No syndication filter — let syndicateToTargets() handle dedup
48
- "properties.post-status": {
49
- $ne: "draft",
50
- },
51
- })
52
-
53
- .sort({ "properties.published": -1 })
54
- .toArray();
34
+ export const getPostData = async (postsCollection, url) => {
35
+ if (url) {
36
+ return postsCollection.findOne({
37
+ "properties.url": url,
38
+ });
39
+ }
55
40
 
56
- return items;
41
+ const [oldest] = await getPostsAwaitingSyndication(postsCollection);
42
+ return oldest;
57
43
  };
58
44
 
45
+ /**
46
+ * Get ALL posts awaiting syndication (batch mode)
47
+ *
48
+ * This was a second copy of `getPostsAwaitingSyndication`'s query that differed
49
+ * only in sorting `published: -1`, so a backlog went out newest first and
50
+ * arrived on the timeline in reverse. The duplicate is gone: both callers now
51
+ * share one query, oldest first, which is what the comment above already
52
+ * claimed and what upstream's batch path does.
53
+ * @param {object} postsCollection - Posts database collection
54
+ * @returns {Promise<Array<object>>} Post data, oldest first
55
+ */
56
+ export const getAllPostData = getPostsAwaitingSyndication;
57
+
59
58
  /**
60
59
  * Check if target already returned a syndication URL
61
60
  * @param {Array} syndicatedUrls - Syndication URLs
@@ -221,7 +220,6 @@ export const syndicateToTargets = async (
221
220
  };
222
221
  };
223
222
 
224
-
225
223
  /**
226
224
  * HEAD a URL and report its status, 0 on network error or timeout
227
225
  * @param {string} url - URL to check
@@ -246,13 +244,16 @@ const headCheck = async (url) => {
246
244
  /**
247
245
  * GET a page and return its status plus the og:image it declares.
248
246
  * @param {string} url - Page URL
249
- * @returns {Promise<{status: number, ogImage: string|undefined}>}
247
+ * @returns {Promise<{status: number, ogImage?: string}>}
250
248
  */
251
249
  const fetchOgImage = async (url) => {
252
250
  try {
253
251
  const controller = new AbortController();
254
252
  const timeoutId = setTimeout(() => controller.abort(), 5000);
255
- const response = await fetch(url, { redirect: "follow", signal: controller.signal });
253
+ const response = await fetch(url, {
254
+ redirect: "follow",
255
+ signal: controller.signal,
256
+ });
256
257
  clearTimeout(timeoutId);
257
258
  if (response.status !== 200) return { status: response.status };
258
259
  const html = await response.text();
@@ -301,4 +302,3 @@ export async function isPostReady(postUrl, me, options = {}) {
301
302
  const ogStatus = await headCheck(ogImage);
302
303
  return { ready: ogStatus === 200, postStatus, ogStatus };
303
304
  }
304
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rmdes/indiekit-endpoint-syndicate",
3
- "version": "1.0.0-beta.45",
3
+ "version": "1.0.0-beta.48",
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",
@@ -29,13 +29,13 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@indiekit/error": "^1.0.0-beta.28",
32
- "express": "^5.2.1",
32
+ "express": "^5.3.0",
33
33
  "jsonwebtoken": "^9.0.3"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },
38
38
  "scripts": {
39
- "test": "node --test test/*.js"
39
+ "test": "node --test \"tests/**/*.test.js\""
40
40
  }
41
41
  }