Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Validate the briefcase
let isValidBriefcase: boolean = true;
if (briefcase.currentChangeSetId !== briefcase.targetChangeSetId) {
if (openParams.syncMode === SyncMode.FixedVersion) {
Logger.logError(loggerCategory, "Briefcase found is invalid (is not of required version). Deleting it to allow retries", () => briefcase.getDebugInfo());
isValidBriefcase = false;
} else {
if (nativeDb.hasUnsavedChanges() || nativeDb.hasSavedChanges())
Logger.logWarning(loggerCategory, "Briefcase found with local changes, but is not of required version. Ignoring required version", () => briefcase.getDebugInfo());
else
isValidBriefcase = false; // Invalidate the briefcase to refetch it
}
}
if (briefcase.briefcaseId !== briefcaseId) {
Logger.logError(loggerCategory, "Briefcase found is invalid (does not have the expected briefcase id). Deleting it to allow retries", () => ({ expectedBriefcaseId: briefcaseId, ...briefcase.getDebugInfo() }));
isValidBriefcase = false;
}
if (!isValidBriefcase) {
BriefcaseManager.closeBriefcase(briefcase, false);
BriefcaseManager.deleteBriefcaseFromLocalDisk(briefcase);
return undefined;
}
const cachedBriefcase = this._cache.findBriefcase(briefcase);
if (cachedBriefcase) {
// TODO: Turn this into an assertion, after ensuring this doesn't happen in deployments
Logger.logError(loggerCategory, "Attempting to open and/or create briefcase twice", () => briefcase.getDebugInfo());
return cachedBriefcase;
}
BriefcaseManager._cache.addBriefcase(briefcase);
briefcase.isPending = this.finishOpenBriefcase(requestContext, briefcase);
}
// is the source view too wide? If so, we need to scale the image down.
if (canvas.width > result.maxWidth) {
// yes, we have to scale it down, create a new canvas and set the new canvas' size
const newCanvas = document.createElement("canvas");
newCanvas.width = result.maxWidth;
newCanvas.height = canvas.height * (result.maxWidth / canvas.width);
newCanvas.getContext("2d")!.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, newCanvas.width, newCanvas.height);
canvas = newCanvas; // return the image from adjusted canvas, not view canvas.
}
// return the markup data to be saved by the application.
image = (!result.imageFormat ? undefined : canvas.toDataURL(result.imageFormat));
} catch (e) {
Logger.logError(FrontendLoggerCategory.Package + ".markup", "Error creating image from svg", () => ({ message: e.message }));
}
return { rect: { width: canvas.width, height: canvas.height }, svg, image };
}
private _setEnabled = async (item: ListItem, _enabled: boolean) => {
const activeContentControl = ContentViewManager.getActiveContentControl() as unknown as SupportsViewSelectorChange;
if (!activeContentControl || !activeContentControl.supportsViewSelectorChange) {
Logger.logError(UiFramework.loggerCategory(this), `No active ContentControl for ViewSelector change`);
return;
}
// Enable the item temporarily to let user see that their click was registered
// while we query for view state and change the current view which may take a bit
if (_enabled && item.type !== ListItemType.Container) {
// This itemMapper simply looks through all the list items and their nested children and enables the one
// that we have registered to enable
// Also disable all other items
let itemMapper: (tempItem: ListItem) => ListItem;
itemMapper = (tempItem: ListItem) => {
if (tempItem.type === ListItemType.Container) {
return { ...tempItem, children: tempItem.children!.map(itemMapper) };
} else if (tempItem.key === item.key) {
return { ...tempItem, enabled: true };
} else {
}
}
if (briefcase.briefcaseId !== briefcaseId) {
Logger.logError(loggerCategory, "Briefcase found is invalid (does not have the expected briefcase id). Deleting it to allow retries", () => ({ expectedBriefcaseId: briefcaseId, ...briefcase.getDebugInfo() }));
isValidBriefcase = false;
}
if (!isValidBriefcase) {
BriefcaseManager.closeBriefcase(briefcase, false);
BriefcaseManager.deleteBriefcaseFromLocalDisk(briefcase);
return undefined;
}
const cachedBriefcase = this._cache.findBriefcase(briefcase);
if (cachedBriefcase) {
// TODO: Turn this into an assertion, after ensuring this doesn't happen in deployments
Logger.logError(loggerCategory, "Attempting to open and/or create briefcase twice", () => briefcase.getDebugInfo());
return cachedBriefcase;
}
BriefcaseManager._cache.addBriefcase(briefcase);
briefcase.isPending = this.finishOpenBriefcase(requestContext, briefcase);
return briefcase;
}
public memoize = (...args: any[]): QueryablePromise => {
const key: string = this._generateKeyFn(...args);
let qp: QueryablePromise | undefined = this._cachedPromises.get(key);
if (qp)
return qp;
if (this._cachedPromises.size >= this._maxCacheSize) {
this._purgeResolvedEntries();
if (this._cachedPromises.size >= this._maxCacheSize) {
Logger.logError(BackendLoggerCategory.PromiseMemoizer, "Cleared too many unresolved entries in memoizer cache");
this.clearCache();
}
}
const p = this._memoizeFn(...args);
qp = new QueryablePromise(p);
this._cachedPromises.set(key, qp);
return qp;
}
public static fromJson(typedConstructor: new () => T, applicationKey: string, ecJsonInstance: any): T | undefined {
const mappedClassEntry: ClassEntry | undefined = ECJsonTypeMap.getClassByType(typedConstructor);
if (!mappedClassEntry) {
Logger.logError(loggerCategory, `Type ${typedConstructor.name} is not mapped to an ECClass. Supply the appropriate class decorator`);
return undefined;
}
const lowCaseApplicationKey = applicationKey.toLowerCase();
const mappedApplicationEntry: ApplicationEntry | undefined = mappedClassEntry.getApplicationByKey(lowCaseApplicationKey);
if (!mappedApplicationEntry) {
Logger.logError(loggerCategory, `Type ${typedConstructor.name} is not mapped for the supplied application.`);
return undefined;
}
let ecJsonClassKey: string;
if (mappedApplicationEntry.classKeyMapInfo.classKeyPropertyName)
ecJsonClassKey = ecJsonInstance[mappedApplicationEntry.classKeyMapInfo.classKeyPropertyName];
else if (mappedApplicationEntry.classKeyMapInfo.schemaPropertyName && mappedApplicationEntry.classKeyMapInfo.classPropertyName)
ecJsonClassKey = ecJsonInstance[mappedApplicationEntry.classKeyMapInfo.schemaPropertyName] + "." + ecJsonInstance[mappedApplicationEntry.classKeyMapInfo.classPropertyName];
else {
assert(false, "Unexpected classKeyMapInfo");
return undefined;
}
if (mappedApplicationEntry.classKey !== ecJsonClassKey) {
Logger.logError(loggerCategory, `The ClassKey ${mappedApplicationEntry.classKey} was specified to map with ${typedConstructor.name}, but does not match that specified in the JSON: ${ecJsonClassKey} `);
return undefined;
const curPath = path.join(folderPath, file);
if (IModelJsFs.lstatSync(curPath)!.isDirectory) {
BriefcaseManager.deleteFolderRecursive(curPath);
} else {
try {
IModelJsFs.unlinkSync(curPath);
} catch (error) {
Logger.logError(loggerCategory, `Cannot delete file ${curPath}`);
throw error;
}
}
}
try {
IModelJsFs.rmdirSync(folderPath);
} catch (error) {
Logger.logError(loggerCategory, `Cannot delete folder: ${folderPath}`);
throw error;
}
} catch (error) {
}
return !IModelJsFs.existsSync(folderPath);
}
public static registerEntityState(classFullName: string, classType: typeof EntityState) {
const lowerName = classFullName.toLowerCase();
if (this._entityClasses.has(lowerName)) {
const errMsg = "Class " + classFullName + " is already registered. Make sure static schemaName and className members are correct on class " + classType.name;
Logger.logError(FrontendLoggerCategory.IModelConnection, errMsg);
throw new Error(errMsg);
}
this._entityClasses.set(lowerName, classType);
}
public getControlFromElement(node: React.ReactNode): ContentControl | undefined {
if (this._contentSetMap.size === 0)
this.getContentNodes();
if (node && (node as React.ReactElement).key)
return this._contentSetMap.get((node as React.ReactElement).key as string);
Logger.logError(UiFramework.loggerCategory(this), `getControlFromElement: no control found for element`);
return undefined;
}