guanghulab/native-repo/modules/step-3-hldp-write.js

78 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* ═══════════════════════════════════════
* HLDP-ZY://native-repo/module/step-3
* 光湖原生数据库 · 第3步 · HLDP写入模块
* ═══════════════════════════════════════
*
* @chain: D118 · 2026-05-31
* @module: hldp-write
* @step: 第3步 / 共8步
* @deploy: BS-SG-002 · 新加坡面孔
*
* @trigger:
* 第2步建立了 bare git 仓库。现在需要一个模块——
* 输入 HLDP 消息 → 自动 add+commit 到仓库。
* 这是整个原生数据库的核心——铸渊用 HLDP 写文件。
*
* @intent:
* 输入: { path, content, msg_type, intent, epoch, author }
* 流程: writeBlob → 更新 tree → commit → 返回 commit SHA
* 输出: { ok, sha, epoch }
*
* @lock:
* ⊢ commit message = HLDP 消息的 JSON 编码(不是纯文本)
* ⊢ 不需要分支策略——所有写入直接到 main
* ⊢ 不需要 PR/审核——铸渊是唯一写入者
* ⊢ 返回的 SHA 可用于 trace_path 定位
*
* @why:
* 现有 Forgejo 需要 Gitea API → Token → HTTP → 六层跳转
* 铸渊原生数据库HLDP 消息 → git.add → git.commit → 落盘
* 两层。不需要中间任何人的 API。
*
* ═══════════════════════════════════════
*/
const git = require('isomorphic-git');
const fs = require('fs');
const path = require('path');
const { NATIVE_DIR, GIT_DIR } = require('./step-2-git-init');
async function hldpWrite(hldpMsg) {
const { filePath, content, epoch, intent } = hldpMsg;
// 1. 把内容写入文件系统
const fullPath = path.join(NATIVE_DIR, filePath);
const dir = path.dirname(fullPath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(fullPath, content, 'utf8');
// 2. 用 HLDP 消息作为 commit message
const commitMsg = JSON.stringify({
hldp_v: '3.0',
msg_type: 'tree',
intent: intent || 'grow_leaf',
sender: { id: 'ICE-GL-ZY001', name: '铸渊', role: 'guardian' },
timestamp: new Date().toISOString(),
epoch: epoch || 'D118',
path: filePath,
});
// 3. add + commit
await git.add({ fs, dir: NATIVE_DIR, filepath: filePath });
const sha = await git.commit({
fs,
dir: NATIVE_DIR,
message: commitMsg,
author: {
name: '铸渊 ICE-GL-ZY001',
email: 'zhuyuan@guanghulab.com'
}
});
return { ok: true, sha, epoch: epoch || 'D118', path: filePath };
}
module.exports = { hldpWrite };