This commit is contained in:
tamaina
2023-02-12 11:11:20 +00:00
parent 16d2b7fd1f
commit 548bf14846
7 changed files with 5764 additions and 771 deletions

24
.swcrc Normal file
View File

@ -0,0 +1,24 @@
{
"$schema": "https://json.schemastore.org/swcrc",
"jsc": {
"parser": {
"syntax": "typescript",
"dynamicImport": true,
"decorators": true
},
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true
},
"experimental": {
"keepImportAssertions": true
},
"baseUrl": ".",
"paths": {},
"target": "es2021"
},
"module": {
"type": "es6"
},
"minify": true
}

208
jest.config.js Normal file
View File

@ -0,0 +1,208 @@
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/en/configuration.html
*/
export default {
// All imported modules in your tests should be mocked automatically
// automock: false,
// Stop running tests after `n` failures
// bail: 0,
// The directory where Jest should store its cached dependency information
// cacheDirectory: "C:\\Users\\ai\\AppData\\Local\\Temp\\jest",
// Automatically clear mock calls and instances between every test
// clearMocks: false,
// Indicates whether the coverage information should be collected while executing the test
// collectCoverage: false,
// An array of glob patterns indicating a set of files for which coverage information should be collected
collectCoverageFrom: ['src/**/*.ts'],
// The directory where Jest should output its coverage files
coverageDirectory: "coverage",
// An array of regexp pattern strings used to skip coverage collection
// coveragePathIgnorePatterns: [
// "\\\\node_modules\\\\"
// ],
// Indicates which provider should be used to instrument code for coverage
coverageProvider: "v8",
// A list of reporter names that Jest uses when writing coverage reports
// coverageReporters: [
// "json",
// "text",
// "lcov",
// "clover"
// ],
// An object that configures minimum threshold enforcement for coverage results
// coverageThreshold: undefined,
// A path to a custom dependency extractor
// dependencyExtractor: undefined,
// Make calling deprecated APIs throw helpful error messages
// errorOnDeprecated: false,
// Force coverage collection from ignored files using an array of glob patterns
// forceCoverageMatch: [],
// A path to a module which exports an async function that is triggered once before all test suites
// globalSetup: undefined,
// A path to a module which exports an async function that is triggered once after all test suites
// globalTeardown: undefined,
// A set of global variables that need to be available in all test environments
globals: {
},
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "json",
// "jsx",
// "ts",
// "tsx",
// "node"
// ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: {
// Do not resolve .wasm.js to .wasm by the rule below
'^(.+)\\.wasm\\.js$': '$1.wasm.js',
// SWC converts @/foo/bar.js to `../../src/foo/bar.js`, and then this rule
// converts it again to `../../src/foo/bar` which then can be resolved to
// `.ts` files.
// See https://github.com/swc-project/jest/issues/64#issuecomment-1029753225
// TODO: Use `--allowImportingTsExtensions` on TypeScript 5.0 so that we can
// directly import `.ts` files without this hack.
'^(\\.{1,2}/.*)\\.js$': '$1',
},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
// Activates notifications for test results
// notify: false,
// An enum that specifies notification mode. Requires { notify: true }
// notifyMode: "failure-change",
// A preset that is used as a base for Jest's configuration
//preset: "ts-jest/presets/js-with-ts-esm",
// Run tests from one or more projects
// projects: undefined,
// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
// Automatically reset mock state between every test
// resetMocks: false,
// Reset the module registry before running each individual test
// resetModules: false,
// A path to a custom resolver
// resolver: './jest-resolver.cjs',
// Automatically restore mock state between every test
restoreMocks: true,
// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
// A list of paths to directories that Jest should use to search for files in
roots: [
"<rootDir>"
],
// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
// snapshotSerializers: [],
// The test environment that will be used for testing
testEnvironment: "node",
// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
// Adds a location field to test results
// testLocationInResults: false,
// The glob patterns Jest uses to detect test files
testMatch: [
"<rootDir>/test/index.ts",
],
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "\\\\node_modules\\\\"
// ],
// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
// This option allows use of a custom test runner
// testRunner: "jasmine2",
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
// testURL: "http://localhost",
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
// timers: "real",
// A map from regular expressions to paths to transformers
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
// transformIgnorePatterns: [
// "\\\\node_modules\\\\",
// "\\.pnp\\.[^\\\\]+$"
// ],
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
// unmockedModulePathPatterns: undefined,
// Indicates whether each individual test should be reported during the run
// verbose: undefined,
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
// watchPathIgnorePatterns: [],
// Whether to use watchman for file crawling
// watchman: true,
extensionsToTreatAsEsm: ['.ts'],
};

