com.jimuwd.xian.registry-proxy 1.0.47 → 1.0.48

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/dist/index.js +20 -20
  2. package/package.json +1 -1
  3. package/src/index.ts +19 -20
package/dist/index.js CHANGED
@@ -160,31 +160,32 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
160
160
  }
161
161
  }
162
162
  // 修改后的 writeSuccessfulResponse 函数
163
- async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, req, proxyInfo, proxyPort, registryInfos) {
164
- if (!response.ok)
165
- throw new Error("Only 2xx response is supported");
163
+ async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamResponse, req, proxyInfo, proxyPort, registryInfos) {
164
+ if (!upstreamResponse.ok)
165
+ throw new Error("Only 2xx upstream response is supported");
166
166
  try {
167
- const upstreamContentType = response.headers.get('Content-Type');
167
+ const upstreamContentType = upstreamResponse.headers.get('Content-Type');
168
+ const upstreamConnection = upstreamResponse.headers.get("connection");
168
169
  const contentType = upstreamContentType || 'application/octet-stream';
170
+ const connection = upstreamConnection || 'keep-alive';
169
171
  // 准备通用头信息
170
- const safeHeaders = {
171
- 'content-type': contentType,
172
- 'connection': 'keep-alive'
173
- };
172
+ const safeHeaders = { 'connection': connection, 'content-type': contentType, };
174
173
  // 复制所有可能需要的头信息
175
174
  const headersToCopy = [
176
- 'cache-control', 'etag', 'last-modified',
177
- 'content-encoding', 'content-length',
178
- 'transfer-encoding'
175
+ 'etag', 'last-modified',
176
+ 'cache-control',
177
+ 'content-length',
178
+ 'content-encoding',
179
+ 'transfer-encoding',
179
180
  ];
180
181
  headersToCopy.forEach(header => {
181
- const value = response.headers.get(header);
182
+ const value = upstreamResponse.headers.get(header);
182
183
  if (value)
183
184
  safeHeaders[header] = value;
184
185
  });
185
186
  if (contentType.includes('application/json')) {
186
187
  // JSON 处理逻辑
187
- const data = await response.json();
188
+ const data = await upstreamResponse.json();
188
189
  if (data.versions) {
189
190
  const host = req.headers.host || `localhost:${proxyPort}`;
190
191
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
@@ -198,23 +199,22 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, r
198
199
  }
199
200
  }
200
201
  }
201
- res.writeHead(response.status, { "content-type": contentType });
202
- res.end(JSON.stringify(data));
202
+ res.writeHead(upstreamResponse.status, safeHeaders).end(JSON.stringify(data));
203
203
  }
204
204
  else {
205
205
  // 二进制流处理
206
- if (!response.body) {
206
+ if (!upstreamResponse.body) {
207
207
  console.error(`Empty response body from ${targetUrl}`);
208
- res.writeHead(response.status, safeHeaders).end();
208
+ res.writeHead(502).end('Empty Upstream Response');
209
209
  }
210
210
  else {
211
- res.writeHead(response.status, safeHeaders).end(response.body);
212
- /*await pipeline(response.body, res).finally(() => res.end());*/
211
+ res.writeHead(upstreamResponse.status, safeHeaders);
212
+ upstreamResponse.body.pipe(res, { end: true });
213
213
  }
214
214
  }
215
215
  }
