MCPcopy Create free account
hub / github.com/observablehq/framework / getModuleInfo

Function getModuleInfo

src/javascript/module.ts:125–179  ·  view source on GitHub ↗
(root: string, path: string)

Source from the content-addressed store, hash-verified

123 * source root, or undefined if the module does not exist or has invalid syntax.
124 */
125export function getModuleInfo(root: string, path: string): ModuleInfo | undefined {
126 const module = findModule(root, path);
127 if (!module) return; // TODO delete stale entry?
128 const key = join(root, module.path);
129 let mtimeMs: number;
130 try {
131 mtimeMs = Math.floor(statSync(key).mtimeMs);
132 } catch {
133 moduleInfoCache.delete(key); // delete stale entry
134 return; // ignore missing file
135 }
136 let info = moduleInfoCache.get(key);
137 if (!info || info.mtimeMs < mtimeMs) {
138 let source: string;
139 let body: Program;
140 try {
141 source = readJavaScriptSync(key);
142 body = parseProgram(source, module.params);
143 } catch {
144 moduleInfoCache.delete(key); // delete stale entry
145 return; // ignore parse error
146 }
147 const hash = createHash("sha256").update(source).digest("hex");
148 const imports = findImports(body, path, source);
149 const files = findFiles(body, path, source);
150 const localStaticImports = new Set<string>();
151 const localDynamicImports = new Set<string>();
152 const globalStaticImports = new Set<string>();
153 const globalDynamicImports = new Set<string>();
154 for (const i of imports) {
155 (i.type === "local"
156 ? i.method === "static"
157 ? localStaticImports
158 : localDynamicImports
159 : i.method === "static"
160 ? globalStaticImports
161 : globalDynamicImports
162 ).add(i.name);
163 }
164 moduleInfoCache.set(
165 key,
166 (info = {
167 mtimeMs,
168 hash,
169 files: new Set(files.map((f) => f.name)),
170 fileMethods: new Set(files.map((f) => f.method).filter((m): m is string => m !== undefined)),
171 localStaticImports,
172 localDynamicImports,
173 globalStaticImports,
174 globalDynamicImports
175 })
176 );
177 }
178 return info;
179}
180
181/**
182 * Returns the content hash for the specified file within the source root. If

Callers 6

module-test.tsFile · 0.85
redactModuleInfoFunction · 0.85
resolveResolversFunction · 0.85
getModuleStaticImportsFunction · 0.85
getModuleHashInternalFunction · 0.85
getLocalModuleHashFunction · 0.85

Calls 6

findModuleFunction · 0.85
readJavaScriptSyncFunction · 0.85
parseProgramFunction · 0.85
findImportsFunction · 0.85
deleteMethod · 0.80
findFilesFunction · 0.70

Tested by

no test coverage detected