com.jimuwd.xian.registry-proxy 1.0.47 → 1.0.48
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 +20 -20
- package/package.json +1 -1
- package/src/index.ts +19 -20
package/dist/index.js
CHANGED
|
@@ -160,31 +160,32 @@ async function fetchFromRegistry(registry, targetUrl, limiter) {
|
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
// 修改后的 writeSuccessfulResponse 函数
|
|
163
|
-
async function writeSuccessfulResponse(registryInfo, targetUrl, res,
|
|
164
|
-
if (!
|
|
165
|
-
throw new Error("Only 2xx response is supported");
|
|
163
|
+
async function writeSuccessfulResponse(registryInfo, targetUrl, res, upstreamResponse, req, proxyInfo, proxyPort, registryInfos) {
|
|
164
|
+
if (!upstreamResponse.ok)
|
|
165
|
+
throw new Error("Only 2xx upstream response is supported");
|
|
166
166
|
try {
|
|
167
|
-
const upstreamContentType =
|
|
167
|
+
const upstreamContentType = upstreamResponse.headers.get('Content-Type');
|
|
168
|
+
const upstreamConnection = upstreamResponse.headers.get("connection");
|
|
168
169
|
const contentType = upstreamContentType || 'application/octet-stream';
|
|
170
|
+
const connection = upstreamConnection || 'keep-alive';
|
|
169
171
|
// 准备通用头信息
|
|
170
|
-
const safeHeaders = {
|
|
171
|
-
'content-type': contentType,
|
|
172
|
-
'connection': 'keep-alive'
|
|
173
|
-
};
|
|
172
|
+
const safeHeaders = { 'connection': connection, 'content-type': contentType, };
|
|
174
173
|
// 复制所有可能需要的头信息
|
|
175
174
|
const headersToCopy = [
|
|
176
|
-
'
|
|
177
|
-
'
|
|
178
|
-
'
|
|
175
|
+
'etag', 'last-modified',
|
|
176
|
+
'cache-control',
|
|
177
|
+
'content-length',
|
|
178
|
+
'content-encoding',
|
|
179
|
+
'transfer-encoding',
|
|
179
180
|
];
|
|
180
181
|
headersToCopy.forEach(header => {
|
|
181
|
-
const value =
|
|
182
|
+
const value = upstreamResponse.headers.get(header);
|
|
182
183
|
if (value)
|
|
183
184
|
safeHeaders[header] = value;
|
|
184
185
|
});
|
|
185
186
|
if (contentType.includes('application/json')) {
|
|
186
187
|
// JSON 处理逻辑
|
|
187
|
-
const data = await
|
|
188
|
+
const data = await upstreamResponse.json();
|
|
188
189
|
if (data.versions) {
|
|
189
190
|
const host = req.headers.host || `localhost:${proxyPort}`;
|
|
190
191
|
const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
|
|
@@ -198,23 +199,22 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, r
|
|
|
198
199
|
}
|
|
199
200
|
}
|
|
200
201
|
}
|
|
201
|
-
res.writeHead(
|
|
202
|
-
res.end(JSON.stringify(data));
|
|
202
|
+
res.writeHead(upstreamResponse.status, safeHeaders).end(JSON.stringify(data));
|
|
203
203
|
}
|
|
204
204
|
else {
|
|
205
205
|
// 二进制流处理
|
|
206
|
-
if (!
|
|
206
|
+
if (!upstreamResponse.body) {
|
|
207
207
|
console.error(`Empty response body from ${targetUrl}`);
|
|
208
|
-
res.writeHead(
|
|
208
|
+
res.writeHead(502).end('Empty Upstream Response');
|
|
209
209
|
}
|
|
210
210
|
else {
|
|
211
|
-
res.writeHead(
|
|
212
|
-
|
|
211
|
+
res.writeHead(upstreamResponse.status, safeHeaders);
|
|
212
|
+
upstreamResponse.body.pipe(res, { end: true });
|
|
213
213
|
}
|
|
214
214
|
}
|
|
215
215
|
}
|
|
216
216
|
catch (err) {
|
|
217
|
-
console.error('Failed to write
|
|
217
|
+
console.error('Failed to write upstreamResponse:', err);
|
|
218
218
|
if (!res.headersSent) {
|
|
219
219
|
res.writeHead(502).end('Internal Server Error');
|
|
220
220
|
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -8,7 +8,6 @@ import fetch, {Response} from 'node-fetch';
|
|
|
8
8
|
import {homedir} from 'os';
|
|
9
9
|
import {join, resolve} from 'path';
|
|
10
10
|
import {URL} from 'url';
|
|
11
|
-
import {pipeline} from "node:stream/promises";
|
|
12
11
|
|
|
13
12
|
const {readFile, writeFile} = fsPromises;
|
|
14
13
|
|
|
@@ -218,40 +217,41 @@ async function writeSuccessfulResponse(
|
|
|
218
217
|
registryInfo: RegistryInfo,
|
|
219
218
|
targetUrl: string,
|
|
220
219
|
res: ServerResponse,
|
|
221
|
-
|
|
220
|
+
upstreamResponse: Response,
|
|
222
221
|
req: IncomingMessage,
|
|
223
222
|
proxyInfo: ProxyInfo,
|
|
224
223
|
proxyPort: number,
|
|
225
224
|
registryInfos: RegistryInfo[]
|
|
226
225
|
): Promise<void> {
|
|
227
226
|
|
|
228
|
-
if (!
|
|
227
|
+
if (!upstreamResponse.ok) throw new Error("Only 2xx upstream response is supported");
|
|
229
228
|
|
|
230
229
|
try {
|
|
231
|
-
const upstreamContentType =
|
|
230
|
+
const upstreamContentType = upstreamResponse.headers.get('Content-Type');
|
|
231
|
+
const upstreamConnection = upstreamResponse.headers.get("connection");
|
|
232
232
|
const contentType = upstreamContentType || 'application/octet-stream';
|
|
233
|
+
const connection = upstreamConnection || 'keep-alive';
|
|
233
234
|
|
|
234
235
|
// 准备通用头信息
|
|
235
|
-
const safeHeaders: OutgoingHttpHeaders = {
|
|
236
|
-
'content-type': contentType,
|
|
237
|
-
'connection': 'keep-alive'
|
|
238
|
-
};
|
|
236
|
+
const safeHeaders: OutgoingHttpHeaders = {'connection': connection, 'content-type': contentType,};
|
|
239
237
|
|
|
240
238
|
// 复制所有可能需要的头信息
|
|
241
239
|
const headersToCopy = [
|
|
242
|
-
'
|
|
243
|
-
'
|
|
244
|
-
'
|
|
240
|
+
'etag', 'last-modified',
|
|
241
|
+
'cache-control',
|
|
242
|
+
'content-length',
|
|
243
|
+
'content-encoding',
|
|
244
|
+
'transfer-encoding',
|
|
245
245
|
];
|
|
246
246
|
|
|
247
247
|
headersToCopy.forEach(header => {
|
|
248
|
-
const value =
|
|
248
|
+
const value = upstreamResponse.headers.get(header);
|
|
249
249
|
if (value) safeHeaders[header] = value;
|
|
250
250
|
});
|
|
251
251
|
|
|
252
252
|
if (contentType.includes('application/json')) {
|
|
253
253
|
// JSON 处理逻辑
|
|
254
|
-
const data = await
|
|
254
|
+
const data = await upstreamResponse.json() as PackageData;
|
|
255
255
|
if (data.versions) {
|
|
256
256
|
const host = req.headers.host || `localhost:${proxyPort}`;
|
|
257
257
|
const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
|
|
@@ -266,20 +266,19 @@ async function writeSuccessfulResponse(
|
|
|
266
266
|
}
|
|
267
267
|
}
|
|
268
268
|
}
|
|
269
|
-
res.writeHead(
|
|
270
|
-
res.end(JSON.stringify(data));
|
|
269
|
+
res.writeHead(upstreamResponse.status, safeHeaders).end(JSON.stringify(data));
|
|
271
270
|
} else {
|
|
272
271
|
// 二进制流处理
|
|
273
|
-
if (!
|
|
272
|
+
if (!upstreamResponse.body) {
|
|
274
273
|
console.error(`Empty response body from ${targetUrl}`);
|
|
275
|
-
res.writeHead(
|
|
274
|
+
res.writeHead(502).end('Empty Upstream Response');
|
|
276
275
|
} else {
|
|
277
|
-
res.writeHead(
|
|
278
|
-
|
|
276
|
+
res.writeHead(upstreamResponse.status, safeHeaders);
|
|
277
|
+
upstreamResponse.body.pipe(res, {end: true});
|
|
279
278
|
}
|
|
280
279
|
}
|
|
281
280
|
} catch (err) {
|
|
282
|
-
console.error('Failed to write
|
|
281
|
+
console.error('Failed to write upstreamResponse:', err);
|
|
283
282
|
if (!res.headersSent) {
|
|
284
283
|
res.writeHead(502).end('Internal Server Error');
|
|
285
284
|
}
|