- 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.
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { createHash } from 'node:crypto'
|
|
import type { Configuration } from '#domain/configuration/configuration'
|
|
|
|
/**
|
|
* Computes a 12-character hex content hash of a Configuration's desired state.
|
|
* The hash is based on the sorted desiredParameters and firmwareTarget.
|
|
*/
|
|
export function computeContentHash(config: Configuration): string {
|
|
const payload = JSON.stringify({
|
|
desiredParameters: config.desiredParameters
|
|
.slice()
|
|
.sort((a, b) => a.name.localeCompare(b.name)),
|
|
firmwareTarget: config.firmwareTarget || null,
|
|
})
|
|
return createHash('sha256').update(payload).digest('hex').substring(0, 12)
|
|
}
|
|
|
|
/**
|
|
* Builds the ProvisioningCode string for a Configuration.
|
|
* Format: `<identifier>(v<version>)-<hash12>`
|
|
* Example: `BM632w-standard(v1.0.1)-a1b2c3d4e5f6`
|
|
*/
|
|
export function buildProvisioningCode(config: Configuration): string {
|
|
const hash = computeContentHash(config)
|
|
return `${config.identifier}(v${config.version})-${hash}`
|
|
}
|
|
|
|
/**
|
|
* Parses an existing ProvisioningCode string.
|
|
* Returns null if the format is invalid.
|
|
*/
|
|
export function parseProvisioningCode(
|
|
code: string
|
|
): { identifier: string; version: string; hash: string } | null {
|
|
const match = code.match(/^(.+)\(v(.+)\)-([a-f0-9]{12})$/)
|
|
if (!match) return null
|
|
return { identifier: match[1], version: match[2], hash: match[3] }
|
|
}
|