shogun-core 6.4.3 โ†’ 6.4.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.
@@ -123242,17 +123242,38 @@ 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 () {
123261
+ /**
123262
+ * Constructs a new DataBase instance connected to a GunDB instance.
123263
+ * @param gun The main GunDB instance.
123264
+ * @param appScope The namespace under which data/nodes are stored. Default: "shogun"
123265
+ * @param core Optionally, the root Gun instance (unused in this context).
123266
+ * @param sea Optional cryptography (Gun SEA) instance; will be auto-discovered if not provided.
123267
+ * @throws If gun or gun.user() is not provided.
123268
+ */
123251
123269
  function DataBase(gun, appScope, core, sea) {
123252
123270
  if (appScope === void 0) { appScope = "shogun"; }
123253
123271
  var _a;
123272
+ /** Cached user instance or `null` if not logged in */
123254
123273
  this.user = null;
123274
+ /** Registered callbacks for auth state changes */
123255
123275
  this.onAuthCallbacks = [];
123276
+ /** Whether the database instance has been destroyed */
123256
123277
  this._isDestroyed = false;
123257
123278
  this.eventEmitter = new EventEmitter();
123258
123279
  if (!gun) {
@@ -123282,10 +123303,19 @@ var DataBase = /** @class */ (function () {
123282
123303
  this.usernamesNode = this.gun.get("usernames");
123283
123304
  console.log("[DB] DataBase initialization completed");
123284
123305
  }
123306
+ /**
123307
+ * Re-initialize with a different app scope/namespace.
123308
+ * @param appScope The new namespace.
123309
+ */
123285
123310
  DataBase.prototype.initialize = function (appScope) {
123286
123311
  if (appScope === void 0) { appScope = "shogun"; }
123287
123312
  this.node = this.gun.get(appScope);
123288
123313
  };
123314
+ /**
123315
+ * Internal: subscribe to GunDB "auth" events and notify listeners.
123316
+ * Listeners are invoked on authentication status change.
123317
+ * @internal
123318
+ */
123289
123319
  DataBase.prototype.subscribeToAuthEvents = function () {
123290
123320
  var _this = this;
123291
123321
  this.gun.on("auth", function (ack) {
@@ -123298,10 +123328,20 @@ var DataBase = /** @class */ (function () {
123298
123328
  }
123299
123329
  });
123300
123330
  };
123331
+ /**
123332
+ * Internal: notify all onAuth callbacks with current user.
123333
+ * @param pub User's public key (pub).
123334
+ * @internal
123335
+ */
123301
123336
  DataBase.prototype.notifyAuthListeners = function (pub) {
123302
123337
  var user = this.gun.user();
123303
123338
  this.onAuthCallbacks.forEach(function (cb) { return cb(user); });
123304
123339
  };
123340
+ /**
123341
+ * Listen for authentication/sign-in events (login, logout, etc).
123342
+ * @param callback Function to call with new user instance.
123343
+ * @returns Function to remove the registered callback.
123344
+ */
123305
123345
  DataBase.prototype.onAuth = function (callback) {
123306
123346
  var _this = this;
123307
123347
  this.onAuthCallbacks.push(callback);
@@ -123314,6 +123354,10 @@ var DataBase = /** @class */ (function () {
123314
123354
  _this.onAuthCallbacks.splice(i, 1);
123315
123355
  };
123316
123356
  };
123357
+ /**
123358
+ * Check if a user is currently logged in (there is a valid session).
123359
+ * @returns `true` if logged in; otherwise `false`.
123360
+ */
123317
123361
  DataBase.prototype.isLoggedIn = function () {
123318
123362
  try {
123319
123363
  var user = this.gun.user();
@@ -123323,6 +123367,10 @@ var DataBase = /** @class */ (function () {
123323
123367
  return false;
123324
123368
  }
123325
123369
  };
123370
+ /**
123371
+ * Attempt to restore a previously saved session from sessionStorage.
123372
+ * @returns Object indicating success, error, and userPub if restored.
123373
+ */
123326
123374
  DataBase.prototype.restoreSession = function () {
123327
123375
  try {
123328
123376
  if (typeof sessionStorage === "undefined") {
@@ -123353,6 +123401,9 @@ var DataBase = /** @class */ (function () {
123353
123401
  return { success: false, error: String(error) };
123354
123402
  }
123355
123403
  };
123404
+ /**
123405
+ * Log out the current user, clear local state and remove session from storage.
123406
+ */
123356
123407
  DataBase.prototype.logout = function () {
123357
123408
  try {
123358
123409
  var currentUser = this.gun.user();
@@ -123368,6 +123419,11 @@ var DataBase = /** @class */ (function () {
123368
123419
  console.error("[DB] Error during logout:", error);
123369
123420
  }
123370
123421
  };
123422
+ /**
123423
+ * Validate that a provided password meets minimum length requirements.
123424
+ * @param password Password string to validate.
123425
+ * @returns Object indicating validity and, if invalid, an error.
123426
+ */
123371
123427
  DataBase.prototype.validatePasswordStrength = function (password) {
123372
123428
  if (password.length < CONFIG.PASSWORD.MIN_LENGTH) {
123373
123429
  return {
@@ -123377,6 +123433,13 @@ var DataBase = /** @class */ (function () {
123377
123433
  }
123378
123434
  return { valid: true };
123379
123435
  };
123436
+ /**
123437
+ * Validate a signup request's username, password, and/or cryptographic pair.
123438
+ * @param username Username string.
123439
+ * @param password Password string.
123440
+ * @param pair Optional cryptographic SEA pair.
123441
+ * @returns Object with validation status and optional error.
123442
+ */
123380
123443
  DataBase.prototype.validateSignupCredentials = function (username, password, pair) {
123381
123444
  if (!username || username.length < 1) {
123382
123445
  return {
@@ -123398,6 +123461,12 @@ var DataBase = /** @class */ (function () {
123398
123461
  }
123399
123462
  return this.validatePasswordStrength(password);
123400
123463
  };
123464
+ /**
123465
+ * Ensures that an alias/username is available in GunDB for registration.
123466
+ * @param alias Username to check.
123467
+ * @param timeout Timeout in milliseconds (default 5000ms).
123468
+ * @throws If the alias is already taken.
123469
+ */
123401
123470
  DataBase.prototype.ensureAliasAvailable = function (alias_1) {
123402
123471
  return db_awaiter(this, arguments, void 0, function (alias, timeout) {
123403
123472
  var available;
@@ -123415,6 +123484,13 @@ var DataBase = /** @class */ (function () {
123415
123484
  });
123416
123485
  });
123417
123486
  };
123487
+ /**
123488
+ * Checks if a given alias/username is available on GunDB.
123489
+ * @param alias Username to check for availability.
123490
+ * @param timeout Timeout in ms (default: 5000).
123491
+ * @returns Promise resolving to `true` if available; otherwise `false`.
123492
+ * @throws If alias is invalid or on I/O error.
123493
+ */
123418
123494
  DataBase.prototype.isAliasAvailable = function (alias_1) {
123419
123495
  return db_awaiter(this, arguments, void 0, function (alias, timeout) {
123420
123496
  var normalizedAlias;
@@ -123444,9 +123520,39 @@ var DataBase = /** @class */ (function () {
123444
123520
  });
123445
123521
  });
123446
123522
  };
123523
+ /**
123524
+ * Checks if a given alias/username is taken on GunDB.
123525
+ * @param alias Username to check for availability.
123526
+ * @returns Promise resolving to `true` if taken; otherwise `false`.
123527
+ * @throws If alias is invalid or on I/O error.
123528
+ */
123529
+ DataBase.prototype.isAliasTaken = function (alias) {
123530
+ return db_awaiter(this, void 0, void 0, function () {
123531
+ var _this = this;
123532
+ return db_generator(this, function (_a) {
123533
+ return [2 /*return*/, new Promise(function (resolve, reject) {
123534
+ _this.gun.get("~@".concat(alias)).once(function (user) {
123535
+ if (user) {
123536
+ resolve(false);
123537
+ }
123538
+ else {
123539
+ resolve(true);
123540
+ }
123541
+ });
123542
+ })];
123543
+ });
123544
+ });
123545
+ };
123546
+ /**
123547
+ * Register a new alias (username) โ†’ public key mapping on GunDB.
123548
+ * @param alias The username/alias to register.
123549
+ * @param userPub The user's public key.
123550
+ * @param timeout Timeout in ms (default 5000).
123551
+ * @throws If alias/userPub is invalid or the alias cannot be registered.
123552
+ */
123447
123553
  DataBase.prototype.registerAlias = function (alias_1, userPub_1) {
123448
123554
  return db_awaiter(this, arguments, void 0, function (alias, userPub, timeout) {
123449
- var normalizedAlias, available;
123555
+ var normalizedAlias, available, taken;
123450
123556
  var _this = this;
123451
123557
  if (timeout === void 0) { timeout = 5000; }
123452
123558
  return db_generator(this, function (_a) {
@@ -123465,6 +123571,12 @@ var DataBase = /** @class */ (function () {
123465
123571
  })];
123466
123572
  case 1:
123467
123573
  available = _a.sent();
123574
+ return [4 /*yield*/, this.isAliasTaken(normalizedAlias)];
123575
+ case 2:
123576
+ taken = _a.sent();
123577
+ if (taken) {
123578
+ throw new Error("Alias \"".concat(normalizedAlias, "\" is already taken"));
123579
+ }
123468
123580
  if (!available) {
123469
123581
  throw new Error("Alias \"".concat(normalizedAlias, "\" is no longer available for registration"));
123470
123582
  }
@@ -123491,13 +123603,17 @@ var DataBase = /** @class */ (function () {
123491
123603
  console.error("[DB] Failed to register alias:", error);
123492
123604
  throw error;
123493
123605
  })];
123494
- case 2:
123606
+ case 3:
123495
123607
  _a.sent();
123496
123608
  return [2 /*return*/];
123497
123609
  }
123498
123610
  });
123499
123611
  });
123500
123612
  };
123613
+ /**
123614
+ * Reset gun.user() authentication state and clear cached user.
123615
+ * @internal
123616
+ */
123501
123617
  DataBase.prototype.resetAuthState = function () {
123502
123618
  try {
123503
123619
  var user = this.gun.user();
@@ -123522,6 +123638,13 @@ var DataBase = /** @class */ (function () {
123522
123638
  // Ignore
123523
123639
  }
123524
123640
  };
123641
+ /**
123642
+ * Assemble a standard AuthResult object after a successful login.
123643
+ * @param username Resulting username.
123644
+ * @param userPub Public key (pub) for logged-in user.
123645
+ * @returns AuthResult.
123646
+ * @internal
123647
+ */
123525
123648
  DataBase.prototype.buildLoginResult = function (username, userPub) {
123526
123649
  var _a, _b;
123527
123650
  var seaPair = (_b = (_a = this.gun.user()) === null || _a === void 0 ? void 0 : _a._) === null || _b === void 0 ? void 0 : _b.sea;
@@ -123539,6 +123662,10 @@ var DataBase = /** @class */ (function () {
123539
123662
  : undefined,
123540
123663
  };
123541
123664
  };
123665
+ /**
123666
+ * Save credentials for the current session to sessionStorage, if available.
123667
+ * @param userInfo The credentials and user identity to store.
123668
+ */
123542
123669
  DataBase.prototype.saveCredentials = function (userInfo) {
123543
123670
  try {
123544
123671
  if (typeof sessionStorage !== "undefined") {
@@ -123556,6 +123683,13 @@ var DataBase = /** @class */ (function () {
123556
123683
  console.error("[DB] Error saving credentials:", error);
123557
123684
  }
123558
123685
  };
123686
+ /**
123687
+ * Register and authenticate a new user account.
123688
+ * @param username The username to create/account for.
123689
+ * @param password The user's password.
123690
+ * @param pair Optional cryptographic pair (for `auth` instead of password).
123691
+ * @returns SignUpResult Promise.
123692
+ */
123559
123693
  DataBase.prototype.signUp = function (username, password, pair) {
123560
123694
  return db_awaiter(this, void 0, void 0, function () {
123561
123695
  var validation, normalizedUsername, user, loginResult, e_1, aliasError_1, result;
@@ -123727,6 +123861,13 @@ var DataBase = /** @class */ (function () {
123727
123861
  });
123728
123862
  });
123729
123863
  };
123864
+ /**
123865
+ * Sign in (authenticate) as an existing user by username/password or SEA pair.
123866
+ * @param username Username to log in as.
123867
+ * @param password User's password (or "" if using pair).
123868
+ * @param pair Optional cryptographic SEA pair.
123869
+ * @returns AuthResult Promise.
123870
+ */
123730
123871
  DataBase.prototype.login = function (username, password, pair) {
123731
123872
  return db_awaiter(this, void 0, void 0, function () {
123732
123873
  var normalizedUsername, user;
@@ -123800,6 +123941,10 @@ var DataBase = /** @class */ (function () {
123800
123941
  });
123801
123942
  });
123802
123943
  };
123944
+ /**
123945
+ * Returns the currently authenticated user's public key and Gun user instance, if logged in.
123946
+ * @returns Object containing `pub` (public key) and optionally `user`, or `null`.
123947
+ */
123803
123948
  DataBase.prototype.getCurrentUser = function () {
123804
123949
  try {
123805
123950
  var user = this.gun.user();
@@ -123816,8 +123961,8 @@ var DataBase = /** @class */ (function () {
123816
123961
  }
123817
123962
  };
123818
123963
  /**
123819
- * Get current user's public key
123820
- * @returns {string | null} User's public key or null if not logged in
123964
+ * Get current user's public key.
123965
+ * @returns User's public key or null if not logged in.
123821
123966
  */
123822
123967
  DataBase.prototype.getUserPub = function () {
123823
123968
  var _a;
@@ -123830,11 +123975,11 @@ var DataBase = /** @class */ (function () {
123830
123975
  }
123831
123976
  };
123832
123977
  /**
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
123978
+ * Authenticate using a SEA pair directly (no password required).
123979
+ * @param username The user's username for identification (not cryptographically enforced).
123980
+ * @param pair GunDB SEA pair for authentication.
123981
+ * @returns Promise with authentication result.
123982
+ * @description Authenticates user using a GunDB pair directly without password.
123838
123983
  */
123839
123984
  DataBase.prototype.loginWithPair = function (username, pair) {
123840
123985
  return db_awaiter(this, void 0, void 0, function () {
@@ -123885,6 +124030,12 @@ var DataBase = /** @class */ (function () {
123885
124030
  });
123886
124031
  });
123887
124032
  };
124033
+ /**
124034
+ * Legacy API: Sign in using a username and SEA pair (password parameter is unused).
124035
+ * @param username Username to sign in as.
124036
+ * @param pair SEA key pair.
124037
+ * @returns AuthResult Promise.
124038
+ */
123888
124039
  DataBase.prototype.loginWithPairLegacy = function (username, pair) {
123889
124040
  return db_awaiter(this, void 0, void 0, function () {
123890
124041
  return db_generator(this, function (_a) {
@@ -123892,9 +124043,17 @@ var DataBase = /** @class */ (function () {
123892
124043
  });
123893
124044
  });
123894
124045
  };
124046
+ /**
124047
+ * Returns the bound RxJS GunDB helper (reactive streams).
124048
+ * @returns RxJS instance.
124049
+ */
123895
124050
  DataBase.prototype.rx = function () {
123896
124051
  return this._rxjs;
123897
124052
  };
124053
+ /**
124054
+ * Tears down the DataBase instance and performs cleanup of all resources/listeners.
124055
+ * No further actions should be performed on this instance after destruction.
124056
+ */
123898
124057
  DataBase.prototype.destroy = function () {
123899
124058
  if (this._isDestroyed)
123900
124059
  return;
@@ -123914,21 +124073,45 @@ var DataBase = /** @class */ (function () {
123914
124073
  this._rxjs = undefined;
123915
124074
  console.log("[DB] DataBase instance destroyed");
123916
124075
  };
124076
+ /**
124077
+ * Aggressively clean up authentication state and session. Typically used for error recovery.
124078
+ */
123917
124079
  DataBase.prototype.aggressiveAuthCleanup = function () {
123918
124080
  console.log("๐Ÿงน Performing aggressive auth cleanup...");
123919
124081
  this.resetAuthState();
123920
124082
  this.logout();
123921
124083
  console.log("โœ“ Aggressive auth cleanup completed");
123922
124084
  };
124085
+ /**
124086
+ * Register an event handler.
124087
+ * @param event Event name.
124088
+ * @param listener Listener function.
124089
+ */
123923
124090
  DataBase.prototype.on = function (event, listener) {
123924
124091
  this.eventEmitter.on(event, listener);
123925
124092
  };
124093
+ /**
124094
+ * Remove an event handler.
124095
+ * @param event Event name.
124096
+ * @param listener Listener function.
124097
+ */
123926
124098
  DataBase.prototype.off = function (event, listener) {
123927
124099
  this.eventEmitter.off(event, listener);
123928
124100
  };
124101
+ /**
124102
+ * Register an event handler for a single event occurrence.
124103
+ * @param event Event name.
124104
+ * @param listener Listener function.
124105
+ */
123929
124106
  DataBase.prototype.once = function (event, listener) {
123930
124107
  this.eventEmitter.once(event, listener);
123931
124108
  };
124109
+ /**
124110
+ * Emit a custom event.
124111
+ * @param event Event name.
124112
+ * @param data Optional associated data.
124113
+ * @returns `true` if listeners were notified; otherwise `false`.
124114
+ */
123932
124115
  DataBase.prototype.emit = function (event, data) {
123933
124116
  return this.eventEmitter.emit(event, data);
123934
124117
  };