pups-client 0.5.0 → 0.6.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.
- package/README.md +2 -2
- package/dist/feed.d.ts +25 -9
- package/dist/feed.d.ts.map +1 -1
- package/dist/feed.js +92 -40
- package/dist/feed.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mentions.d.ts +2 -2
- package/dist/mentions.d.ts.map +1 -1
- package/dist/mentions.js +7 -5
- package/dist/mentions.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/feed.ts +111 -45
- package/src/index.ts +3 -1
- package/src/mentions.ts +9 -5
- package/src/version.ts +1 -1
package/src/feed.ts
CHANGED
|
@@ -19,6 +19,9 @@ export const POST_URL_TEMPLATE = 'https://bigwide.net/ns/post-url-template'
|
|
|
19
19
|
/** The `about` of the `_pups` object: RFC-7. Not its final location; readers must not rely on it. */
|
|
20
20
|
export const PUPS_FEED_SPEC = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-7-Feed-Publishing-with-PUPS.md'
|
|
21
21
|
|
|
22
|
+
/** The `about` of the `_archive` object: RFC-13 (JSON Feed Archives). Not its final location either. */
|
|
23
|
+
export const ARCHIVE_SPEC = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-13-JSON-Feed-Archives.md'
|
|
24
|
+
|
|
22
25
|
const RFC1_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-1-Source-for-JSON-Feeds.md'
|
|
23
26
|
const RFC4_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-4-Feed-Submissions.md'
|
|
24
27
|
const JSON_FEED_VERSION = 'https://jsonfeed.org/version/1.1'
|
|
@@ -27,9 +30,9 @@ const maxArchiveNumber = 999_999
|
|
|
27
30
|
/** The feed file keeps at least this many items, also when `maxBytes` is exceeded (RFC-7 "Size in bytes"). */
|
|
28
31
|
const minFeedItems = 10
|
|
29
32
|
|
|
30
|
-
const mainComment = `This feed is maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. It contains the newest items. If it has
|
|
33
|
+
const mainComment = `This feed is maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. It contains the newest items. If it has _archive.archive_url, all items are in the archives, starting with the one there.`
|
|
31
34
|
const archiveComment = (feedUrl: string) =>
|
|
32
|
-
`This is an archive of ${feedUrl}, maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. New items appear in the feed at
|
|
35
|
+
`This is an archive of ${feedUrl}, maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. New items appear in the feed at _archive.updates_url, older ones are in the archive at next_url.`
|
|
33
36
|
|
|
34
37
|
/** Feed names (RFC-7 "Named feeds"): 1 to 40 lowercase letters, digits and `-`, not starting or ending with `-`. */
|
|
35
38
|
const feedNamePattern = /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/
|
|
@@ -38,7 +41,7 @@ const feedNamePattern = /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/
|
|
|
38
41
|
const mainMediaFolders = ['images', 'media']
|
|
39
42
|
|
|
40
43
|
/** Top-level fields that are fixed or managed by RFC-7, and can't be set through `create()` or `updateSettings()`. */
|
|
41
|
-
const reservedFields = new Set(['version', 'home_page_url', 'feed_url', 'user_comment', 'next_url', 'items', 'hubs', '_pups', 'icon', 'favicon'])
|
|
44
|
+
const reservedFields = new Set(['version', 'home_page_url', 'feed_url', 'user_comment', 'next_url', 'items', 'hubs', '_pups', '_archive', 'icon', 'favicon'])
|
|
42
45
|
|
|
43
46
|
export interface FeedAuthor {
|
|
44
47
|
name?: string
|
|
@@ -73,28 +76,43 @@ export interface FeedItem {
|
|
|
73
76
|
tags?: string[]
|
|
74
77
|
language?: string
|
|
75
78
|
attachments?: FeedAttachment[]
|
|
76
|
-
/** The item's `
|
|
77
|
-
|
|
79
|
+
/** The item's `_archive` object, with its index (RFC-13 "Indexes"). */
|
|
80
|
+
_archive?: ItemArchive
|
|
78
81
|
[field: string]: unknown
|
|
79
82
|
}
|
|
80
83
|
|
|
81
|
-
/** The `
|
|
82
|
-
export interface
|
|
83
|
-
/** The item's index: 0 for a feed's first item, then counting up; never changed, never used again (RFC-
|
|
84
|
+
/** The `_archive` object of an item (RFC-13). */
|
|
85
|
+
export interface ItemArchive {
|
|
86
|
+
/** The item's index: 0 for a feed's first item, then counting up; never changed, never used again (RFC-13 "Indexes"). */
|
|
84
87
|
index?: number
|
|
85
88
|
[field: string]: unknown
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
/** The `_pups`
|
|
91
|
+
/** @deprecated The index moved from `_pups` to `_archive` (RFC-13): use ItemArchive. */
|
|
92
|
+
export type ItemMarker = ItemArchive
|
|
93
|
+
|
|
94
|
+
/** The `_pups` object of a feed file or archive: the marker of RFC-7. */
|
|
89
95
|
export interface PupsMarker {
|
|
90
96
|
about?: string
|
|
91
97
|
pupsControlled?: boolean
|
|
98
|
+
[field: string]: unknown
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The `_archive` object of a feed file or archive (RFC-13). */
|
|
102
|
+
export interface ArchiveFields {
|
|
103
|
+
about?: string
|
|
92
104
|
/** In the feed file: the public URL of the newest archive. */
|
|
93
105
|
archive_url?: string
|
|
106
|
+
/** In the feed file: the URL of every archive, with `{number}` for its six-digit number. */
|
|
107
|
+
archive_template?: string
|
|
108
|
+
/** In the feed file: the most items an archive holds. */
|
|
109
|
+
archive_size?: number
|
|
94
110
|
/** In an archive: the public URL of the feed file, which readers poll. */
|
|
95
111
|
updates_url?: string
|
|
112
|
+
/** In an archive: its number, from 0 for the oldest. */
|
|
113
|
+
number?: number
|
|
96
114
|
/**
|
|
97
|
-
* The index the next new item gets (RFC-
|
|
115
|
+
* The index the next new item gets (RFC-13 "Indexes"). Valid in the newest archive, or in the feed file of a feed
|
|
98
116
|
* without archives.
|
|
99
117
|
*/
|
|
100
118
|
next_index?: number
|
|
@@ -118,6 +136,7 @@ export interface FeedDocument {
|
|
|
118
136
|
hubs?: unknown[]
|
|
119
137
|
items: FeedItem[]
|
|
120
138
|
_pups?: PupsMarker
|
|
139
|
+
_archive?: ArchiveFields
|
|
121
140
|
[field: string]: unknown
|
|
122
141
|
}
|
|
123
142
|
|
|
@@ -343,13 +362,42 @@ function mergeItems(existing: FeedItem[], incoming: FeedItem[]): FeedItem[] {
|
|
|
343
362
|
return [...incoming.filter(item => !present.has(item.id)), ...existing.map(item => byId.get(item.id) ?? item)]
|
|
344
363
|
}
|
|
345
364
|
|
|
346
|
-
/** The index of an item (RFC-
|
|
365
|
+
/** The index of an item (RFC-13 "Indexes"), also from `_pups` of earlier drafts, or undefined if it has none. */
|
|
347
366
|
export function itemIndex(item: FeedItem): number | undefined {
|
|
348
|
-
const index = isObject(item._pups) ? item._pups.index : undefined
|
|
367
|
+
const index = isObject(item._archive) && item._archive.index !== undefined ? item._archive.index : isObject(item._pups) ? item._pups.index : undefined
|
|
349
368
|
return typeof index == 'number' && Number.isSafeInteger(index) && index >= 0 ? index : undefined
|
|
350
369
|
}
|
|
351
370
|
|
|
352
|
-
const withIndex = (item: FeedItem, index: number): FeedItem => ({ ...item,
|
|
371
|
+
const withIndex = (item: FeedItem, index: number): FeedItem => ({ ...item, _archive: { ...isObject(item._archive) ? item._archive : {}, index } })
|
|
372
|
+
|
|
373
|
+
/** The fields of RFC-13 that earlier drafts of RFC-7 kept in `_pups`. */
|
|
374
|
+
const legacyArchiveFields = ['archive_url', 'updates_url', 'next_index'] as const
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Moves the fields that earlier drafts kept in `_pups` into `_archive` (RFC-13 "Earlier drafts"), in the file and its
|
|
378
|
+
* items, where `_archive` doesn't have them already. Changes `doc`.
|
|
379
|
+
*/
|
|
380
|
+
function upgradeArchiveFields(doc: FeedDocument): FeedDocument {
|
|
381
|
+
const move = (from: Record<string, unknown>, to: Record<string, unknown>, keys: readonly string[]) => {
|
|
382
|
+
for (const key of keys) if (key in from) {
|
|
383
|
+
if (to[key] === undefined) to[key] = from[key]
|
|
384
|
+
delete from[key]
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
if (isObject(doc._pups) && legacyArchiveFields.some(key => key in doc._pups!)) {
|
|
388
|
+
const archive: ArchiveFields = { about: ARCHIVE_SPEC, ...isObject(doc._archive) ? doc._archive : {} }
|
|
389
|
+
move(doc._pups, archive, legacyArchiveFields)
|
|
390
|
+
doc._archive = archive
|
|
391
|
+
}
|
|
392
|
+
for (const item of doc.items)
|
|
393
|
+
if (isObject(item._pups) && 'index' in item._pups) {
|
|
394
|
+
const archive: ItemArchive = isObject(item._archive) ? item._archive : {}
|
|
395
|
+
move(item._pups, archive, ['index'])
|
|
396
|
+
item._archive = archive
|
|
397
|
+
if (!Object.keys(item._pups).length) delete item._pups
|
|
398
|
+
}
|
|
399
|
+
return doc
|
|
400
|
+
}
|
|
353
401
|
|
|
354
402
|
/** A post URL template (RFC-6 "Post URLs"): an https URL with `{index}` or `{id}`. */
|
|
355
403
|
function validTemplate(template: unknown): template is string {
|
|
@@ -361,9 +409,9 @@ function validTemplate(template: unknown): template is string {
|
|
|
361
409
|
}
|
|
362
410
|
}
|
|
363
411
|
|
|
364
|
-
/** The counter `
|
|
412
|
+
/** The counter `_archive.next_index` of a file, if it has a valid one. */
|
|
365
413
|
function counterOf(doc: FeedDocument): number | undefined {
|
|
366
|
-
const next = doc.
|
|
414
|
+
const next = doc._archive?.next_index
|
|
367
415
|
return typeof next == 'number' && Number.isSafeInteger(next) && next >= 0 ? next : undefined
|
|
368
416
|
}
|
|
369
417
|
|
|
@@ -653,21 +701,21 @@ export class PupsFeed {
|
|
|
653
701
|
if (firstAttempt && main.doc.items.some(i => i.id == item.id))
|
|
654
702
|
throw new PupsError('invalid_request', `The feed already has an item with the id "${item.id}".`)
|
|
655
703
|
firstAttempt = false
|
|
656
|
-
const doc: FeedDocument = { ...main.doc,
|
|
657
|
-
const hasArchives = this.#archiveNumber(doc.
|
|
704
|
+
const doc: FeedDocument = { ...main.doc, _archive: { ...main.doc._archive } }
|
|
705
|
+
const hasArchives = this.#archiveNumber(doc._archive!.archive_url) !== undefined
|
|
658
706
|
if (!hasArchives && !this.#archivesWanted) {
|
|
659
707
|
// A feed without archives: the feed file is all there is, and holds the counter (RFC-7 "Posting").
|
|
660
708
|
archiveUrl = this.url
|
|
661
709
|
const indexed = indexItems(doc.items, nextFree(doc.items, counterOf(doc)))
|
|
662
710
|
stored = withIndex(item, indexed.next)
|
|
663
|
-
doc.
|
|
711
|
+
doc._archive!.next_index = indexed.next + 1
|
|
664
712
|
await this.#writeMain({ ...doc, items: this.#newestItems([stored], indexed.items, doc) }, main.etag, signal)
|
|
665
713
|
return
|
|
666
714
|
}
|
|
667
715
|
if (!hasArchives) {
|
|
668
716
|
const started = await this.#startArchives(main, signal)
|
|
669
717
|
if (started) {
|
|
670
|
-
doc.
|
|
718
|
+
doc._archive!.archive_url = started.url
|
|
671
719
|
doc.items = started.items
|
|
672
720
|
}
|
|
673
721
|
}
|
|
@@ -675,12 +723,12 @@ export class PupsFeed {
|
|
|
675
723
|
const added = await this.#addToArchive(main, doc, item, signal, template)
|
|
676
724
|
archiveUrl = added.url
|
|
677
725
|
stored = added.item
|
|
678
|
-
doc.
|
|
726
|
+
doc._archive!.archive_url = added.url
|
|
679
727
|
if (!hasArchives) {
|
|
680
728
|
// The old chain of a handed-over feed now continues behind the oldest archive, and the newest archive
|
|
681
729
|
// holds the counter.
|
|
682
730
|
delete doc.next_url
|
|
683
|
-
delete doc.
|
|
731
|
+
delete doc._archive!.next_index
|
|
684
732
|
}
|
|
685
733
|
doc.items = this.#newestItems(added.recent, doc.items, doc)
|
|
686
734
|
await this.#writeMain(doc, main.etag, signal)
|
|
@@ -697,11 +745,11 @@ export class PupsFeed {
|
|
|
697
745
|
const signal = options.signal
|
|
698
746
|
await this.#transaction(async () => {
|
|
699
747
|
const main = await this.#readControlled(this.path, signal)
|
|
700
|
-
const newest = this.#archiveNumber(main.doc.
|
|
748
|
+
const newest = this.#archiveNumber(main.doc._archive?.archive_url)
|
|
701
749
|
if (newest === undefined) {
|
|
702
750
|
const indexed = indexItems(main.doc.items, nextFree(main.doc.items, counterOf(main.doc)))
|
|
703
751
|
if (indexed.changed || counterOf(main.doc) !== indexed.next)
|
|
704
|
-
await this.#writeMain({ ...main.doc, items: indexed.items,
|
|
752
|
+
await this.#writeMain({ ...main.doc, items: indexed.items, _archive: { ...main.doc._archive, next_index: indexed.next } }, main.etag, signal)
|
|
705
753
|
return
|
|
706
754
|
}
|
|
707
755
|
const archive = await this.#readArchive(this.#archivePath(newest), signal)
|
|
@@ -721,7 +769,7 @@ export class PupsFeed {
|
|
|
721
769
|
}
|
|
722
770
|
|
|
723
771
|
/**
|
|
724
|
-
* All posts, newest first: the archives from `
|
|
772
|
+
* All posts, newest first: the archives from `_archive.archive_url` along `next_url` (for a feed without archives,
|
|
725
773
|
* the feed file and its `next_url` chain). Each post is returned once.
|
|
726
774
|
*/
|
|
727
775
|
async *posts(options: Signal = {}): AsyncGenerator<FeedItem> {
|
|
@@ -762,7 +810,7 @@ export class PupsFeed {
|
|
|
762
810
|
// The index never changes (RFC-7 "Indexes").
|
|
763
811
|
if (oldIndex !== undefined) item = withIndex(item, oldIndex)
|
|
764
812
|
// A post of a feed with archives that never had a url may get one (RFC-7 "Post URLs").
|
|
765
|
-
if (oldIndex !== undefined && !old.url && !item.url && this.#archiveNumber(main.doc.
|
|
813
|
+
if (oldIndex !== undefined && !old.url && !item.url && this.#archiveNumber(main.doc._archive?.archive_url) !== undefined)
|
|
766
814
|
item = this.#withPostUrl(item, oldIndex, await this.#postUrlTemplate(signal))
|
|
767
815
|
if (file != main) await this.#writeArchive(file, replaceAt(file.doc.items, index, item), signal)
|
|
768
816
|
const mainIndex = main.doc.items.findIndex(i => i.id == id)
|
|
@@ -904,7 +952,7 @@ export class PupsFeed {
|
|
|
904
952
|
// Not JSON: handled below.
|
|
905
953
|
}
|
|
906
954
|
if (!isFeedDocument(doc)) throw new PupsError('invalid_feed', `${path} is not a JSON Feed.`)
|
|
907
|
-
return { path, doc, etag: file.etag }
|
|
955
|
+
return { path, doc: upgradeArchiveFields(doc), etag: file.etag }
|
|
908
956
|
}
|
|
909
957
|
|
|
910
958
|
/** Reads a file that must exist and carry the `_pups` marker. */
|
|
@@ -917,13 +965,13 @@ export class PupsFeed {
|
|
|
917
965
|
}
|
|
918
966
|
|
|
919
967
|
/**
|
|
920
|
-
* The files that hold all items: the archives from `
|
|
968
|
+
* The files that hold all items: the archives from `_archive.archive_url` along `next_url`, as far as they are files
|
|
921
969
|
* on this server. A feed without archives yet (handed over, not posted to) holds its items in the feed file and
|
|
922
970
|
* the pages its `next_url` names.
|
|
923
971
|
*/
|
|
924
972
|
async *#chain(main: Loaded, signal?: AbortSignal): AsyncGenerator<Loaded> {
|
|
925
973
|
const visited = new Set<string>([main.path])
|
|
926
|
-
const archive = main.doc.
|
|
974
|
+
const archive = main.doc._archive?.archive_url
|
|
927
975
|
let file: Loaded | undefined = main
|
|
928
976
|
let next = archive && this.#archiveNumber(archive) !== undefined ? archive : undefined
|
|
929
977
|
if (next) file = undefined
|
|
@@ -974,8 +1022,8 @@ export class PupsFeed {
|
|
|
974
1022
|
|
|
975
1023
|
/**
|
|
976
1024
|
* Writes the feed file. Sets the fixed fields, so that a feed the owner handed over gets them on its first write,
|
|
977
|
-
* and upgrades JSON Feed 1.0 (RFC-7 "Handing over an existing feed").
|
|
978
|
-
*
|
|
1025
|
+
* and upgrades JSON Feed 1.0 (RFC-7 "Handing over an existing feed"). A feed with archives gets the archive template
|
|
1026
|
+
* and size of RFC-13, and its optional `next_url` is kept equal to `archive_url` (RFC-7 "Reading").
|
|
979
1027
|
*/
|
|
980
1028
|
async #writeMain(doc: FeedDocument, etag: string, signal?: AbortSignal): Promise<FeedDocument> {
|
|
981
1029
|
const { author, ...rest } = doc
|
|
@@ -987,14 +1035,25 @@ export class PupsFeed {
|
|
|
987
1035
|
user_comment: mainComment,
|
|
988
1036
|
}
|
|
989
1037
|
if (author !== undefined && fixed.authors === undefined) fixed.authors = [author as FeedAuthor]
|
|
990
|
-
|
|
1038
|
+
const archiveUrl = fixed._archive?.archive_url
|
|
1039
|
+
if (fixed._archive) fixed._archive = { about: ARCHIVE_SPEC, ...fixed._archive }
|
|
1040
|
+
if (this.#archiveNumber(archiveUrl) !== undefined) {
|
|
1041
|
+
fixed._archive = { ...fixed._archive, archive_template: this.#archiveTemplate(), archive_size: this.#archiveSize }
|
|
1042
|
+
if (fixed.next_url !== undefined) fixed.next_url = archiveUrl
|
|
1043
|
+
}
|
|
991
1044
|
await this.#session.replace(this.path, serialize(fixed), etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
992
1045
|
return fixed
|
|
993
1046
|
}
|
|
994
1047
|
|
|
995
|
-
/**
|
|
1048
|
+
/**
|
|
1049
|
+
* Writes an archive with `items`, and with the counter `next_index` if given. An archive of an earlier draft gets its
|
|
1050
|
+
* `_archive` fields and comment of RFC-13 (`number`, `updates_url`).
|
|
1051
|
+
*/
|
|
996
1052
|
async #writeArchive(archive: Loaded, items: FeedItem[], signal?: AbortSignal, nextIndex?: number) {
|
|
997
|
-
const
|
|
1053
|
+
const n = this.#archiveNumber(this.#session.publicUrl(archive.path))
|
|
1054
|
+
const fields: ArchiveFields = { about: ARCHIVE_SPEC, ...archive.doc._archive, updates_url: this.url, ...n === undefined ? {} : { number: n } }
|
|
1055
|
+
if (nextIndex !== undefined) fields.next_index = nextIndex
|
|
1056
|
+
const doc: FeedDocument = { ...archive.doc, user_comment: archiveComment(this.url), items, _archive: fields }
|
|
998
1057
|
await this.#session.replace(archive.path, serialize(doc), archive.etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
999
1058
|
}
|
|
1000
1059
|
|
|
@@ -1021,6 +1080,11 @@ export class PupsFeed {
|
|
|
1021
1080
|
return this.#session.publicUrl(this.#archivePath(n))
|
|
1022
1081
|
}
|
|
1023
1082
|
|
|
1083
|
+
/** `_archive.archive_template` (RFC-13): the archive URL with `{number}` for the number. */
|
|
1084
|
+
#archiveTemplate() {
|
|
1085
|
+
return this.#archiveUrl(0).replace(/000000\.json$/, '{number}.json')
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1024
1088
|
/** The number of the archive at `url`, or undefined if `url` is not an archive file of this feed. */
|
|
1025
1089
|
#archiveNumber(url: string | undefined): number | undefined {
|
|
1026
1090
|
const path = url && pathFromPublicUrl(this.#session.server, url)
|
|
@@ -1030,12 +1094,11 @@ export class PupsFeed {
|
|
|
1030
1094
|
}
|
|
1031
1095
|
|
|
1032
1096
|
/**
|
|
1033
|
-
* A new archive with the feed fields of the feed file (RFC-
|
|
1034
|
-
* accepts comments is said by its feed file alone.
|
|
1097
|
+
* A new archive with the feed fields of the feed file (RFC-13 "Feed fields of an archive"), except `_submissions`:
|
|
1098
|
+
* whether a feed accepts comments is said by its feed file alone.
|
|
1035
1099
|
*/
|
|
1036
1100
|
#newArchive(n: number, main: FeedDocument, items: FeedItem[], nextUrl: string | undefined, nextIndex?: number): FeedDocument {
|
|
1037
|
-
const { items: _items, next_url: _next, user_comment: _comment, hubs: _hubs, author: _author, _pups, _submissions: _s, ...fields } = main
|
|
1038
|
-
const { archive_url: _archive, next_index: _counter, ...marker } = _pups ?? {}
|
|
1101
|
+
const { items: _items, next_url: _next, user_comment: _comment, hubs: _hubs, author: _author, _pups, _archive, _submissions: _s, ...fields } = main
|
|
1039
1102
|
const podcast = fields._podcast
|
|
1040
1103
|
if (isObject(podcast) && 'liveItems' in podcast) {
|
|
1041
1104
|
const { liveItems: _live, ...rest } = podcast
|
|
@@ -1049,7 +1112,8 @@ export class PupsFeed {
|
|
|
1049
1112
|
user_comment: archiveComment(this.url),
|
|
1050
1113
|
...nextUrl ? { next_url: nextUrl } : {},
|
|
1051
1114
|
items,
|
|
1052
|
-
_pups: { about: PUPS_FEED_SPEC, ...
|
|
1115
|
+
_pups: { about: PUPS_FEED_SPEC, ..._pups, pupsControlled: true },
|
|
1116
|
+
_archive: { about: ARCHIVE_SPEC, updates_url: this.url, number: n, ...nextIndex === undefined ? {} : { next_index: nextIndex } },
|
|
1053
1117
|
}
|
|
1054
1118
|
}
|
|
1055
1119
|
|
|
@@ -1095,7 +1159,7 @@ export class PupsFeed {
|
|
|
1095
1159
|
const existing = await this.#readArchive(this.#archivePath(n), signal)
|
|
1096
1160
|
const ids = new Set(items.map(item => item.id))
|
|
1097
1161
|
if (!existing || existing == 'foreign' || !existing.doc.items.every(item => ids.has(item.id)))
|
|
1098
|
-
throw new PupsError('conflict', `${this.#archivePath(n)} exists already, but ${this.path} has no archive_url. Remove the file, or set
|
|
1162
|
+
throw new PupsError('conflict', `${this.#archivePath(n)} exists already, but ${this.path} has no archive_url. Remove the file, or set _archive.archive_url (RFC-7, Starting the archives).`)
|
|
1099
1163
|
await this.#writeArchive(existing, items, signal, nextIndex)
|
|
1100
1164
|
}
|
|
1101
1165
|
url = this.#archiveUrl(n)
|
|
@@ -1144,7 +1208,7 @@ export class PupsFeed {
|
|
|
1144
1208
|
*/
|
|
1145
1209
|
async #addToArchive(main: Loaded, doc: FeedDocument, item: FeedItem, signal?: AbortSignal, template?: string): Promise<{ url: string; recent: FeedItem[]; item: FeedItem }> {
|
|
1146
1210
|
const place = (index: number) => this.#withPostUrl(withIndex(item, index), index, template)
|
|
1147
|
-
const newest = this.#archiveNumber(doc.
|
|
1211
|
+
const newest = this.#archiveNumber(doc._archive?.archive_url)
|
|
1148
1212
|
let previous: FeedItem[] = []
|
|
1149
1213
|
// The index for the item if it goes into a new archive.
|
|
1150
1214
|
let next = nextFree(doc.items, newest === undefined ? counterOf(doc) : undefined)
|
|
@@ -1235,11 +1299,13 @@ export class PupsFeed {
|
|
|
1235
1299
|
if (markdown !== undefined) this.#setMarkdown(item, markdown)
|
|
1236
1300
|
if (item.content_html === undefined && item.content_text === undefined)
|
|
1237
1301
|
throw new PupsError('invalid_request', 'A post needs content_html, content_text or markdown (JSON Feed requires content).')
|
|
1238
|
-
// The index is given out when the item is added (RFC-
|
|
1239
|
-
|
|
1240
|
-
const
|
|
1241
|
-
if (
|
|
1242
|
-
|
|
1302
|
+
// The index is given out when the item is added (RFC-13 "Indexes").
|
|
1303
|
+
for (const key of ['_archive', '_pups'] as const) {
|
|
1304
|
+
const value = item[key]
|
|
1305
|
+
if (!isObject(value)) continue
|
|
1306
|
+
const { index: _index, ...rest } = value
|
|
1307
|
+
if (Object.keys(rest).length) item[key] = rest
|
|
1308
|
+
else delete item[key]
|
|
1243
1309
|
}
|
|
1244
1310
|
return item
|
|
1245
1311
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,8 +16,9 @@ export type {
|
|
|
16
16
|
} from './client.js'
|
|
17
17
|
export { PupsSession } from './session.js'
|
|
18
18
|
export type { Body, JsonBody, SessionChangeEvent, StoredFile, WriteOptions, WriteResult } from './session.js'
|
|
19
|
-
export { FEED_REL, POST_URL_TEMPLATE, PUPS_FEED_SPEC, PupsFeed, feedScopes, itemIndex } from './feed.js'
|
|
19
|
+
export { ARCHIVE_SPEC, FEED_REL, POST_URL_TEMPLATE, PUPS_FEED_SPEC, PupsFeed, feedScopes, itemIndex } from './feed.js'
|
|
20
20
|
export type {
|
|
21
|
+
ArchiveFields,
|
|
21
22
|
CommentPolicy,
|
|
22
23
|
CommentsState,
|
|
23
24
|
FeedAttachment,
|
|
@@ -30,6 +31,7 @@ export type {
|
|
|
30
31
|
FeedSettingsChanges,
|
|
31
32
|
FeedSetup,
|
|
32
33
|
FeedState,
|
|
34
|
+
ItemArchive,
|
|
33
35
|
ItemMarker,
|
|
34
36
|
MentionChoice,
|
|
35
37
|
MediaInput,
|
package/src/mentions.ts
CHANGED
|
@@ -151,6 +151,10 @@ function splitRef(ref: string): [string, string | undefined] {
|
|
|
151
151
|
return [ref.slice(0, at), id]
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/** A field of a feed's RFC-13 `_archive`, or of `_pups` in feeds of earlier drafts (RFC-13 "Earlier drafts"). */
|
|
155
|
+
const archiveField = (doc: Record<string, unknown>, key: string): unknown =>
|
|
156
|
+
(isObject(doc._archive) ? doc._archive[key] : undefined) ?? (isObject(doc._pups) ? doc._pups[key] : undefined)
|
|
157
|
+
|
|
154
158
|
/**
|
|
155
159
|
* Sends webmentions: RFC-11 mentions to the domains a post names or answers, found through their `host-meta.json`,
|
|
156
160
|
* and RFC-4 submissions to the endpoint a target feed names in `_submissions`. Plain Webmention, form-encoded.
|
|
@@ -363,16 +367,16 @@ export class Mentions {
|
|
|
363
367
|
}
|
|
364
368
|
|
|
365
369
|
/**
|
|
366
|
-
* Finds an item, given as feed URL plus `#<id>`, as RFC-4 does: in the feed, then along `next_url` (or RFC-
|
|
367
|
-
* `
|
|
370
|
+
* Finds an item, given as feed URL plus `#<id>`, as RFC-4 does: in the feed, then along `next_url` (or RFC-13's
|
|
371
|
+
* `_archive.archive_url`) on the same host, at most 10 pages. JSON Feed only.
|
|
368
372
|
* @throws {PupsError} `network`, `not_found` or `invalid_feed` if the first feed can't be read.
|
|
369
373
|
*/
|
|
370
374
|
async findItem(ref: string, options: { signal?: AbortSignal; item?: boolean } = {}): Promise<FoundItem> {
|
|
371
375
|
const [url, id] = splitRef(ref)
|
|
372
376
|
const first = await this.#readFeed(url, options.signal)
|
|
373
|
-
// An RFC-
|
|
377
|
+
// An RFC-13 archive takes its _submissions from its feed file (RFC-7 "Receiving comments").
|
|
374
378
|
let owner = first
|
|
375
|
-
const updates =
|
|
379
|
+
const updates = archiveField(first.doc, 'updates_url')
|
|
376
380
|
if (typeof updates == 'string') {
|
|
377
381
|
const feedUrl = new URL(updates, first.url)
|
|
378
382
|
owner = feedUrl.host == new URL(first.url).host ? await this.#readFeed(feedUrl.href, options.signal).catch(() => ({ url: feedUrl.href, doc: {} })) : { url: feedUrl.href, doc: {} }
|
|
@@ -390,7 +394,7 @@ export class Mentions {
|
|
|
390
394
|
const author = accountHost(socialspec(item)) ?? (submissions ? undefined : accountHost(socialspec(owner.doc)))
|
|
391
395
|
return { ...result, item, ...author ? { author } : {} }
|
|
392
396
|
}
|
|
393
|
-
const next: unknown = feed.doc.next_url ?? (
|
|
397
|
+
const next: unknown = feed.doc.next_url ?? archiveField(feed.doc, 'archive_url')
|
|
394
398
|
if (typeof next != 'string') break
|
|
395
399
|
const nextUrl: URL = new URL(next, feed.url)
|
|
396
400
|
if (nextUrl.host != new URL(url).host || seen.has(nextUrl.href)) break
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.6.0'
|