From bcbe33cd22de865687b8b095fc483d9667d189cc Mon Sep 17 00:00:00 2001 From: Waldemar Hummer Date: Thu, 21 Sep 2023 12:27:24 +0200 Subject: [PATCH] add utility to inject hostname into localhost URLs for node18 compat --- README.md | 2 +- package-lock.json | 4 ++-- src/index.js | 23 +++++++++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 36fe113..d5e2e06 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ custom: ## Change Log -* v1.1.2: Unify construction of target endpoint URL, add support for configuring `$AWS_ENDPOINT_URL` +* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL` * v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying * v1.1.0: Fix SSM environment variables resolving issues with serverless v3, change default for `BUCKET_MARKER_LOCAL` to `hot-reload` * v1.0.6: Add `BUCKET_MARKER_LOCAL` configuration for customizing S3 bucket for lambda mount and [Hot Reloading](https://docs.localstack.cloud/user-guide/tools/lambda-tools/hot-reloading/). diff --git a/package-lock.json b/package-lock.json index d142d76..b5c04ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "serverless-localstack", - "version": "1.1.1", + "version": "1.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "serverless-localstack", - "version": "1.1.1", + "version": "1.1.2", "license": "MIT", "dependencies": { "adm-zip": "^0.5.10", diff --git a/src/index.js b/src/index.js index f44bd9f..446d8e2 100644 --- a/src/index.js +++ b/src/index.js @@ -759,19 +759,34 @@ class LocalstackPlugin { getServiceURL(hostname) { if (process.env.AWS_ENDPOINT_URL) { - return process.env.AWS_ENDPOINT_URL; + return injectHostnameIntoLocalhostURL(process.env.AWS_ENDPOINT_URL, hostname); } hostname = hostname || 'localhost'; const proto = TRUE_VALUES.includes(process.env.USE_SSL) ? 'https' : 'http'; const port = this.getEdgePort(); - if (proto === 'https' && `${port}` === '443') { - // little hack here - required to remove the default HTTPS port 443, as otherwise - // routing for some platforms and ephemeral instances (e.g., on namespace.so) fails + // little hack here - required to remove the default HTTPS port 443, as otherwise + // routing for some platforms and ephemeral instances (e.g., on namespace.so) fails + const isDefaultPort = + (proto === 'http' && `${port}` === '80') || + (proto === 'https' && `${port}` === '443'); + if (isDefaultPort) { return `${proto}://${hostname}`; } return `${proto}://${hostname}:${port}`; } + /** + * If the given `endpointURL` points to `localhost`, then inject the given `hostname` into the URL + * and return it. This helps fix IPv6 issues with node v18+ (see also getConnectHostname() above) + */ + injectHostnameIntoLocalhostURL(endpointURL, hostname) { + const url = new URL(endpointURL); + if (hostname && url.hostname === 'localhost') { + url.hostname = hostname; + } + return url.href; + } + log(msg) { if (this.serverless.cli) { this.serverless.cli.log.call(this.serverless.cli, msg);