Skip to content

Commit

Permalink
Dynamic Imports of SDK Modules (#260)
Browse files Browse the repository at this point in the history
* Save approach 1 while working on approach 2

* Almost have module loader working

* Dynamic load for all except discovery

* Complete dynamic imports of sdk modules

* code clean up

* update jsdoc

* update discoveryContext dynamically

* create generateDefaultSdkConstants

* Organize class logic

* remove logs and refine logic

* test versions < 0.11

* fix for versions less than 0.11

* fix pascalcase issue

* fix unit tests

* Add constants

* update year on cp

* Update to refresh old test

* fix unit tests
  • Loading branch information
ksentak authored Dec 17, 2024
1 parent 302b6b6 commit 4547f28
Show file tree
Hide file tree
Showing 11 changed files with 530 additions and 113 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"build-dev": "webpack --config webpack.dev.js",
"test": "jest --config test/jest.config.js",
"testCoverage": "NODE_ENV=test jest --config test/jest.config.js",
"postinstall": "npm update @firebolt-js/manage-sdk",
"format": "prettier --write \"**/*.js\"",
"lint": "eslint ."
},
Expand Down
96 changes: 27 additions & 69 deletions src/FireboltExampleInvoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,40 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Accessibility, Account, Advertising, Authentication, Capabilities, Device, Discovery, Keyboard, Lifecycle, Localization, Metrics, Profile, Parameters, SecondScreen, SecureStorage } from '@firebolt-js/sdk';
import {
Advertising as ManageAdvertising,
AcknowledgeChallenge,
Device as ManageDevice,
Wifi,
Account as ManageAccount,
ClosedCaptions,
Keyboard as ManageKeyboard,
Localization as ManageLocalization,
PinChallenge,
Privacy,
VoiceGuidance,
UserGrants,
Metrics as ManageMetrics,
SecureStorage as ManageSecureStorage,
Discovery as ManageDiscovery,
AudioDescriptions,
HDMIInput,
} from '@firebolt-js/manage-sdk';
import { Content } from '@firebolt-js/discovery-sdk';
import * as CoreSDK from '@firebolt-js/sdk';
import * as ManageSDK from '@firebolt-js/manage-sdk';
import FireboltSdkManager from './utils/FireboltSdkManager';
import DiscoveryInvoker from './invokers/DiscoveryInvoker';
const discoveryInvoker = new DiscoveryInvoker();
const logger = require('./utils/Logger')('FireboltExampleInvoker.js');
import { removeSetInMethodName } from './utils/Utils';
import { eventEmitter } from './Toast';
import { CONSTANTS } from './constant';

/**
* Dynamically check if the Discovery SDK is available as a dependency.
* If available, require it. Otherwise, log a warning.
*/
const dependencies = DEPENDENCIES; // Injected by Webpack DefinePlugin
let DiscoverySDK;

if (dependencies.hasOwnProperty('@firebolt-js/discovery-sdk')) {
try {
DiscoverySDK = require('@firebolt-js/discovery-sdk');
} catch (error) {
console.warn('DiscoverySDK is not available:', error);
}
}

// Initialize the Firebolt SDK Module Loader
const sdkManager = new FireboltSdkManager(CoreSDK, ManageSDK, DiscoverySDK, dependencies);

// Dynamically generate the module map based on the imported SDKs
const moduleMap = sdkManager.generateModuleMap();

// Export the dynamically created module map
export const MODULE_MAP = moduleMap;

