diff --git a/lib/StartSdk.ts b/lib/StartSdk.ts index b5d97d9..dee1f03 100644 --- a/lib/StartSdk.ts +++ b/lib/StartSdk.ts @@ -18,9 +18,8 @@ import { ActionResult, BackupOptions, DeepPartial, - Address, } from "./types" -import * as regexes from "./util/regexes" +import * as patterns from "./util/patterns" import { Utils } from "./util/utils" import { DependencyConfig } from "./dependencyConfig/DependencyConfig" import { BackupSet, Backups } from "./backup/Backups" @@ -52,8 +51,6 @@ import { SetInterfaces, setupInterfaces, } from "./interfaces/setupInterfaces" -import { AddressReceipt } from "./interfaces/AddressReceipt" -import { Host } from "./interfaces/Host" // prettier-ignore type AnyNeverCond = @@ -105,7 +102,7 @@ export class StartSdk { of: healthCheck, runHealthScript, }, - regexes, + patterns, setupActions: (...createdActions: CreatedAction[]) => setupActions(...createdActions), setupBackups: (...args: SetupBackupsParams) => diff --git a/lib/util/getNetworkInterface.ts b/lib/util/getNetworkInterface.ts index df1973e..f3d3e7b 100644 --- a/lib/util/getNetworkInterface.ts +++ b/lib/util/getNetworkInterface.ts @@ -1,11 +1,4 @@ -import { - Address, - Effects, - EnsureStorePath, - HostName, - NetworkInterface, - hostName, -} from "../types" +import { Address, Effects, HostName, NetworkInterface } from "../types" import * as regexes from "./regexes" export type UrlString = string @@ -72,10 +65,10 @@ export const filledAddress = ( ...address, hostnames, get onionHostnames() { - return hostnames.filter(regexes.onionHost.test) + return hostnames.filter(regexes.torHostname.test) }, get localHostnames() { - return hostnames.filter(regexes.localHost.test) + return hostnames.filter(regexes.localHostname.test) }, get ipHostnames() { return hostnames.filter(either(regexes.ipv4.test, regexes.ipv6.test)) @@ -96,10 +89,10 @@ export const filledAddress = ( return hostnames.map(toUrl) }, get onionUrls() { - return hostnames.filter(regexes.onionHost.test).map(toUrl) + return hostnames.filter(regexes.torHostname.test).map(toUrl) }, get localUrls() { - return hostnames.filter(regexes.localHost.test).map(toUrl) + return hostnames.filter(regexes.localHostname.test).map(toUrl) }, get ipUrls() { return hostnames diff --git a/lib/util/getNetworkInterfaces.ts b/lib/util/getNetworkInterfaces.ts index 5fc4fbf..f56321a 100644 --- a/lib/util/getNetworkInterfaces.ts +++ b/lib/util/getNetworkInterfaces.ts @@ -1,5 +1,4 @@ -import { Address, Effects, EnsureStorePath, HostName, hostName } from "../types" -import * as regexes from "./regexes" +import { Effects, HostName } from "../types" import { HostId, NetworkInterfaceFilled, diff --git a/lib/util/patterns.ts b/lib/util/patterns.ts new file mode 100644 index 0000000..775fc4e --- /dev/null +++ b/lib/util/patterns.ts @@ -0,0 +1,59 @@ +import { Pattern } from "../config/configTypes" +import * as regexes from "./regexes" + +export const ipv6: Pattern = { + regex: regexes.ipv6.toString(), + description: "Must be a valid IPv6 address", +} + +export const ipv4: Pattern = { + regex: regexes.ipv4.toString(), + description: "Must be a valid IPv4 address", +} + +export const hostname = { + regex: regexes.hostname.toString(), + description: "Must be a valid hostname", +} + +export const localHostname: Pattern = { + regex: regexes.localHostname.toString(), + description: 'Must be a valid ".local" hostname', +} + +export const torHostname: Pattern = { + regex: regexes.torHostname.toString(), + description: 'Must be a valid Tor (".onion") hostname', +} + +export const url: Pattern = { + regex: regexes.url.toString(), + description: "Must be a valid URL", +} + +export const localUrl: Pattern = { + regex: regexes.localUrl.toString(), + description: 'Must be a valid ".local" URL', +} + +export const torUrl: Pattern = { + regex: regexes.torUrl.toString(), + description: 'Must be a valid Tor (".onion") URL', +} + +export const ascii: Pattern = { + regex: regexes.ascii.toString(), + description: + "May only contain ASCII characters. See https://www.w3schools.com/charsets/ref_html_ascii.asp", +} + +export const email: Pattern = { + regex: regexes.email.toString(), + description: "Must be a valid email address", +} + +export const base64: Pattern = { + regex: regexes.base64.toString(), + description: + "May only contain base64 characters. See https://base64.guru/learn/base64-characters", +} diff --git a/lib/util/regexPatterns.ts b/lib/util/regexPatterns.ts deleted file mode 100644 index 26e9eba..0000000 --- a/lib/util/regexPatterns.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Pattern } from "../config/configTypes" -import * as regexes from "./regexes" -export const ipv6: Pattern = { - regex: regexes.ipv6.toString(), - description: "", -} - -export const ipv4: Pattern = { regex: regexes.ipv4.toString(), description: "" } - -export const url: Pattern = { regex: regexes.url.toString(), description: "" } - -export const local: Pattern = { - regex: regexes.local.toString(), - description: "", -} - -export const localHost: Pattern = { - regex: regexes.localHost.toString(), - description: "", -} -export const onion: Pattern = { - regex: regexes.onion.toString(), - description: "", -} - -export const onionHost: Pattern = { - regex: regexes.onionHost.toString(), - description: "", -} -export const ascii: Pattern = { - regex: regexes.ascii.toString(), - description: "", -} -export const email: Pattern = { - regex: regexes.email.toString(), - description: "", -} -export const base64: Pattern = { - regex: regexes.base64.toString(), - description: "", -} diff --git a/lib/util/regexes.ts b/lib/util/regexes.ts index 55b5a84..3af48f8 100644 --- a/lib/util/regexes.ts +++ b/lib/util/regexes.ts @@ -6,19 +6,23 @@ export const ipv6 = export const ipv4 = /(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}/ +export const hostname = + /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/ + +export const localHostname = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local/ + +export const torHostname = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion/ + // https://ihateregex.io/expr/url/ export const url = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/ -export const local = +export const localUrl = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/ -export const localHost = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.local/ - -export const onion = +export const torUrl = /(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)/ -export const onionHost = /[-a-zA-Z0-9@:%._\+~#=]{1,256}\.onion/ // https://ihateregex.io/expr/ascii/ export const ascii = /^[ -~]*$/