Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ethanblakes
Copy link

Pull Request

📋 变更概述

fix: 使用流式处理优化 npm registry 响应,避免 1102 错误,并支持 @scope/package 名称

变更类型

  • 🐛 Bug 修复
  • ⚡ 性能优化

🔍 详细说明

变更内容

  • 将 npm registry 响应从一次性 response.text() 替换改为 流式替换,避免大 JSON(如 prisma 包)导致 Worker CPU 超时 (1102)。
  • 更新正则匹配规则,支持 @scope/package 格式的 npm 包名。

实现方法

  • 使用 TransformStream + TextDecoderStream + TextEncoderStream 组合,实现边读边替换。
  • 引入 tail 缓冲,解决跨 chunk 边界匹配不完整问题。
  • 正则由 ([^/]+) 改为 (@?[^/]+),确保带 @scope 的包路径也能正确重写。

影响范围

  • 仅影响 npm registry 响应重写逻辑
  • 不影响 PyPI、Docker、Git 等其他平台。

🧪 测试

验证步骤

  1. curl 请求 metadata:
    curl -s https://<workers>.dev/npm/react | jq '.versions["18.2.0"].dist.tarball'
    👉 tarball 链接应替换为镜像域名。
  2. curl -I 请求 tarball:
    curl -I https://<workers>.dev/npm/react/-/react-18.2.0.tgz
    👉 应返回 200 OK。
  3. npm install react --registry=https://<workers>.dev/npm
    👉 能正常安装。
  4. npm install prisma --registry=https://<workers>.dev/npm
    👉 大包测试通过,无 1102 错误。

测试结果

  • ✅ React 安装成功
  • ✅ Prisma 安装成功
  • ✅ WARN 消息(@scope 包)已消失

📊 性能影响

  • 变更前:大包 metadata (prisma) 容易触发 Cloudflare Worker 1102 CPU 超时。
  • 变更后:流式处理,CPU 占用明显降低,长 JSON 请求稳定完成。

🔒 安全考虑

  • 仅替换公共 npm registry 链接,无额外安全风险。
  • 保持 JSON 格式不变,不影响 npm CLI 解析。

ethanblakes and others added 3 commits September 25, 2025 19:05
fix: 使用流式处理优化npm registry响应,避免1102错误 支持 @scope/package 名称
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants