From 5caa2c0e3a152d1fc947b86329778db462139db0 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 7 Nov 2024 14:26:30 -0500 Subject: [PATCH] fix: logging in AppConnection --- src/puter-js/src/index.js | 2 ++ src/puter-js/src/modules/UI.js | 57 +++++++++++++++++----------------- src/putility/src/libs/log.js | 27 ++++++---------- 3 files changed, 40 insertions(+), 46 deletions(-) diff --git a/src/puter-js/src/index.js b/src/puter-js/src/index.js index 05c550e6..eae7e576 100644 --- a/src/puter-js/src/index.js +++ b/src/puter-js/src/index.js @@ -104,6 +104,8 @@ window.puter = (function() { const context = new putility.libs.context.Context() .follow(this, ['env', 'util', 'authToken', 'APIOrigin', 'appID']); + context.puter = this; + this.services = new putility.system.ServiceManager({ context }); this.context = context; context.services = this.services; diff --git a/src/puter-js/src/modules/UI.js b/src/puter-js/src/modules/UI.js index 1b395540..1d318644 100644 --- a/src/puter-js/src/modules/UI.js +++ b/src/puter-js/src/modules/UI.js @@ -18,13 +18,11 @@ class AppConnection extends EventListener { // (Closing and close events will still function.) #usesSDK; - static from (values, { appInstanceID, messageTarget }) { - const connection = new AppConnection( - messageTarget, - appInstanceID, - values.appInstanceID, - values.usesSDK - ); + static from (values, context) { + const connection = new AppConnection(context, { + target: values.appInstanceID, + usesSDK: values.usesSDK, + }); // When a connection is established the app is able to // provide some additional information about itself @@ -33,25 +31,25 @@ class AppConnection extends EventListener { return connection; } - constructor(messageTarget, appInstanceID, targetAppInstanceID, usesSDK) { + constructor(context, { target, usesSDK }) { super([ 'message', // The target sent us something with postMessage() 'close', // The target app was closed ]); - this.messageTarget = messageTarget; - this.appInstanceID = appInstanceID; - this.targetAppInstanceID = targetAppInstanceID; + this.messageTarget = context.messageTarget; + this.appInstanceID = context.appInstanceID; + this.targetAppInstanceID = target; this.#isOpen = true; this.#usesSDK = usesSDK; - this.log = globalThis.puter.log.fields({ + this.log = context.puter.log.fields({ category: 'ipc', }); this.log.fields({ - constructor_appInstanceID: appInstanceID, - puter_appInstanceID: puter.appInstanceID, - targetAppInstanceID, - }).info(`AppConnection created to ${targetAppInstanceID}`, this); + cons_source: context.appInstanceID, + source: context.puter.appInstanceID, + target, + }).info(`AppConnection created to ${target}`, this); // TODO: Set this.#puterOrigin to the puter origin @@ -225,6 +223,7 @@ class UI extends EventListener { ]; super(eventNames); this.#eventNames = eventNames; + this.context = context; this.appInstanceID = appInstanceID; this.parentInstanceID = parentInstanceID; this.appID = context.appID; @@ -238,8 +237,17 @@ class UI extends EventListener { return; } + // Context to pass to AppConnection instances + this.context = this.context.sub({ + appInstanceID: this.appInstanceID, + messageTarget: this.messageTarget, + }); + if (this.parentInstanceID) { - this.#parentAppConnection = new AppConnection(this.messageTarget, this.appInstanceID, this.parentInstanceID, true); + this.#parentAppConnection = new AppConnection(this.context, { + target: this.parentInstanceID, + usesSDK: true + }); } // Tell the host environment that this app is using the Puter SDK and is ready to receive messages, @@ -481,10 +489,7 @@ class UI extends EventListener { } else if ( e.data.msg === 'connection' ) { e.data.usesSDK = true; // we can safely assume this - const conn = AppConnection.from(e.data, { - appInstanceID: this.appInstanceID, - messageTarget: window.parent, - }); + const conn = AppConnection.from(e.data, this.context); const accept = value => { this.messageTarget?.postMessage({ $: 'connection-resp', @@ -995,10 +1000,7 @@ class UI extends EventListener { }, }); - return AppConnection.from(app_info, { - appInstanceID: this.appInstanceID, - messageTarget: this.messageTarget, - }); + return AppConnection.from(app_info, this.context); } connectToInstance = async function connectToInstance (app_name) { @@ -1009,10 +1011,7 @@ class UI extends EventListener { } }); - return AppConnection.from(app_info, { - appInstanceID: this.appInstanceID, - messageTarget: this.messageTarget, - }); + return AppConnection.from(app_info, this.context); } parentApp() { diff --git a/src/putility/src/libs/log.js b/src/putility/src/libs/log.js index 7fd5eb6f..c5a20870 100644 --- a/src/putility/src/libs/log.js +++ b/src/putility/src/libs/log.js @@ -73,13 +73,14 @@ class ConsoleLogger extends AdvancedBase { // util: require('util'), util: { - inspect: v => { - if (typeof v === 'string') return v; - try { - return JSON.stringify(v); - } catch (e) {} - return '' + v; - } + inspect: v => v, + // inspect: v => { + // if (typeof v === 'string') return v; + // try { + // return JSON.stringify(v); + // } catch (e) {} + // return '' + v; + // } } } static PROPERTIES = { @@ -113,23 +114,15 @@ class ConsoleLogger extends AdvancedBase { str += `${l.ansii}[${level.toUpperCase()}]\x1b[0m `; str += message; - // values - if (values.length) { - str += ' '; - str += values - .map(v => util.inspect(v)) - .join(' '); - } - // fields if (Object.keys(fields).length) { str += ' '; str += Object.entries(fields) .map(([k, v]) => `\n ${k}=${util.inspect(v)}`) - .join(' '); + .join(' ') + '\n'; } - (this.console ?? console)[l.err ? 'error' : 'log'](str); + (this.console ?? console)[l.err ? 'error' : 'log'](str, ...values); } } }