1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-04 22:25:50 +02:00

perf(backend): use node-html-parser instead of microformats-parser (#16907)

* perf(backend): use node-html-parser instead of microformats-parser

microformats-parser は内部的に parse5 に依存していて無駄

* Update OAuth2ProviderService.ts

* Add 'id' parameter to parseMicroformats function

* Update OAuth2ProviderService.ts

* Update OAuth2ProviderService.ts
This commit is contained in:
syuilo
2025-11-30 18:45:56 +09:00
committed by GitHub
parent dfd479bec5
commit 5e2a6021ae
3 changed files with 83 additions and 146 deletions

View File

@@ -17,7 +17,6 @@ import pug from 'pug';
import bodyParser from 'body-parser';
import fastifyExpress from '@fastify/express';
import { verifyChallenge } from 'pkce-challenge';
import { mf2 } from 'microformats-parser';
import { permissions as kinds } from 'misskey-js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
@@ -98,6 +97,32 @@ interface ClientInformation {
logo: string | null;
}
function parseMicroformats(doc: htmlParser.HTMLElement, baseUrl: string, id: string): { name: string | null; logo: string | null; } {
let name: string | null = null;
let logo: string | null = null;
const hApp = doc.querySelector('.h-app');
if (hApp == null) return { name, logo };
const nameEl = hApp.querySelector('.p-name');
if (nameEl != null) {
const href = nameEl.attributes.href || nameEl.attributes.src;
if (href != null && new URL(href, baseUrl).toString() === new URL(id).toString()) {
name = nameEl.textContent.trim();
}
}
const logoEl = hApp.querySelector('.u-logo');
if (logoEl != null) {
const href = logoEl.attributes.href || logoEl.attributes.src;
if (href != null) {
logo = new URL(href, baseUrl).toString();
}
}
return { name, logo };
}
// https://indieauth.spec.indieweb.org/#client-information-discovery
// "Authorization servers SHOULD support parsing the [h-app] Microformat from the client_id,
// and if there is an [h-app] with a url property matching the client_id URL,
@@ -120,24 +145,19 @@ async function discoverClientInformation(logger: Logger, httpRequestService: Htt
}
const text = await res.text();
const fragment = htmlParser.parse(`<div>${text}</div>`);
const doc = htmlParser.parse(`<div>${text}</div>`);
redirectUris.push(...[...fragment.querySelectorAll('link[rel=redirect_uri][href]')].map(el => el.attributes.href));
redirectUris.push(...[...doc.querySelectorAll('link[rel=redirect_uri][href]')].map(el => el.attributes.href));
let name = id;
let logo: string | null = null;
if (text) {
const microformats = mf2(text, { baseUrl: res.url });
const correspondingProperties = microformats.items.find(item => item.type?.includes('h-app') && item.properties.url.includes(id));
if (correspondingProperties) {
const nameProperty = correspondingProperties.properties.name?.[0];
if (typeof nameProperty === 'string') {
name = nameProperty;
}
const logoProperty = correspondingProperties.properties.logo?.[0];
if (typeof logoProperty === 'string') {
logo = logoProperty;
}
const microformats = parseMicroformats(doc, res.url, id);
if (typeof microformats.name === 'string') {
name = microformats.name;
}
if (typeof microformats.logo === 'string') {
logo = microformats.logo;
}
}