Open History to see the last commit
1import {builtinModules as nativeBuiltinModules} from 'node:module'
2import {createHash} from 'node:crypto'
3import {readFile,writeFile} from 'node:fs/promises'
4import {fileURLToPath} from 'node:url'
5import path from 'node:path'
6
7const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..')
8const artifactPath=path.join(root,'public/kernel-runtime/builtins.json')
9const jsonPath=path.join(root,'reports/node-api-surface.json')
10const markdownPath=path.join(root,'reports/node-api-surface.md')
11const warning='Export-name coverage only measures API shape. It does not prove that an export matches native Node behavior.'
12const sorted=values=>[...values].sort((a,b)=>a.localeCompare(b,'en'))
13const publicName=value=>{
14 const name=value.startsWith('node:')?value.slice(5):value
15 if(name==='sys'||name.startsWith('_')||name.startsWith('internal/'))return null
16 return name
17}
18
19export async function generateNodeAPISurface(){
20 const artifactBytes=await readFile(artifactPath)
21 const artifact=JSON.parse(artifactBytes.toString('utf8'))
22 const nativeModules=sorted(new Set(nativeBuiltinModules.map(publicName).filter(Boolean)))
23 const supportedModules=sorted(Object.keys(artifact.modules).map(publicName).filter(Boolean))
24 const nativeSet=new Set(nativeModules),supportedSet=new Set(supportedModules)
25 const modules=[]
26 for(const name of supportedModules){
27 const nativeExports=nativeSet.has(name)?sorted(Object.keys(await import('node:'+name))):[]
28 const runtimeExports=sorted(artifact.modules['node:'+name]?.exports??[])
29 const nativeExportSet=new Set(nativeExports),runtimeExportSet=new Set(runtimeExports)
30 modules.push({
31 module:'node:'+name,
32 present:runtimeExports.filter(value=>nativeExportSet.has(value)),
33 missing:nativeExports.filter(value=>!runtimeExportSet.has(value)),
34 extra:runtimeExports.filter(value=>!nativeExportSet.has(value)),
35 })
36 }
37 return {
38 schemaVersion:1,
39 nativeRuntime:process.version,
40 runtimeArtifactVersion:artifact.version,
41 runtimeArtifactSHA256:createHash('sha256').update(artifactBytes).digest('hex'),
42 warning,
43 nativePublicModuleCount:nativeModules.length,
44 supportedModuleCount:supportedModules.length,
45 missingPublicModules:nativeModules.filter(value=>!supportedSet.has(value)).map(value=>'node:'+value),
46 modules,
47 }
48}
49
50export function renderNodeAPISurface(report){
51 const names=values=>values.length?values.map(value=>'`'+value+'`').join(', '):'None'
52 const lines=[
53 '# Node API surface inventory','',
54 `Native control: ${report.nativeRuntime}`,'',
55 `Runtime artifact: ${report.runtimeArtifactVersion}`,'',
56 `Runtime artifact SHA-256: ${report.runtimeArtifactSHA256}`,'',
57 `Supported public modules: ${report.supportedModuleCount} of ${report.nativePublicModuleCount}`,'',
58 `**Warning:** ${report.warning}`,'',
59 '## Missing public modules','',
60 ...report.missingPublicModules.map(name=>'- `'+name+'`'),'',
61 '## Supported module exports','',
62 '| Module | Present exports | Missing exports | Extra exports |',
63 '| --- | --- | --- | --- |',
64 ...report.modules.map(row=>`| \`${row.module}\` | ${names(row.present)} | ${names(row.missing)} | ${names(row.extra)} |`),
65 '',
66 ]
67 return lines.join('\n')
68}
69
70export async function writeNodeAPISurface(){
71 const report=await generateNodeAPISurface()
72 await writeFile(jsonPath,JSON.stringify(report,null,2)+'\n')
73 await writeFile(markdownPath,renderNodeAPISurface(report))
74 return report
75}
76
77if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)){
78 const report=await writeNodeAPISurface()
79 console.log(JSON.stringify({nativeRuntime:report.nativeRuntime,supported:report.supportedModuleCount,missing:report.missingPublicModules.length}))
80}