@arke-institute/sdk 2.0.0 → 2.2.0

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.cjs CHANGED
@@ -42,6 +42,8 @@ __export(src_exports, {
42
42
  NotFoundError: () => NotFoundError,
43
43
  ValidationError: () => ValidationError,
44
44
  createArkeClient: () => createArkeClient,
45
+ getAuthorizationHeader: () => getAuthorizationHeader,
46
+ isApiKey: () => isApiKey,
45
47
  parseApiError: () => parseApiError
46
48
  });
47
49
  module.exports = __toCommonJS(src_exports);
@@ -57,9 +59,9 @@ var DEFAULT_CONFIG = {
57
59
 
58
60
  // src/client/errors.ts
59
61
  var ArkeError = class extends Error {
60
- constructor(message, code, status, details) {
62
+ constructor(message, code2, status, details) {
61
63
  super(message);
62
- this.code = code;
64
+ this.code = code2;
63
65
  this.status = status;
64
66
  this.details = details;
65
67
  this.name = "ArkeError";
@@ -136,6 +138,15 @@ function parseApiError(status, body) {
136
138
  }
137
139
 
138
140
  // src/client/ArkeClient.ts
141
+ function isApiKey(token) {
142
+ return token.startsWith("ak_") || token.startsWith("uk_");
143
+ }
144
+ function getAuthorizationHeader(token) {
145
+ if (isApiKey(token)) {
146
+ return `ApiKey ${token}`;
147
+ }
148
+ return `Bearer ${token}`;
149
+ }
139
150
  var ArkeClient = class {
140
151
  constructor(config = {}) {
141
152
  this.config = {
@@ -150,7 +161,7 @@ var ArkeClient = class {
150
161
  ...this.config.headers
151
162
  };
152
163
  if (this.config.authToken) {
153
- headers["Authorization"] = `Bearer ${this.config.authToken}`;
164
+ headers["Authorization"] = getAuthorizationHeader(this.config.authToken);
154
165
  }
155
166
  if (this.config.network === "test") {
156
167
  headers["X-Arke-Network"] = "test";
@@ -198,6 +209,603 @@ function createArkeClient(config) {
198
209
  return new ArkeClient(config);
199
210
  }
200
211
 
212
+ // src/operations/upload/cid.ts
213
+ var import_cid = require("multiformats/cid");
214
+ var import_sha2 = require("multiformats/hashes/sha2");
215
+ var raw = __toESM(require("multiformats/codecs/raw"), 1);
216
+ async function computeCid(data) {
217
+ let bytes;
218
+ if (data instanceof Blob) {
219
+ const buffer = await data.arrayBuffer();
220
+ bytes = new Uint8Array(buffer);
221
+ } else if (data instanceof ArrayBuffer) {
222
+ bytes = new Uint8Array(data);
223
+ } else {
224
+ bytes = data;
225
+ }
226
+ const hash = await import_sha2.sha256.digest(bytes);
227
+ const cid = import_cid.CID.create(1, raw.code, hash);
228
+ return cid.toString();
229
+ }
230
+
231
+ // src/operations/upload/engine.ts
232
+ var PHASE_COUNT = 4;
233
+ var PHASE_INDEX = {
234
+ "computing-cids": 0,
235
+ "creating": 1,
236
+ "backlinking": 2,
237
+ "uploading": 3,
238
+ "complete": 4,
239
+ "error": -1
240
+ };
241
+ async function parallelLimit(items, concurrency, fn) {
242
+ const results = [];
243
+ let index = 0;
244
+ async function worker() {
245
+ while (index < items.length) {
246
+ const currentIndex = index++;
247
+ const item = items[currentIndex];
248
+ results[currentIndex] = await fn(item, currentIndex);
249
+ }
250
+ }
251
+ const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
252
+ await Promise.all(workers);
253
+ return results;
254
+ }
255
+ var TARGET_BYTES_IN_FLIGHT = 200 * 1024 * 1024;
256
+ var BytePool = class {
257
+ constructor(targetBytes = TARGET_BYTES_IN_FLIGHT) {
258
+ this.targetBytes = targetBytes;
259
+ this.bytesInFlight = 0;
260
+ this.waitQueue = [];
261
+ }
262
+ async run(size, fn) {
263
+ while (this.bytesInFlight > 0 && this.bytesInFlight + size > this.targetBytes) {
264
+ await new Promise((resolve) => this.waitQueue.push(resolve));
265
+ }
266
+ this.bytesInFlight += size;
267
+ try {
268
+ return await fn();
269
+ } finally {
270
+ this.bytesInFlight -= size;
271
+ const next = this.waitQueue.shift();
272
+ if (next) next();
273
+ }
274
+ }
275
+ };
276
+ function getParentPath(relativePath) {
277
+ const lastSlash = relativePath.lastIndexOf("/");
278
+ if (lastSlash === -1) return null;
279
+ return relativePath.slice(0, lastSlash);
280
+ }
281
+ function groupFoldersByDepth(folders) {
282
+ const byDepth = /* @__PURE__ */ new Map();
283
+ for (const folder of folders) {
284
+ const depth = folder.relativePath.split("/").length - 1;
285
+ if (!byDepth.has(depth)) byDepth.set(depth, []);
286
+ byDepth.get(depth).push(folder);
287
+ }
288
+ return byDepth;
289
+ }
290
+ async function uploadTree(client, tree, options) {
291
+ const { target, onProgress, concurrency = 10, continueOnError = false, note } = options;
292
+ const errors = [];
293
+ const createdFolders = [];
294
+ const createdFiles = [];
295
+ const foldersByPath = /* @__PURE__ */ new Map();
296
+ const totalEntities = tree.files.length + tree.folders.length;
297
+ const totalBytes = tree.files.reduce((sum, f) => sum + f.size, 0);
298
+ let completedEntities = 0;
299
+ let bytesUploaded = 0;
300
+ const reportProgress = (progress) => {
301
+ if (onProgress) {
302
+ const phase = progress.phase || "computing-cids";
303
+ const phaseIndex = PHASE_INDEX[phase] ?? -1;
304
+ let phasePercent = 0;
305
+ if (phase === "computing-cids") {
306
+ const done = progress.completedEntities ?? completedEntities;
307
+ phasePercent = tree.files.length > 0 ? Math.round(done / tree.files.length * 100) : 100;
308
+ } else if (phase === "creating") {
309
+ const done = progress.completedEntities ?? completedEntities;
310
+ phasePercent = totalEntities > 0 ? Math.round(done / totalEntities * 100) : 100;
311
+ } else if (phase === "backlinking") {
312
+ const done = progress.completedParents ?? 0;
313
+ const total = progress.totalParents ?? 0;
314
+ phasePercent = total > 0 ? Math.round(done / total * 100) : 100;
315
+ } else if (phase === "uploading") {
316
+ const done = progress.bytesUploaded ?? bytesUploaded;
317
+ phasePercent = totalBytes > 0 ? Math.round(done / totalBytes * 100) : 100;
318
+ } else if (phase === "complete") {
319
+ phasePercent = 100;
320
+ }
321
+ onProgress({
322
+ phase,
323
+ phaseIndex,
324
+ phaseCount: PHASE_COUNT,
325
+ phasePercent,
326
+ totalEntities,
327
+ completedEntities,
328
+ totalParents: 0,
329
+ completedParents: 0,
330
+ totalBytes,
331
+ bytesUploaded,
332
+ ...progress
333
+ });
334
+ }
335
+ };
336
+ try {
337
+ let collectionId;
338
+ let collectionCid;
339
+ let collectionCreated = false;
340
+ if (target.createCollection) {
341
+ const collectionBody = {
342
+ label: target.createCollection.label,
343
+ description: target.createCollection.description,
344
+ roles: target.createCollection.roles,
345
+ note
346
+ };
347
+ const { data, error } = await client.api.POST("/collections", {
348
+ body: collectionBody
349
+ });
350
+ if (error || !data) {
351
+ throw new Error(`Failed to create collection: ${JSON.stringify(error)}`);
352
+ }
353
+ collectionId = data.id;
354
+ collectionCid = data.cid;
355
+ collectionCreated = true;
356
+ } else if (target.collectionId) {
357
+ collectionId = target.collectionId;
358
+ const { data, error } = await client.api.GET("/collections/{id}", {
359
+ params: { path: { id: collectionId } }
360
+ });
361
+ if (error || !data) {
362
+ throw new Error(`Failed to fetch collection: ${JSON.stringify(error)}`);
363
+ }
364
+ collectionCid = data.cid;
365
+ } else {
366
+ throw new Error("Must provide either collectionId or createCollection in target");
367
+ }
368
+ const rootParentId = target.parentId ?? collectionId;
369
+ reportProgress({ phase: "computing-cids", completedEntities: 0 });
370
+ const preparedFiles = [];
371
+ let cidProgress = 0;
372
+ await parallelLimit(tree.files, Math.max(concurrency, 20), async (file) => {
373
+ try {
374
+ const data = await file.getData();
375
+ const cid = await computeCid(data);
376
+ preparedFiles.push({ ...file, cid });
377
+ cidProgress++;
378
+ reportProgress({
379
+ phase: "computing-cids",
380
+ completedEntities: cidProgress,
381
+ currentItem: file.relativePath
382
+ });
383
+ } catch (err) {
384
+ const errorMsg = err instanceof Error ? err.message : String(err);
385
+ if (continueOnError) {
386
+ errors.push({ path: file.relativePath, error: `CID computation failed: ${errorMsg}` });
387
+ } else {
388
+ throw new Error(`Failed to compute CID for ${file.relativePath}: ${errorMsg}`);
389
+ }
390
+ }
391
+ });
392
+ reportProgress({ phase: "creating", completedEntities: 0 });
393
+ const foldersByDepth = groupFoldersByDepth(tree.folders);
394
+ const sortedDepths = [...foldersByDepth.keys()].sort((a, b) => a - b);
395
+ for (const depth of sortedDepths) {
396
+ const foldersAtDepth = foldersByDepth.get(depth);
397
+ await Promise.all(
398
+ foldersAtDepth.map(async (folder) => {
399
+ try {
400
+ const parentPath = getParentPath(folder.relativePath);
401
+ const parentId = parentPath ? foldersByPath.get(parentPath).id : rootParentId;
402
+ const parentType = parentPath ? "folder" : parentId === collectionId ? "collection" : "folder";
403
+ const folderBody = {
404
+ label: folder.name,
405
+ collection: collectionId,
406
+ note,
407
+ relationships: [{ predicate: "in", peer: parentId, peer_type: parentType }]
408
+ };
409
+ const { data, error } = await client.api.POST("/folders", {
410
+ body: folderBody
411
+ });
412
+ if (error || !data) {
413
+ throw new Error(JSON.stringify(error));
414
+ }
415
+ foldersByPath.set(folder.relativePath, { id: data.id, cid: data.cid });
416
+ createdFolders.push({
417
+ name: folder.name,
418
+ relativePath: folder.relativePath,
419
+ id: data.id,
420
+ entityCid: data.cid
421
+ });
422
+ completedEntities++;
423
+ reportProgress({
424
+ phase: "creating",
425
+ completedEntities,
426
+ currentItem: folder.relativePath
427
+ });
428
+ } catch (err) {
429
+ const errorMsg = err instanceof Error ? err.message : String(err);
430
+ if (continueOnError) {
431
+ errors.push({ path: folder.relativePath, error: `Folder creation failed: ${errorMsg}` });
432
+ completedEntities++;
433
+ } else {
434
+ throw new Error(`Failed to create folder ${folder.relativePath}: ${errorMsg}`);
435
+ }
436
+ }
437
+ })
438
+ );
439
+ }
440
+ const FILE_CREATION_CONCURRENCY = 50;
441
+ await parallelLimit(preparedFiles, FILE_CREATION_CONCURRENCY, async (file) => {
442
+ try {
443
+ const parentPath = getParentPath(file.relativePath);
444
+ const parentId = parentPath ? foldersByPath.get(parentPath).id : rootParentId;
445
+ const parentType = parentPath ? "folder" : parentId === collectionId ? "collection" : "folder";
446
+ const fileBody = {
447
+ key: file.cid,
448
+ filename: file.name,
449
+ content_type: file.mimeType,
450
+ size: file.size,
451
+ cid: file.cid,
452
+ collection: collectionId,
453
+ relationships: [{ predicate: "in", peer: parentId, peer_type: parentType }]
454
+ };
455
+ const { data, error } = await client.api.POST("/files", {
456
+ body: fileBody
457
+ });
458
+ if (error || !data) {
459
+ throw new Error(`Entity creation failed: ${JSON.stringify(error)}`);
460
+ }
461
+ createdFiles.push({
462
+ ...file,
463
+ id: data.id,
464
+ entityCid: data.cid,
465
+ uploadUrl: data.upload_url,
466
+ uploadExpiresAt: data.upload_expires_at
467
+ });
468
+ completedEntities++;
469
+ reportProgress({
470
+ phase: "creating",
471
+ completedEntities,
472
+ currentItem: file.relativePath
473
+ });
474
+ } catch (err) {
475
+ const errorMsg = err instanceof Error ? err.message : String(err);
476
+ if (continueOnError) {
477
+ errors.push({ path: file.relativePath, error: errorMsg });
478
+ completedEntities++;
479
+ } else {
480
+ throw new Error(`Failed to create file ${file.relativePath}: ${errorMsg}`);
481
+ }
482
+ }
483
+ });
484
+ const childrenByParent = /* @__PURE__ */ new Map();
485
+ for (const folder of createdFolders) {
486
+ const parentPath = getParentPath(folder.relativePath);
487
+ const parentId = parentPath ? foldersByPath.get(parentPath).id : rootParentId;
488
+ if (!childrenByParent.has(parentId)) childrenByParent.set(parentId, []);
489
+ childrenByParent.get(parentId).push({ id: folder.id, type: "folder" });
490
+ }
491
+ for (const file of createdFiles) {
492
+ const parentPath = getParentPath(file.relativePath);
493
+ const parentId = parentPath ? foldersByPath.get(parentPath).id : rootParentId;
494
+ if (!childrenByParent.has(parentId)) childrenByParent.set(parentId, []);
495
+ childrenByParent.get(parentId).push({ id: file.id, type: "file" });
496
+ }
497
+ const totalParents = childrenByParent.size;
498
+ let completedParents = 0;
499
+ reportProgress({ phase: "backlinking", totalParents, completedParents: 0 });
500
+ const parentEntries = [...childrenByParent.entries()];
501
+ await parallelLimit(parentEntries, concurrency, async ([parentId, children]) => {
502
+ try {
503
+ const isCollection = parentId === collectionId;
504
+ const relationshipsAdd = children.map((child) => ({
505
+ predicate: "contains",
506
+ peer: child.id,
507
+ peer_type: child.type
508
+ }));
509
+ if (isCollection) {
510
+ const { data: collData, error: getError } = await client.api.GET("/collections/{id}", {
511
+ params: { path: { id: parentId } }
512
+ });
513
+ if (getError || !collData) {
514
+ throw new Error(`Failed to fetch collection: ${JSON.stringify(getError)}`);
515
+ }
516
+ const updateBody = {
517
+ expect_tip: collData.cid,
518
+ relationships_add: relationshipsAdd,
519
+ note: note ? `${note} (backlink)` : "Upload backlink"
520
+ };
521
+ const { error } = await client.api.PUT("/collections/{id}", {
522
+ params: { path: { id: parentId } },
523
+ body: updateBody
524
+ });
525
+ if (error) {
526
+ throw new Error(JSON.stringify(error));
527
+ }
528
+ } else {
529
+ const { data: folderData, error: getError } = await client.api.GET("/folders/{id}", {
530
+ params: { path: { id: parentId } }
531
+ });
532
+ if (getError || !folderData) {
533
+ throw new Error(`Failed to fetch folder: ${JSON.stringify(getError)}`);
534
+ }
535
+ const updateBody = {
536
+ expect_tip: folderData.cid,
537
+ relationships_add: relationshipsAdd,
538
+ note: note ? `${note} (backlink)` : "Upload backlink"
539
+ };
540
+ const { error } = await client.api.PUT("/folders/{id}", {
541
+ params: { path: { id: parentId } },
542
+ body: updateBody
543
+ });
544
+ if (error) {
545
+ throw new Error(JSON.stringify(error));
546
+ }
547
+ }
548
+ completedParents++;
549
+ reportProgress({
550
+ phase: "backlinking",
551
+ totalParents,
552
+ completedParents,
553
+ currentItem: `parent:${parentId}`
554
+ });
555
+ } catch (err) {
556
+ const errorMsg = err instanceof Error ? err.message : String(err);
557
+ if (continueOnError) {
558
+ errors.push({ path: `parent:${parentId}`, error: `Backlink failed: ${errorMsg}` });
559
+ completedParents++;
560
+ } else {
561
+ throw new Error(`Failed to backlink parent ${parentId}: ${errorMsg}`);
562
+ }
563
+ }
564
+ });
565
+ reportProgress({ phase: "uploading", bytesUploaded: 0 });
566
+ const pool = new BytePool();
567
+ await Promise.all(
568
+ createdFiles.map(async (file) => {
569
+ await pool.run(file.size, async () => {
570
+ try {
571
+ const fileData = await file.getData();
572
+ let body;
573
+ if (fileData instanceof Blob) {
574
+ body = fileData;
575
+ } else if (fileData instanceof Uint8Array) {
576
+ const arrayBuffer = new ArrayBuffer(fileData.byteLength);
577
+ new Uint8Array(arrayBuffer).set(fileData);
578
+ body = new Blob([arrayBuffer], { type: file.mimeType });
579
+ } else {
580
+ body = new Blob([fileData], { type: file.mimeType });
581
+ }
582
+ const uploadResponse = await fetch(file.uploadUrl, {
583
+ method: "PUT",
584
+ body,
585
+ headers: { "Content-Type": file.mimeType }
586
+ });
587
+ if (!uploadResponse.ok) {
588
+ throw new Error(`S3 upload failed with status ${uploadResponse.status}`);
589
+ }
590
+ let confirmTip = file.entityCid;
591
+ let confirmAttempts = 0;
592
+ const MAX_CONFIRM_ATTEMPTS = 3;
593
+ while (confirmAttempts < MAX_CONFIRM_ATTEMPTS) {
594
+ confirmAttempts++;
595
+ const { error: confirmError } = await client.api.POST("/files/{id}/confirm-upload", {
596
+ params: { path: { id: file.id } },
597
+ body: {
598
+ expect_tip: confirmTip,
599
+ note: note ? `${note} (confirmed)` : "Upload confirmed"
600
+ }
601
+ });
602
+ if (!confirmError) {
603
+ break;
604
+ }
605
+ const errorStr = JSON.stringify(confirmError);
606
+ if (errorStr.includes("409") || errorStr.includes("CAS") || errorStr.includes("conflict")) {
607
+ const { data: currentFile, error: fetchError } = await client.api.GET("/files/{id}", {
608
+ params: { path: { id: file.id } }
609
+ });
610
+ if (fetchError || !currentFile) {
611
+ throw new Error(`Failed to fetch file for confirm retry: ${JSON.stringify(fetchError)}`);
612
+ }
613
+ confirmTip = currentFile.cid;
614
+ } else {
615
+ throw new Error(`Confirm upload failed: ${errorStr}`);
616
+ }
617
+ }
618
+ if (confirmAttempts >= MAX_CONFIRM_ATTEMPTS) {
619
+ throw new Error(`Confirm upload failed after ${MAX_CONFIRM_ATTEMPTS} CAS retries`);
620
+ }
621
+ bytesUploaded += file.size;
622
+ reportProgress({
623
+ phase: "uploading",
624
+ bytesUploaded,
625
+ currentItem: file.relativePath
626
+ });
627
+ } catch (err) {
628
+ const errorMsg = err instanceof Error ? err.message : String(err);
629
+ if (continueOnError) {
630
+ errors.push({ path: file.relativePath, error: `Upload failed: ${errorMsg}` });
631
+ } else {
632
+ throw new Error(`Failed to upload ${file.relativePath}: ${errorMsg}`);
633
+ }
634
+ }
635
+ });
636
+ })
637
+ );
638
+ reportProgress({ phase: "complete", totalParents, completedParents, bytesUploaded });
639
+ const resultFolders = createdFolders.map((f) => ({
640
+ id: f.id,
641
+ cid: f.entityCid,
642
+ type: "folder",
643
+ relativePath: f.relativePath
644
+ }));
645
+ const resultFiles = createdFiles.map((f) => ({
646
+ id: f.id,
647
+ cid: f.entityCid,
648
+ type: "file",
649
+ relativePath: f.relativePath
650
+ }));
651
+ return {
652
+ success: errors.length === 0,
653
+ collection: {
654
+ id: collectionId,
655
+ cid: collectionCid,
656
+ created: collectionCreated
657
+ },
658
+ folders: resultFolders,
659
+ files: resultFiles,
660
+ errors
661
+ };
662
+ } catch (err) {
663
+ const errorMsg = err instanceof Error ? err.message : String(err);
664
+ reportProgress({
665
+ phase: "error",
666
+ error: errorMsg
667
+ });
668
+ return {
669
+ success: false,
670
+ collection: {
671
+ id: target.collectionId ?? "",
672
+ cid: "",
673
+ created: false
674
+ },
675
+ folders: createdFolders.map((f) => ({
676
+ id: f.id,
677
+ cid: f.entityCid,
678
+ type: "folder",
679
+ relativePath: f.relativePath
680
+ })),
681
+ files: createdFiles.map((f) => ({
682
+ id: f.id,
683
+ cid: f.entityCid,
684
+ type: "file",
685
+ relativePath: f.relativePath
686
+ })),
687
+ errors: [...errors, { path: "", error: errorMsg }]
688
+ };
689
+ }
690
+ }
691
+
692
+ // src/operations/upload/scanners.ts
693
+ function getMimeType(filename) {
694
+ const ext = filename.toLowerCase().split(".").pop() || "";
695
+ const mimeTypes = {
696
+ // Images
697
+ jpg: "image/jpeg",
698
+ jpeg: "image/jpeg",
699
+ png: "image/png",
700
+ gif: "image/gif",
701
+ webp: "image/webp",
702
+ svg: "image/svg+xml",
703
+ ico: "image/x-icon",
704
+ bmp: "image/bmp",
705
+ tiff: "image/tiff",
706
+ tif: "image/tiff",
707
+ // Documents
708
+ pdf: "application/pdf",
709
+ doc: "application/msword",
710
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
711
+ xls: "application/vnd.ms-excel",
712
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
713
+ ppt: "application/vnd.ms-powerpoint",
714
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
715
+ odt: "application/vnd.oasis.opendocument.text",
716
+ ods: "application/vnd.oasis.opendocument.spreadsheet",
717
+ odp: "application/vnd.oasis.opendocument.presentation",
718
+ // Text
719
+ txt: "text/plain",
720
+ md: "text/markdown",
721
+ csv: "text/csv",
722
+ html: "text/html",
723
+ htm: "text/html",
724
+ css: "text/css",
725
+ xml: "text/xml",
726
+ rtf: "application/rtf",
727
+ // Code
728
+ js: "text/javascript",
729
+ mjs: "text/javascript",
730
+ ts: "text/typescript",
731
+ jsx: "text/javascript",
732
+ tsx: "text/typescript",
733
+ json: "application/json",
734
+ yaml: "text/yaml",
735
+ yml: "text/yaml",
736
+ // Archives
737
+ zip: "application/zip",
738
+ tar: "application/x-tar",
739
+ gz: "application/gzip",
740
+ rar: "application/vnd.rar",
741
+ "7z": "application/x-7z-compressed",
742
+ // Audio
743
+ mp3: "audio/mpeg",
744
+ wav: "audio/wav",
745
+ ogg: "audio/ogg",
746
+ m4a: "audio/mp4",
747
+ flac: "audio/flac",
748
+ // Video
749
+ mp4: "video/mp4",
750
+ webm: "video/webm",
751
+ avi: "video/x-msvideo",
752
+ mov: "video/quicktime",
753
+ mkv: "video/x-matroska",
754
+ // Fonts
755
+ woff: "font/woff",
756
+ woff2: "font/woff2",
757
+ ttf: "font/ttf",
758
+ otf: "font/otf",
759
+ // Other
760
+ wasm: "application/wasm"
761
+ };
762
+ return mimeTypes[ext] || "application/octet-stream";
763
+ }
764
+ async function scanDirectory(directoryPath, options = {}) {
765
+ const fs = await import("fs/promises");
766
+ const path = await import("path");
767
+ const { ignore = ["node_modules", ".git", ".DS_Store"], includeHidden = false } = options;
768
+ const files = [];
769
+ const folders = [];
770
+ const rootName = path.basename(directoryPath);
771
+ async function scanDir(dirPath, relativePath) {
772
+ const entries = await fs.readdir(dirPath, { withFileTypes: true });
773
+ for (const entry of entries) {
774
+ const name = entry.name;
775
+ if (!includeHidden && name.startsWith(".")) {
776
+ continue;
777
+ }
778
+ if (ignore.some((pattern) => name === pattern || name.match(pattern))) {
779
+ continue;
780
+ }
781
+ const fullPath = path.join(dirPath, name);
782
+ const entryRelativePath = relativePath ? `${relativePath}/${name}` : name;
783
+ if (entry.isDirectory()) {
784
+ folders.push({
785
+ name,
786
+ relativePath: entryRelativePath
787
+ });
788
+ await scanDir(fullPath, entryRelativePath);
789
+ } else if (entry.isFile()) {
790
+ const stat = await fs.stat(fullPath);
791
+ files.push({
792
+ name,
793
+ relativePath: entryRelativePath,
794
+ size: stat.size,
795
+ mimeType: getMimeType(name),
796
+ getData: async () => {
797
+ const buffer = await fs.readFile(fullPath);
798
+ return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
799
+ }
800
+ });
801
+ }
802
+ }
803
+ }
804
+ await scanDir(directoryPath, "");
805
+ folders.sort((a, b) => a.relativePath.split("/").length - b.relativePath.split("/").length);
806
+ return { files, folders };
807
+ }
808
+
201
809
  // src/operations/folders.ts
202
810
  var FolderOperations = class {
203
811
  constructor(client) {
@@ -206,15 +814,32 @@ var FolderOperations = class {
206
814
  /**
207
815
  * Upload a local directory to Arke
208
816
  *
209
- * TODO: Implement this method
210
- * Steps:
211
- * 1. Scan directory structure
212
- * 2. Create folder hierarchy (depth-first)
213
- * 3. Upload files in parallel (with concurrency limit)
214
- * 4. Create bidirectional relationships (folder contains file)
817
+ * @deprecated Use uploadTree and scanDirectory instead
215
818
  */
216
- async uploadDirectory(_localPath, _options) {
217
- throw new Error("FolderOperations.uploadDirectory is not yet implemented");
819
+ async uploadDirectory(localPath, options) {
820
+ const tree = await scanDirectory(localPath);
821
+ const result = await uploadTree(this.client, tree, {
822
+ target: {
823
+ collectionId: options.collectionId,
824
+ parentId: options.parentFolderId
825
+ },
826
+ concurrency: options.concurrency,
827
+ onProgress: options.onProgress ? (p) => {
828
+ options.onProgress({
829
+ phase: p.phase === "computing-cids" ? "creating-folders" : p.phase === "creating" ? "uploading-files" : p.phase === "backlinking" ? "linking" : p.phase === "complete" ? "complete" : "scanning",
830
+ totalFiles: p.totalEntities,
831
+ completedFiles: p.completedEntities,
832
+ totalFolders: p.totalParents,
833
+ completedFolders: p.completedParents,
834
+ currentFile: p.currentItem
835
+ });
836
+ } : void 0
837
+ });
838
+ return {
839
+ rootFolder: result.folders[0] || null,
840
+ folders: result.folders,
841
+ files: result.files
842
+ };
218
843
  }
219
844
  };
220
845
 
@@ -290,6 +915,8 @@ var CryptoOperations = class {
290
915
  NotFoundError,
291
916
  ValidationError,
292
917
  createArkeClient,
918
+ getAuthorizationHeader,
919
+ isApiKey,
293
920
  parseApiError
294
921
  });
295
922
  //# sourceMappingURL=index.cjs.map