zen-fs-config 0.3.13 → 0.3.15

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
@@ -422,38 +422,77 @@ registerBackend("GitHub", async (options) => {
422
422
  });
423
423
  registerBackend("Gitee", async (options) => {
424
424
  const zenGitee = await import("zen-fs-gitee");
425
- const { GiteeAPI } = await import("zen-fs-gitee/dist/gitee-api.js");
426
- const originalGetTree = GiteeAPI.prototype.getTree;
427
- GiteeAPI.prototype.getTree = async function(recursive = true) {
425
+ const { GiteeFS } = zenGitee;
426
+ const origInit = GiteeFS.prototype.init;
427
+ const patched = /* @__PURE__ */ new WeakSet();
428
+ GiteeFS.prototype.init = async function() {
429
+ let firstErr;
428
430
  try {
429
- return await originalGetTree.call(this, recursive);
431
+ return await origInit.call(this);
430
432
  } catch (err) {
431
433
  if (!err.message?.includes("404") && !err.message?.includes("Tree not found")) {
432
434
  throw err;
433
435
  }
436
+ firstErr = err;
437
+ }
438
+ if (patched.has(this)) throw firstErr;
439
+ patched.add(this);
440
+ console.log(`[Gitee] init failed with branch="${this.api.branch}", resolving to SHA...`);
441
+ const baseUrl = this.api.baseUrl || "https://gitee.com/api/v5";
442
+ const auth = `access_token=${this.api.token}`;
443
+ const branchUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${this.api.branch}?${auth}`;
444
+ let branchRes = await fetch(branchUrl);
445
+ let branchData;
446
+ if (branchRes.ok) {
447
+ branchData = await branchRes.json();
448
+ } else {
449
+ console.log(`[Gitee] Branch "${this.api.branch}" not found, creating...`);
450
+ const defaultBranch = this.api.branch === "master" ? "main" : "master";
451
+ let defaultSha;
452
+ for (const name of [defaultBranch, "main", "master"]) {
453
+ const dr = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${name}?${auth}`);
454
+ if (dr.ok) {
455
+ const dd = await dr.json();
456
+ defaultSha = dd.commit?.sha;
457
+ if (defaultSha) break;
458
+ }
459
+ }
460
+ if (!defaultSha) {
461
+ console.log(`[Gitee] No branches found in repo, skipping tree init`);
462
+ this.initialized = true;
463
+ return;
464
+ }
465
+ const createRes = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches?${auth}`, {
466
+ method: "POST",
467
+ headers: { "Content-Type": "application/json" },
468
+ body: JSON.stringify({
469
+ refs: defaultSha,
470
+ branch_name: this.api.branch
471
+ })
472
+ });
473
+ if (!createRes.ok) {
474
+ const errText = await createRes.text().catch(() => "");
475
+ throw new Error(`Gitee: failed to create branch "${this.api.branch}": ${createRes.status} ${errText}`);
476
+ }
477
+ branchData = await createRes.json();
478
+ console.log(`[Gitee] Branch "${this.api.branch}" created from ${defaultSha.slice(0, 8)}`);
434
479
  }
435
- console.log(`[Gitee] getTree failed with branch="${this.branch}", resolving to SHA...`);
436
- const baseUrl = this.baseUrl || "https://gitee.com/api/v5";
437
- const sep = "?";
438
- const auth = `access_token=${this.token}`;
439
- const branchUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/branches/${this.branch}${sep}${auth}`;
440
- const branchRes = await fetch(branchUrl);
441
- if (!branchRes.ok) throw new Error(`Gitee: branch "${this.branch}" not found (${branchRes.status})`);
442
- const branchData = await branchRes.json();
443
480
  const commitSha = branchData.commit?.sha;
444
- if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.branch}"`);
445
- const commitUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/git/commits/${commitSha}${sep}${auth}`;
481
+ if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.api.branch}"`);
482
+ const commitUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/git/commits/${commitSha}?${auth}`;
446
483
  const commitRes = await fetch(commitUrl);
447
484
  if (!commitRes.ok) throw new Error(`Gitee: commit ${commitSha} not found (${commitRes.status})`);
448
485
  const commitData = await commitRes.json();
449
486
  const treeSha = commitData.tree?.sha;
450
487
  if (!treeSha) throw new Error(`Gitee: could not get tree SHA from commit ${commitSha}`);
451
- console.log(`[Gitee] Resolved branch="${this.branch}" \u2192 commit=${commitSha.slice(0, 8)} \u2192 tree=${treeSha.slice(0, 8)}`);
452
- const treeUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/git/trees/${treeSha}${sep}recursive=${recursive ? 1 : 0}&${auth}`;
453
- const treeRes = await fetch(treeUrl);
454
- if (!treeRes.ok) throw new Error(`Gitee: tree ${treeSha} not found (${treeRes.status})`);
455
- const treeData = await treeRes.json();
456
- return treeData.tree || [];
488
+ const realBranch = this.api.branch;
489
+ this.api.branch = treeSha;
490
+ console.log(`[Gitee] Resolved branch="${realBranch}" \u2192 commit=${commitSha.slice(0, 8)} \u2192 tree=${treeSha.slice(0, 8)}`);
491
+ try {
492
+ return await origInit.call(this);
493
+ } finally {
494
+ this.api.branch = realBranch;
495
+ }
457
496
  };
