|
| 1 | +import crypto from "crypto"; |
| 2 | +import fs from "fs/promises"; |
| 3 | +import path from "path"; |
| 4 | +import { fileURLToPath, pathToFileURL } from "url"; |
| 5 | +import type { RawSourceMap } from "source-map"; |
| 6 | +import { Response } from "../../http"; |
| 7 | +import { Log } from "../../shared"; |
| 8 | + |
| 9 | +function maybeParseURL(url: string): URL | undefined { |
| 10 | + if (path.isAbsolute(url)) return; |
| 11 | + try { |
| 12 | + return new URL(url); |
| 13 | + } catch {} |
| 14 | +} |
| 15 | + |
| 16 | +export class SourceMapRegistry { |
| 17 | + static PATHNAME_PREFIX = "/core/source-map/"; |
| 18 | + |
| 19 | + constructor( |
| 20 | + private readonly log: Log, |
| 21 | + private readonly loopbackPort: number |
| 22 | + ) {} |
| 23 | + |
| 24 | + readonly #map = new Map<string /* id */, string /* sourceMapPath */>(); |
| 25 | + |
| 26 | + register(script: string, scriptPath: string): string /* newScript */ { |
| 27 | + // Try to find the last source mapping URL in the file, if none could be |
| 28 | + // found, return the script as is |
| 29 | + const mappingURLIndex = script.lastIndexOf("//# sourceMappingURL="); |
| 30 | + if (mappingURLIndex === -1) return script; |
| 31 | + |
| 32 | + // `pathToFileURL()` will resolve `scriptPath` relative to the current |
| 33 | + // working directory if needed |
| 34 | + const scriptURL = pathToFileURL(scriptPath); |
| 35 | + |
| 36 | + const sourceSegment = script.substring(0, mappingURLIndex); |
| 37 | + const mappingURLSegment = script |
| 38 | + .substring(mappingURLIndex) |
| 39 | + .replace(/^\/\/# sourceMappingURL=(.+)/, (substring, mappingURL) => { |
| 40 | + // If the mapping URL is already a URL (e.g. `data:`), return it as is |
| 41 | + if (maybeParseURL(mappingURL) !== undefined) return substring; |
| 42 | + |
| 43 | + // Otherwise, resolve it relative to the script, and register it |
| 44 | + const resolvedMappingURL = new URL(mappingURL, scriptURL); |
| 45 | + const resolvedMappingPath = fileURLToPath(resolvedMappingURL); |
| 46 | + |
| 47 | + // We intentionally register source maps in a map to prevent arbitrary |
| 48 | + // file access via the loopback server. |
| 49 | + const id = crypto.randomUUID(); |
| 50 | + this.#map.set(id, resolvedMappingPath); |
| 51 | + mappingURL = `http://localhost:${this.loopbackPort}${SourceMapRegistry.PATHNAME_PREFIX}${id}`; |
| 52 | + |
| 53 | + this.log.verbose( |
| 54 | + `Registered source map ${JSON.stringify( |
| 55 | + resolvedMappingPath |
| 56 | + )} at ${mappingURL}` |
| 57 | + ); |
| 58 | + |
| 59 | + return `//# sourceMappingURL=${mappingURL}`; |
| 60 | + }); |
| 61 | + |
| 62 | + return sourceSegment + mappingURLSegment; |
| 63 | + } |
| 64 | + |
| 65 | + async get(url: URL): Promise<Response | undefined> { |
| 66 | + // Try to get source map from registry |
| 67 | + const id = url.pathname.substring(SourceMapRegistry.PATHNAME_PREFIX.length); |
| 68 | + const sourceMapPath = this.#map.get(id); |
| 69 | + if (sourceMapPath === undefined) return; |
| 70 | + |
| 71 | + // Try to load and parse source map from disk |
| 72 | + let contents: string; |
| 73 | + try { |
| 74 | + contents = await fs.readFile(sourceMapPath, "utf8"); |
| 75 | + } catch (e) { |
| 76 | + this.log.warn( |
| 77 | + `Error reading source map ${JSON.stringify(sourceMapPath)}: ${e}` |
| 78 | + ); |
| 79 | + return; |
| 80 | + } |
| 81 | + let map: RawSourceMap; |
| 82 | + try { |
| 83 | + map = JSON.parse(contents); |
| 84 | + } catch (e) { |
| 85 | + this.log.warn( |
| 86 | + `Error parsing source map ${JSON.stringify(sourceMapPath)}: ${e}` |
| 87 | + ); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + // Modify the `sourceRoot` so source files get the correct paths. Note, |
| 92 | + // `sourceMapPath` will always be an absolute path. |
| 93 | + const sourceMapDir = path.dirname(sourceMapPath); |
| 94 | + map.sourceRoot = |
| 95 | + map.sourceRoot === undefined |
| 96 | + ? sourceMapDir |
| 97 | + : path.resolve(sourceMapDir, map.sourceRoot); |
| 98 | + |
| 99 | + return Response.json(map, { |
| 100 | + // This source map will be served from the loopback server to DevTools, |
| 101 | + // which will likely be on a different origin. |
| 102 | + headers: { "Access-Control-Allow-Origin": "*" }, |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments