|
| 1 | +/* |
| 2 | + * cache.ts |
| 3 | + * |
| 4 | + * A persistent cache for sass compilation based partly on Deno.KV. |
| 5 | + * |
| 6 | + * Copyright (C) 2024 Posit Software, PBC |
| 7 | + */ |
| 8 | + |
| 9 | +import { InternalError } from "../lib/error.ts"; |
| 10 | +import { md5Hash } from "../hash.ts"; |
| 11 | +import { join } from "../../deno_ral/path.ts"; |
| 12 | +import { ensureDirSync, existsSync } from "../../deno_ral/fs.ts"; |
| 13 | +import { dartCompile } from "../dart-sass.ts"; |
| 14 | +import { TempContext } from "../temp.ts"; |
| 15 | +import { safeRemoveIfExists } from "../path.ts"; |
| 16 | +import * as log from "../../deno_ral/log.ts"; |
| 17 | + |
| 18 | +class SassCache { |
| 19 | + kv: Deno.Kv; |
| 20 | + path: string; |
| 21 | + |
| 22 | + constructor(kv: Deno.Kv, path: string) { |
| 23 | + this.kv = kv; |
| 24 | + this.path = path; |
| 25 | + } |
| 26 | + |
| 27 | + async getFromHash( |
| 28 | + hash: string, |
| 29 | + inputHash: string, |
| 30 | + force?: boolean, |
| 31 | + ): Promise<string | null> { |
| 32 | + log.debug( |
| 33 | + `SassCache.getFromHash(hash=${hash}, inputHash=${inputHash}, force=${force})`, |
| 34 | + ); |
| 35 | + // verify that the hash is a valid md5 hash |
| 36 | + if (hash.length !== 32 || !/^[0-9a-f]{32}$/.test(hash)) { |
| 37 | + throw new InternalError(`Invalid hash length: ${hash.length}`); |
| 38 | + } |
| 39 | + |
| 40 | + const result = await this.kv.get(["entry", hash]); |
| 41 | + if (result.value === null) { |
| 42 | + log.debug(` cache miss`); |
| 43 | + return null; |
| 44 | + } |
| 45 | + if (typeof result.value !== "object") { |
| 46 | + throw new InternalError( |
| 47 | + `Unsupported SassCache entry type\nExpected SassCacheEntry, got ${typeof result |
| 48 | + .value}`, |
| 49 | + ); |
| 50 | + } |
| 51 | + const v = result.value as Record<string, unknown>; |
| 52 | + if (typeof v.key !== "string" || typeof v.hash !== "string") { |
| 53 | + throw new InternalError( |
| 54 | + `Unsupported SassCache entry type\nExpected SassCacheEntry, got ${typeof result |
| 55 | + .value}`, |
| 56 | + ); |
| 57 | + } |
| 58 | + const outputFilePath = join(this.path, `${hash}.css`); |
| 59 | + |
| 60 | + // if the hash doesn't match the key, return null |
| 61 | + if ((v.hash !== inputHash && !force) || !existsSync(outputFilePath)) { |
| 62 | + if (v.hash !== inputHash) { |
| 63 | + log.debug(` hash mismatch: ${v.hash} !== ${inputHash}`); |
| 64 | + } else if (force) { |
| 65 | + log.debug(` forcing recomputation`); |
| 66 | + } else { |
| 67 | + log.debug(` output file missing: ${outputFilePath}`); |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + log.debug(` cache hit`); |
| 73 | + return outputFilePath; |
| 74 | + } |
| 75 | + |
| 76 | + async setFromHash( |
| 77 | + identifierHash: string, |
| 78 | + inputHash: string, |
| 79 | + input: string, |
| 80 | + loadPaths: string[], |
| 81 | + temp: TempContext, |
| 82 | + cacheIdentifier: string, |
| 83 | + compressed?: boolean, |
| 84 | + ): Promise<string> { |
| 85 | + log.debug(`SassCache.setFromHash(${identifierHash}, ${inputHash}), ...`); |
| 86 | + const outputFilePath = join(this.path, `${identifierHash}.css`); |
| 87 | + try { |
| 88 | + await dartCompile( |
| 89 | + input, |
| 90 | + outputFilePath, |
| 91 | + temp, |
| 92 | + loadPaths, |
| 93 | + compressed, |
| 94 | + ); |
| 95 | + } catch (error) { |
| 96 | + // Compilation failed, so clear out the output file (if exists) |
| 97 | + // which will be invalid CSS |
| 98 | + try { |
| 99 | + safeRemoveIfExists(outputFilePath); |
| 100 | + } finally { |
| 101 | + // doesn't matter |
| 102 | + } |
| 103 | + throw error; |
| 104 | + } |
| 105 | + await this.kv.set(["entry", identifierHash], { |
| 106 | + key: cacheIdentifier, |
| 107 | + hash: inputHash, |
| 108 | + }); |
| 109 | + return outputFilePath; |
| 110 | + } |
| 111 | + |
| 112 | + async set( |
| 113 | + input: string, |
| 114 | + loadPaths: string[], |
| 115 | + temp: TempContext, |
| 116 | + cacheIdentifier: string, |
| 117 | + compressed?: boolean, |
| 118 | + ): Promise<string> { |
| 119 | + const identifierHash = md5Hash(cacheIdentifier); |
| 120 | + const inputHash = md5Hash(input); |
| 121 | + return this.setFromHash( |
| 122 | + identifierHash, |
| 123 | + inputHash, |
| 124 | + input, |
| 125 | + loadPaths, |
| 126 | + temp, |
| 127 | + cacheIdentifier, |
| 128 | + compressed, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + async getOrSet( |
| 133 | + input: string, |
| 134 | + loadPaths: string[], |
| 135 | + temp: TempContext, |
| 136 | + cacheIdentifier: string, |
| 137 | + compressed?: boolean, |
| 138 | + ): Promise<string> { |
| 139 | + log.debug(`SassCache.getOrSet(...)`); |
| 140 | + const identifierHash = md5Hash(cacheIdentifier); |
| 141 | + const inputHash = md5Hash(input); |
| 142 | + const existing = await this.getFromHash(identifierHash, inputHash); |
| 143 | + if (existing !== null) { |
| 144 | + log.debug(` cache hit`); |
| 145 | + return existing; |
| 146 | + } |
| 147 | + log.debug(` cache miss, setting`); |
| 148 | + return this.setFromHash( |
| 149 | + identifierHash, |
| 150 | + inputHash, |
| 151 | + input, |
| 152 | + loadPaths, |
| 153 | + temp, |
| 154 | + cacheIdentifier, |
| 155 | + compressed, |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +const currentSassCacheVersion = 1; |
| 161 | + |
| 162 | +const requiredQuartoVersions: Record<number, string> = { |
| 163 | + 1: "1.6.0", |
| 164 | +}; |
| 165 | + |
| 166 | +async function checkVersion(kv: Deno.Kv, path: string) { |
| 167 | + const version = await kv.get(["version"]); |
| 168 | + if (version.value === null) { |
| 169 | + await kv.set(["version"], 1); |
| 170 | + } else { |
| 171 | + if (typeof version.value !== "number") { |
| 172 | + throw new Error( |
| 173 | + `Unsupported SassCache version type in ${path}\nExpected number, got ${typeof version |
| 174 | + .value}`, |
| 175 | + ); |
| 176 | + } |
| 177 | + if (version.value < currentSassCacheVersion) { |
| 178 | + // in the future we should clean this automatically, but this is v1 and there should be |
| 179 | + // no old data anywhere. |
| 180 | + throw new Error( |
| 181 | + `Found outdated SassCache version. Please clear ${path}.`, |
| 182 | + ); |
| 183 | + } |
| 184 | + if (version.value > currentSassCacheVersion) { |
| 185 | + throw new Error( |
| 186 | + `Found a SassCache version that's newer than supported. Please clear ${path} or upgrade Quarto to ${ |
| 187 | + requiredQuartoVersions[currentSassCacheVersion] |
| 188 | + } or later.`, |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +const _sassCache: Record<string, SassCache> = {}; |
| 195 | +export async function sassCache(path: string): Promise<SassCache> { |
| 196 | + if (!_sassCache[path]) { |
| 197 | + log.debug(`Creating SassCache at ${path}`); |
| 198 | + ensureDirSync(path); |
| 199 | + const kvFile = join(path, "sass.kv"); |
| 200 | + const kv = await Deno.openKv(kvFile); |
| 201 | + await checkVersion(kv, kvFile); |
| 202 | + _sassCache[path] = new SassCache(kv, path); |
| 203 | + } |
| 204 | + log.debug(`Returning SassCache at ${path}`); |
| 205 | + const result = _sassCache[path]; |
| 206 | + return result; |
| 207 | +} |
0 commit comments