Files
plates-server/docs/expo-updates.md

2.8 KiB
Raw Blame History

Expo Updates 服务端实现COS 资源版)

本服务实现了 Expo Updates 协议 v0 和 v1支持 React Native 应用的 OTA 热更新。 资源文件存储在腾讯云 COS 上,服务端只负责返回 manifest。

API 端点

1. 获取 Manifest

GET /expo-updates/manifest

请求头:

头部 必需 说明
expo-platform 平台类型:iosandroid
expo-runtime-version 运行时版本号
expo-protocol-version 协议版本:01(默认 0
expo-current-update-id 当前更新 ID

2. 注册更新版本

POST /expo-updates/updates

请求体:

{
  "runtimeVersion": "1.0.0",
  "createdAt": "2024-01-01T00:00:00.000Z",
  "ios": {
    "launchAsset": {
      "url": "https://your-bucket.cos.ap-guangzhou.myqcloud.com/updates/1.0.0/ios/bundle.js",
      "hash": "Base64URL编码的SHA256哈希"
    },
    "assets": [
      {
        "url": "https://your-bucket.cos.ap-guangzhou.myqcloud.com/updates/1.0.0/assets/icon.png",
        "hash": "Base64URL编码的SHA256哈希",
        "key": "icon",
        "contentType": "image/png",
        "fileExtension": ".png"
      }
    ]
  },
  "android": {
    "launchAsset": {
      "url": "https://your-bucket.cos.ap-guangzhou.myqcloud.com/updates/1.0.0/android/bundle.js",
      "hash": "Base64URL编码的SHA256哈希"
    },
    "assets": []
  },
  "expoClient": {
    "name": "YourApp",
    "version": "1.0.0"
  }
}

3. 获取所有更新版本

GET /expo-updates/updates

4. 获取指定版本

GET /expo-updates/updates/:runtimeVersion

5. 删除更新版本

DELETE /expo-updates/updates/:runtimeVersion

客户端配置

在 React Native 应用的 app.json 中:

{
  "expo": {
    "updates": {
      "url": "https://your-server.com/expo-updates/manifest",
      "enabled": true
    },
    "runtimeVersion": "1.0.0"
  }
}

生成资源哈希

资源的 hash 需要是 Base64URL 编码的 SHA256 哈希:

const crypto = require('crypto');
const fs = require('fs');

function getAssetHash(filePath) {
  const content = fs.readFileSync(filePath);
  const hash = crypto.createHash('sha256').update(content).digest('base64');
  // 转换为 Base64URL
  return hash.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}

测试

# 注册更新
curl -X POST http://localhost:3000/expo-updates/updates \
  -H "Content-Type: application/json" \
  -d '{
    "runtimeVersion": "1.0.0",
    "ios": {
      "launchAsset": {
        "url": "https://cos.example.com/bundle.js",
        "hash": "abc123"
      },
      "assets": []
    }
  }'

# 获取 manifest
curl -H "expo-platform: ios" \
     -H "expo-runtime-version: 1.0.0" \
     http://localhost:3000/expo-updates/manifest