BasePage.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const WebWorker = require('../WebWorker');//singleton
  2. const XmlParser = require('../xml/XmlParser');
  3. class BasePage {
  4. constructor(config) {
  5. this.config = config;
  6. this.webWorker = new WebWorker(config);
  7. this.rootTag = 'feed';
  8. this.opdsRoot = config.opdsRoot;
  9. }
  10. makeEntry(entry = {}) {
  11. if (!entry.id)
  12. throw new Error('makeEntry: no id');
  13. if (!entry.title)
  14. throw new Error('makeEntry: no title');
  15. const result = {
  16. updated: (new Date()).toISOString().substring(0, 19) + 'Z',
  17. };
  18. return Object.assign(result, entry);
  19. }
  20. myEntry() {
  21. return this.makeEntry({
  22. id: this.id,
  23. title: this.title,
  24. link: this.navLink({rel: 'subsection', href: `/${this.id}`}),
  25. });
  26. }
  27. makeLink(attrs) {
  28. return {'*ATTRS': attrs};
  29. }
  30. navLink(attrs) {
  31. return this.makeLink({
  32. href: this.opdsRoot + (attrs.href || ''),
  33. rel: attrs.rel || '',
  34. type: 'application/atom+xml; profile=opds-catalog; kind=navigation',
  35. });
  36. }
  37. baseLinks() {
  38. return [
  39. this.navLink({rel: 'start'}),
  40. this.navLink({rel: 'self', href: (this.id ? `/${this.id}` : '')}),
  41. ];
  42. }
  43. makeBody(content) {
  44. const base = this.makeEntry({id: this.id, title: this.title});
  45. base['*ATTRS'] = {
  46. 'xmlns': 'http://www.w3.org/2005/Atom',
  47. 'xmlns:dc': 'http://purl.org/dc/terms/',
  48. 'xmlns:opds': 'http://opds-spec.org/2010/catalog',
  49. };
  50. if (!content.link)
  51. base.link = this.baseLinks();
  52. const xml = new XmlParser();
  53. const xmlObject = {};
  54. xmlObject[this.rootTag] = Object.assign(base, content);
  55. xml.fromObject(xmlObject);
  56. return xml.toString({format: true});
  57. }
  58. async body() {
  59. throw new Error('Body not implemented');
  60. }
  61. }
  62. module.exports = BasePage;