diff --git a/bin/hlt b/bin/hlt index 256e745..9fc2969 100755 --- a/bin/hlt +++ b/bin/hlt @@ -1,3 +1,3 @@ #!/usr/bin/env node -require('../client'); \ No newline at end of file +require('../dist/client'); \ No newline at end of file diff --git a/package.json b/package.json index 3a35202..521f5a8 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "@cubetiq/hlt", - "version": "0.0.9", + "version": "0.1.0", "description": "A lightweight http tunnel client using nodejs and socket.io client", - "main": "client.js", + "main": "dist/client.js", "bin": { "hlt": "bin/hlt" }, "scripts": { "start": "ts-node-dev --respawn --transpile-only src/client.ts", - "build": "rm -rf dist && tsc" + "build": "tsc" }, "repository": { "type": "git", diff --git a/src/client.ts b/src/client.ts index 900eb65..ed54795 100644 --- a/src/client.ts +++ b/src/client.ts @@ -67,7 +67,7 @@ function initClient(options: any) { release: os.release(), }; - const initParams = { + const initParams: any = { path: "/$cubetiq_http_tunnel", transports: ["websocket"], auth: { @@ -92,7 +92,7 @@ function initClient(options: any) { const clientLogPrefix = `client: ${clientId} on profile: ${profile}`; socket.on("connect", () => { - if (socket.connected) { + if (socket!.connected) { console.log(`${clientLogPrefix} is connected to server successfully!`); } }); @@ -113,7 +113,7 @@ function initClient(options: any) { socket.on("disconnect_exit", (reason) => { console.log(`${clientLogPrefix} disconnected and exited ${reason}!`); - socket.disconnect(); + socket?.disconnect(); process.exit(1); }); @@ -127,15 +127,12 @@ function initClient(options: any) { request.headers.host = options.origin; } - const tunnelRequest = new TunnelRequest({ - requestId, - socket: socket, - }); + const tunnelRequest = new TunnelRequest(socket!, requestId); const localReq = http.request(request); tunnelRequest.pipe(localReq); - const onTunnelRequestError = (e) => { + const onTunnelRequestError = (e: any) => { tunnelRequest.off("end", onTunnelRequestEnd); localReq.destroy(e); }; @@ -147,17 +144,14 @@ function initClient(options: any) { tunnelRequest.once("error", onTunnelRequestError); tunnelRequest.once("end", onTunnelRequestEnd); - const onLocalResponse = (localRes) => { + const onLocalResponse = (localRes: any) => { localReq.off("error", onLocalError); if (isWebSocket && localRes.upgrade) { return; } - const tunnelResponse = new TunnelResponse({ - responseId: requestId, - socket: socket, - }); + const tunnelResponse = new TunnelResponse(socket!, requestId); tunnelResponse.writeHead( localRes.statusCode, @@ -169,23 +163,18 @@ function initClient(options: any) { localRes.pipe(tunnelResponse); }; - const onLocalError = (error) => { + const onLocalError = (error: any) => { console.log(error); localReq.off("response", onLocalResponse); - socket.emit("request-error", requestId, error && error.message); + socket?.emit("request-error", requestId, error && error.message); tunnelRequest.destroy(error); }; - const onUpgrade = (localRes, localSocket, localHead) => { + const onUpgrade = (localRes: any, localSocket: any, localHead: any) => { // localSocket.once('error', onTunnelRequestError); if (localHead && localHead.length) localSocket.unshift(localHead); - const tunnelResponse = new TunnelResponse({ - responseId: requestId, - socket: socket, - duplex: true, - }); - + const tunnelResponse = new TunnelResponse(socket!, requestId, true); tunnelResponse.writeHead(null, null, localRes.headers); localSocket.pipe(tunnelResponse).pipe(localSocket); }; @@ -201,7 +190,7 @@ function initClient(options: any) { // reconnect manually const tryReconnect = () => { setTimeout(() => { - socket.io.open((err) => { + socket!.io.open((err) => { if (err) { tryReconnect(); } diff --git a/src/lib.ts b/src/lib.ts index a5e1c91..c91dd93 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -100,6 +100,7 @@ class TunnelResponse extends stream.Duplex { this.socket.on("response-pipe-error", onResponsePipeError); this.socket.on("response-pipe-end", onResponsePipeEnd); } + } _write(chunk: any, encoding: BufferEncoding, callback: (error?: Error | null) => void) {