var fs = require('fs') var path = require('path') var mainPath = path.resolve(__dirname, 'index.js') var mtime var main function loadMain() { var stats = fs.statSync(mainPath) var prevMtime = mtime mtime = stats.mtime.getTime() if (mtime !== prevMtime) { delete require.cache[mainPath] main = require(mainPath) var k if (main.manifest && exports.manifest) { for (k in main.manifest) exports.manifest[k] = main.manifest[k] } for (k in main) exports[k] = main[k] return true } } loadMain() exports.init = function (sbot, config) { var api var wsUseHandler var wsUse = sbot.ws && sbot.ws.use if (wsUse) sbot.ws.use = function (handler) { if (!wsUseHandler) wsUseHandler = handler else wsUse.call(sbot.ws, handler) } function loadApi() { if (loadMain() || !api) { var prevApi = api var init = typeof main === 'function' ? main : main.init wsUseHandler = null api = init.call(main, sbot, config) } } if (wsUse) wsUse(function (req, res, next) { loadApi() if (wsUseHandler) wsUseHandler(req, res, next) else if (next) next() }) loadApi() var wrappedApi = {} function wrapMethod(method) { return function () { loadApi() return api[method].apply(api, arguments) } } for (var method in main.manifest || api) { wrappedApi[method] = wrapMethod(method) } return wrappedApi }