458
497
  return wrapZenFSFileSystem({
459
498
  backend: zenGitee.Gitee,
package/dist/index.mjs CHANGED
@@ -375,38 +375,77 @@ registerBackend("GitHub", async (options) => {
375
375
  });
376
376
  registerBackend("Gitee", async (options) => {
377
377
  const zenGitee = await import("zen-fs-gitee");
378
- const { GiteeAPI } = await import("zen-fs-gitee/dist/gitee-api.js");
379
- const originalGetTree = GiteeAPI.prototype.getTree;
380
- GiteeAPI.prototype.getTree = async function(recursive = true) {
378
+ const { GiteeFS } = zenGitee;
379
+ const origInit = GiteeFS.prototype.init;
380
+ const patched = /* @__PURE__ */ new WeakSet();
381
+ GiteeFS.prototype.init = async function() {
382
+ let firstErr;
381
383
  try {
382
- return await originalGetTree.call(this, recursive);
384
+ return await origInit.call(this);
383
385
  } catch (err) {
384
386
  if (!err.message?.includes("404") && !err.message?.includes("Tree not found")) {
385
387
  throw err;
386
388
  }
389
+ firstErr = err;
390
+ }
391
+ if (patched.has(this)) throw firstErr;
392
+ patched.add(this);
393
+ console.log(`[Gitee] init failed with branch="${this.api.branch}", resolving to SHA...`);
394
+ const baseUrl = this.api.baseUrl || "https://gitee.com/api/v5";
395
+ const auth = `access_token=${this.api.token}`;
396
+ const branchUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${this.api.branch}?${auth}`;
397
+ let branchRes = await fetch(branchUrl);
398
+ let branchData;
399
+ if (branchRes.ok) {
400
+ branchData = await branchRes.json();
401
+ } else {
402
+ console.log(`[Gitee] Branch "${this.api.branch}" not found, creating...`);
403
+ const defaultBranch = this.api.branch === "master" ? "main" : "master";
404
+ let defaultSha;
405
+ for (const name of [defaultBranch, "main", "master"]) {
406
+ const dr = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches/${name}?${auth}`);
407
+ if (dr.ok) {
408
+ const dd = await dr.json();
409
+ defaultSha = dd.commit?.sha;
410
+ if (defaultSha) break;
411
+ }
412
+ }
413
+ if (!defaultSha) {
414
+ console.log(`[Gitee] No branches found in repo, skipping tree init`);
415
+ this.initialized = true;
416
+ return;
417
+ }
418
+ const createRes = await fetch(`${baseUrl}/repos/${this.api.owner}/${this.api.repo}/branches?${auth}`, {
419
+ method: "POST",
420
+ headers: { "Content-Type": "application/json" },
421
+ body: JSON.stringify({
422
+ refs: defaultSha,
423
+ branch_name: this.api.branch
424
+ })
425
+ });
426
+ if (!createRes.ok) {
427
+ const errText = await createRes.text().catch(() => "");
428
+ throw new Error(`Gitee: failed to create branch "${this.api.branch}": ${createRes.status} ${errText}`);
429
+ }
430
+ branchData = await createRes.json();
431
+ console.log(`[Gitee] Branch "${this.api.branch}" created from ${defaultSha.slice(0, 8)}`);
387
432
  }
388
- console.log(`[Gitee] getTree failed with branch="${this.branch}", resolving to SHA...`);
389
- const baseUrl = this.baseUrl || "https://gitee.com/api/v5";
390
- const sep = "?";
391
- const auth = `access_token=${this.token}`;
392
- const branchUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/branches/${this.branch}${sep}${auth}`;
393
- const branchRes = await fetch(branchUrl);
394
- if (!branchRes.ok) throw new Error(`Gitee: branch "${this.branch}" not found (${branchRes.status})`);
395
- const branchData = await branchRes.json();
396
433
  const commitSha = branchData.commit?.sha;
397
- if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.branch}"`);
398
- const commitUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/git/commits/${commitSha}${sep}${auth}`;
434
+ if (!commitSha) throw new Error(`Gitee: could not get commit SHA for branch "${this.api.branch}"`);
435
+ const commitUrl = `${baseUrl}/repos/${this.api.owner}/${this.api.repo}/git/commits/${commitSha}?${auth}`;
399
436
  const commitRes = await fetch(commitUrl);
400
437
  if (!commitRes.ok) throw new Error(`Gitee: commit ${commitSha} not found (${commitRes.status})`);
401
438
  const commitData = await commitRes.json();
402
439
  const treeSha = commitData.tree?.sha;
403
440
  if (!treeSha) throw new Error(`Gitee: could not get tree SHA from commit ${commitSha}`);
404
- console.log(`[Gitee] Resolved branch="${this.branch}" \u2192 commit=${commitSha.slice(0, 8)} \u2192 tree=${treeSha.slice(0, 8)}`);
405
- const treeUrl = `${baseUrl}/repos/${this.owner}/${this.repo}/git/trees/${treeSha}${sep}recursive=${recursive ? 1 : 0}&${auth}`;
406
- const treeRes = await fetch(treeUrl);
407
- if (!treeRes.ok) throw new Error(`Gitee: tree ${treeSha} not found (${treeRes.status})`);
408
- const treeData = await treeRes.json();
409
- return treeData.tree || [];
441
+ const realBranch = this.api.branch;
442
+ this.api.branch = treeSha;
443
+ console.log(`[Gitee] Resolved branch="${realBranch}" \u2192 commit=${commitSha.slice(0, 8)} \u2192 tree=${treeSha.slice(0, 8)}`);
444
+ try {
445
+ return await origInit.call(this);
446
+ } finally {
447
+ this.api.branch = realBranch;
448
+ }
410
449
  };
411
450
  return wrapZenFSFileSystem({
412
451
  backend: zenGitee.Gitee,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zen-fs-config",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
4
4
  "description": "Distributed config management library built on ZenFS, zen-fs-cache, and zen-fs-sync",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",