@molecule/api-feed-rss-parser 1.0.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/LICENSE +115 -0
- package/dist/browser-guard.d.ts +2 -0
- package/dist/browser-guard.d.ts.map +1 -0
- package/dist/browser-guard.js +18 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +52 -0
- package/dist/parseAtom.d.ts +18 -0
- package/dist/parseAtom.d.ts.map +1 -0
- package/dist/parseAtom.js +151 -0
- package/dist/parseFeed.d.ts +43 -0
- package/dist/parseFeed.d.ts.map +1 -0
- package/dist/parseFeed.js +113 -0
- package/dist/parseJsonFeed.d.ts +19 -0
- package/dist/parseJsonFeed.d.ts.map +1 -0
- package/dist/parseJsonFeed.js +131 -0
- package/dist/parseRdf.d.ts +18 -0
- package/dist/parseRdf.d.ts.map +1 -0
- package/dist/parseRdf.js +65 -0
- package/dist/parseRss.d.ts +18 -0
- package/dist/parseRss.d.ts.map +1 -0
- package/dist/parseRss.js +151 -0
- package/dist/sanitize.d.ts +27 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +47 -0
- package/dist/types.d.ts +129 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +13 -0
- package/dist/utilities.d.ts +77 -0
- package/dist/utilities.d.ts.map +1 -0
- package/dist/utilities.js +162 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-guard.d.ts","sourceRoot":"","sources":["../src/browser-guard.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,CAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser guard — `@molecule/api-feed-rss-parser` is SERVER-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Generated by scripts/gen-browser-guards.mjs (workspace root) — edit THAT, not this.
|
|
5
|
+
* Evaluating a server package in a browser bundle is always an import-graph mistake
|
|
6
|
+
* (node APIs, secrets); without this guard it surfaces as a cryptic downstream crash
|
|
7
|
+
* ("Buffer is not defined") far from the culprit. Throwing here names the package and
|
|
8
|
+
* the fix at the exact moment the client bundle evaluates it. jsdom tests and SSR are
|
|
9
|
+
* unaffected: the throw requires browser globals AND the absence of a node runtime.
|
|
10
|
+
*/
|
|
11
|
+
const g = globalThis;
|
|
12
|
+
if (g.window !== undefined && g.document !== undefined && !g.process?.versions?.node) {
|
|
13
|
+
throw new Error('@molecule/api-feed-rss-parser is SERVER-ONLY: it was bundled into browser/client code. Import it only ' +
|
|
14
|
+
'from server code (a server route/function or your API), or dynamic-import it inside ' +
|
|
15
|
+
'the server handler — never from components or shared client modules, and never ' +
|
|
16
|
+
'polyfill Buffer/process to silence this.');
|
|
17
|
+
}
|
|
18
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-function feed parser for molecule.dev — RSS 2.0, Atom 1.0, RSS 1.0
|
|
3
|
+
* (RDF), and JSON Feed 1.0 / 1.1, normalized into a single `{ feed, items[] }`
|
|
4
|
+
* shape. Used by `news-aggregator`, `blog`, and `podcast` apps for ingesting
|
|
5
|
+
* external feeds and dedup'ing items by stable id.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { parseFeed } from '@molecule/api-feed-rss-parser'
|
|
10
|
+
*
|
|
11
|
+
* const res = await fetch('https://example.com/feed.xml')
|
|
12
|
+
* const { feed, items } = parseFeed(await res.text(), {
|
|
13
|
+
* contentType: res.headers.get('content-type') ?? undefined,
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* for (const item of items) {
|
|
17
|
+
* console.log(item.id, item.title, item.publishedAt)
|
|
18
|
+
* for (const enclosure of item.enclosures ?? []) {
|
|
19
|
+
* console.log(' ↳', enclosure.url, enclosure.type, enclosure.durationSeconds)
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* The parser is a pure function — it accepts the response body string plus
|
|
26
|
+
* optional `contentType` / `format` hints and returns a normalized result.
|
|
27
|
+
* No fetch, no state, no caching: callers fetch the document themselves
|
|
28
|
+
* (native `fetch` or the bonded `@molecule/api-http` provider) and can
|
|
29
|
+
* layer `@molecule/api-cache` on top for TTL caching.
|
|
30
|
+
*
|
|
31
|
+
* iTunes podcast namespace fields (`<itunes:duration>`, `<itunes:author>`,
|
|
32
|
+
* `<itunes:image>`, `<itunes:category>`) are extracted into the normalized
|
|
33
|
+
* shape — duration ends up on the matching `<enclosure>` as
|
|
34
|
+
* `enclosure.durationSeconds`. Dublin Core (`dc:creator`, `dc:date`,
|
|
35
|
+
* `dc:subject`) is also recognized for RSS 1.0 / RDF feeds.
|
|
36
|
+
*
|
|
37
|
+
* Item content fields are sanitized by default — `<script>` blocks, inline
|
|
38
|
+
* event handlers, and `javascript:` URLs are stripped. Pass
|
|
39
|
+
* `sanitizeHtml: false` to opt out (e.g. when sanitization happens further
|
|
40
|
+
* down the pipeline).
|
|
41
|
+
*
|
|
42
|
+
* Item identifiers are derived in priority order: explicit GUID / Atom id /
|
|
43
|
+
* JSON Feed id → item link URL → SHA-1 of `title + publishedAt`. The result
|
|
44
|
+
* is always non-empty and stable across re-parses, so deduplication on
|
|
45
|
+
* `item.id` is safe.
|
|
46
|
+
*
|
|
47
|
+
* @module
|
|
48
|
+
*/
|
|
49
|
+
export * from './browser-guard.js';
|
|
50
|
+
export * from './parseFeed.js';
|
|
51
|
+
export * from './sanitize.js';
|
|
52
|
+
export * from './types.js';
|
|
53
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AAEH,cAAc,oBAAoB,CAAA;AAClC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-function feed parser for molecule.dev — RSS 2.0, Atom 1.0, RSS 1.0
|
|
3
|
+
* (RDF), and JSON Feed 1.0 / 1.1, normalized into a single `{ feed, items[] }`
|
|
4
|
+
* shape. Used by `news-aggregator`, `blog`, and `podcast` apps for ingesting
|
|
5
|
+
* external feeds and dedup'ing items by stable id.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { parseFeed } from '@molecule/api-feed-rss-parser'
|
|
10
|
+
*
|
|
11
|
+
* const res = await fetch('https://example.com/feed.xml')
|
|
12
|
+
* const { feed, items } = parseFeed(await res.text(), {
|
|
13
|
+
* contentType: res.headers.get('content-type') ?? undefined,
|
|
14
|
+
* })
|
|
15
|
+
*
|
|
16
|
+
* for (const item of items) {
|
|
17
|
+
* console.log(item.id, item.title, item.publishedAt)
|
|
18
|
+
* for (const enclosure of item.enclosures ?? []) {
|
|
19
|
+
* console.log(' ↳', enclosure.url, enclosure.type, enclosure.durationSeconds)
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* The parser is a pure function — it accepts the response body string plus
|
|
26
|
+
* optional `contentType` / `format` hints and returns a normalized result.
|
|
27
|
+
* No fetch, no state, no caching: callers fetch the document themselves
|
|
28
|
+
* (native `fetch` or the bonded `@molecule/api-http` provider) and can
|
|
29
|
+
* layer `@molecule/api-cache` on top for TTL caching.
|
|
30
|
+
*
|
|
31
|
+
* iTunes podcast namespace fields (`<itunes:duration>`, `<itunes:author>`,
|
|
32
|
+
* `<itunes:image>`, `<itunes:category>`) are extracted into the normalized
|
|
33
|
+
* shape — duration ends up on the matching `<enclosure>` as
|
|
34
|
+
* `enclosure.durationSeconds`. Dublin Core (`dc:creator`, `dc:date`,
|
|
35
|
+
* `dc:subject`) is also recognized for RSS 1.0 / RDF feeds.
|
|
36
|
+
*
|
|
37
|
+
* Item content fields are sanitized by default — `<script>` blocks, inline
|
|
38
|
+
* event handlers, and `javascript:` URLs are stripped. Pass
|
|
39
|
+
* `sanitizeHtml: false` to opt out (e.g. when sanitization happens further
|
|
40
|
+
* down the pipeline).
|
|
41
|
+
*
|
|
42
|
+
* Item identifiers are derived in priority order: explicit GUID / Atom id /
|
|
43
|
+
* JSON Feed id → item link URL → SHA-1 of `title + publishedAt`. The result
|
|
44
|
+
* is always non-empty and stable across re-parses, so deduplication on
|
|
45
|
+
* `item.id` is safe.
|
|
46
|
+
*
|
|
47
|
+
* @module
|
|
48
|
+
*/
|
|
49
|
+
export * from './browser-guard.js';
|
|
50
|
+
export * from './parseFeed.js';
|
|
51
|
+
export * from './sanitize.js';
|
|
52
|
+
export * from './types.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ParsedFeed } from './types.js';
|
|
2
|
+
interface ParseAtomArgs {
|
|
3
|
+
/** The fast-xml-parser document — already known to contain a `<feed>` root. */
|
|
4
|
+
doc: Record<string, unknown>;
|
|
5
|
+
/** Whether to run {@link sanitizeHtml} on `summary` / `content` fields. */
|
|
6
|
+
sanitize: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Parse an Atom 1.0 document into the normalized {@link ParsedFeed} shape.
|
|
10
|
+
*
|
|
11
|
+
* @param args - Parsed XML document plus options.
|
|
12
|
+
* @param args.doc
|
|
13
|
+
* @param args.sanitize
|
|
14
|
+
* @returns Normalized feed + items.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseAtom({ doc, sanitize }: ParseAtomArgs): ParsedFeed;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=parseAtom.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseAtom.d.ts","sourceRoot":"","sources":["../src/parseAtom.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAqD,UAAU,EAAE,MAAM,YAAY,CAAA;AAG/F,UAAU,aAAa;IACrB,+EAA+E;IAC/E,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC5B,2EAA2E;IAC3E,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,aAAa,GAAG,UAAU,CAgCtE"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { sanitizeHtml } from './sanitize.js';
|
|
2
|
+
import { attrOf, synthesizeItemId, textOf, toArray, toIsoDate } from './utilities.js';
|
|
3
|
+
/**
|
|
4
|
+
* Parse an Atom 1.0 document into the normalized {@link ParsedFeed} shape.
|
|
5
|
+
*
|
|
6
|
+
* @param args - Parsed XML document plus options.
|
|
7
|
+
* @param args.doc
|
|
8
|
+
* @param args.sanitize
|
|
9
|
+
* @returns Normalized feed + items.
|
|
10
|
+
*/
|
|
11
|
+
export function parseAtom({ doc, sanitize }) {
|
|
12
|
+
const atom = (doc['feed'] ?? {});
|
|
13
|
+
const links = toArray(atom['link']);
|
|
14
|
+
const alternateLink = pickLink(links, 'alternate') ?? pickLink(links, undefined);
|
|
15
|
+
const selfLink = pickLink(links, 'self');
|
|
16
|
+
const updated = toIsoDate(textOf(atom['updated']));
|
|
17
|
+
const language = attrOf(atom, 'xml:lang') ??
|
|
18
|
+
textOf(atom['language']) ??
|
|
19
|
+
atom['@_xml:lang'];
|
|
20
|
+
const feed = {
|
|
21
|
+
title: textOf(atom['title']) ?? '',
|
|
22
|
+
description: textOf(atom['subtitle']) ?? textOf(atom['summary']),
|
|
23
|
+
link: alternateLink,
|
|
24
|
+
feedUrl: selfLink,
|
|
25
|
+
language,
|
|
26
|
+
updatedAt: updated,
|
|
27
|
+
author: extractAtomAuthor(atom['author']),
|
|
28
|
+
copyright: textOf(atom['rights']),
|
|
29
|
+
imageUrl: textOf(atom['logo']) ?? textOf(atom['icon']),
|
|
30
|
+
format: 'atom-1.0',
|
|
31
|
+
};
|
|
32
|
+
const rawEntries = toArray(atom['entry']);
|
|
33
|
+
const items = rawEntries.map((raw) => buildAtomEntry(raw, sanitize));
|
|
34
|
+
return { feed, items };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Select the first href from an array of Atom link objects that matches the given `rel` value.
|
|
38
|
+
*
|
|
39
|
+
* @param links
|
|
40
|
+
* @param rel
|
|
41
|
+
*/
|
|
42
|
+
function pickLink(links, rel) {
|
|
43
|
+
for (const link of links) {
|
|
44
|
+
if (typeof link === 'string') {
|
|
45
|
+
if (rel === undefined || rel === 'alternate') {
|
|
46
|
+
const trimmed = link.trim();
|
|
47
|
+
if (trimmed)
|
|
48
|
+
return trimmed;
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (link && typeof link === 'object') {
|
|
53
|
+
const obj = link;
|
|
54
|
+
const linkRel = obj['@_rel'] ?? 'alternate';
|
|
55
|
+
const href = obj['@_href'];
|
|
56
|
+
if (!href)
|
|
57
|
+
continue;
|
|
58
|
+
if (rel === undefined && linkRel === 'alternate')
|
|
59
|
+
return href;
|
|
60
|
+
if (rel !== undefined && linkRel === rel)
|
|
61
|
+
return href;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extract a display name or email string from an Atom `<author>` value.
|
|
68
|
+
*
|
|
69
|
+
* @param value
|
|
70
|
+
*/
|
|
71
|
+
function extractAtomAuthor(value) {
|
|
72
|
+
if (value == null)
|
|
73
|
+
return undefined;
|
|
74
|
+
const candidate = Array.isArray(value) ? value[0] : value;
|
|
75
|
+
if (typeof candidate === 'string')
|
|
76
|
+
return candidate.trim() || undefined;
|
|
77
|
+
if (candidate && typeof candidate === 'object') {
|
|
78
|
+
const obj = candidate;
|
|
79
|
+
return textOf(obj['name']) ?? textOf(obj['email']);
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Build a `NormalizedFeedItem` from a raw Atom `<entry>` object.
|
|
85
|
+
*
|
|
86
|
+
* @param raw
|
|
87
|
+
* @param sanitize
|
|
88
|
+
*/
|
|
89
|
+
function buildAtomEntry(raw, sanitize) {
|
|
90
|
+
const title = textOf(raw['title']) ?? '';
|
|
91
|
+
const links = toArray(raw['link']);
|
|
92
|
+
const link = pickLink(links, 'alternate') ?? pickLink(links, undefined);
|
|
93
|
+
const guid = textOf(raw['id']);
|
|
94
|
+
const publishedAt = toIsoDate(textOf(raw['published']) ?? textOf(raw['issued']));
|
|
95
|
+
const updatedAt = toIsoDate(textOf(raw['updated']) ?? textOf(raw['modified']));
|
|
96
|
+
const rawSummary = textOf(raw['summary']);
|
|
97
|
+
const rawContent = textOf(raw['content']);
|
|
98
|
+
const summary = sanitize ? sanitizeHtml(rawSummary) : rawSummary;
|
|
99
|
+
const content = sanitize ? sanitizeHtml(rawContent) : rawContent;
|
|
100
|
+
const author = extractAtomAuthor(raw['author']);
|
|
101
|
+
const categories = [];
|
|
102
|
+
for (const cat of toArray(raw['category'])) {
|
|
103
|
+
const term = attrOf(cat, 'term') ?? attrOf(cat, 'label') ?? textOf(cat);
|
|
104
|
+
if (term)
|
|
105
|
+
categories.push(term);
|
|
106
|
+
}
|
|
107
|
+
const enclosures = [];
|
|
108
|
+
for (const link of links) {
|
|
109
|
+
if (!link || typeof link !== 'object')
|
|
110
|
+
continue;
|
|
111
|
+
const obj = link;
|
|
112
|
+
if (obj['@_rel'] !== 'enclosure')
|
|
113
|
+
continue;
|
|
114
|
+
const url = obj['@_href'];
|
|
115
|
+
if (!url)
|
|
116
|
+
continue;
|
|
117
|
+
const enclosure = { url };
|
|
118
|
+
const type = obj['@_type'];
|
|
119
|
+
if (type)
|
|
120
|
+
enclosure.type = type;
|
|
121
|
+
const length = obj['@_length'];
|
|
122
|
+
if (length) {
|
|
123
|
+
const n = Number(length);
|
|
124
|
+
if (Number.isFinite(n) && n >= 0)
|
|
125
|
+
enclosure.length = n;
|
|
126
|
+
}
|
|
127
|
+
const title = obj['@_title'];
|
|
128
|
+
if (title)
|
|
129
|
+
enclosure.title = title;
|
|
130
|
+
enclosures.push(enclosure);
|
|
131
|
+
}
|
|
132
|
+
const id = guid ?? link ?? synthesizeItemId(title, publishedAt);
|
|
133
|
+
const item = { id, title };
|
|
134
|
+
if (link)
|
|
135
|
+
item.link = link;
|
|
136
|
+
if (summary)
|
|
137
|
+
item.summary = summary;
|
|
138
|
+
if (content)
|
|
139
|
+
item.content = content;
|
|
140
|
+
if (author)
|
|
141
|
+
item.author = author;
|
|
142
|
+
if (publishedAt)
|
|
143
|
+
item.publishedAt = publishedAt;
|
|
144
|
+
if (updatedAt)
|
|
145
|
+
item.updatedAt = updatedAt;
|
|
146
|
+
if (categories.length > 0)
|
|
147
|
+
item.categories = categories;
|
|
148
|
+
if (enclosures.length > 0)
|
|
149
|
+
item.enclosures = enclosures;
|
|
150
|
+
return item;
|
|
151
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { FeedFormat, ParsedFeed, ParseFeedOptions } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Detect the source format of a feed body.
|
|
4
|
+
*
|
|
5
|
+
* Detection runs in this order:
|
|
6
|
+
* 1. If `contentType` includes `json`, OR the trimmed body begins with `{`/`[`,
|
|
7
|
+
* return `'json-feed'` without parsing XML.
|
|
8
|
+
* 2. Otherwise the XML root element selects the format:
|
|
9
|
+
* - `rss` → `'rss-2.0'`
|
|
10
|
+
* - `feed` → `'atom-1.0'`
|
|
11
|
+
* - `rdf:RDF` (or `RDF`) → `'rdf-1.0'`
|
|
12
|
+
*
|
|
13
|
+
* @param body - Raw body string.
|
|
14
|
+
* @param contentType - Optional `Content-Type` header value from the HTTP response.
|
|
15
|
+
* @returns Detected format, or `undefined` when the body cannot be classified.
|
|
16
|
+
*/
|
|
17
|
+
export declare function detectFeedFormat(body: string, contentType: string | undefined): FeedFormat | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Parse an RSS / Atom / RDF / JSON Feed body into a normalized
|
|
20
|
+
* `{ feed, items[] }` shape.
|
|
21
|
+
*
|
|
22
|
+
* Pure function — no I/O, no state. Pass the response body and (optionally)
|
|
23
|
+
* the HTTP `Content-Type` header. Format detection is automatic; pass
|
|
24
|
+
* `options.format` to override.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* import { parseFeed } from '@molecule/api-feed-rss-parser'
|
|
29
|
+
*
|
|
30
|
+
* const res = await fetch('https://example.com/feed.xml')
|
|
31
|
+
* const { feed, items } = parseFeed(await res.text(), {
|
|
32
|
+
* contentType: res.headers.get('content-type') ?? undefined,
|
|
33
|
+
* })
|
|
34
|
+
* console.log(feed.title, items.length)
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @param body - The full HTTP response body. Required, must be non-empty.
|
|
38
|
+
* @param options - Parser options. See {@link ParseFeedOptions}.
|
|
39
|
+
* @returns Normalized feed + items.
|
|
40
|
+
* @throws {FeedParseError} when the body cannot be classified or parsed.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseFeed(body: string, options?: ParseFeedOptions): ParsedFeed;
|
|
43
|
+
//# sourceMappingURL=parseFeed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseFeed.d.ts","sourceRoot":"","sources":["../src/parseFeed.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AA4B1E;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,UAAU,GAAG,SAAS,CAaxB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,UAAU,CA2BlF"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { XMLParser } from 'fast-xml-parser';
|
|
2
|
+
import { parseAtom } from './parseAtom.js';
|
|
3
|
+
import { parseJsonFeed } from './parseJsonFeed.js';
|
|
4
|
+
import { parseRdf } from './parseRdf.js';
|
|
5
|
+
import { parseRss } from './parseRss.js';
|
|
6
|
+
import { FeedParseError } from './types.js';
|
|
7
|
+
const xmlParser = new XMLParser({
|
|
8
|
+
ignoreAttributes: false,
|
|
9
|
+
attributeNamePrefix: '@_',
|
|
10
|
+
textNodeName: '#text',
|
|
11
|
+
// Preserve namespace prefixes (`itunes:duration`, `dc:creator`, `content:encoded`)
|
|
12
|
+
// so each parser can pick out the metadata it needs.
|
|
13
|
+
removeNSPrefix: false,
|
|
14
|
+
// Atom + RSS commonly contain CDATA-wrapped HTML in description/content
|
|
15
|
+
// fields. Flag those so callers see the inner string verbatim.
|
|
16
|
+
cdataPropName: false,
|
|
17
|
+
parseTagValue: false,
|
|
18
|
+
parseAttributeValue: false,
|
|
19
|
+
trimValues: true,
|
|
20
|
+
// Normalize duplicate <link> / <category> / <enclosure> elements into arrays
|
|
21
|
+
// so the downstream parsers can iterate consistently.
|
|
22
|
+
isArray: (name) => name === 'item' ||
|
|
23
|
+
name === 'entry' ||
|
|
24
|
+
name === 'link' ||
|
|
25
|
+
name === 'category' ||
|
|
26
|
+
name === 'enclosure' ||
|
|
27
|
+
name === 'author' ||
|
|
28
|
+
name === 'itunes:category',
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* Detect the source format of a feed body.
|
|
32
|
+
*
|
|
33
|
+
* Detection runs in this order:
|
|
34
|
+
* 1. If `contentType` includes `json`, OR the trimmed body begins with `{`/`[`,
|
|
35
|
+
* return `'json-feed'` without parsing XML.
|
|
36
|
+
* 2. Otherwise the XML root element selects the format:
|
|
37
|
+
* - `rss` → `'rss-2.0'`
|
|
38
|
+
* - `feed` → `'atom-1.0'`
|
|
39
|
+
* - `rdf:RDF` (or `RDF`) → `'rdf-1.0'`
|
|
40
|
+
*
|
|
41
|
+
* @param body - Raw body string.
|
|
42
|
+
* @param contentType - Optional `Content-Type` header value from the HTTP response.
|
|
43
|
+
* @returns Detected format, or `undefined` when the body cannot be classified.
|
|
44
|
+
*/
|
|
45
|
+
export function detectFeedFormat(body, contentType) {
|
|
46
|
+
const ctLower = (contentType ?? '').toLowerCase();
|
|
47
|
+
const trimmed = body.trimStart();
|
|
48
|
+
if (ctLower.includes('json') || trimmed.startsWith('{') || trimmed.startsWith('[')) {
|
|
49
|
+
return 'json-feed';
|
|
50
|
+
}
|
|
51
|
+
// Cheap-but-deterministic root-element sniff before paying for a full parse.
|
|
52
|
+
// We still parse for the actual extraction; this just selects a parser.
|
|
53
|
+
const head = trimmed.slice(0, 2048);
|
|
54
|
+
if (/<rss\b/i.test(head))
|
|
55
|
+
return 'rss-2.0';
|
|
56
|
+
if (/<feed\b/i.test(head))
|
|
57
|
+
return 'atom-1.0';
|
|
58
|
+
if (/<rdf:RDF\b/i.test(head) || /<RDF\b/i.test(head))
|
|
59
|
+
return 'rdf-1.0';
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse an RSS / Atom / RDF / JSON Feed body into a normalized
|
|
64
|
+
* `{ feed, items[] }` shape.
|
|
65
|
+
*
|
|
66
|
+
* Pure function — no I/O, no state. Pass the response body and (optionally)
|
|
67
|
+
* the HTTP `Content-Type` header. Format detection is automatic; pass
|
|
68
|
+
* `options.format` to override.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* import { parseFeed } from '@molecule/api-feed-rss-parser'
|
|
73
|
+
*
|
|
74
|
+
* const res = await fetch('https://example.com/feed.xml')
|
|
75
|
+
* const { feed, items } = parseFeed(await res.text(), {
|
|
76
|
+
* contentType: res.headers.get('content-type') ?? undefined,
|
|
77
|
+
* })
|
|
78
|
+
* console.log(feed.title, items.length)
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @param body - The full HTTP response body. Required, must be non-empty.
|
|
82
|
+
* @param options - Parser options. See {@link ParseFeedOptions}.
|
|
83
|
+
* @returns Normalized feed + items.
|
|
84
|
+
* @throws {FeedParseError} when the body cannot be classified or parsed.
|
|
85
|
+
*/
|
|
86
|
+
export function parseFeed(body, options = {}) {
|
|
87
|
+
if (typeof body !== 'string' || body.trim() === '') {
|
|
88
|
+
throw new FeedParseError('Feed body is empty');
|
|
89
|
+
}
|
|
90
|
+
const sanitize = options.sanitizeHtml !== false;
|
|
91
|
+
const format = options.format ?? detectFeedFormat(body, options.contentType);
|
|
92
|
+
if (!format) {
|
|
93
|
+
throw new FeedParseError('Unable to detect feed format — pass options.format to override');
|
|
94
|
+
}
|
|
95
|
+
if (format === 'json-feed') {
|
|
96
|
+
return parseJsonFeed({ body, sanitize });
|
|
97
|
+
}
|
|
98
|
+
let doc;
|
|
99
|
+
try {
|
|
100
|
+
doc = xmlParser.parse(body);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
throw new FeedParseError(`XML parse error: ${err.message}`, format);
|
|
104
|
+
}
|
|
105
|
+
if (format === 'rss-2.0')
|
|
106
|
+
return parseRss({ doc, sanitize });
|
|
107
|
+
if (format === 'atom-1.0')
|
|
108
|
+
return parseAtom({ doc, sanitize });
|
|
109
|
+
if (format === 'rdf-1.0')
|
|
110
|
+
return parseRdf({ doc, sanitize });
|
|
111
|
+
// Defensive — exhaustiveness guard for new FeedFormat values.
|
|
112
|
+
throw new FeedParseError(`Unsupported feed format: ${format}`);
|
|
113
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ParsedFeed } from './types.js';
|
|
2
|
+
interface ParseJsonFeedArgs {
|
|
3
|
+
/** The raw response body. */
|
|
4
|
+
body: string;
|
|
5
|
+
/** Whether to run {@link sanitizeHtml} on `summary` / `content` fields. */
|
|
6
|
+
sanitize: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Parse a JSON Feed 1.0 / 1.1 body into the normalized {@link ParsedFeed} shape.
|
|
10
|
+
*
|
|
11
|
+
* @param args - Raw body plus options.
|
|
12
|
+
* @param args.body
|
|
13
|
+
* @param args.sanitize
|
|
14
|
+
* @returns Normalized feed + items.
|
|
15
|
+
* @throws {FeedParseError} when the body is not valid JSON or lacks a `feed` shape.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseJsonFeed({ body, sanitize }: ParseJsonFeedArgs): ParsedFeed;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=parseJsonFeed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseJsonFeed.d.ts","sourceRoot":"","sources":["../src/parseJsonFeed.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAqD,UAAU,EAAE,MAAM,YAAY,CAAA;AAI/F,UAAU,iBAAiB;IACzB,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAA;IACZ,2EAA2E;IAC3E,QAAQ,EAAE,OAAO,CAAA;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,iBAAiB,GAAG,UAAU,CA6B/E"}
|