com.jimuwd.xian.registry-proxy 1.0.76 → 1.0.78
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 +10 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -173,7 +173,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
173
173
|
const safeHeaders = { 'connection': connection, 'content-type': contentType, };
|
|
174
174
|
// 复制所有可能需要的头信息
|
|
175
175
|
const headersToCopy = [
|
|
176
|
-
'content-length',
|
|
176
|
+
// 'content-length', upstream response body is modified by proxy, the content-length is not valid anymore.
|
|
177
177
|
'content-encoding',
|
|
178
178
|
'transfer-encoding',
|
|
179
179
|
];
|
|
@@ -198,7 +198,10 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
198
198
|
}
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
|
-
|
|
201
|
+
const bodyData = JSON.stringify(data);
|
|
202
|
+
// body is modified, reset the content-length
|
|
203
|
+
safeHeaders["content-length"] = bodyData.length;
|
|
204
|
+
resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(bodyData);
|
|
202
205
|
}
|
|
203
206
|
else if (contentType.includes('application/octet-stream')) {
|
|
204
207
|
// 二进制流处理
|
|
@@ -209,22 +212,19 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
209
212
|
else {
|
|
210
213
|
// write back to client
|
|
211
214
|
resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders);
|
|
212
|
-
//
|
|
215
|
+
// stop pipe when req from client is closed accidentally.
|
|
213
216
|
const cleanup = () => {
|
|
214
217
|
reqFromDownstreamClient.off('close', cleanup);
|
|
215
|
-
logger.
|
|
218
|
+
logger.info(`Req from downstream client is closed, stop pipe from upstream ${targetUrl} to downstream client.`);
|
|
216
219
|
upstreamResponse.body?.unpipe();
|
|
217
|
-
resToDownstreamClient.destroy();
|
|
218
|
-
reqFromDownstreamClient.destroy();
|
|
219
220
|
};
|
|
220
221
|
reqFromDownstreamClient.on('close', cleanup);
|
|
222
|
+
reqFromDownstreamClient.on('end', () => logger.info("Req from downstream client ends."));
|
|
221
223
|
// clean up when server closes connection to downstream client.
|
|
222
224
|
const cleanup0 = () => {
|
|
223
225
|
resToDownstreamClient.off('close', cleanup0);
|
|
224
226
|
logger.info(`Close connection to downstream client, upstream url is ${targetUrl}`);
|
|
225
|
-
|
|
226
|
-
// resToDownstreamClient.destroy();
|
|
227
|
-
// reqFromDownstreamClient.destroy();
|
|
227
|
+
upstreamResponse.body?.unpipe();
|
|
228
228
|
};
|
|
229
229
|
resToDownstreamClient.on('close', cleanup0);
|
|
230
230
|
// write back body data (chunked probably)
|
|
@@ -250,7 +250,7 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
250
250
|
}
|
|
251
251
|
else {
|
|
252
252
|
logger.warn(`Unsupported response content-type from upstream ${targetUrl}`);
|
|
253
|
-
resToDownstreamClient.writeHead(upstreamResponse.status,
|
|
253
|
+
resToDownstreamClient.writeHead(upstreamResponse.status, safeHeaders).end(upstreamResponse.text());
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
catch (err) {
|