codebuff 1.0.641 → 1.0.643

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.
Files changed (3) hide show
  1. package/http.js +176 -0
  2. package/index.js +6 -119
  3. package/package.json +2 -1
package/http.js ADDED
@@ -0,0 +1,176 @@
1
+ const http = require('http')
2
+ const https = require('https')
3
+ const tls = require('tls')
4
+
5
+ function createReleaseHttpClient({
6
+ env = process.env,
7
+ userAgent,
8
+ requestTimeout,
9
+ httpModule = http,
10
+ httpsModule = https,
11
+ tlsModule = tls,
12
+ }) {
13
+ function getProxyUrl() {
14
+ return (
15
+ env.HTTPS_PROXY ||
16
+ env.https_proxy ||
17
+ env.HTTP_PROXY ||
18
+ env.http_proxy ||
19
+ null
20
+ )
21
+ }
22
+
23
+ function shouldBypassProxy(hostname) {
24
+ const noProxy = env.NO_PROXY || env.no_proxy || ''
25
+ if (!noProxy) return false
26
+
27
+ const domains = noProxy
28
+ .split(',')
29
+ .map((domain) => domain.trim().toLowerCase().replace(/:\d+$/, ''))
30
+ const host = hostname.toLowerCase()
31
+
32
+ return domains.some((domain) => {
33
+ if (domain === '*') return true
34
+ if (domain.startsWith('.')) {
35
+ return host.endsWith(domain) || host === domain.slice(1)
36
+ }
37
+ return host === domain || host.endsWith(`.${domain}`)
38
+ })
39
+ }
40
+
41
+ function connectThroughProxy(proxyUrl, targetHost, targetPort) {
42
+ return new Promise((resolve, reject) => {
43
+ const proxy = new URL(proxyUrl)
44
+ const isHttpsProxy = proxy.protocol === 'https:'
45
+ const connectOptions = {
46
+ hostname: proxy.hostname,
47
+ port: proxy.port || (isHttpsProxy ? 443 : 80),
48
+ method: 'CONNECT',
49
+ path: `${targetHost}:${targetPort}`,
50
+ headers: {
51
+ Host: `${targetHost}:${targetPort}`,
52
+ },
53
+ }
54
+
55
+ if (proxy.username || proxy.password) {
56
+ const auth = Buffer.from(
57
+ `${decodeURIComponent(proxy.username || '')}:${decodeURIComponent(
58
+ proxy.password || '',
59
+ )}`,
60
+ ).toString('base64')
61
+ connectOptions.headers['Proxy-Authorization'] = `Basic ${auth}`
62
+ }
63
+
64
+ const transport = isHttpsProxy ? httpsModule : httpModule
65
+ const req = transport.request(connectOptions)
66
+
67
+ req.on('connect', (res, socket) => {
68
+ if (res.statusCode === 200) {
69
+ resolve(socket)
70
+ return
71
+ }
72
+
73
+ socket.destroy()
74
+ reject(new Error(`Proxy CONNECT failed with status ${res.statusCode}`))
75
+ })
76
+
77
+ req.on('error', (error) => {
78
+ reject(new Error(`Proxy connection failed: ${error.message}`))
79
+ })
80
+
81
+ req.setTimeout(requestTimeout, () => {
82
+ req.destroy()
83
+ reject(new Error('Proxy connection timeout.'))
84
+ })
85
+
86
+ req.end()
87
+ })
88
+ }
89
+
90
+ async function buildRequestOptions(url, options = {}) {
91
+ const parsedUrl = new URL(url)
92
+ const reqOptions = {
93
+ hostname: parsedUrl.hostname,
94
+ port: parsedUrl.port || 443,
95
+ path: parsedUrl.pathname + parsedUrl.search,
96
+ headers: {
97
+ 'User-Agent': userAgent,
98
+ ...options.headers,
99
+ },
100
+ }
101
+
102
+ const proxyUrl = getProxyUrl()
103
+ if (!proxyUrl || shouldBypassProxy(parsedUrl.hostname)) {
104
+ return reqOptions
105
+ }
106
+
107
+ const tunnelSocket = await connectThroughProxy(
108
+ proxyUrl,
109
+ parsedUrl.hostname,
110
+ parsedUrl.port || 443,
111
+ )
112
+
113
+ class TunnelAgent extends httpsModule.Agent {
114
+ createConnection(_options, callback) {
115
+ const secureSocket = tlsModule.connect({
116
+ socket: tunnelSocket,
117
+ servername: parsedUrl.hostname,
118
+ })
119
+
120
+ if (typeof callback === 'function') {
121
+ if (typeof secureSocket.once === 'function') {
122
+ let settled = false
123
+ const finish = (error) => {
124
+ if (settled) return
125
+ settled = true
126
+ callback(error || null, error ? undefined : secureSocket)
127
+ }
128
+
129
+ secureSocket.once('secureConnect', () => finish(null))
130
+ secureSocket.once('error', (error) => finish(error))
131
+ } else {
132
+ callback(null, secureSocket)
133
+ }
134
+ }
135
+
136
+ return secureSocket
137
+ }
138
+ }
139
+
140
+ reqOptions.agent = new TunnelAgent({ keepAlive: false })
141
+ return reqOptions
142
+ }
143
+
144
+ async function httpGet(url, options = {}) {
145
+ const reqOptions = await buildRequestOptions(url, options)
146
+
147
+ return new Promise((resolve, reject) => {
148
+ const req = httpsModule.get(reqOptions, (res) => {
149
+ if (res.statusCode === 301 || res.statusCode === 302) {
150
+ res.resume()
151
+ httpGet(new URL(res.headers.location, url).href, options)
152
+ .then(resolve)
153
+ .catch(reject)
154
+ return
155
+ }
156
+
157
+ resolve(res)
158
+ })
159
+
160
+ req.on('error', reject)
161
+ req.setTimeout(options.timeout || requestTimeout, () => {
162
+ req.destroy()
163
+ reject(new Error('Request timeout.'))
164
+ })
165
+ })
166
+ }
167
+
168
+ return {
169
+ getProxyUrl,
170
+ httpGet,
171
+ }
172
+ }
173
+
174
+ module.exports = {
175
+ createReleaseHttpClient,
176
+ }
package/index.js CHANGED
@@ -6,10 +6,10 @@ const http = require('http')
6
6
  const https = require('https')
