diff --git a/packages/grafana-data/src/types/vector.ts b/packages/grafana-data/src/types/vector.ts index bb6895246ae..be903d96abf 100644 --- a/packages/grafana-data/src/types/vector.ts +++ b/packages/grafana-data/src/types/vector.ts @@ -13,42 +13,46 @@ declare global { // JS original sin // this if condition is because Jest will re-exec this block multiple times (in a browser this only runs once) -if (!Object.getOwnPropertyDescriptor(Array.prototype, 'toArray')) { - Object.defineProperties(Array.prototype, { - get: { - value: function (idx: number) { - return this[idx]; +export function patchArrayVectorProrotypeMethods() { + if (!Object.getOwnPropertyDescriptor(Array.prototype, 'toArray')) { + Object.defineProperties(Array.prototype, { + get: { + value: function (idx: number) { + return this[idx]; + }, + writable: true, + enumerable: false, + configurable: true, }, - writable: true, - enumerable: false, - configurable: true, - }, - set: { - value: function (idx: number, value: unknown) { - this[idx] = value; + set: { + value: function (idx: number, value: unknown) { + this[idx] = value; + }, + writable: true, + enumerable: false, + configurable: true, }, - writable: true, - enumerable: false, - configurable: true, - }, - add: { - value: function (value: unknown) { - this.push(value); + add: { + value: function (value: unknown) { + this.push(value); + }, + writable: true, + enumerable: false, + configurable: true, }, - writable: true, - enumerable: false, - configurable: true, - }, - toArray: { - value: function () { - return this; + toArray: { + value: function () { + return this; + }, + writable: true, + enumerable: false, + configurable: true, }, - writable: true, - enumerable: false, - configurable: true, - }, - }); + }); + } } +//this function call is intentional +patchArrayVectorProrotypeMethods(); /** @deprecated use a simple Array */ export interface Vector extends Array { diff --git a/public/app/features/plugins/sandbox/code_loader.ts b/public/app/features/plugins/sandbox/code_loader.ts index 6b7626cd802..110af632476 100644 --- a/public/app/features/plugins/sandbox/code_loader.ts +++ b/public/app/features/plugins/sandbox/code_loader.ts @@ -1,4 +1,4 @@ -import { PluginMeta } from '@grafana/data'; +import { PluginMeta, patchArrayVectorProrotypeMethods } from '@grafana/data'; import { transformPluginSourceForCDN } from '../cdn/utils'; import { resolveWithCache } from '../loader/cache'; @@ -91,3 +91,11 @@ function patchPluginSourceMap(meta: PluginMeta, pluginCode: string): string { } return pluginCode; } + +export function patchSandboxEnvironmentPrototype(sandboxEnvironment: SandboxEnvironment) { + // same as https://github.com/grafana/grafana/blob/main/packages/grafana-data/src/types/vector.ts#L16 + // Array is a "reflective" type in Near-membrane and doesn't get an identify continuity + sandboxEnvironment.evaluate( + `${patchArrayVectorProrotypeMethods.toString()};${patchArrayVectorProrotypeMethods.name}()` + ); +} diff --git a/public/app/features/plugins/sandbox/sandbox_plugin_loader.ts b/public/app/features/plugins/sandbox/sandbox_plugin_loader.ts index 15a06af9f5d..e169905fab6 100644 --- a/public/app/features/plugins/sandbox/sandbox_plugin_loader.ts +++ b/public/app/features/plugins/sandbox/sandbox_plugin_loader.ts @@ -7,7 +7,7 @@ import { defaultTrustedTypesPolicy } from 'app/core/trustedTypePolicies'; import { getPluginSettings } from '../pluginSettings'; -import { getPluginCode } from './code_loader'; +import { getPluginCode, patchSandboxEnvironmentPrototype } from './code_loader'; import { getGeneralSandboxDistortionMap, distortLiveApis } from './distortion_map'; import { getSafeSandboxDomElement, @@ -179,6 +179,8 @@ async function doImportPluginModuleInSandbox(meta: PluginMeta): Promise