com.jimuwd.xian.registry-proxy 1.0.134 → 1.1.0
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/server/index.js +12 -12
- package/package.json +2 -3
- package/src/client/yarn-install.sh +3 -0
package/dist/server/index.js
CHANGED
|
@@ -129,17 +129,17 @@ async function fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, l
|
|
|
129
129
|
// 替换“Host”头为upstream的host
|
|
130
130
|
const upstreamHost = new URL(registry.normalizedRegistryUrl).host;
|
|
131
131
|
if (mergedHeaders.host) {
|
|
132
|
-
logger.
|
|
132
|
+
logger.debug(() => `Replace 'Host=${mergedHeaders.host}' header in downstream request to upstream 'Host=${upstreamHost}' header when proxying to upstream ${targetUrl}.`);
|
|
133
133
|
mergedHeaders.host = upstreamHost;
|
|
134
134
|
}
|
|
135
135
|
const response = await fetch(targetUrl, { headers: mergedHeaders });
|
|
136
136
|
if (response.ok) {
|
|
137
|
-
logger.debug(`Success response from upstream ${targetUrl}: ${response.status} ${response.statusText}
|
|
137
|
+
logger.debug(() => `Success response from upstream ${targetUrl}: ${response.status} ${response.statusText}
|
|
138
138
|
content-type=${response.headers.get('content-type')} content-encoding=${response.headers.get('content-encoding')} content-length=${response.headers.get('content-length')} transfer-encoding=${response.headers.get('transfer-encoding')}`);
|
|
139
139
|
return response;
|
|
140
140
|
}
|
|
141
141
|
else {
|
|
142
|
-
logger.
|
|
142
|
+
logger.debug(async () => `Failure response from upstream ${targetUrl}: ${response.status} ${response.statusText}
|
|
143
143
|
content-type=${response.headers.get('content-type')} content-encoding=${response.headers.get('content-encoding')} content-length=${response.headers.get('content-length')} transfer-encoding=${response.headers.get('transfer-encoding')}
|
|
144
144
|
body=${await response.text()}`);
|
|
145
145
|
return null;
|
|
@@ -161,7 +161,7 @@ async function fetchFromRegistry(registry, targetUrl, reqFromDownstreamClient, l
|
|
|
161
161
|
}
|
|
162
162
|
}
|
|
163
163
|
async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDownstreamClient, upstreamResponse, reqFromDownstreamClient, proxyInfo, _proxyPort, registryInfos) {
|
|
164
|
-
logger.
|
|
164
|
+
logger.debug(() => `Writing upstream registry server ${registryInfo.normalizedRegistryUrl}'s ${upstreamResponse.status}${upstreamResponse.statusText ? (' "' + upstreamResponse.statusText + '"') : ''} response to downstream client.`);
|
|
165
165
|
if (!upstreamResponse.ok)
|
|
166
166
|
throw new Error("Only 2xx upstream response is supported");
|
|
167
167
|
try {
|
|
@@ -173,7 +173,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
173
173
|
else if (contentType.includes('application/json')) { // JSON 处理逻辑
|
|
174
174
|
const data = await upstreamResponse.json();
|
|
175
175
|
if (data.versions) { // 处理node依赖包元数据
|
|
176
|
-
logger.
|
|
176
|
+
logger.debug(() => "Write package meta data application/json response from upstream to downstream", targetUrl);
|
|
177
177
|
const host = reqFromDownstreamClient.headers.host /*|| `[::1]:${_proxyPort}`*/;
|
|
178
178
|
const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
|
|
179
179
|
for (const versionKey in data.versions) {
|
|
@@ -201,7 +201,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
201
201
|
resToDownstreamClient.writeHead(upstreamResponse.status).end(bodyData);
|
|
202
202
|
}
|
|
203
203
|
else if (contentType.includes('application/octet-stream')) { // 二进制流处理
|
|
204
|
-
logger.
|
|
204
|
+
logger.debug(() => `Write application/octet-stream response from upstream to downstream, upstream url is ${targetUrl}`);
|
|
205
205
|
if (!upstreamResponse.body) {
|
|
206
206
|
logger.error(`Empty response body from upstream ${targetUrl}`);
|
|
207
207
|
resToDownstreamClient.writeHead(502).end('Empty Upstream Response');
|
|
@@ -233,7 +233,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
233
233
|
// this is good when proxying big stream from upstream to downstream.
|
|
234
234
|
const cleanup = () => {
|
|
235
235
|
reqFromDownstreamClient.off('close', cleanup);
|
|
236
|
-
logger.
|
|
236
|
+
logger.debug(() => `Req from downstream client is closed, stop pipe from upstream ${targetUrl} to downstream client.`);
|
|
237
237
|
upstreamResponse.body?.unpipe();
|
|
238
238
|
};
|
|
239
239
|
reqFromDownstreamClient.on('close', cleanup);
|
|
@@ -241,7 +241,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
241
241
|
// clean up when server closes connection to downstream client.
|
|
242
242
|
const cleanup0 = () => {
|
|
243
243
|
resToDownstreamClient.off('close', cleanup0);
|
|
244
|
-
logger.
|
|
244
|
+
logger.debug(() => `Close connection to downstream client, upstream url is ${targetUrl}`);
|
|
245
245
|
// upstreamResponse.body?.unpipe();
|
|
246
246
|
};
|
|
247
247
|
resToDownstreamClient.on('close', cleanup0);
|
|
@@ -249,10 +249,10 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
249
249
|
// pipe upstream body-stream to downstream stream and automatically ends the stream to downstream when upstream stream is ended.
|
|
250
250
|
upstreamResponse.body.pipe(resToDownstreamClient, { end: true });
|
|
251
251
|
upstreamResponse.body
|
|
252
|
-
.on('data', (chunk) => logger.
|
|
253
|
-
.on('end', () => logger.info(`Upstream server ${targetUrl} response.body ended.`))
|
|
252
|
+
.on('data', (chunk) => logger.info(`Chunk transferred from ${targetUrl} to downstream client size=${Buffer.byteLength(chunk)}bytes`))
|
|
253
|
+
.on('end', () => logger.info(`Upstream server ${targetUrl} response.body stream ended.`))
|
|
254
254
|
// connection will be closed automatically when all chunk data is transferred (after stream ends).
|
|
255
|
-
.on('close', () => logger.
|
|
255
|
+
.on('close', () => logger.debug(() => `Upstream ${targetUrl} closed stream.`))
|
|
256
256
|
.on('error', (err) => {
|
|
257
257
|
const errMsg = `Stream error between upstream and registry-proxy server: ${err.message}. Upstream url is ${targetUrl}`;
|
|
258
258
|
logger.error(errMsg);
|
|
@@ -270,7 +270,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
270
270
|
resToDownstreamClient.removeHeader('Keep-Alive');
|
|
271
271
|
resToDownstreamClient.setHeader('content-type', contentType);
|
|
272
272
|
resToDownstreamClient.setHeader('content-length', Buffer.byteLength(bodyData));
|
|
273
|
-
logger.
|
|
273
|
+
logger.debug(() => `Response to downstream client headers ${JSON.stringify(resToDownstreamClient.getHeaders())} Upstream url is ${targetUrl}`);
|
|
274
274
|
resToDownstreamClient.writeHead(upstreamResponse.status).end(bodyData);
|
|
275
275
|
}
|
|
276
276
|
}
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "com.jimuwd.xian.registry-proxy",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A lightweight npm registry proxy with fallback support",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/server/index.js",
|
|
7
7
|
"bin": {
|
|
8
8
|
"registry-proxy": "dist/server/index.js",
|
|
9
|
-
"yarn-install": "dist/client/yarn-install.js"
|
|
10
|
-
"yarn-install-bash": "src/client/yarn-install.sh"
|
|
9
|
+
"yarn-install": "dist/client/yarn-install.js"
|
|
11
10
|
},
|
|
12
11
|
"files": [
|
|
13
12
|
"dist",
|