From 533003cdf8324499b11bbb9c5ad97d6d8474c058 Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Sun, 10 Nov 2024 17:39:44 +0900 Subject: [PATCH] =?UTF-8?q?trace-redirect=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: あわわわとーにゅ <17376330+u1-liquid@users.noreply.github.com> --- package.json | 3 +-- pnpm-lock.yaml | 8 -------- src/index.ts | 36 +++++++++++++++++++++++++++--------- src/utils/got.ts | 8 ++++---- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 2f5a8dc6..0294d377 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,6 @@ "html-entities": "2.5.2", "iconv-lite": "0.6.3", "jschardet": "3.1.4", - "private-ip": "3.0.2", - "trace-redirect": "1.0.6" + "private-ip": "3.0.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bd45f06c..7557aae5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,9 +29,6 @@ importers: private-ip: specifier: 3.0.2 version: 3.0.2 - trace-redirect: - specifier: 1.0.6 - version: 1.0.6 devDependencies: '@jest/globals': specifier: ^29.7.0 @@ -2343,9 +2340,6 @@ packages: resolution: {integrity: sha512-/m8M+2BJUpoJdgAHoG+baCwBT+tf2VraSfkBgl0Y00qIWt41DJ8R5B8nsEw0I58YwF5IZH6z24/2TobDKnqSWw==} engines: {node: '>=12'} - trace-redirect@1.0.6: - resolution: {integrity: sha512-UUfa1DjjU5flcjMdaFIiIEGDTyu2y/IiMjOX4uGXa7meKBS4vD4f2Uy/tken9Qkd4Jsm4sRsfZcIIPqrRVF3Mg==} - ts-api-utils@1.4.0: resolution: {integrity: sha512-032cPxaEKwM+GT3vA5JXNzIaizx388rhsSW79vGRNGXfRRAdEAn2mvk36PvK5HnOchyWZ7afLEXqYCvPCrzuzQ==} engines: {node: '>=16'} @@ -5186,8 +5180,6 @@ snapshots: toad-cache@3.7.0: {} - trace-redirect@1.0.6: {} - ts-api-utils@1.4.0(typescript@5.6.3): dependencies: typescript: 5.6.3 diff --git a/src/index.ts b/src/index.ts index a7f8e078..84b96f76 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,14 +4,13 @@ */ import { URL } from 'node:url'; -import tracer from 'trace-redirect'; -import * as Got from 'got'; +import { got, type Agents as GotAgents } from 'got'; import type { FastifyInstance } from 'fastify'; import { SummalyResult } from '@/summary.js'; import { SummalyPlugin } from '@/iplugin.js'; export * from '@/iplugin.js'; import general, { GeneralScrapingOptions } from '@/general.js'; -import { setAgent } from '@/utils/got.js'; +import { DEFAULT_OPERATION_TIMEOUT, DEFAULT_RESPONSE_TIMEOUT, agent, setAgent } from '@/utils/got.js'; import { plugins as builtinPlugins } from '@/plugins/index.js'; export type SummalyOptions = { @@ -33,7 +32,7 @@ export type SummalyOptions = { /** * Custom HTTP agent */ - agent?: Got.Agents; + agent?: GotAgents; /** * User-Agent for the request @@ -85,7 +84,26 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise url)にすればいいけど、jestにtrace-redirectを食わせるのが面倒なのでtry-catch try { - actualUrl = await tracer(url); + const timeout = opts.responseTimeout ?? DEFAULT_RESPONSE_TIMEOUT; + const operationTimeout = opts.operationTimeout ?? DEFAULT_OPERATION_TIMEOUT; + actualUrl = await got + .head(url, { + timeout: { + lookup: timeout, + connect: timeout, + secureConnect: timeout, + socket: timeout, // read timeout + response: timeout, + send: timeout, + request: operationTimeout, // whole operation timeout + }, + agent, + http2: false, + retry: { + limit: 0, + }, + }) + .then(res => res.url); } catch (e) { actualUrl = url; } @@ -121,10 +139,10 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise void) { fastify.get<{ - Querystring: { - url?: string; - lang?: string; - }; + Querystring: { + url?: string; + lang?: string; + }; }>('/', async (req, reply) => { const url = req.query.url as string; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition diff --git a/src/utils/got.ts b/src/utils/got.ts index f54ad413..6a2b6d9d 100644 --- a/src/utils/got.ts +++ b/src/utils/got.ts @@ -31,10 +31,10 @@ export type GotOptions = { const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8')); -const DEFAULT_RESPONSE_TIMEOUT = 20 * 1000; -const DEFAULT_OPERATION_TIMEOUT = 60 * 1000; -const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024; -const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`; +export const DEFAULT_RESPONSE_TIMEOUT = 20 * 1000; +export const DEFAULT_OPERATION_TIMEOUT = 60 * 1000; +export const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024; +export const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`; export async function scpaping( url: string,