Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle ETIMEDOUT error; add missing typings #8

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion easyTunnel.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {EventEmitter} from 'events';

declare interface Tunnel extends EventEmitter {
export declare interface Tunnel extends EventEmitter {
url: string;
clientId: string;
close(): void;
}

declare interface BootstrapOpts {
Expand Down
8 changes: 7 additions & 1 deletion easyTunnel.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,24 @@ describe('localtunnel', () => {
remoteSocket.close();
});

it('handle --connect-timeout on initial request', async () => {
it('handle --connect-timeout on initial request (ECONNREFUSED)', async () => {
const tunnel = easyTunnel({ port: fakePort, host: 'http://8.8.8.8', connect_timeout: 2000 });
await assert.rejects(tunnel, { message: 'timeout of 2000ms exceeded' });
});

it('handle --connect-timeout on initial request (ETIMEDOUT)', async () => {
const tunnel = easyTunnel({ port: fakePort, host: 'http://172.30.68.144', connect_timeout: 2000 });
await assert.rejects(tunnel, { message: 'timeout of 2000ms exceeded' });
});

it('handle --connect-timeout on socket connect', async () => {
const maxSockets = 1;
nock(fakeHost).get('/?new').reply(200, {
id: 'test',
port: tunnelPort,
max_conn_count: maxSockets,
is_tunnel_secure: false,
remote_ip: '8.8.8.8',
url: 'https://test.localhost',
});

Expand Down
4 changes: 3 additions & 1 deletion lib/Tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = class Tunnel extends EventEmitter {
if (!this.opts.host) {
this.opts.host = 'http://localhost:8087';
}
this.opts.connect_timeout = this.opts.connect_timeout ?? 10_000;
this.destroyTimer = null;
}

Expand Down Expand Up @@ -54,7 +55,7 @@ module.exports = class Tunnel extends EventEmitter {
is_tunnel_secure,
local_max_retries,
local_reconnect_delay,
connect_timeout: connect_timeout ?? 10_000,
connect_timeout: connect_timeout,
idle_timeout: idle_timeout ?? 15_000,
};
/* eslint-enable camelcase */
Expand Down Expand Up @@ -224,5 +225,6 @@ module.exports = class Tunnel extends EventEmitter {

destroy() {
this.emit('error', new Error('Tunnel timed out'));
this.close();
}
};
7 changes: 3 additions & 4 deletions lib/TunnelCluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ module.exports = class TunnelCluster extends EventEmitter {
const localProtocol = opt.local_https ? 'https' : 'http';
const allowInvalidCert = opt.allow_invalid_cert;
const isTunnelSecure = opt.is_tunnel_secure;
const localReconnectionMaxRetryCount =
opt.local_max_retries !== undefined ? opt.local_max_retries : Infinity;
const localReconnectionMaxRetryCount = opt.local_max_retries !== undefined ? opt.local_max_retries : Infinity;
const localReconnectionDelay = opt.local_reconnect_delay !== undefined ? opt.local_reconnect_delay : 1000;

debug(
Expand Down Expand Up @@ -64,7 +63,7 @@ module.exports = class TunnelCluster extends EventEmitter {

// emit connection refused errors immediately, because they
// indicate that the tunnel can't be established.
if (err.code === 'ECONNREFUSED') {
if (['ECONNREFUSED', 'ETIMEDOUT'].includes(err.code)) {
this.emit(
'error',
new Error(`connection refused: ${remoteHostOrIp}:${remotePort} (check your firewall settings)`)
Expand Down Expand Up @@ -140,7 +139,7 @@ module.exports = class TunnelCluster extends EventEmitter {

pump(stream, local, remote, err => {
debug('stream finished', err);
this.emit('dead');
this.emit('dead', { idleMonitoring });
});

// when local closes, also get a new remote
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"et": "bin/et.js"
},
"scripts": {
"dev:client": "nodemon --inspect ./bin/et.js --port 3001 --host http://lvh.me:8087 --subdomain test --open --idle-timeout=6000000",
"dev:client": "nodemon --inspect ./bin/et.js --port 3001 --host http://lvh.me:8087 --subdomain test --open",
"dev:backend": "nodemon server.js",
"dev": "DEBUG=mytunnel:* concurrently --raw npm:dev:backend npm:dev:client",
"test": "mocha --timeout 15000 --exit -- *.spec.js"
Expand Down
Loading