shogun-core 4.2.2 → 4.2.4

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.
@@ -133841,6 +133841,7 @@ class DataBase {
133841
133841
  constructor(gun, appScope = "shogun") {
133842
133842
  this.user = null;
133843
133843
  this.onAuthCallbacks = [];
133844
+ console.log("[DB] Initializing DataBase");
133844
133845
  // Initialize event emitter
133845
133846
  this.eventEmitter = new eventEmitter_1.EventEmitter();
133846
133847
  // Validate Gun instance
@@ -133860,31 +133861,38 @@ class DataBase {
133860
133861
  throw new Error(`Gun instance is invalid: gun.on is not a function. Received gun.on type: ${typeof gun.on}`);
133861
133862
  }
133862
133863
  this.gun = gun;
133864
+ console.log("[DB] Gun instance validated");
133863
133865
  // Recall only if NOT disabled and there's a "pair" in sessionStorage
133864
133866
  this.user = this.gun.user().recall({ sessionStorage: true });
133867
+ console.log("[DB] User recall completed");
133865
133868
  this.subscribeToAuthEvents();
133869
+ console.log("[DB] Auth events subscribed");
133866
133870
  this.crypto = crypto;
133867
133871
  this.sea = sea_1.default;
133868
133872
  this._rxjs = new rxjs_1.RxJS(this.gun);
133869
133873
  this.node = null;
133874
+ console.log("[DB] DataBase initialization completed");
133870
133875
  }
133871
133876
  /**
133872
133877
  * Initialize the GunInstance asynchronously
133873
133878
  * This method should be called after construction to perform async operations
133874
133879
  */
133875
133880
  initialize(appScope = "shogun") {
133881
+ console.log(`[DB] Initializing with appScope: ${appScope}`);
133876
133882
  try {
133877
133883
  const sessionResult = this.restoreSession();
133884
+ console.log(`[DB] Session restore result: ${sessionResult.success ? "success" : "failed"}`);
133878
133885
  this.node = this.gun.get(appScope);
133886
+ console.log("[DB] App scope node initialized");
133879
133887
  if (sessionResult.success) {
133880
- // Session automatically restored
133888
+ console.log("[DB] Session automatically restored");
133881
133889
  }
133882
133890
  else {
133883
- // No previous session to restore
133891
+ console.log("[DB] No previous session to restore");
133884
133892
  }
133885
133893
  }
133886
133894
  catch (error) {
133887
- console.error("Error during automatic session restoration:", error);
133895
+ console.error("[DB] Error during automatic session restoration:", error);
133888
133896
  }
133889
133897
  }
133890
133898
  subscribeToAuthEvents() {
@@ -133907,13 +133915,16 @@ class DataBase {
133907
133915
  * @param peer URL of the peer to add
133908
133916
  */
133909
133917
  addPeer(peer) {
133918
+ console.log(`[PEER] Adding peer: ${peer}`);
133910
133919
  this.gun.opt({ peers: [peer] });
133920
+ console.log(`[PEER] Peer added successfully`);
133911
133921
  }
133912
133922
  /**
133913
133923
  * Removes a peer from the network
133914
133924
  * @param peer URL of the peer to remove
133915
133925
  */
133916
133926
  removePeer(peer) {
133927
+ console.log(`[PEER] Removing peer: ${peer}`);
133917
133928
  try {
133918
133929
  // Get current peers from Gun instance
133919
133930
  const gunOpts = this.gun._.opt;
@@ -133925,13 +133936,14 @@ class DataBase {
133925
133936
  if (peerConnection && typeof peerConnection.close === "function") {
133926
133937
  peerConnection.close();
133927
133938
  }
133939
+ console.log(`[PEER] Peer removed successfully`);
133928
133940
  }
133929
133941
  else {
133930
- console.error(`Peer not found in current connections: ${peer}`);
133942
+ console.error(`[PEER] Peer not found in current connections: ${peer}`);
133931
133943
  }
133932
133944
  }
133933
133945
  catch (error) {
133934
- console.error(`Error removing peer ${peer}:`, error);
133946
+ console.error(`[PEER] Error removing peer ${peer}:`, error);
133935
133947
  }
133936
133948
  }
133937
133949
  /**
@@ -134664,18 +134676,20 @@ class DataBase {
134664
134676
  });
134665
134677
  }
134666
134678
  async runPostAuthOnAuthResult(username, userPub, authResult) {
134667
- // Setting up user profile after authentication
134679
+ console.log(`[POSTAUTH] Starting post-auth setup for user: ${username}, userPub: ${userPub}`);
134668
134680
  try {
134681
+ console.log(`[POSTAUTH] Validating parameters for user: ${username}`);
134669
134682
  // Validate required parameters
134670
134683
  if (!username ||
134671
134684
  typeof username !== "string" ||
134672
134685
  username.trim().length === 0) {
134686
+ console.error(`[POSTAUTH] Invalid username provided: ${username}`);
134673
134687
  throw new Error("Invalid username provided");
134674
134688
  }
134675
134689
  if (!userPub ||
134676
134690
  typeof userPub !== "string" ||
134677
134691
  userPub.trim().length === 0) {
134678
- console.error("Invalid userPub provided:", {
134692
+ console.error("[POSTAUTH] Invalid userPub provided:", {
134679
134693
  userPub,
134680
134694
  type: typeof userPub,
134681
134695
  authResult,
@@ -134684,30 +134698,38 @@ class DataBase {
134684
134698
  }
134685
134699
  // Additional validation for userPub format
134686
134700
  if (!userPub.includes(".") || userPub.length < 10) {
134687
- console.error("Invalid userPub format:", userPub);
134701
+ console.error(`[POSTAUTH] Invalid userPub format: ${userPub}`);
134688
134702
  throw new Error("Invalid userPub format");
134689
134703
  }
134704
+ console.log(`[POSTAUTH] Parameters validated for user: ${username}`);
134690
134705
  // Normalize username to prevent path issues
134691
134706
  const normalizedUsername = username.trim().toLowerCase();
134692
134707
  if (normalizedUsername.length === 0) {
134708
+ console.error(`[POSTAUTH] Normalized username is empty for user: ${username}`);
134693
134709
  throw new Error("Username cannot be empty");
134694
134710
  }
134711
+ console.log(`[POSTAUTH] Normalized username: ${normalizedUsername}`);
134712
+ console.log(`[POSTAUTH] Checking if user exists: ${userPub}`);
134695
134713
  const existingUser = await this.gun.get(userPub).then();
134696
134714
  const isNewUser = !existingUser || !existingUser.alias;
134697
- // const isNewUser = true;
134715
+ console.log(`[POSTAUTH] User is ${isNewUser ? "NEW" : "EXISTING"}: ${userPub}`);
134698
134716
  // Get user's encryption public key (epub) for comprehensive tracking
134699
134717
  const userInstance = this.gun.user();
134700
134718
  const userSea = userInstance?._?.sea;
134701
134719
  const epub = userSea?.epub;
134720
+ console.log(`[POSTAUTH] User epub retrieved: ${epub ? "yes" : "no"}`);
134702
134721
  // Enhanced user tracking system
134722
+ console.log(`[POSTAUTH] Setting up comprehensive user tracking for: ${normalizedUsername}`);
134703
134723
  const trackingResult = await this.setupComprehensiveUserTracking(normalizedUsername, userPub, epub);
134704
134724
  if (!trackingResult) {
134725
+ console.error(`[POSTAUTH] Comprehensive user tracking setup failed for: ${normalizedUsername}`);
134705
134726
  return {
134706
134727
  success: false,
134707
134728
  error: "Comprehensive user tracking setup failed",
134708
134729
  };
134709
134730
  }
134710
- return {
134731
+ console.log(`[POSTAUTH] User tracking setup completed successfully for: ${normalizedUsername}`);
134732
+ const result = {
134711
134733
  success: true,
134712
134734
  userPub: userPub,
134713
134735
  username: normalizedUsername,
@@ -134722,9 +134744,11 @@ class DataBase {
134722
134744
  }
134723
134745
  : undefined,
134724
134746
  };
134747
+ console.log(`[POSTAUTH] Post-auth setup completed successfully for user: ${username}`);
134748
+ return result;
134725
134749
  }
134726
134750
  catch (error) {
134727
- console.error(`Error in post-authentication setup: ${error}`);
134751
+ console.error(`[POSTAUTH] Error in post-authentication setup for ${username}:`, error);
134728
134752
  return {
134729
134753
  success: false,
134730
134754
  error: `Post-authentication setup failed: ${error}`,
@@ -134736,43 +134760,66 @@ class DataBase {
134736
134760
  * Creates multiple indexes for efficient user discovery
134737
134761
  */
134738
134762
  async setupComprehensiveUserTracking(username, userPub, epub) {
134763
+ console.log(`[TRACKING] Starting comprehensive user tracking setup for: ${username}, userPub: ${userPub}`);
134739
134764
  try {
134740
134765
  // 1. Create alias index: ~@alias -> userPub (for GunDB compatibility)
134766
+ console.log(`[TRACKING] Step 1: Creating alias index for ${username}`);
134741
134767
  const aliasIndexResult = await this.createAliasIndex(username, userPub);
134742
134768
  if (!aliasIndexResult) {
134769
+ console.error(`[TRACKING] Failed to create alias index for ${username}`);
134743
134770
  return false;
134744
134771
  }
134772
+ console.log(`[TRACKING] Step 1 completed: Alias index created for ${username}`);
134745
134773
  // 2. Create username mapping: usernames/alias -> userPub
134774
+ console.log(`[TRACKING] Step 2: Creating username mapping for ${username}`);
134746
134775
  const usernameMappingResult = await this.createUsernameMapping(username, userPub);
134747
134776
  if (!usernameMappingResult) {
134777
+ console.error(`[TRACKING] Failed to create username mapping for ${username}`);
134748
134778
  return false;
134749
134779
  }
134780
+ console.log(`[TRACKING] Step 2 completed: Username mapping created for ${username}`);
134750
134781
  // 3. Create user registry: users/userPub -> user data
134782
+ console.log(`[TRACKING] Step 3: Creating user registry for ${username}`);
134751
134783
  const userRegistryResult = await this.createUserRegistry(username, userPub, epub);
134752
134784
  if (!userRegistryResult) {
134785
+ console.error(`[TRACKING] Failed to create user registry for ${username}`);
134753
134786
  return false;
134754
134787
  }
134788
+ console.log(`[TRACKING] Step 3 completed: User registry created for ${username}`);
134755
134789
  // 4. Create reverse lookup: userPub -> alias
134790
+ console.log(`[TRACKING] Step 4: Creating reverse lookup for ${username}`);
134756
134791
  const reverseLookupResult = await this.createReverseLookup(username, userPub);
134757
134792
  if (!reverseLookupResult) {
134793
+ console.error(`[TRACKING] Failed to create reverse lookup for ${username}`);
134758
134794
  return false;
134759
134795
  }
134796
+ console.log(`[TRACKING] Step 4 completed: Reverse lookup created for ${username}`);
134760
134797
  // 5. Create epub index: epubKeys/epub -> userPub (for encryption lookups)
134761
134798
  if (epub) {
134799
+ console.log(`[TRACKING] Step 5: Creating epub index for ${username}`);
134762
134800
  const epubIndexResult = await this.createEpubIndex(epub, userPub);
134763
134801
  if (!epubIndexResult) {
134802
+ console.error(`[TRACKING] Failed to create epub index for ${username}`);
134764
134803
  return false;
134765
134804
  }
134805
+ console.log(`[TRACKING] Step 5 completed: Epub index created for ${username}`);
134806
+ }
134807
+ else {
134808
+ console.log(`[TRACKING] Step 5 skipped: No epub available for ${username}`);
134766
134809
  }
134767
134810
  // 6. Create user metadata in user's own node
134811
+ console.log(`[TRACKING] Step 6: Creating user metadata for ${username}`);
134768
134812
  const userMetadataResult = await this.createUserMetadata(username, userPub, epub);
134769
134813
  if (!userMetadataResult) {
134814
+ console.error(`[TRACKING] Failed to create user metadata for ${username}`);
134770
134815
  return false;
134771
134816
  }
134817
+ console.log(`[TRACKING] Step 6 completed: User metadata created for ${username}`);
134818
+ console.log(`[TRACKING] Comprehensive user tracking setup completed successfully for: ${username}`);
134772
134819
  return true;
134773
134820
  }
134774
134821
  catch (error) {
134775
- console.error(`Error in comprehensive user tracking setup: ${error}`);
134822
+ console.error(`[TRACKING] Error in comprehensive user tracking setup for ${username}:`, error);
134776
134823
  // Don't throw - continue with other operations
134777
134824
  return false;
134778
134825
  }