usenet-indexer/app/services/YencService.ts

37 lines
1022 B
TypeScript

export function parseYencMeta(buffer: Buffer): { header: Record<string, any>; crc32?: string } {
const text = buffer.toString('latin1')
const lines = text.split(/\\r?\\n/)
const header: Record<string, any> = {}
let crc32: string | undefined
for (const line of lines) {
if (line.startsWith('=ybegin')) {
const parts = line.split(' ')
parts.forEach((part) => {
if (part.includes('=')) {
const [key, value] = part.split('=')
if (key === 'name') {
header[key] = value.trim()
} else {
header[key] = parseInt(value, 10)
}
}
})
} else if (line.startsWith('=ypart')) {
const match = /begin=(\d+)/.exec(line)
if (match) {
header.partBegin = parseInt(match[1], 10)
}
} else if (line.startsWith('=yend')) {
const match = /crc32=([a-fA-F0-9]+)/.exec(line)
if (match) {
crc32 = match[1]
}
break // End of yenc data
}
}
return { header, crc32 }
}