mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-08-07 16:54:01 +09:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
509a35abe2 | |||
79d9eadd58 | |||
c261071a82 | |||
71fe234d3e | |||
718465e498 | |||
77c53be159 | |||
541a0ddd0a |
116
.eslintrc.cjs
116
.eslintrc.cjs
@ -3,120 +3,14 @@ module.exports = {
|
|||||||
parser: '@typescript-eslint/parser',
|
parser: '@typescript-eslint/parser',
|
||||||
parserOptions: {
|
parserOptions: {
|
||||||
tsconfigRootDir: __dirname,
|
tsconfigRootDir: __dirname,
|
||||||
project: ['./tsconfig.json'],
|
project: ['./tsconfig.json', './test/tsconfig.json'],
|
||||||
},
|
},
|
||||||
plugins: [
|
ignorePatterns: ['**/.eslintrc.cjs'],
|
||||||
'@typescript-eslint',
|
|
||||||
'import'
|
|
||||||
],
|
|
||||||
extends: [
|
extends: [
|
||||||
'eslint:recommended',
|
'plugin:@misskey-dev/recommended',
|
||||||
'plugin:@typescript-eslint/recommended',
|
|
||||||
'plugin:import/recommended',
|
|
||||||
'plugin:import/typescript'
|
|
||||||
],
|
],
|
||||||
rules: {
|
rules: {
|
||||||
'indent': ['warn', 'tab', {
|
'@typescript-eslint/prefer-nullish-coalescing': 'off',
|
||||||
'SwitchCase': 1,
|
'import/no-default-export': 'off',
|
||||||
'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'],
|
|
||||||
}]
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
6
.github/workflows/npm-publish.yml
vendored
6
.github/workflows/npm-publish.yml
vendored
@ -10,6 +10,10 @@ jobs:
|
|||||||
publish:
|
publish:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
id-token: write
|
||||||
|
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
node-version: [20.10.0]
|
node-version: [20.10.0]
|
||||||
@ -26,6 +30,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
cache: 'pnpm'
|
cache: 'pnpm'
|
||||||
|
registry-url: 'https://registry.npmjs.org'
|
||||||
- name: Publish package
|
- name: Publish package
|
||||||
run: |
|
run: |
|
||||||
corepack enable
|
corepack enable
|
||||||
@ -34,3 +39,4 @@ jobs:
|
|||||||
pnpm publish --access public --no-git-checks --provenance
|
pnpm publish --access public --no-git-checks --provenance
|
||||||
env:
|
env:
|
||||||
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
|
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
|
||||||
|
NPM_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
|
||||||
|
21
CHANGELOG.md
21
CHANGELOG.md
@ -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
|
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
|
* 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
|
||||||
|
49
README.md
49
README.md
@ -43,12 +43,17 @@ npm run serve
|
|||||||
|
|
||||||
#### opts (SummalyOptions)
|
#### opts (SummalyOptions)
|
||||||
|
|
||||||
| Property | Type | Description | Default |
|
| Property | Type | Description | Default |
|
||||||
| :------------------ | :--------------------- | :------------------------------ | :------ |
|
|:--------------------------|:-----------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|:-----------------------|
|
||||||
| **lang** | *string* | Accept-Language for the request | `null` |
|
| **lang** | *string* | Accept-Language for the request | `null` |
|
||||||
| **followRedirects** | *boolean* | Whether follow redirects | `true` |
|
| **followRedirects** | *boolean* | Whether follow redirects | `true` |
|
||||||
| **plugins** | *plugin[]* (see below) | Custom plugins | `null` |
|
| **plugins** | *plugin[]* (see below) | Custom plugins | `null` |
|
||||||
| **agent** | *Got.Agents* | Custom HTTP agent (see below) | `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
|
#### Plugin
|
||||||
|
|
||||||
@ -78,17 +83,17 @@ A Promise of an Object that contains properties below:
|
|||||||
|
|
||||||
#### SummalyResult
|
#### SummalyResult
|
||||||
|
|
||||||
| Property | Type | Description |
|
| Property | Type | Description |
|
||||||
| :-------------- | :------- | :------------------------------------------ |
|
|:----------------|:-------------------|:-----------------------------------------------------------|
|
||||||
| **title** | *string* \| *null* | The title of the web page |
|
| **title** | *string* \| *null* | The title of the web page |
|
||||||
| **icon** | *string* \| *null* | The url of the icon 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 |
|
| **description** | *string* \| *null* | The description of the web page |
|
||||||
| **thumbnail** | *string* \| *null* | The url of the thumbnail 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 |
|
| **sitename** | *string* \| *null* | The name of the web site |
|
||||||
| **player** | *Player* | The player of the web page |
|
| **player** | *Player* | The player of the web page |
|
||||||
| **sensitive** | *boolean* | Whether the url is sensitive |
|
| **sensitive** | *boolean* | Whether the url is sensitive |
|
||||||
| **activityPub** | *string* \| *null* | The url of the ActivityPub representation of that web page |
|
| **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
|
#### Summary
|
||||||
|
|
||||||
@ -96,12 +101,12 @@ A Promise of an Object that contains properties below:
|
|||||||
|
|
||||||
#### Player
|
#### Player
|
||||||
|
|
||||||
| Property | Type | Description |
|
| Property | Type | Description |
|
||||||
| :-------------- | :--------- | :---------------------------------------------- |
|
|:-----------|:-------------------|:------------------------------------------------|
|
||||||
| **url** | *string* \| *null* | The url of the player |
|
| **url** | *string* \| *null* | The url of the player |
|
||||||
| **width** | *number* \| *null* | The width of the player |
|
| **width** | *number* \| *null* | The width of the player |
|
||||||
| **height** | *number* \| *null* | The height of the player |
|
| **height** | *number* \| *null* | The height of the player |
|
||||||
| **allow** | *string[]* | The names of the allowed permissions for iframe |
|
| **allow** | *string[]* | The names of the allowed permissions for iframe |
|
||||||
|
|
||||||
Currently the possible items in `allow` are:
|
Currently the possible items in `allow` are:
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@misskey-dev/summaly",
|
"name": "@misskey-dev/summaly",
|
||||||
"version": "5.0.0",
|
"version": "5.1.0",
|
||||||
"description": "Get web page's summary",
|
"description": "Get web page's summary",
|
||||||
"author": "syuilo <syuilotan@yahoo.co.jp>",
|
"author": "syuilo <syuilotan@yahoo.co.jp>",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@ -16,12 +16,13 @@
|
|||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"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",
|
"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",
|
||||||
"@swc/core": "^1.3.101",
|
"@swc/core": "^1.3.101",
|
||||||
"@swc/jest": "^0.2.29",
|
"@swc/jest": "^0.2.29",
|
||||||
"@types/cheerio": "0.22.18",
|
"@types/cheerio": "0.22.18",
|
||||||
|
17
pnpm-lock.yaml
generated
17
pnpm-lock.yaml
generated
@ -34,6 +34,9 @@ devDependencies:
|
|||||||
'@jest/globals':
|
'@jest/globals':
|
||||||
specifier: ^29.7.0
|
specifier: ^29.7.0
|
||||||
version: 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':
|
'@swc/core':
|
||||||
specifier: ^1.3.101
|
specifier: ^1.3.101
|
||||||
version: 1.3.101
|
version: 1.3.101
|
||||||
@ -781,6 +784,20 @@ packages:
|
|||||||
'@jridgewell/sourcemap-codec': 1.4.15
|
'@jridgewell/sourcemap-codec': 1.4.15
|
||||||
dev: true
|
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:
|
/@nodelib/fs.scandir@2.1.5:
|
||||||
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
|
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
|
||||||
engines: {node: '>= 8'}
|
engines: {node: '>= 8'}
|
||||||
|
@ -36,7 +36,7 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
|
|||||||
const body = (() => {
|
const body = (() => {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(oEmbed);
|
return JSON.parse(oEmbed);
|
||||||
} catch {}
|
} catch { /* empty */ }
|
||||||
})();
|
})();
|
||||||
|
|
||||||
if (!body || body.version !== '1.0' || !['rich', 'video'].includes(body.type)) {
|
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;
|
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;
|
||||||
|
|
||||||
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 $ = res.$;
|
||||||
const twitterCard =
|
const twitterCard =
|
||||||
$('meta[name="twitter:card"]').attr('content') ||
|
$('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') ||
|
$('meta[property="twitter:title"]').attr('content') ||
|
||||||
$('title').text();
|
$('title').text();
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
if (title === undefined || title === null) {
|
if (title === undefined || title === null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
54
src/index.ts
54
src/index.ts
@ -5,14 +5,14 @@
|
|||||||
|
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import tracer from 'trace-redirect';
|
import tracer from 'trace-redirect';
|
||||||
|
import * as Got from 'got';
|
||||||
import { SummalyResult } from './summary.js';
|
import { SummalyResult } from './summary.js';
|
||||||
import { SummalyPlugin } from './iplugin.js';
|
import { SummalyPlugin } from './iplugin.js';
|
||||||
export * from './iplugin.js';
|
export * from './iplugin.js';
|
||||||
import general from './general.js';
|
import general, { GeneralScrapingOptions } from './general.js';
|
||||||
import * as Got from 'got';
|
|
||||||
import { setAgent } from './utils/got.js';
|
import { setAgent } from './utils/got.js';
|
||||||
import type { FastifyInstance } from 'fastify';
|
|
||||||
import { plugins as builtinPlugins } from './plugins/index.js';
|
import { plugins as builtinPlugins } from './plugins/index.js';
|
||||||
|
import type { FastifyInstance } from 'fastify';
|
||||||
|
|
||||||
export type SummalyOptions = {
|
export type SummalyOptions = {
|
||||||
/**
|
/**
|
||||||
@ -34,6 +34,35 @@ export type SummalyOptions = {
|
|||||||
* Custom HTTP agent
|
* Custom HTTP agent
|
||||||
*/
|
*/
|
||||||
agent?: Got.Agents;
|
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 = {
|
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];
|
const match = plugins.filter(plugin => plugin.test(_url))[0];
|
||||||
|
|
||||||
// Get summary
|
// 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) {
|
if (summary == null) {
|
||||||
throw new Error('failed summarize');
|
throw new Error('failed summarize');
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.assign(summary, {
|
return Object.assign(summary, {
|
||||||
url: actualUrl
|
url: actualUrl,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -87,9 +126,10 @@ export default function (fastify: FastifyInstance, options: SummalyOptions, done
|
|||||||
};
|
};
|
||||||
}>('/', 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
|
||||||
if (url == null) {
|
if (url == null) {
|
||||||
return reply.status(400).send({
|
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;
|
return summary;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return reply.status(500).send({
|
return reply.status(500).send({
|
||||||
error: e
|
error: e,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import type { URL } from 'node:url';
|
|
||||||
import Summary from './summary.js';
|
import Summary from './summary.js';
|
||||||
|
import type { URL } from 'node:url';
|
||||||
|
import { GeneralScrapingOptions } from '@/general';
|
||||||
|
|
||||||
export interface SummalyPlugin {
|
export interface SummalyPlugin {
|
||||||
test: (url: URL) => boolean;
|
test: (url: URL) => boolean;
|
||||||
summarize: (url: URL, lang?: string) => Promise<Summary | null>;
|
summarize: (url: URL, opts?: GeneralScrapingOptions) => Promise<Summary | null>;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { scpaping } from '../utils/got.js';
|
import general, { GeneralScrapingOptions } from '../general.js';
|
||||||
import general 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 {
|
||||||
@ -9,10 +8,10 @@ export function test(url: URL): boolean {
|
|||||||
url.hostname === 'spotify.link';
|
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
|
// https://help.branch.io/using-branch/docs/creating-a-deep-link#redirections
|
||||||
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
|
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
|
||||||
url.searchParams.append('$web_only', 'true');
|
url.searchParams.append('$web_only', 'true');
|
||||||
|
|
||||||
return await general(url, lang);
|
return await general(url, opts);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { SummalyPlugin } from '@/iplugin.js';
|
|
||||||
import * as amazon from './amazon.js';
|
import * as amazon from './amazon.js';
|
||||||
import * as wikipedia from './wikipedia.js';
|
import * as wikipedia from './wikipedia.js';
|
||||||
import * as branchIoDeeplinks from './branchio-deeplinks.js';
|
import * as branchIoDeeplinks from './branchio-deeplinks.js';
|
||||||
|
import { SummalyPlugin } from '@/iplugin.js';
|
||||||
|
|
||||||
export const plugins: SummalyPlugin[] = [
|
export const plugins: SummalyPlugin[] = [
|
||||||
amazon,
|
amazon,
|
||||||
wikipedia,
|
wikipedia,
|
||||||
branchIoDeeplinks,
|
branchIoDeeplinks,
|
||||||
];
|
];
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { get } from '../utils/got.js';
|
|
||||||
import debug from 'debug';
|
import debug from 'debug';
|
||||||
|
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';
|
||||||
|
|
||||||
@ -20,6 +20,7 @@ export async function summarize(url: URL): Promise<summary> {
|
|||||||
log(`title is ${title}`);
|
log(`title is ${title}`);
|
||||||
log(`endpoint is ${endpoint}`);
|
log(`endpoint is ${endpoint}`);
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
let body = await get(endpoint) as any;
|
let body = await get(endpoint) as any;
|
||||||
body = JSON.parse(body);
|
body = JSON.parse(body);
|
||||||
log(body);
|
log(body);
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* 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 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 x = escapeRegExp(siteName);
|
||||||
|
|
||||||
const patterns = [
|
const patterns = [
|
||||||
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`
|
`^(.+?)\\s?[\\-\\|:・]\\s?${x}$`,
|
||||||
];
|
];
|
||||||
|
|
||||||
for (let i = 0; i < patterns.length; i++) {
|
for (let i = 0; i < patterns.length; i++) {
|
||||||
const pattern = new RegExp(patterns[i]);
|
const pattern = new RegExp(patterns[i]);
|
||||||
const [, match] = pattern.exec(title) || [null, null];
|
const [, match] = pattern.exec(title) ?? [null, null];
|
||||||
if (match) {
|
if (match) {
|
||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ export default function(s: string, max: number): string {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-param-reassign
|
||||||
s = s.trim();
|
s = s.trim();
|
||||||
|
|
||||||
if (s.length > max) {
|
if (s.length > max) {
|
||||||
|
@ -11,6 +11,7 @@ const regCharset = new RegExp(/charset\s*=\s*["']?([\w-]+)/, 'i');
|
|||||||
export function detectEncoding(body: Buffer): string {
|
export function detectEncoding(body: Buffer): string {
|
||||||
// By detection
|
// By detection
|
||||||
const detected = jschardet.detect(body, { minimumThreshold: 0.99 });
|
const detected = jschardet.detect(body, { minimumThreshold: 0.99 });
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
if (detected) {
|
if (detected) {
|
||||||
const candicate = detected.encoding;
|
const candicate = detected.encoding;
|
||||||
const encoding = toEncoding(candicate);
|
const encoding = toEncoding(candicate);
|
||||||
|
121
src/utils/got.ts
121
src/utils/got.ts
@ -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 { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
import { fileURLToPath } from 'node:url';
|
||||||
import { readFileSync } from 'node:fs';
|
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 _filename = fileURLToPath(import.meta.url);
|
||||||
const _dirname = dirname(_filename);
|
const _dirname = dirname(_filename);
|
||||||
|
|
||||||
export let agent: Got.Agents = {};
|
export let agent: Got.Agents = {};
|
||||||
|
|
||||||
export function setAgent(_agent: Got.Agents) {
|
export function setAgent(_agent: Got.Agents) {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||||
agent = _agent || {};
|
agent = _agent || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -21,34 +23,60 @@ export type GotOptions = {
|
|||||||
body?: string;
|
body?: string;
|
||||||
headers: Record<string, string | undefined>;
|
headers: Record<string, string | undefined>;
|
||||||
typeFilter?: RegExp;
|
typeFilter?: RegExp;
|
||||||
|
responseTimeout?: number;
|
||||||
|
operationTimeout?: number;
|
||||||
|
contentLengthLimit?: number;
|
||||||
|
contentLengthRequired?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8'));
|
const repo = JSON.parse(readFileSync(`${_dirname}/../../package.json`, 'utf8'));
|
||||||
|
|
||||||
const RESPONSE_TIMEOUT = 20 * 1000;
|
const DEFAULT_RESPONSE_TIMEOUT = 20 * 1000;
|
||||||
const OPERATION_TIMEOUT = 60 * 1000;
|
const DEFAULT_OPERATION_TIMEOUT = 60 * 1000;
|
||||||
const MAX_RESPONSE_SIZE = 10 * 1024 * 1024;
|
const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024;
|
||||||
const BOT_UA = `SummalyBot/${repo.version}`;
|
const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`;
|
||||||
|
|
||||||
export async function scpaping(url: string, opts?: { lang?: string; }) {
|
export async function scpaping(
|
||||||
const response = await getResponse({
|
url: string,
|
||||||
|
opts?: {
|
||||||
|
lang?: string;
|
||||||
|
userAgent?: string;
|
||||||
|
responseTimeout?: number;
|
||||||
|
operationTimeout?: number;
|
||||||
|
contentLengthLimit?: number;
|
||||||
|
contentLengthRequired?: boolean;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const args: Omit<GotOptions, 'method'> = {
|
||||||
url,
|
url,
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
headers: {
|
||||||
'accept': 'text/html,application/xhtml+xml',
|
'accept': 'text/html,application/xhtml+xml',
|
||||||
'user-agent': BOT_UA,
|
'user-agent': opts?.userAgent ?? DEFAULT_BOT_UA,
|
||||||
'accept-language': opts?.lang
|
'accept-language': opts?.lang,
|
||||||
},
|
},
|
||||||
typeFilter: /^(text\/html|application\/xhtml\+xml)/,
|
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はテスト用
|
// SUMMALY_ALLOW_PRIVATE_IPはテスト用
|
||||||
const allowPrivateIp = process.env.SUMMALY_ALLOW_PRIVATE_IP === 'true' || Object.keys(agent).length > 0;
|
const allowPrivateIp = process.env.SUMMALY_ALLOW_PRIVATE_IP === 'true' || Object.keys(agent).length > 0;
|
||||||
|
if (!allowPrivateIp && headResponse.ip && PrivateIp(headResponse.ip)) {
|
||||||
if (!allowPrivateIp && response.ip && PrivateIp(response.ip)) {
|
throw new StatusError(`Private IP rejected ${headResponse.ip}`, 400, 'Private IP Rejected');
|
||||||
throw new StatusError(`Private IP rejected ${response.ip}`, 400, 'Private IP Rejected');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const response = await getResponse({
|
||||||
|
...args,
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
const encoding = detectEncoding(response.rawBody);
|
const encoding = detectEncoding(response.rawBody);
|
||||||
const body = toUtf8(response.rawBody, encoding);
|
const body = toUtf8(response.rawBody, encoding);
|
||||||
const $ = cheerio.load(body);
|
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) {
|
export async function head(url: string) {
|
||||||
const res = await getResponse({
|
return await getResponse({
|
||||||
url,
|
url,
|
||||||
method: 'HEAD',
|
method: 'HEAD',
|
||||||
headers: {
|
headers: {
|
||||||
'accept': '*/*',
|
'accept': '*/*',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return await res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getResponse(args: GotOptions) {
|
async function getResponse(args: GotOptions) {
|
||||||
const timeout = RESPONSE_TIMEOUT;
|
const timeout = args.responseTimeout ?? DEFAULT_RESPONSE_TIMEOUT;
|
||||||
const operationTimeout = OPERATION_TIMEOUT;
|
const operationTimeout = args.operationTimeout ?? DEFAULT_OPERATION_TIMEOUT;
|
||||||
|
|
||||||
const req = got<string>(args.url, {
|
const req = got<string>(args.url, {
|
||||||
method: args.method,
|
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 req = args.req;
|
||||||
const maxSize = MAX_RESPONSE_SIZE;
|
const maxSize = args.opts.contentLengthLimit ?? DEFAULT_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`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// 受信中のデータでサイズチェック
|
// 受信中のデータでサイズチェック
|
||||||
req.on('downloadProgress', (progress: Got.Progress) => {
|
req.on('downloadProgress', (progress: Got.Progress) => {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
|
||||||
export default function(val: string): boolean {
|
export default function(val: string): boolean {
|
||||||
if (val === undefined) {
|
if (val === undefined) {
|
||||||
return true;
|
return true;
|
||||||
|
273
test/index.ts
273
test/index.ts
@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
import fs, { readdirSync } from 'node:fs';
|
import fs, { readdirSync } from 'node:fs';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
import fastify from 'fastify';
|
|
||||||
import { summaly } from '../src/index.js';
|
|
||||||
import { dirname } from 'node:path';
|
import { dirname } from 'node:path';
|
||||||
import { fileURLToPath } from 'node:url';
|
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 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 fastify from 'fastify';
|
||||||
|
import { summaly } from '../src/index.js';
|
||||||
import { StatusError } from '../src/utils/status-error.js';
|
import { StatusError } from '../src/utils/status-error.js';
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
const _filename = fileURLToPath(import.meta.url);
|
||||||
@ -35,7 +35,6 @@ const host = `http://localhost:${port}`;
|
|||||||
process.on('unhandledRejection', console.dir);
|
process.on('unhandledRejection', console.dir);
|
||||||
|
|
||||||
let app: ReturnType<typeof fastify> | null = null;
|
let app: ReturnType<typeof fastify> | null = null;
|
||||||
let n = 0;
|
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
if (app) {
|
if (app) {
|
||||||
@ -49,7 +48,10 @@ afterEach(async () => {
|
|||||||
test('basic', async () => {
|
test('basic', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
expect(await summaly(host)).toEqual({
|
expect(await summaly(host)).toEqual({
|
||||||
@ -61,10 +63,10 @@ test('basic', async () => {
|
|||||||
url: null,
|
url: null,
|
||||||
width: null,
|
width: null,
|
||||||
height: null,
|
height: null,
|
||||||
"allow": [
|
'allow': [
|
||||||
"autoplay",
|
'autoplay',
|
||||||
"encrypted-media",
|
'encrypted-media',
|
||||||
"fullscreen",
|
'fullscreen',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
sitename: 'localhost:3060',
|
sitename: 'localhost:3060',
|
||||||
@ -80,35 +82,38 @@ test('Stage Bye Stage', async () => {
|
|||||||
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
|
const summary = await summaly('https://www.youtube.com/watch?v=NMIEAhH_fTU');
|
||||||
expect(summary).toEqual(
|
expect(summary).toEqual(
|
||||||
{
|
{
|
||||||
"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/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...",
|
'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': {
|
||||||
"url": "https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed",
|
'url': 'https://www.youtube.com/embed/NMIEAhH_fTU?feature=oembed',
|
||||||
"width": 200,
|
'width': 200,
|
||||||
"height": 113,
|
'height': 113,
|
||||||
"allow": [
|
'allow': [
|
||||||
"autoplay",
|
'autoplay',
|
||||||
"clipboard-write",
|
'clipboard-write',
|
||||||
"encrypted-media",
|
'encrypted-media',
|
||||||
"picture-in-picture",
|
'picture-in-picture',
|
||||||
"web-share",
|
'web-share',
|
||||||
"fullscreen",
|
'fullscreen',
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
"sitename": "YouTube",
|
'sitename': 'YouTube',
|
||||||
"sensitive": false,
|
'sensitive': false,
|
||||||
"activityPub": null,
|
'activityPub': null,
|
||||||
"url": "https://www.youtube.com/watch?v=NMIEAhH_fTU"
|
'url': 'https://www.youtube.com/watch?v=NMIEAhH_fTU',
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', async () => {
|
test('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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());
|
app.get('/favicon.ico', (_, reply) => reply.status(200).send());
|
||||||
await app.listen({ port });
|
await app.listen({ port });
|
||||||
@ -120,7 +125,10 @@ test('faviconがHTML上で指定されていないが、ルートに存在する
|
|||||||
test('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', async () => {
|
test('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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());
|
app.get('*', (_, reply) => reply.status(404).send());
|
||||||
await app.listen({ port });
|
await app.listen({ port });
|
||||||
@ -132,7 +140,10 @@ test('faviconがHTML上で指定されていなくて、ルートにも存在し
|
|||||||
test('titleがcleanupされる', async () => {
|
test('titleがcleanupされる', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -145,7 +156,10 @@ describe('Private IP blocking', () => {
|
|||||||
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'false';
|
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'false';
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('*', (request, reply) => {
|
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 });
|
return app.listen({ port });
|
||||||
});
|
});
|
||||||
@ -164,7 +178,7 @@ describe('Private IP blocking', () => {
|
|||||||
agent: {
|
agent: {
|
||||||
http: new httpAgent({ keepAlive: true }),
|
http: new httpAgent({ keepAlive: true }),
|
||||||
https: new httpsAgent({ keepAlive: true }),
|
https: new httpsAgent({ keepAlive: true }),
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
expect(summary.title).toBe('Strawberry Pasta');
|
expect(summary.title).toBe('Strawberry Pasta');
|
||||||
});
|
});
|
||||||
@ -187,7 +201,10 @@ describe('OGP', () => {
|
|||||||
test('title', async () => {
|
test('title', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('*', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -198,7 +215,10 @@ describe('OGP', () => {
|
|||||||
test('description', async () => {
|
test('description', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -209,7 +229,10 @@ describe('OGP', () => {
|
|||||||
test('site_name', async () => {
|
test('site_name', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -220,7 +243,10 @@ describe('OGP', () => {
|
|||||||
test('thumbnail', async () => {
|
test('thumbnail', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -233,7 +259,10 @@ describe('TwitterCard', () => {
|
|||||||
test('title', async () => {
|
test('title', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -244,7 +273,10 @@ describe('TwitterCard', () => {
|
|||||||
test('description', async () => {
|
test('description', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -255,7 +287,10 @@ describe('TwitterCard', () => {
|
|||||||
test('thumbnail', async () => {
|
test('thumbnail', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -266,7 +301,10 @@ describe('TwitterCard', () => {
|
|||||||
test('Player detection - PeerTube:video => video', async () => {
|
test('Player detection - PeerTube:video => video', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -278,7 +316,10 @@ describe('TwitterCard', () => {
|
|||||||
test('Player detection - Pleroma:video => video', async () => {
|
test('Player detection - Pleroma:video => video', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -290,7 +331,10 @@ describe('TwitterCard', () => {
|
|||||||
test('Player detection - Pleroma:image => image', async () => {
|
test('Player detection - Pleroma:image => image', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -299,19 +343,23 @@ describe('TwitterCard', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("oEmbed", () => {
|
describe('oEmbed', () => {
|
||||||
const setUpFastify = async (oEmbedPath: string, htmlPath = 'htmls/oembed.html') => {
|
const setUpFastify = async (oEmbedPath: string, htmlPath = 'htmls/oembed.html') => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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) => {
|
app.get('/oembed.json', (request, reply) => {
|
||||||
return reply.send(fs.createReadStream(
|
const content = fs.readFileSync(new URL(oEmbedPath, new URL('oembed/', import.meta.url)));
|
||||||
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 });
|
await app.listen({ port });
|
||||||
}
|
};
|
||||||
|
|
||||||
for (const filename of readdirSync(new URL('oembed/invalid', import.meta.url))) {
|
for (const filename of readdirSync(new URL('oembed/invalid', import.meta.url))) {
|
||||||
test(`Invalidity test: ${filename}`, async () => {
|
test(`Invalidity test: ${filename}`, async () => {
|
||||||
@ -433,7 +481,10 @@ describe('ActivityPub', () => {
|
|||||||
test('Basic', async () => {
|
test('Basic', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('*', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -444,7 +495,10 @@ describe('ActivityPub', () => {
|
|||||||
test('Null', async () => {
|
test('Null', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('*', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
|
|
||||||
@ -457,7 +511,10 @@ describe('sensitive', () => {
|
|||||||
test('default', async () => {
|
test('default', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
expect((await summaly(host)).sensitive).toBe(false);
|
expect((await summaly(host)).sensitive).toBe(false);
|
||||||
@ -466,9 +523,115 @@ describe('sensitive', () => {
|
|||||||
test('mixi:content-rating 1', async () => {
|
test('mixi:content-rating 1', async () => {
|
||||||
app = fastify();
|
app = fastify();
|
||||||
app.get('/', (request, reply) => {
|
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 });
|
await app.listen({ port });
|
||||||
expect((await summaly(host)).sensitive).toBe(true);
|
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
6
test/tsconfig.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"extends": "../tsconfig.json",
|
||||||
|
"include": [
|
||||||
|
"./**/*.ts"
|
||||||
|
],
|
||||||
|
}
|
Reference in New Issue
Block a user