7 Commits
5.0.0 ... 5.1.0

19 changed files with 467 additions and 257 deletions

View File

@ -3,120 +3,14 @@ module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
project: ['./tsconfig.json', './test/tsconfig.json'],
},
plugins: [
'@typescript-eslint',
'import'
],
ignorePatterns: ['**/.eslintrc.cjs'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/recommended',
'plugin:import/typescript'
'plugin:@misskey-dev/recommended',
],
rules: {
'indent': ['warn', 'tab', {
'SwitchCase': 1,
'MemberExpression': 1,
'flatTernaryExpressions': true,
'ArrayExpression': 'first',
'ObjectExpression': 'first',
}],
'eol-last': ['error', 'always'],
'semi': ['error', 'always'],
'semi-spacing': ['error', { 'before': false, 'after': true }],
'quotes': ['warn', 'single'],
'comma-dangle': ['warn', 'always-multiline'],
'comma-spacing': ['error', { 'before': false, 'after': true }],
'array-bracket-spacing': ['error', 'never'],
'keyword-spacing': ['error', {
'before': true,
'after': true,
}],
'key-spacing': ['error', {
'beforeColon': false,
'afterColon': true,
}],
'arrow-spacing': ['error', {
'before': true,
'after': true,
}],
'brace-style': ['error', '1tbs', {
'allowSingleLine': true,
}],
'padded-blocks': ['error', 'never'],
/* TODO: path aliasを使わないとwarnする
'no-restricted-imports': ['warn', {
'patterns': [
]
}],
*/
'eqeqeq': ['error', 'always', { 'null': 'ignore' }],
'no-multi-spaces': ['error'],
'no-var': ['error'],
'prefer-arrow-callback': ['error'],
'no-throw-literal': ['error'],
'no-param-reassign': ['warn'],
'no-constant-condition': ['warn'],
'no-empty-pattern': ['warn'],
'no-async-promise-executor': ['off'],
'no-useless-escape': ['off'],
'no-multiple-empty-lines': ['error', { 'max': 1 }],
'no-control-regex': ['warn'],
'no-empty': ['warn'],
'no-inner-declarations': ['off'],
'no-sparse-arrays': ['off'],
'nonblock-statement-body-position': ['error', 'beside'],
'object-curly-spacing': ['error', 'always'],
'space-infix-ops': ['error'],
'space-before-blocks': ['error', 'always'],
'padding-line-between-statements': [
'error',
{ 'blankLine': 'always', 'prev': 'function', 'next': '*' },
{ 'blankLine': 'always', 'prev': '*', 'next': 'function' },
],
"lines-between-class-members": "off",
/* typescript-eslint では enforce に対応してないっぽい
'@typescript-eslint/lines-between-class-members': ['error', {
enforce: [{
blankLine: 'always',
prev: 'method',
next: '*',
}]
}],
*/
'@typescript-eslint/func-call-spacing': ['error', 'never'],
'@typescript-eslint/no-explicit-any': ['warn'],
'@typescript-eslint/no-unused-vars': ['warn'],
'@typescript-eslint/no-unnecessary-condition': ['warn'],
'@typescript-eslint/no-var-requires': ['warn'],
'@typescript-eslint/no-inferrable-types': ['warn'],
'@typescript-eslint/no-empty-function': ['off'],
'@typescript-eslint/no-non-null-assertion': ['warn'],
'@typescript-eslint/explicit-function-return-type': ['off'],
'@typescript-eslint/no-misused-promises': ['error', {
'checksVoidReturn': false,
}],
'@typescript-eslint/consistent-type-imports': 'off',
'@typescript-eslint/prefer-nullish-coalescing': [
'warn',
],
'@typescript-eslint/naming-convention': [
'error',
{
"selector": "typeLike",
"format": ["PascalCase"]
},
{
"selector": "typeParameter",
"format": []
}
],
'import/no-unresolved': ['off'],
'import/no-default-export': ['warn'],
'import/order': ['warn', {
'groups': ['builtin', 'external', 'internal', 'parent', 'sibling', 'index', 'object', 'type'],
}]
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'import/no-default-export': 'off',
},
};

View File

