mirror of
				https://codeberg.org/yeentown/barkey.git
				synced 2025-11-04 07:24:13 +00:00 
			
		
		
		
	refactor(ApDbResolverService.ts): URLを扱う複雑な正規表現をURLインターフェイスで置き換え (#11123)
				
					
				
			* refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え * fixup! refactor(`ApDbResolverService.ts`): URLを扱う複雑な正規表現をURLインターフェイスで置き換え
This commit is contained in:
		
							parent
							
								
									be143f91b2
								
							
						
					
					
						commit
						9959f5bd04
					
				
					 1 changed files with 12 additions and 20 deletions
				
			
		| 
						 | 
					@ -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,
 | 
					 | 
				
			||||||
			};
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue