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
------------------
* GETリクエストよりも前にHEADリクエストを送信し、その結果を使用して検証するように (#22)
* 下記のパラメータを`summaly`メソッドのオプションに追加
- userAgent

View File

@ -134,7 +134,7 @@ will be ... ↓
```json
{
"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...",
"thumbnail": "https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg",
"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
// directly import `.ts` files without this hack.
'^(\\.{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

View File

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

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

View File

@ -3,16 +3,15 @@
* https://github.com/misskey-dev/summaly
*/
import { URL } from 'node:url';
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 { got, type Agents as GotAgents } from 'got';
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 = {
/**
@ -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<Su
if (opts.followRedirects) {
// .catch(() => 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;
}
@ -107,7 +125,7 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
};
// 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) {
throw new Error('failed summarize');
@ -118,6 +136,7 @@ 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) {
fastify.get<{
Querystring: {

View File

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

View File

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

View File

@ -1,6 +1,5 @@
import { URL } from 'node:url';
import general, { GeneralScrapingOptions } from '../general.js';
import Summary from '../summary.js';
import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';
import Summary from '@/summary.js';
export function test(url: URL): boolean {
// Branch.io を使用したディープリンクにマッチ
@ -13,5 +12,5 @@ export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promis
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
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 { get } from '../utils/got.js';
import summary from '../summary.js';
import clip from './../utils/clip.js';
import { get } from '@/utils/got.js';
import summary from '@/summary.js';
import { clip } from '@/utils/clip.js';
const log = debug('summaly:plugins:wikipedia');

View File

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

View File

@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
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();
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)) {
return s;
}

View File

@ -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,

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
export default function(val: string): boolean {
export function nullOrEmpty(val: string): boolean {
if (val === undefined) {
return true;
} 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 httpsAgent } from 'node:https';
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 { StatusError } from '../src/utils/status-error.js';
@ -34,7 +34,7 @@ const host = `http://localhost:${port}`;
// Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir);
let app: ReturnType<typeof fastify> | null = null;
let app: FastifyInstance | null = null;
afterEach(async () => {
if (app) {
@ -71,7 +71,7 @@ test('basic', async () => {
},
sitename: 'localhost:3060',
sensitive: false,
url: host,
url: host + '/',
activityPub: null,
});
});
@ -83,7 +83,7 @@ test('Stage Bye Stage', async () => {
expect(summary).toEqual(
{
'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...',
'thumbnail': 'https://i.ytimg.com/vi/NMIEAhH_fTU/maxresdefault.jpg',
'player': {

View File

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

View File

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