11/**
22 * Uploader Module
33 *
4- * Exports pure functions (core) and I/O operations for screenshot uploading.
4+ * Exports pure functions (core), I/O operations, and the public uploader
5+ * factory for screenshot uploading.
56 */
67
8+ import { readFile , stat } from 'node:fs/promises' ;
9+ import { glob } from 'glob' ;
10+ import { checkShas , createApiClient , createBuild } from '../api/index.js' ;
11+ import {
12+ TimeoutError ,
13+ UploadError ,
14+ ValidationError ,
15+ } from '../errors/vizzly-error.js' ;
16+ import { getDefaultBranch } from '../utils/git.js' ;
17+ import * as output from '../utils/output.js' ;
18+ import { resolveBatchSize , resolveTimeout } from './core.js' ;
19+ import {
20+ upload as uploadOperation ,
21+ waitForBuild as waitForBuildOperation ,
22+ } from './operations.js' ;
23+
24+ export function createUploader (
25+ { apiKey, apiUrl, userAgent, command, upload : uploadConfig = { } } = { } ,
26+ options = { }
27+ ) {
28+ let signal = options . signal || new AbortController ( ) . signal ;
29+ let client = createApiClient ( {
30+ baseUrl : apiUrl ,
31+ token : apiKey ,
32+ command : command || 'upload' ,
33+ sdkUserAgent : userAgent ,
34+ allowNoToken : true ,
35+ } ) ;
36+
37+ let batchSize = resolveBatchSize ( options , uploadConfig ) ;
38+ let timeout = resolveTimeout ( options , uploadConfig ) ;
39+ let deps = options . deps || {
40+ client,
41+ createBuild,
42+ getDefaultBranch,
43+ glob,
44+ readFile,
45+ stat,
46+ checkShas,
47+ createError : ( message , code , context ) => {
48+ let error = new UploadError ( message , context ) ;
49+ error . code = code ;
50+ return error ;
51+ } ,
52+ createValidationError : ( message , context ) =>
53+ new ValidationError ( message , context ) ,
54+ createUploadError : ( message , context ) => new UploadError ( message , context ) ,
55+ createTimeoutError : ( message , context ) =>
56+ new TimeoutError ( message , context ) ,
57+ output,
58+ } ;
59+
60+ async function upload ( uploadOptions ) {
61+ return uploadOperation ( {
62+ uploadOptions,
63+ config : { apiKey, apiUrl } ,
64+ signal,
65+ batchSize,
66+ deps : {
67+ ...deps ,
68+ client : deps . client || client ,
69+ } ,
70+ } ) ;
71+ }
72+
73+ async function waitForBuild ( buildId , waitTimeout = timeout ) {
74+ return waitForBuildOperation ( {
75+ buildId,
76+ timeout : waitTimeout ,
77+ signal,
78+ client : deps . client || client ,
79+ deps : {
80+ createError : deps . createError ,
81+ createTimeoutError : deps . createTimeoutError ,
82+ } ,
83+ } ) ;
84+ }
85+
86+ return { upload, waitForBuild } ;
87+ }
88+
789// Core - pure functions
890export {
991 buildBuildInfo ,
@@ -18,6 +100,7 @@ export {
18100 buildWaitResult ,
19101 computeSha256 ,
20102 DEFAULT_BATCH_SIZE ,
103+ DEFAULT_BUILD_POLL_INTERVAL ,
21104 DEFAULT_SHA_CHECK_BATCH_SIZE ,
22105 DEFAULT_TIMEOUT ,
23106 extractBrowserFromFilename ,
0 commit comments