@duckmind/dm-windows-x64 0.63.0 → 0.63.4

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.
Files changed (71) hide show
  1. package/dm.exe +0 -0
  2. package/extensions/.dm-extensions.json +138 -2
  3. package/extensions/dm-web-access/SECURITY.md +5 -0
  4. package/extensions/dm-web-access/activity.js +65 -0
  5. package/extensions/dm-web-access/anysearch.js +158 -0
  6. package/extensions/dm-web-access/auth-fetch.js +131 -0
  7. package/extensions/dm-web-access/bocha.js +214 -0
  8. package/extensions/dm-web-access/brave.js +196 -0
  9. package/extensions/dm-web-access/brightdata-unlocker.js +202 -0
  10. package/extensions/dm-web-access/brightdata.js +334 -0
  11. package/extensions/dm-web-access/chrome-cookies.js +627 -0
  12. package/extensions/dm-web-access/content-find.js +114 -0
  13. package/extensions/dm-web-access/credential-source.js +150 -0
  14. package/extensions/dm-web-access/curator-page.js +3559 -0
  15. package/extensions/dm-web-access/curator-server.js +691 -0
  16. package/extensions/dm-web-access/data-uri-sanitize.js +312 -0
  17. package/extensions/dm-web-access/datalab-pdf-extract.js +346 -0
  18. package/extensions/dm-web-access/declared-web-links.js +167 -0
  19. package/extensions/dm-web-access/dm-web-fetch-demo.mp4 +0 -0
  20. package/extensions/dm-web-access/duckduckgo.js +118 -0
  21. package/extensions/dm-web-access/exa.js +401 -0
  22. package/extensions/dm-web-access/extract.js +1220 -0
  23. package/extensions/dm-web-access/feature-config.js +24 -0
  24. package/extensions/dm-web-access/fetch-params.js +81 -0
  25. package/extensions/dm-web-access/firecrawl.js +378 -0
  26. package/extensions/dm-web-access/gemini-adc.js +241 -0
  27. package/extensions/dm-web-access/gemini-api.js +258 -0
  28. package/extensions/dm-web-access/gemini-pdf-extract.js +74 -0
  29. package/extensions/dm-web-access/gemini-search.js +889 -0
  30. package/extensions/dm-web-access/gemini-url-context.js +97 -0
  31. package/extensions/dm-web-access/gemini-web-config.js +84 -0
  32. package/extensions/dm-web-access/gemini-web.js +351 -0
  33. package/extensions/dm-web-access/github-api.js +166 -0
  34. package/extensions/dm-web-access/github-extract.js +991 -0
  35. package/extensions/dm-web-access/github-issue-pr.js +750 -0
  36. package/extensions/dm-web-access/index.js +3117 -0
  37. package/extensions/dm-web-access/jina-search.js +242 -0
  38. package/extensions/dm-web-access/kagi.js +255 -0
  39. package/extensions/dm-web-access/kimi-search.js +214 -0
  40. package/extensions/dm-web-access/ollama.js +209 -0
  41. package/extensions/dm-web-access/openai-search.js +510 -0
  42. package/extensions/dm-web-access/package.json +34 -0
  43. package/extensions/dm-web-access/page-query.js +124 -0
  44. package/extensions/dm-web-access/parallel-mcp.js +223 -0
  45. package/extensions/dm-web-access/parallel.js +345 -0
  46. package/extensions/dm-web-access/pdf-extract.js +257 -0
  47. package/extensions/dm-web-access/perplexity.js +151 -0
  48. package/extensions/dm-web-access/querit.js +327 -0
  49. package/extensions/dm-web-access/query-rewrite.js +40 -0
  50. package/extensions/dm-web-access/render-search-error.js +80 -0
  51. package/extensions/dm-web-access/rsc-extract.js +347 -0
  52. package/extensions/dm-web-access/search1api.js +245 -0
  53. package/extensions/dm-web-access/searchinfinity.js +221 -0
  54. package/extensions/dm-web-access/searxng.js +223 -0
  55. package/extensions/dm-web-access/serpbase.js +205 -0
  56. package/extensions/dm-web-access/serpdive.js +238 -0
  57. package/extensions/dm-web-access/serper.js +200 -0
  58. package/extensions/dm-web-access/source-check.js +198 -0
  59. package/extensions/dm-web-access/ssrf-protection.js +436 -0
  60. package/extensions/dm-web-access/storage.js +451 -0
  61. package/extensions/dm-web-access/summary-model-scope.js +83 -0
  62. package/extensions/dm-web-access/summary-review.js +364 -0
  63. package/extensions/dm-web-access/tavily.js +199 -0
  64. package/extensions/dm-web-access/tinyfish.js +325 -0
  65. package/extensions/dm-web-access/utils.js +476 -0
  66. package/extensions/dm-web-access/valyu.js +189 -0
  67. package/extensions/dm-web-access/video-extract.js +336 -0
  68. package/extensions/dm-web-access/xai-search.js +285 -0
  69. package/extensions/dm-web-access/xcrawl.js +221 -0
  70. package/extensions/dm-web-access/youtube-extract.js +279 -0
  71. package/package.json +9 -1
