Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/minimax/src/artifacts.ts

Raw
import { lookup } from "node:dns/promises";
import { constants } from "node:fs";
import { lstat, open, unlink } from "node:fs/promises";
import { request as httpsRequest } from "node:https";
import { BlockList, isIP } from "node:net";
import { isAbsolute } from "node:path";

const blocked = new BlockList();
for (const [ip, prefix] of [
	["0.0.0.0", 8],
	["10.0.0.0", 8],
	["100.64.0.0", 10],
	["127.0.0.0", 8],
	["169.254.0.0", 16],
	["172.16.0.0", 12],
	["192.0.0.0", 24],
	["192.0.2.0", 24],
	["192.88.99.0", 24],
	["192.168.0.0", 16],
	["198.18.0.0", 15],
	["198.51.100.0", 24],
	["203.0.113.0", 24],
	["224.0.0.0", 4],
	["240.0.0.0", 4],
] as const)
	blocked.addSubnet(ip, prefix, "ipv4");
blocked.addSubnet("2001::", 23, "ipv6");
blocked.addSubnet("2001:db8::", 32, "ipv6");
blocked.addSubnet("2002::", 16, "ipv6");
blocked.addSubnet("3fff::", 20, "ipv6");
export function publicAddress(address: string): boolean {
	const type = isIP(address);
	if (type === 4) return !blocked.check(address, "ipv4");
	// Accept only global unicast IPv6; mapped/private/transition addresses fail closed.
	return (
		type === 6 && /^[23]/i.test(address) && !blocked.check(address, "ipv6")
	);
}
export function artifactUrl(raw: string): URL {
	let url: URL;
	if (raw.length > 8192) throw new Error("Artifact URL exceeds length limit.");
	try {
		url = new URL(raw);
	} catch {
		throw new Error("Invalid MiniMax artifact URL.");
	}
	if (url.protocol === "http:") url.protocol = "https:";
	if (
		url.protocol !== "https:" ||
		url.username ||
		url.password ||
		(url.port && url.port !== "443") ||
		url.hash
	)
		throw new Error(
			"MiniMax artifacts require credential-free HTTPS on port 443.",
		);
	return url;
}

/** Never forward API headers, follow redirects, use proxy env, or resolve again at connect time. */
export async function download(
	raw: string,
	maxBytes: number,
	signal?: AbortSignal,
): Promise<Buffer> {
	const url = artifactUrl(raw);
	const combined = AbortSignal.any([
		AbortSignal.timeout(30_000),
		...(signal ? [signal] : []),
	]);
	combined.throwIfAborted();
	const hostname = url.hostname.replace(/^\[|\]$/g, "");
	// DNS itself is raced against cancellation; the HTTPS connection uses this exact vetted address.
	const addresses = await new Promise<Awaited<ReturnType<typeof lookup>>[]>(
		(resolve, reject) => {
			const onAbort = () => reject(new Error("Artifact download cancelled."));
			combined.addEventListener("abort", onAbort, { once: true });
			void lookup(hostname, { all: true, verbatim: true }).then(
				(value) => {
					combined.removeEventListener("abort", onAbort);
					resolve(value);
				},
				() => {
					combined.removeEventListener("abort", onAbort);
					reject(new Error("Artifact DNS lookup failed."));
				},
			);
			if (combined.aborted) onAbort();
		},
	);
	if (!addresses.length || addresses.some((a) => !publicAddress(a.address)))
		throw new Error(
			"Artifact host resolved to non-public address; retrieval refused.",
		);
	const pinned = addresses[0];
	if (!pinned) throw new Error("Artifact address unavailable.");
	combined.throwIfAborted();
	return new Promise((resolve, reject) => {
		const req = httpsRequest(
			url,
			{
				method: "GET",
				signal: combined,
				agent: false,
				lookup: (_host, options, callback) => {
					if (options.all) callback(null, [pinned]);
					else callback(null, pinned.address, pinned.family);
				},
			},
			(res) => {
				if (res.statusCode !== 200) {
					res.destroy();
					reject(
						new Error(
							"Artifact retrieval requires HTTP 200; redirects rejected, no HTTP downgrade.",
						),
					);
					return;
				}
				if (Number(res.headers["content-length"] ?? 0) > maxBytes) {
					res.destroy();
					reject(new Error("Artifact exceeds byte limit."));
					return;
				}
				const chunks: Buffer[] = [];
				let bytes = 0;
				res.on("data", (chunk: Buffer) => {
					bytes += chunk.length;
					if (bytes > maxBytes) {
						res.destroy();
						reject(new Error("Artifact exceeds byte limit."));
					} else chunks.push(chunk);
				});
				res.on("end", () => resolve(Buffer.concat(chunks, bytes)));
				res.on("error", () =>
					reject(new Error("Artifact download interrupted; no resubmission.")),
				);
			},
		);
		req.on("error", () =>
			reject(
				new Error(
					"Artifact HTTPS request failed/cancelled; signed URL redacted. No resubmission.",
				),
			),
		);
		req.end();
	});
}

