From e638b155a197b4a408a70c1837558def76fa7fd1 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 16 Oct 2024 18:11:08 -0400 Subject: [PATCH] js: Kill gGlobalSettings. Decouples things a bit. --- core/core.js | 114 +++++++++++++++++++++++++-------------------------- deps/c-ares | 2 +- 2 files changed, 57 insertions(+), 59 deletions(-) diff --git a/core/core.js b/core/core.js index 6a986dbe..eac01400 100644 --- a/core/core.js +++ b/core/core.js @@ -87,10 +87,6 @@ const k_global_settings = { }, }; -let gGlobalSettings = { - index: '/~core/apps/', -}; - let kPingInterval = 60 * 1000; /** @@ -345,59 +341,59 @@ async function getProcessBlob(blobId, key, options) { return []; } }, - permissionsGranted: function () { + permissionsGranted: async function () { let user = process?.credentials?.session?.name; + let settings = await loadSettings(); if ( user && options?.packageOwner && options?.packageName && - gGlobalSettings.userPermissions && - gGlobalSettings.userPermissions[user] && - gGlobalSettings.userPermissions[user][options.packageOwner] + settings.userPermissions && + settings.userPermissions[user] && + settings.userPermissions[user][options.packageOwner] ) { - return gGlobalSettings.userPermissions[user][ + return settings.userPermissions[user][ options.packageOwner ][options.packageName]; } }, - allPermissionsGranted: function () { + allPermissionsGranted: async function () { let user = process?.credentials?.session?.name; + let settings = await loadSettings(); if ( user && options?.packageOwner && options?.packageName && - gGlobalSettings.userPermissions && - gGlobalSettings.userPermissions[user] + settings.userPermissions && + settings.userPermissions[user] ) { - return gGlobalSettings.userPermissions[user]; + return settings.userPermissions[user]; } }, - permissionsForUser: function (user) { - return ( - (gGlobalSettings?.permissions - ? gGlobalSettings.permissions[user] - : []) ?? [] - ); + permissionsForUser: async function (user) { + let settings = await loadSettings(); + return settings?.permissions?.[user] ?? []; }, apps: (user) => getApps(user, process), getSockets: getSockets, - permissionTest: function (permission) { + permissionTest: async function (permission) { let user = process?.credentials?.session?.name; + let settings = await loadSettings(); if (!user || !options?.packageOwner || !options?.packageName) { return; } else if ( - gGlobalSettings.userPermissions && - gGlobalSettings.userPermissions[user] && - gGlobalSettings.userPermissions[user][options.packageOwner] && - gGlobalSettings.userPermissions[user][options.packageOwner][ + settings.userPermissions && + settings.userPermissions[user] && + settings.userPermissions[user][options.packageOwner] && + settings.userPermissions[user][options.packageOwner][ options.packageName ] && - gGlobalSettings.userPermissions[user][options.packageOwner][ + settings.userPermissions[user][options.packageOwner][ options.packageName ][permission] !== undefined ) { if ( - gGlobalSettings.userPermissions[user][options.packageOwner][ + settings.userPermissions[user][options.packageOwner][ options.packageName ][permission] ) { @@ -509,23 +505,24 @@ async function getProcessBlob(blobId, key, options) { } }; if (process.credentials?.permissions?.administration) { - imports.core.globalSettingsDescriptions = function () { + imports.core.globalSettingsDescriptions = async function () { let settings = Object.assign({}, k_global_settings); - for (let [key, value] of Object.entries(gGlobalSettings)) { + for (let [key, value] of Object.entries(await loadSettings())) { if (settings[key]) { settings[key].value = value; } } return settings; }; - imports.core.globalSettingsGet = function (key) { - return gGlobalSettings[key]; + imports.core.globalSettingsGet = async function (key) { + let settings = await loadSettings(); + return settings?.[key]; }; imports.core.globalSettingsSet = async function (key, value) { print('Setting', key, value); - await loadSettings(); - gGlobalSettings[key] = value; - setGlobalSettings(gGlobalSettings); + let settings = await loadSettings(); + settings[key] = value; + await setGlobalSettings(settings); print('Done.'); }; imports.core.deleteUser = async function (user) { @@ -707,8 +704,9 @@ async function getProcessBlob(blobId, key, options) { imports.ssb.addEventListener = undefined; imports.ssb.removeEventListener = undefined; imports.ssb.getIdentityInfo = undefined; - imports.fetch = function (url, options) { - return http.fetch(url, options, gGlobalSettings.fetch_hosts); + imports.fetch = async function (url, options) { + let settings = await loadSettings(); + return http.fetch(url, options, settings?.fetch_hosts); }; if ( @@ -759,13 +757,13 @@ async function getProcessBlob(blobId, key, options) { ); }; } - process.sendPermissions = function sendPermissions() { + process.sendPermissions = async function sendPermissions() { process.app.send({ action: 'permissions', - permissions: imports.core.permissionsGranted(), + permissions: await imports.core.permissionsGranted(), }); }; - process.resetPermission = function resetPermission(permission) { + process.resetPermission = async function resetPermission(permission) { let user = process?.credentials?.session?.name; storePermission( user, @@ -774,7 +772,7 @@ async function getProcessBlob(blobId, key, options) { permission, undefined ); - process.sendPermissions(); + return process.sendPermissions(); }; process.task.setImports(imports); process.task.activate(); @@ -807,7 +805,7 @@ async function getProcessBlob(blobId, key, options) { broadcastEvent('onSessionBegin', [getUser(process, process)]); if (process.app) { process.app.send({action: 'ready', version: version()}); - process.sendPermissions(); + await process.sendPermissions(); } await process.task.execute({name: appSourceName, source: appSource}); resolveReady(process); @@ -837,7 +835,6 @@ async function getProcessBlob(blobId, key, options) { * @returns */ async function setGlobalSettings(settings) { - gGlobalSettings = settings; try { return await new Database('core').set('settings', JSON.stringify(settings)); } catch (error) { @@ -1229,7 +1226,7 @@ async function loadSettings() { data[key] = value.default_value; } } - gGlobalSettings = data; + return data; } /** @@ -1254,9 +1251,9 @@ function sendStats() { * TODOC */ loadSettings() - .then(function () { - if (tildefriends.https_port && gGlobalSettings.http_redirect) { - httpd.set_http_redirect(gGlobalSettings.http_redirect); + .then(function (settings) { + if (tildefriends.https_port && settings.http_redirect) { + httpd.set_http_redirect(settings.http_redirect); } httpd.all('/app/socket', app.socket); httpd.all('', function default_http_handler(request, response) { @@ -1332,34 +1329,35 @@ loadSettings() * @param {*} permission * @param {*} allow */ -function storePermission(user, packageOwner, packageName, permission, allow) { - if (!gGlobalSettings.userPermissions) { - gGlobalSettings.userPermissions = {}; +async function storePermission(user, packageOwner, packageName, permission, allow) { + let settings = await loadSettings(); + if (!settings.userPermissions) { + settings.userPermissions = {}; } - if (!gGlobalSettings.userPermissions[user]) { - gGlobalSettings.userPermissions[user] = {}; + if (!settings.userPermissions[user]) { + settings.userPermissions[user] = {}; } - if (!gGlobalSettings.userPermissions[user][packageOwner]) { - gGlobalSettings.userPermissions[user][packageOwner] = {}; + if (!settings.userPermissions[user][packageOwner]) { + settings.userPermissions[user][packageOwner] = {}; } - if (!gGlobalSettings.userPermissions[user][packageOwner][packageName]) { - gGlobalSettings.userPermissions[user][packageOwner][packageName] = {}; + if (!settings.userPermissions[user][packageOwner][packageName]) { + settings.userPermissions[user][packageOwner][packageName] = {}; } if ( - gGlobalSettings.userPermissions[user][packageOwner][packageName][ + settings.userPermissions[user][packageOwner][packageName][ permission ] !== allow ) { if (allow === undefined) { - delete gGlobalSettings.userPermissions[user][packageOwner][packageName][ + delete settings.userPermissions[user][packageOwner][packageName][ permission ]; } else { - gGlobalSettings.userPermissions[user][packageOwner][packageName][ + settings.userPermissions[user][packageOwner][packageName][ permission ] = allow; } - setGlobalSettings(gGlobalSettings); + return setGlobalSettings(settings); } } diff --git a/deps/c-ares b/deps/c-ares index 0c1c60dc..a57ff692 160000 --- a/deps/c-ares +++ b/deps/c-ares @@ -1 +1 @@ -Subproject commit 0c1c60dc60114812eb5950e6b50fa69d923a20e6 +Subproject commit a57ff692eeab8d21c853dc1ddaf0164f517074c3