diff --git a/.changeset/quiet-melons-chew.md b/.changeset/quiet-melons-chew.md new file mode 100644 index 0000000..fc1a84b --- /dev/null +++ b/.changeset/quiet-melons-chew.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/https-proxy-socket': patch +--- + +Fixed socket timeout and use mongo driver existing socket diff --git a/.changeset/vast-memes-repair.md b/.changeset/vast-memes-repair.md new file mode 100644 index 0000000..0d3cfdc --- /dev/null +++ b/.changeset/vast-memes-repair.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/https-proxy-socket': patch +--- + +Mongo hack fix, multi socket closure diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2a5b662..00e16f6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -45,15 +45,15 @@ jobs: - name: Build run: pnpm build -# - name: Create Release Pull Request or Publish to npm -# id: changesets -# uses: changesets/action@v1 -# if: ${{ github.event_name == 'push' }} -# with: -# version: pnpm ci:version -# publish: pnpm ci:publish -# env: -# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Create Release Pull Request or Publish to npm + id: changesets + uses: changesets/action@v1 + if: ${{ github.event_name == 'push' }} + with: + version: pnpm ci:version + publish: pnpm ci:publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Dev publish if: ${{ github.event_name == 'workflow_dispatch' }} diff --git a/src/HttpsProxySocket.ts b/src/HttpsProxySocket.ts index 0590a23..735ddc5 100644 --- a/src/HttpsProxySocket.ts +++ b/src/HttpsProxySocket.ts @@ -87,7 +87,7 @@ export class HttpsProxySocket { let buffersLength = 0; function read() { - var b = socket.read(); + const b = socket.read(); if (b) { ondata(b); } else { diff --git a/src/mongoPatch.ts b/src/mongoPatch.ts index c647cf0..bbaa5cb 100644 --- a/src/mongoPatch.ts +++ b/src/mongoPatch.ts @@ -8,22 +8,41 @@ interface Config { /** The journey apps cc egress proxy domain */ proxy: string; } + /** * The patch should be called before instantiating the MongoClient * @param config - The configuration for the proxy */ export function useProxyForMongo(config: Config) { - let socket: tls.TLSSocket; + let sockets: tls.TLSSocket[] = []; socks.SocksClient.createConnection = async (options, callback) => { - const proxy = new HttpsProxySocket(`https://${config.proxy}`, { auth: config.auth }); - return new Promise(async (resolve, reject) => { - socket = await proxy.connect({ host: options.destination.host, port: options.destination.port }); - resolve({ - socket, - }); + const socket = await new HttpsProxySocket({ socket: options.existing_socket }, { auth: config.auth }).connect({ + host: options.destination.host, + port: options.destination.port, + }); + + socket.on('timeout', () => { + console.error('Socket timeout'); }); + sockets.push(socket); + return { + socket, + }; }; return { - close: () => socket?.end(), + close: async () => { + await Promise.all( + sockets.map( + (socket) => + new Promise((resolve) => { + socket.once('close', () => { + resolve(); + }); + socket.destroySoon(); + }), + ), + ); + sockets = []; + }, }; }