5849
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -15,24 +15,26 @@
], ],
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"test": "mocha --harmony", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --silent=false --verbose false",
"start": "fastify start ./built/index.js" "start": "fastify start ./built/index.js"
}, },
"optionalDependencies": { "optionalDependencies": {
"fastify": "3.24.1" "fastify": "3.24.1"
}, },
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.4.2",
"@swc/core": "^1.3.35",
"@swc/jest": "^0.2.24",
"@types/cheerio": "0.22.18", "@types/cheerio": "0.22.18",
"@types/debug": "4.1.7", "@types/debug": "4.1.7",
"@types/escape-regexp": "^0.0.1", "@types/escape-regexp": "^0.0.1",
"@types/html-entities": "1.3.4", "@types/html-entities": "1.3.4",
"@types/mocha": "9.0.0",
"@types/node": "16.11.12", "@types/node": "16.11.12",
"debug": "^4.3.4", "debug": "^4.3.4",
"express": "^4.18.2", "express": "^4.18.2",
"fastify": "^4.13.0", "fastify": "^4.13.0",
"fastify-cli": "^5.7.1", "fastify-cli": "^5.7.1",
"mocha": "^9.2.2", "jest": "^29.4.2",
"typescript": "4.5.3" "typescript": "4.5.3"
}, },
"dependencies": { "dependencies": {

View File

@ -59,7 +59,7 @@ export const summaly = async (url: string, options?: Options): Promise<Result> =
const plugins = builtinPlugins.concat(opts.plugins || []); const plugins = builtinPlugins.concat(opts.plugins || []);
const actualUrl = opts.followRedirects ? await tracer(url).catch(() => url) : url; const actualUrl = /*opts.followRedirects ? await tracer(url).catch(() => url) :*/ url;
const _url = URL.parse(actualUrl, true); const _url = URL.parse(actualUrl, true);
@ -78,7 +78,7 @@ export const summaly = async (url: string, options?: Options): Promise<Result> =
}); });
}; };
export default function (fastify: FastifyInstance, options: {}, done: (err?: Error) => void) { export default function (fastify: FastifyInstance, options: Options, done: (err?: Error) => void) {
fastify.get<{ fastify.get<{
Querystring: { Querystring: {
url?: string; url?: string;
@ -96,6 +96,7 @@ export default function (fastify: FastifyInstance, options: {}, done: (err?: Err
const summary = await summaly(url, { const summary = await summaly(url, {
lang: req.query.lang as string, lang: req.query.lang as string,
followRedirects: false, followRedirects: false,
...options,
}); });
return summary; return summary;

View File

@ -1,216 +0,0 @@
/**
* Tests!
*/
'use strict';
/* dependencies below */
import * as assert from 'assert';
import express from 'express';
import { summaly } from '../built/index.js';
/* settings below */
Error.stackTraceLimit = Infinity;
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'true';
// Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir);
const port = 3000;
const host = `http://localhost:${port}`;
let server;
afterEach(() => {
server.close();
});
/* tests below */
it('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/no-favicon.html');
});
app.get('/favicon.ico', (_, res) => res.sendStatus(200));
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.icon, `${host}/favicon.ico`);
done();
});
});
it('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/no-favicon.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.icon, null);
done();
});
});
it('titleがcleanupされる', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/dirty-title.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.title, 'Strawberry Pasta');
done();
});
});
describe('Private IP blocking', () => {
before(() => {
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'false';
});
it('private ipなサーバーの情報を取得できない', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/og-title.html');
});
server = app.listen(port, async () => {
await assert.rejects(async () => await summaly(host));
done();
});
});
after(() => {
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'true';
});
});
describe('OGP', () => {
it('title', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/og-title.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.title, 'Strawberry Pasta');
done();
});
});
it('description', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/og-description.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.description, 'Strawberry Pasta');
done();
});
});
it('site_name', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/og-site_name.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.sitename, 'Strawberry Pasta');
done();
});
});
it('thumbnail', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/og-image.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.thumbnail, 'https://himasaku.net/himasaku.png');
done();
});
});
});
describe('TwitterCard', () => {
it('title', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/twitter-title.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.title, 'Strawberry Pasta');
done();
});
});
it('description', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/twitter-description.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.description, 'Strawberry Pasta');
done();
});
});
it('thumbnail', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/twitter-image.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.thumbnail, 'https://himasaku.net/himasaku.png');
done();
});
});
it('Player detection - PeerTube:video => video', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/player-peertube-video.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.player.url, 'https://example.com/embedurl');
done();
});
});
it('Player detection - Pleroma:video => video', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/player-pleroma-video.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.player.url, 'https://example.com/embedurl');
done();
});
});
it('Player detection - Pleroma:image => image', done => {
const app = express();
app.get('/', (req, res) => {
res.sendFile(__dirname + '/htmls/player-pleroma-image.html');
});
server = app.listen(port, async () => {
const summary = await summaly(host);
assert.equal(summary.player.url, null);
done();
});
});
});

