shogun-core 5.2.0 → 5.2.2
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 +145 -1143
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js +1220 -0
- package/dist/browser/defaultVendors-node_modules_hpke_chacha20poly1305_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js +844 -0
- package/dist/browser/defaultVendors-node_modules_hpke_hybridkem-x-wing_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js +2335 -0
- package/dist/browser/defaultVendors-node_modules_mlkem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js +999 -0
- package/dist/browser/defaultVendors-node_modules_noble_ciphers_chacha_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js +1651 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_curve_js-node_modules_noble_curves_esm_-1ce4ed.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js +825 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_abstract_edwards_js-node_modules_noble_curves_es-a82056.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js +508 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed25519_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js +747 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_ed448_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js +1608 -0
- package/dist/browser/defaultVendors-node_modules_noble_curves_esm_nist_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js +2117 -0
- package/dist/browser/defaultVendors-node_modules_noble_post-quantum_ml-dsa_js.shogun-core.js.map +1 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js +86 -0
- package/dist/browser/defaultVendors-node_modules_openpgp_dist_openpgp_min_mjs.shogun-core.js.map +1 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js +539 -0
- package/dist/browser/node_modules_hpke_ml-kem_esm_mod_js.shogun-core.js.map +1 -0
- package/dist/browser/shogun-core.js +160386 -0
- package/dist/browser/shogun-core.js.map +1 -0
- package/dist/config/simplified-config.js +236 -0
- package/dist/core.js +329 -0
- package/dist/crypto/asymmetric.js +99 -0
- package/dist/crypto/double-ratchet.js +370 -0
- package/dist/crypto/file-encryption.js +213 -0
- package/dist/crypto/hashing.js +87 -0
- package/dist/crypto/index.js +34 -0
- package/dist/crypto/mls-codec.js +202 -0
- package/dist/crypto/mls.js +550 -0
- package/dist/crypto/pgp.js +390 -0
- package/dist/crypto/random-generation.js +341 -0
- package/dist/crypto/sframe.js +350 -0
- package/dist/crypto/signal-protocol.js +376 -0
- package/dist/crypto/symmetric.js +91 -0
- package/dist/crypto/types.js +2 -0
- package/dist/crypto/utils.js +140 -0
- package/dist/examples/auth-test.js +253 -0
- package/dist/examples/crypto-identity-example.js +151 -0
- package/dist/examples/crypto-working-test.js +83 -0
- package/dist/examples/double-ratchet-test.js +155 -0
- package/dist/examples/mls-advanced-example.js +294 -0
- package/dist/examples/mls-sframe-test.js +304 -0
- package/dist/examples/pgp-example.js +200 -0
- package/dist/examples/quick-auth-test.js +61 -0
- package/dist/examples/random-generation-test.js +151 -0
- package/dist/examples/signal-protocol-test.js +38 -0
- package/dist/examples/simple-api-test.js +114 -0
- package/dist/examples/simple-crypto-identity-example.js +84 -0
- package/dist/examples/timeout-test.js +227 -0
- package/dist/examples/zkproof-credentials-example.js +212 -0
- package/dist/examples/zkproof-example.js +201 -0
- package/dist/gundb/api.js +435 -0
- package/dist/gundb/crypto.js +283 -0
- package/dist/gundb/db.js +1946 -0
- package/dist/gundb/derive.js +232 -0
- package/dist/gundb/errors.js +76 -0
- package/dist/gundb/index.js +22 -0
- package/dist/gundb/rxjs.js +447 -0
- package/dist/gundb/types.js +5 -0
- package/dist/index.js +58 -0
- package/dist/interfaces/common.js +2 -0
- package/dist/interfaces/events.js +40 -0
- package/dist/interfaces/plugin.js +2 -0
- package/dist/interfaces/shogun.js +37 -0
- package/dist/managers/AuthManager.js +226 -0
- package/dist/managers/CoreInitializer.js +228 -0
- package/dist/managers/CryptoIdentityManager.js +366 -0
- package/dist/managers/EventManager.js +70 -0
- package/dist/managers/PluginManager.js +299 -0
- package/dist/plugins/base.js +50 -0
- package/dist/plugins/index.js +32 -0
- package/dist/plugins/nostr/index.js +20 -0
- package/dist/plugins/nostr/nostrConnector.js +419 -0
- package/dist/plugins/nostr/nostrConnectorPlugin.js +453 -0
- package/dist/plugins/nostr/nostrSigner.js +319 -0
- package/dist/plugins/nostr/types.js +2 -0
- package/dist/plugins/smartwallet/index.js +18 -0
- package/dist/plugins/smartwallet/smartWalletPlugin.js +511 -0
- package/dist/plugins/smartwallet/types.js +2 -0
- package/dist/plugins/web3/index.js +20 -0
- package/dist/plugins/web3/types.js +2 -0
- package/dist/plugins/web3/web3Connector.js +533 -0
- package/dist/plugins/web3/web3ConnectorPlugin.js +455 -0
- package/dist/plugins/web3/web3Signer.js +314 -0
- package/dist/plugins/webauthn/index.js +19 -0
- package/dist/plugins/webauthn/types.js +14 -0
- package/dist/plugins/webauthn/webauthn.js +496 -0
- package/dist/plugins/webauthn/webauthnPlugin.js +489 -0
- package/dist/plugins/webauthn/webauthnSigner.js +310 -0
- package/dist/plugins/zkproof/index.js +53 -0
- package/dist/plugins/zkproof/types.js +2 -0
- package/dist/plugins/zkproof/zkCredentials.js +213 -0
- package/dist/plugins/zkproof/zkProofConnector.js +198 -0
- package/dist/plugins/zkproof/zkProofPlugin.js +272 -0
- package/dist/storage/storage.js +145 -0
- package/dist/types/config/simplified-config.d.ts +114 -0
- package/dist/types/core.d.ts +305 -0
- package/dist/types/crypto/asymmetric.d.ts +6 -0
- package/dist/types/crypto/double-ratchet.d.ts +22 -0
- package/dist/types/crypto/file-encryption.d.ts +19 -0
- package/dist/types/crypto/hashing.d.ts +9 -0
- package/dist/types/crypto/index.d.ts +13 -0
- package/dist/types/crypto/mls-codec.d.ts +39 -0
- package/dist/types/crypto/mls.d.ts +130 -0
- package/dist/types/crypto/pgp.d.ts +95 -0
- package/dist/types/crypto/random-generation.d.ts +35 -0
- package/dist/types/crypto/sframe.d.ts +102 -0
- package/dist/types/crypto/signal-protocol.d.ts +26 -0
- package/dist/types/crypto/symmetric.d.ts +9 -0
- package/dist/types/crypto/types.d.ts +144 -0
- package/dist/types/crypto/utils.d.ts +22 -0
- package/dist/types/examples/auth-test.d.ts +8 -0
- package/dist/types/examples/crypto-identity-example.d.ts +5 -0
- package/dist/types/examples/crypto-working-test.d.ts +1 -0
- package/dist/types/examples/double-ratchet-test.d.ts +1 -0
- package/dist/types/examples/mls-advanced-example.d.ts +53 -0
- package/dist/types/examples/mls-sframe-test.d.ts +1 -0
- package/dist/types/examples/pgp-example.d.ts +75 -0
- package/dist/types/examples/quick-auth-test.d.ts +8 -0
- package/dist/types/examples/random-generation-test.d.ts +1 -0
- package/dist/types/examples/signal-protocol-test.d.ts +1 -0
- package/dist/types/examples/simple-api-test.d.ts +10 -0
- package/dist/types/examples/simple-crypto-identity-example.d.ts +6 -0
- package/dist/types/examples/timeout-test.d.ts +8 -0
- package/dist/types/examples/zkproof-credentials-example.d.ts +12 -0
- package/dist/types/examples/zkproof-example.d.ts +11 -0
- package/dist/types/gundb/api.d.ts +185 -0
- package/dist/types/gundb/crypto.d.ts +95 -0
- package/dist/types/gundb/db.d.ts +397 -0
- package/dist/types/gundb/derive.d.ts +21 -0
- package/dist/types/gundb/errors.d.ts +42 -0
- package/dist/types/gundb/index.d.ts +3 -0
- package/dist/types/gundb/rxjs.d.ts +110 -0
- package/dist/types/gundb/types.d.ts +255 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/interfaces/common.d.ts +85 -0
- package/dist/types/interfaces/events.d.ts +131 -0
- package/dist/types/interfaces/plugin.d.ts +162 -0
- package/dist/types/interfaces/shogun.d.ts +208 -0
- package/dist/types/managers/AuthManager.d.ts +72 -0
- package/dist/types/managers/CoreInitializer.d.ts +40 -0
- package/dist/types/managers/CryptoIdentityManager.d.ts +102 -0
- package/dist/types/managers/EventManager.d.ts +49 -0
- package/dist/types/managers/PluginManager.d.ts +145 -0
- package/dist/types/plugins/base.d.ts +35 -0
- package/dist/types/plugins/index.d.ts +18 -0
- package/dist/types/plugins/nostr/index.d.ts +4 -0
- package/dist/types/plugins/nostr/nostrConnector.d.ts +119 -0
- package/dist/types/plugins/nostr/nostrConnectorPlugin.d.ts +163 -0
- package/dist/types/plugins/nostr/nostrSigner.d.ts +105 -0
- package/dist/types/plugins/nostr/types.d.ts +122 -0
- package/dist/types/plugins/smartwallet/index.d.ts +2 -0
- package/dist/types/plugins/smartwallet/smartWalletPlugin.d.ts +67 -0
- package/dist/types/plugins/smartwallet/types.d.ts +80 -0
- package/dist/types/plugins/web3/index.d.ts +4 -0
- package/dist/types/plugins/web3/types.d.ts +107 -0
- package/dist/types/plugins/web3/web3Connector.d.ts +129 -0
- package/dist/types/plugins/web3/web3ConnectorPlugin.d.ts +160 -0
- package/dist/types/plugins/web3/web3Signer.d.ts +114 -0
- package/dist/types/plugins/webauthn/index.d.ts +3 -0
- package/dist/types/plugins/webauthn/types.d.ts +183 -0
- package/dist/types/plugins/webauthn/webauthn.d.ts +129 -0
- package/dist/types/plugins/webauthn/webauthnPlugin.d.ts +179 -0
- package/dist/types/plugins/webauthn/webauthnSigner.d.ts +91 -0
- package/dist/types/plugins/zkproof/index.d.ts +48 -0
- package/dist/types/plugins/zkproof/types.d.ts +123 -0
- package/dist/types/plugins/zkproof/zkCredentials.d.ts +112 -0
- package/dist/types/plugins/zkproof/zkProofConnector.d.ts +46 -0
- package/dist/types/plugins/zkproof/zkProofPlugin.d.ts +76 -0
- package/dist/types/storage/storage.d.ts +51 -0
- package/dist/types/utils/errorHandler.d.ts +119 -0
- package/dist/types/utils/eventEmitter.d.ts +39 -0
- package/dist/types/utils/seedPhrase.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +27 -0
- package/dist/utils/errorHandler.js +246 -0
- package/dist/utils/eventEmitter.js +79 -0
- package/dist/utils/seedPhrase.js +97 -0
- package/dist/utils/validation.js +81 -0
- package/package.json +10 -57
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Simplified API layer focused on valuable helper methods.
|
|
4
|
+
* Provides quick-start initialization and high-level convenience methods.
|
|
5
|
+
*
|
|
6
|
+
* For basic operations (get, put, set, remove, auth), use DataBase directly.
|
|
7
|
+
* This class provides:
|
|
8
|
+
* - Quick initialization helpers (QuickStart, AutoQuickStart)
|
|
9
|
+
* - Array/Object conversion utilities for GunDB
|
|
10
|
+
* - High-level user data helpers (profile, settings, collections)
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.AutoQuickStart = exports.QuickStart = exports.SimpleGunAPI = void 0;
|
|
14
|
+
exports.createSimpleAPI = createSimpleAPI;
|
|
15
|
+
exports.quickStart = quickStart;
|
|
16
|
+
exports.autoQuickStart = autoQuickStart;
|
|
17
|
+
const db_1 = require("./db");
|
|
18
|
+
/**
|
|
19
|
+
* Simple API wrapper that provides high-level helper methods.
|
|
20
|
+
* For basic operations, use the DataBase instance directly via the `database` property.
|
|
21
|
+
*/
|
|
22
|
+
class SimpleGunAPI {
|
|
23
|
+
/**
|
|
24
|
+
* Create a new SimpleGunAPI instance.
|
|
25
|
+
* @param db The DataBase instance to use.
|
|
26
|
+
*/
|
|
27
|
+
constructor(db) {
|
|
28
|
+
this.db = db;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Get direct access to the DataBase instance for full control.
|
|
32
|
+
* Use this for basic operations like get, put, set, remove, login, etc.
|
|
33
|
+
*/
|
|
34
|
+
get database() {
|
|
35
|
+
return this.db;
|
|
36
|
+
}
|
|
37
|
+
// =========================
|
|
38
|
+
// Array utilities for GunDB
|
|
39
|
+
// =========================
|
|
40
|
+
/**
|
|
41
|
+
* Convert an array to an indexed object for GunDB storage.
|
|
42
|
+
* GunDB doesn't store arrays natively, so this converts them to objects indexed by ID.
|
|
43
|
+
* Example: [{id: '1', ...}, {id: '2', ...}] => { "1": {...}, "2": {...} }
|
|
44
|
+
* @param arr The array to convert (each item must have an 'id' property).
|
|
45
|
+
* @returns The indexed object suitable for GunDB storage.
|
|
46
|
+
*/
|
|
47
|
+
arrayToIndexedObject(arr) {
|
|
48
|
+
// Filter out null/undefined items and ensure they have valid id
|
|
49
|
+
const validItems = (arr || []).filter((item) => item &&
|
|
50
|
+
typeof item === "object" &&
|
|
51
|
+
item.id !== null &&
|
|
52
|
+
item.id !== undefined);
|
|
53
|
+
return validItems.reduce((acc, item) => {
|
|
54
|
+
return {
|
|
55
|
+
...acc,
|
|
56
|
+
[item.id]: item,
|
|
57
|
+
};
|
|
58
|
+
}, {});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Convert an indexed object back to an array.
|
|
62
|
+
* Reverses the arrayToIndexedObject conversion.
|
|
63
|
+
* Example: { "1": {...}, "2": {...} } => [{id: '1', ...}, {id: '2', ...}]
|
|
64
|
+
* @param indexedObj The indexed object to convert.
|
|
65
|
+
* @returns The array of items.
|
|
66
|
+
*/
|
|
67
|
+
indexedObjectToArray(indexedObj) {
|
|
68
|
+
if (!indexedObj || typeof indexedObj !== "object") {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
// Remove GunDB metadata and convert to array
|
|
72
|
+
const cleanObj = { ...indexedObj };
|
|
73
|
+
delete cleanObj._; // Remove GunDB metadata
|
|
74
|
+
// Filter out null/undefined values and ensure they are valid objects
|
|
75
|
+
return Object.values(cleanObj).filter((item) => item && typeof item === "object");
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @deprecated This method is unreliable with GunDB. Use direct GunDB operations instead.
|
|
79
|
+
* Store an array at a global path by converting it to an indexed object.
|
|
80
|
+
* @param path The global path to store the array at
|
|
81
|
+
* @param arr The array to store (each item must have an 'id' property)
|
|
82
|
+
* @returns True if successful, false otherwise
|
|
83
|
+
*/
|
|
84
|
+
async putArray(path, arr) {
|
|
85
|
+
console.warn("DEPRECATED: putArray() is unreliable with GunDB. Use direct GunDB operations instead.");
|
|
86
|
+
try {
|
|
87
|
+
const indexed = this.arrayToIndexedObject(arr);
|
|
88
|
+
await this.db.put(path, indexed);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.warn("Failed to put array:", error);
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @deprecated This method is unreliable with GunDB. Use direct GunDB operations instead.
|
|
98
|
+
* Retrieve an array from a global path by converting from indexed object.
|
|
99
|
+
* @param path The global path to retrieve the array from
|
|
100
|
+
* @returns The array of items, or empty array on error
|
|
101
|
+
*/
|
|
102
|
+
async getArray(path) {
|
|
103
|
+
console.warn("DEPRECATED: getArray() is unreliable with GunDB. Use direct GunDB operations instead.");
|
|
104
|
+
try {
|
|
105
|
+
const indexedObj = await this.db.getData(path);
|
|
106
|
+
return this.indexedObjectToArray(indexedObj);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
console.warn("Failed to get array:", error);
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// =========================
|
|
114
|
+
// High-level user data helpers
|
|
115
|
+
// =========================
|
|
116
|
+
/**
|
|
117
|
+
* Get all user data (returns user's entire data tree).
|
|
118
|
+
* Requires user to be logged in.
|
|
119
|
+
* @returns The complete user data tree, or null if not logged in or on error.
|
|
120
|
+
*/
|
|
121
|
+
async getAllUserData() {
|
|
122
|
+
try {
|
|
123
|
+
if (!this.db.isLoggedIn()) {
|
|
124
|
+
console.warn("User not logged in");
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const user = this.db.getUser();
|
|
128
|
+
const pubkey = user.is?.pub;
|
|
129
|
+
const node = this.db.get(`${pubkey}`);
|
|
130
|
+
if (node) {
|
|
131
|
+
const userData = await new Promise((resolve, reject) => {
|
|
132
|
+
node.once((data) => {
|
|
133
|
+
resolve(data);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
return userData;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.warn("Failed to get all user data:", error);
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Update user profile with common fields.
|
|
147
|
+
* Provides a standardized location for user profile data.
|
|
148
|
+
* @param profileData Profile data to save (name, email, bio, avatar, etc.)
|
|
149
|
+
* @returns True if successful, false otherwise.
|
|
150
|
+
*/
|
|
151
|
+
async updateProfile(profileData) {
|
|
152
|
+
try {
|
|
153
|
+
if (!this.db.isLoggedIn()) {
|
|
154
|
+
console.warn("User not logged in");
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const user = this.db.getUser();
|
|
158
|
+
await user.get("profile").put(profileData).then();
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
console.warn("Failed to update profile:", error);
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get user profile data.
|
|
168
|
+
* @returns The user profile data, or null if not found or not logged in.
|
|
169
|
+
*/
|
|
170
|
+
async getProfile() {
|
|
171
|
+
try {
|
|
172
|
+
if (!this.db.isLoggedIn()) {
|
|
173
|
+
console.warn("User not logged in");
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
const user = this.db.getUser();
|
|
177
|
+
const profileData = await user.get("profile").once().then();
|
|
178
|
+
return profileData;
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
console.warn("Failed to get profile:", error);
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Save user settings.
|
|
187
|
+
* Provides a standardized location for application settings.
|
|
188
|
+
* @param settings Settings object to save.
|
|
189
|
+
* @returns True if successful, false otherwise.
|
|
190
|
+
*/
|
|
191
|
+
async saveSettings(settings) {
|
|
192
|
+
try {
|
|
193
|
+
if (!this.db.isLoggedIn()) {
|
|
194
|
+
console.warn("User not logged in");
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
const user = this.db.getUser();
|
|
198
|
+
await user.get("settings").put(settings).then();
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
console.warn("Failed to save settings:", error);
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get user settings.
|
|
208
|
+
* @returns The user settings, or null if not found or not logged in.
|
|
209
|
+
*/
|
|
210
|
+
async getSettings() {
|
|
211
|
+
try {
|
|
212
|
+
if (!this.db.isLoggedIn()) {
|
|
213
|
+
console.warn("User not logged in");
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
const user = this.db.getUser();
|
|
217
|
+
const settingsData = await user.get("settings").once().then();
|
|
218
|
+
return settingsData;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
console.warn("Failed to get settings:", error);
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Save user preferences.
|
|
227
|
+
* Provides a standardized location for user preferences (distinct from settings).
|
|
228
|
+
* @param preferences Preferences object to save.
|
|
229
|
+
* @returns True if successful, false otherwise.
|
|
230
|
+
*/
|
|
231
|
+
async savePreferences(preferences) {
|
|
232
|
+
try {
|
|
233
|
+
if (!this.db.isLoggedIn()) {
|
|
234
|
+
console.warn("User not logged in");
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
const user = this.db.getUser();
|
|
238
|
+
await user.get("preferences").put(preferences).then();
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
catch (error) {
|
|
242
|
+
console.warn("Failed to save preferences:", error);
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Get user preferences.
|
|
248
|
+
* @returns The user preferences, or null if not found or not logged in.
|
|
249
|
+
*/
|
|
250
|
+
async getPreferences() {
|
|
251
|
+
try {
|
|
252
|
+
if (!this.db.isLoggedIn()) {
|
|
253
|
+
console.warn("User not logged in");
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
const user = this.db.getUser();
|
|
257
|
+
const preferencesData = await user.get("preferences").once().then();
|
|
258
|
+
return preferencesData;
|
|
259
|
+
}
|
|
260
|
+
catch (error) {
|
|
261
|
+
console.warn("Failed to get preferences:", error);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Create a user collection with initial items.
|
|
267
|
+
* Provides a standardized location for user collections.
|
|
268
|
+
* @param collectionName The name of the collection.
|
|
269
|
+
* @param items The initial items for the collection.
|
|
270
|
+
* @returns True if successful, false otherwise.
|
|
271
|
+
*/
|
|
272
|
+
async createCollection(collectionName, items) {
|
|
273
|
+
try {
|
|
274
|
+
if (!this.db.isLoggedIn()) {
|
|
275
|
+
console.warn("User not logged in");
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
const user = this.db.getUser();
|
|
279
|
+
await user.get(`collections/${collectionName}`).put(items).then();
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
console.warn(`Failed to create collection ${collectionName}:`, error);
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Add an item to a user collection.
|
|
289
|
+
* @param collectionName The name of the collection.
|
|
290
|
+
* @param itemId The ID of the item to add.
|
|
291
|
+
* @param item The item data.
|
|
292
|
+
* @returns True if successful, false otherwise.
|
|
293
|
+
*/
|
|
294
|
+
async addToCollection(collectionName, itemId, item) {
|
|
295
|
+
try {
|
|
296
|
+
if (!this.db.isLoggedIn()) {
|
|
297
|
+
console.warn("User not logged in");
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
const user = this.db.getUser();
|
|
301
|
+
await user
|
|
302
|
+
.get(`collections/${collectionName}/${itemId}`)
|
|
303
|
+
.put(item)
|
|
304
|
+
.then();
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
catch (error) {
|
|
308
|
+
console.warn(`Failed to add item to collection ${collectionName}:`, error);
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Get a user collection.
|
|
314
|
+
* @param collectionName The name of the collection.
|
|
315
|
+
* @returns The collection data, or null if not found or not logged in.
|
|
316
|
+
*/
|
|
317
|
+
async getCollection(collectionName) {
|
|
318
|
+
try {
|
|
319
|
+
if (!this.db.isLoggedIn()) {
|
|
320
|
+
console.warn("User not logged in");
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
const user = this.db.getUser();
|
|
324
|
+
const collectionData = await user
|
|
325
|
+
.get(`collections/${collectionName}`)
|
|
326
|
+
.once()
|
|
327
|
+
.then();
|
|
328
|
+
return collectionData;
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
console.warn(`Failed to get collection ${collectionName}:`, error);
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Remove an item from a user collection.
|
|
337
|
+
* @param collectionName The name of the collection.
|
|
338
|
+
* @param itemId The ID of the item to remove.
|
|
339
|
+
* @returns True if successful, false otherwise.
|
|
340
|
+
*/
|
|
341
|
+
async removeFromCollection(collectionName, itemId) {
|
|
342
|
+
try {
|
|
343
|
+
if (!this.db.isLoggedIn()) {
|
|
344
|
+
console.warn("User not logged in");
|
|
345
|
+
return false;
|
|
346
|
+
}
|
|
347
|
+
const user = this.db.getUser();
|
|
348
|
+
await user
|
|
349
|
+
.get(`collections/${collectionName}/${itemId}`)
|
|
350
|
+
.put(null)
|
|
351
|
+
.then();
|
|
352
|
+
return true;
|
|
353
|
+
}
|
|
354
|
+
catch (error) {
|
|
355
|
+
console.warn(`Failed to remove item from collection ${collectionName}:`, error);
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
exports.SimpleGunAPI = SimpleGunAPI;
|
|
361
|
+
/**
|
|
362
|
+
* Factory function to create a simple API instance
|
|
363
|
+
*/
|
|
364
|
+
function createSimpleAPI(db) {
|
|
365
|
+
return new SimpleGunAPI(db);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Quick start helper - creates a simple API with minimal configuration
|
|
369
|
+
*/
|
|
370
|
+
class QuickStart {
|
|
371
|
+
constructor(gunInstance, appScope = "shogun") {
|
|
372
|
+
this.db = new db_1.DataBase(gunInstance, appScope);
|
|
373
|
+
this.simpleAPI = new SimpleGunAPI(this.db);
|
|
374
|
+
}
|
|
375
|
+
// Initialize the database
|
|
376
|
+
async init() {
|
|
377
|
+
await this.db.initialize();
|
|
378
|
+
}
|
|
379
|
+
// Get the simple API
|
|
380
|
+
get api() {
|
|
381
|
+
return this.simpleAPI;
|
|
382
|
+
}
|
|
383
|
+
// Get the full database instance for advanced usage
|
|
384
|
+
get database() {
|
|
385
|
+
return this.db;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
exports.QuickStart = QuickStart;
|
|
389
|
+
/**
|
|
390
|
+
* Auto Quick Start helper - creates a simple API with automatic Gun instance creation
|
|
391
|
+
* No need to pass a Gun instance, it creates one automatically
|
|
392
|
+
*/
|
|
393
|
+
class AutoQuickStart {
|
|
394
|
+
constructor(config) {
|
|
395
|
+
const gunConfig = {
|
|
396
|
+
peers: config?.peers || [],
|
|
397
|
+
...config,
|
|
398
|
+
};
|
|
399
|
+
// Remove appScope from gunConfig as it's not a Gun configuration option
|
|
400
|
+
delete gunConfig.appScope;
|
|
401
|
+
this.gunInstance = (0, db_1.createGun)(gunConfig);
|
|
402
|
+
const appScope = config?.appScope || "shogun";
|
|
403
|
+
this.db = new db_1.DataBase(this.gunInstance, appScope);
|
|
404
|
+
this.simpleAPI = new SimpleGunAPI(this.db);
|
|
405
|
+
}
|
|
406
|
+
// Initialize the database
|
|
407
|
+
async init() {
|
|
408
|
+
await this.db.initialize();
|
|
409
|
+
}
|
|
410
|
+
// Get the simple API
|
|
411
|
+
get api() {
|
|
412
|
+
return this.simpleAPI;
|
|
413
|
+
}
|
|
414
|
+
// Get the full database instance for advanced usage
|
|
415
|
+
get database() {
|
|
416
|
+
return this.db;
|
|
417
|
+
}
|
|
418
|
+
// Get the Gun instance for advanced usage
|
|
419
|
+
get gun() {
|
|
420
|
+
return this.gunInstance;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
exports.AutoQuickStart = AutoQuickStart;
|
|
424
|
+
/**
|
|
425
|
+
* Global helper for quick setup
|
|
426
|
+
*/
|
|
427
|
+
function quickStart(gunInstance, appScope) {
|
|
428
|
+
return new QuickStart(gunInstance, appScope);
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Global helper for auto quick setup - creates Gun instance automatically
|
|
432
|
+
*/
|
|
433
|
+
function autoQuickStart(config) {
|
|
434
|
+
return new AutoQuickStart(config);
|
|
435
|
+
}
|