// Commenting the below APIs as they have been deprecated from discovery sdk , can be uncommented when added as ripple-rpc APIs in future ticket
const MAP = {
'discovery.purchasedContent': discoveryInvoker.purchasedContent.bind(discoveryInvoker),
Expand All @@ -52,54 +58,6 @@ const MAP = {
// 'content.entity': discoveryInvoker.getEntityInfo.bind(discoveryInvoker),
};

const CORE_MODULE_MAP = {
accessibility: Accessibility,
account: Account,
advertising: Advertising,
authentication: Authentication,
capabilities: Capabilities,
device: Device,
discovery: Discovery,
keyboard: Keyboard,
lifecycle: Lifecycle,
localization: Localization,
metrics: Metrics,
profile: Profile,
parameters: Parameters,
secondscreen: SecondScreen,
securestorage: SecureStorage,
};

const MANAGE_MODULE_MAP = {
advertising: ManageAdvertising,
acknowledgechallenge: AcknowledgeChallenge,
device: ManageDevice,
wifi: Wifi,
account: ManageAccount,
closedcaptions: ClosedCaptions,
keyboard: ManageKeyboard,
pinchallenge: PinChallenge,
privacy: Privacy,
voiceguidance: VoiceGuidance,
localization: ManageLocalization,
usergrants: UserGrants,
metrics: ManageMetrics,
securestorage: ManageSecureStorage,
discovery: ManageDiscovery,
audiodescriptions: AudioDescriptions,
hdmiinput: HDMIInput,
};

const DISCOVERY_MODULE_MAP = {
content: Content,
};

export const MODULE_MAP = {
core: CORE_MODULE_MAP,
manage: MANAGE_MODULE_MAP,
discovery: DISCOVERY_MODULE_MAP,
};

// importing additional invoker which has external sdk's being exported and adding those modules in the MODULE_MAP
try {
const additionalInvoker = require('../plugins/FireboltExtensionInvoker').default;
Expand Down
1 change: 1 addition & 0 deletions src/FireboltTransportInvoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import Transport from '@firebolt-js/sdk/dist/lib/Transport';
import { CONSTANTS } from './constant';
const logger = require('./utils/Logger')('FireboltTransportInvoker.js');

let invokeManager, invokeProvider;
try {
Expand Down
57 changes: 27 additions & 30 deletions src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,30 @@
*/

import CONFIG_CONSTANTS from 'config';
import CORE_OPEN_RPC from '@firebolt-js/sdk/dist/firebolt-core-open-rpc';
import MANAGE_OPEN_RPC from '@firebolt-js/manage-sdk/dist/firebolt-manage-open-rpc';
import DISCOVERY_OPEN_RPC from '@firebolt-js/discovery-sdk/dist/firebolt-discovery-open-rpc';
import * as CoreSDK from '@firebolt-js/sdk';
import * as ManageSDK from '@firebolt-js/manage-sdk';
import FireboltSdkManager from './utils/FireboltSdkManager';

/**
* Dynamically check if the Discovery SDK is available as a dependency.
* If available, require it. Otherwise, log a warning.
*/
const dependencies = DEPENDENCIES; // Injected by Webpack DefinePlugin
let DiscoverySDK;

if (dependencies.hasOwnProperty('@firebolt-js/discovery-sdk')) {
try {
DiscoverySDK = require('@firebolt-js/discovery-sdk');
} catch (error) {
console.warn('DiscoverySDK is not available:', error);
}
}

// Initialize the Firebolt SDK Module Loader
const sdkManager = new FireboltSdkManager(CoreSDK, ManageSDK, DiscoverySDK, dependencies);

const defaultSdks = sdkManager.generateDefaultSdkConstants();

export const CONSTANTS = {
ALL_SDKS: 'ALL SDKS',
SDK: 'SDK',
Expand Down Expand Up @@ -142,37 +163,13 @@ export const CONSTANTS = {
EXCLUDED_METHODS_FOR_SDK: [],
EXCLUDED_METHODS_FOR_TRANSPORT: [],
REGISTERPROVIDER: 'registerprovider',
defaultSDKs: defaultSdks,
INVOKEPROVIDER: 'invokeProvider',
INVOKEMANAGER: 'invokeManager',
defaultSDKs: [
{
name: 'Core',
openRpc: CORE_OPEN_RPC,
validation: function () {
return !(process.env.MF_VALUE && !process.env.MOCKOS);
},
unavailableMessage: 'MockOs is not running',
},
{
name: 'Manage',
openRpc: MANAGE_OPEN_RPC,
validation: function () {
return !(process.env.MF_VALUE && !process.env.MOCKOS);
},
unavailableMessage: 'MockOs is not running',
},
{
name: 'Discovery',
openRpc: DISCOVERY_OPEN_RPC,
validation: function () {
return !(process.env.MF_VALUE && !process.env.MOCKOS);
},
unavailableMessage: 'MockOs is not running',
},
],

additionalSDKs: [],
EXCLUDED_METHODS_FOR_MFOS: [],
PASCAL_CASED_MODULES: ['SecondScreen', 'SecureStorage', 'ClosedCaptions', 'AcknowledgeChallenge', 'DeveloperTools', 'LifecycleManagement', 'PinChallenge', 'UserGrants', 'VoiceGuidance'],
X_MODULE_DESCRIPTIONS: 'x-module-descriptions',
...CONFIG_CONSTANTS,
VERSIONS: 'Versions',
NO_RESULT_OR_ERROR_MESSAGE: 'No result or error in response. eg: {jsonrpc: "2.0", id: x }',
Expand Down
Loading

0 comments on commit 4547f28

Please sign in to comment.