mirror of
https://github.com/misskey-dev/misskey.git
synced 2025-02-21 07:15:20 +09:00
use "userland" punycode
, plus tests
thanks to CenTdemeern1 for the `import` incantation
This commit is contained in:
parent
77662b0d2a
commit
7e5baed63a
@ -4,7 +4,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { URL } from 'node:url';
|
import { URL } from 'node:url';
|
||||||
import { toASCII } from 'punycode';
|
import punycode from 'punycode/';
|
||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import RE2 from 're2';
|
import RE2 from 're2';
|
||||||
import psl from 'psl';
|
import psl from 'psl';
|
||||||
@ -107,13 +107,13 @@ export class UtilityService {
|
|||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public toPuny(host: string): string {
|
public toPuny(host: string): string {
|
||||||
return toASCII(host.toLowerCase());
|
return punycode.toASCII(host.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public toPunyNullable(host: string | null | undefined): string | null {
|
public toPunyNullable(host: string | null | undefined): string | null {
|
||||||
if (host == null) return null;
|
if (host == null) return null;
|
||||||
return toASCII(host.toLowerCase());
|
return punycode.toASCII(host.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
|
39
packages/backend/test/unit/UtilityService.ts
Normal file
39
packages/backend/test/unit/UtilityService.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import * as assert from 'assert';
|
||||||
|
import { Test } from '@nestjs/testing';
|
||||||
|
|
||||||
|
import { CoreModule } from '@/core/CoreModule.js';
|
||||||
|
import { UtilityService } from '@/core/UtilityService.js';
|
||||||
|
import { GlobalModule } from '@/GlobalModule.js';
|
||||||
|
|
||||||
|
describe('UtilityService', () => {
|
||||||
|
let utilityService: UtilityService;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const app = await Test.createTestingModule({
|
||||||
|
imports: [GlobalModule, CoreModule],
|
||||||
|
}).compile();
|
||||||
|
utilityService = app.get<UtilityService>(UtilityService);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('punyHost', () => {
|
||||||
|
test('simple', () => {
|
||||||
|
assert.equal(utilityService.punyHost('http://www.foo.com'),'www.foo.com');
|
||||||
|
});
|
||||||
|
test('japanese', () => {
|
||||||
|
assert.equal(utilityService.punyHost('http://www.新聞.com'),'www.xn--efvv70d.com');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('punyHostPSLDomain', () => {
|
||||||
|
test('simple', () => {
|
||||||
|
assert.equal(utilityService.punyHostPSLDomain('http://www.foo.com'),'foo.com');
|
||||||
|
});
|
||||||
|
test('japanese', () => {
|
||||||
|
assert.equal(utilityService.punyHostPSLDomain('http://www.新聞.com'),'xn--efvv70d.com');
|
||||||
|
});
|
||||||
|
test('lower', () => {
|
||||||
|
assert.equal(utilityService.punyHostPSLDomain('http://foo.github.io'),'foo.github.io');
|
||||||
|
assert.equal(utilityService.punyHostPSLDomain('http://foo.bar.github.io'),'bar.github.io');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user