forked from johnfactotum/foliate-js
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathopds.js
More file actions
400 lines (358 loc) · 15.7 KB
/
opds.js
File metadata and controls
400 lines (358 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
const NS = {
ATOM: 'http://www.w3.org/2005/Atom',
OPDS: 'http://opds-spec.org/2010/catalog',
THR: 'http://purl.org/syndication/thread/1.0',
DC: 'http://purl.org/dc/elements/1.1/',
DCTERMS: 'http://purl.org/dc/terms/',
FH: 'http://purl.org/syndication/history/1.0',
PSE: 'http://vaemendis.net/opds-pse/ns',
OS: 'http://a9.com/-/spec/opensearch/1.1/',
}
const MIME = {
ATOM: 'application/atom+xml',
OPDS2: 'application/opds+json',
}
export const REL = {
ACQ: 'http://opds-spec.org/acquisition',
FACET: 'http://opds-spec.org/facet',
GROUP: 'http://opds-spec.org/group',
COVER: [
'http://opds-spec.org/image',
'http://opds-spec.org/cover', // ManyBooks legacy, not in spec
'x-stanza-cover-image', // Lexcycle Stanza legacy
],
THUMBNAIL: [
'http://opds-spec.org/image/thumbnail',
'http://opds-spec.org/thumbnail', // ManyBooks legacy, not in spec
'x-stanza-cover-image-thumbnail', // Lexcycle Stanza legacy
],
STREAM: 'http://vaemendis.net/opds-pse/stream',
}
export const SYMBOL = {
SUMMARY: Symbol('summary'),
CONTENT: Symbol('content'),
}
const FACET_GROUP = Symbol('facetGroup')
const groupByArray = (arr, f) => {
const map = new Map()
if (arr) {
for (const el of arr) {
const keys = f(el)
const keyArray = keys == null ? [] : (Array.isArray(keys) ? keys : [keys])
for (const key of keyArray) {
const group = map.get(key)
if (group) group.push(el)
else map.set(key, [el])
}
}
}
return map
}
// https://www.rfc-editor.org/rfc/rfc7231#section-3.1.1
const parseMediaType = str => {
if (!str) return
const [mediaType, ...ps] = str.split(/ *; */)
if (!mediaType) return
return {
mediaType: mediaType.toLowerCase(),
parameters: ps.reduce((acc, p) => {
const [name, val] = p.split('=')
if (name) {
acc[name.toLowerCase()] = val?.replace(/(^"|"$)/g, '')
}
return acc
}, {}),
}
}
export const isOPDSCatalog = str => {
const parsed = parseMediaType(str)
if (!parsed) return false
const { mediaType, parameters } = parsed
if (mediaType === MIME.OPDS2) return true
return mediaType === MIME.ATOM && parameters.profile?.toLowerCase() === 'opds-catalog'
}
// ignore the namespace if it doesn't appear in document at all
const useNS = (doc, ns) =>
doc.lookupNamespaceURI(null) === ns || doc.lookupPrefix(ns) ? ns : undefined
const filterNS = ns => ns
? name => el => el.namespaceURI === ns && el.localName === name
: name => el => el.localName === name
const getContent = el => {
if (!el) return
const type = el.getAttribute('type') ?? 'text'
const value = type === 'xhtml' ? el.innerHTML
: type === 'html' ? el.textContent
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('&', '&')
: el.textContent
return { value, type }
}
const getTextContent = el => {
const content = getContent(el)
if (content?.type === 'text') return content.value
}
const getSummary = (a, b) => getTextContent(a) ?? getTextContent(b)
// Fetch only direct children to avoid polluting with nested deep indirect acquisitions
const getDirectChildren = (el, ns, localName, tagName) => {
return Array.from(el.childNodes).filter(node =>
node.nodeType === 1 &&
(
(node.namespaceURI === ns && node.localName === localName) ||
(node.tagName === tagName)
)
)
}
const getPrice = link => {
const prices = getDirectChildren(link, NS.OPDS, 'price', 'opds:price')
if (!prices.length) return
const parsed = prices.reduce((acc, price) => {
const value = parseFloat(price.textContent)
if (!Number.isNaN(value)) {
acc.push({
currency: price.getAttribute('currencycode') ?? undefined,
value,
})
}
return acc
}, [])
if (!parsed.length) return
// OPDS 1.x allows multiple prices, OPDS 2.0 schema defines price as a single object
return parsed.length === 1 ? parsed[0] : parsed
}
const getIndirectAcquisition = el => {
const ias = getDirectChildren(el, NS.OPDS, 'indirectAcquisition', 'opds:indirectAcquisition')
if (!ias.length) return []
return ias.reduce((acc, ia) => {
const type = ia.getAttribute('type')
if (type) {
acc.push({
type,
child: getIndirectAcquisition(ia),
})
}
return acc
}, [])
}
const getLink = link => {
const relAttr = link.getAttribute('rel')
const rel = relAttr ? relAttr.split(/ +/) : undefined
const isAcquisition = rel?.some(r => r.startsWith(REL.ACQ) || r === 'preview')
const isStream = rel?.includes(REL.STREAM)
// Map OPDS 1.x active facets to OPDS 2.0 "self" link
const activeFacet = link.getAttributeNS(NS.OPDS, 'activeFacet') || link.getAttribute('opds:activeFacet')
const mappedRel = activeFacet === 'true' ? [rel ?? []].flat().concat('self') : rel
// Maps OPDS 1.x thr:count seamlessly to OPDS 2.0 properties.numberOfItems
const thrCount = link.getAttributeNS(NS.THR, 'count') || link.getAttribute('thr:count')
// Support for systems that incorrectly use standard `count` for facet hints
const fallbackCount = link.getAttribute('count')
// --- OPDS-PSE Extensions ---
const pseCount = link.getAttributeNS(NS.PSE, 'count') || link.getAttribute('pse:count')
const pseLastRead = link.getAttributeNS(NS.PSE, 'lastRead') || link.getAttribute('pse:lastRead')
const pseLastReadDate = link.getAttributeNS(NS.PSE, 'lastReadDate') || link.getAttribute('pse:lastReadDate')
return {
rel: mappedRel,
href: link.getAttribute('href') ?? undefined,
type: link.getAttribute('type') ?? undefined,
title: link.getAttribute('title') ?? undefined,
// --- Facet Grouping ---
[FACET_GROUP]: (link.getAttributeNS(NS.OPDS, 'facetGroup') || link.getAttribute('opds:facetGroup')) ?? undefined,
properties: {
price: (isAcquisition || isStream) ? getPrice(link) : undefined,
indirectAcquisition: (isAcquisition || isStream) ? getIndirectAcquisition(link) : undefined,
// --- Pagination / Facet Counters ---
numberOfItems: thrCount != null ? Number(thrCount) : (!isStream && fallbackCount != null) ? Number(fallbackCount) : undefined,
'pse:count': isStream && (pseCount ?? fallbackCount) != null ? Number(pseCount ?? fallbackCount) : undefined,
'pse:lastRead': isStream && pseLastRead != null ? Number(pseLastRead) : undefined,
'pse:lastReadDate': isStream ? pseLastReadDate ?? undefined : undefined,
},
}
}
const getPerson = person => {
const NS = person.namespaceURI
const uri = person.getElementsByTagNameNS(NS, 'uri')[0]?.textContent
return {
name: person.getElementsByTagNameNS(NS, 'name')[0]?.textContent ?? undefined,
links: uri ? [{ href: uri }] : [],
}
}
export const getPublication = entry => {
const filter = filterNS(useNS(entry.ownerDocument, NS.ATOM))
const children = Array.from(entry.children)
const filterDCEL = filterNS(NS.DC)
const filterDCTERMS = filterNS(NS.DCTERMS)
const filterDC = x => y => filterDCEL(x)(y) || filterDCTERMS(x)(y)
const links = children.filter(filter('link')).map(getLink)
const linksByRel = groupByArray(links, link => link.rel)
return {
metadata: {
id: children.find(filter('id'))?.textContent ?? undefined,
updated: children.find(filter('updated'))?.textContent ?? undefined,
title: children.find(filter('title'))?.textContent ?? undefined,
author: children.filter(filter('author')).map(getPerson),
contributor: children.filter(filter('contributor')).map(getPerson),
publisher: children.find(filterDC('publisher'))?.textContent ?? undefined,
published: (children.find(filter('published'))
?? children.find(filterDCTERMS('issued'))
?? children.find(filterDC('date')))?.textContent ?? undefined,
language: children.find(filterDC('language'))?.textContent ?? undefined,
identifier: children.find(filterDC('identifier'))?.textContent ?? undefined,
subject: children.filter(filter('category')).map(category => ({
name: category.getAttribute('label') ?? undefined,
code: category.getAttribute('term') ?? undefined,
scheme: category.getAttribute('scheme') ?? undefined,
})),
rights: children.find(filter('rights'))?.textContent ?? undefined,
[SYMBOL.CONTENT]: getContent(children.find(filter('content')) ?? children.find(filter('summary'))) ?? undefined,
},
links,
images: REL.COVER.concat(REL.THUMBNAIL)
.map(R => linksByRel.get(R)?.[0]).filter(Boolean),
}
}
export const getFeed = doc => {
const ns = useNS(doc, NS.ATOM)
const filter = filterNS(ns)
const children = Array.from(doc.documentElement.children)
const entries = children.filter(filter('entry'))
const links = children.filter(filter('link')).map(getLink)
const linksByRel = groupByArray(links, link => link.rel)
const filterFH = filterNS(NS.FH)
const filterOS = filterNS(NS.OS)
const groupedItems = new Map([[undefined, []]])
const groupLinkMap = new Map()
for (const entry of entries) {
const children = Array.from(entry.children)
const links = children.filter(filter('link')).map(getLink)
const linksByRel = groupByArray(links, link => link.rel)
const isPub = Array.from(linksByRel.keys())
.some(rel => rel?.startsWith(REL.ACQ) || rel === 'preview' || rel === REL.STREAM)
const groupLinks = linksByRel.get(REL.GROUP) ?? linksByRel.get('collection')
const groupLink = groupLinks?.length
? groupLinks.find(link => groupedItems.has(link.href)) ?? groupLinks[0] : undefined
if (groupLink?.href && !groupLinkMap.has(groupLink.href)) {
groupLinkMap.set(groupLink.href, groupLink)
}
const item = isPub
? getPublication(entry)
: Object.assign(links.find(link => isOPDSCatalog(link.type)) ?? links[0] ?? {}, {
title: children.find(filter('title'))?.textContent ?? undefined,
[SYMBOL.SUMMARY]: getSummary(children.find(filter('summary')),
children.find(filter('content'))) ?? undefined,
})
// Flag the item with its resolved OPDS 2.0 type
item.__type = isPub ? 'publication' : 'navigation'
const arr = groupedItems.get(groupLink?.href)
if (arr) arr.push(item)
else groupedItems.set(groupLink.href, [item])
}
const [items, ...groups] = Array.from(groupedItems, ([key, groupItems]) => {
// Separate publications and navigation items within the group
const publications = []
const navigation = []
for (const item of groupItems) {
if (item.__type === 'publication') {
delete item.__type
publications.push(item)
} else {
delete item.__type
navigation.push(item)
}
}
const groupContent = {}
if (publications.length) groupContent.publications = publications
if (navigation.length) groupContent.navigation = navigation
if (key === undefined) return groupContent
const link = groupLinkMap.get(key)
return {
metadata: {
title: link?.title,
numberOfItems: link?.properties?.numberOfItems,
},
links: [{ rel: 'self', href: link?.href, type: link?.type }],
...groupContent,
}
})
// --- OPDS 2.0 Pagination (derived from OpenSearch / RFC 5005) ---
const totalResults = children.find(filterOS('totalResults'))?.textContent
const itemsPerPage = children.find(filterOS('itemsPerPage'))?.textContent
const startIndex = children.find(filterOS('startIndex'))?.textContent
let currentPage
if (startIndex != null && itemsPerPage != null) {
const start = Number(startIndex)
const items = Number(itemsPerPage)
// Resolves typical 1-based offset to a page number
currentPage = Math.floor((start > 0 ? start - 1 : 0) / items) + 1
}
return {
metadata: {
id: children.find(filter('id'))?.textContent ?? undefined,
updated: children.find(filter('updated'))?.textContent ?? undefined,
title: children.find(filter('title'))?.textContent ?? undefined,
subtitle: children.find(filter('subtitle'))?.textContent ?? undefined,
numberOfItems: totalResults != null ? Number(totalResults) : undefined,
itemsPerPage: itemsPerPage != null ? Number(itemsPerPage) : undefined,
currentPage,
},
links,
isComplete: !!children.find(filterFH('complete')) || undefined,
isArchive: !!children.find(filterFH('archive')) || undefined,
...items, // contains top-level 'publications' and/or 'navigation' arrays
groups: groups.length ? groups : undefined,
facets: Array.from(
groupByArray(linksByRel.get(REL.FACET) ?? [], link => link[FACET_GROUP]),
([facet, links]) => ({ metadata: { title: facet ?? undefined }, links })
),
}
}
export const getSearch = async link => {
const { replace, getVariables } = await import('./uri-template.js')
const href = link.href || ''
return {
metadata: {
title: link.title ?? undefined,
},
search: map => replace(href, map.get(undefined)),
params: Array.from(getVariables(href), name => ({ name })),
}
}
export const getOpenSearch = doc => {
const defaultNS = doc.documentElement.namespaceURI
const filter = filterNS(defaultNS)
const children = Array.from(doc.documentElement.children)
const $$urls = children.filter(filter('Url'))
const $url = $$urls.find(url => isOPDSCatalog(url.getAttribute('type'))) ?? $$urls[0]
if (!$url) throw new Error('document must contain at least one Url element')
const regex = /{(?:([^}]+?):)?(.+?)(\?)?}/g
const defaultMap = new Map([
['count', '100'],
['startIndex', $url.getAttribute('indexOffset') ?? '0'],
['startPage', $url.getAttribute('pageOffset') ?? '0'],
['language', '*'],
['inputEncoding', 'UTF-8'],
['outputEncoding', 'UTF-8'],
])
const template = $url.getAttribute('template') || ''
return {
metadata: {
title: (children.find(filter('LongName')) ?? children.find(filter('ShortName')))?.textContent ?? undefined,
description: children.find(filter('Description'))?.textContent ?? undefined,
},
search: map => template.replace(regex, (_, prefix, param) => {
const namespace = prefix ? $url.lookupNamespaceURI(prefix) : undefined
const ns = namespace === defaultNS ? undefined : namespace
const val = map.get(ns)?.get(param)
return encodeURIComponent(val ?? (!ns ? defaultMap.get(param) ?? '' : ''))
}),
params: Array.from(template.matchAll(regex), ([, prefix, param, optional]) => {
const namespace = prefix ? $url.lookupNamespaceURI(prefix) : undefined
const ns = namespace === defaultNS ? undefined : namespace
return {
ns,
name: param, // (.+?) ensures `param` is non-empty
required: !optional,
value: ns && ns !== defaultNS ? undefined : defaultMap.get(param) ?? undefined,
}
}),
}
}