225
test/index.ts Normal file
View File

@ -0,0 +1,225 @@
/**
* Tests!
*/
'use strict';
/* dependencies below */
import * as fs from 'fs';
import fastify from 'fastify';
import { summaly } from '../built/index.js';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import {expect, jest, test, describe, beforeEach, afterEach} from '@jest/globals';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
/* settings below */
Error.stackTraceLimit = Infinity;
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'true';
const port = 3060;
const host = `http://localhost:${port}`;
// Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir);
let app: ReturnType<typeof fastify>;
afterEach(() => {
if (app) app.close();
});
/* tests below */
test('faviconがHTML上で指定されていないが、ルートに存在する場合、正しく設定される', async () => {
console.log('AAAAAAAAAAAAAAAAAAAAAAAA')
app = fastify({
logger: true,
});
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/no-favicon.html'));
});
app.get('/favicon.ico', (_, reply) => reply.status(200));
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.icon).toBe(`${host}/favicon.ico`);
});
/*
test('faviconがHTML上で指定されていなくて、ルートにも存在しなかった場合 null になる', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/no-favicon.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.icon).toBe(null);
});
test('titleがcleanupされる', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/ditry-title.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.title).toBe('Strawberry Pasta');
});
describe('Private IP blocking', () => {
beforeEach(() => {
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'false';
});
test('private ipなサーバーの情報を取得できない', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html'));
});
await app.listen({ port });
await app.ready();
expect(summaly(host)).toThrow();
});
afterEach(() => {
process.env.SUMMALY_ALLOW_PRIVATE_IP = 'true';
});
});
describe('OGP', () => {
test('title', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-title.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.title).toBe('Strawberry Pasta');
});
test('description', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-description.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.description).toBe('Strawberry Pasta');
});
test('site_name', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-site_name.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.sitename).toBe('Strawberry Pasta');
});
test('thumbnail', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/og-image.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.thumbnail).toBe('https://himasaku.net/himasaku.png');
});
});
describe('TwitterCard', () => {
test('title', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-title.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.title).toBe('Strawberry Pasta');
});
test('description', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-description.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.description).toBe('Strawberry Pasta');
});
test('thumbnail', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/twitter-image.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.thumbnail).toBe('https://himasaku.net/himasaku.png');
});
test('Player detection - PeerTube:video => video', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-peertube-video.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/embedurl');
});
test('Player detection - Pleroma:video => video', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-video.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/embedurl');
});
test('Player detection - Pleroma:image => image', async () => {
app = fastify();
app.get('/', (request, reply) => {
return reply.send(fs.createReadStream(_dirname + '/htmls/player-pleroma-image.html'));
});
await app.listen({ port });
await app.ready();
const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/embedurl');
});
});
*/