__CAPGO_KEEP_0__

捆绑包

捆绑包是Capgo中的核心更新包。每个捆绑包包含构成应用内容的Web资产(HTML、CSS、JS)。捆绑包API允许您管理这些更新包,包括列出和删除它们。

捆绑包代表应用Web内容的特定捆绑包(版本),并包含:

  • 捆绑包(版本): 语义版本号 为捆绑包
  • 校验和: 验证捆绑包完整性的唯一哈希
  • 存储信息: 关于捆绑包存储位置和方式的详细信息
  • 原生要求: 原生应用程序的最低版本要求
  • 元数据: 创建时间、拥有者和其他跟踪信息

手动捆绑包创建 (不含 CLI)

手动捆绑包创建 (不含 CLI)

以下是如何手动创建和上传捆绑包而不使用 Capgo CLI:

步骤 1:构建您的应用

步骤 1:构建您的应用

首先,构建您的应用的 Web 资产:

终端窗口
npm run build

步骤 2:使用相同的包创建捆绑包 ZIP(Capgo CLI 内部使用的包):

步骤 2:使用相同的包创建捆绑包 ZIP(Capgo CLI 内部使用的包)

重要: 使用 Capgo CLI 内部使用的相同 JavaScript 包来确保兼容性。

安装必需的包

安装必需的包
终端窗口
npm install adm-zip @tomasklaen/checksum

使用 JavaScript 创建 ZIP 压缩包(与 Capgo CLI 相同)

标题:使用 JavaScript 创建 ZIP 压缩包(与 Capgo CLI 相同)

注意:以下示例中, version 指的是 API 使用的包(版本)名称。

const fs = require('node:fs');
const path = require('node:path');
const os = require('node:os');
const AdmZip = require('adm-zip');
const { checksum: getChecksum } = require('@tomasklaen/checksum');
// Exact same implementation as Capgo CLI
function zipFileUnix(filePath) {
const zip = new AdmZip();
zip.addLocalFolder(filePath);
return zip.toBuffer();
}
async function zipFileWindows(filePath) {
console.log('Zipping file windows mode');
const zip = new AdmZip();
const addToZip = (folderPath, zipPath) => {
const items = fs.readdirSync(folderPath);
for (const item of items) {
const itemPath = path.join(folderPath, item);
const stats = fs.statSync(itemPath);
if (stats.isFile()) {
const fileContent = fs.readFileSync(itemPath);
zip.addFile(path.join(zipPath, item).split(path.sep).join('/'), fileContent);
} else if (stats.isDirectory()) {
addToZip(itemPath, path.join(zipPath, item));
}
}
};
addToZip(filePath, '');
return zip.toBuffer();
}
// Main zipFile function (exact same logic as CLI)
async function zipFile(filePath) {
if (os.platform() === 'win32') {
return zipFileWindows(filePath);
} else {
return zipFileUnix(filePath);
}
}
async function createBundle(inputPath, outputPath, version) {
// Create zip using exact same method as Capgo CLI
const zipped = await zipFile(inputPath);
// Write to file
fs.writeFileSync(outputPath, zipped);
// Calculate checksum using exact same package as CLI
const checksum = await getChecksum(zipped, 'sha256');
return {
filename: path.basename(outputPath),
version: version,
size: zipped.length,
checksum: checksum
};
}
// Usage
async function main() {
try {
const result = await createBundle('./dist', './my-app-1.2.3.zip', '1.2.3');
console.log('Bundle info:', JSON.stringify(result, null, 2));
} catch (error) {
console.error('Error creating bundle:', error);
}
}
main();

步骤 3:使用相同的包 CLI 计算 SHA256 校验和

标题:步骤 3:使用相同的包 CLI 计算 SHA256 校验和
const fs = require('node:fs');
const { checksum: getChecksum } = require('@tomasklaen/checksum');
async function calculateChecksum(filePath) {
const fileBuffer = fs.readFileSync(filePath);
// Use exact same package and method as Capgo CLI
const checksum = await getChecksum(fileBuffer, 'sha256');
return checksum;
}
// Usage
async function main() {
const checksum = await calculateChecksum('./my-app-1.2.3.zip');
console.log('Checksum:', checksum);
}
main();

步骤 4:上传压缩包到您的存储空间

标题:步骤 4:上传压缩包到您的存储空间

将您的 zip 文件上传到任何可通过 Web 访问的存储中:

终端窗口
# Example: Upload to your server via scp
scp my-app-1.2.3.zip user@your-server.com:/var/www/bundles/
# Example: Upload to S3 using AWS CLI
aws s3 cp my-app-1.2.3.zip s3://your-bucket/bundles/
# Example: Upload via curl to a custom endpoint
curl -X POST https://your-storage-api.com/upload \
-H "Authorization: Bearer YOUR_TOKEN" \
-F "file=@my-app-1.2.3.zip"

重要: 必须通过 HTTPS URL 公开访问(无需身份验证)。 __CAPGO_KEEP_0__ 的服务器需要从此 URL 下载包。 有效的公共 URL 的示例: 步骤 5:将包注册到 Capgo __CAPGO_KEEP_1__