export function outputPath(path: string): void {
	if (!isAbsolute(path) || path.includes("\0"))
		throw new Error(
			"Provide explicit absolute outputPath authorizing this artifact destination; existing files are never overwritten.",
		);
}
export async function availableOutput(path: string): Promise<void> {
	outputPath(path);
	try {
		await lstat(path);
	} catch (error) {
		if ((error as NodeJS.ErrnoException).code === "ENOENT") return;
		throw error;
	}
	throw new Error(
		"Output already exists; choose a new outputPath. No overwrite.",
	);
}
export async function persist(
	path: string,
	bytes: Buffer,
	signal?: AbortSignal,
): Promise<void> {
	outputPath(path);
	signal?.throwIfAborted();
	const file = await open(path, "wx", 0o600);
	try {
		await file.writeFile(bytes, { ...(signal ? { signal } : {}) });
	} catch (error) {
		await file.close();
		await unlink(path).catch(() => {});
		throw error;
	}
	await file.close();
}

export async function imageSource(
	source: string,
	signal?: AbortSignal,
): Promise<string> {
	const maxBytes = 8 * 1024 * 1024;
	let bytes: Buffer;
	if (/^https?:\/\//.test(source))
		bytes = await download(source, maxBytes, signal);
	else {
		if (!isAbsolute(source))
			throw new Error(
				"Image source must be absolute local path or public HTTPS URL.",
			);
		const file = await open(source, constants.O_RDONLY | constants.O_NONBLOCK);
		try {
			const stat = await file.stat();
			if (!stat.isFile() || stat.size > maxBytes)
				throw new Error("Image must be regular file ≤8 MiB.");
			bytes = Buffer.alloc(maxBytes + 1);
			let offset = 0;
			while (offset < bytes.length) {
				signal?.throwIfAborted();
				const part = await file.read(
					bytes,
					offset,
					bytes.length - offset,
					null,
				);
				if (!part.bytesRead) break;
				offset += part.bytesRead;
			}
			if (offset > maxBytes) throw new Error("Image exceeds 8 MiB.");
			bytes = bytes.subarray(0, offset);
		} finally {
			await file.close();
		}
	}
	const mime = bytes.subarray(0, 3).equals(Buffer.from([255, 216, 255]))
		? "image/jpeg"
		: bytes
					.subarray(0, 8)
					.equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
			? "image/png"
			: bytes.subarray(0, 4).toString() === "RIFF" &&
					bytes.subarray(8, 12).toString() === "WEBP"
				? "image/webp"
				: undefined;
	if (!mime) throw new Error("Understanding accepts JPEG, PNG, WebP only.");
	return `data:${mime};base64,${bytes.toString("base64")}`;
}