shogun-core 6.9.6 → 6.9.8
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/README.md
CHANGED
|
@@ -89,92 +89,93 @@ const shogun = new ShogunCore({
|
|
|
89
89
|
// All other APIs work the same way!
|
|
90
90
|
```
|
|
91
91
|
|
|
92
|
-
##
|
|
92
|
+
## Database API Reference
|
|
93
|
+
|
|
94
|
+
The `shogun.db` instance provides a high-level API for interacting with the decentralized database. Shogun Core automatically handles the differences between **Gun** and **Holster** backends.
|
|
95
|
+
|
|
96
|
+
### 1. Connection & Session Management
|
|
97
|
+
These methods work with both backends.
|
|
98
|
+
|
|
99
|
+
- `db.isLoggedIn(): boolean`: Returns `true` if a user is currently authenticated.
|
|
100
|
+
- `db.getCurrentUser(): { pub: string; user?: any } | null`: Returns the current user's public key and instance.
|
|
101
|
+
- `db.getUserPub(): string | null`: Returns the current user's public key.
|
|
102
|
+
- `db.onAuth(callback: (user: any) => void): () => void`: Listens for authentication changes. Returns an unsubscribe function.
|
|
103
|
+
- `db.restoreSession(): Promise<RestoreResult>`: Attempts to restore a previous session from `sessionStorage`.
|
|
104
|
+
|
|
105
|
+
### 2. Promise-based Advanced Utilities (Firegun API)
|
|
106
|
+
*Note: These methods currently provide full Promise support and auto-retries primarily when using the **Gun** backend.*
|
|
93
107
|
|
|
94
108
|
```typescript
|
|
95
109
|
const db = shogun.db;
|
|
96
110
|
|
|
97
|
-
//
|
|
98
|
-
await db.
|
|
99
|
-
name: 'Alice Smith',
|
|
100
|
-
email: 'alice@example.com',
|
|
101
|
-
});
|
|
111
|
+
// Fetch data with auto-retry and 5s timeout
|
|
112
|
+
const data = await db.Get('public/posts/123');
|
|
102
113
|
|
|
103
|
-
//
|
|
104
|
-
|
|
114
|
+
// Save data (supports deep object merging)
|
|
115
|
+
await db.Put('public/settings', { theme: 'dark' });
|
|
105
116
|
|
|
106
|
-
//
|
|
107
|
-
await db
|
|
108
|
-
.get('users')
|
|
109
|
-
.get('alice')
|
|
110
|
-
.get('profile')
|
|
111
|
-
.get('name')
|
|
112
|
-
.put('Alice Johnson');
|
|
117
|
+
// Insert into a collection with a random key
|
|
118
|
+
await db.Set('public/logs', { event: 'login', time: Date.now() });
|
|
113
119
|
|
|
114
|
-
//
|
|
115
|
-
db.
|
|
116
|
-
console.log(`User ${userId}:`, user);
|
|
117
|
-
});
|
|
118
|
-
```
|
|
120
|
+
// Recursive load of nested nodes
|
|
121
|
+
const fullData = await db.Load('public/complex-node');
|
|
119
122
|
|
|
120
|
-
|
|
123
|
+
// "Delete" a node (Tombstoning)
|
|
124
|
+
await db.Del('public/temp-data');
|
|
121
125
|
|
|
122
|
-
|
|
126
|
+
// Deeply nullify all keys in a node
|
|
127
|
+
await db.purge('public/old-config');
|
|
128
|
+
```
|
|
123
129
|
|
|
130
|
+
### 3. Real-time Subscriptions
|
|
124
131
|
```typescript
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// Promise-based Root GET with auto-retry
|
|
128
|
-
const data = await db.Get('public/posts/123');
|
|
132
|
+
// Listen to changes with an identifier for easy unsubscription
|
|
133
|
+
db.On('public/feed', (data) => console.log('Update:', data), 'myListener');
|
|
129
134
|
|
|
130
|
-
//
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// Listen to node changes with easy unsubscribe
|
|
134
|
-
await db.On(
|
|
135
|
-
'public/feed',
|
|
136
|
-
(data) => console.log('Feed updated:', data),
|
|
137
|
-
'myFeedListener',
|
|
138
|
-
);
|
|
139
|
-
await db.Off('myFeedListener'); // Cleanly remove the listener
|
|
140
|
-
|
|
141
|
-
// Content Addressed Storage (CAS)
|
|
142
|
-
// Hashes the data automatically using SHA-256 and saves it immutably
|
|
143
|
-
const ack = await db.addContentAdressing('#immutable-posts', {
|
|
144
|
-
text: 'Hello Web3!',
|
|
145
|
-
});
|
|
135
|
+
// Stop listening
|
|
136
|
+
db.Off('myListener');
|
|
146
137
|
|
|
147
|
-
//
|
|
148
|
-
|
|
138
|
+
// One-time fetch of initial state + future changes
|
|
139
|
+
db.Listen('public/status', (status) => console.log('Status:', status));
|
|
140
|
+
```
|
|
149
141
|
|
|
150
|
-
|
|
151
|
-
|
|
142
|
+
### 4. User-Space Operations (Gun Only)
|
|
143
|
+
These methods automatically prefix the path with `~pubkey/` of the logged-in user.
|
|
152
144
|
|
|
153
|
-
|
|
154
|
-
|
|
145
|
+
- `db.userGet(path: string)`: Read from current user's graph.
|
|
146
|
+
- `db.userPut(path: string, data: any)`: Write to current user's graph.
|
|
147
|
+
- `db.userDel(path: string)`: Delete node from user's graph.
|
|
148
|
+
- `db.userLoad(path: string)`: Recursively load user-space data.
|
|
155
149
|
|
|
156
|
-
|
|
157
|
-
|
|
150
|
+
### 5. Advanced Features
|
|
151
|
+
- **Reactive Streams**: `db.rx()` returns an RxJS helper for observable-based data handling.
|
|
152
|
+
- **Content Addressing**: `db.addContentAdressing('#key', data)` hashes data using SHA-256 for immutable storage.
|
|
153
|
+
- **Security**: `db.generatePublicCert()` creates a public certificate for P2P interactions.
|
|
154
|
+
- **Cleanup**: `db.aggressiveAuthCleanup()` forcefully clears all local auth state.
|
|
158
155
|
|
|
159
|
-
|
|
160
|
-
await db.purge('~pubkey/public/posts/123');
|
|
161
|
-
```
|
|
156
|
+
## Authentication API
|
|
162
157
|
|
|
163
|
-
|
|
158
|
+
Shogun Core provides a unified authentication interface. Plugins (Web3, WebAuthn, etc.) extend this system.
|
|
164
159
|
|
|
165
|
-
###
|
|
160
|
+
### Core Methods
|
|
166
161
|
|
|
167
162
|
```typescript
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
163
|
+
// 1. Traditional Signup/Login
|
|
164
|
+
await shogun.signUp('alice', 'Password123!');
|
|
165
|
+
await shogun.login('alice', 'Password123!');
|
|
166
|
+
|
|
167
|
+
// 2. Pair-based Authentication (Gun Only)
|
|
168
|
+
const pair = await shogun.db.crypto.createPair();
|
|
169
|
+
await shogun.loginWithPair('alice', pair);
|
|
170
|
+
|
|
171
|
+
// 3. Mnemonic Seed Authentication
|
|
172
|
+
const mnemonic = 'word1 word2 ...';
|
|
173
|
+
await shogun.loginWithSeed('alice', mnemonic);
|
|
174
|
+
|
|
175
|
+
// 4. Session Check & Logout
|
|
176
|
+
if (shogun.isLoggedIn()) {
|
|
177
|
+
console.log('User Pub:', shogun.db.getUserPub());
|
|
178
|
+
shogun.logout();
|
|
178
179
|
}
|
|
179
180
|
```
|
|
180
181
|
|
|
@@ -61676,6 +61676,8 @@ var DataBaseHolster = /** @class */ (function () {
|
|
|
61676
61676
|
var _a;
|
|
61677
61677
|
/** Cached user instance or `null` if not logged in */
|
|
61678
61678
|
this.user = null;
|
|
61679
|
+
this.prefix = '';
|
|
61680
|
+
this.ev = {};
|
|
61679
61681
|
/** Registered callbacks for auth state changes */
|
|
61680
61682
|
this.onAuthCallbacks = [];
|
|
61681
61683
|
/** Whether the database instance has been destroyed */
|
|
@@ -62724,6 +62726,85 @@ var DataBaseHolster = /** @class */ (function () {
|
|
|
62724
62726
|
DataBaseHolster.prototype.emit = function (event, data) {
|
|
62725
62727
|
return this.eventEmitter.emit(event, data);
|
|
62726
62728
|
};
|
|
62729
|
+
// --- Firegun Methods Stubs ---
|
|
62730
|
+
DataBaseHolster.prototype._timeout = function (ms) {
|
|
62731
|
+
return db_holster_awaiter(this, void 0, void 0, function () {
|
|
62732
|
+
return db_holster_generator(this, function (_a) {
|
|
62733
|
+
return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, ms); })];
|
|
62734
|
+
});
|
|
62735
|
+
});
|
|
62736
|
+
};
|
|
62737
|
+
DataBaseHolster.prototype.Off = function (ev) {
|
|
62738
|
+
if (ev === void 0) { ev = 'default'; }
|
|
62739
|
+
console.warn('Off not implemented in DataBaseHolster');
|
|
62740
|
+
};
|
|
62741
|
+
DataBaseHolster.prototype.Listen = function (path, callback, prefix) {
|
|
62742
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62743
|
+
console.warn('Listen not implemented in DataBaseHolster');
|
|
62744
|
+
};
|
|
62745
|
+
DataBaseHolster.prototype.On = function (path, callback, ev, different, prefix) {
|
|
62746
|
+
if (ev === void 0) { ev = 'default'; }
|
|
62747
|
+
if (different === void 0) { different = true; }
|
|
62748
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62749
|
+
console.warn('On not implemented in DataBaseHolster');
|
|
62750
|
+
};
|
|
62751
|
+
DataBaseHolster.prototype.addContentAdressing = function (key, data) {
|
|
62752
|
+
return Promise.reject(new Error('addContentAdressing not implemented in DataBaseHolster'));
|
|
62753
|
+
};
|
|
62754
|
+
DataBaseHolster.prototype.userGet = function (path, repeat, prefix) {
|
|
62755
|
+
if (repeat === void 0) { repeat = 1; }
|
|
62756
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62757
|
+
return Promise.reject(new Error('userGet not implemented in DataBaseHolster'));
|
|
62758
|
+
};
|
|
62759
|
+
DataBaseHolster.prototype.userLoad = function (path, async, repeat, prefix) {
|
|
62760
|
+
if (async === void 0) { async = false; }
|
|
62761
|
+
if (repeat === void 0) { repeat = 1; }
|
|
62762
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62763
|
+
return Promise.reject(new Error('userLoad not implemented in DataBaseHolster'));
|
|
62764
|
+
};
|
|
62765
|
+
DataBaseHolster.prototype.Get = function (path, repeat, prefix) {
|
|
62766
|
+
if (repeat === void 0) { repeat = 1; }
|
|
62767
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62768
|
+
return Promise.reject(new Error('Get not implemented in DataBaseHolster'));
|
|
62769
|
+
};
|
|
62770
|
+
DataBaseHolster.prototype.userPut = function (path, data, async, prefix) {
|
|
62771
|
+
if (async === void 0) { async = false; }
|
|
62772
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62773
|
+
return Promise.reject(new Error('userPut not implemented in DataBaseHolster'));
|
|
62774
|
+
};
|
|
62775
|
+
DataBaseHolster.prototype.Set = function (path, data, async, prefix, opt) {
|
|
62776
|
+
if (async === void 0) { async = false; }
|
|
62777
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62778
|
+
if (opt === void 0) { opt = undefined; }
|
|
62779
|
+
return Promise.reject(new Error('Set not implemented in DataBaseHolster'));
|
|
62780
|
+
};
|
|
62781
|
+
DataBaseHolster.prototype.Put = function (path, data, async, prefix, opt) {
|
|
62782
|
+
if (async === void 0) { async = false; }
|
|
62783
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62784
|
+
if (opt === void 0) { opt = undefined; }
|
|
62785
|
+
return Promise.reject(new Error('Put not implemented in DataBaseHolster'));
|
|
62786
|
+
};
|
|
62787
|
+
DataBaseHolster.prototype.purge = function (path) {
|
|
62788
|
+
return Promise.reject(new Error('purge not implemented in DataBaseHolster'));
|
|
62789
|
+
};
|
|
62790
|
+
DataBaseHolster.prototype.userDel = function (path, putNull) {
|
|
62791
|
+
if (putNull === void 0) { putNull = true; }
|
|
62792
|
+
return Promise.reject(new Error('userDel not implemented in DataBaseHolster'));
|
|
62793
|
+
};
|
|
62794
|
+
DataBaseHolster.prototype.Del = function (path, putNull, cert) {
|
|
62795
|
+
if (putNull === void 0) { putNull = true; }
|
|
62796
|
+
if (cert === void 0) { cert = ''; }
|
|
62797
|
+
return Promise.reject(new Error('Del not implemented in DataBaseHolster'));
|
|
62798
|
+
};
|
|
62799
|
+
DataBaseHolster.prototype.Load = function (path, async, repeat, prefix) {
|
|
62800
|
+
if (async === void 0) { async = false; }
|
|
62801
|
+
if (repeat === void 0) { repeat = 1; }
|
|
62802
|
+
if (prefix === void 0) { prefix = this.prefix; }
|
|
62803
|
+
return Promise.reject(new Error('Load not implemented in DataBaseHolster'));
|
|
62804
|
+
};
|
|
62805
|
+
DataBaseHolster.prototype.generatePublicCert = function () {
|
|
62806
|
+
return Promise.reject(new Error('generatePublicCert not implemented in DataBaseHolster'));
|
|
62807
|
+
};
|
|
62727
62808
|
Object.defineProperty(DataBaseHolster.prototype, "gun", {
|
|
62728
62809
|
/**
|
|
62729
62810
|
* Get the Holster instance (for backward compatibility with gun property).
|