2023-08-31 15:45:44 +02:00
|
|
|
import {
|
|
|
|
|
AppPlugin,
|
|
|
|
|
DataSourceApi,
|
|
|
|
|
DataSourceJsonData,
|
|
|
|
|
DataSourcePlugin,
|
|
|
|
|
DataSourcePluginMeta,
|
2024-09-09 10:38:35 +01:00
|
|
|
PluginLoadingStrategy,
|
2023-08-31 15:45:44 +02:00
|
|
|
PluginMeta,
|
|
|
|
|
} from '@grafana/data';
|
2024-10-04 14:55:09 +02:00
|
|
|
import { config } from '@grafana/runtime';
|
2023-08-31 15:45:44 +02:00
|
|
|
import { DataQuery } from '@grafana/schema';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2022-07-20 09:25:09 +02:00
|
|
|
import { GenericDataSourcePlugin } from '../datasources/types';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
|
|
|
|
import builtInPlugins from './built_in_plugins';
|
2024-04-24 12:27:28 +02:00
|
|
|
import { getPluginFromCache, registerPluginInCache } from './loader/cache';
|
2024-03-21 09:22:35 +01:00
|
|
|
// SystemJS has to be imported before the sharedDependenciesMap
|
|
|
|
|
import { SystemJS } from './loader/systemjs';
|
|
|
|
|
// eslint-disable-next-line import/order
|
2023-08-31 15:45:44 +02:00
|
|
|
import { sharedDependenciesMap } from './loader/sharedDependencies';
|
|
|
|
|
import { decorateSystemJSFetch, decorateSystemJSResolve, decorateSystemJsOnload } from './loader/systemjsHooks';
|
|
|
|
|
import { SystemJSWithLoaderHooks } from './loader/types';
|
2024-09-09 10:38:35 +01:00
|
|
|
import { buildImportMap, resolveModulePath } from './loader/utils';
|
2023-06-05 10:51:36 +02:00
|
|
|
import { importPluginModuleInSandbox } from './sandbox/sandbox_plugin_loader';
|
2023-08-31 15:45:44 +02:00
|
|
|
import { isFrontendSandboxSupported } from './sandbox/utils';
|
2023-04-27 07:18:38 +02:00
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
const imports = buildImportMap(sharedDependenciesMap);
|
2024-03-21 09:22:35 +01:00
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
SystemJS.addImportMap({ imports });
|
2017-10-26 12:07:23 +02:00
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
const systemJSPrototype: SystemJSWithLoaderHooks = SystemJS.constructor.prototype;
|
2018-06-14 12:32:57 +01:00
|
|
|
|
2024-05-02 11:55:20 +02:00
|
|
|
// This instructs SystemJS to load plugin assets using fetch and eval if it returns a truthy value, otherwise
|
2024-09-09 10:38:35 +01:00
|
|
|
// it will load the plugin using a script tag. The logic that sets loadingStrategy comes from the backend.
|
|
|
|
|
// See: pkg/services/pluginsintegration/pluginassets/pluginassets.go
|
2024-04-24 12:27:28 +02:00
|
|
|
systemJSPrototype.shouldFetch = function (url) {
|
|
|
|
|
const pluginInfo = getPluginFromCache(url);
|
2024-05-02 11:55:20 +02:00
|
|
|
const jsTypeRegEx = /^[^#?]+\.(js)([?#].*)?$/;
|
2024-04-24 12:27:28 +02:00
|
|
|
|
2024-09-09 10:38:35 +01:00
|
|
|
if (!jsTypeRegEx.test(url)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Boolean(pluginInfo?.loadingStrategy !== PluginLoadingStrategy.script);
|
2024-04-24 12:27:28 +02:00
|
|
|
};
|
2017-10-20 10:38:52 +02:00
|
|
|
|
2024-02-21 13:25:00 +00:00
|
|
|
const originalImport = systemJSPrototype.import;
|
|
|
|
|
// Hook Systemjs import to support plugins that only have a default export.
|
|
|
|
|
systemJSPrototype.import = function (...args: Parameters<typeof originalImport>) {
|
|
|
|
|
return originalImport.apply(this, args).then((module) => {
|
|
|
|
|
if (module && module.__useDefault) {
|
|
|
|
|
return module.default;
|
|
|
|
|
}
|
|
|
|
|
return module;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
const systemJSFetch = systemJSPrototype.fetch;
|
|
|
|
|
systemJSPrototype.fetch = function (url: string, options?: Record<string, unknown>) {
|
|
|
|
|
return decorateSystemJSFetch(systemJSFetch, url, options);
|
|
|
|
|
};
|
2019-04-12 09:45:22 -07:00
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
const systemJSResolve = systemJSPrototype.resolve;
|
|
|
|
|
systemJSPrototype.resolve = decorateSystemJSResolve.bind(systemJSPrototype, systemJSResolve);
|
2017-10-01 20:02:25 +02:00
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
// Older plugins load .css files which resolves to a CSS Module.
|
|
|
|
|
// https://github.com/WICG/webcomponents/blob/gh-pages/proposals/css-modules-v1-explainer.md#importing-a-css-module
|
|
|
|
|
// Any css files loaded via SystemJS have their styles applied onload.
|
|
|
|
|
systemJSPrototype.onload = decorateSystemJsOnload;
|
2017-10-01 20:02:25 +02:00
|
|
|
|
2023-06-05 10:51:36 +02:00
|
|
|
export async function importPluginModule({
|
|
|
|
|
path,
|
2024-09-09 10:38:35 +01:00
|
|
|
pluginId,
|
|
|
|
|
loadingStrategy,
|
2023-06-05 10:51:36 +02:00
|
|
|
version,
|
|
|
|
|
isAngular,
|
2024-10-04 14:55:09 +02:00
|
|
|
moduleHash,
|
2023-06-05 10:51:36 +02:00
|
|
|
}: {
|
|
|
|
|
path: string;
|
|
|
|
|
pluginId: string;
|
2024-09-09 10:38:35 +01:00
|
|
|
loadingStrategy: PluginLoadingStrategy;
|
2023-06-05 10:51:36 +02:00
|
|
|
version?: string;
|
|
|
|
|
isAngular?: boolean;
|
2024-10-04 14:55:09 +02:00
|
|
|
moduleHash?: string;
|
2023-08-31 15:45:44 +02:00
|
|
|
}): Promise<System.Module> {
|
2021-11-10 11:54:58 +01:00
|
|
|
if (version) {
|
2024-09-09 10:38:35 +01:00
|
|
|
registerPluginInCache({ path, version, loadingStrategy });
|
2021-11-10 11:54:58 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-26 17:14:40 +02:00
|
|
|
const builtIn = builtInPlugins[path];
|
2017-10-01 20:02:25 +02:00
|
|
|
if (builtIn) {
|
2019-09-03 09:29:02 +01:00
|
|
|
// for handling dynamic imports
|
|
|
|
|
if (typeof builtIn === 'function') {
|
|
|
|
|
return await builtIn();
|
|
|
|
|
} else {
|
2021-11-10 19:06:55 +01:00
|
|
|
return builtIn;
|
2019-09-03 09:29:02 +01:00
|
|
|
}
|
2017-10-01 20:02:25 +02:00
|
|
|
}
|
2023-06-05 10:51:36 +02:00
|
|
|
|
2024-10-04 14:55:09 +02:00
|
|
|
const modulePath = resolveModulePath(path);
|
|
|
|
|
|
|
|
|
|
// inject integrity hash into SystemJS import map
|
|
|
|
|
if (config.featureToggles.pluginsSriChecks) {
|
|
|
|
|
const resolvedModule = System.resolve(modulePath);
|
|
|
|
|
const integrityMap = System.getImportMap().integrity;
|
|
|
|
|
|
|
|
|
|
if (moduleHash && integrityMap && !integrityMap[resolvedModule]) {
|
|
|
|
|
SystemJS.addImportMap({
|
|
|
|
|
integrity: {
|
|
|
|
|
[resolvedModule]: moduleHash,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-08 12:19:28 +01:00
|
|
|
|
2023-06-05 10:51:36 +02:00
|
|
|
// the sandboxing environment code cannot work in nodejs and requires a real browser
|
2023-11-20 12:00:31 +01:00
|
|
|
if (await isFrontendSandboxSupported({ isAngular, pluginId })) {
|
2023-06-05 10:51:36 +02:00
|
|
|
return importPluginModuleInSandbox({ pluginId });
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 12:19:28 +01:00
|
|
|
return SystemJS.import(modulePath);
|
2023-06-05 10:51:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
export function importDataSourcePlugin(meta: DataSourcePluginMeta): Promise<GenericDataSourcePlugin> {
|
2024-05-13 09:55:26 +02:00
|
|
|
const isAngular = meta.angular?.detected ?? meta.angularDetected;
|
2024-09-09 10:38:35 +01:00
|
|
|
const fallbackLoadingStrategy = meta.loadingStrategy ?? PluginLoadingStrategy.fetch;
|
2023-06-05 10:51:36 +02:00
|
|
|
return importPluginModule({
|
|
|
|
|
path: meta.module,
|
|
|
|
|
version: meta.info?.version,
|
2024-05-13 09:55:26 +02:00
|
|
|
isAngular,
|
2024-09-09 10:38:35 +01:00
|
|
|
loadingStrategy: fallbackLoadingStrategy,
|
2023-06-05 10:51:36 +02:00
|
|
|
pluginId: meta.id,
|
2024-10-04 14:55:09 +02:00
|
|
|
moduleHash: meta.moduleHash,
|
2023-06-05 10:51:36 +02:00
|
|
|
}).then((pluginExports) => {
|
2019-04-04 18:30:15 +02:00
|
|
|
if (pluginExports.plugin) {
|
2023-08-31 15:45:44 +02:00
|
|
|
const dsPlugin: GenericDataSourcePlugin = pluginExports.plugin;
|
2019-04-30 22:36:46 -07:00
|
|
|
dsPlugin.meta = meta;
|
|
|
|
|
return dsPlugin;
|
2019-04-04 18:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pluginExports.Datasource) {
|
2023-08-31 15:45:44 +02:00
|
|
|
const dsPlugin = new DataSourcePlugin<
|
|
|
|
|
DataSourceApi<DataQuery, DataSourceJsonData>,
|
|
|
|
|
DataQuery,
|
|
|
|
|
DataSourceJsonData
|
2019-11-15 09:18:09 +00:00
|
|
|
>(pluginExports.Datasource);
|
2019-04-04 18:30:15 +02:00
|
|
|
dsPlugin.setComponentsFromLegacyExports(pluginExports);
|
2019-04-30 22:36:46 -07:00
|
|
|
dsPlugin.meta = meta;
|
2019-04-04 18:30:15 +02:00
|
|
|
return dsPlugin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Error('Plugin module is missing DataSourcePlugin or Datasource constructor export');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 15:45:44 +02:00
|
|
|
export function importAppPlugin(meta: PluginMeta): Promise<AppPlugin> {
|
2024-05-13 09:55:26 +02:00
|
|
|
const isAngular = meta.angular?.detected ?? meta.angularDetected;
|
2024-09-09 10:38:35 +01:00
|
|
|
const fallbackLoadingStrategy = meta.loadingStrategy ?? PluginLoadingStrategy.fetch;
|
2023-06-05 10:51:36 +02:00
|
|
|
return importPluginModule({
|
|
|
|
|
path: meta.module,
|
|
|
|
|
version: meta.info?.version,
|
2024-05-13 09:55:26 +02:00
|
|
|
isAngular,
|
2024-09-09 10:38:35 +01:00
|
|
|
loadingStrategy: fallbackLoadingStrategy,
|
2023-06-05 10:51:36 +02:00
|
|
|
pluginId: meta.id,
|
2024-10-04 14:55:09 +02:00
|
|
|
moduleHash: meta.moduleHash,
|
2023-06-05 10:51:36 +02:00
|
|
|
}).then((pluginExports) => {
|
2023-08-31 15:45:44 +02:00
|
|
|
const plugin: AppPlugin = pluginExports.plugin ? pluginExports.plugin : new AppPlugin();
|
2019-05-20 13:05:23 -07:00
|
|
|
plugin.init(meta);
|
|
|
|
|
plugin.meta = meta;
|
2019-06-15 08:26:31 +02:00
|
|
|
plugin.setComponentsFromLegacyExports(pluginExports);
|
2019-04-30 22:36:46 -07:00
|
|
|
return plugin;
|
2019-04-04 18:30:15 +02:00
|
|
|
});
|
|
|
|
|
}
|