UNPKG

@wayrisignals/kicad-webview

Version:

Interactive KiCad schematic, PCB and 3D viewer for the web, with Gerber, Excellon, linked views and offline HTML exports

3 lines (2 loc) • 3.6 kB
/*! KiCad WebView 1.2.1 | Copyright (c) 2026 wayri | SPDX-License-Identifier: MPL-2.0 | Source: https://github.com/wayri/kicad-webview/tree/v1.2.1 | Keep LICENSE and THIRD-PARTY-NOTICES.md; bundled dependencies retain their licenses. */ import{d as f}from"./chunk-ISOTB6CX.js";var s=(e,n,r=240)=>{if(typeof e!="string"||!e.trim()||e.length>r||/[\u0000-\u001f\u007f]/.test(e))throw new TypeError("Invalid analysis "+n+".");return e},o=(e,n,r=1e9)=>{if(typeof e!="number"||!Number.isFinite(e)||Math.abs(e)>r)throw new TypeError("Invalid analysis "+n+".");return e};function A(e){if(typeof e=="string"){if(e.length>8*1024*1024||new TextEncoder().encode(e).length>8*1024*1024)throw new RangeError("Analysis JSON exceeds 8 MiB.");e=JSON.parse(e)}if(!e||e.schema!=="kicad-webview.analysis/v1"||e.coordinateSystem!=="kicad-mm")throw new TypeError("Expected analysis/v1 in saved KiCad millimetre coordinates.");if(!/^[a-f0-9]{64}$/.test(e.board?.sha256||""))throw new TypeError("Analysis requires the exact saved PCB SHA-256.");let n={sha256:e.board.sha256};e.board.path!==void 0&&(n.path=s(e.board.path,"board path",1e3));let r=e.provenance;if(!r||!["computed","measured","illustrative"].includes(r.kind))throw new TypeError("Choose computed, measured or illustrative provenance.");if(!Array.isArray(r.assumptions)||r.assumptions.length>24)throw new TypeError("Supply bounded analysis assumptions (an empty array is allowed).");let a={kind:r.kind,tool:s(r.tool,"tool"),runId:s(r.runId,"run ID"),assumptions:r.assumptions.map(t=>s(t,"assumption",500))};if(r.generatedAt!==void 0){if(typeof r.generatedAt!="string"||r.generatedAt.length>40||!Number.isFinite(Date.parse(r.generatedAt)))throw new TypeError("Invalid analysis timestamp.");a.generatedAt=r.generatedAt}if(!Array.isArray(e.fields)||!e.fields.length||e.fields.length>8)throw new RangeError("Supply 1\u20138 analysis fields.");let c=new Set,p=0,w=e.fields.map(t=>{let l=s(t?.id,"field ID",80);if(c.has(l))throw new TypeError("Duplicate analysis field ID.");if(c.add(l),!Array.isArray(t.range)||t.range.length!==2)throw new TypeError("Supply a fixed scalar range.");let d=t.range.map(i=>o(i,"range"));if(d[1]<=d[0])throw new RangeError("Analysis range must increase.");if(!Array.isArray(t.samples)||!t.samples.length||(p+=t.samples.length)>1e4)throw new RangeError("Analysis is limited to 10,000 cells in total.");let m={id:l,name:s(t.name,"field name"),unit:s(t.unit,"unit",40),range:d,samples:t.samples.map(i=>{let y={x:o(i?.x,"x",1e6),y:o(i?.y,"y",1e6),width:o(i?.width,"width",1e6),height:o(i?.height,"height",1e6),value:o(i?.value,"value")};if(y.width<=0||y.height<=0)throw new RangeError("Analysis cells must have positive dimensions.");return y})};return t.layer!==void 0&&(m.layer=s(t.layer,"layer",80)),m}),g={schema:e.schema,coordinateSystem:e.coordinateSystem,board:n,provenance:a,fields:w},h=t=>{if(t&&typeof t=="object"){for(let l of Object.values(t))h(l);Object.freeze(t)}return t};return h(g)}async function b(e){if(typeof e!="string")throw new TypeError("Load a saved PCB source first.");if(!globalThis.crypto?.subtle)throw new Error("Analysis binding requires a secure context (HTTPS or localhost).");return[...new Uint8Array(await crypto.subtle.digest("SHA-256",new TextEncoder().encode(e)))].map(n=>n.toString(16).padStart(2,"0")).join("")}function x(e,n){return"rgb("+f(e,n).join(",")+")"}function E(e,n){for(let r=e.samples.length-1;r>=0;r--){let a=e.samples[r];if(n.x>=a.x&&n.x<=a.x+a.width&&n.y>=a.y&&n.y<=a.y+a.height)return a}return null}function S(e){return String(Number(e.toPrecision(6)))}export{A as a,b,x as c,E as d,S as e};