@wixc3/engine-core
Advanced tools
Comparing version 49.1.1 to 49.2.0
import type { Communication } from '../communication.js'; | ||
import type { InitializerOptions } from './types.js'; | ||
export declare const FETCH_OPTIONS_PARAM_NAME = "fetch-options-from-parent"; | ||
export declare const INSTANCE_ID_PARAM_NAME = "iframe-instance-id"; | ||
@@ -42,3 +43,4 @@ export interface IIframeInitializerOptions { | ||
export declare function startIframe({ com, iframe, instanceId, src, envReadyPromise }: StartIframeParams): Promise<void>; | ||
export declare function installRunOptionsInitMessageHandler(target: Window, getRunOptionsParams: () => URLSearchParams): () => void; | ||
export {}; | ||
//# sourceMappingURL=iframe.d.ts.map |
import { WindowInitializerService } from '../window-initializer-service.js'; | ||
import { RUN_OPTIONS_PROVIDED_KIND, RUN_OPTIONS_REQUESTED_KIND } from '../../types.js'; | ||
export const FETCH_OPTIONS_PARAM_NAME = 'fetch-options-from-parent'; | ||
export const INSTANCE_ID_PARAM_NAME = 'iframe-instance-id'; | ||
@@ -73,2 +75,14 @@ export async function iframeInitializer({ communication, env, ...initializerOptions }) { | ||
} | ||
export function installRunOptionsInitMessageHandler(target, getRunOptionsParams) { | ||
function listenForRunOptionsRequest(evt) { | ||
if ('kind' in evt.data && evt.data.kind === RUN_OPTIONS_REQUESTED_KIND && evt.source === target) { | ||
evt.source.postMessage({ | ||
kind: RUN_OPTIONS_PROVIDED_KIND, | ||
runOptionsParams: getRunOptionsParams().toString(), | ||
}, evt.origin); | ||
} | ||
} | ||
window.addEventListener('message', listenForRunOptionsRequest); | ||
return () => window.removeEventListener('message', listenForRunOptionsRequest); | ||
} | ||
const defaultHtmlSourceFactory = (envName, publicPath = '', hashParams, origin = '') => { | ||
@@ -75,0 +89,0 @@ return `${origin}${publicPath}${envName}.html${location.search}${hashParams ?? ''}`; |
@@ -1,2 +0,2 @@ | ||
import type { IRunOptions, TopLevelConfig } from './types.js'; | ||
import { type IRunOptions, type TopLevelConfig } from './types.js'; | ||
import type { AnyEnvironment } from './entities/index.js'; | ||
@@ -22,3 +22,3 @@ import { IFeatureLoader } from './run-engine-app.js'; | ||
*/ | ||
export declare function main({ env, contextualConfig, publicConfigsRoute, featureLoaders, configLoaders, featureName, configName, options, }: MainEntryParams): Promise<RuntimeEngine<AnyEnvironment>>; | ||
export declare function main({ env, contextualConfig, publicConfigsRoute, featureLoaders, configLoaders, featureName, configName, options: providedOptions, }: MainEntryParams): Promise<RuntimeEngine<AnyEnvironment>>; | ||
//# sourceMappingURL=runtime-main.d.ts.map |
@@ -0,5 +1,6 @@ | ||
import { RUN_OPTIONS_PROVIDED_KIND, RUN_OPTIONS_REQUESTED_KIND, } from './types.js'; | ||
import { FeatureLoadersRegistry } from './run-engine-app.js'; | ||
import { RuntimeConfigurations } from './runtime-configurations.js'; | ||
import { RuntimeEngine } from './runtime-engine.js'; | ||
import { INSTANCE_ID_PARAM_NAME } from './com/index.js'; | ||
import { FETCH_OPTIONS_PARAM_NAME, INSTANCE_ID_PARAM_NAME } from './com/index.js'; | ||
/** | ||
@@ -9,3 +10,5 @@ * main engine environment entry point flow | ||
*/ | ||
export async function main({ env, contextualConfig, publicConfigsRoute, featureLoaders, configLoaders, featureName, configName, options, }) { | ||
export async function main({ env, contextualConfig, publicConfigsRoute, featureLoaders, configLoaders, featureName, configName, options: providedOptions, }) { | ||
const fetchOptionsFromParent = providedOptions.get(FETCH_OPTIONS_PARAM_NAME) === 'true'; | ||
const options = fetchOptionsFromParent ? await getRunningOptionsFromParent(providedOptions) : providedOptions; | ||
const runtimeConfiguration = new RuntimeConfigurations(env.env, publicConfigsRoute, configLoaders, options); | ||
@@ -28,2 +31,31 @@ const featureLoader = new FeatureLoadersRegistry(featureLoaders); | ||
} | ||
/** | ||
* Request run options from parent window and combine with current options. | ||
* @param options current options to be overridden by options from parent | ||
* @returns promise with combined options | ||
*/ | ||
async function getRunningOptionsFromParent(options) { | ||
if (window.parent === window) { | ||
// if there is no parent window then we assume intentional usage of the env | ||
return options; | ||
} | ||
return await new Promise((resolve) => { | ||
function listenForEnvId(evt) { | ||
if ('kind' in evt.data && evt.data.kind === RUN_OPTIONS_PROVIDED_KIND) { | ||
window.removeEventListener('message', listenForEnvId); | ||
const paramsFromParent = new URLSearchParams(evt.data.runOptionsParams); | ||
for (const [key, value] of options) { | ||
if (!paramsFromParent.has(key) && value) { | ||
paramsFromParent.set(key, value.toString()); | ||
} | ||
} | ||
resolve(paramsFromParent); | ||
} | ||
} | ||
window.addEventListener('message', listenForEnvId); | ||
window.parent.postMessage({ | ||
kind: RUN_OPTIONS_REQUESTED_KIND, | ||
}, '*'); | ||
}); | ||
} | ||
//# sourceMappingURL=runtime-main.js.map |
@@ -124,3 +124,5 @@ import type { LogMessage } from './common-types.js'; | ||
} | ||
export declare const RUN_OPTIONS_REQUESTED_KIND = "engine-run-options-requested"; | ||
export declare const RUN_OPTIONS_PROVIDED_KIND = "engine-run-options-provided"; | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
@@ -9,2 +9,4 @@ import { CONFIGURABLE, CREATE_RUNTIME, IDENTIFY_API, REGISTER_VALUE } from './symbols.js'; | ||
})(LogLevel || (LogLevel = {})); | ||
export const RUN_OPTIONS_REQUESTED_KIND = 'engine-run-options-requested'; | ||
export const RUN_OPTIONS_PROVIDED_KIND = 'engine-run-options-provided'; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "@wixc3/engine-core", | ||
"version": "49.1.1", | ||
"version": "49.2.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import type { Communication } from '../communication.js'; | ||
import type { InitializerOptions } from './types.js'; | ||
import { WindowInitializerService } from '../window-initializer-service.js'; | ||
import { RUN_OPTIONS_PROVIDED_KIND, RUN_OPTIONS_REQUESTED_KIND } from '../../types.js'; | ||
export const FETCH_OPTIONS_PARAM_NAME = 'fetch-options-from-parent'; | ||
export const INSTANCE_ID_PARAM_NAME = 'iframe-instance-id'; | ||
@@ -132,4 +134,22 @@ export interface IIframeInitializerOptions { | ||
export function installRunOptionsInitMessageHandler(target: Window, getRunOptionsParams: () => URLSearchParams) { | ||
function listenForRunOptionsRequest(evt: MessageEvent) { | ||
if ('kind' in evt.data && evt.data.kind === RUN_OPTIONS_REQUESTED_KIND && evt.source === target) { | ||
evt.source.postMessage( | ||
{ | ||
kind: RUN_OPTIONS_PROVIDED_KIND, | ||
runOptionsParams: getRunOptionsParams().toString(), | ||
}, | ||
evt.origin, | ||
); | ||
} | ||
} | ||
window.addEventListener('message', listenForRunOptionsRequest); | ||
return () => window.removeEventListener('message', listenForRunOptionsRequest); | ||
} | ||
const defaultHtmlSourceFactory = (envName: string, publicPath = '', hashParams?: string, origin = '') => { | ||
return `${origin}${publicPath}${envName}.html${location.search}${hashParams ?? ''}`; | ||
}; |
@@ -1,2 +0,7 @@ | ||
import type { IRunOptions, TopLevelConfig } from './types.js'; | ||
import { | ||
RUN_OPTIONS_PROVIDED_KIND, | ||
RUN_OPTIONS_REQUESTED_KIND, | ||
type IRunOptions, | ||
type TopLevelConfig, | ||
} from './types.js'; | ||
import type { AnyEnvironment } from './entities/index.js'; | ||
@@ -6,3 +11,3 @@ import { FeatureLoadersRegistry, IFeatureLoader } from './run-engine-app.js'; | ||
import { RuntimeEngine } from './runtime-engine.js'; | ||
import { INSTANCE_ID_PARAM_NAME } from './com/index.js'; | ||
import { FETCH_OPTIONS_PARAM_NAME, INSTANCE_ID_PARAM_NAME } from './com/index.js'; | ||
@@ -32,4 +37,6 @@ export interface MainEntryParams { | ||
configName, | ||
options, | ||
options: providedOptions, | ||
}: MainEntryParams) { | ||
const fetchOptionsFromParent = providedOptions.get(FETCH_OPTIONS_PARAM_NAME) === 'true'; | ||
const options = fetchOptionsFromParent ? await getRunningOptionsFromParent(providedOptions) : providedOptions; | ||
const runtimeConfiguration = new RuntimeConfigurations(env.env, publicConfigsRoute, configLoaders, options); | ||
@@ -62,1 +69,36 @@ const featureLoader = new FeatureLoadersRegistry(featureLoaders); | ||
} | ||
/** | ||
* Request run options from parent window and combine with current options. | ||
* @param options current options to be overridden by options from parent | ||
* @returns promise with combined options | ||
*/ | ||
async function getRunningOptionsFromParent(options: IRunOptions) { | ||
if (window.parent === window) { | ||
// if there is no parent window then we assume intentional usage of the env | ||
return options; | ||
} | ||
return await new Promise<IRunOptions>((resolve) => { | ||
function listenForEnvId(evt: MessageEvent) { | ||
if ('kind' in evt.data && evt.data.kind === RUN_OPTIONS_PROVIDED_KIND) { | ||
window.removeEventListener('message', listenForEnvId); | ||
const paramsFromParent = new URLSearchParams(evt.data.runOptionsParams); | ||
for (const [key, value] of options) { | ||
if (!paramsFromParent.has(key) && value) { | ||
paramsFromParent.set(key, value.toString()); | ||
} | ||
} | ||
resolve(paramsFromParent); | ||
} | ||
} | ||
window.addEventListener('message', listenForEnvId); | ||
window.parent.postMessage( | ||
{ | ||
kind: RUN_OPTIONS_REQUESTED_KIND, | ||
}, | ||
'*', | ||
); | ||
}); | ||
} |
@@ -224,1 +224,4 @@ import type { LogMessage } from './common-types.js'; | ||
} | ||
export const RUN_OPTIONS_REQUESTED_KIND = 'engine-run-options-requested'; | ||
export const RUN_OPTIONS_PROVIDED_KIND = 'engine-run-options-provided'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
460126
7673