@tramvai/module-http-proxy-agent 1.85.0 → 1.90.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  import type { AgentOptions } from 'https';
3
3
  import type { Socket } from 'net';
4
4
  import type { LOGGER_TOKEN } from '@tramvai/tokens-common';
5
+ import type { METRICS_MODULE_TOKEN } from '@tramvai/tokens-metrics';
5
6
  declare type Proxy = {
6
7
  hostname: string;
7
8
  port: number;
@@ -28,7 +29,8 @@ export interface ConnectOptions {
28
29
  /**
29
30
  * Fork of https://github.com/mknj/node-keepalive-proxy-agent with monkeypatching and no_proxy support
30
31
  */
31
- export declare const addProxyToHttpsAgent: ({ logger }: {
32
+ export declare const addProxyToHttpsAgent: ({ logger, metrics, }: {
32
33
  logger: ReturnType<typeof LOGGER_TOKEN>;
34
+ metrics: typeof METRICS_MODULE_TOKEN;
33
35
  }) => void;
34
36
  export {};
package/lib/server.es.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { __decorate } from 'tslib';
2
2
  import { Module, commandLineListTokens } from '@tramvai/core';
3
3
  import { LOGGER_TOKEN } from '@tramvai/tokens-common';
4
+ import { METRICS_MODULE_TOKEN } from '@tramvai/tokens-metrics';
4
5
  import https from 'https';
5
6
  import net from 'net';
6
7
  import url from 'url';
@@ -52,10 +53,15 @@ const matchNoProxy = ({ noProxy, hostname, }) => {
52
53
  /**
53
54
  * Fork of https://github.com/mknj/node-keepalive-proxy-agent with monkeypatching and no_proxy support
54
55
  */
55
- const addProxyToHttpsAgent = ({ logger }) => {
56
+ const addProxyToHttpsAgent = ({ logger, metrics, }) => {
56
57
  const httpsProxyEnv = getHttpsProxy();
57
58
  const noProxyEnv = getNoProxy();
58
59
  const noProxyMatchResults = {};
60
+ const metricsConnectionCounter = metrics.counter({
61
+ name: 'http_proxy_connect_total',
62
+ help: 'Number of proxy connects',
63
+ labelNames: ['host'],
64
+ });
59
65
  if (!httpsProxyEnv) {
60
66
  return;
61
67
  }
@@ -89,11 +95,16 @@ const addProxyToHttpsAgent = ({ logger }) => {
89
95
  }
90
96
  function createConnectionHttpsAfterHttp(options, cb) {
91
97
  const proxySocket = net.connect(+proxy.port, proxy.hostname);
98
+ const host = options.hostname || options.host;
92
99
  const errorListener = (error) => {
93
100
  proxySocket.destroy();
94
101
  cb(error);
95
102
  };
103
+ const successConnectionListener = () => {
104
+ metricsConnectionCounter.inc({ host });
105
+ };
96
106
  proxySocket.once('error', errorListener);
107
+ proxySocket.on('connect', successConnectionListener);
97
108
  let response = '';
98
109
  const dataListener = (data) => {
99
110
  response += data.toString();
@@ -117,10 +128,6 @@ const addProxyToHttpsAgent = ({ logger }) => {
117
128
  cb(null, originalCreateConnection.call(this, options));
118
129
  };
119
130
  proxySocket.on('data', dataListener);
120
- let host = options.hostname;
121
- if (!host) {
122
- host = options.host;
123
- }
124
131
  // https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.6
125
132
  let cmd = `CONNECT ${host}:${options.port} HTTP/1.1\r\n`;
126
133
  cmd += `Host: ${host}:${options.port}\r\n`;
@@ -148,17 +155,18 @@ HttpProxyAgentModule = __decorate([
148
155
  httpProxyEnabled() && {
149
156
  provide: commandLineListTokens.init,
150
157
  multi: true,
151
- useFactory: ({ loggerFactory }) => function addHttpsProxy() {
158
+ useFactory: ({ loggerFactory, metrics }) => function addHttpsProxy() {
152
159
  const logger = loggerFactory('http-proxy-agent');
153
160
  logger.debug({
154
161
  event: 'proxy agent enabled',
155
162
  proxyEnv: getHttpsProxy(),
156
163
  noProxyEnv: getNoProxy(),
157
164
  });
158
- addProxyToHttpsAgent({ logger });
165
+ addProxyToHttpsAgent({ logger, metrics });
159
166
  },
160
167
  deps: {
161
168
  loggerFactory: LOGGER_TOKEN,
169
+ metrics: METRICS_MODULE_TOKEN,
162
170
  },
163
171
  },
164
172
  ].filter(Boolean),
package/lib/server.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var tslib = require('tslib');
6
6
  var core = require('@tramvai/core');
7
7
  var tokensCommon = require('@tramvai/tokens-common');
8
+ var tokensMetrics = require('@tramvai/tokens-metrics');
8
9
  var https = require('https');
9
10
  var net = require('net');
10
11
  var url = require('url');
@@ -62,10 +63,15 @@ const matchNoProxy = ({ noProxy, hostname, }) => {
62
63
  /**
63
64
  * Fork of https://github.com/mknj/node-keepalive-proxy-agent with monkeypatching and no_proxy support
64
65
  */
65
- const addProxyToHttpsAgent = ({ logger }) => {
66
+ const addProxyToHttpsAgent = ({ logger, metrics, }) => {
66
67
  const httpsProxyEnv = getHttpsProxy();
67
68
  const noProxyEnv = getNoProxy();
68
69
  const noProxyMatchResults = {};
70
+ const metricsConnectionCounter = metrics.counter({
71
+ name: 'http_proxy_connect_total',
72
+ help: 'Number of proxy connects',
73
+ labelNames: ['host'],
74
+ });
69
75
  if (!httpsProxyEnv) {
70
76
  return;
71
77
  }
@@ -99,11 +105,16 @@ const addProxyToHttpsAgent = ({ logger }) => {
99
105
  }
100
106
  function createConnectionHttpsAfterHttp(options, cb) {
101
107
  const proxySocket = net__default["default"].connect(+proxy.port, proxy.hostname);
108
+ const host = options.hostname || options.host;
102
109
  const errorListener = (error) => {
103
110
  proxySocket.destroy();
104
111
  cb(error);
105
112
  };
113
+ const successConnectionListener = () => {
114
+ metricsConnectionCounter.inc({ host });
115
+ };
106
116
  proxySocket.once('error', errorListener);
117
+ proxySocket.on('connect', successConnectionListener);
107
118
  let response = '';
108
119
  const dataListener = (data) => {
109
120
  response += data.toString();
@@ -127,10 +138,6 @@ const addProxyToHttpsAgent = ({ logger }) => {
127
138
  cb(null, originalCreateConnection.call(this, options));
128
139
  };
129
140
  proxySocket.on('data', dataListener);
130
- let host = options.hostname;
131
- if (!host) {
132
- host = options.host;
133
- }
134
141
  // https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.6
135
142
  let cmd = `CONNECT ${host}:${options.port} HTTP/1.1\r\n`;
136
143
  cmd += `Host: ${host}:${options.port}\r\n`;
@@ -158,17 +165,18 @@ exports.HttpProxyAgentModule = tslib.__decorate([
158
165
  httpProxyEnabled() && {
159
166
  provide: core.commandLineListTokens.init,
160
167
  multi: true,
161
- useFactory: ({ loggerFactory }) => function addHttpsProxy() {
168
+ useFactory: ({ loggerFactory, metrics }) => function addHttpsProxy() {
162
169
  const logger = loggerFactory('http-proxy-agent');
163
170
  logger.debug({
164
171
  event: 'proxy agent enabled',
165
172
  proxyEnv: getHttpsProxy(),
166
173
  noProxyEnv: getNoProxy(),
167
174
  });
168
- addProxyToHttpsAgent({ logger });
175
+ addProxyToHttpsAgent({ logger, metrics });
169
176
  },
170
177
  deps: {
171
178
  loggerFactory: tokensCommon.LOGGER_TOKEN,
179
+ metrics: tokensMetrics.METRICS_MODULE_TOKEN,
172
180
  },
173
181
  },
174
182
  ].filter(Boolean),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tramvai/module-http-proxy-agent",
3
- "version": "1.85.0",
3
+ "version": "1.90.2",
4
4
  "description": "Enable support for http_proxy, https_proxy and no_proxy env variables",
5
5
  "browser": "lib/browser.js",
6
6
  "main": "lib/server.js",
@@ -24,12 +24,13 @@
24
24
  "registry": "https://registry.npmjs.org/"
25
25
  },
26
26
  "dependencies": {
27
- "@tramvai/tokens-common": "1.85.0"
27
+ "@tramvai/tokens-common": "1.90.2"
28
28
  },
29
29
  "devDependencies": {},
30
30
  "peerDependencies": {
31
- "@tramvai/core": "1.85.0",
31
+ "@tramvai/core": "1.90.2",
32
32
  "@tinkoff/dippy": "0.7.39",
33
+ "@tramvai/tokens-metrics": "1.90.2",
33
34
  "tslib": "^2.0.3"
34
35
  }
35
36
  }