@arke-institute/sdk 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,35 +1,630 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
2
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
3
+ var __esm = (fn, res) => function __init() {
4
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
5
+ };
6
6
  var __export = (target, all) => {
7
7
  for (var name in all)
8
8
  __defProp(target, name, { get: all[name], enumerable: true });
9
9
  };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+
11
+ // src/upload/utils/errors.ts
12
+ function isRetryableError(error) {
13
+ if (error instanceof NetworkError) {
14
+ return true;
15
15
  }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ if (error instanceof WorkerAPIError) {
17
+ return error.statusCode ? error.statusCode >= 500 : false;
18
+ }
19
+ if (error instanceof UploadError) {
20
+ if (error.statusCode) {
21
+ return error.statusCode >= 500 || error.statusCode === 429;
22
+ }
23
+ return false;
24
+ }
25
+ if (error.code === "ECONNRESET" || error.code === "ETIMEDOUT" || error.code === "ENOTFOUND" || error.code === "ECONNREFUSED") {
26
+ return true;
27
+ }
28
+ return false;
29
+ }
30
+ var WorkerAPIError, UploadError, ValidationError, NetworkError, ScanError;
31
+ var init_errors = __esm({
32
+ "src/upload/utils/errors.ts"() {
33
+ "use strict";
34
+ WorkerAPIError = class extends Error {
35
+ constructor(message, statusCode, details) {
36
+ super(message);
37
+ this.statusCode = statusCode;
38
+ this.details = details;
39
+ this.name = "WorkerAPIError";
40
+ Error.captureStackTrace(this, this.constructor);
41
+ }
42
+ };
43
+ UploadError = class extends Error {
44
+ constructor(message, fileName, statusCode, cause) {
45
+ super(message);
46
+ this.fileName = fileName;
47
+ this.statusCode = statusCode;
48
+ this.cause = cause;
49
+ this.name = "UploadError";
50
+ Error.captureStackTrace(this, this.constructor);
51
+ }
52
+ };
53
+ ValidationError = class extends Error {
54
+ constructor(message, field) {
55
+ super(message);
56
+ this.field = field;
57
+ this.name = "ValidationError";
58
+ Error.captureStackTrace(this, this.constructor);
59
+ }
60
+ };
61
+ NetworkError = class extends Error {
62
+ constructor(message, cause) {
63
+ super(message);
64
+ this.cause = cause;
65
+ this.name = "NetworkError";
66
+ Error.captureStackTrace(this, this.constructor);
67
+ }
68
+ };
69
+ ScanError = class extends Error {
70
+ constructor(message, path2) {
71
+ super(message);
72
+ this.path = path2;
73
+ this.name = "ScanError";
74
+ Error.captureStackTrace(this, this.constructor);
75
+ }
76
+ };
77
+ }
78
+ });
19
79
 
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- CollectionsClient: () => CollectionsClient,
24
- CollectionsError: () => CollectionsError
80
+ // src/upload/platforms/common.ts
81
+ function detectPlatform() {
82
+ if (typeof process !== "undefined" && process.versions != null && process.versions.node != null) {
83
+ return "node";
84
+ }
85
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
86
+ return "browser";
87
+ }
88
+ return "unknown";
89
+ }
90
+ function normalizePath(p) {
91
+ return p.replace(/\\/g, "/");
92
+ }
93
+ function getExtension(filename) {
94
+ const lastDot = filename.lastIndexOf(".");
95
+ return lastDot === -1 ? "" : filename.slice(lastDot + 1).toLowerCase();
96
+ }
97
+ function getMimeType(filename) {
98
+ const ext = getExtension(filename);
99
+ const mimeTypes = {
100
+ // Images
101
+ "jpg": "image/jpeg",
102
+ "jpeg": "image/jpeg",
103
+ "png": "image/png",
104
+ "gif": "image/gif",
105
+ "webp": "image/webp",
106
+ "tif": "image/tiff",
107
+ "tiff": "image/tiff",
108
+ "bmp": "image/bmp",
109
+ "svg": "image/svg+xml",
110
+ // Documents
111
+ "pdf": "application/pdf",
112
+ "txt": "text/plain",
113
+ "json": "application/json",
114
+ "xml": "application/xml",
115
+ "html": "text/html",
116
+ "htm": "text/html",
117
+ "css": "text/css",
118
+ "js": "application/javascript",
119
+ // Archives
120
+ "zip": "application/zip",
121
+ "tar": "application/x-tar",
122
+ "gz": "application/gzip",
123
+ // Audio
124
+ "mp3": "audio/mpeg",
125
+ "wav": "audio/wav",
126
+ "ogg": "audio/ogg",
127
+ // Video
128
+ "mp4": "video/mp4",
129
+ "webm": "video/webm",
130
+ "mov": "video/quicktime"
131
+ };
132
+ return mimeTypes[ext] || "application/octet-stream";
133
+ }
134
+ var init_common = __esm({
135
+ "src/upload/platforms/common.ts"() {
136
+ "use strict";
137
+ }
138
+ });
139
+
140
+ // src/upload/lib/validation.ts
141
+ function validateFileSize(size) {
142
+ if (size <= 0) {
143
+ throw new ValidationError("File size must be greater than 0");
144
+ }
145
+ if (size > MAX_FILE_SIZE) {
146
+ throw new ValidationError(
147
+ `File size (${formatBytes(size)}) exceeds maximum allowed size (${formatBytes(MAX_FILE_SIZE)})`
148
+ );
149
+ }
150
+ }
151
+ function validateBatchSize(totalSize) {
152
+ if (totalSize > MAX_BATCH_SIZE) {
153
+ throw new ValidationError(
154
+ `Total batch size (${formatBytes(totalSize)}) exceeds maximum allowed size (${formatBytes(MAX_BATCH_SIZE)})`
155
+ );
156
+ }
157
+ }
158
+ function validateLogicalPath(path2) {
159
+ if (!path2.startsWith("/")) {
160
+ throw new ValidationError("Logical path must start with /", "path");
161
+ }
162
+ if (INVALID_PATH_CHARS.test(path2)) {
163
+ throw new ValidationError(
164
+ "Logical path contains invalid characters",
165
+ "path"
166
+ );
167
+ }
168
+ const segments = path2.split("/").filter((s) => s.length > 0);
169
+ if (segments.length === 0 && path2 !== "/") {
170
+ throw new ValidationError("Logical path cannot be empty", "path");
171
+ }
172
+ for (const segment of segments) {
173
+ if (segment === "." || segment === "..") {
174
+ throw new ValidationError(
175
+ "Logical path cannot contain . or .. segments",
176
+ "path"
177
+ );
178
+ }
179
+ }
180
+ }
181
+ function validateRefJson(content, fileName, logger) {
182
+ let parsed;
183
+ try {
184
+ parsed = JSON.parse(content);
185
+ } catch (error) {
186
+ throw new ValidationError(
187
+ `Invalid JSON in ${fileName}: ${error.message}`,
188
+ "ref"
189
+ );
190
+ }
191
+ if (typeof parsed !== "object" || Array.isArray(parsed) || parsed === null) {
192
+ throw new ValidationError(
193
+ `${fileName} must contain a JSON object`,
194
+ "ref"
195
+ );
196
+ }
197
+ if (!parsed.url || typeof parsed.url !== "string") {
198
+ throw new ValidationError(
199
+ `${fileName} must contain a 'url' field with a string value`,
200
+ "ref"
201
+ );
202
+ }
203
+ try {
204
+ const url = new URL(parsed.url);
205
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
206
+ throw new Error("URL must use HTTP or HTTPS protocol");
207
+ }
208
+ } catch (error) {
209
+ throw new ValidationError(
210
+ `Invalid URL in ${fileName}: ${error.message}`,
211
+ "ref"
212
+ );
213
+ }
214
+ if (!parsed.type) {
215
+ if (logger) {
216
+ logger.warn(`${fileName}: Missing 'type' field (optional but recommended)`);
217
+ }
218
+ }
219
+ if (parsed.type && OCR_PROCESSABLE_TYPES.includes(parsed.type)) {
220
+ const typeToExt = {
221
+ "image/jpeg": ".jpg",
222
+ "image/png": ".png",
223
+ "image/webp": ".webp"
224
+ };
225
+ const expectedExt = typeToExt[parsed.type];
226
+ if (expectedExt && !fileName.includes(`${expectedExt}.ref.json`)) {
227
+ if (logger) {
228
+ logger.warn(
229
+ `${fileName}: Type is '${parsed.type}' but filename doesn't include '${expectedExt}.ref.json' pattern. This file may not be processed by OCR. Consider renaming to include the extension (e.g., 'photo${expectedExt}.ref.json').`
230
+ );
231
+ }
232
+ }
233
+ }
234
+ }
235
+ function formatBytes(bytes) {
236
+ if (bytes === 0) return "0 B";
237
+ const k = 1024;
238
+ const sizes = ["B", "KB", "MB", "GB", "TB"];
239
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
240
+ return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
241
+ }
242
+ function validateCustomPrompts(prompts) {
243
+ if (!prompts) return;
244
+ const MAX_LENGTH = 5e4;
245
+ const MAX_TOTAL_LENGTH = 75e3;
246
+ const fields = [
247
+ "general",
248
+ "reorganization",
249
+ "pinax",
250
+ "description",
251
+ "cheimarros"
252
+ ];
253
+ let totalLength = 0;
254
+ for (const field of fields) {
255
+ const value = prompts[field];
256
+ if (value) {
257
+ if (value.length > MAX_LENGTH) {
258
+ throw new ValidationError(
259
+ `Custom prompt '${field}' exceeds maximum length of ${MAX_LENGTH} characters (current: ${value.length})`,
260
+ "customPrompts"
261
+ );
262
+ }
263
+ totalLength += value.length;
264
+ }
265
+ }
266
+ if (totalLength > MAX_TOTAL_LENGTH) {
267
+ throw new ValidationError(
268
+ `Total custom prompts length (${totalLength}) exceeds maximum of ${MAX_TOTAL_LENGTH} characters`,
269
+ "customPrompts"
270
+ );
271
+ }
272
+ }
273
+ function validateCustomPromptsLocation(processingConfig) {
274
+ if (!processingConfig) return;
275
+ if ("customPrompts" in processingConfig) {
276
+ throw new ValidationError(
277
+ "customPrompts must be a top-level field in UploaderConfig, not inside the processing config. Use: new ArkeUploader({ customPrompts: {...}, processing: {...} }) NOT: new ArkeUploader({ processing: { customPrompts: {...} } })",
278
+ "processing"
279
+ );
280
+ }
281
+ }
282
+ var MAX_FILE_SIZE, MAX_BATCH_SIZE, INVALID_PATH_CHARS, OCR_PROCESSABLE_TYPES;
283
+ var init_validation = __esm({
284
+ "src/upload/lib/validation.ts"() {
285
+ "use strict";
286
+ init_errors();
287
+ MAX_FILE_SIZE = 5 * 1024 * 1024 * 1024;
288
+ MAX_BATCH_SIZE = 100 * 1024 * 1024 * 1024;
289
+ INVALID_PATH_CHARS = /[<>:"|?*\x00-\x1f]/;
290
+ OCR_PROCESSABLE_TYPES = [
291
+ "image/jpeg",
292
+ "image/png",
293
+ "image/webp"
294
+ ];
295
+ }
296
+ });
297
+
298
+ // src/upload/utils/hash.ts
299
+ import { CID } from "multiformats/cid";
300
+ import * as raw from "multiformats/codecs/raw";
301
+ import { sha256 } from "multiformats/hashes/sha2";
302
+ async function computeFileCID(filePath) {
303
+ const fs2 = await import("fs/promises");
304
+ try {
305
+ const fileBuffer = await fs2.readFile(filePath);
306
+ const hash = await sha256.digest(fileBuffer);
307
+ const cid = CID.create(1, raw.code, hash);
308
+ return cid.toString();
309
+ } catch (error) {
310
+ throw new Error(`CID computation failed: ${error.message}`);
311
+ }
312
+ }
313
+ async function computeCIDFromBuffer(data) {
314
+ const hash = await sha256.digest(data);
315
+ const cid = CID.create(1, raw.code, hash);
316
+ return cid.toString();
317
+ }
318
+ var init_hash = __esm({
319
+ "src/upload/utils/hash.ts"() {
320
+ "use strict";
321
+ }
322
+ });
323
+
324
+ // src/upload/types/processing.ts
325
+ var DEFAULT_PROCESSING_CONFIG;
326
+ var init_processing = __esm({
327
+ "src/upload/types/processing.ts"() {
328
+ "use strict";
329
+ DEFAULT_PROCESSING_CONFIG = {
330
+ ocr: true,
331
+ describe: true,
332
+ pinax: true
333
+ };
334
+ }
335
+ });
336
+
337
+ // src/upload/platforms/node.ts
338
+ var node_exports = {};
339
+ __export(node_exports, {
340
+ NodeScanner: () => NodeScanner
341
+ });
342
+ import fs from "fs/promises";
343
+ import path from "path";
344
+ var NodeScanner;
345
+ var init_node = __esm({
346
+ "src/upload/platforms/node.ts"() {
347
+ "use strict";
348
+ init_errors();
349
+ init_validation();
350
+ init_hash();
351
+ init_processing();
352
+ init_common();
353
+ NodeScanner = class {
354
+ /**
355
+ * Scan directory recursively and collect file metadata
356
+ */
357
+ async scanFiles(source, options) {
358
+ const dirPath = Array.isArray(source) ? source[0] : source;
359
+ if (!dirPath || typeof dirPath !== "string") {
360
+ throw new ScanError("Node.js scanner requires a directory path", "");
361
+ }
362
+ const files = [];
363
+ try {
364
+ const stats = await fs.stat(dirPath);
365
+ if (!stats.isDirectory()) {
366
+ throw new ScanError(`Path is not a directory: ${dirPath}`, dirPath);
367
+ }
368
+ } catch (error) {
369
+ if (error.code === "ENOENT") {
370
+ throw new ScanError(`Directory not found: ${dirPath}`, dirPath);
371
+ }
372
+ throw new ScanError(`Cannot access directory: ${error.message}`, dirPath);
373
+ }
374
+ validateLogicalPath(options.rootPath);
375
+ const globalProcessingConfig = options.defaultProcessingConfig || DEFAULT_PROCESSING_CONFIG;
376
+ async function loadDirectoryProcessingConfig(dirPath2) {
377
+ const configPath = path.join(dirPath2, ".arke-process.json");
378
+ try {
379
+ const content = await fs.readFile(configPath, "utf-8");
380
+ return JSON.parse(content);
381
+ } catch (error) {
382
+ if (error.code !== "ENOENT") {
383
+ console.warn(`Error reading processing config ${configPath}: ${error.message}`);
384
+ }
385
+ return null;
386
+ }
387
+ }
388
+ function mergeProcessingConfig(defaults, override) {
389
+ if (!override) return defaults;
390
+ return {
391
+ ocr: override.ocr ?? defaults.ocr,
392
+ describe: override.describe ?? defaults.describe,
393
+ pinax: override.pinax ?? defaults.pinax
394
+ };
395
+ }
396
+ async function walk(currentPath, relativePath = "") {
397
+ const dirConfigOverride = await loadDirectoryProcessingConfig(currentPath);
398
+ const currentProcessingConfig = mergeProcessingConfig(
399
+ globalProcessingConfig,
400
+ dirConfigOverride
401
+ );
402
+ let entries;
403
+ try {
404
+ entries = await fs.readdir(currentPath, { withFileTypes: true });
405
+ } catch (error) {
406
+ console.warn(`Cannot read directory: ${currentPath}`, error.message);
407
+ return;
408
+ }
409
+ for (const entry of entries) {
410
+ const fullPath = path.join(currentPath, entry.name);
411
+ const relPath = path.join(relativePath, entry.name);
412
+ try {
413
+ if (entry.isSymbolicLink()) {
414
+ if (!options.followSymlinks) {
415
+ continue;
416
+ }
417
+ const stats = await fs.stat(fullPath);
418
+ if (stats.isDirectory()) {
419
+ await walk(fullPath, relPath);
420
+ } else if (stats.isFile()) {
421
+ await processFile(fullPath, relPath, stats.size, currentProcessingConfig);
422
+ }
423
+ continue;
424
+ }
425
+ if (entry.isDirectory()) {
426
+ await walk(fullPath, relPath);
427
+ continue;
428
+ }
429
+ if (entry.isFile()) {
430
+ const stats = await fs.stat(fullPath);
431
+ await processFile(fullPath, relPath, stats.size, currentProcessingConfig);
432
+ }
433
+ } catch (error) {
434
+ if (error instanceof ScanError && error.message.includes(".ref.json")) {
435
+ throw error;
436
+ }
437
+ console.warn(`Error processing ${fullPath}: ${error.message}`);
438
+ continue;
439
+ }
440
+ }
441
+ }
442
+ async function processFile(fullPath, relativePath, size, processingConfig) {
443
+ const fileName = path.basename(fullPath);
444
+ if (fileName === ".arke-process.json") {
445
+ return;
446
+ }
447
+ if (fileName.endsWith(".ref.json")) {
448
+ try {
449
+ const content = await fs.readFile(fullPath, "utf-8");
450
+ validateRefJson(content, fileName, console);
451
+ } catch (error) {
452
+ throw new ScanError(
453
+ `Invalid .ref.json file: ${fileName} - ${error.message}`,
454
+ fullPath
455
+ );
456
+ }
457
+ }
458
+ try {
459
+ validateFileSize(size);
460
+ } catch (error) {
461
+ console.warn(`Skipping file that exceeds size limit: ${fileName}`, error.message);
462
+ return;
463
+ }
464
+ const normalizedRelPath = normalizePath(relativePath);
465
+ const logicalPath = path.posix.join(options.rootPath, normalizedRelPath);
466
+ try {
467
+ validateLogicalPath(logicalPath);
468
+ } catch (error) {
469
+ console.warn(`Skipping file with invalid logical path: ${logicalPath}`, error.message);
470
+ return;
471
+ }
472
+ const contentType = getMimeType(fileName);
473
+ try {
474
+ await fs.access(fullPath, fs.constants.R_OK);
475
+ } catch (error) {
476
+ console.warn(`Skipping unreadable file: ${fullPath}`);
477
+ return;
478
+ }
479
+ let cid;
480
+ try {
481
+ cid = await computeFileCID(fullPath);
482
+ } catch (error) {
483
+ console.warn(`Warning: CID computation failed for ${fullPath}, continuing without CID:`, error.message);
484
+ cid = void 0;
485
+ }
486
+ files.push({
487
+ localPath: fullPath,
488
+ logicalPath,
489
+ fileName,
490
+ size,
491
+ contentType,
492
+ cid,
493
+ processingConfig
494
+ });
495
+ }
496
+ await walk(dirPath);
497
+ files.sort((a, b) => a.size - b.size);
498
+ return files;
499
+ }
500
+ /**
501
+ * Read file contents as ArrayBuffer
502
+ */
503
+ async readFile(file) {
504
+ const buffer = await fs.readFile(file.localPath);
505
+ return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
506
+ }
507
+ };
508
+ }
509
+ });
510
+
511
+ // src/upload/platforms/browser.ts
512
+ var browser_exports = {};
513
+ __export(browser_exports, {
514
+ BrowserScanner: () => BrowserScanner
515
+ });
516
+ var BrowserScanner;
517
+ var init_browser = __esm({
518
+ "src/upload/platforms/browser.ts"() {
519
+ "use strict";
520
+ init_errors();
521
+ init_validation();
522
+ init_hash();
523
+ init_processing();
524
+ init_common();
525
+ BrowserScanner = class {
526
+ /**
527
+ * Scan files from File or FileList
528
+ */
529
+ async scanFiles(source, options) {
530
+ const fileList = Array.isArray(source) ? source : [source];
531
+ if (fileList.length === 0) {
532
+ throw new ScanError("No files provided", "");
533
+ }
534
+ validateLogicalPath(options.rootPath);
535
+ const globalProcessingConfig = options.defaultProcessingConfig || DEFAULT_PROCESSING_CONFIG;
536
+ const files = [];
537
+ for (const file of fileList) {
538
+ try {
539
+ const fileInfo = await this.processFile(file, options.rootPath, globalProcessingConfig);
540
+ if (fileInfo) {
541
+ files.push(fileInfo);
542
+ }
543
+ } catch (error) {
544
+ console.warn(`Error processing ${file.name}: ${error.message}`);
545
+ continue;
546
+ }
547
+ }
548
+ files.sort((a, b) => a.size - b.size);
549
+ return files;
550
+ }
551
+ /**
552
+ * Process a single File object
553
+ */
554
+ async processFile(file, rootPath, processingConfig) {
555
+ const fileName = file.name;
556
+ const size = file.size;
557
+ if (fileName.startsWith(".")) {
558
+ return null;
559
+ }
560
+ const skipFiles = ["Thumbs.db", "desktop.ini", "__MACOSX"];
561
+ if (skipFiles.includes(fileName)) {
562
+ return null;
563
+ }
564
+ if (fileName === ".arke-process.json") {
565
+ return null;
566
+ }
567
+ try {
568
+ validateFileSize(size);
569
+ } catch (error) {
570
+ console.warn(`Skipping file that exceeds size limit: ${fileName}`, error.message);
571
+ return null;
572
+ }
573
+ let relativePath = "";
574
+ if ("webkitRelativePath" in file && file.webkitRelativePath) {
575
+ const parts = file.webkitRelativePath.split("/");
576
+ if (parts.length > 1) {
577
+ relativePath = parts.slice(1).join("/");
578
+ } else {
579
+ relativePath = fileName;
580
+ }
581
+ } else {
582
+ relativePath = fileName;
583
+ }
584
+ const normalizedRelPath = normalizePath(relativePath);
585
+ const logicalPath = `${rootPath}/${normalizedRelPath}`.replace(/\/+/g, "/");
586
+ try {
587
+ validateLogicalPath(logicalPath);
588
+ } catch (error) {
589
+ console.warn(`Skipping file with invalid logical path: ${logicalPath}`, error.message);
590
+ return null;
591
+ }
592
+ const contentType = file.type || getMimeType(fileName);
593
+ let cid;
594
+ try {
595
+ const buffer = await file.arrayBuffer();
596
+ cid = await computeCIDFromBuffer(new Uint8Array(buffer));
597
+ } catch (error) {
598
+ console.warn(`Warning: CID computation failed for ${fileName}, continuing without CID:`, error.message);
599
+ cid = void 0;
600
+ }
601
+ return {
602
+ localPath: `__browser_file__${fileName}`,
603
+ // Special marker for browser files
604
+ logicalPath,
605
+ fileName,
606
+ size,
607
+ contentType,
608
+ cid,
609
+ processingConfig
610
+ };
611
+ }
612
+ /**
613
+ * Read file contents as ArrayBuffer
614
+ * Note: In browser context, the File object should be passed directly
615
+ */
616
+ async readFile(file) {
617
+ throw new Error("Browser scanner requires File objects to be provided directly during upload");
618
+ }
619
+ };
620
+ }
25
621
  });
26
- module.exports = __toCommonJS(src_exports);
27
622
 
28
623
  // src/collections/errors.ts
29
624
  var CollectionsError = class extends Error {
30
- constructor(message, code = "UNKNOWN_ERROR", details) {
625
+ constructor(message, code2 = "UNKNOWN_ERROR", details) {
31
626
  super(message);
32
- this.code = code;
627
+ this.code = code2;
33
628
  this.details = details;
34
629
  this.name = "CollectionsError";
35
630
  }
@@ -48,8 +643,8 @@ var CollectionsClient = class {
48
643
  // ---------------------------------------------------------------------------
49
644
  // Request helpers
50
645
  // ---------------------------------------------------------------------------
51
- buildUrl(path, query) {
52
- const url = new URL(`${this.baseUrl}${path}`);
646
+ buildUrl(path2, query) {
647
+ const url = new URL(`${this.baseUrl}${path2}`);
53
648
  if (query) {
54
649
  Object.entries(query).forEach(([key, value]) => {
55
650
  if (value !== void 0 && value !== null) {
@@ -71,9 +666,9 @@ var CollectionsClient = class {
71
666
  }
72
667
  return headers;
73
668
  }
74
- async request(path, options = {}) {
669
+ async request(path2, options = {}) {
75
670
  const authRequired = options.authRequired ?? false;
76
- const url = this.buildUrl(path, options.query);
671
+ const url = this.buildUrl(path2, options.query);
77
672
  const headers = new Headers(this.getHeaders(authRequired));
78
673
  if (options.headers) {
79
674
  Object.entries(options.headers).forEach(([k, v]) => {
@@ -92,10 +687,11 @@ var CollectionsClient = class {
92
687
  return await response.text();
93
688
  }
94
689
  let body;
690
+ const text = await response.text();
95
691
  try {
96
- body = await response.json();
692
+ body = JSON.parse(text);
97
693
  } catch {
98
- body = await response.text();
694
+ body = text;
99
695
  }
100
696
  const message = body?.error && typeof body.error === "string" ? body.error : `Request failed with status ${response.status}`;
101
697
  throw new CollectionsError(message, "HTTP_ERROR", {
@@ -224,9 +820,791 @@ var CollectionsClient = class {
224
820
  return this.request(`/pi/${pi}/permissions`, { method: "GET" });
225
821
  }
226
822
  };
227
- // Annotate the CommonJS export names for ESM import in node:
228
- 0 && (module.exports = {
823
+
824
+ // src/upload/lib/worker-client-fetch.ts
825
+ init_errors();
826
+
827
+ // src/upload/utils/retry.ts
828
+ init_errors();
829
+ var DEFAULT_OPTIONS = {
830
+ maxRetries: 3,
831
+ initialDelay: 1e3,
832
+ // 1 second
833
+ maxDelay: 3e4,
834
+ // 30 seconds
835
+ shouldRetry: isRetryableError,
836
+ jitter: true
837
+ };
838
+ async function retryWithBackoff(fn, options = {}) {
839
+ const opts = { ...DEFAULT_OPTIONS, ...options };
840
+ let lastError;
841
+ for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
842
+ try {
843
+ return await fn();
844
+ } catch (error) {
845
+ lastError = error;
846
+ if (attempt >= opts.maxRetries) {
847
+ throw error;
848
+ }
849
+ if (opts.shouldRetry && !opts.shouldRetry(error)) {
850
+ throw error;
851
+ }
852
+ let delay;
853
+ if (error.statusCode === 429 && error.retryAfter) {
854
+ delay = Math.min(error.retryAfter * 1e3, opts.maxDelay);
855
+ } else {
856
+ delay = Math.min(
857
+ opts.initialDelay * Math.pow(2, attempt),
858
+ opts.maxDelay
859
+ );
860
+ }
861
+ if (opts.jitter) {
862
+ const jitterAmount = delay * 0.25;
863
+ delay = delay + (Math.random() * jitterAmount * 2 - jitterAmount);
864
+ }
865
+ await sleep(Math.floor(delay));
866
+ }
867
+ }
868
+ throw lastError;
869
+ }
870
+ function sleep(ms) {
871
+ return new Promise((resolve) => setTimeout(resolve, ms));
872
+ }
873
+
874
+ // src/upload/lib/worker-client-fetch.ts
875
+ var WorkerClient = class {
876
+ constructor(config) {
877
+ this.baseUrl = config.baseUrl.replace(/\/$/, "");
878
+ this.authToken = config.authToken;
879
+ this.timeout = config.timeout ?? 3e4;
880
+ this.maxRetries = config.maxRetries ?? 3;
881
+ this.retryInitialDelay = config.retryInitialDelay ?? 1e3;
882
+ this.retryMaxDelay = config.retryMaxDelay ?? 3e4;
883
+ this.retryJitter = config.retryJitter ?? true;
884
+ this.debug = config.debug ?? false;
885
+ }
886
+ setAuthToken(token) {
887
+ this.authToken = token;
888
+ }
889
+ /**
890
+ * Make HTTP request with fetch
891
+ */
892
+ async request(method, path2, body) {
893
+ const url = `${this.baseUrl}${path2}`;
894
+ if (this.debug) {
895
+ console.log(`HTTP Request: ${method} ${url}`, body);
896
+ }
897
+ try {
898
+ const controller = new AbortController();
899
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
900
+ const headers = {
901
+ "Content-Type": "application/json"
902
+ };
903
+ if (this.authToken) {
904
+ headers["Authorization"] = `Bearer ${this.authToken}`;
905
+ }
906
+ const response = await fetch(url, {
907
+ method,
908
+ headers,
909
+ body: body ? JSON.stringify(body) : void 0,
910
+ signal: controller.signal
911
+ });
912
+ clearTimeout(timeoutId);
913
+ const data = await response.json();
914
+ if (this.debug) {
915
+ console.log(`HTTP Response: ${response.status}`, data);
916
+ }
917
+ if (!response.ok) {
918
+ const errorData = data;
919
+ throw new WorkerAPIError(
920
+ errorData.error || "Request failed",
921
+ response.status,
922
+ errorData.details
923
+ );
924
+ }
925
+ return data;
926
+ } catch (error) {
927
+ if (error instanceof WorkerAPIError) {
928
+ throw error;
929
+ }
930
+ if (error.name === "AbortError") {
931
+ throw new NetworkError(`Request timeout after ${this.timeout}ms`);
932
+ }
933
+ throw new NetworkError(`Network request failed: ${error.message}`);
934
+ }
935
+ }
936
+ /**
937
+ * Initialize a new batch upload
938
+ */
939
+ async initBatch(params) {
940
+ return retryWithBackoff(
941
+ () => this.request("POST", "/ingest/batches/init", params),
942
+ {
943
+ maxRetries: this.maxRetries,
944
+ initialDelay: this.retryInitialDelay,
945
+ maxDelay: this.retryMaxDelay,
946
+ jitter: this.retryJitter
947
+ }
948
+ );
949
+ }
950
+ /**
951
+ * Request presigned URLs for a file upload
952
+ */
953
+ async startFileUpload(batchId, params) {
954
+ return retryWithBackoff(
955
+ () => this.request(
956
+ "POST",
957
+ `/ingest/batches/${batchId}/files/start`,
958
+ params
959
+ ),
960
+ {
961
+ maxRetries: this.maxRetries,
962
+ initialDelay: this.retryInitialDelay,
963
+ maxDelay: this.retryMaxDelay,
964
+ jitter: this.retryJitter
965
+ }
966
+ );
967
+ }
968
+ /**
969
+ * Mark a file upload as complete
970
+ */
971
+ async completeFileUpload(batchId, params) {
972
+ return retryWithBackoff(
973
+ () => this.request(
974
+ "POST",
975
+ `/ingest/batches/${batchId}/files/complete`,
976
+ params
977
+ ),
978
+ {
979
+ maxRetries: this.maxRetries,
980
+ initialDelay: this.retryInitialDelay,
981
+ maxDelay: this.retryMaxDelay,
982
+ jitter: this.retryJitter
983
+ }
984
+ );
985
+ }
986
+ /**
987
+ * Finalize the batch after all files are uploaded
988
+ * Returns root_pi immediately for small batches, or status='discovery' for large batches
989
+ */
990
+ async finalizeBatch(batchId) {
991
+ return retryWithBackoff(
992
+ () => this.request(
993
+ "POST",
994
+ `/ingest/batches/${batchId}/finalize`,
995
+ {}
996
+ ),
997
+ {
998
+ maxRetries: this.maxRetries,
999
+ initialDelay: this.retryInitialDelay,
1000
+ maxDelay: this.retryMaxDelay,
1001
+ jitter: this.retryJitter
1002
+ }
1003
+ );
1004
+ }
1005
+ /**
1006
+ * Get current batch status (used for polling during async discovery)
1007
+ */
1008
+ async getBatchStatus(batchId) {
1009
+ return retryWithBackoff(
1010
+ () => this.request(
1011
+ "GET",
1012
+ `/ingest/batches/${batchId}/status`
1013
+ ),
1014
+ {
1015
+ maxRetries: this.maxRetries,
1016
+ initialDelay: this.retryInitialDelay,
1017
+ maxDelay: this.retryMaxDelay,
1018
+ jitter: this.retryJitter
1019
+ }
1020
+ );
1021
+ }
1022
+ };
1023
+
1024
+ // src/upload/uploader.ts
1025
+ init_common();
1026
+
1027
+ // src/upload/lib/simple-fetch.ts
1028
+ init_errors();
1029
+ async function uploadSimple(fileData, presignedUrl, contentType, options = {}) {
1030
+ const { maxRetries = 3, retryInitialDelay, retryMaxDelay, retryJitter } = options;
1031
+ await retryWithBackoff(
1032
+ async () => {
1033
+ let response;
1034
+ try {
1035
+ response = await fetch(presignedUrl, {
1036
+ method: "PUT",
1037
+ body: fileData,
1038
+ headers: {
1039
+ ...contentType ? { "Content-Type": contentType } : {}
1040
+ }
1041
+ });
1042
+ } catch (error) {
1043
+ throw new UploadError(`Upload failed: ${error.message}`, void 0, void 0, error);
1044
+ }
1045
+ if (!response.ok) {
1046
+ const retryAfter = response.headers.get("retry-after");
1047
+ const error = new UploadError(
1048
+ `Upload failed with status ${response.status}: ${response.statusText}`,
1049
+ void 0,
1050
+ response.status
1051
+ );
1052
+ if (retryAfter && response.status === 429) {
1053
+ error.retryAfter = parseInt(retryAfter, 10);
1054
+ }
1055
+ throw error;
1056
+ }
1057
+ },
1058
+ {
1059
+ maxRetries,
1060
+ initialDelay: retryInitialDelay,
1061
+ maxDelay: retryMaxDelay,
1062
+ jitter: retryJitter
1063
+ }
1064
+ );
1065
+ }
1066
+
1067
+ // src/upload/lib/multipart-fetch.ts
1068
+ init_errors();
1069
+ var DEFAULT_PART_SIZE = 10 * 1024 * 1024;
1070
+ async function uploadMultipart(fileData, presignedUrls, concurrency = 3, options = {}) {
1071
+ const totalSize = fileData.byteLength;
1072
+ const partSize = Math.ceil(totalSize / presignedUrls.length);
1073
+ const parts = [];
1074
+ const queue = [];
1075
+ const { maxRetries = 3, retryInitialDelay, retryMaxDelay, retryJitter } = options;
1076
+ for (let i = 0; i < presignedUrls.length; i++) {
1077
+ const partNumber = i + 1;
1078
+ const start = i * partSize;
1079
+ const end = Math.min(start + partSize, totalSize);
1080
+ const partData = fileData.slice(start, end);
1081
+ const url = presignedUrls[i];
1082
+ queue.push(async () => {
1083
+ const etag = await uploadPart(partData, url, partNumber, maxRetries, {
1084
+ initialDelay: retryInitialDelay,
1085
+ maxDelay: retryMaxDelay,
1086
+ jitter: retryJitter
1087
+ });
1088
+ parts.push({ part_number: partNumber, etag });
1089
+ });
1090
+ }
1091
+ await executeWithConcurrency(queue, concurrency);
1092
+ parts.sort((a, b) => a.part_number - b.part_number);
1093
+ return parts;
1094
+ }
1095
+ async function uploadPart(partData, presignedUrl, partNumber, maxRetries = 3, retryOptions = {}) {
1096
+ return retryWithBackoff(
1097
+ async () => {
1098
+ let response;
1099
+ try {
1100
+ response = await fetch(presignedUrl, {
1101
+ method: "PUT",
1102
+ body: partData
1103
+ });
1104
+ } catch (error) {
1105
+ throw new UploadError(
1106
+ `Part ${partNumber} upload failed: ${error.message}`,
1107
+ void 0,
1108
+ void 0,
1109
+ error
1110
+ );
1111
+ }
1112
+ if (!response.ok) {
1113
+ const retryAfter = response.headers.get("retry-after");
1114
+ const error = new UploadError(
1115
+ `Part ${partNumber} upload failed with status ${response.status}: ${response.statusText}`,
1116
+ void 0,
1117
+ response.status
1118
+ );
1119
+ if (retryAfter && response.status === 429) {
1120
+ error.retryAfter = parseInt(retryAfter, 10);
1121
+ }
1122
+ throw error;
1123
+ }
1124
+ const etag = response.headers.get("etag");
1125
+ if (!etag) {
1126
+ throw new UploadError(
1127
+ `Part ${partNumber} upload succeeded but no ETag returned`,
1128
+ void 0,
1129
+ response.status
1130
+ );
1131
+ }
1132
+ return etag.replace(/"/g, "");
1133
+ },
1134
+ {
1135
+ maxRetries,
1136
+ initialDelay: retryOptions.initialDelay,
1137
+ maxDelay: retryOptions.maxDelay,
1138
+ jitter: retryOptions.jitter
1139
+ }
1140
+ );
1141
+ }
1142
+ async function executeWithConcurrency(tasks, concurrency) {
1143
+ const queue = [...tasks];
1144
+ const workers = [];
1145
+ const processNext = async () => {
1146
+ while (queue.length > 0) {
1147
+ const task = queue.shift();
1148
+ await task();
1149
+ }
1150
+ };
1151
+ for (let i = 0; i < Math.min(concurrency, tasks.length); i++) {
1152
+ workers.push(processNext());
1153
+ }
1154
+ await Promise.all(workers);
1155
+ }
1156
+
1157
+ // src/upload/uploader.ts
1158
+ init_errors();
1159
+ init_validation();
1160
+ var MULTIPART_THRESHOLD = 5 * 1024 * 1024;
1161
+ var ArkeUploader = class {
1162
+ constructor(config) {
1163
+ this.scanner = null;
1164
+ validateCustomPromptsLocation(config.processing);
1165
+ this.config = {
1166
+ rootPath: "/uploads",
1167
+ // Must have at least one segment (not just '/')
1168
+ parallelUploads: 5,
1169
+ parallelParts: 3,
1170
+ ...config
1171
+ };
1172
+ this.workerClient = new WorkerClient({
1173
+ baseUrl: config.gatewayUrl,
1174
+ authToken: config.authToken,
1175
+ timeout: config.timeout,
1176
+ maxRetries: config.maxRetries,
1177
+ retryInitialDelay: config.retryInitialDelay,
1178
+ retryMaxDelay: config.retryMaxDelay,
1179
+ retryJitter: config.retryJitter,
1180
+ debug: false
1181
+ });
1182
+ this.platform = detectPlatform();
1183
+ }
1184
+ /**
1185
+ * Get platform-specific scanner
1186
+ */
1187
+ async getScanner() {
1188
+ if (this.scanner) {
1189
+ return this.scanner;
1190
+ }
1191
+ if (this.platform === "node") {
1192
+ const { NodeScanner: NodeScanner2 } = await Promise.resolve().then(() => (init_node(), node_exports));
1193
+ this.scanner = new NodeScanner2();
1194
+ } else if (this.platform === "browser") {
1195
+ const { BrowserScanner: BrowserScanner2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
1196
+ this.scanner = new BrowserScanner2();
1197
+ } else {
1198
+ throw new ValidationError("Unsupported platform");
1199
+ }
1200
+ return this.scanner;
1201
+ }
1202
+ /**
1203
+ * Upload a batch of files
1204
+ * @param source - Directory path (Node.js) or File[]/FileList (browser)
1205
+ * @param options - Upload options
1206
+ */
1207
+ async uploadBatch(source, options = {}) {
1208
+ const startTime = Date.now();
1209
+ const { onProgress, dryRun = false } = options;
1210
+ this.reportProgress(onProgress, {
1211
+ phase: "scanning",
1212
+ filesTotal: 0,
1213
+ filesUploaded: 0,
1214
+ bytesTotal: 0,
1215
+ bytesUploaded: 0,
1216
+ percentComplete: 0
1217
+ });
1218
+ const scanner = await this.getScanner();
1219
+ const files = await scanner.scanFiles(source, {
1220
+ rootPath: this.config.rootPath || "/",
1221
+ followSymlinks: true,
1222
+ defaultProcessingConfig: this.config.processing
1223
+ });
1224
+ if (files.length === 0) {
1225
+ throw new ValidationError("No files found to upload");
1226
+ }
1227
+ const totalSize = files.reduce((sum, f) => sum + f.size, 0);
1228
+ validateBatchSize(totalSize);
1229
+ if (this.config.customPrompts) {
1230
+ validateCustomPrompts(this.config.customPrompts);
1231
+ const promptFields = Object.keys(this.config.customPrompts).filter(
1232
+ (key) => this.config.customPrompts[key]
1233
+ );
1234
+ console.log(`[Arke Upload SDK] Custom prompts configured: ${promptFields.join(", ")}`);
1235
+ }
1236
+ if (dryRun) {
1237
+ return {
1238
+ batchId: "dry-run",
1239
+ rootPi: "dry-run",
1240
+ filesUploaded: files.length,
1241
+ bytesUploaded: totalSize,
1242
+ durationMs: Date.now() - startTime
1243
+ };
1244
+ }
1245
+ const { batch_id } = await this.workerClient.initBatch({
1246
+ uploader: this.config.uploader,
1247
+ root_path: this.config.rootPath || "/",
1248
+ parent_pi: this.config.parentPi || "",
1249
+ metadata: this.config.metadata,
1250
+ file_count: files.length,
1251
+ total_size: totalSize,
1252
+ custom_prompts: this.config.customPrompts
1253
+ });
1254
+ if (this.config.customPrompts) {
1255
+ console.log(`[Arke Upload SDK] Custom prompts sent to worker for batch ${batch_id}`);
1256
+ }
1257
+ this.reportProgress(onProgress, {
1258
+ phase: "uploading",
1259
+ filesTotal: files.length,
1260
+ filesUploaded: 0,
1261
+ bytesTotal: totalSize,
1262
+ bytesUploaded: 0,
1263
+ percentComplete: 0
1264
+ });
1265
+ let filesUploaded = 0;
1266
+ let bytesUploaded = 0;
1267
+ const { failedFiles } = await this.uploadFilesWithConcurrency(
1268
+ batch_id,
1269
+ files,
1270
+ source,
1271
+ this.config.parallelUploads || 5,
1272
+ (file, bytes) => {
1273
+ filesUploaded++;
1274
+ bytesUploaded += bytes;
1275
+ this.reportProgress(onProgress, {
1276
+ phase: "uploading",
1277
+ filesTotal: files.length,
1278
+ filesUploaded,
1279
+ bytesTotal: totalSize,
1280
+ bytesUploaded,
1281
+ currentFile: file.fileName,
1282
+ percentComplete: Math.round(bytesUploaded / totalSize * 100)
1283
+ });
1284
+ }
1285
+ );
1286
+ if (failedFiles.length === files.length) {
1287
+ throw new ValidationError(
1288
+ `All ${files.length} files failed to upload. First error: ${failedFiles[0]?.error || "Unknown"}`
1289
+ );
1290
+ }
1291
+ if (failedFiles.length > 0) {
1292
+ console.warn(
1293
+ `Warning: ${failedFiles.length} of ${files.length} files failed to upload:`,
1294
+ failedFiles.map((f) => `${f.file.fileName}: ${f.error}`).join(", ")
1295
+ );
1296
+ }
1297
+ this.reportProgress(onProgress, {
1298
+ phase: "finalizing",
1299
+ filesTotal: files.length,
1300
+ filesUploaded,
1301
+ bytesTotal: totalSize,
1302
+ bytesUploaded,
1303
+ percentComplete: 95
1304
+ });
1305
+ const finalizeResult = await this.workerClient.finalizeBatch(batch_id);
1306
+ let rootPi;
1307
+ if (finalizeResult.root_pi) {
1308
+ rootPi = finalizeResult.root_pi;
1309
+ } else if (finalizeResult.status === "discovery") {
1310
+ this.reportProgress(onProgress, {
1311
+ phase: "discovery",
1312
+ filesTotal: files.length,
1313
+ filesUploaded,
1314
+ bytesTotal: totalSize,
1315
+ bytesUploaded,
1316
+ percentComplete: 97
1317
+ });
1318
+ rootPi = await this.pollForRootPi(batch_id, onProgress, files.length, totalSize, bytesUploaded);
1319
+ } else {
1320
+ throw new ValidationError(
1321
+ `Finalization returned unexpected status: ${finalizeResult.status} without root_pi`
1322
+ );
1323
+ }
1324
+ this.reportProgress(onProgress, {
1325
+ phase: "complete",
1326
+ filesTotal: files.length,
1327
+ filesUploaded,
1328
+ bytesTotal: totalSize,
1329
+ bytesUploaded,
1330
+ percentComplete: 100
1331
+ });
1332
+ return {
1333
+ batchId: batch_id,
1334
+ rootPi,
1335
+ filesUploaded,
1336
+ bytesUploaded,
1337
+ durationMs: Date.now() - startTime
1338
+ };
1339
+ }
1340
+ /**
1341
+ * Poll for root_pi during async discovery
1342
+ */
1343
+ async pollForRootPi(batchId, onProgress, filesTotal, bytesTotal, bytesUploaded) {
1344
+ const POLL_INTERVAL_MS = 2e3;
1345
+ const MAX_POLL_TIME_MS = 30 * 60 * 1e3;
1346
+ const startTime = Date.now();
1347
+ while (Date.now() - startTime < MAX_POLL_TIME_MS) {
1348
+ const status = await this.workerClient.getBatchStatus(batchId);
1349
+ if (status.root_pi) {
1350
+ return status.root_pi;
1351
+ }
1352
+ if (status.status === "failed") {
1353
+ throw new ValidationError(`Batch discovery failed`);
1354
+ }
1355
+ if (status.discovery_progress && onProgress) {
1356
+ const { total, published } = status.discovery_progress;
1357
+ const discoveryPercent = total > 0 ? Math.round(published / total * 100) : 0;
1358
+ this.reportProgress(onProgress, {
1359
+ phase: "discovery",
1360
+ filesTotal,
1361
+ filesUploaded: filesTotal,
1362
+ bytesTotal,
1363
+ bytesUploaded,
1364
+ percentComplete: 95 + Math.round(discoveryPercent * 0.04)
1365
+ // 95-99%
1366
+ });
1367
+ }
1368
+ await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
1369
+ }
1370
+ throw new ValidationError(`Discovery timed out after ${MAX_POLL_TIME_MS / 1e3} seconds`);
1371
+ }
1372
+ /**
1373
+ * Upload files with controlled concurrency
1374
+ */
1375
+ async uploadFilesWithConcurrency(batchId, files, source, concurrency, onFileComplete) {
1376
+ const queue = [...files];
1377
+ const workers = [];
1378
+ const failedFiles = [];
1379
+ const processNext = async () => {
1380
+ while (queue.length > 0) {
1381
+ const file = queue.shift();
1382
+ try {
1383
+ await this.uploadSingleFile(batchId, file, source);
1384
+ onFileComplete(file, file.size);
1385
+ } catch (error) {
1386
+ const errorMessage = error.message || "Unknown error";
1387
+ console.error(`Failed to upload ${file.fileName}: ${errorMessage}`);
1388
+ failedFiles.push({ file, error: errorMessage });
1389
+ }
1390
+ }
1391
+ };
1392
+ for (let i = 0; i < Math.min(concurrency, files.length); i++) {
1393
+ workers.push(processNext());
1394
+ }
1395
+ await Promise.all(workers);
1396
+ return { failedFiles };
1397
+ }
1398
+ /**
1399
+ * Upload a single file
1400
+ */
1401
+ async uploadSingleFile(batchId, file, source) {
1402
+ const uploadInfo = await this.workerClient.startFileUpload(batchId, {
1403
+ file_name: file.fileName,
1404
+ file_size: file.size,
1405
+ logical_path: file.logicalPath,
1406
+ content_type: file.contentType,
1407
+ cid: file.cid,
1408
+ processing_config: file.processingConfig
1409
+ });
1410
+ const fileData = await this.getFileData(file, source);
1411
+ const retryOptions = {
1412
+ maxRetries: this.config.maxRetries,
1413
+ retryInitialDelay: this.config.retryInitialDelay,
1414
+ retryMaxDelay: this.config.retryMaxDelay,
1415
+ retryJitter: this.config.retryJitter
1416
+ };
1417
+ if (uploadInfo.upload_type === "simple") {
1418
+ await uploadSimple(fileData, uploadInfo.presigned_url, file.contentType, retryOptions);
1419
+ } else {
1420
+ const partUrls = uploadInfo.presigned_urls.map((p) => p.url);
1421
+ const parts = await uploadMultipart(
1422
+ fileData,
1423
+ partUrls,
1424
+ this.config.parallelParts || 3,
1425
+ retryOptions
1426
+ );
1427
+ await this.workerClient.completeFileUpload(batchId, {
1428
+ r2_key: uploadInfo.r2_key,
1429
+ upload_id: uploadInfo.upload_id,
1430
+ parts
1431
+ });
1432
+ return;
1433
+ }
1434
+ await this.workerClient.completeFileUpload(batchId, {
1435
+ r2_key: uploadInfo.r2_key
1436
+ });
1437
+ }
1438
+ /**
1439
+ * Get file data based on platform
1440
+ */
1441
+ async getFileData(file, source) {
1442
+ if (this.platform === "node") {
1443
+ const fs2 = await import("fs/promises");
1444
+ const buffer = await fs2.readFile(file.localPath);
1445
+ return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
1446
+ } else if (this.platform === "browser") {
1447
+ const files = Array.isArray(source) ? source : [source];
1448
+ const browserFile = files.find(
1449
+ (f) => f instanceof File && f.name === file.fileName
1450
+ );
1451
+ if (!browserFile) {
1452
+ throw new Error(`Could not find browser File object for ${file.fileName}`);
1453
+ }
1454
+ return browserFile.arrayBuffer();
1455
+ }
1456
+ throw new Error("Unsupported platform for file reading");
1457
+ }
1458
+ /**
1459
+ * Report progress to callback
1460
+ */
1461
+ reportProgress(callback, progress) {
1462
+ if (callback) {
1463
+ callback(progress);
1464
+ }
1465
+ }
1466
+ };
1467
+
1468
+ // src/upload/client.ts
1469
+ init_errors();
1470
+ function getUserIdFromToken(token) {
1471
+ try {
1472
+ const parts = token.split(".");
1473
+ if (parts.length !== 3) return null;
1474
+ const payload = parts[1].replace(/-/g, "+").replace(/_/g, "/");
1475
+ let decoded;
1476
+ if (typeof atob === "function") {
1477
+ decoded = atob(payload);
1478
+ } else {
1479
+ decoded = Buffer.from(payload, "base64").toString("utf-8");
1480
+ }
1481
+ const data = JSON.parse(decoded);
1482
+ return data.sub || null;
1483
+ } catch {
1484
+ return null;
1485
+ }
1486
+ }
1487
+ var UploadClient = class {
1488
+ constructor(config) {
1489
+ const uploader = config.uploader || getUserIdFromToken(config.authToken) || "unknown";
1490
+ this.config = { ...config, uploader };
1491
+ this.collectionsClient = new CollectionsClient({
1492
+ gatewayUrl: config.gatewayUrl,
1493
+ authToken: config.authToken,
1494
+ fetchImpl: config.fetchImpl
1495
+ });
1496
+ }
1497
+ /**
1498
+ * Update the auth token (e.g., after token refresh)
1499
+ */
1500
+ setAuthToken(token) {
1501
+ this.config = { ...this.config, authToken: token };
1502
+ this.collectionsClient.setAuthToken(token);
1503
+ }
1504
+ /**
1505
+ * Create a new collection and upload files to it
1506
+ *
1507
+ * Anyone authenticated can create a new collection.
1508
+ * The root PI of the uploaded files becomes the collection's root.
1509
+ */
1510
+ async createCollection(options) {
1511
+ const { files, collectionMetadata, customPrompts, processing, onProgress, dryRun } = options;
1512
+ const metadata = {
1513
+ ...collectionMetadata,
1514
+ visibility: collectionMetadata.visibility || "public"
1515
+ };
1516
+ const uploader = new ArkeUploader({
1517
+ gatewayUrl: this.config.gatewayUrl,
1518
+ authToken: this.config.authToken,
1519
+ uploader: this.config.uploader,
1520
+ customPrompts,
1521
+ processing
1522
+ });
1523
+ const batchResult = await uploader.uploadBatch(files, {
1524
+ onProgress,
1525
+ dryRun
1526
+ });
1527
+ if (dryRun) {
1528
+ return {
1529
+ ...batchResult,
1530
+ collection: {
1531
+ id: "dry-run",
1532
+ title: metadata.title,
1533
+ slug: metadata.slug,
1534
+ description: metadata.description,
1535
+ visibility: metadata.visibility,
1536
+ rootPi: "dry-run"
1537
+ }
1538
+ };
1539
+ }
1540
+ const collection = await this.collectionsClient.registerRoot({
1541
+ ...metadata,
1542
+ rootPi: batchResult.rootPi
1543
+ });
1544
+ return {
1545
+ ...batchResult,
1546
+ collection
1547
+ };
1548
+ }
1549
+ /**
1550
+ * Add files to an existing collection
1551
+ *
1552
+ * Requires owner or editor role on the collection containing the parent PI.
1553
+ * Use this to add a folder or files to an existing collection hierarchy.
1554
+ */
1555
+ async addToCollection(options) {
1556
+ const { files, parentPi, customPrompts, processing, onProgress, dryRun } = options;
1557
+ if (!dryRun) {
1558
+ const permissions = await this.collectionsClient.getPiPermissions(parentPi);
1559
+ if (!permissions.canEdit) {
1560
+ if (!permissions.collection) {
1561
+ throw new ValidationError(
1562
+ `Cannot add files: PI "${parentPi}" is not part of any collection`
1563
+ );
1564
+ }
1565
+ throw new ValidationError(
1566
+ `Cannot add files to collection "${permissions.collection.title}": you need editor or owner role (current role: ${permissions.collection.role || "none"})`
1567
+ );
1568
+ }
1569
+ }
1570
+ const uploader = new ArkeUploader({
1571
+ gatewayUrl: this.config.gatewayUrl,
1572
+ authToken: this.config.authToken,
1573
+ uploader: this.config.uploader,
1574
+ parentPi,
1575
+ customPrompts,
1576
+ processing
1577
+ });
1578
+ return uploader.uploadBatch(files, {
1579
+ onProgress,
1580
+ dryRun
1581
+ });
1582
+ }
1583
+ /**
1584
+ * Check if you can edit a specific PI (i.e., add files to its collection)
1585
+ */
1586
+ async canEdit(pi) {
1587
+ return this.collectionsClient.getPiPermissions(pi);
1588
+ }
1589
+ /**
1590
+ * Get access to the underlying collections client for other operations
1591
+ */
1592
+ get collections() {
1593
+ return this.collectionsClient;
1594
+ }
1595
+ };
1596
+
1597
+ // src/index.ts
1598
+ init_errors();
1599
+ export {
1600
+ ArkeUploader,
229
1601
  CollectionsClient,
230
- CollectionsError
231
- });
1602
+ CollectionsError,
1603
+ NetworkError,
1604
+ ScanError,
1605
+ UploadClient,
1606
+ UploadError,
1607
+ ValidationError,
1608
+ WorkerAPIError
1609
+ };
232
1610
  //# sourceMappingURL=index.js.map