标题:步骤 5:将包注册到 __CAPGO_KEEP_0__ __CAPGO_KEEP_1__

  • https://your-storage.com/bundles/my-app-1.2.3.zip
  • https://github.com/username/repo/releases/download/v1.2.3/bundle.zip
  • https://cdn.jsdelivr.net/gh/username/repo@v1.2.3/dist.zip

使用直接 API 调用将外部包注册到 Capgo:

Section titled “Step 5: Register Bundle with Capgo API”

Register the external bundle with Capgo using direct API calls:

async function registerWithCapgo(appId, version, bundleUrl, checksum, apiKey) {
const fetch = require('node-fetch');
// Create bundle (version)
const response = await fetch('https://api.capgo.app/bundle/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': apiKey
},
body: JSON.stringify({
app_id: appId,
version: version,
external_url: bundleUrl,
checksum: checksum
})
});
if (!response.ok) {
throw new Error(`Failed to create bundle: ${response.statusText}`);
}
const data = await response.json();
console.log('Bundle created:', data);
return data;
}
参数描述必填
app_id您的应用程序标识符
version包(版本) 语义版本 (例如,“1.2.3”)
external_url可公开访问 HTTPS URL where bundle can be downloaded (无需认证)Yes
checksumSHA256 校验和(zip 文件)Yes

Bundle 结构要求

Bundle 结构要求

您的 bundle zip 必须遵循这些要求:

  1. 根索引文件: 必须有 index.html 在根目录
  2. Capacitor 集成: 必须调用 notifyAppReady() 在您的应用程序中 code
  3. 资源路径: 使用所有资源的相对路径

有效的捆绑结构

标题:有效的捆绑结构
bundle.zip
├── index.html
├── assets/
│ ├── app.js
│ └── styles.css
└── images/

完整的手动工作流程示例

标题:完整的手动工作流程示例

一个简单的 Node.js 脚本来压缩、校验和上传到 Capgo:

const fs = require('node:fs');
const os = require('node:os');
const AdmZip = require('adm-zip');
const { checksum: getChecksum } = require('@tomasklaen/checksum');
const fetch = require('node-fetch');
async function deployToCapgo() {
const APP_ID = 'com.example.app';
const VERSION = '1.2.3';
const BUNDLE_URL = 'https://your-storage.com/bundles/app-1.2.3.zip';
const API_KEY = process.env.CAPGO_API_KEY;
// 1. Create zip (same as Capgo CLI)
const zip = new AdmZip();
zip.addLocalFolder('./dist');
const zipped = zip.toBuffer();
// 2. Calculate checksum (same as Capgo CLI)
const checksum = await getChecksum(zipped, 'sha256');
console.log('Checksum:', checksum);
// 3. Upload to your storage (replace with your upload logic)
// fs.writeFileSync('./bundle.zip', zipped);
// ... upload bundle.zip to your storage ...
// 4. Register with Capgo API
const response = await fetch('https://api.capgo.app/bundle/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': API_KEY
},
body: JSON.stringify({
app_id: APP_ID,
version: VERSION,
external_url: BUNDLE_URL,
checksum: checksum
})
});
if (!response.ok) {
throw new Error(`Failed: ${response.statusText}`);
}
console.log('Bundle registered with Capgo!');
}
deployToCapgo().catch(console.error);

安装依赖项:

终端窗口
npm install adm-zip @tomasklaen/checksum node-fetch

校验和验证

校验和验证

JavaScript 校验和计算(与 Capgo CLI 相同)

JavaScript 校验和计算(与 Capgo CLI 相同)

使用 Capgo CLI 内部使用的相同包和方法:

const fs = require('node:fs');
const { checksum: getChecksum } = require('@tomasklaen/checksum');
async function calculateChecksum(filePath) {
const fileBuffer = fs.readFileSync(filePath);
// Use exact same package and method as Capgo CLI
const checksum = await getChecksum(fileBuffer, 'sha256');
return checksum;
}
// Verify checksum matches
async function verifyChecksum(filePath, expectedChecksum) {
const actualChecksum = await calculateChecksum(filePath);
const isValid = actualChecksum === expectedChecksum;
console.log(`File: ${filePath}`);
console.log(`Expected: ${expectedChecksum}`);
console.log(`Actual: ${actualChecksum}`);
console.log(`Valid: ${isValid}`);
return isValid;
}
// Usage
async function main() {
const bundleChecksum = await calculateChecksum('./my-app-1.2.3.zip');
console.log('SHA256 Checksum:', bundleChecksum);
}
main();

校验和重要性

校验和重要性
  • 捆绑完整性: 确保捆绑在传输过程中没有被损坏
  • API 验证: Capgo 校验和验证前接受捆绑包
  • 插件验证: 移动插件在应用更新前校验和验证
  1. 捆绑包(版本)管理: 使用 语义化版本 一致
  2. 存储优化: 定期清除未使用的捆绑包
  3. 捆绑包(版本)兼容性: 必须设置适当的原生最低版本要求
  4. 备份策略: 必须备份关键的捆绑包(版本)

https://api.capgo.app/bundle/

