parent
4a82c62958
commit
367efb4b53
4
lib/ipreg.ts
Normal file
4
lib/ipreg.ts
Normal file
@ -0,0 +1,4 @@
|
||||
"use strict";
|
||||
// License: MIT
|
||||
|
||||
export const IPReg = /^(?:(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$|^(?:(?:(?:[0-9a-fA-F]{1,4}):){7}(?:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){6}(?:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(?:[0-9a-fA-F]{1,4})|:)|(?:(?:[0-9a-fA-F]{1,4}):){5}(?::((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,2}|:)|(?:(?:[0-9a-fA-F]{1,4}):){4}(?:(:(?:[0-9a-fA-F]{1,4})){0,1}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,3}|:)|(?:(?:[0-9a-fA-F]{1,4}):){3}(?:(:(?:[0-9a-fA-F]{1,4})){0,2}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,4}|:)|(?:(?:[0-9a-fA-F]{1,4}):){2}(?:(:(?:[0-9a-fA-F]{1,4})){0,3}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,5}|:)|(?:(?:[0-9a-fA-F]{1,4}):){1}(?:(:(?:[0-9a-fA-F]{1,4})){0,4}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(?:[0-9a-fA-F]{1,4})){1,6}|:)|(?::((?::(?:[0-9a-fA-F]{1,4})){0,5}:((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(?::(?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z]{1,})?$/;
|
@ -3,6 +3,7 @@
|
||||
|
||||
import * as psl from "psl";
|
||||
import { identity, memoize } from "./memoize";
|
||||
import { IPReg } from "./ipreg";
|
||||
export { debounce } from "../uikit/lib/util";
|
||||
|
||||
export class Promised {
|
||||
@ -237,7 +238,10 @@ export interface URLd extends URL {
|
||||
Object.defineProperty(URL.prototype, "domain", {
|
||||
get() {
|
||||
try {
|
||||
return hostToDomain(this.host) || this.host;
|
||||
const {hostname} = this;
|
||||
return IPReg.test(hostname) ?
|
||||
hostname :
|
||||
hostToDomain(hostname) || hostname;
|
||||
}
|
||||
catch (ex) {
|
||||
console.error(ex);
|
||||
|
43
tests/test_urld.js
Normal file
43
tests/test_urld.js
Normal file
@ -0,0 +1,43 @@
|
||||
/* eslint-env node */
|
||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||
"use strict";
|
||||
// License: CC0 1.0
|
||||
|
||||
require("../lib/util");
|
||||
|
||||
describe("URLd", function() {
|
||||
it("basic domain", function() {
|
||||
let u = new URL("https://www.google.de");
|
||||
expect(u.domain).to.equal("google.de");
|
||||
u = new URL("https://www.google.de:8443");
|
||||
expect(u.domain).to.equal("google.de");
|
||||
});
|
||||
|
||||
it("plain basic domain", function() {
|
||||
const u = new URL("https://google.de");
|
||||
expect(u.domain).to.equal("google.de");
|
||||
});
|
||||
|
||||
it("special domain", function() {
|
||||
let u = new URL("https://www.google.co.uk");
|
||||
expect(u.domain).to.equal("google.co.uk");
|
||||
u = new URL("https://google.co.uk");
|
||||
expect(u.domain).to.equal("google.co.uk");
|
||||
u = new URL("https://www.google.co.uk:8443");
|
||||
expect(u.domain).to.equal("google.co.uk");
|
||||
});
|
||||
|
||||
it("ipv4", function() {
|
||||
let u = new URL("https://127.0.0.1:8443");
|
||||
expect(u.domain).to.equal("127.0.0.1");
|
||||
u = new URL("https://0.0.0.0:8443");
|
||||
expect(u.domain).to.equal("0.0.0.0");
|
||||
});
|
||||
|
||||
it("ipv6", function() {
|
||||
let u = new URL("https://[::1]:8443");
|
||||
expect(u.domain).to.equal("[::1]");
|
||||
u = new URL("https://[2a00:1450:4005:800::2003]:8443");
|
||||
expect(u.domain).to.equal("[2a00:1450:4005:800::2003]");
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user