Line data Source code
1 : import type { z } from 'zod'
2 :
3 : import type {
4 : zCompleteModelUploadBody,
5 : zReportModelUploadEventBody,
6 : zStartModelUploadBody
7 : } from '@comfyorg/ingest-types/zod'
8 : import {
9 : zCompleteModelUploadResponse,
10 : zErrorResponse,
11 : zStartModelUploadResponse
12 : } from '@comfyorg/ingest-types/zod'
13 :
14 : import { api } from '@/scripts/api'
15 :
16 2355 : const UPLOADS_ENDPOINT = '/assets/uploads'
17 2355 : const STORAGE_OBJECT_ALREADY_WRITTEN = 412
18 2355 : const UPLOAD_CLIENT = 'editor'
19 2355 : const TRANSFER_REPORT_TIMEOUT_MS = 5_000
20 :
21 : type ModelFileUploadFailure =
22 : | 'unsupportedFile'
23 : | 'unsupportedType'
24 : | 'tooLarge'
25 : | 'privateModelsDisabled'
26 : | 'planUnavailable'
27 : | 'sessionExpired'
28 : | 'cancelled'
29 : | 'failed'
30 :
31 2355 : const FAILURE_BY_CODE: Partial<Record<string, ModelFileUploadFailure>> = {
32 : UNSUPPORTED_FILE_TYPE: 'unsupportedFile',
33 : UNSUPPORTED_MODEL_TYPE: 'unsupportedType',
34 : PRIVATE_MODELS_DISABLED: 'privateModelsDisabled',
35 : PRIVATE_MODELS_UNAVAILABLE: 'planUnavailable'
36 : }
37 :
38 : interface ModelFileUploadStopped {
39 : ok: false
40 : reason: ModelFileUploadFailure
41 : message?: string
42 : cause?: unknown
43 : }
44 :
45 : /** The outcome of sending one model file: the server task that stores it, or why it stopped. */
46 : export type ModelFileUploadResult =
47 : | { ok: true; taskId: string }
48 : | ModelFileUploadStopped
49 :
50 : interface ModelFileUploadOptions {
51 : tags: string[]
52 : signal: AbortSignal
53 : onProgress: (sentBytes: number) => void
54 : onReconnecting?: (reconnecting: boolean) => void
55 : }
56 :
57 0 : async function readFailure(res: Response): Promise<ModelFileUploadStopped> {
58 0 : const body = zErrorResponse.safeParse(await res.json().catch(() => null))
59 0 : const code = body.success ? body.data.code : undefined
60 0 : const reason = code ? FAILURE_BY_CODE[code] : undefined
61 0 : if (reason) return { ok: false, reason }
62 0 : if (code === 'FILE_TOO_LARGE' && body.success) {
63 0 : return { ok: false, reason: 'tooLarge', message: body.data.message }
64 : }
65 0 : return { ok: false, reason: 'failed', cause: `${res.status} ${code ?? ''}` }
66 : }
67 :
68 : interface StorageAnswer {
69 : status: number
70 : range: string | null
71 : }
72 :
73 0 : function putFile(
74 : start: Pick<
75 : ReturnType<typeof zStartModelUploadResponse.parse>,
76 : 'method' | 'upload_url' | 'headers'
77 : >,
78 : file: Blob | null,
79 : { signal, onProgress }: ModelFileUploadOptions,
80 : contentRange?: string
81 : ): Promise<StorageAnswer | ModelFileUploadStopped> {
82 0 : return new Promise((resolve) => {
83 0 : if (signal.aborted) {
84 0 : resolve({ ok: false, reason: 'cancelled' })
85 0 : return
86 : }
87 0 : const request = new XMLHttpRequest()
88 0 : let timer: ReturnType<typeof setTimeout> | undefined
89 0 : let loaded = 0
90 0 : const resetDeadline = () => {
91 0 : if (!contentRange) return
92 0 : clearTimeout(timer)
93 0 : timer = setTimeout(() => {
94 0 : resolve({ status: 0, range: null })
95 0 : request.abort()
96 : }, 30_000)
97 : }
98 0 : request.upload.onprogress = (event) => {
99 0 : if (event.loaded > loaded) {
100 0 : loaded = event.loaded
101 0 : resetDeadline()
102 : }
103 0 : if (file) onProgress(event.loaded)
104 : }
105 0 : request.onload = () =>
106 : resolve({
107 : status: request.status,
108 : range: request.getResponseHeader('Range')
109 : })
110 0 : request.onerror = request.ontimeout = () =>
111 : resolve({ status: 0, range: null })
112 0 : request.onabort = () => resolve({ ok: false, reason: 'cancelled' })
113 0 : const abort = () => request.abort()
114 0 : const cleanup = () => {
115 0 : clearTimeout(timer)
116 0 : signal.removeEventListener('abort', abort)
117 : }
118 0 : request.onloadend = cleanup
119 0 : signal.addEventListener('abort', abort, { once: true })
120 0 : try {
121 0 : request.open(start.method, start.upload_url)
122 0 : for (const [name, value] of Object.entries(start.headers)) {
123 0 : request.setRequestHeader(name, value)
124 : }
125 0 : if (contentRange) request.setRequestHeader('Content-Range', contentRange)
126 0 : resetDeadline()
127 0 : request.send(file)
128 : } catch {
129 0 : cleanup()
130 0 : resolve({ ok: false, reason: 'failed', cause: 'storage request failed' })
131 : }
132 : })
133 : }
134 :
135 0 : function readStorageAnswer(status: number): ModelFileUploadStopped | undefined {
136 0 : if (status >= 200 && status < 300) return undefined
137 0 : if (status === STORAGE_OBJECT_ALREADY_WRITTEN) return undefined
138 0 : return { ok: false, reason: 'failed', cause: `storage ${status}` }
139 : }
140 :
141 0 : function retryWait(delay: number, signal: AbortSignal): Promise<boolean> {
142 0 : if (signal.aborted) return Promise.resolve(false)
143 0 : return new Promise((resolve) => {
144 0 : const finish = () => {
145 0 : clearTimeout(timer)
146 0 : signal.removeEventListener('abort', finish)
147 0 : resolve(!signal.aborted)
148 : }
149 0 : const timer = setTimeout(finish, delay)
150 0 : signal.addEventListener('abort', finish, { once: true })
151 : })
152 : }
153 :
154 0 : function heldBytes(range: string | null, size: number): number | undefined {
155 0 : if (range === null) return 0
156 0 : const match = /^bytes=0-(\d+)$/.exec(range)
157 0 : const next = match ? Number(match[1]) + 1 : NaN
158 0 : return Number.isSafeInteger(next) && next > 0 && next <= size
159 : ? next
160 : : undefined
161 : }
162 :
163 : type ResumableAnswer =
164 : | { status: 'complete' | 'retry' }
165 : | { status: 'held'; bytes: number }
166 : | ModelFileUploadStopped
167 :
168 0 : function readResumableAnswer(
169 : answer: StorageAnswer,
170 : size: number
171 : ): ResumableAnswer {
172 0 : const { status, range } = answer
173 0 : if ([200, 201, 412].includes(status)) return { status: 'complete' }
174 0 : if (status === 404 || status === 410) {
175 0 : return { ok: false, reason: 'sessionExpired' }
176 : }
177 0 : if (status === 308) {
178 0 : const bytes = heldBytes(range, size)
179 0 : return bytes === undefined
180 : ? { ok: false, reason: 'failed', cause: 'invalid storage range' }
181 : : { status: 'held', bytes }
182 : }
183 0 : if ([0, 408, 429].includes(status) || status >= 500)
184 0 : return { status: 'retry' }
185 0 : return { ok: false, reason: 'failed', cause: `storage ${status}` }
186 : }
187 :
188 : interface TransferPosition {
189 : offset: number
190 : query: boolean
191 : delay: number
192 : resumed: boolean
193 : }
194 :
195 0 : async function sendPiece(
196 : start: ReturnType<typeof zStartModelUploadResponse.parse>,
197 : file: File,
198 : options: ModelFileUploadOptions,
199 : { offset, query }: TransferPosition
200 : ): Promise<ResumableAnswer> {
201 0 : const chunkSize = Number(start.chunk_size_bytes ?? 64 * 1024 * 1024)
202 0 : if (!Number.isSafeInteger(chunkSize) || chunkSize <= 0) {
203 0 : return { ok: false, reason: 'failed', cause: 'invalid upload piece size' }
204 : }
205 0 : const end = Math.min(offset + chunkSize, file.size)
206 0 : if (!query) options.onReconnecting?.(false)
207 0 : const answer = await putFile(
208 : start,
209 : query ? null : file.slice(offset, end),
210 : { ...options, onProgress: (sent) => options.onProgress(offset + sent) },
211 : query ? `bytes */${file.size}` : `bytes ${offset}-${end - 1}/${file.size}`
212 : )
213 0 : return 'status' in answer ? readResumableAnswer(answer, file.size) : answer
214 : }
215 :
216 0 : function nextPosition(
217 : previous: TransferPosition,
218 : next: number,
219 : size: number
220 : ): TransferPosition {
221 0 : return {
222 : offset: next,
223 : query: next >= size || (!previous.query && next <= previous.offset),
224 : delay: next > previous.offset ? 1000 : previous.delay,
225 : resumed: previous.resumed || (previous.query && next > 0 && next < size)
226 : }
227 : }
228 :
229 : interface Transfer {
230 : stopped?: ModelFileUploadStopped
231 : resumed?: boolean
232 : }
233 :
234 0 : async function putResumable(
235 : start: ReturnType<typeof zStartModelUploadResponse.parse>,
236 : file: File,
237 : options: ModelFileUploadOptions
238 : ): Promise<Transfer> {
239 0 : let position: TransferPosition = {
240 : offset: 0,
241 : query: false,
242 : delay: 1000,
243 : resumed: false
244 : }
245 0 : while (!options.signal.aborted) {
246 0 : const outcome = await sendPiece(start, file, options, position)
247 0 : if ('ok' in outcome) return { stopped: outcome, resumed: position.resumed }
248 0 : if (outcome.status === 'complete') {
249 0 : options.onProgress(file.size)
250 0 : options.onReconnecting?.(false)
251 0 : return { resumed: position.resumed }
252 : }
253 0 : if (outcome.status === 'held') {
254 0 : position = nextPosition(position, outcome.bytes, file.size)
255 0 : options.onProgress(position.offset)
256 0 : if (!position.query) continue
257 : }
258 0 : options.onReconnecting?.(true)
259 0 : if (!(await retryWait(position.delay, options.signal))) break
260 0 : position = {
261 : ...position,
262 : query: true,
263 : delay: Math.min(position.delay * 2, 60_000)
264 : }
265 : }
266 0 : return {
267 : stopped: { ok: false, reason: 'cancelled' },
268 : resumed: position.resumed
269 : }
270 : }
271 :
272 0 : async function putOnce(
273 : start: ReturnType<typeof zStartModelUploadResponse.parse>,
274 : file: File,
275 : options: ModelFileUploadOptions
276 : ): Promise<Transfer> {
277 0 : const answer = await putFile(start, file, options)
278 0 : return {
279 : stopped: 'status' in answer ? readStorageAnswer(answer.status) : answer
280 : }
281 : }
282 :
283 0 : async function postJson(route: string, body: unknown, signal: AbortSignal) {
284 0 : return api.fetchApi(route, {
285 : method: 'POST',
286 : headers: { 'Content-Type': 'application/json' },
287 : body: JSON.stringify(body),
288 : signal
289 : })
290 : }
291 :
292 0 : async function startFileUpload(
293 : file: File,
294 : options: ModelFileUploadOptions
295 : ): Promise<Response> {
296 0 : const body = {
297 : filename: file.name,
298 : size_bytes: file.size,
299 : resumable: true,
300 : tags: options.tags,
301 : upload_analytics: { client: UPLOAD_CLIENT }
302 : } satisfies z.input<typeof zStartModelUploadBody>
303 0 : const response = await postJson(UPLOADS_ENDPOINT, body, options.signal)
304 0 : if (response.status !== 403) return response
305 0 : const refusal = zErrorResponse.safeParse(
306 : await response
307 : .clone()
308 : .json()
309 : .catch(() => null)
310 : )
311 0 : if (!refusal.success || refusal.data.code !== 'FEATURE_DISABLED')
312 0 : return response
313 0 : return postJson(
314 : UPLOADS_ENDPOINT,
315 : { ...body, resumable: false },
316 : options.signal
317 : )
318 : }
319 :
320 : type TransferReport = z.input<typeof zReportModelUploadEventBody>
321 :
322 0 : function transferReason({
323 : reason,
324 : cause
325 : }: ModelFileUploadStopped): TransferReport['reason'] {
326 0 : if (reason === 'cancelled') return 'canceled'
327 0 : if (reason === 'sessionExpired') return 'session_expired'
328 0 : const status = typeof cause === 'string' && /^storage (\d+)$/.exec(cause)
329 0 : if (!status) return 'unknown'
330 0 : return status[1] === '0' ? 'network' : 'http'
331 : }
332 :
333 0 : function reportTransferStop(uploadId: string, report: TransferReport): void {
334 0 : const controller = new AbortController()
335 0 : const timer = setTimeout(() => controller.abort(), TRANSFER_REPORT_TIMEOUT_MS)
336 0 : void postJson(
337 : `${UPLOADS_ENDPOINT}/${encodeURIComponent(uploadId)}/events`,
338 : report,
339 : controller.signal
340 : )
341 : .catch(() => undefined)
342 : .finally(() => clearTimeout(timer))
343 : }
344 :
345 : interface StagedFile {
346 : uploadId: string
347 : resumed?: boolean
348 : }
349 :
350 0 : async function stageFile(
351 : file: File,
352 : options: ModelFileUploadOptions
353 : ): Promise<StagedFile | ModelFileUploadStopped> {
354 0 : const startRes = await startFileUpload(file, options)
355 0 : if (!startRes.ok) return await readFailure(startRes)
356 0 : const start = zStartModelUploadResponse.safeParse(await startRes.json())
357 0 : if (!start.success) {
358 0 : return { ok: false, reason: 'failed', cause: start.error }
359 : }
360 0 : const uploadId = start.data.upload_id
361 0 : const { stopped, resumed } =
362 : start.data.upload_mode === 'resumable'
363 : ? await putResumable(start.data, file, options)
364 : : await putOnce(start.data, file, options)
365 0 : if (!stopped) return { uploadId, resumed }
366 0 : if (start.data.upload_analytics_version === 1) {
367 0 : reportTransferStop(uploadId, {
368 : client: UPLOAD_CLIENT,
369 : size_bytes: file.size,
370 : model_folder: options.tags.find((tag) => tag !== 'models') ?? '',
371 : reason: transferReason(stopped),
372 : resumed
373 : })
374 : }
375 0 : return stopped
376 : }
377 :
378 0 : async function uploadModelFile(
379 : file: File,
380 : options: ModelFileUploadOptions
381 : ): Promise<ModelFileUploadResult> {
382 0 : const { tags, signal } = options
383 0 : try {
384 0 : const staged = await stageFile(file, options)
385 0 : if ('ok' in staged) return staged
386 :
387 0 : const completeRes = await postJson(
388 : `${UPLOADS_ENDPOINT}/${encodeURIComponent(staged.uploadId)}/complete`,
389 : {
390 : filename: file.name,
391 : tags,
392 : upload_analytics: { client: UPLOAD_CLIENT, resumed: staged.resumed }
393 : } satisfies z.input<typeof zCompleteModelUploadBody>,
394 : signal
395 : )
396 0 : if (!completeRes.ok) return await readFailure(completeRes)
397 0 : const task = zCompleteModelUploadResponse.safeParse(
398 : await completeRes.json()
399 : )
400 0 : if (!task.success) {
401 0 : return { ok: false, reason: 'failed', cause: task.error }
402 : }
403 0 : return { ok: true, taskId: task.data.task_id }
404 : } catch (cause) {
405 0 : return signal.aborted
406 : ? { ok: false, reason: 'cancelled' }
407 : : { ok: false, reason: 'failed', cause }
408 : }
409 : }
410 :
411 : /** Sends a model file from the computer into the workspace library; never throws. */
412 2355 : export const modelFileUploadApi = { uploadModelFile }
|