Skip to content

Commit becde9c

Browse files
authored
fix nodejs 17 localhost connection error with ipv4 fallback (#80)
1 parent 3b19c99 commit becde9c

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

.github/workflows/integrations.go.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ jobs:
2323
strategy:
2424
matrix:
2525
include:
26-
- { language: go, node-version: '16.x', go-version: '1.16.15', region: us-east-1}
26+
# The AWS CDK only supports third-party languages "until its EOL (End Of Life) shared by the vendor or community"
27+
# https://github.com/aws/aws-cdk
28+
# Golang EOL overview: https://endoflife.date/go
2729
- { language: go, node-version: '16.x', go-version: '1.17.8', region: us-east-1}
2830
- { language: go, node-version: '16.x', go-version: '1.18', region: us-east-1}
31+
- { language: go, node-version: '18.x', go-version: '1.20', region: us-east-1}
2932

3033
env:
3134
AWS_REGION: ${{ matrix.region }}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ $ awslocal sns list-topics
6767

6868
## Change Log
6969

70+
* 2.17.0: Fix IPv4 fallback check to prevent IPv6 connection issue with `localhost` on macOS
7071
* 2.16.0: Add check to prevent IPv6 connection issue with `localhost` on MacOS
7172
* 2.15.0: Fix issue with undefined BUCKET_NAME_OUTPUT variable; add CI build and eslint config
7273
* 2.14.0: Add switches in patches to accommodate new esbuild packaging mechanism in CDK v2.14.0+

bin/cdklocal

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,53 @@ const getLocalHost = async () => {
3333
}
3434

3535
var hostname = process.env.LOCALSTACK_HOSTNAME || DEFAULT_HOSTNAME;
36-
// attempt to connect to the given host/port
37-
const socket = new net.Socket();
38-
try {
39-
await socket.connect({ host: hostname, port });
40-
} catch (e) {
41-
if (hostname === "localhost") {
42-
// fall back to using local IPv4 address - to fix IPv6 issue on MacOS
43-
// see, https://github.com/localstack/serverless-localstack/issues/125
44-
// and https://github.com/localstack/aws-cdk-local/issues/78
36+
// Fall back to using local IPv4 address if connection to localhost fails.
37+
// This workaround transparently handles systems (e.g., macOS) where
38+
// localhost resolves to IPv6 when using Nodejs >=v17. See discussion:
39+
// https://github.com/localstack/aws-cdk-local/issues/76#issuecomment-1412590519
40+
// Issue: https://github.com/localstack/aws-cdk-local/issues/78
41+
if (hostname === "localhost") {
42+
try {
43+
const options = { host: hostname, port: port };
44+
await checkTCPConnection(options);
45+
} catch (e) {
4546
hostname = "127.0.0.1";
4647
}
47-
} finally {
48-
socket.destroy();
4948
}
5049

5150
resolvedHostname = hostname;
5251
return `${hostname}:${port}`;
5352
};
5453

54+
/**
55+
* Checks whether a TCP connection to the given "options" can be established.
56+
* @param {object} options connection options of net.socket.connect()
57+
* https://nodejs.org/api/net.html#socketconnectoptions-connectlistener
58+
* Example: { host: "localhost", port: 4566 }
59+
* @returns {Promise} A fulfilled empty promise on successful connection and
60+
* a rejected promise on any connection error.
61+
*/
62+
const checkTCPConnection = async (options) => {
63+
return new Promise((resolve, reject) => {
64+
const socket = new net.Socket();
65+
const client = socket.connect(options, () => {
66+
client.end();
67+
resolve();
68+
});
69+
70+
client.setTimeout(500); // milliseconds
71+
client.on("timeout", err => {
72+
client.destroy();
73+
reject(err);
74+
});
75+
76+
client.on("error", err => {
77+
client.destroy();
78+
reject(err);
79+
});
80+
});
81+
}
82+
5583
const useLocal = () => {
5684
// TODO make configurable..?
5785
return true;

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "aws-cdk-local",
33
"description": "CDK Toolkit for use with LocalStack",
4-
"version": "2.16.0",
4+
"version": "2.17.0",
55
"bin": {
66
"cdklocal": "bin/cdklocal"
77
},

0 commit comments

Comments
 (0)