@ -10,6 +10,10 @@ jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
strategy:
matrix:
node-version: [20.10.0]
@ -26,6 +30,7 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
- name: Publish package
run: |
corepack enable
@ -34,3 +39,4 @@ jobs:
pnpm publish --access public --no-git-checks --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
NPM_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}

View File

@ -1,3 +1,24 @@
5.1.0 / 2024-03-18
* GETリクエストよりも前にHEADリクエストを送信し、その結果を使用して検証するように (#22)
* 下記のパラメータを`summaly`メソッドのオプションに追加
- userAgent
- responseTimeout
- operationTimeout
- contentLengthLimit
- contentLengthRequired
5.0.3 / 2023-12-30
------------------
* Fix .github/workflows/npm-publish.yml
5.0.2 / 2023-12-30
------------------
* Fix .github/workflows/npm-publish.yml
5.0.1 / 2023-12-30
------------------
* Fix .github/workflows/npm-publish.yml
5.0.0 / 2023-12-30
------------------
* support `<link rel="alternate" type="application/activitypub+json" href="{href}">` https://github.com/misskey-dev/summaly/pull/10, https://github.com/misskey-dev/summaly/pull/11

View File

@ -43,12 +43,17 @@ npm run serve
#### opts (SummalyOptions)
| Property | Type | Description | Default |
| :------------------ | :--------------------- | :------------------------------ | :------ |
| **lang** | *string* | Accept-Language for the request | `null` |
| **followRedirects** | *boolean* | Whether follow redirects | `true` |
| **plugins** | *plugin[]* (see below) | Custom plugins | `null` |
| **agent** | *Got.Agents* | Custom HTTP agent (see below) | `null` |
| Property | Type | Description | Default |
|:--------------------------|:-----------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------|
| **lang** | *string* | Accept-Language for the request | `null` |
| **followRedirects** | *boolean* | Whether follow redirects | `true` |
| **plugins** | *plugin[]* (see below) | Custom plugins | `null` |
| **agent** | *Got.Agents* | Custom HTTP agent (see below) | `null` |
| **userAgent** | *string* | User-Agent for the request | `SummalyBot/[version]` |
| **responseTimeout** | *number* | Set timeouts for each phase, such as host name resolution and socket communication. | `20000` |
| **operationTimeout** | *number* | Set the timeout from the start to the end of the request. | `60000` |
| **contentLengthLimit** | *number* | If set to true, an error will occur if the content-length value returned from the other server is larger than this parameter (or if the received body size exceeds this parameter). | `10485760` |
| **contentLengthRequired** | *boolean* | If set to true, it will be an error if the other server does not return content-length. | `false` |
#### Plugin
@ -78,17 +83,17 @@ A Promise of an Object that contains properties below:
#### SummalyResult
| Property | Type | Description |
| :-------------- | :------- | :------------------------------------------ |
| **title** | *string* \| *null* | The title of the web page |
| **icon** | *string* \| *null* | The url of the icon of the web page |
| **description** | *string* \| *null* | The description of the web page |
| **thumbnail** | *string* \| *null* | The url of the thumbnail of the web page |
| **sitename** | *string* \| *null* | The name of the web site |
| **player** | *Player* | The player of the web page |
| **sensitive** | *boolean* | Whether the url is sensitive |
| Property | Type | Description |
|:----------------|:-------------------|:-----------------------------------------------------------|
| **title** | *string* \| *null* | The title of the web page |
| **icon** | *string* \| *null* | The url of the icon of the web page |
| **description** | *string* \| *null* | The description of the web page |
| **thumbnail** | *string* \| *null* | The url of the thumbnail of the web page |
| **sitename** | *string* \| *null* | The name of the web site |
| **player** | *Player* | The player of the web page |
| **sensitive** | *boolean* | Whether the url is sensitive |
| **activityPub** | *string* \| *null* | The url of the ActivityPub representation of that web page |
| **url** | *string* | The url of the web page |
| **url** | *string* | The url of the web page |
#### Summary
@ -96,12 +101,12 @@ A Promise of an Object that contains properties below:
#### Player
| Property | Type | Description |
| :-------------- | :--------- | :---------------------------------------------- |
| **url** | *string* \| *null* | The url of the player |
| **width** | *number* \| *null* | The width of the player |
| **height** | *number* \| *null* | The height of the player |
| **allow** | *string[]* | The names of the allowed permissions for iframe |
| Property | Type | Description |
|:-----------|:-------------------|:------------------------------------------------|
| **url** | *string* \| *null* | The url of the player |
| **width** | *number* \| *null* | The width of the player |
| **height** | *number* \| *null* | The height of the player |
| **allow** | *string[]* | The names of the allowed permissions for iframe |
Currently the possible items in `allow` are:

View File

@ -1,6 +1,6 @@
{
"name": "@misskey-dev/summaly",
"version": "5.0.0",
"version": "5.1.0",
"description": "Get web page's summary",
"author": "syuilo <syuilotan@yahoo.co.jp>",
"license": "MIT",
@ -16,12 +16,13 @@
],
"scripts": {
"build": "tsc",
"eslint": "eslint --quiet \"src/**/*.ts\"",
"eslint": "eslint src --ext .js,.jsx,.ts,.tsx",
"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",

17
pnpm-lock.yaml generated
View File

@ -34,6 +34,9 @@ devDependencies:
'@jest/globals':
specifier: ^29.7.0
version: 29.7.0
'@misskey-dev/eslint-plugin':
specifier: ^1.0.0
version: 1.0.0(@typescript-eslint/eslint-plugin@6.16.0)(@typescript-eslint/parser@6.16.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0)
'@swc/core':
specifier: ^1.3.101
version: 1.3.101
@ -781,6 +784,20 @@ packages:
'@jridgewell/sourcemap-codec': 1.4.15
dev: true
/@misskey-dev/eslint-plugin@1.0.0(@typescript-eslint/eslint-plugin@6.16.0)(@typescript-eslint/parser@6.16.0)(eslint-plugin-import@2.29.1)(eslint@8.56.0):
resolution: {integrity: sha512-dh6UbcrNDVg5DD8k8Qh4ab30OPpuEYIlJCqaBV/lkIV8wNN/AfCJ2V7iTP8V8KjryM4t+sf5IqzQLQnT0mWI4A==}
peerDependencies:
'@typescript-eslint/eslint-plugin': '>= 6'
'@typescript-eslint/parser': '>= 6'
eslint: '>= 3'
eslint-plugin-import: '>= 2'
dependencies:
'@typescript-eslint/eslint-plugin': 6.16.0(@typescript-eslint/parser@6.16.0)(eslint@8.56.0)(typescript@5.3.3)
'@typescript-eslint/parser': 6.16.0(eslint@8.56.0)(typescript@5.3.3)
eslint: 8.56.0
eslint-plugin-import: 2.29.1(@typescript-eslint/parser@6.16.0)(eslint@8.56.0)
dev: true
/@nodelib/fs.scandir@2.1.5:
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
engines: {node: '>= 8'}

View File

@ -36,7 +36,7 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
const body = (() => {
try {
return JSON.parse(oEmbed);
} catch {}
} catch { /* empty */ }
})();
if (!body || body.version !== '1.0' || !['rich', 'video'].includes(body.type)) {
@ -130,12 +130,30 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
};
}
export default async (_url: URL | string, lang: string | null = null): Promise<Summary | null> => {
export type GeneralScrapingOptions = {
lang?: string | null;
userAgent?: string;
responseTimeout?: number;
operationTimeout?: number;
contentLengthLimit?: number;
contentLengthRequired?: boolean;
}
export default async (_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;
const res = await scpaping(url.href, { lang: lang || undefined });
const res = await scpaping(url.href, {
lang: lang || undefined,
userAgent: opts?.userAgent,
responseTimeout: opts?.responseTimeout,
operationTimeout: opts?.operationTimeout,
contentLengthLimit: opts?.contentLengthLimit,
contentLengthRequired: opts?.contentLengthRequired,
});
const $ = res.$;
const twitterCard =
$('meta[name="twitter:card"]').attr('content') ||
@ -153,6 +171,7 @@ export default async (_url: URL | string, lang: string | null = null): Promise<S
$('meta[property="twitter:title"]').attr('content') ||
$('title').text();
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (title === undefined || title === null) {
return null;
}

View File

@ -5,14 +5,14 @@
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 from './general.js';
import * as Got from 'got';
import general, { GeneralScrapingOptions } from './general.js';
import { setAgent } from './utils/got.js';
import type { FastifyInstance } from 'fastify';
import { plugins as builtinPlugins } from './plugins/index.js';
import type { FastifyInstance } from 'fastify';
export type SummalyOptions = {
/**
@ -34,6 +34,35 @@ export type SummalyOptions = {
* Custom HTTP agent
*/
agent?: Got.Agents;
/**
* User-Agent for the request
*/
userAgent?: string;
/**
* Response timeout.
* Set timeouts for each phase, such as host name resolution and socket communication.
*/
responseTimeout?: number;
/**
* Operation timeout.
* Set the timeout from the start to the end of the request.
*/
operationTimeout?: number;
/**
* Maximum content length.
* If set to true, an error will occur if the content-length value returned from the other server is larger than this parameter (or if the received body size exceeds this parameter).
*/
contentLengthLimit?: number;
/**
* Content length required.
* If set to true, it will be an error if the other server does not return content-length.
*/
contentLengthRequired?: boolean;
};
export const summalyDefaultOptions = {
@ -68,14 +97,24 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
const match = plugins.filter(plugin => plugin.test(_url))[0];
// Get summary
const summary = await (match ? match.summarize : general)(_url, opts.lang || undefined);
const scrapingOptions: GeneralScrapingOptions = {
lang: opts.lang,
userAgent: opts.userAgent,
responseTimeout: opts.responseTimeout,
operationTimeout: opts.operationTimeout,
contentLengthLimit: opts.contentLengthLimit,
contentLengthRequired: opts.contentLengthRequired,
};
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const summary = await (match ? match.summarize : general)(_url, scrapingOptions);
if (summary == null) {
throw new Error('failed summarize');
}
return Object.assign(summary, {
url: actualUrl
url: actualUrl,
});
};
@ -87,9 +126,10 @@ export default function (fastify: FastifyInstance, options: SummalyOptions, done
};
}>('/', async (req, reply) => {
const url = req.query.url as string;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (url == null) {
return reply.status(400).send({
error: 'url is required'
error: 'url is required',
});
}
@ -103,7 +143,7 @@ export default function (fastify: FastifyInstance, options: SummalyOptions, done
return summary;
} catch (e) {
return reply.status(500).send({
error: e
error: e,
});
}
});

View File

@ -1,7 +1,8 @@
import type { URL } from 'node:url';
import Summary from './summary.js';
import type { URL } from 'node:url';
import { GeneralScrapingOptions } from '@/general';
export interface SummalyPlugin {
test: (url: URL) => boolean;
summarize: (url: URL, lang?: string) => Promise<Summary | null>;
summarize: (url: URL, opts?: GeneralScrapingOptions) => Promise<Summary | null>;
}

View File

@ -1,6 +1,5 @@
import { URL } from 'node:url';
import { scpaping } from '../utils/got.js';
import general from '../general.js';
import general, { GeneralScrapingOptions } from '../general.js';
import Summary from '../summary.js';
export function test(url: URL): boolean {
@ -9,10 +8,10 @@ export function test(url: URL): boolean {
url.hostname === 'spotify.link';
}
export async function summarize(url: URL, lang: string | null = null): Promise<Summary | null> {
export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promise<Summary | null> {
// https://help.branch.io/using-branch/docs/creating-a-deep-link#redirections
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
url.searchParams.append('$web_only', 'true');
return await general(url, lang);
return await general(url, opts);
}

View File

@ -1,10 +1,10 @@
import { SummalyPlugin } from '@/iplugin.js';
import * as amazon from './amazon.js';
import * as wikipedia from './wikipedia.js';
import * as branchIoDeeplinks from './branchio-deeplinks.js';
import { SummalyPlugin } from '@/iplugin.js';
export const plugins: SummalyPlugin[] = [
amazon,
wikipedia,
branchIoDeeplinks,
amazon,
wikipedia,
branchIoDeeplinks,
];

View File

@ -1,6 +1,6 @@
import { URL } from 'node:url';
import { get } from '../utils/got.js';
import debug from 'debug';
import { get } from '../utils/got.js';
import summary from '../summary.js';
import clip from './../utils/clip.js';
@ -20,6 +20,7 @@ export async function summarize(url: URL): Promise<summary> {
log(`title is ${title}`);
log(`endpoint is ${endpoint}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let body = await get(endpoint) as any;
body = JSON.parse(body);
log(body);

View File

@ -1,3 +1,4 @@
/* eslint-disable no-param-reassign */
import escapeRegExp from 'escape-regexp';
export default function(title: string, siteName?: string | null): string {
@ -9,12 +10,12 @@ export default function(title: string, siteName?: string | null): string {
const x = escapeRegExp(siteName);
const patterns = [
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`,
];
for (let i = 0; i < patterns.length; i++) {
const pattern = new RegExp(patterns[i]);
const [, match] = pattern.exec(title) || [null, null];
const [, match] = pattern.exec(title) ?? [null, null];
if (match) {
return match;
}

View File

@ -5,6 +5,7 @@ export default function(s: string, max: number): string {
return s;
}
// eslint-disable-next-line no-param-reassign
s = s.trim();
if (s.length > max) {

View File

@ -11,6 +11,7 @@ const regCharset = new RegExp(/charset\s*=\s*["']?([\w-]+)/, 'i');
export function detectEncoding(body: Buffer): string {
// By detection
const detected = jschardet.detect(body, { minimumThreshold: 0.99 });
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (detected) {
const candicate = detected.encoding;
const encoding = toEncoding(candicate);

View File

@ -1,17 +1,19 @@
import got, * as Got from 'got';
import { StatusError } from './status-error.js';
import { detectEncoding, toUtf8 } from './encoding.js';
import * as cheerio from 'cheerio';
import PrivateIp from 'private-ip';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import got, * as Got from 'got';
import * as cheerio from 'cheerio';
import PrivateIp from 'private-ip';
import { StatusError } from './status-error.js';
import { detectEncoding, toUtf8 } from './encoding.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
export let agent: Got.Agents = {};
export function setAgent(_agent: Got.Agents) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
agent = _agent || {};
}
@ -21,34 +23,60 @@ export type GotOptions = {
body?: string;
headers: Record<string, string | undefined>;
typeFilter?: RegExp;
responseTimeout?: number;
operationTimeout?: number;
contentLengthLimit?: number;
contentLengthRequired?: boolean;
}
const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8'));
const RESPONSE_TIMEOUT = 20 * 1000;
const OPERATION_TIMEOUT = 60 * 1000;
const MAX_RESPONSE_SIZE = 10 * 1024 * 1024;
const BOT_UA = `SummalyBot/${repo.version}`;
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 async function scpaping(url: string, opts?: { lang?: string; }) {
const response = await getResponse({
export async function scpaping(
url: string,
opts?: {
lang?: string;
userAgent?: string;
responseTimeout?: number;
operationTimeout?: number;
contentLengthLimit?: number;
contentLengthRequired?: boolean;
},
) {
const args: Omit<GotOptions, 'method'> = {
url,
method: 'GET',
headers: {
'accept': 'text/html,application/xhtml+xml',
'user-agent': BOT_UA,
'accept-language': opts?.lang
'user-agent': opts?.userAgent ?? DEFAULT_BOT_UA,
'accept-language': opts?.lang,
},
typeFilter: /^(text\/html|application\/xhtml\+xml)/,
responseTimeout: opts?.responseTimeout,
operationTimeout: opts?.operationTimeout,
contentLengthLimit: opts?.contentLengthLimit,
contentLengthRequired: opts?.contentLengthRequired,
};
const headResponse = await getResponse({
...args,
method: 'HEAD',
});
// SUMMALY_ALLOW_PRIVATE_IPはテスト用
const allowPrivateIp = process.env.SUMMALY_ALLOW_PRIVATE_IP === 'true' || Object.keys(agent).length > 0;
if (!allowPrivateIp && response.ip && PrivateIp(response.ip)) {
throw new StatusError(`Private IP rejected ${response.ip}`, 400, 'Private IP Rejected');
if (!allowPrivateIp && headResponse.ip && PrivateIp(headResponse.ip)) {
throw new StatusError(`Private IP rejected ${headResponse.ip}`, 400, 'Private IP Rejected');
}
const response = await getResponse({
...args,
method: 'GET',
});
const encoding = detectEncoding(response.rawBody);
const body = toUtf8(response.rawBody, encoding);
const $ = cheerio.load(body);
@ -69,24 +97,22 @@ export async function get(url: string) {
},
});
return await res.body;
return res.body;
}
export async function head(url: string) {
const res = await getResponse({
return await getResponse({
url,
method: 'HEAD',
headers: {
'accept': '*/*',
},
});
return await res;
}
async function getResponse(args: GotOptions) {
const timeout = RESPONSE_TIMEOUT;
const operationTimeout = OPERATION_TIMEOUT;
const timeout = args.responseTimeout ?? DEFAULT_RESPONSE_TIMEOUT;
const operationTimeout = args.operationTimeout ?? DEFAULT_OPERATION_TIMEOUT;
const req = got<string>(args.url, {
method: args.method,
@ -108,30 +134,37 @@ async function getResponse(args: GotOptions) {
},
});
return await receiveResponse({ req, typeFilter: args.typeFilter });
const res = await receiveResponse({ req, opts: args });
// Check html
const contentType = res.headers['content-type'];
if (args.typeFilter && !contentType?.match(args.typeFilter)) {
throw new Error(`Rejected by type filter ${contentType}`);
}
// 応答ヘッダでサイズチェック
const contentLength = res.headers['content-length'];
if (contentLength) {
const maxSize = args.contentLengthLimit ?? DEFAULT_MAX_RESPONSE_SIZE;
const size = Number(contentLength);
if (size > maxSize) {
throw new Error(`maxSize exceeded (${size} > ${maxSize}) on response`);
}
} else {
if (args.contentLengthRequired) {
throw new Error('content-length required');
}
}
return res;
}
async function receiveResponse<T>(args: { req: Got.CancelableRequest<Got.Response<T>>, typeFilter?: RegExp }) {
async function receiveResponse<T>(args: {
req: Got.CancelableRequest<Got.Response<T>>,
opts: GotOptions,
}) {
const req = args.req;
const maxSize = MAX_RESPONSE_SIZE;
req.on('response', (res: Got.Response) => {
// Check html
if (args.typeFilter && !res.headers['content-type']?.match(args.typeFilter)) {
// console.warn(res.headers['content-type']);
req.cancel(`Rejected by type filter ${res.headers['content-type']}`);
return;
}
// 応答ヘッダでサイズチェック
const contentLength = res.headers['content-length'];
if (contentLength != null) {
const size = Number(contentLength);
if (size > maxSize) {
req.cancel(`maxSize exceeded (${size} > ${maxSize}) on response`);
}
}
});
const maxSize = args.opts.contentLengthLimit ?? DEFAULT_MAX_RESPONSE_SIZE;
// 受信中のデータでサイズチェック
req.on('downloadProgress', (progress: Got.Progress) => {

View File

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

View File

@ -8,13 +8,13 @@
import fs, { readdirSync } from 'node:fs';
import process from 'node:process';
import fastify from 'fastify';
import { summaly } from '../src/index.js';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { expect, jest, test, describe, beforeEach, afterEach } from '@jest/globals';
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 { summaly } from '../src/index.js';
import { StatusError } from '../src/utils/status-error.js';
const _filename = fileURLToPath(import.meta.url);
@ -35,7 +35,6 @@ const host = `http://localhost:${port}`;
process.on('unhandledRejection', console.dir);
let app: ReturnType<typeof fastify> | null = null;
let n = 0;
afterEach(async () => {
if (app) {
@ -49,7 +48,10 @@ afterEach(async () => {
test('basic', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/basic.html'));
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect(await summaly(host)).toEqual({
@ -61,10 +63,10 @@ test('basic', async () => {
url: null,
width: null,
height: null,
"allow": [
"autoplay",
"encrypted-media",
"fullscreen",
'allow': [
'autoplay',
'encrypted-media',
'fullscreen',
],
},
sitename: 'localhost:3060',
@ -80,35 +82,38 @@ test('Stage Bye Stage', async () => {
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
expect(summary).toEqual(
{
"title": "【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)",
"icon": "https://www.youtube.com/s/desktop/28b0985e/img/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": {
"url": "https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed",
"width": 200,
"height": 113,
"allow": [
"autoplay",
"clipboard-write",
"encrypted-media",
"picture-in-picture",
"web-share",
"fullscreen",
]
'title': '【アイドルマスター】「Stage Bye Stage」(歌:島村卯月、渋谷凛、本田未央)',
'icon': 'https://www.youtube.com/s/desktop/4feff1e2/img/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': {
'url': 'https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed',
'width': 200,
'height': 113,
'allow': [
'autoplay',
'clipboard-write',
'encrypted-media',
'picture-in-picture',
'web-share',
'fullscreen',
],
},
"sitename": "YouTube",
"sensitive": false,
"activityPub": null,
"url": "https://www.youtube.com/watch?v=NMIEAhH_fTU"
}
'sitename': 'YouTube',
'sensitive': false,
'activityPub': null,
'url': 'https://www.youtube.com/watch?v=NMIEAhH_fTU',
},
);
});
test('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/no-favicon.html'));
const content = fs.readFileSync(_dirname + '/htmls/no-favicon.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
app.get('/favicon.ico', (_, reply) => reply.status(200).send());
await app.listen({ port });
@ -120,7 +125,10 @@ test('faviconがHTML上で指定されていないが、ルートに存在する
test('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/no-favicon.html'));
const content = fs.readFileSync(_dirname + '/htmls/no-favicon.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
app.get('*', (_, reply) => reply.status(404).send());
await app.listen({ port });
@ -132,7 +140,10 @@ test('faviconがHTML上で指定されていなくて、ルートにも存在し
test('titleがcleanupされる', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/dirty-title.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-title.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -145,7 +156,10 @@ describe('Private IP blocking', () => {
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'false';
app = fastify();
app.get('*', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-title.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
return app.listen({ port });
});
@ -164,7 +178,7 @@ describe('Private IP blocking', () => {
agent: {
http: new httpAgent({ keepAlive: true }),
https: new httpsAgent({ keepAlive: true }),
}
},
});
expect(summary.title).toBe('Strawberry Pasta');
});
@ -187,7 +201,10 @@ describe('OGP', () => {
test('title', async () => {
app = fastify();
app.get('*', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-title.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -198,7 +215,10 @@ describe('OGP', () => {
test('description', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-description.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-description.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -209,7 +229,10 @@ describe('OGP', () => {
test('site_name', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-site_name.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-site_name.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -220,7 +243,10 @@ describe('OGP', () => {
test('thumbnail', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-image.html'));
const content = fs.readFileSync(_dirname + '/htmls/og-image.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -233,7 +259,10 @@ describe('TwitterCard', () => {
test('title', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-title.html'));
const content = fs.readFileSync(_dirname + '/htmls/twitter-title.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -244,7 +273,10 @@ describe('TwitterCard', () => {
test('description', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-description.html'));
const content = fs.readFileSync(_dirname + '/htmls/twitter-description.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -255,7 +287,10 @@ describe('TwitterCard', () => {
test('thumbnail', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-image.html'));
const content = fs.readFileSync(_dirname + '/htmls/twitter-image.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -266,7 +301,10 @@ describe('TwitterCard', () => {
test('Player detection - PeerTube:video => video', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-peertube-video.html'));
const content = fs.readFileSync(_dirname + '/htmls/player-peertube-video.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -278,7 +316,10 @@ describe('TwitterCard', () => {
test('Player detection - Pleroma:video => video', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-video.html'));
const content = fs.readFileSync(_dirname + '/htmls/player-pleroma-video.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -290,7 +331,10 @@ describe('TwitterCard', () => {
test('Player detection - Pleroma:image => image', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-image.html'));
const content = fs.readFileSync(_dirname + '/htmls/player-pleroma-image.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -299,19 +343,23 @@ describe('TwitterCard', () => {
});
});
describe("oEmbed", () => {
describe('oEmbed', () => {
const setUpFastify = async (oEmbedPath: string, htmlPath = 'htmls/oembed.html') => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(new URL(htmlPath, import.meta.url)));
const content = fs.readFileSync(new URL(htmlPath, import.meta.url));
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
app.get('/oembed.json', (request, reply) => {
return reply.send(fs.createReadStream(
new URL(oEmbedPath, new URL('oembed/', import.meta.url))
));
const content = fs.readFileSync(new URL(oEmbedPath, new URL('oembed/', import.meta.url)));
reply.header('content-length', content.length);
reply.header('content-type', 'application/json');
return reply.send(content);
});
await app.listen({ port });
}
};
for (const filename of readdirSync(new URL('oembed/invalid', import.meta.url))) {
test(`Invalidity test: ${filename}`, async () => {
@ -433,7 +481,10 @@ describe('ActivityPub', () => {
test('Basic', async () => {
app = fastify();
app.get('*', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/activitypub.html'));
const content = fs.readFileSync(_dirname + '/htmls/activitypub.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -444,7 +495,10 @@ describe('ActivityPub', () => {
test('Null', async () => {
app = fastify();
app.get('*', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/basic.html'));
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
@ -457,7 +511,10 @@ describe('sensitive', () => {
test('default', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/basic.html'));
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect((await summaly(host)).sensitive).toBe(false);
@ -466,9 +523,115 @@ describe('sensitive', () => {
test('mixi:content-rating 1', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/mixi-sensitive.html'));
const content = fs.readFileSync(_dirname + '/htmls/mixi-sensitive.html');
reply.header('content-length', content.length);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect((await summaly(host)).sensitive).toBe(true);
});
});
describe('UserAgent', () => {
test('UA設定が反映されていること', async () => {
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
let ua: string | undefined = undefined;
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-length', content.byteLength);
reply.header('content-type', 'text/html');
ua = request.headers['user-agent'];
return reply.send(content);
});
await app.listen({ port });
await summaly(host, { userAgent: 'test-ua' });
expect(ua).toBe('test-ua');
});
});
describe('content-length limit', () => {
test('content-lengthの上限以内であればエラーが起こらないこと', async () => {
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-length', content.byteLength);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect(await summaly(host, { contentLengthLimit: content.byteLength })).toBeDefined();
});
test('content-lengthの上限を超えているとエラーになる事', async () => {
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-length', content.byteLength);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
await expect(summaly(host, { contentLengthLimit: content.byteLength - 1 })).rejects.toThrow();
});
});
describe('content-length required', () => {
test('[オプション有効化時] content-lengthが返された場合はエラーとならないこと', async () => {
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-length', content.byteLength);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect(await summaly(host, { contentLengthRequired: true, contentLengthLimit: content.byteLength })).toBeDefined();
});
test('[オプション有効化時] content-lengthが返されない場合はエラーとなること', async () => {
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-type', 'text/html');
// streamで渡さないとcontent-lengthを自動で設定されてしまう
return reply.send(fs.createReadStream(_dirname + '/htmls/basic.html'));
});
await app.listen({ port });
await expect(summaly(host, { contentLengthRequired: true })).rejects.toThrow();
});
test('[オプション無効化時] content-lengthが返された場合はエラーとならないこと', async () => {
const content = fs.readFileSync(_dirname + '/htmls/basic.html');
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-length', content.byteLength);
reply.header('content-type', 'text/html');
return reply.send(content);
});
await app.listen({ port });
expect(await summaly(host, { contentLengthRequired: false, contentLengthLimit: content.byteLength })).toBeDefined();
});
test('[オプション無効化時] content-lengthが返されなくてもエラーとならないこと', async () => {
app = fastify();
app.get('/', (request, reply) => {
reply.header('content-type', 'text/html');
// streamで渡さないとcontent-lengthを自動で設定されてしまう
return reply.send(fs.createReadStream(_dirname + '/htmls/basic.html'));
});
await app.listen({ port });
expect(await summaly(host, { contentLengthRequired: false })).toBeDefined();
});
});

6
test/tsconfig.json Normal file
View File

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