From b7277cf88531c7b4fc14e42272d987e4495a1b5f Mon Sep 17 00:00:00 2001 From: Ben Allfree Date: Sat, 29 Jun 2024 17:10:11 -0700 Subject: [PATCH] enh(multi): Add public instance URL calculators and refactor HTTP protocol detection --- .changeset/1719706211493.md | 6 ++++++ packages/plugin-launcher-spawn/src/plugin.ts | 12 +++++------- packages/pockethost/src/constants.ts | 8 +++++--- 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 .changeset/1719706211493.md diff --git a/.changeset/1719706211493.md b/.changeset/1719706211493.md new file mode 100644 index 00000000..4a95401a --- /dev/null +++ b/.changeset/1719706211493.md @@ -0,0 +1,6 @@ +--- +'@pockethost/plugin-launcher-spawn': minor +'pockethost': minor +--- + +Add public instance URL calculators and refactor HTTP protocol detection \ No newline at end of file diff --git a/packages/plugin-launcher-spawn/src/plugin.ts b/packages/plugin-launcher-spawn/src/plugin.ts index 5ba5d6fb..5f64be63 100644 --- a/packages/plugin-launcher-spawn/src/plugin.ts +++ b/packages/plugin-launcher-spawn/src/plugin.ts @@ -21,10 +21,9 @@ import { } from 'pockethost' import { APEX_DOMAIN, - INSTANCE_DATA_DIR, + HTTP_PROTOCOL, PORT, - exitHook, - tryFetch, + PUBLIC_INSTANCE_URL, } from 'pockethost/core' import { PLUGIN_NAME, settings } from './constants' import { DbService } from './db' @@ -92,15 +91,14 @@ export const plugin: PocketHostPlugin = async ({}) => { /** Display some informational alerts to help the user get started. */ onAfterServerStartAction(async () => { - const protocol = PORT() === 443 ? 'https' : 'http' + const protocol = HTTP_PROTOCOL() { - const url = new URL(`${protocol}://*.${APEX_DOMAIN()}`) + const url = new URL(`${protocol}//*.${APEX_DOMAIN()}`) url.port = `${PORT() === 80 || PORT() == 443 ? '' : PORT()}` info(`Listening for requests on ${url}`) } { - const url = new URL(`${protocol}://hello.${APEX_DOMAIN()}`) - url.port = `${PORT() === 80 || PORT() == 443 ? '' : PORT()}` + const url = PUBLIC_INSTANCE_URL({ subdomain: 'hello' }) info(`Try visiting ${url}`) } }) diff --git a/packages/pockethost/src/constants.ts b/packages/pockethost/src/constants.ts index 302398a7..ed9cc6d0 100644 --- a/packages/pockethost/src/constants.ts +++ b/packages/pockethost/src/constants.ts @@ -57,7 +57,9 @@ export const DATA_DIR = (...paths: string[]) => export const NODE_ENV = () => process.env.NODE_ENV export const INSTANCE_DATA_DIR = (id: InstanceId, ...paths: string[]) => join(DATA_DIR(), id, ...paths) - -if (DEBUG()) { - logSettings(settings) +export const HTTP_PROTOCOL = () => (PORT() === 443 ? 'https:' : 'http:') +export const PUBLIC_INSTANCE_URL = ({ subdomain }: Partial) => { + const url = new URL(`${HTTP_PROTOCOL()}//${subdomain}.${APEX_DOMAIN()}`) + url.port = `${PORT() === 80 || PORT() == 443 ? '' : PORT()}` + return url }