53 lines
3.1 KiB
JavaScript
53 lines
3.1 KiB
JavaScript
'use strict'
|
||
// 存储入口收敛守卫(2026-11-06)
|
||
//
|
||
// 背景:项目已有统一封装(src/storage-local.js 的 IWBStorageLocal.getStore + StorageContract.localKeys),
|
||
// 但 src 模块曾各自裸调 localStorage ⇒ ① 配额满/隐私模式时静默丢写(不经过降级运行时)
|
||
// ② 42 个 key 硬编码分散 ③ storage-health 统计不到。
|
||
//
|
||
// 本守卫钉两件事:
|
||
// ① 直调(localStorage.getItem/setItem/removeItem)只允许出现在 storage-*.js(封装本体)
|
||
// ② 直调总数只降不升(棘轮 MAX_DIRECT)
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const PATTERN = /localStorage\.(?:get|set|remove)Item/g
|
||
const read = (p) => fs.readFileSync(path.join(root, p), 'utf8')
|
||
|
||
const files = ['app.js'].concat(
|
||
fs.readdirSync(path.join(root, 'src')).filter((n) => n.endsWith('.js')).map((n) => 'src/' + n)
|
||
)
|
||
let total = 0, appDirect = 0
|
||
const offenders = []
|
||
for (const f of files) {
|
||
const n = (read(f).match(PATTERN) || []).length
|
||
if (!n) continue
|
||
total += n
|
||
if (f === 'app.js') appDirect += n
|
||
const isWrapper = /(^|\/)storage-[^/]*\.js$/.test(f)
|
||
// app.js 是「装配层 + 历史遗留」:用独立棘轮逐步搬迁(见 MAX_APP_DIRECT)
|
||
if (!isWrapper && f !== 'app.js') offenders.push(f + ' × ' + n)
|
||
}
|
||
|
||
// ① 封装与 app.js 之外不得直调(src 模块必须走统一门面)
|
||
assert.deepStrictEqual(offenders, [], '存储直调只允许出现在 src/storage-*.js(封装本体)与 app.js(待迁移);违规:' + offenders.join(', '))
|
||
|
||
// ② 棘轮:只降不升(每迁移一批就把数字调小)
|
||
// 2026-11-17 桌面版:新增 src/storage-file.js(文件镜像层,属 storage-* 封装本体),
|
||
// 需要直调 localStorage 做「首次迁移 + 目录指针读取」,故总数 11 → 17。
|
||
// 守卫 ①(非 storage-* 的 src 模块禁止直调)不变。
|
||
const MAX_DIRECT = 17 // 2026-11-17 = src 12(含 storage-file 新增 6)+ app.js 5
|
||
const MAX_APP_DIRECT = 5 // app.js 仅剩 lsGet/lsSet/lsRemove/lsRawGet/lsRawSet 本体 5 处(这是封装,不是绕过)
|
||
assert.ok(total <= MAX_DIRECT, '存储直调总数不得增加(现 ' + total + ' / 上限 ' + MAX_DIRECT + ');新代码请用 IWBStorageLocal.getStore()')
|
||
assert.ok(appDirect <= MAX_APP_DIRECT, 'app.js 直调不得增加(现 ' + appDirect + ' / 上限 ' + MAX_APP_DIRECT + ')')
|
||
|
||
// ③ 门面必须存在且被 src 模块使用(防止"迁移了但没人用")
|
||
const localSrc = read('src/storage-local.js')
|
||
assert.ok(/global\.IWBStorageLocal = \{ create, friendly, getStore \}/.test(localSrc), 'storage-local 必须导出 friendly / getStore 门面')
|
||
const usingModules = files.filter((f) => f !== 'src/storage-local.js' && /IWBStorageLocal\.getStore\(\)/.test(read(f)))
|
||
assert.ok(usingModules.length >= 5, '至少 5 个模块应改用统一门面(现 ' + usingModules.length + ':' + usingModules.join(', ') + ')')
|
||
|
||
console.log('storage convergence checks passed(直调 ' + total + ' 处,全部在封装本体;' + usingModules.length + ' 个模块走统一门面)')
|