import fs from 'node:fs' import assert from 'node:assert/strict' // Offline migration input must be a captured current-published snapshot, not a draft. const [snapshotPath,outputPath]=process.argv.slice(2) if(!snapshotPath||!outputPath)throw new Error('Usage: node normalize_scanner_parameter_defaults.mjs ') const snapshot=JSON.parse(fs.readFileSync(snapshotPath,'utf8').replace(/^\uFEFF/,'')) assert(snapshot.current?.publishedVersionId&&snapshot.published?.flowData,'A current published version snapshot is required') assert.equal(snapshot.current.publishedVersionId,snapshot.published.id) const graph=JSON.parse(snapshot.published.flowData),before=structuredClone(graph) // Replace only the top-level value token. In particular, never parse/stringify // unrelated SDK integers or overwrite a saved brightness (including zero). function distanceBody(text,body){ const stringEnd=start=>{ for(let i=start+1;i{while(/\s/.test(text[start]||'')&&startstart&&/\s/.test(text[stop-1]))stop-- ranges.push([start,stop]) } i=end-1 }else if(text[i]==='{'||text[i]==='[')depth++ else if(text[i]==='}'||text[i]===']')depth-- } assert(ranges.length<=1,'Duplicate scanner pointDistance requires review') if(ranges.length){const [start,end]=ranges[0];return text.slice(0,start)+'0.7'+text.slice(end)} const end=text.lastIndexOf('}') return text.slice(0,end)+(Object.keys(body).length?',':'')+'\n "pointDistance": 0.7\n'+text.slice(end) } let nodes=0,changed=0 for(const node of graph.nodes){ if(node.properties?.action!=='SCANNER_PARAMETERS')continue nodes++ const parameter=node.properties.nodeParams?.find(field=>field.name==='requestJson') assert.equal(parameter?.type,'input',`Dynamic scanner request requires a separate review: ${node.id}`) assert(!node.properties.nodeParams?.find(field=>field.name==='requestFields')?.children?.some(field=>field.name==='pointDistance'),`Bound pointDistance requires a separate review: ${node.id}`) const body=JSON.parse(parameter.input) assert(body&&typeof body==='object'&&!Array.isArray(body)) if(body.pointDistance===0.7)continue parameter.input=distanceBody(parameter.input,body) const previous={...body},next={...JSON.parse(parameter.input)} delete previous.pointDistance;delete next.pointDistance assert.deepEqual(next,previous,'Only pointDistance may change inside scanner parameters') changed++ } assert(nodes>0,'No scanner parameter nodes') // No path, edge, resource assignment or other scanner setting may change. const verified=structuredClone(graph) for(let i=0;ifield.name==='requestJson') verified.nodes[i].properties.nodeParams.find(field=>field.name==='requestJson').input=old.input } assert.deepEqual(verified,before) fs.writeFileSync(outputPath,JSON.stringify(graph,null,2)) console.log(JSON.stringify({flowId:snapshot.current.id,nodes,changed,brightnessUnchanged:true,pointDistance:0.7,pathUnchanged:true}))