dsh-design-studio 0.17.0 → 0.19.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.
- package/lib/canvthumb.js +278 -0
- package/lib/client.js +477 -60
- package/lib/core.js +3 -1
- package/lib/gallery/routes.js +53 -4
- package/lib/gallery/submit.js +288 -0
- package/lib/index.js +182 -17
- package/lib/serve.js +16 -3
- package/lib/works.js +12 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -62,6 +62,7 @@ import { createPhone } from './phone.js'
|
|
|
62
62
|
import { qrMatrix } from './qr.js'
|
|
63
63
|
import { makePdf, makePptx, jpegInfo } from './doc.js'
|
|
64
64
|
import { createBrowser } from './browser.js'
|
|
65
|
+
import { createCanvasThumbs, createDirRevs } from './canvthumb.js'
|
|
65
66
|
import { designOf, fitSize, metaOfFile, sizeOf, pickScreens, measureAll, shootAll, cropIssues, formatMeasure, issueSnapshot, formatShots, whereOf, MAX_SCREENS, MAX_SIZES, MAX_SHOTS, MAX_CROPS } from './look.js'
|
|
66
67
|
import { SYSTEMS_DIR, extractSystem, listSystems, setDefault, skillSection } from './system.js'
|
|
67
68
|
import { readTweaks, applyTweaks, lintTweaks } from './tweaks.js'
|
|
@@ -790,6 +791,7 @@ export function apply(ctx) {
|
|
|
790
791
|
// 「设计库不联网」那行开着:市场不访问任何网址
|
|
791
792
|
const market = createMarket({ version: VERSION, log, seedDir: fs.existsSync(path.join(seedDir, 'index.json')) ? seedDir : '', online: () => studio.offline === 0 })
|
|
792
793
|
const drafts = createDrafts({ log }) // 正在写的页面(边写边看):tracker 往里推,预览服务器往外吐
|
|
794
|
+
const thumbDirs = createDirRevs() // 画布缩略图(issue #17):各目录的资源修订号,预览服务器报改动时更新
|
|
793
795
|
const hub = createHub({
|
|
794
796
|
log,
|
|
795
797
|
history,
|
|
@@ -804,8 +806,13 @@ export function apply(ctx) {
|
|
|
804
806
|
return [...own, ...trustedOrigins(conn && conn.trustedHosts)]
|
|
805
807
|
},
|
|
806
808
|
onChange: (key, file, files) => {
|
|
807
|
-
if (
|
|
809
|
+
if (!key.startsWith('file:')) return
|
|
810
|
+
dropBundlesOf(key.slice(5))
|
|
811
|
+
thumbDirs.touched(key.slice(5)) // 先于 tracker:页面看到改动再来要缩略图时,目录的修订号已经变了
|
|
812
|
+
if (tracker) tracker.changed(key.slice(5), files)
|
|
808
813
|
},
|
|
814
|
+
onWatch: (key) => { if (key.startsWith('file:')) thumbDirs.watched(key.slice(5)) },
|
|
815
|
+
onUnwatch: (key) => { if (key.startsWith('file:')) thumbDirs.unwatched(key.slice(5)) },
|
|
809
816
|
})
|
|
810
817
|
const open = async (target, opts) => {
|
|
811
818
|
const r = await hub.open(target.kind === 'file' ? { kind: 'file', root: target.root } : target, opts)
|
|
@@ -814,6 +821,7 @@ export function apply(ctx) {
|
|
|
814
821
|
}
|
|
815
822
|
tracker = createTracker({
|
|
816
823
|
open,
|
|
824
|
+
watchTag: (root) => thumbDirs.tag(root),
|
|
817
825
|
drafts,
|
|
818
826
|
log,
|
|
819
827
|
history,
|
|
@@ -823,6 +831,7 @@ export function apply(ctx) {
|
|
|
823
831
|
autoOpen: () => studio.autoOpen > 0,
|
|
824
832
|
// 装好的别人的设计系统退回旧版:退回来的页面先记成别人做的(预览照样加规矩)
|
|
825
833
|
beforeRestore: (root, html) => { if (html.length) library.addPast(root, html) },
|
|
834
|
+
trust: () => { try { return library.trustRev() } catch (e) { log(e); return '' } },
|
|
826
835
|
skipPorts: () => {
|
|
827
836
|
const s = new Set(previewPorts)
|
|
828
837
|
const own = ownHost(ctx).split(':')[1]
|
|
@@ -1150,6 +1159,104 @@ export function apply(ctx) {
|
|
|
1150
1159
|
json(res, 200, { items: locateRules(tracker.stateOf(String(body.session || '')), items) })
|
|
1151
1160
|
}
|
|
1152
1161
|
|
|
1162
|
+
// ── 远程看的打包缓存 ──────────────────────────────────────────────────────
|
|
1163
|
+
// 一屏连同样式、图片打成一整份很贵(3MB 的屏要几十毫秒,而且同步阻塞事件循环),但只要这一屏
|
|
1164
|
+
// 用到的文件一个都没动,结果就完全一样。按「每个文件的 mtime + 大小 + inode」判断上次那份还能不能用;
|
|
1165
|
+
// 能用就直接给,并带上 ETag:浏览器带 If-None-Match 过来时回 304,body 一个字节都不传。
|
|
1166
|
+
// 远程访问的主要成本就在这份 body 上——画布一次要 N 份,切 单页 ↔ 画布、关掉侧栏再开都要再来一遍。
|
|
1167
|
+
const bundleCache = new Map() // `${session}|${root}|${v}|${key}` → { etag, root, href, parent, v, picker, body, stamps, bytes }
|
|
1168
|
+
let bundleBytes = 0
|
|
1169
|
+
const BUNDLE_CACHE_BYTES = 64 * 1024 * 1024 // 存着的字符串总量上限,超了按 LRU 丢
|
|
1170
|
+
const BUNDLE_CACHE_MAX = 64
|
|
1171
|
+
const BUNDLE_CACHE_ONE = 24 * 1024 * 1024 // 单份太大(图多)就不进缓存,免得一份把整池占满
|
|
1172
|
+
|
|
1173
|
+
function dropBundle(name) {
|
|
1174
|
+
const e = bundleCache.get(name)
|
|
1175
|
+
if (!e) return
|
|
1176
|
+
bundleBytes -= e.bytes
|
|
1177
|
+
bundleCache.delete(name)
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* 目录里有改动(预览服务器的监听报上来的):这个目录的打包结果全不要了。
|
|
1182
|
+
* 印记看不出来的改动(保留 mtime 的覆盖、粗精度盘上同一刻度里改两次、大小没变)靠这个兜住
|
|
1183
|
+
*/
|
|
1184
|
+
function dropBundlesOf(root) {
|
|
1185
|
+
const r = path.resolve(root)
|
|
1186
|
+
for (const [name, e] of bundleCache) if (e.root === r) dropBundle(name)
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
function keepBundle(name, entry) {
|
|
1190
|
+
dropBundle(name)
|
|
1191
|
+
bundleCache.set(name, entry)
|
|
1192
|
+
bundleBytes += entry.bytes
|
|
1193
|
+
while (bundleCache.size > BUNDLE_CACHE_MAX || bundleBytes > BUNDLE_CACHE_BYTES) {
|
|
1194
|
+
const oldest = bundleCache.keys().next()
|
|
1195
|
+
if (oldest.done) break
|
|
1196
|
+
dropBundle(oldest.value)
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
/**
|
|
1201
|
+
* 一个文件现在的样子。inode 也看:编辑器常写临时文件再换名,mtime 可能没变但文件已经换了。
|
|
1202
|
+
* 读不到记 '-',后来才建出来的文件会让判断从 '-' 变成新值,缓存自然失效。
|
|
1203
|
+
*/
|
|
1204
|
+
function stampOf(root, v, rel) {
|
|
1205
|
+
try {
|
|
1206
|
+
const f = bundleFile(root, v, rel)
|
|
1207
|
+
if (!f) return '-'
|
|
1208
|
+
const s = fs.statSync(f)
|
|
1209
|
+
return `${s.mtimeMs}:${s.size}:${s.ino}`
|
|
1210
|
+
} catch {
|
|
1211
|
+
return '-'
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* 打包时这个文件从哪读。打包(read)、印记(stampOf)、命中时重算 strict 都走这一套:
|
|
1217
|
+
* 旧版本里没有这个文件时,都退回目录里的当前文件,不然退回的那份改了、缓存却不会失效
|
|
1218
|
+
*/
|
|
1219
|
+
function bundleFile(root, v, rel) {
|
|
1220
|
+
let f = ''
|
|
1221
|
+
if (v) f = history.fileOf(root, v, rel) || ''
|
|
1222
|
+
if (!f) {
|
|
1223
|
+
const hit = resolveInRoot(root, '/' + rel.split('/').map(encodeURIComponent).join('/'))
|
|
1224
|
+
f = hit && hit.real ? hit.real : ''
|
|
1225
|
+
}
|
|
1226
|
+
return f
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
/** picker.js 的指纹:它被内联进每一份包里,换了就不能再用留着的包 */
|
|
1230
|
+
function pickerStamp() {
|
|
1231
|
+
try {
|
|
1232
|
+
const s = fs.statSync(PICKER_FILE)
|
|
1233
|
+
return `${s.size}:${Math.round(s.mtimeMs)}`
|
|
1234
|
+
} catch {
|
|
1235
|
+
return '-'
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
/**
|
|
1240
|
+
* 交打包结果:浏览器手上那份就是这份(ETag 按内容算,对上了内容就一样)就 304,让它用自己缓存的。
|
|
1241
|
+
*/
|
|
1242
|
+
function sendBundle(req, res, entry) {
|
|
1243
|
+
const headers = {
|
|
1244
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
1245
|
+
'Cache-Control': 'private, max-age=0, must-revalidate',
|
|
1246
|
+
ETag: entry.etag,
|
|
1247
|
+
}
|
|
1248
|
+
// 反代可能压缩过:同一份内容的字节表示不止一种,所以是弱校验(W/)
|
|
1249
|
+
const inm = String(req.headers['if-none-match'] || '')
|
|
1250
|
+
if (inm && inm.split(',').some((x) => x.trim() === entry.etag)) {
|
|
1251
|
+
res.writeHead(304, headers)
|
|
1252
|
+
res.end()
|
|
1253
|
+
return
|
|
1254
|
+
}
|
|
1255
|
+
headers['Content-Length'] = Buffer.byteLength(entry.body)
|
|
1256
|
+
res.writeHead(200, headers)
|
|
1257
|
+
res.end(entry.body)
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1153
1260
|
/**
|
|
1154
1261
|
* 远程看(手机走外网 / 别的电脑打开 DSH 网页,连不上本机的预览端口):这一屏连同样式、图片打成一份 HTML(见 bundle.js),
|
|
1155
1262
|
* 右侧栏放进沙盒 iframe。回 JSON(不直接给 HTML:在 DSH 的地址下直接打开时,页面脚本不能拿 DSH 的源跑)。
|
|
@@ -1166,33 +1273,80 @@ export function apply(ctx) {
|
|
|
1166
1273
|
const v = Math.max(0, Math.floor(Number(q.get('v')) || 0))
|
|
1167
1274
|
let parent = ''
|
|
1168
1275
|
try { const u = new URL(q.get('parent') || ''); if (u.protocol === 'http:' || u.protocol === 'https:') parent = u.origin } catch { parent = '' }
|
|
1276
|
+
const href = st.origin ? `${st.origin}/${v ? `__dsh_design__/v/${v}/` : ''}${key.split('/').map(encodeURIComponent).join('/')}` : ''
|
|
1277
|
+
const name = `${q.get('session') || ''}|${t.root}|${v}|${key}`
|
|
1278
|
+
// fresh=1:右侧栏要求「真的重取」(刷新按钮、目录里刚改过、失败重试)。这种情况下服务端必须重新打包,
|
|
1279
|
+
// 不能拿印记去复用——mtime 精度粗的盘(HFS+ 1s、FAT32 2s、部分网络盘)上,同一刻度里改两次、
|
|
1280
|
+
// 大小又没变,印记一模一样,光靠标记永远换不出新内容
|
|
1281
|
+
const fresh = q.get('fresh') === '1'
|
|
1282
|
+
// 打的是哪个目标:右侧栏拿它核对,请求发出后会话换了目录(或预览服务器换了端口)的回包不存进它的缓存
|
|
1283
|
+
const whose = { root: t.root, origin: st.origin || '' }
|
|
1284
|
+
// 先看大小,太大的不读(打包总量也封顶,见 bundle.js)
|
|
1285
|
+
const readRel = (rel) => {
|
|
1286
|
+
const f = bundleFile(t.root, v, rel)
|
|
1287
|
+
if (!f) return null
|
|
1288
|
+
const s = fs.statSync(f)
|
|
1289
|
+
return s.isFile() && s.size <= BUNDLE_MAX ? fs.readFileSync(f) : null
|
|
1290
|
+
}
|
|
1291
|
+
// 别人做的设计(设计库来的、没改过):加硬规矩,右侧栏的沙盒也不放行表单
|
|
1292
|
+
const strictOf = (page) => library.strictHtml(page) || (v > 0 && library.strictVersions(t.root))
|
|
1293
|
+
const had = fresh ? null : bundleCache.get(name)
|
|
1294
|
+
// 上次那份还能用:用到的文件一个都没动,地址没换(预览端口变了,里面的 <base> 就不一样了),
|
|
1295
|
+
// 内联的 picker 没换,而且现在仍然不用加硬规矩——文件没动、信任却变了(退回旧版时记下了别人做的页面)
|
|
1296
|
+
// 也不能再给那份没加规矩的。进缓存的都是不加规矩的,所以这里只要看现在是不是要加
|
|
1297
|
+
let hit = false
|
|
1298
|
+
try {
|
|
1299
|
+
hit = !!had && had.href === href && had.parent === parent && had.v === v && had.picker === pickerStamp()
|
|
1300
|
+
&& [...had.stamps].every(([rel, stamp]) => stampOf(t.root, v, rel) === stamp)
|
|
1301
|
+
if (hit) { const page = readRel(key); hit = !!page && !strictOf(page) }
|
|
1302
|
+
} catch {
|
|
1303
|
+
hit = false
|
|
1304
|
+
}
|
|
1305
|
+
if (hit) {
|
|
1306
|
+
keepBundle(name, had) // 挪到队尾,当 LRU 用
|
|
1307
|
+
return sendBundle(req, res, had)
|
|
1308
|
+
}
|
|
1309
|
+
dropBundle(name)
|
|
1310
|
+
const started = Date.now()
|
|
1311
|
+
const picker = pickerStamp()
|
|
1169
1312
|
const deps = new Set() // 这一屏用到的文件(含没找到的):右侧栏看到这些改了才重新打包
|
|
1313
|
+
const pre = new Map() // 读之前的印记:和读完后的对不上(打包这段时间里被改、被删)就不进缓存
|
|
1170
1314
|
const read = (rel) => {
|
|
1171
|
-
deps.add(rel)
|
|
1172
|
-
|
|
1173
|
-
const small = (f) => { const s = fs.statSync(f); return s.isFile() && s.size <= BUNDLE_MAX }
|
|
1174
|
-
if (v) {
|
|
1175
|
-
const f = history.fileOf(t.root, v, rel)
|
|
1176
|
-
if (f) return small(f) ? fs.readFileSync(f) : null
|
|
1177
|
-
}
|
|
1178
|
-
const hit = resolveInRoot(t.root, '/' + rel.split('/').map(encodeURIComponent).join('/'))
|
|
1179
|
-
if (!hit || !hit.real || !small(hit.real)) return null
|
|
1180
|
-
return fs.readFileSync(hit.real)
|
|
1315
|
+
if (!deps.has(rel)) { deps.add(rel); pre.set(rel, stampOf(t.root, v, rel)) }
|
|
1316
|
+
return readRel(rel)
|
|
1181
1317
|
}
|
|
1182
1318
|
try {
|
|
1183
1319
|
const page = read(key)
|
|
1184
1320
|
if (!page) return json(res, 404, { error: 'no-screen' })
|
|
1185
|
-
const
|
|
1186
|
-
// 别人做的设计(设计库来的、没改过):加硬规矩,右侧栏的沙盒也不放行表单
|
|
1187
|
-
const strict = library.strictHtml(page) || (v > 0 && library.strictVersions(t.root))
|
|
1321
|
+
const strict = strictOf(page)
|
|
1188
1322
|
const nonce = strict ? crypto.randomBytes(16).toString('base64') : ''
|
|
1189
1323
|
const lead = strict ? strictMeta(nonce) + ERR_CATCHER.replace('<script>', `<script nonce="${nonce}">`) : ERR_CATCHER
|
|
1190
1324
|
let html = bundle(strict ? defang(page.toString('utf8')) : page.toString('utf8'), { key, read, base: href, lead, tail: inlinePicker(fs.readFileSync(PICKER_FILE, 'utf8'), parent, href, nonce) })
|
|
1191
1325
|
// 打包后图片变成 data: 地址,文件名没了:把用到的 AI 示意图记在页面里,导出时照样提醒
|
|
1192
1326
|
const aiNames = [...deps].filter((x) => isPhotoAsset(x)).map((x) => path.basename(x))
|
|
1193
1327
|
if (aiNames.length) html = html.replace(lead, () => `${lead}<meta name="ddp-ai-photos" content="${aiNames.join(',').replace(/[^a-z0-9.,-]/gi, '')}">`)
|
|
1194
|
-
//
|
|
1195
|
-
json(res, 200, { html, deps:
|
|
1328
|
+
// 用到的文件太多就不列也不缓存(null = 右侧栏不按文件筛):一次 stat 两百多个不划算,宁可重新打包
|
|
1329
|
+
if (deps.size > 200) return json(res, 200, { html, deps: null, strict, ...whose })
|
|
1330
|
+
// strict 那一份里带的是这次现生成的随机 nonce:不复用,也就不进缓存
|
|
1331
|
+
if (strict) return json(res, 200, { html, deps: [...deps], strict, ...whose })
|
|
1332
|
+
const stamps = new Map([...deps].map((rel) => [rel, stampOf(t.root, v, rel)]))
|
|
1333
|
+
const body = JSON.stringify({ html, deps: [...deps], strict, ...whose })
|
|
1334
|
+
// 这几种情况不进缓存:打包这段时间里有文件变了(读之前和读完后的印记对不上:被改、被换、被删,
|
|
1335
|
+
// 可能是半新半旧),picker 换了,或者某个文件刚刚才写过(2 秒内的 mtime 说明它还在被反复改,
|
|
1336
|
+
// 粗精度盘上这时候的印记不可信)
|
|
1337
|
+
const raced = picker !== pickerStamp() || [...stamps].some(([rel, s]) => {
|
|
1338
|
+
if (pre.get(rel) !== s) return true
|
|
1339
|
+
if (s === '-') return false
|
|
1340
|
+
const ms = Number(s.slice(0, s.indexOf(':')))
|
|
1341
|
+
return ms >= started || started - ms < 2000
|
|
1342
|
+
})
|
|
1343
|
+
if (body.length > BUNDLE_CACHE_ONE || raced) return json(res, 200, { html, deps: [...deps], strict, ...whose })
|
|
1344
|
+
// ETag 按交出去的内容算:内容不一样 ETag 就一定不一样,浏览器手上的旧内容不会被 304 认成新的
|
|
1345
|
+
// (按文件印记算的话,印记看不出来的改动会让新旧两份共用一个 ETag)
|
|
1346
|
+
const etag = 'W/"' + crypto.createHash('sha256').update(body).digest('hex').slice(0, 32) + '"'
|
|
1347
|
+
const entry = { etag, root: path.resolve(t.root), href, parent, v, picker, body, stamps, bytes: body.length }
|
|
1348
|
+
keepBundle(name, entry)
|
|
1349
|
+
sendBundle(req, res, entry)
|
|
1196
1350
|
} catch (e) {
|
|
1197
1351
|
json(res, 500, { error: e.message })
|
|
1198
1352
|
}
|
|
@@ -1371,9 +1525,20 @@ export function apply(ctx) {
|
|
|
1371
1525
|
return known
|
|
1372
1526
|
}
|
|
1373
1527
|
const lib = createLibraryRoutes({ admit, readBody, json, tracker, library, inspiration, studio, open, storeLoad: fileStore.load, version: VERSION, exported, log, sessionCwd })
|
|
1528
|
+
// 画布上睡着的屏的缩略图(issue #17)
|
|
1529
|
+
const canvasThumbs = createCanvasThumbs({
|
|
1530
|
+
tracker,
|
|
1531
|
+
dirs: thumbDirs,
|
|
1532
|
+
browser: () => studio.browser,
|
|
1533
|
+
trust: () => { try { return library.trustRev() } catch (e) { log(e); return '' } },
|
|
1534
|
+
strictRoot: (root) => { try { return library.strictRoot(root) || market.isStaging(root) } catch (e) { log(e); return true } },
|
|
1535
|
+
admit,
|
|
1536
|
+
json,
|
|
1537
|
+
log,
|
|
1538
|
+
})
|
|
1374
1539
|
const mk = createMarketRoutes({ admit, readBody, json, tracker, library, market, open, systemsDir: SYSTEMS_DIR, log, sessionCwd })
|
|
1375
1540
|
|
|
1376
|
-
const routes = { works: lib.works, workthumb: lib.workthumb, pack: lib.pack, usezip: lib.usezip, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, systhumb: systhumbRoute, project: projectRoute }
|
|
1541
|
+
const routes = { works: lib.works, workthumb: lib.workthumb, scrthumb: canvasThumbs.route, pack: lib.pack, usezip: lib.usezip, ghauth: lib.ghauth, submit: lib.submit, inspire: lib.inspire, inspimg: lib.inspimg, ...mk, state, open: openRoute, view: viewRoute, close: closeRoute, history: historyRoute, restore: restoreRoute, edit: editRoute, style: styleRoute, bundle: bundleRoute, ref: refRoute, export: exportRoute, doc: docRoute, reveal: revealRoute, phone: phoneRoute, check: checkRoute, locate: locateRoute, tweak: tweakRoute, systems: systemsRoute, systhumb: systhumbRoute, project: projectRoute }
|
|
1377
1542
|
for (const [key, handler] of Object.entries(routes)) {
|
|
1378
1543
|
ctx.effect(() => ctx.webServer.register({
|
|
1379
1544
|
kind: 'exact',
|
package/lib/serve.js
CHANGED
|
@@ -232,6 +232,8 @@ export function trustedOrigins(hosts) {
|
|
|
232
232
|
/**
|
|
233
233
|
* @param opts.log 出错时写日志
|
|
234
234
|
* @param opts.onChange (key, file, files) 目录里有文件改了(file 目标;files:这一批的相对路径)
|
|
235
|
+
* @param opts.onWatch (key) 开始监听一个目录(之前没在听:那段时间改了什么不知道)
|
|
236
|
+
* @param opts.onUnwatch (key) 不再监听(关了、监听出错):之后改了什么不知道
|
|
235
237
|
* @param opts.history 设计版本(lib/history.js):/__dsh_design__/v/<版本>/<文件> 给出那一版的文件
|
|
236
238
|
* @param opts.parents () → 还有哪些 DSH 页面的源能嵌预览(DSH 网页端的地址)
|
|
237
239
|
* @param opts.strict { html(buf) → 这份 HTML 是别人做的、没改过吗, root(dir) → 这个目录是别人做的设计吗 }:是就加硬规矩
|
|
@@ -306,7 +308,7 @@ export function draftInjector(tag) {
|
|
|
306
308
|
}
|
|
307
309
|
}
|
|
308
310
|
|
|
309
|
-
export function createHub({ log = () => {}, onChange = () => {}, history = null, parents = () => [], strict = null, drafts = null, skipAnnots = () => false } = {}) {
|
|
311
|
+
export function createHub({ log = () => {}, onChange = () => {}, onWatch = () => {}, onUnwatch = () => {}, history = null, parents = () => [], strict = null, drafts = null, skipAnnots = () => false } = {}) {
|
|
310
312
|
const strictHtml = (buf) => { try { return !!(strict && strict.html && strict.html(buf)) } catch (e) { log(e); return true } }
|
|
311
313
|
const strictRoot = (dir) => { try { return !!(strict && strict.root && strict.root(dir)) } catch (e) { log(e); return true } }
|
|
312
314
|
const strictVer = (dir) => { try { return !!(strict && strict.versions && strict.versions(dir)) } catch (e) { log(e); return true } }
|
|
@@ -519,8 +521,9 @@ export function createHub({ log = () => {}, onChange = () => {}, history = null,
|
|
|
519
521
|
let timer = null
|
|
520
522
|
let last = ''
|
|
521
523
|
const batch = new Set()
|
|
524
|
+
const lost = () => { try { onUnwatch(entry.key) } catch (e) { log(e) } }
|
|
522
525
|
try {
|
|
523
|
-
|
|
526
|
+
const w = fs.watch(entry.root, { recursive: true }, (_type, name) => {
|
|
524
527
|
const rel = String(name || '')
|
|
525
528
|
if (IGNORED.test(rel) || /(^|[\\/])\./.test(rel) || EXPORTS.test(rel) || HANDOFF.test(rel) || (skipAnnots() && ANNOTATIONS.test(rel))) return
|
|
526
529
|
last = rel
|
|
@@ -534,9 +537,18 @@ export function createHub({ log = () => {}, onChange = () => {}, history = null,
|
|
|
534
537
|
try { onChange(entry.key, path.join(entry.root, last), files) } catch (e) { log(e) }
|
|
535
538
|
}, DEBOUNCE_MS)
|
|
536
539
|
})
|
|
537
|
-
entry.watcher
|
|
540
|
+
entry.watcher = w
|
|
541
|
+
// 监听出错(句柄用光、目录被删…):关掉、清空,下次 open 再补上
|
|
542
|
+
w.on('error', (e) => {
|
|
543
|
+
log(e)
|
|
544
|
+
try { w.close() } catch { /* 已经关了 */ }
|
|
545
|
+
if (entry.watcher === w) { entry.watcher = null; lost() }
|
|
546
|
+
})
|
|
547
|
+
try { onWatch(entry.key) } catch (e) { log(e) }
|
|
538
548
|
} catch (e) {
|
|
539
549
|
log(e)
|
|
550
|
+
entry.watcher = null
|
|
551
|
+
lost()
|
|
540
552
|
}
|
|
541
553
|
}
|
|
542
554
|
|
|
@@ -765,6 +777,7 @@ export function createHub({ log = () => {}, onChange = () => {}, history = null,
|
|
|
765
777
|
if (!entry) return
|
|
766
778
|
servers.delete(key)
|
|
767
779
|
try { entry.watcher && entry.watcher.close() } catch { /* 已经关了 */ }
|
|
780
|
+
if (entry.kind === 'file') try { onUnwatch(key) } catch (e) { log(e) }
|
|
768
781
|
for (const c of entry.clients) c.end()
|
|
769
782
|
for (const s of entry.sockets) s.destroy()
|
|
770
783
|
entry.server.close()
|
package/lib/works.js
CHANGED
|
@@ -57,6 +57,7 @@ export function createLibrary({ dir = LIBRARY_DIR, log = () => {} } = {}) {
|
|
|
57
57
|
let hashSet = null // 所有「别人做的、还没改过」的 HTML 的 sha256
|
|
58
58
|
let roots = null // 别人做的设计在哪些目录(真实路径)
|
|
59
59
|
let sysRoots = null // 其中装好的设计系统
|
|
60
|
+
let trustSig = '' // 上面三样的指纹(trustRev)
|
|
60
61
|
|
|
61
62
|
function load() {
|
|
62
63
|
let st = null
|
|
@@ -104,6 +105,16 @@ export function createLibrary({ dir = LIBRARY_DIR, log = () => {} } = {}) {
|
|
|
104
105
|
if (w.kind === 'system') sysRoots.add(realOf(w.dir))
|
|
105
106
|
for (const h of [...Object.values(w.hashes || {}), ...(Array.isArray(w.pastHashes) ? w.pastHashes.flat() : [])]) if (typeof h === 'string' && /^[0-9a-f]{64}$/.test(h)) hashSet.add(h)
|
|
106
107
|
}
|
|
108
|
+
trustSig = sha256([[...hashSet].sort().join(','), [...roots].sort().join(','), [...sysRoots].sort().join(',')].join('|')).slice(0, 16)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 「哪些算别人做的」现在的指纹:只随信任相关的记录变(装了、退回旧版记下的、删了),
|
|
113
|
+
* 「我的作品」里记个打开时间这种不算。远程看的右侧栏拿它判断留着的打包结果还能不能用
|
|
114
|
+
*/
|
|
115
|
+
function trustRev() {
|
|
116
|
+
index()
|
|
117
|
+
return trustSig
|
|
107
118
|
}
|
|
108
119
|
|
|
109
120
|
/** 这份 HTML 是不是别人做的、还没改过(要加硬规矩预览) */
|
|
@@ -234,5 +245,5 @@ export function createLibrary({ dir = LIBRARY_DIR, log = () => {} } = {}) {
|
|
|
234
245
|
return w
|
|
235
246
|
}
|
|
236
247
|
|
|
237
|
-
return { strictHtml, strictRoot, strictVersions, addPast, get, put, hide, move, sync, list: () => load().works.slice(), file }
|
|
248
|
+
return { strictHtml, strictRoot, strictVersions, trustRev, addPast, get, put, hide, move, sync, list: () => load().works.slice(), file }
|
|
238
249
|
}
|