216
216
  catch (err) {
217
- console.error('Failed to write response:', err);
217
+ console.error('Failed to write upstreamResponse:', err);
218
218
  if (!res.headersSent) {
219
219
  res.writeHead(502).end('Internal Server Error');
220
220
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.47",
3
+ "version": "1.0.48",
4
4
  "type": "module",
5
5
  "description": "A lightweight npm registry proxy with fallback support",
6
6
  "main": "dist/index.js",
package/src/index.ts CHANGED
@@ -8,7 +8,6 @@ import fetch, {Response} from 'node-fetch';
8
8
  import {homedir} from 'os';
9
9
  import {join, resolve} from 'path';
10
10
  import {URL} from 'url';
11
- import {pipeline} from "node:stream/promises";
12
11
 
13
12
  const {readFile, writeFile} = fsPromises;
14
13
 
@@ -218,40 +217,41 @@ async function writeSuccessfulResponse(
218
217
  registryInfo: RegistryInfo,
219
218
  targetUrl: string,
220
219
  res: ServerResponse,
221
- response: Response,
220
+ upstreamResponse: Response,
222
221
  req: IncomingMessage,
223
222
  proxyInfo: ProxyInfo,
224
223
  proxyPort: number,
225
224
  registryInfos: RegistryInfo[]
226
225
  ): Promise<void> {
227
226
 
228
- if (!response.ok) throw new Error("Only 2xx response is supported");
227
+ if (!upstreamResponse.ok) throw new Error("Only 2xx upstream response is supported");
229
228
 
230
229
  try {
231
- const upstreamContentType = response.headers.get('Content-Type');
230
+ const upstreamContentType = upstreamResponse.headers.get('Content-Type');
231
+ const upstreamConnection = upstreamResponse.headers.get("connection");
232
232
  const contentType = upstreamContentType || 'application/octet-stream';
233
+ const connection = upstreamConnection || 'keep-alive';
233
234
 
234
235
  // 准备通用头信息
235
- const safeHeaders: OutgoingHttpHeaders = {
236
- 'content-type': contentType,
237
- 'connection': 'keep-alive'
238
- };
236
+ const safeHeaders: OutgoingHttpHeaders = {'connection': connection, 'content-type': contentType,};
239
237
 
240
238
  // 复制所有可能需要的头信息
241
239
  const headersToCopy = [
242
- 'cache-control', 'etag', 'last-modified',
243
- 'content-encoding', 'content-length',
244
- 'transfer-encoding'
240
+ 'etag', 'last-modified',
241
+ 'cache-control',
242
+ 'content-length',
243
+ 'content-encoding',
244
+ 'transfer-encoding',
245
245
  ];
246
246
 
247
247
  headersToCopy.forEach(header => {
248
- const value = response.headers.get(header);
248
+ const value = upstreamResponse.headers.get(header);
249
249
  if (value) safeHeaders[header] = value;
250
250
  });
251
251
 
252
252
  if (contentType.includes('application/json')) {
253
253
  // JSON 处理逻辑
254
- const data = await response.json() as PackageData;
254
+ const data = await upstreamResponse.json() as PackageData;
255
255
  if (data.versions) {
256
256
  const host = req.headers.host || `localhost:${proxyPort}`;
257
257
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
@@ -266,20 +266,19 @@ async function writeSuccessfulResponse(
266
266
  }
267
267
  }
268
268
  }
269
- res.writeHead(response.status, {"content-type": contentType});
270
- res.end(JSON.stringify(data));
269
+ res.writeHead(upstreamResponse.status, safeHeaders).end(JSON.stringify(data));
271
270
  } else {
272
271
  // 二进制流处理
273
- if (!response.body) {
272
+ if (!upstreamResponse.body) {
274
273
  console.error(`Empty response body from ${targetUrl}`);
275
- res.writeHead(response.status, safeHeaders).end();
274
+ res.writeHead(502).end('Empty Upstream Response');
276
275
  } else {
277
- res.writeHead(response.status, safeHeaders).end(response.body)
278
- /*await pipeline(response.body, res).finally(() => res.end());*/
276
+ res.writeHead(upstreamResponse.status, safeHeaders);
277
+ upstreamResponse.body.pipe(res, {end: true});
279
278
  }
280
279
  }
281
280
  } catch (err) {
282
- console.error('Failed to write response:', err);
281
+ console.error('Failed to write upstreamResponse:', err);
283
282
  if (!res.headersSent) {
284
283
  res.writeHead(502).end('Internal Server Error');
285
284
  }