|
| 1 | +import { |
| 2 | + call, |
| 3 | + type Operation, |
| 4 | + resource, |
| 5 | + until, |
| 6 | + useAbortSignal, |
| 7 | +} from "effection"; |
| 8 | +import { dirname, join, normalize } from "@std/path"; |
| 9 | +import { ensureDir } from "@std/fs/ensure-dir"; |
| 10 | +import { fromHtml } from "hast-util-from-html"; |
| 11 | +import { toHtml } from "hast-util-to-html"; |
| 12 | +import { selectAll } from "hast-util-select"; |
| 13 | +import { useTaskBuffer } from "./task-buffer.ts"; |
| 14 | +import { createApi } from "@effectionx/context-api"; |
| 15 | + |
| 16 | +export interface Downloader extends Operation<void> { |
| 17 | + download(spec: string, context?: URL): Operation<void>; |
| 18 | +} |
| 19 | + |
| 20 | +export interface DownloaderOptions { |
| 21 | + host: URL; |
| 22 | + base: URL; |
| 23 | + outdir: string; |
| 24 | +} |
| 25 | + |
| 26 | +export type DownloadResult = |
| 27 | + | { ok: true; bytes: number } |
| 28 | + | { ok: false; url: string; referrer: string }; |
| 29 | + |
| 30 | +export const DownloadApi = createApi("@staticalize/download", { |
| 31 | + *download( |
| 32 | + downloader: Downloader, |
| 33 | + opts: DownloaderOptions, |
| 34 | + source: URL, |
| 35 | + referrer: URL, |
| 36 | + ): Operation<DownloadResult> { |
| 37 | + let { host, base, outdir } = opts; |
| 38 | + let signal = yield* useAbortSignal(); |
| 39 | + let path = normalize(join(outdir, source.pathname)); |
| 40 | + |
| 41 | + let response = yield* until(fetch(source.toString(), { signal })); |
| 42 | + if (response.ok) { |
| 43 | + if (response.headers.get("Content-Type")?.includes("html")) { |
| 44 | + let destpath = join(path, "index.html"); |
| 45 | + let content = yield* until(response.text()); |
| 46 | + let html = fromHtml(content); |
| 47 | + |
| 48 | + let links = selectAll("link[href]", html); |
| 49 | + |
| 50 | + for (let link of links) { |
| 51 | + let href = link.properties.href as string; |
| 52 | + yield* downloader.download(href, source); |
| 53 | + |
| 54 | + // replace self-referencing absolute urls with the destination site |
| 55 | + if (href.startsWith(host.origin)) { |
| 56 | + let url = new URL(href); |
| 57 | + url.host = base.host; |
| 58 | + url.port = base.port; |
| 59 | + url.protocol = base.protocol; |
| 60 | + link.properties.href = url.href; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + let assets = selectAll("[src]", html); |
| 65 | + |
| 66 | + for (let element of assets) { |
| 67 | + let src = element.properties.src as string; |
| 68 | + yield* downloader.download(src, source); |
| 69 | + |
| 70 | + // replace self-referencing absolute urls with the destination site |
| 71 | + if (src.startsWith(host.origin)) { |
| 72 | + let url = new URL(src); |
| 73 | + url.host = base.host; |
| 74 | + url.port = base.port; |
| 75 | + url.protocol = base.protocol; |
| 76 | + element.properties.src = url.href; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + let withContents = selectAll("[content]", html); |
| 81 | + for (let element of withContents) { |
| 82 | + let attr = String(element.properties.content); |
| 83 | + if (attr.startsWith(host.origin)) { |
| 84 | + yield* downloader.download(attr, source); |
| 85 | + let url = new URL(attr); |
| 86 | + url.host = base.host; |
| 87 | + url.port = base.port; |
| 88 | + url.protocol = base.protocol; |
| 89 | + element.properties.content = url.href; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + let output = toHtml(html); |
| 94 | + yield* call(async () => { |
| 95 | + let destdir = dirname(destpath); |
| 96 | + await ensureDir(destdir); |
| 97 | + await Deno.writeTextFile(destpath, output); |
| 98 | + }); |
| 99 | + return { ok: true, bytes: new TextEncoder().encode(output).byteLength }; |
| 100 | + } else { |
| 101 | + let size = Number(response.headers.get("Content-Length") ?? 0); |
| 102 | + yield* call(async () => { |
| 103 | + let destdir = dirname(path); |
| 104 | + await ensureDir(destdir); |
| 105 | + await Deno.writeFile(path, response.body!); |
| 106 | + }); |
| 107 | + return { ok: true, bytes: size }; |
| 108 | + } |
| 109 | + } else { |
| 110 | + return { |
| 111 | + ok: false, |
| 112 | + url: source.toString(), |
| 113 | + referrer: referrer.toString(), |
| 114 | + }; |
| 115 | + } |
| 116 | + }, |
| 117 | +}); |
| 118 | + |
| 119 | +const { download } = DownloadApi.operations; |
| 120 | + |
| 121 | +export function useDownloader(opts: DownloaderOptions): Operation<Downloader> { |
| 122 | + let seen = new Map<string, boolean>(); |
| 123 | + return resource(function* (provide) { |
| 124 | + let { host } = opts; |
| 125 | + |
| 126 | + let buffer = yield* useTaskBuffer(75); |
| 127 | + |
| 128 | + let downloader: Downloader = { |
| 129 | + *download(loc, context = host) { |
| 130 | + if (loc.startsWith("//")) { |
| 131 | + return; |
| 132 | + } |
| 133 | + let source = loc.match(/^\w+:/) ? new URL(loc) : new URL(loc, context); |
| 134 | + if (source.host !== host.host) { |
| 135 | + return; |
| 136 | + } |
| 137 | + let key = source.href; |
| 138 | + if (seen.get(key)) { |
| 139 | + return; |
| 140 | + } |
| 141 | + seen.set(key, true); |
| 142 | + |
| 143 | + yield* buffer.spawn(() => download(downloader, opts, source, context)); |
| 144 | + }, |
| 145 | + *[Symbol.iterator]() { |
| 146 | + yield* buffer; |
| 147 | + }, |
| 148 | + }; |
| 149 | + |
| 150 | + yield* provide(downloader); |
| 151 | + }); |
| 152 | +} |
0 commit comments