Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Main] Fix extension / channel not being readonly #2018

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions channels/applicationinsights-channel-js/src/Sender.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import dynamicProto from "@microsoft/dynamicproto-js";
import {
BreezeChannelIdentifier, DEFAULT_BREEZE_ENDPOINT, DEFAULT_BREEZE_PATH, DisabledPropertyName, Event, Exception, IChannelControlsAI,
IConfig, IEnvelope, ISample, Metric, PageView, PageViewPerformance, ProcessLegacy, RemoteDependencyData, RequestHeaders, SampleRate,
Trace, eRequestHeaders, isInternalApplicationInsightsEndpoint, utlCanUseSessionStorage
BreezeChannelIdentifier, DEFAULT_BREEZE_ENDPOINT, DEFAULT_BREEZE_PATH, DisabledPropertyName, Event, Exception, IConfig, IEnvelope,
ISample, Metric, PageView, PageViewPerformance, ProcessLegacy, RemoteDependencyData, RequestHeaders, SampleRate, Trace, eRequestHeaders,
isInternalApplicationInsightsEndpoint, utlCanUseSessionStorage
} from "@microsoft/applicationinsights-common";
import {
BaseTelemetryPlugin, IAppInsightsCore, IConfigDefaults, IConfiguration, IDiagnosticLogger, INotificationManager, IPlugin,
IProcessTelemetryContext, IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryPluginChain, ITelemetryUnloadState,
BaseTelemetryPlugin, IAppInsightsCore, IChannelControls, IConfigDefaults, IConfiguration, IDiagnosticLogger, INotificationManager,
IPlugin, IProcessTelemetryContext, IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryPluginChain, ITelemetryUnloadState,
SendRequestReason, _eInternalMessageId, _throwInternal, _warnToConsole, arrForEach, cfgDfBoolean, cfgDfValidate,
createProcessTelemetryContext, createUniqueNamespace, dateNow, dumpObj, eLoggingSeverity, getExceptionName, getIEVersion, getJSON,
getNavigator, getWindow, isArray, isBeaconsSupported, isFetchSupported, isNullOrUndefined, isXhrSupported, mergeEvtNamespace, objExtend,
objKeys, onConfigChange, useXDomainRequest
} from "@microsoft/applicationinsights-core-js";
import { ITimerHandler, isTruthy, objDeepFreeze, objDefineProp, scheduleTimeout } from "@nevware21/ts-utils";
import { ITimerHandler, isTruthy, objDeepFreeze, objDefine, scheduleTimeout } from "@nevware21/ts-utils";
import {
DependencyEnvelopeCreator, EventEnvelopeCreator, ExceptionEnvelopeCreator, MetricEnvelopeCreator, PageViewEnvelopeCreator,
PageViewPerformanceEnvelopeCreator, TraceEnvelopeCreator
Expand Down Expand Up @@ -81,7 +81,7 @@ const EnvelopeTypeCreator: { [key:string] : EnvelopeCreator } = {
[RemoteDependencyData.dataType]: DependencyEnvelopeCreator
};

export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
export class Sender extends BaseTelemetryPlugin implements IChannelControls {

public static constructEnvelope(orig: ITelemetryItem, iKey: string, logger: IDiagnosticLogger, convertUndefined?: any): IEnvelope {
let envelope: ITelemetryItem;
Expand Down Expand Up @@ -235,10 +235,8 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
let ctx = createProcessTelemetryContext(null, config, core);
let senderConfig = ctx.getExtCfg(identifier, defaultAppInsightsChannelConfig);

objDefineProp(_self, "_senderConfig", {
enumerable: true,
configurable: true,
get: function() {
objDefine(_self, "_senderConfig", {
g: function() {
return senderConfig;
}
});
Expand Down Expand Up @@ -1152,10 +1150,8 @@ export class Sender extends BaseTelemetryPlugin implements IChannelControlsAI {
_sessionStorageUsed = null;
_namePrefix = UNDEFINED_VALUE;

objDefineProp(_self, "_senderConfig", {
enumerable: true,
configurable: true,
get: function() {
objDefine(_self, "_senderConfig", {
g: function() {
return objExtend({}, defaultAppInsightsChannelConfig);
}
});
Expand Down
21 changes: 16 additions & 5 deletions channels/tee-channel-js/src/TeeChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// Licensed under the MIT License.

import dynamicProto from "@microsoft/dynamicproto-js";
import { IChannelControlsAI, IConfig } from "@microsoft/applicationinsights-common";
import { IConfig } from "@microsoft/applicationinsights-common";
import {
BaseTelemetryPlugin, IAppInsightsCore, IChannelControls, IConfigDefaults, IConfiguration, IPlugin, IProcessTelemetryContext,
IProcessTelemetryUnloadContext, IProcessTelemetryUpdateContext, ITelemetryItem, ITelemetryPluginChain, ITelemetryUnloadState,
ITelemetryUpdateState, cfgDfBoolean, createProcessTelemetryContext, initializePlugins, onConfigChange, proxyFunctions
BaseTelemetryPlugin, IAppInsightsCore, IChannelControls, IChannelControlsHost, IConfigDefaults, IConfiguration, IPlugin,
IProcessTelemetryContext, IProcessTelemetryUnloadContext, IProcessTelemetryUpdateContext, ITelemetryItem, ITelemetryPluginChain,
ITelemetryUnloadState, ITelemetryUpdateState, cfgDfBoolean, createProcessTelemetryContext, initializePlugins, onConfigChange,
proxyFunctions
} from "@microsoft/applicationinsights-core-js";
import { arrForEach, isArray, objDeepFreeze, objFreeze, throwError } from "@nevware21/ts-utils";
import { ChannelControllerPriority, IChannelController, _IInternalChannels, createChannelControllerPlugin } from "./ChannelController";
Expand Down Expand Up @@ -64,7 +65,7 @@ function _createChannelQueues(config: ITeeChannelConfig, core: IAppInsightsCore,
return channelQueue;
}

export class TeeChannel extends BaseTelemetryPlugin implements IChannelControlsAI {
export class TeeChannel extends BaseTelemetryPlugin implements IChannelControlsHost {
public readonly identifier: string = "TeeChannelController";
public readonly priority: number = 999;

Expand Down Expand Up @@ -215,4 +216,14 @@ export class TeeChannel extends BaseTelemetryPlugin implements IChannelControlsA
public onunloadFlush() {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}

/**
* Get and return the named channel instance (if present) from the queues
* @param pluginIdentifier - The identifier name of the plugin
*/
public getChannel<T extends IPlugin = IPlugin>(pluginIdentifier: string): T {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
}

}
4 changes: 2 additions & 2 deletions examples/shared-worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"mintest": "",
"perftest": "",
"lint": "tslint -p tsconfig.json",
"ai-min": "grunt example-shared-worker-min",
"ai-restore": "grunt example-shared-worker-restore"
"ai-min": "",
"ai-restore": ""
},
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ import {
ITelemetryPluginChain, ITelemetryUnloadState, InstrumentEvent, TelemetryInitializerFunction, _eInternalMessageId, arrForEach,
cfgDfBoolean, cfgDfSet, cfgDfString, cfgDfValidate, createProcessTelemetryContext, createUniqueNamespace, dumpObj, eLoggingSeverity,
eventOff, eventOn, generateW3CId, getDocument, getExceptionName, getHistory, getLocation, getWindow, hasHistory, hasWindow, isFunction,
isNullOrUndefined, isString, isUndefined, mergeEvtNamespace, objDefineAccessors, onConfigChange, safeGetCookieMgr, strUndefined,
throwError
isNullOrUndefined, isString, isUndefined, mergeEvtNamespace, onConfigChange, safeGetCookieMgr, strUndefined, throwError
} from "@microsoft/applicationinsights-core-js";
import { PropertiesPlugin } from "@microsoft/applicationinsights-properties-js";
import { isError, objDeepFreeze, objDefineProp, scheduleTimeout, strIndexOf } from "@nevware21/ts-utils";
import { isError, objDeepFreeze, objDefine, scheduleTimeout, strIndexOf } from "@nevware21/ts-utils";
import { IAppInsightsInternal, PageViewManager } from "./Telemetry/PageViewManager";
import { PageViewPerformanceManager } from "./Telemetry/PageViewPerformanceManager";
import { PageVisitTimeManager } from "./Telemetry/PageVisitTimeManager";
Expand Down Expand Up @@ -890,18 +889,16 @@ export class AnalyticsPlugin extends BaseTelemetryPlugin implements IAppInsights
_extConfig = null;

// Define _self.config
objDefineProp(_self, "config", {
configurable: true,
enumerable: true,
get: () => _extConfig
objDefine(_self, "config", {
g: () => _extConfig
});
}

// For backward compatibility
objDefineAccessors(_self, "_pageViewManager", () => _pageViewManager);
objDefineAccessors(_self, "_pageViewPerformanceManager", () => _pageViewPerformanceManager);
objDefineAccessors(_self, "_pageVisitTimeManager", () => _pageVisitTimeManager);
objDefineAccessors(_self, "_evtNamespace", () => "." + _evtNamespace);
objDefine<any>(_self, "_pageViewManager", { g: () => _pageViewManager });
objDefine<any>(_self, "_pageViewPerformanceManager", { g: () => _pageViewPerformanceManager });
objDefine<any>(_self, "_pageVisitTimeManager", { g: () => _pageVisitTimeManager });
objDefine<any>(_self, "_evtNamespace", { g: () => "." + _evtNamespace });
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
utlCanUseSessionStorage, utlGetSessionStorage, utlRemoveSessionStorage, utlSetSessionStorage
} from "@microsoft/applicationinsights-common";
import {
IDiagnosticLogger, _warnToConsole, dateNow, dumpObj, getJSON, hasJSON, objDefineAccessors, throwError
IDiagnosticLogger, _warnToConsole, dateNow, dumpObj, getJSON, hasJSON, throwError
} from "@microsoft/applicationinsights-core-js";
import { objDefine } from "@nevware21/ts-utils";

/**
* Used to track page visit durations
Expand Down Expand Up @@ -47,38 +48,23 @@ export class PageVisitTimeManager {
let prevPageVisitData: PageVisitData = null;
try {
prevPageVisitData = stopPageVisitTimer();
startPageVisitTimer(pageName, pageUrl);

} catch (e) {
_warnToConsole(logger, "Call to restart failed: " + dumpObj(e));
prevPageVisitData = null;
}

return prevPageVisitData;
}

/**
* Starts timing visit duration of pageName
* @param pageName
* @returns {}
*/
function startPageVisitTimer(pageName: string, pageUrl: string) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removing and collapsing a function that is only used once

try {
if (utlCanUseSessionStorage()) {
if (utlGetSessionStorage(logger, prevPageVisitDataKeyName) != null) {
throwError("Cannot call startPageVisit consecutively without first calling stopPageVisit");
}

const currPageVisitData = new PageVisitData(pageName, pageUrl);
const currPageVisitDataStr = getJSON().stringify(currPageVisitData);
const currPageVisitDataStr = getJSON().stringify(new PageVisitData(pageName, pageUrl));
utlSetSessionStorage(logger, prevPageVisitDataKeyName, currPageVisitDataStr);
}

} catch (e) {
// TODO: Remove this catch in next phase, since if start is called twice in a row the exception needs to be propagated out
_warnToConsole(logger, "Call to start failed: " + dumpObj(e));
_warnToConsole(logger, "Call to restart failed: " + dumpObj(e));
prevPageVisitData = null;
}

return prevPageVisitData;
}

/**
* Stops timing of current page, if exists.
* @returns {PageVisitData} Page visit data (including duration) of pageName from call to start, if exists. Null if not.
Expand Down Expand Up @@ -112,8 +98,8 @@ export class PageVisitTimeManager {
}

// For backward compatibility
objDefineAccessors(_self, "_logger", () => logger);
objDefineAccessors(_self, "pageVisitTimeTrackingHandler", () => pageVisitTimeTrackingHandler);
objDefine<any>(_self, "_logger", { g: () => logger });
objDefine<any>(_self, "pageVisitTimeTrackingHandler", { g: () => pageVisitTimeTrackingHandler});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
getExceptionName, isNullOrUndefined, onConfigChange, throwError, unloadComponents
} from "@microsoft/applicationinsights-core-js";
import { PropertiesPlugin } from "@microsoft/applicationinsights-properties-js";
import { hasDocument, objDeepFreeze, objDefineProp } from "@nevware21/ts-utils";
import { hasDocument, objDeepFreeze } from "@nevware21/ts-utils";
import {
IAutoCaptureHandler, IClickAnalyticsConfiguration, IContentHandler, ICoreData, ICustomDataTags, IPageActionTelemetry, IValueCallback
} from "./Interfaces/Datamodel";
Expand Down Expand Up @@ -153,22 +153,15 @@ export class ClickAnalyticsPlugin extends BaseTelemetryPlugin {
_autoCapture = autoCapture;
}));
}
});

function _initDefaults() {
_config = null;
_pageAction = null;
_autoCaptureHandler = null;
_contentHandler = null;
_autoCapture = false;

// Define _self.config
objDefineProp(self, "config", {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually adding a config property to window...
Note: the self is missing the _ prefix and even if it had that it's outside of the closure that defines _self...
So just removing as it's not used (or valid) to populate window.config

configurable: true,
enumerable: true,
get: () => _config
});
}
function _initDefaults() {
_config = null;
_pageAction = null;
_autoCaptureHandler = null;
_contentHandler = null;
_autoCapture = false;
}
});
}

public initialize(config: IConfiguration & IConfig, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
IAppInsightsCore, ICookieMgr, IUnloadHookContainer, _eInternalMessageId, _throwInternal, eLoggingSeverity, newId, onConfigChange,
safeGetCookieMgr, safeGetLogger, toISOString
} from "@microsoft/applicationinsights-core-js";
import { objDefineProp } from "@nevware21/ts-utils";
import { objDefine } from "@nevware21/ts-utils";
import { IPropertiesConfig } from "../Interfaces/IPropertiesConfig";

function _validateUserInput(id: string): boolean {
Expand Down Expand Up @@ -76,10 +76,8 @@ export class User implements IUserContext {

dynamicProto(User, this, (_self) => {
// Define _self.config
objDefineProp(_self, "config", {
configurable: true,
enumerable: true,
get: () => config
objDefine(_self, "config", {
g: () => config
});

let unloadHook = onConfigChange(config, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
IProcessTelemetryUnloadContext, ITelemetryItem, ITelemetryPluginChain, ITelemetryUnloadState, _InternalLogMessage, _eInternalMessageId,
_logInternalMessage, createProcessTelemetryContext, eLoggingSeverity, getNavigator, getSetValue, isNullOrUndefined, onConfigChange
} from "@microsoft/applicationinsights-core-js";
import { objDeepFreeze, objDefineProp } from "@nevware21/ts-utils";
import { objDeepFreeze, objDefine } from "@nevware21/ts-utils";
import { IPropTelemetryContext } from "./Interfaces/IPropTelemetryContext";
import { IPropertiesConfig } from "./Interfaces/IPropertiesConfig";
import { TelemetryContext } from "./TelemetryContext";
Expand Down Expand Up @@ -56,10 +56,8 @@ export default class PropertiesPlugin extends BaseTelemetryPlugin implements IPr

_initDefaults();

objDefineProp(_self, "context", {
enumerable: true,
configurable: true,
get: function() {
objDefine(_self, "context", {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using objDefine() as the defaults for enumerable and configurable are true and it avoids adding these uncompressible names

g: function() {
return _context;
}
});
Expand Down
7 changes: 0 additions & 7 deletions shared/AppInsightsCommon/src/Interfaces/IChannelControlsAI.ts

This file was deleted.

1 change: 0 additions & 1 deletion shared/AppInsightsCommon/src/applicationinsights-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export { PageViewPerformance } from "./Telemetry/PageViewPerformance";
export { Data } from "./Telemetry/Common/Data";
export { eSeverityLevel, SeverityLevel } from "./Interfaces/Contracts/SeverityLevel";
export { IConfig, ConfigurationManager } from "./Interfaces/IConfig";
export { IChannelControlsAI } from "./Interfaces/IChannelControlsAI";
export { IContextTagKeys, ContextTagKeys } from "./Interfaces/Contracts/ContextTagKeys";
export {
DataSanitizerValues,
Expand Down
Loading