com.jimuwd.xian.registry-proxy 1.0.98 → 1.0.99
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 +23 -17
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -184,33 +184,34 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
184
184
|
// 默认是 connection: keep-alive 和 keep-alive: timeout=5,这里直接给它咔嚓掉
|
|
185
185
|
resToDownstreamClient.setHeader('Connection', 'close');
|
|
186
186
|
resToDownstreamClient.removeHeader('Keep-Alive');
|
|
187
|
-
resToDownstreamClient.setHeader('content-type',
|
|
187
|
+
resToDownstreamClient.setHeader('content-type', contentType);
|
|
188
188
|
resToDownstreamClient.setHeader('content-length', Buffer.byteLength(bodyData));
|
|
189
189
|
logger.info(`Response to downstream client headers`, JSON.stringify(resToDownstreamClient.getHeaders()), targetUrl);
|
|
190
190
|
resToDownstreamClient.writeHead(upstreamResponse.status).end(bodyData);
|
|
191
191
|
}
|
|
192
192
|
else if (contentType.includes('application/octet-stream')) { // 二进制流处理
|
|
193
193
|
logger.info("Write application/octet-stream response from upstream to downstream", targetUrl);
|
|
194
|
-
// 准备通用响应头信息
|
|
195
|
-
const safeHeaders = {};
|
|
196
|
-
// 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如access-control-allow-origin、set-cookie、server、strict-transport-security等,这意味着代理服务器向下游客户端屏蔽了这些认证等安全数据)
|
|
197
|
-
// 也不能包含cf-cache-status、cf-ray(Cloudflare 特有字段)可能干扰客户端解析。
|
|
198
|
-
const headersToCopy = ['cache-control', 'connection', 'content-type', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
|
|
199
|
-
headersToCopy.forEach(header => {
|
|
200
|
-
const value = upstreamResponse.headers.get(header);
|
|
201
|
-
if (value)
|
|
202
|
-
safeHeaders[header] = value;
|
|
203
|
-
});
|
|
204
|
-
if (!safeHeaders['content-type'])
|
|
205
|
-
safeHeaders['content-type'] = 'application/octet-stream';
|
|
206
194
|
if (!upstreamResponse.body) {
|
|
207
195
|
logger.error(`Empty response body from upstream ${targetUrl}`);
|
|
208
196
|
resToDownstreamClient.writeHead(502).end('Empty Upstream Response');
|
|
209
197
|
}
|
|
210
198
|
else {
|
|
211
199
|
// write back to client
|
|
200
|
+
// 准备通用响应头信息
|
|
201
|
+
const safeHeaders = { 'content-type': contentType };
|
|
202
|
+
// 复制所有可能需要的头信息(不包含安全相关的敏感头信息,如access-control-allow-origin、set-cookie、server、strict-transport-security等,这意味着代理服务器向下游客户端屏蔽了这些认证等安全数据)
|
|
203
|
+
// 也不能包含cf-cache-status、cf-ray(Cloudflare 特有字段)可能干扰客户端解析。
|
|
204
|
+
const headersToCopy = ['cache-control', 'connection', 'content-encoding', 'content-length', 'date', 'etag', 'last-modified', 'transfer-encoding', 'vary',];
|
|
205
|
+
headersToCopy.forEach(header => {
|
|
206
|
+
const value = upstreamResponse.headers.get(header);
|
|
207
|
+
if (value)
|
|
208
|
+
safeHeaders[header] = value;
|
|
209
|
+
});
|
|
210
|
+
// 必须使用 ServerResponse.setHeaders(safeHeaders)来覆盖现有headers而不是ServerResponse.writeHead(status,headers)来合并headers!
|
|
211
|
+
// 这个坑害我浪费很久事件来调试!
|
|
212
|
+
resToDownstreamClient.setHeaders(safeHeaders);
|
|
212
213
|
logger.info(`Response to downstream client headers`, JSON.stringify(safeHeaders), targetUrl);
|
|
213
|
-
resToDownstreamClient.writeHead(upstreamResponse.status
|
|
214
|
+
resToDownstreamClient.writeHead(upstreamResponse.status);
|
|
214
215
|
// stop pipe when req from client is closed accidentally.
|
|
215
216
|
const cleanup = () => {
|
|
216
217
|
reqFromDownstreamClient.off('close', cleanup);
|
|
@@ -250,9 +251,14 @@ async function writeResponseToDownstreamClient(registryInfo, targetUrl, resToDow
|
|
|
250
251
|
else {
|
|
251
252
|
logger.warn(`Write unsupported content-type=${contentType} response from upstream to downstream ${targetUrl}`);
|
|
252
253
|
const bodyData = await upstreamResponse.text();
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
resToDownstreamClient.
|
|
254
|
+
resToDownstreamClient.removeHeader('Transfer-Encoding');
|
|
255
|
+
// 默认是 connection: keep-alive 和 keep-alive: timeout=5,这里直接给它咔嚓掉
|
|
256
|
+
resToDownstreamClient.setHeader('Connection', 'close');
|
|
257
|
+
resToDownstreamClient.removeHeader('Keep-Alive');
|
|
258
|
+
resToDownstreamClient.setHeader('content-type', contentType);
|
|
259
|
+
resToDownstreamClient.setHeader('content-length', Buffer.byteLength(bodyData));
|
|
260
|
+
logger.info(`Response to downstream client headers`, JSON.stringify(resToDownstreamClient.getHeaders()), targetUrl);
|
|
261
|
+
resToDownstreamClient.writeHead(upstreamResponse.status).end(bodyData);
|
|
256
262
|
}
|
|
257
263
|
}
|
|
258
264
|
catch (err) {
|