2026-11-27 11:08:06 +08:00
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 <snapshot.json> <output-flow.json>' )
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 )
2026-11-27 21:08:11 +08:00
// 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 < text . length ; i ++ ) {
if ( text [ i ] === '\\' ) i ++
else if ( text [ i ] === '"' ) return i + 1
}
throw new Error ( 'Unterminated scanner JSON string' )
}
const spaces = start => { while ( /\s/ . test ( text [ start ] || '' ) && start < text . length ) start ++ ; return start }
let depth = 0 ; const ranges = [ ]
for ( let i = 0 ; i < text . length ; i ++ ) {
if ( text [ i ] === '"' ) {
const end = stringEnd ( i ) , colon = spaces ( end )
if ( depth === 1 && text [ colon ] === ':' && JSON . parse ( text . slice ( i , end ) ) === 'pointDistance' ) {
const start = spaces ( colon + 1 ) ; let stop = start , nested = 0
for ( ; stop < text . length ; stop ++ ) {
if ( text [ stop ] === '"' ) { stop = stringEnd ( stop ) - 1 ; continue }
if ( text [ stop ] === '{' || text [ stop ] === '[' ) nested ++
else if ( text [ stop ] === '}' || text [ stop ] === ']' ) { if ( nested === 0 ) break ; nested -- }
else if ( text [ stop ] === ',' && nested === 0 ) break
}
while ( stop > start && /\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 )
}
2026-11-27 11:08:06 +08:00
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 } ` )
2026-11-27 21:08:11 +08:00
assert ( ! node . properties . nodeParams ? . find ( field => field . name === 'requestFields' ) ? . children ? . some ( field => field . name === 'pointDistance' ) , ` Bound pointDistance requires a separate review: ${ node . id } ` )
2026-11-27 11:08:06 +08:00
const body = JSON . parse ( parameter . input )
assert ( body && typeof body === 'object' && ! Array . isArray ( body ) )
2026-11-27 21:08:11 +08:00
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' )
2026-11-27 11:08:06 +08:00
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 ; i < verified . nodes . length ; i ++ ) if ( verified . nodes [ i ] . properties ? . action === 'SCANNER_PARAMETERS' ) {
const old = before . nodes [ i ] . properties . nodeParams . find ( field => field . 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 ) )
2026-11-27 21:08:11 +08:00
console . log ( JSON . stringify ( { flowId : snapshot . current . id , nodes , changed , brightnessUnchanged : true , pointDistance : 0.7 , pathUnchanged : true } ) )