@@ -0,0 +1,436 @@
1
+ import { lookup as dnsLookup } from "node:dns/promises";
2
+ import { existsSync, readFileSync, statSync } from "node:fs";
3
+ import net from "node:net";
4
+ import { getActiveProxy, getWebSearchConfigPath, hasScopedProxyDecision, isProxyBypassedUrl } from "./utils.js";
5
+ const DEFAULT_MAX_REDIRECTS = 5;
6
+ const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
7
+ const LOOPBACK_ALLOW_RANGES = ["127.0.0.0/8", "::1", "::ffff:127.0.0.0/104"];
8
+ const WEB_SEARCH_CONFIG_PATH = getWebSearchConfigPath();
9
+ let cachedConfigRoot = null;
10
+ function loadConfigRoot() {
11
+ if (!existsSync(WEB_SEARCH_CONFIG_PATH))
12
+ return null;
13
+ let signature;
14
+ try {
15
+ const stat = statSync(WEB_SEARCH_CONFIG_PATH);
16
+ signature = `${stat.mtimeMs}:${stat.size}`;
17
+ } catch {
18
+ return null;
19
+ }
20
+ if (cachedConfigRoot?.signature === signature)
21
+ return cachedConfigRoot.value;
22
+ let raw;
23
+ try {
24
+ raw = readFileSync(WEB_SEARCH_CONFIG_PATH, "utf-8");
25
+ } catch {
26
+ return null;
27
+ }
28
+ let parsed;
29
+ try {
30
+ parsed = JSON.parse(raw);
31
+ } catch (err) {
32
+ const message = err instanceof Error ? err.message : String(err);
33
+ throw new Error(`Failed to parse ${WEB_SEARCH_CONFIG_PATH}: ${message}`);
34
+ }
35
+ const value = parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : null;
36
+ cachedConfigRoot = { signature, value };
37
+ return value;
38
+ }
39
+ const DEFAULT_DOMAIN_POLICY = { allow: [], deny: [] };
40
+ export function loadFetchContentDomainPolicy() {
41
+ const parsed = loadConfigRoot();
42
+ if (!parsed)
43
+ return { ...DEFAULT_DOMAIN_POLICY };
44
+ const fetchContent = parsed.fetchContent;
45
+ if (fetchContent === undefined || fetchContent === null)
46
+ return { ...DEFAULT_DOMAIN_POLICY };
47
+ if (typeof fetchContent !== "object" || Array.isArray(fetchContent)) {
48
+ throw new Error(`fetchContent in ${WEB_SEARCH_CONFIG_PATH} must be an object`);
49
+ }
50
+ const policy = fetchContent.domainPolicy;
51
+ if (policy === undefined || policy === null)
52
+ return { ...DEFAULT_DOMAIN_POLICY };
53
+ if (typeof policy !== "object" || Array.isArray(policy)) {
54
+ throw new Error(`fetchContent.domainPolicy in ${WEB_SEARCH_CONFIG_PATH} must be an object`);
55
+ }
56
+ const config = policy;
57
+ return {
58
+ allow: parseDomainEntries(config.allow, "allow"),
59
+ deny: parseDomainEntries(config.deny, "deny")
60
+ };
61
+ }
62
+ function parseDomainEntries(value, field) {
63
+ if (value === undefined || value === null)
64
+ return [];
65
+ if (!Array.isArray(value)) {
66
+ throw new Error(`fetchContent.domainPolicy.${field} in ${WEB_SEARCH_CONFIG_PATH} must be an array of hostnames`);
67
+ }
68
+ return value.map((entry, index) => {
69
+ if (typeof entry !== "string") {
70
+ throw new Error(`fetchContent.domainPolicy.${field} in ${WEB_SEARCH_CONFIG_PATH} must contain only hostnames; entry ${index + 1} is ${typeof entry}`);
71
+ }
72
+ const hostname = normalizeDomainEntry(entry);
73
+ if (!hostname) {
74
+ throw new Error(`fetchContent.domainPolicy.${field} in ${WEB_SEARCH_CONFIG_PATH} contains an invalid hostname: ${JSON.stringify(entry)}`);
75
+ }
76
+ return hostname;
77
+ });
78
+ }
79
+ function normalizeDomainEntry(entry) {
80
+ const hostname = normalizeHostname(entry.trim());
81
+ if (!hostname || /\s|[\\/?:#@]/.test(hostname))
82
+ return null;
83
+ if (net.isIP(hostname))
84
+ return hostname;
85
+ if (hostname.length > 253 || !/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i.test(hostname))
86
+ return null;
87
+ return hostname;
88
+ }
89
+ export function loadSsrfConfig() {
90
+ const parsed = loadConfigRoot();
91
+ if (!parsed)
92
+ return { allowRanges: [], trustEnvProxy: false };
93
+ const ssrf = parsed.ssrf;
94
+ if (ssrf === undefined || ssrf === null)
95
+ return { allowRanges: [], trustEnvProxy: false };
96
+ if (typeof ssrf !== "object" || Array.isArray(ssrf)) {
97
+ throw new Error(`ssrf in ${WEB_SEARCH_CONFIG_PATH} must be an object`);
98
+ }
99
+ const config = ssrf;
100
+ if (config.allowRanges !== undefined && config.allowRanges !== null && !Array.isArray(config.allowRanges)) {
101
+ throw new Error(`ssrf.allowRanges in ${WEB_SEARCH_CONFIG_PATH} must be an array of CIDR strings`);
102
+ }
103
+ if (config.trustEnvProxy !== undefined && typeof config.trustEnvProxy !== "boolean") {
104
+ throw new Error(`ssrf.trustEnvProxy in ${WEB_SEARCH_CONFIG_PATH} must be a boolean`);
105
+ }
106
+ const allowRangesValue = Array.isArray(config.allowRanges) ? config.allowRanges : [];
107
+ const allowRanges = allowRangesValue.map((entry, index) => {
108
+ if (typeof entry !== "string") {
109
+ throw new Error(`ssrf.allowRanges in ${WEB_SEARCH_CONFIG_PATH} must contain only CIDR strings; entry ${index + 1} is ${typeof entry}`);
110
+ }
111
+ return entry.trim();
112
+ }).filter(Boolean);
113
+ parseAllowRanges(allowRanges);
114
+ return { allowRanges, trustEnvProxy: config.trustEnvProxy === true };
115
+ }
116
+ async function defaultLookup(hostname) {
117
+ return dnsLookup(hostname, { all: true, verbatim: true });
118
+ }
119
+ export async function validateRemoteUrl(rawUrl, options = {}) {
120
+ const url = rawUrl instanceof URL ? rawUrl : new URL(rawUrl);
121
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
122
+ throw new Error("Only HTTP and HTTPS URLs can be fetched remotely");
123
+ }
124
+ const hostname = normalizeHostname(url.hostname);
125
+ if (!hostname)
126
+ throw new Error("URL must include a hostname");
127
+ if (hostname === "localhost") {
128
+ if (options.allowLoopback === true)
129
+ return url;
130
+ throw new Error(`Blocked internal hostname: ${hostname}`);
131
+ }
132
+ if (hostname.endsWith(".localhost")) {
133
+ throw new Error(`Blocked internal hostname: ${hostname}`);
134
+ }
135
+ const allowRanges = parseAllowRanges(options.allowRanges);
136
+ assertDomainPolicy(hostname, options.domainPolicy);
137
+ if (net.isIP(hostname)) {
138
+ const addressAllowRanges = options.allowLoopback === true ? [...allowRanges, ...parseAllowRanges(LOOPBACK_ALLOW_RANGES)] : allowRanges;
139
+ assertPublicAddress(hostname, hostname, addressAllowRanges);
140
+ return url;
141
+ }
142
+ if (shouldTrustEnvProxy(url, options.trustEnvProxy === true))
143
+ return url;
144
+ let addresses;
145
+ try {
146
+ addresses = await (options.lookup ?? defaultLookup)(hostname);
147
+ } catch (err) {
148
+ const message = err instanceof Error ? err.message : String(err);
149
+ throw new Error(`Failed to resolve ${hostname}: ${message}`);
150
+ }
151
+ if (addresses.length === 0)
152
+ throw new Error(`Failed to resolve ${hostname}: no addresses returned`);
153
+ for (const { address } of addresses) {
154
+ assertPublicAddress(address, hostname, allowRanges);
155
+ }
156
+ return url;
157
+ }
158
+ export async function fetchRemoteUrl(url, init = {}, options = {}) {
159
+ const fetchImpl = options.fetch ?? fetch;
160
+ const maxRedirects = options.maxRedirects ?? DEFAULT_MAX_REDIRECTS;
161
+ let current = await validateRemoteUrl(url, options);
162
+ let requestInit = init;
163
+ for (let redirects = 0;redirects <= maxRedirects; redirects++) {
164
+ const response = await fetchImpl(current, { ...requestInit, redirect: "manual" });
165
+ if (!REDIRECT_STATUSES.has(response.status))
166
+ return response;
167
+ const location = response.headers.get("location");
168
+ if (!location)
169
+ return response;
170
+ if (redirects === maxRedirects)
171
+ throw new Error(`Too many redirects fetching ${current.toString()}`);
172
+ const from = current;
173
+ current = await validateRemoteUrl(new URL(location, current), options);
174
+ if (response.status === 303 || (response.status === 301 || response.status === 302) && requestInit.method?.toUpperCase() === "POST") {
175
+ const { body: _body, ...nextInit } = requestInit;
176
+ requestInit = { ...nextInit, method: "GET" };
177
+ }
178
+ if (options.onRedirect)
179
+ requestInit = options.onRedirect({ from, to: current, init: requestInit, response });
180
+ }
181
+ throw new Error(`Too many redirects fetching ${current.toString()}`);
182
+ }
183
+ function normalizeHostname(hostname) {
184
+ return hostname.toLowerCase().replace(/^\[|\]$/g, "").replace(/\.$/, "");
185
+ }
186
+ function assertDomainPolicy(hostname, policy) {
187
+ if (!policy)
188
+ return;
189
+ if (policy.deny.some((entry) => domainMatches(hostname, entry))) {
190
+ throw new Error(`Blocked hostname by fetch_content domain policy: ${hostname}`);
191
+ }
192
+ if (policy.allow.length > 0 && !policy.allow.some((entry) => domainMatches(hostname, entry))) {
193
+ throw new Error(`Hostname not allowed by fetch_content domain policy: ${hostname}`);
194
+ }
195
+ }
196
+ function domainMatches(hostname, entry) {
197
+ return hostname === entry || hostname.endsWith(`.${entry}`);
198
+ }
199
+ function getProxyForProtocol(protocol) {
200
+ const candidates = protocol === "http:" ? [process.env.HTTP_PROXY, process.env.http_proxy, process.env.ALL_PROXY, process.env.all_proxy] : protocol === "https:" ? [process.env.HTTPS_PROXY, process.env.https_proxy, process.env.HTTP_PROXY, process.env.http_proxy, process.env.ALL_PROXY, process.env.all_proxy] : [];
201
+ for (const candidate of candidates) {
202
+ const value = candidate?.trim();
203
+ if (!value)
204
+ continue;
205
+ try {
206
+ const proxyUrl = new URL(value);
207
+ if ((proxyUrl.protocol === "http:" || proxyUrl.protocol === "https:") && proxyUrl.hostname)
208
+ return value;
209
+ } catch {}
210
+ }
211
+ return "";
212
+ }
213
+ function hostnameMatchesNoProxy(hostname, port, entry) {
214
+ const trimmed = entry.trim();
215
+ if (!trimmed)
216
+ return false;
217
+ if (trimmed === "*")
218
+ return true;
219
+ let hostEntry = trimmed;
220
+ let entryPort;
221
+ if (hostEntry.startsWith("[")) {
222
+ const closingBracket = hostEntry.indexOf("]");
223
+ if (closingBracket >= 0) {
224
+ const suffix = hostEntry.slice(closingBracket + 1);
225
+ if (/^:\\d+$/.test(suffix))
226
+ entryPort = suffix.slice(1);
227
+ hostEntry = hostEntry.slice(0, closingBracket + 1);
228
+ }
229
+ } else {
230
+ const colon = hostEntry.lastIndexOf(":");
231
+ if (colon > -1 && /^\d+$/.test(hostEntry.slice(colon + 1))) {
232
+ entryPort = hostEntry.slice(colon + 1);
233
+ hostEntry = hostEntry.slice(0, colon);
234
+ }
235
+ }
236
+ if (entryPort !== undefined && entryPort !== port)
237
+ return false;
238
+ const normalizedEntry = normalizeHostname(hostEntry);
239
+ if (!normalizedEntry)
240
+ return false;
241
+ if (normalizedEntry === hostname)
242
+ return true;
243
+ const suffix = normalizedEntry.startsWith("*.") ? normalizedEntry.slice(1) : normalizedEntry.startsWith(".") ? normalizedEntry : `.${normalizedEntry}`;
244
+ return hostname.endsWith(suffix);
245
+ }
246
+ function shouldTrustEnvProxy(url, enabled) {
247
+ if (!enabled || !getProxyForProtocol(url.protocol))
248
+ return false;
249
+ if (hasScopedProxyDecision())
250
+ return false;
251
+ const activeProxy = getActiveProxy();
252
+ if (activeProxy && !isProxyBypassedUrl(url))
253
+ return false;
254
+ const hostname = normalizeHostname(url.hostname);
255
+ const port = url.port || (url.protocol === "https:" ? "443" : "80");
256
+ const noProxy = process.env.NO_PROXY || process.env.no_proxy || "";
257
+ return !noProxy.split(",").some((entry) => hostnameMatchesNoProxy(hostname, port, entry));
258
+ }
259
+ function assertPublicAddress(address, hostname, allowRanges = []) {
260
+ const normalized = normalizeHostname(address);
261
+ const ipVersion = net.isIP(normalized);
262
+ if (ipVersion === 0)
263
+ throw new Error(`Resolved non-IP address for ${hostname}: ${address}`);
264
+ if (isInAllowedRange(normalized, ipVersion, allowRanges))
265
+ return;
266
+ if (ipVersion === 4 && isBlockedIPv4(normalized)) {
267
+ const hint = isFakeIpProxyAddress(normalized) ? '. This address is in 198.18.0.0/15, commonly used by TUN/fake-IP proxies. If that matches your setup, configure ssrf.allowRanges with ["198.18.0.0/15"] in web-search.json.' : "";
268
+ throw new Error(`Blocked internal address for ${hostname}: ${normalized}${hint}`);
269
+ }
270
+ if (ipVersion === 6 && isBlockedIPv6(normalized)) {
271
+ throw new Error(`Blocked internal address for ${hostname}: ${normalized}`);
272
+ }
273
+ }
274
+ function isFakeIpProxyAddress(address) {
275
+ const [a, b] = address.split(".").map((part) => Number(part));
276
+ return a === 198 && (b === 18 || b === 19);
277
+ }
278
+ function isBlockedIPv4(address) {
279
+ const parts = address.split(".").map((part) => Number(part));
280
+ if (parts.length !== 4 || parts.some((part) => !Number.isInteger(part) || part < 0 || part > 255))
281
+ return true;
282
+ const [a, b] = parts;
283
+ return a === 0 || a === 10 || a === 127 || a === 100 && b >= 64 && b <= 127 || a === 169 && b === 254 || a === 172 && b >= 16 && b <= 31 || a === 192 && b === 168 || isFakeIpProxyAddress(address) || a >= 224;
284
+ }
285
+ function isBlockedIPv6(address) {
286
+ const groups = parseIPv6(address);
287
+ if (!groups)
288
+ return true;
289
+ const first = groups[0];
290
+ if (groups.every((group) => group === 0))
291
+ return true;
292
+ if (groups.slice(0, 7).every((group) => group === 0) && groups[7] === 1)
293
+ return true;
294
+ if ((first & 65024) === 64512)
295
+ return true;
296
+ if ((first & 65472) === 65152)
297
+ return true;
298
+ const isMappedIPv4 = groups.slice(0, 5).every((group) => group === 0) && groups[5] === 65535;
299
+ if (isMappedIPv4) {
300
+ const ipv4 = [groups[6] >> 8, groups[6] & 255, groups[7] >> 8, groups[7] & 255].join(".");
301
+ return isBlockedIPv4(ipv4);
302
+ }
303
+ return false;
304
+ }
305
+ function parseIPv6(address) {
306
+ if (address.includes(".")) {
307
+ const lastColon = address.lastIndexOf(":");
308
+ const ipv4 = address.slice(lastColon + 1);
309
+ if (net.isIP(ipv4) !== 4)
310
+ return null;
311
+ const octets = ipv4.split(".").map((part) => Number(part));
312
+ address = `${address.slice(0, lastColon)}:${(octets[0] << 8 | octets[1]).toString(16)}:${(octets[2] << 8 | octets[3]).toString(16)}`;
313
+ }
314
+ const pieces = address.split("::");
315
+ if (pieces.length > 2)
316
+ return null;
317
+ const left = pieces[0] ? pieces[0].split(":") : [];
318
+ const right = pieces.length === 2 && pieces[1] ? pieces[1].split(":") : [];
319
+ const missing = 8 - left.length - right.length;
320
+ if (pieces.length === 1 && missing !== 0)
321
+ return null;
322
+ if (pieces.length === 2 && missing < 0)
323
+ return null;
324
+ const groups = [...left, ...Array(missing).fill("0"), ...right].map((part) => {
325
+ if (!/^[0-9a-f]{1,4}$/i.test(part))
326
+ return -1;
327
+ return parseInt(part, 16);
328
+ });
329
+ return groups.length === 8 && groups.every((group) => group >= 0 && group <= 65535) ? groups : null;
330
+ }
331
+ function parseAllowRanges(input) {
332
+ if (input === undefined || input === null)
333
+ return [];
334
+ if (!Array.isArray(input)) {
335
+ throw new Error("ssrf.allowRanges must be an array of CIDR strings");
336
+ }
337
+ const rules = [];
338
+ for (const entry of input) {
339
+ if (typeof entry !== "string") {
340
+ throw new Error(`ssrf.allowRanges entries must be strings, got ${typeof entry}`);
341
+ }
342
+ const rule = parseCidr(entry.trim());
343
+ if (!rule) {
344
+ throw new Error(`Invalid CIDR notation in ssrf.allowRanges: "${entry}"`);
345
+ }
346
+ rules.push(rule);
347
+ }
348
+ return rules;
349
+ }
350
+ function parseCidr(raw) {
351
+ if (!raw)
352
+ return null;
353
+ const slash = raw.lastIndexOf("/");
354
+ const addrPart = slash >= 0 ? raw.slice(0, slash) : raw;
355
+ const prefixPart = slash >= 0 ? raw.slice(slash + 1) : null;
356
+ if (prefixPart !== null && !/^\d+$/.test(prefixPart))
357
+ return null;
358
+ const version = net.isIP(addrPart);
359
+ if (version === 4) {
360
+ const bytes = ipv4ToBytes(addrPart);
361
+ if (!bytes)
362
+ return null;
363
+ const prefix = prefixPart === null ? 32 : Number(prefixPart);
364
+ if (!Number.isInteger(prefix) || prefix < 1 || prefix > 32)
365
+ return null;
366
+ return { bytes, prefix };
367
+ }
368
+ if (version === 6) {
369
+ const groups = parseIPv6(addrPart);
370
+ if (!groups)
371
+ return null;
372
+ const prefix = prefixPart === null ? 128 : Number(prefixPart);
373
+ if (!Number.isInteger(prefix) || prefix < 1 || prefix > 128)
374
+ return null;
375
+ return { bytes: ipv6GroupsToBytes(groups), prefix };
376
+ }
377
+ return null;
378
+ }
379
+ function ipv4ToBytes(address) {
380
+ const parts = address.split(".");
381
+ if (parts.length !== 4)
382
+ return null;
383
+ const bytes = new Uint8Array(4);
384
+ for (let i = 0;i < 4; i++) {
385
+ const octet = Number(parts[i]);
386
+ if (!Number.isInteger(octet) || octet < 0 || octet > 255)
387
+ return null;
388
+ bytes[i] = octet;
389
+ }
390
+ return bytes;
391
+ }
392
+ function ipv6GroupsToBytes(groups) {
393
+ const bytes = new Uint8Array(16);
394
+ for (let i = 0;i < 8; i++) {
395
+ bytes[i * 2] = groups[i] >> 8;
396
+ bytes[i * 2 + 1] = groups[i] & 255;
397
+ }
398
+ return bytes;
399
+ }
400
+ function ipToBytes(address, version) {
401
+ if (version === 4)
402
+ return ipv4ToBytes(address);
403
+ if (version === 6) {
404
+ const groups = parseIPv6(address);
405
+ return groups ? ipv6GroupsToBytes(groups) : null;
406
+ }
407
+ return null;
408
+ }
409
+ function isInAllowedRange(address, ipVersion, allowRanges) {
410
+ if (allowRanges.length === 0)
411
+ return false;
412
+ const addrBytes = ipToBytes(address, ipVersion);
413
+ if (!addrBytes)
414
+ return false;
415
+ for (const rule of allowRanges) {
416
+ if (rule.bytes.length !== addrBytes.length)
417
+ continue;
418
+ if (bytesMatchPrefix(addrBytes, rule.bytes, rule.prefix))
419
+ return true;
420
+ }
421
+ return false;
422
+ }
423
+ function bytesMatchPrefix(addr, network, prefix) {
424
+ const fullBytes = prefix >> 3;
425
+ const remBits = prefix & 7;
426
+ for (let i = 0;i < fullBytes; i++) {
427
+ if (addr[i] !== network[i])
428
+ return false;
429
+ }
430
+ if (remBits > 0 && fullBytes < addr.length) {
431
+ const mask = 255 << 8 - remBits & 255;
432
+ if ((addr[fullBytes] & mask) !== (network[fullBytes] & mask))
433
+ return false;
434
+ }
435
+ return true;
436
+ }