Fixed and updated for client and lib models and package version and compile for typescript in http tunnel client

This commit is contained in:
Sambo Chea 2022-07-10 20:50:50 +07:00
parent 1af63d5955
commit 4412b6edd6
4 changed files with 17 additions and 27 deletions

View File

@ -1,3 +1,3 @@
#!/usr/bin/env node
require('../client');
require('../dist/client');

View File

@ -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",

View File

@ -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();
}

View File

@ -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) {