Skill Tracer
Skills › Archify › archify › revision.mjs

revision.mjs

20 lines · archify/explainer/revision.mjs at a406594

1import { createHash } from 'node:crypto';
2const hash = text => createHash('sha256').update(text).digest('hex');
3
4/** Stable authored-content identity; excludes filesystem relocation and outer reader presentation. */
5export function computeExplainerRevision({document,sourceText=JSON.stringify(document),preparedSources={},preparedDiagrams={},dependencies}) {
6 const sourceSha256 = hash(sourceText);
7 const sources = preparedSources.sources || preparedSources;
8 const assets = preparedSources.assets || {};
9 const diagrams = preparedDiagrams.diagrams || preparedDiagrams;
10 const stable = value => Array.isArray(value) ? value.map(stable) : value && typeof value === 'object' ? Object.fromEntries(Object.entries(value).filter(([,entry])=>entry !== undefined).sort(([a],[b])=>a < b ? -1 : a > b ? 1 : 0).map(([key,entry])=>[key,stable(entry)])) : value;
11 const ordered = entries => entries.map(stable).sort((a,b)=>JSON.stringify(a) < JSON.stringify(b) ? -1 : JSON.stringify(a) > JSON.stringify(b) ? 1 : 0);
12 const resourceIdentity = {
13 version:1,sourceSha256,documentSha256:hash(JSON.stringify(stable(document))),
14 dependencies:ordered((dependencies || [...(preparedSources.dependencies || []),...(preparedDiagrams.dependencies || [])]).map(({kind,id,sha256,bytes})=>({kind,id,sha256,bytes}))),
15 sources:Object.fromEntries(Object.entries(sources).map(([id,source])=>[id,{title:source.title,url:source.url,path:source.path,lines:source.lines,revision:source.revision,textSha256:typeof source.text === 'string' ? hash(source.text) : undefined}])),
16 children:Object.fromEntries(Object.entries(diagrams).map(([id,child])=>[id,{htmlSha256:hash(child.html),specSha256:hash(JSON.stringify(stable(child.spec || {})))}])),
17 assets:Object.fromEntries(Object.entries(assets).map(([id,asset])=>[id,{sha256:asset.sha256,dataSha256:hash(asset.dataUrl || ''),mime:asset.mime,bytes:asset.bytes}]))
18 };
19 return hash(JSON.stringify(stable(resourceIdentity)));