From 45c875fdd31ce385b9819724356b7fd540f354b1 Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Tue, 21 Nov 2023 14:20:31 +0100 Subject: [PATCH 1/2] Add hooks to compat functions Made by @cooldracula --- compat/db.js | 16 +++++++++------- compat/log-stream.js | 5 +++-- compat/publish.js | 6 ++++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/compat/db.js b/compat/db.js index 78b3e9ac..64d07c34 100644 --- a/compat/db.js +++ b/compat/db.js @@ -3,6 +3,7 @@ // SPDX-License-Identifier: LGPL-3.0-only const pull = require('pull-stream') +const Hookable = require('hoox') // exports.name is blank to merge into global namespace @@ -13,8 +14,8 @@ exports.manifest = { } exports.init = function (sbot, config) { - sbot.add = sbot.db.add - sbot.get = function get(idOrObject, cb) { + sbot.add = Hookable(sbot.db.add) + sbot.get = Hookable(function get(idOrObject, cb) { if (typeof idOrObject === 'object' && idOrObject.meta) { sbot.db.getMsg(idOrObject.id, cb) } else if (typeof idOrObject === 'object') { @@ -22,12 +23,13 @@ exports.init = function (sbot, config) { } else { sbot.db.get(idOrObject, cb) } - } + }) + // already hooked in the publish compat plugin (if loaded) sbot.publish = sbot.db.publish - sbot.whoami = () => ({ id: sbot.id }) - sbot.ready = () => true + sbot.whoami = Hookable(() => ({ id: sbot.id })) + sbot.ready = Hookable(() => true) sbot.keys = config.keys - sbot.createWriteStream = function createWriteStream(cb) { + sbot.createWriteStream = Hookable(function createWriteStream(cb) { return pull( pull.asyncMap(sbot.db.add), pull.drain( @@ -36,5 +38,5 @@ exports.init = function (sbot, config) { cb || ((err) => console.error(new Error('ssb-db2 createWriteStream failed to add messages'), {cause: err})) ) ) - } + }) } diff --git a/compat/log-stream.js b/compat/log-stream.js index 908e3b0f..7fbe4757 100644 --- a/compat/log-stream.js +++ b/compat/log-stream.js @@ -4,6 +4,7 @@ const pull = require('pull-stream') const cat = require('pull-cat') +const Hookable = require('hoox') const { descending, live, toPullStream } = require('../operators') // exports.name is blank to merge into global namespace @@ -13,7 +14,7 @@ exports.manifest = { } exports.init = function (sbot) { - sbot.createLogStream = function createLogStream(opts) { + sbot.createLogStream = Hookable(function createLogStream(opts) { // Apply default values opts = opts || {} const optsKeys = opts.keys === false ? false : true @@ -48,5 +49,5 @@ exports.init = function (sbot) { if (optsOld && !optsSync) return applyLimit(cat([old$, live$])) if (!optsOld && optsSync) return applyLimit(cat([sync$, live$])) if (!optsOld && !optsSync) return applyLimit(live$) - } + }) } diff --git a/compat/publish.js b/compat/publish.js index bfdc71f5..d674d13e 100644 --- a/compat/publish.js +++ b/compat/publish.js @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: LGPL-3.0-only +const Hookable = require('hoox') + exports.init = function (sbot, config) { function publish(content, cb) { publishAs(config.keys, content, cb) @@ -19,6 +21,6 @@ exports.init = function (sbot, config) { ) } - sbot.db.publish = publish - sbot.db.publishAs = publishAs + sbot.db.publish = Hookable(publish) + sbot.db.publishAs = Hookable(publishAs) } From c344520af89a1516000e7f681c12699200b6429d Mon Sep 17 00:00:00 2001 From: Jacob Karlsson Date: Tue, 21 Nov 2023 14:29:09 +0100 Subject: [PATCH 2/2] Hook some more compat things --- compat/ebt.js | 15 ++++++++------- compat/feedstate.js | 5 +++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/compat/ebt.js b/compat/ebt.js index 94b78b2a..23bd40a4 100644 --- a/compat/ebt.js +++ b/compat/ebt.js @@ -3,26 +3,27 @@ // SPDX-License-Identifier: LGPL-3.0-only const pull = require('pull-stream') +const Hookable = require('hoox') const EBTIndex = require('../indexes/ebt') const { onceWhen } = require('../utils') exports.init = function (sbot, config) { sbot.db.registerIndex(EBTIndex) if (!sbot.post) sbot.post = sbot.db.post - sbot.getAtSequence = (key, cb) => { + sbot.getAtSequence = Hookable((key, cb) => { sbot.db.onDrain('ebt', () => { sbot.db.getIndex('ebt').getMessageFromAuthorSequence(key, cb) }) - } - sbot.getAtSequenceNativeMsg = (key, feedFormat, cb) => { + }) + sbot.getAtSequenceNativeMsg = Hookable((key, feedFormat, cb) => { sbot.db.onDrain('ebt', () => { sbot.db .getIndex('ebt') .getNativeMsgFromAuthorSequence(key, feedFormat, cb) }) - } - sbot.add = sbot.db.add - sbot.getVectorClock = function getVectorClock(cb) { + }) + sbot.add = Hookable(sbot.db.add) + sbot.getVectorClock = Hookable(function getVectorClock(cb) { onceWhen( sbot.db2migrate && sbot.db2migrate.synchronized, (isSynced) => isSynced === true, @@ -45,5 +46,5 @@ exports.init = function (sbot, config) { }) } ) - } + }) } diff --git a/compat/feedstate.js b/compat/feedstate.js index c53758c2..d74302ca 100644 --- a/compat/feedstate.js +++ b/compat/feedstate.js @@ -2,10 +2,11 @@ // // SPDX-License-Identifier: LGPL-3.0-only +const Hookable = require('hoox') const { onceWhen } = require('../utils') exports.init = function (sbot, config) { - sbot.getFeedState = function (feedId, cb) { + sbot.getFeedState = Hookable(function (feedId, cb) { onceWhen( sbot.db.stateFeedsReady, (ready) => ready === true, @@ -21,5 +22,5 @@ exports.init = function (sbot, config) { }) } ) - } + }) }