formatting changes
| @@ -9,7 +9,6 @@ import path from 'node:path'; | ||
| 9 | 9 | import util from 'node:util'; |
| 10 | 10 | import net from 'node:net'; |
| 11 | 11 | import dns from 'node:dns'; |
| 12 | -import { promises as dnsPromise } from 'node:dns'; | |
| 13 | 12 | import process from 'node:process'; |
| 14 | 13 | import { fileURLToPath } from 'node:url'; |
| 15 | 14 | |
| @@ -71,6 +70,7 @@ import { | ||
| 71 | 70 | getSeparator, |
| 72 | 71 | stringToBool, |
| 73 | 72 | urlHostnameToIPv6, |
| 73 | + canResolve, | |
| 74 | 74 | } from './src/util.js'; |
| 75 | 75 | import { UPLOADS_DIRECTORY } from './src/constants.js'; |
| 76 | 76 | import { ensureThumbnailCache } from './src/endpoints/thumbnails.js'; |
| @@ -383,37 +383,6 @@ function getSessionCookieAge() { | ||
| 383 | 383 | return undefined; |
| 384 | 384 | } |
| 385 | 385 | |
| 386 | - | |
| 387 | -async function canResolve(name, useIPv6 = true, useIPv4 = true) { | |
| 388 | - try { | |
| 389 | - let v6Resolved = false; | |
| 390 | - let v4Resolved = false; | |
| 391 | - | |
| 392 | - if (useIPv6) { | |
| 393 | - try { | |
| 394 | - await dnsPromise.resolve6(name); | |
| 395 | - v6Resolved = true; | |
| 396 | - } catch (error) { | |
| 397 | - v6Resolved = false; | |
| 398 | - } | |
| 399 | - } | |
| 400 | - | |
| 401 | - if (useIPv4) { | |
| 402 | - try { | |
| 403 | - await dnsPromise.resolve(name); | |
| 404 | - v4Resolved = true; | |
| 405 | - } catch (error) { | |
| 406 | - v4Resolved = false; | |
| 407 | - } | |
| 408 | - } | |
| 409 | - | |
| 410 | - return v6Resolved || v4Resolved; | |
| 411 | - | |
| 412 | - } catch (error) { | |
| 413 | - return false; | |
| 414 | - } | |
| 415 | -} | |
| 416 | - | |
| 417 | 386 | async function getHasIP() { |
| 418 | 387 | let hasIPv6 = false; |
| 419 | 388 | let hasIPv6Local = false; |
| @@ -794,6 +763,8 @@ async function getAutorunHostname(useIPv6, useIPv4) { | ||
| 794 | 763 | * Tasks that need to be run after the server starts listening. |
| 795 | 764 | * @param {boolean} v6Failed If the server failed to start on IPv6 |
| 796 | 765 | * @param {boolean} v4Failed If the server failed to start on IPv4 |
| 766 | + * @param {boolean} useIPv6 If the server is using IPv6 | |
| 767 | + * @param {boolean} useIPv4 If the server is using IPv4 | |
| 797 | 768 | */ |
| 798 | 769 | const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { |
| 799 | 770 | const autorunUrl = new URL( |
| @@ -5,6 +5,7 @@ import process from 'node:process'; | ||
| 5 | 5 | import { Readable } from 'node:stream'; |
| 6 | 6 | import { createRequire } from 'node:module'; |
| 7 | 7 | import { Buffer } from 'node:buffer'; |
| 8 | +import { promises as dnsPromise } from 'node:dns'; | |
| 8 | 9 | |
| 9 | 10 | import yaml from 'yaml'; |
| 10 | 11 | import { sync as commandExistsSync } from 'command-exists'; |
| @@ -692,6 +693,10 @@ export function isValidUrl(url) { | ||
| 692 | 693 | } |
| 693 | 694 | } |
| 694 | 695 | |
| 696 | +/** | |
| 697 | + * removes starting `[` or ending `]` from hostname. | |
| 698 | + * @param {string} hostname hostname to use | |
| 699 | + */ | |
| 695 | 700 | export function urlHostnameToIPv6(hostname) { |
| 696 | 701 | if (hostname.startsWith('[')) { |
| 697 | 702 | hostname = hostname.slice(1); |
| @@ -702,6 +707,47 @@ export function urlHostnameToIPv6(hostname) { | ||
| 702 | 707 | return hostname; |
| 703 | 708 | } |
| 704 | 709 | |
| 710 | +/** | |
| 711 | + * Test if can resolve a dns name. | |
| 712 | + * @param {string} name Domain name to use | |
| 713 | + * @param {boolean} useIPv6 If use IPv6 | |
| 714 | + * @param {boolean} useIPv4 If use IPv4 | |
| 715 | + */ | |
| 716 | +export async function canResolve(name, useIPv6 = true, useIPv4 = true) { | |
| 717 | + try { | |
| 718 | + let v6Resolved = false; | |
| 719 | + let v4Resolved = false; | |
| 720 | + | |
| 721 | + if (useIPv6) { | |
| 722 | + try { | |
| 723 | + await dnsPromise.resolve6(name); | |
| 724 | + v6Resolved = true; | |
| 725 | + } catch (error) { | |
| 726 | + v6Resolved = false; | |
| 727 | + } | |
| 728 | + } | |
| 729 | + | |
| 730 | + if (useIPv4) { | |
| 731 | + try { | |
| 732 | + await dnsPromise.resolve(name); | |
| 733 | + v4Resolved = true; | |
| 734 | + } catch (error) { | |
| 735 | + v4Resolved = false; | |
| 736 | + } | |
| 737 | + } | |
| 738 | + | |
| 739 | + return v6Resolved || v4Resolved; | |
| 740 | + | |
| 741 | + } catch (error) { | |
| 742 | + return false; | |
| 743 | + } | |
| 744 | +} | |
| 745 | + | |
| 746 | + | |
| 747 | +/** | |
| 748 | + * converts string to boolean accepts 'true' or 'false' else it returns the string put in | |
| 749 | + * @param {string} str Input string | |
| 750 | + */ | |
| 705 | 751 | export function stringToBool(str) { |
| 706 | 752 | if (str === 'true') return true; |
| 707 | 753 | if (str === 'false') return false; |