Forked from harshallmstudio/document-streamer
Forked from harshallmstudio/document-streamer
src / mhtmlParser.ts
interface MimePart {
headers: string;
body: string;
}
function unfoldHeaders(value: string): string {
return value.replace(/\r?\n[ \t]+/g, " ");
}
function getHeader(headers: string, name: string): string {
const unfolded = unfoldHeaders(headers);
const expression = new RegExp(`^${name}:[ \\t]*(.*)$`, "im");
return expression.exec(unfolded)?.[1]?.trim() ?? "";
}
function getMimeParameter(
value: string,
name: string,
): string | undefined {
const expression = new RegExp(
`(?:^|;)\\s*${name}\\s*=\\s*(?:"([^"]*)"|([^;\\s]*))`,
"i",
);
const match = expression.exec(value);
return match?.[1] ?? match?.[2];
}
function splitMimePart(part: string): MimePart {
const separator = /\r?\n\r?\n/.exec(part);
if (!separator || separator.index === undefined) {
return { headers: part, body: "" };
}
return {
headers: part.slice(0, separator.index),
body: part.slice(separator.index + separator[0].length),
};
}
/** Decode quoted-printable data without corrupting multi-byte UTF-8 text. */
function decodeQuotedPrintable(value: string): Buffer {
const normalized = value.replace(/=\r?\n/g, "");
const bytes: number[] = [];
for (let index = 0; index < normalized.length; index += 1) {
const character = normalized[index];
if (character === "=" && index + 2 < normalized.length) {
const hexadecimal = normalized.slice(index + 1, index + 3);
if (/^[0-9A-Fa-f]{2}$/.test(hexadecimal)) {
bytes.push(Number.parseInt(hexadecimal, 16));
index += 2;
continue;
}
}
bytes.push(character.charCodeAt(0) & 0xff);
}
return Buffer.from(bytes);
}
function decodePartBody(part: MimePart): string {
const transferEncoding = getHeader(
part.headers,
"Content-Transfer-Encoding",
).toLowerCase();
let bytes: Buffer;
if (transferEncoding === "base64") {
bytes = Buffer.from(part.body.replace(/\s/g, ""), "base64");
} else if (transferEncoding === "quoted-printable") {
bytes = decodeQuotedPrintable(part.body);
} else {
bytes = Buffer.from(part.body, "latin1");
}
const charset =
getMimeParameter(
getHeader(part.headers, "Content-Type"),
"charset",
) ?? "utf-8";
try {
return new TextDecoder(charset).decode(bytes);
} catch {
return bytes.toString("utf8");
}
}
/**
* Extract the primary HTML resource from an MHTML/MHT archive.
* Images, CSS, scripts, and other archived resources are ignored.
*/
export function extractMhtmlHtml(source: Buffer): string {
const input = source.toString("latin1");
const root = splitMimePart(input);
if (!root.headers.trim()) {
throw new Error("Invalid MHTML: MIME headers are missing.");
}
const rootContentType = getHeader(root.headers, "Content-Type");
const boundary = getMimeParameter(rootContentType, "boundary");
if (!boundary) {
if (!rootContentType.toLowerCase().startsWith("text/html")) {
throw new Error("The MHTML multipart boundary is missing.");
}
const html = decodePartBody(root);
if (!html.trim()) {
throw new Error("The MHTML HTML document is empty.");
}
return html;
}
const snapshotLocation = getHeader(
root.headers,
"Snapshot-Content-Location",
);
const sections = input.split(`--${boundary}`).slice(1);
let firstHtml: string | undefined;
for (const rawSection of sections) {
if (rawSection.trimStart().startsWith("--")) {
break;
}
const part = splitMimePart(rawSection.replace(/^\r?\n/, ""));
const contentType = getHeader(part.headers, "Content-Type");
if (!contentType.toLowerCase().startsWith("text/html")) {
continue;
}
const html = decodePartBody(part);
if (!html.trim()) {
continue;
}
const location = getHeader(part.headers, "Content-Location");
if (snapshotLocation && location === snapshotLocation) {
return html;
}
firstHtml ??= html;
}
if (firstHtml !== undefined) {
return firstHtml;
}
throw new Error("No HTML document part was found in the MHTML archive.");
}
src / mhtmlParser.ts
interface MimePart {
headers: string;
body: string;
}
function unfoldHeaders(value: string): string {
return value.replace(/\r?\n[ \t]+/g, " ");
}
function getHeader(headers: string, name: string): string {
const unfolded = unfoldHeaders(headers);
const expression = new RegExp(`^${name}:[ \\t]*(.*)$`, "im");
return expression.exec(unfolded)?.[1]?.trim() ?? "";
}
function getMimeParameter(
value: string,
name: string,
): string | undefined {
const expression = new RegExp(
`(?:^|;)\\s*${name}\\s*=\\s*(?:"([^"]*)"|([^;\\s]*))`,
"i",
);
const match = expression.exec(value);
return match?.[1] ?? match?.[2];
}
function splitMimePart(part: string): MimePart {
const separator = /\r?\n\r?\n/.exec(part);
if (!separator || separator.index === undefined) {
return { headers: part, body: "" };
}
return {
headers: part.slice(0, separator.index),
body: part.slice(separator.index + separator[0].length),
};
}
/** Decode quoted-printable data without corrupting multi-byte UTF-8 text. */
function decodeQuotedPrintable(value: string): Buffer {
const normalized = value.replace(/=\r?\n/g, "");
const bytes: number[] = [];
for (let index = 0; index < normalized.length; index += 1) {
const character = normalized[index];
if (character === "=" && index + 2 < normalized.length) {
const hexadecimal = normalized.slice(index + 1, index + 3);
if (/^[0-9A-Fa-f]{2}$/.test(hexadecimal)) {
bytes.push(Number.parseInt(hexadecimal, 16));
index += 2;
continue;
}
}
bytes.push(character.charCodeAt(0) & 0xff);
}
return Buffer.from(bytes);
}
function decodePartBody(part: MimePart): string {
const transferEncoding = getHeader(
part.headers,
"Content-Transfer-Encoding",
).toLowerCase();
let bytes: Buffer;
if (transferEncoding === "base64") {
bytes = Buffer.from(part.body.replace(/\s/g, ""), "base64");
} else if (transferEncoding === "quoted-printable") {
bytes = decodeQuotedPrintable(part.body);
} else {
bytes = Buffer.from(part.body, "latin1");
}
const charset =
getMimeParameter(
getHeader(part.headers, "Content-Type"),
"charset",
) ?? "utf-8";
try {
return new TextDecoder(charset).decode(bytes);
} catch {
return bytes.toString("utf8");
}
}
/**
* Extract the primary HTML resource from an MHTML/MHT archive.
* Images, CSS, scripts, and other archived resources are ignored.
*/
export function extractMhtmlHtml(source: Buffer): string {
const input = source.toString("latin1");
const root = splitMimePart(input);
if (!root.headers.trim()) {
throw new Error("Invalid MHTML: MIME headers are missing.");
}
const rootContentType = getHeader(root.headers, "Content-Type");
const boundary = getMimeParameter(rootContentType, "boundary");
if (!boundary) {
if (!rootContentType.toLowerCase().startsWith("text/html")) {
throw new Error("The MHTML multipart boundary is missing.");
}
const html = decodePartBody(root);
if (!html.trim()) {
throw new Error("The MHTML HTML document is empty.");
}
return html;
}
const snapshotLocation = getHeader(
root.headers,
"Snapshot-Content-Location",
);
const sections = input.split(`--${boundary}`).slice(1);
let firstHtml: string | undefined;
for (const rawSection of sections) {
if (rawSection.trimStart().startsWith("--")) {
break;
}
const part = splitMimePart(rawSection.replace(/^\r?\n/, ""));
const contentType = getHeader(part.headers, "Content-Type");
if (!contentType.toLowerCase().startsWith("text/html")) {
continue;
}
const html = decodePartBody(part);
if (!html.trim()) {
continue;
}
const location = getHeader(part.headers, "Content-Location");
if (snapshotLocation && location === snapshotLocation) {
return html;
}
firstHtml ??= html;
}
if (firstHtml !== undefined) {
return firstHtml;
}
throw new Error("No HTML document part was found in the MHTML archive.");
}