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

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 +32 -22
  2. package/package.json +2 -2
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,10 +22,24 @@ export const getPostData = async (postsCollection, url) => {
27
22
  },
28
23
  })
29
24
  .sort({ "properties.published": 1 })
30
- .limit(1)
31
25
  .toArray();
26
+ };
27
+
28
+ /**
29
+ * Get post data
30
+ * @param {object} postsCollection - Posts database collection
31
+ * @param {string} url - URL of existing post (optional)
32
+ * @returns {Promise<object>} Post data for given URL else oldest post awaiting syndication
33
+ */
34
+ export const getPostData = async (postsCollection, url) => {
35
+ if (url) {
36
+ return postsCollection.findOne({
37
+ "properties.url": url,
38
+ });
39
+ }
32
40
 
33
- return items[0];
41
+ const [oldest] = await getPostsAwaitingSyndication(postsCollection);
42
+ return oldest;
34
43
  };
35
44
 
36
45
  /**
@@ -221,7 +230,6 @@ export const syndicateToTargets = async (
221
230
  };
222
231
  };
223
232
 
224
-
225
233
  /**
226
234
  * HEAD a URL and report its status, 0 on network error or timeout
227
235
  * @param {string} url - URL to check
@@ -246,13 +254,16 @@ const headCheck = async (url) => {
246
254
  /**
247
255
  * GET a page and return its status plus the og:image it declares.
248
256
  * @param {string} url - Page URL
249
- * @returns {Promise<{status: number, ogImage: string|undefined}>}
257
+ * @returns {Promise<{status: number, ogImage?: string}>}
250
258
  */
251
259
  const fetchOgImage = async (url) => {
252
260
  try {
253
261
  const controller = new AbortController();
254
262
  const timeoutId = setTimeout(() => controller.abort(), 5000);
255
- const response = await fetch(url, { redirect: "follow", signal: controller.signal });
263
+ const response = await fetch(url, {
264
+ redirect: "follow",
265
+ signal: controller.signal,
266
+ });
256
267
  clearTimeout(timeoutId);
257
268
  if (response.status !== 200) return { status: response.status };
258
269
  const html = await response.text();
@@ -301,4 +312,3 @@ export async function isPostReady(postUrl, me, options = {}) {
301
312
  const ogStatus = await headCheck(ogImage);
302
313
  return { ready: ogStatus === 200, postStatus, ogStatus };
303
314
  }
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.46",
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,7 +29,7 @@
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": {