dsh-redteam-memory 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ // 生成 lib/host.js 与 lib/client.js。
2
+ //
3
+ // 为什么要生成而不是手写:src/host.js 与 src/client.js 是权威实现,lib/ 只是它们在
4
+ // 【常驻插件包】形态下的包装。手抄两份必然漂移;生成保证「改 src → 跑一次 npm run build:lib」即同步。
5
+ //
6
+ // 两半边的包装差异都集中在垫片里,主体逻辑逐字不动:
7
+ // host : harness.defineTool/registerTool/handle -> defineTool / ctx.tools.register / HTTP 路由
8
+ // client : host.call(闭包符号)-> fetch 到宿主路由;styles.insert(闭包符号)-> 自插 <style>
9
+ //
10
+ // 用法: node tools/build-lib.mjs [--check]
11
+ // --check 只比对、不写入,不一致时非零退出(可用于 CI)
12
+
13
+ import fs from 'node:fs'
14
+ import path from 'node:path'
15
+
16
+ const root = path.join(import.meta.dirname, '..')
17
+ const read = (p) => fs.readFileSync(path.join(root, p), 'utf8')
18
+ const check = process.argv.includes('--check')
19
+
20
+ // 插件名从包自身推导:pkg 名去掉 'dsh-' 前缀即界面/工具标识,
21
+ // 这样生成器可被任意包复用,不必逐包改字符串。
22
+ const PKG = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
23
+ const PKG_NAME = PKG.name
24
+ const PLUGIN_NAME = PKG_NAME.replace(/^dsh-/, '')
25
+ // RPC 路由挂在包命名空间下,避免与其它插件的路由相撞。
26
+ const ROUTE_BASE = '/' + PKG_NAME
27
+
28
+ const CLIENT_WRAPPER = "\nreturn {\n name: '" + PLUGIN_NAME + "',\n inject: ['slots', 'timer'],\n apply: applyClient\n}\n"
29
+
30
+ function strip(source, wrapper, file) {
31
+ if (!source.endsWith(wrapper)) {
32
+ console.error(`build-lib: ${file} 结尾与预期不符,无法剥离动态包装`)
33
+ process.exit(1)
34
+ }
35
+ return source.slice(0, -wrapper.length)
36
+ }
37
+
38
+ // 模板里的占位符按包替换(模板因此可跨包复用)
39
+ function fill(text) {
40
+ return text
41
+ .split('__PKG_NAME__').join(PKG_NAME)
42
+ .split('__PLUGIN_NAME__').join(PLUGIN_NAME)
43
+ .split('__ROUTE_BASE__').join(ROUTE_BASE)
44
+ }
45
+
46
+ // ── Host 半边 ────────────────────────────────────────────────────────────────
47
+ const hostHead = read('lib/parts/host.head.js')
48
+ const hostTail = read('lib/parts/host.tail.js')
49
+ // 本插件的 src/host.js 只写 applyHost 的函数体(函数头与收尾由 lib/parts 提供),
50
+ // 因此这里不做「剥离动态包装」——那个包装是 asset-graph 那种内联插件对象才有的。
51
+ let hostOut = fill(hostHead + read('src/host.js') + hostTail)
52
+
53
+ // ── Client 半边 ──────────────────────────────────────────────────────────────
54
+ // 垫片注入到主体自己的 applyClient 开头,包装保持极薄(只做作用域与导出)。
55
+ const clientHead = read('lib/parts/client.head.js')
56
+ const clientShim = read('lib/parts/client.shim.js')
57
+ const clientTail = read('lib/parts/client.tail.js')
58
+ const ANCHOR = 'function applyClient(ctx) {\n const slots = ctx.slots\n'
59
+ let clientBody = strip(read('src/client.js'), CLIENT_WRAPPER, 'src/client.js')
60
+ if (clientBody.split(ANCHOR).length !== 2) {
61
+ console.error('build-lib: src/client.js 中未找到唯一的 applyClient 入口锚点')
62
+ process.exit(1)
63
+ }
64
+ clientBody = clientBody.replace(ANCHOR, clientShim + ' const slots = ctx.slots\n')
65
+ let clientOut = fill(clientHead + clientBody + clientTail)
66
+
67
+
68
+
69
+ const targets = [
70
+ ['lib/host.js', hostOut],
71
+ ['lib/client.js', clientOut],
72
+ ]
73
+
74
+ let bad = 0
75
+ for (const [rel, content] of targets) {
76
+ const abs = path.join(root, rel)
77
+ if (check) {
78
+ const current = fs.existsSync(abs) ? fs.readFileSync(abs, 'utf8') : ''
79
+ if (current !== content) { console.error(`build-lib --check: ${rel} 与 src/ 不同步`); bad++ }
80
+ else console.log(`build-lib --check: ${rel} 同步`)
81
+ } else {
82
+ fs.writeFileSync(abs, content, 'utf8')
83
+ console.log(`build-lib: 已生成 ${rel}(${Buffer.byteLength(content)} 字节)`)
84
+ }
85
+ }
86
+ process.exit(bad === 0 ? 0 : 1)
@@ -0,0 +1,66 @@
1
+ // 为 GitHub Packages 生成发布副本。
2
+ //
3
+ // 为什么需要这个脚本:GitHub Packages 要求包名是作用域形式,且作用域必须等于仓库属主
4
+ // (这里 owner = Fasthei,故只能叫 @fasthei/…)。而 npmjs 不允许发布不属于自己的 scope,
5
+ // 因此同一个包在这两个 registry 上必须是**两个名字**:
6
+ //
7
+ // npmjs -> dsh-redteam-asset-graph (未作用域,公开、可被搜索)
8
+ // GitHub Packages -> @fasthei/dsh-redteam-asset-graph (作用域名,需属主)
9
+ //
10
+ // 与其手抄两份 package.json(必然漂移),不如由本脚本从唯一的 package.json 生成副本,
11
+ // 只替换 name 与 publishConfig:其余字段(dsh.bundle/client/dynamic、exports、files…)
12
+ // 逐字继承,保证两个 registry 上的包行为一致。
13
+ //
14
+ // 用法:
15
+ // node tools/prepare-gh-packages.mjs # 生成到 build/gh-packages/
16
+ // 然后在 build/gh-packages/ 下执行 npm publish
17
+
18
+ import fs from 'node:fs'
19
+ import path from 'node:path'
20
+
21
+ const root = path.join(import.meta.dirname, '..')
22
+ const OUT = path.join(root, 'build', 'gh-packages')
23
+
24
+ const OWNER = 'fasthei'
25
+ const GH_SCOPE = '@' + OWNER
26
+
27
+ const base = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
28
+ const baseName = base.name
29
+ const scopedName = `${GH_SCOPE}/${baseName}`
30
+
31
+ const manifest = {
32
+ ...base,
33
+ name: scopedName,
34
+ publishConfig: {
35
+ registry: 'https://npm.pkg.github.com',
36
+ // 作用域包在 GitHub Packages 上默认是 private;显式声明 public 才能被他人安装。
37
+ access: 'public',
38
+ },
39
+ }
40
+
41
+ // 递归复制发布所需内容(只复制 files 白名单 + 清单),与 npm pack 的口径一致。
42
+ function copyListed(rel) {
43
+ const src = path.join(root, rel)
44
+ const dst = path.join(OUT, rel)
45
+ if (!fs.existsSync(src)) {
46
+ console.warn(`prepare-gh-packages: 跳过缺失项 ${rel}`)
47
+ return
48
+ }
49
+ const st = fs.statSync(src)
50
+ if (st.isDirectory()) {
51
+ fs.mkdirSync(dst, { recursive: true })
52
+ for (const entry of fs.readdirSync(src)) copyListed(path.join(rel, entry))
53
+ } else {
54
+ fs.mkdirSync(path.dirname(dst), { recursive: true })
55
+ fs.copyFileSync(src, dst)
56
+ }
57
+ }
58
+
59
+ fs.rmSync(OUT, { recursive: true, force: true })
60
+ fs.mkdirSync(OUT, { recursive: true })
61
+ for (const rel of base.files ?? []) copyListed(rel)
62
+ fs.writeFileSync(path.join(OUT, 'package.json'), JSON.stringify(manifest, null, 2) + '\n', 'utf8')
63
+
64
+ console.log(`prepare-gh-packages: ${baseName}@${base.version} -> ${scopedName}`)
65
+ console.log(`prepare-gh-packages: registry = https://npm.pkg.github.com`)
66
+ console.log(`prepare-gh-packages: 输出目录 = ${path.relative(root, OUT)}`)