Files
adam-acs/app/protocol/soap.ts
T
Kevin L'Hereec 9cceb139b7 fix: eslint consistent-type-imports and eqeqeq violations
- Convert value imports used only as types to 'import type' across
  all modified files (cwmp_message_builder, task_queue, device_action_executor,
  main_state_machine, immediate_command_state_machine, configuration_evaluation_state,
  cwmp_controller, cwmp_middleware, device.ts, soap.ts, soap_envelope.ts, tr069_types.ts)
- Replace != null with !== null && !== undefined in main_state_machine and cwmp_controller
2026-07-20 17:14:12 +02:00

117 lines
3.8 KiB
TypeScript

import { XMLParser } from 'fast-xml-parser'
import type { CwmpMessage, SoapEnvelope, SoapHeader } from './tr069_types.js'
export { type SoapEnvelope, type SoapHeader, type CwmpMessage } from './tr069_types.js'
export const CWMP_VERSIONS: Record<string, string> = {
'urn:dslforum-org:cwmp-1-0': '1.0',
'urn:dslforum-org:cwmp-1-1': '1.1',
'urn:dslforum-org:cwmp-1-2': '1.2',
'urn:dslforum-org:cwmp-1-3': '1.3',
'urn:dslforum-org:cwmp-1-4': '1.4',
}
const xmlParser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '$',
textNodeName: '_',
numberParseOptions: { leadingZeros: false, hex: false },
isArray: (name) => {
const arrayTags = ['ParameterValueStruct', 'ParameterInfoStruct', 'EventStruct', 'string']
return arrayTags.includes(name)
},
})
export function parseSoapXml(xml: string): SoapEnvelope {
const parsed = xmlParser.parse(xml) as Record<string, unknown>
const envelope =
(parsed['soap-env:Envelope'] as Record<string, unknown>) ||
(parsed['SOAP-ENV:Envelope'] as Record<string, unknown>) ||
(parsed['soapenv:Envelope'] as Record<string, unknown>) ||
{}
const header =
(envelope['soap-env:Header'] as Record<string, unknown>) ||
(envelope['SOAP-ENV:Header'] as Record<string, unknown>) ||
(envelope['soapenv:Header'] as Record<string, unknown>) ||
{}
const body =
(envelope['soap-env:Body'] as Record<string, unknown>) ||
(envelope['SOAP-ENV:Body'] as Record<string, unknown>) ||
(envelope['soapenv:Body'] as Record<string, unknown>) ||
{}
// Detect CWMP version from namespace attributes
let cwmpVersion = '1.0'
for (const [, value] of Object.entries(envelope)) {
if (typeof value === 'string' && value.startsWith('urn:dslforum-org:cwmp-')) {
cwmpVersion = CWMP_VERSIONS[value] || '1.0'
break
}
}
const soapHeader: SoapHeader = {}
const idNode = header['cwmp:ID'] || header['cwmp:Id']
if (idNode) {
soapHeader.id =
typeof idNode === 'string' ? idNode : (idNode as { _?: string })?._ || String(idNode)
}
return { header: soapHeader, body, cwmpVersion }
}
/** Types de méthodes CWMP acceptés en entrée (CPE→ACS) */
const ALLOWED_CWMP_TYPES = new Set([
'Inform',
'TransferComplete',
'GetParameterNamesResponse',
'GetParameterValuesResponse',
'GetParameterAttributesResponse',
'SetParameterValuesResponse',
'SetParameterAttributesResponse',
'RebootResponse',
'FactoryResetResponse',
'DownloadResponse',
'UploadResponse',
'ScheduleInformResponse',
'Fault',
])
export function extractCwmpMessage(body: Record<string, unknown>): CwmpMessage | null {
for (const key of Object.keys(body)) {
if (key.startsWith('cwmp:')) {
const type = key.replace('cwmp:', '')
if (!ALLOWED_CWMP_TYPES.has(type)) return null
const data = body[key]
return { type, data } as CwmpMessage
}
}
// SOAP Fault envoyé par le CPE (soap-env:Fault / SOAP-ENV:Fault)
const faultKey = Object.keys(body).find(
(k) => k.toLowerCase().endsWith(':fault') || k === 'Fault'
)
if (faultKey) {
return { type: 'Fault', data: body[faultKey] } as CwmpMessage
}
return null
}
export function buildSoapEnvelope(cwmpId: string | undefined, bodyContent: string): string {
const id = cwmpId || Math.random().toString(36).substring(2, 10)
return `<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cwmp="urn:dslforum-org:cwmp-1-0">
<soap-env:Header>
<cwmp:ID soap-env:mustUnderstand="1">${id}</cwmp:ID>
</soap-env:Header>
<soap-env:Body>
${bodyContent}
</soap-env:Body>
</soap-env:Envelope>`
}