7
7
  const os = require('os')
8
8
  const path = require('path')
9
- const tls = require('tls')
10
9
  const zlib = require('zlib')
11
10
 
12
11
  const tar = require('tar')
12
+ const { createReleaseHttpClient } = require('./http')
13
13
 
14
14
  const packageName = 'codebuff'
15
15
 
@@ -66,6 +66,11 @@ function createConfig(packageName) {
66
66
  }
67
67
 
68
68
  const CONFIG = createConfig(packageName)
69
+ const { getProxyUrl, httpGet } = createReleaseHttpClient({
70
+ env: process.env,
71
+ userAgent: CONFIG.userAgent,
72
+ requestTimeout: CONFIG.requestTimeout,
73
+ })
69
74
 
70
75
  function getPostHogConfig() {
71
76
  const apiKey =
@@ -130,76 +135,6 @@ function trackUpdateFailed(errorMessage, version, context = {}) {
130
135
  }
131
136
  }
132
137
 
133
- function getProxyUrl() {
134
- return (
135
- process.env.HTTPS_PROXY ||
136
- process.env.https_proxy ||
137
- process.env.HTTP_PROXY ||
138
- process.env.http_proxy ||
139
- null
140
- )
141
- }
142
-
143
- function shouldBypassProxy(hostname) {
144
- const noProxy = process.env.NO_PROXY || process.env.no_proxy || ''
145
- if (!noProxy) return false
146
- const domains = noProxy.split(',').map((d) => d.trim().toLowerCase().replace(/:\d+$/, ''))
147
- const host = hostname.toLowerCase()
148
- return domains.some((d) => {
149
- if (d === '*') return true
150
- if (d.startsWith('.')) return host.endsWith(d) || host === d.slice(1)
151
- return host === d || host.endsWith('.' + d)
152
- })
153
- }
154
-
155
- function connectThroughProxy(proxyUrl, targetHost, targetPort) {
156
- return new Promise((resolve, reject) => {
157
- const proxy = new URL(proxyUrl)
158
- const isHttpsProxy = proxy.protocol === 'https:'
159
- const connectOptions = {
160
- hostname: proxy.hostname,
161
- port: proxy.port || (isHttpsProxy ? 443 : 80),
162
- method: 'CONNECT',
163
- path: `${targetHost}:${targetPort}`,
164
- headers: {
165
- Host: `${targetHost}:${targetPort}`,
166
- },
167
- }
168
-
169
- if (proxy.username || proxy.password) {
170
- const auth = Buffer.from(
171
- `${decodeURIComponent(proxy.username || '')}:${decodeURIComponent(proxy.password || '')}`,
172
- ).toString('base64')
173
- connectOptions.headers['Proxy-Authorization'] = `Basic ${auth}`
174
- }
175
-
176
- const transport = isHttpsProxy ? https : http
177
- const req = transport.request(connectOptions)
178
-
179
- req.on('connect', (res, socket) => {
180
- if (res.statusCode === 200) {
181
- resolve(socket)
182
- } else {
183
- socket.destroy()
184
- reject(
185
- new Error(`Proxy CONNECT failed with status ${res.statusCode}`),
186
- )
187
- }
188
- })
189
-
190
- req.on('error', (err) => {
191
- reject(new Error(`Proxy connection failed: ${err.message}`))
192
- })
193
-
194
- req.setTimeout(CONFIG.requestTimeout, () => {
195
- req.destroy()
196
- reject(new Error('Proxy connection timeout.'))
197
- })
198
-
199
- req.end()
200
- })
201
- }
202
-
203
138
  const PLATFORM_TARGETS = {
204
139
  'linux-x64': `${packageName}-linux-x64.tar.gz`,
205
140
  'linux-arm64': `${packageName}-linux-arm64.tar.gz`,
@@ -224,54 +159,6 @@ const term = {
224
159
  },
225
160
  }