检索捆绑包信息。每页返回 50 个捆绑包。

  • app_id: 必须。您的应用程序的 ID
  • page: 可选。分页的页码

响应类型

响应类型部分
interface Bundle {
app_id: string
bucket_id: string | null
checksum: string | null
created_at: string | null
deleted: boolean
external_url: string | null
id: number
minUpdateVersion: string | null
name: string
native_packages: Json[] | null
owner_org: string
r2_path: string | null
session_key: string | null
storage_provider: string
updated_at: string | null
user_id: string | null
}

示例请求

示例请求部分
终端窗口
# Get all bundles
curl -H "authorization: your-api-key" \
"https://api.capgo.app/bundle/?app_id=app_123"
# Get next page
curl -H "authorization: your-api-key" \
"https://api.capgo.app/bundle/?app_id=app_123&page=1"

示例响应

示例响应部分
{
"data": [
{
"id": 1,
"app_id": "app_123",
"name": "1.0.0",
"checksum": "abc123...",
"minUpdateVersion": "1.0.0",
"storage_provider": "r2",
"created_at": "2024-01-01T00:00:00Z",
"updated_at": "2024-01-01T00:00:00Z",
"deleted": false,
"owner_org": "org_123",
"user_id": "user_123"
}
]
}

https://api.capgo.app/bundle/

删除一个或所有应用程序的包。请谨慎使用,因为这个操作无法撤销。

要删除特定包:

interface BundleDelete {
app_id: string
version: string
}

要删除所有包:

interface BundleDeleteAll {
app_id: string
}
终端窗口
# Delete specific bundle
curl -X DELETE \
-H "authorization: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"app_id": "app_123",
"version": "1.0.0"
}' \
https://api.capgo.app/bundle/
# Delete all bundles
curl -X DELETE \
-H "authorization: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"app_id": "app_123"
}' \
https://api.capgo.app/bundle/
{
"status": "ok"
}

https://api.capgo.app/bundle/

创建一个包含外部 URL 的新捆绑包。

interface CreateBundleBody {
app_id: string
version: string
external_url: string // Must be publicly accessible HTTPS URL
checksum: string
}
终端窗口
curl -X POST \
-H "authorization: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"app_id": "com.example.app",
"version": "1.2.3",
"external_url": "https://your-storage.com/bundles/app-1.2.3.zip",
"checksum": "a1b2c3d4e5f6789abcdef123456789abcdef123456789abcdef123456789abcd"
}' \
https://api.capgo.app/bundle/

成功响应

成功响应部分
{
"status": "ok"
}

https://api.capgo.app/bundle/metadata

更新包元数据,如链接和评论信息。

interface UpdateMetadataBody {
app_id: string
version_id: number // bundle (version) id
link?: string
comment?: string
}

示例请求

示例请求部分
终端窗口
curl -X POST \
-H "authorization: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"app_id": "app_123",
"version_id": 456,
"link": "https://github.com/myorg/myapp/releases/tag/v1.0.0",
"comment": "Fixed critical bug in authentication"
}' \
https://api.capgo.app/bundle/metadata
{
"status": "success"
}

https://api.capgo.app/bundle/

将一个包设置到一个特定的频道。这个操作将一个包(版本)与一个频道关联起来,用于分发。

interface SetChannelBody {
app_id: string
version_id: number // bundle (version) id
channel_id: number
}
终端窗口
curl -X PUT \
-H "authorization: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"app_id": "app_123",
"version_id": 456,
"channel_id": 789
}' \
https://api.capgo.app/bundle/
{
"status": "success",
"message": "Bundle 1.0.0 set to channel production"
}

常见错误场景和响应:

// Bundle not found
{
"error": "Bundle not found",
"status": "KO"
}
// Invalid bundle (version) format
{
"error": "Invalid version format",
"status": "KO"
}
// Storage error
{
"error": "Failed to delete bundle from storage",
"status": "KO"
}
// Permission denied
{
"error": "Insufficient permissions to manage bundles",
"status": "KO"
}
  1. 清除旧版本打包物
// Delete outdated beta bundles (versions)
{
"app_id": "app_123",
"version": "1.0.0-beta.1"
}
  1. 应用重置
// Remove all bundles to start fresh
{
"app_id": "app_123"
}

存储考虑

存储考虑
  1. 保留策略: 定义保留旧包的时间
  2. 大小管理: 监控包大小和存储使用
  3. 备份策略: 考虑备份关键包(版本)
  4. 成本优化: 优化存储成本,去掉不必要的包

如果您正在使用 Bundles 来规划存储和文件处理,连接它到 @capgo/capacitor-data-storage-sqlite 在 @capgo/capacitor-data-storage-sqlite 中了解实现细节 使用 @capgo/capacitor-data-storage-sqlite 为原生能力在使用 @capgo/capacitor-data-storage-sqlite 中 @capgo/capacitor-file 在 @capgo/capacitor-file 中了解实现细节 使用 @capgo/capacitor-file 为原生能力在使用 @capgo/capacitor-file, 和 @capgo/capacitor-uploader 为实现细节在 @capgo/capacitor-uploader。