com.jimuwd.xian.registry-proxy 1.0.61 → 1.0.63
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.
- package/dist/index.js +25 -23
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -160,8 +160,7 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
|
|
|
160
160
|
limiter.release();
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
|
-
|
|
164
|
-
async function writeSuccessfulResponse(registryInfo, targetUrl, resToDownstream, upstreamResponse, downstreamReq, proxyInfo, proxyPort, registryInfos) {
|
|
163
|
+
async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, downstreamReq, proxyInfo, proxyPort, registryInfos) {
|
|
165
164
|
if (!upstreamResponse.ok)
|
|
166
165
|
throw new Error("Only 2xx upstream response is supported");
|
|
167
166
|
try {
|
|
@@ -196,17 +195,17 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, resToDownstream,
|
|
|
196
195
|
}
|
|
197
196
|
}
|
|
198
197
|
}
|
|
199
|
-
|
|
198
|
+
resToDownstreamClient.writeHead(upstreamResponse.status, { "content-type": contentType }).end(JSON.stringify(data));
|
|
200
199
|
}
|
|
201
200
|
else {
|
|
202
201
|
// 二进制流处理
|
|
203
202
|
if (!upstreamResponse.body) {
|
|
204
203
|
logger.error(`Empty response body from ${targetUrl}`);
|
|
205
|
-
|
|
204
|
+
resToDownstreamClient.writeHead(502).end('Empty Upstream Response');
|
|
206
205
|
}
|
|
207
206
|
else {
|
|
208
207
|
// write back to client
|
|
209
|
-
|
|
208
|
+
resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
|
|
210
209
|
// stop transfer if client is closed accidentally.
|
|
211
210
|
const cleanup = () => {
|
|
212
211
|
downstreamReq.off('close', cleanup);
|
|
@@ -215,27 +214,30 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, resToDownstream,
|
|
|
215
214
|
};
|
|
216
215
|
downstreamReq.on('close', cleanup);
|
|
217
216
|
// write back body data (chunked probably)
|
|
218
|
-
// pipe upstream body to client
|
|
219
|
-
upstreamResponse.body.pipe(
|
|
217
|
+
// pipe upstream body to downstream client and end when upstream ends.
|
|
218
|
+
upstreamResponse.body.pipe(resToDownstreamClient, { end: true });
|
|
220
219
|
upstreamResponse.body
|
|
221
|
-
.on('data', (chunk) => logger.info(`chunk transferred size=${chunk.
|
|
220
|
+
.on('data', (chunk) => logger.info(`chunk transferred size=${chunk.length}`))
|
|
221
|
+
.on('end', () => {
|
|
222
|
+
logger.info(`Upstream server ${registryInfo.normalizedRegistryUrl} response.body ended on url ${targetUrl}`);
|
|
223
|
+
})
|
|
222
224
|
.on('close', () => {
|
|
223
225
|
const errMsg = `Upstream server ${registryInfo.normalizedRegistryUrl} closed connection while transferring data on url ${targetUrl}`;
|
|
224
226
|
logger.error(errMsg);
|
|
225
|
-
|
|
227
|
+
// resToDownstreamClient.destroy(new Error(errMsg));
|
|
226
228
|
})
|
|
227
229
|
.on('error', (err) => {
|
|
228
230
|
const errMsg = `Stream error: ${err.message}`;
|
|
229
231
|
logger.error(errMsg);
|
|
230
|
-
|
|
232
|
+
resToDownstreamClient.destroy(new Error(errMsg, { cause: err, }));
|
|
231
233
|
});
|
|
232
234
|
}
|
|
233
235
|
}
|
|
234
236
|
}
|
|
235
237
|
catch (err) {
|
|
236
238
|
logger.error('Failed to write upstreamResponse:', err);
|
|
237
|
-
if (!
|
|
238
|
-
|
|
239
|
+
if (!resToDownstreamClient.headersSent) {
|
|
240
|
+
resToDownstreamClient.writeHead(502).end('Internal Server Error');
|
|
239
241
|
}
|
|
240
242
|
}
|
|
241
243
|
}
|
|
@@ -247,41 +249,41 @@ export async function startProxyServer(proxyConfigPath, localYarnConfigPath, glo
|
|
|
247
249
|
logger.info('Proxy base path:', basePathPrefixedWithSlash);
|
|
248
250
|
logger.info('HTTPS:', !!proxyInfo.https);
|
|
249
251
|
let proxyPort;
|
|
250
|
-
const requestHandler = async (
|
|
251
|
-
if (!
|
|
252
|
-
|
|
252
|
+
const requestHandler = async (reqFromDownstreamClient, resToDownstreamClient) => {
|
|
253
|
+
if (!reqFromDownstreamClient.url || !reqFromDownstreamClient.headers.host) {
|
|
254
|
+
resToDownstreamClient.writeHead(400).end('Invalid Request');
|
|
253
255
|
return;
|
|
254
256
|
}
|
|
255
|
-
const fullUrl = new URL(
|
|
257
|
+
const fullUrl = new URL(reqFromDownstreamClient.url, `${proxyInfo.https ? 'https' : 'http'}://${reqFromDownstreamClient.headers.host}`);
|
|
256
258
|
if (!fullUrl.pathname.startsWith(basePathPrefixedWithSlash)) {
|
|
257
|
-
|
|
259
|
+
resToDownstreamClient.writeHead(404).end('Not Found');
|
|
258
260
|
return;
|
|
259
261
|
}
|
|
260
262
|
const path = basePathPrefixedWithSlash === '/'
|
|
261
263
|
? fullUrl.pathname
|
|
262
264
|
: fullUrl.pathname.slice(basePathPrefixedWithSlash.length);
|
|
263
265
|
// 顺序尝试注册表,获取第一个成功响应
|
|
264
|
-
let
|
|
266
|
+
let successfulResponseFromUpstream = null;
|
|
265
267
|
let targetRegistry = null;
|
|
266
268
|
let targetUrl = null;
|
|
267
269
|
for (const registry of registryInfos) {
|
|
268
|
-
if (
|
|
270
|
+
if (reqFromDownstreamClient.destroyed)
|
|
269
271
|
break;
|
|
270
272
|
targetRegistry = registry;
|
|
271
273
|
const search = fullUrl.search || '';
|
|
272
274
|
targetUrl = `${registry.normalizedRegistryUrl}${path}${search}`;
|
|
273
275
|
const okResponseOrNull = await fetchFromRegistry(registry, targetUrl, limiter);
|
|
274
276
|
if (okResponseOrNull) {
|
|
275
|
-
|
|
277
|
+
successfulResponseFromUpstream = okResponseOrNull;
|
|
276
278
|
break;
|
|
277
279
|
}
|
|
278
280
|
}
|
|
279
281
|
// 统一回写响应
|
|
280
|
-
if (
|
|
281
|
-
await
|
|
282
|
+
if (successfulResponseFromUpstream) {
|
|
283
|
+
await writeResponseToDownstreamClient(targetRegistry, targetUrl, resToDownstreamClient, successfulResponseFromUpstream, reqFromDownstreamClient, proxyInfo, proxyPort, registryInfos);
|
|
282
284
|
}
|
|
283
285
|
else {
|
|
284
|
-
|
|
286
|
+
resToDownstreamClient.writeHead(404).end('All upstream registries failed');
|
|
285
287
|
}
|
|
286
288
|
};
|
|
287
289
|
let server;
|