@blocklet/crawler 2.1.229 → 2.1.231

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.
@@ -61,15 +61,19 @@ Object.keys(_config).forEach(function (key) {
61
61
  }
62
62
  });
63
63
  });
64
+ var _util = require("util");
65
+ var _child_process = require("child_process");
64
66
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
65
67
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
66
68
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
67
69
  const {
68
70
  logger
69
71
  } = _config.default;
72
+ const execAsync = (0, _util.promisify)(_child_process.exec);
70
73
  let browser;
71
74
  let checkBrowserTimer;
72
75
  const BROWSER_WS_ENDPOINT_KEY = `browserWSEndpoint-${process.env.BLOCKLET_DID || "unknown"}`;
76
+ let cachedRedisUrl = null;
73
77
  const api = exports.api = _axios.default.create({
74
78
  timeout: 1e3 * 10,
75
79
  // 10s
@@ -310,11 +314,65 @@ const getRelativePath = url => {
310
314
  exports.getRelativePath = getRelativePath;
311
315
  let ttl = 1e3 * 60 * 60 * 24 * 7;
312
316
  const MAX_REDIS_RETRY = 3;
317
+ async function getDefaultGateway() {
318
+ try {
319
+ const {
320
+ stdout
321
+ } = await execAsync("cat /proc/net/route | grep '^eth0' | head -1 | awk '{print $3}'");
322
+ if (stdout.trim()) {
323
+ const hex = stdout.trim();
324
+ const ip = [parseInt(hex.slice(6, 8), 16), parseInt(hex.slice(4, 6), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(0, 2), 16)].join(".");
325
+ logger.info(`Detected default gateway address: ${ip}`);
326
+ return ip;
327
+ }
328
+ } catch (err) {}
329
+ return null;
330
+ }
331
+ async function detectBestRedisUrl() {
332
+ if (cachedRedisUrl) {
333
+ logger.info(`Using cached Redis connection: ${cachedRedisUrl}`);
334
+ return cachedRedisUrl;
335
+ }
336
+ logger.info("Starting to detect best Redis connection...");
337
+ const defaultGateway = await getDefaultGateway();
338
+ const possibleUrls = [
339
+ // environment variable priority
340
+ process.env.REDIS_URL,
341
+ // default gateway
342
+ defaultGateway ? `redis://${defaultGateway}:6379` : null,
343
+ // common Docker gateway
344
+ "redis://192.168.0.1:6379",
345
+ // Docker special DNS
346
+ "redis://host.docker.internal:6379",
347
+ // local Redis (last try)
348
+ "redis://localhost:6379"].filter(Boolean);
349
+ for (const url of possibleUrls) {
350
+ try {
351
+ logger.debug(`Testing Redis connection: ${url}`);
352
+ const testClient = (0, _redis.createClient)({
353
+ url
354
+ });
355
+ testClient.on("error", () => {});
356
+ await Promise.race([testClient.connect(), new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), 2e3))]);
357
+ const pingResult = await testClient.ping();
358
+ await testClient.disconnect();
359
+ logger.info(`\u2705 Found available Redis connection: ${url}`);
360
+ cachedRedisUrl = url;
361
+ return url;
362
+ } catch (err) {
363
+ logger.debug(`Redis connection failed ${url}: ${err.message}`);
364
+ }
365
+ }
366
+ logger.warn("\u26A0\uFE0F No available Redis connection found, using default value");
367
+ return "redis://localhost:6379";
368
+ }
313
369
  const cachePool = exports.cachePool = (0, _genericPool.createPool)({
314
370
  create: async () => {
315
371
  let cache = null;
316
372
  try {
373
+ const redisUrl = await detectBestRedisUrl();
317
374
  const redisClient = (0, _redis.createClient)({
375
+ url: redisUrl,
318
376
  socket: {
319
377
  // @ts-ignore
320
378
  reconnectStrategy: retries => {
@@ -325,9 +383,13 @@ const cachePool = exports.cachePool = (0, _genericPool.createPool)({
325
383
  }
326
384
  }
327
385
  });
386
+ redisClient.on("error", err => logger.warn("Redis Client Error:", err));
328
387
  await redisClient.connect();
388
+ logger.info(`Successfully connected to Redis: ${redisUrl}`);
329
389
  cache = redisClient;
330
- } catch (error) {}
390
+ } catch (error) {
391
+ logger.warn("Redis connection failed", error);
392
+ }
331
393
  return cache;
332
394
  },
333
395
  destroy: async client => {
@@ -11,11 +11,15 @@ import { parseSitemap } from "sitemap";
11
11
  import flattenDeep from "lodash/flattenDeep";
12
12
  import uniq from "lodash/uniq";
13
13
  import config from "@blocklet/sdk/lib/config";
14
+ import { promisify } from "util";
15
+ import { exec } from "child_process";
14
16
  export * from "@blocklet/sdk/lib/config";
15
17
  const { logger } = config;
18
+ const execAsync = promisify(exec);
16
19
  let browser;
17
20
  let checkBrowserTimer;
18
21
  const BROWSER_WS_ENDPOINT_KEY = `browserWSEndpoint-${process.env.BLOCKLET_DID || "unknown"}`;
22
+ let cachedRedisUrl = null;
19
23
  export const api = axios.create({
20
24
  timeout: 1e3 * 10,
21
25
  // 10s
@@ -326,12 +330,73 @@ export const getRelativePath = (url) => {
326
330
  };
327
331
  let ttl = 1e3 * 60 * 60 * 24 * 7;
328
332
  const MAX_REDIS_RETRY = 3;
333
+ async function getDefaultGateway() {
334
+ try {
335
+ const { stdout } = await execAsync("cat /proc/net/route | grep '^eth0' | head -1 | awk '{print $3}'");
336
+ if (stdout.trim()) {
337
+ const hex = stdout.trim();
338
+ const ip = [
339
+ parseInt(hex.slice(6, 8), 16),
340
+ parseInt(hex.slice(4, 6), 16),
341
+ parseInt(hex.slice(2, 4), 16),
342
+ parseInt(hex.slice(0, 2), 16)
343
+ ].join(".");
344
+ logger.info(`Detected default gateway address: ${ip}`);
345
+ return ip;
346
+ }
347
+ } catch (err) {
348
+ }
349
+ return null;
350
+ }
351
+ async function detectBestRedisUrl() {
352
+ if (cachedRedisUrl) {
353
+ logger.info(`Using cached Redis connection: ${cachedRedisUrl}`);
354
+ return cachedRedisUrl;
355
+ }
356
+ logger.info("Starting to detect best Redis connection...");
357
+ const defaultGateway = await getDefaultGateway();
358
+ const possibleUrls = [
359
+ // environment variable priority
360
+ process.env.REDIS_URL,
361
+ // default gateway
362
+ defaultGateway ? `redis://${defaultGateway}:6379` : null,
363
+ // common Docker gateway
364
+ "redis://192.168.0.1:6379",
365
+ // Docker special DNS
366
+ "redis://host.docker.internal:6379",
367
+ // local Redis (last try)
368
+ "redis://localhost:6379"
369
+ ].filter(Boolean);
370
+ for (const url of possibleUrls) {
371
+ try {
372
+ logger.debug(`Testing Redis connection: ${url}`);
373
+ const testClient = createClient({ url });
374
+ testClient.on("error", () => {
375
+ });
376
+ await Promise.race([
377
+ testClient.connect(),
378
+ new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), 2e3))
379
+ ]);
380
+ const pingResult = await testClient.ping();
381
+ await testClient.disconnect();
382
+ logger.info(`\u2705 Found available Redis connection: ${url}`);
383
+ cachedRedisUrl = url;
384
+ return url;
385
+ } catch (err) {
386
+ logger.debug(`Redis connection failed ${url}: ${err.message}`);
387
+ }
388
+ }
389
+ logger.warn("\u26A0\uFE0F No available Redis connection found, using default value");
390
+ return "redis://localhost:6379";
391
+ }
329
392
  export const cachePool = createPool(
330
393
  {
331
394
  create: async () => {
332
395
  let cache = null;
333
396
  try {
397
+ const redisUrl = await detectBestRedisUrl();
334
398
  const redisClient = createClient({
399
+ url: redisUrl,
335
400
  socket: {
336
401
  // @ts-ignore
337
402
  reconnectStrategy: (retries) => {
@@ -342,9 +407,12 @@ export const cachePool = createPool(
342
407
  }
343
408
  }
344
409
  });
410
+ redisClient.on("error", (err) => logger.warn("Redis Client Error:", err));
345
411
  await redisClient.connect();
412
+ logger.info(`Successfully connected to Redis: ${redisUrl}`);
346
413
  cache = redisClient;
347
414
  } catch (error) {
415
+ logger.warn("Redis connection failed", error);
348
416
  }
349
417
  return cache;
350
418
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/crawler",
3
- "version": "2.1.229",
3
+ "version": "2.1.231",
4
4
  "description": "blocklet crawler lib",
5
5
  "publishConfig": {
6
6
  "access": "public"