|
9 | 9 | unlinkSync, |
10 | 10 | utimesSync, |
11 | 11 | } from "node:fs"; |
12 | | -import { basename, dirname, isAbsolute, join, relative, sep } from "node:path"; |
| 12 | +import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path"; |
13 | 13 | import { findFfBinary } from "@hyperframes/parsers/ff-binaries"; |
14 | 14 | import { probeMediaMetadata } from "./mediaMetadata.js"; |
15 | 15 | import { cleanupProxyCache } from "./proxyCache.js"; |
@@ -96,7 +96,7 @@ export class ProxyCapacityError extends ProxyTranscodeError { |
96 | 96 |
|
97 | 97 | export class ProxySourceOutsideProjectError extends ProxyTranscodeError { |
98 | 98 | constructor() { |
99 | | - super("media proxy source must be inside the project", null, ""); |
| 99 | + super("media proxy source must be addressed through the project", null, ""); |
100 | 100 | this.name = "ProxySourceOutsideProjectError"; |
101 | 101 | } |
102 | 102 | } |
@@ -129,41 +129,60 @@ export async function waitForProxy<T>( |
129 | 129 | } |
130 | 130 |
|
131 | 131 | /** |
132 | | - * Cache key inputs per the plan: source path relative to the project (so the |
133 | | - * cache is portable across checkouts at different absolute locations), mtime |
134 | | - * and file size (mtime alone can collide on same-second re-exports on |
135 | | - * coarse-timestamp filesystems; size catches nearly all such cases at zero |
136 | | - * cost), and a params version token so changing the ffmpeg recipe below |
137 | | - * invalidates every cached proxy cleanly. |
| 132 | + * Cache key inputs per the plan: project-relative source path (portable across |
| 133 | + * checkouts), canonical source identity (so retargeted external symlinks do |
| 134 | + * not reuse a proxy), mtime and file size (mtime alone can collide on |
| 135 | + * same-second re-exports on coarse-timestamp filesystems; size catches nearly |
| 136 | + * all such cases at zero cost), and a params version token so changing the |
| 137 | + * ffmpeg recipe below invalidates every cached proxy cleanly. |
138 | 138 | */ |
139 | 139 | type CanonicalProxySource = { |
140 | 140 | projectDir: string; |
141 | 141 | sourcePath: string; |
142 | 142 | relativePath: string; |
| 143 | + cacheIdentity: string; |
143 | 144 | }; |
144 | 145 |
|
145 | 146 | function canonicalizeProxySource( |
146 | 147 | projectDir: string, |
147 | 148 | absoluteSourcePath: string, |
148 | 149 | ): CanonicalProxySource { |
149 | | - const canonicalProjectDir = realpathSync(projectDir); |
150 | | - const canonicalSourcePath = realpathSync(absoluteSourcePath); |
151 | | - const relPath = relative(canonicalProjectDir, canonicalSourcePath); |
152 | | - if (relPath === ".." || relPath.startsWith(`..${sep}`) || isAbsolute(relPath)) { |
| 150 | + const requestedProjectDir = resolve(projectDir); |
| 151 | + const requestedSourcePath = resolve(absoluteSourcePath); |
| 152 | + const requestedRelativePath = relative(requestedProjectDir, requestedSourcePath); |
| 153 | + if ( |
| 154 | + requestedRelativePath === ".." || |
| 155 | + requestedRelativePath.startsWith(`..${sep}`) || |
| 156 | + isAbsolute(requestedRelativePath) |
| 157 | + ) { |
153 | 158 | throw new ProxySourceOutsideProjectError(); |
154 | 159 | } |
| 160 | + |
| 161 | + const canonicalProjectDir = realpathSync(projectDir); |
| 162 | + const canonicalSourcePath = realpathSync(absoluteSourcePath); |
| 163 | + const canonicalRelativePath = relative(canonicalProjectDir, canonicalSourcePath); |
| 164 | + const sourceIsInsideCanonicalProject = |
| 165 | + canonicalRelativePath !== ".." && |
| 166 | + !canonicalRelativePath.startsWith(`..${sep}`) && |
| 167 | + !isAbsolute(canonicalRelativePath); |
155 | 168 | return { |
156 | 169 | projectDir: canonicalProjectDir, |
157 | 170 | sourcePath: canonicalSourcePath, |
158 | | - relativePath: relPath.normalize("NFC"), |
| 171 | + relativePath: requestedRelativePath.normalize("NFC"), |
| 172 | + // An external target needs a stable identity in addition to its project-local |
| 173 | + // symlink path, otherwise retargeting the link can reuse an unrelated proxy. |
| 174 | + cacheIdentity: (sourceIsInsideCanonicalProject |
| 175 | + ? canonicalRelativePath |
| 176 | + : canonicalSourcePath |
| 177 | + ).normalize("NFC"), |
159 | 178 | }; |
160 | 179 | } |
161 | 180 |
|
162 | 181 | function buildProxyCacheKey(source: CanonicalProxySource, variant: ProxyVariant): string { |
163 | 182 | const stat = statSync(source.sourcePath); |
164 | 183 | return createHash("sha256") |
165 | 184 | .update( |
166 | | - `${source.relativePath}\0${stat.mtimeMs}\0${stat.size}\0${PROXY_PARAMS_VERSION}\0${variant}`, |
| 185 | + `${source.relativePath}\0${source.cacheIdentity}\0${stat.mtimeMs}\0${stat.size}\0${PROXY_PARAMS_VERSION}\0${variant}`, |
167 | 186 | ) |
168 | 187 | .digest("hex"); |
169 | 188 | } |
|
0 commit comments