134 lines
2.8 KiB
Markdown
134 lines
2.8 KiB
Markdown
# Expo Updates 服务端实现(COS 资源版)
|
||
|
||
本服务实现了 Expo Updates 协议 v0 和 v1,支持 React Native 应用的 OTA 热更新。
|
||
资源文件存储在腾讯云 COS 上,服务端只负责返回 manifest。
|
||
|
||
## API 端点
|
||
|
||
### 1. 获取 Manifest
|
||
|
||
```
|
||
GET /expo-updates/manifest
|
||
```
|
||
|
||
**请求头:**
|
||
| 头部 | 必需 | 说明 |
|
||
|------|------|------|
|
||
| `expo-platform` | 是 | 平台类型:`ios` 或 `android` |
|
||
| `expo-runtime-version` | 是 | 运行时版本号 |
|
||
| `expo-protocol-version` | 否 | 协议版本:`0` 或 `1`(默认 `0`) |
|
||
| `expo-current-update-id` | 否 | 当前更新 ID |
|
||
|
||
### 2. 注册更新版本
|
||
|
||
```
|
||
POST /expo-updates/updates
|
||
```
|
||
|
||
**请求体:**
|
||
```json
|
||
{
|
||
"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` 中:
|
||
|
||
```json
|
||
{
|
||
"expo": {
|
||
"updates": {
|
||
"url": "https://your-server.com/expo-updates/manifest",
|
||
"enabled": true
|
||
},
|
||
"runtimeVersion": "1.0.0"
|
||
}
|
||
}
|
||
```
|
||
|
||
## 生成资源哈希
|
||
|
||
资源的 hash 需要是 Base64URL 编码的 SHA256 哈希:
|
||
|
||
```javascript
|
||
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(/=+$/, '');
|
||
}
|
||
```
|
||
|
||
## 测试
|
||
|
||
```bash
|
||
# 注册更新
|
||
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
|
||
```
|