mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(ocm-cli): chunked mirror upload and macOS metadata exclusion (#238)
This commit is contained in:
1 parent
0597bf6a37
commit
04ab63932b
8 files changed
+801
-344
No files matched your search
+92
-25
@@ -1,3 +1,56 @@
|
||||
export interface MirrorBeginOpts {
|
||||
force?: boolean
|
||||
create?: { name: string; originUrl: string | null; branch: string | null }
|
||||
}
|
||||
|
||||
export interface MirrorBeginResult {
|
||||
uploadId: string
|
||||
repoId: number
|
||||
chunkSize: number
|
||||
created: boolean
|
||||
}
|
||||
|
||||
export interface MirrorCommitResult {
|
||||
repoId: number
|
||||
fullPath: string
|
||||
branch: string | null
|
||||
head: string | null
|
||||
created: boolean
|
||||
}
|
||||
|
||||
export class ManagerApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number,
|
||||
public readonly code: string | null,
|
||||
public readonly operation: string,
|
||||
) {
|
||||
super(message)
|
||||
this.name = 'ManagerApiError'
|
||||
}
|
||||
}
|
||||
|
||||
async function formatErrorResponse(res: Response, operation: string): Promise<ManagerApiError> {
|
||||
const text = await res.text().catch(() => '')
|
||||
let code: string | null = null
|
||||
let detail = text
|
||||
if (text) {
|
||||
try {
|
||||
const parsed = JSON.parse(text) as { error?: unknown; message?: unknown }
|
||||
const errField = typeof parsed.error === 'string' ? parsed.error : null
|
||||
const msgField = typeof parsed.message === 'string' ? parsed.message : null
|
||||
code = errField
|
||||
detail = msgField ?? errField ?? text
|
||||
} catch {
|
||||
/* not JSON, keep raw text */
|
||||
}
|
||||
}
|
||||
const message = detail
|
||||
? `${operation} failed (${res.status}): ${detail}`
|
||||
: `${operation} failed (${res.status})`
|
||||
return new ManagerApiError(message, res.status, code, operation)
|
||||
}
|
||||
|
||||
export class ManagerApi {
|
||||
constructor(
|
||||
private baseUrl: string,
|
||||
@@ -8,35 +61,49 @@ export class ManagerApi {
|
||||
return { Authorization: `Bearer ${this.token}`, ...extra }
|
||||
}
|
||||
|
||||
async mirrorUp(
|
||||
repoId: number,
|
||||
body: ReadableStream<Uint8Array>,
|
||||
opts: { force?: boolean; create?: { name: string; originUrl: string | null; branch: string | null } },
|
||||
): Promise<{ repoId: number; branch: string; head: string; created: boolean }> {
|
||||
const params = new URLSearchParams()
|
||||
if (opts.force) params.set('force', '1')
|
||||
async mirrorBegin(repoId: number, opts: MirrorBeginOpts): Promise<MirrorBeginResult> {
|
||||
const url = `${this.baseUrl}/api/internal/repos/${repoId}/mirror/begin`
|
||||
const body: Record<string, unknown> = { force: opts.force === true }
|
||||
if (opts.create) {
|
||||
params.set('create', '1')
|
||||
params.set('name', opts.create.name)
|
||||
if (opts.create.originUrl) params.set('originUrl', opts.create.originUrl)
|
||||
if (opts.create.branch) params.set('branch', opts.create.branch)
|
||||
body.create = true
|
||||
body.name = opts.create.name
|
||||
if (opts.create.originUrl) body.originUrl = opts.create.originUrl
|
||||
if (opts.create.branch) body.branch = opts.create.branch
|
||||
}
|
||||
|
||||
const qs = params.toString() ? `?${params.toString()}` : ''
|
||||
const url = `${this.baseUrl}/api/internal/repos/${repoId}/mirror${qs}`
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...this.headers(),
|
||||
'Content-Type': 'application/x-tar',
|
||||
},
|
||||
body,
|
||||
duplex: 'half',
|
||||
} as RequestInit & { duplex: 'half' })
|
||||
headers: { ...this.headers(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
if (!res.ok) throw await formatErrorResponse(res, 'mirror begin')
|
||||
return (await res.json()) as MirrorBeginResult
|
||||
}
|
||||
|
||||
if (!res.ok) throw new Error(`mirror ${res.status}: ${await res.text()}`)
|
||||
return (await res.json()) as { repoId: number; branch: string; head: string; created: boolean }
|
||||
async mirrorUploadPart(repoId: number, uploadId: string, index: number, chunk: Buffer): Promise<void> {
|
||||
const url = `${this.baseUrl}/api/internal/repos/${repoId}/mirror/parts/${uploadId}/${index}`
|
||||
const ab = chunk.buffer.slice(chunk.byteOffset, chunk.byteOffset + chunk.byteLength)
|
||||
const res = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: { ...this.headers(), 'Content-Type': 'application/octet-stream' },
|
||||
body: ab as ArrayBuffer,
|
||||
})
|
||||
if (!res.ok) throw await formatErrorResponse(res, `mirror part ${index}`)
|
||||
}
|
||||
|
||||
async mirrorCommit(repoId: number, uploadId: string, totalParts: number): Promise<MirrorCommitResult> {
|
||||
const url = `${this.baseUrl}/api/internal/repos/${repoId}/mirror/commit`
|
||||
const res = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: { ...this.headers(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ uploadId, totalParts }),
|
||||
})
|
||||
if (!res.ok) throw await formatErrorResponse(res, 'mirror commit')
|
||||
return (await res.json()) as MirrorCommitResult
|
||||
}
|
||||
|
||||
async mirrorAbort(repoId: number, uploadId: string): Promise<void> {
|
||||
const url = `${this.baseUrl}/api/internal/repos/${repoId}/mirror/uploads/${uploadId}`
|
||||
await fetch(url, { method: 'DELETE', headers: this.headers() }).catch(() => { /* best-effort */ })
|
||||
}
|
||||
|
||||
async mirrorDown(repoId: number): Promise<ReadableStream<Uint8Array>> {
|
||||
@@ -44,7 +111,7 @@ export class ManagerApi {
|
||||
headers: this.headers(),
|
||||
})
|
||||
|
||||
if (!res.ok) throw new Error(`mirror ${res.status}: ${await res.text()}`)
|
||||
if (!res.ok) throw await formatErrorResponse(res, 'mirror download')
|
||||
return res.body!
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user