The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as inspector from "node:inspector"; |
| 2 | import * as NodeModule from "node:module"; |
| 3 | import { pathToFileURL } from "node:url"; |
| 4 | import type { transformSync } from "rolldown/utils"; |
| 5 | |
| 6 | export type SourceMap = NonNullable<ReturnType<typeof transformSync>["map"]>; |
| 7 | |
| 8 | /** |
| 9 | * The map as stored in the cache: `sources` names the file by URL and |
| 10 | * `sourcesContent` is dropped. A bare path in `sources` is resolved against |
| 11 | * the map's own location — the shared cache directory — whereas a URL is |
| 12 | * location-independent and correct on Windows too. Every source is a file |
| 13 | * on this machine, so embedding its text only makes the map larger than |
| 14 | * the code it describes and every process that loads the module pay for it. |
| 15 | */ |
| 16 | export const storedSourceMap = ( |
| 17 | { sourcesContent: _sourcesContent, ...map }: SourceMap, |
| 18 | filePath: string, |
| 19 | ): string => JSON.stringify({ ...map, sources: [pathToFileURL(filePath).href] }); |
| 20 | |
| 21 | /** Where a module's source map is, and how to get its text if needed. */ |
| 22 | export interface SourceMapRef { |
| 23 | /** Absolute path of the map on disk, if it has been written there. */ |
| 24 | readonly file: string | undefined; |
| 25 | readonly text: () => string; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Whether a debugger can be attached to this process. Checked per module, so |
| 30 | * an inspector opened after startup (VS Code auto-attach, `SIGUSR1`, |
| 31 | * `inspector.open()`) covers every module loaded from then on. |
| 32 | */ |
| 33 | const isInspectorActive = () => inspector.url() !== undefined; |
| 34 | |
| 35 | const inlineComment = (map: string) => |
| 36 | `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(map).toString("base64")}`; |
| 37 | |
| 38 | /** |
| 39 | * Map by reference. Node's source-map support only understands `data:` |
| 40 | * URLs and scheme-less paths (it resolves the latter against the module |
| 41 | * URL and reads the file), so this is the file URL's path component — |
| 42 | * `/var/…/x.map` on POSIX, `/C:/…/x.map` on Windows — never a `file:` URL. |
| 43 | */ |
| 44 | const fileComment = (mapFile: string) => |
| 45 | `\n//# sourceMappingURL=${pathToFileURL(mapFile).pathname}`; |
| 46 | |
| 47 | /** |
| 48 | * `code` with its source map attached. |
| 49 | * |
| 50 | * Normally by reference to the file on disk: an inline `data:` map is part |
| 51 | * of the script source V8 keeps for the process lifetime, which for a graph |
| 52 | * the size of alchemy's is hundreds of megabytes. Inlined when there is no |
| 53 | * file, and — with the source embedded — while a debugger is attached: a |
| 54 | * map in the shared cache directory is one debuggers either refuse to read |
| 55 | * (VS Code only loads maps under the workspace by default) or cannot fetch |
| 56 | * over the inspector protocol (DevTools), leaving every module to show up |
| 57 | * as transpiled output from a foreign folder. |
| 58 | */ |
| 59 | export const attachSourceMap = (code: string, map: SourceMapRef, readSource: () => string) => { |
| 60 | if (isInspectorActive()) { |
| 61 | return ( |
| 62 | code + |
| 63 | inlineComment(JSON.stringify({ ...JSON.parse(map.text()), sourcesContent: [readSource()] })) |
| 64 | ); |
| 65 | } |
| 66 | return code + (map.file === undefined ? inlineComment(map.text()) : fileComment(map.file)); |
| 67 | }; |
| 68 | |
| 69 | const supportKey = Symbol.for("@alchemy.run/node-utils/source-map-support"); |
| 70 | |
| 71 | interface SourceMapSupportLease { |
| 72 | holders: number; |
| 73 | readonly previous: ReturnType<typeof NodeModule.getSourceMapsSupport>; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Turns Node's source-map support on for as long as any holder of a lease |
| 78 | * keeps it, and restores the previous setting once the last one releases |
| 79 | * theirs. Transformed sources only reference their maps; Node reads and |
| 80 | * applies them to stack traces when this is on. `nodeModules` stays on: a |
| 81 | * published alchemy runs its own `lib/` from `node_modules`, and ships maps |
| 82 | * back to its `src/`. Leases are counted on `globalThis` because a checkout |
| 83 | * can load this module twice (src/ and lib/). |
| 84 | */ |
| 85 | export const leaseSourceMapSupport = (): (() => void) => { |
| 86 | const leases = globalThis as typeof globalThis & { [supportKey]?: SourceMapSupportLease }; |
| 87 | let lease = leases[supportKey]; |
| 88 | if (lease === undefined) { |
| 89 | const previous = NodeModule.getSourceMapsSupport(); |
| 90 | NodeModule.setSourceMapsSupport(true, { |
| 91 | nodeModules: true, |
| 92 | generatedCode: previous.generatedCode, |
| 93 | }); |
| 94 | lease = leases[supportKey] = { holders: 0, previous }; |
| 95 | } |
| 96 | lease.holders++; |
| 97 | let released = false; |
| 98 | return () => { |
| 99 | if (released) return; |
| 100 | released = true; |
| 101 | if (--lease.holders > 0) return; |
| 102 | delete leases[supportKey]; |
| 103 | const { enabled, ...options } = lease.previous; |
| 104 | NodeModule.setSourceMapsSupport(enabled, options); |
| 105 | }; |
| 106 | }; |