com.jimuwd.xian.registry-proxy 1.0.39 → 1.0.41

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 CHANGED
@@ -7,7 +7,6 @@ import fetch from 'node-fetch';
7
7
  import { homedir } from 'os';
8
8
  import { join, resolve } from 'path';
9
9
  import { URL } from 'url';
10
- import { Readable } from "node:stream";
11
10
  import { pipeline } from "node:stream/promises";
12
11
  const { readFile, writeFile } = fsPromises;
13
12
  class ConcurrencyLimiter {
@@ -183,51 +182,33 @@ async function writeSuccessfulResponse(registryInfo, targetUrl, res, response, r
183
182
  if (value)
184
183
  safeHeaders[header] = value;
185
184
  });
186
- res.writeHead(response.status, safeHeaders);
187
185
  if (contentType.includes('application/json')) {
188
186
  // JSON 处理逻辑
189
187
  const data = await response.json();
190
188
  if (data.versions) {
191
189
  const host = req.headers.host || `localhost:${proxyPort}`;
192
190
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
193
- for (const version in data.versions) {
194
- const tarball = data.versions[version]?.dist?.tarball;
191
+ for (const versionKey in data.versions) {
192
+ const packageVersion = data.versions[versionKey];
193
+ const tarball = packageVersion?.dist?.tarball;
195
194
  if (tarball) {
196
195
  const path = removeRegistryPrefix(tarball, registryInfos);
197
196
  const proxiedTarballUrl = `${baseUrl}${path}${new URL(tarball).search || ''}`;
198
- data.versions[version].dist.tarball = proxiedTarballUrl;
197
+ packageVersion.dist.tarball = proxiedTarballUrl;
199
198
  }
200
199
  }
201
200
  }
201
+ res.writeHead(response.status, { "content-type": contentType });
202
202
  res.end(JSON.stringify(data));
203
203
  }
204
204
  else {
205
205
  // 二进制流处理
206
+ res.writeHead(response.status, safeHeaders);
206
207
  if (!response.body) {
207
208
  console.error(`Empty response body from ${targetUrl}`);
208
209
  }
209
210
  else {
210
- let nodeStream;
211
- if (typeof Readable.fromWeb === 'function') {
212
- // Node.js 17+ 标准方式
213
- nodeStream = Readable.fromWeb(response.body);
214
- }
215
- else {
216
- // Node.js 16 及以下版本的兼容方案
217
- const { PassThrough } = await import('stream');
218
- const passThrough = new PassThrough();
219
- const reader = response.body.getReader();
220
- const pump = async () => {
221
- const { done, value } = await reader.read();
222
- if (done)
223
- return passThrough.end();
224
- passThrough.write(value);
225
- await pump();
226
- };
227
- pump().catch(err => passThrough.destroy(err));
228
- nodeStream = passThrough;
229
- }
230
- await pipeline(nodeStream, res);
211
+ await pipeline(response.body, res);
231
212
  }
232
213
  }
233
214
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.jimuwd.xian.registry-proxy",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
4
4
  "type": "module",
5
5
  "description": "A lightweight npm registry proxy with fallback support",
6
6
  "main": "dist/index.js",
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 {Readable} from "node:stream";
12
11
  import {pipeline} from "node:stream/promises";
13
12
 
14
13
  const {readFile, writeFile} = fsPromises;
@@ -249,9 +248,6 @@ async function writeSuccessfulResponse(
249
248
  if (value) safeHeaders[header] = value;
250
249
  });
251
250
 
252
-
253
- res.writeHead(response.status, safeHeaders);
254
-
255
251
  if (contentType.includes('application/json')) {
256
252
  // JSON 处理逻辑
257
253
  const data = await response.json() as PackageData;
@@ -259,43 +255,25 @@ async function writeSuccessfulResponse(
259
255
  const host = req.headers.host || `localhost:${proxyPort}`;
260
256
  const baseUrl = `${proxyInfo.https ? 'https' : 'http'}://${host}${proxyInfo.basePath === '/' ? '' : proxyInfo.basePath}`;
261
257
 
262
- for (const version in data.versions) {
263
- const tarball = data.versions[version]?.dist?.tarball;
258
+ for (const versionKey in data.versions) {
259
+ const packageVersion = data.versions[versionKey];
260
+ const tarball = packageVersion?.dist?.tarball;
264
261
  if (tarball) {
265
262
  const path = removeRegistryPrefix(tarball, registryInfos);
266
263
  const proxiedTarballUrl: string = `${baseUrl}${path}${new URL(tarball).search || ''}`;
267
- data.versions[version]!.dist!.tarball = proxiedTarballUrl as string;
264
+ packageVersion!.dist!.tarball = proxiedTarballUrl as string;
268
265
  }
269
266
  }
270
267
  }
268
+ res.writeHead(response.status, {"content-type": contentType});
271
269
  res.end(JSON.stringify(data));
272
270
  } else {
273
271
  // 二进制流处理
272
+ res.writeHead(response.status, safeHeaders);
274
273
  if (!response.body) {
275
274
  console.error(`Empty response body from ${targetUrl}`);
276
275
  } else {
277
- let nodeStream: NodeJS.ReadableStream;
278
- if (typeof Readable.fromWeb === 'function') {
279
- // Node.js 17+ 标准方式
280
- nodeStream = Readable.fromWeb(response.body as any);
281
- } else {
282
- // Node.js 16 及以下版本的兼容方案
283
- const {PassThrough} = await import('stream');
284
- const passThrough = new PassThrough();
285
- const reader = (response.body as any).getReader();
286
-
287
- const pump = async () => {
288
- const {done, value} = await reader.read();
289
- if (done) return passThrough.end();
290
- passThrough.write(value);
291
- await pump();
292
- };
293
-
294
- pump().catch(err => passThrough.destroy(err));
295
- nodeStream = passThrough;
296
- }
297
-
298
- await pipeline(nodeStream, res);
276
+ await pipeline(response.body, res);
299
277
  }
300
278
  }
301
279
  } catch (err) {