Skip to content

Commit

Permalink
add utility to inject hostname into localhost URLs for node18 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
whummer committed Sep 21, 2023
1 parent f5dd9d1 commit bcbe33c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check failure on line 762 in src/index.js

View workflow job for this annotation

GitHub Actions / Serverless LocalStack CI (3)

'injectHostnameIntoLocalhostURL' is not defined
}
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);
Expand Down

0 comments on commit bcbe33c

Please sign in to comment.