Files
adam-acs/app/protocol/soap.ts
T
Kevin L'Hereec f9e56c78b1 refactor: migrate all internal imports to package.json subpath aliases
- Add #domain, #protocol, #utils aliases to package.json imports map
- Replace all relative imports (./, ../, ../..) in app/ and
  database/mongo_migrations/ with the corresponding #alias imports,
  eliminating relative path traversal across the codebase
- Remove dead code: ConfigurationVersionModel.findByConfigurationIdentifier
  (unused, superseded by paginateVersions) and unused exception classes
  (NotFoundException, UnprocessableEntityException, DatabaseException)
  from app/exceptions/index.ts, keeping only BadRequestException
- Fix pre-existing syntax error in configuration_version.ts (mismatched
  parenthesis) that broke tsc
- Fix eqeqeq violation in configuration_service.ts and isNaN ->
  Number.isNaN in configuration_engine.ts / cwmp-1-0.ts / exchange_controller.ts
- Fix no-shadow violation in cwmp-1-0.ts EventStruct constructor
- Prettier formatting pass on touched files

Validated: tsc --noEmit clean, eslint app/ clean, full test suite green.
2026-07-22 11:45:14 +02:00

117 lines
3.8 KiB
TypeScript

import { XMLParser } from 'fast-xml-parser'
import type { CwmpMessage, SoapEnvelope, SoapHeader } from '#protocol/tr069_types'
export { type SoapEnvelope, type SoapHeader, type CwmpMessage } from '#protocol/tr069_types'
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>`
}