update deps (#33)

* update deps

* lint fixes

* fix

* trace-redirectを削除

Co-Authored-By: あわわわとーにゅ <17376330+u1-liquid@users.noreply.github.com>

* attempt to fix test

* refactor

* fix test

---------

Co-authored-by: あわわわとーにゅ <17376330+u1-liquid@users.noreply.github.com>
This commit is contained in:
かっこかり 2024-11-11 04:14:01 +09:00 committed by GitHub
parent 509a35abe2
commit 3e09d27613
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
21 changed files with 3932 additions and 3247 deletions

View File

@ -1,16 +0,0 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json', './test/tsconfig.json'],
},
ignorePatterns: ['**/.eslintrc.cjs'],
extends: [
'plugin:@misskey-dev/recommended',
],
rules: {
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'import/no-default-export': 'off',
},
};

View File

@ -1,4 +1,10 @@
(unreleased)
------------------
* 依存関係の更新
* eslintの設定を更新
5.1.0 / 2024-03-18 5.1.0 / 2024-03-18
------------------
* GETリクエストよりも前にHEADリクエストを送信し、その結果を使用して検証するように (#22) * GETリクエストよりも前にHEADリクエストを送信し、その結果を使用して検証するように (#22)
* 下記のパラメータを`summaly`メソッドのオプションに追加 * 下記のパラメータを`summaly`メソッドのオプションに追加
- userAgent - userAgent

View File

@ -134,7 +134,7 @@ will be ... ↓
```json ```json
{ {
"title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)", "title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)",
"icon": "https://www.youtube.com/s/desktop/28b0985e/img/favicon.ico", "icon": "https://www.youtube.com/s/desktop/711fd789/img/logos/favicon.ico",
"description": "Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...", "description": "Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...",
"thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg", "thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg",
"player": { "player": {

38
eslint.config.js Normal file
View File

@ -0,0 +1,38 @@
import pluginMisskey from '@misskey-dev/eslint-plugin';
import tsParser from '@typescript-eslint/parser';
//@ts-check
/** @type {import('eslint').Linter.Config[]} */
export default [ // eslint-disable-line import/no-default-export
...pluginMisskey.configs['recommended'],
{
ignores: [
'**/node_modules',
'src/@types/package.json.d.ts',
'built',
'jest.config.js',
'test',
],
},
{
files: ['**/*.ts', '**/*.tsx'],
languageOptions: {
parserOptions: {
parser: tsParser,
project: ['./tsconfig.json', './test/tsconfig.json'],
sourceType: 'module',
tsConfigRootDir: import.meta.dirname,
},
},
rules: {
// 空文字でもフォールバックしたいので無効
'@typescript-eslint/prefer-nullish-coalescing': 'off',
},
},
{
files: ['**/*.js', '**/*.cjs'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
},
},
];

View File

@ -92,6 +92,9 @@ export default {
// TODO: Use `--allowImportingTsExtensions` on TypeScript 5.0 so that we can // TODO: Use `--allowImportingTsExtensions` on TypeScript 5.0 so that we can
// directly import `.ts` files without this hack. // directly import `.ts` files without this hack.
'^(\\.{1,2}/.*)\\.js$': '$1', '^(\\.{1,2}/.*)\\.js$': '$1',
// Resolve @/foo/bar to `../../src/foo/bar`
'^@/(.*)\\.js$': '<rootDir>/src/$1',
}, },
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader

View File

@ -9,44 +9,43 @@
"main": "./built/index.js", "main": "./built/index.js",
"type": "module", "type": "module",
"types": "./built/index.d.ts", "types": "./built/index.d.ts",
"packageManager": "pnpm@8.13.1", "packageManager": "pnpm@9.12.3",
"files": [ "files": [
"built", "built",
"LICENSE" "LICENSE"
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"eslint": "eslint src --ext .js,.jsx,.ts,.tsx", "eslint": "eslint",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --silent=false --verbose false", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --silent=false --verbose false",
"serve": "fastify start ./built/index.js" "serve": "fastify start ./built/index.js"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.7.0", "@jest/globals": "^29.7.0",
"@misskey-dev/eslint-plugin": "^1.0.0", "@misskey-dev/eslint-plugin": "^2.0.3",
"@swc/core": "^1.3.101", "@swc/core": "^1.9.1",
"@swc/jest": "^0.2.29", "@swc/jest": "^0.2.37",
"@types/cheerio": "0.22.18", "@types/cheerio": "0.22.35",
"@types/debug": "4.1.7", "@types/debug": "4.1.12",
"@types/escape-regexp": "^0.0.1", "@types/escape-regexp": "^0.0.3",
"@types/node": "20.10.6", "@types/node": "22.9.0",
"@typescript-eslint/eslint-plugin": "^6.16.0", "@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^6.16.0", "@typescript-eslint/parser": "^7.17.0",
"debug": "^4.3.4", "debug": "^4.3.7",
"eslint": "^8.56.0", "eslint": "^9.14.0",
"eslint-plugin-import": "^2.29.1", "eslint-plugin-import": "^2.31.0",
"fastify": "^4.25.2", "fastify": "^5.1.0",
"fastify-cli": "^5.9.0", "fastify-cli": "^7.0.1",
"jest": "^29.7.0", "jest": "^29.7.0",
"typescript": "5.3.3" "typescript": "5.6.3"
}, },
"dependencies": { "dependencies": {
"cheerio": "1.0.0-rc.12", "cheerio": "1.0.0",
"escape-regexp": "0.0.1", "escape-regexp": "0.0.1",
"got": "^12.6.1", "got": "^14.4.4",
"html-entities": "2.3.2", "html-entities": "2.5.2",
"iconv-lite": "0.6.3", "iconv-lite": "0.6.3",
"jschardet": "3.0.0", "jschardet": "3.1.4",
"private-ip": "2.3.3", "private-ip": "3.0.2"
"trace-redirect": "1.0.6"
} }
} }

6938
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,10 @@
import { URL } from 'node:url';
import { decode as decodeHtml } from 'html-entities'; import { decode as decodeHtml } from 'html-entities';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import clip from './utils/clip.js'; import type { default as Summary, Player } from '@/summary.js';
import cleanupTitle from './utils/cleanup-title.js'; import { clip } from '@/utils/clip.js';
import { cleanupTitle } from '@/utils/cleanup-title.js';
import { get, head, scpaping } from './utils/got.js'; import { get, head, scpaping } from '@/utils/got.js';
import type { default as Summary, Player } from './summary.js';
/** /**
* Contains only the html snippet for a sanitized iframe as the thumbnail is * Contains only the html snippet for a sanitized iframe as the thumbnail is
@ -139,9 +138,8 @@ export type GeneralScrapingOptions = {
contentLengthRequired?: boolean; contentLengthRequired?: boolean;
} }
export default async (_url: URL | string, opts?: GeneralScrapingOptions): Promise<Summary | null> => { export async function parseGeneral(_url: URL | string, opts?: GeneralScrapingOptions): Promise<Summary | null> {
let lang = opts?.lang; let lang = opts?.lang;
// eslint-disable-next-line no-param-reassign
if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null; if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null;
const url = typeof _url === 'string' ? new URL(_url) : _url; const url = typeof _url === 'string' ? new URL(_url) : _url;
@ -280,4 +278,4 @@ export default async (_url: URL | string, opts?: GeneralScrapingOptions): Promis
sensitive, sensitive,
activityPub, activityPub,
}; };
}; }

View File

@ -3,16 +3,15 @@
* https://github.com/misskey-dev/summaly * https://github.com/misskey-dev/summaly
*/ */
import { URL } from 'node:url'; import { got, type Agents as GotAgents } from 'got';
import tracer from 'trace-redirect';
import * as Got from 'got';
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 { plugins as builtinPlugins } from './plugins/index.js';
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import { SummalyResult } from '@/summary.js';
import { SummalyPlugin as _SummalyPlugin } from '@/iplugin.js';
import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';
import { DEFAULT_OPERATION_TIMEOUT, DEFAULT_RESPONSE_TIMEOUT, agent, setAgent } from '@/utils/got.js';
import { plugins as builtinPlugins } from '@/plugins/index.js';
export type SummalyPlugin = _SummalyPlugin;
export type SummalyOptions = { export type SummalyOptions = {
/** /**
@ -33,7 +32,7 @@ export type SummalyOptions = {
/** /**
* Custom HTTP agent * Custom HTTP agent
*/ */
agent?: Got.Agents; agent?: GotAgents;
/** /**
* User-Agent for the request * User-Agent for the request
@ -85,7 +84,26 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
if (opts.followRedirects) { if (opts.followRedirects) {
// .catch(() => url)にすればいいけど、jestにtrace-redirectを食わせるのが面倒なのでtry-catch // .catch(() => url)にすればいいけど、jestにtrace-redirectを食わせるのが面倒なのでtry-catch
try { 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) { } catch (e) {
actualUrl = url; actualUrl = url;
} }
@ -107,7 +125,7 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
}; };
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const summary = await (match ? match.summarize : general)(_url, scrapingOptions); const summary = await (match ? match.summarize : parseGeneral)(_url, scrapingOptions);
if (summary == null) { if (summary == null) {
throw new Error('failed summarize'); throw new Error('failed summarize');
@ -118,12 +136,13 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
}); });
}; };
// eslint-disable-next-line import/no-default-export
export default function (fastify: FastifyInstance, options: SummalyOptions, done: (err?: Error) => void) { export default function (fastify: FastifyInstance, options: SummalyOptions, done: (err?: Error) => void) {
fastify.get<{ fastify.get<{
Querystring: { Querystring: {
url?: string; url?: string;
lang?: string; lang?: string;
}; };
}>('/', async (req, reply) => { }>('/', async (req, reply) => {
const url = req.query.url as string; const url = req.query.url as string;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition

View File

@ -1,6 +1,5 @@
import Summary from './summary.js'; import type Summary from '@/summary.js';
import type { URL } from 'node:url'; import type { GeneralScrapingOptions } from '@/general.js';
import { GeneralScrapingOptions } from '@/general';
export interface SummalyPlugin { export interface SummalyPlugin {
test: (url: URL) => boolean; test: (url: URL) => boolean;

View File

@ -1,6 +1,5 @@
import { URL } from 'node:url'; import { scpaping } from '@/utils/got.js';
import { scpaping } from '../utils/got.js'; import summary from '@/summary.js';
import summary from '../summary.js';
export function test(url: URL): boolean { export function test(url: URL): boolean {
return url.hostname === 'www.amazon.com' || return url.hostname === 'www.amazon.com' ||

View File

@ -1,6 +1,5 @@
import { URL } from 'node:url'; import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';
import general, { GeneralScrapingOptions } from '../general.js'; import Summary from '@/summary.js';
import Summary from '../summary.js';
export function test(url: URL): boolean { export function test(url: URL): boolean {
// Branch.io を使用したディープリンクにマッチ // Branch.io を使用したディープリンクにマッチ
@ -13,5 +12,5 @@ export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promis
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ // Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
url.searchParams.append('$web_only', 'true'); url.searchParams.append('$web_only', 'true');
return await general(url, opts); return await parseGeneral(url, opts);
} }

View File

@ -1,8 +1,7 @@
import { URL } from 'node:url';
import debug from 'debug'; import debug from 'debug';
import { get } from '../utils/got.js'; import { get } from '@/utils/got.js';
import summary from '../summary.js'; import summary from '@/summary.js';
import clip from './../utils/clip.js'; import { clip } from '@/utils/clip.js';
const log = debug('summaly:plugins:wikipedia'); const log = debug('summaly:plugins:wikipedia');

View File

@ -47,6 +47,7 @@ export type SummalyResult = Summary & {
url: string; url: string;
}; };
// eslint-disable-next-line import/no-default-export
export default Summary; export default Summary;
export type Player = { export type Player = {

View File

@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */ /* eslint-disable no-param-reassign */
import escapeRegExp from 'escape-regexp'; import escapeRegExp from 'escape-regexp';
export default function(title: string, siteName?: string | null): string { export function cleanupTitle(title: string, siteName?: string | null): string {
title = title.trim(); title = title.trim();
if (siteName) { if (siteName) {

View File

@ -1,6 +1,6 @@
import nullOrEmpty from './null-or-empty.js'; import { nullOrEmpty } from './null-or-empty.js';
export default function(s: string, max: number): string { export function clip(s: string, max: number): string {
if (nullOrEmpty(s)) { if (nullOrEmpty(s)) {
return s; return s;
} }

View File

@ -31,10 +31,10 @@ export type GotOptions = {
const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8')); const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8'));
const DEFAULT_RESPONSE_TIMEOUT = 20 * 1000; export const DEFAULT_RESPONSE_TIMEOUT = 20 * 1000;
const DEFAULT_OPERATION_TIMEOUT = 60 * 1000; export const DEFAULT_OPERATION_TIMEOUT = 60 * 1000;
const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024; export const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024;
const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`; export const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`;
export async function scpaping( export async function scpaping(
url: string, url: string,

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable @typescript-eslint/no-unnecessary-condition */
export default function(val: string): boolean { export function nullOrEmpty(val: string): boolean {
if (val === undefined) { if (val === undefined) {
return true; return true;
} else if (val === null) { } else if (val === null) {

View File

@ -13,7 +13,7 @@ import { fileURLToPath } from 'node:url';
import { Agent as httpAgent } from 'node:http'; import { Agent as httpAgent } from 'node:http';
import { Agent as httpsAgent } from 'node:https'; import { Agent as httpsAgent } from 'node:https';
import { expect, test, describe, beforeEach, afterEach } from '@jest/globals'; import { expect, test, describe, beforeEach, afterEach } from '@jest/globals';
import fastify from 'fastify'; import fastify, { type FastifyInstance } from 'fastify';
import { summaly } from '../src/index.js'; import { summaly } from '../src/index.js';
import { StatusError } from '../src/utils/status-error.js'; import { StatusError } from '../src/utils/status-error.js';
@ -34,7 +34,7 @@ const host = `http://localhost:${port}`;
// Display detail of unhandled promise rejection // Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir); process.on('unhandledRejection', console.dir);
let app: ReturnType<typeof fastify> | null = null; let app: FastifyInstance | null = null;
afterEach(async () => { afterEach(async () => {
if (app) { if (app) {
@ -71,7 +71,7 @@ test('basic', async () => {
}, },
sitename: 'localhost:3060', sitename: 'localhost:3060',
sensitive: false, sensitive: false,
url: host, url: host + '/',
activityPub: null, activityPub: null,
}); });
}); });
@ -83,7 +83,7 @@ test('Stage Bye Stage', async () => {
expect(summary).toEqual( expect(summary).toEqual(
{ {
'title': '【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)', 'title': '【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)',
'icon': 'https://www.youtube.com/s/desktop/4feff1e2/img/favicon.ico', 'icon': 'https://www.youtube.com/s/desktop/711fd789/img/logos/favicon.ico',
'description': 'Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...', 'description': 'Website▶https://columbia.jp/idolmaster/Playlist▶https://www.youtube.com/playlist?list=PL83A2998CF3BBC86D2018年7月18日発売予定THE IDOLM@STER CINDERELLA GIRLS CG STAR...',
'thumbnail': 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg', 'thumbnail': 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg',
'player': { 'player': {

View File

@ -1,6 +0,0 @@
{
"extends": "../tsconfig.json",
"include": [
"./**/*.ts"
],
}

View File

@ -10,8 +10,8 @@
"declaration": true, "declaration": true,
"sourceMap": false, "sourceMap": false,
"target": "es2021", "target": "es2021",
"module": "esnext", "module": "nodenext",
"moduleResolution": "node", "moduleResolution": "nodenext",
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"removeComments": false, "removeComments": false,
"noLib": false, "noLib": false,
@ -23,7 +23,6 @@
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,
"resolveJsonModule": true, "resolveJsonModule": true,
"isolatedModules": true, "isolatedModules": true,
"rootDir": "./src",
"baseUrl": "./", "baseUrl": "./",
"paths": { "paths": {
"@/*": [ "@/*": [
@ -44,6 +43,10 @@
}, },
"compileOnSave": false, "compileOnSave": false,
"include": [ "include": [
"./src/**/*.ts" "./src/**/*"
], ],
"exclude": [
"node_modules",
"test/**/*"
]
} }