@tinycloud/cli 0.7.0-beta.20 → 0.7.0-beta.22

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
@@ -22167,7 +22167,7 @@ var init_version18 = __esm({
22167
22167
  });
22168
22168
 
22169
22169
  // ../../node_modules/@ethersproject/random/lib.esm/random.js
22170
- function getGlobal() {
22170
+ function getGlobal2() {
22171
22171
  if (typeof self !== "undefined") {
22172
22172
  return self;
22173
22173
  }
@@ -22195,7 +22195,7 @@ var init_random = __esm({
22195
22195
  init_lib();
22196
22196
  init_version18();
22197
22197
  logger23 = new Logger(version18);
22198
- anyGlobal = getGlobal();
22198
+ anyGlobal = getGlobal2();
22199
22199
  crypto2 = anyGlobal.crypto || anyGlobal.msCrypto;
22200
22200
  if (!crypto2 || !crypto2.getRandomValues) {
22201
22201
  logger23.warn("WARNING: Missing strong random number source");
@@ -36544,6 +36544,200 @@ function serviceError(code2, message, service, options) {
36544
36544
  meta: options?.meta
36545
36545
  };
36546
36546
  }
36547
+ var DEBUG_FLAG = "TinyCloud_debug";
36548
+ var MAX_EVENTS = 1e3;
36549
+ function getGlobal() {
36550
+ return globalThis;
36551
+ }
36552
+ function nowMs() {
36553
+ const performanceNow = globalThis.performance?.now?.bind(globalThis.performance);
36554
+ return typeof performanceNow === "function" ? performanceNow() : Date.now();
36555
+ }
36556
+ function isTrue(value) {
36557
+ return value === true || value === "true" || value === "1";
36558
+ }
36559
+ function getProcessDebugFlag() {
36560
+ const processLike = globalThis.process;
36561
+ return processLike?.env?.[DEBUG_FLAG];
36562
+ }
36563
+ function isBrowserWindow() {
36564
+ const global2 = getGlobal();
36565
+ return global2.window === globalThis;
36566
+ }
36567
+ function getLocalStorage() {
36568
+ if (!isBrowserWindow()) {
36569
+ return void 0;
36570
+ }
36571
+ try {
36572
+ return globalThis.localStorage;
36573
+ } catch {
36574
+ return void 0;
36575
+ }
36576
+ }
36577
+ function getStoredDebugFlag() {
36578
+ const storage = getLocalStorage();
36579
+ if (!storage) {
36580
+ return void 0;
36581
+ }
36582
+ try {
36583
+ return storage.getItem(DEBUG_FLAG);
36584
+ } catch {
36585
+ return void 0;
36586
+ }
36587
+ }
36588
+ function setStoredDebugFlag(enabled) {
36589
+ const storage = getLocalStorage();
36590
+ if (!storage) {
36591
+ return;
36592
+ }
36593
+ try {
36594
+ if (enabled) {
36595
+ storage.setItem(DEBUG_FLAG, "true");
36596
+ } else {
36597
+ storage.removeItem(DEBUG_FLAG);
36598
+ }
36599
+ } catch {
36600
+ }
36601
+ }
36602
+ function shouldStartEnabled() {
36603
+ const global2 = getGlobal();
36604
+ return isTrue(global2[DEBUG_FLAG]) || isTrue(getStoredDebugFlag()) || isTrue(getProcessDebugFlag());
36605
+ }
36606
+ var TinyCloudDebugLogger = class {
36607
+ constructor() {
36608
+ this.enabled = shouldStartEnabled();
36609
+ this.sequence = 0;
36610
+ this.events = [];
36611
+ this.maxEvents = MAX_EVENTS;
36612
+ }
36613
+ isEnabled() {
36614
+ return this.enabled || isTrue(getGlobal()[DEBUG_FLAG]) || isTrue(getProcessDebugFlag());
36615
+ }
36616
+ enable(options = {}) {
36617
+ this.enabled = true;
36618
+ getGlobal()[DEBUG_FLAG] = true;
36619
+ if (options.persist !== false) {
36620
+ setStoredDebugFlag(true);
36621
+ }
36622
+ this.log("debug.enabled", { persisted: options.persist !== false });
36623
+ }
36624
+ disable(options = {}) {
36625
+ this.log("debug.disabled", { persisted: options.persist !== false });
36626
+ this.enabled = false;
36627
+ getGlobal()[DEBUG_FLAG] = false;
36628
+ if (options.persist !== false) {
36629
+ setStoredDebugFlag(false);
36630
+ }
36631
+ }
36632
+ clear() {
36633
+ this.events = [];
36634
+ }
36635
+ getLogs() {
36636
+ return [...this.events];
36637
+ }
36638
+ log(event, data, message) {
36639
+ if (!this.isEnabled()) {
36640
+ return void 0;
36641
+ }
36642
+ return this.record({
36643
+ event,
36644
+ data,
36645
+ message,
36646
+ level: "debug",
36647
+ timestamp: Date.now(),
36648
+ timestampIso: (/* @__PURE__ */ new Date()).toISOString()
36649
+ });
36650
+ }
36651
+ startTimer(event, data) {
36652
+ if (!this.isEnabled()) {
36653
+ return { stop: () => void 0 };
36654
+ }
36655
+ const startedAt = nowMs();
36656
+ this.log(`${event}.start`, data);
36657
+ return {
36658
+ stop: (finishData) => {
36659
+ const endedAt = nowMs();
36660
+ return this.record({
36661
+ event: `${event}.end`,
36662
+ data: finishData,
36663
+ durationMs: endedAt - startedAt,
36664
+ startedAt,
36665
+ endedAt,
36666
+ level: "debug",
36667
+ timestamp: Date.now(),
36668
+ timestampIso: (/* @__PURE__ */ new Date()).toISOString()
36669
+ });
36670
+ }
36671
+ };
36672
+ }
36673
+ async timeAsync(event, operation, data) {
36674
+ if (!this.isEnabled()) {
36675
+ return operation();
36676
+ }
36677
+ const timer2 = this.startTimer(event, data);
36678
+ try {
36679
+ const result = await operation();
36680
+ timer2.stop({ ok: true });
36681
+ return result;
36682
+ } catch (error) {
36683
+ timer2.stop({ ok: false, error });
36684
+ throw error;
36685
+ }
36686
+ }
36687
+ time(event, operation, data) {
36688
+ if (!this.isEnabled()) {
36689
+ return operation();
36690
+ }
36691
+ const timer2 = this.startTimer(event, data);
36692
+ try {
36693
+ const result = operation();
36694
+ timer2.stop({ ok: true });
36695
+ return result;
36696
+ } catch (error) {
36697
+ timer2.stop({ ok: false, error });
36698
+ throw error;
36699
+ }
36700
+ }
36701
+ record(event) {
36702
+ const debugEvent = {
36703
+ ...event,
36704
+ sequence: ++this.sequence
36705
+ };
36706
+ this.events.push(debugEvent);
36707
+ if (this.events.length > this.maxEvents) {
36708
+ this.events.splice(0, this.events.length - this.maxEvents);
36709
+ }
36710
+ try {
36711
+ globalThis.console?.debug?.("[TinyCloud]", debugEvent.event, debugEvent);
36712
+ } catch {
36713
+ }
36714
+ return debugEvent;
36715
+ }
36716
+ };
36717
+ var tinyCloudDebugLogger = new TinyCloudDebugLogger();
36718
+ function enableTinyCloudDebug(options) {
36719
+ tinyCloudDebugLogger.enable(options);
36720
+ return tinyCloudDebugLogger;
36721
+ }
36722
+ function disableTinyCloudDebug(options) {
36723
+ tinyCloudDebugLogger.disable(options);
36724
+ return tinyCloudDebugLogger;
36725
+ }
36726
+ function getTinyCloudDebugLogs() {
36727
+ return tinyCloudDebugLogger.getLogs();
36728
+ }
36729
+ function clearTinyCloudDebugLogs() {
36730
+ tinyCloudDebugLogger.clear();
36731
+ }
36732
+ function installTinyCloudDebugGlobals() {
36733
+ const global2 = getGlobal();
36734
+ global2.TinyCloudDebug = tinyCloudDebugLogger;
36735
+ global2.enableTinyCloudDebug = enableTinyCloudDebug;
36736
+ global2.disableTinyCloudDebug = disableTinyCloudDebug;
36737
+ global2.getTinyCloudDebugLogs = getTinyCloudDebugLogs;
36738
+ global2.clearTinyCloudDebugLogs = clearTinyCloudDebugLogs;
36739
+ }
36740
+ installTinyCloudDebugGlobals();
36547
36741
  var ServiceErrorSchema = external_exports.object({
36548
36742
  /** Error code for programmatic handling (e.g., 'KV_NOT_FOUND', 'AUTH_EXPIRED') */
36549
36743
  code: external_exports.string(),
@@ -42730,10 +42924,10 @@ function serviceFromActions(actions) {
42730
42924
 
42731
42925
  // src/lib/sdk.ts
42732
42926
  async function createSDKInstance(ctx, options) {
42733
- const profile = await ProfileManager.getProfile(ctx.profile);
42927
+ const profile = options?.privateKey ? await ProfileManager.getProfile(ctx.profile).catch(() => null) : await ProfileManager.getProfile(ctx.profile);
42734
42928
  const session = await ProfileManager.getSession(ctx.profile);
42735
42929
  const key2 = await ProfileManager.getKey(ctx.profile);
42736
- const effectivePrivateKey = options?.privateKey ?? profile.privateKey;
42930
+ const effectivePrivateKey = options?.privateKey ?? profile?.privateKey;
42737
42931
  if (!key2 && !effectivePrivateKey) {
42738
42932
  throw new CLIError(
42739
42933
  "AUTH_REQUIRED",
@@ -42741,7 +42935,7 @@ async function createSDKInstance(ctx, options) {
42741
42935
  ExitCode.AUTH_REQUIRED
42742
42936
  );
42743
42937
  }
42744
- if (profile.authMethod === "local" && effectivePrivateKey) {
42938
+ if (profile?.authMethod === "local" && effectivePrivateKey) {
42745
42939
  const node2 = new TinyCloudNode({
42746
42940
  host: ctx.host,
42747
42941
  privateKey: effectivePrivateKey
@@ -42752,7 +42946,7 @@ async function createSDKInstance(ctx, options) {
42752
42946
  delegationCid: session.delegationCid,
42753
42947
  spaceId: session.spaceId,
42754
42948
  jwk: session.jwk ?? key2,
42755
- verificationMethod: session.verificationMethod ?? profile.sessionDid ?? profile.did,
42949
+ verificationMethod: session.verificationMethod ?? profile?.sessionDid ?? profile?.did,
42756
42950
  address: session.address,
42757
42951
  chainId: session.chainId,
42758
42952
  siwe: session.siwe,
@@ -42776,7 +42970,7 @@ async function createSDKInstance(ctx, options) {
42776
42970
  delegationCid: session.delegationCid,
42777
42971
  spaceId: session.spaceId,
42778
42972
  jwk: session.jwk ?? key2,
42779
- verificationMethod: session.verificationMethod ?? profile.did,
42973
+ verificationMethod: session.verificationMethod ?? profile?.did,
42780
42974
  address: session.address,
42781
42975
  chainId: session.chainId,
42782
42976
  siwe: session.siwe,
@@ -42787,6 +42981,9 @@ async function createSDKInstance(ctx, options) {
42787
42981
  return node;
42788
42982
  }
42789
42983
  async function ensureAuthenticated(ctx, options) {
42984
+ if (options?.privateKey) {
42985
+ return createSDKInstance(ctx, options);
42986
+ }
42790
42987
  const profile = await ProfileManager.getProfile(ctx.profile).catch(() => null);
42791
42988
  if (profile?.authMethod === "local" && profile.privateKey) {
42792
42989
  return createSDKInstance(ctx, { privateKey: profile.privateKey });