@depwatch/source-rss 0.0.0-stage → 0.0.7

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 depwatch contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,44 @@
1
- # Temporary Holding Version
1
+ # @depwatch/source-rss
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Streaming RSS status polling and safe XML parsing for provider-specific availability adapters.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @depwatch/core@next @depwatch/source-rss@next
9
+ ```
10
+
11
+ Requires Node.js 18+. Includes ES modules, CommonJS and TypeScript declarations. This is an early preview.
12
+
13
+ ## Parse a feed
14
+
15
+ ```js
16
+ import { parseRss, htmlToPlainText } from '@depwatch/source-rss';
17
+
18
+ const feed = parseRss('<rss version="2.0"><channel><title>Status</title><item><title>Maintenance</title><description>Scheduled work</description></item></channel></rss>');
19
+ for (const item of feed.items) {
20
+ console.log(item.title, htmlToPlainText(item.descriptionHtml));
21
+ }
22
+ ```
23
+
24
+ CommonJS: `const { parseRss } = require('@depwatch/source-rss')`.
25
+
26
+ ## Build a status adapter
27
+
28
+ Generic RSS toolkit and abstract `RssStatusAdapter` class extending core's `StatusSourceAdapter`. Imports and construction perform no I/O. Subclasses implement `normalizeItem()` and `reduceIncident()`; optional `finalizeObservations()` handles provider-specific revision conflicts. Inherited `parse()` validates normalized records against the core schema. Pure parsing helpers remain usable independently.
29
+
30
+ `poll(context)` performs one request through the core HttpClient port. Selection is adapter `httpClient`, shared `context.httpClient`, legacy `context.fetch` bridge, then default Fetch. The adapter owns cancellation, a timeout covering headers and streamed body reads, byte limits, HTTP status/Retry-After handling, and ETag/Last-Modified validators. Successful polls return normalized observations and validator state; a conditional 304 returns `not_modified`. Error results contain sanitized messages, and caller cancellation is distinct from timeout. The caller persists checkpoints only after successfully processing evidence. A 304 without a prior successful validator is an error. No retry loop, scheduler or database is created. The host owns proxy/agent/client configuration and lifecycle.
31
+
32
+ Import `createAxiosHttpClient` or `createFetchHttpClient` from core. An Axios instance must retain its full response and unbuffered stream. Custom transports return lowercase header records, AsyncIterable byte bodies and an idempotent `cancel()` method. `createRssSourceContext` also accepts `httpClient`, while preserving the old `fetch` property when supplied.
33
+
34
+ - `parseRss(xml, limits?)`: RSS 2.0 items with string title/descriptionHtml/guid/link/pubDate. Defaults: 1 MB UTF-8, 1,000 items, XML depth 64. Rejects malformed XML, DTD/entity declarations, undeclared entities, invalid characters, duplicate scalar nodes, and non-RSS documents. Description HTML must be escaped or CDATA.
35
+ - `htmlToPlainText(html, maxBytes?)`: one HTML decoding pass, no external resources, script/style/image content removed and whitespace collapsed.
36
+ - `parsePublicationDate(text)`: strict RFC2822 with explicit numeric offset, GMT or UT; invalid dates return null.
37
+ - `parseZonedDateTime(parts, zone)`: full local date/time required. Returns a Date or an issue for invalid, nonexistent, or ambiguous times. No silent DST normalization.
38
+ - `RssParseError`: stable error code with no raw payload in its message.
39
+
40
+ Dependencies are confined to this package: fast-xml-parser 5.x, html-to-text 9.x (Node 18 compatible), and Luxon 3.x. Vendor phrases, component names and timezone rules belong to your provider adapter. `createRssSourceContext` remains available for compatibility. Scheduling, persistence and retry policy belong to the host. No vendor-specific adapter or current-state evaluator is bundled. Absence of a matching RSS notice must not be interpreted as operational status.
41
+
42
+ Licensed under the [MIT License](LICENSE).
43
+
44
+
@@ -0,0 +1,23 @@
1
+ import { StatusSourceAdapter } from '@depwatch/core';
2
+ import type { StatusSourceOptions, SourceObservation, PollContext, PollResult } from '@depwatch/core';
3
+ import type { ParseLimits, RssItem } from './parsing.js';
4
+ export interface RssStatusOptions extends StatusSourceOptions {
5
+ url: string;
6
+ requestTimeoutMs?: number;
7
+ limits?: ParseLimits;
8
+ }
9
+ /** Template method: generic transport/XML processing delegates vendor interpretation to hooks. */
10
+ export declare abstract class RssStatusAdapter extends StatusSourceAdapter {
11
+ readonly url: string;
12
+ readonly requestTimeoutMs: number;
13
+ readonly limits: Readonly<Required<ParseLimits>>;
14
+ constructor(options: RssStatusOptions);
15
+ parse(xml: string, options: {
16
+ observedAt: Date;
17
+ limits?: ParseLimits;
18
+ }): SourceObservation[];
19
+ protected abstract normalizeItem(item: RssItem, observedAt: Date): SourceObservation;
20
+ protected finalizeObservations(observations: SourceObservation[]): SourceObservation[];
21
+ /** One request only. Scheduling, retry/backoff, persistence, and lease ownership belong to the runner. */
22
+ poll(context: PollContext): Promise<PollResult>;
23
+ }
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RssStatusAdapter = void 0;
4
+ const core_1 = require("@depwatch/core");
5
+ const parsing_js_1 = require("./parsing.js");
6
+ /** Template method: generic transport/XML processing delegates vendor interpretation to hooks. */
7
+ class RssStatusAdapter extends core_1.StatusSourceAdapter {
8
+ url;
9
+ requestTimeoutMs;
10
+ limits;
11
+ constructor(options) {
12
+ super(options);
13
+ const url = new URL(options.url);
14
+ if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
15
+ throw new TypeError('RSS URL must be HTTP(S) without embedded credentials');
16
+ this.url = url.href;
17
+ this.requestTimeoutMs = (0, core_1.positiveDuration)(options.requestTimeoutMs ?? 15_000, 'requestTimeoutMs');
18
+ if (this.requestTimeoutMs > 2_147_483_647)
19
+ throw new TypeError('requestTimeoutMs exceeds the Node timer range');
20
+ this.limits = Object.freeze({
21
+ maxBytes: (0, core_1.positiveDuration)(options.limits?.maxBytes ?? 1_000_000, 'maxBytes'),
22
+ maxItems: (0, core_1.positiveDuration)(options.limits?.maxItems ?? 1000, 'maxItems'),
23
+ maxDepth: (0, core_1.positiveDuration)(options.limits?.maxDepth ?? 64, 'maxDepth'),
24
+ });
25
+ }
26
+ parse(xml, options) {
27
+ this.validateObservedAt(options?.observedAt);
28
+ const items = (0, parsing_js_1.parseRss)(xml, { ...this.limits, ...options.limits }).items;
29
+ const observations = items.map(item => this.normalizeItem(item, new Date(options.observedAt)));
30
+ return this.finalizeObservations(observations).map(observation => {
31
+ const parsed = core_1.SourceObservationSchema.parse(observation);
32
+ if (parsed.system !== this.system || parsed.environment !== this.environment || parsed.sourceId !== this.sourceId)
33
+ throw new TypeError('Normalized observation does not belong to this source');
34
+ return parsed;
35
+ });
36
+ }
37
+ finalizeObservations(observations) { return observations; }
38
+ /** One request only. Scheduling, retry/backoff, persistence, and lease ownership belong to the runner. */
39
+ async poll(context) {
40
+ this.validateObservedAt(context.now);
41
+ const previous = context.previous;
42
+ if (previous && (previous.system !== this.system || previous.environment !== this.environment || previous.sourceId !== this.sourceId))
43
+ throw new TypeError('Checkpoint does not belong to this source');
44
+ const headers = { Accept: 'application/rss+xml, application/xml, text/xml' };
45
+ const validator = (value) => typeof value === 'string' && value.length > 0 && value.length <= 1024 && !/[\r\n]/.test(value);
46
+ if (previous?.lastSuccessfulCheckAt) {
47
+ if (validator(previous.state.etag))
48
+ headers['If-None-Match'] = previous.state.etag;
49
+ if (validator(previous.state.lastModified))
50
+ headers['If-Modified-Since'] = previous.state.lastModified;
51
+ }
52
+ const controller = new AbortController();
53
+ let timedOut = false;
54
+ let response;
55
+ let rejectAborted = () => { };
56
+ const aborted = new Promise((_, reject) => { rejectAborted = reject; });
57
+ const cancelResponse = () => {
58
+ try {
59
+ if (response)
60
+ void Promise.resolve(response.cancel()).catch(() => { });
61
+ }
62
+ catch { /* Cleanup must not hide the poll result. */ }
63
+ };
64
+ const abort = () => { controller.abort(); cancelResponse(); rejectAborted(new Error('request_aborted')); };
65
+ const timer = setTimeout(() => { timedOut = true; abort(); }, this.requestTimeoutMs);
66
+ context.signal.addEventListener('abort', abort, { once: true });
67
+ if (context.signal.aborted)
68
+ abort();
69
+ let stage = 'network';
70
+ const request = async () => {
71
+ if (controller.signal.aborted)
72
+ throw new Error('request_aborted');
73
+ response = await this.getHttpClient(context).request({ url: this.url, method: 'GET', headers, signal: controller.signal });
74
+ if (controller.signal.aborted) {
75
+ cancelResponse();
76
+ throw new Error('request_aborted');
77
+ }
78
+ const nextState = {};
79
+ const etag = response.headers['etag'];
80
+ const modified = response.headers['last-modified'];
81
+ if (validator(etag))
82
+ nextState.etag = etag;
83
+ if (validator(modified))
84
+ nextState.lastModified = modified;
85
+ if (response.status === 304) {
86
+ cancelResponse();
87
+ if (!headers['If-None-Match'] && !headers['If-Modified-Since'])
88
+ return { outcome: 'error', error: { kind: 'http', message: 'Unexpected RSS 304 without a cached validator' } };
89
+ return { outcome: 'not_modified', nextState: { ...previous?.state, ...nextState } };
90
+ }
91
+ if (response.status < 200 || response.status >= 300) {
92
+ cancelResponse();
93
+ const retry = response.headers['retry-after'];
94
+ const retryMs = retry && /^\d+$/.test(retry) ? Number(retry) * 1000 : retry ? Date.parse(retry) - context.now.getTime() : NaN;
95
+ return { outcome: 'error', error: { kind: [401, 403].includes(response.status) ? 'auth' : 'http', message: `RSS HTTP ${response.status}`, ...(Number.isSafeInteger(retryMs) && retryMs >= 0 ? { retryAfterMs: retryMs } : {}) } };
96
+ }
97
+ stage = 'parse';
98
+ const contentType = response.headers['content-type']?.split(';')[0]?.trim().toLowerCase();
99
+ const length = response.headers['content-length'];
100
+ if (contentType && !['application/rss+xml', 'application/xml', 'text/xml'].includes(contentType)) {
101
+ cancelResponse();
102
+ throw new parsing_js_1.RssParseError('invalid_content_type');
103
+ }
104
+ if (length && Number(length) > this.limits.maxBytes) {
105
+ cancelResponse();
106
+ throw new parsing_js_1.RssParseError('response_too_large');
107
+ }
108
+ if (!response.body)
109
+ throw new parsing_js_1.RssParseError('empty_response');
110
+ const iterator = response.body[Symbol.asyncIterator]();
111
+ const chunks = [];
112
+ let bytes = 0;
113
+ while (true) {
114
+ stage = 'network';
115
+ const { done, value } = await iterator.next();
116
+ if (controller.signal.aborted)
117
+ throw new Error('request_aborted');
118
+ if (done)
119
+ break;
120
+ stage = 'parse';
121
+ if (!(value instanceof Uint8Array))
122
+ throw new TypeError('HTTP body must yield byte chunks');
123
+ bytes += value.byteLength;
124
+ if (bytes > this.limits.maxBytes) {
125
+ cancelResponse();
126
+ throw new parsing_js_1.RssParseError('response_too_large');
127
+ }
128
+ chunks.push(value);
129
+ }
130
+ stage = 'parse';
131
+ const xml = new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks, bytes));
132
+ return { outcome: 'ok', observations: this.parse(xml, { observedAt: context.now }), nextState };
133
+ };
134
+ try {
135
+ return await Promise.race([request(), aborted]);
136
+ }
137
+ catch (error) {
138
+ // Do not copy URLs, headers or arbitrary fetch/parser exception messages into stored errors.
139
+ const kind = timedOut ? 'timeout' : context.signal.aborted ? 'cancelled' : stage;
140
+ return { outcome: 'error', error: { kind, message: error instanceof parsing_js_1.RssParseError ? error.code : `RSS ${kind} failure` } };
141
+ }
142
+ finally {
143
+ clearTimeout(timer);
144
+ context.signal.removeEventListener('abort', abort);
145
+ cancelResponse();
146
+ }
147
+ }
148
+ }
149
+ exports.RssStatusAdapter = RssStatusAdapter;
150
+ //# sourceMappingURL=RssStatusAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RssStatusAdapter.js","sourceRoot":"","sources":["../../src/RssStatusAdapter.ts"],"names":[],"mappings":";;;AAAA,yCAAgG;AAEhG,6CAAuD;AASvD,kGAAkG;AAClG,MAAsB,gBAAiB,SAAQ,0BAAmB;IACvD,GAAG,CAAS;IACZ,gBAAgB,CAAS;IACzB,MAAM,CAAkC;IAEjD,YAAY,OAAyB;QACnC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;YAAE,MAAM,IAAI,SAAS,CAAC,sDAAsD,CAAC,CAAC;QAC7J,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC;QACpB,IAAI,CAAC,gBAAgB,GAAG,IAAA,uBAAgB,EAAC,OAAO,CAAC,gBAAgB,IAAI,MAAM,EAAE,kBAAkB,CAAC,CAAC;QACjG,IAAI,IAAI,CAAC,gBAAgB,GAAG,aAAa;YAAE,MAAM,IAAI,SAAS,CAAC,+CAA+C,CAAC,CAAC;QAChH,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YAC1B,QAAQ,EAAE,IAAA,uBAAgB,EAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,IAAI,SAAS,EAAE,UAAU,CAAC;YAC7E,QAAQ,EAAE,IAAA,uBAAgB,EAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,IAAI,IAAI,EAAE,UAAU,CAAC;YACxE,QAAQ,EAAE,IAAA,u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F;YAC7F,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC;YACjF,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,YAAY,0BAAa,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,IAAI,UAAU,EAAE,EAAE,CAAC;QAC7H,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACnD,cAAc,EAAE,CAAC;QACnB,CAAC;IACH,CAAC;CACF;AAlHD,4CAkHC"}
@@ -0,0 +1,17 @@
1
+ import type { Logger, HttpClient } from '@depwatch/core';
2
+ export * from './parsing.js';
3
+ export * from './RssStatusAdapter.js';
4
+ export interface RssSourceContext {
5
+ readonly url: string;
6
+ readonly httpClient: HttpClient;
7
+ /** @deprecated Use httpClient. */
8
+ readonly fetch?: typeof globalThis.fetch;
9
+ readonly logger: Logger;
10
+ }
11
+ /** Compatibility context helper; use adapter options or PollContext.httpClient for polling. */
12
+ export declare function createRssSourceContext(options: {
13
+ url: string;
14
+ httpClient?: HttpClient;
15
+ fetch?: typeof globalThis.fetch;
16
+ logger: Logger;
17
+ }): RssSourceContext;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.createRssSourceContext = createRssSourceContext;
18
+ const core_1 = require("@depwatch/core");
19
+ __exportStar(require("./parsing.js"), exports);
20
+ __exportStar(require("./RssStatusAdapter.js"), exports);
21
+ /** Compatibility context helper; use adapter options or PollContext.httpClient for polling. */
22
+ function createRssSourceContext(options) {
23
+ const url = new URL(options.url);
24
+ if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
25
+ throw new TypeError('RSS URL must be HTTP(S) without embedded credentials');
26
+ return Object.freeze({ url: url.href, httpClient: options.httpClient ?? (0, core_1.createFetchHttpClient)(options.fetch), ...(options.fetch ? { fetch: options.fetch } : {}), logger: options.logger.child({ component: 'depwatch.source-rss' }) });
27
+ }
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAaA,wDAIC;AAjBD,yCAAuD;AAEvD,+CAA6B;AAC7B,wDAAsC;AAStC,+FAA+F;AAC/F,SAAgB,sBAAsB,CAAC,OAAkG;IACvI,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;QAAE,MAAM,IAAI,SAAS,CAAC,sDAAsD,CAAC,CAAC;IAC7J,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,IAAA,4BAAqB,EAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAAC,CAAC;AAC1O,CAAC"}
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,42 @@
1
+ export interface RssItem {
2
+ title: string;
3
+ descriptionHtml: string;
4
+ guid: string;
5
+ link: string;
6
+ pubDate: string;
7
+ }
8
+ export interface RssFeed {
9
+ title: string;
10
+ items: RssItem[];
11
+ }
12
+ export interface ParseLimits {
13
+ maxBytes?: number;
14
+ maxItems?: number;
15
+ maxDepth?: number;
16
+ }
17
+ export declare class RssParseError extends Error {
18
+ readonly code: string;
19
+ constructor(code: string);
20
+ }
21
+ /** Pure, bounded RSS 2.0 parsing. Descriptions must contain escaped HTML or CDATA. */
22
+ export declare function parseRss(xml: string, limits?: ParseLimits): RssFeed;
23
+ /** One HTML decoding pass following XML decoding; never recursively decode strings. */
24
+ export declare function htmlToPlainText(html: string, maxBytes?: number): string;
25
+ /** RFC2822 with an explicit offset, including weekday validation; no local-zone fallback. */
26
+ export declare function parsePublicationDate(value: string): Date | null;
27
+ export interface LocalDateTime {
28
+ year: number;
29
+ month: number;
30
+ day: number;
31
+ hour: number;
32
+ minute: number;
33
+ }
34
+ export type DateTimeResult = {
35
+ ok: true;
36
+ value: Date;
37
+ } | {
38
+ ok: false;
39
+ issue: 'invalid_local_time' | 'nonexistent_local_time' | 'ambiguous_local_time';
40
+ };
41
+ /** Reject DST gaps/folds instead of accepting Luxon's default normalization/offset choice. */
42
+ export declare function parseZonedDateTime(parts: LocalDateTime, zone: string): DateTimeResult;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RssParseError = void 0;
4
+ exports.parseRss = parseRss;
5
+ exports.htmlToPlainText = htmlToPlainText;
6
+ exports.parsePublicationDate = parsePublicationDate;
7
+ exports.parseZonedDateTime = parseZonedDateTime;
8
+ const fast_xml_parser_1 = require("fast-xml-parser");
9
+ const html_to_text_1 = require("html-to-text");
10
+ const luxon_1 = require("luxon");
11
+ class RssParseError extends Error {
12
+ code;
13
+ constructor(code) {
14
+ super(code);
15
+ this.code = code;
16
+ this.name = 'RssParseError';
17
+ }
18
+ }
19
+ exports.RssParseError = RssParseError;
20
+ function positive(value, name) {
21
+ if (!Number.isSafeInteger(value) || value <= 0)
22
+ throw new TypeError(`${name} must be a positive integer`);
23
+ return value;
24
+ }
25
+ function text(value) {
26
+ if (value === undefined || value === '')
27
+ return '';
28
+ if (typeof value === 'string')
29
+ return value;
30
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
31
+ const node = value;
32
+ if (Object.keys(node).every(k => k === '#text' || k.startsWith('@_')))
33
+ return text(node['#text']);
34
+ }
35
+ throw new RssParseError('invalid_text_node');
36
+ }
37
+ function object(value) {
38
+ if (!value || typeof value !== 'object' || Array.isArray(value))
39
+ throw new RssParseError('invalid_rss_structure');
40
+ return value;
41
+ }
42
+ /** Pure, bounded RSS 2.0 parsing. Descriptions must contain escaped HTML or CDATA. */
43
+ function parseRss(xml, limits = {}) {
44
+ const maxBytes = positive(limits.maxBytes ?? 1_000_000, 'maxBytes');
45
+ const maxItems = positive(limits.maxItems ?? 1000, 'maxItems');
46
+ const maxDepth = positive(limits.maxDepth ?? 64, 'maxDepth');
47
+ if (typeof xml !== 'string')
48
+ throw new TypeError('XML must be a string');
49
+ if (Buffer.byteLength(xml, 'utf8') > maxBytes)
50
+ throw new RssParseError('response_too_large');
51
+ if (/[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/u.test(xml))
52
+ throw new RssParseError('invalid_xml_character');
53
+ // Reject declarations before invoking any XML engine, including internal/external entities.
54
+ if (/<!\s*(?:DOCTYPE|ENTITY)\b/i.test(xml))
55
+ throw new RssParseError('forbidden_xml_declaration');
56
+ // CDATA/comments are opaque to XML entity processing. Only XML's five named entities are allowed.
57
+ const structural = xml.replace(/<!\[CDATA\[[\s\S]*?\]\]>|<!--[\s\S]*?-->/g, '');
58
+ if (/&(?!amp;|lt;|gt;|apos;|quot;|#\d+;|#x[\da-f]+;)/i.test(structural))
59
+ throw new RssParseError('invalid_xml_entity');
60
+ for (const match of structural.matchAll(/&#(x[\da-f]+|\d+);/gi)) {
61
+ const literal = match[1];
62
+ const cp = /^x/i.test(literal) ? parseInt(literal.slice(1), 16) : Number(literal);
63
+ if (!(cp === 9 || cp === 10 || cp === 13 || (cp >= 32 && cp <= 0xD7FF) || (cp >= 0xE000 && cp <= 0xFFFD) || (cp >= 0x10000 && cp <= 0x10FFFF)))
64
+ throw new RssParseError('invalid_xml_character');
65
+ }
66
+ let depth = 0;
67
+ for (const match of structural.matchAll(/<\/?[A-Za-z_][\w.:-]*(?:\s+(?:[^<>"']|"[^"]*"|'[^']*')*)?\s*\/?>/g)) {
68
+ const tag = match[0];
69
+ if (tag.startsWith('</'))
70
+ depth--;
71
+ else if (!tag.endsWith('/>'))
72
+ depth++;
73
+ if (depth > maxDepth)
74
+ throw new RssParseError('xml_too_deep');
75
+ }
76
+ if (fast_xml_parser_1.XMLValidator.validate(xml) !== true)
77
+ throw new RssParseError('malformed_xml');
78
+ const parser = new fast_xml_parser_1.XMLParser({ ignoreAttributes: false, parseTagValue: false, parseAttributeValue: false, trimValues: false, processEntities: true });
79
+ let document;
80
+ try {
81
+ document = object(parser.parse(xml));
82
+ }
83
+ catch {
84
+ throw new RssParseError('malformed_xml');
85
+ }
86
+ if (Object.keys(document).some(k => k !== 'rss' && k !== '?xml'))
87
+ throw new RssParseError('invalid_rss_structure');
88
+ const rss = object(document.rss);
89
+ if (rss['@_version'] !== '2.0')
90
+ throw new RssParseError('unsupported_rss_version');
91
+ const channel = object(rss.channel);
92
+ const raw = channel.item === undefined ? [] : Array.isArray(channel.item) ? channel.item : [channel.item];
93
+ if (raw.length > maxItems)
94
+ throw new RssParseError('too_many_items');
95
+ return {
96
+ title: text(channel.title),
97
+ items: raw.map(value => {
98
+ const item = object(value);
99
+ return { title: text(item.title), descriptionHtml: text(item.description), guid: text(item.guid).trim(), link: text(item.link).trim(), pubDate: text(item.pubDate).trim() };
100
+ }),
101
+ };
102
+ }
103
+ /** One HTML decoding pass following XML decoding; never recursively decode strings. */
104
+ function htmlToPlainText(html, maxBytes = 1_000_000) {
105
+ positive(maxBytes, 'maxBytes');
106
+ if (Buffer.byteLength(html, 'utf8') > maxBytes)
107
+ throw new RssParseError('html_too_large');
108
+ return (0, html_to_text_1.convert)(html, {
109
+ wordwrap: false,
110
+ limits: { maxInputLength: maxBytes, maxDepth: 64 },
111
+ selectors: [{ selector: 'a', options: { ignoreHref: true } }, { selector: 'img', format: 'skip' }, { selector: 'script', format: 'skip' }, { selector: 'style', format: 'skip' }],
112
+ }).replace(/\s+/g, ' ').trim();
113
+ }
114
+ /** RFC2822 with an explicit offset, including weekday validation; no local-zone fallback. */
115
+ function parsePublicationDate(value) {
116
+ if (!/(?:[+-]\d{4}|GMT|UT)$/i.test(value.trim()))
117
+ return null;
118
+ const parsed = luxon_1.DateTime.fromRFC2822(value, { setZone: true });
119
+ return parsed.isValid ? parsed.toJSDate() : null;
120
+ }
121
+ /** Reject DST gaps/folds instead of accepting Luxon's default normalization/offset choice. */
122
+ function parseZonedDateTime(parts, zone) {
123
+ const fields = ['year', 'month', 'day', 'hour', 'minute'];
124
+ if (!parts || fields.some(key => !Number.isSafeInteger(parts[key])) || Object.keys(parts).some(key => !fields.includes(key)))
125
+ return { ok: false, issue: 'invalid_local_time' };
126
+ let parsed;
127
+ try {
128
+ parsed = luxon_1.DateTime.fromObject(parts, { zone });
129
+ }
130
+ catch {
131
+ return { ok: false, issue: 'invalid_local_time' };
132
+ }
133
+ if (!parsed.isValid)
134
+ return { ok: false, issue: 'invalid_local_time' };
135
+ if (Object.entries(parts).some(([key, value]) => parsed.get(key) !== value))
136
+ return { ok: false, issue: 'nonexistent_local_time' };
137
+ if (parsed.getPossibleOffsets().length !== 1)
138
+ return { ok: false, issue: 'ambiguous_local_time' };
139
+ return { ok: true, value: parsed.toJSDate() };
140
+ }
141
+ //# sourceMappingURL=parsing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parsing.js","sourceRoot":"","sources":["../../src/parsing.ts"],"names":[],"mappings":";;;AAoCA,4BAyCC;AAGD,0CAQC;AAGD,oDAIC;AAMD,gDASC;AA9GD,qDAA0D;AAC1D,+CAAuC;AACvC,iCAAiC;AAWjC,MAAa,aAAc,SAAQ,KAAK;IACV;IAA5B,YAA4B,IAAY;QAAI,KAAK,CAAC,IAAI,CAAC,CAAC;QAA5B,SAAI,GAAJ,IAAI,CAAQ;QAAiB,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAAC,CAAC;CACxF;AAFD,sCAEC;AAED,SAAS,QAAQ,CAAC,KAAa,EAAE,IAAY;IAC3C,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;QAAE,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,6BAA6B,CAAC,CAAC;IAC1G,OAAO,KAAK,CAAC;AACf,CAAC;AACD,SAAS,IAAI,CAAC,KAAc;IAC1B,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAChE,MAAM,IAAI,GAAG,KAAgC,CAAC;QAC9C,IAA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AAI,EAAE,CAAC,CAAC;IAAC,CAAC;IAAC,MAAM,CAAC;QAAC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC;IAAC,CAAC;IACnH,IAAI,CAAC,MAAM,CAAC,OAAO;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,oBAAoB,EAAE,CAAC;IACvE,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAA0B,CAAC,KAAK,KAAK,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,wBAAwB,EAAE,CAAC;IAC1J,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,sBAAsB,EAAE,CAAC;IAClG,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;AAChD,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { StatusSourceAdapter } from '@depwatch/core';
2
+ import type { StatusSourceOptions, SourceObservation, PollContext, PollResult } from '@depwatch/core';
3
+ import type { ParseLimits, RssItem } from './parsing.js';
4
+ export interface RssStatusOptions extends StatusSourceOptions {
5
+ url: string;
6
+ requestTimeoutMs?: number;
7
+ limits?: ParseLimits;
8
+ }
9
+ /** Template method: generic transport/XML processing delegates vendor interpretation to hooks. */
10
+ export declare abstract class RssStatusAdapter extends StatusSourceAdapter {
11
+ readonly url: string;
12
+ readonly requestTimeoutMs: number;
13
+ readonly limits: Readonly<Required<ParseLimits>>;
14
+ constructor(options: RssStatusOptions);
15
+ parse(xml: string, options: {
16
+ observedAt: Date;
17
+ limits?: ParseLimits;
18
+ }): SourceObservation[];
19
+ protected abstract normalizeItem(item: RssItem, observedAt: Date): SourceObservation;
20
+ protected finalizeObservations(observations: SourceObservation[]): SourceObservation[];
21
+ /** One request only. Scheduling, retry/backoff, persistence, and lease ownership belong to the runner. */
22
+ poll(context: PollContext): Promise<PollResult>;
23
+ }
@@ -0,0 +1,146 @@
1
+ import { StatusSourceAdapter, SourceObservationSchema, positiveDuration } from '@depwatch/core';
2
+ import { parseRss, RssParseError } from './parsing.js';
3
+ /** Template method: generic transport/XML processing delegates vendor interpretation to hooks. */
4
+ export class RssStatusAdapter extends StatusSourceAdapter {
5
+ url;
6
+ requestTimeoutMs;
7
+ limits;
8
+ constructor(options) {
9
+ super(options);
10
+ const url = new URL(options.url);
11
+ if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
12
+ throw new TypeError('RSS URL must be HTTP(S) without embedded credentials');
13
+ this.url = url.href;
14
+ this.requestTimeoutMs = positiveDuration(options.requestTimeoutMs ?? 15_000, 'requestTimeoutMs');
15
+ if (this.requestTimeoutMs > 2_147_483_647)
16
+ throw new TypeError('requestTimeoutMs exceeds the Node timer range');
17
+ this.limits = Object.freeze({
18
+ maxBytes: positiveDuration(options.limits?.maxBytes ?? 1_000_000, 'maxBytes'),
19
+ maxItems: positiveDuration(options.limits?.maxItems ?? 1000, 'maxItems'),
20
+ maxDepth: positiveDuration(options.limits?.maxDepth ?? 64, 'maxDepth'),
21
+ });
22
+ }
23
+ parse(xml, options) {
24
+ this.validateObservedAt(options?.observedAt);
25
+ const items = parseRss(xml, { ...this.limits, ...options.limits }).items;
26
+ const observations = items.map(item => this.normalizeItem(item, new Date(options.observedAt)));
27
+ return this.finalizeObservations(observations).map(observation => {
28
+ const parsed = SourceObservationSchema.parse(observation);
29
+ if (parsed.system !== this.system || parsed.environment !== this.environment || parsed.sourceId !== this.sourceId)
30
+ throw new TypeError('Normalized observation does not belong to this source');
31
+ return parsed;
32
+ });
33
+ }
34
+ finalizeObservations(observations) { return observations; }
35
+ /** One request only. Scheduling, retry/backoff, persistence, and lease ownership belong to the runner. */
36
+ async poll(context) {
37
+ this.validateObservedAt(context.now);
38
+ const previous = context.previous;
39
+ if (previous && (previous.system !== this.system || previous.environment !== this.environment || previous.sourceId !== this.sourceId))
40
+ throw new TypeError('Checkpoint does not belong to this source');
41
+ const headers = { Accept: 'application/rss+xml, application/xml, text/xml' };
42
+ const validator = (value) => typeof value === 'string' && value.length > 0 && value.length <= 1024 && !/[\r\n]/.test(value);
43
+ if (previous?.lastSuccessfulCheckAt) {
44
+ if (validator(previous.state.etag))
45
+ headers['If-None-Match'] = previous.state.etag;
46
+ if (validator(previous.state.lastModified))
47
+ headers['If-Modified-Since'] = previous.state.lastModified;
48
+ }
49
+ const controller = new AbortController();
50
+ let timedOut = false;
51
+ let response;
52
+ let rejectAborted = () => { };
53
+ const aborted = new Promise((_, reject) => { rejectAborted = reject; });
54
+ const cancelResponse = () => {
55
+ try {
56
+ if (response)
57
+ void Promise.resolve(response.cancel()).catch(() => { });
58
+ }
59
+ catch { /* Cleanup must not hide the poll result. */ }
60
+ };
61
+ const abort = () => { controller.abort(); cancelResponse(); rejectAborted(new Error('request_aborted')); };
62
+ const timer = setTimeout(() => { timedOut = true; abort(); }, this.requestTimeoutMs);
63
+ context.signal.addEventListener('abort', abort, { once: true });
64
+ if (context.signal.aborted)
65
+ abort();
66
+ let stage = 'network';
67
+ const request = async () => {
68
+ if (controller.signal.aborted)
69
+ throw new Error('request_aborted');
70
+ response = await this.getHttpClient(context).request({ url: this.url, method: 'GET', headers, signal: controller.signal });
71
+ if (controller.signal.aborted) {
72
+ cancelResponse();
73
+ throw new Error('request_aborted');
74
+ }
75
+ const nextState = {};
76
+ const etag = response.headers['etag'];
77
+ const modified = response.headers['last-modified'];
78
+ if (validator(etag))
79
+ nextState.etag = etag;
80
+ if (validator(modified))
81
+ nextState.lastModified = modified;
82
+ if (response.status === 304) {
83
+ cancelResponse();
84
+ if (!headers['If-None-Match'] && !headers['If-Modified-Since'])
85
+ return { outcome: 'error', error: { kind: 'http', message: 'Unexpected RSS 304 without a cached validator' } };
86
+ return { outcome: 'not_modified', nextState: { ...previous?.state, ...nextState } };
87
+ }
88
+ if (response.status < 200 || response.status >= 300) {
89
+ cancelResponse();
90
+ const retry = response.headers['retry-after'];
91
+ const retryMs = retry && /^\d+$/.test(retry) ? Number(retry) * 1000 : retry ? Date.parse(retry) - context.now.getTime() : NaN;
92
+ return { outcome: 'error', error: { kind: [401, 403].includes(response.status) ? 'auth' : 'http', message: `RSS HTTP ${response.status}`, ...(Number.isSafeInteger(retryMs) && retryMs >= 0 ? { retryAfterMs: retryMs } : {}) } };
93
+ }
94
+ stage = 'parse';
95
+ const contentType = response.headers['content-type']?.split(';')[0]?.trim().toLowerCase();
96
+ const length = response.headers['content-length'];
97
+ if (contentType && !['application/rss+xml', 'application/xml', 'text/xml'].includes(contentType)) {
98
+ cancelResponse();
99
+ throw new RssParseError('invalid_content_type');
100
+ }
101
+ if (length && Number(length) > this.limits.maxBytes) {
102
+ cancelResponse();
103
+ throw new RssParseError('response_too_large');
104
+ }
105
+ if (!response.body)
106
+ throw new RssParseError('empty_response');
107
+ const iterator = response.body[Symbol.asyncIterator]();
108
+ const chunks = [];
109
+ let bytes = 0;
110
+ while (true) {
111
+ stage = 'network';
112
+ const { done, value } = await iterator.next();
113
+ if (controller.signal.aborted)
114
+ throw new Error('request_aborted');
115
+ if (done)
116
+ break;
117
+ stage = 'parse';
118
+ if (!(value instanceof Uint8Array))
119
+ throw new TypeError('HTTP body must yield byte chunks');
120
+ bytes += value.byteLength;
121
+ if (bytes > this.limits.maxBytes) {
122
+ cancelResponse();
123
+ throw new RssParseError('response_too_large');
124
+ }
125
+ chunks.push(value);
126
+ }
127
+ stage = 'parse';
128
+ const xml = new TextDecoder('utf-8', { fatal: true }).decode(Buffer.concat(chunks, bytes));
129
+ return { outcome: 'ok', observations: this.parse(xml, { observedAt: context.now }), nextState };
130
+ };
131
+ try {
132
+ return await Promise.race([request(), aborted]);
133
+ }
134
+ catch (error) {
135
+ // Do not copy URLs, headers or arbitrary fetch/parser exception messages into stored errors.
136
+ const kind = timedOut ? 'timeout' : context.signal.aborted ? 'cancelled' : stage;
137
+ return { outcome: 'error', error: { kind, message: error instanceof RssParseError ? error.code : `RSS ${kind} failure` } };
138
+ }
139
+ finally {
140
+ clearTimeout(timer);
141
+ context.signal.removeEventListener('abort', abort);
142
+ cancelResponse();
143
+ }
144
+ }
145
+ }
146
+ //# sourceMappingURL=RssStatusAdapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RssStatusAdapter.js","sourceRoot":"","sources":["../../src/RssStatusAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEhG,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AASvD,kGAAkG;AAClG,MAAM,OAAgB,gBAAiB,SAAQ,mBAAmB;IACvD,GAAG,CAAS;IACZ,gBAAgB,CAAS;IACzB,MAAM,CAAkC;IAEjD,YAAY,OAAyB;QACnC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACjC,IAAI,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;YAAE,MAAM,IAAI,SAAS,CAAC,sDAAsD,CAAC,CAAC;QAC7J,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC,IAAI,CAAC;QACpB,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,CAAC,OAAO,CAAC,gBAAgB,IAAI,MAAM,EAAE,kBAAkB,CAAC,CAAC;QACjG,IAAI,IAAI,CAAC,gBAAgB,GAAG,aAAa;YAAE,MAAM,IAAI,SAAS,CAAC,+CAA+C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CAAC,GAAG,EAAE,EAAE,UAAU,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC;QAClG,CAAC,CAAC;QACF,IAAI,CAAC;YAAC,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC;QAAC,CAAC;QACxD,OAAO,KAAK,EAAE,CAAC;YACb,6FAA6F;YAC7F,MAAM,IAAI,GAAG,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC;YACjF,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,YAAY,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,IAAI,UAAU,EAAE,EAAE,CAAC;QAC7H,CAAC;gBAAS,CAAC;YACT,YAAY,CAAC,KAAK,CAAC,CAAC;YACpB,OAAO,CAAC,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YACnD,cAAc,EAAE,CAAC;QACnB,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,17 @@
1
+ import type { Logger, HttpClient } from '@depwatch/core';
2
+ export * from './parsing.js';
3
+ export * from './RssStatusAdapter.js';
4
+ export interface RssSourceContext {
5
+ readonly url: string;
6
+ readonly httpClient: HttpClient;
7
+ /** @deprecated Use httpClient. */
8
+ readonly fetch?: typeof globalThis.fetch;
9
+ readonly logger: Logger;
10
+ }
11
+ /** Compatibility context helper; use adapter options or PollContext.httpClient for polling. */
12
+ export declare function createRssSourceContext(options: {
13
+ url: string;
14
+ httpClient?: HttpClient;
15
+ fetch?: typeof globalThis.fetch;
16
+ logger: Logger;
17
+ }): RssSourceContext;
@@ -0,0 +1,11 @@
1
+ import { createFetchHttpClient } from '@depwatch/core';
2
+ export * from './parsing.js';
3
+ export * from './RssStatusAdapter.js';
4
+ /** Compatibility context helper; use adapter options or PollContext.httpClient for polling. */
5
+ export function createRssSourceContext(options) {
6
+ const url = new URL(options.url);
7
+ if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password)
8
+ throw new TypeError('RSS URL must be HTTP(S) without embedded credentials');
9
+ return Object.freeze({ url: url.href, httpClient: options.httpClient ?? createFetchHttpClient(options.fetch), ...(options.fetch ? { fetch: options.fetch } : {}), logger: options.logger.child({ component: 'depwatch.source-rss' }) });
10
+ }
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAEvD,cAAc,cAAc,CAAC;AAC7B,cAAc,uBAAuB,CAAC;AAStC,+FAA+F;AAC/F,MAAM,UAAU,sBAAsB,CAAC,OAAkG;IACvI,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACjC,IAAI,CAAC,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ;QAAE,MAAM,IAAI,SAAS,CAAC,sDAAsD,CAAC,CAAC;IAC7J,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,IAAI,qBAAqB,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,SAAS,EAAE,qBAAqB,EAAE,CAAC,EAAE,CAAC,CAAC;AAC1O,CAAC"}
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,42 @@
1
+ export interface RssItem {
2
+ title: string;
3
+ descriptionHtml: string;
4
+ guid: string;
5
+ link: string;
6
+ pubDate: string;
7
+ }
8
+ export interface RssFeed {
9
+ title: string;
10
+ items: RssItem[];
11
+ }
12
+ export interface ParseLimits {
13
+ maxBytes?: number;
14
+ maxItems?: number;
15
+ maxDepth?: number;
16
+ }
17
+ export declare class RssParseError extends Error {
18
+ readonly code: string;
19
+ constructor(code: string);
20
+ }
21
+ /** Pure, bounded RSS 2.0 parsing. Descriptions must contain escaped HTML or CDATA. */
22
+ export declare function parseRss(xml: string, limits?: ParseLimits): RssFeed;
23
+ /** One HTML decoding pass following XML decoding; never recursively decode strings. */
24
+ export declare function htmlToPlainText(html: string, maxBytes?: number): string;
25
+ /** RFC2822 with an explicit offset, including weekday validation; no local-zone fallback. */
26
+ export declare function parsePublicationDate(value: string): Date | null;
27
+ export interface LocalDateTime {
28
+ year: number;
29
+ month: number;
30
+ day: number;
31
+ hour: number;
32
+ minute: number;
33
+ }
34
+ export type DateTimeResult = {
35
+ ok: true;
36
+ value: Date;
37
+ } | {
38
+ ok: false;
39
+ issue: 'invalid_local_time' | 'nonexistent_local_time' | 'ambiguous_local_time';
40
+ };
41
+ /** Reject DST gaps/folds instead of accepting Luxon's default normalization/offset choice. */
42
+ export declare function parseZonedDateTime(parts: LocalDateTime, zone: string): DateTimeResult;
@@ -0,0 +1,133 @@
1
+ import { XMLParser, XMLValidator } from 'fast-xml-parser';
2
+ import { convert } from 'html-to-text';
3
+ import { DateTime } from 'luxon';
4
+ export class RssParseError extends Error {
5
+ code;
6
+ constructor(code) {
7
+ super(code);
8
+ this.code = code;
9
+ this.name = 'RssParseError';
10
+ }
11
+ }
12
+ function positive(value, name) {
13
+ if (!Number.isSafeInteger(value) || value <= 0)
14
+ throw new TypeError(`${name} must be a positive integer`);
15
+ return value;
16
+ }
17
+ function text(value) {
18
+ if (value === undefined || value === '')
19
+ return '';
20
+ if (typeof value === 'string')
21
+ return value;
22
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
23
+ const node = value;
24
+ if (Object.keys(node).every(k => k === '#text' || k.startsWith('@_')))
25
+ return text(node['#text']);
26
+ }
27
+ throw new RssParseError('invalid_text_node');
28
+ }
29
+ function object(value) {
30
+ if (!value || typeof value !== 'object' || Array.isArray(value))
31
+ throw new RssParseError('invalid_rss_structure');
32
+ return value;
33
+ }
34
+ /** Pure, bounded RSS 2.0 parsing. Descriptions must contain escaped HTML or CDATA. */
35
+ export function parseRss(xml, limits = {}) {
36
+ const maxBytes = positive(limits.maxBytes ?? 1_000_000, 'maxBytes');
37
+ const maxItems = positive(limits.maxItems ?? 1000, 'maxItems');
38
+ const maxDepth = positive(limits.maxDepth ?? 64, 'maxDepth');
39
+ if (typeof xml !== 'string')
40
+ throw new TypeError('XML must be a string');
41
+ if (Buffer.byteLength(xml, 'utf8') > maxBytes)
42
+ throw new RssParseError('response_too_large');
43
+ if (/[\u0000-\u0008\u000B\u000C\u000E-\u001F\uD800-\uDFFF\uFFFE\uFFFF]/u.test(xml))
44
+ throw new RssParseError('invalid_xml_character');
45
+ // Reject declarations before invoking any XML engine, including internal/external entities.
46
+ if (/<!\s*(?:DOCTYPE|ENTITY)\b/i.test(xml))
47
+ throw new RssParseError('forbidden_xml_declaration');
48
+ // CDATA/comments are opaque to XML entity processing. Only XML's five named entities are allowed.
49
+ const structural = xml.replace(/<!\[CDATA\[[\s\S]*?\]\]>|<!--[\s\S]*?-->/g, '');
50
+ if (/&(?!amp;|lt;|gt;|apos;|quot;|#\d+;|#x[\da-f]+;)/i.test(structural))
51
+ throw new RssParseError('invalid_xml_entity');
52
+ for (const match of structural.matchAll(/&#(x[\da-f]+|\d+);/gi)) {
53
+ const literal = match[1];
54
+ const cp = /^x/i.test(literal) ? parseInt(literal.slice(1), 16) : Number(literal);
55
+ if (!(cp === 9 || cp === 10 || cp === 13 || (cp >= 32 && cp <= 0xD7FF) || (cp >= 0xE000 && cp <= 0xFFFD) || (cp >= 0x10000 && cp <= 0x10FFFF)))
56
+ throw new RssParseError('invalid_xml_character');
57
+ }
58
+ let depth = 0;
59
+ for (const match of structural.matchAll(/<\/?[A-Za-z_][\w.:-]*(?:\s+(?:[^<>"']|"[^"]*"|'[^']*')*)?\s*\/?>/g)) {
60
+ const tag = match[0];
61
+ if (tag.startsWith('</'))
62
+ depth--;
63
+ else if (!tag.endsWith('/>'))
64
+ depth++;
65
+ if (depth > maxDepth)
66
+ throw new RssParseError('xml_too_deep');
67
+ }
68
+ if (XMLValidator.validate(xml) !== true)
69
+ throw new RssParseError('malformed_xml');
70
+ const parser = new XMLParser({ ignoreAttributes: false, parseTagValue: false, parseAttributeValue: false, trimValues: false, processEntities: true });
71
+ let document;
72
+ try {
73
+ document = object(parser.parse(xml));
74
+ }
75
+ catch {
76
+ throw new RssParseError('malformed_xml');
77
+ }
78
+ if (Object.keys(document).some(k => k !== 'rss' && k !== '?xml'))
79
+ throw new RssParseError('invalid_rss_structure');
80
+ const rss = object(document.rss);
81
+ if (rss['@_version'] !== '2.0')
82
+ throw new RssParseError('unsupported_rss_version');
83
+ const channel = object(rss.channel);
84
+ const raw = channel.item === undefined ? [] : Array.isArray(channel.item) ? channel.item : [channel.item];
85
+ if (raw.length > maxItems)
86
+ throw new RssParseError('too_many_items');
87
+ return {
88
+ title: text(channel.title),
89
+ items: raw.map(value => {
90
+ const item = object(value);
91
+ return { title: text(item.title), descriptionHtml: text(item.description), guid: text(item.guid).trim(), link: text(item.link).trim(), pubDate: text(item.pubDate).trim() };
92
+ }),
93
+ };
94
+ }
95
+ /** One HTML decoding pass following XML decoding; never recursively decode strings. */
96
+ export function htmlToPlainText(html, maxBytes = 1_000_000) {
97
+ positive(maxBytes, 'maxBytes');
98
+ if (Buffer.byteLength(html, 'utf8') > maxBytes)
99
+ throw new RssParseError('html_too_large');
100
+ return convert(html, {
101
+ wordwrap: false,
102
+ limits: { maxInputLength: maxBytes, maxDepth: 64 },
103
+ selectors: [{ selector: 'a', options: { ignoreHref: true } }, { selector: 'img', format: 'skip' }, { selector: 'script', format: 'skip' }, { selector: 'style', format: 'skip' }],
104
+ }).replace(/\s+/g, ' ').trim();
105
+ }
106
+ /** RFC2822 with an explicit offset, including weekday validation; no local-zone fallback. */
107
+ export function parsePublicationDate(value) {
108
+ if (!/(?:[+-]\d{4}|GMT|UT)$/i.test(value.trim()))
109
+ return null;
110
+ const parsed = DateTime.fromRFC2822(value, { setZone: true });
111
+ return parsed.isValid ? parsed.toJSDate() : null;
112
+ }
113
+ /** Reject DST gaps/folds instead of accepting Luxon's default normalization/offset choice. */
114
+ export function parseZonedDateTime(parts, zone) {
115
+ const fields = ['year', 'month', 'day', 'hour', 'minute'];
116
+ if (!parts || fields.some(key => !Number.isSafeInteger(parts[key])) || Object.keys(parts).some(key => !fields.includes(key)))
117
+ return { ok: false, issue: 'invalid_local_time' };
118
+ let parsed;
119
+ try {
120
+ parsed = DateTime.fromObject(parts, { zone });
121
+ }
122
+ catch {
123
+ return { ok: false, issue: 'invalid_local_time' };
124
+ }
125
+ if (!parsed.isValid)
126
+ return { ok: false, issue: 'invalid_local_time' };
127
+ if (Object.entries(parts).some(([key, value]) => parsed.get(key) !== value))
128
+ return { ok: false, issue: 'nonexistent_local_time' };
129
+ if (parsed.getPossibleOffsets().length !== 1)
130
+ return { ok: false, issue: 'ambiguous_local_time' };
131
+ return { ok: true, value: parsed.toJSDate() };
132
+ }
133
+ //# sourceMappingURL=parsing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parsing.js","sourceRoot":"","sources":["../../src/parsing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,QAAQ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,KAAK,EAAE,KAAK,EAAE,wBAAwB,EAAE,CAAC;IAC1J,IAAI,MAAM,CAAC,kBAAkB,EAAE,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,sBAAsB,EAAE,CAAC;IAClG,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC;AAChD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@depwatch/source-rss",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.7",
4
+ "type": "module",
5
+ "engines": {
6
+ "node": ">=18"
7
+ },
8
+ "main": "./dist/cjs/index.js",
9
+ "module": "./dist/esm/index.js",
10
+ "types": "./dist/esm/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "import": {
14
+ "types": "./dist/esm/index.d.ts",
15
+ "default": "./dist/esm/index.js"
16
+ },
17
+ "require": {
18
+ "types": "./dist/cjs/index.d.ts",
19
+ "default": "./dist/cjs/index.js"
20
+ }
21
+ }
22
+ },
23
+ "files": [
24
+ "dist",
25
+ "README.md",
26
+ "LICENSE"
27
+ ],
28
+ "sideEffects": false,
29
+ "scripts": {
30
+ "build": "node ../../scripts/build.mjs",
31
+ "prepack": "npm run build"
32
+ },
33
+ "peerDependencies": {
34
+ "@depwatch/core": "0.0.7"
35
+ },
36
+ "dependencies": {
37
+ "fast-xml-parser": "^5.12.1",
38
+ "html-to-text": "^9.0.5",
39
+ "luxon": "^3.7.2"
40
+ },
41
+ "description": "Streaming RSS status polling and safe XML parsing for provider-specific availability adapters.",
42
+ "license": "MIT",
43
+ "keywords": [
44
+ "availability",
45
+ "monitoring",
46
+ "status",
47
+ "depwatch"
48
+ ],
49
+ "publishConfig": {
50
+ "registry": "https://registry.npmjs.org/",
51
+ "tag": "next"
52
+ }
53
+ }