refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え (#11123)

* refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え

* fixup! refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え
This commit is contained in:
okayurisotto 2023-07-06 08:47:47 +09:00 committed by GitHub
parent be143f91b2
commit 9959f5bd04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 20 deletions

View File

@ -1,5 +1,4 @@
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common'; import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import escapeRegexp from 'escape-regexp';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js'; import type { NotesRepository, UserPublickeysRepository, UsersRepository } from '@/models/index.js';
import type { Config } from '@/config.js'; import type { Config } from '@/config.js';
@ -56,25 +55,18 @@ export class ApDbResolverService implements OnApplicationShutdown {
@bindThis @bindThis
public parseUri(value: string | IObject): UriParseResult { public parseUri(value: string | IObject): UriParseResult {
const uri = getApId(value); const separator = '/';
// the host part of a URL is case insensitive, so use the 'i' flag. const uri = new URL(getApId(value));
const localRegex = new RegExp('^' + escapeRegexp(this.config.url) + '/(\\w+)/(\\w+)(?:\/(.+))?', 'i'); if (uri.origin !== this.config.url) return { local: false, uri: uri.href };
const matchLocal = uri.match(localRegex);
const [, type, id, ...rest] = uri.pathname.split(separator);
if (matchLocal) { return {
return { local: true,
local: true, type,
type: matchLocal[1], id,
id: matchLocal[2], rest: rest.length === 0 ? undefined : rest.join(separator),
rest: matchLocal[3], };
};
} else {
return {
local: false,
uri,
};
}
} }
/** /**