Attempted fixes

This commit is contained in:
Raven Scott
2026-05-28 18:10:40 -04:00
parent 343102d4b5
commit d046f6a8e2
2 changed files with 56 additions and 2 deletions
+44 -2
View File
@@ -54,6 +54,22 @@ const sodium = require('sodium-native');
*/
const pluginRegistry = new Map();
// Serialize HyperDB insert/delete/flush per plugin (HyperDB refuses flush while mutating)
const dbWriteQueues = new Map();
/**
* Run a write operation after prior writes for the same plugin complete.
* @param {string} pluginDomain
* @param {Function} fn
* @returns {Promise<*>}
*/
function enqueueDbWrite(pluginDomain, fn) {
const prev = dbWriteQueues.get(pluginDomain) || Promise.resolve();
const next = prev.then(() => fn(), () => fn());
dbWriteQueues.set(pluginDomain, next.catch(() => {}));
return next;
}
// Current active plugin domain (for request handling)
let activePluginDomain = null;
@@ -3940,9 +3956,18 @@ const sdk = {
* @returns {Promise<void>}
*/
async insert(collection, doc) {
const pluginDomain = _getPluginDomain();
if (!pluginDomain) {
logError('PluginSDK', `[db.insert] Plugin domain not available`);
throw new Error('Plugin domain not available');
}
return enqueueDbWrite(pluginDomain, () => this._insert(collection, doc));
},
async _insert(collection, doc) {
const startTime = Date.now();
logDebug('PluginSDK', `[db.insert] Starting insert to ${collection}`);
const pluginDomain = _getPluginDomain();
if (!pluginDomain) {
logError('PluginSDK', `[db.insert] Plugin domain not available`);
@@ -4103,6 +4128,14 @@ const sdk = {
* @returns {Promise<void>}
*/
async delete(collection, query) {
const pluginDomain = _getPluginDomain();
if (!pluginDomain) {
throw new Error('Plugin domain not available');
}
return enqueueDbWrite(pluginDomain, () => this._delete(collection, query));
},
async _delete(collection, query) {
const db = this._getDatabase();
if (!db) {
throw new Error('Database not initialized for this plugin');
@@ -4115,9 +4148,18 @@ const sdk = {
* @returns {Promise<void>}
*/
async flush() {
const pluginDomain = _getPluginDomain();
if (!pluginDomain) {
logError('PluginSDK', `[db.flush] Plugin domain not available`);
throw new Error('Plugin domain not available');
}
return enqueueDbWrite(pluginDomain, () => this._flush());
},
async _flush() {
const startTime = Date.now();
logDebug('PluginSDK', `[db.flush] Starting flush`);
const pluginDomain = _getPluginDomain();
if (!pluginDomain) {
logError('PluginSDK', `[db.flush] Plugin domain not available`);