46 lines
2.8 KiB
TypeScript
46 lines
2.8 KiB
TypeScript
import {catalogByType} from './widgets/catalog';
|
|
import type {HmiWidget} from './types';
|
|
|
|
export interface WidgetPortablePackage {
|
|
format:'widgetworkbench.widget'; version:1;
|
|
exportedAt:string; widget:HmiWidget;
|
|
image?:{name:string;mime:string;base64:string};
|
|
}
|
|
const safeKey=/^[a-zA-Z0-9_.-]{1,120}$/;
|
|
const colorsafe=/^image\/(png|jpeg|webp)$/;
|
|
|
|
export function parseWidgetPackage(raw:string):WidgetPortablePackage {
|
|
if(raw.length>4_000_000)throw Error('Widget-Datei ist zu groß (max. 4 MB)');
|
|
const parsed:unknown=JSON.parse(raw);
|
|
if(!parsed || typeof parsed!=='object')throw Error('Kein Widget-Paket');
|
|
const pkg=parsed as Partial<WidgetPortablePackage>;
|
|
if(pkg.format!=='widgetworkbench.widget'||pkg.version!==1)throw Error('Unbekanntes Widget-Paketformat');
|
|
const w=pkg.widget as HmiWidget|undefined;
|
|
if(!w||typeof w!=='object'||!Object.prototype.hasOwnProperty.call(catalogByType,w.type))throw Error('Unbekannter Widget-Typ');
|
|
if(typeof w.id!=='string'||!safeKey.test(w.id))throw Error('Ungültige Widget-ID');
|
|
if(!Number.isInteger(w.x)||w.x<0||w.x>11||!Number.isInteger(w.y)||w.y<0||w.y>200||
|
|
!Number.isInteger(w.w)||w.w<1||w.w>12||!Number.isInteger(w.h)||w.h<1||w.h>20||w.x+w.w>12)
|
|
throw Error('Ungültige Widget-Position oder Größe');
|
|
if(!w.props||typeof w.props!=='object'||Array.isArray(w.props)||JSON.stringify(w.props).length>16000)
|
|
throw Error('Ungültige Widget-Konfiguration');
|
|
if(!Array.isArray(w.visibleToRoles)||w.visibleToRoles.length>30||w.visibleToRoles.some(r=>typeof r!=='string'||!safeKey.test(r)))
|
|
throw Error('Ungültige Rollenzuordnung');
|
|
if(typeof w.mandatory!=='boolean'||typeof w.personalizable!=='boolean')throw Error('Widget-Flags fehlen');
|
|
if(w.mandatory && w.visibleToRoles.length)throw Error('Pflichtwidget darf keine Sichtbarkeitsrollen haben');
|
|
if(w.type==='ui.cheerpj-workplace' && (w.props.jnlpUrl!=='/backend/api/hmi/v1/cheerpj/workplace/manifest.jnlp'||Number(w.props.javaVersion)!==11))
|
|
throw Error('Ungültige iTAC Workplace-JNLP-Konfiguration');
|
|
if(w.type==='ui.script' && (typeof w.props.script!=='string'||w.props.script.length>8000))
|
|
throw Error('Ungültiger JavaScript-Code');
|
|
if(pkg.image){
|
|
const asset=pkg.image;
|
|
if(typeof asset.name!=='string'||asset.name.length>255||typeof asset.mime!=='string'||!colorsafe.test(asset.mime)||
|
|
typeof asset.base64!=='string'||asset.base64.length>2_850_000||!/^[A-Za-z0-9+/]*={0,2}$/.test(asset.base64))
|
|
throw Error('Ungültiges eingebettetes Bild');
|
|
if(w.type!=='ui.image')throw Error('Bilder dürfen nur im Bildwidget vorkommen');
|
|
}
|
|
return pkg as WidgetPortablePackage;
|
|
}
|
|
export function makeExportPackage(widget:HmiWidget):WidgetPortablePackage {
|
|
return {format:'widgetworkbench.widget',version:1,exportedAt:new Date().toISOString(),widget:JSON.parse(JSON.stringify(widget))};
|
|
}
|