Files
MikrotikManager/backend/scripts/writer-profile/catalogue.mts
T

58 lines
5.3 KiB
TypeScript

import assert from 'node:assert/strict'
import {Fixed44CatalogueValueBridgeData} from './catalogue-value-bridge.js'
import {FIXED44_CATALOGUE_BUILD_SQL} from './fixed44-build-sql.js'
import {BUILD44_PROVENANCE,FIXED44_ROLE_TEMPLATE} from './fixed44-build-definition.js'
import {BUILD56_PROVENANCE,FIXED56_ROLE_TEMPLATE} from './fixed56-build-definition.js'
import {BUILD57_PROVENANCE,FIXED57_ROLE_TEMPLATE} from './fixed57-build-definition.js'
import {BUILD58_PROVENANCE,FIXED58_ROLE_TEMPLATE} from './fixed58-build-definition.js'
import {assertRestoreProfileQuarantine} from '../../src/db/database-restore-quarantine-profile.js'
import {canonical,sha} from './application-writer-profile-canonical.js'
import type {CatalogueEdition} from './catalogue-projection.js'
export function approvedDefinition(value:unknown){
assert(value&&typeof value==='object');const v=value as Record<string,unknown>
assert.deepEqual(Object.keys(v).sort(),['format','provenance','template'])
if(v.format==='application-writer-source44-definition-approval-v1'){
assert.equal(canonical(v.provenance),canonical(BUILD44_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED44_ROLE_TEMPLATE))
return Object.freeze({definitionSha256:BUILD44_PROVENANCE.templateSha256,manifestSha256:BUILD44_PROVENANCE.manifestSha256})
}
if(v.format==='application-writer-source57-definition-approval-v1'){
assert.equal(canonical(v.provenance),canonical(BUILD57_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED57_ROLE_TEMPLATE))
return Object.freeze({edition:57 as const,definitionSha256:BUILD57_PROVENANCE.templateSha256,manifestSha256:BUILD57_PROVENANCE.manifestSha256})
}
if(v.format==='application-writer-source58-definition-approval-v1'){
assert.equal(canonical(v.provenance),canonical(BUILD58_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED58_ROLE_TEMPLATE))
return Object.freeze({edition:58 as const,definitionSha256:BUILD58_PROVENANCE.templateSha256,manifestSha256:BUILD58_PROVENANCE.manifestSha256})
}
assert.equal(v.format,'application-writer-source56-definition-approval-v1')
assert.equal(canonical(v.provenance),canonical(BUILD56_PROVENANCE));assert.equal(canonical(v.template),canonical(FIXED56_ROLE_TEMPLATE))
return Object.freeze({edition:56 as const,definitionSha256:BUILD56_PROVENANCE.templateSha256,manifestSha256:BUILD56_PROVENANCE.manifestSha256})
}
type CatalogueSession = {query:(sql:string,values?:unknown[])=>Promise<{rows:Record<string,unknown>[]} >}
type CatalogueInstallation = {roleBindings:ConstructorParameters<typeof Fixed44CatalogueValueBridgeData>[0]}
assert.equal(FIXED44_CATALOGUE_BUILD_SQL.split('LIMIT 104').length,2)
const fixed56Sql = FIXED44_CATALOGUE_BUILD_SQL.replace('LIMIT 104','LIMIT 110')
/** Real incremental DATA verifier on caller's retained transaction. No pool or attestor. */
export const collectSource44Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 44)
export const collectSource56Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 56)
export const collectSource57Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 57)
export const collectSource58Catalogue = (s:CatalogueSession, installation:CatalogueInstallation) => collectCatalogue(s, installation, 58)
async function collectCatalogue(s:CatalogueSession,installation:CatalogueInstallation,edition:CatalogueEdition){
const settings=(await s.query("SELECT pg_catalog.current_setting('search_path') AS path,pg_catalog.current_setting('TimeZone') AS zone,pg_catalog.current_setting('DateStyle') AS dates")).rows
assert.equal(settings.length,1);assert(['path','zone','dates'].every(k=>typeof settings[0][k]==='string'))
let cursor=false,primary:unknown,failed=false
try{
await s.query("SELECT pg_catalog.set_config('search_path','pg_catalog, public',true),pg_catalog.set_config('TimeZone','UTC',true),pg_catalog.set_config('DateStyle','ISO, YMD',true)")
const bindings=installation.roleBindings,values=['operator','runtime','capture','publisher','control'].map(k=>bindings[k as keyof typeof bindings].name)
if(edition===58)await assertRestoreProfileQuarantine(s,bindings.runtime.name,bindings.operator.name,bindings.control.name)
const bridge=new Fixed44CatalogueValueBridgeData(bindings,edition)
await s.query('DECLARE mm_publisher44 NO SCROLL CURSOR WITHOUT HOLD FOR '+(edition===44?FIXED44_CATALOGUE_BUILD_SQL:fixed56Sql),values);cursor=true
for(;;){const page=(await s.query('FETCH FORWARD 128 FROM mm_publisher44')).rows;bridge.acceptPage(page);if(page.length===0)break}
const result=bridge.finish();await s.query('CLOSE mm_publisher44');cursor=false
return {catalogSha256:sha(canonical({values:result.values.snapshotSha256,catalogue:result.catalogue})),definitionSha256:result.catalogue.definitionSha256,manifestSha256:result.catalogue.manifestSha256,leafCount:result.catalogue.leafRelations}
}catch(error){primary=error;failed=true;throw error}
finally{
try{if(cursor)await s.query('CLOSE mm_publisher44');await s.query("SELECT pg_catalog.set_config('search_path',$1,true),pg_catalog.set_config('TimeZone',$2,true),pg_catalog.set_config('DateStyle',$3,true)",[settings[0].path,settings[0].zone,settings[0].dates])}
catch(error){if(failed)throw new AggregateError([primary,error],'Catalogue and context cleanup failed');throw error}
}
}