refactor(ogg): share OpusTags prefix reads (#17081)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-05 21:20:46 +08:00
1 parent 1b519a0a64
commit dcee5d1d7d
1 file changed
+28 -45
+28 -45
View File
@@ -252,58 +252,41 @@ export const oggOpusLayout = async (
continue;
}
if (!tagsPacketOpen) {
const probeLength =
page.packetLength >= OPUS_TAGS.length
? Math.min(page.packetLength, TAGS_PREFIX_LIMIT)
: 0;
const probe =
probeLength > 0
? await readRange(page.packetOffset, probeLength)
: Buffer.alloc(0);
if (probe === "truncated") {
cutOff = { kind: "truncated", requiredEnd: page.offset + page.length };
break;
}
if (
probe.length >= OPUS_TAGS.length &&
probe.subarray(0, OPUS_TAGS.length).equals(OPUS_TAGS)
) {
tagsPacketOpen = true;
pages.push(page);
tagsPrefixChunks.push(probe);
tagsPrefixBytes += probe.length;
at = page.offset + page.length;
if (!page.continues) {
tagsComplete = true;
break;
}
continue;
}
// RFC 7845 section 3 requires OpusTags as the stream's second packet.
secondPacketNotTags = true;
limitations.push(
`The Opus stream's second packet, on the page at offset ${String(page.offset)}, is not OpusTags; no comment header was read.`,
);
const prefixLength =
!tagsPacketOpen && page.packetLength < OPUS_TAGS.length
? 0
: Math.min(page.packetLength, TAGS_PREFIX_LIMIT - tagsPrefixBytes);
const chunk =
prefixLength > 0
? await readRange(page.packetOffset, prefixLength)
: undefined;
if (chunk === "truncated") {
cutOff = { kind: "truncated", requiredEnd: page.offset + page.length };
break;
}
if (tagsPrefixBytes < TAGS_PREFIX_LIMIT && page.packetLength > 0) {
const chunk = await readRange(
page.packetOffset,
Math.min(page.packetLength, TAGS_PREFIX_LIMIT - tagsPrefixBytes),
);
if (chunk === "truncated") {
cutOff = { kind: "truncated", requiredEnd: page.offset + page.length };
if (!tagsPacketOpen) {
if (
chunk === undefined ||
chunk.length < OPUS_TAGS.length ||
!chunk.subarray(0, OPUS_TAGS.length).equals(OPUS_TAGS)
) {
// RFC 7845 section 3 requires OpusTags as the stream's second packet.
secondPacketNotTags = true;
limitations.push(
`The Opus stream's second packet, on the page at offset ${String(page.offset)}, is not OpusTags; no comment header was read.`,
);
break;
}
tagsPrefixChunks.push(chunk);
tagsPrefixBytes += chunk.length;
}
if ((page.headerType & OGG_CONTINUED_PACKET) === 0)
tagsPacketOpen = true;
} else if ((page.headerType & OGG_CONTINUED_PACKET) === 0) {
limitations.push(
`The OpusTags packet continues onto the page at offset ${String(page.offset)}, but that page does not set the continued-packet flag.`,
);
}
if (chunk !== undefined) {
tagsPrefixChunks.push(chunk);
tagsPrefixBytes += chunk.length;
}
pages.push(page);
at = page.offset + page.length;
if (!page.continues) {