@blocklet/crawler 2.1.233 → 2.1.235

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.
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.initSEOMiddleware = void 0;
7
7
  var _utils = require("./utils");
8
- var _stream = require("stream");
9
8
  const initSEOMiddleware = ({
10
9
  autoReturnHtml = true,
11
10
  allowCrawler = true
@@ -29,11 +28,7 @@ const initSEOMiddleware = ({
29
28
  res.setHeader("Last-Modified", req.cachedLastmod);
30
29
  }
31
30
  if (autoReturnHtml && req.cachedHtml) {
32
- const stream = new _stream.Readable();
33
- stream.push(req.cachedHtml);
34
- stream.push(null);
35
- res.set("Content-Type", "text/html, charset=utf-8");
36
- stream.pipe(res);
31
+ res.send(req.cachedHtml);
37
32
  return;
38
33
  }
39
34
  next();
@@ -63,6 +63,7 @@ Object.keys(_config).forEach(function (key) {
63
63
  });
64
64
  var _util = require("util");
65
65
  var _child_process = require("child_process");
66
+ var _component = require("@blocklet/sdk/lib/component");
66
67
  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); }
67
68
  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; }
68
69
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -349,6 +350,19 @@ async function detectBestRedisUrl() {
349
350
  const possibleUrls = [
350
351
  // environment variable priority
351
352
  process.env.REDIS_URL,
353
+ // by web component endpoint
354
+ (() => {
355
+ try {
356
+ const endpoint = (0, _component.getComponentWebEndpoint)(_config.default.env.componentDid);
357
+ if (endpoint) {
358
+ const url = new URL(endpoint);
359
+ return `redis://${url.hostname}:6379`;
360
+ }
361
+ } catch (err) {
362
+ logger.warn(`Failed to get component endpoint: ${err.message}`);
363
+ }
364
+ return null;
365
+ })(),
352
366
  // default gateway
353
367
  defaultGateway ? `redis://${defaultGateway}:6379` : null,
354
368
  // common Docker gateway
@@ -365,7 +379,7 @@ async function detectBestRedisUrl() {
365
379
  });
366
380
  testClient.on("error", () => {});
367
381
  await Promise.race([testClient.connect(), new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), 2e3))]);
368
- const pingResult = await testClient.ping();
382
+ await testClient.ping();
369
383
  await testClient.disconnect();
370
384
  logger.info(`\u2705 Found available Redis connection: ${url}`);
371
385
  cachedRedisUrl = url;
@@ -1,5 +1,4 @@
1
1
  import { isBotUserAgent, getFullUrl, useCache, isAcceptCrawler, isSelfCrawler, getRelativePath } from "./utils.js";
2
- import { Readable } from "stream";
3
2
  export const initSEOMiddleware = ({ autoReturnHtml = true, allowCrawler = true } = {}) => {
4
3
  return async (req, res, next) => {
5
4
  const isBot = isBotUserAgent(req);
@@ -20,11 +19,7 @@ export const initSEOMiddleware = ({ autoReturnHtml = true, allowCrawler = true }
20
19
  res.setHeader("Last-Modified", req.cachedLastmod);
21
20
  }
22
21
  if (autoReturnHtml && req.cachedHtml) {
23
- const stream = new Readable();
24
- stream.push(req.cachedHtml);
25
- stream.push(null);
26
- res.set("Content-Type", "text/html, charset=utf-8");
27
- stream.pipe(res);
22
+ res.send(req.cachedHtml);
28
23
  return;
29
24
  }
30
25
  next();
@@ -13,6 +13,7 @@ import uniq from "lodash/uniq";
13
13
  import config from "@blocklet/sdk/lib/config";
14
14
  import { promisify } from "util";
15
15
  import { exec } from "child_process";
16
+ import { getComponentWebEndpoint } from "@blocklet/sdk/lib/component";
16
17
  export * from "@blocklet/sdk/lib/config";
17
18
  const { logger } = config;
18
19
  const execAsync = promisify(exec);
@@ -374,6 +375,19 @@ async function detectBestRedisUrl() {
374
375
  const possibleUrls = [
375
376
  // environment variable priority
376
377
  process.env.REDIS_URL,
378
+ // by web component endpoint
379
+ (() => {
380
+ try {
381
+ const endpoint = getComponentWebEndpoint(config.env.componentDid);
382
+ if (endpoint) {
383
+ const url = new URL(endpoint);
384
+ return `redis://${url.hostname}:6379`;
385
+ }
386
+ } catch (err) {
387
+ logger.warn(`Failed to get component endpoint: ${err.message}`);
388
+ }
389
+ return null;
390
+ })(),
377
391
  // default gateway
378
392
  defaultGateway ? `redis://${defaultGateway}:6379` : null,
379
393
  // common Docker gateway
@@ -393,7 +407,7 @@ async function detectBestRedisUrl() {
393
407
  testClient.connect(),
394
408
  new Promise((_, reject) => setTimeout(() => reject(new Error("Connection timeout")), 2e3))
395
409
  ]);
396
- const pingResult = await testClient.ping();
410
+ await testClient.ping();
397
411
  await testClient.disconnect();
398
412
  logger.info(`\u2705 Found available Redis connection: ${url}`);
399
413
  cachedRedisUrl = url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/crawler",
3
- "version": "2.1.233",
3
+ "version": "2.1.235",
4
4
  "description": "blocklet crawler lib",
5
5
  "publishConfig": {
6
6
  "access": "public"