@cloudhal/schedules 0.6.4 → 0.7.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.
@@ -12,6 +12,7 @@
12
12
 
13
13
  import { instantMs, MINUTE_MS } from "./instant.js";
14
14
  import { plainLine } from "./text.js";
15
+ import { cdataParts, elementContents, mediaUrlValues } from "./xml-scan.js";
15
16
 
16
17
  /** Documents larger than this are not parsed (`feed-invalid`). */
17
18
  export const MAX_DOCUMENT_CHARS = 5_000_000;
@@ -153,15 +154,10 @@ const ENTITIES = {
153
154
 
154
155
  /** Element text: CDATA unwrapped, the five XML entities and numeric references decoded. */
155
156
  export function xmlText(raw ) {
156
- const parts = [];
157
- const re = /<!\[CDATA\[([\s\S]*?)\]\]>/g;
158
- let last = 0;
159
- for (const m of raw.matchAll(re)) {
160
- parts.push(decode(raw.slice(last, m.index)), m[1] );
161
- last = (m.index ?? 0) + m[0].length;
162
- }
163
- parts.push(decode(raw.slice(last)));
164
- return parts.join("").trim();
157
+ return cdataParts(raw)
158
+ .map((p) => decode(p.text) + (p.cdata ?? ""))
159
+ .join("")
160
+ .trim();
165
161
  }
166
162
 
167
163
  function decode(s ) {
@@ -178,30 +174,20 @@ function safeChar(code ) {
178
174
  : undefined;
179
175
  }
180
176
 
181
- function escapeName(name ) {
182
- return name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
183
- }
184
-
185
177
  /** Every `<name>…</name>` block (non-nesting elements such as `item`, `url`). */
186
178
  function blocks(text , name ) {
187
- const n = escapeName(name);
188
- return [...text.matchAll(new RegExp(`<${n}(?:\\s[^>]*)?>([\\s\\S]*?)</${n}\\s*>`, "g"))].map(
189
- (m) => m[1] ,
190
- );
179
+ return elementContents(text, name);
191
180
  }
192
181
 
193
182
  /** The text of the first `<name>` child, or undefined. */
194
183
  function child(block , name ) {
195
- const n = escapeName(name);
196
- const m = new RegExp(`<${n}(?:\\s[^>]*)?>([\\s\\S]*?)</${n}\\s*>`).exec(block);
197
- return m ? xmlText(m[1] ) : undefined;
184
+ const first = elementContents(block, name, 1)[0];
185
+ return first === undefined ? undefined : xmlText(first);
198
186
  }
199
187
 
200
188
  /** `url="…"` attributes of `<enclosure>` and `<media:content>`. */
201
189
  function mediaAttributes(block ) {
202
- return [...block.matchAll(/<(?:enclosure|media:content)\b[^>]*?\burl\s*=\s*(["'])(.*?)\1/g)].map(
203
- (m) => decode(m[2] ),
204
- );
190
+ return mediaUrlValues(block).map(decode);
205
191
  }
206
192
 
207
193
  /** A URL's path as the site routes it (no query, fragment or trailing slash), or undefined. */
@@ -214,7 +200,10 @@ export function urlPath(url ) {
214
200
  } catch {
215
201
  // keep the encoded form
216
202
  }
217
- return p.length > 1 ? p.replace(/\/+$/, "") || "/" : p || "/";
203
+ // A linear trim of trailing "/" (`/\/+$/` backtracks on a long run of "/" inside the path).
204
+ let end = p.length;
205
+ while (end > 1 && p[end - 1] === "/") end--;
206
+ return p.length > 1 ? p.slice(0, end) : p || "/";
218
207
  } catch {
219
208
  return undefined;
220
209
  }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Linear scanners for the feed check (#174). Each returns what the regex it replaces returned,
3
+ * without rescanning to the end of the document for every opener whose closer is missing:
4
+ * `/<!\[CDATA\[([\s\S]*?)\]\]>/g`, `<name(?:\s[^>]*)?>([\s\S]*?)</name\s*>` and
5
+ * `/<(?:enclosure|media:content)\b[^>]*?\burl\s*=\s*(["'])(.*?)\1/g` were quadratic on such input.
6
+ */
7
+ /** `raw` split into text outside CDATA sections and their contents, in order. */
8
+ export declare function cdataParts(raw: string): {
9
+ text: string;
10
+ cdata?: string;
11
+ }[];
12
+ /**
13
+ * The contents of every `<name>…</name>` element (an opening tag `<name>` or `<name …>`, the
14
+ * nearest `</name>` closer, whitespace allowed before its `>`), as group 1 of the global regex.
15
+ * `limit` stops after that many (1 for the first, as a non-global `exec` finds it).
16
+ */
17
+ export declare function elementContents(text: string, name: string, limit?: number): string[];
18
+ /**
19
+ * The `url="…"` values of `<enclosure>` and `<media:content>` tags, as group 2 of the regex: the
20
+ * first `url` attribute inside the tag (before its `>`) whose quoted value closes on its line.
21
+ */
22
+ export declare function mediaUrlValues(text: string): string[];
@@ -0,0 +1,173 @@
1
+ /**
2
+ * Linear scanners for the feed check (#174). Each returns what the regex it replaces returned,
3
+ * without rescanning to the end of the document for every opener whose closer is missing:
4
+ * `/<!\[CDATA\[([\s\S]*?)\]\]>/g`, `<name(?:\s[^>]*)?>([\s\S]*?)</name\s*>` and
5
+ * `/<(?:enclosure|media:content)\b[^>]*?\burl\s*=\s*(["'])(.*?)\1/g` were quadratic on such input.
6
+ */
7
+
8
+ const isSpace = (ch ) => ch !== undefined && /\s/.test(ch);
9
+ const isWord = (ch ) => ch !== undefined && /\w/.test(ch);
10
+ /** What `.` does not match without the `s` flag. */
11
+ const isLineEnd = (ch ) => ch === "\n" || ch === "\r" || ch === "\u2028" || ch === "\u2029";
12
+
13
+ const CDATA_OPEN = "<![CDATA[";
14
+ const CDATA_CLOSE = "]]>";
15
+
16
+ /** `raw` split into text outside CDATA sections and their contents, in order. */
17
+ export function cdataParts(raw ) {
18
+ const out = [];
19
+ let from = 0;
20
+ for (;;) {
21
+ const open = raw.indexOf(CDATA_OPEN, from);
22
+ if (open < 0) break;
23
+ const close = raw.indexOf(CDATA_CLOSE, open + CDATA_OPEN.length);
24
+ // No closer after this opener means none after any later one either.
25
+ if (close < 0) break;
26
+ out.push({ text: raw.slice(from, open), cdata: raw.slice(open + CDATA_OPEN.length, close) });
27
+ from = close + CDATA_CLOSE.length;
28
+ }
29
+ out.push({ text: raw.slice(from) });
30
+ return out;
31
+ }
32
+
33
+ /**
34
+ * The contents of every `<name>…</name>` element (an opening tag `<name>` or `<name …>`, the
35
+ * nearest `</name>` closer, whitespace allowed before its `>`), as group 1 of the global regex.
36
+ * `limit` stops after that many (1 for the first, as a non-global `exec` finds it).
37
+ */
38
+ export function elementContents(text , name , limit = Infinity) {
39
+ const out = [];
40
+ const open = `<${name}`;
41
+ const close = `</${name}`;
42
+ // Searches start at growing positions, so each "first … at or after" answer can be kept.
43
+ let gt ; // -1: no `>` left
44
+ const nextGt = (from ) => {
45
+ if (gt === undefined || (gt >= 0 && gt < from)) gt = text.indexOf(">", from);
46
+ return gt;
47
+ };
48
+ let closer ;
49
+ const nextCloser = (from ) => {
50
+ if (closer === null) return undefined;
51
+ if (closer && closer.at >= from) return closer;
52
+ for (let c = text.indexOf(close, from); c >= 0; c = text.indexOf(close, c + 1)) {
53
+ let j = c + close.length;
54
+ while (isSpace(text[j])) j++;
55
+ if (text[j] === ">") {
56
+ closer = { at: c, end: j + 1 };
57
+ return closer;
58
+ }
59
+ }
60
+ closer = null;
61
+ return undefined;
62
+ };
63
+ for (let at = text.indexOf(open); at >= 0 && out.length < limit; ) {
64
+ const after = at + open.length;
65
+ let start ;
66
+ if (text[after] === ">") start = after + 1;
67
+ else if (isSpace(text[after])) {
68
+ const g = nextGt(after + 1);
69
+ if (g < 0) break; // no `>` left: no later opening tag can end either
70
+ start = g + 1;
71
+ }
72
+ if (start === undefined) {
73
+ at = text.indexOf(open, at + 1);
74
+ continue;
75
+ }
76
+ const c = nextCloser(start);
77
+ if (!c) break; // no closer left: no later element can end either
78
+ out.push(text.slice(start, c.at));
79
+ at = text.indexOf(open, c.end);
80
+ }
81
+ return out;
82
+ }
83
+
84
+ const MEDIA_TAGS = ["<enclosure", "<media:content"] ;
85
+
86
+ /**
87
+ * The next `<enclosure` or `<media:content` tag at or after a position, followed by a non-word
88
+ * character (the regex's `\b`). Positions only grow, so each name's next tag is kept between calls.
89
+ */
90
+ function mediaTagFinder(text ) {
91
+ const next = new Map (); // -1: none left
92
+ const find = (tag , from ) => {
93
+ const known = next.get(tag);
94
+ if (known !== undefined && (known < 0 || known >= from)) return known;
95
+ let at = text.indexOf(tag, from);
96
+ while (at >= 0 && isWord(text[at + tag.length])) at = text.indexOf(tag, at + 1);
97
+ next.set(tag, at);
98
+ return at;
99
+ };
100
+ return (from ) => {
101
+ let best ;
102
+ for (const tag of MEDIA_TAGS) {
103
+ const at = find(tag, from);
104
+ if (at >= 0 && (!best || at < best.at)) best = { at, nameEnd: at + tag.length };
105
+ }
106
+ return best;
107
+ };
108
+ }
109
+
110
+ /**
111
+ * `indexOf(needle, from)` that keeps its last answer: a query at or after the last one, and not
112
+ * past the kept position, is answered without searching again (-1 stays -1).
113
+ */
114
+ function forwardFinder(text , needle ) {
115
+ let asked = -1;
116
+ let found = -1;
117
+ return (from) => {
118
+ if (asked >= 0 && from >= asked && (found < 0 || found >= from)) return found;
119
+ asked = from;
120
+ found = text.indexOf(needle, from);
121
+ return found;
122
+ };
123
+ }
124
+
125
+ /** At `p`: `url`, optional spaces, `=`, optional spaces, a quote; the value and where it ends. */
126
+ function urlAttribute(text , p ) {
127
+ if (isWord(text[p - 1]) || !text.startsWith("url", p)) return undefined;
128
+ let i = p + 3;
129
+ while (isSpace(text[i])) i++;
130
+ if (text[i] !== "=") return undefined;
131
+ i++;
132
+ while (isSpace(text[i])) i++;
133
+ const q = text[i];
134
+ if (q !== '"' && q !== "'") return undefined;
135
+ for (let j = i + 1; j < text.length; j++) {
136
+ const ch = text[j] ;
137
+ if (ch === q) return { value: text.slice(i + 1, j), end: j + 1 };
138
+ if (isLineEnd(ch)) return undefined;
139
+ }
140
+ return undefined;
141
+ }
142
+
143
+ /**
144
+ * The `url="…"` values of `<enclosure>` and `<media:content>` tags, as group 2 of the regex: the
145
+ * first `url` attribute inside the tag (before its `>`) whose quoted value closes on its line.
146
+ */
147
+ export function mediaUrlValues(text ) {
148
+ const out = [];
149
+ const nextTag = mediaTagFinder(text);
150
+ // Tags are visited in order, so the next `>` and the next `url` are each found once and kept
151
+ // until a query passes them: a per-tag `indexOf` made `<enclosure>` × n quadratic.
152
+ const nextGt = forwardFinder(text, ">");
153
+ const nextUrl = forwardFinder(text, "url");
154
+ let from = 0;
155
+ for (let tag = nextTag(from); tag; tag = nextTag(from)) {
156
+ const gt = nextGt(tag.nameEnd);
157
+ const stop = gt < 0 ? text.length : gt;
158
+ let found ;
159
+ for (let p = nextUrl(tag.nameEnd); p >= 0 && p < stop; p = nextUrl(p + 1)) {
160
+ found = urlAttribute(text, p);
161
+ if (found) break;
162
+ }
163
+ if (found) {
164
+ out.push(found.value);
165
+ from = found.end;
166
+ continue;
167
+ }
168
+ // Every later tag that starts before this `>` sees the same candidates, which all failed.
169
+ if (gt < 0) break;
170
+ from = gt + 1;
171
+ }
172
+ return out;
173
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/schedules",
3
- "version": "0.6.4",
3
+ "version": "0.7.0",
4
4
  "description": "Cloudhal schedules (RFC-0008): the time model, the RRULE subset and its sentences, releases (pin, closure, rl1: hash, compose, conflicts, state machine), automation and run state, taint and budget accounting, the Scheduler and runDue, and the SQLite/D1 store. Worker-safe.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://cloudhal.dev",
@@ -44,8 +44,8 @@
44
44
  "README.md"
45
45
  ],
46
46
  "dependencies": {
47
- "@cloudhal/core-contracts": "0.6.4",
48
- "@cloudhal/storage": "0.6.4",
47
+ "@cloudhal/core-contracts": "0.7.0",
48
+ "@cloudhal/storage": "0.7.0",
49
49
  "temporal-polyfill": "1.0.6"
50
50
  },
51
51
  "peerDependencies": {