@astratra/web-search 1.2.2 → 1.2.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astratra/web-search",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Web search for AI agents: several engines (Serper, Brave, Tavily, Exa, or a gateway of your own) with automatic fallback, rank fusion without content mixing (one entry = one page = one site), verbatim passages with their position, SSRF-safe page reading, content framed as data, cache, quotas, cost report, tool definitions and an MCP server, and an integrated read-only browser (shared, isolated Chromium service) for JavaScript pages, site links and site search.",
5
5
  "engines": {
6
6
  "node": ">=22"
@@ -67,14 +67,27 @@ function parseRobots(text) {
67
67
  return { groups };
68
68
  }
69
69
 
70
- /** Le motif d'une règle en expression régulière (ancrée au début). */
71
- function patternOf(path) {
72
- const anchored = path.endsWith('$');
73
- const body = (anchored ? path.slice(0, -1) : path)
74
- .split('*')
75
- .map((part) => part.replace(/[.+?^${}()|[\]\\]/g, '\\$&'))
76
- .join('.*');
77
- return new RegExp(`^${body}${anchored ? '$' : ''}`);
70
+ /**
71
+ * Vrai si le chemin commence par la règle (`*` : n'importe quelle suite de caractères ; `$` final : la fin du chemin).
72
+ *
73
+ * Sans expression régulière : chaque morceau entre deux étoiles est cherché au plus tôt après le précédent, ce qui
74
+ * suffit quand le seul joker est l'étoile. Le temps reste borné par la longueur du chemin fois celle de la règle ; une
75
+ * règle écrite par le site (« /*a*a*a…z ») ne peut pas figer le service.
76
+ */
77
+ function ruleMatches(rule, path) {
78
+ const anchored = rule.endsWith('$');
79
+ const parts = (anchored ? rule.slice(0, -1) : rule).split('*');
80
+ if (!path.startsWith(parts[0])) return false;
81
+ let position = parts[0].length;
82
+ if (parts.length === 1) return !anchored || position === path.length;
83
+ for (let i = 1; i < parts.length - 1; i += 1) {
84
+ const at = path.indexOf(parts[i], position);
85
+ if (at === -1) return false;
86
+ position = at + parts[i].length;
87
+ }
88
+ const last = parts[parts.length - 1];
89
+ if (!anchored) return path.indexOf(last, position) !== -1;
90
+ return path.length - last.length >= position && path.endsWith(last);
78
91
  }
79
92
 
80
93
  /** Les règles qui s'appliquent à un agent (jeton exact, sinon `*`). */
@@ -98,7 +111,7 @@ function robotsAllows(parsed, target, token = AGENT_TOKEN) {
98
111
  if (url.pathname === '/robots.txt') return true;
99
112
  let best = null;
100
113
  for (const rule of rulesFor(parsed, token)) {
101
- if (!patternOf(rule.path).test(path)) continue;
114
+ if (!ruleMatches(rule.path, path)) continue;
102
115
  const length = rule.path.replace(/\$$/, '').length;
103
116
  if (!best || length > best.length || (length === best.length && rule.allow && !best.allow)) best = { length, allow: rule.allow };
104
117
  }
@@ -164,4 +177,4 @@ function createRobotsCache(options = {}) {
164
177
  return { check, clear: () => entries.clear(), size: () => entries.size };
165
178
  }
166
179
 
167
- module.exports = { parseRobots, robotsAllows, createRobotsCache, ROBOTS_AGENT_TOKEN: AGENT_TOKEN };
180
+ module.exports = { parseRobots, robotsAllows, ruleMatches, createRobotsCache, ROBOTS_AGENT_TOKEN: AGENT_TOKEN };
package/src/text.js CHANGED
@@ -18,9 +18,11 @@ function decodeEntities(text) {
18
18
  const code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
19
19
  return Number.isFinite(code) && code > 0 && code <= 0x10ffff && !(code >= 0xd800 && code <= 0xdfff) ? String.fromCodePoint(code) : '';
20
20
  }
21
- if (NAMED[body] !== undefined) return NAMED[body];
22
- const named = NAMED[body.toLowerCase()];
23
- if (named === undefined) return whole;
21
+ /* Seules les entrées propres de la table comptent : « &constructor; » n'est pas une entité. */
22
+ if (Object.hasOwn(NAMED, body)) return NAMED[body];
23
+ const lower = body.toLowerCase();
24
+ if (!Object.hasOwn(NAMED, lower)) return whole;
25
+ const named = NAMED[lower];
24
26
  /* &Eacute; → É : la majuscule suit celle du nom de l'entité. */
25
27
  return /^[A-Z][a-z]+$/.test(body) && /^\p{Ll}$/u.test(named) ? named.toUpperCase() : named;
26
28
  });