shogun-core 6.4.3 โ†’ 6.4.5

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.
@@ -123242,17 +123242,36 @@ var db_generator = (undefined && undefined.__generator) || function (thisArg, bo
123242
123242
 
123243
123243
 
123244
123244
 
123245
+ /**
123246
+ * GunDB configuration constants.
123247
+ * @internal
123248
+ */
123245
123249
  var CONFIG = {
123246
123250
  PASSWORD: {
123247
123251
  MIN_LENGTH: 8,
123248
123252
  },
123249
123253
  };
123254
+ /**
123255
+ * DataBase
123256
+ *
123257
+ * Manages GunDB user authentication and various utility helpers for
123258
+ * session, alias/username, SEA cryptography, event handling, and reactive streams.
123259
+ */
123250
123260
  var DataBase = /** @class */ (function () {
123251
- function DataBase(gun, appScope, core, sea) {
123252
- if (appScope === void 0) { appScope = "shogun"; }
123261
+ /**
123262
+ * Constructs a new DataBase instance connected to a GunDB instance.
123263
+ * @param gun The main GunDB instance.
123264
+ * @param core Optionally, the root Gun instance (unused in this context).
123265
+ * @param sea Optional cryptography (Gun SEA) instance; will be auto-discovered if not provided.
123266
+ * @throws If gun or gun.user() is not provided.
123267
+ */
123268
+ function DataBase(gun, core, sea) {
123253
123269
  var _a;
123270
+ /** Cached user instance or `null` if not logged in */
123254
123271
  this.user = null;
123272
+ /** Registered callbacks for auth state changes */
123255
123273
  this.onAuthCallbacks = [];
123274
+ /** Whether the database instance has been destroyed */
123256
123275
  this._isDestroyed = false;
123257
123276
  this.eventEmitter = new EventEmitter();
123258
123277
  if (!gun) {
@@ -123278,14 +123297,20 @@ var DataBase = /** @class */ (function () {
123278
123297
  }
123279
123298
  }
123280
123299
  this._rxjs = new RxJS(this.gun);
123281
- this.node = this.gun.get(appScope);
123282
123300
  this.usernamesNode = this.gun.get("usernames");
123283
123301
  console.log("[DB] DataBase initialization completed");
123284
123302
  }
123285
- DataBase.prototype.initialize = function (appScope) {
123286
- if (appScope === void 0) { appScope = "shogun"; }
123287
- this.node = this.gun.get(appScope);
123303
+ /**
123304
+ * Initialize the database instance.
123305
+ */
123306
+ DataBase.prototype.initialize = function () {
123307
+ // Database is already initialized in constructor
123288
123308
  };
123309
+ /**
123310
+ * Internal: subscribe to GunDB "auth" events and notify listeners.
123311
+ * Listeners are invoked on authentication status change.
123312
+ * @internal
123313
+ */
123289
123314
  DataBase.prototype.subscribeToAuthEvents = function () {
123290
123315
  var _this = this;
123291
123316
  this.gun.on("auth", function (ack) {
@@ -123298,10 +123323,20 @@ var DataBase = /** @class */ (function () {
123298
123323
  }
123299
123324
  });
123300
123325
  };
123326
+ /**
123327
+ * Internal: notify all onAuth callbacks with current user.
123328
+ * @param pub User's public key (pub).
123329
+ * @internal
123330
+ */
123301
123331
  DataBase.prototype.notifyAuthListeners = function (pub) {
123302
123332
  var user = this.gun.user();
123303
123333
  this.onAuthCallbacks.forEach(function (cb) { return cb(user); });
123304
123334
  };
123335
+ /**
123336
+ * Listen for authentication/sign-in events (login, logout, etc).
123337
+ * @param callback Function to call with new user instance.
123338
+ * @returns Function to remove the registered callback.
123339
+ */
123305
123340
  DataBase.prototype.onAuth = function (callback) {
123306
123341
  var _this = this;
123307
123342
  this.onAuthCallbacks.push(callback);
@@ -123314,6 +123349,10 @@ var DataBase = /** @class */ (function () {
123314
123349
  _this.onAuthCallbacks.splice(i, 1);
123315
123350
  };
123316
123351
  };
123352
+ /**
123353
+ * Check if a user is currently logged in (there is a valid session).
123354
+ * @returns `true` if logged in; otherwise `false`.
123355
+ */
123317
123356
  DataBase.prototype.isLoggedIn = function () {
123318
123357
  try {
123319
123358
  var user = this.gun.user();
@@ -123323,6 +123362,10 @@ var DataBase = /** @class */ (function () {
123323
123362
  return false;
123324
123363
  }
123325
123364
  };
123365
+ /**
123366
+ * Attempt to restore a previously saved session from sessionStorage.
123367
+ * @returns Object indicating success, error, and userPub if restored.
123368
+ */
123326
123369
  DataBase.prototype.restoreSession = function () {
123327
123370
  try {
123328
123371
  if (typeof sessionStorage === "undefined") {
@@ -123353,6 +123396,9 @@ var DataBase = /** @class */ (function () {
123353
123396
  return { success: false, error: String(error) };
123354
123397
  }
123355
123398
  };
123399
+ /**
123400
+ * Log out the current user, clear local state and remove session from storage.
123401
+ */
123356
123402
  DataBase.prototype.logout = function () {
123357
123403
  try {
123358
123404
  var currentUser = this.gun.user();
@@ -123368,6 +123414,11 @@ var DataBase = /** @class */ (function () {
123368
123414
  console.error("[DB] Error during logout:", error);
123369
123415
  }
123370
123416
  };
123417
+ /**
123418
+ * Validate that a provided password meets minimum length requirements.
123419
+ * @param password Password string to validate.
123420
+ * @returns Object indicating validity and, if invalid, an error.
123421
+ */
123371
123422
  DataBase.prototype.validatePasswordStrength = function (password) {
123372
123423
  if (password.length < CONFIG.PASSWORD.MIN_LENGTH) {
123373
123424
  return {
@@ -123377,6 +123428,13 @@ var DataBase = /** @class */ (function () {
123377
123428
  }
123378
123429
  return { valid: true };
123379
123430
  };
123431
+ /**
123432
+ * Validate a signup request's username, password, and/or cryptographic pair.
123433
+ * @param username Username string.
123434
+ * @param password Password string.
123435
+ * @param pair Optional cryptographic SEA pair.
123436
+ * @returns Object with validation status and optional error.
123437
+ */
123380
123438
  DataBase.prototype.validateSignupCredentials = function (username, password, pair) {
123381
123439
  if (!username || username.length < 1) {
123382
123440
  return {
@@ -123398,6 +123456,12 @@ var DataBase = /** @class */ (function () {
123398
123456
  }
123399
123457
  return this.validatePasswordStrength(password);
123400
123458
  };
123459
+ /**
123460
+ * Ensures that an alias/username is available in GunDB for registration.
123461
+ * @param alias Username to check.
123462
+ * @param timeout Timeout in milliseconds (default 5000ms).
123463
+ * @throws If the alias is already taken.
123464
+ */
123401
123465
  DataBase.prototype.ensureAliasAvailable = function (alias_1) {
123402
123466
  return db_awaiter(this, arguments, void 0, function (alias, timeout) {
123403
123467
  var available;
@@ -123415,6 +123479,13 @@ var DataBase = /** @class */ (function () {
123415
123479
  });
123416
123480
  });
123417
123481
  };
123482
+ /**
123483
+ * Checks if a given alias/username is available on GunDB.
123484
+ * @param alias Username to check for availability.
123485
+ * @param timeout Timeout in ms (default: 5000).
123486
+ * @returns Promise resolving to `true` if available; otherwise `false`.
123487
+ * @throws If alias is invalid or on I/O error.
123488
+ */
123418
123489
  DataBase.prototype.isAliasAvailable = function (alias_1) {
123419
123490
  return db_awaiter(this, arguments, void 0, function (alias, timeout) {
123420
123491
  var normalizedAlias;
@@ -123444,9 +123515,39 @@ var DataBase = /** @class */ (function () {
123444
123515
  });
123445
123516
  });
123446
123517
  };
123518
+ /**
123519
+ * Checks if a given alias/username is taken on GunDB.
123520
+ * @param alias Username to check for availability.
123521
+ * @returns Promise resolving to `true` if taken; otherwise `false`.
123522
+ * @throws If alias is invalid or on I/O error.
123523
+ */
123524
+ DataBase.prototype.isAliasTaken = function (alias) {
123525
+ return db_awaiter(this, void 0, void 0, function () {
123526
+ var _this = this;
123527
+ return db_generator(this, function (_a) {
123528
+ return [2 /*return*/, new Promise(function (resolve, reject) {
123529
+ _this.gun.get("~@".concat(alias)).once(function (user) {
123530
+ if (user) {
123531
+ resolve(false);
123532
+ }
123533
+ else {
123534
+ resolve(true);
123535
+ }
123536
+ });
123537
+ })];
123538
+ });
123539
+ });
123540
+ };
123541
+ /**
123542
+ * Register a new alias (username) โ†’ public key mapping on GunDB.
123543
+ * @param alias The username/alias to register.
123544
+ * @param userPub The user's public key.
123545
+ * @param timeout Timeout in ms (default 5000).
123546
+ * @throws If alias/userPub is invalid or the alias cannot be registered.
123547
+ */
123447
123548
  DataBase.prototype.registerAlias = function (alias_1, userPub_1) {
123448
123549
  return db_awaiter(this, arguments, void 0, function (alias, userPub, timeout) {
123449
- var normalizedAlias, available;
123550
+ var normalizedAlias, available, taken;
123450
123551
  var _this = this;
123451
123552
  if (timeout === void 0) { timeout = 5000; }
123452
123553
  return db_generator(this, function (_a) {
@@ -123465,6 +123566,12 @@ var DataBase = /** @class */ (function () {
123465
123566
  })];
123466
123567
  case 1:
123467
123568
  available = _a.sent();
123569
+ return [4 /*yield*/, this.isAliasTaken(normalizedAlias)];
123570
+ case 2:
123571
+ taken = _a.sent();
123572
+ if (taken) {
123573
+ throw new Error("Alias \"".concat(normalizedAlias, "\" is already taken"));
123574
+ }
123468
123575
  if (!available) {
123469
123576
  throw new Error("Alias \"".concat(normalizedAlias, "\" is no longer available for registration"));
123470
123577
  }
@@ -123491,13 +123598,17 @@ var DataBase = /** @class */ (function () {
123491
123598
  console.error("[DB] Failed to register alias:", error);
123492
123599
  throw error;
123493
123600
  })];
123494
- case 2:
123601
+ case 3:
123495
123602
  _a.sent();
123496
123603
  return [2 /*return*/];
123497
123604
  }
123498
123605
  });
123499
123606
  });
123500
123607
  };
123608
+ /**
123609
+ * Reset gun.user() authentication state and clear cached user.
123610
+ * @internal
123611
+ */
123501
123612
  DataBase.prototype.resetAuthState = function () {
123502
123613
  try {
123503
123614
  var user = this.gun.user();
@@ -123522,6 +123633,13 @@ var DataBase = /** @class */ (function () {
123522
123633
  // Ignore
123523
123634
  }
123524
123635
  };
123636
+ /**
123637
+ * Assemble a standard AuthResult object after a successful login.
123638
+ * @param username Resulting username.
123639
+ * @param userPub Public key (pub) for logged-in user.
123640
+ * @returns AuthResult.
123641
+ * @internal
123642
+ */
123525
123643
  DataBase.prototype.buildLoginResult = function (username, userPub) {
123526
123644
  var _a, _b;
123527
123645
  var seaPair = (_b = (_a = this.gun.user()) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.sea;
@@ -123539,6 +123657,10 @@ var DataBase = /** @class */ (function () {
123539
123657
  : undefined,
123540
123658
  };
123541
123659
  };
123660
+ /**
123661
+ * Save credentials for the current session to sessionStorage, if available.
123662
+ * @param userInfo The credentials and user identity to store.
123663
+ */
123542
123664
  DataBase.prototype.saveCredentials = function (userInfo) {
123543
123665
  try {
123544
123666
  if (typeof sessionStorage !== "undefined") {
@@ -123556,6 +123678,13 @@ var DataBase = /** @class */ (function () {
123556
123678
  console.error("[DB] Error saving credentials:", error);
123557
123679
  }
123558
123680
  };
123681
+ /**
123682
+ * Register and authenticate a new user account.
123683
+ * @param username The username to create/account for.
123684
+ * @param password The user's password.
123685
+ * @param pair Optional cryptographic pair (for `auth` instead of password).
123686
+ * @returns SignUpResult Promise.
123687
+ */
123559
123688
  DataBase.prototype.signUp = function (username, password, pair) {
123560
123689
  return db_awaiter(this, void 0, void 0, function () {
123561
123690
  var validation, normalizedUsername, user, loginResult, e_1, aliasError_1, result;
@@ -123727,6 +123856,13 @@ var DataBase = /** @class */ (function () {
123727
123856
  });
123728
123857
  });
123729
123858
  };
123859
+ /**
123860
+ * Sign in (authenticate) as an existing user by username/password or SEA pair.
123861
+ * @param username Username to log in as.
123862
+ * @param password User's password (or "" if using pair).
123863
+ * @param pair Optional cryptographic SEA pair.
123864
+ * @returns AuthResult Promise.
123865
+ */
123730
123866
  DataBase.prototype.login = function (username, password, pair) {
123731
123867
  return db_awaiter(this, void 0, void 0, function () {
123732
123868
  var normalizedUsername, user;
@@ -123800,6 +123936,10 @@ var DataBase = /** @class */ (function () {
123800
123936
  });
123801
123937
  });
123802
123938
  };
123939
+ /**
123940
+ * Returns the currently authenticated user's public key and Gun user instance, if logged in.
123941
+ * @returns Object containing `pub` (public key) and optionally `user`, or `null`.
123942
+ */
123803
123943
  DataBase.prototype.getCurrentUser = function () {
123804
123944
  try {
123805
123945
  var user = this.gun.user();
@@ -123816,8 +123956,8 @@ var DataBase = /** @class */ (function () {
123816
123956
  }
123817
123957
  };
123818
123958
  /**
123819
- * Get current user's public key
123820
- * @returns {string | null} User's public key or null if not logged in
123959
+ * Get current user's public key.
123960
+ * @returns User's public key or null if not logged in.
123821
123961
  */
123822
123962
  DataBase.prototype.getUserPub = function () {
123823
123963
  var _a;
@@ -123830,11 +123970,11 @@ var DataBase = /** @class */ (function () {
123830
123970
  }
123831
123971
  };
123832
123972
  /**
123833
- * Login with SEA pair directly
123834
- * @param username - Username for identification
123835
- * @param pair - GunDB SEA pair for authentication
123836
- * @returns {Promise<AuthResult>} Promise with authentication result
123837
- * @description Authenticates user using a GunDB pair directly without password
123973
+ * Authenticate using a SEA pair directly (no password required).
123974
+ * @param username The user's username for identification (not cryptographically enforced).
123975
+ * @param pair GunDB SEA pair for authentication.
123976
+ * @returns Promise with authentication result.
123977
+ * @description Authenticates user using a GunDB pair directly without password.
123838
123978
  */
123839
123979
  DataBase.prototype.loginWithPair = function (username, pair) {
123840
123980
  return db_awaiter(this, void 0, void 0, function () {
@@ -123885,6 +124025,12 @@ var DataBase = /** @class */ (function () {
123885
124025
  });
123886
124026
  });
123887
124027
  };
124028
+ /**
124029
+ * Legacy API: Sign in using a username and SEA pair (password parameter is unused).
124030
+ * @param username Username to sign in as.
124031
+ * @param pair SEA key pair.
124032
+ * @returns AuthResult Promise.
124033
+ */
123888
124034
  DataBase.prototype.loginWithPairLegacy = function (username, pair) {
123889
124035
  return db_awaiter(this, void 0, void 0, function () {
123890
124036
  return db_generator(this, function (_a) {
@@ -123892,9 +124038,17 @@ var DataBase = /** @class */ (function () {
123892
124038
  });
123893
124039
  });
123894
124040
  };
124041
+ /**
124042
+ * Returns the bound RxJS GunDB helper (reactive streams).
124043
+ * @returns RxJS instance.
124044
+ */
123895
124045
  DataBase.prototype.rx = function () {
123896
124046
  return this._rxjs;
123897
124047
  };
124048
+ /**
124049
+ * Tears down the DataBase instance and performs cleanup of all resources/listeners.
124050
+ * No further actions should be performed on this instance after destruction.
124051
+ */
123898
124052
  DataBase.prototype.destroy = function () {
123899
124053
  if (this._isDestroyed)
123900
124054
  return;
@@ -123914,21 +124068,45 @@ var DataBase = /** @class */ (function () {
123914
124068
  this._rxjs = undefined;
123915
124069
  console.log("[DB] DataBase instance destroyed");
123916
124070
  };
124071
+ /**
124072
+ * Aggressively clean up authentication state and session. Typically used for error recovery.
124073
+ */
123917
124074
  DataBase.prototype.aggressiveAuthCleanup = function () {
123918
124075
  console.log("๐Ÿงน Performing aggressive auth cleanup...");
123919
124076
  this.resetAuthState();
123920
124077
  this.logout();
123921
124078
  console.log("โœ“ Aggressive auth cleanup completed");
123922
124079
  };
124080
+ /**
124081
+ * Register an event handler.
124082
+ * @param event Event name.
124083
+ * @param listener Listener function.
124084
+ */
123923
124085
  DataBase.prototype.on = function (event, listener) {
123924
124086
  this.eventEmitter.on(event, listener);
123925
124087
  };
124088
+ /**
124089
+ * Remove an event handler.
124090
+ * @param event Event name.
124091
+ * @param listener Listener function.
124092
+ */
123926
124093
  DataBase.prototype.off = function (event, listener) {
123927
124094
  this.eventEmitter.off(event, listener);
123928
124095
  };
124096
+ /**
124097
+ * Register an event handler for a single event occurrence.
124098
+ * @param event Event name.
124099
+ * @param listener Listener function.
124100
+ */
123929
124101
  DataBase.prototype.once = function (event, listener) {
123930
124102
  this.eventEmitter.once(event, listener);
123931
124103
  };
124104
+ /**
124105
+ * Emit a custom event.
124106
+ * @param event Event name.
124107
+ * @param data Optional associated data.
124108
+ * @returns `true` if listeners were notified; otherwise `false`.
124109
+ */
123932
124110
  DataBase.prototype.emit = function (event, data) {
123933
124111
  return this.eventEmitter.emit(event, data);
123934
124112
  };
@@ -124084,8 +124262,7 @@ var CoreInitializer = /** @class */ (function () {
124084
124262
  sea = __webpack_require__.g.Gun.SEA;
124085
124263
  }
124086
124264
  }
124087
- this.core.db = new DataBase(this.core._gun, "shogun", // Default app scope
124088
- this.core, sea);
124265
+ this.core.db = new DataBase(this.core._gun, this.core, sea);
124089
124266
  return true;
124090
124267
  }
124091
124268
  catch (error) {