226
161
 
227
- async function httpGet(url, options = {}) {
228
- const parsedUrl = new URL(url)
229
- const proxyUrl = getProxyUrl()
230
-
231
- const reqOptions = {
232
- hostname: parsedUrl.hostname,
233
- path: parsedUrl.pathname + parsedUrl.search,
234
- headers: {
235
- 'User-Agent': CONFIG.userAgent,
236
- ...options.headers,
237
- },
238
- }
239
-
240
- if (proxyUrl && !shouldBypassProxy(parsedUrl.hostname)) {
241
- const tunnelSocket = await connectThroughProxy(
242
- proxyUrl,
243
- parsedUrl.hostname,
244
- parsedUrl.port || 443,
245
- )
246
- reqOptions.agent = false
247
- reqOptions.createConnection = () =>
248
- tls.connect({
249
- socket: tunnelSocket,
250
- servername: parsedUrl.hostname,
251
- })
252
- }
253
-
254
- return new Promise((resolve, reject) => {
255
- const req = https.get(reqOptions, (res) => {
256
- if (res.statusCode === 302 || res.statusCode === 301) {
257
- res.resume()
258
- return httpGet(new URL(res.headers.location, url).href, options)
259
- .then(resolve)
260
- .catch(reject)
261
- }
262
- resolve(res)
263
- })
264
-
265
- req.on('error', reject)
266
-
267
- const timeout = options.timeout || CONFIG.requestTimeout
268
- req.setTimeout(timeout, () => {
269
- req.destroy()
270
- reject(new Error('Request timeout.'))
271
- })
272
- })
273
- }
274
-
275
162
  async function getLatestVersion() {
276
163
  try {
277
164
  const res = await httpGet(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codebuff",
3
- "version": "1.0.641",
3
+ "version": "1.0.643",
4
4
  "description": "AI coding agent",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -13,6 +13,7 @@
13
13
  },
14
14
  "files": [
15
15
  "index.js",
16
+ "http.js",
16
17
  "postinstall.js",
17
18
  "README.md"
18
19
  ],