com.jimuwd.xian.registry-proxy 1.0.60 → 1.0.61

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 (2) hide show
  1. package/dist/index.js +26 -12
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -161,7 +161,7 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
161
161
  }
162
162
  }
163
163
  // 修改后的 writeSuccessfulResponse 函数
164
- async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamResponse, req, proxyInfo, proxyPort, registryInfos) {
164
+ async function writeSuccessfulResponse(registryInfo, targetUrl, resToDownstream, upstreamResponse, downstreamReq, proxyInfo, proxyPort, registryInfos) {
165
165
  if (!upstreamResponse.ok)
166
166
  throw new Error("Only 2xx upstream response is supported");
167
167
  try {
@@ -184,7 +184,7 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamRes
184
184
  // JSON 处理逻辑
185
185
  const data = await upstreamResponse.json();
186
186
  if (data.versions) {
187
- const host = req.headers.host || `localhost:${proxyPort}`;
187
+ const host = downstreamReq.headers.host || `localhost:${proxyPort}`;
188
188
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
189
189
  for (const versionKey in data.versions) {
190
190
  const packageVersion = data.versions[versionKey];
@@ -196,32 +196,46 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamRes
196
196
  }
197
197
  }
198
198
  }
199
- res.writeHead(upstreamResponse.status, { "content-type": contentType }).end(JSON.stringify(data));
199
+ resToDownstream.writeHead(upstreamResponse.status, { "content-type": contentType }).end(JSON.stringify(data));
200
200
  }
201
201
  else {
202
202
  // 二进制流处理
203
203
  if (!upstreamResponse.body) {
204
204
  logger.error(`Empty response body from ${targetUrl}`);
205
- res.writeHead(502).end('Empty Upstream Response');
205
+ resToDownstream.writeHead(502).end('Empty Upstream Response');
206
206
  }
207
207
  else {
208
208
  // write back to client
209
- res.writeHead(upstreamResponse.status, safeHeaders);
209
+ resToDownstream.writeHead(upstreamResponse.status, safeHeaders);
210
210
  // stop transfer if client is closed accidentally.
211
- // req.on('close', () => upstreamResponse.body?.unpipe());
211
+ const cleanup = () => {
212
+ downstreamReq.off('close', cleanup);
213
+ logger.warn("Stop transfer when client is closed accidentally.");
214
+ upstreamResponse.body?.unpipe();
215
+ };
216
+ downstreamReq.on('close', cleanup);
212
217
  // write back body data (chunked probably)
218
+ // pipe upstream body to client
219
+ upstreamResponse.body.pipe(resToDownstream, { end: true });
213
220
  upstreamResponse.body
214
- .on('data', (chunk) => res.write(chunk))
215
- .on('end', () => res.end())
216
- .on('close', () => res.destroy(new Error(`Upstream server ${registryInfo.normalizedRegistryUrl} closed connection while transferring data on url ${targetUrl}`)))
217
- .on('error', (err) => res.destroy(new Error(`Stream error: ${err.message}`, { cause: err, })));
221
+ .on('data', (chunk) => logger.info(`chunk transferred size=${chunk.size}`))
222
+ .on('close', () => {
223
+ const errMsg = `Upstream server ${registryInfo.normalizedRegistryUrl} closed connection while transferring data on url ${targetUrl}`;
224
+ logger.error(errMsg);
225
+ resToDownstream.destroy(new Error(errMsg));
226
+ })
227
+ .on('error', (err) => {
228
+ const errMsg = `Stream error: ${err.message}`;
229
+ logger.error(errMsg);
230
+ resToDownstream.destroy(new Error(errMsg, { cause: err, }));
231
+ });
218
232
  }
219
233
  }
220
234
  }
221
235
  catch (err) {
222
236
  logger.error('Failed to write upstreamResponse:', err);
223
- if (!res.headersSent) {
224
- res.writeHead(502).end('Internal Server Error');
237
+ if (!resToDownstream.headersSent) {
238
+ resToDownstream.writeHead(502).end('Internal Server Error');
225
239
  }
226
240
  }
227
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.60",
3
+ "version": "1.0.61",
4
4
  "description": "A lightweight npm registry proxy with fallback support",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",